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.14 by khahn, Sat May 5 21:43:55 2012 UTC vs.
Revision 1.17 by khahn, Thu May 10 00:14:35 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"
67  
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 + //
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 99 | Line 123 | int main(int argc, char** argv)
123        return 1;
124      }
125    ctrl.dump();
102  assert( ctrl.mH != 0 );
126  
127  
128  
# Line 116 | 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 152 | Line 179 | int main(int argc, char** argv)
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 ) {
# Line 179 | Line 211 | int main(int argc, char** argv)
211    initElectronIsoMVA();
212    initTrigger();
213    
214 <  
183 <  //##########################################################################
184 <  // Setup tree I/O
185 <  //##########################################################################
214 >
215    
216    //
217 <  // Access samples and fill histograms
217 >  // Setup tree I/O
218 >  //--------------------------------------------------------------------------------------------------------------
219    TFile *inputFile=0;
220    TTree *eventTree=0;  
221    string currentFile("");
222  
193  // Data structures to store info from TTrees
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::PileupEnergyDensity>  *puArr         = new mithep::Array<mithep::PileupEnergyDensity>();
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();
# Line 208 | Line 238 | int main(int argc, char** argv)
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);
# Line 218 | Line 249 | int main(int argc, char** argv)
249    chain->SetBranchAddress(fMuonName,        &muonArr);
250    chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
251    chain->SetBranchAddress(fPFCandidateName, &pfArr);
252 <  chain->SetBranchAddress(fPileupEnergyDensityName, &puArr);
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;
# Line 243 | Line 275 | int main(int argc, char** argv)
275    cout << "nEntries: " << imax << endl;
276  
277  
278 <  //##########################################################################
279 <  // Loop !!!!!!!!! should alternate events here and +1 in the training ...
280 <  //##########################################################################
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 <
285 >      if( ientry > 100) break;
286        setPV( ctrl, vtxArr, vtx);
287  
288 +
289 +      //
290 +      // data/MC
291 +      //
292        if(ctrl.mc) {
293 <        weights.w = getWeight(xstab,entrymap,chain);
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);
# Line 265 | Line 324 | int main(int argc, char** argv)
324          }
325          if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
326          fillTriggerBits( hltTable, trigMask, triggerBits );
268      }
269      
270      
271      //
272      // trigger
273      //
274      if( !ctrl.mc ) {
327          if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
328            if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
329            continue;
330          }
331        }
332 <      
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 <                              puArr,
342 >                              puDArr,
343                                electronArr,
344                                &electronReferencePreSelection,
345                                &electronReferenceIDMVASelection,
# Line 294 | Line 348 | int main(int argc, char** argv)
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;
368 >      //      cout << "bits: " << ret4l.status.selectionBits << endl;
369        
370        if( ret4l.status.pass() ) {
302        
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 324 | Line 399 | int main(int argc, char** argv)
399          pass++;
400          //      if( pass > 3 ) break;
401  
402 <      }
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  
# Line 383 | Line 482 | bool setPV(ControlFlags ctrl,
482    vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
483    return true;
484   };
485 +
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 +  //      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 +  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