-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMassAna.cc
1274 lines (1050 loc) · 37.7 KB
/
MassAna.cc
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 "MassAna.h"
MassAna::MassAna(){
ptype = ".gif";
fitInput = new MassAnaInput();
fitInput->Initialize( &hfolder );
string decayType ;
fitInput->GetParameters( "DecayType", &decayType);
cname = decayType;
fitInput->GetParameters( "MassLBound", &mL);
fitInput->GetParameters( "MassHBound", &mH);
fitFunc = new MassFitFunction();
gSystem->mkdir(hfolder);
}
MassAna::~MassAna(){
delete fitFunc;
delete fitInput;
}
// chi2 test + minimum chi2 fit, output best estimated mass
double MassAna::Chi2Test( TString mName, TH1D* theData, int lowBound, int upBound, int nPar, int NBTag, Double_t* statErr, int rbin, bool isWeight ) {
plot9 = hfolder+"Chi2Test_"+mName+".gif";
gStyle->SetOptFit(111);
c9 = new TCanvas("c9","", 800, 600);
c9->SetGrid();
c9->SetFillColor(10);
c9->SetFillColor(10);
c9->cd();
Double_t mAssumption[16] = {0.};
Double_t nChi2[16] ={0.};
Double_t fpar[12];
double maxChi2 = 0. ;
TF1 *func0 = 0;
if ( cname == "had" && !isWeight ) func0 = new TF1("func0", MassFitFunction::fitData1, lowBound, upBound, 12);
if ( cname == "lep" || isWeight ) func0 = new TF1("func0", MassFitFunction::fitData2, lowBound, upBound, 12);
TF1 *fS =0;
if ( cname == "had" && !isWeight ) fS = new TF1("fS" , MassFitFunction::fitSG, lowBound, upBound, 6);
if ( cname == "lep" || isWeight ) fS = new TF1("fS" , MassFitFunction::fitSG1, lowBound, upBound, 6);
TF1 *fW = new TF1("fW", MassFitFunction::fitLD, lowBound, upBound, 3);
TF1 *fB = new TF1("fB", MassFitFunction::fitLD, lowBound, upBound, 3);
for (int i =0; i < 16; i++ ) {
// set up the mass assumption
mAssumption[i] = 156. + (i*2.) ;
SetFitParameters( mAssumption[i], fpar, nPar, NBTag, rbin );
// Fit the data
func0->SetParLimits(0, 10., 100.);
func0->FixParameter(1, fpar[1] );
func0->FixParameter(2, fpar[2] );
func0->SetParLimits(3, fpar[3]-0.1*fpar[3], fpar[3]+0.1*fpar[3] );
func0->FixParameter(4, fpar[4] );
func0->FixParameter(5, fpar[5] );
func0->FixParameter(6, fpar[6] );
func0->FixParameter(7, fpar[7] );
func0->FixParameter(8, fpar[8] );
func0->FixParameter(9, fpar[9] );
func0->SetParLimits(10,fpar[10]-0.1*fpar[10], fpar[10]+0.1*fpar[10] );
func0->FixParameter(11,fpar[11]);
theData->Fit( func0, "RQ0","", lowBound, upBound);
nChi2[i] = getChi2(theData, func0, fS, fB, fW, 130, 210, nPar );
if ( nChi2[i] > maxChi2 ) maxChi2 = nChi2[i] ;
}
double mErr[16]={0.};
double x2Err[16]={0.};
TGraph* x2test = new TGraph(16, mAssumption, nChi2);
x2test->SetTitle(" chi2 test ");
x2test->SetMarkerColor(4);
x2test->SetMarkerStyle(21);
x2test->SetMaximum(maxChi2*1.25);
x2test->SetMinimum(0.0);
x2test->GetXaxis()->SetTitle(" input mass assumption ");
x2test->GetYaxis()->SetTitle(" chi2 ");
x2test->Draw("AP");
TF1 *func5 = new TF1("func5",MassFitFunction::fitParabola, 154, 188, 3 );
func5->SetParLimits(0, 150, 190);
func5->SetParameter(1, 3.5 );
x2test->Fit( func5, "NR0", "",154,188 );
std::vector<bool> rejList = fitFunc->DataRejection( func5, mAssumption, nChi2, 16);
for (int i=0; i<16; i++) {
if ( !rejList[i] ) x2Err[i] = 0.1 ;
if ( rejList[i] ) x2Err[i] = 10. ;
}
TGraphErrors* x2testErr = new TGraphErrors(16, mAssumption, nChi2, mErr, x2Err );
x2testErr->SetTitle(" chi2 test ");
x2testErr->SetMarkerColor(4);
x2testErr->SetMarkerStyle(21);
x2testErr->SetMaximum(maxChi2*1.25);
x2testErr->SetMinimum(0.0);
x2testErr->GetXaxis()->SetTitle(" input mass assumption ");
x2testErr->GetYaxis()->SetTitle(" chi2 ");
x2testErr->Draw("AP");
x2testErr->Fit( func5, "R", "sames",154,188 );
// re-fit the chi2 curve
if ( func5->GetChisquare() > 650. ) {
double p1 = func5->GetParameter(0);
double L1 = p1 - (p1 - 158)/2 ;
double H1 = p1 + (188 - p1)/2 ;
if ( p1 > 188 ) {
L1 = 171;
H1 = 188;
}
if ( p1 < 158 ) {
L1 = 158;
H1 = 171;
}
x2testErr->Fit( func5, "R", "sames", L1, H1 );
}
c9->Update();
c9->Print(plot9);
double massCandidate = func5->GetParameter(0);
if (statErr != NULL ) *statErr = func5->GetParameter(1);
gStyle->SetOptFit(111);
delete fS;
delete fW;
delete fB;
delete func0;
delete func5;
delete x2test;
delete x2testErr;
return massCandidate ;
}
double MassAna::getChi2( TH1D* theData1, TF1* theFunc, TF1* fS, TF1* fB, TF1* fW, double lowBound, double upBound, int nPar ) {
for (int s=0; s<6; s++) {
fS->FixParameter( s, theFunc->GetParameter(s) );
}
fW->FixParameter( 0, theFunc->GetParameter(11)*theFunc->GetParameter(0) );
fW->FixParameter( 1, theFunc->GetParameter(8) );
fW->FixParameter( 2, theFunc->GetParameter(9) );
fB->FixParameter( 0, theFunc->GetParameter(10)*theFunc->GetParameter(0) );
fB->FixParameter( 1, theFunc->GetParameter(6) );
fB->FixParameter( 2, theFunc->GetParameter(7) );
int nbin = theData1->GetNbinsX() ;
double bW = (mH -mL) / nbin ;
Int_t b1 = lowBound / bW ;
Int_t b2 = upBound / bW ;
double chi2 = 0;
for (int i= b1 ; i< b2+1; i++) {
double k = (i-1)*bW + bW/2. ;
double theF = theFunc->Eval(k);
double theW = fW->Eval(k);
double theB = fB->Eval(k);
//double theS = fS->Eval(k);
double theH = theData1->GetBinContent(i);
double x2 = ( theH - theF )*( theH - theF ) / ( theF + theB + theW );
if ( nPar == 9 ) x2 = ( theH - theF )*( theH - theF ) / ( theF + theB );
chi2 += x2;
}
//chi2 = chi2 / ( b2+1 - b1 - 3 ) ;
return chi2;
}
void MassAna::GetAllCoeff( TString mName, int rbin, int lowBound, int upBound, Bool_t *comp ) {
Double_t sPar[6];
Double_t sErr[6];
Double_t bPar[6];
Double_t bErr[6];
if ( comp == NULL ) {
FitTtbar( mName, rbin, sPar, sErr );
FitBackground( mName, rbin, lowBound, upBound, bPar, bErr );
} else {
FitSignal( mName, rbin, sPar, sErr );
FitBackground( mName, rbin, lowBound, upBound, bPar, bErr );
}
parfile = fopen(hfolder+"/paraf.log","a");
errfile = fopen(hfolder+"/perrf.log","a");
for ( int i =0; i< 6; i++) {
fprintf(parfile," %.3f", sPar[i] );
fprintf(errfile," %.3f", sErr[i] );
}
for ( int i =0; i< 6; i++) {
if ( i != 0 && i != 3 ) {
fprintf(parfile," %.3f", bPar[i] );
fprintf(errfile," %.3f", bErr[i] );
}
}
double err_bs0 = sqrt( pow(bErr[0]/sPar[0], 2) + pow( (bPar[0]*sErr[0]/(sPar[0]*sPar[0])),2 ) ) ;
double err_bs3 = sqrt( pow(bErr[3]/sPar[0], 2) + pow( (bPar[3]*sErr[0]/(sPar[0]*sPar[0])),2 ) ) ;
fprintf(parfile," %.2f", bPar[0]/sPar[0] );
fprintf(errfile," %.2f", err_bs0 );
fprintf(parfile," %.2f", bPar[3]/sPar[0] );
fprintf(errfile," %.2f", err_bs3 );
fprintf(parfile," \n" );
fprintf(errfile," \n" );
fclose(parfile);
fclose(errfile);
}
void MassAna::FitSignal1( TString mName, int rbin ) {
gStyle->SetOptFit(111);
gStyle->SetOptStat("nirm");
gStyle->SetStatY(0.95);
gStyle->SetStatX(0.95);
gStyle->SetStatTextColor(1);
TString plot7 = hfolder+"FitTest_"+mName+".gif";
TString plot6 = hfolder+"FitMC_"+mName+".gif";
c7 = new TCanvas("c7","", 1000, 800);
c7->SetFillColor(10);
c7->SetFillColor(10);
c7->Divide(2,2);
double m1 = MassDigi(mName);
double lowBound = m1 - 50 ;
double upBound = m1 + 170 ;
c7->cd(1);
// tmp0: get template signal distribution
int nbin = (mH - mL ) / rbin ;
TH1D* sg = new TH1D("sg","", nbin, mL, mH );
fitInput->getTt( sg, m1 );
//fitInput->getSignal( sg, 1, mName );
sg->SetFillColor(7);
sg->Draw();
TF1* func0 = new TF1("func0", MassFitFunction::fitLG , 80, 450, 3);
TF1* func7 = new TF1("func7", MassFitFunction::fitGS , lowBound, upBound, 3);
TF1* func2 = new TF1("func2", MassFitFunction::fitSG , lowBound-20, upBound, 6);
if ( cname == "lep" ) {
func0 = new TF1("func0", MassFitFunction::fitLD , 80, 450, 3);
func2 = new TF1("func2", MassFitFunction::fitSG1 , lowBound-20, upBound, 6);
}
TF1* func3 = new TF1("func3", MassFitFunction::fitLD , 0, 480, 3);
// pre-set the value
// Hadronic top : gaus + log-normal
// Leptonic top : gaus + landau
double p0 = 50. ;
double p1 = m1 ;
double p2 = 20. ;
double p3 = 33. ;
double p4 = log(m1) + sqrt(1./20.) ;
double p5 = 5. ;
if ( cname == "lep" ) {
p3 = 1.8;
p4 = m1 ;
p5 = 30 ;
}
// 1st Fit, Fix "mean" value for gaussisan & log-normal and allow normalization and width vary
func2->SetParLimits( 0, 10., p0+1.0*p0);
func2->FixParameter( 1, p1 );
func2->SetParLimits( 2, p2-0.3*p2, p2+1.0*p2);
if ( cname == "had" ) func2->SetParLimits(3, p3-0.1*p3, p3+0.1*p3);
if ( cname == "lep" ) func2->SetParLimits(3, p3-0.3*p3, p3+0.3*p3);
func2->FixParameter(4, p4 );
func2->FixParameter(5, p5 );
sg->Fit( func2, "RQ0","", lowBound, upBound );
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p5 = func2->GetParameter(5);
// Draw the gaussian
func7->FixParameter(0, p0 );
func7->FixParameter(1, p1 );
func7->FixParameter(2, p2 );
func7->SetLineColor(4);
func7->Draw("sames");
c7->Update();
c7->cd(2);
sg->SetFillColor(7);
sg->Draw();
// 2nd Fit , Allow gaussian change
func2->SetParLimits(0, p0-0.1*p0, p0+0.1*p0);
func2->SetParLimits(1, m1-0.1*m1, m1+0.1*m1 );
func2->SetParLimits(2, p2-0.1*p2, p2+0.1*p2 );
func2->SetParLimits(3, p3-0.1*p3, p3+0.1*p3);
func2->FixParameter(4, p4 );
func2->FixParameter(5, p5 );
cout<<" Orignal P4 = "<<p4<<endl;
sg->Fit( func2, "R","sames", lowBound, upBound);
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p4 = func2->GetParameter(4);
p5 = func2->GetParameter(5);
func0->FixParameter(0, p0*p3);
func0->FixParameter(1, p4 );
func0->FixParameter(2, p5 );
func0->SetLineColor(2);
func0->SetLineWidth(3);
func0->SetLineStyle(2);
func0->Draw("sames");
c7->Update();
c7->cd(3);
// 3rd Fit , tunning tail distribution
double p4Max = p4;
double p4Min = p4;
if ( cname == "had" ) {
p4Min = ( m1 < p1 ) ? log(m1) + sqrt(1./20.) : log(p1) + sqrt(1./20.) ;
p4Max = ( m1 > p1 ) ? log(m1) + sqrt(1./20.) : log(p1) + sqrt(1./20.) ;
}
if ( cname == "lep" ) {
p4Min = ( m1 < p1) ? m1 : p1 ;
p4Max = ( m1 > p1) ? m1 : p1 ;
}
func2->SetParameter(0, p0 );
func2->SetParameter(1, p1 );
func2->SetParameter(2, p2 );
func2->FixParameter(3, p3 );
func2->SetParLimits(4, p4Min, p4Max );
func2->FixParameter(5, p5 );
sg->SetFillColor(7);
sg->Fit( func2, "R","", lowBound, upBound);
//func2->Draw("sames");
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p1 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p4 = func2->GetParameter(4);
p5 = func2->GetParameter(5);
func0->FixParameter(0, p0*p3);
func0->FixParameter(1, p4 );
func0->FixParameter(2, p5 );
func0->SetLineColor(2);
func0->SetLineWidth(3);
func0->SetLineStyle(2);
func0->Draw("sames");
c7->Update();
// 4th Fit , Final tunning
c7->cd(4);
func2->SetParameter(0, p0 );
func2->SetParameter(1, p1 );
func2->SetParameter(2, p2 );
func2->FixParameter(3, p3 );
func2->FixParameter(4, p4 );
func2->FixParameter(5, p5 );
sg->SetFillColor(7);
sg->Draw();
sg->Fit( func2, "R","sames", lowBound, upBound);
func2->Draw("sames");
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p4 = func2->GetParameter(4);
p5 = func2->GetParameter(5);
func0->FixParameter(0, p0*p3);
func0->FixParameter(1, p4 );
func0->FixParameter(2, p5 );
func0->SetLineColor(2);
func0->SetLineWidth(3);
func0->SetLineStyle(2);
func0->Draw("sames");
c7->Update();
c7->Print(plot7);
TCanvas* c6 = new TCanvas("c6","", 1000, 800);
c6->SetFillColor(10);
sg->Draw();
func2->Draw("sames");
func0->Draw("sames");
c6->Update();
c6->Print(plot6);
sg->Draw();
delete c7;
delete c6;
delete func3;
delete func2;
delete func7;
delete func0;
delete sg;
}
void MassAna::FitSignal(TString mName, int rbin, Double_t* para, Double_t* perr) {
FILE* testfile = fopen(hfolder+"/Sgpara.log","a");
gStyle->SetOptFit(111);
gStyle->SetOptStat("nirm");
gStyle->SetStatY(0.95);
gStyle->SetStatX(0.95);
gStyle->SetStatTextColor(1);
TString plot7 = hfolder+"FitTest_"+mName+".gif";
TString plot6 = hfolder+"FitMC_"+mName+".gif";
c7 = new TCanvas("c7","", 1000, 800);
c7->SetFillColor(10);
c7->SetFillColor(10);
c7->Divide(2,2);
double m1 = MassDigi(mName);
double lowBound = m1 - 50 ;
double upBound = m1 + 170 ;
vector<string> msets;
fitInput->GetParameters( "TMassAssumption", &msets );
for (size_t i=0; i< msets.size(); i++) {
cout << " m assumption = "<<msets[i].substr(0,3) <<endl;
}
c7->cd(1);
// tmp0: get template signal distribution
int nbin = ( mH - mL ) / rbin ;
TH1D* sg = new TH1D("sg","", nbin, mL, mH );
fitInput->getTt( sg, m1 );
//fitInput->getSignal( sg, 1, mName );
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
TF1* func0 = new TF1("func0", MassFitFunction::fitLG , 80, 450, 3);
TF1* func7 = new TF1("func7", MassFitFunction::fitGS , lowBound, upBound, 3);
TF1* func2 = new TF1("func2", MassFitFunction::fitSG , lowBound-20, upBound, 6);
if ( cname == "lep" ) {
func0 = new TF1("func0", MassFitFunction::fitLD , 80, 450, 3);
func2 = new TF1("func2", MassFitFunction::fitSG1 , lowBound-20, upBound, 6);
}
TF1* func3 = new TF1("func3", MassFitFunction::fitLD , 0, 480, 3);
// pre-set the value
// Hadronic top : gaus + log-normal
// Leptonic top : gaus + landau
double p0 = 50. ;
double p1 = m1 ;
double p2 = 20. ;
double p3 = 33. ;
double p4 = log(m1) + sqrt(1./20.) ;
double p5 = 5. ;
if ( cname == "lep" ) {
p3 = 1.8;
p4 = m1 ;
p5 = 30 ;
}
// 1st Fit, Fix "mean" value for gaussisan & log-normal and allow normalization and width vary
func2->SetParLimits( 0, 10., p0+1.0*p0);
func2->FixParameter( 1, p1 );
func2->SetParLimits( 2, p2-0.3*p2, p2+1.0*p2);
if ( cname == "had" ) func2->SetParLimits(3, p3-0.1*p3, p3+0.1*p3);
if ( cname == "lep" ) func2->SetParLimits(3, p3-0.3*p3, p3+0.3*p3);
func2->FixParameter(4, p4 );
func2->FixParameter(5, p5 );
sg->Fit( func2, "RQ0","", lowBound, upBound );
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p5 = func2->GetParameter(5);
// Draw the gaussian
func7->FixParameter(0, p0 );
func7->FixParameter(1, p1 );
func7->FixParameter(2, p2 );
func7->SetLineColor(4);
func7->Draw("sames");
c7->Update();
/*
c7->cd(4);
func3->FixParameter(0, p3*p0 );
func3->FixParameter(1, p4 );
func3->FixParameter(2, p5 );
func3->Draw();
c7->Update();
*/
c7->cd(2);
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
// 2nd Fit , Allow gaussian change
func2->SetParLimits(0, p0-0.1*p0, p0+0.1*p0);
func2->SetParLimits(1, m1 - 5., m1 + 5. );
func2->SetParLimits(2, p2-0.1*p2, p2+0.1*p2 );
func2->SetParLimits(3, p3-0.05*p3,p3+0.05*p3);
func2->FixParameter(4, p4 );
func2->FixParameter(5, p5 );
sg->Fit( func2, "R","sames", lowBound, upBound);
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p4 = func2->GetParameter(4);
p5 = func2->GetParameter(5);
func0->FixParameter(0, p0*p3);
func0->FixParameter(1, p4 );
func0->FixParameter(2, p5 );
func0->SetLineColor(2);
func0->SetLineWidth(3);
func0->SetLineStyle(2);
func0->Draw("sames");
c7->Update();
c7->cd(3);
// 3rd Fit , Final tunning
func2->SetParLimits(0, p0- 0.1*p0 , p0+0.1*p0);
func2->SetParLimits(1, m1 - 1. , m1 + 1. );
func2->SetParLimits(2, p2- 0.1*p2, p2+ 0.1*p2);
func2->SetParLimits(3, p3-0.01*p3, p3+0.01*p3);
func2->FixParameter(4, p4 );
func2->SetParLimits(5, p5-0.01*p5, p5+0.01*p5 );
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
sg->Fit( func2, "R","sames", lowBound, upBound);
func2->Draw("sames");
p0 = func2->GetParameter(0);
p1 = func2->GetParameter(1);
p2 = func2->GetParameter(2);
p3 = func2->GetParameter(3);
p4 = func2->GetParameter(4);
p5 = func2->GetParameter(5);
fprintf(testfile," %.1f", m1 );
for (int i=0; i<6; i++) {
fprintf(testfile," %.3f %.3f", func2->GetParameter(i), func2->GetParError(i) );
if ( para != NULL ) para[i] = func2->GetParameter(i);
if ( perr != NULL ) perr[i] = func2->GetParError(i) ;
}
fprintf(testfile," \n" );
func0->FixParameter(0, p0*p3);
func0->FixParameter(1, p4 );
func0->FixParameter(2, p5 );
func0->SetLineColor(2);
func0->SetLineWidth(3);
func0->SetLineStyle(2);
func0->Draw("sames");
c7->Update();
c7->Print(plot7);
TCanvas* c6 = new TCanvas("c6","", 1000, 800);
c6->SetFillColor(10);
sg->Draw();
func2->Draw("sames");
func0->Draw("sames");
c6->Update();
c6->Print(plot6);
sg->Draw();
delete c7;
delete c6;
delete func3;
delete func2;
delete func7;
delete func0;
delete sg;
fclose(testfile);
}
// for kinematic constrain
void MassAna::FitTtbar(TString mName, int rbin, Double_t* para, Double_t* perr) {
FILE* testfile = fopen(hfolder+"Sgpara.log","a");
gStyle->SetOptFit(111);
gStyle->SetOptStat("nirm");
gStyle->SetStatY(0.95);
gStyle->SetStatX(0.95);
gStyle->SetStatTextColor(1);
TString plot7 = hfolder+"FitTest_"+mName+".gif";
c7 = new TCanvas("c7","", 1000, 800);
c7->SetFillColor(10);
c7->SetFillColor(10);
c7->Divide(2,2);
double m1 = MassDigi(mName);
double lowBound = m1 - 50 ;
double upBound = m1 + 170 ;
int nbin = ( mH - mL ) / rbin ;
TF1* fnG = new TF1("fnG", MassFitFunction::fitGS , lowBound, upBound, 3);
TF1* fnL = new TF1("fnL", MassFitFunction::fitLD , 80, 450, 3);
TF1* fnSG = new TF1("fnSG", MassFitFunction::fitSG1 , lowBound, upBound, 6);
// A testing platform
c7->cd(4);
gStyle->SetOptStat("ei");
gStyle->SetStatY(0.99);
TString theBrName = cname+"TM" ;
TH1D* mph = new TH1D("mph","", nbin, mL, mH );
fitInput->getMostProb( mName, theBrName, mph );
mph->Draw();
c7->Update();
gStyle->SetStatY(0.84);
gStyle->SetStatTextColor(4);
TH1D* mch = new TH1D("mch","", nbin, mL, mH );
fitInput->getMcMatching( mName, theBrName, mch );
mch->SetLineColor(4);
fnG->SetParLimits( 0, 1, 20 );
fnG->SetParameter( 1, m1 );
fnG->SetParLimits( 2, 10, 30 );
fnG->SetLineColor(4);
mch->Fit( fnG, "R","sames", lowBound-20, upBound );
c7->Update();
gStyle->SetStatY(0.55);
gStyle->SetStatTextColor(2);
TH1D* tbg = new TH1D("tbg","", nbin, mL, mH );
tbg->Add( mph );
tbg->Add( mch, -1. );
tbg->SetLineColor(2);
fnL->SetParLimits( 0, 1, 100 );
fnL->SetParLimits( 1, m1+10, m1+200 );
fnL->SetParLimits( 2, 5, 55 );
fnL->SetLineColor(2);
tbg->Fit( fnL, "R","sames", lowBound-20, upBound );
c7->Update();
c7->cd(1);
gStyle->SetStatTextColor(1);
gStyle->SetStatY(0.95);
// tmp0: get template signal distribution
TH1D* sg = new TH1D("sg","", nbin, mL, mH );
fitInput->getTt( sg, m1 );
//fitInput->getSignal( sg, 0, mName );
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
// pre-set the value
double p0 = 40. ;
double p1 = m1 ;
double p2 = fnG->GetParameter(2);
double p3 = fnL->GetParameter(0);
double p4 = fnL->GetParameter(1);
double p5 = fnL->GetParameter(2);
// 1st Fit, Fix "mean" value for gaussisan & landau and allow normalization and width vary
fnSG->SetParLimits( 0, p0-0.1*p0, p0+1.0*p0);
fnSG->FixParameter( 1, p1 );
fnSG->SetParLimits( 2, p2-0.2*p2, p2+0.2*p2);
fnSG->SetParLimits( 3, 0.1, 8 );
fnSG->SetParameter( 4, p4 );
fnSG->SetParLimits( 5, p5-0.1*p5, p5+0.1*p5);
sg->Fit( fnSG, "RQ0","", lowBound, upBound );
p0 = fnSG->GetParameter(0);
p1 = fnSG->GetParameter(1);
p2 = fnSG->GetParameter(2);
p3 = fnSG->GetParameter(3);
p5 = fnSG->GetParameter(5);
// Draw the gaussian
fnG->FixParameter(0, p0 );
fnG->FixParameter(1, p1 );
fnG->FixParameter(2, p2 );
fnG->SetLineColor(4);
fnG->Draw("sames");
c7->Update();
c7->cd(2);
gStyle->SetOptStat("nirm");
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
// 2nd Fit , Allow gaussian change
fnSG->SetParLimits(0, p0-0.1*p0, p0+0.1*p0);
fnSG->SetParLimits(1, m1 - 5. , m1 + 5. );
fnSG->SetParLimits(2, p2-0.1*p2, p2+0.1*p2 );
fnSG->SetParLimits(3, p3-0.1*p3, p3+0.1*p3);
fnSG->FixParameter(4, p4 );
fnSG->FixParameter(5, p5 );
sg->Fit( fnSG, "R","sames", lowBound, upBound);
p0 = fnSG->GetParameter(0);
p1 = fnSG->GetParameter(1);
p2 = fnSG->GetParameter(2);
p3 = fnSG->GetParameter(3);
p4 = fnSG->GetParameter(4);
p5 = fnSG->GetParameter(5);
fnL->FixParameter(0, p0*p3);
fnL->FixParameter(1, p4 );
fnL->FixParameter(2, p5 );
fnL->SetLineColor(2);
fnL->SetLineWidth(3);
fnL->SetLineStyle(2);
fnL->Draw("sames");
c7->Update();
c7->cd(3);
// 3rd Fit , Final tunning
fnSG->SetParLimits(0, p0- 0.1*p0 , p0+0.1*p0);
fnSG->SetParLimits(1, m1 - 1. , m1 + 1. );
fnSG->SetParLimits(2, p2- 0.1*p2, p2+ 0.1*p2);
fnSG->SetParLimits(3, p3-0.01*p3, p3+0.01*p3);
fnSG->SetParLimits(4, p4-0.01*p4, p4+0.01*p4);
fnSG->SetParLimits(5, p5-0.01*p5, p5+0.01*p5 );
sg->SetFillColor(7);
//sg->SetMaximum( yMax );
sg->Draw();
sg->Fit( fnSG, "R","sames", lowBound, upBound);
fnSG->Draw("sames");
p0 = fnSG->GetParameter(0);
p1 = fnSG->GetParameter(1);
p2 = fnSG->GetParameter(2);
p3 = fnSG->GetParameter(3);
p4 = fnSG->GetParameter(4);
p5 = fnSG->GetParameter(5);
fprintf(testfile," %.1f", m1 );
for (int i=0; i<6; i++) {
fprintf(testfile," %.3f %.3f", fnSG->GetParameter(i), fnSG->GetParError(i) );
if ( para != NULL ) para[i] = fnSG->GetParameter(i);
if ( perr != NULL ) perr[i] = fnSG->GetParError(i) ;
}
fprintf(testfile," \n" );
fnL->FixParameter(0, p0*p3);
fnL->FixParameter(1, p4 );
fnL->FixParameter(2, p5 );
fnL->SetLineColor(2);
fnL->SetLineWidth(3);
fnL->SetLineStyle(2);
fnL->Draw("sames");
c7->Update();
c7->Print(plot7);
delete c7;
delete fnG;
delete fnL;
delete fnSG;
delete sg;
delete tbg;
delete mch;
delete mph;
fclose(testfile);
}
// background 1 : tt-wrong combinatorics and all other backgrounds seperated
// background 2 : tt-wrong + all other background channels
void MassAna::FitBackground( TString mName, int rbin, int lowBound, int upBound, Double_t *para, Double_t *perr ){
FILE* bgpara = fopen(hfolder+"bgpara_AlgoZero.log","a");
double m1 = MassDigi(mName);
plot8 = hfolder+cname+"_BG_"+mName+".gif";
// get the tt wrong combinatorics
int nbin = ( mH - mL ) / rbin ;
TH1D* tt = new TH1D("tt","tt wrong combinatorics", nbin, mL, mH );
fitInput->getTt( tt, m1, false );
// background group 1, qcd
THStack* bg1stk = new THStack("bg1stk", "Sum of backgrounds w/o tt");
TH1D* bg1 = new TH1D("bg1","Background group 1", nbin, mL, mH );
vector<TH1D*> bglist1;
fitInput->getBackground( bg1, bg1stk, bglist1, 1 );
// background group 2, all other background channels
THStack* bg2stk = new THStack("bg2stk", "Sum of backgrounds w/ tt");
TH1D* bg2 = new TH1D("bg2","Background group 2", nbin, mL, mH );
vector<TH1D*> bglist2;
fitInput->getBackground( bg2, bg2stk, bglist2 );
/// add ttbar wrong combinatorics
TH1D* tt1 = (TH1D*) tt->Clone("tt1") ;
/*
bg2->Add(tt1, 1);
bg2stk->Add( tt1 ) ;
*/
// Fitting and Plotting
gStyle->SetOptFit(111);
gStyle->SetOptStat("nirm");
gStyle->SetStatY(0.95);
gStyle->SetStatX(0.95);
gStyle->SetStatTextColor(1);
c8 = new TCanvas("c8","", 800, 600);
c8->SetFillColor(10);
c8->SetFillColor(10);
c8->Divide(2,2);
// tt-wrong combinatorics
c8->cd(1);
tt->Draw();
TF1* func0 = new TF1("func0", MassFitFunction::fitLD , 90, 380, 3);
func0->SetParLimits(0, 5., tt->Integral() );
if (cname == "Had" ) func0->SetParLimits(1, m1-10, m1+20.);
if (cname == "Lep" ) func0->SetParLimits(1, m1-20, m1+15.);
func0->SetParLimits(2, 1., 200.);
func0->SetLineStyle(2);
tt->Fit( func0, "R","sames", lowBound, upBound );
c8->Update();
// backgrounds 1, qcd
c8->cd(2);
bg1stk->Draw();
TF1* func1 = new TF1("func1", MassFitFunction::fitLD , 90, 380, 3);
func1->SetParLimits(0, 1., bg1->Integral() );
//if (cname == "Had" ) func1->SetParLimits(1, 90, m1+20.);
//if (cname == "Lep" ) func1->SetParLimits(1, m1-20, m1+15.);
//func1->SetParLimits(1, 90., 200.);
//func1->SetParLimits(2, 1., 200.);
func1->SetLineColor(1);
bg1->Fit( func1, "R","sames", lowBound, 450 );
c8->Update();
// background 2, all other channels
c8->cd(3);
bg2stk->Draw();
TF1* func2 = new TF1("func2", MassFitFunction::fitLD , 90, 380, 3);
func2->SetParLimits(0, 1., bg2->Integral() );
func2->SetParLimits(1, m1-10, m1+20.);
func2->SetParLimits(2, 1., 200.);
bg2->Fit( func2, "N0R","", lowBound, upBound );
double p0 = func2->GetParameter(0);
double p1 = func2->GetParameter(1);
double p2 = func2->GetParameter(2);
func2->SetParLimits(0, p0-0.1*p0, p0+0.1*p0 );
func2->SetParLimits(1, p1-0.1*p1, p1+0.1*p1 );
func2->SetParLimits(2, p2-0.1*p2, p2+0.1*p2 );
bg2->Fit( func2, "R","sames", lowBound, upBound );
c8->Update();
c8->cd(4);
//bg2->Add(bg1, 1);
//bg2->Add(tt1, 1);
bg2stk->Add( bglist1[0] ) ;
bg2stk->Add( tt1 ) ;
bg2stk->Draw();
c8->Update();
c8->Print(plot8);
// para[0~2] : background group 1 / tt wrong permutation
// para[3~5] : background group 2 / combined backgrounds
fprintf(bgpara," %.1f", m1 );
for (int i=0; i<6; i++) {
if ( i < 3 ) {
fprintf(bgpara," %.3f %.3f", func0->GetParameter(i), func0->GetParError(i) );
if ( para != NULL ) para[i] = func0->GetParameter(i);
if ( perr != NULL ) perr[i] = func0->GetParError(i) ;
} else {
fprintf(bgpara," %.3f %.3f", func2->GetParameter(i-3), func2->GetParError(i-3) );
if ( para != NULL ) para[i] = func2->GetParameter(i-3);
if ( perr != NULL ) perr[i] = func2->GetParError(i-3) ;
}
}
fprintf(bgpara," \n" );
delete c8;
delete bg1;
delete bg2;
delete tt;
delete tt1;
delete func0;
delete func1;
delete func2;
delete bg1stk;
delete bg2stk;
fclose(bgpara);
}
// another method for kinematic constrain case
/*
void MassAna::FitBackground( TString mName, int rbin, int lowBound, int upBound, Double_t *para, Double_t *perr ){
FILE* bgpara = fopen(hfolder+"/Bgpara.log","a");
double m1 = MassDigi(mName);
plot8 = hfolder+"/"+cname+"_BG_"+mName+".gif";
int nbin = ( mH - mL ) / rbin ;
TH1D* wj = new TH1D("wj", "", nbin, mL, mH ); // tmp2: wjets, single Top t, single Top tW, QCD
TH1D* stt = new TH1D("stt", "", nbin, mL, mH );
TH1D* stw = new TH1D("stw", "", nbin, mL, mH );
fitInput->getBackground( wj, 2, nbin, mName );
fitInput->getBackground( stt, 3, nbin, mName );
fitInput->getBackground( stw, 4, nbin, mName );
TH1D* bg1 = new TH1D("bg1","Background group 1", nbin, mL, mH );
bg1->Add(wj, 1.);
bg1->Add(stt,1.);
bg1->Add(stw,1.);
c8 = new TCanvas("c8","", 800, 600);
c8->SetFillColor(10);
c8->SetFillColor(10);
// show the backgrounds compositions
c8->cd();
gStyle->SetOptStat("ni");
THStack* allbgstk = new THStack("allbgstk", "Sum of backgrounds");
stt->SetFillColor(4);
allbgstk->Add(stt);
stw->SetFillColor(6);
allbgstk->Add(stw);
wj->SetFillColor(2);
allbgstk->Add(wj);
allbgstk->Draw();
gStyle->SetStatY(0.30);
gStyle->SetStatTextColor(2);