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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines