ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitHzz4l/Selection/src/SelectionFuncs.cc
Revision: 1.11
Committed: Sat Oct 13 19:47:56 2012 UTC (12 years, 7 months ago) by anlevin
Content type: text/plain
Branch: MAIN
Changes since 1.10: +6 -0 lines
Log Message:
added new way of counting events

File Contents

# Content
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 extern TH1D* hpu_2011;
13 extern TH1D* hpu_2012;
14
15 //----------------------------------------------------------------------------
16 bool setPV(ControlFlags ctrl,
17 const mithep::Array<mithep::Vertex> * vtxArr,
18 const mithep::Vertex* &vtx)
19 //----------------------------------------------------------------------------
20 {
21
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(const 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 //--------------------------------------------------------------------------------
182 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 //--------------------------------------------------------------------------------
190 {
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
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 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 // 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 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 wlegs.push_back( muonPerLegOfflineEfficiencyWeight( era, fabs(lepvec[k].vec.Eta()),
228 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 evec.push_back( tmppair );
236
237 // wlegs.push_back( elePerLegOfflineEfficiencyWeight( fabs(lepvec[k].vec.Eta()),
238 // lepvec[k].vec.Pt() ) );
239
240 wlegs.push_back( elePerLegOfflineEfficiencyWeight( era,
241 fabs(lepvec[k].vec.Eta()),
242 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 pair<double,double> onpair = getOnlineEfficiencyWeight( triggerBits, hltTable, hltObjArr, fTrigObjs, muvec, elvec );
253 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 //----------------------------------------------------------------------------------------
279 void setEra(string fname, ControlFlags &ctrl)
280 {
281 //----------------------------------------------------------------------------------------
282 if( ctrl.mc && (fname.find("f11-") != string::npos) ) ctrl.era=2011 ;
283 else if( ctrl.mc && (fname.find("s12-") != string::npos) ) ctrl.era=2012 ;
284 else if( !ctrl.mc && (fname.find("r11") != string::npos) ) ctrl.era=2011 ;
285 else if( !ctrl.mc && (fname.find("r12") != string::npos) ) ctrl.era=2012 ;
286 else assert(0);
287 }
288
289 //----------------------------------------------------------------------------------------
290 unsigned getNPU(mithep::Array<mithep::PileupInfo> *puArr, int bx)
291 //----------------------------------------------------------------------------------------
292 {
293
294 // Identify bunch crossing 0 in PU info
295 int ibx0=-1, ibxm=-1, ibxp=-1;
296 assert(puArr);
297 for(unsigned i=0; i<puArr->GetEntries(); ++i) {
298 if(puArr->At(i)->GetBunchCrossing() == 0) ibx0=i;
299 if(puArr->At(i)->GetBunchCrossing() ==-1) ibxm=i;
300 if(puArr->At(i)->GetBunchCrossing() == 1) ibxp=i;
301 }
302
303 if(bx==0) return puArr->At(ibx0)->GetPU_NumInteractions();
304 else if(bx==-1) return puArr->At(ibxm)->GetPU_NumInteractions();
305 else if(bx== 1) return puArr->At(ibxp)->GetPU_NumInteractions();
306 else assert(0);
307
308 return 0;
309 }
310
311
312 //----------------------------------------------------------------------------
313 void initPUWeights()
314 //----------------------------------------------------------------------------
315 {
316 TFile * puf;
317
318 puf= new TFile("data/PileupReweighting.Summer11DYmm_To_Full2011.root");
319 hpu_2011 = (TH1D*)(puf->Get("puWeights"));
320 hpu_2011->SetDirectory(0);
321 puf->Close();
322
323 puf = new TFile("data/PUWeights_S12To2012_1616ipb_from_valentina.root");
324 hpu_2012 = (TH1D*)(puf->Get("puWeights"));
325 hpu_2012->SetDirectory(0);
326 puf->Close();
327 }
328
329 //----------------------------------------------------------------------------
330 double getPUWeight(unsigned era,unsigned npu)
331 //----------------------------------------------------------------------------
332 {
333 if( era == 2011 )
334 return hpu_2011->GetBinContent(hpu_2011->FindBin(npu));
335 else if (era == 2012 ) {
336 cerr << "npu : " << npu
337 << "\tbin: " << hpu_2012->FindBin(npu)
338 << "\tnpuw: " << hpu_2012->GetBinContent(hpu_2012->FindBin(npu))
339 << endl;
340 return hpu_2012->GetBinContent(hpu_2012->FindBin(npu));
341 }
342 else return 1;
343 }
344
345 TString getChannel(int z1type, int z2type)
346 {
347 if( abs(z1type) == 11 && abs(z2type) == 11 ) return "4e";//0;
348 else if( abs(z1type) == 13 && abs(z2type) == 13 ) return "4m";//1;
349 else return "2e2m";//2;
350 }