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