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.14 by khahn, Sat May 5 21:43:55 2012 UTC vs.
Revision 1.70 by anlevin, Sun Jan 20 06:55:17 2013 UTC

# Line 5 | Line 5
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>                  
14 > #include <TFile.h>
15 > #include <TRegexp.h>
16   #include <TTree.h>                  
17   #include <TChain.h>                
18   #include <TBranch.h>                
# Line 25 | Line 27 | using namespace std;
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   //
# Line 32 | Line 37 | using namespace std;
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   // our headers
# Line 45 | Line 59 | using namespace std;
59   #include "ParseArgs.h"
60   #include "MuonSelection.h"
61   #include "ElectronSelection.h"
62 + #include "ElectronMomentumCorrection.h"
63 + #include "JetSelection.h"
64   #include "IsolationSelection.h"
49
50 //#include "Selection.h"
65   #include "ReferenceSelection.h"
66 + #include "MuonMomentumCorrection.h"
67  
68   #include "TriggerUtils.h"
69   #include "PassHLT.h"
55 #include "Angles.h"
70   #include "KinematicsStruct.h"
71   #include "InfoStruct.h"
72 < //#include "GenInfoStruct.h"
72 > #include "GenInfoStruct.h"
73   #include "WeightStruct.h"
60 #include "GlobalDataAndFuncs.h"
74   #include "AngleTuple.h"
75 + #include "FOTuple.h"
76  
77 + #include "RunLumiRangeMap.h"
78   #include "SampleWeight.h"
79   #include "EfficiencyWeightsInterface.h"
80 + #include "SelectionFuncs.h"
81 +
82 + #include "SimpleLepton.h"
83  
84   #ifndef CMSSW_BASE
85   #define CMSSW_BASE "../"
# Line 69 | Line 87 | using namespace std;
87  
88   using namespace mithep;
89  
90 <
90 > //
91 > // globals
92 > //----------------------------------------------------------------------------
93 > TH1D *hpu_2011, *hpu_2012;
94 > RunLumiRangeMap rlrm;
95 > vector<SimpleLepton> failingLeptons ; // for fake estimation
96 > vector<SimpleLepton> passingLeptons;      // for fake estimation
97 > vector<unsigned> cutvec;
98 > vector<vector<unsigned> > zcutvec;
99 > vector<vector<unsigned> > zzcutvec;
100 > map<unsigned,float>       evtrhoMap;
101 > TrigInfo ti;
102 > vector<string> cutstrs;
103 > bool passes_HLT;
104 > vector<bool>   PFnoPUflag;;
105 > map<TString, map<TString,int>* > counts;
106 > ElectronMomentumCorrection electron_momentum_correction;
107 > MuCorr *muCorr;
108 > void addDummyZ2Lepotons(EventData &ret4l);
109 > void setNtupleVals(ControlFlags &ctrl, EventData &ret4l, InfoStruct &evtinfo, Angles &angles, KinematicsStruct &kine,
110 >                   JetInfoStruct &ji, WeightStruct &weights, EventHeader *info,  Array<Muon> *muonArr, Array<Electron> *electronArr, Array<PFJet> *jetArr, Array<PFMet> *metArr,
111 >                   vector<SimpleLepton> &simpleLeptonJets, FactorizedJetCorrector *fJetCorrector, Array<PileupInfo> *puArr,
112 >                   Array<PileupEnergyDensity> *puDArr, JetIDMVA *fJetIDMVA, Array<Vertex> *vtxArr, bitset<1024> &triggerBits,
113 >                   TriggerTable *hltTable, Array<TriggerObject> *hltObjArr, TriggerObjectsTable *fTrigObjs, AngleTuple *nt);
114   //----------------------------------------------------------------------------
115 < bool setPV(ControlFlags,const mithep::Array<mithep::Vertex>*, mithep::Vertex& );
115 > void initRunLumiRangeMap(ControlFlags &ctrl)
116 > //----------------------------------------------------------------------------
117 > {
118 >  if(ctrl.jsonFile==""){
119 >    cout << "WARNING: running with empty jsonFile, so setting noJSON to true" << endl;
120 >    ctrl.noJSON = true;
121 >  }
122 >  rlrm.AddJSONFile(string(ctrl.jsonFile));  
123 > }
124 >
125 > //
126 > // prototypes
127   //----------------------------------------------------------------------------
128  
129 < //=== MAIN =================================================================================================
129 > //
130 > // MAIN
131 > //----------------------------------------------------------------------------
132   int main(int argc, char** argv)
133 + //----------------------------------------------------------------------------
134   {
135 +  vector<TString> cuts;
136 +  cuts.push_back("start"); // NOTE: is incremented in ReferenceSelection, so if you turn on JSONS be careful
137 +  cuts.push_back("trigger");
138 +  cuts.push_back("sfOsHiPt");
139 +  cuts.push_back("4<mZ2<120");
140 +  cuts.push_back("pt>20,10");
141 +  cuts.push_back("mll>4");
142 +  cuts.push_back("m4l>70");
143 +  cuts.push_back("mZ2>12");
144 +  cuts.push_back("m4l>100");
145 +  cuts.push_back("m4l > 100 && >= 1 jets");
146 +  cuts.push_back("m4l > 100 && 2 jets");
147 +  cuts.push_back("m4l > 100 && fisher > 0.4");
148 +
149 +  for(unsigned icut=0; icut<cuts.size(); icut++)
150 +    counts[cuts[icut]] = new map<TString,int>;
151 +
152 +  map<TString,map<TString,int>* >::iterator cit;
153 +  for(cit=counts.begin(); cit!=counts.end(); cit++) {
154 +    map<TString,int> *cts = (*cit).second;
155 +    (*cts)["all"]  = int(0);
156 +    (*cts)["4e"]   = int(0);
157 +    (*cts)["4m"]   = int(0);
158 +    (*cts)["2e2m"] = int(0);
159 +  }
160 +  
161    string cmsswpath(CMSSW_BASE);
162    cmsswpath.append("/src");
163 <  std::bitset<1024> triggerBits;
163 >  bitset<1024> triggerBits;
164    vector<vector<string> > inputFiles;
165    inputFiles.push_back(vector<string>());
166 <  map<string,double> entrymap; // number of unskimmed entries in each file
166 >  map<string,unsigned> entrymap; // number of unskimmed entries in each file
167  
168    //
169    // args
170    //--------------------------------------------------------------------------------------------------------------
171    ControlFlags ctrl;
172    parse_args( argc, argv, ctrl );
92  if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
93    {
94      cerr << "usage: applySelection.exe <flags> " << endl;
95      cerr << "\tmandoatory flags : " << endl;
96      cerr << "\t--inputfiles |  file containing a list of ntuples to run over" << endl;
97      cerr << "\t--inputfile |  a file to run over" << endl;
98      cerr << "\t--outputfile | file to store  selected evet" << endl;
99      return 1;
100    }
173    ctrl.dump();
174 <  assert( ctrl.mH != 0 );
175 <
104 <
174 >  cout << "\n\nNOTE: now setting era with arguments... double check this doesnt mess something else up\n\n" << endl;
175 >  assert(ctrl.doFSR);
176  
177    //
178    // File I/O
179    //--------------------------------------------------------------------------------------------------------------
180    TChain * chain = new TChain("Events");
181    TChain * hltchain = new TChain("HLT");
182 +  TChain * runchain = new TChain(Names::gkRunTreeName);
183  
184    string fname;
185 +  unsigned total_unskimmed=0;
186    if( !ctrl.inputfiles.empty() ) {
187      ifstream f(ctrl.inputfiles.c_str());
188 <    while (f >> fname) {
188 >    while (f >> fname) {
189        if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
190 +      if(fname.find("/store/") != string::npos) fname = "root://eoscms//"+fname;
191        cout << "adding inputfile : " << fname.c_str() << endl;
192        entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
193 +      cerr << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
194 +      total_unskimmed += entrymap[string(fname.c_str())];
195        chain->AddFile(fname.c_str());
196        hltchain->AddFile(fname.c_str());
197 +      runchain->AddFile(fname.c_str());
198      }
199    } else {
200 +    if(ctrl.inputfile.find("/store/") != string::npos) ctrl.inputfile = "root://eoscms//"+ctrl.inputfile;
201      cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
202 <    entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
202 >    unsigned unsk_ents = unskimmedEntries(ctrl.inputfile.c_str());
203 >    entrymap[string(ctrl.inputfile.c_str())] = unsk_ents;
204 >    cerr << "unskimmed entries: " << unsk_ents << endl;
205 >    total_unskimmed += unsk_ents;
206      chain->AddFile(ctrl.inputfile.c_str());
207      hltchain->AddFile(ctrl.inputfile.c_str());
208 +    runchain->AddFile(ctrl.inputfile.c_str());
209    }
210 +  // write the total number of unskimmed events that went into making this output file to a text file
211 +  writeEntries(ctrl,total_unskimmed);
212 +  ctrl.totalMC = total_unskimmed;
213  
214    const char * ofname;
215    if( strcmp( ctrl.outputfile.c_str(), "") ) {
# Line 136 | Line 221 | int main(int argc, char** argv)
221    string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
222    cout << "xspath: " << xspath.c_str() << endl;
223    SimpleTable xstab(xspath.c_str());
224 <
224 >  if(ctrl.mc) {
225 >    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
226 >  }
227  
228    //
229    // Setup
230    //--------------------------------------------------------------------------------------------------------------
231    Angles angles;
232 <  KinematicsStruct kinematics;
232 >  KinematicsStruct kine;
233    InfoStruct evtinfo;
234    WeightStruct weights;
235 <  //  GenInfoStruct geninfo;
235 >  GenInfoStruct geninfo;
236 >  JetInfoStruct ji;
237 >  vector<SimpleLepton> simpleLeptonJets;
238  
239    AngleTuple nt( (const char*)ofname, (const char*)"zznt");
240    nt.makeAngleBranch(angles);
241 <  nt.makeKinematicsBranch(kinematics);
241 >  nt.makeKinematicsBranch(kine);
242    nt.makeInfoBranch(evtinfo);
243 +  nt.makeJetInfoBranch(ji);
244 +  //nt.makeJetAngleBranches("DataStruct/data/jetAngleVars.txt");
245  
246 <  TH1F * h_w_hpt;
246 >  FOTuple *foTree=0;
247 >  if(ctrl.fakeScheme != "none") {
248 >    foTree = new FOTuple( nt.getFile(), (const char*)"FOtree");
249 >    foTree->makeInfoBranch(evtinfo);
250 >    foTree->makeFailedLeptonBranch(failingLeptons);
251 >    foTree->makePassedLeptonBranch(passingLeptons);
252 >  }
253 >
254 >  int genSampleType(0);
255    if(ctrl.mc) {
256      nt.makeWeightBranch(weights);
257 <    //    nt.makeGenInfoBranch(geninfo);
258 <    initEfficiencyWeights();
259 <    /*
260 <    // Higgs only, pt reweighting
261 <    if( ctrl.mH <= 140 ) {
262 <      char wptname[256];
263 <      sprintf( wptname, "/data/blue/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
165 <      TFile * f_hpt = new TFile(wptname);
166 <      f_hpt->Print();
167 <      sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
168 <      h_w_hpt  = (TH1F*)(f_hpt->FindObjectAny(wptname));
169 <      h_w_hpt->Print();
257 >    if(ctrl.fillGen) {
258 >      genSampleType = getGenSampleType(ofname);
259 >      if(genSampleType == -1) {
260 >        cout << "\ngenSampleType returned as -1, setting fillGen to false" << endl;
261 >        ctrl.fillGen = false;
262 >      } else
263 >        nt.makeGenInfoBranch(geninfo);
264      }
265 <    */
265 >    if(ctrl.use_eff_weights) initEfficiencyWeights();
266    } else {
267 <    initRunLumiRangeMap();
267 >    if(!(ctrl.noJSON) )
268 >      initRunLumiRangeMap(ctrl);
269    }
270  
271 <  //  initMuonIDMVA();
177 <  initMuonIsoMVA();
178 <  initElectronIDMVA();
179 <  initElectronIsoMVA();
271 >  initElectronIDMVAV1();
272    initTrigger();
273 <  
274 <  
275 <  //##########################################################################
276 <  // Setup tree I/O
277 <  //##########################################################################
278 <  
273 >  initAnalysisTriggers(ti);
274 >  initMela(ctrl);
275 >  initMassErrors();
276 >  ti.dump();
277 >
278 >  if(ctrl.do_escale) {
279 >    string regPath("EGamma/EGammaAnalysisTools/data/eleEnergyRegWeights_V1.root");
280 >    // if(TString(getenv("HOSTNAME")).Contains(TRegexp("t3[bd][te][cs][hk]")))
281 >      regPath = cmsswpath + "/" + regPath;
282 >    electron_momentum_correction.initialize_regression(regPath);
283 >    if(ctrl.mc) {
284 >      if(ctrl.era == 2011) ctrl.smearing_scaling_dataset = "Fall11";
285 >      else                 ctrl.smearing_scaling_dataset = "Summer12_DR53X_HCP2012";
286 >    } else {
287 >      if(ctrl.era == 2011) ctrl.smearing_scaling_dataset = "Jan16ReReco";
288 >      else                 ctrl.smearing_scaling_dataset = "2012Jul13ReReco";
289 >    }
290 >  }
291 >  if(ctrl.correct_muon_momentum) {
292 >    muCorr = new MuCorr;
293 >    muCorr->corr2011 = new rochcor;
294 >    muCorr->corr2012 = new rochcor2012;
295 >  }
296 >
297 >  vector<TString> fJetCorrParsv;
298 >  vector<JetCorrectorParameters> correctionParameters;
299 >  FactorizedJetCorrector *fJetCorrector;
300 >  JetIDMVA *fJetIDMVA;
301 >  initJets(ctrl, cmsswpath, fJetCorrParsv, correctionParameters, fJetCorrector, fJetIDMVA);
302 >
303    //
304 <  // Access samples and fill histograms
304 >  // Setup tree I/O
305 >  //--------------------------------------------------------------------------------------------------------------
306    TFile *inputFile=0;
307    TTree *eventTree=0;  
308    string currentFile("");
309  
310 <  // Data structures to store info from TTrees
311 <  mithep::EventHeader *info    = new mithep::EventHeader();
312 <  //  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
313 <  mithep::Array<mithep::Electron>             *electronArr   = new mithep::Array<mithep::Electron>();
314 <  mithep::Array<mithep::Muon>                 *muonArr       = new mithep::Array<mithep::Muon>();
315 <  mithep::Array<mithep::Vertex>               *vtxArr        = new mithep::Array<mithep::Vertex>();
316 <  mithep::Array<mithep::PFCandidate>          *pfArr         = new mithep::Array<mithep::PFCandidate>();
317 <  mithep::Array<mithep::PileupEnergyDensity>  *puArr         = new mithep::Array<mithep::PileupEnergyDensity>();
318 <  mithep::Array<mithep::Track>                *trkArr        = new mithep::Array<mithep::Track>();
319 <  mithep::TriggerMask                         *trigMask      = new mithep::TriggerMask();
320 <  mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
321 <  vector<string>                              *hltTableStrings  = new vector<string>();
322 <
323 <  TString fElectronName(Names::gkElectronBrn);
324 <  TString fMuonName(Names::gkMuonBrn);
325 <  TString fInfoName(Names::gkEvtHeaderBrn);
326 <  TString fPrimVtxName(Names::gkPVBrn);
327 <  TString fPFCandidateName(Names::gkPFCandidatesBrn);
328 <  TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
329 <  TString fTrackName(Names::gkTrackBrn);
330 <  TString fTriggerMaskName(Names::gkHltBitBrn);
214 <  TString fTriggerTableName(Names::gkHltTableBrn);
310 >  UInt_t fNMaxTriggers = TRIGGER_BIG_NUMBER;
311 >  EventHeader                   *info                    = new EventHeader();
312 >  Array<PFMet>                  *metArr                  = new Array<PFMet>();
313 >  Array<Electron>               *electronArr             = new Array<Electron>();
314 >  Array<Muon>                   *muonArr                 = new Array<Muon>();
315 >  Array<Vertex>                 *vtxArr                  = new Array<Vertex>();
316 >  Array<PFCandidate>            *pfArr                   = new Array<PFCandidate>();
317 >  Array<PFJet>                  *jetArr                  = new Array<PFJet>();
318 >  Array<PileupInfo>             *puArr                   = new Array<PileupInfo>();
319 >  Array<PileupEnergyDensity>    *puDArr                  = new Array<PileupEnergyDensity>();
320 >  Array<Track>                  *trkArr                  = new Array<Track>();
321 >  Array<MCParticle>             *mcArr                   = new Array<MCParticle>();
322 >  MCEventInfo                   *mcEvtInfo               = new MCEventInfo();
323 >  TriggerMask                   *trigMask                = new TriggerMask();
324 >  TriggerTable                  *hltTable                = new TriggerTable(fNMaxTriggers);
325 >  vector<string>                *hltTableStrings         = new vector<string>();
326 >  vector<string>                *hltLabelStrings         = new vector<string>();
327 >  Array<TriggerObject>          *hltObjArr               = new Array<TriggerObject>();
328 >  Array<TriggerObjectRel>       *hltRelsArr              = new Array<TriggerObjectRel>();
329 >  TriggerObjectsTable           *fTrigObjs               = new TriggerObjectsTable(hltTable,fNMaxTriggers);
330 >  RunInfo                       *runInfo                 = new RunInfo();
331    
332 <  chain->SetBranchAddress(fInfoName,        &info);
333 <  chain->SetBranchAddress(fElectronName,    &electronArr);
334 <  chain->SetBranchAddress(fMuonName,        &muonArr);
335 <  chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
336 <  chain->SetBranchAddress(fPFCandidateName, &pfArr);
337 <  chain->SetBranchAddress(fPileupEnergyDensityName, &puArr);
338 <  chain->SetBranchAddress(fTrackName, &trkArr);
339 <  chain->SetBranchAddress(fTriggerMaskName, &trigMask);
340 <  cout << "hlttable: " << fTriggerTableName << endl;
341 <  hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
342 <
343 <  mithep::Vertex              vtx;          // best primary vertex in the event
344 <
345 <  //  ginfo = NULL;
346 <  // if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
347 <
348 <  int count=0, pass=0;
349 <  float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
350 <  float denom[3]={0.,0.,0.};
351 <  float numer[3]={0.,0.,0.};
352 <  float numercorr[3]={0.,0.,0.};
332 >  chain->SetBranchAddress(Names::gkEvtHeaderBrn,                &info);
333 >  chain->SetBranchAddress("PFMet",                              &metArr);
334 >  chain->SetBranchAddress(Names::gkElectronBrn,                 &electronArr);
335 >  chain->SetBranchAddress(Names::gkMuonBrn,                     &muonArr);
336 >  chain->SetBranchAddress(Names::gkPVBrn,                       &vtxArr);
337 >  chain->SetBranchAddress(Names::gkPFCandidatesBrn,             &pfArr);
338 >  chain->SetBranchAddress(Names::gkPFJetBrn,                    &jetArr);
339 >  if( ctrl.mc ) {
340 >    chain->SetBranchAddress(Names::gkPileupInfoBrn,             &puArr);
341 >    chain->SetBranchAddress(Names::gkMCPartBrn,                 &mcArr);
342 >    chain->SetBranchAddress(Names::gkMCEvtInfoBrn,              &mcEvtInfo);
343 >  }
344 >  chain->SetBranchAddress(Names::gkPileupEnergyDensityBrn,      &puDArr);
345 >  chain->SetBranchAddress(Names::gkTrackBrn,                    &trkArr);
346 >  chain->SetBranchAddress(Names::gkHltBitBrn,                   &trigMask);
347 >  chain->SetBranchAddress(Names::gkHltObjBrn,                   &hltObjArr);
348 >  chain->SetBranchAddress("HLTObjectsRelation",                 &hltRelsArr);
349 >  hltchain->SetBranchAddress(Names::gkHltTableBrn,              &hltTableStrings);
350 >  hltchain->SetBranchAddress(Names::gkHltLabelBrn,              &hltLabelStrings);
351 >
352 >  runchain->SetBranchAddress(Names::gkRunInfoBrn, &runInfo);
353 >
354 >  Vertex              vtx;          // best primary vertex in the event
355  
238  // only 1 HLT table / file ???
356    hltchain->GetEntry(0);
240  cerr << "here... " << endl;
357  
358 <  int imax = chain->GetEntries();
359 <  cout << "nEntries: " << imax << endl;
358 >  int imax = (ctrl.n_events_to_process < 0) ? chain->GetEntries() : ctrl.n_events_to_process;
359 >  cerr << "processing: " << imax << " ( / " << chain->GetEntries() << " )" << endl;
360  
361 +  if(ctrl.mc && ctrl.debugOffMc) // necessary for batch mode -- we need debug output for, data but size is too large to store MC output
362 +    ctrl.debug = false;
363  
364 <  //##########################################################################
365 <  // Loop !!!!!!!!! should alternate events here and +1 in the training ...
366 <  //##########################################################################
367 <  for(UInt_t ientry=0; ientry<imax; ientry++)
368 <    {
369 <      chain->GetEntry(ientry);
370 <      if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
371 <
372 <      setPV( ctrl, vtxArr, vtx);
373 <
374 <      if(ctrl.mc) {
375 <        weights.w = getWeight(xstab,entrymap,chain);
376 <      } else {
377 <        if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
378 <          currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
379 <          hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
380 <          hltchain->GetEntry(0);
381 <          hltTable->Clear();
382 <          fillTriggerBits(hltTable, hltTableStrings );
383 <        }
384 <        if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
385 <        fillTriggerBits( hltTable, trigMask, triggerBits );
386 <      }
387 <      
364 >  int lastHltEntry=-2;
365 >  for(UInt_t ientry=0; ientry<imax; ientry++) {
366 >    chain->GetEntry(ientry);
367 >    if(!(ientry%1000)) cerr << "entry: " << ientry << " ( / " << imax << " )" << endl;
368 >
369 >    increment(counts,"start");
370 >    if( ctrl.debug ) {
371 >      cout << "-----------------------------------------------------------------" << endl;
372 >      cout << "-----------------------------------------------------------------" << endl;
373 >      cout << "Run: " << info->RunNum()
374 >           << "\tEvt: " << info->EvtNum()
375 >           << "\tLumi: " << info->LumiSec()
376 >           << endl;
377 >      cerr << "Run: " << info->RunNum()
378 >           << "\tEvt: " << info->EvtNum()
379 >           << "\tLumi: " << info->LumiSec()
380 >           << endl;
381 >      cout << "-----------------------------------------------------------------" << endl;
382 >    }
383 >
384 >    runchain->GetEvent(info->RunEntry());
385 >    hltchain->GetEntry(runInfo->HltEntry());
386 >    hltTable->Clear();
387 >    fillTriggerNames(hltTable, hltTableStrings );
388 >    // if(ctrl.debug && (lastHltEntry != runInfo->HltEntry())) hltTable->Print();
389 >    lastHltEntry = runInfo->HltEntry();
390 >    fillTriggerBits( hltTable, trigMask, triggerBits );
391 >    setHLTObjectRelations( hltObjArr, hltRelsArr, hltTableStrings, hltLabelStrings, fTrigObjs );
392 >    // printTriggerObjs( hltTable, fTrigObjs);
393        
394 <      //
395 <      // trigger
396 <      //
397 <      if( !ctrl.mc ) {
398 <        if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
399 <          if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
394 >    string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
395 >    if(ctrl.debug) cout << "era is " << ctrl.era << endl;
396 >    if( ctrl.era == 0  ) {
397 >      setEra( fname, ctrl );
398 >      if(ctrl.debug) cout << "era is now " << ctrl.era << endl;
399 >    }
400 >
401 >    // pfNoPU
402 >    PFnoPUflag.clear();
403 >    int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
404 >    assert(pfnopu_size == pfArr->GetEntries());
405 >
406 >    // trigger
407 >    if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
408 >      currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
409 >    }
410 >    if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
411 >    passes_HLT = passHLT(ctrl, triggerBits, ti, info->RunNum(), hltTable, fTrigObjs); // NOTE: run value is not used if it's MC
412 >
413 >    // data/MC
414 >    if(ctrl.mc) {
415 >      if(ctrl.fillGen) fillGenInfo( mcArr, mcEvtInfo, geninfo, genSampleType, ctrl);
416 >      // NOTE: xs weight and npuw are reset in merge.cc
417 >      if(ctrl.use_xs_weights)
418 >        weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
419 >      else
420 >        weights.w = 1;
421 >      weights.npu = getNPU(puArr, 0);
422 >      weights.npuw = getPUWeight(ctrl.era, fname, weights.npu);
423 >    } else {
424 >      // JSON
425 >      if(!(ctrl.noJSON) ) {
426 >        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
427 >        if( !(rlrm.HasRunLumi(rl)) )  {
428 >          if( ctrl.debug ) cout << "fails JSON" << endl;
429            continue;
430          }
431        }
432 <      
281 <      //
282 <      // lepton/kinematic selection ...
283 <      //
284 <      EventData ret4l =
285 <        apply_HZZ4L_reference_selection(ctrl, info,
286 <                              vtx,
287 <                              pfArr,
288 <                              puArr,
289 <                              electronArr,
290 <                              &electronReferencePreSelection,
291 <                              &electronReferenceIDMVASelection,
292 <                              &electronReferenceIsoSelection,
293 <                              muonArr,
294 <                              &muonReferencePreSelection,
295 <                              &muonIDPFSelection,
296 <                              &muonReferenceIsoSelection);
297 <      if( ctrl.debug ) cout << endl;
432 >    }
433  
434 <      cout << "bits: " << ret4l.status.selectionBits << endl;
434 >    //
435 >    // lepton/kinematic selection ...
436 >    //
437 >    failingLeptons.clear();
438 >    passingLeptons.clear();
439 >    EventData ret4l = apply_HZZ4L_reference_selection(ctrl, info,
440 >                                                      vtxArr,
441 >                                                      pfArr,
442 >                                                      puDArr,
443 >                                                      electronArr,
444 >                                                      &electronReferencePreSelection,
445 >                                                      &electronReferenceIDMVASelectionV1, // not used anymore! (Blame the damn regression implementation, not me!)
446 >                                                      &electronReferenceIsoSelection,
447 >                                                      muonArr,
448 >                                                      &muonReferencePreSelection,
449 >                                                      &muonIDPFSelection,
450 >                                                      &muonReferenceIsoSelection);
451        
452 <      if( ret4l.status.pass() ) {
453 <        
454 <        fillAngles(ret4l,angles);
455 <        fillKinematics(ret4l,kinematics);
456 <        fillEventInfo(info,evtinfo);
306 <        //if(ctrl.mc) fillGenInfo(ginfo,geninfo);
307 <        
308 <        /*
309 <        // only for Higgs < 140
310 <        geninfo.weight *= h_w_hpt->GetBinContent(h_w_hpt->FindBin(geninfo.pt_zz));
311 <        angles.bdt = reader->EvaluateMVA("BDTG");
312 <        */
452 >    if( ctrl.debug ) cout << endl;
453 >
454 >    if(ctrl.fakeScheme == "none") { // fill angletuple if event passes full selection
455 >      if( ret4l.status.pass() ) {
456 >        setNtupleVals(ctrl, ret4l, evtinfo, angles, kine, ji, weights, info, muonArr, electronArr, jetArr, metArr, simpleLeptonJets, fJetCorrector, puArr, puDArr, fJetIDMVA, vtxArr, triggerBits, hltTable, hltObjArr, fTrigObjs, &nt);
457          nt.Fill();
458 <        
459 <        cerr  << "PASS:: "
460 <              << "\trun: " << evtinfo.run
461 <              << "\tevt: " << evtinfo.evt
462 <              << "\tlumi: " << evtinfo.lumi
319 <              << "\tcostheta1: " << angles.costheta1
320 <              << "\tcostheta2: " << angles.costheta2
321 <              << "\tcostheta*: " << angles.costhetastar
322 <              << "\tbdt: " << angles.bdt
323 <              << endl;
324 <        pass++;
325 <        //      if( pass > 3 ) break;
458 >    
459 >        int theZ1type = kine.l1type;
460 >        int theZ2type = kine.l3type;
461 >
462 >        if(ctrl.debug) cout << "simpleLeptonJets.size() = " << simpleLeptonJets.size() << endl;
463  
464 <      }
465 <    }  
464 >        if(kine.m4l > 100){
465 >          if(simpleLeptonJets.size() >= 1) {
466 >            increment(counts,"m4l > 100 && >= 1 jets",theZ1type,theZ2type);
467 >            if(simpleLeptonJets.size() == 2) {
468 >              increment(counts,"m4l > 100 && 2 jets",theZ1type,theZ2type);
469 >              SimpleLepton j1 = simpleLeptonJets[0];
470 >              SimpleLepton j2 = simpleLeptonJets[1];
471 >              double deta = abs(j1.vec.Eta()-j2.vec.Eta());
472 >              double mjj = (j1.vec + j2.vec).M();
473 >              double fisher = 0.09407 * deta  +0.00041581*mjj;
474 >              if(fisher > 0.4) increment(counts, "m4l > 100 && fisher > 0.4",theZ1type,theZ2type);
475 >            }
476 >          }
477 >        }
478 >
479 >        
480 >      } else {
481 >        if(ctrl.debug) cout << "failing with code : " << ret4l.status.selectionBits << endl;
482 >      }
483 >    } else { // fill FOtuple and angletuple if event has a good Z and one or two extra leptons
484 >      assert(ctrl.fakeScheme=="ZPlusF" || ctrl.fakeScheme=="ZPlusFF");
485 >      if(ret4l.status.selectionBits.test(PASS_GOODZ1) ) {
486 >        unsigned nleps = passingLeptons.size() + failingLeptons.size();
487 >        if( (nleps>=3 && ctrl.fakeScheme=="ZPlusF" ) || (nleps>=4 && ctrl.fakeScheme=="ZPlusFF" ) ) {
488 >          addDummyZ2Lepotons(ret4l);
489 >          setNtupleVals(ctrl, ret4l, evtinfo, angles, kine, ji, weights, info, muonArr, electronArr, jetArr,  metArr, simpleLeptonJets, fJetCorrector, puArr, puDArr, fJetIDMVA, vtxArr, triggerBits, hltTable, hltObjArr, fTrigObjs, &nt);
490 >          nt.Fill();
491 >          foTree->Fill();
492 >        }
493 >      } else {
494 >        if(ctrl.debug) cout << "failing PASS_GOODZ1" << endl;
495 >      }
496 >    }
497 >  }
498  
499 +  if(ctrl.fakeScheme != "none") {
500 +    foTree->getFile()->cd();
501 +    foTree->getTree()->Write();
502 +  }
503    nt.WriteClose();
331 }
504  
505 < //----------------------------------------------------------------------------
506 < //
507 < // Get primary vertices
508 < // Assumes primary vertices are ordered by sum-pT^2 (as should be in CMSSW)
509 < // NOTE: if no PV is found from fitting tracks, the beamspot is used
510 < //
511 < //----------------------------------------------------------------------------
512 < bool setPV(ControlFlags ctrl,
513 <           const mithep::Array<mithep::Vertex> * vtxArr,
514 <           mithep::Vertex & vtx)
515 < //----------------------------------------------------------------------------
516 < {
517 <  const Vertex *bestPV = 0;
518 <  float best_sumpt=-1;
519 <
520 <  // good PV requirements
521 <  const UInt_t   fMinNTracksFit = 0;
522 <  const Double_t fMinNdof       = 4;
523 <  const Double_t fMaxAbsZ       = 24;
524 <  const Double_t fMaxRho        = 2;
525 <  
526 <  for(int i=0; i<vtxArr->GetEntries(); ++i) {
527 <    const Vertex *pv = (Vertex*)(vtxArr->At(i));
528 <    if( ctrl.debug ) cout << "vertex :: " << i << "\tntrks: " << pv->NTracks() << endl;
529 <    
530 <    // Select best PV for corrected d0; if no PV passing cuts, the first PV in the collection will be used
531 <    //  if(!pv->IsValid()) continue;
532 <    if(pv->NTracksFit()       < fMinNTracksFit)       continue;
533 <    if(pv->Ndof()                 < fMinNdof)         continue;
534 <    if(fabs(pv->Z()) > fMaxAbsZ)                      continue;
535 <    if(pv->Position().Rho()   > fMaxRho)              continue;    
536 <    
537 <    // take the first ...
538 <    bestPV = pv;
539 <    break;
540 <
541 <    // this never reached ...    
542 <    float tmp_sumpt=0;
543 <    for( int t=0; t<pv->NTracks(); t++ )
544 <      tmp_sumpt += pv->Trk(t)->Pt();
545 <    
546 <    if( tmp_sumpt > best_sumpt  ) {
547 <      bestPV = pv;
548 <      best_sumpt = tmp_sumpt;
549 <      if( ctrl.debug) cout << "new PV set, pt : " << best_sumpt << endl;
505 >  for(unsigned icut=0; icut<cuts.size(); icut++) {
506 >    map<TString,int> thisCount(*(counts[cuts[icut]]));
507 >    cerr << setw(25) << cuts[icut] << "\t" << setw(9) << thisCount["all"] << "  ("
508 >         << setw(9) << thisCount["4e"] << setw(9) << thisCount["4m"] << setw(9) << thisCount["2e2m"] << ")" << endl;
509 >  }
510 > }
511 > //----------------------------------------------------------------------------------------
512 > void addDummyZ2Lepotons(EventData &ret4l)
513 > // for fake ntuples, we don't know which are the Z2 leptons yet, but need to fill something so we don't seg fault
514 > {
515 >  SimpleLepton dum1,dum2;
516 >  dum1.vec.SetPtEtaPhiM(1,2,3,4);
517 >  dum2.vec.SetPtEtaPhiM(5,6,7,8);
518 >  ret4l.Z2leptons.push_back(dum1);
519 >  ret4l.Z2leptons.push_back(dum2);
520 > }
521 > //----------------------------------------------------------------------------------------
522 > void setNtupleVals(ControlFlags &ctrl, EventData &ret4l, InfoStruct &evtinfo, Angles &angles, KinematicsStruct &kine,
523 >                   JetInfoStruct &ji, WeightStruct &weights, EventHeader *info, Array<Muon> *muonArr, Array<Electron> *electronArr, Array<PFJet> *jetArr, Array<PFMet> *metArr,
524 >                   vector<SimpleLepton> &simpleLeptonJets, FactorizedJetCorrector *fJetCorrector, Array<PileupInfo> *puArr,
525 >                   Array<PileupEnergyDensity> *puDArr, JetIDMVA *fJetIDMVA, Array<Vertex> *vtxArr, bitset<1024> &triggerBits,
526 >                   TriggerTable *hltTable, Array<TriggerObject> *hltObjArr, TriggerObjectsTable *fTrigObjs, AngleTuple *nt)
527 > {
528 >  fillKinematics(ret4l,kine);
529 >
530 >  //the last two arguments are usePt and useY respectively
531 >  fillMela(kine,0,0);
532 >
533 >  fillMassErrors(ctrl,ret4l,muonArr,electronArr,kine,false);
534 >
535 >  TLorentzVector pfmet; pfmet.SetPxPyPzE(metArr->At(0)->Mex(),metArr->At(0)->Mey(),0,0);
536 >  fillEventInfo( info, pfmet, evtinfo, ctrl.mc ? getNPU(puArr) : 0, vtxArr->GetEntries());
537 >  fillJets(ctrl, jetArr, simpleLeptonJets, fJetCorrector, puDArr, fJetIDMVA, vtxArr, ret4l);
538 >  fillJetInfo(simpleLeptonJets, ji, ctrl);
539 >  //fillJetAngleBranches(ji, ret4l, nt, ctrl);
540 >  evtinfo.status = 0;
541 >  evtinfo.status = setHltBits(ti, triggerBits);
542 >
543 >  if(ctrl.fakeScheme == "none") {
544 >    fillAngles(ret4l,angles);
545 >
546 >    if( ctrl.mc) {
547 >      if(ctrl.use_eff_weights) setEfficiencyWeights(ctrl, ctrl.era, ret4l, triggerBits, hltTable, hltObjArr, fTrigObjs, weights);
548 >      if(ctrl.debug)
549 >        cout << "w: " << weights.w << "\t" << "npuw: " << weights.npuw << "\t" << "won: " << weights.won << "\t" << "woff: " << weights.woff << endl;
550 >    }
551 >
552 >    if(ctrl.debug) {
553 >      stringstream ss;
554 >      ss << "PASS:: " << "\trun: " << evtinfo.run << "\tevt: " << evtinfo.evt << "\tlumi: " << evtinfo.lumi << "\tchannel: "
555 >         << kine.channel << "\tm4l: " << kine.m4l << "\tmZ1: " << kine.mZ1 << "\tmZ2: " << kine.mZ2 << endl;
556 >      cerr << ss.str();
557 >      cout << ss.str();
558      }
559 +  } else {
560 +    // 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
561    }
562 <  
381 <  if(!bestPV) bestPV = vtxArr->At(0);
382 <  vtx.SetPosition(bestPV->X(),bestPV->Y(),bestPV->Z());
383 <  vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
384 <  return true;
385 < };
562 > }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines