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.5 by dkralph, Wed Oct 12 17:25:15 2011 UTC vs.
Revision 1.35 by khahn, Tue Jun 5 19:35:48 2012 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>                 //
20 < #include <TBranch.h>                // class to access branches in TTree
21 < #include <TClonesArray.h>           // ROOT array class
22 < #include <TCanvas.h>                // class for drawing
23 < #include <TH1F.h>                   // 1D histograms
24 < #include <TBenchmark.h>             // class to track macro running statistics
25 < #include <TLorentzVector.h>         // 4-vector class
26 < #include <TVector3.h>               // 3D vector class
14 > #include <TFile.h>                  
15 > #include <TTree.h>                  
16 > #include <TChain.h>                
17 > #include <TBranch.h>                
18 > #include <TClonesArray.h>          
19 >
20 >
21 > //
22 > // TMVA
23 > //
24 > #include "TMVA/Reader.h"
25 > #include "TMVA/Tools.h"
26 > #include "TMVA/Config.h"
27 > #include "TMVA/MethodBDT.h"
28  
29   //
30   // ntuple format headers
31   //
32 < #include "HiggsAnaDefs.hh"
33 < #include "TEventInfo.hh"
34 < #include "TElectron.hh"
35 < #include "TMuon.hh"
36 < #include "TJet.hh"
37 < #include "RunLumiRangeMap.h"
32 > #include "EventHeader.h"
33 > #include "Electron.h"
34 > #include "Muon.h"
35 > #include "Vertex.h"
36 > #include "PFCandidate.h"
37 > #include "PFCandidateCol.h"
38 > #include "PileupInfoCol.h"
39 > #include "PileupEnergyDensity.h"
40 > #include "MCParticle.h"
41 > #include "TriggerMask.h"
42 > #include "TriggerTable.h"
43 > #include "Names.h"
44 > #include "BaseMod.h"
45  
46   //
47 < // utility headers
47 > // our headers
48   //
49   #include "ParseArgs.h"
50 + #include "MuonSelection.h"
51 + #include "ElectronSelection.h"
52 + #include "IsolationSelection.h"
53 + #include "ReferenceSelection.h"
54 +
55 + #include "TriggerUtils.h"
56 + #include "PassHLT.h"
57 + #include "Angles.h"
58 + #include "KinematicsStruct.h"
59 + #include "InfoStruct.h"
60 + #include "GenInfoStruct.h"
61 + #include "WeightStruct.h"
62 + #include "AngleTuple.h"
63 + #include "FOTuple.h"
64 +
65 + #include "RunLumiRangeMap.h"
66   #include "SampleWeight.h"
67 < #include "Selection.h"
68 < #include "HZZCiCElectronSelection.h"
69 < #include "HZZLikelihoodElectronSelection.h"
70 < #include "HZZBDTElectronSelection.h"
71 < #include "SampleWeight.h"
67 > #include "EfficiencyWeightsInterface.h"
68 >
69 > #include "SimpleLepton.h"
70 >
71 > #ifndef CMSSW_BASE
72 > #define CMSSW_BASE "../"
73 > #endif
74 >
75 > using namespace mithep;
76 >
77 > //
78 > // globals
79 > //----------------------------------------------------------------------------
80 > TH1D * hpu;
81 > RunLumiRangeMap rlrm;
82 > vector<SimpleLepton> failingLeptons ; // for fake estimation
83 > vector<SimpleLepton> passingLeptons;      // for fake estimation
84 > vector<unsigned> cutvec;
85 > vector<vector<unsigned> > zcutvec;
86 > vector<vector<unsigned> > zzcutvec;
87 > map<unsigned,float>       evtrhoMap;
88 > vector<string> cutstrs;
89 > bool passes_HLT_MC;
90 > vector<bool>   PFnoPUflag;;
91 >
92 > //
93 > // prototypes
94 > //----------------------------------------------------------------------------
95 > void initPUWeights();
96 > double getPUWeight(unsigned npu);
97 > void setEra(string, ControlFlags&);
98 > void setEffiencyWeights(EventData&, WeightStruct& );
99 > void initRunLumiRangeMap();
100 > void initEvtRhoMap(map<unsigned,float> &);
101 > unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
102 >                         vector<bool> &pfNoPileUpFlag,
103 >                         const mithep::Array<mithep::Vertex>     * vtxArr );
104 > //----------------------------------------------------------------------------
105  
53 //#define THEIR_EVENTS
106  
107 < //=== MAIN =================================================================================================
107 > //
108 > // MAIN
109 > //----------------------------------------------------------------------------
110   int main(int argc, char** argv)
111 + //----------------------------------------------------------------------------
112   {
113 <  
113 >  cutstrs.push_back(string("skim0"));              //0
114 >  cutstrs.push_back(string("skim1"));              //1
115 >  cutstrs.push_back(string("skim2"));              //1
116 >  cutstrs.push_back(string("trigger"));            //2
117 >  // -------------------------------------------------
118 >  cutstrs.push_back(string("Z candidate"));        //3
119 >  cutstrs.push_back(string("good Z1"));            //4
120 >  // -------------------------------------------------
121 >  cutstrs.push_back(string("4l"));                 //5  
122 >  cutstrs.push_back(string("ZZ candidate"));       //6
123 >  cutstrs.push_back(string("good Z2"));            //7
124 >  cutstrs.push_back(string("ZZ 20/10"));           //8
125 >  cutstrs.push_back(string("resonance"));          //9
126 >  cutstrs.push_back(string("m4l>70"));             //10
127 >  cutstrs.push_back(string("m4l>100, mZ2 > 12"));                  //11
128 >
129 >
130 >
131 >
132 >  string cmsswpath(CMSSW_BASE);
133 >  cmsswpath.append("/src");
134 >  std::bitset<1024> triggerBits;
135    vector<vector<string> > inputFiles;
136    inputFiles.push_back(vector<string>());
137 +  map<string,unsigned> entrymap; // number of unskimmed entries in each file
138 +  for( int i=0; i<cutstrs.size(); i++ ) cutvec.push_back(0);
139 +  for( int i=0; i<2; i++ )  {
140 +    zcutvec.push_back(vector<unsigned>());
141 +    for( int j=0; j<cutstrs.size(); j++ ) zcutvec[i].push_back(0);
142 +  }
143 +  for( int i=0; i<3; i++ )  {
144 +    zzcutvec.push_back(vector<unsigned>());
145 +    for( int j=0; j<cutstrs.size(); j++ ) zzcutvec[i].push_back(0);
146 +  }
147 +
148  
149    //
150    // args
151    //--------------------------------------------------------------------------------------------------------------
152    ControlFlags ctrl;
153    parse_args( argc, argv, ctrl );
154 <  if( ctrl.inputfile.empty() ||
155 <      ctrl.inputfile.empty() ) {
156 <    cerr << "usage: applySelection.exe <flags> " << endl;
157 <    cerr << "\tmandoatory flags : " << endl;
158 <    cerr << "\t--inputfile |  file containing a list of ntuples to run over" << endl;
159 <    cerr << "\t--outputfile | file to store  selected evet" << endl;
160 <    return 1;
161 <  }
154 >  if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
155 >    {
156 >      cerr << "usage: applySelection.exe <flags> " << endl;
157 >      cerr << "\tmandoatory flags : " << endl;
158 >      cerr << "\t--inputfiles |  file containing a list of ntuples to run over" << endl;
159 >      cerr << "\t--inputfile |  a file to run over" << endl;
160 >      cerr << "\t--outputfile | file to store  selected evet" << endl;
161 >      return 1;
162 >    }
163    ctrl.dump();
164  
165 <  map<string,double> entrymap; // number of unskimmed entries in each file
165 >
166  
167    //
168    // File I/O
169    //--------------------------------------------------------------------------------------------------------------
170    TChain * chain = new TChain("Events");
171 <  ifstream f(ctrl.inputfile.c_str());
171 >  TChain * hltchain = new TChain("HLT");
172 >
173    string fname;
174 <  while (f >> fname) {
175 <    if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
176 <    cout << "adding inputfile : " << fname.c_str() << endl;
177 <    entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
178 <    chain->AddFile(fname.c_str());
174 >  if( !ctrl.inputfiles.empty() ) {
175 >    ifstream f(ctrl.inputfiles.c_str());
176 >    while (f >> fname) {
177 >      if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
178 >      cout << "adding inputfile : " << fname.c_str() << endl;
179 >      entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
180 >      cout << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
181 >      chain->AddFile(fname.c_str());
182 >      hltchain->AddFile(fname.c_str());
183 >    }
184 >  } else {
185 >    cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
186 >    unsigned tmpent = unskimmedEntries(ctrl.inputfile.c_str());
187 >    cout << "tmpent: " << tmpent << endl;
188 >    entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
189 >    cout << "unskimmed entries: " << entrymap[string(ctrl.inputfile.c_str())] << endl;
190 >    chain->AddFile(ctrl.inputfile.c_str());
191 >    hltchain->AddFile(ctrl.inputfile.c_str());
192    }
193  
194 +  const char * ofname;
195 +  if( strcmp( ctrl.outputfile.c_str(), "") ) {
196 +    ofname = ctrl.outputfile.c_str();
197 +  } else {
198 +    ofname = "tmp.root";
199 +  }
200    // table of cross section values  
201 <  SimpleTable xstab("./data/xs.dat");
201 >  string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
202 >  cout << "xspath: " << xspath.c_str() << endl;
203 >  SimpleTable xstab(xspath.c_str());
204 >
205  
206    //
207    // Setup
208    //--------------------------------------------------------------------------------------------------------------
209 <  TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
210 <  TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 1024, 0, 1024 );
211 <  TNtuple * passtuple = new TNtuple( "passtuple", "passtuple", "run:evt:lumi:channel:mZ1:mZ2:m4l:pt4l:w" );
212 <  initCiCSelection();
213 <  if(ctrl.eleSele=="lik") initLikSelection();
214 <  if(ctrl.eleSele=="bdt") initBDTSelection();
215 <  initRunLumiRangeMap();
209 >  Angles angles;
210 >  KinematicsStruct kinematics;
211 >  InfoStruct evtinfo;
212 >  WeightStruct weights;
213 >  GenInfoStruct geninfo;
214 >
215 >  AngleTuple nt( (const char*)ofname, (const char*)"zznt");
216 >  nt.makeAngleBranch(angles);
217 >  nt.makeKinematicsBranch(kinematics);
218 >  nt.makeInfoBranch(evtinfo);
219 >
220 >  
221 >  FOTuple foTree( nt.getFile(), (const char*)"FOtree");
222 >  foTree.makeFailedLeptonBranch(failingLeptons);
223 >  foTree.makePassedLeptonBranch(passingLeptons);
224 >
225 >  TH1F * h_wf11_hpt;
226 >  if(ctrl.mc) {
227 >    nt.makeWeightBranch(weights);
228 >    if(ctrl.fillGen ) nt.makeGenInfoBranch(geninfo);
229 >    initEfficiencyWeights();
230 >    initPUWeights();
231 >
232 >    // Higgs only, pt reweighting
233 >    if( ctrl.mH <= 140 ) {
234 >      char wptname[256];
235 >      sprintf( wptname, "/data/blue/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
236 >      TFile * f_hpt = new TFile(wptname);
237 >      f_hpt->Print();
238 >      sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
239 >      h_wf11_hpt  = (TH1F*)(f_hpt->FindObjectAny(wptname));
240 >    }
241 >
242 >  } else {
243 >    initRunLumiRangeMap();
244 >  }
245 >
246 >  //  initMuonIDMVA();
247 >  initElectronIDMVAV1();
248 >  initTrigger();
249 >
250 >  // hack
251 >  initEvtRhoMap(evtrhoMap);
252  
106  //
107  // Loop
108  //--------------------------------------------------------------------------------------------------------------
253    
254    //
255 <  // Access samples and fill histograms
255 >  // Setup tree I/O
256 >  //--------------------------------------------------------------------------------------------------------------
257    TFile *inputFile=0;
258    TTree *eventTree=0;  
259 <  
115 <  // Data structures to store info from TTrees
116 <  mithep::TEventInfo *info    = new mithep::TEventInfo();
117 <  TClonesArray *electronArr   = new TClonesArray("mithep::TElectron");
118 <  TClonesArray *muonArr       = new TClonesArray("mithep::TMuon");
119 <  
120 <
121 <  TBranch *infoBr;
122 <  TBranch *electronBr;
123 <  TBranch *muonBr;
124 <
125 < //   chain->SetBranchStatus("*",0); //disable all branches
126 < //   chain->SetBranchStatus("Info", 1);
127 < //   chain->SetBranchStatus("Muon", 1);
128 < //   chain->SetBranchStatus("Electron", 1);
129 <
130 <  chain->SetBranchAddress("Info",       &info);
131 <  chain->SetBranchAddress("Electron", &electronArr);
132 <  chain->SetBranchAddress("Muon", &muonArr);
259 >  string currentFile("");
260  
261 +  mithep::EventHeader *info    = new mithep::EventHeader();
262 +  mithep::Array<mithep::Electron>             *electronArr   = new mithep::Array<mithep::Electron>();
263 +  mithep::Array<mithep::Muon>                 *muonArr       = new mithep::Array<mithep::Muon>();
264 +  mithep::Array<mithep::Vertex>               *vtxArr        = new mithep::Array<mithep::Vertex>();
265 +  mithep::Array<mithep::PFCandidate>          *pfArr         = new mithep::Array<mithep::PFCandidate>();
266 +  mithep::Array<mithep::PileupInfo>           *puArr         = new mithep::Array<mithep::PileupInfo>();
267 +  mithep::Array<mithep::PileupEnergyDensity>  *puDArr        = new mithep::Array<mithep::PileupEnergyDensity>();
268 +  mithep::Array<mithep::Track>                *trkArr        = new mithep::Array<mithep::Track>();
269 +  mithep::Array<mithep::MCParticle>           *mcArr         = new mithep::Array<mithep::MCParticle>();
270 +  mithep::MCEventInfo                         *mcEvtInfo     = new mithep::MCEventInfo();
271 +  mithep::TriggerMask                         *trigMask      = new mithep::TriggerMask();
272 +  mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
273 +  vector<string>                              *hltTableStrings  = new vector<string>();
274 +
275 +  TString fElectronName(Names::gkElectronBrn);
276 +  TString fMuonName(Names::gkMuonBrn);
277 +  TString fInfoName(Names::gkEvtHeaderBrn);
278 +  TString fPrimVtxName(Names::gkPVBrn);
279 +  TString fPFCandidateName(Names::gkPFCandidatesBrn);
280 +  TString fPileupInfoName(Names::gkPileupInfoBrn);
281 +  TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
282 +  TString fTrackName(Names::gkTrackBrn);
283 +  TString fMCParticleName(Names::gkMCPartBrn);
284 +  TString fMCEvtInfoName(Names::gkMCEvtInfoBrn);
285 +  TString fTriggerMaskName(Names::gkHltBitBrn);
286 +  TString fTriggerTableName(Names::gkHltTableBrn);
287 +  
288 +  chain->SetBranchAddress(fInfoName,        &info);
289 +  chain->SetBranchAddress(fElectronName,    &electronArr);
290 +  chain->SetBranchAddress(fMuonName,        &muonArr);
291 +  chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
292 +  chain->SetBranchAddress(fPFCandidateName, &pfArr);
293 +  if( ctrl.mc ) {
294 +    chain->SetBranchAddress(fPileupInfoName,  &puArr);
295 +    chain->SetBranchAddress(fMCParticleName,  &mcArr);
296 +    chain->SetBranchAddress(fMCEvtInfoName,  &mcEvtInfo);
297 +  }
298 +  chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
299 +  chain->SetBranchAddress(fTrackName, &trkArr);
300 +  chain->SetBranchAddress(fTriggerMaskName, &trigMask);
301 +  cout << "hlttable: " << fTriggerTableName << endl;
302 +  hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
303 +
304 +  mithep::Vertex              vtx;          // best primary vertex in the event
305 +
306 +  //  ginfo = NULL;
307 +  // if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
308 +
309 +  int count=0, pass=0;
310 +  float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
311 +  float denom[3]={0.,0.,0.};
312 +  float numer[3]={0.,0.,0.};
313 +  float numercorr[3]={0.,0.,0.};
314 +
315 +  // only 1 HLT table / file ???
316 +  hltchain->GetEntry(0);
317 +  cerr << "here... " << endl;
318  
319 +  int imax = chain->GetEntries();
320 +  cout << "nEntries: " << imax << endl;
321  
136  cout << "nEntries: " << chain->GetEntries() << endl;
137  for(UInt_t ientry=0; ientry<chain->GetEntries(); ientry++) {
138
139    chain->GetEntry(ientry);
140    if(!(ientry%100000)) cout << "entry: " << ientry << endl;
141
142    // get event weight for this file                                                    
143    double eventweight=1;
144    if(ctrl.mc) eventweight = getWeight(xstab,entrymap,chain);
145
146 #ifdef THEIR_EVENTS
147    if( !( info->evtNum == 504867308  ||
148           info->evtNum == 368148849  ||
149           info->evtNum == 129514273  ||
150           info->evtNum == 286336207  ||
151           info->evtNum == 344708580  ||
152           info->evtNum == 30998576   ||
153           info->evtNum == 155679852  ||
154           info->evtNum == 394010457  ||
155           info->evtNum == 917379387  ||
156           info->evtNum == 78213037   ||
157           info->evtNum == 337493970  ||
158           info->evtNum == 1491724484 ||
159           info->evtNum == 480301165  ||
160           info->evtNum == 1038911933 ||
161           info->evtNum == 876658967  ||
162           info->evtNum == 966824024  ||
163           info->evtNum == 141954801  ||
164           info->evtNum == 160966858  ||
165           info->evtNum == 191231387  ||
166           info->evtNum == 66033190   ||
167           info->evtNum == 10347106   ||
168           info->evtNum == 107360878  ) ) continue;
169 #endif
322  
323 <    unsigned evtfail = fails_HZZ4L_selection(ctrl, info, electronArr, muonArr, eventweight, passtuple);    
324 <    h_evtfail->Fill( evtfail, eventweight );
325 <    if( ctrl.debug ) cout << endl << endl;  
326 <          
327 <  } //end loop over data    
323 >  //
324 >  // Loop !!!!!!!!!
325 >  //--------------------------------------------------------------------------------------------------------------
326 >  for(UInt_t ientry=0; ientry<imax; ientry++)
327 >    {
328 >      chain->GetEntry(ientry);
329 >      if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
330 >
331 >      //if( info->EvtNum() != 217998 ) continue;
332 >      //      if( info->EvtNum() != 238893 ) continue;
333 >      //      if( info->EvtNum() != 289066 ) continue;
334 >
335 >      string fname = string(chain->GetFile()->GetEndpointUrl()->GetFile());
336 >      setEra( fname, ctrl );
337 >
338 >
339 >      //
340 >      // pfNoPU
341 >      //
342 >      //mithep::Array<PFCandidate> pfNoPileUp;
343 >      PFnoPUflag.clear();
344 >      int pfnopu_size = makePFnoPUArray( pfArr, PFnoPUflag, vtxArr );
345 >      assert(pfnopu_size == pfArr->GetEntries());
346 >
347 >      //
348 >      // data/MC
349 >      //
350 >      if(ctrl.mc) {
351 >        //
352 >        // gen info
353 >        //
354 >        if( ctrl.fillGen )
355 >          fillGenInfo( mcArr, mcEvtInfo, geninfo, ESampleType::kHZZ, ctrl);
356 >
357 >        //
358 >        // xsec & PU weights
359 >        //
360 >        weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
361 >        cout << "xsec weight: " << weights.w << endl;
362 >        int npu = -1;
363 >        for(int i=0;i<puArr->GetEntries();i++) {
364 >          if(puArr->At(i)->GetBunchCrossing() == 0)
365 >            npu = puArr->At(i)->GetPU_NumInteractions();
366 >        }
367 >        assert(npu>=0);
368 >        evtinfo.npu;
369 >        weights.npuw = getPUWeight(evtinfo.npu);
370 >        
371 >        //
372 >        // pt reweighting for Higgs < 140, F11
373 >        //
374 >        if( ctrl.fillGen ) {
375 >          if( (fname.find("f11-h115zz4l") != string::npos) ||
376 >              (fname.find("f11-h120zz4l") != string::npos) ||
377 >              (fname.find("f11-h130zz4l") != string::npos)  ) {
378 >            weights.w *= h_wf11_hpt->GetBinContent(h_wf11_hpt->FindBin(geninfo.pt_zz));
379 >          }
380 >        }
381 >
382 >        //
383 >        // trigger
384 >        //
385 >        if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
386 >          currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
387 >          hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
388 >          hltchain->GetEntry(0);
389 >          hltTable->Clear();
390 >          fillTriggerNames(hltTable, hltTableStrings );
391 >        }
392 >        if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
393 >        fillTriggerBits( hltTable, trigMask, triggerBits );
394 >        passes_HLT_MC = true; // passed to apply as extern global, just for sync
395 >        if( !passHLTMC(triggerBits, info->RunNum(), info->EvtNum(), k2012_MC ) ) {
396 >          passes_HLT_MC = false;
397 >        }
398 >      } else {
399 >        //
400 >        // JSON
401 >        //
402 >        /*
403 >        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
404 >        if( !(rlrm.HasRunLumi(rl)) )  {
405 >          if( ctrl.debug ) cout << "\tfails JSON" << endl;
406 >          continue;
407 >        }
408 >        */
409 >        //
410 >        // trigger
411 >        //
412 >        if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
413 >          currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
414 >          hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
415 >          hltchain->GetEntry(0);
416 >          hltTable->Clear();
417 >          fillTriggerNames(hltTable, hltTableStrings );
418 >        }
419 >        if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
420 >        /*
421 >        fillTriggerBits( hltTable, trigMask, triggerBits );
422 >        if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
423 >          if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
424 >          continue;
425 >        }
426 >        */
427 >      }
428 >
429 >      //
430 >      // lepton/kinematic selection ...
431 >      //
432 >      failingLeptons.clear();
433 >      passingLeptons.clear();
434 >      EventData ret4l =
435 >
436 >
437 >        // reference
438 >        apply_HZZ4L_reference_selection(ctrl, info,
439 >                                        vtxArr,
440 >                                        pfArr,
441 >                                        puDArr,
442 >                                        electronArr,
443 >                                        &electronReferencePreSelection,
444 >                                        &electronReferenceIDMVASelectionV1,
445 >                                        &electronReferenceIsoSelection,
446 >                                        muonArr,
447 >                                        &muonReferencePreSelection,
448 >                                        &muonIDPFSelection,
449 >                                        &muonReferenceIsoSelection);
450 >      
451 >      if( ctrl.debug ) cout << endl;
452 >
453 >
454 >      int Z1type=0, Z2type=0;
455 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZCANDIDATE ) {
456 >        if( abs(ret4l.Z1leptons[0].type) == 11  ) Z1type = 11;
457 >        if( abs(ret4l.Z1leptons[0].type) == 13  ) Z1type = 13;
458 >      }  
459 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZZCANDIDATE ) {
460 >        if( abs(ret4l.Z2leptons[0].type) == 11  ) Z2type = 11;
461 >        if( abs(ret4l.Z2leptons[0].type) == 13  ) Z2type = 13;
462 >      }  
463 >      
464 > #ifdef SYNC
465 >      cout  << "bits: " << ret4l.status.selectionBits  << "\t"
466 >            << "Z1: " << Z1type << "\t"
467 >            << "Z2: " << Z2type << endl;
468 >      cout << endl;
469 > #endif      
470 >      
471 >      if( ret4l.status.pass() ) {
472 >        fillAngles(ret4l,angles);
473 >        fillKinematics(ret4l,kinematics);
474 >        fillMassErrors(ret4l,muonArr,electronArr,kinematics);
475 >        fillEventInfo(info,evtinfo);
476 >        if( ctrl.mc) {
477 >          setEffiencyWeights(ret4l, weights);
478 >           if(ctrl.debug)
479 >             cout << "w: " << weights.w << "\t"
480 >                  << "won: " << weights.won << "\t"
481 >                  << "woff: " << weights.woff << endl;
482 >        }
483 >        
484 >
485 >        nt.Fill();
486 >        
487 >        cerr  << "PASS:: "
488 >              << "\trun: " << evtinfo.run
489 >              << "\tevt: " << evtinfo.evt
490 >              << "\tlumi: " << evtinfo.lumi
491 >              << "\tcostheta1: " << angles.costheta1
492 >              << "\tcostheta2: " << angles.costheta2
493 >              << "\tcostheta*: " << angles.costhetastar
494 >              << "\tbdt: " << angles.bdt
495 >              << endl;
496 >        pass++;
497 >        //      if( pass > 3 ) break;
498 >
499 >      } else if( ret4l.status.selectionBits.test(PASS_GOODZ1) &&
500 >                 ret4l.status.selectionBits.to_ulong() < PASS_4L) { // save for fakes ...
501 >        //      if(ctrl.debug) {
502 >          cout << "failingLeptons : " << failingLeptons.size() << endl;
503 >          for( int f=0; f<failingLeptons.size(); f++ ) {
504 >            SimpleLepton  l = failingLeptons[f];
505 >            cout << "f: " << f << "\t"
506 >                 << "type: " << l.type << "\t"
507 >                 << "pT: " << l.vec.Pt()  << "\t"
508 >                 << "eta: " << l.vec.Eta()
509 >                 << endl;
510 >            //    }
511 >        }
512 >        foTree.Fill();
513 >      }
514 >    }  
515  
516 +  
517 +  foTree.getFile()->cd();
518 +  foTree.getTree()->Write();
519 +  nt.WriteClose();
520 +
521 +  cout << endl;
522 +  cout << endl;
523 +  for( int i=0; i<cutvec.size(); i++ ) {
524 +    cout << "cut: " << i << "\t"
525 +         << "pass: " << cutvec[i];
526 +
527 +    if( i>PASS_TRIGGER&&i<=PASS_GOODZ1 )
528 +      cout << "\t2e: " << zcutvec[0][i]
529 +           << "\t2m: " << zcutvec[1][i];
530 +
531 +    if( i>PASS_ZCANDIDATE )
532 +      cout << "\t4e: " << zzcutvec[0][i]
533 +           << "\t4m: " << zzcutvec[1][i]
534 +           << "\t2e2m: " << zzcutvec[2][i];
535  
536 +    cout << "\t" << cutstrs[i] << endl;
537  
538 <  delete info;
539 <  delete electronArr;
181 <  delete muonArr;
538 >    cout << endl;
539 >  }
540  
541 + }
542  
543  
544 <  //--------------------------------------------------------------------------------------------------------------
545 <  // Save Histograms;
546 <  //==============================================================================================================
547 <  const char * ofname;
548 <  if( strcmp( ctrl.outputfile.c_str(), "") ) {
549 <    ofname = ctrl.outputfile.c_str();
550 <  } else {
551 <    ofname = "tmp.root";
544 >
545 > //----------------------------------------------------------------------------
546 > void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
547 > //----------------------------------------------------------------------------
548 > {
549 >  vector<SimpleLepton> lepvec = evtdat.Z1leptons;
550 >  lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
551 >  double w_offline=-1, werr_offline=0;
552 >  double w_online=-1, werr_online=0;
553 >  
554 >  vector< pair <double,double> > wlegs; // pair here is eff & err
555 >  vector< pair <float,float> > mvec;  // pair here is eta & pt
556 >
557 >  //      vector< pair <float,float> > evec;  // pair here is eta & pt
558 >  // now deal with medium vs loose
559 >  vector< pair< bool, pair <float,float> > > evec;  // pair here is eta & pt
560 >  
561 >  for( int k=0; k<lepvec.size(); k++ ) {
562 >    if( abs(lepvec[k].type) == 13 ) {
563 >      mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
564 >      wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
565 >                                                          lepvec[k].vec.Pt() ) );
566 >    } else {
567 >      
568 >      // now deal with medium vs loose
569 >      //              evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
570 >      
571 >      std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
572 >      evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
573 >      
574 >      //              wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
575 >      //                                                                 lepvec[k].vec.Pt() ) );
576 >      
577 >      wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
578 >                                                          lepvec[k].vec.Pt(),
579 >                                                          lepvec[k].isTight ) );
580 >      
581 >    }
582    }
583 +  
584 +  pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
585 +  weights.woff    = offpair.first;
586 +  weights.werroff = offpair.second;
587 +  
588 +  pair<double,double> onpair  = getOnlineEfficiencyWeight( mvec, evec );
589 +  weights.won    = onpair.first;
590 +  weights.werron = onpair.second;
591 + }
592  
195  TFile *file = new TFile(ofname, "RECREATE");
196  h_evt->Write();
197  h_evtfail->Write();
198  passtuple->Write();
199  file->Close();
200  delete file;
593  
594 <  // map<TString,TMVA::Reader*>::iterator it;
595 <  // for(it=readers.begin(); it!=readers.end(); it++) delete (*it).second;
594 > //----------------------------------------------------------------------------
595 > void initRunLumiRangeMap()
596 > //----------------------------------------------------------------------------
597 > {
598 >  /*
599 >  rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
600 >  //  rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
601 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
602 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));  
603 >  rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));  
604 >  // r11b
605 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));  
606 >  */
607 > };
608  
205  cerr << "done!" << endl;
206 }
609  
610 + //----------------------------------------------------------------------------
611 + void initPUWeights()
612 + //----------------------------------------------------------------------------
613 + {
614 +  TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
615 +  hpu = (TH1D*)(puf->Get("puWeights"));
616 + }
617 +
618 + //----------------------------------------------------------------------------
619 + double getPUWeight(unsigned npu)
620 + //----------------------------------------------------------------------------
621 + {
622 +  return hpu->GetBinContent(hpu->FindBin(npu));
623 + }
624  
625 + //----------------------------------------------------------------------------
626 + void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
627 + //----------------------------------------------------------------------------
628 + {
629 +  unsigned evt;
630 +  float rho;
631  
632 +  cout << "initialzing EvtRhoMap ";
633 +  //FILE * f = fopen("evtrho.dat", "r");
634 +  //  FILE * f = fopen("allrho.cali.uniq.dat", "r");
635 +  FILE * f = fopen("/data/blue/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
636 +  int lcount=0;
637 +  while( fscanf( f, "%u:%f", &evt, &rho ) ) {
638 +    if( feof(f) ) break;
639 +    evtrhoMap[evt] = rho;
640 +    lcount++;
641 +    if( !(lcount%1000) ) cout << "."; flush(cout);
642 +  }
643 +  cout << " done" << endl;
644 + }
645 +
646 + //----------------------------------------------------------------------------
647 + unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
648 +                         vector<bool> &pfNoPileUpflag,
649 +                         const mithep::Array<mithep::Vertex>      * vtxArr )
650 + //----------------------------------------------------------------------------
651 + {
652 +  for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
653 +    const PFCandidate *pf = fPFCandidates->At(i);
654 +    assert(pf);
655 +
656 +    if(pf->PFType() == PFCandidate::eHadron) {
657 +      if(pf->HasTrackerTrk() &&
658 +         vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
659 +         vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
660 +
661 +        pfNoPileUpflag.push_back(1);
662 +
663 +      } else {
664 +
665 +        Bool_t vertexFound = kFALSE;
666 +        const Vertex *closestVtx = 0;
667 +        Double_t dzmin = 10000;
668 +        
669 +        // loop over vertices
670 +        for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
671 +          const Vertex *vtx = vtxArr->At(j);
672 +          assert(vtx);
673 +          
674 +          if(pf->HasTrackerTrk() &&
675 +             vtx->HasTrack(pf->TrackerTrk()) &&
676 +             vtx->TrackWeight(pf->TrackerTrk()) > 0) {
677 +            vertexFound = kTRUE;
678 +            closestVtx = vtx;
679 +            break;
680 +          }
681 +          Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
682 +          if(dz < dzmin) {
683 +            closestVtx = vtx;
684 +            dzmin = dz;
685 +          }
686 +        }
687 +
688 +        //      if(fCheckClosestZVertex) {
689 +        if(1) {
690 +          // Fallback: if track is not associated with any vertex,
691 +          // associate it with the vertex closest in z
692 +          if(vertexFound || closestVtx != vtxArr->At(0)) {
693 +            //      pfPileUp->Add(pf);
694 +            pfNoPileUpflag.push_back(0);
695 +          } else {
696 +            pfNoPileUpflag.push_back(1);
697 +          }
698 +        } else {
699 +          if(vertexFound && closestVtx != vtxArr->At(0)) {
700 +            //      pfPileUp->Add(pf);
701 +            pfNoPileUpflag.push_back(0);
702 +          } else {
703 +            //      PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
704 +            pfNoPileUpflag.push_back(1);
705 +          }
706 +        }
707 +      } //hadron & trk stuff
708 +    } else { // hadron
709 +      //      PFCandidate * ptr = pfNoPileUp->AddNew();
710 +      pfNoPileUpflag.push_back(1);
711 +    }
712 +  } // Loop over PF candidates
713 +
714 +  return pfNoPileUpflag.size();
715 + }
716 +
717 + void setEra(string fname, ControlFlags &ctrl)
718 + {
719 +  if( ctrl.mc && (fname.find("f11-") != string::npos)  ) ctrl.era=2011 ;
720 +  if( ctrl.mc && (fname.find("s12-") != string::npos)  ) ctrl.era=2012 ;
721 +  if( !ctrl.mc && (fname.find("r11-") != string::npos)  ) ctrl.era=2011 ;
722 +  if( !ctrl.mc && (fname.find("r12-") != string::npos)  ) ctrl.era=2012 ;
723 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines