forked from erc-asymow/TemplateStudies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmassscales_data.cpp
1207 lines (1084 loc) · 53.2 KB
/
massscales_data.cpp
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 <ROOT/RDataFrame.hxx>
#include "TFile.h"
#include "TRandom3.h"
#include "TVector.h"
#include "TVectorT.h"
#include "TMath.h"
#include "TError.h"
#include "TF1.h"
#include "TF2.h"
#include "TGraphErrors.h"
#include <TMatrixD.h>
#include <TMatrixDSymfwd.h>
#include <TStopwatch.h>
#include <ROOT/RVec.hxx>
#include <iostream>
#include <Math/Vector4D.h>
#include <Math/VectorUtil.h>
#include <boost/program_options.hpp>
#include "Minuit2/FunctionMinimum.h"
#include "Minuit2/MnMinimize.h"
#include "Minuit2/MnMigrad.h"
#include "Minuit2/MnHesse.h"
#include "Minuit2/MnPrint.h"
#include "Minuit2/MnUserParameterState.h"
#include "Minuit2/FCNGradientBase.h"
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Dense>
#include "RooRealVar.h"
#include "RooDerivative.h"
#include "RooDataSet.h"
#include "RooGaussian.h"
#include "RooAddPdf.h"
#include "RooExponential.h"
#include "RooCrystalBall.h"
#include "RooFitResult.h"
#include "RooMsgService.h"
#include "RooPlot.h"
using namespace RooFit;
//#include <Eigen/Core>
//#include <Eigen/Dense>
using Eigen::MatrixXd;
using Eigen::VectorXd;
using namespace std;
using namespace ROOT;
using namespace ROOT::Minuit2;
typedef ROOT::VecOps::RVec<double> RVecD;
typedef ROOT::VecOps::RVec<unsigned int> RVecUI;
typedef ROOT::VecOps::RVec<int> RVecI;
typedef ROOT::VecOps::RVec<float> RVecF;
typedef ROOT::VecOps::RVec<bool> RVecB;
using ROOT::RDF::RNode;
using namespace boost::program_options;
constexpr double lumiMC2016 = 3.33369e+08/2001.9e+03;
constexpr double lumiMC2017 = 4.9803e+07/2001.9e+03;
constexpr double lumiMC2018 = 6.84093e+07/2001.9e+03;
int main(int argc, char* argv[])
{
TStopwatch sw;
sw.Start();
ROOT::EnableImplicitMT();
variables_map vm;
try
{
options_description desc{"Options"};
desc.add_options()
("help,h", "Help screen")
("minNumEvents", value<int>()->default_value(100), "number of events")
("minNumEventsPerBin", value<int>()->default_value(10), "bias")
("lumi", value<float>()->default_value(16.1), "number of events")
("tag", value<std::string>()->default_value("closure"), "run type")
("run", value<std::string>()->default_value("closure"), "run type")
("saveHistos", bool_switch()->default_value(false), "")
("firstIter", value<int>()->default_value(-1), "firstIter")
("lastIter", value<int>()->default_value(-1), "lastIter")
("nRMSforGausFit", value<float>()->default_value(-1.), "number of events")
("minNumMassBins", value<int>()->default_value(4), "number of events")
("rebin", value<int>()->default_value(2), "rebin")
("fitWidth", bool_switch()->default_value(false), "")
("fitNorm", bool_switch()->default_value(false), "")
("usePrevFit", bool_switch()->default_value(false), "")
("tagPrevFit", value<std::string>()->default_value("closure"), "run type")
("runPrevFit", value<std::string>()->default_value("closure"), "run type")
("useSmearFit", bool_switch()->default_value(false), "")
("tagSmearFit", value<std::string>()->default_value("closure"), "run type")
("runSmearFit", value<std::string>()->default_value("closure"), "run type")
("useKf", bool_switch()->default_value(false), "")
("scaleToData", bool_switch()->default_value(false), "")
("useCBpdf", bool_switch()->default_value(false), "")
("minFrac", value<float>()->default_value(0.995), "")
("y2016", bool_switch()->default_value(false), "")
("y2017", bool_switch()->default_value(false), "")
("y2018", bool_switch()->default_value(false), "")
("seed", value<int>()->default_value(4357), "seed");
store(parse_command_line(argc, argv, desc), vm);
notify(vm);
if (vm.count("help")){
std::cout << desc << '\n';
return 0;
}
if (vm.count("tag")) std::cout << "Tag: " << vm["tag"].as<std::string>() << '\n';
if (vm.count("run")) std::cout << "Run: " << vm["run"].as<std::string>() << '\n';
}
catch (const error &ex)
{
std::cerr << ex.what() << '\n';
}
int minNumEvents = vm["minNumEvents"].as<int>();
float lumi = vm["lumi"].as<float>();
float nRMSforGausFit = vm["nRMSforGausFit"].as<float>();
std::string tag = vm["tag"].as<std::string>();
std::string run = vm["run"].as<std::string>();
int seed = vm["seed"].as<int>();
int minNumEventsPerBin = vm["minNumEventsPerBin"].as<int>();
int minNumMassBins = vm["minNumMassBins"].as<int>();
int rebin = vm["rebin"].as<int>();
int firstIter = vm["firstIter"].as<int>();
int lastIter = vm["lastIter"].as<int>();
bool saveHistos = vm["saveHistos"].as<bool>();
bool fitWidth = vm["fitWidth"].as<bool>();
bool fitNorm = vm["fitNorm"].as<bool>();
bool usePrevFit = vm["usePrevFit"].as<bool>();
bool useSmearFit = vm["useSmearFit"].as<bool>();
bool useKf = vm["useKf"].as<bool>();
bool y2016 = vm["y2016"].as<bool>();
bool y2017 = vm["y2017"].as<bool>();
bool y2018 = vm["y2018"].as<bool>();
std::string tagPrevFit = vm["tagPrevFit"].as<std::string>();
std::string runPrevFit = vm["runPrevFit"].as<std::string>();
std::string tagSmearFit = vm["tagSmearFit"].as<std::string>();
std::string runSmearFit = vm["runSmearFit"].as<std::string>();
bool scaleToData = vm["scaleToData"].as<bool>();
bool useCBpdf = vm["useCBpdf"].as<bool>();
float minFrac = vm["minFrac"].as<float>();
assert( y2016 || y2017 || y2018 );
TRandom3* ran0 = new TRandom3(seed);
vector<float> pt_edges = {25, 30, 35, 40, 45, 55};
vector<float> eta_edges = {-2.4, -2.2, -2.0, -1.8, -1.6, -1.4, -1.2, -1.0, -0.8, -0.6, -0.4, -0.2, 0.0,
0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.0, 2.2, 2.4};
TH1F* h_pt_edges = new TH1F("h_pt_edges", "", pt_edges.size()-1, pt_edges.data());
TH1F* h_eta_edges = new TH1F("h_eta_edges", "", eta_edges.size()-1, eta_edges.data());
unsigned int n_pt_bins = pt_edges.size()-1;
unsigned int n_eta_bins = eta_edges.size()-1;
int n_bins = n_pt_bins*n_pt_bins*n_eta_bins*n_eta_bins;
TH1F* h_A_vals_nom = new TH1F("h_A_vals_nom", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_e_vals_nom = new TH1F("h_e_vals_nom", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_M_vals_nom = new TH1F("h_M_vals_nom", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_A_vals_prevfit = new TH1F("h_A_vals_prevfit", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_e_vals_prevfit = new TH1F("h_e_vals_prevfit", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_M_vals_prevfit = new TH1F("h_M_vals_prevfit", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_c_vals_prevfit = new TH1F("h_c_vals_prevfit", "", n_eta_bins, 0, n_eta_bins );
TH1F* h_d_vals_prevfit = new TH1F("h_d_vals_prevfit", "", n_eta_bins, 0, n_eta_bins );
float kmean_val = 0.5*( 1./pt_edges[0] + 1./pt_edges[ pt_edges.size()-1] );
VectorXd A_vals_fit( n_eta_bins );
VectorXd e_vals_fit( n_eta_bins );
VectorXd M_vals_fit( n_eta_bins );
VectorXd c_vals_fit( n_eta_bins );
VectorXd d_vals_fit( n_eta_bins );
// bias for A out
for(unsigned int i=0; i<n_eta_bins; i++){
h_A_vals_nom->SetBinContent(i+1, 0.0);
h_A_vals_prevfit->SetBinContent(i+1, 0.0);
h_c_vals_prevfit->SetBinContent(i+1, 0.0);
h_d_vals_prevfit->SetBinContent(i+1, 0.0);
A_vals_fit(i) = 0.0;
}
// bias for e out
for(unsigned int i=0; i<n_eta_bins; i++){
h_e_vals_nom->SetBinContent(i+1, 0.0);
h_e_vals_prevfit->SetBinContent(i+1, 0.0);
e_vals_fit(i) = 0.0;
}
// bias for M out
for(unsigned int i=0; i<n_eta_bins; i++){
h_M_vals_nom->SetBinContent(i+1, 0.0);
h_M_vals_prevfit->SetBinContent(i+1, 0.0);
M_vals_fit(i) = 0.0;
}
std::vector<string> recos = {"reco", "smear0"};
std::map<string, TH1D*> h_map;
for(unsigned int r = 0; r<recos.size(); r++){
h_map.insert( std::make_pair<string, TH1D* >("mean_"+recos[r], 0 ) );
h_map.insert( std::make_pair<string, TH1D* >("rms_"+recos[r], 0 ) );
h_map.insert( std::make_pair<string, TH1D* >("mask_"+recos[r], 0 ) );
}
std::map<string, TH2D*> hCB_map;
for(unsigned int r = 0; r<recos.size(); r++){
hCB_map.insert( std::make_pair<string, TH2D* >("lnder_"+recos[r], 0 ) );
}
// map of positions in RVecF "masses"
std::map<string, unsigned int> idx_map;
idx_map.insert( std::make_pair<string, unsigned int >("reco", 1 ) );
idx_map.insert( std::make_pair<string, unsigned int >("smear0", 2 ) );
if(usePrevFit){
TFile* ffit = TFile::Open(("./massfit_"+tagPrevFit+"_"+runPrevFit+".root").c_str(), "READ");
if(ffit!=0){
cout << "Using fit results from " << std::string(ffit->GetName()) << " as new nominal for smear0" << endl;
TH1D* h_A_vals_prevfit_in = (TH1D*)ffit->Get("h_A_vals_prevfit");
TH1D* h_e_vals_prevfit_in = (TH1D*)ffit->Get("h_e_vals_prevfit");
TH1D* h_M_vals_prevfit_in = (TH1D*)ffit->Get("h_M_vals_prevfit");
for(unsigned int i=0; i<n_eta_bins; i++){
A_vals_fit(i) = -h_A_vals_prevfit_in->GetBinContent(i+1);
e_vals_fit(i) = -h_e_vals_prevfit_in->GetBinContent(i+1);
M_vals_fit(i) = -h_M_vals_prevfit_in->GetBinContent(i+1);
}
h_A_vals_prevfit->Add(h_A_vals_prevfit_in, -1.0);
h_e_vals_prevfit->Add(h_e_vals_prevfit_in, -1.0);
h_M_vals_prevfit->Add(h_M_vals_prevfit_in, -1.0);
ffit->Close();
}
else{
cout << "No mass fit file!" << endl;
}
}
if(useSmearFit){
TFile* ffit = TFile::Open(("./resolfit_"+tagSmearFit+"_"+runSmearFit+".root").c_str(), "READ");
if(ffit!=0){
cout << "Using fit results from " << std::string(ffit->GetName()) << " as MC smear" << endl;
TH1D* h_c_vals_prevfit_in = (TH1D*)ffit->Get("h_c_vals_prevfit");
TH1D* h_d_vals_prevfit_in = (TH1D*)ffit->Get("h_d_vals_prevfit");
for(unsigned int i=0; i<n_eta_bins; i++){
c_vals_fit(i) = h_c_vals_prevfit_in->GetBinContent(i+1);
d_vals_fit(i) = h_d_vals_prevfit_in->GetBinContent(i+1);
}
h_c_vals_prevfit->Add(h_c_vals_prevfit_in, +1.0);
h_d_vals_prevfit->Add(h_d_vals_prevfit_in, +1.0);
ffit->Close();
}
else{
cout << "No smear fit file!" << endl;
}
}
TFile* fout = TFile::Open(("./massscales_"+tag+"_"+run+".root").c_str(), firstIter<2 ? "RECREATE" : "UPDATE");
for(int iter=-1; iter<3; iter++){
if( !(iter>=firstIter && iter<=lastIter) ) continue;
cout << "Doing iter " << iter << endl;
//TTree* tree = new TTree("tree", "tree");
vector<string> in_files = {};
if(iter>=0){
if(y2016){
in_files = {
"/scratch/wmass/y2016/DYJetsToMuMu_H2ErratumFix_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MCPostVFP_TrackFitV722_NanoProdv6/240509_040854/0000/NanoV9MCPostVFP_*.root",
"/scratch/wmass/y2016/DYJetsToMuMu_H2ErratumFix_PDFExt_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MCPostVFP_TrackFitV722_NanoProdv6/240509_041233/0000/NanoV9MCPostVFP_*.root",
"/scratch/wmass/y2016/DYJetsToMuMu_H2ErratumFix_PDFExt_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MCPostVFP_TrackFitV722_NanoProdv6/240509_041233/0001/NanoV9MCPostVFP_*.root",
"/scratch/wmass/y2016/DYJetsToMuMu_H2ErratumFix_PDFExt_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MCPostVFP_TrackFitV722_NanoProdv6/240509_041233/0002/NanoV9MCPostVFP_*.root"
};
}
else if(y2017){
in_files = {
"/scratch/wmass/y2017/DYJetsToMuMu_H2ErratumFix_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MC2017_TrackFitV722_NanoProdv3/NanoV9MC2017_*.root"
};
}
else if(y2018){
in_files = {
"/scratch/wmass/y2018/DYJetsToMuMu_H2ErratumFix_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MC2018_TrackFitV722_NanoProdv3/240124_121800/0000/NanoV9MC2018_*.root",
"/scratch/wmass/y2018/DYJetsToMuMu_H2ErratumFix_TuneCP5_13TeV-powhegMiNNLO-pythia8-photos/NanoV9MC2018_TrackFitV722_NanoProdv3/240124_121800/0001/NanoV9MC2018_*.root"
};
}
}
else{
if(y2016){
in_files = {
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016FDataPostVFP_TrackFitV722_NanoProdv6/240509_051502/0000/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016GDataPostVFP_TrackFitV722_NanoProdv6/240509_051653/0000/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016GDataPostVFP_TrackFitV722_NanoProdv6/240509_051653/0001/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016GDataPostVFP_TrackFitV722_NanoProdv6/240509_051653/0002/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016GDataPostVFP_TrackFitV722_NanoProdv6/240509_051653/0003/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016GDataPostVFP_TrackFitV722_NanoProdv6/240509_051653/0004/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0000/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0001/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0002/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0003/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0004/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0005/NanoV9DataPostVFP_*.root",
"/scratch/wmass/y2016/SingleMuon/NanoV9Run2016HDataPostVFP_TrackFitV722_NanoProdv6/240509_051807/0006/NanoV9DataPostVFP_*.root"
};
}
else if(y2017){
in_files = {
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017B_TrackFitV722_NanoProdv3/240127_110915/0000/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017B_TrackFitV722_NanoProdv3/240127_110915/0001/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017B_TrackFitV722_NanoProdv3/240127_110915/0002/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017B_TrackFitV722_NanoProdv3/240127_110915/0003/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0000/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0001/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0002/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0003/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0004/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017C_TrackFitV722_NanoProdv3/240127_115941/0005/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017D_TrackFitV722_NanoProdv3/240127_120137/0000/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017D_TrackFitV722_NanoProdv3/240127_120137/0001/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017D_TrackFitV722_NanoProdv3/240127_120137/0002/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017E_TrackFitV722_NanoProdv3/240127_121346/0000/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017E_TrackFitV722_NanoProdv3/240127_121346/0001/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017E_TrackFitV722_NanoProdv3/240127_121346/0002/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017E_TrackFitV722_NanoProdv3/240127_121346/0003/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017E_TrackFitV722_NanoProdv3/240127_121346/0004/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0000/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0001/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0002/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0003/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0004/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0005/NanoV9Data2017_*.root",
"/scratch/wmass/y2017/SingleMuon/NanoV9Run2017F_TrackFitV722_NanoProdv3/240127_122701/0006/NanoV9Data2017_*.root"
};
}
else if(y2018){
in_files = {
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0000/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0001/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0002/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0003/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0004/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0005/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018A_TrackFitV722_NanoProdv3/231102_185937/0006/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018B_TrackFitV722_NanoProdv3/231103_093816/0000/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018B_TrackFitV722_NanoProdv3/231103_093816/0001/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018B_TrackFitV722_NanoProdv3/231103_093816/0002/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018C_TrackFitV722_NanoProdv3/231103_101410/0000/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018C_TrackFitV722_NanoProdv3/231103_101410/0001/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018C_TrackFitV722_NanoProdv3/231103_101410/0002/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0000/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0001/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0002/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0003/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0004/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0005/NanoV9Data2018_*.root",
"/scratch/wmass/y2018/SingleMuon/NanoV9Run2018D_TrackFitV722_NanoProdv3/231107_134901/0006/NanoV9Data2018_*.root"
};
}
}
ROOT::RDataFrame d( "Events", in_files );
unsigned int nslots = d.GetNSlots();
std::vector<TRandom3*> rans = {};
for(unsigned int i = 0; i < nslots; i++){
rans.emplace_back( new TRandom3(seed + i*10 + iter) );
}
auto dlast = std::make_unique<RNode>(d);
// MC
if(iter>=0){
dlast = std::make_unique<RNode>(dlast->Define("idxs", [&](UInt_t nMuon, RVecB Muon_looseId, RVecF Muon_dxybs, RVecB Muon_isGlobal,
RVecB Muon_highPurity, RVecB Muon_mediumId, RVecF Muon_pfRelIso04_all,
RVecF Muon_pt, RVecF Muon_eta)->RVecUI {
RVecUI out;
for(unsigned int i = 0; i < nMuon; i++){
if( Muon_looseId[i] && TMath::Abs(Muon_dxybs[i]) < 0.05 && Muon_isGlobal[i] && Muon_highPurity[i] && Muon_mediumId[i] && Muon_pfRelIso04_all[i]<0.15 &&
Muon_pt[i] >= pt_edges[0] && Muon_pt[i] < pt_edges[ n_pt_bins ] && Muon_eta[i]>=eta_edges[0] && Muon_eta[i]<=eta_edges[ n_eta_bins ] ){
out.emplace_back(i);
}
}
return out;
}, {"nMuon", "Muon_looseId", "Muon_dxybs", "Muon_isGlobal",
"Muon_highPurity","Muon_mediumId", "Muon_pfRelIso04_all",
useKf ? "Muon_pt" : "Muon_cvhidealPt", useKf ? "Muon_eta" : "Muon_cvhidealEta" } ));
dlast = std::make_unique<RNode>(dlast->Filter( [](RVecUI idxs, RVecI Muon_charge, bool HLT_IsoMu24 ){
if( idxs.size()!=2 || !HLT_IsoMu24) return false;
if( Muon_charge[idxs[0]]*Muon_charge[idxs[1]] > 0 ) return false;
return true;
}, {"idxs", "Muon_charge", "HLT_IsoMu24"} ));
dlast = std::make_unique<RNode>(dlast->Define("weight", [](float weight)->float{
return std::copysign(1.0, weight);
}, {"Generator_weight"} ));
dlast = std::make_unique<RNode>(dlast->DefineSlot("Muon_ksmear", [&](unsigned int nslot, RVecUI idxs,
RVecF Muon_pt, RVecF Muon_eta, RVecF Muon_phi, RVecF Muon_mass, RVecI Muon_charge,
UInt_t nGenPart, RVecI GenPart_status, RVecI GenPart_statusFlags, RVecI GenPart_pdgId,
RVecF GenPart_pt, RVecF GenPart_eta, RVecF GenPart_phi, RVecF GenPart_mass)->RVecF {
RVecF out;
unsigned int idxP = Muon_charge[idxs[0]]>0 ? idxs[0] : idxs[1];
unsigned int idxM = Muon_charge[idxs[0]]>0 ? idxs[1] : idxs[0];
ROOT::Math::PtEtaPhiMVector muP( Muon_pt[ idxP ], Muon_eta[ idxP ], Muon_phi[ idxP ], Muon_mass[ idxP ] );
ROOT::Math::PtEtaPhiMVector muM( Muon_pt[ idxM ], Muon_eta[ idxM ], Muon_phi[ idxM ], Muon_mass[ idxM ] );
ROOT::Math::PtEtaPhiMVector gmuP;
ROOT::Math::PtEtaPhiMVector gmuM;
for(unsigned int i = 0; i < nGenPart; i++){
bool isGoodGenPart = (GenPart_status[i]==1 && (GenPart_statusFlags[i] & 1 || (GenPart_statusFlags[i] & (1<<5))) && TMath::Abs(GenPart_pdgId[i])==13);
if(!isGoodGenPart) continue;
ROOT::Math::PtEtaPhiMVector gen(GenPart_pt[i], GenPart_eta[i], GenPart_phi[i], GenPart_mass[i]);
if( ROOT::Math::VectorUtil::DeltaR(gen, muP) < 0.1 && ROOT::Math::VectorUtil::DeltaR(gen, muM) > 0.1) gmuP = gen;
else if( ROOT::Math::VectorUtil::DeltaR(gen, muP) > 0.1 && ROOT::Math::VectorUtil::DeltaR(gen, muM) < 0.1) gmuM = gen;
}
if( gmuP.Pt()>10. && gmuM.Pt()>10.){
float kmuP = 1./muP.Pt();
float kmuM = 1./muM.Pt();
float kgmuP = 1./gmuP.Pt();
float kgmuM = 1./gmuM.Pt();
float scale_smear0P = 1.0;
float scale_smear0M = 1.0;
float extraSmear0P = 0.0;
float extraSmear0M = 0.0;
unsigned int ietaP = n_eta_bins;
for(unsigned int ieta_p = 0; ieta_p<n_eta_bins; ieta_p++){
float eta_p_low = eta_edges[ieta_p];
float eta_p_up = eta_edges[ieta_p+1];
if( muP.Eta()>=eta_p_low && muP.Eta()<eta_p_up) ietaP = ieta_p;
}
unsigned int ietaM = n_eta_bins;
for(unsigned int ieta_m = 0; ieta_m<n_eta_bins; ieta_m++){
float eta_m_low = eta_edges[ieta_m];
float eta_m_up = eta_edges[ieta_m+1];
if( muM.Eta()>=eta_m_low && muM.Eta()<eta_m_up) ietaM = ieta_m;
}
if(ietaP<n_eta_bins && ietaM<n_eta_bins){
scale_smear0P = (1. + A_vals_fit(ietaP) + e_vals_fit(ietaP)*kmuP - M_vals_fit(ietaP)/kmuP);
scale_smear0M = (1. + A_vals_fit(ietaM) + e_vals_fit(ietaM)*kmuM + M_vals_fit(ietaM)/kmuM);
//cout << "smear0:" << scale_smear0P << ": " << 1 << " + " << A_vals_fit(ietaP) << " + " << e_vals_fit(ietaP)*kmuP << " - " << M_vals_fit(ietaP)/kmuP << endl;
//cout << "smear1:" << scale_smear1P << ": " << 1 << " + " << A_vals_nom(ietaP) << " + " << e_vals_nom(ietaP)*kmuP << " - " << M_vals_nom(ietaP)/kmuP << endl;
if(useSmearFit){
extraSmear0P = TMath::Sqrt( TMath::Max( 1.0 + c_vals_fit(ietaP) + d_vals_fit(ietaP)*kmuP, 0.0) ) - 1.0;
extraSmear0M = TMath::Sqrt( TMath::Max( 1.0 + c_vals_fit(ietaM) + d_vals_fit(ietaM)*kmuM, 0.0) ) - 1.0;
//cout << "extraSmear0P: sqrt( max(1.0 + " << c_vals_fit(ietaP) << " + " << d_vals_fit(ietaP)*kmuP << ")) - 1.0 = " << extraSmear0P << endl;
//cout << "extraSmear0M: sqrt( max(1.0 + " << c_vals_fit(ietaM) << " + " << d_vals_fit(ietaM)*kmuM << ")) - 1.0 = " << extraSmear0M << endl;
}
}
float kmuPsmear0 = (kgmuP + (kmuP - kgmuP)*(1.0 + extraSmear0P))*scale_smear0P;
float kmuMsmear0 = (kgmuM + (kmuM - kgmuM)*(1.0 + extraSmear0M))*scale_smear0M;
out.emplace_back( kmuPsmear0 );
out.emplace_back( kmuMsmear0 );
}
else{
out.emplace_back(0.0);
out.emplace_back(0.0);
}
return out;
}, {"idxs",
useKf ? "Muon_pt" : "Muon_cvhidealPt", useKf ? "Muon_eta" : "Muon_cvhidealEta", useKf ? "Muon_phi" : "Muon_cvhidealPhi", "Muon_mass", "Muon_charge",
"nGenPart", "GenPart_status", "GenPart_statusFlags", "GenPart_pdgId",
"GenPart_pt", "GenPart_eta", "GenPart_phi", "GenPart_mass"} ));
dlast = std::make_unique<RNode>(dlast->Define("indexes", [&](RVecUI idxs, RVecF Muon_pt, RVecF Muon_eta, RVecI Muon_charge, RVecF Muon_ksmear)-> RVecUI {
unsigned int idxP = Muon_charge[idxs[0]]>0 ? idxs[0] : idxs[1];
unsigned int idxM = Muon_charge[idxs[0]]>0 ? idxs[1] : idxs[0];
float ptP = Muon_pt[idxP];
float ptM = Muon_pt[idxM];
float ksmear0P = Muon_ksmear[0]>0. ? Muon_ksmear[0] : 1./(pt_edges[0]-0.01);
float ksmear0M = Muon_ksmear[1]>0. ? Muon_ksmear[1] : 1./(pt_edges[0]-0.01);
float etaP = Muon_eta[idxP];
float etaM = Muon_eta[idxM];
RVecUI out;
out.emplace_back(n_bins);
out.emplace_back(n_bins);
unsigned int ibin = 0;
for(unsigned int ieta_p = 0; ieta_p<n_eta_bins; ieta_p++){
float eta_p_low = eta_edges[ieta_p];
float eta_p_up = eta_edges[ieta_p+1];
for(unsigned int ipt_p = 0; ipt_p<n_pt_bins; ipt_p++){
float pt_p_low = pt_edges[ipt_p];
float pt_p_up = pt_edges[ipt_p+1];
for(unsigned int ieta_m = 0; ieta_m<n_eta_bins; ieta_m++){
float eta_m_low = eta_edges[ieta_m];
float eta_m_up = eta_edges[ieta_m+1];
for(unsigned int ipt_m = 0; ipt_m<n_pt_bins; ipt_m++){
float pt_m_low = pt_edges[ipt_m];
float pt_m_up = pt_edges[ipt_m+1];
if( etaP>=eta_p_low && etaP<eta_p_up &&
etaM>=eta_m_low && etaM<eta_m_up &&
ptP>=pt_p_low && ptP<pt_p_up &&
ptM>=pt_m_low && ptM<pt_m_up
) out[0] = ibin;
if( etaP>=eta_p_low && etaP<eta_p_up &&
etaM>=eta_m_low && etaM<eta_m_up &&
1./ksmear0P>=pt_p_low && 1./ksmear0P<pt_p_up &&
1./ksmear0M>=pt_m_low && 1./ksmear0M<pt_m_up
) out[1] = ibin;
ibin++;
}
}
}
}
return out;
}, {"idxs", useKf ? "Muon_pt" : "Muon_cvhidealPt", useKf ? "Muon_eta" : "Muon_cvhidealEta", "Muon_charge", "Muon_ksmear"} ));
for(unsigned int r = 0 ; r<recos.size(); r++){
dlast = std::make_unique<RNode>(dlast->Define( TString(("index_"+recos[r]).c_str()), [r](RVecUI indexes){
return indexes.at(r);
}, {"indexes"} ));
}
dlast = std::make_unique<RNode>(dlast->Define("masses", [&](RVecUI idxs,
RVecF Muon_pt, RVecF Muon_eta, RVecF Muon_phi, RVecF Muon_mass, RVecI Muon_charge,
UInt_t nGenPart, RVecI GenPart_status, RVecI GenPart_statusFlags, RVecI GenPart_pdgId,
RVecF GenPart_pt, RVecF GenPart_eta, RVecF GenPart_phi, RVecF GenPart_mass,
RVecF Muon_ksmear)->RVecF {
RVecF out;
unsigned int idxP = Muon_charge[idxs[0]]>0 ? idxs[0] : idxs[1];
unsigned int idxM = Muon_charge[idxs[0]]>0 ? idxs[1] : idxs[0];
ROOT::Math::PtEtaPhiMVector muP( Muon_pt[ idxP ], Muon_eta[ idxP ], Muon_phi[ idxP ], Muon_mass[ idxP ] );
ROOT::Math::PtEtaPhiMVector muM( Muon_pt[ idxM ], Muon_eta[ idxM ], Muon_phi[ idxM ], Muon_mass[ idxM ] );
ROOT::Math::PtEtaPhiMVector gmuP;
ROOT::Math::PtEtaPhiMVector gmuM;
for(unsigned int i = 0; i < nGenPart; i++){
bool isGoodGenPart = (GenPart_status[i]==1 && (GenPart_statusFlags[i] & 1 || (GenPart_statusFlags[i] & (1<<5))) && TMath::Abs(GenPart_pdgId[i])==13);
if(!isGoodGenPart) continue;
ROOT::Math::PtEtaPhiMVector gen(GenPart_pt[i], GenPart_eta[i], GenPart_phi[i], GenPart_mass[i]);
if( ROOT::Math::VectorUtil::DeltaR(gen, muP) < 0.1 && ROOT::Math::VectorUtil::DeltaR(gen, muM) > 0.1) gmuP = gen;
else if( ROOT::Math::VectorUtil::DeltaR(gen, muP) > 0.1 && ROOT::Math::VectorUtil::DeltaR(gen, muM) < 0.1) gmuM = gen;
}
if( gmuP.Pt()>10. && gmuM.Pt()>10.){
out.emplace_back( (gmuP + gmuM).M() );
out.emplace_back( (muP + muM).M() );
float ksmear0P = Muon_ksmear[0]>0. ? Muon_ksmear[0] : 1./(pt_edges[0]-0.01);
float ksmear0M = Muon_ksmear[1]>0. ? Muon_ksmear[1] : 1./(pt_edges[0]-0.01);
ROOT::Math::PtEtaPhiMVector muP_smear0( 1./ksmear0P,
Muon_eta[ idxP ], Muon_phi[ idxP ], Muon_mass[ idxP ] );
ROOT::Math::PtEtaPhiMVector muM_smear0( 1./ksmear0M,
Muon_eta[ idxM ], Muon_phi[ idxM ], Muon_mass[ idxM ] );
out.emplace_back( (muP_smear0 + muM_smear0).M() );
}
return out;
}, {"idxs",
useKf ? "Muon_pt" : "Muon_cvhidealPt", useKf ? "Muon_eta" : "Muon_cvhidealEta", useKf ? "Muon_phi" : "Muon_cvhidealPhi", "Muon_mass", "Muon_charge",
"nGenPart", "GenPart_status", "GenPart_statusFlags", "GenPart_pdgId",
"GenPart_pt", "GenPart_eta", "GenPart_phi", "GenPart_mass",
"Muon_ksmear"} ));
for(unsigned int r = 0 ; r<recos.size(); r++){
unsigned int mpos = idx_map.at(recos[r]);
dlast = std::make_unique<RNode>(dlast->Define(TString( (recos[r]+"_m").c_str() ), [mpos](RVecF masses){
return masses.size()>0 ? masses.at( mpos ) : -99.;
}, {"masses"} ));
dlast = std::make_unique<RNode>(dlast->Define(TString( (recos[r]+"_dm").c_str() ), [mpos](RVecF masses){
return masses.size()>0 ? masses.at( mpos ) - masses.at(0) : -99.;
}, {"masses"} ));
}
dlast = std::make_unique<RNode>(dlast->Define("weights_jac", [n_bins,recos,h_map,hCB_map,idx_map,useCBpdf](RVecF masses, RVecUI indexes)->RVecF{
RVecF out;
if(masses.size()==0){
for(unsigned int r = 0 ; r<recos.size(); r++){
out.emplace_back(0.0);
out.emplace_back(0.0);
}
return out;
}
float gen_m = masses.at(0);
if(!useCBpdf){
for(unsigned int r = 0 ; r<recos.size(); r++){
unsigned int rpos = idx_map.at(recos[r]);
TH1D* h_mean = h_map.at("mean_"+recos[r]);
TH1D* h_rms = h_map.at("rms_"+recos[r]);
float reco_m = masses.at( rpos );
float reco_delta = 0.;
float reco_sigma = 0.;
if(indexes[r]<n_bins){
reco_delta = h_mean->GetBinContent(indexes[r]+1);
reco_sigma = h_rms->GetBinContent(indexes[r]+1);
}
float reco_jscale = reco_sigma>0. ? +(reco_m - (gen_m+reco_delta) )*(gen_m+reco_delta)/reco_sigma/reco_sigma : 0.0;
float reco_jwidth = reco_sigma>0. ? +(reco_m - (gen_m+reco_delta) )*(reco_m - (gen_m+reco_delta) )/reco_sigma/reco_sigma - 1.0 : 0.0;
out.emplace_back(reco_jscale);
out.emplace_back(reco_jwidth);
}
}
else{
for(unsigned int r = 0 ; r<recos.size(); r++){
TH2D* h_lnder = hCB_map.at("lnder_"+recos[r]);
unsigned int rpos = idx_map.at(recos[r]);
float reco_m = masses.at( rpos );
float dm = reco_m-gen_m;
int bin_lnder = h_lnder->GetYaxis()->FindBin( dm );
if( bin_lnder==0 )
bin_lnder = 1;
else if( bin_lnder==h_lnder->GetYaxis()->GetNbins()+1 )
bin_lnder = h_lnder->GetYaxis()->GetNbins();
float lnder = indexes[r]<n_bins ? h_lnder->GetBinContent(indexes[r]+1, bin_lnder) : 0.;
float reco_jscale = -lnder*reco_m;
float reco_jwidth = -(1.0 + lnder*dm);
out.emplace_back(reco_jscale);
out.emplace_back(reco_jwidth);
}
}
return out;
}, {"masses", "indexes"} ));
for(unsigned int r = 0 ; r<recos.size(); r++){
unsigned int jpos = (idx_map.at(recos[r])-1)*2;
dlast = std::make_unique<RNode>(dlast->Define( TString((recos[r]+"_jscale_weight").c_str()), [jpos](RVecF weights_jac, float weight)->float{
return weights_jac.at( jpos )*weight;
}, {"weights_jac", "weight" } ));
dlast = std::make_unique<RNode>(dlast->Define( TString((recos[r]+"_jwidth_weight").c_str()), [jpos](RVecF weights_jac, float weight)->float{
return weights_jac.at( jpos+1 )*weight;
}, {"weights_jac", "weight" } ));
}
}
// data
else{
dlast = std::make_unique<RNode>(dlast->Define("idxs", [&](UInt_t nMuon, RVecB Muon_looseId, RVecF Muon_dxybs, RVecB Muon_isGlobal,
RVecB Muon_highPurity, RVecB Muon_mediumId, RVecF Muon_pfRelIso04_all,
RVecF Muon_pt, RVecF Muon_eta)->RVecUI {
RVecUI out;
for(unsigned int i = 0; i < nMuon; i++){
if( Muon_looseId[i] && TMath::Abs(Muon_dxybs[i]) < 0.05 && Muon_isGlobal[i] && Muon_highPurity[i] && Muon_mediumId[i] && Muon_pfRelIso04_all[i]<0.15 &&
Muon_pt[i] >= pt_edges[0] && Muon_pt[i] < pt_edges[ n_pt_bins ] && Muon_eta[i]>=eta_edges[0] && Muon_eta[i]<=eta_edges[ n_eta_bins ] ){
out.emplace_back(i);
}
}
return out;
}, {"nMuon", "Muon_looseId", "Muon_dxybs", "Muon_isGlobal",
"Muon_highPurity","Muon_mediumId", "Muon_pfRelIso04_all",
useKf ? "Muon_pt" : "Muon_cvhPt", useKf ? "Muon_eta" : "Muon_cvhEta" } ));
dlast = std::make_unique<RNode>(dlast->Filter( [](RVecUI idxs, RVecI Muon_charge, bool HLT_IsoMu24 ){
if( idxs.size()!=2 || !HLT_IsoMu24) return false;
if( Muon_charge[idxs[0]]*Muon_charge[idxs[1]] > 0 ) return false;
return true;
}, {"idxs", "Muon_charge", "HLT_IsoMu24"} ));
dlast = std::make_unique<RNode>(dlast->Define("weight", []()->float{ return 1.0; }, {} ));
dlast = std::make_unique<RNode>(dlast->Define("index_data", [&](RVecUI idxs, RVecF Muon_pt, RVecF Muon_eta, RVecI Muon_charge)-> unsigned int {
unsigned int idxP = Muon_charge[idxs[0]]>0 ? idxs[0] : idxs[1];
unsigned int idxM = Muon_charge[idxs[0]]>0 ? idxs[1] : idxs[0];
float ptP = Muon_pt[idxP];
float ptM = Muon_pt[idxM];
float etaP = Muon_eta[idxP];
float etaM = Muon_eta[idxM];
unsigned int out = n_bins;
unsigned int ibin = 0;
for(unsigned int ieta_p = 0; ieta_p<n_eta_bins; ieta_p++){
float eta_p_low = eta_edges[ieta_p];
float eta_p_up = eta_edges[ieta_p+1];
for(unsigned int ipt_p = 0; ipt_p<n_pt_bins; ipt_p++){
float pt_p_low = pt_edges[ipt_p];
float pt_p_up = pt_edges[ipt_p+1];
for(unsigned int ieta_m = 0; ieta_m<n_eta_bins; ieta_m++){
float eta_m_low = eta_edges[ieta_m];
float eta_m_up = eta_edges[ieta_m+1];
for(unsigned int ipt_m = 0; ipt_m<n_pt_bins; ipt_m++){
float pt_m_low = pt_edges[ipt_m];
float pt_m_up = pt_edges[ipt_m+1];
if( etaP>=eta_p_low && etaP<eta_p_up &&
etaM>=eta_m_low && etaM<eta_m_up &&
ptP>=pt_p_low && ptP<pt_p_up &&
ptM>=pt_m_low && ptM<pt_m_up
) out = ibin;
ibin++;
}
}
}
}
return out;
}, {"idxs", useKf ? "Muon_pt" : "Muon_cvhPt", useKf ? "Muon_eta" : "Muon_cvhEta", "Muon_charge"} ));
dlast = std::make_unique<RNode>(dlast->Define("data_m", [&](RVecUI idxs,
RVecF Muon_pt, RVecF Muon_eta, RVecF Muon_phi, RVecF Muon_mass, RVecI Muon_charge)->float {
float out = 0.0;
unsigned int idxP = Muon_charge[idxs[0]]>0 ? idxs[0] : idxs[1];
unsigned int idxM = Muon_charge[idxs[0]]>0 ? idxs[1] : idxs[0];
ROOT::Math::PtEtaPhiMVector muP( Muon_pt[ idxP ], Muon_eta[ idxP ], Muon_phi[ idxP ], Muon_mass[ idxP ] );
ROOT::Math::PtEtaPhiMVector muM( Muon_pt[ idxM ], Muon_eta[ idxM ], Muon_phi[ idxM ], Muon_mass[ idxM ] );
out = (muP + muM).M();
return out;
}, {"idxs",
useKf ? "Muon_pt" : "Muon_cvhPt", useKf ? "Muon_eta" : "Muon_cvhEta", useKf ? "Muon_phi" : "Muon_cvhPhi", "Muon_mass", "Muon_charge"} ));
}
std::vector<ROOT::RDF::RResultPtr<TH1D> > histos1D;
std::vector<ROOT::RDF::RResultPtr<TH2D> > histos2D;
const int x_nbins = 40;
const double x_low = 70.0;
const double x_high = 110.0;
//histos1D.emplace_back(dlast->Histo1D({"h_gen_m", "nominal", x_nbins, x_low, x_high}, "gen_m", "weight"));
//histos1D.emplace_back(dlast->Histo1D({"h_reco_m", "nominal", x_nbins, x_low, x_high}, "reco_m", "weight"));
//histos1D.emplace_back(dlast->Histo1D({"h_smear_m", "nominal", x_nbins, x_low, x_high}, "smear_m", "weight"));
if(iter==-1){
histos2D.emplace_back(dlast->Histo2D({ "h_data_bin_m", "nominal", n_bins, 0, double(n_bins), x_nbins, x_low, x_high}, "index_data", "data_m", "weight" ));
auto colNames = dlast->GetColumnNames();
double total = *(dlast->Count());
std::cout << colNames.size() << " columns created. Total event count is " << total << std::endl;
}
else if(iter==0){
for(unsigned int r = 0 ; r<recos.size(); r++){
histos2D.emplace_back(dlast->Histo2D({ "h_"+TString(recos[r].c_str())+"_bin_m", "nominal", n_bins, 0, double(n_bins), x_nbins, x_low, x_high}, "index_"+TString(recos[r].c_str()), TString(recos[r].c_str())+"_m", "weight" ));
histos2D.emplace_back(dlast->Histo2D({ "h_"+TString(recos[r].c_str())+"_bin_dm", "nominal", n_bins, 0, double(n_bins), 40, -10.0, 10.0}, "index_"+TString(recos[r].c_str()), TString(recos[r].c_str())+"_dm", "weight"));
}
auto colNames = dlast->GetColumnNames();
double total = *(dlast->Count());
std::cout << colNames.size() << " columns created. Total event count is " << total << std::endl;
}
else if(iter==1){
for(unsigned int r = 0 ; r<recos.size(); r++){
if(recos[r]!="smear0") continue;
histos2D.emplace_back(dlast->Histo2D({"h_"+TString(recos[r].c_str())+"_bin_jac_scale", "nominal", n_bins, 0, double(n_bins), x_nbins, x_low, x_high}, "index_"+TString(recos[r].c_str()), TString(recos[r].c_str())+"_m", TString(recos[r].c_str())+"_jscale_weight"));
histos2D.emplace_back(dlast->Histo2D({"h_"+TString(recos[r].c_str())+"_bin_jac_width", "nominal", n_bins, 0, double(n_bins), x_nbins, x_low, x_high}, "index_"+TString(recos[r].c_str()), TString(recos[r].c_str())+"_m", TString(recos[r].c_str())+"_jwidth_weight"));
}
}
if(iter<2){
fout->cd();
std::cout << "Writing histos..." << std::endl;
double lumiMC = lumiMC2016;
if(y2017) lumiMC = lumiMC2017;
else if(y2018) lumiMC = lumiMC2018;
double sf = lumi>0. ? lumi/lumiMC : 1.0; //double(lumi)/double(minNumEvents);
for(auto h : histos1D){
if(iter>=0) h->Scale(sf);
string h_name = std::string(h->GetName());
h->Write();
}
for(auto h : histos2D){
if(iter>=0) h->Scale(sf);
string h_name = std::string(h->GetName());
std::cout << "Total number of events in 2D histo " << h_name << ": " << h->GetEntries() << std::endl;
h->Write();
}
std::cout << "Total slots: " << dlast->GetNSlots() << std::endl;
}
// fill histos
if(iter==0){
cout << "Writing aux files" << endl;
h_pt_edges->Write();
h_eta_edges->Write();
h_A_vals_nom->Write();
h_e_vals_nom->Write();
h_M_vals_nom->Write();
h_A_vals_prevfit->Write();
h_e_vals_prevfit->Write();
h_M_vals_prevfit->Write();
h_c_vals_prevfit->Write();
h_d_vals_prevfit->Write();
for(unsigned int r = 0 ; r<recos.size(); r++){
TH2D* h_reco_dm = (TH2D*)fout->Get(TString( ("h_"+recos[r]+"_bin_dm").c_str()) );
TH2D* h_reco_m = (TH2D*)fout->Get(TString( ("h_"+recos[r]+"_bin_m").c_str()) );
if( h_reco_dm==0 || h_reco_m==0 ){
cout << "h_reco_dm/h_reco_m NOT FOUND" << endl;
continue;
}
if(useCBpdf){
hCB_map["lnder_"+recos[r]] = new TH2D( TString( ("h_lnder_"+recos[r]+"_bin_dm").c_str() ),"", n_bins, 0, double(n_bins),
h_reco_dm->GetXaxis()->GetNbins()*2, h_reco_dm->GetXaxis()->GetXmin(), h_reco_dm->GetXaxis()->GetXmax());
}
else{
h_map["mean_"+recos[r]] = new TH1D( TString( ("h_mean_"+recos[r]+"_bin_dm").c_str() ),"", n_bins, 0, double(n_bins));
h_map["rms_"+recos[r]] = new TH1D( TString( ("h_rms_"+recos[r]+"_bin_dm").c_str() ), "", n_bins, 0, double(n_bins));
}
h_map["mask_"+recos[r]] = new TH1D( TString( ("h_mask_"+recos[r]+"_bin_dm").c_str() ),"", n_bins, 0, double(n_bins));
for(unsigned int i = 0; i<n_bins; i++ ){
if(i%1000==0){
if(!useCBpdf)
cout << "Doing gaus fit for bin " << i << " / " << n_bins << endl;
else
cout << "Doing CB fit for bin " << i << " / " << n_bins << endl;
}
TString projname(Form("bin_%d_", i));
projname += TString( recos[r].c_str() );
TH1D* hi = (TH1D*)h_reco_dm->ProjectionY( projname+"_dm", i+1, i+1 );
TH1D* hi_m = (TH1D*)h_reco_m->ProjectionY( projname+"_m", i+1, i+1 );
// gaus pdf
if(!useCBpdf){
double mean_i = 0.0;
double meanerr_i = 0.0;
double rms_i = 0.0;
double rmserr_i = 0.0;
//cout << hi_m->Integral() << ", " << hi->Integral() << ", " << hi_m->GetMean() << endl;
if( hi_m->Integral() > minNumEvents && hi->Integral() > minNumEvents && hi_m->GetMean()>75. && hi_m->GetMean()<105. ){
h_map.at("mask_"+recos[r])->SetBinContent(i+1, 1);
TF1* gf = new TF1("gf","[0]/TMath::Sqrt(2*TMath::Pi())/[2]*TMath::Exp( -0.5*(x-[1])*(x-[1])/[2]/[2] )",
hi->GetXaxis()->GetBinLowEdge(1), hi->GetXaxis()->GetBinUpEdge( hi->GetXaxis()->GetNbins() ));
gf->SetParameter(0, hi->Integral());
gf->SetParameter(1, hi->GetMean());
gf->SetParameter(2, hi->GetRMS() );
float m_min = nRMSforGausFit>0. ? TMath::Max(-nRMSforGausFit*hi->GetRMS(), -6.0) : -6.0;
float m_max = nRMSforGausFit>0. ? TMath::Min(+nRMSforGausFit*hi->GetRMS(), +6.0) : +6.0;
hi->Fit("gf", "QR", "", m_min, m_max );
mean_i = gf->GetParameter(1);
meanerr_i = gf->GetParError(1);
rms_i = TMath::Abs(gf->GetParameter(2));
rmserr_i = gf->GetParError(2);
//cout << "Fit " << mean_i << endl;
delete gf;
}
else{
h_map.at("mask_"+recos[r])->SetBinContent(i+1, 0);
}
h_map.at("mean_"+recos[r])->SetBinContent(i+1, mean_i);
h_map.at("mean_"+recos[r])->SetBinError(i+1, meanerr_i);
h_map.at("rms_"+recos[r])->SetBinContent(i+1, rms_i);
h_map.at("rms_"+recos[r])->SetBinError(i+1, rmserr_i);
}
// CB pdf
else{
if( !(hi_m->Integral() > minNumEvents && hi->Integral() > minNumEvents && hi_m->GetMean()>75. && hi_m->GetMean()<105.) ){
h_map.at("mask_"+recos[r])->SetBinContent(i+1, 0);
continue;
}
h_map.at("mask_"+recos[r])->SetBinContent(i+1, 1);
//continue;
bool verbosity = false;
int printlevel = -1;
RooMsgService::instance().setGlobalKillBelow(RooFit::FATAL);
gErrorIgnoreLevel = 6001;
int status = -99;
int flag = -99;
float fr = -99.;
RooRealVar mass("mass", Form("mass for bin %d", i), hi->GetXaxis()->GetXmin(), hi->GetXaxis()->GetXmax());
mass.setRange("r1", -10.0, 10.0);
RooDataHist data("data", "", RooArgList(mass), hi );
RooRealVar x0("x0", "", hi->GetMean(), mass.getMin(), mass.getMax() );
RooRealVar sigmaL("sigmaL", "", hi->GetRMS(), hi->GetRMS()*0.5, hi->GetRMS()*2 );
RooRealVar sigmaR("sigmaR", "", hi->GetRMS(), hi->GetRMS()*0.5, hi->GetRMS()*2 );
RooRealVar alphaL("alphaL", "", 1.0, 0.2, +10 );
RooRealVar alphaR("alphaR", "", 1.0, 0.2, +10 );
RooRealVar nL("nL", "", 2, 1, 100 );
RooRealVar nR("nR", "", 2, 1, 100 );
RooRealVar tau("tau", "", 0., -10, 10);
RooRealVar frac("frac", "", 0.9, 0., 1.);
RooCrystalBall pdf("pdf", "", mass, x0, sigmaL, sigmaR, alphaL, nL, alphaR, nR);
RooGaussian gaus("pdf", "", mass, x0, sigmaL);
RooExponential bkg("bkg", "", mass, tau);
RooAddPdf pdfTot("pdfTot", "", {pdf, bkg}, frac);
TString rname = "r1";
pdfTot.fixCoefRange( rname.Data() );
pdfTot.fixCoefNormalization(mass);
std::shared_ptr<RooFitResult> rfit{pdfTot.fitTo(data,
InitialHesse(true),
Minimizer("Minuit2"),
Range( rname.Data() ),
Save(), SumW2Error(true),
PrintLevel(printlevel),
Verbose(verbosity) )};
status = rfit->status();
fr = frac.getVal();
flag = 0;
if(fr>minFrac){
std::shared_ptr<RooFitResult> rn{pdf.fitTo(data,
InitialHesse(true),
Minimizer("Minuit2"),
Range( rname.Data() ),
Save(), SumW2Error(true),
PrintLevel(printlevel),
Verbose(verbosity) )};
rfit = rn;
flag = 1;
}
status = rfit->status();
if(status!=0){
rname = "r2";
mass.setRange( rname.Data(), -8.0, 8.0);
std::shared_ptr<RooFitResult> rn{pdf.fitTo(data,
InitialHesse(true),
Minimizer("Minuit2"),
Range( rname.Data() ),
Save(), SumW2Error(true),
PrintLevel(printlevel),
Verbose(verbosity) )};
rfit = rn;
flag = 2;
}
status = rfit->status();
if(status!=0){
rname = "r3";
mass.setRange( rname.Data() , -6.0, 6.0);
std::shared_ptr<RooFitResult> rn{pdf.fitTo(data,
InitialHesse(true),
Minimizer("Minuit2"),
Range( rname.Data() ),
Save(), SumW2Error(true),
PrintLevel(printlevel),
Verbose(verbosity) )};
rfit = rn;
flag = 3;
}
status = rfit->status();
if(status!=0){
rname = "r4";
mass.setRange( rname.Data() , -3.0, 3.0);
std::shared_ptr<RooFitResult> rn{gaus.fitTo(data,
InitialHesse(true),
Minimizer("Minuit2"),
Range( rname.Data() ),
Save(), SumW2Error(true),
PrintLevel(printlevel),
Verbose(verbosity) )};
rfit = rn;
flag = 4;
}
status = rfit->status();
RooDerivative* der = 0;
if(flag==0){
der = pdfTot.derivative(mass, 1, 0.00005 );
}
else if(flag==1 || flag==2 || flag==3){
der = pdf.derivative(mass, 1, 0.00005 );
}
else if(flag==4){
der = gaus.derivative(mass, 1, 0.00005 );
}
TH2D* h_der = hCB_map.at("lnder_"+recos[r]);
for(int ib=0; ib<h_der->GetYaxis()->GetNbins();ib++){
double x = h_der->GetYaxis()->GetBinCenter(ib+1);
mass.setVal( x );
double fprime = der->getVal();
double f = 0.;
if(flag==0)
f = pdfTot.getVal();
else if(flag==1 || flag==2 || flag==3)
f = pdf.getVal();
else if(flag==4)
f = gaus.getVal();
h_der->SetBinContent(i+1,ib+1, fprime/f);
}
}
delete hi;
delete hi_m;
}
}
fout->cd();
for(unsigned int r = 0 ; r<recos.size(); r++){
h_map["mask_"+recos[r]]->Write();
if(!useCBpdf){
h_map["mean_"+recos[r]]->Write();
h_map["rms_"+recos[r]]->Write();
}
//else{
// hCB_map["lnder_"+recos[r]]->Write();
//}
}