-
Notifications
You must be signed in to change notification settings - Fork 26
/
ExampleRun.C
407 lines (307 loc) · 13.4 KB
/
ExampleRun.C
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
#include "ExampleRun.h"
ExampleRun::ExampleRun(){
}
void ExampleRun::initializeAnalyzer(){
//================================================================
//==== Example 1
//==== Dimuon Z-peak events with two muon IDs, with systematics
//================================================================
//==== if you use "--userflags RunSyst" with SKFlat.py, HasFlag("RunSyst") will return "true"
RunSyst = HasFlag("RunSyst");
cout << "[ExampleRun::initializeAnalyzer] RunSyst = " << RunSyst << endl;
//==== Dimuon Z-peak with two muon IDs
//==== I defined "vector<TString> MuonIDs;" in Analyzers/include/ExampleRun.h
MuonIDs = {
"POGMedium",
"POGTight"
};
//==== corresponding Muon ID SF Keys for mcCorr->MuonID_SF()
MuonIDSFKeys = {
"NUM_MediumID_DEN_TrackerMuons",
"NUM_TightID_DEN_TrackerMuons",
};
//==== At this point, sample informations (e.g., IsDATA, DataStream, MCSample, or DataYear) are all set
//==== You can define sample-dependent or year-dependent variables here
//==== (Example) Year-dependent variables
//==== I defined "TString IsoMuTriggerName;" and "double TriggerSafePtCut;" in Analyzers/include/ExampleRun.h
//==== IsoMuTriggerName is a year-dependent variable, and you don't want to do "if(Dataer==~~)" for every event (let's save cpu time).
//==== Then, do it here, which only ran once for each macro
if(DataYear==2016){
IsoMuTriggerName = "HLT_IsoMu24_v";
TriggerSafePtCut = 26.;
}
else if(DataYear==2017){
IsoMuTriggerName = "HLT_IsoMu27_v";
TriggerSafePtCut = 29.;
}
cout << "[ExampleRun::initializeAnalyzer] IsoMuTriggerName = " << IsoMuTriggerName << endl;
cout << "[ExampleRun::initializeAnalyzer TriggerSafePtCut = " << TriggerSafePtCut << endl;
//==== B-Tagging
//==== add taggers and WP that you want to use in analysis
std::vector<JetTagging::Parameters> jtps;
//==== If you want to use 1a or 2a method,
jtps.push_back( JetTagging::Parameters(JetTagging::DeepCSV, JetTagging::Medium, JetTagging::incl, JetTagging::comb) );
//==== set
mcCorr->SetJetTaggingParameters(jtps);
//================================
//==== Example 2
//==== Using new PDF
//==== It consumes so much time, so only being actiavted with --userflags RunNewPDF
//================================
RunNewPDF = HasFlag("RunNewPDF");
cout << "[ExampleRun::initializeAnalyzer] RunNewPDF = " << RunNewPDF << endl;
if(RunNewPDF && !IsDATA){
LHAPDFHandler LHAPDFHandler_Prod;
LHAPDFHandler_Prod.CentralPDFName = "NNPDF31_nnlo_hessian_pdfas";
LHAPDFHandler_Prod.init();
LHAPDFHandler LHAPDFHandler_New;
LHAPDFHandler_New.CentralPDFName = "NNPDF31_nlo_hessian_pdfas";
LHAPDFHandler_New.ErrorSetMember_Start = 1;
LHAPDFHandler_New.ErrorSetMember_End = 100;
LHAPDFHandler_New.AlphaSMember_Down = 101;
LHAPDFHandler_New.AlphaSMember_Up = 102;
LHAPDFHandler_New.init();
pdfReweight->SetProdPDF( LHAPDFHandler_Prod.PDFCentral );
pdfReweight->SetNewPDF( LHAPDFHandler_New.PDFCentral );
pdfReweight->SetNewPDFErrorSet( LHAPDFHandler_New.PDFErrorSet );
pdfReweight->SetNewPDFAlphaS( LHAPDFHandler_New.PDFAlphaSDown, LHAPDFHandler_New.PDFAlphaSUp );
}
//================================================
//==== Example 3
//==== How to estimate xsec errors (PDF & Scale)
//==== For example, MET
//================================================
RunXSecSyst = HasFlag("RunXSecSyst");
cout << "[ExampleRun::initializeAnalyzer] RunXSecSyst = " << RunXSecSyst << endl;
}
ExampleRun::~ExampleRun(){
//==== Destructor of this Analyzer
}
void ExampleRun::executeEvent(){
//================================================================
//==== Example 1
//==== Dimuon Z-peak events with two muon IDs, with systematics
//================================================================
//==== *IMPORTANT TO SAVE CPU TIME*
//==== Every GetMuon() funtion first collect ALL MINIAOD muons with GetAllMuons(),
//==== and then check ID booleans.
//==== GetAllMuons not only loops over all MINIAOD muons, but also actually CONSTRUCT muon objects for each muons.
//==== We are now running systematics, and you don't want to do this for every systematic sources
//==== So, I defined "vector<Muon> AllMuons;" in Analyzers/include/ExampleRun.h,
//==== and save muons objects at the very beginning of executeEvent().
//==== Later, do "SelectMuons(AllMuons, ID, pt, eta)" to get muons with ID cuts
AllMuons = GetAllMuons();
//=== Jets too
AllJets = GetAllJets();
//==== Get L1Prefire reweight
//==== If data, 1.;
//==== If MC && DataYear > 2017, 1.;
//==== If MC && DataYear <= 2017, we have to reweight the event with this value
//==== I defined "double weight_Prefire;" in Analyzers/include/ExampleRun.h
weight_Prefire = GetPrefireWeight(0);
//==== Declare AnalyzerParameter
AnalyzerParameter param;
//==== Loop over muon IDs
for(unsigned int it_MuonID=0; it_MuonID<MuonIDs.size(); it_MuonID++){
TString MuonID = MuonIDs.at(it_MuonID);
TString MuonIDSFKey = MuonIDSFKeys.at(it_MuonID);
//==== 1) First, let's run Central values of the systematics
//==== clear parameter set
param.Clear();
//==== set which systematic sources you want to run this time
//==== default syst_ is AnalyzerParameter::Central
param.syst_ = AnalyzerParameter::Central;
//==== set name of the parameter set
//==== this will be used for the directory name of histograms
param.Name = MuonID+"_"+"Central";
//==== You can define lepton ID string here
param.Muon_Tight_ID = MuonID;
param.Muon_ID_SF_Key = MuonIDSFKey;
//==== And, Jet ID
param.Jet_ID = "tight";
//==== Now, all parameters are set. Run executeEventFromParameter() with this parameter set
executeEventFromParameter(param);
//==== 2) Now, loop over systematic sources
//==== without --userflag RunSyst, this will not be ran
if(RunSyst){
for(int it_syst=1; it_syst<AnalyzerParameter::NSyst; it_syst++){
//==== Everything else remains same, but only change syst_ and parameter name
param.syst_ = AnalyzerParameter::Syst(it_syst);
param.Name = MuonID+"_"+"Syst_"+param.GetSystType();
executeEventFromParameter(param);
}
}
}
//================================
//==== Example 2
//==== Using new PDF
//================================
if(RunNewPDF && !IsDATA){
//cout << "[ExampleRun::executeEvent] PDF reweight = " << GetPDFReweight() << endl;
FillHist("NewPDF_PDFReweight", GetPDFReweight(), 1., 2000, 0.90, 1.10);
//cout << "[ExampleRun::executeEvent] PDF reweight for error set (NErrorSet = "<<pdfReweight->NErrorSet<< ") :" << endl;
for(int i=0; i<pdfReweight->NErrorSet; i++){
//cout << "[ExampleRun::executeEvent] " << GetPDFReweight(i) << endl;
FillHist("NewPDF_PDFErrorSet/PDFReweight_Member_"+TString::Itoa(i,10), GetPDFReweight(i), 1., 2000, 0.90, 1.10);
}
}
//================================================
//==== Example 3
//==== How to estimate xsec errors (PDF & Scale)
//==== For example, MET
//================================================
if(RunXSecSyst && !IsDATA){
Event ev = GetEvent();
double MET = ev.GetMETVector().Pt();
//==== 1) PDF Error
//==== Obtain RMS of the distribution later
for(unsigned int i=0; i<weight_PDF->size(); i++){
FillHist("XSecError/MET_PDFError_"+TString::Itoa(i,10), MET, weight_PDF->at(i), 200, 0., 200.);
}
//==== 2) PDF AlphaS
//==== Look for PDF4LHC paper..
//==== https://arxiv.org/abs/1510.03865
if(weight_AlphaS->size()==2){
FillHist("XSecError/MET_PDFAlphaS_Down", MET, weight_AlphaS->at(0), 200, 0., 200.);
FillHist("XSecError/MET_PDFAlphaS_Up", MET, weight_AlphaS->at(1), 200, 0., 200.);
}
//==== 3) Scale
//==== Obtain the envelop of the distribution later
for(unsigned int i=0; i<weight_Scale->size(); i++){
//==== i=5 and 7 are unphysical
if(i==5) continue;
if(i==7) continue;
FillHist("XSecError/MET_Scale_"+TString::Itoa(i,10), MET, weight_Scale->at(i), 200, 0., 200.);
}
}
}
void ExampleRun::executeEventFromParameter(AnalyzerParameter param){
//=============
//==== No Cut
//=============
FillHist(param.Name+"/NoCut_"+param.Name, 0., 1., 1, 0., 1.);
//========================
//==== MET Filter
//========================
if(!PassMETFilter()) return;
Event ev = GetEvent();
Particle METv = ev.GetMETVector();
//==============
//==== Trigger
//==============
if(! (ev.PassTrigger(IsoMuTriggerName) )) return;
//======================
//==== Copy AllObjects
//======================
vector<Muon> this_AllMuons = AllMuons;
vector<Jet> this_AllJets = AllJets;
//==== Then, for each systematic sources
//==== 1) Smear or scale them
//==== 2) Then apply ID selections
//==== This order should be explicitly followed
//==== Below are all variables for available systematic sources
if(param.syst_ == AnalyzerParameter::Central){
}
else if(param.syst_ == AnalyzerParameter::JetResUp){
this_AllJets = SmearJets( this_AllJets, +1 );
//this_AllFatJets = SmearFatJets( this_AllFatJets, +1 );
}
else if(param.syst_ == AnalyzerParameter::JetResDown){
this_AllJets = SmearJets( this_AllJets, -1 );
//this_AllFatJets = SmearFatJets( this_AllFatJets, -1 );
}
else if(param.syst_ == AnalyzerParameter::JetEnUp){
this_AllJets = ScaleJets( this_AllJets, +1 );
//this_AllFatJets = ScaleFatJets( this_AllFatJets, +1 );
}
else if(param.syst_ == AnalyzerParameter::JetEnDown){
this_AllJets = ScaleJets( this_AllJets, -1 );
//this_AllFatJets = ScaleFatJets( this_AllFatJets, -1 );
}
else if(param.syst_ == AnalyzerParameter::MuonEnUp){
this_AllMuons = ScaleMuons( this_AllMuons, +1 );
}
else if(param.syst_ == AnalyzerParameter::MuonEnDown){
this_AllMuons = ScaleMuons( this_AllMuons, -1 );
}
else if(param.syst_ == AnalyzerParameter::ElectronResUp){
//this_AllElectrons = SmearElectrons( this_AllElectrons, +1 );
}
else if(param.syst_ == AnalyzerParameter::ElectronResDown){
//this_AllElectrons = SmearElectrons( this_AllElectrons, -1 );
}
else if(param.syst_ == AnalyzerParameter::ElectronEnUp){
//this_AllElectrons = ScaleElectrons( this_AllElectrons, +1 );
}
else if(param.syst_ == AnalyzerParameter::ElectronEnDown){
//this_AllElectrons = ScaleElectrons( this_AllElectrons, -1 );
}
else{
cout << "[ExampleRun::executeEventFromParameter] Wrong syst" << endl;
exit(EXIT_FAILURE);
}
//==================================================
//==== Then, apply ID selections using this_AllXXX
//==================================================
vector<Muon> muons = SelectMuons(this_AllMuons, param.Muon_Tight_ID, 20., 2.4);
vector<Jet> jets = SelectJets(this_AllJets, param.Jet_ID, 30., 2.4);
//=======================
//==== Sort in pt-order
//=======================
//==== 1) leptons : after scaling/smearing, pt ordring can differ from MINIAOD
std::sort(muons.begin(), muons.end(), PtComparing);
//==== 2) jets : similar, but also when applying new JEC, ordering is changes. This is important if you use leading jets
std::sort(jets.begin(), jets.end(), PtComparing);
int NBJets_NoSF(0), NBJets_WithSF_2a(0);
JetTagging::Parameters jtp_DeepCSV_Medium = JetTagging::Parameters(JetTagging::DeepCSV,
JetTagging::Medium,
JetTagging::incl, JetTagging::comb);
//==== b tagging
//==== method 1a)
//==== multiply "btagWeight" to the event weight
double btagWeight = mcCorr->GetBTaggingReweight_1a(jets, jtp_DeepCSV_Medium);
//==== method 2a)
for(unsigned int ij = 0 ; ij < jets.size(); ij++){
double this_discr = jets.at(ij).GetTaggerResult(JetTagging::DeepCSV);
//==== No SF
if( this_discr > mcCorr->GetJetTaggingCutValue(JetTagging::DeepCSV, JetTagging::Medium) ) NBJets_NoSF++;
//==== 2a
if( mcCorr->IsBTagged_2a(jtp_DeepCSV_Medium, jets.at(ij)) ) NBJets_WithSF_2a++;
}
//=========================
//==== Event selections..
//=========================
//==== dimuon
if(muons.size() != 2) return;
//==== leading muon has trigger-safe pt
if( muons.at(0).Pt() <= TriggerSafePtCut ) return;
//==== On-Z
Particle ZCand = muons.at(0) + muons.at(1);
if(!IsOnZ(ZCand.M(), 15.)) return;
//===================
//==== Event weight
//===================
double weight = 1.;
//==== If MC
if(!IsDATA){
//==== MCweight is normalized to 1 pb-1.
weight *= MCweight();
//==== you can pass trigger names to ev.GetTriggerLumi(), but if you are using unprescaled trigger, simply pass "Full"
weight *= ev.GetTriggerLumi("Full");
//==== L1Prefire reweight
weight *= weight_Prefire;
//==== Example of applying Muon scale factors
for(unsigned int i=0; i<muons.size(); i++){
double this_idsf = 1.;
//double this_idsf = mcCorr->MuonID_SF (param.Muon_ID_SF_Key, muons.at(i).Eta(), muons.at(i).MiniAODPt());
//==== If you have iso SF, do below. Here we don't.
//double this_isosf = mcCorr->MuonISO_SF(param.Muon_ISO_SF_Key, muons.at(i).Eta(), muons.at(i).MiniAODPt());
double this_isosf = 1.;
weight *= this_idsf*this_isosf;
}
}
//==========================
//==== Now fill histograms
//==========================
FillHist(param.Name+"/ZCand_Mass_"+param.Name, ZCand.M(), weight, 40, 70., 110.);
}