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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines