ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/SelectionFuncs.cc
Revision: 1.6
Committed: Thu Jun 21 21:50:39 2012 UTC (12 years, 10 months ago) by dkralph
Content type: text/plain
Branch: MAIN
Changes since 1.5: +3 -6 lines
Log Message:
change prototype of getNPU

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     //--------------------------------------------------------------------------------
183     void setEffiencyWeights(unsigned era, EventData & evtdat, WeightStruct & weights )
184     //--------------------------------------------------------------------------------
185 dkralph 1.1 {
186     vector<SimpleLepton> lepvec = evtdat.Z1leptons;
187     lepvec.insert(lepvec.end(), evtdat.Z2leptons.begin(), evtdat.Z2leptons.end());
188     double w_offline=-1, werr_offline=0;
189     double w_online=-1, werr_online=0;
190    
191     vector< pair <double,double> > wlegs; // pair here is eff & err
192     vector< pair <float,float> > mvec; // pair here is eta & pt
193    
194     // vector< pair <float,float> > evec; // pair here is eta & pt
195     // now deal with medium vs loose
196     vector< pair< bool, pair <float,float> > > evec; // pair here is eta & pt
197    
198     for( int k=0; k<lepvec.size(); k++ ) {
199     if( abs(lepvec[k].type) == 13 ) {
200     mvec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
201 khahn 1.4 wlegs.push_back( muonPerLegOfflineEfficiencyWeight( era, fabs(lepvec[k].vec.Eta()),
202 dkralph 1.1 lepvec[k].vec.Pt() ) );
203     } else {
204    
205     // now deal with medium vs loose
206     // evec.push_back( std::pair<float,float> (fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt()) );
207    
208     std::pair<float,float> tmppair(fabs(lepvec[k].vec.Eta()), lepvec[k].vec.Pt());
209     evec.push_back( std::pair<bool, std::pair<float,float> > (lepvec[k].isTight, tmppair) );
210    
211     // wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
212     // lepvec[k].vec.Pt() ) );
213    
214 khahn 1.4 wlegs.push_back( elePerLegOfflineEfficiencyWeight( era,
215     fabs(lepvec[k].vec.Eta()),
216 dkralph 1.1 lepvec[k].vec.Pt(),
217     lepvec[k].isTight ) );
218    
219     }
220     }
221    
222     pair<double,double> offpair = getOfflineEfficiencyWeight( wlegs );
223     weights.woff = offpair.first;
224     weights.werroff = offpair.second;
225    
226     pair<double,double> onpair = getOnlineEfficiencyWeight( mvec, evec );
227     weights.won = onpair.first;
228     weights.werron = onpair.second;
229     }
230     //----------------------------------------------------------------------------
231     void initEvtRhoMap( map<unsigned, float> & evtrhoMap )
232     //----------------------------------------------------------------------------
233     {
234     unsigned evt;
235     float rho;
236    
237     cout << "initialzing EvtRhoMap ";
238     //FILE * f = fopen("evtrho.dat", "r");
239     // FILE * f = fopen("allrho.cali.uniq.dat", "r");
240     // FILE * f = fopen("/data/blue/khahn/CMSSW_4_4_2/src/MitHzz4l/allrhoAA.cali.uniq.dat", "r");
241     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");
242     int lcount=0;
243     while( fscanf( f, "%u:%f", &evt, &rho ) ) {
244     if( feof(f) ) break;
245     evtrhoMap[evt] = rho;
246     lcount++;
247     if( !(lcount%1000) ) cout << "."; flush(cout);
248     }
249     cout << " done" << endl;
250     }
251    
252 khahn 1.5 //----------------------------------------------------------------------------------------
253 dkralph 1.1 void setEra(string fname, ControlFlags &ctrl)
254     {
255 khahn 1.5 //----------------------------------------------------------------------------------------
256 dkralph 1.1 if( ctrl.mc && (fname.find("f11-") != string::npos) ) ctrl.era=2011 ;
257     if( ctrl.mc && (fname.find("s12-") != string::npos) ) ctrl.era=2012 ;
258     if( !ctrl.mc && (fname.find("r11-") != string::npos) ) ctrl.era=2011 ;
259     if( !ctrl.mc && (fname.find("r12-") != string::npos) ) ctrl.era=2012 ;
260     }
261 khahn 1.5
262 dkralph 1.1 //----------------------------------------------------------------------------------------
263 dkralph 1.6 unsigned getNPU(mithep::Array<mithep::PileupInfo> *puArr, int bx)
264 khahn 1.5 //----------------------------------------------------------------------------------------
265 dkralph 1.1 {
266    
267     // Identify bunch crossing 0 in PU info
268     int ibx0=-1, ibxm=-1, ibxp=-1;
269     assert(puArr);
270     for(unsigned i=0; i<puArr->GetEntries(); ++i) {
271     if(puArr->At(i)->GetBunchCrossing() == 0) ibx0=i;
272     if(puArr->At(i)->GetBunchCrossing() ==-1) ibxm=i;
273     if(puArr->At(i)->GetBunchCrossing() == 1) ibxp=i;
274     }
275    
276     if(bx==0) return puArr->At(ibx0)->GetPU_NumInteractions();
277     else if(bx==-1) return puArr->At(ibxm)->GetPU_NumInteractions();
278     else if(bx== 1) return puArr->At(ibxp)->GetPU_NumInteractions();
279     else assert(0);
280    
281 dkralph 1.6 return 0;
282 dkralph 1.1 }
283 khahn 1.5
284    
285     //----------------------------------------------------------------------------
286     void initPUWeights()
287     //----------------------------------------------------------------------------
288     {
289     TFile * puf;
290    
291     puf= new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
292     hpu_2011 = (TH1D*)(puf->Get("puWeights"));
293     hpu_2011->SetDirectory(0);
294     puf->Close();
295    
296     puf = new TFile("data/PUWeights_S12To2012_1616ipb_from_valentina.root");
297     hpu_2012 = (TH1D*)(puf->Get("puWeights"));
298     hpu_2012->SetDirectory(0);
299     puf->Close();
300     }
301    
302     //----------------------------------------------------------------------------
303     double getPUWeight(unsigned era,unsigned npu)
304     //----------------------------------------------------------------------------
305     {
306     if( era == 2011 )
307     return hpu_2011->GetBinContent(hpu_2011->FindBin(npu));
308     else if (era == 2012 )
309     return hpu_2012->GetBinContent(hpu_2012->FindBin(npu));
310     else return 1;
311     }
312