-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHH2ggbbFitter_mgg.cc
1179 lines (943 loc) · 44.7 KB
/
HH2ggbbFitter_mgg.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
/** \macro H2GGFitter.cc
*
* The analysis root trees produced in a simple format
*
* TFile file(filename,"RECREATE", "X->jj input tree for unbinned maximum-likelihood fit");
* TTree* outTree = new TTree("XTojj","X->jj input tree for unbinned maximum-likelihood fit");
* Float_t mass;
* Int_t CAT3;
* Float_t weight;
*
* outTree->Branch("mass",&mass,"mass/F");
* outTree->Branch("weight",&weight,"weight/F");
* outTree->Branch("CAT4",&CAT4,"CAT4/I");
* {
* .............
* outTree->Fill();
* }
*
* file.Write();
* file.Close();
* delete outTree;
*
* are used as input files. They have to be produced for
* data and Monte Carlo signal and background data sets
* after all analysis selections to be applied. *
*/
// Loading: .L HH2ggbbFitter.cc
// Running: runfits("hgg120-shapes-combined-Unbinned.root")
//
using namespace RooFit;
using namespace RooStats ;
static const Int_t NCAT = 4; // chiara
std::string filePOSTfix="";
//double signalScaler=0.005; // chiara
double signalScaler=1.00;
void AddSigData(RooWorkspace*, Float_t);
void AddBkgData(RooWorkspace*);
void SigModelFit(RooWorkspace*, Float_t);
RooFitResult* BkgModelFitBernstein(RooWorkspace*, Bool_t);
RooFitResult* BkgModelFitExpo(RooWorkspace*, Bool_t);
void MakePlots(RooWorkspace*, Float_t, RooFitResult*);
void MakeSigWS(RooWorkspace* w, const char* filename);
void MakeBkgWS(RooWorkspace* w, const char* filename);
void SetConstantParams(const RooArgSet* params);
TPaveText* get_labelCMS( int legendQuadrant = 0 , std::string year="2012", bool sim=false) {
if( legendQuadrant!=0 && legendQuadrant!=1 && legendQuadrant!=2 && legendQuadrant!=3 ) {
std::cout << "WARNING! Legend quadrant '" << legendQuadrant << "' not yet implemented for CMS label. Using 2." << std::endl;
legendQuadrant = 2;
}
float x1, y1, x2, y2;
if( legendQuadrant==1 ) {
x1 = 0.63;
y1 = 0.83;
x2 = 0.8;
y2 = 0.87;
} else if( legendQuadrant==2 ) {
x1 = 0.25;
y1 = 0.83;
x2 = 0.42;
y2 = 0.87;
} else if( legendQuadrant==3 ) {
x1 = 0.25;
y1 = 0.2;
x2 = 0.42;
y2 = 0.24;
} else if( legendQuadrant==0 ) {
x1 = 0.175;
y1 = 0.953;
x2 = 0.6;
y2 = 0.975;
}
TPaveText* cmslabel = new TPaveText( x1, y1, x2, y2, "brNDC" );
cmslabel->SetFillColor(kWhite);
cmslabel->SetTextSize(0.038);
if( legendQuadrant==0 ) cmslabel->SetTextAlign(11);
cmslabel->SetTextSize(0.038);
cmslabel->SetTextFont(42);
std::string leftText;
if(year == "2012" && !sim) leftText = "CMS Preliminary 2012, 19.5 pb^{-1}";
if (sim) leftText = "CMS Simulation"; //cwr ->remove 2011
cmslabel->AddText(leftText.c_str());
return cmslabel;
}
TPaveText* get_labelSqrt( int legendQuadrant ) {
if( legendQuadrant!=0 && legendQuadrant!=1 && legendQuadrant!=2 && legendQuadrant!=3 ) {
std::cout << "WARNING! Legend quadrant '" << legendQuadrant << "' not yet implemented for Sqrt label. Using 2." << std::endl;
legendQuadrant = 2;
}
float x1, y1, x2, y2;
if( legendQuadrant==1 ) {
x1 = 0.63;
y1 = 0.78;
x2 = 0.8;
y2 = 0.82;
} else if( legendQuadrant==2 ) {
x1 = 0.25;
y1 = 0.78;
x2 = 0.42;
y2 = 0.82;
} else if( legendQuadrant==3 ) {
x1 = 0.25;
y1 = 0.16;
x2 = 0.42;
y2 = 0.2;
} else if( legendQuadrant==0 ) {
x1 = 0.65;
y1 = 0.953;
x2 = 0.87;
y2 = 0.975;
}
TPaveText* label_sqrt = new TPaveText(x1,y1,x2,y2, "brNDC");
label_sqrt->SetFillColor(kWhite);
label_sqrt->SetTextSize(0.038);
label_sqrt->SetTextFont(42);
label_sqrt->SetTextAlign(31); // align right
label_sqrt->AddText("#sqrt{s} = 8 TeV");
return label_sqrt;
}
RooArgSet* defineVariables() {
// define variables of the input ntuple //livia
RooRealVar* PhotonsMass = new RooRealVar("PhotonsMass", "M(gg)",140, 500,"GeV");
// RooRealVar* mjj = new RooRealVar("mjj", "M(jj)",10,300,"GeV");//livia
// RooRealVar* mggjj = new RooRealVar("mggjj", "M(ggjj)",10,1500,"GeV");//livia
RooRealVar* ph1_eta = new RooRealVar("ph1_eta", "eta(g1)",-10,10,"");
RooRealVar* ph2_eta = new RooRealVar("ph2_eta", "eta(g2)",-10,10,"");
RooRealVar* ph1_r9 = new RooRealVar("ph1_r9", "R9(g1)",-10,10,"");
RooRealVar* ph2_r9 = new RooRealVar("ph2_r9", "R9(g2)",-10,10,"");
RooRealVar* evweight = new RooRealVar("evweight","Reweightings",0,10,"");
// RooRealVar* btagCategory = new RooRealVar("btagCategory","event category",0.9,2.1,"") ;
RooArgSet* ntplVars = new RooArgSet(*PhotonsMass, *ph1_eta, *ph2_eta, *ph1_r9, *ph2_r9, *evweight);
return ntplVars;
}
void runfits(const Float_t mass=150, Bool_t dobands = false) {
//******************************************************************//
// Running mode corresponds to the following cases
// - full run set:
// - create signal and background data sets
// - make and fit signal and background models
// - write signal and background workspaces in root files
// - write data card
//*******************************************************************//
TString fileBaseName(TString::Format("Hgg.mX%.1f", mass));
TString fileBkgName(TString::Format("Hgg.inputbkg_8TeV", mass));
TString card_name("HighMass-hgg_mgg_models_Bkg_8TeV_test.rs");
HLFactory hlf("HLFactory", card_name, false);
RooWorkspace* w = hlf.GetWs();
RooFitResult* fitresults;
Double_t MMIN = 140; //livia
Double_t MMAX = 500; //livia
w->var("PhotonsMass")->setMin(MMIN);
w->var("PhotonsMass")->setMax(MMAX);
// Add data to the workspace
cout << endl; cout << "Now AddSigData" << endl;
AddSigData(w, mass);
cout << endl; cout << "Now AddBkgData" << endl;
AddBkgData(w);
// Add the signal and background models to the workspace.
// Inside this function you will find a discription our model.
cout << endl; cout << "Now SigModelFit" << endl;
SigModelFit(w, mass);
cout << endl; cout << "Now BkgModelFit" << endl;
// fitresults = BkgModelFitBernstein(w, dobands);
fitresults = BkgModelFitExpo(w, dobands);
// Make statistical treatment. Setup the limit on X production
cout << endl; cout << "Now make signal workspace" << endl;
MakeSigWS(w, fileBaseName+"_8TeV");
cout << endl; cout << "Now make background workspace" << endl;
MakeBkgWS(w, fileBkgName);
cout << endl; cout << "Now prepare datacards" << endl;
int ncat = NCAT;
for (int c=0; c<ncat; c++) MakeDataCard_1Channel(w, fileBaseName, fileBkgName, c);
// Make plots for data and fit results
cout << endl; cout << "Preparing final plots" << endl;
MakePlots(w, mass, fitresults);
return;
}
// Signal Data Set
void AddSigData(RooWorkspace* w, Float_t mass) {
Int_t ncat = NCAT;
TString inDir = "";
Float_t MASS(mass);
// Luminosity:
Float_t Lum = 19500.0;
RooRealVar lumi("lumi","lumi",Lum);
w->import(lumi);
// Variables
RooArgSet* ntplVars = defineVariables();
int iMass = abs(mass);
TFile sigFile1("histograms_CMS-HGG_19032013.root"); //ggh prod mode tree livia
//chain summing up all production modes
TChain* sigTree1 = new TChain();
sigTree1->Add(TString::Format("histograms_CMS-HGG_19032013.root/ggh_m%d_8TeV", iMass));
sigTree1->Add(TString::Format("histograms_CMS-HGG_19032013.root/vbf_m%d_8TeV", iMass));
sigTree1->Add(TString::Format("histograms_CMS-HGG_19032013.root/wzh_m%d_8TeV", iMass));
sigTree1->Add(TString::Format("histograms_CMS-HGG_19032013.root/tth_m%d_8TeV", iMass));
sigTree1->SetTitle("sigTree1");
sigTree1->SetName("sigTree1");
// common preselection cut
TString mainCut("PhotonsMass>=140 && PhotonsMass<=500"); // livia
// Create signal dataset composed with different productions, the weight is already applied in our ntuples
RooDataSet sigWeighted("sigWeighted","dataset",sigTree1,*ntplVars,mainCut,"evweight");
cout << endl;
cout << "sigWeighted" << endl;
sigWeighted.Print("v");
cout << "---- nX: " << sigWeighted.sumEntries() << endl;
// apply a common preselection cut; split in categories
cout << endl;
RooDataSet* signal[NCAT];
for (int c=0; c<ncat; ++c) {
// 0) chiara: 1cat only
// signal[c] = (RooDataSet*) sigWeighted.reduce(*w->var("massggnewvtx"),mainCut); //chiara, for 1 cat only
// 1) prime 4 cat livia
if (c==0) signal[c] = (RooDataSet*) sigWeighted.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)<1.5 && abs(ph2_eta)<1.5) && (ph1_r9>0.94 && ph2_r9>0.94 )"));
if (c==1) signal[c] = (RooDataSet*) sigWeighted.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)<1.5 && abs(ph2_eta)<1.5) && (ph1_r9<0.94 || ph2_r9<0.94 ) "));
if (c==2) signal[c] = (RooDataSet*) sigWeighted.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)>1.5 || abs(ph2_eta)>1.5) && (ph1_r9>0.94 && ph2_r9>0.94 )"));
if (c==3) signal[c] = (RooDataSet*) sigWeighted.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)>1.5 || abs(ph2_eta)>1.5) && (ph1_r9>0.94 || ph2_r9>0.94 ) "));
w->import(*signal[c],Rename(TString::Format("SigWeight_cat%d",c)));
cout << "cat " << c << ", signal[c]: " << endl;
signal[c]->Print("v");
cout << "---- for category " << c << ", nX for signal[c]: " << signal[c]->sumEntries() << endl;
cout << endl;
}
// Create full weighted signal data set without categorization
RooDataSet* signalAll = (RooDataSet*) sigWeighted.reduce(*w->var("PhotonsMass"),mainCut);
w->import(*signalAll, Rename("SigWeight"));
cout << "now signalAll" << endl;
signalAll->Print("v");
cout << "---- nX for signalAll: " << signalAll->sumEntries() << endl;
cout << endl;
}
// Data dataset
void AddBkgData(RooWorkspace* w) {
// initializations
Int_t ncat = NCAT;
Float_t minMassFit(140),maxMassFit(500);
// retrieve the data tree; no common preselection cut applied yet;
TString inDir = "";
TFile dataFile("histograms_CMS-HGG_19032013.root");
TTree* dataTree = (TTree*) dataFile.Get("Data");
// Variables
RooArgSet* ntplVars = defineVariables();
// common preselection cut
TString mainCut("PhotonsMass>=140 && PhotonsMass<=500");//livia
// Create dataset
RooDataSet Data("Data","dataset",dataTree,*ntplVars,mainCut,"evweight");
cout << endl;
cout << "Data, everything: " << endl;
Data.Print("v");
cout << "---- nX: " << Data.sumEntries() << endl;
cout << endl;
// split into NCAT categories
RooDataSet* dataToFit[NCAT];
for (int c=0; c<ncat; ++c) {
int theCat = c+1;
// 1) prime 4 cat livia
if (c==0) dataToFit[c] = (RooDataSet*) Data.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)<1.5 && abs(ph2_eta)<1.5) && (ph1_r9>0.94 && ph2_r9>0.94 )"));
if (c==1) dataToFit[c] = (RooDataSet*) Data.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)<1.5 && abs(ph2_eta)<1.5) && (ph1_r9<0.94 || ph2_r9<0.94 ) "));
if (c==2) dataToFit[c] = (RooDataSet*) Data.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)>1.5 || abs(ph2_eta)>1.5) && (ph1_r9>0.94 && ph2_r9>0.94 )"));
if (c==3) dataToFit[c] = (RooDataSet*) Data.reduce(*w->var("PhotonsMass"),mainCut+TString::Format("&& (abs(ph1_eta)>1.5 || abs(ph2_eta)>1.5) && (ph1_r9>0.94 || ph2_r9>0.94 ) "));
cout << endl; cout << "for category = " << c << endl;
dataToFit[c]->Print("v");
cout << "---- nX: " << dataToFit[c]->sumEntries() << endl;
w->import(*dataToFit[c],Rename(TString::Format("Data_cat%d",c)));
}
// Create full data set without categorization
RooDataSet* data = (RooDataSet*) Data.reduce(*w->var("PhotonsMass"),mainCut);
w->import(*data, Rename("Data"));
cout << endl;
cout << "data, no split" << endl;
data->Print("v");
cout << "---- nX: " << data->sumEntries() << endl;
}
// Fit signal with model pdfs
void SigModelFit(RooWorkspace* w, Float_t mass) {
Int_t ncat = NCAT;
RooDataSet* sigToFit[NCAT];
RooAbsPdf* PhotonsMassSig[NCAT];
Float_t MASS(mass);
Float_t minMassFit(mass-20),maxMassFit(mass+20);
// Fit Signal
for (int c=0; c<ncat; ++c) {
cout << "---------- Category = " << c << endl;
sigToFit[c] = (RooDataSet*) w->data(TString::Format("SigWeight_cat%d",c));
PhotonsMassSig[c] = (RooAbsPdf*) w->pdf("PhotonsMassSig"+TString::Format("_cat%d",c));
((RooRealVar*) w->var("PhotonsMass"+TString::Format("_sig_m0_cat%d",c)))->setVal(MASS);
cout << "---------------- Peak Val = "
<< w->var("PhotonsMass"+TString::Format("_sig_m0_cat%d",c))->getVal()
<< ", Mass = " << MASS << endl;
PhotonsMassSig[c] ->fitTo(*sigToFit[c],Range(minMassFit,maxMassFit),SumW2Error(kTRUE));
// Plot to verify everything is ok
RooPlot* plotPhotonsMassAll = w->var("PhotonsMass")->frame(Range(minMassFit-30,maxMassFit+30),Bins(100));
sigToFit[c]->plotOn(plotPhotonsMassAll);
PhotonsMassSig[c]->plotOn(plotPhotonsMassAll);
/* if (c==0) {
PhotonsMassSig[c]->plotOn(plotPhotonsMassAll,Components("PhotonsMassGaussSig_cat0"),LineStyle(kDashed),LineColor(kGreen));
PhotonsMassSig[c]->plotOn(plotPhotonsMassAll,Components("PhotonsMassxCBSig_cat0"),LineStyle(kDashed),LineColor(kRed));
} else if (c==1) {
PhotonsMassSig[c]->plotOn(plotPhotonsMassAll,Components("PhotonsMassGaussSig_cat1"),LineStyle(kDashed),LineColor(kGreen));
PhotonsMassSig[c]->plotOn(plotPhotonsMassAll,Components("PhotonsMassCBSig_cat1"),LineStyle(kDashed),LineColor(kRed));
}*/
// PhotonsMassSig[c]->paramOn(plotPhotonsMassAll, ShowConstants(true), Layout(0.15,0.55,0.9), Format("NEU",AutoPrecision(2)));
TCanvas* c1 = new TCanvas("c1","Massggnewvtx",0,0,500,500);
c1->cd(1);
plotPhotonsMassAll->Draw();
c1->SaveAs("prelimSignal"+TString::Format("_cat%d.png",c));
c1->SaveAs("prelimSignal"+TString::Format("_cat%d.root",c));
// IMPORTANT: fix all pdf parameters to constant
w->defineSet(TString::Format("SigPdfParam_cat%d",c), RooArgSet(*w->var("PhotonsMass"+TString::Format("_sig_m0_cat%d",c)),
*w->var("PhotonsMass"+TString::Format("_sig_sigma0_cat%d",c)),
*w->var("PhotonsMass"+TString::Format("_sig_m1_cat%d",c)),
*w->var("PhotonsMass"+TString::Format("_sig_sigma1_cat%d",c))));
SetConstantParams(w->set(TString::Format("SigPdfParam_cat%d",c)));
}
}
RooFitResult* BkgModelFitExpo(RooWorkspace* w, Bool_t dobands) {
Int_t ncat = NCAT;
RooDataSet* data[NCAT];
RooAbsPdf* PhotonsMassBkg[NCAT];
RooFitResult* fitresult[NCAT];;
RooPlot* plotPhotonsMassBkg[NCAT];
Float_t minMassFit(140),maxMassFit(500);
// Fit data with background pdf for data limit
RooRealVar* PhotonsMass = w->var("PhotonsMass");
PhotonsMass->setUnit("GeV");
for (int c=0; c<ncat; ++c) {
cout << "---------- category = " << c << endl;
data[c] = (RooDataSet*) w->data(TString::Format("Data_cat%d",c));
PhotonsMassBkg[c] = (RooAbsPdf*) w->pdf("PhotonsMassBkg"+TString::Format("_cat%d",c));
fitresult[c] = PhotonsMassBkg[c]->fitTo(*data[c], Strategy(1),Minos(kFALSE), Range(minMassFit,maxMassFit),SumW2Error(kTRUE), Save(kTRUE));
// Plot to verify everything is ok
plotPhotonsMassBkg[c] = w->var("PhotonsMass")->frame(Range(140,500),Bins(360));
data[c]->plotOn(plotPhotonsMassBkg[c]);
PhotonsMassBkg[c]->plotOn(plotPhotonsMassBkg[c]);
PhotonsMassBkg[c]->paramOn(plotPhotonsMassBkg[c], ShowConstants(true), Layout(0.15,0.55,0.9), Format("NEU",AutoPrecision(2)));
TCanvas* c1 = new TCanvas("c1","PhotonsMass",0,0,500,500);
c1->cd(1);
plotPhotonsMassBkg[c]->Draw();
c1->SaveAs("prelimBkg"+TString::Format("_cat%d.png",c));
c1->SaveAs("prelimBkg"+TString::Format("_cat%d.root",c));
//********************************************************************************//
if (dobands) {
RooAbsPdf *cpdf; cpdf = PhotonsMassBkg[c];
TGraphAsymmErrors *onesigma = new TGraphAsymmErrors();
TGraphAsymmErrors *twosigma = new TGraphAsymmErrors();
RooRealVar *nlim = new RooRealVar(TString::Format("nlim%d",c),"",0.0,0.0,10.0);
nlim->removeRange();
RooCurve *nomcurve = dynamic_cast<RooCurve*>(plotPhotonsMassBkg[c]->getObject(1));
for (int i=1; i<(plotPhotonsMassBkg[c]->GetXaxis()->GetNbins()+1); ++i) {
double lowedge = plotPhotonsMassBkg[c]->GetXaxis()->GetBinLowEdge(i);
double upedge = plotPhotonsMassBkg[c]->GetXaxis()->GetBinUpEdge(i);
double center = plotPhotonsMassBkg[c]->GetXaxis()->GetBinCenter(i);
double nombkg = nomcurve->interpolate(center);
nlim->setVal(nombkg);
PhotonsMass->setRange("errRange",lowedge,upedge);
RooAbsPdf *epdf = 0;
epdf = new RooExtendPdf("epdf","",*cpdf,*nlim,"errRange");
RooAbsReal *nll = epdf->createNLL(*(data[c]),Extended());
RooMinimizer minim(*nll);
minim.setStrategy(0);
double clone = 1.0 - 2.0*RooStats::SignificanceToPValue(1.0);
double cltwo = 1.0 - 2.0*RooStats::SignificanceToPValue(2.0);
minim.migrad();
minim.minos(*nlim);
// printf("errlo = %5f, errhi = %5f\n",nlim->getErrorLo(),nlim->getErrorHi());
onesigma->SetPoint(i-1,center,nombkg);
onesigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi());
minim.setErrorLevel(0.5*pow(ROOT::Math::normal_quantile(1-0.5*(1-cltwo),1.0), 2)); // the 0.5 is because qmu is -2*NLL
// eventually if cl = 0.95 this is the usual 1.92!
minim.migrad();
minim.minos(*nlim);
twosigma->SetPoint(i-1,center,nombkg);
twosigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi());
delete nll;
delete epdf;
}
PhotonsMass->setRange("errRange",minMassFit,maxMassFit);
twosigma->SetLineColor(kGreen);
twosigma->SetFillColor(kGreen);
twosigma->SetMarkerColor(kGreen);
twosigma->Draw("L3 SAME");
onesigma->SetLineColor(kYellow);
onesigma->SetFillColor(kYellow);
onesigma->SetMarkerColor(kYellow);
onesigma->Draw("L3 SAME");
plotPhotonsMassBkg[c]->Draw("SAME");
}
}
return fitresult;
}
RooFitResult* BkgModelFitBernstein(RooWorkspace* w, Bool_t dobands) {
Int_t ncat = NCAT;
// retrieve pdfs and datasets from workspace to fit with pdf models
RooDataSet* data[NCAT];
RooBernstein* MassggnewvtxBkg[NCAT];
RooFitResult* fitresult[NCAT];;
RooPlot* plotMassggnewvtxBkg[NCAT];
// dobands and dosignal
RooDataSet* signal[NCAT];
RooAbsPdf* MassggnewvtxSig[NCAT];
Float_t minMassFit(MMIN),maxMassFit(MMAX);
// Fit data with background pdf for data limit
RooRealVar* massggnewvtx = w->var("massggnewvtx");
massggnewvtx->setUnit("GeV");
TLatex *text = new TLatex();
text->SetNDC();
text->SetTextSize(0.04);
for (int c = 0; c < ncat; ++c) {
data[c] = (RooDataSet*) w->data(TString::Format("Data_cat%d",c));
// fit a la dijets
((RooRealVar*) w->var(TString::Format("massggnewvtx_bkg_8TeV_slope3_cat%d",c)))->setConstant(true);
cout << "---------------- Parameter 3 set to const" << endl;
RooFormulaVar *p1mod = new RooFormulaVar(TString::Format("p1mod_cat%d",c),"","@0",*w->var(TString::Format("massggnewvtx_bkg_8TeV_slope1_cat%d",c)));
RooFormulaVar *p2mod = new RooFormulaVar(TString::Format("p2mod_cat%d",c),"","@0",*w->var(TString::Format("massggnewvtx_bkg_8TeV_slope2_cat%d",c)));
RooFormulaVar *p3mod = new RooFormulaVar(TString::Format("p3mod_cat%d",c),"","@0",*w->var(TString::Format("massggnewvtx_bkg_8TeV_slope3_cat%d",c)));
RooFormulaVar *sqrtS = new RooFormulaVar(TString::Format("sqrtS_cat%d",c),"","@0",*w->var("sqrtS"));
RooFormulaVar *x = new RooFormulaVar(TString::Format("x_cat%d",c),"","@0/@1",RooArgList(*massggnewvtx, *sqrtS));
RooAbsPdf* MassggnewvtxBkgTmp0 = new RooGenericPdf(TString::Format("DijetBackground_%d",c), "pow(1-@0, @1)/pow(@0, @2+@3*log(@0))", RooArgList(*x, *p1mod, *p2mod, *p3mod));
w->factory(TString::Format("massggnewvtx_bkg_8TeV_norm_cat%d[4000.0,0.0,10000000]",c));
RooExtendPdf MassggnewvtxBkgTmp(TString::Format("MassggnewvtxBkg_cat%d",c),"",*MassggnewvtxBkgTmp0,*w->var(TString::Format("massggnewvtx_bkg_8TeV_norm_cat%d",c)));
fitresult[c] = MassggnewvtxBkgTmp.fitTo(*data[c], Strategy(1),Minos(kFALSE), Range(minMassFit,maxMassFit),SumW2Error(kTRUE), Save(kTRUE));
w->import(MassggnewvtxBkgTmp);
//************************************************
// Plot Massggnewvtx background fit results per categories
TCanvas* ctmp = new TCanvas("ctmp","Massggnewvtx Background Categories",0,0,500,500);
Int_t nBinsMass(19);
plotMassggnewvtxBkg[c] = massggnewvtx->frame(nBinsMass);
data[c]->plotOn(plotMassggnewvtxBkg[c],LineColor(kWhite),MarkerColor(kWhite));
MassggnewvtxBkgTmp.plotOn(plotMassggnewvtxBkg[c],LineColor(kBlue),Range("fitrange"),NormRange("fitrange"));
data[c]->plotOn(plotMassggnewvtxBkg[c]);
plotMassggnewvtxBkg[c]->Draw();
//********************************************************************************//
if (dobands) {
RooAbsPdf *cpdf; cpdf = MassggnewvtxBkgTmp0;
TGraphAsymmErrors *onesigma = new TGraphAsymmErrors();
TGraphAsymmErrors *twosigma = new TGraphAsymmErrors();
RooRealVar *nlim = new RooRealVar(TString::Format("nlim%d",c),"",0.0,0.0,10.0);
nlim->removeRange();
RooCurve *nomcurve = dynamic_cast<RooCurve*>(plotMassggnewvtxBkg[c]->getObject(1));
for (int i=1; i<(plotMassggnewvtxBkg[c]->GetXaxis()->GetNbins()+1); ++i) {
double lowedge = plotMassggnewvtxBkg[c]->GetXaxis()->GetBinLowEdge(i);
double upedge = plotMassggnewvtxBkg[c]->GetXaxis()->GetBinUpEdge(i);
double center = plotMassggnewvtxBkg[c]->GetXaxis()->GetBinCenter(i);
double nombkg = nomcurve->interpolate(center);
nlim->setVal(nombkg);
massggnewvtx->setRange("errRange",lowedge,upedge);
RooAbsPdf *epdf = 0;
epdf = new RooExtendPdf("epdf","",*cpdf,*nlim,"errRange");
RooAbsReal *nll = epdf->createNLL(*(data[c]),Extended());
RooMinimizer minim(*nll);
minim.setStrategy(0);
double clone = 1.0 - 2.0*RooStats::SignificanceToPValue(1.0);
double cltwo = 1.0 - 2.0*RooStats::SignificanceToPValue(2.0);
minim.migrad();
minim.minos(*nlim);
// printf("errlo = %5f, errhi = %5f\n",nlim->getErrorLo(),nlim->getErrorHi());
onesigma->SetPoint(i-1,center,nombkg);
onesigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi());
minim.setErrorLevel(0.5*pow(ROOT::Math::normal_quantile(1-0.5*(1-cltwo),1.0), 2)); // the 0.5 is because qmu is -2*NLL
// eventually if cl = 0.95 this is the usual 1.92!
minim.migrad();
minim.minos(*nlim);
twosigma->SetPoint(i-1,center,nombkg);
twosigma->SetPointError(i-1,0.,0.,-nlim->getErrorLo(),nlim->getErrorHi());
delete nll;
delete epdf;
}
massggnewvtx->setRange("errRange",minMassFit,maxMassFit);
twosigma->SetLineColor(kGreen);
twosigma->SetFillColor(kGreen);
twosigma->SetMarkerColor(kGreen);
twosigma->Draw("L3 SAME");
onesigma->SetLineColor(kYellow);
onesigma->SetFillColor(kYellow);
onesigma->SetMarkerColor(kYellow);
onesigma->Draw("L3 SAME");
plotMassggnewvtxBkg[c]->Draw("SAME");
}
}
return fitresult;
}
void SetConstantParams(const RooArgSet* params) {
cout << endl; cout << "Entering SetConstantParams" << endl;
TIterator* iter(params->createIterator());
for (TObject *a = iter->Next(); a != 0; a = iter->Next()) {
RooRealVar *rrv = dynamic_cast<RooRealVar *>(a);
if (rrv) { rrv->setConstant(true); std::cout << " " << rrv->GetName(); }
}
}
void MakePlots(RooWorkspace* w, Float_t mass, RooFitResult* fitresults) {
Int_t ncat = NCAT;
cout << endl; cout << "Retreive everything:" << endl;
w->Print();
// retrieve data sets from the workspace
RooDataSet* dataAll = (RooDataSet*) w->data("Data");
RooDataSet* signalAll = (RooDataSet*) w->data("SigWeight");
// maximum 9 cat...
RooDataSet* data[9];
RooDataSet* signal[9];
RooAbsPdf* PhotonsMassGaussSig[9];
RooAbsPdf* PhotonsMassGaussSig_bis[9];
RooAbsPdf* PhotonsMassSig[9];
RooAbsPdf* PhotonsMassBkg[9];
for (int c=0; c<ncat; ++c) {
data[c] = (RooDataSet*) w->data(TString::Format("Data_cat%d",c));
signal[c] = (RooDataSet*) w->data(TString::Format("SigWeight_cat%d",c));
PhotonsMassGaussSig[c] = (RooAbsPdf*) w->pdf(TString::Format("PhotonsMassGaussSig_cat%d",c));
PhotonsMassGaussSig_bis[c] = (RooAbsPdf*) w->pdf(TString::Format("PhotonsMassGaussSig_cat%d_bis",c));
PhotonsMassSig[c] = (RooAbsPdf*) w->pdf("PhotonsMassSig"+TString::Format("_cat%d",c));
PhotonsMassBkg[c] = (RooAbsPdf*) w->pdf(TString::Format("PhotonsMassBkg_cat%d",c));
}
// retrieve mass observable from the workspace
RooRealVar* PhotonsMass = w->var("PhotonsMass");
PhotonsMass->setUnit("GeV");
// retrieve pdfs after the fits
RooAbsPdf* PhotonsMassGaussSigAll = w->pdf("PhotonsMassGaussSig");
RooAbsPdf* PhotonsMassGaussSigAll_bis = w->pdf("PhotonsMassGaussSig_bis");
RooAbsPdf* PhotonsMassSigAll = w->pdf("PhotonsMassSig");
RooAbsPdf* PhotonsMassBkgAll = w->pdf("PhotonsMassBkgAll");
Float_t minMassFit(mass-20),maxMassFit(mass+20);
Float_t MASS(mass);
int iMass = abs(mass);
Int_t nBinsMass(40); // chiara
// ****************************
cout << endl; cout << "Progress plotting: signal" << endl;
RooPlot* plotPhotonsMassAll = PhotonsMass->frame(Range(minMassFit,maxMassFit),Bins(nBinsMass));
signalAll->plotOn(plotPhotonsMassAll);
gStyle->SetOptTitle(0);
PhotonsMassSigAll->plotOn(plotPhotonsMassAll);
PhotonsMassSigAll->plotOn(plotPhotonsMassAll,Components("PhotonsMassGaussSig"),LineStyle(kDashed),LineColor(kGreen));
PhotonsMassSigAll->plotOn(plotPhotonsMassAll,Components("PhotonsMassGaussSig_bis"),LineStyle(kDashed),LineColor(kRed));
// PhotonsMassSigAll->paramOn(plotPhotonsMassAll, ShowConstants(true), Layout(0.15,0.55,0.9), Format("NEU",AutoPrecision(2)));
plotPhotonsMassAll->getAttText()->SetTextSize(0.03);
TCanvas* c1 = new TCanvas("c1","PhotonsMass",0,0,500,500);
TPaveText* label_cms = get_labelCMS(0, "2012", true);
TPaveText* label_sqrt = get_labelSqrt(0);
c1->cd(1);
plotPhotonsMassAll->Draw();
label_cms->Draw("same");
label_sqrt->Draw("same");
c1->SaveAs("plots/sigmodel_"+TString::Format("%d.png", iMass));
c1->SaveAs("plots/sigmodel_"+TString::Format("%d.root", iMass));
// ****************************
cout << endl; cout << "Progress plotting: signal per categories" << endl;
TLatex *text = new TLatex();
text->SetNDC();
text->SetTextSize(0.04);
RooPlot* plotPhotonsMass[9];
for (int c=0; c<ncat; ++c) {
plotPhotonsMass[c] = PhotonsMass->frame(Range(minMassFit,maxMassFit),Bins(nBinsMass));
signal[c]->plotOn(plotPhotonsMass[c],LineColor(kWhite),MarkerColor(kWhite));
PhotonsMassSig[c] ->plotOn(plotPhotonsMass[c]);
PhotonsMassSig[c] ->plotOn(plotPhotonsMass[c],Components("PhotonsMassGaussSig"+TString::Format("_cat%d",c)),LineStyle(kDashed),LineColor(kGreen));
PhotonsMassSig[c] ->plotOn(plotPhotonsMass[c],Components("PhotonsMassGaussSig"+TString::Format("_cat%d_bis",c)),LineStyle(kDashed),LineColor(kRed));
// PhotonsMassSig[c] ->paramOn(plotPhotonsMass[c], ShowConstants(true), Layout(0.15,0.55,0.9), Format("NEU",AutoPrecision(2)));
signal[c] ->plotOn(plotPhotonsMass[c]);
TCanvas* dummy = new TCanvas("dummy", "dummy",0, 0, 400, 400);
TH1F *hist = new TH1F("hist", "hist", 400, minMassFit, maxMassFit);
plotPhotonsMass[c]->SetTitle("");
plotPhotonsMass[c]->SetMinimum(0.0);
plotPhotonsMass[c]->SetMaximum(1.40*plotPhotonsMass[c]->GetMaximum());
plotPhotonsMass[c]->GetXaxis()->SetTitle("m_{#gamma#gamma} (GeV)");
TCanvas* ctmp = new TCanvas("ctmp","PhotonsMass Background Categories",0,0,500,500);
plotPhotonsMass[c]->Draw();
plotPhotonsMass[c]->Draw("SAME");
label_cms->Draw("same");
label_sqrt->Draw("same");
TLegend *legmc = new TLegend(0.62,0.75,0.92,0.9);
legmc->AddEntry(plotPhotonsMass[c]->getObject(5),"Simulation","LPE");
legmc->AddEntry(plotPhotonsMass[c]->getObject(1),"Parametric Model","L");
legmc->AddEntry(plotPhotonsMass[c]->getObject(3),"Gaussian 2 component","L");
legmc->AddEntry(plotPhotonsMass[c]->getObject(2),"Gaussian 1 component","L");
legmc->SetBorderSize(0);
legmc->SetFillStyle(0);
legmc->Draw();
TLatex *lat = new TLatex(minMassFit+1.5,0.85*plotPhotonsMass[c]->GetMaximum(),"#scale[1.0]{CMS Preliminary}");
lat->Draw();
ctmp->SaveAs("plots/sigmodel_"+TString::Format("%d_cat%d.png", iMass, c));
ctmp->SaveAs("plots/sigmodel_"+TString::Format("%d_cat%d.root", iMass, c));
}
// ****************************
cout << endl; cout << "Progress plotting: background" << endl;
TCanvas* c4 = new TCanvas("c4","PhotonsMass Background Categories",0,0,400,400);
RooPlot* plotPhotonsMassBkg[9];
for (int c=0; c<ncat; ++c) {
plotPhotonsMassBkg[c] = PhotonsMass->frame(nBinsMass);
data[c]->plotOn(plotPhotonsMassBkg[c],LineColor(kWhite),MarkerColor(kWhite));
PhotonsMassBkg[c]->plotOn(plotPhotonsMassBkg[c],LineColor(kBlue),Range("fitrange"),NormRange("fitrange"));
data[c]->plotOn(plotPhotonsMassBkg[c]);
//PhotonsMassBkg[c]->paramOn(plotPhotonsMassBkg[c], ShowConstants(true), Layout(0.65,0.9,0.9), Format("NEU",AutoPrecision(4)));
plotPhotonsMassBkg[c]->getAttText()->SetTextSize(0.03);
plotPhotonsMassBkg[c]->Draw();
gPad->SetLogy(1);
plotPhotonsMassBkg[c]->SetAxisRange(0.1,plotPhotonsMassBkg[c]->GetMaximum()*1.5,"Y");
label_cms->Draw("same");
label_sqrt->Draw("same");
c4->SaveAs("plots/backgrounds_log_"+TString::Format("cat%d.png", c));
c4->SaveAs("plots/backgrounds_log_"+TString::Format("cat%d.root", c));
}
TCanvas* c5 = new TCanvas("c5","PhotonsMass Background Categories",0,0,400,400);
RooPlot* plotPhotonsMassBkg[9];
for (int c=0; c<ncat; ++c) {
plotPhotonsMassBkg[c] = PhotonsMass->frame(nBinsMass);
data[c]->plotOn(plotPhotonsMassBkg[c],LineColor(kWhite),MarkerColor(kWhite));
PhotonsMassBkg[c]->plotOn(plotPhotonsMassBkg[c],LineColor(kBlue),Range("fitrange"),NormRange("fitrange"));
data[c]->plotOn(plotPhotonsMassBkg[c]);
//PhotonsMassBkg[c]->paramOn(plotPhotonsMassBkg[c], ShowConstants(true), Layout(0.65,0.9,0.9), Format("NEU",AutoPrecision(4)));
plotPhotonsMassBkg[c]->getAttText()->SetTextSize(0.03);
plotPhotonsMassBkg[c]->Draw();
c5->SaveAs("plots/backgrounds_"+TString::Format("cat%d.png", c));
c5->SaveAs("plots/backgrounds_"+TString::Format("cat%d.root", c));
}
}
// Write signal pdfs and datasets into the workspace
void MakeSigWS(RooWorkspace* w, const char* fileBaseName){
TString wsDir = "workspaces/"+filePOSTfix;
Int_t ncat = NCAT;
RooWorkspace *wAll = new RooWorkspace("w_all","w_all");
//********************************//
// Retrieve P.D.F.s
RooAbsPdf* PhotonsMassSigPdf[NCAT];
for (int c=0; c<ncat; ++c) {
PhotonsMassSigPdf[c] = (RooAbsPdf*) w->pdf("PhotonsMassSig"+TString::Format("_cat%d",c));
wAll->import(*w->pdf("PhotonsMassSig"+TString::Format("_cat%d",c)));
}
std::cout << "done with importing signal pdfs" << std::endl;
// (2) Systematics on energy scale and resolution // chiara: per ora tutte le sistematiche non hanno senso
// wAll->factory("CMS_hgg_sig_m0_absShift[1,1.0,1.0]");
// wAll->factory("CMS_hgg_sig_m0_absShift_cat0[1,1.0,1.0]");
// wAll->factory("CMS_hgg_sig_m0_absShift_cat1[1,1.0,1.0]");
// wAll->factory("prod::CMS_hgg_sig_m0_cat0(massggnewvtx_sig_m0_cat0, CMS_hgg_sig_m0_absShift)");
// wAll->factory("prod::CMS_hgg_sig_m0_cat1(massggnewvtx_sig_m0_cat1, CMS_hgg_sig_m0_absShift)");
// (3) Systematics on resolution: create new sigmas
// wAll->factory("CMS_hgg_sig_sigmaScale[1,1.0,1.0]");
// wAll->factory("CMS_hgg_sig_sigmaScale_cat0[1,1.0,1.0]");
// wAll->factory("CMS_hgg_sig_sigmaScale_cat1[1,1.0,1.0]");
// wAll->factory("prod::CMS_hgg_sig_sigma_cat0(massggnewvtx_sig_sigma0_cat0, CMS_hgg_sig_sigmaScale)");
// wAll->factory("prod::CMS_hgg_sig_gsigma_cat0(massggnewvtx_sig_sigma1_cat0, CMS_hgg_sig_sigmaScale)");
// wAll->factory("prod::CMS_hgg_sig_sigma_cat1(massggnewvtx_sig_sigma0_cat1, CMS_hgg_sig_sigmaScale)");
// wAll->factory("prod::CMS_hgg_sig_gsigma_cat1(massggnewvtx_sig_sigma1_cat1, CMS_hgg_sig_sigmaScale)")
TString filename(wsDir+TString(fileBaseName)+".inputsig.root");
wAll->writeToFile(filename);
cout << "Write signal workspace in: " << filename << " file" << endl;
return;
}
// Write background pdfs and datasets into the workspace
void MakeBkgWS(RooWorkspace* w, const char* fileBaseName) {
TString wsDir = "workspaces/"+filePOSTfix;
Int_t ncat = NCAT;
RooWorkspace *wAll = new RooWorkspace("w_all","w_all");
//********************************//
// Retrieve the datasets and PDFs
RooDataSet* data[NCAT];
RooAbsPdf* PhotonsMassBkgPdf[NCAT];
for (int c=0; c<ncat; ++c) {
/*
cout << "For category " << c << endl;
data[c] = (RooDataSet*) w->data(TString::Format("Data_cat%d",c));
((RooRealVar*) data[c]->get()->find("massggnewvtx"))->setBins(MMAX-MMIN) ;
RooDataHist* dataBinned = data[c]->binnedClone();
MassggnewvtxBkgPdf[c] = (RooExtendPdf*) w->pdf(TString::Format("MassggnewvtxBkg_cat%d",c));
// wAll->import(*data[c], Rename(TString::Format("data_obs_cat%d",c)));
wAll->import(*dataBinned, Rename(TString::Format("data_obs_cat%d",c)));
wAll->import(*w->pdf(TString::Format("MassggnewvtxBkg_cat%d",c)));
double mean = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_norm_cat%d",c))->getVal();
double min = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_norm_cat%d",c))->getMin();
double max = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_norm_cat%d",c))->getMax();
wAll->factory(TString::Format("CMS_hgg_bkg_8TeV_cat%d_norm[%g,%g,%g]", c, mean, min, max));
double mean = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope1_cat%d",c))->getVal();
double min = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope1_cat%d",c))->getMin();
double max = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope1_cat%d",c))->getMax();
wAll->factory(TString::Format("CMS_hgg_bkg_8TeV_slope1_cat%d[%g,%g,%g]", c, mean, min, max));
double mean = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope2_cat%d",c))->getVal();
double min = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope2_cat%d",c))->getMin();
double max = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope2_cat%d",c))->getMax();
wAll->factory(TString::Format("CMS_hgg_bkg_8TeV_slope2_cat%d[%g,%g,%g]", c, mean, min, max));
double mean = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope3_cat%d",c))->getVal();
double min = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope3_cat%d",c))->getMin();
double max = wAll->var(TString::Format("massggnewvtx_bkg_8TeV_slope3_cat%d",c))->getMax();
wAll->factory(TString::Format("CMS_hgg_bkg_8TeV_slope3_cat%d[%g,%g,%g]", c, mean, mean, mean));
*/
PhotonsMassBkgPdf[c] = (RooAbsPdf*) w->pdf("PhotonsMassBkg"+TString::Format("_cat%d",c));
data[c] = (RooDataSet*) w->data(TString::Format("Data_cat%d",c));
((RooRealVar*) data[c]->get()->find("PhotonsMass"))->setBins(500-140) ;
RooDataHist* dataBinned = data[c]->binnedClone();
wAll->import(*w->pdf("PhotonsMassBkg"+TString::Format("_cat%d",c)));
wAll->import(*dataBinned, Rename(TString::Format("data_obs_cat%d",c)));
}
std::cout << "done with importing background pdfs" << std::endl;
// (2) do reparametrization of background
// for (int c=0; c<ncat; ++c) {
//wAll->factory(
// TString::Format("EDIT::CMS_hgg_bkg_8TeV_cat%d(MassggnewvtxBkg_cat%d,",c,c) +
// TString::Format(" massggnewvtx_bkg_exp_cat%d=CMS_hgg_bkg_8TeV_cat%d_norm,", c,c)
// );
//}
TString filename(wsDir+TString(fileBaseName)+".root");
wAll->writeToFile(filename);
cout << "Write background workspace in: " << filename << " file" << endl;
std::cout << std::endl;
std::cout << "observation:" << std::endl;
for (int c=0; c<ncat; ++c) {
std::cout << "cat " << c << ", " << wAll->data(TString::Format("data_obs_cat%d",c))->sumEntries() << endl;
wAll->data(TString::Format("data_obs_cat%d",c))->Print();
}
std::cout << std::endl;
return;
}
Double_t effSigma(TH1 *hist) {
TAxis *xaxis = hist->GetXaxis();
Int_t nb = xaxis->GetNbins();
if(nb < 10) {
std::cout << "effsigma: Not a valid histo. nbins = " << nb << std::endl;
return 0.;
}
Double_t bwid = xaxis->GetBinWidth(1);
if(bwid == 0) {
std::cout << "effsigma: Not a valid histo. bwid = " << bwid << std::endl;
return 0.;
}
Double_t xmax = xaxis->GetXmax();
Double_t xmin = xaxis->GetXmin();
Double_t ave = hist->GetMean();
Double_t rms = hist->GetRMS();
Double_t total=0.;
for(Int_t i=0; i<nb+2; i++) {
total+=hist->GetBinContent(i);
}
if(total < 100.) {
std::cout << "effsigma: Too few entries " << total << std::endl;
return 0.;
}
Int_t ierr=0;
Int_t ismin=999;
Double_t rlim=0.683*total;
Int_t nrms=rms/(bwid); // Set scan size to +/- rms
if(nrms > nb/10) nrms=nb/10; // Could be tuned...
Double_t widmin=9999999.;
for(Int_t iscan=-nrms;iscan<nrms+1;iscan++) { // Scan window centre
Int_t ibm=(ave-xmin)/bwid+1+iscan;
Double_t x=(ibm-0.5)*bwid+xmin;
Double_t xj=x;
Double_t xk=x;
Int_t jbm=ibm;
Int_t kbm=ibm;
Double_t bin=hist->GetBinContent(ibm);
total=bin;
for(Int_t j=1;j<nb;j++){
if(jbm < nb) {
jbm++;
xj+=bwid;
bin=hist->GetBinContent(jbm);
total+=bin;
if(total > rlim) break;
}
else ierr=1;
if(kbm > 0) {
kbm--;
xk-=bwid;
bin=hist->GetBinContent(kbm);
total+=bin;
if(total > rlim) break;
}
else ierr=1;
}
Double_t dxf=(total-rlim)*bwid/bin;
Double_t wid=(xj-xk+bwid-dxf)*0.5;
if(wid < widmin) {
widmin=wid;
ismin=iscan;
}
}
if(ismin == nrms || ismin == -nrms) ierr=3;
if(ierr != 0) std::cout << "effsigma: Error of type " << ierr << std::endl;
return widmin;
}
// preparing datacards
void MakeDataCard_1Channel(RooWorkspace* w, const char* fileBaseName, const char* fileBkgName, int iChan) {
TString cardDir = "datacards/"+filePOSTfix;
Int_t ncat = NCAT;
TString wsDir = "../workspaces/"+filePOSTfix;
// **********************
// Retrieve the datasets
cout << "Start retrieving dataset" << endl;