forked from mflechl/ProductionFromNano
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTEvent.cxx
509 lines (440 loc) · 17.7 KB
/
HTTEvent.cxx
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
#ifdef PROJECT_NAME
#include "m2n/HTTDataFormats/interface/HTTEvent.h"
#include "m2n/HTT/interface/GenInfoHelper.h"
#else
#include "HTTEvent.h"
#include "gghWG1uncertainties.cxx"
#endif
std::map<string,PropertyEnum> HTTEvent::usePropertyFor = {};
HTTAnalysis::sysEffects HTTParticle::corrType = HTTAnalysis::NOMINAL;
////////////////////////////////////////////////
////////////////////////////////////////////////
const TString HTTEvent::tauIDStrings[ntauIds] = {
"againstMuonLoose3",
"againstMuonTight3",
"againstElectronVLooseMVA6",
"againstElectronLooseMVA6",
"againstElectronMediumMVA6",
"againstElectronTightMVA6",
"againstElectronVTightMVA6",
"byVLooseIsolationMVArun2v1DBoldDMwLT",
"byLooseIsolationMVArun2v1DBoldDMwLT",
"byMediumIsolationMVArun2v1DBoldDMwLT",
"byTightIsolationMVArun2v1DBoldDMwLT",
"byVTightIsolationMVArun2v1DBoldDMwLT",
"byVVTightIsolationMVArun2v1DBoldDMwLT",
// deepTauIDv2 vs jet discriminator
"byVVVLooseDeepTau2017v2VSjet",
"byVVLooseDeepTau2017v2VSjet",
"byVLooseDeepTau2017v2VSjet",
"byLooseDeepTau2017v2VSjet",
"byMediumDeepTau2017v2VSjet",
"byTightDeepTau2017v2VSjet",
"byVTightDeepTau2017v2VSjet",
"byVVTightDeepTau2017v2VSjet",
// deepTauIDv2 vs electron discriminator
"byVVVLooseDeepTau2017v2VSe",
"byVVLooseDeepTau2017v2VSe",
"byVLooseDeepTau2017v2VSe",
"byLooseDeepTau2017v2VSe",
"byMediumDeepTau2017v2VSe",
"byTightDeepTau2017v2VSe",
"byVTightDeepTau2017v2VSe",
"byVVTightDeepTau2017v2VSe",
// deepTauIDv2 vs muon discriminator
"byVLooseDeepTau2017v2VSmu",
"byLooseDeepTau2017v2VSmu",
"byMediumDeepTau2017v2VSmu",
"byTightDeepTau2017v2VSmu"
};
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTEvent::clear(){
runId = 0;
eventId = 0;
lsId = 0;
nPU = 0;
nPV = 0;
rho = 0;
puWeight = 1.;
mcWeight = 1.0;
topPtReWeight =1.0;
topPtReWeightR1 =1.0;
isSetGenBosonP4 = false;
zPtReWeight =1.0;
zPtReWeightSUSY =1.0;
lheHt = 1.0;
lheNOutPartons = 0;
aMCatNLOweight = 1.0;
bosP4*=0;
bosVisP4*=0;
selectionWord.ResetAllBits();
THU_uncertainties = {1.0,1.0,1.0,
1.0,1.0,1.0,
1.0,1.0,1.0,
};
}
void HTTEvent::setSampleType(string sampletype)
{
if(sampletype == "SingleMuon") sampleType = MuonData;
if(sampletype == "SingleElectron") sampleType = EleData;
if(sampletype == "Tau") sampleType = TauData;
if(sampletype == "dy") sampleType = DY;
if(sampletype == "dy_lowmass") sampleType = DYLowM;
if(sampletype == "w") sampleType = WJets;
if(sampletype == "tt") sampleType = TTbar;
if(sampletype == "st") sampleType = ST;
if(sampletype == "diboson") sampleType = Diboson;
if(sampletype == "ewk") sampleType = EWK;
if(sampletype == "signal") sampleType = h;
sampletype = DUMMY;
}
void HTTEvent::setTHU_uncertainties(int njets, double ptH, int stxs1)
{
THU_uncertainties = qcd_ggF_uncertSF_2017(njets, ptH, stxs1, 1.0);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HTTParticle::clear(){
p4*=0;
properties.clear();
deltaVector.SetMagPhi(0.,0.);
lastSystEffect = HTTAnalysis::NOMINAL;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTParticle::setP4(const TLorentzVector &aP4 )
{
p4 = aP4;
currentP4 = getShiftedP4(corrType);
lastSystEffect = corrType;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
const TLorentzVector & HTTParticle::getP4(HTTAnalysis::sysEffects defaultType) const
{
HTTAnalysis::sysEffects type = defaultType != HTTAnalysis::NOMINAL ? defaultType : corrType;
if(type == lastSystEffect) return currentP4;
lastSystEffect = type;
currentP4 = getShiftedP4(type); // Set currentP4 before returning
return currentP4;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
const TLorentzVector HTTParticle::getShiftedP4(HTTAnalysis::sysEffects shift) const
{
TLorentzVector shiftedP4 = p4;
int pdg = std::abs(getPDGid());
int dm = getProperty(PropertyEnum::decayMode);
int mc_match = getProperty(PropertyEnum::mc_match);
float tauES = HTTAnalysis::getEnergyScale(pdg, mc_match, p4.Eta(), dm, shift); // Defined in AnalysisEnums.h
float tauES_mass;
if(pdg == 15 ) tauES_mass = tauES;
if(dm == 0 || pdg != 15) tauES_mass=0;
shiftedP4.SetPtEtaPhiM(p4.Pt() * (1.0+tauES),
p4.Eta(),
p4.Phi(),
p4.M() * (1.0+tauES_mass) );
deltaVector.SetMagPhi( (shiftedP4 - p4).Pt(), p4.Phi() );
return shiftedP4;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HTTPair::clear()
{
met.clear();
met[""] = TVector2(0.,0.);
metCache = TVector2(0.,0.);
p4Vector.clear();
p4Vector[""].SetPtEtaPhiM(-10,-10,-10,-10);
metMatrix.clear();
leg1.clear();
leg2.clear();
lastMETShift = "";
}
////////////////////////////////////////////////
////////////////////////////////////////////////
const TLorentzVector HTTPair::getP4(P4Type type)
{
if(type == P4Type::SVFit)
{
if( p4Vector.find(lastMETShift) == p4Vector.end() ) return p4Vector.at("");
return p4Vector.at(lastMETShift);
} else if(type == P4Type::Simple)
{
TLorentzVector vmet;
vmet.SetPtEtaPhiM(getMET().Mod(),0,getMET().Phi(),0);
return (vmet + leg1.getP4() + leg2.getP4() );
} else
{
return ( leg1.getP4() + leg2.getP4() );
}
}
////////////////////////////////////////////////
////////////////////////////////////////////////
HTTAnalysis::finalState HTTPair::getFinalState()
{
int pdg1=std::abs(getLeg1().getProperty(PropertyEnum::pdgId));
int pdg2=std::abs(getLeg2().getProperty(PropertyEnum::pdgId));
if(pdg1 == 11 && pdg2 == 15) return HTTAnalysis::EleTau;
if(pdg1 == 13 && pdg2 == 15) return HTTAnalysis::MuTau;
if(pdg1 == 15 && pdg2 == 15) return HTTAnalysis::TauTau;
return HTTAnalysis::NONE;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
bool HTTPair::isInLooseSR()
{
bool mt = getMTLeg1() < 50;
bool os = leg1.getCharge()*leg2.getCharge() < 0;
bool tauiso = true;
HTTAnalysis::finalState fs = getFinalState();
if( fs == HTTAnalysis::EleTau || fs == HTTAnalysis::MuTau )
{
tauiso = ((unsigned int)leg2.getProperty( HTTEvent::usePropertyFor.at("tauID") ) & 0x10) == 0x10;
} else if( fs == HTTAnalysis::TauTau )
{
tauiso = ((unsigned int)leg1.getProperty( HTTEvent::usePropertyFor.at("tauID") ) & 0x10) == 0x10;
tauiso &= ((unsigned int)leg2.getProperty( HTTEvent::usePropertyFor.at("tauID") ) & 0x10) == 0x10;
}
return (mt && os && tauiso);
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTPair::setCurrentMETShift(string uncert)
{
if( met.find(uncert) == met.end() )
{
metCache = met.at("");
}else
{
metCache = met.at(uncert);
}
lastMETShift = uncert;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
float HTTPair::getMTTOT(HTTAnalysis::sysEffects defaultType) const
{
HTTAnalysis::sysEffects type = defaultType != HTTAnalysis::NOMINAL ? defaultType : HTTParticle::corrType;
float mt1 = 2. * leg1.getP4(type).Pt() * getMET().Mod() * (1. - TMath::Cos(leg1.getP4(type).Phi()-getMET().Phi()));
float mt2 = 2. * leg2.getP4(type).Pt() * getMET().Mod() * (1. - TMath::Cos(leg2.getP4(type).Phi()-getMET().Phi()));
float mt3 = 2. * leg1.getP4(type).Pt() * leg2.getP4(type).Pt() * (1. - TMath::Cos(leg1.getP4(type).Phi()-leg2.getP4(type).Phi()));
return TMath::Sqrt( mt1 + mt2 + mt3 );
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HTTJet::clear()
{
p4.SetPtEtaPhiM(-10.,-10.,-10.,-10.);
currentP4.SetPtEtaPhiM(-10.,-10.,-10.,-10.);
jecUncertSourceValues.clear();
}
////////////////////////////////////////////////
////////////////////////////////////////////////
const TLorentzVector & HTTJet::getP4(string uncert, bool up)
{
currentP4 = p4;
if(uncert == "") return currentP4;
double shift = up ? 1. : -1.;
double scale = jecUncertSourceValues[uncert];
currentP4.SetPtEtaPhiM( p4.Pt()*(1 + scale*shift),
p4.Eta(),
p4.Phi(),
p4.M()*(1 + scale*shift)
);
return currentP4;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void HTTJetCollection::initCollection(bool isMC, bool isSync)
{
jecShifts = { {"",{"",true}} };
if( HTTParticle::corrType == HTTAnalysis::NOMINAL && isMC && !isSync)
{
for(auto uncert : JecAfterSplitting)
{
for(auto shift : { make_pair("Up",true), make_pair("Down",false) } )
{
jecShifts.push_back( make_pair(uncert.first + shift.first, make_pair(uncert.first, shift.second) ) );
}
}
}
clear();
}
void HTTJetCollection::clear()
{
dijet.SetPtEtaPhiM(-10.,-10.,-10.,-10.);
jetCollection.clear();
jetCurrentCollection.clear();
btagCollection.clear();
antibtagCollection.clear();
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTJetCollection::initForPromoteDemote()
{
usePromoteDemote = true;
//FIXME new csv file needs to be added here
calib = BTagCalibration("CSVv2", "utils/BTagCalibration/data/CSVv2_Moriond17_B_H.csv");
reader = BTagCalibrationReader(BTagEntry::OP_MEDIUM, "central",{"up","down"});
reader.load(calib, BTagEntry::FLAV_B, "comb");
reader.load(calib, BTagEntry::FLAV_C, "comb");
reader.load(calib, BTagEntry::FLAV_UDSG, "incl");
eff_file = new TFile("utils/BTagCalibration/data/tagging_efficiencies_march2018_btageff-all_samp-inc-DeepCSV_medium.root");
hb_eff = dynamic_cast<TH2D*>(eff_file->Get("btag_eff_b") );
hc_eff = dynamic_cast<TH2D*>(eff_file->Get("btag_eff_c") );
hoth_eff = dynamic_cast<TH2D*>(eff_file->Get("btag_eff_oth") );
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTJetCollection::btagPromoteDemote(string mistagsys, string btagsys){
btagCurrentCollection = btagCollection;
if(!usePromoteDemote) return;
TRandom3 rand;
double scaleFactor;
double scaleRatio;
double jetPt;
double jetPhi;
double jetEta;
double tagging_efficiency;
double rn;
int index = 0;
for(auto & jet : btagCurrentCollection){
jetPt= jet.Pt();
jetPhi= jet.Phi();
jetEta= jet.Eta();
rand.SetSeed((int)((jetEta+5)*100000));
// rand.SetSeed( static_cast<int>(( jetEta +5 )*1000)*1000 + static_cast<int>(( jetPhi + 4 )*1000) );
rn = rand.Uniform();
if( jet.getProperty(PropertyEnum::hadronFlavour) ==5 ) scaleFactor = reader.eval_auto_bounds(mistagsys ,BTagEntry::FLAV_B, jetEta, jetPt);
else if( jet.getProperty(PropertyEnum::hadronFlavour) ==4 ) scaleFactor = reader.eval_auto_bounds(mistagsys ,BTagEntry::FLAV_C, jetEta, jetPt);
else scaleFactor = reader.eval_auto_bounds(mistagsys ,BTagEntry::FLAV_UDSG, jetEta, jetPt);
if(scaleFactor < 1){
scaleRatio = fabs( 1-scaleFactor );
//cout << (int)(( jetEta +5 )*1000)*1000 + (int)(( jetPhi + 4 )*1000)<< " " << jet.getProperty(PropertyEnum::hadronFlavour) << " " << jetPt << " " << jetEta << " "<< jetPhi<< " " << scaleFactor<< " " << rn <<endl;
if( rn < scaleRatio ) btagCurrentCollection.erase(btagCurrentCollection.begin() + index);
}
++index;
}
for(auto & jet : antibtagCollection){
jetPt= jet.Pt();
jetPhi= jet.Phi();
jetEta= jet.Eta();
// rand.SetSeed((int)((jetEta+5)*100000));
rand.SetSeed( static_cast<int>(( jetEta +5 )*1000)*1000 + static_cast<int>(( jetPhi + 4 )*1000) );
rn = rand.Uniform();
if( jet.getProperty(PropertyEnum::hadronFlavour)==5 ){
scaleFactor = reader.eval_auto_bounds(btagsys ,BTagEntry::FLAV_B, jetEta, jetPt);
if(jetPt>hb_eff->GetXaxis()->GetBinLowEdge(hb_eff->GetNbinsX()+1)) tagging_efficiency = hb_eff->GetBinContent(hb_eff->GetNbinsX(),hb_eff->GetYaxis()->FindBin(fabs( jetEta ) ));
else tagging_efficiency = hb_eff->GetBinContent(hb_eff->GetXaxis()->FindBin(jetPt),hb_eff->GetYaxis()->FindBin(fabs( jetEta )));
}
else if( jet.getProperty(PropertyEnum::hadronFlavour)==4 ){
scaleFactor = reader.eval_auto_bounds(btagsys ,BTagEntry::FLAV_C, jetEta, jetPt);
if(jetPt>hc_eff->GetXaxis()->GetBinLowEdge(hc_eff->GetNbinsX()+1)) tagging_efficiency = hc_eff->GetBinContent(hc_eff->GetNbinsX(),hc_eff->GetYaxis()->FindBin(fabs( jetEta )));
else tagging_efficiency = hc_eff->GetBinContent(hc_eff->GetXaxis()->FindBin(jetPt),hc_eff->GetYaxis()->FindBin(fabs( jetEta )));
}
else{
scaleFactor = reader.eval_auto_bounds(btagsys ,BTagEntry::FLAV_UDSG, jetEta, jetPt);
if(jetPt>hoth_eff->GetXaxis()->GetBinLowEdge(hoth_eff->GetNbinsX()+1)) {
tagging_efficiency = hoth_eff->GetBinContent(hoth_eff->GetNbinsX(),hoth_eff->GetYaxis()->FindBin(fabs( jetEta )));
}
else {
tagging_efficiency = hoth_eff->GetBinContent(hoth_eff->GetXaxis()->FindBin(jetPt),hoth_eff->GetYaxis()->FindBin(fabs( jetEta )));
}
}
if(scaleFactor > 1){
scaleRatio = fabs( ( 1-scaleFactor )/( 1-(1/ tagging_efficiency ) ) );
if( rn < scaleRatio ) btagCurrentCollection.push_back( jet );
}
}
std::sort(btagCurrentCollection.begin(), btagCurrentCollection.end(), sortJets);
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTJetCollection::fillCurrentCollections(string uncert, bool up)
{
// std::cout<<"fillCurrentCollections"<<std::endl;
jetCurrentCollection.clear();
btagCollection.clear();
antibtagCollection.clear();
for(auto jet : jetCollection)
{
// std::cout<<"for(auto jet : jetCollection)"<<std::endl;
jet.setUncertShift(uncert, up);
if(jet.Pt() > 20)
jetCurrentCollection.push_back(jet);
if(jet.Pt() > 20 && abs(jet.Eta()) < 2.4 )
{
if( jet.getProperty(PropertyEnum::btagDeepB)> 0.6321) // see https://twiki.cern.ch/twiki/bin/viewauth/CMS/HiggsToTauTauWorkingLegacyRun2#B_jets
{
// std::cout<<"jet.getProperty(PropertyEnum::btagDeepB)> 0.4184"<<std::endl;
btagCollection.push_back( jet );
}else
{
// std::cout<<"else jet.getProperty(PropertyEnum::btagDeepB)> 0.4184"<<std::endl;
antibtagCollection.push_back( jet );
}
}
}
// std::cout<<"end for loop "<<std::endl;
// If promote-demote is not enabled only current btagCollection will be filled
btagPromoteDemote();
// std::cout<<"btagPromoteDemote"<<std::endl;
if(jetCurrentCollection.size() > 1)
setDijetP4();
}
////////////////////////////////////////////////
////////////////////////////////////////////////
const TVector2 HTTJetCollection::getTotalJetShift(string uncert, bool up)
{
TLorentzVector before; before.SetPtEtaPhiE(0,0,0,0);
TLorentzVector after; after.SetPtEtaPhiE(0,0,0,0);
for(auto jet : jetCollection){
before += jet.P4();
jet.setUncertShift(uncert, up);
after += jet.P4();
}
return TVector2( (after - before).Px(), (after - before).Py() ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
void HTTJetCollection::setDijetP4()
{
dijet = jetCurrentCollection.at(0).P4()+jetCurrentCollection.at(1).P4();
}
////////////////////////////////////////////////
////////////////////////////////////////////////
int HTTJetCollection::getNJets(double pt)
{
int njets = 0;
for(auto &jet : jetCurrentCollection)
if(jet.Pt() > pt )njets++;
return njets;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
int HTTJetCollection::getNJetInGap(double pt)
{
int njetingap = 0;
if(jetCurrentCollection.size() < 2) return 0;
double j1_eta = jetCurrentCollection.at(0).Eta();
double j2_eta = jetCurrentCollection.at(1).Eta();
for (unsigned ij=2; ij<jetCurrentCollection.size(); ij++)
{
TLorentzVector addJet = jetCurrentCollection.at(ij).P4();
if ( addJet.Pt() < pt ) continue;
float aj_eta=addJet.Eta();
if( (aj_eta<j1_eta && aj_eta>j2_eta)
|| (aj_eta>j1_eta && aj_eta<j2_eta)
) njetingap++;
}
return njetingap;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
bool HTTJetCollection::sortJets(HTTJet j1, HTTJet j2)
{
if( j1.Pt() == j2.Pt() ) return true;
return j1.Pt() > j2.Pt();
}