#include "TFile.h" #include "TTree.h" #include "TCanvas.h" #include "TF1.h" #include "TH1F.h" #include "TStyle.h" #include "TLegend.h" #include "TPaveStats.h" void fitting() { const Int_t bins = 200; TFile *fr = new TFile("simeventsNew.root"); // TFile *fr = new TFile("simeventsNew1.root"); TTree *tr = (TTree*)fr->Get("simevents"); TCanvas *c1 = new TCanvas("name1", "title of canvas 1", 0, 0, 950, 700); c1->Divide(2,2); c1->cd(1); tr->Draw("E", "E>0"); c1->cd(2); TH1F *hE = new TH1F("hE", "Interesting region", bins, 0.7, 1.3); tr->Draw("E>>hE", "E>0.7"); c1->cd(3); TH1F *hEfitSep = new TH1F("hEfitSep", "Fit by two functions", bins, 0.7, 1.3); hE->Draw(); tr->Draw("E >> hEfitSep", "", ""); TF1 *g1 = new TF1("m1","gaus",0.9,1.05); TF1 *g2 = new TF1("m2","gaus",1.08,1.15); // The total is the sum of the three, each has 3 parameters TF1 *total = new TF1("mstotal","gaus(0)+gaus(3)",0.9,1.3); Double_t par[6]; // Fit each function and add it to the list of functions hEfitSep->Fit(g1,"R"); hEfitSep->Fit(g2,"R+"); c1->cd(4); TH1F *hEtot = new TH1F("hEtot", "Fit by multiple function", bins, 0.7, 1.3); hEtot->Draw(); tr->Draw("E >> hEtot", "", ""); // Get the parameters from the fit g1->GetParameters(&par[0]); g2->GetParameters(&par[3]); // Use the parameters on the sum total->SetParameters(par); hEtot->Fit(total,"R+"); TCanvas *c2 = new TCanvas("name2", "title of canvas 2", 0, 0, 950, 700); c2->Divide(2,2); c2->cd(1); TH1F *hEDecomposition = new TH1F(*hE); hEDecomposition->Draw(); Double_t parTotal[6]; total->GetParameters(&parTotal[0]); TF1 *gLow = new TF1("mLow","gaus",0.8,1.2); TF1 *gHigh = new TF1("mHigh","gaus",1.0,1.3); gHigh->SetLineColor(kGreen); gLow->SetParameter(0, parTotal[0]); gLow->SetParameter(1, parTotal[1]); gLow->SetParameter(2, parTotal[2]); gHigh->SetParameter(0, parTotal[3]); gHigh->SetParameter(1, parTotal[4]); gHigh->SetParameter(2, parTotal[5]); gLow->Draw("same"); gHigh->Draw("same"); c2->cd(2); hEtot->Draw(); gHigh->Draw("same"); c2->cd(3); TH1F *hInput = new TH1F(*hEDecomposition); hInput->SetName("hInput"); tr->Draw("Egamma >> hInput", ""); c2->cd(4); // TH1F *hEtotZoom = new TH1F(*hEtot); TH1F *hEtotZoom = (TH1F*)hEtot->Clone(); hEtotZoom->SetName("hEtotZoom"); // hEtotZoom-> hEtotZoom->GetXaxis()->SetRangeUser(1.04, 1.18); hEtotZoom->Draw(); // total->Draw("same"); gHigh->Draw("same"); Double_t maxSingle = gHigh->GetMaximumX(1.08, 1.14); Double_t maxTotal = total->GetMaximumX(1.08, 1.14); // cout << maxSingle << endl; TLine *l1 = new TLine(maxSingle, 100., maxSingle, 4500.); l1->SetLineWidth(2); l1->SetLineColor(gHigh->GetLineColor()); l1->Draw(""); TLine *l2 = new TLine(maxTotal, 100., maxTotal, 4500.); l2->SetLineWidth(2); l2->SetLineColor(total->GetLineColor()); l2->Draw(""); }