ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/SelectionFuncs.cc
Revision: 1.8
Committed: Thu Jul 12 15:50:27 2012 UTC (12 years, 10 months ago) by khahn
Content type: text/plain
Branch: MAIN
Changes since 1.7: +33 -7 lines
Log Message:
eff weights

File Contents

# User Rev Content
1 dkralph 1.1 #include "SelectionFuncs.h"
2    
3     extern vector<SimpleLepton> failingLeptons;
4     extern vector<SimpleLepton> passingLeptons;
5    
6     extern vector<unsigned> cutvec;
7     extern vector<vector<unsigned> > zcutvec;
8     extern vector<vector<unsigned> > zzcutvec;
9     extern map<unsigned,float> evtrhoMap;
10     extern bool passes_HLT_MC;
11    
12 khahn 1.5 extern TH1D* hpu_2011;
13     extern TH1D* hpu_2012;
14    
15 dkralph 1.1 //----------------------------------------------------------------------------
16     bool setPV(ControlFlags ctrl,
17     const mithep::Array<mithep::Vertex> * vtxArr,
18     const mithep::Vertex* &vtx)
19     //----------------------------------------------------------------------------
20 dkralph 1.6 {
21 dkralph 1.1
22     const mithep::Vertex *bestPV = 0;
23     float best_sumpt=-1;
24    
25     // good PV requirements
26     const UInt_t fMinNTracksFit = 0;
27     const Double_t fMinNdof = 4;
28     const Double_t fMaxAbsZ = 24;
29     const Double_t fMaxRho = 2;
30    
31     for(int i=0; i<vtxArr->GetEntries(); ++i) {
32     const mithep::Vertex *pv = (mithep::Vertex*)(vtxArr->At(i));
33     if( ctrl.debug ) cout << "vertex :: " << i << "\tntrks: " << pv->NTracks() << endl;
34    
35     // Select best PV for corrected d0; if no PV passing cuts, the first PV in the collection will be used
36     if(!pv->IsValid()) continue;
37     if(pv->NTracksFit() < fMinNTracksFit) continue;
38     if(pv->Ndof() < fMinNdof) continue;
39     if(fabs(pv->Z()) > fMaxAbsZ) continue;
40     if(pv->Position().Rho() > fMaxRho) continue;
41    
42     // take the first ...
43     bestPV = pv;
44     break;
45    
46     // this never reached ...
47     float tmp_sumpt=0;
48     for( int t=0; t<pv->NTracks(); t++ )
49     tmp_sumpt += pv->Trk(t)->Pt();
50    
51     if( tmp_sumpt > best_sumpt ) {
52     bestPV = pv;
53     best_sumpt = tmp_sumpt;
54     if( ctrl.debug) cout << "new PV set, pt : " << best_sumpt << endl;
55     }
56     }
57    
58     // sync
59     if(!bestPV)
60     return false;
61     else {
62     vtx = bestPV;
63     return true;
64     }
65     }
66     //----------------------------------------------------------------------------------------
67     void writeEntries(ControlFlags ctrl, unsigned total_unskimmed)
68     {
69     ofstream ofs;
70     TString ofname(ctrl.outputfile);
71     ofs.open(ofname.ReplaceAll(".root",".nevents"));
72     assert(ofs.is_open());
73     ofs << total_unskimmed << endl;
74     ofs.close();
75     }
76     //----------------------------------------------------------------------------------------
77     void writeEntries(FOFlags foctrl, unsigned total_unskimmed)
78     {
79     ControlFlags ctrl;
80     ctrl.outputfile = foctrl.outputfile;
81     writeEntries(ctrl,total_unskimmed);
82     }
83     //----------------------------------------------------------------------------------------
84     void getEATargets(ControlFlags &ctrl, mithep::MuonTools::EMuonEffectiveAreaTarget &eraMu, mithep::ElectronTools::EElectronEffectiveAreaTarget &eraEle)
85     {
86     if( !ctrl.mc && ctrl.era == 2011 ) {
87     eraMu = mithep::MuonTools::kMuEAData2011;
88     eraEle = mithep::ElectronTools::kEleEAData2011;
89     } else if( !ctrl.mc && ctrl.era == 2012 ) {
90     eraMu = mithep::MuonTools::kMuEAData2012;
91     eraEle = mithep::ElectronTools::kEleEAData2012;
92     } else if( ctrl.mc && ctrl.era == 2011 ) {
93     eraMu = mithep::MuonTools::kMuEAFall11MC;
94     eraEle = mithep::ElectronTools::kEleEAFall11MC;
95     } else if( ctrl.mc && ctrl.era == 2012 ) {
96     eraMu = mithep::MuonTools::kMuEAData2012;
97     eraEle = mithep::ElectronTools::kEleEAData2012;
98     } else {
99     cerr << "unknown era for effective areas ... quitting." << endl;
100     exit(1);
101     }
102     }
103     //----------------------------------------------------------------------------------------
104     void getEATargets(FOFlags foctrl, mithep::MuonTools::EMuonEffectiveAreaTarget &eraMu, mithep::ElectronTools::EElectronEffectiveAreaTarget &eraEle)
105     {
106     ControlFlags ctrl;
107     ctrl.mc = foctrl.mc;
108     ctrl.era = foctrl.era;
109     getEATargets(ctrl,eraMu,eraEle);
110     }
111     //----------------------------------------------------------------------------
112     unsigned makePFnoPUArray(mithep::Array<PFCandidate> * fPFCandidates,
113     vector<bool> &pfNoPileUpflag,
114     const mithep::Array<mithep::Vertex> * vtxArr )
115     //----------------------------------------------------------------------------
116     {
117     for(UInt_t i = 0; i < fPFCandidates->GetEntries(); i++) {
118     const PFCandidate *pf = fPFCandidates->At(i);
119     assert(pf);
120    
121     if(pf->PFType() == PFCandidate::eHadron) {
122     if(pf->HasTrackerTrk() &&
123     vtxArr->At(0)->HasTrack(pf->TrackerTrk()) &&
124     vtxArr->At(0)->TrackWeight(pf->TrackerTrk()) > 0) {
125    
126     pfNoPileUpflag.push_back(1);
127    
128     } else {
129    
130     Bool_t vertexFound = kFALSE;
131     const Vertex *closestVtx = 0;
132     Double_t dzmin = 10000;
133    
134     // loop over vertices
135     for(UInt_t j = 0; j < vtxArr->GetEntries(); j++) {
136     const Vertex *vtx = vtxArr->At(j);
137     assert(vtx);
138    
139     if(pf->HasTrackerTrk() &&
140     vtx->HasTrack(pf->TrackerTrk()) &&
141     vtx->TrackWeight(pf->TrackerTrk()) > 0) {
142     vertexFound = kTRUE;
143     closestVtx = vtx;
144     break;
145     }
146     Double_t dz = fabs(pf->SourceVertex().Z() - vtx->Z());
147     if(dz < dzmin) {
148     closestVtx = vtx;
149     dzmin = dz;
150     }
151     }
152    
153     // if(fCheckClosestZVertex) {
154     if(1) {
155     // Fallback: if track is not associated with any vertex,
156     // associate it with the vertex closest in z
157     if(vertexFound || closestVtx != vtxArr->At(0)) {
158     // pfPileUp->Add(pf);
159     pfNoPileUpflag.push_back(0);
160     } else {
161     pfNoPileUpflag.push_back(1);
162     }
163     } else {
164     if(vertexFound && closestVtx != vtxArr->At(0)) {
165     // pfPileUp->Add(pf);
166     pfNoPileUpflag.push_back(0);
167     } else {
168     // PFCandidate * pfNoPileUp->AddNew(); // Ridiculous but that's how it is
169     pfNoPileUpflag.push_back(1);
170     }
171     }
172     } //hadron & trk stuff
173     } else { // hadron
174     // PFCandidate * ptr = pfNoPileUp->AddNew();
175     pfNoPileUpflag.push_back(1);
176     }
177     } // Loop over PF candidates
178    
179     return pfNoPileUpflag.size();
180     }
181 khahn 1.4 //--------------------------------------------------------------------------------
182 khahn 1.8 void setEffiencyWeights(unsigned era,
183     EventData & evtdat,
184     std::bitset<1024> triggerBits,
185     mithep::TriggerTable *hltTable,
186     mithep::Array<mithep::TriggerObject> *hltObjArr,
187     mithep::TriggerObjectsTable *fTrigObjs,
188     WeightStruct & weights )
189 khahn 1.4 //--------------------------------------------------------------------------------
190 dkralph 1.1 {
191     vector<SimpleLepton> lepvec = evtdat.Z1leptons;
192     lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
193     double w_offline=-1, werr_offline=0;
194     double w_online=-1, werr_online=0;
195 khahn 1.8
196     vector<SimpleLepton> muvec, elvec;
197     if(abs(evtdat.Z1leptons[0].type) == 11 ) {
198     // lepvec.insert(elvec.end(), evtdat.Z1leptons.begin(), evtdat.Z1leptons.end());
199     elvec.push_back(evtdat.Z1leptons[0]);
200     elvec.push_back(evtdat.Z1leptons[1]);
201     } else {
202     // lepvec.insert(muvec.end(), evtdat.Z1leptons.begin(), evtdat.Z1leptons.end());
203     muvec.push_back(evtdat.Z1leptons[0]);
204     muvec.push_back(evtdat.Z1leptons[1]);
205     }
206     if(abs(evtdat.Z2leptons[0].type) == 11 ) {
207     //lepvec.insert(elvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
208     elvec.push_back(evtdat.Z2leptons[0]);
209     elvec.push_back(evtdat.Z2leptons[1]);
210     } else {
211     // lepvec.insert(muvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
212     muvec.push_back(evtdat.Z2leptons[0]);
213     muvec.push_back(evtdat.Z2leptons[1]);
214     }
215    
216 dkralph 1.1 vector< pair <double,double> > wlegs; // pair here is eff & err
217     vector< pair <float,float> > mvec; // pair here is eta & pt
218    
219     // vector< pair <float,float> > evec; // pair here is eta & pt
220     // now deal with medium vs loose
221 khahn 1.8 // vector< pair< bool, pair <float,float> > > evec; // pair here is eta & pt
222     vector< pair <float,float> > evec; // pair here is eta & pt
223    
224 dkralph 1.1 for( int k=0; k<lepvec.size(); k++ ) {
225     if( abs(lepvec[k].type) == 13 ) {
226     mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
227 khahn 1.4 wlegs.push_back( muonPerLegOfflineEfficiencyWeight( era, fabs(lepvec[k].vec.Eta()),
228 dkralph 1.1 lepvec[k].vec.Pt() ) );
229     } else {
230    
231     // now deal with medium vs loose
232     // evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
233    
234     std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
235 khahn 1.8 evec.push_back( tmppair );
236 dkralph 1.1
237     // wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
238     // lepvec[k].vec.Pt() ) );
239    
240 khahn 1.4 wlegs.push_back( elePerLegOfflineEfficiencyWeight( era,
241     fabs(lepvec[k].vec.Eta()),
242 dkralph 1.1 lepvec[k].vec.Pt(),
243     lepvec[k].isTight ) );
244    
245     }
246     }
247    
248     pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
249     weights.woff = offpair.first;
250     weights.werroff = offpair.second;
251    
252 khahn 1.8 pair<double,double> onpair = getOnlineEfficiencyWeight( triggerBits, hltTable, hltObjArr, fTrigObjs, muvec, elvec );
253 dkralph 1.1 weights.won = onpair.first;
254     weights.werron = onpair.second;
255     }
256     //----------------------------------------------------------------------------
257     void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
258     //----------------------------------------------------------------------------
259     {
260     unsigned evt;
261     float rho;
262    
263     cout << "initialzing EvtRhoMap ";
264     //FILE * f = fopen("evtrho.dat", "r");
265     // FILE * f = fopen("allrho.cali.uniq.dat", "r");
266     // FILE * f = fopen("/data/blue/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
267     assert(fopen("/scratch/dkralph/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat","r")); FILE * f = fopen("/scratch/dkralph/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
268     int lcount=0;
269     while( fscanf( f, "%u:%f", &evt, &rho ) ) {
270     if( feof(f) ) break;
271     evtrhoMap[evt] = rho;
272     lcount++;
273     if( !(lcount%1000) ) cout << "."; flush(cout);
274     }
275     cout << " done" << endl;
276     }
277    
278 khahn 1.5 //----------------------------------------------------------------------------------------
279 dkralph 1.1 void setEra(string fname, ControlFlags &ctrl)
280     {
281 khahn 1.5 //----------------------------------------------------------------------------------------
282 dkralph 1.1 if( ctrl.mc && (fname.find("f11-") != string::npos) ) ctrl.era=2011 ;
283     if( ctrl.mc && (fname.find("s12-") != string::npos) ) ctrl.era=2012 ;
284 khahn 1.7 if( !ctrl.mc && (fname.find("r11") != string::npos) ) ctrl.era=2011 ;
285     if( !ctrl.mc && (fname.find("r12") != string::npos) ) ctrl.era=2012 ;
286 dkralph 1.1 }
287 khahn 1.5
288 dkralph 1.1 //----------------------------------------------------------------------------------------
289 dkralph 1.6 unsigned getNPU(mithep::Array<mithep::PileupInfo> *puArr, int bx)
290 khahn 1.5 //----------------------------------------------------------------------------------------
291 dkralph 1.1 {
292    
293     // Identify bunch crossing 0 in PU info
294     int ibx0=-1, ibxm=-1, ibxp=-1;
295     assert(puArr);
296     for(unsigned i=0; i<puArr->GetEntries(); ++i) {
297     if(puArr->At(i)->GetBunchCrossing() == 0) ibx0=i;
298     if(puArr->At(i)->GetBunchCrossing() ==-1) ibxm=i;
299     if(puArr->At(i)->GetBunchCrossing() == 1) ibxp=i;
300     }
301    
302     if(bx==0) return puArr->At(ibx0)->GetPU_NumInteractions();
303     else if(bx==-1) return puArr->At(ibxm)->GetPU_NumInteractions();
304     else if(bx== 1) return puArr->At(ibxp)->GetPU_NumInteractions();
305     else assert(0);
306    
307 dkralph 1.6 return 0;
308 dkralph 1.1 }
309 khahn 1.5
310    
311     //----------------------------------------------------------------------------
312     void initPUWeights()
313     //----------------------------------------------------------------------------
314     {
315     TFile * puf;
316    
317     puf= new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
318     hpu_2011 = (TH1D*)(puf->Get("puWeights"));
319     hpu_2011->SetDirectory(0);
320     puf->Close();
321    
322     puf = new TFile("data/PUWeights_S12To2012_1616ipb_from_valentina.root");
323     hpu_2012 = (TH1D*)(puf->Get("puWeights"));
324     hpu_2012->SetDirectory(0);
325     puf->Close();
326     }
327    
328     //----------------------------------------------------------------------------
329     double getPUWeight(unsigned era,unsigned npu)
330     //----------------------------------------------------------------------------
331     {
332     if( era == 2011 )
333     return hpu_2011->GetBinContent(hpu_2011->FindBin(npu));
334 khahn 1.7 else if (era == 2012 ) {
335     cerr << "npu : " << npu
336     << "\tbin: " << hpu_2012->FindBin(npu)
337     << "\tnpuw: " << hpu_2012->GetBinContent(hpu_2012->FindBin(npu))
338     << endl;
339 khahn 1.5 return hpu_2012->GetBinContent(hpu_2012->FindBin(npu));
340 khahn 1.7 }
341 khahn 1.5 else return 1;
342     }
343