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.12 by khahn, Sun Oct 30 15:35:25 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
23 < #include <TChain.h>                 //
24 < #include <TBranch.h>                // class to access branches in TTree
25 < #include <TClonesArray.h>           // ROOT array class
26 < #include <TCanvas.h>                // class for drawing
27 < #include <TH1F.h>                   // 1D histograms
28 < #include <TBenchmark.h>             // class to track macro running statistics
29 < #include <TLorentzVector.h>         // 4-vector class
30 < #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 "SiMVAElectronSelection.h"
37 < #include "RunLumiRangeMap.h"
38 < #include "EfficiencyWeightsInterface.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"
51 < #include "HZZLikelihoodElectronSelection.h"
52 < #include "HZZBDTElectronSelection.h"
53 < #include "SampleWeight.h"
54 <
55 < //#define BDTFAIL_THEIR_EVENTS
56 < //#define THEIR_EVENTS
57 <
58 < Double_t etaFromY(Double_t pt, Double_t y, Double_t phi, Double_t m)
60 < {
61 <  Double_t a  = (1+exp(2*y))/(exp(2*y)-1); // intermediate term
62 <  if(a*a<1) { cout << "a too small" << endl; assert(0); }
63 <  Double_t E  = sqrt( a*a*(pt*pt+m*m)/(a*a-1) );
64 <  Double_t pz = E*E - pt*pt - m*m;
65 <  if(pz<0) { cout << "imag. pz" << endl; assert(0); }
66 <  pz = sqrt(pz);
67 <  if(y<0) pz *= -1;
68 <  TLorentzVector v;
69 <  v.SetPxPyPzE(pt*cos(phi),pt*sin(phi),pz,E);
70 <  Double_t th = v.Theta();
71 <  return -log(tan(th/2));
72 < };
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 < int getChannel(mithep::TGenInfo * ginfo) {
63 > #ifndef CMSSW_BASE
64 > #define CMSSW_BASE "../"
65 > #endif
66  
67 <    int gchannel=-1;
67 > using namespace mithep;
68  
79    if( abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kElectron )  gchannel=0;
80    else if( abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kMuon ) gchannel=1;
81    else if( (abs(ginfo->id_1_a) == EGenType::kElectron && abs(ginfo->id_1_b) == EGenType::kMuon) ||
82             (abs(ginfo->id_1_a) == EGenType::kMuon && abs(ginfo->id_1_b) == EGenType::kElectron) ) gchannel=2;
83    
84    return gchannel;
85 }
69  
70 + //----------------------------------------------------------------------------
71 + bool setPV(ControlFlags,const mithep::Array<mithep::Vertex>*, mithep::Vertex& );
72 + //----------------------------------------------------------------------------
73  
88
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  
111  map<string,double> entrymap; // number of unskimmed entries in each file
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 <    entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
114 <    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  // table of cross section values  
127  SimpleTable xstab("./data/xs.dat");
128
129  //
130  // Setup
131  //--------------------------------------------------------------------------------------------------------------
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 <  TFile *file = new TFile(ofname, "RECREATE");
133 <  TH1F * h_evt = new TH1F("hevt", "hevt", 2, 0, 2 );
134 <  TH1F * h_evtfail = new TH1F("hevtfail", "hevtfail", 1024, 0, 1024 );
135 <  TTree * passtuple = new TTree("passtuple", "passtuple" );
142 <  unsigned run, evt, lumi, channel, gchannel;
143 <  float mZ1, mZ2, m4l, pt4l;
144 <  unsigned tZ1, tZ2;
145 <  double w, woff, werroff, won, werron;
146 <  if( ctrl.mc )   {
147 <    passtuple->Branch("gchannel", &gchannel);
148 <    passtuple->Branch("woff", &woff);
149 <    passtuple->Branch("werroff", &werroff);
150 <    passtuple->Branch("won", &won);
151 <    passtuple->Branch("werron", &werron);
152 <  }
153 <  passtuple->Branch("channel", &channel);
154 <  passtuple->Branch("run", &run);
155 <  passtuple->Branch("evt", &evt);
156 <  passtuple->Branch("lumi", &lumi);
157 <  passtuple->Branch("mZ1", &mZ1);
158 <  passtuple->Branch("mZ2", &mZ2);
159 <  passtuple->Branch("tZ1", &tZ1);
160 <  passtuple->Branch("tZ2", &tZ2);
161 <  passtuple->Branch("m4l", &m4l);
162 <  passtuple->Branch("pt4l", &pt4l);
163 <  passtuple->Branch("w", &w);
164 <
165 <  initCiCSelection();
166 <  if(ctrl.eleSele=="lik") initLikSelection();
167 <  if(ctrl.eleSele=="bdt") initBDTSelection();
168 <  // if(ctrl.eleSele=="si" ) initSiMVAElectronSelection();
169 <  initRunLumiRangeMap();
170 <  initEfficiencyWeights();
171 <
172 <
132 >  // table of cross section values  
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 <  // Loop
139 >  // Setup
140    //--------------------------------------------------------------------------------------------------------------
141 <  if(ctrl.fakeScheme!="none") {cout << "wrong executable!" << endl; assert(0);}
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 >  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 <  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
193 <  TClonesArray *electronArr   = new TClonesArray("mithep::TElectron");
194 <  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 <  chain->SetBranchAddress("Info",       &info);
193 <  chain->SetBranchAddress("Electron", &electronArr);
194 <  chain->SetBranchAddress("Muon", &muonArr);
195 <  if( ctrl.mc ) { chain->SetBranchAddress("Gen",        &ginfo);}
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 <  UInt_t imax = chain->GetEntries();
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 <  for(UInt_t ientry=0; ientry<imax; ientry++) {
244 <    chain->GetEntry(ientry);
245 <    if(!(ientry%100000)) cerr << "entry: " << ientry << endl;
246 <
247 <    // get event weight for this file                                                    
248 <    double eventweight=1;
249 <    if(ctrl.mc) eventweight = getWeight(xstab,entrymap,chain);
250 <
251 < #ifdef THEIR_EVENTS
252 <    if( !( info->evtNum == 504867308  ||
253 <           info->evtNum == 368148849  ||
254 <           //      info->evtNum == 129514273  ||
255 <           info->evtNum == 286336207  ||
256 <           info->evtNum == 344708580  ||
257 <           info->evtNum == 30998576   ||
258 <           info->evtNum == 155679852  ||
259 <           info->evtNum == 394010457  ||
260 <           info->evtNum == 917379387  ||
261 <           info->evtNum == 78213037   ||
262 <           info->evtNum == 862270386  ||
263 <           info->evtNum == 337493970  ||  // not baseline anymore?
264 <           info->evtNum == 1491724484 ||
265 <           info->evtNum == 480301165  ||
266 <           info->evtNum == 1038911933 ||
267 <           info->evtNum == 876658967  ||
268 <           info->evtNum == 966824024  ||
269 <           info->evtNum == 141954801  ||
270 <           info->evtNum == 160966858  ||
271 <           info->evtNum == 191231387  ||
272 <           info->evtNum == 66033190   ||
273 <           info->evtNum == 10347106   ||
274 <           info->evtNum == 107360878  ||
275 <           info->evtNum == 2554393033 ||
276 <           info->evtNum == 933807102  ||
277 <           info->evtNum == 1188043146 ||
278 <           info->evtNum == 559839432  ||
279 <           info->evtNum == 16706390   ||
280 <           info->evtNum == 65557571   ||
281 <           info->evtNum == 389185367  ||
282 <           info->evtNum == 2722114329 ) ) continue;
283 < #endif
284 <
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 < #ifdef BDTFAIL_THEIR_EVENTS
327 <    if( !( info->evtNum == 78213037   ||
252 <           info->evtNum == 876658967  ) ) continue;
253 <          
254 < #endif
326 >      }
327 >    }  
328  
329 <    unsigned evtfail = fails_HZZ4L_selection(ctrl, info, electronArr, muonArr, eventweight, passtuple, NULL);
330 <    //    if( !evtfail ) {
258 < //       cout << "run: " << info->runNum
259 < //         << "\tevt: " << info->evtNum
260 < //         << "\tlumi: " << info->lumiSec
261 < //         << "\tfile: " << chain->GetFile()->GetEndpointUrl()->GetFile()
262 < //         << endl;
263 < //    }
329 >  nt.WriteClose();
330 > }
331  
332 < #ifdef THEIR_EVENTS
333 <    if(evtfail!=0) cout << "Failed!" << endl;
334 < #endif
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 <    h_evtfail->Fill( evtfail, eventweight );
358 <
359 <    if( ctrl.mc ) {
360 <      // BR is implicit here ...
361 <      int gchannel = getChannel(ginfo);
362 <        if( gchannel >= 0 ) {
363 < //        denom[gchannel] += eventweight;
364 < //        if( !evtfail ) numer[gchannel] += eventweight;
365 <          denom[gchannel] += 1;
366 <          if( !evtfail ) numer[gchannel] += 1;
367 <
368 <          // get eff weight for current evt
369 <          if( !evtfail ) {
370 <            double thewoff, thewon;
371 <            passtuple->SetBranchAddress("woff", &thewoff); // must put this here ...
372 <            passtuple->SetBranchAddress("won",  &thewon); // must put this here ...
373 <            //      cout << "thewoff: " << thewoff << "\tthewon: " << thewon << endl;
374 <            int ret = passtuple->GetEntry(passtuple->GetEntries()-1);
375 <            numercorr[gchannel] += thewoff*thewon;        
376 <          }
289 <        }
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      }
291
292    count++;
293    if( !evtfail ) pass++;
294          
295  } //end loop over data    
296
297  if( ctrl.mc ) {
298    cout << "--------------" << endl;
299    cout << "denom: " << count << endl;
300    cout << "pass: " << pass << endl;
301    cout << "axe: " << (float)pass/count << endl;
302    cout << "--------------" << endl;
378    }
379 <
380 <  delete info;
381 <  delete electronArr;
382 <  delete muonArr;
383 <
384 <
310 <
311 <  //--------------------------------------------------------------------------------------------------------------
312 <  // Save Histograms;
313 <  //==============================================================================================================
314 <  // const char * ofname;
315 <  // if( strcmp( ctrl.outputfile.c_str(), "") ) {
316 <  //   ofname = ctrl.outputfile.c_str();
317 <  // } else {
318 <  //   ofname = "tmp.root";
319 <  // }
320 <
321 <  // TFile *file = new TFile(ofname, "RECREATE");
322 <  gROOT->cd(file->GetPath());
323 <  h_evt->Write();
324 <  h_evtfail->Write();
325 <  passtuple->Write();
326 <  file->Close();
327 <  delete file;
328 <
329 <  // map<TString,TMVA::Reader*>::iterator it;
330 <  // for(it=readers.begin(); it!=readers.end(); it++) delete (*it).second;
331 <
332 <
333 <  float acc_4e        = numer[0]/denom[0];
334 <  float acc_4mu       = numer[1]/denom[1];
335 <  float acc_2e2mu     = numer[2]/denom[2];
336 <  float acc_err_4e    = (1./denom[0])*sqrt(numer[0]*(1-acc_4e));
337 <  float acc_err_4mu   = (1./denom[1])*sqrt(numer[1]*(1-acc_4mu));
338 <  float acc_err_2e2mu = (1./denom[2])*sqrt(numer[2]*(1-acc_2e2mu));
339 <
340 <  float acc_c_4e        = numercorr[0]/denom[0];
341 <  float acc_c_4mu       = numercorr[1]/denom[1];
342 <  float acc_c_2e2mu     = numercorr[2]/denom[2];
343 <  float acc_c_err_4e    = (1./denom[0])*sqrt(numer[0]*(1-acc_4e));
344 <  float acc_c_err_4mu   = (1./denom[1])*sqrt(numer[1]*(1-acc_4mu));
345 <  float acc_c_err_2e2mu = (1./denom[2])*sqrt(numer[2]*(1-acc_2e2mu));
346 <
347 <  if( ctrl.mc ) {
348 <  cerr << "---------------------" << endl;
349 < //   cerr << "acc_4e: " << fixed << setw(8) << setprecision(7) << numer[0] << endl; // numer already weighted ...
350 < //   cerr << "acc_4mu: " << fixed << setw(8) << setprecision(7) << numer[1] <<  endl;
351 < //   cerr << "acc_2e2mu: " << fixed << setw(8) << setprecision(7) << numer[2] << endl;
352 <  cerr << "acc_4e: " << fixed << setw(8) << setprecision(7) << acc_4e << " +/- " << acc_err_4e << endl;
353 <  cerr << "acc_4mu: " << fixed << setw(8) << setprecision(7) << acc_4mu << " +/- " << acc_err_4mu << endl;
354 <  cerr << "acc_2e2mu: " << fixed << setw(8) << setprecision(7) << acc_2e2mu << " +/- " << acc_err_2e2mu << endl;
355 <  cerr << "-------corrected------" << endl;
356 <  cerr << "acc_c_4e: " << fixed << setw(8) << setprecision(7) << acc_c_4e << " +/- " << acc_c_err_4e << endl;
357 <  cerr << "acc_c_4mu: " << fixed << setw(8) << setprecision(7) << acc_c_4mu << " +/- " << acc_c_err_4mu << endl;
358 <  cerr << "acc_c_2e2mu: " << fixed << setw(8) << setprecision(7) << acc_c_2e2mu << " +/- " << acc_c_err_2e2mu << endl;
359 <  cerr << "---------------------" << endl;
360 < }
361 <
362 <  cerr << "done!" << endl;
363 < }
364 <
365 <
366 <
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