-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfitTools.C
1354 lines (986 loc) · 40.5 KB
/
fitTools.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
148
149
150
151
152
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "fitTools.h"
#include <cmath>
#include "TMinuit.h"
#include "RooHistError.h"
Double_t rpf(Double_t *x, Double_t *p);
Double_t NSC(Double_t *x, Double_t *p);
Double_t NSCPF(Double_t *x, Double_t *p);
Double_t powerlaw(Double_t *x, Double_t *p);
double fitTools::delta_phi(double phi1, double phi2) {
double dphi = fabs(phi1 - phi2);
return (dphi <= TMath::Pi())? dphi : TMath::TwoPi() - dphi;
}
float fitTools::delta_phi(float phi1, float phi2) {
float dphi = fabs(phi1 - phi2);
float sgn = (phi1 >= phi2 ? +1. : -1.);
return sgn * (dphi <= TMath::Pi() ? dphi : TMath::TwoPi() - dphi);
}
std::vector<float> fitTools::getPtPhot_binning() {
std::vector<float> returnVector;
////returnVector.push_back(10.);
// returnVector.push_back(15.);
// returnVector.push_back(22.);
// returnVector.push_back(32.);
// returnVector.push_back(47.);
// returnVector.push_back(70.);
// returnVector.push_back(100.);
// returnVector.push_back(150.);
// //returnVector.push_back(220.);
// returnVector.push_back(320.);
// returnVector.push_back(470.);
// returnVector.push_back(3500.);
returnVector.push_back(15.);
returnVector.push_back(22.);
returnVector.push_back(32.);
returnVector.push_back(53.);
returnVector.push_back(80.);
returnVector.push_back(100.);
returnVector.push_back(150.);
returnVector.push_back(220.);
returnVector.push_back(320.);
returnVector.push_back(470.);
returnVector.push_back(700.);
returnVector.push_back(3500.);
//returnVector.push_back(15.);
//returnVector.push_back(18.);
//returnVector.push_back(22.);
//returnVector.push_back(27.);
//returnVector.push_back(32.);
//returnVector.push_back(39.);
//returnVector.push_back(47.);
//returnVector.push_back(57.);
//returnVector.push_back(69.);
//returnVector.push_back(84.);
//returnVector.push_back(100.);
//returnVector.push_back(3500.);
return returnVector;
}
void fitTools::getBins( int nBins_total, Double_t* Lower, Double_t xmin, Double_t xmax, bool plotLog) {
int nBins = nBins_total-1;
const double dx = (plotLog) ? pow((xmax / xmin), (1. / (double)nBins)) : ((xmax - xmin) / (double)nBins);
Lower[0] = xmin;
for (int i = 1; i != nBins; ++i) {
if (plotLog) Lower[i] = Lower[i-1] * dx;
else Lower[i] = Lower[i-1] + dx;
}
Lower[nBins] = xmax;
}
void fitTools::getBins_int( int nBins_total, Double_t* Lower, Double_t xmin, Double_t xmax, bool plotLog) {
Double_t Lower_exact;
int nBins = nBins_total-1;
const double dx = (plotLog) ? pow((xmax / xmin), (1. / (double)nBins)) : ((xmax - xmin) / (double)nBins);
Lower[0] = xmin;
Lower_exact = Lower[0];
for (int i = 1; i != nBins; ++i) {
if (plotLog) {
Lower_exact *= dx;
Lower[i] = TMath::Ceil(Lower_exact);
} else {
Lower[i] = TMath::Ceil(Lower[i-1] + dx);
}
}
Lower[nBins] = xmax;
}
void fitTools::getPtBins(int nBins, Double_t* Lower, bool plotLog) {
//hardwired for now:
//Int_t ii=0;
//Lower[ii++] = 20.;
////Lower[ii++] = 26.;
//Lower[ii++] = 34.;
//Lower[ii++] = 44.;
//Lower[ii++] = 57.;
//Lower[ii++] = 74.;
//Lower[ii++] = 96.;
//Lower[ii++] = 124.;
//Lower[ii++] = 181.;
//Lower[ii++] = 230.;
//Lower[ii++] = 312.;
//Lower[ii++] = 457.;
//Lower[ii++] = 700.;
//Lower[ii++] = 902.;
//Lower[ii++] = 1176.;
//Lower[ii++] = 1533.;
//Lower[ii++] = 2000.;
Int_t ii=0;
Lower[ii++] = 20.;
Lower[ii++] = 42.;
Lower[ii++] = 62.;
Lower[ii++] = 87.;
Lower[ii++] = 108.;
Lower[ii++] = 140.;
Lower[ii++] = 183.;
Lower[ii++] = 239.;
Lower[ii++] = 312.;
Lower[ii++] = 407.;
Lower[ii++] = 530.;
Lower[ii++] = 691.;
Lower[ii++] = 902.;
Lower[ii++] = 1176.;
Lower[ii++] = 1533.;
Lower[ii++] = 2000.;
}
int fitTools::getNbins_stack( const std::string& varName ) {
int nBins = 0;
if( varName=="eta" ) {
nBins = 51;
} else if( varName=="pt" || varName=="ptCorr" ) {
nBins = 51;
} else if( varName=="phi" ) {
nBins = 51;
} else {
std::cout << "Binning not yet implememented for variable '" << varName << "'. Exiting." << std::endl;
exit(38112);
}
return nBins;
}
void fitTools::getBins_stack(int nBins, Double_t* Lower, const std::string& varName ) {
if( varName=="eta" ) {
getBins( nBins, Lower, -5., 5., (bool)false );
} else if( varName=="pt" || varName=="ptCorr" ) {
getBins( nBins, Lower, 5., 100., (bool)true);
} else if( varName=="phi" ) {
getBins( nBins, Lower, -3.142, 3.142, (bool)false);
} else {
std::cout << "Binning not yet implememented for variable '" << varName << "'. Exiting." << std::endl;
exit(98112);
}
}
void fitTools::drawSingleGraph(TGraph* gr, const std::string& canvasName) {
TH2F *h = new TH2F("h_ausialiario","", 10, 20., 1000., 10, 0.2, 1. );
h->SetXTitle("p_{T}^{GEN} [GeV/c]" );
h->SetYTitle( "p_{T}^{RECO}/p_{T}^{GEN}" );
h->SetStats(0);
TCanvas* c1 = new TCanvas("c1", "c1", 800, 600);
c1->cd();
h->Draw();
gr->Draw("*same");
c1->SaveAs(canvasName.c_str());
delete c1;
delete h;
}
void fitTools::fitProjection(TH1D* h1_projection, TF1* gaussian, Float_t nSigma, std::string option, bool add) {
Float_t histMean = h1_projection->GetMean();
Float_t histRMS = h1_projection->GetRMS();
gaussian->SetParameter(0, h1_projection->GetMaximum());
gaussian->SetParameter(1, histMean);
gaussian->SetParameter(2, histRMS);
if( histRMS==0. ) return;
gaussian->SetParLimits(1, 0., 2.*histMean);
Float_t lowerBound = histMean - nSigma*histRMS;
Float_t upperBound = histMean + nSigma*histRMS;
gaussian->SetRange( lowerBound, upperBound );
h1_projection->Fit(gaussian, option.c_str());
int n_iter = 3;
for(int i=0; i<n_iter; ++i) {
Float_t lowerBound = gaussian->GetParameter(1) - nSigma*gaussian->GetParameter(2);
Float_t upperBound = gaussian->GetParameter(1) + nSigma*gaussian->GetParameter(2);
gaussian->SetRange( lowerBound, upperBound );
if( add && (i==(n_iter-1)) )
option = option+"+";
h1_projection->Fit(gaussian, option.c_str());
}
}
void fitTools::fitProjection_sameArea(TH1D* h1_projection, TF1* gaussian, TH1D* newhisto, Float_t percIntegral, const std::string& option, bool useMode) {
if( percIntegral<0. || percIntegral>1. ) {
std::cout << "WARNING! percIntegral is " << percIntegral << "!! Setting it to 90%." << std::endl;
percIntegral = 0.9;
}
Int_t nBins = h1_projection->GetNbinsX();
Double_t xMin = h1_projection->GetXaxis()->GetXmin();
Double_t xMax = h1_projection->GetXaxis()->GetXmax();
Double_t binWidth = (xMax-xMin)/(Double_t)nBins; //WARNING: this works only if bins are of the same size
Double_t integral = h1_projection->Integral();
//first: find maximum
Int_t maxBin;
if( useMode ) {
maxBin = h1_projection->GetMaximumBin();
} else {
TF1* tmp_gaussian = new TF1("tmp_gaussian", "gaus");
fitProjection(h1_projection, tmp_gaussian, 2.5, "RQN");
maxBin = (Int_t)ceil((tmp_gaussian->GetParameter(1)-xMin)/binWidth);
delete tmp_gaussian;
}
// std::cout << "maxBin: " << maxBin << "\tbin center: " << h1_projection->GetXaxis()->GetBinCenter(maxBin) << "\t gauss mu: " << gaussian->GetParameter(1) << std::endl;
TH1D* newHisto_tmp = new TH1D("newHisto_tmp", "", nBins, xMin, xMax);
newHisto_tmp->SetBinContent( maxBin, h1_projection->GetBinContent(maxBin) );
newHisto_tmp->SetBinError( maxBin, h1_projection->GetBinError(maxBin) );
Int_t iBin = maxBin;
Int_t delta_iBin = 1;
Int_t sign = 1;
Float_t xMin_fit = newHisto_tmp->GetXaxis()->GetBinLowEdge(maxBin);
Float_t xMax_fit = newHisto_tmp->GetXaxis()->GetBinUpEdge(maxBin);
//add bins till percent area is reached:
while( newHisto_tmp->Integral() < percIntegral*integral ) {
iBin += sign*delta_iBin;
newHisto_tmp->SetBinContent( iBin, h1_projection->GetBinContent(iBin) );
newHisto_tmp->SetBinError( iBin, h1_projection->GetBinError(iBin) );
if( newHisto_tmp->GetXaxis()->GetBinLowEdge(iBin) < xMin_fit ) xMin_fit = newHisto_tmp->GetXaxis()->GetBinLowEdge(iBin);
if( newHisto_tmp->GetXaxis()->GetBinUpEdge(iBin) > xMax_fit ) xMax_fit = newHisto_tmp->GetXaxis()->GetBinLowEdge(iBin);
delta_iBin += 1;
sign *= -1; //makes it jump from left to right about max
}
// std::cout << "done with rms." << std::endl;
// TCanvas* c1 = new TCanvas("c1", "c1", 800, 600);
// c1->cd();
// h1_projection->Draw();
// newHisto->SetFillColor(kRed);
// newHisto->DrawClone("HISTO same");
//initialize parameters to likely values:
gaussian->SetParameter( 0, newHisto_tmp->Integral() );
gaussian->SetParameter( 1, newHisto_tmp->GetMean() );
gaussian->SetParameter( 2, newHisto_tmp->GetRMS() );
gaussian->SetRange(xMin_fit, xMax_fit);
newHisto_tmp->Fit(gaussian, option.c_str());
*newhisto = *newHisto_tmp;
delete newHisto_tmp;
newHisto_tmp=0;
// return c1;
}
void fitTools::getTruncatedMeanAndRMS(TH1D* h1_projection, Float_t& mean, Float_t& mean_err, Float_t& rms, Float_t& rms_err, Double_t percentIntegral_MEAN, Double_t percentIntegral_RMS) {
//TCanvas* getTruncatedMeanAndRMS(TH1D* h1_projection, Float_t& mean, Float_t& mean_err, Float_t& rms, Float_t& rms_err, Double_t percentIntegral_MEAN=0.9, Double_t percentIntegral_RMS=0.68) {
bool useMode = false;
if( percentIntegral_MEAN<0. || percentIntegral_MEAN>1. ) {
std::cout << "WARNING! percentIntegral_MEAN is " << percentIntegral_MEAN << "!! Setting it to 90%." << std::endl;
percentIntegral_MEAN = 0.9;
}
if( percentIntegral_RMS<0. || percentIntegral_RMS>1. ) {
std::cout << "WARNING! percentIntegral_RMS is " << percentIntegral_RMS << "!! Setting it to 68%." << std::endl;
percentIntegral_RMS = 0.68;
}
Int_t nBins = h1_projection->GetNbinsX();
Double_t xMin = h1_projection->GetXaxis()->GetXmin();
Double_t xMax = h1_projection->GetXaxis()->GetXmax();
Double_t binWidth = (xMax-xMin)/(Double_t)nBins; //WARNING: this works only if bins are of the same size
Double_t integral = h1_projection->Integral();
// std::cout << "xmax: " << xMax << "\txMin: " << xMin << std::endl;
//first: find maximum
// std::cout << "N: " << gaussian->GetParameter(0) << "\tmu: " << gaussian->GetParameter(1) << "\tsigma: " << gaussian->GetParameter(2) << std::endl;
Int_t maxBin;
if( useMode ) {
maxBin = h1_projection->GetMaximumBin();
} else {
TF1* gaussian = new TF1("gaussian", "gaus");
gaussian->SetLineColor(kGreen);
fitProjection(h1_projection, gaussian, 1.5, "RQN");
maxBin = (Int_t)ceil((gaussian->GetParameter(1)-xMin)/binWidth);
delete gaussian;
}
// std::cout << "maxBin: " << maxBin << "\tbin center: " << h1_projection->GetXaxis()->GetBinCenter(maxBin) << "\t gauss mu: " << gaussian->GetParameter(1) << std::endl;
TH1D* newHisto = new TH1D("newHisto", "", nBins, xMin, xMax);
newHisto->SetBinContent( maxBin, h1_projection->GetBinContent(maxBin) );
newHisto->SetBinError( maxBin, h1_projection->GetBinError(maxBin) );
Int_t iBin = maxBin;
Int_t delta_iBin = 1;
Int_t sign = 1;
// std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;
while( newHisto->Integral() < percentIntegral_RMS*integral ) {
iBin += sign*delta_iBin;
// std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;
newHisto->SetBinContent( iBin, h1_projection->GetBinContent(iBin) );
newHisto->SetBinError( iBin, h1_projection->GetBinError(iBin) );
delta_iBin += 1;
sign *= -1;
}
// std::cout << "done with rms." << std::endl;
// TCanvas* c1 = new TCanvas("c1", "c1", 800, 600);
// c1->cd();
// h1_projection->Draw();
// newHisto->SetFillColor(kRed);
// newHisto->DrawClone("HISTO same");
rms = newHisto->GetRMS();
rms_err = newHisto->GetRMSError();
//std::cout << "rms: " << rms << std::endl;
while( newHisto->Integral() < percentIntegral_MEAN*integral ) {
// std::cout << "iBin: " << iBin << "\tint: " << newHisto->Integral()/integral << std::endl;
iBin += sign*delta_iBin;
newHisto->SetBinContent( iBin, h1_projection->GetBinContent(iBin) );
newHisto->SetBinError( iBin, h1_projection->GetBinError(iBin) );
delta_iBin += 1;
sign *= -1;
}
// newHisto->SetFillStyle(3004);
// newHisto->SetFillColor(kBlue);
// newHisto->DrawClone("HISTO same");
mean = newHisto->GetMean();
mean_err = newHisto->GetMeanError();
delete newHisto;
// return c1;
}
void fitTools::fillProfile(TH1F* h1_response_FIT, TH1F* h1_resolution_FIT, TH1F* h1_response_MEAN, TH1F* h1_resolution_RMS, TH2D* h2, std::string name) {
std::string fileName = "Projections/"+name+".root";
TFile* projectionFile;
if( name!= "" ) {
projectionFile = TFile::Open(fileName.c_str(), "RECREATE");
projectionFile->cd();
}
for(int iBin=1; iBin<(h2->GetNbinsX()+1); ++iBin) {
char histName[50];
sprintf(histName, "projection_%d",iBin);
TH1D* h1_projection = h2->ProjectionY(histName, iBin, iBin);
TF1* gaussian_LL = new TF1("gaussian_LL", "gaus");
fitProjection(h1_projection, gaussian_LL, 2., "RQLL");
TF1* gaussian_chi = new TF1("gaussian_chi", "gaus");
fitProjection(h1_projection, gaussian_chi, 2., "RQO+");
if( name!="" ) {
h1_projection->Write();
}
Float_t mu = gaussian_LL->GetParameter(1);
Float_t mu_err = gaussian_chi->GetParError(1);
h1_response_FIT->SetBinContent(iBin, mu);
h1_response_FIT->SetBinError(iBin, mu_err);
Float_t sigma = gaussian_LL->GetParameter(2);
Float_t resolution = (mu!=0.) ? sigma/mu : -1.;
h1_resolution_FIT->SetBinContent(iBin, resolution);
Float_t sigma_err = gaussian_chi->GetParError(2);
Float_t res_err = (mu!=0.) ? sqrt( sigma_err*sigma_err/(mu*mu) + mu_err*mu_err*sigma*sigma/(mu*mu*mu*mu) ) : 0.;
h1_resolution_FIT->SetBinError(iBin, res_err);
Float_t n = h1_projection->GetEntries();
Float_t mean = h1_projection->GetMean();
Float_t mean_err = (n!=0) ? h1_projection->GetRMS()/sqrt(n) : 0.;
h1_response_MEAN->SetBinContent(iBin, mean);
h1_response_MEAN->SetBinError(iBin, mean_err);
Float_t rms = h1_projection->GetRMS();
Float_t rms_err = (n!=0) ? h1_projection->GetRMS()/sqrt(n) : 0.;
resolution = (mean!=0.) ? rms/mean : -1.;
res_err = (mean!=0.) ? sqrt( rms_err*rms_err/(mean*mean) + mean_err*mean_err*rms*rms/(mean*mean*mean*mean) ) : 0.;
if( resolution != 0. ) {
h1_resolution_RMS->SetBinContent(iBin, resolution);
h1_resolution_RMS->SetBinError(iBin, res_err);
}
h1_projection = 0;
} //for bins
if(name!="") {
projectionFile->Write();
projectionFile->Close();
delete projectionFile;
}
projectionFile = 0;
} //fill profile
//new fit reponse:
void fitTools::fitDistribution_TGraph(TH2D* h2, TH2D* genMean, const std::string& varY, const std::string& varX, const std::string& etaRegion, const std::string& flag, const std::string& algoType, const std::string& outFileName, const std::string& name, Float_t percIntegral, bool use_samearea) {
Int_t nBins = h2->GetNbinsX();
Float_t response_FIT_x[nBins];
Float_t response_FIT_y[nBins];
Float_t response_FIT_xerr[nBins];
Float_t response_FIT_yerr[nBins];
Float_t response_MEAN_x[nBins];
Float_t response_MEAN_y[nBins];
Float_t response_MEAN_xerr[nBins];
Float_t response_MEAN_yerr[nBins];
Float_t resolution_FIT_x[nBins];
Float_t resolution_FIT_y[nBins];
Float_t resolution_FIT_xerr[nBins];
Float_t resolution_FIT_yerr[nBins];
Float_t resolution_RMS_x[nBins];
Float_t resolution_RMS_y[nBins];
Float_t resolution_RMS_xerr[nBins];
Float_t resolution_RMS_yerr[nBins];
std::string fileName = "Projections/"+name+".root";
TFile* projectionFile;
if( name!= "" ) {
projectionFile = TFile::Open(fileName.c_str(), "RECREATE");
projectionFile->cd();
}
for(int iBin=1; iBin<nBins+1; ++iBin) {
char projName[100];
sprintf( projName, "%sGenMean_%dbin", varX.c_str(), iBin);
TH1* h1_proj = genMean->ProjectionY(projName, iBin, iBin);
Float_t proj_mean = h1_proj->GetMean();
Float_t proj_rms = h1_proj->GetRMS();
Float_t proj_entries = h1_proj->GetEntries();
response_FIT_x[iBin-1] = proj_mean;
response_MEAN_x[iBin-1] = proj_mean;
resolution_FIT_x[iBin-1] = proj_mean;
resolution_RMS_x[iBin-1] = proj_mean;
response_FIT_xerr[iBin-1] = proj_rms/sqrt(proj_entries);
response_MEAN_xerr[iBin-1] = proj_rms/sqrt(proj_entries);
resolution_FIT_xerr[iBin-1] = proj_rms/sqrt(proj_entries);
resolution_RMS_xerr[iBin-1] = proj_rms/sqrt(proj_entries);
//response_FIT_xerr[iBin-1] = ( proj_entries>1. ) ? proj_rms/sqrt(proj_entries) : proj_rms;
//response_MEAN_xerr[iBin-1] = ( proj_entries>1. ) ? proj_rms/sqrt(proj_entries) : proj_rms;
//resolution_FIT_xerr[iBin-1] = ( proj_entries>1. ) ? proj_rms/sqrt(proj_entries) : proj_rms;
//resolution_RMS_xerr[iBin-1] = ( proj_entries>1. ) ? proj_rms/sqrt(proj_entries) : proj_rms;
char histName[50];
sprintf(histName, "projection_%d",iBin);
TH1D* h1_projection = h2->ProjectionY(histName, iBin, iBin);
TH1D* h1_samearea = new TH1D();
TF1* gaussian = new TF1("gaussian", "gaus");
fitProjection_sameArea(h1_projection, gaussian, h1_samearea, 0.95);
//TF1* gaussian_LL = new TF1("gaussian_LL", "gaus");
//fitProjection(h1_projection, gaussian_LL, nSigma, "RQLL");
//TF1* gaussian_chi = new TF1("gaussian_chi", "gaus");
//fitProjection(h1_projection, gaussian_chi, nSigma, "RQO+");
if( name!="" ) {
h1_proj->Write();
h1_projection->Write();
}
Float_t mu = gaussian->GetParameter(1);
Float_t mu_err = gaussian->GetParError(1);
//Float_t mu = gaussian_chi->GetParameter(1);
//Float_t mu_err = gaussian_chi->GetParError(1);
response_FIT_y[iBin-1] = mu;
response_FIT_yerr[iBin-1] = mu_err;
Float_t sigma = gaussian->GetParameter(2);
//Float_t sigma = gaussian_chi->GetParameter(2);
Float_t resolution = (mu!=0.) ? sigma/mu : -1.;
resolution_FIT_y[iBin-1] = (varY=="response") ? resolution : sigma;
Float_t sigma_err = gaussian->GetParError(2);
//Float_t sigma_err = gaussian_chi->GetParError(2);
Float_t res_err = (mu!=0.) ? sqrt( sigma_err*sigma_err/(mu*mu) + mu_err*mu_err*sigma*sigma/(mu*mu*mu*mu) ) : 0.;
resolution_FIT_yerr[iBin-1] = (varY=="response") ? res_err : sigma_err;
Float_t mean, rms, mean_err, rms_err;
if( use_samearea ) {
mean = h1_projection->GetMean();
mean_err = h1_projection->GetMeanError();
rms = h1_projection->GetRMS();
rms_err = h1_projection->GetRMSError();
} else { //truncated mean/rms:
mean = h1_samearea->GetMean();
mean_err = h1_samearea->GetMeanError();
rms = h1_samearea->GetRMS();
rms_err = h1_samearea->GetRMSError();
}
//getTruncatedMeanAndRMS(h1_projection, mean, mean_err, rms, rms_err, 1., 0.90);
response_MEAN_y[iBin-1] = mean;
response_MEAN_yerr[iBin-1] = mean_err;
resolution = (mean!=0.) ? rms/mean : -1.;
res_err = (mean!=0.) ? sqrt( rms_err*rms_err/(mean*mean) + mean_err*mean_err*rms*rms/(mean*mean*mean*mean) ) : 0.;
if( rms != 0. ) {
resolution_RMS_y[iBin-1] = (varY=="response") ? resolution : rms; // resolutions on other variables dont have to be normalized (e.g. deltaPhi)
resolution_RMS_yerr[iBin-1] = (varY=="response") ? res_err : rms_err;
}
h1_projection = 0;
delete gaussian;
//delete gaussian_LL;
//delete gaussian_chi;
} //for bins
TGraphErrors* gr_response_FIT = new TGraphErrors(nBins, response_FIT_x, response_FIT_y, response_FIT_xerr, response_FIT_yerr);
TGraphErrors* gr_response_MEAN = new TGraphErrors(nBins, response_MEAN_x, response_MEAN_y, response_MEAN_xerr, response_MEAN_yerr);
TGraphErrors* gr_resolution_FIT = new TGraphErrors(nBins, resolution_FIT_x, resolution_FIT_y, resolution_FIT_xerr, resolution_FIT_yerr);
TGraphErrors* gr_resolution_RMS = new TGraphErrors(nBins, resolution_RMS_x, resolution_RMS_y, resolution_RMS_xerr, resolution_RMS_yerr);
std::string gr_name;
gr_name = "gr_" + varY + "_vs_" + varX + "_FIT_" + etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_response_FIT->SetName(gr_name.c_str());
gr_name = "gr_" + varY + "_vs_" + varX + "_MEAN_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_response_MEAN->SetName(gr_name.c_str());
gr_name = "gr_" + varY + "Res_vs_" + varX + "_FIT_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_resolution_FIT->SetName(gr_name.c_str());
gr_name = "gr_" + varY + "Res_vs_" + varX + "_RMS_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_resolution_RMS->SetName(gr_name.c_str());
TFile* outFile = TFile::Open(outFileName.c_str(), "update");
outFile->cd();
gr_response_FIT->Write();
gr_response_MEAN->Write();
gr_resolution_FIT->Write();
gr_resolution_RMS->Write();
outFile->Write();
outFile->Close();
if(name!="") {
projectionFile->Write();
projectionFile->Close();
delete projectionFile;
}
projectionFile = 0;
} //fill profile
//old fit response:
//now deprecated
/*
void fitDistribution_TGraph(TH2D* h2, TProfile* genMean, const std::string& varX, const std::string& etaRegion, const std::string& flag, const std::string& algoType, const std::string& outFileName, const std::string& name="") {
Int_t nBins = h2->GetNbinsX();
Float_t response_FIT_x[nBins];
Float_t response_FIT_y[nBins];
Float_t response_FIT_xerr[nBins];
Float_t response_FIT_yerr[nBins];
Float_t response_MEAN_x[nBins];
Float_t response_MEAN_y[nBins];
Float_t response_MEAN_xerr[nBins];
Float_t response_MEAN_yerr[nBins];
Float_t resolution_FIT_x[nBins];
Float_t resolution_FIT_y[nBins];
Float_t resolution_FIT_xerr[nBins];
Float_t resolution_FIT_yerr[nBins];
Float_t resolution_RMS_x[nBins];
Float_t resolution_RMS_y[nBins];
Float_t resolution_RMS_xerr[nBins];
Float_t resolution_RMS_yerr[nBins];
std::string fileName = "Projections/"+name+".root";
TFile* projectionFile;
if( name!= "" ) {
projectionFile = TFile::Open(fileName.c_str(), "RECREATE");
projectionFile->cd();
}
for(int iBin=1; iBin<nBins+1; ++iBin) {
response_FIT_x[iBin-1] = genMean->GetBinContent(iBin);
response_MEAN_x[iBin-1] = genMean->GetBinContent(iBin);
resolution_FIT_x[iBin-1] = genMean->GetBinContent(iBin);
resolution_RMS_x[iBin-1] = genMean->GetBinContent(iBin);
//response_FIT_xerr[iBin-1] = genMean->GetBinError(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//response_MEAN_xerr[iBin-1] = genMean->GetBinError(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//resolution_FIT_xerr[iBin-1] = genMean->GetBinError(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//resolution_RMS_xerr[iBin-1] = genMean->GetBinError(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
response_FIT_xerr[iBin-1] = genMean->GetBinError(iBin);
response_MEAN_xerr[iBin-1] = genMean->GetBinError(iBin);
resolution_FIT_xerr[iBin-1] = genMean->GetBinError(iBin);
resolution_RMS_xerr[iBin-1] = genMean->GetBinError(iBin);
//response_FIT_xerr[iBin-1] = genMean->GetBinContent(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//response_MEAN_xerr[iBin-1] = genMean->GetBinContent(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//resolution_FIT_xerr[iBin-1] = genMean->GetBinContent(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
//resolution_RMS_xerr[iBin-1] = genMean->GetBinContent(iBin)/sqrt((Float_t)genMean->GetBinEntries(iBin));
char histName[50];
sprintf(histName, "projection_%d",iBin);
TH1D* h1_projection = h2->ProjectionY(histName, iBin, iBin);
TF1* gaussian_LL = new TF1("gaussian_LL", "gaus");
fitProjection(h1_projection, gaussian_LL, 2., "RQLL");
TF1* gaussian_chi = new TF1("gaussian_chi", "gaus");
fitProjection(h1_projection, gaussian_chi, 2., "RQN");
if( name!="" ) {
h1_projection->Write();
}
Float_t mu = gaussian_LL->GetParameter(1);
Float_t mu_err = gaussian_chi->GetParError(1);
response_FIT_y[iBin-1] = mu;
response_FIT_yerr[iBin-1] = mu_err;
Float_t sigma = gaussian_LL->GetParameter(2);
Float_t resolution = (mu!=0.) ? sigma/mu : -1.;
resolution_FIT_y[iBin-1] = resolution;
Float_t sigma_err = gaussian_chi->GetParError(2);
Float_t res_err = (mu!=0.) ? sqrt( sigma_err*sigma_err/(mu*mu) + mu_err*mu_err*sigma*sigma/(mu*mu*mu*mu) ) : 0.;
resolution_FIT_yerr[iBin-1] = res_err;
Float_t n = h1_projection->GetEntries();
Float_t mean = h1_projection->GetMean();
Float_t mean_err = (n!=0) ? h1_projection->GetRMS()/sqrt(n) : 0.;
response_MEAN_y[iBin-1] = mean;
response_MEAN_yerr[iBin-1] = mean_err;
Float_t rms = h1_projection->GetRMS();
Float_t rms_err = (n!=0) ? h1_projection->GetRMS()/sqrt(n) : 0.;
resolution = (mean!=0.) ? rms/mean : -1.;
res_err = (mean!=0.) ? sqrt( rms_err*rms_err/(mean*mean) + mean_err*mean_err*rms*rms/(mean*mean*mean*mean) ) : 0.;
if( resolution != 0. ) {
resolution_RMS_y[iBin-1] = resolution;
resolution_RMS_yerr[iBin-1] = res_err;
}
h1_projection = 0;
delete gaussian_LL;
delete gaussian_chi;
} //for bins
TGraphErrors* gr_response_FIT = new TGraphErrors(nBins, response_FIT_x, response_FIT_y, response_FIT_xerr, response_FIT_yerr);
TGraphErrors* gr_response_MEAN = new TGraphErrors(nBins, response_MEAN_x, response_MEAN_y, response_MEAN_xerr, response_MEAN_yerr);
TGraphErrors* gr_resolution_FIT = new TGraphErrors(nBins, resolution_FIT_x, resolution_FIT_y, resolution_FIT_xerr, resolution_FIT_yerr);
TGraphErrors* gr_resolution_RMS = new TGraphErrors(nBins, resolution_RMS_x, resolution_RMS_y, resolution_RMS_xerr, resolution_RMS_yerr);
std::string gr_name;
gr_name = "gr_response_vs_" + varX + "_FIT_" + etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_response_FIT->SetName(gr_name.c_str());
gr_name = "gr_response_vs_" + varX + "_MEAN_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_response_MEAN->SetName(gr_name.c_str());
gr_name = "gr_resolution_vs_" + varX + "_FIT_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_resolution_FIT->SetName(gr_name.c_str());
gr_name = "gr_resolution_vs_" + varX + "_RMS_"+etaRegion;
gr_name = gr_name + "_" + algoType;
if(flag!="")
gr_name = gr_name + "_" + flag;
gr_resolution_RMS->SetName(gr_name.c_str());
TFile* outFile = TFile::Open(outFileName.c_str(), "update");
outFile->cd();
gr_response_FIT->Write();
gr_response_MEAN->Write();
gr_resolution_FIT->Write();
gr_resolution_RMS->Write();
outFile->Write();
outFile->Close();
if(name!="") {
projectionFile->Write();
projectionFile->Close();
delete projectionFile;
}
projectionFile = 0;
} //fill profile
*/
void fitTools::fillPositionResolution(TH1F* h1_sigmaEta, TH1F* h1_sigmaPhi, TH2D* h2_deltaEta, TH2D* h2_deltaPhi) {
for(int iBin=1; iBin<(h2_deltaEta->GetNbinsX()+1); ++iBin) {
TH1D* h1_projection = h2_deltaEta->ProjectionY("projectiony", iBin, iBin);
TF1* gaussian= new TF1("gaussian", "gaus");
fitProjection(h1_projection, gaussian, 2., "RQLL");
h1_sigmaEta->SetBinContent(iBin, gaussian->GetParameter(2));
h1_sigmaEta->SetBinError(iBin, gaussian->GetParError(2));
delete gaussian;
gaussian = 0;
h1_projection = 0;
} //for bins eta
for(int iBin=1; iBin<(h2_deltaPhi->GetNbinsX()+1); ++iBin) {
TH1D* h1_projection = h2_deltaPhi->ProjectionY("projectiony", iBin, iBin);
TF1* gaussian= new TF1("gaussian", "gaus");
fitProjection(h1_projection, gaussian, 2., "RQLL");
h1_sigmaPhi->SetBinContent(iBin, gaussian->GetParameter(2));
h1_sigmaPhi->SetBinError(iBin, gaussian->GetParError(2));
delete gaussian;
gaussian = 0;
h1_projection = 0;
} //for bins
} //fill position resolution
//used by getEfficiencyHisto (later on):
int fitTools::getEfficiencyUncertainties(int n, int k, double p, double &xmin, double &xmax) {
// create a histogram with binomial distribution
TH1D* h1_binomial = new TH1D("hist_binomial", "", 1000, 0., 1.);
// loop over bins and fill histogram
for (int i = 1; i <= 1000; ++i) {
double x = h1_binomial -> GetBinCenter(i);
double val = TMath::Binomial(n, k) * TMath::Power(x, double(k)) * TMath::Power(1-x, double(n-k));
h1_binomial -> SetBinContent(i, val);
}
// normalize
h1_binomial -> Scale(1.0 / h1_binomial -> Integral());
// calculate quantiles
int nprobSum = 4;
double q[4];
double probSum[4];
probSum[0] = (1.-p)/2.;
probSum[1] = 1.-(1.-p)/2.;
probSum[2] = 0.05;
probSum[3] = 0.95;
h1_binomial -> GetQuantiles(nprobSum, q, probSum);
// delete h1_binomial;
// h1_binomial=0;
double xexp = double(k)/double(n);
if (xexp > q[1]) {
TCanvas* c1 = new TCanvas("c1", "c1", 800, 600);
c1->cd();
h1_binomial->Draw();
c1->SaveAs("prova.eps");
exit(11);
}
// calculate uncertainties
if (n == 0)
{
xmin = 0.0;
xmax = 0.0;
return -3;
}
else if (xexp < q[0])
{
xmin = 0;
xmax = q[3];
return -2;
}
else if (xexp > q[1])
{
xmin = q[2];
xmax = 1.0;
return -1;
}
else
{
xmin = q[0];
xmax = q[1];
return 1;
}
}
TGraphAsymmErrors* fitTools::getEfficiencyGraph(const std::string& name, TH1F* h1_numerator, TH1F* h1_denominator) {
TGraphAsymmErrors* gr_returnGraph = new TGraphAsymmErrors();
gr_returnGraph->SetName(name.c_str());
int npoints = 0;
// set points
for (int i = 1; i <= h1_denominator -> GetNbinsX(); ++i) {
// calculate uncertainties
double xmin;
double xmax;
int flag = fitTools::getEfficiencyUncertainties(
int(h1_denominator -> GetBinContent(i)),
int(h1_numerator -> GetBinContent(i)),
0.68, xmin, xmax);
if (flag == 1)
{
gr_returnGraph -> SetPoint(
npoints,
h1_denominator -> GetBinCenter(i),
h1_numerator -> GetBinContent(i) / h1_denominator -> GetBinContent(i));
// set uncertainties
gr_returnGraph -> SetPointEXhigh(npoints, 0.);
gr_returnGraph -> SetPointEXlow(npoints, 0.);
gr_returnGraph -> SetPointEYhigh(npoints, xmax - h1_numerator -> GetBinContent(i) / h1_denominator -> GetBinContent(i));
gr_returnGraph -> SetPointEYlow(npoints++, h1_numerator -> GetBinContent(i) / h1_denominator -> GetBinContent(i) - xmin);
}
else if (flag == -2)
{
gr_returnGraph -> SetPoint(npoints, h1_denominator -> GetBinCenter(i), 0.);
// set uncertainties
gr_returnGraph -> SetPointEXhigh(npoints, 0.);
gr_returnGraph -> SetPointEXlow(npoints, 0.);
gr_returnGraph -> SetPointEYhigh(npoints, xmax);
gr_returnGraph -> SetPointEYlow(npoints++, 0.);
}
else if (flag == -1)
{
gr_returnGraph -> SetPoint(npoints, h1_denominator -> GetBinCenter(i), 1.);
// set uncertainties
gr_returnGraph -> SetPointEXhigh(npoints, 0.);
gr_returnGraph -> SetPointEXlow(npoints, 0.);
gr_returnGraph -> SetPointEYhigh(npoints, 0.);
gr_returnGraph -> SetPointEYlow(npoints++, 1.-xmin);