fittingBetter.cxx 5.4 KB
Newer Older
Vratislav Chudoba's avatar
Vratislav Chudoba committed
1 2 3 4 5 6 7 8 9 10 11 12
#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 fittingBetter()
{

13
	const Int_t bins = 500;
Vratislav Chudoba's avatar
Vratislav Chudoba committed
14

15
	TFile *fr = new TFile("simeventsNew1.root");
Vratislav Chudoba's avatar
Vratislav Chudoba committed
16 17 18 19 20

	TTree *tr = (TTree*)fr->Get("simevents");

	TCanvas *c1 = new TCanvas("name1", "title of canvas 1");
	c1->Divide(2,2);
21 22
//	c1->Divide(2,2,0.00005,0.00005);
//	c1->Divide(2,2,0,0);
Vratislav Chudoba's avatar
Vratislav Chudoba committed
23 24 25

	c1->cd(1);
	tr->Draw("E", "E>0");
26 27 28 29 30 31 32 33 34 35
	TH1F *htemp = (TH1F*)gPad->GetPrimitive("htemp");
	htemp->GetXaxis()->CenterTitle();
	htemp->GetXaxis()->SetTitle("E [MeV]");
	htemp->GetXaxis()->SetTitleSize(0.06);
	htemp->GetXaxis()->SetTitleOffset(0.74);

	htemp->GetYaxis()->CenterTitle();
	htemp->GetYaxis()->SetTitle("Events");
	htemp->GetYaxis()->SetTitleSize(0.06);
	htemp->GetYaxis()->SetTitleOffset(0.85);
Vratislav Chudoba's avatar
Vratislav Chudoba committed
36 37

	c1->cd(2);
38
	TH1F *hE = new TH1F("hE", "Interesting region", bins, 0.7, 1.3);
Vratislav Chudoba's avatar
Vratislav Chudoba committed
39
	tr->Draw("E>>hE", "E>0.7");
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	hE->GetXaxis()->CenterTitle();
	hE->GetXaxis()->SetTitle("E [MeV]");
	hE->GetYaxis()->CenterTitle();
	hE->GetYaxis()->SetTitle("Events");


	c1->cd(3);
	TH1F *hEfitSep = new TH1F("hEfitSep", "Fit by two functions", bins, 0.7, 1.3);
	hEfitSep->GetXaxis()->CenterTitle();
	hEfitSep->GetXaxis()->SetTitle("E [MeV]");
	hEfitSep->GetYaxis()->CenterTitle();
	hEfitSep->GetYaxis()->SetTitle("Events");
	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->GetXaxis()->CenterTitle();
	hEtot->GetXaxis()->SetTitle("E [MeV]");
	hEtot->GetYaxis()->CenterTitle();
	hEtot->GetYaxis()->SetTitle("Events");
	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");
	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, 800.);
	l1->SetLineWidth(2);
	l1->SetLineColor(gHigh->GetLineColor());
	l1->Draw("");

	TLine *l2 = new TLine(maxTotal, 100., maxTotal, 800.);
	l2->SetLineWidth(2);
	l2->SetLineColor(total->GetLineColor());
	l2->Draw("");


return;


//	gStyle->ToggleEditor();

	TFile *fr = new TFile("simeventsNew.root");
Vratislav Chudoba's avatar
Vratislav Chudoba committed
148

149 150 151 152
	TTree *tr = (TTree*)fr->Get("simevents");

	TCanvas *c1 = new TCanvas("name1", "title of canvas 1");
	c1->Divide(2,2);
Vratislav Chudoba's avatar
Vratislav Chudoba committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

	c1->cd(3);
	//	fr.cd();
	TH1F *hEfitSep = new TH1F("hEfitSep", "Fit by two functions", 200, 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);
	g2->SetLineColor(kBlue);
	// The total is the sum of the three, each has 3 parameters
	TF1 *total = new TF1("mstotal","gaus(0)+gaus(3)",0.8,1.3);

	TLegend *legend = new TLegend(0.6,0.65,0.88,0.85);
	legend->SetTextFont(72);
	legend->SetTextSize(0.04);
	legend->AddEntry(hEfitSep,"Data","lpe");
	legend->AddEntry(g1,"Broad peak","l");
	legend->AddEntry(g2,"Narrow peak","l");

	legend->SetX1(0.15);
	legend->SetX2(0.35);


	legend->Draw();

	cout << legend->GetX1NDC() << endl;
	cout << legend->GetX1() << endl;
	cout << legend->GetX2() << endl;

	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", 200, 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+");
//	gStyle->SetOptFit();
	gStyle->SetOptStat("ne");
	gStyle->SetOptFit();
//	hEtot->GetObjectStat();

	TPaveStats *cstats = (TPaveStats*)gPad->GetPrimitive("stats");
	cout << cstats->GetX1NDC() << endl;
	cout << cstats->GetX1() << endl;

	cout << cstats->GetX2NDC() << endl;
	cout << cstats->GetX2() << endl;

	cout << cstats->GetY1() << endl;
	cout << cstats->GetY2() << endl;

	cstats->SetY1(3000);

	c1->Update();

220
//	gStyle->ToggleEditor();
Vratislav Chudoba's avatar
Vratislav Chudoba committed
221 222 223 224 225 226


	return;


}