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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines