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.15 by khahn, Sun May 6 23:51:01 2012 UTC vs.
Revision 1.16 by khahn, Wed May 9 14:57:20 2012 UTC

# Line 34 | Line 34 | using namespace std;
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"
# Line 57 | Line 59 | using namespace std;
59   #include "InfoStruct.h"
60   //#include "GenInfoStruct.h"
61   #include "WeightStruct.h"
62 < #include "GlobalDataAndFuncs.h"
62 > //#include "GlobalDataAndFuncs.h"
63 > #include "RunLumiRangeMap.h"
64  
65   #include "AngleTuple.h"
66   #include "FOTuple.h"
# Line 65 | Line 68 | using namespace std;
68   #include "SampleWeight.h"
69   #include "EfficiencyWeightsInterface.h"
70  
71 + #include "SimpleLepton.h"
72 +
73   #ifndef CMSSW_BASE
74   #define CMSSW_BASE "../"
75   #endif
76  
77   using namespace mithep;
78  
79 < #include "SimpleLepton.h"
80 < vector<SimpleLepton> failedLeptons; // for fake estimation
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 < //=== MAIN =================================================================================================
97 >
98 > //
99 > // MAIN
100 > //----------------------------------------------------------------------------
101   int main(int argc, char** argv)
102 + //----------------------------------------------------------------------------
103   {
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,double> entrymap; // number of unskimmed entries in each file
109 >  map<string,unsigned> entrymap; // number of unskimmed entries in each file
110  
111    //
112    // args
# Line 119 | Line 139 | int main(int argc, char** argv)
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    }
# Line 150 | Line 174 | int main(int argc, char** argv)
174    WeightStruct weights;
175    //  GenInfoStruct geninfo;
176  
153  
177    AngleTuple nt( (const char*)ofname, (const char*)"zznt");
178    nt.makeAngleBranch(angles);
179    nt.makeKinematicsBranch(kinematics);
180    nt.makeInfoBranch(evtinfo);
181    FOTuple foTree( nt.getFile(), (const char*)"fo");
182 <  foTree.makeSimpleLeptonBranch(failedLeptons);
182 >  foTree.makeFailedLeptonBranch(failingLeptons);
183 >  foTree.makeZLeptonBranch(passingLeptons);
184  
185    TH1F * h_w_hpt;
186    if(ctrl.mc) {
187      nt.makeWeightBranch(weights);
188      //    nt.makeGenInfoBranch(geninfo);
189      initEfficiencyWeights();
190 +    initPUWeights();
191      /*
192      // Higgs only, pt reweighting
193      if( ctrl.mH <= 140 ) {
# Line 187 | Line 212 | int main(int argc, char** argv)
212    
213  
214    
190  //##########################################################################
191  // Setup tree I/O
192  //##########################################################################
193  
215    //
216 <  // Access samples and fill histograms
216 >  // Setup tree I/O
217 >  //--------------------------------------------------------------------------------------------------------------
218    TFile *inputFile=0;
219    TTree *eventTree=0;  
220    string currentFile("");
221  
200  // Data structures to store info from TTrees
222    mithep::EventHeader *info    = new mithep::EventHeader();
223    //  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
224    mithep::Array<mithep::Electron>             *electronArr   = new mithep::Array<mithep::Electron>();
225    mithep::Array<mithep::Muon>                 *muonArr       = new mithep::Array<mithep::Muon>();
226    mithep::Array<mithep::Vertex>               *vtxArr        = new mithep::Array<mithep::Vertex>();
227    mithep::Array<mithep::PFCandidate>          *pfArr         = new mithep::Array<mithep::PFCandidate>();
228 <  mithep::Array<mithep::PileupEnergyDensity>  *puArr         = new mithep::Array<mithep::PileupEnergyDensity>();
228 >  mithep::Array<mithep::PileupInfo>           *puArr         = new mithep::Array<mithep::PileupInfo>();
229 >  mithep::Array<mithep::PileupEnergyDensity>  *puDArr        = new mithep::Array<mithep::PileupEnergyDensity>();
230    mithep::Array<mithep::Track>                *trkArr        = new mithep::Array<mithep::Track>();
231    mithep::TriggerMask                         *trigMask      = new mithep::TriggerMask();
232    mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
# Line 215 | Line 237 | int main(int argc, char** argv)
237    TString fInfoName(Names::gkEvtHeaderBrn);
238    TString fPrimVtxName(Names::gkPVBrn);
239    TString fPFCandidateName(Names::gkPFCandidatesBrn);
240 +  TString fPileupInfoName(Names::gkPileupInfoBrn);
241    TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
242    TString fTrackName(Names::gkTrackBrn);
243    TString fTriggerMaskName(Names::gkHltBitBrn);
# Line 225 | Line 248 | int main(int argc, char** argv)
248    chain->SetBranchAddress(fMuonName,        &muonArr);
249    chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
250    chain->SetBranchAddress(fPFCandidateName, &pfArr);
251 <  chain->SetBranchAddress(fPileupEnergyDensityName, &puArr);
251 >  chain->SetBranchAddress(fPileupInfoName,  &puArr);
252 >  chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
253    chain->SetBranchAddress(fTrackName, &trkArr);
254    chain->SetBranchAddress(fTriggerMaskName, &trigMask);
255    cout << "hlttable: " << fTriggerTableName << endl;
# Line 250 | Line 274 | int main(int argc, char** argv)
274    cout << "nEntries: " << imax << endl;
275  
276  
277 <  //##########################################################################
278 <  // Loop !!!!!!!!! should alternate events here and +1 in the training ...
279 <  //##########################################################################
277 >  //
278 >  // Loop !!!!!!!!!
279 >  //--------------------------------------------------------------------------------------------------------------
280    for(UInt_t ientry=0; ientry<imax; ientry++)
281      {
282        chain->GetEntry(ientry);
283        if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
284 <
284 >      if( ientry>100 )break;
285        setPV( ctrl, vtxArr, vtx);
286  
287 +
288 +      //
289 +      // data/MC
290 +      //
291        if(ctrl.mc) {
292 <        weights.w = getWeight(xstab,entrymap,chain);
292 >        //
293 >        // xsec & PU weights
294 >        //
295 >        weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
296 >        int npu = -1;
297 >        for(int i=0;i<puArr->GetEntries();i++) {
298 >          if(puArr->At(i)->GetBunchCrossing() == 0)
299 >            npu = puArr->At(i)->GetPU_NumInteractions();
300 >        }
301 >        assert(npu>=0);
302 >        evtinfo.npu;
303 >        weights.npuw = getPUWeight(evtinfo.npu);
304 >        //      cout << "weight: " << weights.w << endl;
305        } else {
306 +        //
307 +        // JSON
308 +        //
309 +        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
310 +        if( !(rlrm.HasRunLumi(rl)) )  {
311 +          if( ctrl.debug ) cout << "\tfails JSON" << endl;
312 +          continue;
313 +        }
314 +        //
315 +        // trigger
316 +        //
317          if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
318            currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
319            hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
# Line 272 | Line 323 | int main(int argc, char** argv)
323          }
324          if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
325          fillTriggerBits( hltTable, trigMask, triggerBits );
275      }
276      
277      
278      //
279      // trigger
280      //
281      if( !ctrl.mc ) {
326          if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
327            if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
328            continue;
329          }
330        }
331 <      
331 >
332        //
333        // lepton/kinematic selection ...
334        //
335 +      failingLeptons.clear();
336 +      passingLeptons.clear();
337        EventData ret4l =
338          apply_HZZ4L_reference_selection(ctrl, info,
339                                vtx,
340                                pfArr,
341 <                              puArr,
341 >                              puDArr,
342                                electronArr,
343                                &electronReferencePreSelection,
344                                &electronReferenceIDMVASelection,
# Line 318 | Line 364 | int main(int argc, char** argv)
364  
365        if( ctrl.debug ) cout << endl;
366  
367 <      cout << "bits: " << ret4l.status.selectionBits << endl;
367 >      //      cout << "bits: " << ret4l.status.selectionBits << endl;
368        
369        if( ret4l.status.pass() ) {
370          
371          fillAngles(ret4l,angles);
372          fillKinematics(ret4l,kinematics);
373          fillEventInfo(info,evtinfo);
374 <        //if(ctrl.mc) fillGenInfo(ginfo,geninfo);
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
# Line 346 | Line 399 | int main(int argc, char** argv)
399          pass++;
400          //      if( pass > 3 ) break;
401  
402 <      } else if( ret4l.status.selectionBits.test(8) ) { // save for fakes ...
403 <        cout << "failedLeptons : " << failedLeptons.size() << endl;
404 <        for( int f=0; f<failedLeptons.size(); f++ ) {
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()
409 >               << endl;
410 >        }
411 >        /*
412 >        cout << "passingLeptons : " << passingLeptons.GetSize() << endl;
413 >        for( int f=0; f<passingLeptons.GetSize(); f++ ) {
414            cout << "f: " << f << "\t"
415 <               << "type: " << failedLeptons[f].type << "\t"
416 <               << "pT: " << failedLeptons[f].vec->Pt()
415 >               << "type: " << passingLeptons.At(f).type << "\t"
416 >               << "pT: " << passingLeptons.At(f).vec->Pt()
417                 << endl;
418          }
419 +        */
420          cout << endl;
421 < //      foTree.Fill();
359 < //      break;
421 >        foTree.Fill();
422        }
423      }  
424  
425 <  //  foTree.getTree()->Write();
425 >  
426 >  foTree.getFile()->cd();
427 >  foTree.getTree()->Write();
428    nt.WriteClose();
429   }
430  
# Line 417 | Line 481 | bool setPV(ControlFlags ctrl,
481    vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
482    return true;
483   };
484 +
485 +
486 +
487 + //----------------------------------------------------------------------------
488 + void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
489 + //----------------------------------------------------------------------------
490 + {
491 +  vector<SimpleLepton> lepvec = evtdat.Z1leptons;
492 +  lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
493 +  double w_offline=-1, werr_offline=0;
494 +  double w_online=-1, werr_online=0;
495 +  
496 +  vector< pair <double,double> > wlegs; // pair here is eff & err
497 +  vector< pair <float,float> > mvec;  // pair here is eta & pt
498 +
499 +  //      vector< pair <float,float> > evec;  // pair here is eta & pt
500 +  // now deal with medium vs loose
501 +  vector< pair< bool, pair <float,float> > > evec;  // pair here is eta & pt
502 +  
503 +  for( int k=0; k<lepvec.size(); k++ ) {
504 +    if( abs(lepvec[k].type) == 13 ) {
505 +      mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec->Eta()), lepvec[k].vec->Pt()) );
506 +      wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec->Eta()),
507 +                                                          lepvec[k].vec->Pt() ) );
508 +    } else {
509 +      
510 +      // now deal with medium vs loose
511 +      //              evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
512 +      
513 +      std::pair<float,float> tmppair(fabs(lepvec[k].vec->Eta()), lepvec[k].vec->Pt());
514 +      evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
515 +      
516 +      //              wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec->Eta()),
517 +      //                                                                 lepvec[k].vec->Pt() ) );
518 +      
519 +      wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec->Eta()),
520 +                                                          lepvec[k].vec->Pt(),
521 +                                                          lepvec[k].isTight ) );
522 +      
523 +    }
524 +  }
525 +  
526 +  pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
527 +  weights.woff    = offpair.first;
528 +  weights.werroff = offpair.second;
529 +  
530 +  pair<double,double> onpair  = getOnlineEfficiencyWeight( mvec, evec );
531 +  weights.won    = onpair.first;
532 +  weights.werron = onpair.second;
533 + }
534 +
535 +
536 + //----------------------------------------------------------------------------
537 + void initRunLumiRangeMap()
538 + //----------------------------------------------------------------------------
539 + {
540 +  /*
541 +  rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
542 +  //  rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
543 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
544 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));  
545 +  rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));  
546 +  // r11b
547 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));  
548 +  */
549 + };
550 +
551 +
552 + //----------------------------------------------------------------------------
553 + void initPUWeights()
554 + //----------------------------------------------------------------------------
555 + {
556 +  TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
557 +  hpu = (TH1D*)(puf->Get("puWeights"));
558 + }
559 +
560 + //----------------------------------------------------------------------------
561 + double getPUWeight(unsigned npu)
562 + //----------------------------------------------------------------------------
563 + {
564 +  return hpu->GetBinContent(hpu->FindBin(npu));
565 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines