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.19 by khahn, Thu May 10 22:36:12 2012 UTC

# Line 5 | Line 5
5   #include <iostream>                 // standard I/O
6   #include <iomanip>                  // functions to format standard I/O
7   #include <bitset>  
8 + #include <map>  
9   using namespace std;
10  
11   //
# Line 34 | Line 35 | using namespace std;
35   #include "Vertex.h"
36   #include "PFCandidate.h"
37   #include "PFCandidateCol.h"
38 + #include "PileupInfoCol.h"
39 + #include "PileupEnergyDensity.h"
40   #include "TriggerMask.h"
41   #include "TriggerTable.h"
42   #include "Names.h"
# Line 48 | Line 51 | using namespace std;
51   #include "IsolationSelection.h"
52  
53   //#include "Selection.h"
54 < #include "ReferenceSelection.h"
54 > //#include "ReferenceSelection.h"
55 > #include "ReferenceSelection2.h"
56  
57   #include "TriggerUtils.h"
58   #include "PassHLT.h"
# Line 57 | Line 61 | using namespace std;
61   #include "InfoStruct.h"
62   //#include "GenInfoStruct.h"
63   #include "WeightStruct.h"
64 < #include "GlobalDataAndFuncs.h"
64 > //#include "GlobalDataAndFuncs.h"
65 > #include "RunLumiRangeMap.h"
66 >
67   #include "AngleTuple.h"
68 + #include "FOTuple.h"
69  
70   #include "SampleWeight.h"
71   #include "EfficiencyWeightsInterface.h"
72  
73 + #include "SimpleLepton.h"
74 +
75   #ifndef CMSSW_BASE
76   #define CMSSW_BASE "../"
77   #endif
78  
79   using namespace mithep;
80  
81 + //
82 + // globals
83 + //----------------------------------------------------------------------------
84 + TH1D * hpu;
85 + RunLumiRangeMap rlrm;
86 + vector<SimpleLepton> failingLeptons ; // for fake estimation
87 + vector<SimpleLepton> passingLeptons;      // for fake estimation
88 + vector<unsigned> cutvec;
89 + vector<vector<unsigned> > zcutvec;
90 + vector<vector<unsigned> > zzcutvec;
91 + map<unsigned,float>       evtrhoMap;
92 + vector<string> cutstrs;
93  
94 + //
95 + // prototypes
96   //----------------------------------------------------------------------------
97   bool setPV(ControlFlags,const mithep::Array<mithep::Vertex>*, mithep::Vertex& );
98 + void initPUWeights();
99 + double getPUWeight(unsigned npu);
100 + void setEffiencyWeights(EventData&, WeightStruct& );
101 + void initRunLumiRangeMap();
102 + void initEvtRhoMap(map<unsigned,float> &);
103   //----------------------------------------------------------------------------
104  
105 < //=== MAIN =================================================================================================
105 >
106 > //
107 > // MAIN
108 > //----------------------------------------------------------------------------
109   int main(int argc, char** argv)
110 + //----------------------------------------------------------------------------
111   {
112 +  cutstrs.push_back(string("skim0"));              //0
113 +  cutstrs.push_back(string("skim1"));              //1
114 +  // -------------------------------------------------
115 +  cutstrs.push_back(string("Z candidate"));        //2
116 +  cutstrs.push_back(string("good Z1"));            //3
117 +  // -------------------------------------------------
118 +  cutstrs.push_back(string("4l"));                 //4
119 +  cutstrs.push_back(string("ZZ candidate"));       //5
120 +  cutstrs.push_back(string("good Z2"));            //6
121 +  cutstrs.push_back(string("ZZ 20/10"));           //7
122 +  cutstrs.push_back(string("resonance"));          //8
123 +  cutstrs.push_back(string("m4l>70"));             //9
124 +  cutstrs.push_back(string("m4l>100"));            //10
125 +
126 +
127 +
128 +
129    string cmsswpath(CMSSW_BASE);
130    cmsswpath.append("/src");
131    std::bitset<1024> triggerBits;
132    vector<vector<string> > inputFiles;
133    inputFiles.push_back(vector<string>());
134 <  map<string,double> entrymap; // number of unskimmed entries in each file
134 >  map<string,unsigned> entrymap; // number of unskimmed entries in each file
135 >  for( int i=0; i<11; i++ ) cutvec.push_back(0);
136 >  for( int i=0; i<2; i++ )  {
137 >    zcutvec.push_back(vector<unsigned>());
138 >    for( int j=0; j<11; j++ ) zcutvec[i].push_back(0);
139 >  }
140 >  for( int i=0; i<3; i++ )  {
141 >    zzcutvec.push_back(vector<unsigned>());
142 >    for( int j=0; j<11; j++ ) zzcutvec[i].push_back(0);
143 >  }
144 >
145  
146    //
147    // args
# Line 99 | Line 158 | int main(int argc, char** argv)
158        return 1;
159      }
160    ctrl.dump();
102  assert( ctrl.mH != 0 );
161  
162  
163  
# Line 116 | Line 174 | int main(int argc, char** argv)
174        if( !(strncmp( fname.c_str(), "#", 1 ) ) ) continue; // skip commented lines
175        cout << "adding inputfile : " << fname.c_str() << endl;
176        entrymap[string(fname.c_str())] = unskimmedEntries(fname.c_str());
177 +      cout << "unskimmed entries: " << entrymap[string(fname.c_str())] << endl;
178        chain->AddFile(fname.c_str());
179        hltchain->AddFile(fname.c_str());
180      }
181    } else {
182      cout << "adding inputfile : " << ctrl.inputfile.c_str() << endl;
183 +    unsigned tmpent = unskimmedEntries(ctrl.inputfile.c_str());
184 +    cout << "tmpent: " << tmpent << endl;
185      entrymap[string(ctrl.inputfile.c_str())] = unskimmedEntries(ctrl.inputfile.c_str());
186 +    cout << "unskimmed entries: " << entrymap[string(ctrl.inputfile.c_str())] << endl;
187      chain->AddFile(ctrl.inputfile.c_str());
188      hltchain->AddFile(ctrl.inputfile.c_str());
189    }
# Line 152 | Line 214 | int main(int argc, char** argv)
214    nt.makeKinematicsBranch(kinematics);
215    nt.makeInfoBranch(evtinfo);
216  
217 +  FOTuple foTree( nt.getFile(), (const char*)"FOtree");
218 +  foTree.makeFailedLeptonBranch(failingLeptons);
219 +  foTree.makeZLeptonBranch(passingLeptons);
220 +
221    TH1F * h_w_hpt;
222    if(ctrl.mc) {
223      nt.makeWeightBranch(weights);
224      //    nt.makeGenInfoBranch(geninfo);
225      initEfficiencyWeights();
226 +    initPUWeights();
227      /*
228      // Higgs only, pt reweighting
229      if( ctrl.mH <= 140 ) {
# Line 178 | Line 245 | int main(int argc, char** argv)
245    initElectronIDMVA();
246    initElectronIsoMVA();
247    initTrigger();
248 <  
249 <  
250 <  //##########################################################################
251 <  // Setup tree I/O
185 <  //##########################################################################
248 >
249 >  // hack
250 >  initEvtRhoMap(evtrhoMap);
251 >
252    
253    //
254 <  // Access samples and fill histograms
254 >  // Setup tree I/O
255 >  //--------------------------------------------------------------------------------------------------------------
256    TFile *inputFile=0;
257    TTree *eventTree=0;  
258    string currentFile("");
259  
193  // Data structures to store info from TTrees
260    mithep::EventHeader *info    = new mithep::EventHeader();
261    //  mithep::TGenInfo    *ginfo  = new mithep::TGenInfo();
262    mithep::Array<mithep::Electron>             *electronArr   = new mithep::Array<mithep::Electron>();
263    mithep::Array<mithep::Muon>                 *muonArr       = new mithep::Array<mithep::Muon>();
264    mithep::Array<mithep::Vertex>               *vtxArr        = new mithep::Array<mithep::Vertex>();
265    mithep::Array<mithep::PFCandidate>          *pfArr         = new mithep::Array<mithep::PFCandidate>();
266 <  mithep::Array<mithep::PileupEnergyDensity>  *puArr         = new mithep::Array<mithep::PileupEnergyDensity>();
266 >  mithep::Array<mithep::PileupInfo>           *puArr         = new mithep::Array<mithep::PileupInfo>();
267 >  mithep::Array<mithep::PileupEnergyDensity>  *puDArr        = new mithep::Array<mithep::PileupEnergyDensity>();
268    mithep::Array<mithep::Track>                *trkArr        = new mithep::Array<mithep::Track>();
269    mithep::TriggerMask                         *trigMask      = new mithep::TriggerMask();
270    mithep::TriggerTable                        *hltTable      = new mithep::TriggerTable();
# Line 208 | Line 275 | int main(int argc, char** argv)
275    TString fInfoName(Names::gkEvtHeaderBrn);
276    TString fPrimVtxName(Names::gkPVBrn);
277    TString fPFCandidateName(Names::gkPFCandidatesBrn);
278 +  TString fPileupInfoName(Names::gkPileupInfoBrn);
279    TString fPileupEnergyDensityName(Names::gkPileupEnergyDensityBrn);
280    TString fTrackName(Names::gkTrackBrn);
281    TString fTriggerMaskName(Names::gkHltBitBrn);
# Line 218 | Line 286 | int main(int argc, char** argv)
286    chain->SetBranchAddress(fMuonName,        &muonArr);
287    chain->SetBranchAddress(fPrimVtxName,     &vtxArr);
288    chain->SetBranchAddress(fPFCandidateName, &pfArr);
289 <  chain->SetBranchAddress(fPileupEnergyDensityName, &puArr);
289 >  if( ctrl.mc ) chain->SetBranchAddress(fPileupInfoName,  &puArr);
290 >  chain->SetBranchAddress(fPileupEnergyDensityName, &puDArr);
291    chain->SetBranchAddress(fTrackName, &trkArr);
292    chain->SetBranchAddress(fTriggerMaskName, &trigMask);
293    cout << "hlttable: " << fTriggerTableName << endl;
# Line 243 | Line 312 | int main(int argc, char** argv)
312    cout << "nEntries: " << imax << endl;
313  
314  
315 <  //##########################################################################
316 <  // Loop !!!!!!!!! should alternate events here and +1 in the training ...
317 <  //##########################################################################
315 >  //
316 >  // Loop !!!!!!!!!
317 >  //--------------------------------------------------------------------------------------------------------------
318    for(UInt_t ientry=0; ientry<imax; ientry++)
319      {
320        chain->GetEntry(ientry);
321        if(!(ientry%1000)) cerr << "entry: " << ientry << endl;
253
322        setPV( ctrl, vtxArr, vtx);
323  
324 +      cout << "origrho: " << puDArr->At(0)->Rho()
325 +           << "\torigrholoweta: " << puDArr->At(0)->RhoLowEta()
326 +           << endl;
327 +
328 +
329 +      //
330 +      // data/MC
331 +      //
332        if(ctrl.mc) {
333 <        weights.w = getWeight(xstab,entrymap,chain);
333 >        //
334 >        // xsec & PU weights
335 >        //
336 >        //      weights.w = getWeight(xstab,entrymap,chain)/ctrl.totalMC;
337 >        int npu = -1;
338 >        for(int i=0;i<puArr->GetEntries();i++) {
339 >          if(puArr->At(i)->GetBunchCrossing() == 0)
340 >            npu = puArr->At(i)->GetPU_NumInteractions();
341 >        }
342 >        assert(npu>=0);
343 >        evtinfo.npu;
344 >        weights.npuw = getPUWeight(evtinfo.npu);
345 >        //      cout << "weight: " << weights.w << endl;
346        } else {
347 +        //
348 +        // JSON
349 +        //
350 +        /*
351 +        RunLumiRangeMap::RunLumiPairType rl(info->RunNum(), info->LumiSec());      
352 +        if( !(rlrm.HasRunLumi(rl)) )  {
353 +          if( ctrl.debug ) cout << "\tfails JSON" << endl;
354 +          continue;
355 +        }
356 +        */
357 +        //
358 +        // trigger
359 +        //
360          if( string(chain->GetFile()->GetEndpointUrl()->GetFile()) != currentFile ) {
361            currentFile = string(chain->GetFile()->GetEndpointUrl()->GetFile());
362            hltchain->SetBranchAddress(fTriggerTableName, &hltTableStrings);
# Line 265 | Line 366 | int main(int argc, char** argv)
366          }
367          if( ctrl.debug ) cout << "file is : " << currentFile  << endl;
368          fillTriggerBits( hltTable, trigMask, triggerBits );
268      }
269      
270      
271      //
272      // trigger
273      //
274      if( !ctrl.mc ) {
369          if( !passHLT(triggerBits, info->RunNum(), info->EvtNum() ) ) {
370            if( ctrl.debug ) cout << "\tfails trigger ... " << endl;
371            continue;
372          }
373        }
374 <      
374 >
375        //
376        // lepton/kinematic selection ...
377        //
378 +      failingLeptons.clear();
379 +      passingLeptons.clear();
380        EventData ret4l =
381 +        apply_HZZ4L_reference2_selection(ctrl, info,
382 +                                         vtx,
383 +                                         pfArr,
384 +                                         //                           evtrhoMap[info->EvtNum()],
385 +                                         puDArr,
386 +                                         electronArr,
387 +                                         &electronReferencePreSelection,
388 +                                         &electronReferenceIDMVASelection,
389 +                                         &electronReferenceIsoSelection,
390 +                                         muonArr,
391 +                                         &muonReferencePreSelection,
392 +                                         &muonIDPFSelection,
393 +                                         &muonReferenceIsoSelection);
394 +        /*
395          apply_HZZ4L_reference_selection(ctrl, info,
396                                vtx,
397                                pfArr,
398                                puArr,
399                                electronArr,
400 <                              &electronReferencePreSelection,
401 <                              &electronReferenceIDMVASelection,
402 <                              &electronReferenceIsoSelection,
400 >                              &electronPreSelection,
401 >                              &electronIDMVASelection,
402 >                              &electronIsoMVASelection,
403                                muonArr,
404 <                              &muonReferencePreSelection,
404 >                              &muonPreSelection,
405                                &muonIDPFSelection,
406 <                              &muonReferenceIsoSelection);
406 >                              &muonIsoMVASelection);
407 >        */
408 >
409        if( ctrl.debug ) cout << endl;
410  
411 <      cout << "bits: " << ret4l.status.selectionBits << endl;
411 >
412 >      int Z1type=0, Z2type=0;
413 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZCANDIDATE ) {
414 >        if( abs(ret4l.Z1leptons[0].type) == 11  ) Z1type = 11;
415 >        if( abs(ret4l.Z1leptons[0].type) == 13  ) Z1type = 13;
416 >      }  
417 >      if( ret4l.status.selectionBits.to_ulong() >= PASS_ZZCANDIDATE ) {
418 >        if( abs(ret4l.Z2leptons[0].type) == 11  ) Z2type = 11;
419 >        if( abs(ret4l.Z2leptons[0].type) == 13  ) Z2type = 13;
420 >      }  
421 >      
422 >      cout  << "bits: " << ret4l.status.selectionBits  << "\t"
423 >            << "Z1: " << Z1type << "\t"
424 >            << "Z2: " << Z2type << endl;
425 >      cout << endl;
426 >                      
427        
428        if( ret4l.status.pass() ) {
302        
429          fillAngles(ret4l,angles);
430          fillKinematics(ret4l,kinematics);
431          fillEventInfo(info,evtinfo);
432 <        //if(ctrl.mc) fillGenInfo(ginfo,geninfo);
432 >        if( ctrl.mc) {
433 >        // fillGenInfo(ginfo,geninfo);
434 >          setEffiencyWeights(ret4l, weights);
435 >           if(ctrl.debug)
436 >             cout << "w: " << weights.w << "\t"
437 >                  << "won: " << weights.won << "\t"
438 >                  << "woff: " << weights.woff << endl;
439 >        }
440          
441          /*
442          // only for Higgs < 140
# Line 324 | Line 457 | int main(int argc, char** argv)
457          pass++;
458          //      if( pass > 3 ) break;
459  
460 <      }
460 >      } else if( ret4l.status.selectionBits.test(PASS_ZCANDIDATE) ) { // save for fakes ...
461 >        if(ctrl.debug) {
462 >          cout << "failingLeptons : " << failingLeptons.size() << endl;
463 >          for( int f=0; f<failingLeptons.size(); f++ ) {
464 >            SimpleLepton  l = failingLeptons[f];
465 >            cout << "f: " << f << "\t"
466 >                 << "type: " << l.type << "\t"
467 >                 << "pT: " << l.vec.Pt()  << "\t"
468 >                 << "eta: " << l.vec.Eta()
469 >                 << endl;
470 >          }
471 >        }
472 >        foTree.Fill();
473 >      }
474      }  
475  
476 +  
477 +  foTree.getFile()->cd();
478 +  foTree.getTree()->Write();
479    nt.WriteClose();
480 +
481 +  cout << endl;
482 +  cout << endl;
483 +  for( int i=0; i<cutvec.size(); i++ ) {
484 +    cout << "cut: " << i << "\t"
485 +         << "pass: " << cutvec[i];
486 +
487 +    if( i>1&&i<=3 )
488 +      cout << "\t2e: " << zcutvec[0][i]
489 +           << "\t2m: " << zcutvec[1][i];
490 +
491 +    if( i>3 )
492 +      cout << "\t4e: " << zzcutvec[0][i]
493 +           << "\t4m: " << zzcutvec[1][i]
494 +           << "\t2e2m: " << zzcutvec[2][i];
495 +
496 +    cout << "\t" << cutstrs[i] << endl;
497 +
498 +    cout << endl;
499 +  }
500 +
501   }
502  
503   //----------------------------------------------------------------------------
# Line 383 | Line 553 | bool setPV(ControlFlags ctrl,
553    vtx.SetErrors(bestPV->XErr(),bestPV->YErr(),bestPV->ZErr());
554    return true;
555   };
556 +
557 +
558 +
559 + //----------------------------------------------------------------------------
560 + void setEffiencyWeights(EventData & evtdat, WeightStruct & weights )
561 + //----------------------------------------------------------------------------
562 + {
563 +  vector<SimpleLepton> lepvec = evtdat.Z1leptons;
564 +  lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
565 +  double w_offline=-1, werr_offline=0;
566 +  double w_online=-1, werr_online=0;
567 +  
568 +  vector< pair <double,double> > wlegs; // pair here is eff & err
569 +  vector< pair <float,float> > mvec;  // pair here is eta & pt
570 +
571 +  //      vector< pair <float,float> > evec;  // pair here is eta & pt
572 +  // now deal with medium vs loose
573 +  vector< pair< bool, pair <float,float> > > evec;  // pair here is eta & pt
574 +  
575 +  for( int k=0; k<lepvec.size(); k++ ) {
576 +    if( abs(lepvec[k].type) == 13 ) {
577 +      mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
578 +      wlegs.push_back( muonPerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
579 +                                                          lepvec[k].vec.Pt() ) );
580 +    } else {
581 +      
582 +      // now deal with medium vs loose
583 +      //              evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
584 +      
585 +      std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
586 +      evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
587 +      
588 +      //              wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
589 +      //                                                                 lepvec[k].vec.Pt() ) );
590 +      
591 +      wlegs.push_back( elePerLegOfflineEfficiencyWeight(  fabs(lepvec[k].vec.Eta()),
592 +                                                          lepvec[k].vec.Pt(),
593 +                                                          lepvec[k].isTight ) );
594 +      
595 +    }
596 +  }
597 +  
598 +  pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
599 +  weights.woff    = offpair.first;
600 +  weights.werroff = offpair.second;
601 +  
602 +  pair<double,double> onpair  = getOnlineEfficiencyWeight( mvec, evec );
603 +  weights.won    = onpair.first;
604 +  weights.werron = onpair.second;
605 + }
606 +
607 +
608 + //----------------------------------------------------------------------------
609 + void initRunLumiRangeMap()
610 + //----------------------------------------------------------------------------
611 + {
612 +  /*
613 +  rlrm.AddJSONFile(std::string("./data/Cert_136033-149442_7TeV_Apr21ReReco_Collisions10_JSON.txt"));
614 +  //  rlrm.AddJSONFile(std::string("./data/Cert_160404-173244_7TeV_PromptReco_Collisions11_JSON_v2.txt"));
615 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-178078_7TeV_PromptReco_Collisions11_JSON.txt"));
616 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-163869_7TeV_May10ReReco_Collisions11_JSON_v3.txt"));  
617 +  rlrm.AddJSONFile(std::string("./data/Cert_170249-172619_7TeV_ReReco5Aug_Collisions11_JSON.txt"));  
618 +  // r11b
619 +  rlrm.AddJSONFile(std::string("./data/Cert_160404-180252_7TeV_PromptReco_Collisions11_JSON.txt"));  
620 +  */
621 + };
622 +
623 +
624 + //----------------------------------------------------------------------------
625 + void initPUWeights()
626 + //----------------------------------------------------------------------------
627 + {
628 +  TFile * puf = new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
629 +  hpu = (TH1D*)(puf->Get("puWeights"));
630 + }
631 +
632 + //----------------------------------------------------------------------------
633 + double getPUWeight(unsigned npu)
634 + //----------------------------------------------------------------------------
635 + {
636 +  return hpu->GetBinContent(hpu->FindBin(npu));
637 + }
638 +
639 + //----------------------------------------------------------------------------
640 + void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
641 + //----------------------------------------------------------------------------
642 + {
643 +  unsigned evt;
644 +  float rho;
645 +
646 +  FILE * f = fopen("evtrho.dat", "r");
647 +  while( fscanf( f, "%u:%f", &evt, &rho ) ) {
648 +    evtrhoMap[evt] = rho;
649 +  }
650 +  
651 + }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines