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