hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
ZBiHistos.cxx
Go to the documentation of this file.
1#include "ZBiHistos.h"
2
3#include <sstream>
4
5#include "TMath.h"
6
8
10
11ZBiHistos::ZBiHistos(const std::string& inputName) { m_name = inputName; }
12
13void ZBiHistos::addHisto1d(std::string histoname, std::string xtitle, int nbinsX, float xmin, float xmax) {
14 histos1d[m_name + "_" + histoname] = plot1D(m_name + "_" + histoname, xtitle, nbinsX, xmin, xmax);
15}
16
17void ZBiHistos::addHisto1d(TH1* histo) {
18 if (!histo) {
19 std::cout << "Error: trying to add nullptr histogram!" << std::endl;
20 return;
21 }
22 histos1d[m_name + "_" + histo->GetName()] = histo;
23}
24
25void ZBiHistos::addGraph(TGraphErrors* graph) {
26 if (!graph) {
27 std::cout << "Error: trying to add nullptr graph!" << std::endl;
28 return;
29 }
30 graphs_[m_name + "_" + graph->GetName()] = graph;
31}
32
33void ZBiHistos::addHisto2d(std::string histoname, std::string xtitle, int nbinsX, float xmin, float xmax,
34 std::string ytitle, int nbinsY, float ymin, float ymax) {
35 histos2d[m_name + "_" + histoname] =
36 plot2D(m_name + "_" + histoname, xtitle, nbinsX, xmin, xmax, ytitle, nbinsY, ymin, ymax);
37}
38
39void ZBiHistos::addHisto2d(TH2* histo) {
40 if (!histo) {
41 std::cout << "Error: trying to add nullptr histogram!" << std::endl;
42 return;
43 }
44 histos2d[m_name + "_" + histo->GetName()] = histo;
45}
46
48 for (it1d it = histos1d.begin(); it != histos1d.end(); it++) {
49 if (it->second != nullptr) it->second->Reset();
50 }
51}
52
54 for (it2d it = histos2d.begin(); it != histos2d.end(); it++) {
55 if (it->second != nullptr) it->second->Reset();
56 }
57}
58
59double ZBiHistos::integrateHistogram1D(std::string histoname) {
60 int xmax;
61 int xmin;
62 double integral;
63
64 // Check if histogram exists to integrate
65 if (!histos1d.count(histoname)) {
66 std::cout << "[ZBiHistos}::ERROR::NO HISTOGRAM NAMED " << histoname
67 << " FOUND! DEFINE HISTOGRAM IN HISTOCONFIG!" << std::endl;
68 integral = -9999.9;
69 }
70
71 if (histos1d[histoname] == nullptr) {
72 std::cout << "ERROR: Histogram for " << histoname << " is NULLPTR" << std::endl;
73 return -9999.9;
74 }
75
76 else {
77 xmax = histos1d[histoname]->FindLastBinAbove(0.0);
78 xmin = histos1d[histoname]->FindFirstBinAbove(0.0);
79 // integral = histos1d[histoname]->Integral(0,histos1d[histoname]->GetNbinsX()+1);
80 integral = histos1d[histoname]->Integral(xmin, xmax);
81 }
82 return integral;
83}
84
85double ZBiHistos::cutFractionOfSignalVariable(std::string cutvariable, bool isCutGreaterThan, double cutFraction,
86 double initialIntegral) {
87 TH1* histo = histos1d[m_name + "_" + cutvariable + "_h"];
88 int xmax = histo->FindLastBinAbove(0.0);
89 int xmin = histo->FindFirstBinAbove(0.0);
90
91 if (debug_) {
92 std::cout << "Initial Integral: " << initialIntegral << std::endl;
93 std::cout << "Cut variable: " << cutvariable << std::endl;
94 std::cout << "Cut fraction: " << cutFraction << std::endl;
95 }
96
97 double cutvalue;
98 if (isCutGreaterThan) {
99 cutvalue = histo->GetXaxis()->GetBinLowEdge(xmin);
100 while (histo->Integral(xmin, xmax) > initialIntegral * (1.0 - cutFraction)) {
101 xmin = xmin + 1;
102 cutvalue = histo->GetXaxis()->GetBinLowEdge(xmin);
103 }
104 } else {
105 cutvalue = histo->GetXaxis()->GetBinUpEdge(xmax);
106 while (histo->Integral(xmin, xmax) > initialIntegral * (1.0 - cutFraction)) {
107 xmax = xmax - 1;
108 cutvalue = histo->GetXaxis()->GetBinUpEdge(xmax);
109 }
110 }
111
112 return cutvalue;
113}
114
116 // These histograms are used specifically for tracking the ZBitCutflowProcessor iterative process
117 addHisto2d("persistent_cuts_hh", "pct_sig_cut", 1000, -0.5, 99.5, "cut_id", 50, 0.5, 50.5);
118 addHisto2d("test_cuts_ZBi_hh", "pct_sig_cut", 1000, -0.5, 99.5, "cut_id", 50, 0.5, 50.5);
119 addHisto2d("test_cuts_values_hh", "pct_sig_cut", 1000, -0.5, 99.5, "cut_id", 50, 0.5, 50.5);
120 addHisto2d("test_cuts_zcut_hh", "pct_sig_cut", 1000, -0.5, 99.5, "zcut", 50, 0.5, 50.5);
121 addHisto2d("test_cuts_nbkg_hh", "pct_sig_cut", 1000, -0.5, 99.5, "zcut", 50, 0.5, 50.5);
122 addHisto2d("test_cuts_nsig_hh", "pct_sig_cut", 1000, -0.5, 99.5, "zcut", 50, 0.5, 50.5);
123 addHisto2d("best_test_cut_ZBi_hh", "pct_sig_cut", 1000, -0.5, 99.5, "ZBi", 2000, 0.0, 20.0);
124
125 addHisto1d("best_test_cut_ZBi_h", "pct_sig_cut", 1000, -0.5, 99.5);
126 addHisto1d("best_test_cut_zcut_h", "pct_sig_cut", 1000, -0.5, 99.5);
127 addHisto1d("best_test_cut_nsig_h", "pct_sig_cut", 1000, -0.5, 99.5);
128 addHisto1d("best_test_cut_nbkg_h", "pct_sig_cut", 1000, -0.5, 99.5);
129 addHisto1d("best_test_cut_id_h", "pct_sig_cut", 1000, -0.5, 99.5);
130}
131
132void ZBiHistos::set2DHistoYlabel(std::string histoName, int ybin, std::string ylabel) {
133 histos2d[m_name + "_" + histoName]->GetYaxis()->SetBinLabel(ybin, ylabel.c_str());
134}
135
136std::vector<double> ZBiHistos::defineImpactParameterCut(double alpha) {
137 // alpha defines the % of signal we allow to be cut. Default is 15%
138 TH2F* hh = (TH2F*)histos2d[m_name + "_z0_v_recon_z_hh"];
139 addHisto1d("impact_parameter_up_h", "recon_z [mm]", 450, -20.0, 70.0);
140 addHisto1d("impact_parameter_down_h", "recon_z [mm]", 450, -20.0, 70.0);
141 TH1* up_h = (TH1*)histos1d[m_name + "_impact_parameter_up_h"];
142 TH1* down_h = (TH1*)histos1d[m_name + "_impact_parameter_down_h"];
143
144 for (int i = 0; i < hh->GetNbinsX(); i++) {
145 TH1* projy = (TH1*)hh->ProjectionY(("projy_bin_" + std::to_string(i + 1)).c_str(), i + 1, i + 1);
146 if (projy->GetEntries() < 1) continue;
147
148 // Impact param for z0 > 0
149 int start_bin = projy->FindBin(0.0);
150 int end_bin = projy->FindLastBinAbove(0.0);
151 double refIntegral = projy->Integral(start_bin, end_bin);
152 int cutz0_bin = start_bin;
153 double testIntegral = refIntegral;
154 while (testIntegral > (1.0 - alpha) * refIntegral && cutz0_bin < end_bin - 1) {
155 cutz0_bin = cutz0_bin + 1;
156 testIntegral = projy->Integral(cutz0_bin, end_bin);
157 }
158
159 double cutz0_up = projy->GetXaxis()->GetBinLowEdge(cutz0_bin);
160 for (int j = 0; j < (int)refIntegral; j++) {
161 up_h->Fill(hh->GetXaxis()->GetBinCenter(i + 1), cutz0_up / refIntegral);
162 }
163
164 // impact param for z0 < 0
165 end_bin = projy->FindFirstBinAbove(0.0);
166 start_bin = projy->FindBin(0.0);
167 refIntegral = projy->Integral(end_bin, start_bin);
168 cutz0_bin = start_bin;
169 testIntegral = refIntegral;
170 while (testIntegral > (1.0 - alpha) * refIntegral && cutz0_bin > end_bin + 1) {
171 cutz0_bin = cutz0_bin - 1;
172 testIntegral = projy->Integral(end_bin, cutz0_bin);
173 }
174
175 double cutz0_down = projy->GetXaxis()->GetBinUpEdge(cutz0_bin);
176 for (int j = 0; j < (int)refIntegral; j++) {
177 down_h->Fill(hh->GetXaxis()->GetBinCenter(i + 1), cutz0_down / refIntegral);
178 }
179 }
180
181 TF1* fitFunc = new TF1("linear_fit", "[0]*(x-[1])", 5.0, 70.0);
182 TFitResultPtr fitResult = (TFitResultPtr)up_h->Fit("linear_fit", "QS", "", 5.0, 70.0);
183 fitFunc->Draw();
184 double m_p = fitResult->GetParams()[0];
185 double a_p = fitResult->GetParams()[1];
186
187 fitResult = (TFitResultPtr)down_h->Fit("linear_fit", "QS", "", 5.0, 70.0);
188 fitFunc->Draw();
189 double m_d = fitResult->GetParams()[0];
190 double a_d = fitResult->GetParams()[1];
191
192 // Find the location in z0 where two lines meet
193 double diff = 999.9;
194 double x = 10.0;
195 while (std::abs((m_p * (x - a_p) - (m_d * (x - a_d)))) < diff) {
196 // while( std::abs((a_p+b_p*x) - (a_d+b_d*x)) < diff ){
197 diff = std::abs((m_p * (x - a_p)) - (m_d * (x - a_d)));
198 x = x - 0.01;
199 }
200 double beta = .5 * ((m_p * (x - a_p)) + (m_p * (x - a_d)));
201 std::vector<double> params{m_p, a_p, m_d, a_d, beta, x};
202
203 delete fitFunc;
204 return params;
205}
206
207/*
208TF1* ZBiHistos::fitExponentialPlusConst(std::string histogramName, double start_nevents){
209
210 //Get z vertex distribution corresponding to Test Cut
211 std::string histoname = m_name+"_"+histogramName+"_h";
212
213 //If histogram is empty, return nullptr
214 if(histos1d[histoname]->GetEntries() < 1){
215 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " <<
216 histogramName << " distribution is empty and could not be fit!" << std::endl;
217 return nullptr;
218 }
219 //Start fit where start_nevents are left in tail
220 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
221 int firstbin = lastbin - 1;
222 double test_integral = 0.0;
223 while(test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0){
224 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
225 firstbin = firstbin - 1;
226 }
227
228 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
229 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
230 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin,xmax);
231 std::cout << "bkg xmin: " << xmin << std::endl;
232 std::cout << "bkg xmax: " << xmax << std::endl;
233
234 //Initially fit tail with exponential to seed params
235 double best_seed_chi2 = 9999999.9;
236 double seed_0;
237 double seed_1;
238 TF1* fitFunc_seed = new TF1("fitfunc_seed", "[0]*exp([1]*x)", xmin, xmax);
239 fitFunc_seed->SetParameter(0, (double)start_nevents);
240 fitFunc_seed->SetParameter(1, -0.5);
241 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QLSIM", "", xmin,xmax);
242 seed_0 = fitResult->Parameter(0);
243 seed_1 = fitResult->Parameter(1);
244 if(fitResult->Ndf() <= 0){
245 best_seed_chi2 = fitResult->Chi2()/fitResult->Ndf();
246 }
247
248
249 std::cout << "First Seed 0: " << seed_0 << std::endl;
250 std::cout << "First Seed 1: " << seed_1 << std::endl;
251 std::cout << "First Seed Chi2: " << best_seed_chi2 << std::endl;
252
253 TRandom3* ran = new TRandom3();
254 ran->SetSeed(0);
255 TF1* fitFunc_seed = new TF1("fitfunc_seed", "[0]*exp([1]*x)", xmin, xmax);
256 //fitFunc_seed->SetParameter(0, (double)start_nevents);
257 fitFunc_seed->SetParameter(0, 10.0);
258 fitFunc_seed->SetParameter(1, -0.5);
259 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin,xmax);
260 std::cout << "Start with seed 0:10 and 1:-0.5" << std::endl;
261
262 if(fitResult->Ndf() > 0){
263 best_seed_chi2 = fitResult->Chi2()/fitResult->Ndf();
264 seed_0 = fitResult->Parameter(0);
265 seed_1 = fitResult->Parameter(1);
266 }
267 std::cout << "First Seed 0: " << seed_0 << std::endl;
268 std::cout << "First Seed 1: " << seed_1 << std::endl;
269 std::cout << "First Seed Chi2: " << best_seed_chi2 << std::endl;
270 for(int i=0; i < 50; i++){
271 //fitFunc = new TF1("fitfunc_seed", "[0]*exp([1]*x)", xmin, xmax);
272 //fitFunc_seed->SetParameters(std::abs(ran->Gaus(10.0,2.0)), -std::abs(ran->Uniform(0.1,0.8)));
273 fitFunc_seed->SetParameters(std::abs(ran->Uniform(1.0,20.0)), -std::abs(ran->Uniform(0.1,0.8)));
274 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin,xmax);
275
276 if(fitResult->Ndf() <= 0)
277 continue;
278
279 std::cout << "Iter Seed 0: " << fitResult->Parameter(0) << std::endl;
280 std::cout << "Iter Seed 1: " << fitResult->Parameter(1) << std::endl;
281 std::cout << "Iter Seed Chi2: " << fitResult->Chi2()/fitResult->Ndf() << std::endl;
282
283 if(fitResult->Chi2()/fitResult->Ndf() < best_seed_chi2){
284 best_seed_chi2 = fitResult->Chi2()/fitResult->Ndf();
285 seed_0 = fitResult->Parameter(0);
286 seed_1 = fitResult->Parameter(1);
287 std::cout << "Update Iter Seed 0: " << seed_0 << std::endl;
288 std::cout << "Update Iter Seed 1: " << seed_1 << std::endl;
289 std::cout << "Update Iter Seed Chi2: " << best_seed_chi2 << std::endl;
290 }
291 }
292 std::cout << "Seed 0: " << seed_0 << std::endl;
293 std::cout << "Seed 1: " << seed_1 << std::endl;
294 std::cout << "Seed Chi2: " << best_seed_chi2 << std::endl;
295
296 double best_chi2 = best_seed_chi2;
297 double param_2;
298 TF1* fitFunc = new TF1("fitfunc", " (x<[2])*([0]*exp([1]*x)) + (x>=[2])*([0]*exp([1]*[2]))", xmin, xmax);
299 fitFunc->FixParameter(0, seed_0);
300 fitFunc->FixParameter(1, seed_1);
301 for(int i=0; i < 1000; i++){
302 fitFunc->FixParameter(2, (double)i/10.0);
303 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin,xmax);
304
305 if(fitResult->Ndf() <= 0)
306 continue;
307
308 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
309 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
310 param_2 = fitResult->Parameter(2);
311 }
312 }
313
314 std::cout << "Final Seed 0: " << seed_0 << std::endl;
315 std::cout << "Final Seed 1: " << seed_1 << std::endl;
316 std::cout << "param 2: " << param_2 << std::endl;
317 std::cout << "Best Chi2: " << best_chi2 << std::endl;
318
319 delete ran;
320
321 if(best_chi2 < best_seed_chi2){
322 fitFunc->FixParameter(0, seed_0);
323 fitFunc->FixParameter(1, seed_1);
324 fitFunc->FixParameter(2, param_2);
325 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
326 fitFunc->Draw();
327 delete fitFunc_seed;
328 return fitFunc;
329 }
330 else{
331 fitFunc->FixParameter(0, seed_0);
332 fitFunc->FixParameter(1, seed_1);
333 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QLSIM", "", xmin,xmax);
334 fitFunc_seed->Draw();
335 delete fitFunc;
336 return fitFunc_seed;
337 }
338 if(fitResult->Chi2()/fitResult->Ndf() < best_seed_chi2){
339 delete fitFunc_seed;
340 fitFunc->Draw();
341 return fitFunc;
342 }
343 else{
344 delete fitFunc;
345 fitFunc_seed->Draw();
346 return fitFunc_seed;
347 }
348 */
349
350/* Fails too often. Any failure breaks the code...not very robust
351TRandom3* ran = new TRandom3();
352ran->SetSeed(0);
353
354//Initially fit tail with exponential to seed params
355double best_chi2 = 9999999.9;
356double seed_0;
357double seed_1;
358TF1* fitFunc = new TF1("fitfunc", "[0]*exp([1]*x)", xmin, xmax);
359TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
360seed_0 = fitResult->Parameter(0);
361seed_1 = fitResult->Parameter(1);
362
363for(int i=0; i < 30; i++){
364 fitFunc = new TF1("fitfunc", "[0]*exp([1]*x)", xmin, xmax);
365 fitFunc->SetParameters(ran->Gaus(seed_0,1), ran->Gaus(seed_1,1));
366 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
367
368 if(fitResult->Ndf() <= 0)
369 continue;
370
371 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
372 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
373 seed_0 = fitResult->Parameter(0);
374 seed_1 = fitResult->Parameter(1);
375 }
376}
377
378//Exponential plus constant
379fitFunc = new TF1("fitfunc", " (x<[2])*([0]*exp([1]*x)) + (x>=[2])*([0]*exp([1]*[2]))", xmin, xmax);
380
381best_chi2 = 9999999.9;
382double best_0 = seed_0;
383double best_1 = seed_1;
384double best_2;
385
386int iteration = 30;
387fitFunc->FixParameter(0, seed_0);
388fitFunc->FixParameter(1, seed_1);
389for(int i=0; i < iteration; i++){
390 //fitFunc->SetParameter(0, best_0);
391 //fitFunc->SetParameter(1, best_1);
392 fitFunc->SetParameter(2, ran->Uniform(50.0));
393 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
394
395 if(fitResult->Ndf() <= 0)
396 continue;
397
398 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
399 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
400 //best_0 = fitResult->Parameter(0);
401 //best_1 = fitResult->Parameter(1);
402 best_2 = fitResult->Parameter(2);
403 }
404}
405
406//fitFunc->SetParameters(best_0, best_1, best_2);
407//fitFunc->SetParameters(seed_0, seed_1, best_2);
408fitFunc->FixParameter(0, seed_0);
409fitFunc->FixParameter(1, seed_1);
410fitFunc->FixParameter(2, best_2);
411fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
412fitFunc->Draw();
413
414return fitFunc;
415}*/
416
417TF1* ZBiHistos::fitExponentialPlusExp(std::string histogramName, double start_nevents) {
418 // Get z vertex distribution corresponding to Test Cut
419 std::string histoname = m_name + "_" + histogramName + "_h";
420
421 // If histogram is empty, return nullptr
422 if (histos1d[histoname]->GetEntries() < 1) {
423 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " << histogramName
424 << " distribution is empty and could not be fit!" << std::endl;
425 return nullptr;
426 }
427 double originalXmin = histos1d[histoname]->GetXaxis()->GetXmin();
428 double originalXmax = histos1d[histoname]->GetXaxis()->GetXmax();
429
430 // Start fit where start_nevents are left in tail
431 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
432 if (histos1d[histoname]->Integral() < start_nevents)
433 start_nevents = histos1d[histoname]->GetBinContent(histos1d[histoname]->GetMaximumBin());
434 int firstbin = lastbin - 1;
435 double test_integral = 0.0;
436 while (test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0) {
437 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
438 firstbin = firstbin - 1;
439 }
440
441 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
442 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin + 1);
443 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin, xmax);
444
445 // Initially fit tail with exponential to seed params
446 double best_seed_chi2 = 9999999.9;
447 double seed_0;
448 double seed_1;
449
450 TF1* fitFunc_seed = new TF1("fitFunc_seed", "[0]*exp([1]*x)", xmin, xmax);
451 // TF1* fitFunc_seed = new TF1("fitFunc_seed", "expo", xmin, xmax);
452 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin, xmax);
453 seed_0 = fitResult->Parameter(0);
454 seed_1 = fitResult->Parameter(1);
455 if (fitResult->Ndf() > 0) {
456 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
457 }
458
459 TRandom3* ran = new TRandom3();
460 ran->SetSeed(0);
461
462 best_seed_chi2 = 999999.9;
463 for (int i = 0; i < 50; i++) {
464 fitFunc_seed->SetParameters(std::abs(ran->Gaus((double)start_nevents, 3.0)),
465 -std::abs(ran->Uniform(0.01, 1.0)));
466 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin, xmax);
467
468 if (fitResult->Ndf() <= 0) continue;
469
470 if (fitResult->Chi2() / fitResult->Ndf() < best_seed_chi2) {
471 if (fitResult->Parameter(0) < 1000.0) continue;
472 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
473 seed_0 = fitResult->Parameter(0);
474 seed_1 = fitResult->Parameter(1);
475 }
476 }
477
478 double best_chi2 = 99999.9;
479 double param_2;
480 double best_seed_3;
481 double best_seed_4;
482 // TF1* fitFunc = new TF1("fitFunc", " (x<[2])*([0]*exp([1]*x)) + (x>=[2])*([3]*exp([4]*x))", xmin, xmax);
483 TF1* fitFunc = new TF1("fitFunc", " (x<[2])*([0]*exp([1]*x)) + (x>=[2])*(-[3]*x)", xmin, xmax);
484 fitFunc->FixParameter(0, seed_0);
485 fitFunc->FixParameter(1, seed_1);
486 for (int i = 0; i < 1000; i++) {
487 fitFunc->FixParameter(2, (double)i / 10.0);
488 // TFitResultPtr intermed_fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QS", "", (double)i
489 // /10.0 ,xmax); double seed_3 = intermed_fitResult->Parameter(0); double seed_4 =
490 // intermed_fitResult->Parameter(1); fitFunc->SetParameter(3,seed_3); fitFunc->SetParameter(4,seed_4);
491
492 fitFunc_seed->FixParameter(0, seed_0);
493 fitFunc_seed->FixParameter(1, seed_1);
494 double seed_3 =
495 fitFunc_seed->Eval(histos1d[histoname]->GetBinCenter(histos1d[histoname]->FindLastBinAbove(0.0)));
496 // std::cout << "LOOK FUCKER: " << seed_3 << std::endl;
497 // fitFunc->SetParameter(3,seed_3);
498
499 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
500
501 if (fitResult->Ndf() <= 0) continue;
502
503 // std::cout << "test param 2: " << (double)i/10.0 << std::endl;
504 // std::cout << "chi2: " << fitResult->Chi2()/fitResult->Ndf() << std::endl;
505 if (fitResult->Chi2() / fitResult->Ndf() < best_chi2) {
506 best_chi2 = fitResult->Chi2() / fitResult->Ndf();
507 param_2 = fitResult->Parameter(2);
508 best_seed_3 = seed_3;
509 // best_seed_4 = seed_4;
510 }
511 }
512
513 // delete ran;
514
515 fitFunc->FixParameter(0, seed_0);
516 fitFunc->FixParameter(1, seed_1);
517 fitFunc->FixParameter(2, param_2);
518 fitFunc->FixParameter(3, best_seed_3);
519 // fitFunc->FixParameter(4, best_seed_4);
520 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
521 fitFunc->Draw();
522
523 // return histogram to original range
524 histos1d[histoname]->GetXaxis()->SetRangeUser(originalXmin, originalXmax);
525
526 delete fitFunc_seed;
527 return fitFunc;
528}
529
530TF1* ZBiHistos::fitExponentialTail(std::string histogramName, double start_nevents) {
531 // Get z vertex distribution corresponding to Test Cut
532 std::string histoname = m_name + "_" + histogramName + "_h";
533
534 // If histogram is empty, return nullptr
535 if (histos1d[histoname]->GetEntries() < 1) {
536 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " << histogramName
537 << " distribution is empty and could not be fit!" << std::endl;
538 return nullptr;
539 }
540 double originalXmin = histos1d[histoname]->GetXaxis()->GetXmin();
541 double originalXmax = histos1d[histoname]->GetXaxis()->GetXmax();
542
543 // Start fit where start_nevents are left in tail
544 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
545 if (histos1d[histoname]->Integral() < start_nevents)
546 start_nevents = histos1d[histoname]->GetBinContent(histos1d[histoname]->GetMaximumBin());
547 int firstbin = lastbin - 1;
548 double test_integral = 0.0;
549 while (test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0) {
550 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
551 firstbin = firstbin - 1;
552 }
553
554 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
555 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin + 1);
556 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin, xmax);
557
558 // Initially fit tail with exponential to seed params
559 double best_seed_chi2 = 9999999.9;
560 double seed_0;
561 double seed_1;
562
563 TF1* fitFunc = new TF1("fitFunc", "[0]*exp([1]*x)", xmin, xmax);
564 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
565 seed_0 = fitResult->Parameter(0);
566 seed_1 = fitResult->Parameter(1);
567 if (fitResult->Ndf() > 0) {
568 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
569 }
570
571 TRandom3* ran = new TRandom3();
572 ran->SetSeed(0);
573
574 // best_seed_chi2 = 999999.9;
575 for (int i = 0; i < 30; i++) {
576 fitFunc->SetParameters(std::abs(ran->Gaus((double)start_nevents, 3.0)), -std::abs(ran->Uniform(0.01, 1.0)));
577 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
578
579 if (fitResult->Ndf() <= 0) continue;
580 if (fitFunc->GetProb() < 0.001) continue;
581
582 if (fitResult->Chi2() / fitResult->Ndf() < best_seed_chi2) {
583 if (fitResult->Parameter(0) < 1000.0) continue;
584 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
585 seed_0 = fitResult->Parameter(0);
586 seed_1 = fitResult->Parameter(1);
587 }
588 }
589
590 double param_0 = seed_0;
591 double param_1 = seed_1;
592 for (int i = 0; i < 30; i++) {
593 fitFunc->SetParameters(ran->Gaus(seed_0, 1.0), -std::abs(ran->Gaus(seed_1, 1.0)));
594 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
595 if (fitResult->Ndf() <= 0) continue;
596 if (fitFunc->GetProb() < 0.001) continue;
597
598 if (fitResult->Chi2() / fitResult->Ndf() < best_seed_chi2) {
599 if (fitResult->Parameter(0) < 1000.0) continue;
600 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
601 param_0 = fitResult->Parameter(0);
602 param_1 = fitResult->Parameter(1);
603 }
604 }
605
606 fitFunc->FixParameter(0, param_0);
607 fitFunc->FixParameter(1, param_1);
608 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
609 fitFunc->Draw();
610
611 // return histogram to original range
612 histos1d[histoname]->GetXaxis()->SetRangeUser(originalXmin, originalXmax);
613
614 delete ran;
615 return fitFunc;
616}
617
618TF1* ZBiHistos::fitExponentialPlusConst(std::string histogramName, double start_nevents) {
619 // Get z vertex distribution corresponding to Test Cut
620 std::string histoname = m_name + "_" + histogramName + "_h";
621
622 // If histogram is empty, return nullptr
623 if (histos1d[histoname]->GetEntries() < 1) {
624 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " << histogramName
625 << " distribution is empty and could not be fit!" << std::endl;
626 return nullptr;
627 }
628 double originalXmin = histos1d[histoname]->GetXaxis()->GetXmin();
629 double originalXmax = histos1d[histoname]->GetXaxis()->GetXmax();
630
631 // Start fit where start_nevents are left in tail
632 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
633 if (histos1d[histoname]->Integral() < start_nevents)
634 start_nevents = histos1d[histoname]->GetBinContent(histos1d[histoname]->GetMaximumBin());
635 int firstbin = lastbin - 1;
636 double test_integral = 0.0;
637 while (test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0) {
638 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
639 firstbin = firstbin - 1;
640 }
641
642 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
643 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin + 1);
644 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin, xmax);
645
646 // Initially fit tail with exponential to seed params
647 double best_seed_chi2 = 9999999.9;
648 double seed_0;
649 double seed_1;
650
651 TF1* fitFunc_seed = new TF1("fitFunc_seed", "[0]*exp([1]*x)", xmin, xmax);
652 // TF1* fitFunc_seed = new TF1("fitFunc_seed", "expo", xmin, xmax);
653 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin, xmax);
654 seed_0 = fitResult->Parameter(0);
655 seed_1 = fitResult->Parameter(1);
656 if (fitResult->Ndf() > 0 && fitFunc_seed->GetProb() > 0.001) {
657 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
658 }
659
660 TRandom3* ran = new TRandom3();
661 ran->SetSeed(0);
662
663 // best_seed_chi2 = 999999.9;
664 for (int i = 0; i < 30; i++) {
665 fitFunc_seed->SetParameters(std::abs(ran->Gaus((double)start_nevents, 3.0)),
666 -std::abs(ran->Uniform(0.01, 1.0)));
667 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc_seed, "QSIM", "", xmin, xmax);
668
669 if (fitResult->Ndf() <= 0) continue;
670 if (fitFunc_seed->GetProb() < 0.001) continue;
671
672 if (fitResult->Chi2() / fitResult->Ndf() < best_seed_chi2) {
673 if (fitResult->Parameter(0) < 1000.0) continue;
674 best_seed_chi2 = fitResult->Chi2() / fitResult->Ndf();
675 seed_0 = fitResult->Parameter(0);
676 seed_1 = fitResult->Parameter(1);
677 }
678 }
679
680 double best_chi2 = 99999.9;
681 double param_2;
682 TF1* fitFunc = new TF1("fitFunc", " (x<[2])*([0]*exp([1]*x)) + (x>=[2])*([0]*exp([1]*[2]))", xmin, xmax);
683 fitFunc->FixParameter(0, seed_0);
684 fitFunc->FixParameter(1, seed_1);
685 for (int i = 0; i < 1000; i++) {
686 fitFunc->FixParameter(2, (double)i / 10.0);
687 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
688
689 if (fitResult->Ndf() <= 0) continue;
690 if (fitFunc->GetProb() < 0.001) continue;
691
692 if (fitResult->Chi2() / fitResult->Ndf() < best_chi2) {
693 best_chi2 = fitResult->Chi2() / fitResult->Ndf();
694 param_2 = fitResult->Parameter(2);
695 }
696 }
697
698 delete ran;
699
700 fitFunc->FixParameter(0, seed_0);
701 fitFunc->FixParameter(1, seed_1);
702 fitFunc->FixParameter(2, param_2);
703 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QSIM", "", xmin, xmax);
704 fitFunc->Draw();
705
706 // return histogram to original range
707 histos1d[histoname]->GetXaxis()->SetRangeUser(originalXmin, originalXmax);
708
709 delete fitFunc_seed;
710 return fitFunc;
711}
712
713/*
714
715TF1* ZBiHistos::fitExponentialTail(std::string histogramName, double start_nevents){
716 //Background Model Fit Function
717 //TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x)",0.0,100.0);
718 TF1* fitFunc = new TF1("fitfunc","expo",0.0,100.0);
719
720 //Get z vertex distribution corresponding to Test Cut
721 std::string histoname = m_name+"_"+histogramName+"_h";
722
723 //If histogram is empty, return nullptr
724 if(histos1d[histoname]->GetEntries() < 1){
725 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " <<
726 histogramName << " distribution is empty and could not be fit!" << std::endl;
727 return nullptr;
728 }
729
730 //start mod
731 double originalXmin = histos1d[histoname]->GetXaxis()->GetXmin();
732 double originalXmax = histos1d[histoname]->GetXaxis()->GetXmax();
733
734 //Start fit where start_nevents are left in tail
735 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
736 if(histos1d[histoname]->Integral() < start_nevents)
737 start_nevents = histos1d[histoname]->GetBinContent(histos1d[histoname]->GetMaximumBin());
738 int firstbin = lastbin - 1;
739 double test_integral = 0.0;
740 int peak_bin = histos1d[histoname]->GetMaximumBin();
741 while(test_integral < start_nevents && firstbin > peak_bin){
742 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
743 firstbin = firstbin - 1;
744 }
745
746 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
747 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
748 std::cout << "fit xmin " << xmin << std::endl;
749 std::cout << "fit xmax " << xmax << std::endl;
750 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin,xmax);
751
752 fitFunc->SetParameters(50*start_nevents, -0.5);
753 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
754 double parm0 = fitResult->Parameter(0);
755 double parm1 = fitResult->Parameter(1);
756
757 TRandom3* ran = new TRandom3();
758 ran->SetSeed(0);
759 double best_chi2 = 999999999.9;
760 double best_parm0 = parm0;
761 double best_parm1 = parm1;
762
763 int iteration = 50;
764 for(int i=0; i < iteration; i++){
765 fitFunc->SetParameters(std::abs(ran->Gaus(parm0,2)),ran->Uniform(-1.0,-0.1));
766 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
767 if(fitResult->Ndf() <= 0)
768 continue;
769
770 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
771 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
772 best_parm0 = fitResult->Parameter(0);
773 best_parm1 = fitResult->Parameter(1);
774 }
775 }
776
777 std::cout << "Fit Params: [0] = " << best_parm0 <<" [1] = " << best_parm1
778 << std::endl;
779 fitFunc->SetParameter(0,best_parm0);
780 fitFunc->SetParameter(1,best_parm1);
781 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
782 fitFunc->Draw();
783 //std::cout << "Fit Params: [0] = " << best_parm0 <<" [1] = " << best_parm1
784 // << " [2] = " << best_parm2 << std::endl;
785 std::cout << "Final fit param 0: " << fitResult->Parameter(0) << std::endl;
786 std::cout << "Final fit param 1: " << fitResult->Parameter(1) << std::endl;
787 //return histogram to original range
788 fitFunc->Draw();
789 histos1d[histoname]->GetXaxis()->SetRangeUser(originalXmin,originalXmax);
790
791 return fitFunc;
792}
793*/
794/*
795TF1* ZBiHistos::fitExponentialTail(std::string histogramName, double start_nevents){
796 //Background Model Fit Function
797 TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x)",0.0,100.0);
798
799 //Get z vertex distribution corresponding to Test Cut
800 std::string histoname = m_name+"_"+histogramName+"_h";
801
802 //If histogram is empty, return nullptr
803 if(histos1d[histoname]->GetEntries() < 1){
804 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " <<
805 histogramName << " distribution is empty and could not be fit!" << std::endl;
806 return nullptr;
807 }
808
809 //Start fit where start_nevents are left in tail
810 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
811 int firstbin = lastbin - 1;
812 double test_integral = 0.0;
813 while(test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0){
814 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
815 firstbin = firstbin - 1;
816 }
817
818 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
819 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
820
821 //start mod
822 double originalXmin = histos1d[histoname]->GetXaxis()->GetXmin();
823 double originalXmax = histos1d[histoname]->GetXaxis()->GetXmax();
824
825 //Start fit where start_nevents are left in tail
826 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
827 if(histos1d[histoname]->Integral() < start_nevents)
828 start_nevents = histos1d[histoname]->GetBinContent(histos1d[histoname]->GetMaximumBin());
829 int firstbin = lastbin - 1;
830 double test_integral = 0.0;
831 while(test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0){
832 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
833 firstbin = firstbin - 1;
834 }
835
836 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
837 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
838 std::cout << "fit xmin " << xmin << std::endl;
839 std::cout << "fit xmax " << xmax << std::endl;
840 //double xmax = 100.0;
841 histos1d[histoname]->GetXaxis()->SetRangeUser(xmin,xmax);
842
843 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
844 double parm0 = fitResult->Parameter(0);
845 double parm1 = fitResult->Parameter(1);
846
847 TRandom3* ran = new TRandom3();
848 ran->SetSeed(0);
849 double best_chi2 = 999999999.9;
850 double best_parm0 = parm0;
851 double best_parm1 = parm1;
852 double best_parm2;
853
854 //fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + abs([2])",0.0,100.0);
855 fitFunc = new TF1("fitfunc","[0]*exp([1]*x)",0.0,100.0);
856 int iteration = 25;
857 for(int i=0; i < iteration; i++){
858 //fitFunc->SetParameters(ran->Gaus(parm0,2),ran->Gaus(parm1,2), ran->Uniform(10));
859 fitFunc->SetParameters(ran->Gaus(parm0,2),ran->Gaus(parm1,2));
860 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
861 if(fitResult->Ndf() <= 0)
862 continue;
863
864 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
865 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
866 best_parm0 = fitResult->Parameter(0);
867 best_parm1 = fitResult->Parameter(1);
868 //best_parm2 = fitResult->Parameter(2);
869 }
870 }
871
872 //fitFunc->SetParameters(best_parm0, best_parm1, best_parm2);
873 fitFunc->SetParameters(best_parm0, best_parm1);
874 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
875 fitFunc->Draw();
876 //std::cout << "Fit Params: [0] = " << best_parm0 <<" [1] = " << best_parm1
877 // << " [2] = " << best_parm2 << std::endl;
878 std::cout << "Fit Params: [0] = " << best_parm0 <<" [1] = " << best_parm1
879 << std::endl;
880
881 //return histogram to original range
882 histos1d[histoname]->GetXaxis()->SetRangeUser(originalXmin,originalXmax);
883
884 return fitFunc;
885}
886*/
887
888/*
889TF1* ZBiHistos::fitExponentialTail(std::string histogramName, double start_nevents){
890 //Background Model Fit Function
891 TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x)",10.0,100.0);
892 //TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + [2]*exp([3]*x)",10.0,100.0);
893 //TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + [2]",10.0,100.0);
894
895 //Get z vertex distribution corresponding to Test Cut
896 std::string histoname = m_name+"_"+histogramName+"_h";
897
898 //If histogram is empty, return nullptr
899 if(histos1d[histoname]->GetEntries() < 1){
900 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " <<
901 histogramName << " distribution is empty and could not be fit!" << std::endl;
902 return nullptr;
903 }
904 //Start fit where start_nevents are left in tail
905 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
906 int firstbin = lastbin - 1;
907 double test_integral = 0.0;
908 while(test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0){
909 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
910 firstbin = firstbin - 1;
911 }
912
913 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
914 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
915
916 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
917 double parm0 = fitResult->Parameter(0);
918 double parm1 = fitResult->Parameter(1);
919 //double parm2 = fitResult->Parameter(2);
920 //double parm3 = fitResult->Parameter(3);
921
922 double best_chi2 = 9999999.9;
923 double best_parm0, best_parm1, best_parm2, best_parm3;
924
925 int iteration = 20;
926 for(int i=0; i < iteration; i++){
927 fitFunc->SetParameters(parm0,parm1);
928 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
929 if(fitResult->Ndf() <= 0)
930 continue;
931 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
932 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
933 best_parm0 = fitResult->Parameter(0);
934 best_parm1 = fitResult->Parameter(1);
935 //best_parm2 = fitResult->Parameter(2);
936 //best_parm3 = fitResult->Parameter(3);
937 }
938 }
939
940 //fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + [2]*exp([3]*x)",10.0,100.0); //TRASH
941 fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + [2]",10.0,100.0);
942 double parm2 = best_parm0*0.5;
943 for(int i=0; i < iteration; i++){
944 fitFunc->SetParameters(parm0,parm1,parm2);
945 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
946 if(fitResult->Ndf() <= 0)
947 continue;
948 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
949 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
950 best_parm0 = fitResult->Parameter(0);
951 best_parm1 = fitResult->Parameter(1);
952 best_parm2 = fitResult->Parameter(2);
953 //best_parm3 = fitResult->Parameter(3);
954 }
955 }
956
957
958 //fitFunc->SetParameters(best_parm0,best_parm1, best_parm2, best_parm3);
959 fitFunc->SetParameters(best_parm0,best_parm1, best_parm2);
960 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
961 fitFunc->Draw();
962
963 return fitFunc;
964}
965*/
966
967/*
968TF1* ZBiHistos::fitExponentialTail(std::string histogramName, double start_nevents){
969 //Background Model Fit Function
970 TF1* fitFunc = new TF1("fitfunc","[0]*exp([1]*x)",10.0,100.0);
971
972 //Get z vertex distribution corresponding to Test Cut
973 std::string histoname = m_name+"_"+histogramName+"_h";
974
975 //If histogram is empty, return nullptr
976 if(histos1d[histoname]->GetEntries() < 1){
977 std::cout << "[ZBiHistos]::WARNING: Background Model is NULL: " <<
978 histogramName << " distribution is empty and could not be fit!" << std::endl;
979 return nullptr;
980 }
981 //Start fit where start_nevents are left in tail
982 int lastbin = histos1d[histoname]->FindLastBinAbove(0.0);
983 int firstbin = lastbin - 1;
984 double test_integral = 0.0;
985 while(test_integral < start_nevents && histos1d[histoname]->GetBinLowEdge(firstbin) > 0.0){
986 test_integral = histos1d[histoname]->Integral(firstbin, lastbin);
987 firstbin = firstbin - 1;
988 }
989
990 double xmin = histos1d[histoname]->GetBinLowEdge(firstbin);
991 double xmax = histos1d[histoname]->GetBinLowEdge(lastbin+1);
992
993 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
994 double parm0 = fitResult->Parameter(0);
995 double parm1 = fitResult->Parameter(1);
996
997 double best_chi2 = fitResult->Chi2()/fitResult->Ndf();
998 double best_parm0 = parm0;
999 double best_parm1 = parm1;
1000
1001 TRandom3* ran = new TRandom3();
1002 ran->SetSeed(0);
1003 int iteration = 25;
1004 for(int i=0; i < iteration; i++){
1005 fitFunc->SetParameters(ran->Gaus(best_parm0,2),ran->Gaus(best_parm1,2));
1006 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
1007 if(fitResult->Ndf() <= 0)
1008 continue;
1009
1010 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
1011 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
1012 best_parm0 = fitResult->Parameter(0);
1013 best_parm1 = fitResult->Parameter(1);
1014 }
1015 }
1016
1017 fitFunc = new TF1("fitfunc","[0]*exp([1]*x) + [2]",10.0,100.0);
1018 double best_parm2;
1019 best_chi2 = 999999999.9;
1020 for(int i=0; i < iteration; i++){
1021 fitFunc->SetParameter(2,ran->Uniform(10));
1022 fitFunc->FixParameter(0,best_parm0);
1023 fitFunc->FixParameter(1,best_parm1);
1024 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin,xmax);
1025 if(fitResult->Ndf() <= 0)
1026 continue;
1027
1028 if(fitResult->Chi2()/fitResult->Ndf() < best_chi2){
1029 best_chi2 = fitResult->Chi2()/fitResult->Ndf();
1030 //best_parm0 = fitResult->Parameter(0);
1031 //best_parm1 = fitResult->Parameter(1);
1032 best_parm2 = fitResult->Parameter(2);
1033 //best_parm3 = fitResult->Parameter(3);
1034 }
1035 }
1036
1037 fitFunc->FixParameter(0,best_parm0);
1038 fitFunc->FixParameter(1,best_parm1);
1039 fitFunc->FixParameter(2,best_parm2);
1040 fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
1041 fitFunc->Draw();
1042
1043 return fitFunc;
1044}*/
1045
1046TF1* ZBiHistos::getZTailFit(std::string cutname) {
1047 TF1* fitFunc = new TF1(
1048 "fitfunc",
1049 "[0]* exp( (x<=([1]+[3]))*(-0.5*((x-[1])^2)/([2]^2)) + (x>=[1]+[3])*(-0.5*([3]^2/[2]^2)-(x-[1]-[3])/[4]) )",
1050 -100.0, 100.0);
1051
1052 std::string histoname = m_name + "_tritrig_zVtx_" + cutname + "_h";
1053 if (histos1d[histoname]->GetEntries() < 1) {
1054 std::cout << "WARNING: Background Model is NULL: " << cutname
1055 << " tritrig zVtx distribution was empty and could not be fit!" << std::endl;
1056 return nullptr;
1057 }
1058
1059 TFitResultPtr gausResult = (TFitResultPtr)histos1d[histoname]->Fit("gaus", "QS");
1060 double gaus0 = gausResult->GetParams()[0];
1061 double gaus1 = gausResult->GetParams()[1];
1062 double gaus2 = gausResult->GetParams()[2];
1063 // gausResult = histos1d[histoname]->Fit("gaus","QS","",gaus1-2.5*gaus2, gaus1+10.0*gaus2);
1064 gausResult = histos1d[histoname]->Fit("gaus", "QS", "", gaus1 - 2.5 * gaus2, gaus1 + 10.0 * gaus2);
1065 gaus0 = gausResult->GetParams()[0];
1066 gaus1 = gausResult->GetParams()[1];
1067 gaus2 = gausResult->GetParams()[2];
1068 double xmin = gaus1 - 2.5 * gaus2;
1069 double xmax = histos1d[histoname]->GetBinCenter(histos1d[histoname]->FindLastBinAbove(0.0) + 1);
1070 xmax = 100.0;
1071
1072 // Fit function for a few different seeds for tail start, and length, keep the best fit
1073 double tailZ = gaus1 + 3.0 * gaus2;
1074 double tail_l = 50.0;
1075 TRandom3* ran = new TRandom3();
1076 ran->SetSeed(0);
1077
1078 double best_chi2 = 9999999.9;
1079 double best_tailZ;
1080 double best_tail_l;
1081 double best_gaus0, best_gaus1, best_gaus2;
1082
1083 // Having issues with this fit sometimes. It occassionally underestimates bkg model
1084 int iteration = 80;
1085 tail_l = 50.0;
1086 for (int i = 10; i < iteration; i = i + 2) {
1087 tailZ = gaus1 + (double)(iteration / 10.0) * gaus2;
1088 fitFunc->SetParameters(gaus0, gaus1, gaus2, tailZ, tail_l);
1089 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "QLSIM", "", xmin, xmax);
1090 if (fitResult->Ndf() <= 0) continue;
1091 if (fitResult->Chi2() / fitResult->Ndf() < best_chi2) {
1092 best_chi2 = fitResult->Chi2() / fitResult->Ndf();
1093 best_gaus0 = fitResult->Parameter(0);
1094 best_gaus1 = fitResult->Parameter(1);
1095 best_gaus2 = fitResult->Parameter(2);
1096 best_tailZ = fitResult->Parameter(3);
1097 best_tail_l = fitResult->Parameter(4);
1098 tail_l = fitResult->Parameter(4);
1099 }
1100 }
1101
1102 fitFunc->SetParameters(best_gaus0, best_gaus1, best_gaus2, best_tailZ, best_tail_l);
1103 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
1104 fitFunc->Draw();
1105 std::cout << "Finished fitting ztail" << std::endl;
1106
1107 delete ran;
1108 return fitFunc;
1109}
1110
1111double ZBiHistos::shosFitZTail(std::string cutname, double max_tail_events) {
1112 TF1* fitFunc = new TF1(
1113 "fitfunc",
1114 "[0]* exp( (x<=([1]+[3]))*(-0.5*((x-[1])^2)/([2]^2)) + (x>=[1]+[3])*(-0.5*([3]^2/[2]^2)-(x-[1]-[3])/[4]) )",
1115 -100.0, 100.0);
1116
1117 std::string histoname = m_name + "_tritrig_zVtx_" + cutname + "_h";
1118 if (histos1d[histoname]->GetEntries() < 1) return -4.3;
1119
1120 TFitResultPtr gausResult = (TFitResultPtr)histos1d[histoname]->Fit("gaus", "QS");
1121 double gaus0 = gausResult->GetParams()[0];
1122 double gaus1 = gausResult->GetParams()[1];
1123 double gaus2 = gausResult->GetParams()[2];
1124 gausResult = histos1d[histoname]->Fit("gaus", "QS", "", gaus1 - 3.0 * gaus2, gaus1 + 10.0 * gaus2);
1125 gaus0 = gausResult->GetParams()[0];
1126 gaus1 = gausResult->GetParams()[1];
1127 gaus2 = gausResult->GetParams()[2];
1128 double xmin = gaus1 - 2.5 * gaus2;
1129 double xmax = histos1d[histoname]->GetBinCenter(histos1d[histoname]->FindLastBinAbove(0.0) + 1);
1130 xmax = 100.0;
1131
1132 // Fit function for a few different seeds for tail start, and length, keep the best fit
1133 double tailZ = gaus1 + 3.0 * gaus2;
1134 double tail_l = 50.0;
1135 TRandom3* ran = new TRandom3();
1136 ran->SetSeed(0);
1137
1138 double best_chi2 = 99999.9;
1139 double best_tailZ;
1140 double best_tail_l;
1141 double best_gaus0, best_gaus1, best_gaus2;
1142
1143 int iteration = 80;
1144 for (int i = 10; i < iteration; i = i + 2) {
1145 tail_l = 50.0;
1146 tailZ = gaus1 + (double)(iteration / 10.0) * gaus2;
1147 fitFunc->SetParameters(gaus0, gaus1, gaus2, tailZ, tail_l);
1148 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
1149 if (fitResult->Ndf() <= 0) continue;
1150 if (fitResult->Chi2() < best_chi2) {
1151 best_chi2 = fitResult->Chi2() / fitResult->Ndf();
1152 best_gaus0 = fitResult->Parameter(0);
1153 best_gaus1 = fitResult->Parameter(1);
1154 best_gaus2 = fitResult->Parameter(2);
1155 best_tailZ = fitResult->Parameter(3);
1156 best_tail_l = fitResult->Parameter(4);
1157 }
1158 }
1159
1160 fitFunc->SetParameters(best_gaus0, best_gaus1, best_gaus2, best_tailZ, best_tail_l);
1161 TFitResultPtr fitResult = (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", xmin, xmax);
1162
1163 double zcut = -6.0;
1164 double testIntegral = fitFunc->Integral(zcut, 100.0);
1165 while (testIntegral > max_tail_events) {
1166 zcut = zcut + 0.1;
1167 testIntegral = fitFunc->Integral(zcut, 100.0);
1168 }
1169 fitFunc->Draw();
1170 delete ran;
1171 return zcut;
1172}
1173
1174double ZBiHistos::fitZTail(std::string zVtxHistoname, double max_tail_events) {
1175 TF1* fitFunc = new TF1(
1176 "fitfunc",
1177 "[0]*exp( (((x-[1])/[2])<[3])*(-0.5*(x-[1])^2/[2]^2) + (((x-[1])/[2])>=[3])*(0.5*[3]^2-[3]*(x-[1])/[2]))",
1178 -100.0, 100.0);
1179
1180 std::string histoname = m_name + "_" + zVtxHistoname;
1181 if (histos1d[histoname]->GetEntries() < 1) return -4.3;
1182 TFitResultPtr gausResult = (TFitResultPtr)histos1d[histoname]->Fit("gaus", "QS");
1183 double gaus1 = gausResult->GetParams()[1];
1184 double gaus2 = gausResult->GetParams()[2];
1185 gausResult = histos1d[histoname]->Fit("gaus", "QS", "", gaus1 - 3.0 * gaus2, gaus1 + 10.0 * gaus2);
1186 gaus1 = gausResult->GetParams()[1];
1187 gaus2 = gausResult->GetParams()[2];
1188 double tailZ = gaus1 + 3.0 * gaus2;
1189
1190 fitFunc->SetParameters(gausResult->GetParams()[0], gaus1, gaus2, 3.0);
1191 TFitResultPtr fitResult =
1192 (TFitResultPtr)histos1d[histoname]->Fit(fitFunc, "LSIM", "", gaus1 - 2.0 * gaus2, gaus1 + 10.0 * gaus2);
1193
1194 double zcut = -6.0;
1195 double testIntegral = fitFunc->Integral(zcut, 90.0);
1196 while (testIntegral > max_tail_events) {
1197 zcut = zcut + 0.1;
1198 testIntegral = fitFunc->Integral(zcut, 90.0);
1199 }
1200 fitFunc->Draw();
1201 return zcut;
1202}
1203
1204void ZBiHistos::writeGraphs(TFile* outF, std::string folder) {
1205 if (outF)
1206 outF->cd();
1207 else
1208 return;
1209
1210 TDirectory* dir{nullptr};
1211 if (!folder.empty()) {
1212 dir = outF->mkdir(folder.c_str(), "", true);
1213 dir->cd();
1214 }
1215 for (std::map<std::string, TGraphErrors*>::iterator it = graphs_.begin(); it != graphs_.end(); ++it) {
1216 if (!it->second) {
1217 std::cout << it->first << " Null ptr in saving.." << std::endl;
1218 continue;
1219 }
1220 it->second->Write();
1221 }
1222}
1223
1224void ZBiHistos::writeHistos(TFile* outF, std::string folder) {
1225 if (outF)
1226 outF->cd();
1227 else
1228 return;
1229
1230 TDirectory* dir{nullptr};
1231 std::cout << folder.c_str() << std::endl;
1232 if (!folder.empty()) {
1233 std::cout << "making dir " << folder.c_str() << std::endl;
1234 dir = outF->mkdir(folder.c_str(), "", true);
1235 dir->cd();
1236 }
1237 for (it1d it = histos1d.begin(); it != histos1d.end(); ++it) {
1238 if (!it->second) {
1239 std::cout << it->first << " Null ptr in saving.." << std::endl;
1240 continue;
1241 }
1242 std::cout << "writing " << it->first << std::endl;
1243 it->second->Write();
1244 std::cout << "Finished writing " << it->first << std::endl;
1245 }
1246 for (it2d it = histos2d.begin(); it != histos2d.end(); ++it) {
1247 if (!it->second) {
1248 std::cout << it->first << " Null ptr in saving.." << std::endl;
1249 continue;
1250 }
1251 it->second->Write();
1252 }
1253}
1254
1255TH1* ZBiHistos::getPDF(std::string histoname) {
1256 if (histos1d.find(m_name + "_" + histoname) == histos1d.end()) {
1257 std::cout << "[ZBiHistos]::ERROR: Histogram " << histoname << " not found!" << std::endl;
1258 return nullptr;
1259 }
1260 TH1D* hist = (TH1D*)histos1d[m_name + "_" + histoname]->Clone();
1261 hist->Scale(1.0 / hist->Integral());
1262 return hist;
1263}
1264
1265TGraphErrors* ZBiHistos::configureGraph(std::string name, std::string xlabel, std::string ylabel) {
1266 TGraphErrors* graph = new TGraphErrors();
1267 graph->SetName(name.c_str());
1268 if (xlabel.empty()) {
1269 if (name.find("vtx_proj_sig") != std::string::npos) {
1270 xlabel = "N_{#sigma_{xy}}";
1271 } else if (name.find("iso_significance") != std::string::npos) {
1272 xlabel = "R_{iso}";
1273 } else if (name.find("z0") != std::string::npos || name.find("y0") != std::string::npos) {
1274 xlabel = "iteration";
1275 } else {
1276 xlabel = "cut value";
1277 }
1278 }
1279 graph->SetTitle((";" + xlabel + ";" + ylabel).c_str());
1280 graph->SetMarkerStyle(8);
1281 graph->SetMarkerSize(0.5);
1282 graph->SetMarkerColor(kGreen + 2);
1283 return graph;
1284}
std::map< std::string, TH2 * >::iterator it2d
description
TH2F * plot2D(std::string name, std::string xtitle, int nbinsX, float xmin, float xmax, std::string ytitle, int nbinsY, float ymin, float ymax)
description
std::map< std::string, TH1 * >::iterator it1d
description
std::map< std::string, TH1 * > histos1d
description
TH1F * plot1D(const std::string &name, const std::string &xtitle, int nbinsX, float xmin, float xmax)
description
std::string m_name
description
std::map< std::string, TH2 * > histos2d
description
bool debug_
description
double shosFitZTail(std::string cutname, double max_tail_events)
description
TF1 * getZTailFit(std::string cutname)
description
TF1 * fitExponentialTail(std::string histogramName, double start_nevents)
description
void writeGraphs(TFile *outF, std::string folder)
description
TH1 * getPDF(std::string histoname)
double cutFractionOfSignalVariable(std::string cutvariable, bool isCutGreaterThan, double cutFraction, double initialIntegral)
description
Definition ZBiHistos.cxx:85
TF1 * fitExponentialPlusExp(std::string histogramName, double starnt_nevents)
description
double integrateHistogram1D(std::string histoname)
description
Definition ZBiHistos.cxx:59
void addHisto1d(std::string histoname, std::string xtitle, int nbinsX, float xmin, float xmax)
description
Definition ZBiHistos.cxx:13
TGraphErrors * configureGraph(std::string name, std::string xlabel, std::string ylabel)
double fitZTail(std::string zVtxHistoname, double max_tail_events)
description
std::map< std::string, TGraphErrors * > graphs_
hold graphs
Definition ZBiHistos.h:178
void set2DHistoYlabel(std::string histoName, int ybin, std::string ylabel)
description
void addHisto2d(std::string histoname, std::string xtitle, int nbinsX, float xmin, float xmax, std::string ytitle, int nbinsY, float ymin, float ymax)
description
Definition ZBiHistos.cxx:33
std::vector< double > defineImpactParameterCut(double alpha=0.15)
description
void resetHistograms2d()
description
Definition ZBiHistos.cxx:53
void defineZBiCutflowProcessorHistograms()
description
void resetHistograms1d()
description
Definition ZBiHistos.cxx:47
TF1 * fitExponentialPlusConst(std::string histogramName, double starnt_nevents)
description
void addGraph(TGraphErrors *graph)
Definition ZBiHistos.cxx:25
void writeHistos(TFile *outF, std::string folder)
description