ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/NonMCBackground/src/applyEMU.cc
Revision: 1.6
Committed: Fri Jul 6 15:01:29 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.5: +39 -26 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 //
2 // System headers
3 //
4 #include <vector> // STL vector class
5 #include <iostream> // standard I/O
6 #include <iomanip> // functions to format standard I/O
7 #include <bitset>
8 #include <map>
9 using namespace std;
10
11 //
12 // root headers
13 //
14 #include <TFile.h>
15 #include <TTree.h>
16 #include <TChain.h>
17 #include <TBranch.h>
18 #include <TClonesArray.h>
19
20 //
21 // TMVA
22 //
23 #include "TMVA/Reader.h"
24 #include "TMVA/Tools.h"
25 #include "TMVA/Config.h"
26 #include "TMVA/MethodBDT.h"
27
28 //
29 // ntuple format headers
30 //
31 #include "EventHeader.h"
32 #include "PFMet.h"
33 #include "Electron.h"
34 #include "Muon.h"
35 #include "Vertex.h"
36 #include "PFJet.h"
37 #include "PFCandidate.h"
38 #include "PFCandidateCol.h"
39 #include "PileupInfoCol.h"
40 #include "PileupEnergyDensity.h"
41 #include "MCParticle.h"
42 #include "TriggerMask.h"
43 #include "TriggerTable.h"
44 #include "Names.h"
45 #include "BaseMod.h"
46
47 //
48 // our headers
49 //
50 #include "ParseArgs.h"
51 #include "MuonSelection.h"
52 #include "ElectronSelection.h"
53 #include "IsolationSelection.h"
54 #include "SelectionEMU.h"
55
56 #include "TriggerUtils.h"
57 #include "PassHLT.h"
58 #include "Angles.h"
59 #include "KinematicsStruct.h"
60 #include "InfoStruct.h"
61 #include "GenInfoStruct.h"
62 #include "WeightStruct.h"
63 //#include "GlobalDataAndFuncs.h"
64 #include "RunLumiRangeMap.h"
65 #include "SelectionFuncs.h"
66
67 #include "AngleTuple.h"
68 #include "FOTuple.h"
69
70 #include "SampleWeight.h"
71 #include "EfficiencyWeightsInterface.h"
72 #include "SelectionZ.h"
73
74 #include "SimpleLepton.h"
75
76 #ifndef CMSSW_BASE
77 #define CMSSW_BASE "../"
78 #endif
79
80 using namespace mithep;
81
82 //
83 // globals
84 //----------------------------------------------------------------------------
85 TH1D *hpu_2011, *hpu_2012;
86 mithep::RunLumiRangeMap rlrm;
87 vector<SimpleLepton> failingLeptons ; // for fake estimation
88 vector<SimpleLepton> passingLeptons; // for fake estimation
89 vector<unsigned> cutvec;
90 vector<vector<unsigned> > zcutvec;
91 vector<vector<unsigned> > zzcutvec;
92 map<unsigned,float> evtrhoMap;
93 vector<string> cutstrs;
94 bool passes_HLT_MC;
95 vector<bool> PFnoPUflag;;
96 vector<int> muTrigObjs,eleTrigObjs,muTriggers,eleTriggers;
97 std::bitset<TRIGGER_BIG_NUMBER> triggerBits;
98
99 //
100 // prototypes
101 //----------------------------------------------------------------------------
102 void initRunLumiRangeMap(ControlFlags &ctrl);
103
104 //
105 // MAIN
106 //----------------------------------------------------------------------------
107 int main(int argc, char** argv)
108 //----------------------------------------------------------------------------
109 {
110
111 string cmsswpath(CMSSW_BASE);
112 cmsswpath.append("/src");
113 map<string,unsigned> entrymap; // number of unskimmed entries in each file
114
115 //
116 // args
117 //--------------------------------------------------------------------------------------------------------------
118 ControlFlags ctrl;
119 parse_args( argc, argv, ctrl );
120 if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
121 {
122 cerr << "usage: applySelection.exe <flags> " << endl;
123 cerr << "\tmandoatory flags : " << endl;
124 cerr << "\t--inputfiles | file containing a list of ntuples to run over" << endl;
125 cerr << "\t--inputfile | a file to run over" << endl;
126 cerr << "\t--outputfile | file to store selected evet" << endl;
127 return 1;
128 }
129 ctrl.dump();
130
131
132
133 //
134 // File I/O
135 //--------------------------------------------------------------------------------------------------------------
136 TChain * chain = new TChain("Events");
137 TChain * hltchain = new TChain("HLT");
138
139 string fname;
140 unsigned total_unskimmed=0;
141 if( !ctrl.inputfiles.empty() ) {
142 ifstream f(ctrl.inputfiles.c_str());
143 while (f >> fname) {
144 if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
145 cout << "adding inputfile : " << fname.c_str() << endl;
146 entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
147 total_unskimmed += entrymap[string(fname.c_str())];
148 chain->AddFile(fname.c_str());
149 hltchain->AddFile(fname.c_str());
150 }
151 } else {
152 cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
153 unsigned unsk_ents = unskimmedEntries(ctrl.inputfile.c_str());
154 entrymap[string(ctrl.inputfile.c_str())] = unsk_ents;
155 total_unskimmed += unsk_ents;
156 chain->AddFile(ctrl.inputfile.c_str());
157 hltchain->AddFile(ctrl.inputfile.c_str());
158 }
159 // write the total number of unskimmed events that went into making this output file to a text file
160 writeEntries(ctrl,total_unskimmed);
161
162 const char * ofname;
163 if( strcmp( ctrl.outputfile.c_str(), "") ) {
164 ofname = ctrl.outputfile.c_str();
165 } else {
166 ofname = "tmp.root";
167 }
168 // table of cross section values
169 string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
170 cout << "xspath: " << xspath.c_str() << endl;
171 SimpleTable xstab(xspath.c_str());
172
173 //
174 // Setup
175 //--------------------------------------------------------------------------------------------------------------
176 Angles angles;
177 KinematicsStruct kinematics;
178 InfoStruct evtinfo;
179 WeightStruct weights;
180 GenInfoStruct geninfo;
181
182 AngleTuple nt( (const char*)ofname, (const char*)"zznt");
183 nt.makeAngleBranch(angles);
184 nt.makeKinematicsBranch(kinematics);
185 nt.makeInfoBranch(evtinfo);
186
187
188 FOTuple foTree( nt.getFile(), (const char*)"FOtree");
189 foTree.makeFailedLeptonBranch(failingLeptons);
190 foTree.makePassedLeptonBranch(passingLeptons);
191
192 TH1F * h_wf11_hpt;
193 if(ctrl.mc) {
194 nt.makeWeightBranch(weights);
195 if(ctrl.fillGen ) nt.makeGenInfoBranch(geninfo);
196 // initEfficiencyWeights();
197 cout << "\n\nWARNING: not initializing eff weights\n\n" << endl;
198 initPUWeights();
199
200 // Higgs only, pt reweighting
201 if( ctrl.mH <= 140 ) {
202 char wptname[256];
203 sprintf( wptname, "/scratch/dkralph/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
204 TFile * f_hpt = new TFile(wptname);
205 f_hpt->Print();
206 sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
207 h_wf11_hpt = (TH1F*)(f_hpt->FindObjectAny(wptname));
208 }
209
210 } else {
211 initRunLumiRangeMap(ctrl);
212 }
213
214 // initMuonIDMVA();
215 initElectronIDMVAV1();
216 initMuonIsoMVA();
217 initElectronIDMVA();
218 initElectronIsoMVA();
219 initTrigger();
220
221 muTriggers.push_back(kHLT_IsoMu24_eta2p1); muTrigObjs.push_back(kHLT_IsoMu24_eta2p1_MuObj);
222 eleTriggers.push_back(kHLT_Ele27_WP80); eleTrigObjs.push_back(kHLT_Ele27_WP80_EleObj);
223
224 // hack
225 initEvtRhoMap(evtrhoMap);
226
227
228 //
229 // Setup tree I/O
230 //--------------------------------------------------------------------------------------------------------------
231 string currentFile("");
232
233 UInt_t fNMaxTriggers = TRIGGER_BIG_NUMBER;
234 mithep::EventHeader *info = new mithep::EventHeader();
235 mithep::Array<mithep::PFMet> *metArr = new mithep::Array<mithep::PFMet>();
236 mithep::Array<mithep::Electron> *electronArr = new mithep::Array<mithep::Electron>();
237 mithep::Array<mithep::Muon> *muonArr = new mithep::Array<mithep::Muon>();
238 mithep::Array<mithep::Vertex> *vtxArr = new mithep::Array<mithep::Vertex>();
239 mithep::Array<mithep::PFCandidate> *pfArr = new mithep::Array<mithep::PFCandidate>();
240 mithep::Array<mithep::PFJet> *jetArr = new mithep::Array<mithep::PFJet>();
241 mithep::Array<mithep::PileupInfo> *puArr = new mithep::Array<mithep::PileupInfo>();
242 mithep::Array<mithep::PileupEnergyDensity> *puDArr = new mithep::Array<mithep::PileupEnergyDensity>();
243 mithep::Array<mithep::Track> *trkArr = new mithep::Array<mithep::Track>();
244 mithep::Array<mithep::MCParticle> *mcArr = new mithep::Array<mithep::MCParticle>();
245 mithep::MCEventInfo *mcEvtInfo = new mithep::MCEventInfo();
246 mithep::TriggerMask *trigMask = new mithep::TriggerMask();
247 mithep::TriggerTable *hltTable = new mithep::TriggerTable();
248 vector<string> *hltTableStrings = new vector<string>();
249 vector<string> *hltLabelStrings = new vector<string>();
250 mithep::Array<mithep::TriggerObject> *hltObjArr = new mithep::Array<mithep::TriggerObject>();
251 mithep::Array<mithep::TriggerObjectRel> *hltRelsArr = new mithep::Array<mithep::TriggerObjectRel>();
252 std::vector<std::string> *hltTab = new vector<string>();
253 std::vector<std::string> *hltLab = new vector<string>();
254 mithep::TriggerObjectsTable *fTrigObjs = new TriggerObjectsTable(hltTable,fNMaxTriggers);
255 mithep::Array<mithep::DecayParticle> *fConversions = new mithep::Array<mithep::DecayParticle>();
256
257 TString fTriggerTableName(Names::gkHltTableBrn);
258 chain->SetBranchAddress(Names::gkEvtHeaderBrn, &info);
259 chain->SetBranchAddress("PFMet", &metArr);
260 chain->SetBranchAddress(Names::gkElectronBrn, &electronArr);
261 chain->SetBranchAddress(Names::gkMuonBrn, &muonArr);
262 chain->SetBranchAddress(Names::gkPVBrn, &vtxArr);
263 chain->SetBranchAddress(Names::gkPFCandidatesBrn, &pfArr);
264 chain->SetBranchAddress(Names::gkPFJetBrn, &jetArr);
265 if( ctrl.mc ) {
266 chain->SetBranchAddress(Names::gkPileupInfoBrn, &puArr);
267 chain->SetBranchAddress(Names::gkMCPartBrn, &mcArr);
268 chain->SetBranchAddress(Names::gkMCEvtInfoBrn, &mcEvtInfo);
269 }
270 chain->SetBranchAddress(Names::gkPileupEnergyDensityBrn, &puDArr);
271 chain->SetBranchAddress(Names::gkTrackBrn, &trkArr);
272 chain->SetBranchAddress(Names::gkHltBitBrn, &trigMask);
273 chain->SetBranchAddress(Names::gkHltObjBrn, &hltObjArr);
274 chain->SetBranchAddress("HLTObjectsRelation", &hltRelsArr);
275 chain->SetBranchAddress(Names::gkMvfConversionBrn, &fConversions);
276 hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
277 hltchain->SetBranchAddress(Names::gkHltLabelBrn, &hltLabelStrings);
278
279 mithep::Vertex vtx; // best primary vertex in the event
280
281 // ginfo = NULL;
282 // if( ctrl.mc ) { chain->SetBranchAddress("Gen", &ginfo);}
283
284 // only 1 HLT table / file ???
285 hltchain->GetEntry(0);
286
287 int imax = chain->GetEntries();
288 cout << "nEntries: " << imax << endl;
289
290
291 //
292 // Loop !!!!!!!!!
293 //--------------------------------------------------------------------------------------------------------------
294 for(UInt_t ientry=0; ientry<imax; ientry++) {
295 chain->GetEntry(ientry);
296 if(!(ientry%1000)) cerr << "entry: " << ientry << "\t" << float(ientry)/imax << endl;
297
298 string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
299 if( ctrl.era == 0 )
300 setEra( fname, ctrl );
301
302 // pfNoPU
303 PFnoPUflag.clear();
304 int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
305 assert(pfnopu_size == pfArr->GetEntries());
306
307 // trigger
308 if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
309 currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
310 hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
311 hltchain->GetEntry(0);
312 hltTable->Clear();
313 fillTriggerNames(hltTable, hltTableStrings );
314 }
315 fillTriggerBits( hltTable, trigMask, triggerBits );
316 setHLTObjectRelations( hltObjArr, hltRelsArr, hltTableStrings, hltLabelStrings, fTrigObjs );
317
318 //
319 // data/MC
320 //
321 if(ctrl.mc) {
322 if( ctrl.fillGen )
323 fillGenInfo( mcArr, mcEvtInfo, geninfo, ESampleType::kHZZ, ctrl);
324 } else {
325 if(!(ctrl.noJSON) ) {
326 RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());
327 if( !(rlrm.HasRunLumi(rl)) ) continue;
328 }
329 // if( !passHLTEMU(ctrl,triggerBits) ) {
330 // continue;
331 // }
332 }
333
334 // lepton/kinematic selection ...
335 failingLeptons.clear();
336 passingLeptons.clear();
337 EventData ret4l;
338
339 if(ctrl.fakeScheme.Contains("oneZ"))
340 ret4l = apply_HZZ4L_Z_selection(ctrl, info,
341 hltTable,
342 hltObjArr,
343 fTrigObjs,
344 vtxArr,
345 pfArr,
346 puDArr,
347 electronArr,
348 &electronReferencePreSelection,
349 &electronReferenceIDMVASelectionV1,
350 &electronReferenceIsoSelection,
351 muonArr,
352 &muonReferencePreSelection,
353 &muonIDPFSelection,
354 &muonReferenceIsoSelection);
355 else
356 ret4l = apply_HZZ4L_EMU_selection(ctrl, info,
357 hltTable,
358 hltObjArr,
359 fTrigObjs,
360 vtxArr,
361 pfArr,
362 puDArr,
363 fConversions,
364 electronArr,
365 &electronReferencePreSelection,
366 &electronReferenceIDMVASelectionV1,
367 &electronReferenceIsoSelection,
368 muonArr,
369 &muonReferencePreSelection,
370 &muonIDPFSelection,
371 &muonReferenceIsoSelection);
372
373 if( ret4l.status.pass() ) {
374 fillKinematics(ret4l,kinematics);
375 TLorentzVector pfmet; pfmet.SetPxPyPzE(metArr->At(0)->Mex(),metArr->At(0)->Mey(),0,0);
376 fillEventInfo( info, pfmet, evtinfo, ctrl.mc ? getNPU(puArr) : 0, vtxArr->GetEntries());
377 foTree.Fill();
378 nt.Fill();
379 }
380
381 }
382 foTree.getFile()->cd();
383 foTree.getTree()->Write();
384 nt.WriteClose();
385 }
386 //----------------------------------------------------------------------------
387 void initRunLumiRangeMap(ControlFlags &ctrl)
388 //----------------------------------------------------------------------------
389 {
390 /*
391 rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
392 // rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
393 rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
394 rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));
395 rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));
396 // r11b
397 rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));
398 */
399
400 // 2012 only for now ...
401 rlrm.AddJSONFile(string(ctrl.jsonFile));
402
403 };