ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/kiesel/TreeWriter/treeWriter.cc
Revision: 1.12
Committed: Tue Apr 16 15:11:20 2013 UTC (12 years ago) by kiesel
Content type: text/plain
Branch: MAIN
Changes since 1.11: +120 -60 lines
Log Message:
electron selection applied, jet composition saved

File Contents

# Content
1 #include<iostream>
2 #include<math.h>
3 #include<string>
4
5 #include "TSystem.h"
6
7 #include "treeWriter.h"
8
9 using namespace std;
10
11 TreeWriter::TreeWriter( TString inputName, TString outputName, int loggingVerbosity_ ) {
12 // read the input file
13 inputTree = new TChain("susyTree");
14 if (loggingVerbosity_ > 0)
15 std::cout << "Add files to chain" << std::endl;
16 inputTree->Add( inputName );
17 Init( outputName, loggingVerbosity_ );
18 }
19
20 TreeWriter::TreeWriter( TChain* inputTree_, TString outputName, int loggingVerbosity_ ) {
21 inputTree = inputTree_;
22 Init( outputName, loggingVerbosity_ );
23 }
24
25
26 void TreeWriter::Init( TString outputName, int loggingVerbosity_ ) {
27
28 if (loggingVerbosity_ > 0)
29 std::cout << "Set Branch Address of susy::Event" << std::endl;
30 event = new susy::Event;
31 inputTree->SetBranchAddress("susyEvent", &event);
32
33 // open the output file
34 if (loggingVerbosity_>0)
35 std::cout << "Open file " << outputName << " for writing." << std::endl;
36 outFile = new TFile( outputName, "recreate" );
37 tree = new TTree("susyTree","Tree for single photon analysis");
38
39 // set default parameter
40 processNEvents = -1;
41 reportEvery = 1000;
42 loggingVerbosity = loggingVerbosity_;
43 skim = true;
44 pileupHisto = 0;
45 }
46
47 void TreeWriter::PileUpWeightFile( string pileupFileName ) {
48 TFile *puFile = new TFile( pileupFileName.c_str() );
49 pileupHisto = (TH1F*) puFile->Get("pileup");
50 }
51
52 TreeWriter::~TreeWriter() {
53 if (pileupHisto != 0 )
54 delete pileupHisto;
55 inputTree->GetCurrentFile()->Close();
56 }
57
58 // useful functions
59 float TreeWriter::deltaR( TLorentzVector v1, TLorentzVector v2 ) {
60 return sqrt(pow(v1.Eta() - v2.Eta(), 2) + pow(v1.Phi() - v2.Phi(), 2) );
61 }
62
63 float effectiveAreaElectron( float eta ) {
64 // see https://twiki.cern.ch/twiki/bin/view/CMS/EgammaEARhoCorrection
65 // only for Delta R = 0.3 on 2012 Data
66 eta = fabs( eta );
67 float ea;
68 if( eta < 1.0 ) ea = 0.13;
69 else if( eta < 1.479 ) ea = 0.14;
70 else if( eta < 2.0 ) ea = 0.07;
71 else if( eta < 2.2 ) ea = 0.09;
72 else if( eta < 2.3 ) ea = 0.11;
73 else if( eta < 2.4 ) ea = 0.11;
74 else ea = 0.14;
75 return ea;
76 }
77
78 // correct iso, see https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedPhotonID2012
79 float chargedHadronIso_corrected(susy::Photon gamma, float rho) {
80 float eta = fabs(gamma.caloPosition.Eta());
81 float ea;
82
83 if(eta < 1.0) ea = 0.012;
84 else if(eta < 1.479) ea = 0.010;
85 else if(eta < 2.0) ea = 0.014;
86 else if(eta < 2.2) ea = 0.012;
87 else if(eta < 2.3) ea = 0.016;
88 else if(eta < 2.4) ea = 0.020;
89 else ea = 0.012;
90
91 float iso = gamma.chargedHadronIso;
92 iso = max(iso - rho*ea, (float)0.);
93
94 return iso;
95 }
96
97 float neutralHadronIso_corrected(susy::Photon gamma, float rho) {
98 float eta = fabs(gamma.caloPosition.Eta());
99 float ea;
100
101 if(eta < 1.0) ea = 0.030;
102 else if(eta < 1.479) ea = 0.057;
103 else if(eta < 2.0) ea = 0.039;
104 else if(eta < 2.2) ea = 0.015;
105 else if(eta < 2.3) ea = 0.024;
106 else if(eta < 2.4) ea = 0.039;
107 else ea = 0.072;
108
109 float iso = gamma.neutralHadronIso;
110 iso = max(iso - rho*ea, (float)0.);
111
112 return iso;
113 }
114
115 float photonIso_corrected(susy::Photon gamma, float rho) {
116 float eta = fabs(gamma.caloPosition.Eta());
117 float ea;
118
119 if(eta < 1.0) ea = 0.148;
120 else if(eta < 1.479) ea = 0.130;
121 else if(eta < 2.0) ea = 0.112;
122 else if(eta < 2.2) ea = 0.216;
123 else if(eta < 2.3) ea = 0.262;
124 else if(eta < 2.4) ea = 0.260;
125 else ea = 0.266;
126
127 float iso = gamma.photonIso;
128 iso = max(iso - rho*ea, (float)0.);
129
130 return iso;
131 }
132
133 float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
134 /**
135 * \brief Takes jet p_T as photon p_T
136 *
137 * At first all jets with DeltaR < 0.3 (isolation cone) are searched.
138 * If several jets are found, take the one with the minimal pt difference
139 * compared to the photon. If no such jets are found, keep the photon_pt
140 * TODO: remove photon matched jet from jet-selection?
141 */
142 std::vector<susy::PFJet> nearJets;
143 nearJets.clear();
144
145 std::map<TString,susy::PFJetCollection>::iterator pfJets_it = myEvent.pfJets.find("ak5");
146 if(pfJets_it == myEvent.pfJets.end()){
147 if(myEvent.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
148 } else {
149 susy::PFJetCollection& jetColl = pfJets_it->second;
150 for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
151 it != jetColl.end(); ++it) {
152 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
153 if (s_it == it->jecScaleFactors.end()) {
154 std::cout << "JEC is not available for this jet!!!" << std::endl;
155 continue;
156 }
157 float scale = s_it->second;
158 TLorentzVector corrP4 = scale * it->momentum;
159 float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
160 if (deltaR_ > 0.3) continue;
161 if( loggingVerbosity > 0 )
162 std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
163 nearJets.push_back( *it );
164 }// for jet
165 }// if, else
166
167 if ( nearJets.size() == 0 ) {
168 if( loggingVerbosity > 1 )
169 std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
170 return myPhoton.momentum.Et();
171 }
172
173 float pt = 0;
174 float minPtDifferenz = 1E20; // should be very high
175 for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
176 it != jetEnd; ++it ) {
177 float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
178 if ( ptDiff < minPtDifferenz ) {
179 minPtDifferenz = ptDiff;
180 pt = it->momentum.Et();
181 }
182 }
183
184 // testing
185 if( nearJets.size() > 1 && loggingVerbosity > 0 )
186 std::cout << "There are several jets matching to this photon. "
187 << "Please check if jet-matching is correct." << std::endl;
188 return pt;
189 }
190
191
192 void TreeWriter::Loop() {
193
194 // here the event loop is implemented and the tree is filled
195 if (inputTree == 0) return;
196
197 // get number of events to be proceeded
198 Long64_t nentries = inputTree->GetEntries();
199 if(processNEvents <= 0 || processNEvents > nentries) processNEvents = nentries;
200
201 if( loggingVerbosity > 0 )
202 std::cout << "Processing " << processNEvents << " ouf of "
203 << nentries << " events. " << std::endl;
204
205 tree::Photon *thisphoton = new tree::Photon();
206 tree::Jet *thisjet = new tree::Jet();
207
208 tree->Branch("photon", &photon);
209 tree->Branch("jet", &jet);
210 tree->Branch("electron", &electron);
211 tree->Branch("muon", &muon);
212 tree->Branch("met", &met, "met/F");
213 tree->Branch("metPhi", &met_phi, "metPhi/F");
214 tree->Branch("type1met", &type1met, "type1met/F");
215 tree->Branch("type1metPhi", &type1met_phi, "type1metPhi/F");
216 tree->Branch("ht", &ht, "ht/F");
217 tree->Branch("nVertex", &nVertex, "nVertex/I");
218 tree->Branch("pu_weight", &pu_weight, "pu_weight/F");
219
220
221 for (long jentry=0; jentry < processNEvents; ++jentry) {
222 if ( loggingVerbosity>0 && jentry%reportEvery==0 )
223 std::cout << jentry << " / " << processNEvents << std :: endl;
224 inputTree->GetEntry(jentry);
225
226 photon.clear();
227 jet.clear();
228 electron.clear();
229 muon.clear();
230 ht = 0;
231
232 // weights
233 if (pileupHisto == 0) {
234 pu_weight = 1.;
235 } else {
236 float trueNumInteractions = -1;
237 for( susy::PUSummaryInfoCollection::const_iterator iBX = event->pu.begin();
238 iBX != event->pu.end() && trueNumInteractions < 0; ++iBX) {
239 if (iBX->BX == 0)
240 trueNumInteractions = iBX->trueNumInteractions;
241 }
242 pu_weight = pileupHisto->GetBinContent( pileupHisto->FindBin( trueNumInteractions ) );
243 }
244
245
246 // photons
247 if( loggingVerbosity > 1 )
248 std::cout << "Process photons" << std::endl;
249 std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
250 for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
251 it != phoMap->second.end() && phoMap != event->photons.end(); ++it ) {
252 if( !(it->isEB() || it->isEE()) && skim )
253 continue;
254 thisphoton->pt = getPtFromMatchedJet( *it, *event );
255
256 thisphoton->chargedIso = chargedHadronIso_corrected(*it, event->rho25);
257 thisphoton->neutralIso = neutralHadronIso_corrected(*it, event->rho25);
258 thisphoton->photonIso = photonIso_corrected(*it, event->rho25);
259
260 bool loose_photon_barrel = thisphoton->pt>20
261 && it->isEB()
262 && it->passelectronveto
263 && it->hadTowOverEm<0.05
264 && it->sigmaIetaIeta<0.012
265 && thisphoton->chargedIso<2.6
266 && thisphoton->neutralIso<3.5+0.04*thisphoton->pt
267 && thisphoton->photonIso<1.3+0.005*thisphoton->pt;
268 bool loose_photon_endcap = thisphoton->pt > 20
269 && it->isEE()
270 && it->passelectronveto
271 && it->hadTowOverEm<0.05
272 && it->sigmaIetaIeta<0.034
273 && thisphoton->chargedIso<2.3
274 && thisphoton->neutralIso<2.9+0.04*thisphoton->pt;
275 if(!(loose_photon_endcap || loose_photon_barrel || thisphoton->pt > 75 ) && skim )
276 continue;
277 thisphoton->eta = it->momentum.Eta();
278 thisphoton->phi = it->momentum.Phi();
279 thisphoton->r9 = it->r9;
280 thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
281 thisphoton->hadTowOverEm = it->hadTowOverEm;
282 thisphoton->pixelseed = it->nPixelSeeds;
283 thisphoton->conversionSafeVeto = it->passelectronveto;
284 photon.push_back( *thisphoton );
285 if( loggingVerbosity > 2 )
286 std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
287 }
288
289 if( photon.size() == 0 && skim )
290 continue;
291 std::sort( photon.begin(), photon.end(), tree::EtGreater);
292 if( loggingVerbosity > 1 )
293 std::cout << "Found " << photon.size() << " photons" << std::endl;
294
295 // jets
296 std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event->pfJets.find("ak5");
297 if(pfJets_it == event->pfJets.end()){
298 if(event->pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
299 } else {
300
301 susy::PFJetCollection& jetColl = pfJets_it->second;
302
303 for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
304 it != jetColl.end(); ++it) {
305 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
306 if (s_it == it->jecScaleFactors.end()) {
307 std::cout << "JEC is not available for this jet!!!" << std::endl;
308 continue;
309 }
310 float scale = s_it->second;
311 TLorentzVector corrP4 = scale * it->momentum;
312
313 if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
314 if(corrP4.Et() < 30 && skim ) continue;
315 thisjet->pt = corrP4.Et();
316 thisjet->eta = corrP4.Eta();
317 thisjet->phi = corrP4.Phi();
318 thisjet->bCSV = it->bTagDiscriminators[susy::kCSV];
319 // jet composition
320 thisjet->chargedHadronEnergy = it->chargedHadronEnergy;
321 thisjet->neutralHadronEnergy = it->neutralHadronEnergy;
322 thisjet->photonEnergy = it->photonEnergy;
323 thisjet->electronEnergy = it->electronEnergy;
324 thisjet->muonEnergy = it->muonEnergy;
325 thisjet->HFHadronEnergy = it->HFHadronEnergy;
326 thisjet->HFEMEnergy = it->HFEMEnergy;
327 thisjet->chargedEmEnergy = it->chargedEmEnergy;
328 thisjet->chargedMuEnergy = it->chargedMuEnergy;
329 thisjet->neutralEmEnergy = it->neutralEmEnergy;
330
331 if( loggingVerbosity > 2 )
332 std::cout << " p_T, jet = " << thisjet->pt << std::endl;
333
334 jet.push_back( *thisjet );
335 ht += thisjet->pt;
336 }// for jet
337 }// if, else
338 if( jet.size() < 2 && skim )
339 continue;
340 std::sort( jet.begin(), jet.end(), tree::EtGreater);
341 if( loggingVerbosity > 1 )
342 std::cout << "Found " << jet.size() << " jets" << std::endl;
343
344
345 // met
346 std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet");
347 susy::MET* metobj = &(met_it->second);
348 met = metobj->met();
349 met_phi = metobj->mEt.Phi();
350 if( loggingVerbosity > 2 )
351 std::cout << " met = " << met << std::endl;
352
353 std::map<TString, susy::MET>::iterator type1met_it = event->metMap.find("pfType1CorrectedMet");
354 susy::MET* type1metobj = &(type1met_it->second);
355 type1met = type1metobj->met();
356 type1met_phi = type1metobj->mEt.Phi();
357 if( loggingVerbosity > 2 )
358 std::cout << " type1met = " << type1met << std::endl;
359
360 // electrons
361 tree::Particle* thiselectron = new tree::Particle();
362 map<TString, vector<susy::Electron> >::iterator eleMap = event->electrons.find("gsfElectrons");
363 if(eleMap == event->electrons.end() && loggingVerbosity > 0) {
364 cout << "gsfElectrons not found!" << endl;
365 } else {
366 for(vector<susy::Electron>::iterator it = eleMap->second.begin(); it < eleMap->second.end(); ++it) {
367 // for cuts see https://twiki.cern.ch/twiki/bin/viewauth/CMS/EgammaCutBasedIdentification
368 float iso = ( it->chargedHadronIso + max(it->neutralHadronIso+it->photonIso
369 -effectiveAreaElectron(it->momentum.Eta())*event->rho25, (Float_t)0. )
370 ) / it->momentum.Pt();
371 if ( it->isEE() ){
372 if ( fabs(it->deltaEtaSuperClusterTrackAtVtx) > 0.007
373 || fabs(it->deltaPhiSuperClusterTrackAtVtx) > 0.15
374 || it->sigmaIetaIeta > 0.01
375 || it->hcalOverEcalBc > 0.12
376 || it->vertex.Perp() > 0.02
377 || it->vertex.Z() > 0.2
378 || fabs(1./(it->ecalEnergy) - 1./(it->trackMomentums["AtVtx"].P())) > 0.05
379 || it->convFlags() // not official, but perhaps substitude?
380 || iso > 0.15 )
381 continue;
382 }
383 else if( it->isEB() ) {
384 if ( fabs(it->deltaEtaSuperClusterTrackAtVtx) > 0.009
385 || fabs(it->deltaPhiSuperClusterTrackAtVtx) > 0.10
386 || it->sigmaIetaIeta > 0.03
387 || it->hcalOverEcalBc > 0.10
388 || it->vertex.Perp() > 0.02
389 || it->vertex.Z() > 0.2
390 || fabs(1./(it->ecalEnergy) - 1./(it->trackMomentums["AtVtx"].P())) > 0.05
391 || it->convFlags() // not official, but perhaps substitude?
392 || iso > 0.15 )
393 continue;
394 }
395 else // not in barrel nor in endcap
396 continue;
397 // TODO: conversion rejection information not implemented yet, see twiki for more details
398
399 thiselectron->pt = it->momentum.Et();
400 if( thiselectron->pt < 20 )
401 continue;
402 if( loggingVerbosity > 2 )
403 std::cout << " p_T, electron = " << it->momentum.Et() << std::endl;
404 thiselectron->eta = it->momentum.Eta();
405 thiselectron->phi = it->momentum.Phi();
406 electron.push_back( *thiselectron );
407 }
408 }
409 if( loggingVerbosity > 1 )
410 std::cout << "Found " << electron.size() << " electrons" << std::endl;
411
412 // muons
413 std::vector<susy::Muon> mVector = event->muons["muons"];
414 tree::Particle* thismuon = new tree::Particle();
415 for( std::vector<susy::Muon>::iterator it = mVector.begin(); it != mVector.end(); ++it) {
416 if( !( it->isPFMuon() && ( it->isGlobalMuon() || it->isTrackerMuon() ) ) )
417 continue; // see https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId#Loose_Muon
418 thismuon->pt = it->momentum.Et();
419 if( thismuon->pt < 20 )
420 continue;
421 thismuon->eta = it->momentum.Eta();
422 thismuon->phi = it->momentum.Phi();
423 muon.push_back( *thismuon );
424 }
425 if( loggingVerbosity > 1 )
426 std::cout << "Found " << muon.size() << " muons" << std::endl;
427
428
429 // vertices
430 nVertex = event->vertices.size();
431
432 if( ht < 450 && skim)
433 continue;
434
435 tree->Fill();
436 } // for jentry
437
438
439 outFile->cd();
440 tree->Write();
441 outFile->Write();
442 outFile->Close();
443 }
444