forked from kfjack/bmm4uml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bmm4fitter.cc
3164 lines (2600 loc) · 153 KB
/
bmm4fitter.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 "bmm4common.h"
void BuildGaussianConstaint(RooWorkspace* wspace, TString key, RooAbsReal& mean, RooAbsReal& sigma,
double min = 0., double max = 0., bool always_positive = true)
{
if (min==max) { // auto boundaries, set to +-10 sigma
min = mean.getVal() - sigma.getVal()*10.;
max = mean.getVal() + sigma.getVal()*10.;
if (always_positive && min<0.) min = 0.;
}
RooRealVar main_var(key, "", mean.getVal(), min, max);
main_var.setError(sigma.getVal());
RooGaussian gauss(Form("%s_gau",key.Data()), "", main_var, mean, sigma);
cout << ">>> BuildGaussianConstaint: " << key << ": mu = " << mean.getVal() << ", sigma = " << sigma.getVal() << " [" << min << ", " << max << "]" << endl;
wspace->import(gauss);
}
void BuildGaussianConstaint(RooWorkspace* wspace, TString key, double mean, double sigma,
double min = 0., double max = 0., bool always_positive = true)
{
RooRealVar mean_const(Form("%s_mean",key.Data()), "", mean);
RooRealVar sigma_const(Form("%s_sigma",key.Data()), "", sigma);
BuildGaussianConstaint(wspace, key, mean_const, sigma_const, min, max, always_positive);
}
void BuildLognormalConstaint(RooWorkspace* wspace, TString key, RooAbsReal& mean, RooAbsReal& kappa,
double min = 0., double max = 0., bool always_positive = true)
{
if (min==max) { // auto boundaries, set to +-10 sigma
min = mean.getVal() - (kappa.getVal()-1.)*mean.getVal()*10.;
max = mean.getVal() + (kappa.getVal()-1.)*mean.getVal()*10.;
if (always_positive && min<0.) min = 0.;
}
RooRealVar main_var(key, "", mean.getVal(), min, max);
main_var.setError((kappa.getVal()-1.)*mean.getVal());
RooLognormal Lognormal(Form("%s_lnn",key.Data()), "", main_var, mean, kappa);
cout << ">>> BuildLognormalConstaint: " << key << ": mu = " << mean.getVal() << ", kappa = " << kappa.getVal() << " [" << min << ", " << max << "]" << endl;
wspace->import(Lognormal);
}
void BuildLognormalConstaint(RooWorkspace* wspace, TString key, double mean, double kappa,
double min = 0., double max = 0., bool always_positive = true)
{
RooRealVar mean_const(Form("%s_mean",key.Data()), "", mean);
RooRealVar kappa_const(Form("%s_kappa",key.Data()), "", kappa);
BuildLognormalConstaint(wspace, key, mean_const, kappa_const, min, max, always_positive);
}
void PrepareGlobalVariables(RooWorkspace* wspace, RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareGlobalVariables() start" << endl;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
wspace->import(*wspace_base->var("BF_bs"));
wspace->import(*wspace_base->var("BF_bd"));
wspace->import(*wspace_base->var("Mass"));
wspace->import(*wspace_base->var("ReducedMassRes"));
wspace->import(*wspace_base->var("BDT"));
wspace->import(*wspace_base->var("Tau"));
wspace->import(*wspace_base->var("TauRes"));
wspace->import(*wspace_base->cat("GlobalCat"));
wspace->import(*wspace_base->cat("SpCat"));
wspace->import(*wspace_base->cat("SelCat"));
wspace->import(*wspace_base->cat("PairCat"));
wspace->import(*wspace_base->var("Weight"));
wspace->import(*wspace_base->var("dblmu_corr_scale"));
wspace->import(*wspace_base->pdf("one_over_BRBR_gau"));
wspace->import(*wspace_base->pdf("fs_over_fu_gau"));
wspace->import(*wspace_base->pdf("fs_over_fu_S13_gau"));
wspace->import(*wspace_base->var("EffTau_bs"));
wspace->import(*wspace_base->var("EffTau_bd"));
return;
}
cout << ">>> prepare new variables & constraints." << endl;
vector<TString> keys;
vector<double> values;
keys.push_back("fsfu:val");
keys.push_back("fsfu:err");
keys.push_back("fsfu_S13:val");
keys.push_back("fsfu_S13:err");
keys.push_back("BF_BuToKpsiK:val");
keys.push_back("BF_BuToKpsiK:err");
keys.push_back("BF_JpsiToMuMu:val");
keys.push_back("BF_JpsiToMuMu:err");
keys.push_back("BF_BsToMuMu:val");
keys.push_back("BF_BdToMuMu:val");
keys.push_back("EffTau_BsToMuMu:val");
keys.push_back("EffTau_BdToMuMu:val");
enum {
_fsfu, _fsfu_err,
_fsfu_S13, _fsfu_S13_err,
_BF_bu2jpsik, _BF_bu2jpsik_err,
_BF_jpsi2mumu, _BF_jpsi2mumu_err,
_BF_bs2mumu, _BF_bd2mumu,
_EffTau_bs2mumu, _EffTau_bd2mumu
};
ReadValuesFromTex("input/external_parameters.tex",keys,values);
RooRealVar BF_bs("BF_bs", "", values[_BF_bs2mumu], 0., 1e-8);
RooRealVar BF_bd("BF_bd", "", values[_BF_bd2mumu], 0., 1e-8);
wspace->import(BF_bs);
wspace->import(BF_bd);
double one_over_BRBR_val = 1./ (values[_BF_bu2jpsik] * values[_BF_jpsi2mumu]);
double one_over_BRBR_err = one_over_BRBR_val *
sqrt(pow(values[_BF_bu2jpsik_err]/values[_BF_bu2jpsik],2) + pow(values[_BF_jpsi2mumu_err]/values[_BF_jpsi2mumu],2));
BuildGaussianConstaint(wspace, "one_over_BRBR", one_over_BRBR_val, one_over_BRBR_err);
BuildGaussianConstaint(wspace, "fs_over_fu", values[_fsfu], values[_fsfu_err]);
BuildGaussianConstaint(wspace, "fs_over_fu_S13", values[_fsfu_S13], values[_fsfu_S13_err]);
// Observables
RooRealVar Mass("Mass", "", Mass_bound[0], Mass_bound[1]);
RooRealVar ReducedMassRes("ReducedMassRes", "", ReducedMassRes_bound[0], ReducedMassRes_bound[1]);
RooRealVar BDT("BDT", "", BDT_bound[0], BDT_bound[1]);
RooRealVar Tau("Tau", "", Tau_bound[0], Tau_bound[1]); // in unit of ps
RooRealVar TauRes("TauRes", "", TauRes_bound[0], TauRes_bound[1]); // in unit of ps
// Event category
RooCategory GlobalCat("GlobalCat", "");
GlobalCat.defineType("undefined",-1); // uncategorized events
for (auto& cat: CatMan.cats)
GlobalCat.defineType(cat.id,cat.index);
// Internal observables
RooCategory SpCat("SpCat", ""); // sample index for internal use
for (int i=0;i<max(bmm3::ndecays,bmm4::ndecays);i++)
SpCat.defineType(Form("sp%d",i),i);
RooRealVar Weight("Weight", "", 0., 1E10); // event weighting for internal use
RooCategory SelCat("SelCat", ""); // internal selection (applied for tau & taures boundaries so far)
SelCat.defineType("failed",0);
SelCat.defineType("passed",1);
wspace->import(Mass);
wspace->import(ReducedMassRes);
wspace->import(BDT);
wspace->import(Tau);
wspace->import(TauRes);
wspace->import(GlobalCat);
wspace->import(SpCat);
wspace->import(SelCat);
wspace->import(Weight);
RooRealVar dblmu_corr_scale("dblmu_corr_scale", "", 1.0, 0.1, 3.0); // in-situ "double-mu" correction
//dblmu_corr_scale.setConstant(true);
wspace->import(dblmu_corr_scale);
RooCategory PairCat("PairCat",""); // seagull vs. cowboy
PairCat.defineType("seagull",0);
PairCat.defineType("cowboy",1);
wspace->import(PairCat);
RooRealVar EffTau_bs("EffTau_bs", "", values[_EffTau_bs2mumu], 0.1, 5.0); // in unit of ps, PDG: BH: 1.610 +- 0.012 ps / BL: 1.422 +- 0.008 ps
RooRealVar EffTau_bd("EffTau_bd", "", values[_EffTau_bs2mumu], 0.1, 5.0); // in unit of ps, PDG: 1.520 +- 0.004 ps
EffTau_bs.setConstant(true);
EffTau_bd.setConstant(true);
wspace->import(EffTau_bs);
wspace->import(EffTau_bd);
}
void PrepareBMM3SubVariables(RooWorkspace* wspace, RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareBMM3SubVariables() start" << endl;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
for (auto& cat: CatMan.cats) {
if (!cat.era.Contains("2011") && !cat.era.Contains("2012")) continue;
wspace->import(*wspace_base->pdf(Form("N_bu_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("effratio_bs_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("effratio_bd_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("N_peak_%s_lnn",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("N_semi_%s_gau",cat.id.Data()))); // ISSUE: bmm4 takes lnn for semi background
wspace->import(*wspace_base->pdf(Form("N_h2mu_%s_lnn",cat.id.Data())));
wspace->import(*wspace_base->var(Form("N_comb_%s",cat.id.Data())));
}
return;
}
cout << ">>> build variables & constraints." << endl;
for (auto& cat: CatMan.cats) {
if (!cat.era.Contains("2011") && !cat.era.Contains("2012")) continue;
vector<TString> keys;
vector<double> values;
vector<double> errors;
keys.push_back(Form("N-OBS-BPLUS%d:val",cat.region));
keys.push_back(Form("N-OBS-BPLUS%d:tot",cat.region));
keys.push_back(Form("N-EFF-TOT-BSMM%d:val",cat.region));
keys.push_back(Form("N-EFF-TOT-BSMM%d:tot",cat.region));
keys.push_back(Form("N-EFF-TOT-BDMM%d:val",cat.region));
keys.push_back(Form("N-EFF-TOT-BDMM%d:tot",cat.region));
keys.push_back(Form("N-EFF-TOT-BPLUS%d:val",cat.region));
keys.push_back(Form("N-EFF-TOT-BPLUS%d:tot",cat.region));
enum {
_N_bu, _N_bu_err,
_eff_bs, _eff_bs_err,
_eff_bd, _eff_bd_err,
_eff_bu, _eff_bu_err
};
ReadValuesFromTex(Form("input/%s/anaBmm.plotResults.%s-cat2%d.tex",cat.era.Data(),cat.era.Data(),cat.bdt_bin),keys,values);
if (values[_N_bu_err]>values[_N_bu]) values[_N_bu_err] = values[_N_bu]*0.05; // hotfix for N_bu uncertainties
BuildGaussianConstaint(wspace, Form("N_bu_%s",cat.id.Data()),values[_N_bu],values[_N_bu_err]);
double eff_rel_err = 0.;
vector<TString> sys_keys;
vector<double> sys_values;
vector<double> sys_errors;
sys_keys.push_back(Form("Acceptance_%d",cat.region));
sys_keys.push_back(Form("MassScale_%d",cat.region));
sys_keys.push_back(Form("KaonTrack_%d",cat.region));
sys_keys.push_back(Form("Trigger_%d",cat.region));
sys_keys.push_back(Form("MuonID_%d",cat.region));
enum { _Acceptance, _MassScale, _KaonTrack, _Trigger, _MuonID };
ReadValuesFromPlainText("input/external_numbers.txt",sys_keys,sys_values,sys_errors);
eff_rel_err += pow(sys_values[_Acceptance],2);
eff_rel_err += pow(sys_values[_MassScale],2);
eff_rel_err += pow(sys_values[_KaonTrack],2);
eff_rel_err += pow(sys_values[_Trigger],2);
eff_rel_err += pow(sys_values[_MuonID],2);
sys_keys.clear();
sys_keys.push_back(Form("relDeltaEpsNoDataNoMcchan%d:val",cat.region));
sys_keys.push_back(Form("relDeltaEpsCsDataCsMcchan%d:val",cat.region));
enum { _NO_err, _CS_err };
ReadValuesFromTex(Form("input/%s/anaBmm.plotReducedOverlays.%s.tex",cat.era.Data(),cat.era.Data()),sys_keys,sys_values);
eff_rel_err += pow(sys_values[_NO_err],2);
eff_rel_err += pow(sys_values[_CS_err],2);
eff_rel_err = sqrt(eff_rel_err);
double effratio_bs = values[_eff_bs]/values[_eff_bu];
double effratio_bd = values[_eff_bd]/values[_eff_bu];
double effratio_bs_err = eff_rel_err * effratio_bs;
double effratio_bd_err = eff_rel_err * effratio_bd;
BuildGaussianConstaint(wspace, Form("effratio_bs_%s",cat.id.Data()),effratio_bs,effratio_bs_err);
BuildGaussianConstaint(wspace, Form("effratio_bd_%s",cat.id.Data()),effratio_bd,effratio_bd_err);
vector<TString> types = {"Peak", "Rsl", "Comb"};
vector<TString> fields = {"val", "e1", "e2"};
enum { _N_peak, _N_semi, _N_comb };
double N_val[types.size()], N_err[types.size()];
for (unsigned int i=0; i<types.size(); i++) {
TString& type = types[i];
keys.clear();
for (auto& field : fields) {
keys.push_back(Form("Bg%sLo%d:%s",type.Data(),cat.region,field.Data()));
keys.push_back(Form("Bg%sBd%d:%s",type.Data(),cat.region,field.Data()));
keys.push_back(Form("Bg%sBs%d:%s",type.Data(),cat.region,field.Data()));
keys.push_back(Form("Bg%sHi%d:%s",type.Data(),cat.region,field.Data()));
}
ReadValuesFromTex(Form("input/%s/anaBmm.plotResults.%s-cat2%d.tex",cat.era.Data(),cat.era.Data(),cat.bdt_bin),keys,values);
double val = 0., e1 = 0., e2 = 0.;
for (int j=0; j< 4; j++) {
val += values[j];
e1 += values[j+4];
e2 += values[j+8];
}
N_val[i] = val;
N_err[i] = sqrt(e1*e1 + e2*e2);
}
BuildLognormalConstaint(wspace, Form("N_peak_%s",cat.id.Data()),N_val[_N_peak],1.+N_err[_N_peak]/N_val[_N_peak]);
RooRealVar N_comb(Form("N_comb_%s",cat.id.Data()), "", N_val[_N_comb], 0., N_val[_N_comb]+N_err[_N_comb]*10.);
wspace->import(N_comb);
double N_semi = 0., N_semi_err = 0.;
// read the semileptonic background yields (process with 1 real muon)
for (int idx_sample=bmm3::_bgBs2KMuNu; idx_sample<=bmm3::_bgLb2PMuNu; idx_sample++) {
keys.clear();
keys.push_back(Form("%s:loSideband%d:val",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bdRare%d}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bsRare%d}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:hiSideband%d:val",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:loSideband%d:err",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bdRare%dE}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bsRare%dE}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:hiSideband%d:err",bmm3::decays[idx_sample].Data(),cat.region));
ReadValuesFromTex(Form("input/%s/anaBmm.plotResults.%s-cat2%d.tex",cat.era.Data(),cat.era.Data(),cat.bdt_bin),keys,values);
N_semi += values[0]+values[1]+values[2]+values[3]; // yields
N_semi_err += pow(values[4]+values[5]+values[6]+values[7],2); // error
}
N_semi_err = sqrt(N_semi_err);
double N_h2mu = 0., N_h2mu_err = 0.;
// read the hmumu background yields (process with 2 real muons)
for (int idx_sample=bmm3::_bgBu2PiMuMu; idx_sample<=bmm3::_bgBs2MuMuGamma; idx_sample++) {
keys.clear();
keys.push_back(Form("%s:loSideband%d:val",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bdRare%d}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bsRare%d}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:hiSideband%d:val",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:loSideband%d:err",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bdRare%dE}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:bsRare%dE}",bmm3::decays[idx_sample].Data(),cat.region));
keys.push_back(Form("%s:hiSideband%d:err",bmm3::decays[idx_sample].Data(),cat.region));
ReadValuesFromTex(Form("input/%s/anaBmm.plotResults.%s-cat2%d.tex",cat.era.Data(),cat.era.Data(),cat.bdt_bin),keys,values);
N_h2mu += values[0]+values[1]+values[2]+values[3]; // yields
N_h2mu_err += pow(values[4]+values[5]+values[6]+values[7],2); // error
}
N_h2mu_err = sqrt(N_h2mu_err);
BuildGaussianConstaint(wspace, Form("N_semi_%s",cat.id.Data()), N_semi, N_semi_err);
BuildLognormalConstaint(wspace, Form("N_h2mu_%s",cat.id.Data()), N_h2mu, 1.+N_h2mu_err/N_h2mu);
}
}
void PrepareBMM4SubVariables(RooWorkspace* wspace, RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareBMM4SubVariables() start" << endl;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
for (auto& cat: CatMan.cats) {
wspace->import(*wspace_base->pdf(Form("N_bu_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("effratio_bs_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("effratio_bd_%s_gau",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("N_peak_%s_lnn",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("N_semi_%s_lnn",cat.id.Data())));
wspace->import(*wspace_base->pdf(Form("N_h2mu_%s_lnn",cat.id.Data())));
wspace->import(*wspace_base->var(Form("N_comb_%s",cat.id.Data())));
wspace->import(*wspace_base->var(Form("DeltaMass_%s",cat.id.Data())));
wspace->import(*wspace_base->function(Form("ScaledMass_%s",cat.id.Data())));
}
return;
}
cout << ">>> build variables & constraints." << endl;
for (auto& cat: CatMan.cats) {
cout << ">>> Prepare for category: " << cat.id << endl;
vector<TString> keys;
vector<double> values;
keys.push_back(Form("%s:DeltaMass-chan%d:val",cat.era.Data(),cat.region));
ReadValuesFromTex("input/external_parameters.tex",keys,values);
RooRealVar *Mass = wspace->var("Mass");
RooRealVar DeltaMass(Form("DeltaMass_%s", cat.id.Data()), "", values[0]); // data-MC mass difference
RooFormulaVar ScaledMass(Form("ScaledMass_%s", cat.id.Data()), "", "@0-@1", RooArgList(*Mass, DeltaMass));
wspace->import(ScaledMass);
TString TexSource = Form("input/bmm4/scanBDT-%s.tex",cat.era.Data());
int bdt_min = (int)(cat.bdt_min*100.);
int bdt_max = (int)(cat.bdt_max*100.);
TexVar N_bu(TexSource, Form("bdt_%d_%s:N-W8OBS-bupsik-chan%d",bdt_min,cat.era.Data(),cat.region));
TexVar eff_bs(TexSource, Form("bdt_%d_%s:EFF-TOT-bsmm-chan%d",bdt_min,cat.era.Data(),cat.region));
TexVar eff_bd(TexSource, Form("bdt_%d_%s:EFF-TOT-bdmm-chan%d",bdt_min,cat.era.Data(),cat.region));
TexVar eff_bu(TexSource, Form("bdt_%d_%s:EFF-TOT-bupsik-chan%d",bdt_min,cat.era.Data(),cat.region));
if (bdt_max<100) {
N_bu.SubVar(TexVar(TexSource, Form("bdt_%d_%s:N-W8OBS-bupsik-chan%d",bdt_max,cat.era.Data(),cat.region)));
eff_bs.SubVar(TexVar(TexSource, Form("bdt_%d_%s:EFF-TOT-bsmm-chan%d",bdt_max,cat.era.Data(),cat.region)));
eff_bd.SubVar(TexVar(TexSource, Form("bdt_%d_%s:EFF-TOT-bdmm-chan%d",bdt_max,cat.era.Data(),cat.region)));
eff_bu.SubVar(TexVar(TexSource, Form("bdt_%d_%s:EFF-TOT-bupsik-chan%d",bdt_max,cat.era.Data(),cat.region)));
}
BuildGaussianConstaint(wspace, Form("N_bu_%s",cat.id.Data()),N_bu.val,N_bu.etot);
double effratio_bs = eff_bs.val/eff_bu.val;
double effratio_bd = eff_bd.val/eff_bu.val;
double effratio_bs_err = pow(eff_bs.estat/eff_bs.val,2);
double effratio_bd_err = pow(eff_bd.estat/eff_bd.val,2);
double eff_rel_err = 0.; // common relative uncertainties
keys.clear();
keys.push_back(Form("effmuid4r_systematics:sys")); // muon ID uncertainty for efficiency ratios
keys.push_back(Form("efftrig4r_systematics:sys")); // trigger uncertainty for efficiency ratios
keys.push_back(Form("efftrack_systematics:sys")); // kaon tracking efficiency
keys.push_back(Form("%s:yieldcorr_systematics:sys",cat.era.Data())); // yield instability correction
ReadValuesFromTex("input/external_parameters.tex",keys,values);
eff_rel_err += pow(values[0],2); // muon ID
eff_rel_err += pow(values[1],2); // trigger
eff_rel_err += pow(values[2],2); // kaon
eff_rel_err += pow(values[3],2); // yield instability
keys.clear();
keys.push_back(Form("%s:acceptance_systematics_chan%d:sys",cat.era.Data(),cat.region)); // acceptance
keys.push_back(Form("%s:effana4r_systematics_chan%d:sys",cat.era.Data(),cat.region)); // analysis efficiency
keys.push_back(Form("%s:effcandbupsik_systematics_chan%d:sys",cat.era.Data(),cat.region)); // candidate selection: B+->J/psiK+
keys.push_back(Form("%s:effcandbsmm_systematics_chan%d:sys",cat.era.Data(),cat.region)); // candidate selection: Bs->mumu
keys.push_back(Form("%s:effcandbdmm_systematics_chan%d:sys",cat.era.Data(),cat.region)); // candidate selection: Bd->mumu
ReadValuesFromTex(Form("input/bmm4/plotSystematics.%s.tex",cat.era.Data()),keys,values);
eff_rel_err += pow(values[0],2); // acceptance
eff_rel_err += pow(values[1],2); // analysis efficiency
eff_rel_err += pow(values[2],2); // B+->J/psiK+ candidate
effratio_bs_err += eff_rel_err;
effratio_bd_err += eff_rel_err;
effratio_bs_err += pow(values[3],2); // Bs->mumu candidate
effratio_bd_err += pow(values[4],2); // Bd->mumu candidate
// Adding effective lifetime correction on efficiencies & errors
keys.clear();
keys.push_back(Form("%s:effTau_efficienecy_correction:val",cat.id.Data())); // correction factor
keys.push_back(Form("%s:effTau_efficienecy_correction:sys",cat.id.Data())); // systematic uncertainty
ReadValuesFromTex(binsetup_parameter,keys,values);
effratio_bs *= values[0];
effratio_bs_err += pow(values[1],2);
// convert relative error to absolute error
effratio_bs_err = sqrt(effratio_bs_err)*effratio_bs;
effratio_bd_err = sqrt(effratio_bd_err)*effratio_bd;
BuildGaussianConstaint(wspace, Form("effratio_bs_%s",cat.id.Data()),effratio_bs,effratio_bs_err);
BuildGaussianConstaint(wspace, Form("effratio_bd_%s",cat.id.Data()),effratio_bd,effratio_bd_err);
// peaking background
TexVar N_peak;
for (int idx_sample=bmm4::_bskkMcBg; idx_sample<=bmm4::_lbpkMcBg; idx_sample++) {
TexVar var;
for (int bin=0; bin<=3; bin++) {
TexVar v(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",bdt_min,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region));
if (bdt_max<100) v.SubVar(TexVar(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",
bdt_max,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region)));
var.AddVar(v);
}
cout << ">>> Expected yield for " << cat.id << ": " << bmm4::texlabels[idx_sample] << ": " << var.val << " +- " << var.estat << " +- " << var.esyst << endl;
N_peak.AddVar(var);
}
BuildLognormalConstaint(wspace, Form("N_peak_%s",cat.id.Data()),N_peak.val,1.+N_peak.etot/N_peak.val);
// semileptonic background
TexVar N_semi;
for (int idx_sample=bmm4::_bskmunuMcBg; idx_sample<=bmm4::_lbpmunuMcBg; idx_sample++) {
TexVar var;
for (int bin=0; bin<=3; bin++) {
TexVar v(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",bdt_min,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region));
if (bdt_max<100) v.SubVar(TexVar(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",
bdt_max,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region)));
var.AddVar(v);
}
cout << ">>> Expected yield for " << cat.id << ": " << bmm4::texlabels[idx_sample] << ": " << var.val << " +- " << var.estat << " +- " << var.esyst << endl;
N_semi.AddVar(var);
}
BuildLognormalConstaint(wspace, Form("N_semi_%s",cat.id.Data()),N_semi.val,1.+N_semi.etot/N_semi.val);
// h2mu background
TexVar N_h2mu;
for (int idx_sample=bmm4::_bdpimumuMcBg; idx_sample<=bmm4::_bupimumuMcBg; idx_sample++) {
TexVar var;
for (int bin=0; bin<=3; bin++) {
TexVar v(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",bdt_min,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region));
if (bdt_max<100) v.SubVar(TexVar(TexSource, Form("bdt_%d_%s:N-SCALEDYIELD-MBIN%d-%s-chan%d",
bdt_max,cat.era.Data(),bin,bmm4::texlabels[idx_sample].Data(),cat.region)));
var.AddVar(v);
}
cout << ">>> Expected yield for " << cat.id << ": " << bmm4::texlabels[idx_sample] << ": " << var.val << " +- " << var.estat << " +- " << var.esyst << endl;
N_h2mu.AddVar(var);
}
BuildLognormalConstaint(wspace, Form("N_h2mu_%s",cat.id.Data()),N_h2mu.val,1.+N_h2mu.etot/N_h2mu.val);
// combinatorial background
TexVar N_comb;
for (int bin=0; bin<=3; bin++) {
TexVar v(TexSource, Form("bdt_%d_%s:N-FIT-MBIN%d-CB-chan%d",bdt_min,cat.era.Data(),bin,cat.region));
if (bdt_max<100) v.SubVar(TexVar(TexSource, Form("bdt_%d_%s:N-FIT-MBIN%d-CB-chan%d",bdt_max,cat.era.Data(),bin,cat.region)));
N_comb.AddVar(v);
}
if (N_comb.val<1.) N_comb.val = 1.; // ISSUE: hot fix for too small background
wspace->import(RooRealVar(Form("N_comb_%s",cat.id.Data()), "", N_comb.val, 0., max(10.,N_comb.val+sqrt(N_comb.val)*10.)));
}
}
void LoadDataFromBMM3Tree(RooWorkspace* wspace, RooAbsData *dataset, TString filename, TString era)
{
RooRealVar *Mass = wspace->var("Mass");
RooRealVar *ReducedMassRes = wspace->var("ReducedMassRes");
RooRealVar *BDT = wspace->var("BDT");
RooRealVar *Tau = wspace->var("Tau");
RooRealVar *TauRes = wspace->var("TauRes");
RooCategory *SelCat = wspace->cat("SelCat");
RooCategory *PairCat = wspace->cat("PairCat");
RooCategory *GlobalCat = wspace->cat("GlobalCat");
RooArgSet varlist(*Mass,*ReducedMassRes,*BDT,*Tau,*TauRes,*SelCat,*PairCat,*GlobalCat);
cout << ">>> Load data from tree: " << filename << endl;
exist_protection(filename);
TFile *fin = new TFile(filename);
TTree *tin = (TTree*)fin->Get("SgData_bdt");
double m1eta_t, m2eta_t, m_t, bdt_t, me_t, tau_t;
bool muid_t;
tin->SetBranchAddress("m1eta", &m1eta_t);
tin->SetBranchAddress("m2eta", &m2eta_t);
tin->SetBranchAddress("m", &m_t);
tin->SetBranchAddress("me", &me_t);
tin->SetBranchAddress("bdt", &bdt_t);
tin->SetBranchAddress("tau", &tau_t);
tin->SetBranchAddress("muid", &muid_t);
cout << ">>> parsing " << tin->GetEntries() << " entries." << endl;
for (int evt=0; evt<tin->GetEntries();evt++) {
tin->GetEntry(evt);
bool isBarrel = fabs(m1eta_t)<1.4 && fabs(m2eta_t)<1.4;
if (isBarrel) m_t += 0.006; // shift mass to MC
else m_t += 0.007;
if (m_t < Mass_bound[0] || m_t > Mass_bound[1] || !muid_t) continue;
if (me_t/m_t < ReducedMassRes_bound[0] || me_t/m_t > ReducedMassRes_bound[1]) continue;
if (bdt_t < -1.) continue;
int index = CatMan.index(era, isBarrel?0:1, bdt_t);
if (index<0) continue;
if (tau_t*1E12 > Tau_bound[0] && tau_t*1E12 < Tau_bound[1]) SelCat->setIndex(1); // ISSUE: no tau resolution information
else SelCat->setIndex(0);
Mass->setVal(m_t);
ReducedMassRes->setVal(me_t/m_t);
BDT->setVal(bdt_t);
Tau->setVal(tau_t*1E12);
TauRes->setVal(0.); // ISSUE: no tau resolution information
PairCat->setIndex(evt%2); // ISSUE: no seagull/cowboy information
GlobalCat->setIndex(index);
dataset->add(varlist);
}
fin->Close();
}
void LoadDataFromBMM4Tree(RooWorkspace* wspace, RooAbsData *dataset, TString filename, TString era)
{
RooRealVar *Mass = wspace->var("Mass");
RooRealVar *ReducedMassRes = wspace->var("ReducedMassRes");
RooRealVar *BDT = wspace->var("BDT");
RooRealVar *Tau = wspace->var("Tau");
RooRealVar *TauRes = wspace->var("TauRes");
RooCategory *SelCat = wspace->cat("SelCat");
RooCategory *PairCat = wspace->cat("PairCat");
RooCategory *GlobalCat = wspace->cat("GlobalCat");
RooArgSet varlist(*Mass,*ReducedMassRes,*BDT,*Tau,*TauRes,*SelCat,*PairCat,*GlobalCat);
cout << ">>> Load data from tree: " << filename << endl;
exist_protection(filename);
TFile *fin = new TFile(filename);
TTree *tin = (TTree*)fin->Get("bmmData");
double m_t, me_t, bdt_t, m1phi_t, m2phi_t, tau_t, taue_t;
int chan_t, m1q_t;
bool muid_t;
tin->SetBranchAddress("m", &m_t);
tin->SetBranchAddress("me", &me_t);
tin->SetBranchAddress("bdt", &bdt_t);
tin->SetBranchAddress("tau", &tau_t);
tin->SetBranchAddress("taue", &taue_t);
tin->SetBranchAddress("chan", &chan_t);
tin->SetBranchAddress("muid", &muid_t);
tin->SetBranchAddress("m1phi", &m1phi_t);
tin->SetBranchAddress("m2phi", &m2phi_t);
tin->SetBranchAddress("m1q", &m1q_t);
cout << ">>> parsing " << tin->GetEntries() << " entries." << endl;
for (int evt=0; evt<tin->GetEntries();evt++) {
tin->GetEntry(evt);
if (m_t < Mass_bound[0] || m_t > Mass_bound[1] || !muid_t) continue;
if (me_t/m_t < ReducedMassRes_bound[0] || me_t/m_t > ReducedMassRes_bound[1]) continue;
if (bdt_t < -1.) continue;
int index = CatMan.index(era, chan_t, bdt_t);
if (index<0) continue;
if (tau_t*1E12 > Tau_bound[0] && tau_t*1E12 < Tau_bound[1] &&
taue_t*1E12 > TauRes_bound[0] && taue_t*1E12 < TauRes_bound[1]) SelCat->setIndex(1);
else SelCat->setIndex(0);
double dPhi = m1phi_t-m2phi_t;
while (dPhi >= M_PI) dPhi -= M_PI*2;
while (dPhi < -M_PI) dPhi += M_PI*2;
bool isCowboy = (m1q_t*dPhi > 0);
Mass->setVal(m_t);
ReducedMassRes->setVal(me_t/m_t);
BDT->setVal(bdt_t);
Tau->setVal(tau_t*1E12);
TauRes->setVal(taue_t*1E12);
PairCat->setIndex(isCowboy);
GlobalCat->setIndex(index);
dataset->add(varlist);
}
fin->Close();
}
void PrepareData(RooWorkspace* wspace, RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareData() start" << endl;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
wspace->import(*wspace_base->data("global_data"));
return;
}
cout << ">>> load events from trees." << endl;
RooRealVar *Mass = wspace->var("Mass");
RooRealVar *ReducedMassRes = wspace->var("ReducedMassRes");
RooRealVar *BDT = wspace->var("BDT");
RooRealVar *Tau = wspace->var("Tau");
RooRealVar *TauRes = wspace->var("TauRes");
RooCategory *SelCat = wspace->cat("SelCat");
RooCategory *PairCat = wspace->cat("PairCat");
RooCategory *GlobalCat = wspace->cat("GlobalCat");
RooArgSet varlist(*Mass,*ReducedMassRes,*BDT,*Tau,*TauRes,*SelCat,*PairCat,*GlobalCat);
RooAbsData *global_data = new RooDataSet("global_data", "", varlist);
/* if (CONFIG_BMM3) {
LoadDataFromBMM3Tree(wspace, global_data, "input/2011/small-SgData-unblinded.root", "2011");
LoadDataFromBMM3Tree(wspace, global_data, "input/2012/small-SgData-unblinded.root", "2012");
}*/
if (CONFIG_BMM4) {
LoadDataFromBMM4Tree(wspace, global_data, "input/bmm4/small2016BFs01-bmmData.root", "2016BFs01");
LoadDataFromBMM4Tree(wspace, global_data, "input/bmm4/small2016GHs01-bmmData.root", "2016GHs01");
}
wspace->import(*global_data);
}
// ----------------------------------------------------
// Prepare the resolution functions for lifetime fit
// current model: double-Gaussian, with <taue> as the main scaling factor; error on <taue> has been set to 12%
// available options:
// bsmm - build B->mumu resolution model
// bupsik - build B->J/psi K+ resolution model
// --
// triple - using a triple Guassian model
// double - reduced to a double Guassian model
// --
// available target_cat (other than nominal category IDs):
// mix - using the expected yield to combine MC samples
//
void PrepareLifetimeResolutionModel(RooWorkspace *wspace, TString opt = "bsmm:triple", TString target_cat = "mix", RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareLifetimeResolutionModel() start" << endl;
TString tag, treename;
if (opt.Contains("bsmm")) {
tag = "bsmm";
treename = "bsmmMc";
}else if (opt.Contains("bupsik")) {
tag = "bupsik";
treename = "bupsikMc";
}else {
cout << ">>> Undefined option: " << opt << endl;
return;
}
tag += TString("_")+target_cat;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
RooResolutionModel *TauRes_Model = (RooResolutionModel*)wspace_base->obj(Form("TauRes_Model_%s",tag.Data()));
wspace->import(*TauRes_Model);
return;
}
RooRealVar delTau("delTau","",-0.6,+0.6);
TH1D *h_global_deltau = new TH1D("h_global_deltau","",240,-0.6,+0.6);
TH1D *h_global_taue = new TH1D("h_global_taue","",192,TauRes_bound[0],TauRes_bound[1]);
h_global_deltau->Sumw2();
h_global_taue->Sumw2();
for (auto& cat: CatMan.cats) {
if (target_cat!="mix" && cat.id!=target_cat) continue;
// obtain the expected yield
double yield = 0.;
RooRealVar *N_bu = wspace->var(Form("N_bu_%s", cat.id.Data()));
RooRealVar *effratio_bs = wspace->var(Form("effratio_bs_%s", cat.id.Data()));
if (tag.Contains("bsmm")) yield = N_bu->getVal()*effratio_bs->getVal();
if (tag.Contains("bupsik")) yield = N_bu->getVal();
TH1D *h_deltau = new TH1D("h_deltau","",240,-0.6,+0.6);
TH1D *h_taue = new TH1D("h_taue","",192,TauRes_bound[0],TauRes_bound[1]);
h_deltau->Sumw2();
h_taue->Sumw2();
// load the corresponding MC sample
TString filename = Form("input/bmm4/small%s-%s.root",cat.era.Data(),treename.Data());
TChain *events = new TChain(treename);
exist_protection(filename);
events->Add(filename);
cout << ">>> Loading from " << filename << ", with " << events->GetEntries() << " entries." << endl;
double m_t, me_t, tau_t, taue_t, gtau_t, bdt_t;
int chan_t;
bool muid_t;
events->SetBranchAddress("m", &m_t);
events->SetBranchAddress("me", &me_t);
events->SetBranchAddress("tau", &tau_t);
events->SetBranchAddress("taue", &taue_t);
events->SetBranchAddress("gtau", >au_t);
events->SetBranchAddress("bdt", &bdt_t);
events->SetBranchAddress("chan", &chan_t);
events->SetBranchAddress("muid", &muid_t);
for (int evt=0; evt<events->GetEntries();evt++) {
events->GetEntry(evt);
if (m_t < Mass_bound[0] || m_t > Mass_bound[1] || !muid_t) continue;
if (me_t/m_t < ReducedMassRes_bound[0] || me_t/m_t > ReducedMassRes_bound[1]) continue;
if (bdt_t < -1.) continue;
int index = CatMan.index(cat.era, chan_t, bdt_t);
if (index!=cat.index) continue;
if (tau_t*1E12 < Tau_bound[0] || tau_t*1E12 > Tau_bound[1]) continue;
if (taue_t*1E12 < TauRes_bound[0] || taue_t*1E12 > TauRes_bound[1]) continue;
h_deltau->Fill((tau_t-gtau_t)*1E12);
h_taue->Fill(taue_t*1E12);
}
cout << ">>> Category ID: " << cat.id << endl;
if (target_cat=="mix") {
cout << ">>> " << h_deltau->GetEntries() << " entries filled, to be scaled to " << yield << endl;
double weight = yield/(double)h_deltau->GetEntries();
h_global_deltau->Add(h_deltau,weight);
h_global_taue->Add(h_taue,weight);
}else {
cout << ">>> " << h_deltau->GetEntries() << " entries filled/appended." << endl;
h_global_deltau->Add(h_deltau);
h_global_taue->Add(h_taue);
}
delete h_deltau;
delete h_taue;
delete events;
}
double mean_taue = h_global_taue->GetMean();
cout << ">>> Average tau_err = " << mean_taue << ", to be used in the resolution function." << endl;
RooDataHist *h_deltau_data = new RooDataHist("h_deltau_data", "", RooArgList(delTau), h_global_deltau);
RooRealVar par_taue(Form("respar_%s_taue",tag.Data()),"",mean_taue);
RooRealVar par_mean(Form("respar_%s_mean",tag.Data()),"",0.0,-0.5,0.5);
RooRealVar par_sig1(Form("respar_%s_sig1",tag.Data()),"",0.7,0.3,1.0);
RooRealVar par_sig2(Form("respar_%s_sig2",tag.Data()),"",1.2,0.7,3.5);
RooRealVar par_sig3(Form("respar_%s_sig3",tag.Data()),"",2.6,1.5,5.5);
RooRealVar par_frac1(Form("respar_%s_frac1",tag.Data()),"",0.4,0.0,0.6);
RooRealVar par_frac2(Form("respar_%s_frac2",tag.Data()),"",0.4,0.0,0.6);
RooProduct par_smean(Form("respar_%s_smean",tag.Data()),"",RooArgList(par_taue,par_mean));
RooProduct par_ssig1(Form("respar_%s_ssig1",tag.Data()),"",RooArgList(par_taue,par_sig1));
RooProduct par_ssig2(Form("respar_%s_ssig2",tag.Data()),"",RooArgList(par_taue,par_sig2));
RooProduct par_ssig3(Form("respar_%s_ssig3",tag.Data()),"",RooArgList(par_taue,par_sig3));
RooGaussian model_g1("model_g1","",delTau,par_smean,par_ssig1);
RooGaussian model_g2("model_g2","",delTau,par_smean,par_ssig2);
RooGaussian model_g3("model_g3","",delTau,par_smean,par_ssig3);
// set the taue error to 12%, which is the maximum difference between J/psi K+ data and MC in difference era
par_taue.setError(mean_taue*0.12);
RooAddPdf *model = 0;
if (opt.Contains("triple"))
model = new RooAddPdf("model","",RooArgList(model_g1,model_g2,model_g3),RooArgList(par_frac1,par_frac2));
if (opt.Contains("double")) {
par_frac1.setMax(1.0);
model = new RooAddPdf("model","",RooArgList(model_g1,model_g2),RooArgList(par_frac1));
}
RooFitResult *res = model->fitTo(*h_deltau_data, Extended(false), NumCPU(NCPU), Hesse(false), Save(true), SumW2Error(true));
if (res->status()!=0) converge_protection();
delete res;
RooPlot* frame = delTau.frame(Title(" "));
h_deltau_data->plotOn(frame);
model->plotOn(frame, LineColor(kBlue), LineWidth(3), Name("model"));
frame->SetMinimum(0.);
frame->SetMaximum(frame->GetMaximum()*1.4);
TCanvas* canvas = new TCanvas("canvas", "", 600, 600);
canvas->SetMargin(0.14,0.06,0.13,0.07);
frame->GetYaxis()->SetTitleOffset(1.48);
frame->GetYaxis()->SetTitle("Arbitary Unit");
frame->GetXaxis()->SetTitleOffset(1.15);
frame->GetXaxis()->SetLabelOffset(0.01);
frame->GetXaxis()->SetTitle("tau(rec)-tau(gen) [ps]");
frame->GetXaxis()->SetTitleSize(0.043);
frame->GetYaxis()->SetTitleSize(0.043);
frame->Draw();
TLatex tex;
tex.SetTextFont(42);
tex.SetTextSize(0.035);
tex.SetTextAlign(11);
tex.SetNDC();
tex.DrawLatex(0.14,0.94,"CMS simulation");
TLegend *leg1 = new TLegend(0.45,0.65,0.93,0.91);
leg1->SetFillColor(kWhite);
leg1->SetFillStyle(0);
leg1->SetLineColor(kWhite);
leg1->SetLineWidth(0);
leg1->AddEntry((TObject*)0,Form("<#sigma_{#tau}> = %.4g ps",mean_taue),"");
leg1->AddEntry((TObject*)0,Form("mean = %.4g #pm %.4g",par_mean.getVal(),par_mean.getError()),"");
leg1->AddEntry((TObject*)0,Form("sig1 = %.4g #pm %.4g",par_sig1.getVal(),par_sig1.getError()),"");
leg1->AddEntry((TObject*)0,Form("sig2 = %.4g #pm %.4g",par_sig2.getVal(),par_sig2.getError()),"");
if (opt.Contains("triple")) leg1->AddEntry((TObject*)0,Form("sig3 = %.4g #pm %.4g",par_sig3.getVal(),par_sig3.getError()),"");
leg1->AddEntry((TObject*)0,Form("frac1 = %.4g #pm %.4g",par_frac1.getVal(),par_frac1.getError()),"");
if (opt.Contains("triple")) leg1->AddEntry((TObject*)0,Form("frac2 = %.4g #pm %.4g",par_frac2.getVal(),par_frac2.getError()),"");
leg1->Draw();
canvas->Print(Form("fig/model_taures_%s.pdf",tag.Data()));
RooRealVar *Tau = wspace->var("Tau");
// fixed the parameters and import the model
par_mean.setConstant(true);
par_sig1.setConstant(true);
par_sig2.setConstant(true);
par_sig3.setConstant(true);
par_frac1.setConstant(true);
par_frac2.setConstant(true);
RooGaussModel TauRes_g1(Form("TauRes_g1_%s",tag.Data()),"",*Tau,par_smean,par_ssig1);
RooGaussModel TauRes_g2(Form("TauRes_g2_%s",tag.Data()),"",*Tau,par_smean,par_ssig2);
RooGaussModel TauRes_g3(Form("TauRes_g3_%s",tag.Data()),"",*Tau,par_smean,par_ssig3);
if (opt.Contains("triple")) {
RooAddModel TauRes_Model(Form("TauRes_Model_%s",tag.Data()),"",RooArgList(TauRes_g1,TauRes_g2,TauRes_g3),RooArgList(par_frac1,par_frac2));
wspace->import(TauRes_Model);
}
if (opt.Contains("double")) {
RooAddModel TauRes_Model(Form("TauRes_Model_%s",tag.Data()),"",RooArgList(TauRes_g1,TauRes_g2),RooArgList(par_frac1));
wspace->import(TauRes_Model);
}
// Also produce the projection for taue
h_global_taue->GetYaxis()->SetTitleOffset(1.48);
h_global_taue->GetYaxis()->SetTitle("Arbitary Unit");
h_global_taue->GetXaxis()->SetTitleOffset(1.15);
h_global_taue->GetXaxis()->SetLabelOffset(0.01);
h_global_taue->GetXaxis()->SetTitle("decay time uncertainty #sigma_{#tau} [ps]");
h_global_taue->GetXaxis()->SetTitleSize(0.043);
h_global_taue->GetYaxis()->SetTitleSize(0.043);
h_global_taue->SetFillColor(50);
h_global_taue->SetStats(false);
h_global_taue->Draw("hist");
tex.DrawLatex(0.14,0.94,"CMS simulation");
TLegend *leg2 = new TLegend(0.43,0.80,0.93,0.91);
leg2->SetFillColor(kWhite);
leg2->SetFillStyle(0);
leg2->SetLineColor(kWhite);
leg2->SetLineWidth(0);
leg2->AddEntry((TObject*)0,Form("<#sigma_{#tau}> = %.4g ps",mean_taue),"");
leg2->Draw();
canvas->Print(Form("fig/taures_%s_mc_reco.pdf",tag.Data()));
delete model;
delete h_global_taue;
delete h_global_deltau;
delete h_deltau_data;
delete leg1;
delete leg2;
delete canvas;
}
// ----------------------------------------------------
// Prepare efficiency functions used for lifetime fit
// also produce a histogram PDF for systematic study
//
// available options:
// bsmm - build B->mumu efficiency model
// bupsik - build B->J/psi K+ efficiency model
// --
// one_over_exp - modeling by [0]+[1]*x+[2]*x*x+[3]/(1.+exp(-[4]*x))
// threshold - modeling by threshold function
// --
// available target_cat (other than nominal category IDs):
// mix - using the expected yield to combine MC samples
//
void PrepareLifetimeEfficiencyModel(RooWorkspace *wspace, TString opt = "bsmm:threshold", TString target_cat = "mix", RooWorkspace* wspace_base = 0)
{
cout << ">>> PrepareLifetimeEfficiencyModel() start" << endl;
TString tag, treename;
if (opt.Contains("bsmm")) {
tag = "bsmm";
treename = "bsmmMc";
}else if (opt.Contains("bupsik")) {
tag = "bupsik";
treename = "bupsikMc";
}else {
cout << ">>> Undefined option: " << opt << endl;
return;
}
tag += TString("_")+target_cat;
if (wspace_base!=0) {
cout << ">>> import from existing workspace." << endl;
RooFormulaVar *TauEff_Model = (RooFormulaVar*)wspace_base->obj(Form("TauEff_Model_%s",tag.Data()));
wspace->import(*TauEff_Model);
RooHistPdf *TauEff_Model_Hist = (RooHistPdf*)wspace_base->obj(Form("TauEff_Model_Hist_%s",tag.Data()));
wspace->import(*TauEff_Model_Hist);
return;
}
vector<double> xbins = {
0.5,0.625,0.75,0.875,
1.,1.125,1.25,1.375,1.5,1.625,1.75,1.875,
2.,2.125,2.25,2.375,2.5,2.75,
3.,3.25,3.5,3.75,
4.,4.5,5.,5.5,6.,7.,8.,9.,10.,12.};
TH1D *h_global_taueff = new TH1D("h_global_taueff","",xbins.size()-1,xbins.data());
h_global_taueff->Sumw2();
for (auto& cat: CatMan.cats) {
if (target_cat!="mix" && cat.id!=target_cat) continue;
// obtain the expected yield
double yield = 0.;
RooRealVar *N_bu = wspace->var(Form("N_bu_%s", cat.id.Data()));
RooRealVar *effratio_bs = wspace->var(Form("effratio_bs_%s", cat.id.Data()));
if (tag.Contains("bsmm")) yield = N_bu->getVal()*effratio_bs->getVal();
if (tag.Contains("bupsik")) yield = N_bu->getVal();
TH1D *h_taueff = new TH1D("h_taueff","",xbins.size()-1,xbins.data());
h_taueff->Sumw2();
// load the corresponding MC sample
TString filename = Form("input/bmm4/small%s-%s.root",cat.era.Data(),treename.Data());
TChain *events = new TChain(treename);
exist_protection(filename);
events->Add(filename);
cout << ">>> Loading from " << filename << ", with " << events->GetEntries() << " entries." << endl;
double m_t, me_t, tau_t, taue_t, gtau_t, bdt_t;
int chan_t;
bool muid_t;
events->SetBranchAddress("m", &m_t);
events->SetBranchAddress("me", &me_t);
events->SetBranchAddress("tau", &tau_t);
events->SetBranchAddress("taue", &taue_t);
events->SetBranchAddress("gtau", >au_t);
events->SetBranchAddress("bdt", &bdt_t);
events->SetBranchAddress("chan", &chan_t);
events->SetBranchAddress("muid", &muid_t);