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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines