forked from h2gglobe/h2gglobe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RooContainer.cc
3330 lines (2770 loc) · 130 KB
/
RooContainer.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
/******************************************************
RooContainer.cc
Original Author - Nicholas Wardle - Imperial College
*******************************************************/
#include "RooContainer.h"
#include "RooMsgService.h"
using namespace RooFit;
RooContainer::RooContainer(int n, int s):ncat(n),nsigmas(s),make_systematics(false),save_systematics_data(false),verbosity_(false),fit_systematics(false),save_roodatahists(true){
// Set up the arrays which may be needed
signalVector1 = new double[25];
backgroundVector1 = new double[25];
blind_data = true;
}
RooContainer::~RooContainer(){
delete [] signalVector1;
delete [] backgroundVector1;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::SetNCategories(int n){
ncat = n;
}
void RooContainer::Verbose(bool noisy){
verbosity_=noisy;
if (!noisy) RooMsgService::instance().setGlobalKillBelow(RooFit::WARNING);
}
void RooContainer::AddGlobalSystematic(std::string name,double val_sig, double val_bkg){
global_systematics_[name] = std::pair<double,double>(val_sig,val_bkg);
}
void RooContainer::AddNormalisationSystematics(std::string name,std::vector<std::pair<double,double> > vals, int type){
if (vals.size() != ncat) {
std::cerr << "WARNING! -- RooContainer::AddNormalisationsSystematics -- expected input of errors"
<< "to be equal in size to number of categories=" << ncat << std::endl;
} else {
std::vector<std::pair<double,double> > sys;
if (type ==-1){ // Signal Systematics
for (int cat=0;cat<ncat;cat++){
double value = 1.+(vals[cat].second/vals[cat].first);
sys.push_back(std::pair<double,double> (value,1.0));
}
} else if (type ==1){ // Background Systematics
for (int cat=0;cat<ncat;cat++){
double value = 1.+(vals[cat].second/vals[cat].first);
sys.push_back(std::pair<double,double> (1.0,value));
}
} else if (type ==0){ // Both
for (int cat=0;cat<ncat;cat++){
double value = 1.+(vals[cat].second/vals[cat].first);
sys.push_back(std::pair<double,double> (value,value));
}
} else std::cerr << "WARNING! -- RooContainer::AddNormalisationsSystematics -- type not understood "
<< type << std::endl;
normalisation_systematics_.insert(std::pair <std::string,std::vector <std::pair< double, double > > > (name,sys));
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::SaveSystematicsData(bool save){
save_systematics_data = save;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::BlindData(bool blind){
blind_data = blind;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::MakeSystematicPdfs(bool save){
fit_systematics = save;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::SaveRooDataHists(bool save){
save_roodatahists = save;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::MakeSystematicStudy(std::vector<std::string> sys_names,std::vector<int> sys_types){
make_systematics = true;
std::vector<std::string>::iterator it = sys_names.begin();
std::vector<int>::iterator it_typ = sys_types.begin();
for (;it!=sys_names.end();it++,it_typ++)
systematics_.insert(std::pair<std::string,int>(*it,*it_typ));
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddConstant(std::string name,double init){
RooRealVar temp(name.c_str(),name.c_str(),init,init,init);
ws.import(temp,Silence());
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddObservable(std::string name,double xmin,double xmax){
addRealVar(name,xmin,xmax);
m_real_var_[name].setRange("FullObservableRange",xmin,xmax);
addRealVar(getweightName(name),0.,1.0e6);
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddRealVar(std::string name,double init,double xmin, double xmax){
for (int cat=0;cat<ncat;cat++){
addRealVar(getcatName(name,cat),init,xmin,xmax);
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++)
addRealVar(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),init,xmin,xmax);
for (int sys=1;sys<=nsigmas;sys++)
addRealVar(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),init,xmin,xmax);
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddFormulaVar(std::string name,std::string formula,std::string var){
for (int cat=0;cat<ncat;cat++){
addFormulaVar(getcatName(name,cat),formula,getcatName(var,cat));
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++)
addFormulaVar(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),formula,getsysindexName(getcatName(var,cat),(it_sys->first),sys,-1));
for (int sys=1;sys<=nsigmas;sys++)
addFormulaVar(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),formula,getsysindexName(getcatName(var,cat),(it_sys->first),sys,1));
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddSpecificCategoryPdf(int *categories,std::string name,std::string formula,std::string obs_name
,std::vector<std::string> & var, int form
,double norm_guess, double norm_min, double norm_max){
for (int cat=0;cat<ncat;cat++){
if (!categories[cat]) continue;
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getcatName(*it,cat));
}
addGenericPdf(getcatName(name,cat),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,-1));
}
addGenericPdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
}
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,1));
}
addGenericPdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
}
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::AddGenericPdf(std::string name,std::string formula,std::string obs_name
,std::vector<std::string> & var, int form
,double norm_guess, double norm_min, double norm_max){
for (int cat=0;cat<ncat;cat++){
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getcatName(*it,cat));
}
addGenericPdf(getcatName(name,cat),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,-1));
}
addGenericPdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
}
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_var;
for (std::vector<std::string>::iterator it=var.begin()
;it!=var.end()
;++it){
cat_var.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,1));
}
addGenericPdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),formula,obs_name,cat_var,form,norm_guess,norm_min,norm_max);
}
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::ComposeSpecificCategoryPdf(int *categories,std::string name, std::string composition
,std::vector<std::string> & formula, bool use_extended){
for (int cat=0;cat<ncat;cat++){
if (!categories[cat]) continue;
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getcatName(*it,cat));
}
composePdf(getcatName(name,cat),composition,cat_formula,use_extended);
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,-1));
}
composePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),composition,cat_formula,use_extended);
}
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,1));
}
composePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),composition,cat_formula,use_extended);
}
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::ComposePdf(std::string name, std::string composition
,std::vector<std::string> & formula, bool use_extended){
for (int cat=0;cat<ncat;cat++){
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getcatName(*it,cat));
}
composePdf(getcatName(name,cat),composition,cat_formula,use_extended);
if (make_systematics && fit_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,-1));
}
composePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),composition,cat_formula,use_extended);
}
for (int sys=1;sys<=nsigmas;sys++){
std::vector<std::string> cat_formula;
for (std::vector<std::string>::iterator it=formula.begin()
;it!=formula.end()
;++it){
cat_formula.push_back(getsysindexName(getcatName(*it,cat),(it_sys->first),sys,1));
}
composePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),composition,cat_formula,use_extended);
}
}
}
}
}
// ----------------------------------------------------------------------------------------------------
/*
void RooContainer::ConvolutePdf(std::string name, std::string f_pdf, std::string g_pdf, std::string observable, double norm_guess){
std::map<std::string,RooRealVar>::iterator obs = m_real_var_.find(observable);
if (obs == m_real_var_.end()){
std::cerr << "WARNING -- RooContainer::ConvolutePdf -- No Observable found named "
<< observable << std::endl;
return;
}
for (int cat=0;cat<ncat;cat++){
convolutePdf(getcatName(name,cat),getcatName(f_pdf,cat),getcatName(g_pdf,cat),(*obs).second,norm_guess);
if (make_systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
convolutePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,-1),getsysindexName(getcatName(f_pdf,cat),it_sys->first,sys,-1)
,getsysindexName(getcatName(g_pdf,cat),(it_sys->first),sys,-1),(*obs).second,norm_guess);
}
for (int sys=1;sys<=nsigmas;sys++){
convolutePdf(getsysindexName(getcatName(name,cat),(it_sys->first),sys,1),getsysindexName(getcatName(f_pdf,cat),it_sys->first,sys,1)
,getsysindexName(getcatName(g_pdf,cat),(it_sys->first),sys,1),(*obs).second,norm_guess);
}
}
}
}
}
*/
// ----------------------------------------------------------------------------------------------------
void RooContainer::CreateDataSet(std::string name,std::string data_name,int nbins, double x1, double x2){
for (int cat=0;cat<ncat;cat++){
std::string cat_name = getcatName(data_name,cat);
createDataSet(name,cat_name,nbins,x1,x2);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::GenerateBinnedPdf(std::string hist_name,std::string pdf_name,std::string data_name,int effect_type,int nbins,int mode,double x1,double x2){
for (int cat=0;cat<ncat;cat++){
std::map<std::string,RooRealVar*>::iterator obs_var = m_data_var_ptr_.find(getcatName(data_name,cat));
if (obs_var == m_data_var_ptr_.end()) {
std::cerr << "WARNING!!! -- RooContainer::GenerateBinnedPdf -- No Dataset named "
<< data_name << std::endl;
return;
}
std::string obs_name = data_obs_names_[getcatName(data_name,cat)];
generateBinnedPdf(cat,hist_name,getcatName(hist_name,cat),getcatName(pdf_name,cat),obs_name,obs_var->second,data_[getcatName(data_name,cat)],effect_type,nbins,mode,x1,x2);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::MakeSystematics(std::string observable,std::string s_name, int effect){
std::map<std::string,RooRealVar>::iterator obs = m_real_var_.find(observable);
if (obs == m_real_var_.end()){
std::cerr << "RooContainer::MakeSystematics -- WARNING!!! -- No Observable named "
<< observable << std::endl;
return;
}
for (int cat=0;cat<ncat;cat++){
makeSystematics(observable,getcatName(s_name,cat),effect);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToData(std::string name_func, std::string name_var,double x1, double x2, double x3, double x4){
for (int cat=0;cat<ncat;cat++){
fitToData(getcatName(name_func,cat),getcatName(name_var,cat),getcatName(name_var,cat)
,x1,x2,x3,x4);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToData(std::string name_func, std::string name_var,double x1, double x2){
for (int cat=0;cat<ncat;cat++){
fitToData(getcatName(name_func,cat),getcatName(name_var,cat),getcatName(name_var,cat)
,x1,x2,-999,-999);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToData(std::string name_func, std::string name_var){
for (int cat=0;cat<ncat;cat++){
fitToData(getcatName(name_func,cat),getcatName(name_var,cat),getcatName(name_var,cat)
,-999,-999,-999,-999);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToSystematicSet(std::string name_func,std::string name_var
,std::string sys_name,double x1,double x2,double x3,double x4){
for (int cat=0;cat<ncat;cat++) {
fitToSystematicSet(getcatName(name_func,cat),getcatName(name_var,cat)
,sys_name,x1,x2,x3,x4);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToSystematicSet(std::string name_func,std::string name_var
,std::string sys_name,double x1,double x2){
for (int cat=0;cat<ncat;cat++) {
fitToSystematicSet(getcatName(name_func,cat),getcatName(name_var,cat)
,sys_name,x1,x2,-999,-999);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::FitToSystematicSet(std::string name_func,std::string name_var
,std::string sys_name){
for (int cat=0;cat<ncat;cat++) {
fitToSystematicSet(getcatName(name_func,cat),getcatName(name_var,cat)
,sys_name,-999,-999,-999,-999);
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::Save(){
std::cout << "RooContainer::Save -- Saving To File "
<< std::endl;
std::cout << "RooContainer::Save -- Saving Plots "
<< std::endl;
/*
std::map<RooPlot*,double>::iterator it;
for(it = fit_res_.begin()
;it != fit_res_.end()
;it++ ){
writeRooPlot((*it).first,(*it).second);
}
*/
std::vector<TCanvas*>::iterator it;
for(it = fit_canvases_.begin();it != fit_canvases_.end();it++ ) (*it)->Write();
std::cout << "RooContainer::Save -- Saving Pdfs "
<< std::endl;
// check first and remove duplicates of the pointers.
removeDuplicateElements(pdf_saves_);
std::vector<RooAbsPdf*>::iterator it_pdf;
for(it_pdf = pdf_saves_.begin()
;it_pdf != pdf_saves_.end()
;it_pdf++ ){
ws.import(**it_pdf,Silence());
}
/*
std::map<std::string,RooAbsPdf*>::iterator it_gen;
for(it_gen = m_gen_.begin()
;it_gen != m_gen_.end()
;it_gen++ ){
ws.import(*(it_gen->second));
}
*/
for (std::map<std::string,RooDataSet>::iterator it_data = data_.begin()
;it_data!=data_.end();it_data++) {
ws.import(it_data->second);
}
std::cout << "RooContainer::Save -- Saving Histos "
<< std::endl;
std::map<std::string,TH1F>::iterator it_d = m_th1f_.begin();
std::map<std::string,TH1F>::iterator it_e = m_th1f_.end();
for(;it_d != it_e; it_d++){
writeRooDataHist((*it_d).first,&((*it_d).second));
}
ws.SetName("cms_hgg_workspace");
// Make sure all parameters of the pdfs are set constant
//setAllParametersConstant();
ws.Write();
}
std::vector< std::pair<double,double> > RooContainer::GetFitNormalisationsAndErrors(std::string pdf_name, std::string data_name, double r1,double r2, bool external_fit){
std::vector< std::pair< double,double> > normalisations;
for (int cat=0;cat<ncat;cat++){
std::map<std::string,RooRealVar*>::iterator obs_var = m_data_var_ptr_.find(getcatName(data_name,cat));
if (obs_var == m_data_var_ptr_.end()) {
std::cerr << "WARNING!!! -- RooContainer::GetFitNormalisations -- No Dataset named "
<< data_name << std::endl;
} else {
bool multi_pdf;
RooAbsPdf *pdf_ptr;
std::map<std::string,RooExtendPdf>::iterator exp = m_exp_.find(getcatName(pdf_name,cat));
if (exp != m_exp_.end()) {
pdf_ptr = &(exp->second);
multi_pdf = false;
} else {
std::map<std::string,RooAddPdf>::iterator pdf = m_pdf_.find(getcatName(pdf_name,cat));
if (pdf != m_pdf_.end()) {
multi_pdf = true;
pdf_ptr = &(pdf->second);
}
else {
std::cerr << "WARNING!!! -- RooContainer::GetFitNormalisations -- No Pdf named "
<< pdf_name << std::endl;
}
}
std::cout << getcatName(pdf_name,cat)<< std::endl;
std::string obs_name = data_obs_names_[getcatName(data_name,cat)];
// Just use a weird name (getcatName(obs_name)) for the histogram since we dont need it
normalisations.push_back(getNormalisationAndErrorFromFit(getcatName(pdf_name,cat),getcatName(obs_name,cat),pdf_ptr,obs_var->second,r1,r2,multi_pdf,external_fit));
}
}
return normalisations;
}
std::vector<double> RooContainer::GetFitNormalisations(std::string pdf_name, std::string data_name, double r1,double r2, bool external_fit){
std::vector<double> normalisations;
for (int cat=0;cat<ncat;cat++){
std::map<std::string,RooRealVar*>::iterator obs_var = m_data_var_ptr_.find(getcatName(data_name,cat));
if (obs_var == m_data_var_ptr_.end()) {
std::cerr << "WARNING!!! -- RooContainer::GetFitNormalisations -- No Dataset named "
<< data_name << std::endl;
} else {
bool multi_pdf;
RooAbsPdf *pdf_ptr;
std::map<std::string,RooExtendPdf>::iterator exp = m_exp_.find(getcatName(pdf_name,cat));
if (exp != m_exp_.end()) {
pdf_ptr = &(exp->second);
multi_pdf = false;
} else {
std::map<std::string,RooAddPdf>::iterator pdf = m_pdf_.find(getcatName(pdf_name,cat));
if (pdf != m_pdf_.end()) {
multi_pdf = true;
pdf_ptr = &(pdf->second);
}
else {
std::cerr << "WARNING!!! -- RooContainer::GetFitNormalisations -- No Pdf named "
<< pdf_name << std::endl;
}
}
std::cout << getcatName(pdf_name,cat)<< std::endl;
std::string obs_name = data_obs_names_[getcatName(data_name,cat)];
// Just use a weird name (getcatName(obs_name)) for the histogram since we dont need it
normalisations.push_back(getNormalisationFromFit(getcatName(pdf_name,cat),getcatName(obs_name,cat),pdf_ptr,obs_var->second,r1,r2,multi_pdf,external_fit));
}
}
return normalisations;
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::InputDataPoint(std::string var_name, int cat, double x, double w){
if (cat < 0) return;
if (cat>-1 && cat<ncat){
std::string name = getcatName(var_name,cat);
std::map<std::string, RooDataSet>::iterator it_var = data_.find(name);
if (it_var == data_.end())
std::cerr << "WARNING -- RooContainer::InputDataPoint -- No DataSet named "<< name << std::endl;
else{
double min_x = m_var_min_[name];
double max_x = m_var_max_[name];
if (x > min_x && x < max_x){
*(m_data_var_ptr_[name]) = x;
((*it_var).second).add(RooArgSet(*(m_data_var_ptr_[name])),w);
m_th1f_[name].Fill(x,w);
}
}
}
else {
std::cerr << "WARNING -- RooContainer::InputDataPoint -- No Category Number " << cat
<< ", category must be from 0 to " << ncat-1
<< std::endl;
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::InputBinnedDataPoint(std::string var_name, int cat, double x, double w){
if (cat>-1 && cat<ncat){
std::string name = getcatName(var_name,cat);
std::map<std::string, RooDataSet>::iterator it_var = data_.find(name);
if (it_var == data_.end())
std::cerr << "WARNING -- RooContainer::InputDataPointBinned -- No DataSet named "<< name << std::endl;
else{
double min_x = m_var_min_[name];
double max_x = m_var_max_[name];
if (x > min_x && x < max_x){
m_th1f_[name].Fill(x,w);
}
}
}
/*
else {
std::cerr << "WARNING -- RooContainer::InputDataPointBinned -- No Category Number " << cat
<< ", category must be from 0 to " << ncat-1
<< std::endl;
}
*/
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::InputSystematicPoint(std::string s_name, std::string sys_name,int ishift, int cat,double val,double w){
if (cat>-1 && cat<ncat) {
std::string cat_name = getcatName(s_name,cat);
std::string name = getsysindexName( cat_name, sys_name, abs(ishift), (ishift > 0 ? 1 : -1) );
std::map<std::string,RooDataSet>::iterator it_var = data_.find(cat_name);
if (it_var == data_.end())
std::cerr << "WARNING -- RooContainer::InpusSystematicSet -- No DataSet named "<< cat_name << std::endl;
else {
// Safe to use this since creation of systematic set guaranteed to have come from an already existing dataset
RooRealVar *ptr = m_data_var_ptr_[cat_name];
double min_x = m_var_min_[cat_name];
double max_x = m_var_max_[cat_name];
RooDataSet & data_set = data_[name];
TH1F & th1f_set = m_th1f_[name];
if (val > min_x &&val < max_x ){
// Only make datasets if the save_systematics_data is on
if (save_systematics_data){
*ptr = val;
data_set.add(RooArgSet(*ptr),w);
}
th1f_set.Fill(val,w);
}
}
}
}
// ----------------------------------------------------------------------------------------------------
void RooContainer::InputSystematicSet(std::string s_name, std::string sys_name, std::vector<int> cats
,std::vector<double> x, std::vector<double> weights){
if( weights.empty() ) { weights.resize(x.size(),1.); }
assert( x.size() == cats.size() );
assert( x.size() == weights.size() );
if ((int)x.size() != 2*nsigmas){
std::cerr << "WARNING -- RooContainer::InputSystematicSet -- Size of vector must be equal to "
<< 2*nsigmas << " -- WARNING" << std::endl;
assert(0);
// return;
}
else{
for(size_t istep=0; istep < x.size(); ++istep ) {
int cat = cats[istep];
double w = weights[istep];
if (cat>-1 && cat<ncat) {
/// int ishift = istep - nsigmas;
/// if (ishift==0) ishift++;
int ishift = (int) istep < nsigmas ? (int) istep - nsigmas : (int) istep - nsigmas + 1;
std::string cat_name = getcatName(s_name,cat);
std::string name = getsysindexName( cat_name, sys_name, abs(ishift), (ishift > 0 ? 1 : -1) );
std::map<std::string,RooDataSet>::iterator it_var = data_.find(cat_name);
if (it_var == data_.end())
std::cerr << "WARNING -- RooContainer::InpusSystematicSet -- No DataSet named "<< cat_name << std::endl;
else {
// Safe to use this since creation of systematic set guaranteed to have come from an already existing dataset
RooRealVar *ptr = m_data_var_ptr_[cat_name];
double min_x = m_var_min_[cat_name];
double max_x = m_var_max_[cat_name];
RooDataSet & data_set = data_[name];
TH1F & th1f_set = m_th1f_[name];
double val = x[istep];
if (val > min_x &&val < max_x ){
// Only make datasets if the save_systematics_data is on
if (save_systematics_data){
*ptr = val;
data_set.add(RooArgSet(*ptr),w);
}
th1f_set.Fill(val,w);
}
}
}
//else {
// std::cerr << "WARNING -- RooContainer::InputDataPoint -- No Category Number " << cat
// << ", category must be from 0 to " << ncat-1
// << std::endl;
//}
}
//// else{
////
//// if (cat>-1 && cat<ncat){
////
//// std::string cat_name = getcatName(s_name,cat);
//// std::string name = getsysName(cat_name,sys_name);
////
//// std::map<std::string,RooDataSet>::iterator it_var = data_.find(cat_name);
////
//// if (it_var == data_.end())
//// std::cerr << "WARNING -- RooContainer::InpusSystematicSet -- No DataSet named "<< cat_name << std::endl;
////
//// else {
////
//// // Safe to use this since creation of systematic set guaranteed to have come from an already existing dataset
//// RooRealVar *ptr = m_data_var_ptr_[cat_name];
//// double min_x = m_var_min_[cat_name];
//// double max_x = m_var_max_[cat_name];
////
//// std::vector<RooDataSet*>::iterator data_set_up = data_up_[name].begin();
//// std::vector<RooDataSet*>::iterator data_set_dn = data_dn_[name].begin();
//// std::vector<TH1F*>::iterator th1f_set_up = m_th1f_up_[name].begin();
//// std::vector<TH1F*>::iterator th1f_set_dn = m_th1f_dn_[name].begin();
////
//// std::vector<double>::iterator val = x.begin();
////
////
//// // Loop over the first nsigmas elements as the -1 -> dn sys
//// for (;data_set_dn != data_dn_[name].end()
//// ;data_set_dn++,th1f_set_dn++,val++){
////
//// if (*val > min_x && *val < max_x){
//// *ptr = *val;
//// (*data_set_dn)->add(RooArgSet(*ptr),w);
//// (*th1f_set_dn)->Fill(*val,w);
//// }
//// }
////
//// // Loop over the second nsigmas elements as the +1 -> up sys
//// for (;data_set_up != data_up_[name].end()
//// ;data_set_up++,th1f_set_up++,val++){
////
//// if (*val > min_x && *val < max_x){
//// *ptr = *val;
//// (*data_set_up)->add(RooArgSet(*ptr),w);
//// (*th1f_set_up)->Fill(*val,w);
//// }
//// }
//// }
//// }
//// else {
//// std::cerr << "WARNING -- RooContainer::InputDataPoint -- No Category Number " << cat
//// << ", category must be from 0 to " << ncat-1
//// << std::endl;
//// }
//// }
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::SumBinnedDatasets(std::string new_name, std::string data_one,std::string data_two, std::vector<double> coefficients_one, std::vector<double> coefficients_two, bool scale){
if (coefficients_one.size() != ncat || coefficients_two.size() != ncat ){
std::cerr << "WARNING!! -- RooContainer::SumBinnedDataSets -- number of coefficients should be the same as number of categories " << std::endl;
} else {
for (int cat=0;cat<ncat;cat++){
sumBinnedDatasets(getcatName(new_name,cat),getcatName(data_one,cat),getcatName(data_two,cat),coefficients_one[cat],coefficients_two[cat],scale);
}
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::SumBinnedDatasets(std::string new_name, std::string data_one,std::string data_two,double coefficients_one,double coefficients_two, bool scale){
for (int cat=0;cat<ncat;cat++){
sumBinnedDatasets(getcatName(new_name,cat),getcatName(data_one,cat),getcatName(data_two,cat),coefficients_one,coefficients_two,scale);
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::MergeHistograms(std::string data_one,std::string data_two, bool systematics){
// This will take 2 histograms and stick them one after another on the same x-axis, (as long as xmax_1=xmin_2)
// WARNING- this will not work on Datasets because It replaces the histograms
for (int cat=0;cat<ncat;cat++){
std::string catNameOne = getcatName(data_one,cat);
std::string catNameTwo = getcatName(data_two,cat);
std::map<std::string,RooDataSet>::iterator it_data = data_.find(catNameOne);
if (it_data!=data_.end()){
std::cerr << "WARNING -- RooContainer::MergeHistograms -- Cannot Merge Original Datasets "
<< catNameOne
<< std::endl;
return;
}
std::map<std::string,TH1F>::iterator itOne = m_th1f_.find(catNameOne);
std::map<std::string,TH1F>::iterator itTwo = m_th1f_.find(catNameTwo);
if (itOne!=m_th1f_.end() && itTwo!=m_th1f_.end())
mergeHistograms(catNameOne,&(itOne->second),&(itTwo->second));
else {
std::cerr << "WARNING -- RooContainer::MergeHistograms -- No Histograms found named "
<< catNameOne << " or " << catNameTwo
<< std::endl;
return;
}
if (systematics){
for (it_sys=systematics_.begin(); it_sys!=systematics_.end();it_sys++){
for (int sys=1;sys<=nsigmas;sys++){
std::string sysDNameOne = getsysindexName(catNameOne,it_sys->first,sys,-1);
std::string sysUNameOne = getsysindexName(catNameOne,it_sys->first,sys,1);
std::string sysDNameTwo = getsysindexName(catNameTwo,it_sys->first,sys,-1);
std::string sysUNameTwo = getsysindexName(catNameTwo,it_sys->first,sys,1);
std::map<std::string,TH1F>::iterator itOneD = m_th1f_.find(sysDNameOne);
std::map<std::string,TH1F>::iterator itOneU = m_th1f_.find(sysUNameOne);
std::map<std::string,TH1F>::iterator itTwoD = m_th1f_.find(sysDNameTwo);
std::map<std::string,TH1F>::iterator itTwoU = m_th1f_.find(sysUNameTwo);
if (itOneD!=m_th1f_.end()) {
mergeHistograms(sysDNameOne,&(itOneD->second),&(itTwoD->second));
mergeHistograms(sysUNameOne,&(itOneU->second),&(itTwoU->second));
} else {
std::cerr << "WARNING -- RooContainer::MergeHistograms -- No (systematic) Histograms found named "
<< sysDNameOne
<< std::endl;
}
}
}
}
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::mergeHistograms(std::string nameHist, TH1F* hist1, TH1F* hist2){
//Get Bin Low edges of histogram 1
int nbins1 = hist1->GetNbinsX();
int nbins2 = hist2->GetNbinsX();
int nbinsTot = nbins1+nbins2;
double *arrBins1 = new double[nbins1];
double *arrBins2 = new double[nbins2+1];
double *arrBinsTot = new double[nbinsTot+1];
for (int i=1;i<=nbins1;i++){
arrBinsTot[i-1]=hist1->GetBinLowEdge(i);
}
for (int i=1;i<=nbins2+1;i++){ // Include upper edge in 2nd hist
arrBinsTot[i+nbins1-1]=hist2->GetBinLowEdge(i);
}
const char *histoname = hist1->GetName();
const char *histotitle = hist1->GetTitle();
TH1F *newHist = new TH1F(Form("NUMPTYNAME%s",hist1->GetName()),histotitle,nbinsTot,arrBinsTot);
newHist->SetName(hist1->GetName());
for (int i=1;i<=nbins1;i++){
newHist->SetBinContent(i,hist1->GetBinContent(i));
newHist->SetBinError(i,hist1->GetBinError(i));
}
for (int i=1;i<=nbins2;i++){
newHist->SetBinContent(i+nbins1,hist2->GetBinContent(i));
newHist->SetBinError(i+nbins1,hist2->GetBinError(i));
}
std::cout << "RooContainer::MergeHistograms -- Replacing histogram - "
<< newHist->GetName()
<< std::endl;
// Now the dangerous part!
*hist1 = *newHist;
}
// -----------------------------------------------------------------------------------------
void RooContainer::SumMultiBinnedDatasets(std::string new_name, std::vector<std::string > data, std::vector<double> normalisation,bool scale){
// this version takes a vector of histograms and sums them to the total normalisations.
// if scale == true, then each histogram is summed together and then the total scaled to N
// if scale == false then each is scaled to N/numberofhists then added
// if N < 0 then total histogram is not scaled (if scale=false then N=1)
if (normalisation.size() != ncat ){
std::cerr << "WARNING!! -- RooContainer::SumMultiBinnedDataSets -- number of coefficients should be the same as number of categories " << std::endl;
} else {
for (int cat=0;cat<ncat;cat++){
std::vector<std::string> catNames;
for (std::vector<std::string>::iterator it = data.begin() ; it!=data.end();it++){
catNames.push_back(getcatName(*it,cat));
}
sumMultiBinnedDatasets(getcatName(new_name,cat),catNames,normalisation[cat],scale);
}
}
}
void RooContainer::SumMultiBinnedDatasets(std::string new_name, std::vector<std::string > data, double normalisation,bool scale){
// this version takes a vector of histograms and sums them to the total normalisations.
// if scale == true, then each histogram is summed together and then the total scaled to N
// if scale == false then each is scaled to N/numberofhists then added
// if N < 0 then total histogram is not scaled (if scale=false then N=1)
for (int cat=0;cat<ncat;cat++){
std::vector<std::string> catNames;
for (std::vector<std::string>::iterator it = data.begin() ; it!=data.end();it++){
catNames.push_back(getcatName(*it,cat));
}
sumMultiBinnedDatasets(getcatName(new_name,cat),catNames,normalisation,scale);
}
}
void RooContainer::sumMultiBinnedDatasets(std::string new_name, std::vector<std::string > data, double normalisation,bool scale){
int nHists = data.size();
std::map<std::string,TH1F>::iterator it_one = m_th1f_.find(data[0]); // Get The first histogram
if (!scale && normalisation<0) normalisation=1.0;
if (it_one !=m_th1f_.end() ){
TH1F *histOne = (TH1F*)((*it_one).second).Clone();
histOne->SetName(Form("th1f_%s",new_name.c_str()));
if (!scale) histOne->Scale(1.0/histOne->Integral());
for (int i=1;i<nHists;i++){
std::map<std::string,TH1F>::iterator it_two = m_th1f_.find(data[i]); // Get The first histogram
if (scale) { // histograms are weighted by their own integrals
histOne->Add(&(it_two->second));
} else { // histograms equally weighted
histOne->Add(&(it_two->second),1.0/(it_two->second).Integral());
}
}
if (normalisation > 0) histOne->Scale(normalisation/histOne->Integral());
m_th1f_.insert(std::pair<std::string,TH1F>(new_name,*histOne));
std::cout << "RooContainer::SumBinnedDatasets -- Created New Histogram called " << new_name << std::endl;
} else {
std::cerr << "WARNING -- RooContainer::SumBinnedDatasets -- The following Histogram wasn't found "
<< data[0] << std::endl;
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::sumBinnedDatasets(std::string new_name,std::string data_one,std::string data_two,double c1, double c2, bool scale){
std::map<std::string,TH1F>::iterator it_one = m_th1f_.find(data_one);
std::map<std::string,TH1F>::iterator it_two = m_th1f_.find(data_two);
if (it_one !=m_th1f_.end() && it_two !=m_th1f_.end() ){
TH1F *histOne = (TH1F*)((*it_one).second).Clone();
histOne->SetName(Form("th1f_%s",new_name.c_str()));
if (scale) { // coefficients are just multiples each histogram
histOne->Scale(c1);
histOne->Add(&(it_two->second),c2);
} else {
histOne->Scale((c1/histOne->Integral()));
histOne->Add(&(it_two->second),(c2/((*it_two).second).Integral()));
}
m_th1f_.insert(std::pair<std::string,TH1F>(new_name,*histOne));
std::cout << "RooContainer::SumBinnedDatasets -- Created New Histogram called " << new_name << std::endl;
} else {
std::cerr << "WARNING -- RooContainer::SumBinnedDatasets -- One of the following Histograms wasn't found "
<< data_one << ", " << data_two << std::endl;
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::CombineBinnedDatasets(std::string data_one, std::string data_two, double fraction){
for (int cat=0;cat<ncat;cat++) {
combineBinnedDatasets(getcatName(data_one,cat),getcatName(data_two,cat),fraction);
}
}
// -----------------------------------------------------------------------------------------
void RooContainer::combineBinnedDatasets(std::string data_one, std::string data_two, double fraction){
std::map<std::string,TH1F>::iterator it_one = m_th1f_.find(data_one);
std::map<std::string,TH1F>::iterator it_two = m_th1f_.find(data_two);