-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlyEtr_DT.cpp
299 lines (261 loc) · 11.8 KB
/
OnlyEtr_DT.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
#include <iostream>
#include <vector>
#include "TFile.h"
#include "TTree.h"
#include "TH1D.h"
#include "TH2D.h"
#include "TCanvas.h"
#include "TLorentzVector.h"
// include user defined histograms and auxiliary macros
#include "Histodef.cpp"
#include "Auxiliary.cpp"
#include "/afs/cern.ch/user/g/gdamolin/Johan/TTbar/Python_Analysis/corrections/roccor/RoccoR.cc"
using namespace std;
#define MAX_ARRAY_SIZE 128
void DataAnalysis(string inputFile, string ofile, bool IsFirstDataSet)
{
TFile *fin = TFile::Open(inputFile.c_str());
TTree *tin = static_cast<TTree *>(fin->Get("Events"));
// Set all branches to 0
tin->SetBranchStatus("*", 0);
// get the pt
Float_t Muon_pt[MAX_ARRAY_SIZE], Electron_pt[MAX_ARRAY_SIZE], Jet_pt[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Electron_pt", 1);
tin->SetBranchAddress("Electron_pt", &Electron_pt);
tin->SetBranchStatus("Muon_pt", 1);
tin->SetBranchAddress("Muon_pt", &Muon_pt);
tin->SetBranchStatus("Jet_pt", 1);
tin->SetBranchAddress("Jet_pt", &Jet_pt);
// get the number of muons, electrons
UInt_t nMuon, nElectron;
tin->SetBranchStatus("nElectron", 1);
tin->SetBranchAddress("nElectron", &nElectron);
tin->SetBranchStatus("nMuon", 1);
tin->SetBranchAddress("nMuon", &nMuon);
// get the eta
Float_t Muon_eta[MAX_ARRAY_SIZE], Electron_eta[MAX_ARRAY_SIZE], Jet_eta[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Electron_eta", 1);
tin->SetBranchAddress("Electron_eta", &Electron_eta);
tin->SetBranchStatus("Muon_eta", 1);
tin->SetBranchAddress("Muon_eta", &Muon_eta);
tin->SetBranchStatus("Jet_eta", 1);
tin->SetBranchAddress("Jet_eta", &Jet_eta);
// get the phi
Float_t Muon_phi[MAX_ARRAY_SIZE], Electron_phi[MAX_ARRAY_SIZE], Jet_phi[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Electron_phi", 1);
tin->SetBranchAddress("Electron_phi", &Electron_phi);
tin->SetBranchStatus("Muon_phi", 1);
tin->SetBranchAddress("Muon_phi", &Muon_phi);
tin->SetBranchStatus("Jet_phi", 1);
tin->SetBranchAddress("Jet_phi", &Jet_phi);
// get the mass
Float_t Muon_mass[MAX_ARRAY_SIZE], Electron_mass[MAX_ARRAY_SIZE], Jet_mass[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Electron_mass", 1);
tin->SetBranchAddress("Electron_mass", &Electron_mass);
tin->SetBranchStatus("Muon_mass", 1);
tin->SetBranchAddress("Muon_mass", &Muon_mass);
tin->SetBranchStatus("Jet_mass", 1);
tin->SetBranchAddress("Jet_mass", &Jet_mass);
// collect the trigger information
Bool_t HLT_IsoMu24, HLT_Ele32_WPTight_Gsf;
tin->SetBranchStatus("HLT_IsoMu24", 1);
tin->SetBranchStatus("HLT_Ele32_WPTight_Gsf", 1);
tin->SetBranchAddress("HLT_IsoMu24", &HLT_IsoMu24);
tin->SetBranchAddress("HLT_Ele32_WPTight_Gsf", &HLT_Ele32_WPTight_Gsf);
// collect the triggger Ids
Int_t Muon_charge[MAX_ARRAY_SIZE], Electron_charge[MAX_ARRAY_SIZE];
Bool_t Electron_mvaFall17V2Iso_WP90[MAX_ARRAY_SIZE], Muon_triggerIdLoose[MAX_ARRAY_SIZE], Muon_tightId[MAX_ARRAY_SIZE];
Float_t Muon_pfRelIso04_all[MAX_ARRAY_SIZE], Electron_dxy[MAX_ARRAY_SIZE], Electron_dz[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Muon_tightId", 1);
tin->SetBranchStatus("Muon_charge", 1);
tin->SetBranchStatus("Muon_triggerIdLoose", 1);
tin->SetBranchStatus("Muon_pfRelIso04_all", 1);
tin->SetBranchStatus("Electron_charge", 1);
tin->SetBranchStatus("Electron_mvaFall17V2Iso_WP90", 1);
tin->SetBranchStatus("Electron_dxy", 1);
tin->SetBranchStatus("Electron_dz", 1);
tin->SetBranchAddress("Electron_mvaFall17V2Iso_WP90", &Electron_mvaFall17V2Iso_WP90);
tin->SetBranchAddress("Muon_tightId", &Muon_tightId);
tin->SetBranchAddress("Muon_charge", &Muon_charge);
tin->SetBranchAddress("Muon_triggerIdLoose", &Muon_triggerIdLoose);
tin->SetBranchAddress("Muon_pfRelIso04_all", &Muon_pfRelIso04_all);
tin->SetBranchAddress("Electron_charge", &Electron_charge);
tin->SetBranchAddress("Electron_dxy", &Electron_dxy);
tin->SetBranchAddress("Electron_dz", &Electron_dz);
// Jet tagging , FlavB is the recomennded one, DeepB was used by Anup
Float_t Jet_btagDeepFlavB[MAX_ARRAY_SIZE], Jet_btagDeepB[MAX_ARRAY_SIZE];
UInt_t nJet;
Int_t Jet_jetId[MAX_ARRAY_SIZE], Jet_puId[MAX_ARRAY_SIZE];
tin->SetBranchStatus("Jet_btagDeepB", 1);
tin->SetBranchStatus("Jet_btagDeepFlavB", 1);
tin->SetBranchStatus("nJet", 1);
tin->SetBranchStatus("Jet_jetId", 1);
tin->SetBranchStatus("Jet_puId", 1);
tin->SetBranchAddress("nJet", &nJet);
tin->SetBranchAddress("Jet_btagDeepFlavB", &Jet_btagDeepFlavB);
tin->SetBranchAddress("Jet_btagDeepB", &Jet_btagDeepB);
tin->SetBranchAddress("Jet_jetId", &Jet_jetId);
tin->SetBranchAddress("Jet_puId", &Jet_puId);
int non_matching_muon = 0, non_matching_electron = 0;
int n_dropped = 0;
int trigger_dropped = 0,crosstrigger=0;
const auto nEv = tin->GetEntries();
TLorentzVector *Muon_p4 = new TLorentzVector();
TLorentzVector *Electron_p4 = new TLorentzVector();
TLorentzVector *MainBjet_p4 = new TLorentzVector();
TLorentzVector *OppositeBjet_p4 = new TLorentzVector();
// allow pt, inv mass, and eta to be stored in a Branch
Float_t leading_lepton_pt, invMass, electron_eta, electron_pt, muon_eta, muon_pt;
TFile *fout =new TFile(ofile.c_str(),"RECREATE");
// create a new tree for the output
TTree *tout = new TTree("tout","tout");
// set the branches for the output tree
tout->Branch("leading_lepton_pt", &leading_lepton_pt);
tout->Branch("invMass", &invMass);
tout->Branch("electron_eta", &electron_eta);
tout->Branch("electron_pt", &electron_pt);
tout->Branch("muon_eta", &muon_eta);
tout->Branch("muon_pt", &muon_pt);
RoccoR rc;
rc.init("/afs/cern.ch/user/g/gdamolin/Johan/TTbar/Python_Analysis/corrections/roccor/RoccoR2018UL.txt");
#pragma omp parallel for
for (UInt_t i = 0; i < nEv; i++)
{
tin->GetEntry(i);
if (i % 100000 == 0)
std::cout << "Processing entry " << i << " of " << nEv << std::endl;
// apply triggers
if (! HLT_Ele32_WPTight_Gsf){
trigger_dropped++;
continue;
};
// loop over the muons and electrons and only keep the fist ones that pass the requirements
Int_t muon_idx = -1;
for (UInt_t j = 0; j < nMuon; j++){
if ((abs(Muon_eta[j])<2.4 && Muon_tightId[j] && Muon_pfRelIso04_all[j] < 0.15)){
double scmDT=rc.kScaleDT(Muon_charge[j],Muon_pt[j],Muon_eta[j],Muon_phi[j]);
Muon_pt[j]*= scmDT;
if (Muon_pt[j]>27.){
muon_idx = j;
Muon_p4->SetPtEtaPhiM(Muon_pt[muon_idx], Muon_eta[muon_idx], Muon_phi[muon_idx], Muon_mass[muon_idx]);
break;
}
}
}
if (muon_idx==-1) {
n_dropped++;
continue;
}
Int_t electron_idx = -1;
for (UInt_t j = 0; j < nElectron; j++){
if ((Electron_pt[j]>35 && abs(Electron_eta[j])<2.4 && Electron_mvaFall17V2Iso_WP90[j]&& abs(Electron_dxy[j])<0.2 && abs(Electron_dz[j])<0.5)){
if((abs(Electron_eta[j])>1.44) && (abs(Electron_eta[j])<1.57)) {continue;} //remove electrons in the acceptance break
Electron_p4->SetPtEtaPhiM(Electron_pt[j], Electron_eta[j], Electron_phi[j], Electron_mass[j]);
if(Electron_p4->DeltaR(*Muon_p4)<0.4) {continue;}
else {electron_idx = j; break;}
}
}
if (electron_idx==-1) {
n_dropped++;
continue;
}
bool selection = ((muon_idx > -1) && (electron_idx > -1));
// check the seleected objects for opposite charge
selection = selection && (Muon_charge[muon_idx] * Electron_charge[electron_idx]) < 0;
// the tight working point is 0.71, medium 0.2783, loose 0.0490
Float_t jet_btag_deepFlav_wp = 0.2783;
bool one_Bjet = false;
int id_m_jet=-1;
for (size_t j = 0; j < nJet; j++){
if((abs(Jet_eta[j]) < 2.4) && Jet_pt[j]>25 && (Jet_jetId[j]==2 || Jet_jetId[j]==6) && (Jet_pt[j]>50 || (Jet_puId[j]>=4)))
{
TLorentzVector *Tjet_p4 = new TLorentzVector();
Tjet_p4->SetPtEtaPhiM(Jet_pt[j], Jet_eta[j], Jet_phi[j], Jet_mass[j]);
if((Tjet_p4->DeltaR(*Muon_p4)<0.4) || (Tjet_p4->DeltaR(*Electron_p4)<0.4)) {delete Tjet_p4; continue;}
else {delete Tjet_p4;};
if (Jet_btagDeepFlavB[j] > jet_btag_deepFlav_wp){
if(!one_Bjet) {
MainBjet_p4->SetPtEtaPhiM(Jet_pt[j], Jet_eta[j], Jet_phi[j], Jet_mass[j]);
OppositeBjet_p4->SetPtEtaPhiM(Jet_pt[j], -1*Jet_eta[j], InvertPhi(Jet_phi[j]), Jet_mass[j]);
if ( MainBjet_p4->DeltaR(*Muon_p4)>0.4 && MainBjet_p4->DeltaR(*Electron_p4)>0.4){
one_Bjet = true; id_m_jet=j;
}
}
} //end tag
}//end kinematic if
}
selection = selection && (one_Bjet);
if (!selection){
n_dropped++;
continue;
}
// check whether muon or electron is the leading one
if (Muon_p4->Pt() > Electron_p4->Pt()){
// fill the hist
leading_lepton_pt = Muon_p4->Pt();
} else {
leading_lepton_pt = Electron_p4->Pt();
}
h_leading_lepton_pt->Fill(leading_lepton_pt);
// fill the histograms: not weighted only e trigger
muon_pt = Muon_pt[muon_idx];
muon_eta = Muon_eta[muon_idx];
electron_pt = Electron_pt[electron_idx];
electron_eta = Electron_eta[electron_idx];
h_Muon_pt->Fill(muon_pt);
h_Muon_eta->Fill(muon_eta);
h_Electron_pt->Fill(electron_pt);
h_Electron_eta->Fill(electron_eta);
h_vsPTandEta_onlye->Fill(electron_pt,electron_eta);
if (muon_idx > -1 && electron_idx > -1){
invMass = (*(Muon_p4) + *(Electron_p4)).M();
h_Muon_Electron_invariant_mass->Fill(invMass);
}
//weighted= AND of triggers
if(HLT_IsoMu24){
h_Muon_pt_weighted->Fill(muon_pt);
h_Muon_eta_weighted->Fill(muon_eta);
h_Electron_pt_weighted->Fill(electron_pt);
h_Electron_eta_weighted->Fill(electron_eta);
h_Muon_Electron_invariant_mass_weighted->Fill(invMass);
}
tout->Fill();
}
std::cout << "Total number of events: " << nEv << std::endl;
int NumbEv=nEv;
std::cout << "Removed because in another sample = " << crosstrigger << endl;
std::cout << "trigger dropped = " << trigger_dropped << endl;
std::cout << "selections dropped = " << n_dropped << endl; //remember the cross trigger in Data
NumbEv-=crosstrigger;
std::cout << "Fraction of events discarded by trigger = " << (trigger_dropped * 1. / NumbEv) << endl;
int Rem_trigger=NumbEv-trigger_dropped; //remember the cross trigger in Data
std::cout << "Fraction of events removed by selections = " << (n_dropped * 1. / Rem_trigger) << endl;
std::cout << "Final number of events "<< Rem_trigger - n_dropped<<endl;
// Write the histograms to the file
h_Muon_pt->Write();
h_Muon_eta->Write();
h_Electron_eta->Write();
h_Electron_pt->Write();
h_Muon_Electron_invariant_mass->Write();
h_leading_lepton_pt->Write();
h_Muon_eta_weighted->Write();
h_Muon_pt_weighted->Write();
h_Electron_eta_weighted->Write();
h_Electron_pt_weighted->Write();
h_Muon_Electron_invariant_mass_weighted->Write();
h_leading_lepton_pt_weighted->Write();
h_vsPTandEta_onlye->Write();
fout->Write();
fout->Close();
}
int main(int argc, char **argv)
{
string inputFile = argv[1];
string outputFile = argv[2];
string boolstr=argv[3];
bool IsFirstDataset= (boolstr=="true")||(boolstr=="True");
if (IsFirstDataset) {std::cout<<"############## It is first dataset! ##################"<<std::endl;}
if (!IsFirstDataset) {std::cout<<"@@@@@@@@@@@@@@ NOT first dataset! @@@@@@@@@@@@@@@@@@"<<std::endl;}
HistIniz();
DataAnalysis(inputFile, outputFile, IsFirstDataset);
}