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.8 by khahn, Fri Oct 14 11:20:25 2011 UTC vs.
Revision 1.19 by khahn, Thu May 10 22:36:12 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 "SiMVAElectronSelection.h"
38 < #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"
49 < #include "HZZCiCElectronSelection.h"
50 < #include "HZZLikelihoodElectronSelection.h"
51 < #include "HZZBDTElectronSelection.h"
52 < #include "SampleWeight.h"
71 > #include "EfficiencyWeightsInterface.h"
72  
73 < //#define BDTFAIL_THEIR_EVENTS
55 < //#define THEIR_EVENTS
73 > #include "SimpleLepton.h"
74  
75 < //=== MAIN =================================================================================================
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 >
94 > //
95 > // prototypes
96 > //----------------------------------------------------------------------------
97 > bool setPV(ControlFlags,const mithep::Array<mithep::Vertex>*, mithep::Vertex& );
98 > void initPUWeights();
99 > double getPUWeight(unsigned npu);
100 > void setEffiencyWeights(EventData&, WeightStruct& );
101 > void initRunLumiRangeMap();
102 > void initEvtRhoMap(map<unsigned,float> &);
103 > //----------------------------------------------------------------------------
104 >
105 >
106 > //
107 > // MAIN
108 > //----------------------------------------------------------------------------
109   int main(int argc, char** argv)
110 + //----------------------------------------------------------------------------
111   {
112 <  
112 >  cutstrs.push_back(string("skim0"));              //0
113 >  cutstrs.push_back(string("skim1"));              //1
114 >  // -------------------------------------------------
115 >  cutstrs.push_back(string("Z candidate"));        //2
116 >  cutstrs.push_back(string("good Z1"));            //3
117 >  // -------------------------------------------------
118 >  cutstrs.push_back(string("4l"));                 //4
119 >  cutstrs.push_back(string("ZZ candidate"));       //5
120 >  cutstrs.push_back(string("good Z2"));            //6
121 >  cutstrs.push_back(string("ZZ 20/10"));           //7
122 >  cutstrs.push_back(string("resonance"));          //8
123 >  cutstrs.push_back(string("m4l>70"));             //9
124 >  cutstrs.push_back(string("m4l>100"));            //10
125 >
126 >
127 >
128 >
129 >  string cmsswpath(CMSSW_BASE);
130 >  cmsswpath.append("/src");
131 >  std::bitset<1024> triggerBits;
132    vector<vector<string> > inputFiles;
133    inputFiles.push_back(vector<string>());
134 +  map<string,unsigned> entrymap; // number of unskimmed entries in each file
135 +  for( int i=0; i<11; i++ ) cutvec.push_back(0);
136 +  for( int i=0; i<2; i++ )  {
137 +    zcutvec.push_back(vector<unsigned>());
138 +    for( int j=0; j<11; j++ ) zcutvec[i].push_back(0);
139 +  }
140 +  for( int i=0; i<3; i++ )  {
141 +    zzcutvec.push_back(vector<unsigned>());
142 +    for( int j=0; j<11; j++ ) zzcutvec[i].push_back(0);
143 +  }
144 +
145  
146    //
147    // args
148    //--------------------------------------------------------------------------------------------------------------
149    ControlFlags ctrl;
150    parse_args( argc, argv, ctrl );
151 <  if( ctrl.inputfile.empty() ||
152 <      ctrl.inputfile.empty() ) {
153 <    cerr << "usage: applySelection.exe <flags> " << endl;
154 <    cerr << "\tmandoatory flags : " << endl;
155 <    cerr << "\t--inputfile |  file containing a list of ntuples to run over" << endl;
156 <    cerr << "\t--outputfile | file to store  selected evet" << endl;
157 <    return 1;
158 <  }
151 >  if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
152 >    {
153 >      cerr << "usage: applySelection.exe <flags> " << endl;
154 >      cerr << "\tmandoatory flags : " << endl;
155 >      cerr << "\t--inputfiles |  file containing a list of ntuples to run over" << endl;
156 >      cerr << "\t--inputfile |  a file to run over" << endl;
157 >      cerr << "\t--outputfile | file to store  selected evet" << endl;
158 >      return 1;
159 >    }
160    ctrl.dump();
161  
162 <  map<string,double> entrymap; // number of unskimmed entries in each file
162 >
163  
164    //
165    // File I/O
166    //--------------------------------------------------------------------------------------------------------------
167    TChain * chain = new TChain("Events");
168 <  ifstream f(ctrl.inputfile.c_str());
168 >  TChain * hltchain = new TChain("HLT");
169 >
170    string fname;
171 <  while (f >> fname) {
172 <    if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
173 <    cout << "adding inputfile : " << fname.c_str() << endl;
174 <    entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
175 <    chain->AddFile(fname.c_str());
171 >  if( !ctrl.inputfiles.empty() ) {
172 >    ifstream f(ctrl.inputfiles.c_str());
173 >    while (f >> fname) {
174 >      if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
175 >      cout << "adding inputfile : " << fname.c_str() << endl;
176 >      entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
177 >      cout << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
178 >      chain->AddFile(fname.c_str());
179 >      hltchain->AddFile(fname.c_str());
180 >    }
181 >  } else {
182 >    cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
183 >    unsigned tmpent = unskimmedEntries(ctrl.inputfile.c_str());
184 >    cout << "tmpent: " << tmpent << endl;
185 >    entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
186 >    cout << "unskimmed entries: " << entrymap[string(ctrl.inputfile.c_str())] << endl;
187 >    chain->AddFile(ctrl.inputfile.c_str());
188 >    hltchain->AddFile(ctrl.inputfile.c_str());
189    }
190  
191 +  const char * ofname;
192 +  if( strcmp( ctrl.outputfile.c_str(), "") ) {
193 +    ofname = ctrl.outputfile.c_str();
194 +  } else {
195 +    ofname = "tmp.root";
196 +  }
197    // table of cross section values  
198 <  SimpleTable xstab("./data/xs.dat");
198 >  string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
199 >  cout << "xspath: " << xspath.c_str() << endl;
200 >  SimpleTable xstab(xspath.c_str());
201 >
202  
203    //
204    // Setup
205    //--------------------------------------------------------------------------------------------------------------
206 <  TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
207 <  TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 1024, 0, 1024 );
208 <  //  TNtuple * passtuple = new TNtuple( "passtuple", "passtuple", "run:evt:lumi:channel:mZ1:mZ2:m4l:pt4l:w" );
209 <  TTree * passtuple = new TTree("passtuple", "passtuple" );
210 <  unsigned run, evt, lumi, channel;
211 <  float mZ1, mZ2, m4l, pt4l;
212 <  double w;
213 <  passtuple->Branch("run", &run);
214 <  passtuple->Branch("evt", &evt);
215 <  passtuple->Branch("lumi", &lumi);
216 <  passtuple->Branch("mZ1", &mZ1);
217 <  passtuple->Branch("mZ2", &mZ2);
218 <  passtuple->Branch("m4l", &m4l);
219 <  passtuple->Branch("pt4l", &pt4l);
220 <  passtuple->Branch("w", &w);
221 <  initCiCSelection();
222 <  if(ctrl.eleSele=="lik") initLikSelection();
223 <  if(ctrl.eleSele=="bdt") initBDTSelection();
224 <  if(ctrl.eleSele=="si" ) initSiMVAElectronSelection();
225 <  initRunLumiRangeMap();
206 >  Angles angles;
207 >  KinematicsStruct kinematics;
208 >  InfoStruct evtinfo;
209 >  WeightStruct weights;
210 >  //  GenInfoStruct geninfo;
211 >
212 >  AngleTuple nt( (const char*)ofname, (const char*)"zznt");
213 >  nt.makeAngleBranch(angles);
214 >  nt.makeKinematicsBranch(kinematics);
215 >  nt.makeInfoBranch(evtinfo);
216 >
217 >  FOTuple foTree( nt.getFile(), (const char*)"FOtree");
218 >  foTree.makeFailedLeptonBranch(failingLeptons);
219 >  foTree.makeZLeptonBranch(passingLeptons);
220 >
221 >  TH1F * h_w_hpt;
222 >  if(ctrl.mc) {
223 >    nt.makeWeightBranch(weights);
224 >    //    nt.makeGenInfoBranch(geninfo);
225 >    initEfficiencyWeights();
226 >    initPUWeights();
227 >    /*
228 >    // Higgs only, pt reweighting
229 >    if( ctrl.mH <= 140 ) {
230 >      char wptname[256];
231 >      sprintf( wptname, "/data/blue/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
232 >      TFile * f_hpt = new TFile(wptname);
233 >      f_hpt->Print();
234 >      sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
235 >      h_w_hpt  = (TH1F*)(f_hpt->FindObjectAny(wptname));
236 >      h_w_hpt->Print();
237 >    }
238 >    */
239 >  } else {
240 >    initRunLumiRangeMap();
241 >  }
242 >
243 >  //  initMuonIDMVA();
244 >  initMuonIsoMVA();
245 >  initElectronIDMVA();
246 >  initElectronIsoMVA();
247 >  initTrigger();
248 >
249 >  // hack
250 >  initEvtRhoMap(evtrhoMap);
251  
121  //
122  // Loop
123  //--------------------------------------------------------------------------------------------------------------
252    
253    //
254 <  // Access samples and fill histograms
254 >  // Setup tree I/O
255 >  //--------------------------------------------------------------------------------------------------------------
256    TFile *inputFile=0;
257    TTree *eventTree=0;  
258 <  
130 <  // Data structures to store info from TTrees
131 <  mithep::TEventInfo *info    = new mithep::TEventInfo();
132 <  TClonesArray *electronArr   = new TClonesArray("mithep::TElectron");
133 <  TClonesArray *muonArr       = new TClonesArray("mithep::TMuon");
134 <  
135 <
136 <  TBranch *infoBr;
137 <  TBranch *electronBr;
138 <  TBranch *muonBr;
139 <
140 < //   chain->SetBranchStatus("*",0); //disable all branches
141 < //   chain->SetBranchStatus("Info", 1);
142 < //   chain->SetBranchStatus("Muon", 1);
143 < //   chain->SetBranchStatus("Electron", 1);
144 <
145 <  chain->SetBranchAddress("Info",       &info);
146 <  chain->SetBranchAddress("Electron", &electronArr);
147 <  chain->SetBranchAddress("Muon", &muonArr);
258 >  string currentFile("");
259  
260 +  mithep::EventHeader *info    = new mithep::EventHeader();
261 +  //  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
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::TriggerMask                         *trigMask      = new mithep::TriggerMask();
270 +  mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
271 +  vector<string>                              *hltTableStrings  = new vector<string>();
272 +
273 +  TString fElectronName(Names::gkElectronBrn);
274 +  TString fMuonName(Names::gkMuonBrn);
275 +  TString fInfoName(Names::gkEvtHeaderBrn);
276 +  TString fPrimVtxName(Names::gkPVBrn);
277 +  TString fPFCandidateName(Names::gkPFCandidatesBrn);
278 +  TString fPileupInfoName(Names::gkPileupInfoBrn);
279 +  TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
280 +  TString fTrackName(Names::gkTrackBrn);
281 +  TString fTriggerMaskName(Names::gkHltBitBrn);
282 +  TString fTriggerTableName(Names::gkHltTableBrn);
283 +  
284 +  chain->SetBranchAddress(fInfoName,        &info);
285 +  chain->SetBranchAddress(fElectronName,    &electronArr);
286 +  chain->SetBranchAddress(fMuonName,        &muonArr);
287 +  chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
288 +  chain->SetBranchAddress(fPFCandidateName, &pfArr);
289 +  if( ctrl.mc ) chain->SetBranchAddress(fPileupInfoName,  &puArr);
290 +  chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
291 +  chain->SetBranchAddress(fTrackName, &trkArr);
292 +  chain->SetBranchAddress(fTriggerMaskName, &trigMask);
293 +  cout << "hlttable: " << fTriggerTableName << endl;
294 +  hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
295 +
296 +  mithep::Vertex              vtx;          // best primary vertex in the event
297 +
298 +  //  ginfo = NULL;
299 +  // if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
300 +
301 +  int count=0, pass=0;
302 +  float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
303 +  float denom[3]={0.,0.,0.};
304 +  float numer[3]={0.,0.,0.};
305 +  float numercorr[3]={0.,0.,0.};
306 +
307 +  // only 1 HLT table / file ???
308 +  hltchain->GetEntry(0);
309 +  cerr << "here... " << endl;
310  
311 +  int imax = chain->GetEntries();
312 +  cout << "nEntries: " << imax << endl;
313  
151  cout << "nEntries: " << chain->GetEntries() << endl;
152  for(UInt_t ientry=0; ientry<chain->GetEntries(); ientry++) {
153
154    chain->GetEntry(ientry);
155    if(!(ientry%100000)) cout << "entry: " << ientry << endl;
156
157    // get event weight for this file                                                    
158    double eventweight=1;
159    if(ctrl.mc) eventweight = getWeight(xstab,entrymap,chain);
160
161 #ifdef THEIR_EVENTS
162    if( !( info->evtNum == 504867308  ||
163           info->evtNum == 368148849  ||
164           info->evtNum == 129514273  ||
165           info->evtNum == 286336207  ||
166           info->evtNum == 344708580  ||
167           info->evtNum == 30998576   ||
168           info->evtNum == 155679852  ||
169           info->evtNum == 394010457  ||
170           info->evtNum == 917379387  ||
171           info->evtNum == 78213037   ||
172           info->evtNum == 337493970  ||
173           info->evtNum == 1491724484 ||
174           info->evtNum == 480301165  ||
175           info->evtNum == 1038911933 ||
176           info->evtNum == 876658967  ||
177           info->evtNum == 966824024  ||
178           info->evtNum == 141954801  ||
179           info->evtNum == 160966858  ||
180           info->evtNum == 191231387  ||
181           info->evtNum == 66033190   ||
182           info->evtNum == 10347106   ||
183           info->evtNum == 107360878  ) ) continue;
184 #endif
185
314  
315 < #ifdef BDTFAIL_THEIR_EVENTS
316 <    if( !( info->evtNum == 78213037   ||
317 <           info->evtNum == 876658967  ) ) continue;
318 <          
319 < #endif
315 >  //
316 >  // Loop !!!!!!!!!
317 >  //--------------------------------------------------------------------------------------------------------------
318 >  for(UInt_t ientry=0; ientry<imax; ientry++)
319 >    {
320 >      chain->GetEntry(ientry);
321 >      if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
322 >      setPV( ctrl, vtxArr, vtx);
323 >
324 >      cout << "origrho: " << puDArr->At(0)->Rho()
325 >           << "\torigrholoweta: " << puDArr->At(0)->RhoLowEta()
326 >           << endl;
327 >
328 >
329 >      //
330 >      // data/MC
331 >      //
332 >      if(ctrl.mc) {
333 >        //
334 >        // xsec & PU weights
335 >        //
336 >        //      weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
337 >        int npu = -1;
338 >        for(int i=0;i<puArr->GetEntries();i++) {
339 >          if(puArr->At(i)->GetBunchCrossing() == 0)
340 >            npu = puArr->At(i)->GetPU_NumInteractions();
341 >        }
342 >        assert(npu>=0);
343 >        evtinfo.npu;
344 >        weights.npuw = getPUWeight(evtinfo.npu);
345 >        //      cout << "weight: " << weights.w << endl;
346 >      } else {
347 >        //
348 >        // JSON
349 >        //
350 >        /*
351 >        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
352 >        if( !(rlrm.HasRunLumi(rl)) )  {
353 >          if( ctrl.debug ) cout << "\tfails JSON" << endl;
354 >          continue;
355 >        }
356 >        */
357 >        //
358 >        // trigger
359 >        //
360 >        if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
361 >          currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
362 >          hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
363 >          hltchain->GetEntry(0);
364 >          hltTable->Clear();
365 >          fillTriggerBits(hltTable, hltTableStrings );
366 >        }
367 >        if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
368 >        fillTriggerBits( hltTable, trigMask, triggerBits );
369 >        if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
370 >          if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
371 >          continue;
372 >        }
373 >      }
374 >
375 >      //
376 >      // lepton/kinematic selection ...
377 >      //
378 >      failingLeptons.clear();
379 >      passingLeptons.clear();
380 >      EventData ret4l =
381 >        apply_HZZ4L_reference2_selection(ctrl, info,
382 >                                         vtx,
383 >                                         pfArr,
384 >                                         //                           evtrhoMap[info->EvtNum()],
385 >                                         puDArr,
386 >                                         electronArr,
387 >                                         &electronReferencePreSelection,
388 >                                         &electronReferenceIDMVASelection,
389 >                                         &electronReferenceIsoSelection,
390 >                                         muonArr,
391 >                                         &muonReferencePreSelection,
392 >                                         &muonIDPFSelection,
393 >                                         &muonReferenceIsoSelection);
394 >        /*
395 >        apply_HZZ4L_reference_selection(ctrl, info,
396 >                              vtx,
397 >                              pfArr,
398 >                              puArr,
399 >                              electronArr,
400 >                              &electronPreSelection,
401 >                              &electronIDMVASelection,
402 >                              &electronIsoMVASelection,
403 >                              muonArr,
404 >                              &muonPreSelection,
405 >                              &muonIDPFSelection,
406 >                              &muonIsoMVASelection);
407 >        */
408 >
409 >      if( ctrl.debug ) cout << endl;
410 >
411 >
412 >      int Z1type=0, Z2type=0;
413 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZCANDIDATE ) {
414 >        if( abs(ret4l.Z1leptons[0].type) == 11  ) Z1type = 11;
415 >        if( abs(ret4l.Z1leptons[0].type) == 13  ) Z1type = 13;
416 >      }  
417 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZZCANDIDATE ) {
418 >        if( abs(ret4l.Z2leptons[0].type) == 11  ) Z2type = 11;
419 >        if( abs(ret4l.Z2leptons[0].type) == 13  ) Z2type = 13;
420 >      }  
421 >      
422 >      cout  << "bits: " << ret4l.status.selectionBits  << "\t"
423 >            << "Z1: " << Z1type << "\t"
424 >            << "Z2: " << Z2type << endl;
425 >      cout << endl;
426 >                      
427 >      
428 >      if( ret4l.status.pass() ) {
429 >        fillAngles(ret4l,angles);
430 >        fillKinematics(ret4l,kinematics);
431 >        fillEventInfo(info,evtinfo);
432 >        if( ctrl.mc) {
433 >        // fillGenInfo(ginfo,geninfo);
434 >          setEffiencyWeights(ret4l, weights);
435 >           if(ctrl.debug)
436 >             cout << "w: " << weights.w << "\t"
437 >                  << "won: " << weights.won << "\t"
438 >                  << "woff: " << weights.woff << endl;
439 >        }
440 >        
441 >        /*
442 >        // only for Higgs < 140
443 >        geninfo.weight *= h_w_hpt->GetBinContent(h_w_hpt->FindBin(geninfo.pt_zz));
444 >        angles.bdt = reader->EvaluateMVA("BDTG");
445 >        */
446 >        nt.Fill();
447 >        
448 >        cerr  << "PASS:: "
449 >              << "\trun: " << evtinfo.run
450 >              << "\tevt: " << evtinfo.evt
451 >              << "\tlumi: " << evtinfo.lumi
452 >              << "\tcostheta1: " << angles.costheta1
453 >              << "\tcostheta2: " << angles.costheta2
454 >              << "\tcostheta*: " << angles.costhetastar
455 >              << "\tbdt: " << angles.bdt
456 >              << endl;
457 >        pass++;
458 >        //      if( pass > 3 ) break;
459 >
460 >      } else if( ret4l.status.selectionBits.test(PASS_ZCANDIDATE) ) { // save for fakes ...
461 >        if(ctrl.debug) {
462 >          cout << "failingLeptons : " << failingLeptons.size() << endl;
463 >          for( int f=0; f<failingLeptons.size(); f++ ) {
464 >            SimpleLepton  l = failingLeptons[f];
465 >            cout << "f: " << f << "\t"
466 >                 << "type: " << l.type << "\t"
467 >                 << "pT: " << l.vec.Pt()  << "\t"
468 >                 << "eta: " << l.vec.Eta()
469 >                 << endl;
470 >          }
471 >        }
472 >        foTree.Fill();
473 >      }
474 >    }  
475  
476 <    unsigned evtfail = fails_HZZ4L_selection(ctrl, info, electronArr, muonArr, eventweight, passtuple);    
477 <    h_evtfail->Fill( evtfail, eventweight );
476 >  
477 >  foTree.getFile()->cd();
478 >  foTree.getTree()->Write();
479 >  nt.WriteClose();
480 >
481 >  cout << endl;
482 >  cout << endl;
483 >  for( int i=0; i<cutvec.size(); i++ ) {
484 >    cout << "cut: " << i << "\t"
485 >         << "pass: " << cutvec[i];
486 >
487 >    if( i>1&&i<=3 )
488 >      cout << "\t2e: " << zcutvec[0][i]
489 >           << "\t2m: " << zcutvec[1][i];
490 >
491 >    if( i>3 )
492 >      cout << "\t4e: " << zzcutvec[0][i]
493 >           << "\t4m: " << zzcutvec[1][i]
494 >           << "\t2e2m: " << zzcutvec[2][i];
495  
496 <          
197 <  } //end loop over data    
496 >    cout << "\t" << cutstrs[i] << endl;
497  
498 +    cout << endl;
499 +  }
500  
501 + }
502  
503 <  delete info;
504 <  delete electronArr;
505 <  delete muonArr;
503 > //----------------------------------------------------------------------------
504 > //
505 > // Get primary vertices
506 > // Assumes primary vertices are ordered by sum-pT^2 (as should be in CMSSW)
507 > // NOTE: if no PV is found from fitting tracks, the beamspot is used
508 > //
509 > //----------------------------------------------------------------------------
510 > bool setPV(ControlFlags ctrl,
511 >           const mithep::Array<mithep::Vertex> * vtxArr,
512 >           mithep::Vertex & vtx)
513 > //----------------------------------------------------------------------------
514 > {
515 >  const Vertex *bestPV = 0;
516 >  float best_sumpt=-1;
517 >
518 >  // good PV requirements
519 >  const UInt_t   fMinNTracksFit = 0;
520 >  const Double_t fMinNdof       = 4;
521 >  const Double_t fMaxAbsZ       = 24;
522 >  const Double_t fMaxRho        = 2;
523 >  
524 >  for(int i=0; i<vtxArr->GetEntries(); ++i) {
525 >    const Vertex *pv = (Vertex*)(vtxArr->At(i));
526 >    if( ctrl.debug ) cout << "vertex :: " << i << "\tntrks: " << pv->NTracks() << endl;
527 >    
528 >    // Select best PV for corrected d0; if no PV passing cuts, the first PV in the collection will be used
529 >    //  if(!pv->IsValid()) continue;
530 >    if(pv->NTracksFit()       < fMinNTracksFit)       continue;
531 >    if(pv->Ndof()                 < fMinNdof)         continue;
532 >    if(fabs(pv->Z()) > fMaxAbsZ)                      continue;
533 >    if(pv->Position().Rho()   > fMaxRho)              continue;    
534 >    
535 >    // take the first ...
536 >    bestPV = pv;
537 >    break;
538 >
539 >    // this never reached ...    
540 >    float tmp_sumpt=0;
541 >    for( int t=0; t<pv->NTracks(); t++ )
542 >      tmp_sumpt += pv->Trk(t)->Pt();
543 >    
544 >    if( tmp_sumpt > best_sumpt  ) {
545 >      bestPV = pv;
546 >      best_sumpt = tmp_sumpt;
547 >      if( ctrl.debug) cout << "new PV set, pt : " << best_sumpt << endl;
548 >    }
549 >  }
550 >  
551 >  if(!bestPV) bestPV = vtxArr->At(0);
552 >  vtx.SetPosition(bestPV->X(),bestPV->Y(),bestPV->Z());
553 >  vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
554 >  return true;
555 > };
556  
557  
558  
559 <  //--------------------------------------------------------------------------------------------------------------
560 <  // Save Histograms;
561 <  //==============================================================================================================
562 <  const char * ofname;
563 <  if( strcmp( ctrl.outputfile.c_str(), "") ) {
564 <    ofname = ctrl.outputfile.c_str();
565 <  } else {
566 <    ofname = "tmp.root";
559 > //----------------------------------------------------------------------------
560 > void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
561 > //----------------------------------------------------------------------------
562 > {
563 >  vector<SimpleLepton> lepvec = evtdat.Z1leptons;
564 >  lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
565 >  double w_offline=-1, werr_offline=0;
566 >  double w_online=-1, werr_online=0;
567 >  
568 >  vector< pair <double,double> > wlegs; // pair here is eff & err
569 >  vector< pair <float,float> > mvec;  // pair here is eta & pt
570 >
571 >  //      vector< pair <float,float> > evec;  // pair here is eta & pt
572 >  // now deal with medium vs loose
573 >  vector< pair< bool, pair <float,float> > > evec;  // pair here is eta & pt
574 >  
575 >  for( int k=0; k<lepvec.size(); k++ ) {
576 >    if( abs(lepvec[k].type) == 13 ) {
577 >      mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
578 >      wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
579 >                                                          lepvec[k].vec.Pt() ) );
580 >    } else {
581 >      
582 >      // now deal with medium vs loose
583 >      //              evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
584 >      
585 >      std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
586 >      evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
587 >      
588 >      //              wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
589 >      //                                                                 lepvec[k].vec.Pt() ) );
590 >      
591 >      wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
592 >                                                          lepvec[k].vec.Pt(),
593 >                                                          lepvec[k].isTight ) );
594 >      
595 >    }
596    }
597 +  
598 +  pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
599 +  weights.woff    = offpair.first;
600 +  weights.werroff = offpair.second;
601 +  
602 +  pair<double,double> onpair  = getOnlineEfficiencyWeight( mvec, evec );
603 +  weights.won    = onpair.first;
604 +  weights.werron = onpair.second;
605 + }
606  
217  TFile *file = new TFile(ofname, "RECREATE");
218  h_evt->Write();
219  h_evtfail->Write();
220  passtuple->Write();
221  file->Close();
222  delete file;
607  
608 <  // map<TString,TMVA::Reader*>::iterator it;
609 <  // for(it=readers.begin(); it!=readers.end(); it++) delete (*it).second;
608 > //----------------------------------------------------------------------------
609 > void initRunLumiRangeMap()
610 > //----------------------------------------------------------------------------
611 > {
612 >  /*
613 >  rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
614 >  //  rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
615 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
616 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));  
617 >  rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));  
618 >  // r11b
619 >  rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));  
620 >  */
621 > };
622  
227  cerr << "done!" << endl;
228 }
623  
624 + //----------------------------------------------------------------------------
625 + void initPUWeights()
626 + //----------------------------------------------------------------------------
627 + {
628 +  TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
629 +  hpu = (TH1D*)(puf->Get("puWeights"));
630 + }
631 +
632 + //----------------------------------------------------------------------------
633 + double getPUWeight(unsigned npu)
634 + //----------------------------------------------------------------------------
635 + {
636 +  return hpu->GetBinContent(hpu->FindBin(npu));
637 + }
638  
639 + //----------------------------------------------------------------------------
640 + void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
641 + //----------------------------------------------------------------------------
642 + {
643 +  unsigned evt;
644 +  float rho;
645  
646 +  FILE * f = fopen("evtrho.dat", "r");
647 +  while( fscanf( f, "%u:%f", &evt, &rho ) ) {
648 +    evtrhoMap[evt] = rho;
649 +  }
650 +  
651 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines