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.15 by khahn, Sun May 6 23:51:01 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   using namespace std;
9  
10   //
11 < // ROOT headers
11 > // root headers
12   //
13 < #include <TROOT.h>                  // access to gROOT, entry point to ROOT system
14 < #include <TSystem.h>                // interface to OS
15 < #include <TFile.h>                  // file handle class
16 < #include <TNtuple.h>
17 < #include <TTree.h>                  // class to access ntuples
18 < #include <TChain.h>                 //
19 < #include <TBranch.h>                // class to access branches in TTree
20 < #include <TClonesArray.h>           // ROOT array class
21 < #include <TCanvas.h>                // class for drawing
22 < #include <TH1F.h>                   // 1D histograms
23 < #include <TBenchmark.h>             // class to track macro running statistics
24 < #include <TLorentzVector.h>         // 4-vector class
25 < #include <TVector3.h>               // 3D vector class
13 > #include <TFile.h>                  
14 > #include <TTree.h>                  
15 > #include <TChain.h>                
16 > #include <TBranch.h>                
17 > #include <TClonesArray.h>          
18 >
19 >
20 > //
21 > // TMVA
22 > //
23 > #include "TMVA/Reader.h"
24 > #include "TMVA/Tools.h"
25 > #include "TMVA/Config.h"
26 > #include "TMVA/MethodBDT.h"
27  
28   //
29   // ntuple format headers
30   //
31 < #include "EWKAnaDefs.hh"
32 < #include "TEventInfo.hh"
33 < #include "TElectron.hh"
34 < #include "TMuon.hh"
35 < #include "TJet.hh"
36 < #include "RunLumiRangeMap.h"
31 > #include "EventHeader.h"
32 > #include "Electron.h"
33 > #include "Muon.h"
34 > #include "Vertex.h"
35 > #include "PFCandidate.h"
36 > #include "PFCandidateCol.h"
37 > #include "TriggerMask.h"
38 > #include "TriggerTable.h"
39 > #include "Names.h"
40 > #include "BaseMod.h"
41  
42   //
43 < // utility headers
43 > // our headers
44   //
45   #include "ParseArgs.h"
46 < #include "Selection.h"
47 < #include "HZZCiCElectronSelection.h"
46 > #include "MuonSelection.h"
47 > #include "ElectronSelection.h"
48 > #include "IsolationSelection.h"
49 >
50 > //#include "Selection.h"
51 > #include "ReferenceSelection.h"
52 >
53 > #include "TriggerUtils.h"
54 > #include "PassHLT.h"
55 > #include "Angles.h"
56 > #include "KinematicsStruct.h"
57 > #include "InfoStruct.h"
58 > //#include "GenInfoStruct.h"
59 > #include "WeightStruct.h"
60 > #include "GlobalDataAndFuncs.h"
61 >
62 > #include "AngleTuple.h"
63 > #include "FOTuple.h"
64 >
65 > #include "SampleWeight.h"
66 > #include "EfficiencyWeightsInterface.h"
67 >
68 > #ifndef CMSSW_BASE
69 > #define CMSSW_BASE "../"
70 > #endif
71  
72 < //#define THEIR_EVENTS
72 > using namespace mithep;
73 >
74 > #include "SimpleLepton.h"
75 > vector<SimpleLepton> failedLeptons; // for fake estimation
76 >
77 > //----------------------------------------------------------------------------
78 > bool setPV(ControlFlags,const mithep::Array<mithep::Vertex>*, mithep::Vertex& );
79 > //----------------------------------------------------------------------------
80  
81   //=== MAIN =================================================================================================
82   int main(int argc, char** argv)
83   {
84 <  
84 >  string cmsswpath(CMSSW_BASE);
85 >  cmsswpath.append("/src");
86 >  std::bitset<1024> triggerBits;
87    vector<vector<string> > inputFiles;
88    inputFiles.push_back(vector<string>());
89 +  map<string,double> entrymap; // number of unskimmed entries in each file
90  
91    //
92    // args
93    //--------------------------------------------------------------------------------------------------------------
94    ControlFlags ctrl;
95    parse_args( argc, argv, ctrl );
96 <  if( ctrl.inputfile.empty() ||
97 <      ctrl.inputfile.empty() ) {
98 <    cerr << "usage: applySelection.exe <flags> " << endl;
99 <    cerr << "\tmandoatory flags : " << endl;
100 <    cerr << "\t--inputfile |  file containing a list of ntuples to run over" << endl;
101 <    cerr << "\t--outputfile | file to store  selected evet" << endl;
102 <    return 1;
103 <  }
96 >  if( ctrl.inputfiles.empty() &&ctrl.inputfile.empty() )
97 >    {
98 >      cerr << "usage: applySelection.exe <flags> " << endl;
99 >      cerr << "\tmandoatory flags : " << endl;
100 >      cerr << "\t--inputfiles |  file containing a list of ntuples to run over" << endl;
101 >      cerr << "\t--inputfile |  a file to run over" << endl;
102 >      cerr << "\t--outputfile | file to store  selected evet" << endl;
103 >      return 1;
104 >    }
105    ctrl.dump();
106  
107 +
108 +
109    //
110    // File I/O
111    //--------------------------------------------------------------------------------------------------------------
112    TChain * chain = new TChain("Events");
113 <  ifstream f(ctrl.inputfile.c_str());
113 >  TChain * hltchain = new TChain("HLT");
114 >
115    string fname;
116 <  while (f >> fname) {
117 <    if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines  
118 <    cout << "adding inputfile : " << fname.c_str() << endl;
119 <    chain->AddFile(fname.c_str());
116 >  if( !ctrl.inputfiles.empty() ) {
117 >    ifstream f(ctrl.inputfiles.c_str());
118 >    while (f >> fname) {
119 >      if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
120 >      cout << "adding inputfile : " << fname.c_str() << endl;
121 >      entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
122 >      chain->AddFile(fname.c_str());
123 >      hltchain->AddFile(fname.c_str());
124 >    }
125 >  } else {
126 >    cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
127 >    entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
128 >    chain->AddFile(ctrl.inputfile.c_str());
129 >    hltchain->AddFile(ctrl.inputfile.c_str());
130 >  }
131 >
132 >  const char * ofname;
133 >  if( strcmp( ctrl.outputfile.c_str(), "") ) {
134 >    ofname = ctrl.outputfile.c_str();
135 >  } else {
136 >    ofname = "tmp.root";
137    }
138 +  // table of cross section values  
139 +  string xspath = (cmsswpath+string("/MitPhysics/data/xs.dat"));
140 +  cout << "xspath: " << xspath.c_str() << endl;
141 +  SimpleTable xstab(xspath.c_str());
142 +
143  
144    //
145    // Setup
146    //--------------------------------------------------------------------------------------------------------------
147 <  TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
148 <  TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 100, 0, 100 );
149 <  TNtuple * passtuple = new TNtuple( "passtuple", "passtuple", "run:evt:lumi:channel:mZ1:mZ2:m4l:pt4l:w" );
150 <  initCiCSelection();
151 <  initRunLumiRangeMap();
147 >  Angles angles;
148 >  KinematicsStruct kinematics;
149 >  InfoStruct evtinfo;
150 >  WeightStruct weights;
151 >  //  GenInfoStruct geninfo;
152 >
153 >  
154 >  AngleTuple nt( (const char*)ofname, (const char*)"zznt");
155 >  nt.makeAngleBranch(angles);
156 >  nt.makeKinematicsBranch(kinematics);
157 >  nt.makeInfoBranch(evtinfo);
158 >  FOTuple foTree( nt.getFile(), (const char*)"fo");
159 >  foTree.makeSimpleLeptonBranch(failedLeptons);
160 >
161 >  TH1F * h_w_hpt;
162 >  if(ctrl.mc) {
163 >    nt.makeWeightBranch(weights);
164 >    //    nt.makeGenInfoBranch(geninfo);
165 >    initEfficiencyWeights();
166 >    /*
167 >    // Higgs only, pt reweighting
168 >    if( ctrl.mH <= 140 ) {
169 >      char wptname[256];
170 >      sprintf( wptname, "/data/blue/pharris/Flat/ntupler/root/weight_ptH_%i.root", ctrl.mH );
171 >      TFile * f_hpt = new TFile(wptname);
172 >      f_hpt->Print();
173 >      sprintf(wptname, "weight_hqt_fehipro_fit_%i", ctrl.mH);
174 >      h_w_hpt  = (TH1F*)(f_hpt->FindObjectAny(wptname));
175 >      h_w_hpt->Print();
176 >    }
177 >    */
178 >  } else {
179 >    initRunLumiRangeMap();
180 >  }
181  
182 +  //  initMuonIDMVA();
183 +  initMuonIsoMVA();
184 +  initElectronIDMVA();
185 +  initElectronIsoMVA();
186 +  initTrigger();
187 +  
188  
189 <  //
190 <  // Loop
191 <  //--------------------------------------------------------------------------------------------------------------
189 >  
190 >  //##########################################################################
191 >  // Setup tree I/O
192 >  //##########################################################################
193    
194    //
195    // Access samples and fill histograms
196    TFile *inputFile=0;
197    TTree *eventTree=0;  
198 <  
198 >  string currentFile("");
199 >
200    // Data structures to store info from TTrees
201 <  mithep::TEventInfo *info    = new mithep::TEventInfo();
202 <  TClonesArray *electronArr   = new TClonesArray("mithep::TElectron");
203 <  TClonesArray *muonArr       = new TClonesArray("mithep::TMuon");
201 >  mithep::EventHeader *info    = new mithep::EventHeader();
202 >  //  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
203 >  mithep::Array<mithep::Electron>             *electronArr   = new mithep::Array<mithep::Electron>();
204 >  mithep::Array<mithep::Muon>                 *muonArr       = new mithep::Array<mithep::Muon>();
205 >  mithep::Array<mithep::Vertex>               *vtxArr        = new mithep::Array<mithep::Vertex>();
206 >  mithep::Array<mithep::PFCandidate>          *pfArr         = new mithep::Array<mithep::PFCandidate>();
207 >  mithep::Array<mithep::PileupEnergyDensity>  *puArr         = new mithep::Array<mithep::PileupEnergyDensity>();
208 >  mithep::Array<mithep::Track>                *trkArr        = new mithep::Array<mithep::Track>();
209 >  mithep::TriggerMask                         *trigMask      = new mithep::TriggerMask();
210 >  mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
211 >  vector<string>                              *hltTableStrings  = new vector<string>();
212 >
213 >  TString fElectronName(Names::gkElectronBrn);
214 >  TString fMuonName(Names::gkMuonBrn);
215 >  TString fInfoName(Names::gkEvtHeaderBrn);
216 >  TString fPrimVtxName(Names::gkPVBrn);
217 >  TString fPFCandidateName(Names::gkPFCandidatesBrn);
218 >  TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
219 >  TString fTrackName(Names::gkTrackBrn);
220 >  TString fTriggerMaskName(Names::gkHltBitBrn);
221 >  TString fTriggerTableName(Names::gkHltTableBrn);
222    
223 +  chain->SetBranchAddress(fInfoName,        &info);
224 +  chain->SetBranchAddress(fElectronName,    &electronArr);
225 +  chain->SetBranchAddress(fMuonName,        &muonArr);
226 +  chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
227 +  chain->SetBranchAddress(fPFCandidateName, &pfArr);
228 +  chain->SetBranchAddress(fPileupEnergyDensityName, &puArr);
229 +  chain->SetBranchAddress(fTrackName, &trkArr);
230 +  chain->SetBranchAddress(fTriggerMaskName, &trigMask);
231 +  cout << "hlttable: " << fTriggerTableName << endl;
232 +  hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
233 +
234 +  mithep::Vertex              vtx;          // best primary vertex in the event
235 +
236 +  //  ginfo = NULL;
237 +  // if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
238 +
239 +  int count=0, pass=0;
240 +  float passcorr=0., passcorr_errup=0., passcorr_errdown=0.;
241 +  float denom[3]={0.,0.,0.};
242 +  float numer[3]={0.,0.,0.};
243 +  float numercorr[3]={0.,0.,0.};
244 +
245 +  // only 1 HLT table / file ???
246 +  hltchain->GetEntry(0);
247 +  cerr << "here... " << endl;
248 +
249 +  int imax = chain->GetEntries();
250 +  cout << "nEntries: " << imax << endl;
251 +
252 +
253 +  //##########################################################################
254 +  // Loop !!!!!!!!! should alternate events here and +1 in the training ...
255 +  //##########################################################################
256 +  for(UInt_t ientry=0; ientry<imax; ientry++)
257 +    {
258 +      chain->GetEntry(ientry);
259 +      if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
260 +
261 +      setPV( ctrl, vtxArr, vtx);
262 +
263 +      if(ctrl.mc) {
264 +        weights.w = getWeight(xstab,entrymap,chain);
265 +      } else {
266 +        if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
267 +          currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
268 +          hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
269 +          hltchain->GetEntry(0);
270 +          hltTable->Clear();
271 +          fillTriggerBits(hltTable, hltTableStrings );
272 +        }
273 +        if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
274 +        fillTriggerBits( hltTable, trigMask, triggerBits );
275 +      }
276 +      
277 +      
278 +      //
279 +      // trigger
280 +      //
281 +      if( !ctrl.mc ) {
282 +        if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
283 +          if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
284 +          continue;
285 +        }
286 +      }
287 +      
288 +      //
289 +      // lepton/kinematic selection ...
290 +      //
291 +      EventData ret4l =
292 +        apply_HZZ4L_reference_selection(ctrl, info,
293 +                              vtx,
294 +                              pfArr,
295 +                              puArr,
296 +                              electronArr,
297 +                              &electronReferencePreSelection,
298 +                              &electronReferenceIDMVASelection,
299 +                              &electronReferenceIsoSelection,
300 +                              muonArr,
301 +                              &muonReferencePreSelection,
302 +                              &muonIDPFSelection,
303 +                              &muonReferenceIsoSelection);
304 +        /*
305 +        apply_HZZ4L_reference_selection(ctrl, info,
306 +                              vtx,
307 +                              pfArr,
308 +                              puArr,
309 +                              electronArr,
310 +                              &electronPreSelection,
311 +                              &electronIDMVASelection,
312 +                              &electronIsoMVASelection,
313 +                              muonArr,
314 +                              &muonPreSelection,
315 +                              &muonIDPFSelection,
316 +                              &muonIsoMVASelection);
317 +        */
318 +
319 +      if( ctrl.debug ) cout << endl;
320 +
321 +      cout << "bits: " << ret4l.status.selectionBits << endl;
322 +      
323 +      if( ret4l.status.pass() ) {
324 +        
325 +        fillAngles(ret4l,angles);
326 +        fillKinematics(ret4l,kinematics);
327 +        fillEventInfo(info,evtinfo);
328 +        //if(ctrl.mc) fillGenInfo(ginfo,geninfo);
329 +        
330 +        /*
331 +        // only for Higgs < 140
332 +        geninfo.weight *= h_w_hpt->GetBinContent(h_w_hpt->FindBin(geninfo.pt_zz));
333 +        angles.bdt = reader->EvaluateMVA("BDTG");
334 +        */
335 +        nt.Fill();
336 +        
337 +        cerr  << "PASS:: "
338 +              << "\trun: " << evtinfo.run
339 +              << "\tevt: " << evtinfo.evt
340 +              << "\tlumi: " << evtinfo.lumi
341 +              << "\tcostheta1: " << angles.costheta1
342 +              << "\tcostheta2: " << angles.costheta2
343 +              << "\tcostheta*: " << angles.costhetastar
344 +              << "\tbdt: " << angles.bdt
345 +              << endl;
346 +        pass++;
347 +        //      if( pass > 3 ) break;
348 +
349 +      } else if( ret4l.status.selectionBits.test(8) ) { // save for fakes ...
350 +        cout << "failedLeptons : " << failedLeptons.size() << endl;
351 +        for( int f=0; f<failedLeptons.size(); f++ ) {
352 +          cout << "f: " << f << "\t"
353 +               << "type: " << failedLeptons[f].type << "\t"
354 +               << "pT: " << failedLeptons[f].vec->Pt()
355 +               << endl;
356 +        }
357 +        cout << endl;
358 + //      foTree.Fill();
359 + //      break;
360 +      }
361 +    }  
362  
363 <  TBranch *infoBr;
364 <  TBranch *electronBr;
365 <  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);
122 <
123 <
363 >  //  foTree.getTree()->Write();
364 >  nt.WriteClose();
365 > }
366  
367 <  cout << "nEntries: " << chain->GetEntries() << endl;
368 <  for(UInt_t ientry=0; ientry<chain->GetEntries(); ientry++) {          
367 > //----------------------------------------------------------------------------
368 > //
369 > // Get primary vertices
370 > // Assumes primary vertices are ordered by sum-pT^2 (as should be in CMSSW)
371 > // NOTE: if no PV is found from fitting tracks, the beamspot is used
372 > //
373 > //----------------------------------------------------------------------------
374 > bool setPV(ControlFlags ctrl,
375 >           const mithep::Array<mithep::Vertex> * vtxArr,
376 >           mithep::Vertex & vtx)
377 > //----------------------------------------------------------------------------
378 > {
379 >  const Vertex *bestPV = 0;
380 >  float best_sumpt=-1;
381 >
382 >  // good PV requirements
383 >  const UInt_t   fMinNTracksFit = 0;
384 >  const Double_t fMinNdof       = 4;
385 >  const Double_t fMaxAbsZ       = 24;
386 >  const Double_t fMaxRho        = 2;
387 >  
388 >  for(int i=0; i<vtxArr->GetEntries(); ++i) {
389 >    const Vertex *pv = (Vertex*)(vtxArr->At(i));
390 >    if( ctrl.debug ) cout << "vertex :: " << i << "\tntrks: " << pv->NTracks() << endl;
391      
392 <    chain->GetEntry(ientry);
393 <
394 < #ifdef THEIR_EVENTS
395 <    if( !( info->evtNum == 504867308  ||
396 <           info->evtNum == 368148849  ||
397 <           info->evtNum == 129514273  ||
398 <           info->evtNum == 286336207  ||
399 <           info->evtNum == 344708580  ||
400 <           info->evtNum == 30998576   ||
401 <           info->evtNum == 155679852  ||
402 <           info->evtNum == 394010457  ||
403 <           info->evtNum == 917379387  ||
404 <           info->evtNum == 78213037   ||
405 <           info->evtNum == 337493970  ||
406 <           info->evtNum == 1491724484 ||
407 <           info->evtNum == 480301165  ||
408 <           info->evtNum == 1038911933 ||
409 <           info->evtNum == 876658967  ||
410 <           info->evtNum == 966824024  ||
411 <           info->evtNum == 141954801  ||
412 <           info->evtNum == 160966858  ||
149 <           info->evtNum == 191231387  ||
150 <           info->evtNum == 66033190   ||
151 <           info->evtNum == 10347106   ||
152 <           info->evtNum == 107360878  ) ) continue;
153 < #endif
154 <
155 <    unsigned evtfail = fails_HZZ4L_selection(ctrl, info, electronArr, muonArr, passtuple );    
156 <    double eventweight = info->eventweight;
157 <    h_evtfail->Fill( evtfail, eventweight );
158 <    cout << endl << endl;  
159 <          
160 <  } //end loop over data    
161 <
162 <
163 <
164 <  delete info;
165 <  delete electronArr;
166 <  delete muonArr;
167 <
168 <
169 <
170 <  //--------------------------------------------------------------------------------------------------------------
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";
392 >    // Select best PV for corrected d0; if no PV passing cuts, the first PV in the collection will be used
393 >    //  if(!pv->IsValid()) continue;
394 >    if(pv->NTracksFit()       < fMinNTracksFit)       continue;
395 >    if(pv->Ndof()                 < fMinNdof)         continue;
396 >    if(fabs(pv->Z()) > fMaxAbsZ)                      continue;
397 >    if(pv->Position().Rho()   > fMaxRho)              continue;    
398 >    
399 >    // take the first ...
400 >    bestPV = pv;
401 >    break;
402 >
403 >    // this never reached ...    
404 >    float tmp_sumpt=0;
405 >    for( int t=0; t<pv->NTracks(); t++ )
406 >      tmp_sumpt += pv->Trk(t)->Pt();
407 >    
408 >    if( tmp_sumpt > best_sumpt  ) {
409 >      bestPV = pv;
410 >      best_sumpt = tmp_sumpt;
411 >      if( ctrl.debug) cout << "new PV set, pt : " << best_sumpt << endl;
412 >    }
413    }
414 <
415 <  TFile *file = new TFile(ofname, "RECREATE");
416 <  h_evt->Write();
417 <  h_evtfail->Write();
418 <  passtuple->Write();
419 <  file->Close();
185 <  delete file;
186 <
187 <  cerr << "done!" << endl;
188 < }
189 <
190 <
191 <
414 >  
415 >  if(!bestPV) bestPV = vtxArr->At(0);
416 >  vtx.SetPosition(bestPV->X(),bestPV->Y(),bestPV->Z());
417 >  vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
418 >  return true;
419 > };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines