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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines