ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/applySelection.cc
(Generate patch)

Comparing UserCode/MitHzz4l/Selection/src/applySelection.cc (file contents):
Revision 1.12 by khahn, Sun Oct 30 15:35:25 2011 UTC vs.
Revision 1.73 by anlevin, Tue Feb 5 09:53:47 2013 UTC

# Line 4 | Line 4
4   #include <vector>                   // STL vector class
5   #include <iostream>                 // standard I/O
6   #include <iomanip>                  // functions to format standard I/O
7 < #include <fstream>                  // functions for file I/O
8 < #include <string>                   // C++ string class
9 < #include <sstream>                  // class for parsing strings
10 < #include <assert.h>
11 < #include <stdlib.h>  
12 < #include <getopt.h>
7 > #include <bitset>  
8 > #include <map>  
9   using namespace std;
10  
11   //
12 < // ROOT headers
12 > // root headers
13   //
14 < #include <TROOT.h>                  // access to gROOT, entry point to ROOT system
15 < #include <TSystem.h>                // interface to OS
16 < #include <TFile.h>                  // file handle class
17 < #include <TNtuple.h>
18 < #include <TTree.h>                  // class to access ntuples
19 < #include <TChain.h>                 //
24 < #include <TBranch.h>                // class to access branches in TTree
25 < #include <TClonesArray.h>           // ROOT array class
26 < #include <TCanvas.h>                // class for drawing
27 < #include <TH1F.h>                   // 1D histograms
28 < #include <TBenchmark.h>             // class to track macro running statistics
29 < #include <TLorentzVector.h>         // 4-vector class
30 < #include <TVector3.h>               // 3D vector class
14 > #include <TFile.h>
15 > #include <TRegexp.h>
16 > #include <TTree.h>                  
17 > #include <TChain.h>                
18 > #include <TBranch.h>                
19 > #include <TClonesArray.h>          
20  
21  
22   //
23 + // TMVA
24 + //
25 + #include "TMVA/Reader.h"
26 + #include "TMVA/Tools.h"
27 + #include "TMVA/Config.h"
28 + #include "TMVA/MethodBDT.h"
29 +
30 + #include "FactorizedJetCorrector.h"
31 + #include "JetCorrectorParameters.h"
32 +
33 + //
34   // ntuple format headers
35   //
36 < #include "HiggsAnaDefs.hh"
37 < #include "TEventInfo.hh"
38 < #include "TElectron.hh"
39 < #include "TMuon.hh"
40 < #include "TJet.hh"
41 < #include "SiMVAElectronSelection.h"
42 < #include "RunLumiRangeMap.h"
43 < #include "EfficiencyWeightsInterface.h"
36 > #include "EventHeader.h"
37 > #include "Electron.h"
38 > #include "Muon.h"
39 > #include "Vertex.h"
40 > #include "PFMet.h"
41 > #include "PFJetCol.h"
42 > #include "PFCandidate.h"
43 > #include "PFCandidateCol.h"
44 > #include "PileupInfoCol.h"
45 > #include "PileupEnergyDensity.h"
46 > #include "MCParticle.h"
47 > #include "TriggerMask.h"
48 > #include "TriggerTable.h"
49 > #include "TriggerObject.h"
50 > #include "TriggerObjectRel.h"
51 > #include "Names.h"
52 > #include "BaseMod.h"
53 > #include "TriggerObjectsTable.h"
54 > #include "JetIDMVA.h"
55  
56   //
57 < // utility headers
57 > // our headers
58   //
59   #include "ParseArgs.h"
60 + #include "MuonSelection.h"
61 + #include "ElectronSelection.h"
62 + #include "ElectronMomentumCorrection.h"
63 + #include "JetSelection.h"
64 + #include "IsolationSelection.h"
65 + #include "ReferenceSelection.h"
66 + #include "MuonMomentumCorrection.h"
67 +
68 + #include "TriggerUtils.h"
69 + #include "PassHLT.h"
70 + #include "KinematicsStruct.h"
71 + #include "InfoStruct.h"
72 + #include "GenInfoStruct.h"
73 + #include "WeightStruct.h"
74 + #include "AngleTuple.h"
75 + #include "FOTuple.h"
76 +
77 + #include "RunLumiRangeMap.h"
78   #include "SampleWeight.h"
79 < #include "Selection.h"
80 < #include "HZZCiCElectronSelection.h"
52 < #include "HZZLikelihoodElectronSelection.h"
53 < #include "HZZBDTElectronSelection.h"
54 < #include "SampleWeight.h"
79 > #include "EfficiencyWeightsInterface.h"
80 > #include "SelectionFuncs.h"
81  
82 < //#define BDTFAIL_THEIR_EVENTS
57 < //#define THEIR_EVENTS
82 > #include "SimpleLepton.h"
83  
84 < Double_t etaFromY(Double_t pt, Double_t y, Double_t phi, Double_t m)
84 > #include "Cintex/Cintex.h"
85 >
86 > #ifndef CMSSW_BASE
87 > #define CMSSW_BASE "../"
88 > #endif
89 >
90 > using namespace mithep;
91 >
92 > //
93 > // globals
94 > //----------------------------------------------------------------------------
95 > TH1D *hpu_2011, *hpu_2012;
96 > RunLumiRangeMap rlrm;
97 > vector<SimpleLepton> failingLeptons ; // for fake estimation
98 > vector<SimpleLepton> passingLeptons;      // for fake estimation
99 > vector<unsigned> cutvec;
100 > vector<vector<unsigned> > zcutvec;
101 > vector<vector<unsigned> > zzcutvec;
102 > map<unsigned,float>       evtrhoMap;
103 > TrigInfo ti;
104 > vector<string> cutstrs;
105 > bool passes_HLT;
106 > vector<bool>   PFnoPUflag;;
107 > map<TString, map<TString,int>* > counts;
108 > ElectronMomentumCorrection electron_momentum_correction;
109 > MuCorr *muCorr;
110 > void addDummyZ2Lepotons(EventData &ret4l);
111 > void setNtupleVals(ControlFlags &ctrl, EventData &ret4l, InfoStruct &evtinfo, Angles &angles, KinematicsStruct &kine,
112 >                   JetInfoStruct &ji, WeightStruct &weights, EventHeader *info,  Array<Muon> *muonArr, Array<Electron> *electronArr, Array<PFJet> *jetArr, Array<PFMet> *metArr,
113 >                   vector<SimpleLepton> &simpleLeptonJets, FactorizedJetCorrector *fJetCorrector, Array<PileupInfo> *puArr,
114 >                   Array<PileupEnergyDensity> *puDArr, JetIDMVA *fJetIDMVA, Array<Vertex> *vtxArr, bitset<1024> &triggerBits,
115 >                   TriggerTable *hltTable, Array<TriggerObject> *hltObjArr, TriggerObjectsTable *fTrigObjs, AngleTuple *nt);
116 > //----------------------------------------------------------------------------
117 > void initRunLumiRangeMap(ControlFlags &ctrl)
118 > //----------------------------------------------------------------------------
119   {
120 <  Double_t a  = (1+exp(2*y))/(exp(2*y)-1); // intermediate term
121 <  if(a*a<1) { cout << "a too small" << endl; assert(0); }
122 <  Double_t E  = sqrt( a*a*(pt*pt+m*m)/(a*a-1) );
123 <  Double_t pz = E*E - pt*pt - m*m;
124 <  if(pz<0) { cout << "imag. pz" << endl; assert(0); }
66 <  pz = sqrt(pz);
67 <  if(y<0) pz *= -1;
68 <  TLorentzVector v;
69 <  v.SetPxPyPzE(pt*cos(phi),pt*sin(phi),pz,E);
70 <  Double_t th = v.Theta();
71 <  return -log(tan(th/2));
72 < };
73 <
74 <
75 < int getChannel(mithep::TGenInfo * ginfo) {
76 <
77 <    int gchannel=-1;
78 <
79 <    if( abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kElectron )  gchannel=0;
80 <    else if( abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kMuon ) gchannel=1;
81 <    else if( (abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kMuon) ||
82 <             (abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kElectron) ) gchannel=2;
83 <    
84 <    return gchannel;
120 >  if(ctrl.jsonFile==""){
121 >    cout << "WARNING: running with empty jsonFile, so setting noJSON to true" << endl;
122 >    ctrl.noJSON = true;
123 >  }
124 >  rlrm.AddJSONFile(string(ctrl.jsonFile));  
125   }
126  
127 + //
128 + // prototypes
129 + //----------------------------------------------------------------------------
130  
131 <
132 < //=== MAIN =================================================================================================
131 > //
132 > // MAIN
133 > //----------------------------------------------------------------------------
134   int main(int argc, char** argv)
135 + //----------------------------------------------------------------------------
136   {
137 +
138 +  vector<TString> cuts;
139 +  cuts.push_back("start"); // NOTE: is incremented in ReferenceSelection, so if you turn on JSONS be careful
140 +  cuts.push_back("trigger");
141 +  cuts.push_back("sfOsHiPt");
142 +  cuts.push_back("4<mZ2<120");
143 +  cuts.push_back("pt>20,10");
144 +  cuts.push_back("mll>4");
145 +  cuts.push_back("m4l>70");
146 +  cuts.push_back("mZ2>12");
147 +  cuts.push_back("m4l>100");
148 +  cuts.push_back("m4l > 100 && >= 1 jets");
149 +  cuts.push_back("m4l > 100 && 2 jets");
150 +  cuts.push_back("m4l > 100 && fisher > 0.4");
151 +
152 +  for(unsigned icut=0; icut<cuts.size(); icut++)
153 +    counts[cuts[icut]] = new map<TString,int>;
154 +
155 +  map<TString,map<TString,int>* >::iterator cit;
156 +  for(cit=counts.begin(); cit!=counts.end(); cit++) {
157 +    map<TString,int> *cts = (*cit).second;
158 +    (*cts)["all"]  = int(0);
159 +    (*cts)["4e"]   = int(0);
160 +    (*cts)["4m"]   = int(0);
161 +    (*cts)["2e2m"] = int(0);
162 +  }
163    
164 +  string cmsswpath(CMSSW_BASE);
165 +  cmsswpath.append("/src");
166 +  bitset<1024> triggerBits;
167    vector<vector<string> > inputFiles;
168    inputFiles.push_back(vector<string>());
169 +  map<string,unsigned> entrymap; // number of unskimmed entries in each file
170  
171    //
172    // args
173    //--------------------------------------------------------------------------------------------------------------
174    ControlFlags ctrl;
175    parse_args( argc, argv, ctrl );
101  if( ctrl.inputfile.empty() ||
102      ctrl.inputfile.empty() ) {
103    cerr << "usage: applySelection.exe <flags> " << endl;
104    cerr << "\tmandoatory flags : " << endl;
105    cerr << "\t--inputfile |  file containing a list of ntuples to run over" << endl;
106    cerr << "\t--outputfile | file to store  selected evet" << endl;
107    return 1;
108  }
176    ctrl.dump();
177 <
178 <  map<string,double> entrymap; // number of unskimmed entries in each file
177 >  cout << "\n\nNOTE: now setting era with arguments... double check this doesnt mess something else up\n\n" << endl;
178 >  assert(ctrl.doFSR);
179  
180    //
181    // File I/O
182    //--------------------------------------------------------------------------------------------------------------
183    TChain * chain = new TChain("Events");
184 <  ifstream f(ctrl.inputfile.c_str());
184 >  TChain * hltchain = new TChain("HLT");
185 >  TChain * runchain = new TChain(Names::gkRunTreeName);
186 >
187    string fname;
188 <  while (f >> fname) {
189 <    if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
190 <    cout << "adding inputfile : " << fname.c_str() << endl;
191 <    entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
192 <    chain->AddFile(fname.c_str());
188 >  unsigned total_unskimmed=0;
189 >  if( !ctrl.inputfiles.empty() ) {
190 >    ifstream f(ctrl.inputfiles.c_str());
191 >    while (f >> fname) {
192 >      if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
193 >      if(fname.find("/store/") != string::npos) fname = "root://eoscms//"+fname;
194 >      cout << "adding inputfile : " << fname.c_str() << endl;
195 >      entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
196 >      cerr << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
197 >      total_unskimmed += entrymap[string(fname.c_str())];
198 >      chain->AddFile(fname.c_str());
199 >      hltchain->AddFile(fname.c_str());
200 >      runchain->AddFile(fname.c_str());
201 >    }
202 >  } else {
203 >    if(ctrl.inputfile.find("/store/") != string::npos) ctrl.inputfile = "root://eoscms//"+ctrl.inputfile;
204 >    cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
205 >    unsigned unsk_ents = unskimmedEntries(ctrl.inputfile.c_str());
206 >    entrymap[string(ctrl.inputfile.c_str())] = unsk_ents;
207 >    cerr << "unskimmed entries: " << unsk_ents << endl;
208 >    total_unskimmed += unsk_ents;
209 >    chain->AddFile(ctrl.inputfile.c_str());
210 >    hltchain->AddFile(ctrl.inputfile.c_str());
211 >    runchain->AddFile(ctrl.inputfile.c_str());
212    }
213 +  // write the total number of unskimmed events that went into making this output file to a text file
214 +  writeEntries(ctrl,total_unskimmed);
215 +  ctrl.totalMC = total_unskimmed;
216  
126  // table of cross section values  
127  SimpleTable xstab("./data/xs.dat");
128
129  //
130  // Setup
131  //--------------------------------------------------------------------------------------------------------------
217    const char * ofname;
218    if( strcmp( ctrl.outputfile.c_str(), "") ) {
219      ofname = ctrl.outputfile.c_str();
220    } else {
221      ofname = "tmp.root";
222    }
223 <  TFile *file = new TFile(ofname, "RECREATE");
224 <  TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
225 <  TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 1024, 0, 1024 );
226 <  TTree * passtuple = new TTree("passtuple", "passtuple" );
227 <  unsigned run, evt, lumi, channel, gchannel;
228 <  float mZ1, mZ2, m4l, pt4l;
229 <  unsigned tZ1, tZ2;
145 <  double w, woff, werroff, won, werron;
146 <  if( ctrl.mc )   {
147 <    passtuple->Branch("gchannel", &gchannel);
148 <    passtuple->Branch("woff", &woff);
149 <    passtuple->Branch("werroff", &werroff);
150 <    passtuple->Branch("won", &won);
151 <    passtuple->Branch("werron", &werron);
152 <  }
153 <  passtuple->Branch("channel", &channel);
154 <  passtuple->Branch("run", &run);
155 <  passtuple->Branch("evt", &evt);
156 <  passtuple->Branch("lumi", &lumi);
157 <  passtuple->Branch("mZ1", &mZ1);
158 <  passtuple->Branch("mZ2", &mZ2);
159 <  passtuple->Branch("tZ1", &tZ1);
160 <  passtuple->Branch("tZ2", &tZ2);
161 <  passtuple->Branch("m4l", &m4l);
162 <  passtuple->Branch("pt4l", &pt4l);
163 <  passtuple->Branch("w", &w);
164 <
165 <  initCiCSelection();
166 <  if(ctrl.eleSele=="lik") initLikSelection();
167 <  if(ctrl.eleSele=="bdt") initBDTSelection();
168 <  // if(ctrl.eleSele=="si" ) initSiMVAElectronSelection();
169 <  initRunLumiRangeMap();
170 <  initEfficiencyWeights();
223 >  // table of cross section values  
224 >  string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
225 >  cout << "xspath: " << xspath.c_str() << endl;
226 >  SimpleTable xstab(xspath.c_str());
227 >  if(ctrl.mc) {
228 >    if(ctrl.use_xs_weights) getWeight(xstab,entrymap,chain); // test *now* whether we've got the xs in here, so it fails before it creates the output
229 >  }
230  
231 +  //root will write to this temporary file instead of to memory and the selection will run faster
232  
233 +  //
234 +  // Setup
235 +  //--------------------------------------------------------------------------------------------------------------
236 +  Angles angles;
237 +  KinematicsStruct kine;
238 +  InfoStruct evtinfo;
239 +  WeightStruct weights;
240 +  GenInfoStruct geninfo;
241 +  JetInfoStruct ji;
242 +  vector<SimpleLepton> simpleLeptonJets;
243 +
244 +  AngleTuple nt( (const char*)ofname, (const char*)"zznt");
245 +  nt.makeAngleBranch(angles);
246 +  nt.makeKinematicsBranch(kine);
247 +  nt.makeInfoBranch(evtinfo);
248 +  nt.makeJetInfoBranch(ji);
249 +  //nt.makeJetAngleBranches("DataStruct/data/jetAngleVars.txt");
250 +
251 +  FOTuple *foTree=0;
252 +  if(ctrl.fakeScheme != "none") {
253 +    foTree = new FOTuple( nt.getFile(), (const char*)"FOtree");
254 +    foTree->makeInfoBranch(evtinfo);
255 +    foTree->makeFailedLeptonBranch(failingLeptons);
256 +    foTree->makePassedLeptonBranch(passingLeptons);
257 +  }
258  
259 +  int genSampleType(0);
260 +  if(ctrl.mc) {
261 +    nt.makeWeightBranch(weights);
262 +    if(ctrl.fillGen) {
263 +      genSampleType = getGenSampleType(ofname);
264 +      if(genSampleType == -1) {
265 +        cout << "\ngenSampleType returned as -1, setting fillGen to false" << endl;
266 +        ctrl.fillGen = false;
267 +      } else
268 +        nt.makeGenInfoBranch(geninfo);
269 +    }
270 +    if(ctrl.use_eff_weights) initEfficiencyWeights();
271 +  } else {
272 +    if(!(ctrl.noJSON) )
273 +      initRunLumiRangeMap(ctrl);
274 +  }
275 +
276 +  initElectronIDMVAV1();
277 +  initTrigger();
278 +  initAnalysisTriggers(ti);
279 +  initMela(ctrl);
280 +  initMassErrors();
281 +  ti.dump();
282 +
283 +  TFile tmp_file("tmp_file.root");
284 +
285 +  if(ctrl.do_eregression) {
286 +    string regPath("EGamma/EGammaAnalysisTools/data/eleEnergyRegWeights_V1.root");
287 +    // if(TString(getenv("HOSTNAME")).Contains(TRegexp("t3[bd][te][cs][hk]")))
288 +    regPath = cmsswpath + "/" + regPath;
289 +    electron_momentum_correction.initialize_regression(regPath);
290 +  }
291 +
292 +  if(ctrl.do_escale) {
293 +    if(ctrl.mc) {
294 +      if(ctrl.era == 2011) ctrl.smearing_scaling_dataset = "Fall11";
295 +      else                 ctrl.smearing_scaling_dataset = "Summer12_DR53X_HCP2012";
296 +    } else {
297 +      if(ctrl.era == 2011) ctrl.smearing_scaling_dataset = "Jan16ReReco";
298 +      else                 ctrl.smearing_scaling_dataset = "Moriond2013";
299 +    }
300 +  }
301 +  if(ctrl.correct_muon_momentum) {
302 +    muCorr = new MuCorr;
303 +    muCorr->corr2011 = new rochcor;
304 +    muCorr->corr2012 = new rochcor2012;
305 +  }
306 +
307 +  vector<TString> fJetCorrParsv;
308 +  vector<JetCorrectorParameters> correctionParameters;
309 +  FactorizedJetCorrector *fJetCorrector;
310 +  JetIDMVA *fJetIDMVA;
311 +  initJets(ctrl, cmsswpath, fJetCorrParsv, correctionParameters, fJetCorrector, fJetIDMVA);
312  
313    //
314 <  // Loop
314 >  // Setup tree I/O
315    //--------------------------------------------------------------------------------------------------------------
178  if(ctrl.fakeScheme!="none") {cout << "wrong executable!" << endl; assert(0);}
179  
180  //
181  // Access samples and fill histograms
316    TFile *inputFile=0;
317    TTree *eventTree=0;  
318 <  
319 <  // Data structures to store info from TTrees
320 <  mithep::TEventInfo *info    = new mithep::TEventInfo();
321 <  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
322 <  TClonesArray *electronArr   = new TClonesArray("mithep::TElectron");
323 <  TClonesArray *muonArr       = new TClonesArray("mithep::TMuon");
318 >  string currentFile("");
319 >
320 >  UInt_t fNMaxTriggers = TRIGGER_BIG_NUMBER;
321 >  EventHeader                   *info                    = new EventHeader();
322 >  Array<PFMet>                  *metArr                  = new Array<PFMet>();
323 >  Array<Electron>               *electronArr             = new Array<Electron>();
324 >  Array<Muon>                   *muonArr                 = new Array<Muon>();
325 >  Array<Vertex>                 *vtxArr                  = new Array<Vertex>();
326 >  Array<PFCandidate>            *pfArr                   = new Array<PFCandidate>();
327 >  Array<PFJet>                  *jetArr                  = new Array<PFJet>();
328 >  Array<PileupInfo>             *puArr                   = new Array<PileupInfo>();
329 >  Array<PileupEnergyDensity>    *puDArr                  = new Array<PileupEnergyDensity>();
330 >  Array<Track>                  *trkArr                  = new Array<Track>();
331 >  Array<MCParticle>             *mcArr                   = new Array<MCParticle>();
332 >  MCEventInfo                   *mcEvtInfo               = new MCEventInfo();
333 >  TriggerMask                   *trigMask                = new TriggerMask();
334 >  TriggerTable                  *hltTable                = new TriggerTable(fNMaxTriggers);
335 >  vector<string>                *hltTableStrings         = new vector<string>();
336 >  vector<string>                *hltLabelStrings         = new vector<string>();
337 >  Array<TriggerObject>          *hltObjArr               = new Array<TriggerObject>();
338 >  Array<TriggerObjectRel>       *hltRelsArr              = new Array<TriggerObjectRel>();
339 >  TriggerObjectsTable           *fTrigObjs               = new TriggerObjectsTable(hltTable,fNMaxTriggers);
340 >  RunInfo                       *runInfo                 = new RunInfo();
341    
342 +  chain->SetBranchAddress(Names::gkEvtHeaderBrn,                &info);
343 +  chain->SetBranchAddress("PFMet",                              &metArr);
344 +  chain->SetBranchAddress(Names::gkElectronBrn,                 &electronArr);
345 +  chain->SetBranchAddress(Names::gkMuonBrn,                     &muonArr);
346 +  chain->SetBranchAddress(Names::gkPVBrn,                       &vtxArr);
347 +  chain->SetBranchAddress(Names::gkPFCandidatesBrn,             &pfArr);
348 +  chain->SetBranchAddress(Names::gkPFJetBrn,                    &jetArr);
349 +  if( ctrl.mc ) {
350 +    chain->SetBranchAddress(Names::gkPileupInfoBrn,             &puArr);
351 +    chain->SetBranchAddress(Names::gkMCPartBrn,                 &mcArr);
352 +    chain->SetBranchAddress(Names::gkMCEvtInfoBrn,              &mcEvtInfo);
353 +  }
354 +  chain->SetBranchAddress(Names::gkPileupEnergyDensityBrn,      &puDArr);
355 +  chain->SetBranchAddress(Names::gkTrackBrn,                    &trkArr);
356 +  chain->SetBranchAddress(Names::gkHltBitBrn,                   &trigMask);
357 +  chain->SetBranchAddress(Names::gkHltObjBrn,                   &hltObjArr);
358 +  chain->SetBranchAddress("HLTObjectsRelation",                 &hltRelsArr);
359 +  hltchain->SetBranchAddress(Names::gkHltTableBrn,              &hltTableStrings);
360 +  hltchain->SetBranchAddress(Names::gkHltLabelBrn,              &hltLabelStrings);
361  
362 <  chain->SetBranchAddress("Info",       &info);
193 <  chain->SetBranchAddress("Electron", &electronArr);
194 <  chain->SetBranchAddress("Muon", &muonArr);
195 <  if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
362 >  runchain->SetBranchAddress(Names::gkRunInfoBrn, &runInfo);
363  
364 +  Vertex              vtx;          // best primary vertex in the event
365  
366 <  int count=0, pass=0;
199 <  float denom[3]={0.,0.,0.};
200 <  float numer[3]={0.,0.,0.};
201 <  float numercorr[3]={0.,0.,0.};
366 >  hltchain->GetEntry(0);
367  
368 <  UInt_t imax = chain->GetEntries();
369 <  cout << "nEntries: " << imax << endl;
368 >  int imax = (ctrl.n_events_to_process < 0) ? chain->GetEntries() : ctrl.n_events_to_process;
369 >  cerr << "processing: " << imax << " ( / " << chain->GetEntries() << " )" << endl;
370  
371 +  if(ctrl.mc && ctrl.debugOffMc) // necessary for batch mode -- we need debug output for, data but size is too large to store MC output
372 +    ctrl.debug = false;
373  
374 +  int lastHltEntry=-2;
375    for(UInt_t ientry=0; ientry<imax; ientry++) {
376      chain->GetEntry(ientry);
377 <    if(!(ientry%100000)) cerr << "entry: " << ientry << endl;
377 >    if(!(ientry%1000)) cerr << "entry: " << ientry << " ( / " << imax << " )" << endl;
378  
379 <    // get event weight for this file                                                    
212 <    double eventweight=1;
213 <    if(ctrl.mc) eventweight = getWeight(xstab,entrymap,chain);
214 <
215 < #ifdef THEIR_EVENTS
216 <    if( !( info->evtNum == 504867308  ||
217 <           info->evtNum == 368148849  ||
218 <           //      info->evtNum == 129514273  ||
219 <           info->evtNum == 286336207  ||
220 <           info->evtNum == 344708580  ||
221 <           info->evtNum == 30998576   ||
222 <           info->evtNum == 155679852  ||
223 <           info->evtNum == 394010457  ||
224 <           info->evtNum == 917379387  ||
225 <           info->evtNum == 78213037   ||
226 <           info->evtNum == 862270386  ||
227 <           info->evtNum == 337493970  ||  // not baseline anymore?
228 <           info->evtNum == 1491724484 ||
229 <           info->evtNum == 480301165  ||
230 <           info->evtNum == 1038911933 ||
231 <           info->evtNum == 876658967  ||
232 <           info->evtNum == 966824024  ||
233 <           info->evtNum == 141954801  ||
234 <           info->evtNum == 160966858  ||
235 <           info->evtNum == 191231387  ||
236 <           info->evtNum == 66033190   ||
237 <           info->evtNum == 10347106   ||
238 <           info->evtNum == 107360878  ||
239 <           info->evtNum == 2554393033 ||
240 <           info->evtNum == 933807102  ||
241 <           info->evtNum == 1188043146 ||
242 <           info->evtNum == 559839432  ||
243 <           info->evtNum == 16706390   ||
244 <           info->evtNum == 65557571   ||
245 <           info->evtNum == 389185367  ||
246 <           info->evtNum == 2722114329 ) ) continue;
247 < #endif
248 <
379 >    //if(info->EvtNum() != 217502611) continue;
380  
381 < #ifdef BDTFAIL_THEIR_EVENTS
382 <    if( !( info->evtNum == 78213037   ||
383 <           info->evtNum == 876658967  ) ) continue;
384 <          
385 < #endif
381 >    increment(counts,"start");
382 >    if( ctrl.debug ) {
383 >      cout << "-----------------------------------------------------------------" << endl;
384 >      cout << "-----------------------------------------------------------------" << endl;
385 >      cout << "Run: " << info->RunNum()
386 >           << "\tEvt: " << info->EvtNum()
387 >           << "\tLumi: " << info->LumiSec()
388 >           << endl;
389 >      cerr << "Run: " << info->RunNum()
390 >           << "\tEvt: " << info->EvtNum()
391 >           << "\tLumi: " << info->LumiSec()
392 >           << endl;
393 >      cout << "-----------------------------------------------------------------" << endl;
394 >    }
395  
396 <    unsigned evtfail = fails_HZZ4L_selection(ctrl, info, electronArr, muonArr, eventweight, passtuple, NULL);
397 <    //    if( !evtfail ) {
398 < //       cout << "run: " << info->runNum
399 < //         << "\tevt: " << info->evtNum
400 < //         << "\tlumi: " << info->lumiSec
401 < //         << "\tfile: " << chain->GetFile()->GetEndpointUrl()->GetFile()
402 < //         << endl;
403 < //    }
396 >    runchain->GetEvent(info->RunEntry());
397 >    hltchain->GetEntry(runInfo->HltEntry());
398 >    hltTable->Clear();
399 >    fillTriggerNames(hltTable, hltTableStrings );
400 >    // if(ctrl.debug && (lastHltEntry != runInfo->HltEntry())) hltTable->Print();
401 >    lastHltEntry = runInfo->HltEntry();
402 >    fillTriggerBits( hltTable, trigMask, triggerBits );
403 >    setHLTObjectRelations( hltObjArr, hltRelsArr, hltTableStrings, hltLabelStrings, fTrigObjs );
404 >    // printTriggerObjs( hltTable, fTrigObjs);
405 >      
406 >    string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
407 >    if(ctrl.debug) cout << "era is " << ctrl.era << endl;
408 >    if( ctrl.era == 0  ) {
409 >      setEra( fname, ctrl );
410 >      if(ctrl.debug) cout << "era is now " << ctrl.era << endl;
411 >    }
412  
413 < #ifdef THEIR_EVENTS
414 <    if(evtfail!=0) cout << "Failed!" << endl;
415 < #endif
413 >    // pfNoPU
414 >    PFnoPUflag.clear();
415 >    int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
416 >    assert(pfnopu_size == pfArr->GetEntries());
417 >
418 >    // trigger
419 >    if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
420 >      currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
421 >    }
422 >    if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
423 >    passes_HLT = passHLT(ctrl, triggerBits, ti, info->RunNum(), hltTable, fTrigObjs); // NOTE: run value is not used if it's MC
424 >
425 >    // data/MC
426 >    if(ctrl.mc) {
427 >      if(ctrl.fillGen) fillGenInfo( mcArr, mcEvtInfo, geninfo, genSampleType, ctrl);
428 >      // NOTE: xs weight and npuw are reset in merge.cc
429 >      if(ctrl.use_xs_weights)
430 >        weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
431 >      else
432 >        weights.w = 1;
433 >      weights.npu = getNPU(puArr, 0);
434 >      weights.npuw = getPUWeight(ctrl.era, fname, weights.npu);
435 >    } else {
436 >      // JSON
437 >      if(!(ctrl.noJSON) ) {
438 >        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
439 >        if( !(rlrm.HasRunLumi(rl)) )  {
440 >          if( ctrl.debug ) cout << "fails JSON" << endl;
441 >          continue;
442 >        }
443 >      }
444 >    }
445 >
446 >    //
447 >    // lepton/kinematic selection ...
448 >    //
449 >    failingLeptons.clear();
450 >    passingLeptons.clear();
451 >    EventData ret4l = apply_HZZ4L_reference_selection(ctrl, info,
452 >                                                      vtxArr,
453 >                                                      pfArr,
454 >                                                      puDArr,
455 >                                                      electronArr,
456 >                                                      &electronReferencePreSelection,
457 >                                                      &electronReferenceIDMVASelectionV1, // not used anymore! (Blame the damn regression implementation, not me!)
458 >                                                      &electronReferenceIsoSelection,
459 >                                                      muonArr,
460 >                                                      &muonReferencePreSelection,
461 >                                                      &muonIDPFSelection,
462 >                                                      &muonReferenceIsoSelection);
463 >      
464 >    if( ctrl.debug ) cout << endl;
465 >
466 >    if(ctrl.fakeScheme == "none") { // fill angletuple if event passes full selection
467 >      if( ret4l.status.pass() ) {
468 >        setNtupleVals(ctrl, ret4l, evtinfo, angles, kine, ji, weights, info, muonArr, electronArr, jetArr, metArr, simpleLeptonJets, fJetCorrector, puArr, puDArr, fJetIDMVA, vtxArr, triggerBits, hltTable, hltObjArr, fTrigObjs, &nt);
469 >        nt.Fill();
470      
471 <    h_evtfail->Fill( evtfail, eventweight );
471 >        int theZ1type = kine.l1type;
472 >        int theZ2type = kine.l3type;
473  
474 <    if( ctrl.mc ) {
475 <      // BR is implicit here ...
476 <      int gchannel = getChannel(ginfo);
477 <        if( gchannel >= 0 ) {
478 < //        denom[gchannel] += eventweight;
479 < //        if( !evtfail ) numer[gchannel] += eventweight;
480 <          denom[gchannel] += 1;
481 <          if( !evtfail ) numer[gchannel] += 1;
482 <
483 <          // get eff weight for current evt
484 <          if( !evtfail ) {
485 <            double thewoff, thewon;
486 <            passtuple->SetBranchAddress("woff", &thewoff); // must put this here ...
487 <            passtuple->SetBranchAddress("won",  &thewon); // must put this here ...
285 <            //      cout << "thewoff: " << thewoff << "\tthewon: " << thewon << endl;
286 <            int ret = passtuple->GetEntry(passtuple->GetEntries()-1);
287 <            numercorr[gchannel] += thewoff*thewon;        
474 >        if(ctrl.debug) cout << "simpleLeptonJets.size() = " << simpleLeptonJets.size() << endl;
475 >
476 >        if(kine.m4l > 100){
477 >          if(simpleLeptonJets.size() >= 1) {
478 >            increment(counts,"m4l > 100 && >= 1 jets",theZ1type,theZ2type);
479 >            if(simpleLeptonJets.size() == 2) {
480 >              increment(counts,"m4l > 100 && 2 jets",theZ1type,theZ2type);
481 >              SimpleLepton j1 = simpleLeptonJets[0];
482 >              SimpleLepton j2 = simpleLeptonJets[1];
483 >              double deta = abs(j1.vec.Eta()-j2.vec.Eta());
484 >              double mjj = (j1.vec + j2.vec).M();
485 >              double fisher = 0.09407 * deta  +0.00041581*mjj;
486 >              if(fisher > 0.4) increment(counts, "m4l > 100 && fisher > 0.4",theZ1type,theZ2type);
487 >            }
488            }
489          }
490 +
491 +        
492 +      } else {
493 +        if(ctrl.debug) cout << "failing with code : " << ret4l.status.selectionBits << endl;
494 +      }
495 +    } else { // fill FOtuple and angletuple if event has a good Z and one or two extra leptons
496 +      assert(ctrl.fakeScheme=="ZPlusF" || ctrl.fakeScheme=="ZPlusFF");
497 +      if(ret4l.status.selectionBits.test(PASS_GOODZ1) ) {
498 +        unsigned nleps = passingLeptons.size() + failingLeptons.size();
499 +        if( (nleps>=3 && ctrl.fakeScheme=="ZPlusF" ) || (nleps>=4 && ctrl.fakeScheme=="ZPlusFF" ) ) {
500 +          addDummyZ2Lepotons(ret4l);
501 +          setNtupleVals(ctrl, ret4l, evtinfo, angles, kine, ji, weights, info, muonArr, electronArr, jetArr,  metArr, simpleLeptonJets, fJetCorrector, puArr, puDArr, fJetIDMVA, vtxArr, triggerBits, hltTable, hltObjArr, fTrigObjs, &nt);
502 +          nt.Fill();
503 +          foTree->Fill();
504 +        }
505 +      } else {
506 +        if(ctrl.debug) cout << "failing PASS_GOODZ1" << endl;
507 +      }
508      }
509  
510 <    count++;
293 <    if( !evtfail ) pass++;
294 <          
295 <  } //end loop over data    
510 >    hltTable->Delete();
511  
297  if( ctrl.mc ) {
298    cout << "--------------" << endl;
299    cout << "denom: " << count << endl;
300    cout << "pass: " << pass << endl;
301    cout << "axe: " << (float)pass/count << endl;
302    cout << "--------------" << endl;
512    }
513  
514 <  delete info;
515 <  delete electronArr;
516 <  delete muonArr;
514 >  if(ctrl.fakeScheme != "none") {
515 >    foTree->getFile()->cd();
516 >    foTree->getTree()->Write();
517 >  }
518 >  nt.WriteClose();
519  
520 +  for(unsigned icut=0; icut<cuts.size(); icut++) {
521 +    map<TString,int> thisCount(*(counts[cuts[icut]]));
522 +    cerr << setw(25) << cuts[icut] << "\t" << setw(9) << thisCount["all"] << "  ("
523 +         << setw(9) << thisCount["4e"] << setw(9) << thisCount["4m"] << setw(9) << thisCount["2e2m"] << ")" << endl;
524 +  }
525  
526 +  tmp_file.Close();
527  
311  //--------------------------------------------------------------------------------------------------------------
312  // Save Histograms;
313  //==============================================================================================================
314  // const char * ofname;
315  // if( strcmp( ctrl.outputfile.c_str(), "") ) {
316  //   ofname = ctrl.outputfile.c_str();
317  // } else {
318  //   ofname = "tmp.root";
319  // }
320
321  // TFile *file = new TFile(ofname, "RECREATE");
322  gROOT->cd(file->GetPath());
323  h_evt->Write();
324  h_evtfail->Write();
325  passtuple->Write();
326  file->Close();
327  delete file;
328
329  // map<TString,TMVA::Reader*>::iterator it;
330  // for(it=readers.begin(); it!=readers.end(); it++) delete (*it).second;
331
332
333  float acc_4e        = numer[0]/denom[0];
334  float acc_4mu       = numer[1]/denom[1];
335  float acc_2e2mu     = numer[2]/denom[2];
336  float acc_err_4e    = (1./denom[0])*sqrt(numer[0]*(1-acc_4e));
337  float acc_err_4mu   = (1./denom[1])*sqrt(numer[1]*(1-acc_4mu));
338  float acc_err_2e2mu = (1./denom[2])*sqrt(numer[2]*(1-acc_2e2mu));
339
340  float acc_c_4e        = numercorr[0]/denom[0];
341  float acc_c_4mu       = numercorr[1]/denom[1];
342  float acc_c_2e2mu     = numercorr[2]/denom[2];
343  float acc_c_err_4e    = (1./denom[0])*sqrt(numer[0]*(1-acc_4e));
344  float acc_c_err_4mu   = (1./denom[1])*sqrt(numer[1]*(1-acc_4mu));
345  float acc_c_err_2e2mu = (1./denom[2])*sqrt(numer[2]*(1-acc_2e2mu));
346
347  if( ctrl.mc ) {
348  cerr << "---------------------" << endl;
349 //   cerr << "acc_4e: " << fixed << setw(8) << setprecision(7) << numer[0] << endl; // numer already weighted ...
350 //   cerr << "acc_4mu: " << fixed << setw(8) << setprecision(7) << numer[1] <<  endl;
351 //   cerr << "acc_2e2mu: " << fixed << setw(8) << setprecision(7) << numer[2] << endl;
352  cerr << "acc_4e: " << fixed << setw(8) << setprecision(7) << acc_4e << " +/- " << acc_err_4e << endl;
353  cerr << "acc_4mu: " << fixed << setw(8) << setprecision(7) << acc_4mu << " +/- " << acc_err_4mu << endl;
354  cerr << "acc_2e2mu: " << fixed << setw(8) << setprecision(7) << acc_2e2mu << " +/- " << acc_err_2e2mu << endl;
355  cerr << "-------corrected------" << endl;
356  cerr << "acc_c_4e: " << fixed << setw(8) << setprecision(7) << acc_c_4e << " +/- " << acc_c_err_4e << endl;
357  cerr << "acc_c_4mu: " << fixed << setw(8) << setprecision(7) << acc_c_4mu << " +/- " << acc_c_err_4mu << endl;
358  cerr << "acc_c_2e2mu: " << fixed << setw(8) << setprecision(7) << acc_c_2e2mu << " +/- " << acc_c_err_2e2mu << endl;
359  cerr << "---------------------" << endl;
528   }
529 + //----------------------------------------------------------------------------------------
530 + void addDummyZ2Lepotons(EventData &ret4l)
531 + // for fake ntuples, we don't know which are the Z2 leptons yet, but need to fill something so we don't seg fault
532 + {
533 +  SimpleLepton dum1,dum2;
534 +  dum1.vec.SetPtEtaPhiM(1,2,3,4);
535 +  dum2.vec.SetPtEtaPhiM(5,6,7,8);
536 +  ret4l.Z2leptons.push_back(dum1);
537 +  ret4l.Z2leptons.push_back(dum2);
538 + }
539 + //----------------------------------------------------------------------------------------
540 + void setNtupleVals(ControlFlags &ctrl, EventData &ret4l, InfoStruct &evtinfo, Angles &angles, KinematicsStruct &kine,
541 +                   JetInfoStruct &ji, WeightStruct &weights, EventHeader *info, Array<Muon> *muonArr, Array<Electron> *electronArr, Array<PFJet> *jetArr, Array<PFMet> *metArr,
542 +                   vector<SimpleLepton> &simpleLeptonJets, FactorizedJetCorrector *fJetCorrector, Array<PileupInfo> *puArr,
543 +                   Array<PileupEnergyDensity> *puDArr, JetIDMVA *fJetIDMVA, Array<Vertex> *vtxArr, bitset<1024> &triggerBits,
544 +                   TriggerTable *hltTable, Array<TriggerObject> *hltObjArr, TriggerObjectsTable *fTrigObjs, AngleTuple *nt)
545 + {
546 +  fillKinematics(ret4l,kine);
547  
548 <  cerr << "done!" << endl;
549 < }
548 >  //the last two arguments are usePt and useY respectively
549 >  fillMela(kine,0,0);
550  
551 +  fillMassErrors(ctrl,ret4l,muonArr,electronArr,kine);
552  
553 +  TLorentzVector pfmet; pfmet.SetPxPyPzE(metArr->At(0)->Mex(),metArr->At(0)->Mey(),0,0);
554 +  fillEventInfo( info, pfmet, evtinfo, ctrl.mc ? getNPU(puArr) : 0, vtxArr->GetEntries());
555 +  fillJets(ctrl, jetArr, simpleLeptonJets, fJetCorrector, puDArr, fJetIDMVA, vtxArr, ret4l);
556 +  fillJetInfo(simpleLeptonJets, ji, ctrl);
557 +  //fillJetAngleBranches(ji, ret4l, nt, ctrl);
558 +  evtinfo.status = 0;
559 +  evtinfo.status = setHltBits(ti, triggerBits);
560 +
561 +  if(ctrl.fakeScheme == "none") {
562 +    fillAngles(ret4l,angles);
563 +
564 +    if( ctrl.mc) {
565 +      if(ctrl.use_eff_weights) setEfficiencyWeights(ctrl, ctrl.era, ret4l, triggerBits, hltTable, hltObjArr, fTrigObjs, weights);
566 +      if(ctrl.debug)
567 +        cout << "w: " << weights.w << "\t" << "npuw: " << weights.npuw << "\t" << "won: " << weights.won << "\t" << "woff: " << weights.woff << endl;
568 +    }
569  
570 +    if(ctrl.debug) {
571 +      stringstream ss;
572 +      ss << "PASS:: " << "\trun: " << evtinfo.run << "\tevt: " << evtinfo.evt << "\tlumi: " << evtinfo.lumi << "\tchannel: "
573 +         << kine.channel << "\tm4l: " << kine.m4l << "\tmZ1: " << kine.mZ1 << "\tmZ2: " << kine.mZ2 << endl;
574 +      cerr << ss.str();
575 +      cout << ss.str();
576 +    }
577 +  } else {
578 +    // fillAngles(ret4l,angles); // at this point we don't know which four leptons will constitute the ZZ system, so doesn't make sense to fill the angles
579 +  }
580 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines