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

# User Rev Content
1 kiesel 1.2 #include<iostream>
2 kiesel 1.4 #include<math.h>
3 kiesel 1.7 #include<string>
4 kiesel 1.2
5 kiesel 1.5 #include "TSystem.h"
6    
7     #include "treeWriter.h"
8    
9     using namespace std;
10 kiesel 1.1
11 kiesel 1.9 TreeWriter::TreeWriter( TString inputName, TString outputName, int loggingVerbosity_ ) {
12 kiesel 1.1 // read the input file
13 kiesel 1.5 inputTree = new TChain("susyTree");
14 kiesel 1.6 if (loggingVerbosity_ > 0)
15     std::cout << "Add files to chain" << std::endl;
16 kiesel 1.5 inputTree->Add( inputName );
17 kiesel 1.10 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 kiesel 1.5
28 kiesel 1.6 if (loggingVerbosity_ > 0)
29     std::cout << "Set Branch Address of susy::Event" << std::endl;
30 kiesel 1.1 event = new susy::Event;
31     inputTree->SetBranchAddress("susyEvent", &event);
32    
33 kiesel 1.9 // open the output file
34 kiesel 1.7 if (loggingVerbosity_>0)
35     std::cout << "Open file " << outputName << " for writing." << std::endl;
36 kiesel 1.1 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 kiesel 1.6 loggingVerbosity = loggingVerbosity_;
43 kiesel 1.5 skim = true;
44 kiesel 1.9 pileupHisto = 0;
45     }
46 kiesel 1.1
47 kiesel 1.9 void TreeWriter::PileUpWeightFile( string pileupFileName ) {
48     TFile *puFile = new TFile( pileupFileName.c_str() );
49     pileupHisto = (TH1F*) puFile->Get("pileup");
50 kiesel 1.1 }
51    
52     TreeWriter::~TreeWriter() {
53 kiesel 1.9 if (pileupHisto != 0 )
54     delete pileupHisto;
55     inputTree->GetCurrentFile()->Close();
56 kiesel 1.1 }
57    
58 kiesel 1.8 // useful functions
59 kiesel 1.4 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 kiesel 1.12 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 kiesel 1.8 // correct iso, see https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedPhotonID2012
79     float chargedHadronIso_corrected(susy::Photon gamma, float rho) {
80 kiesel 1.12 float eta = fabs(gamma.caloPosition.Eta());
81     float ea;
82 kiesel 1.8
83 kiesel 1.12 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 kiesel 1.8
91 kiesel 1.12 float iso = gamma.chargedHadronIso;
92     iso = max(iso - rho*ea, (float)0.);
93 kiesel 1.8
94 kiesel 1.12 return iso;
95 kiesel 1.8 }
96    
97     float neutralHadronIso_corrected(susy::Photon gamma, float rho) {
98 kiesel 1.12 float eta = fabs(gamma.caloPosition.Eta());
99     float ea;
100 kiesel 1.8
101 kiesel 1.12 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 kiesel 1.8
109 kiesel 1.12 float iso = gamma.neutralHadronIso;
110     iso = max(iso - rho*ea, (float)0.);
111 kiesel 1.8
112 kiesel 1.12 return iso;
113 kiesel 1.8 }
114    
115     float photonIso_corrected(susy::Photon gamma, float rho) {
116 kiesel 1.12 float eta = fabs(gamma.caloPosition.Eta());
117     float ea;
118 kiesel 1.8
119 kiesel 1.12 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 kiesel 1.8
127 kiesel 1.12 float iso = gamma.photonIso;
128     iso = max(iso - rho*ea, (float)0.);
129 kiesel 1.8
130 kiesel 1.12 return iso;
131 kiesel 1.8 }
132    
133 kiesel 1.5 float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
134 kiesel 1.4 /**
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 kiesel 1.5 * compared to the photon. If no such jets are found, keep the photon_pt
140 kiesel 1.4 * TODO: remove photon matched jet from jet-selection?
141     */
142     std::vector<susy::PFJet> nearJets;
143     nearJets.clear();
144    
145 kiesel 1.5 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 kiesel 1.4 } else {
149     susy::PFJetCollection& jetColl = pfJets_it->second;
150     for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
151 kiesel 1.9 it != jetColl.end(); ++it) {
152 kiesel 1.4 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 kiesel 1.5 float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
160 kiesel 1.4 if (deltaR_ > 0.3) continue;
161 kiesel 1.5 if( loggingVerbosity > 0 )
162     std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
163 kiesel 1.4 nearJets.push_back( *it );
164     }// for jet
165     }// if, else
166    
167     if ( nearJets.size() == 0 ) {
168 kiesel 1.12 if( loggingVerbosity > 1 )
169     std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
170 kiesel 1.5 return myPhoton.momentum.Et();
171 kiesel 1.4 }
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 kiesel 1.9 it != jetEnd; ++it ) {
177 kiesel 1.5 float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
178 kiesel 1.4 if ( ptDiff < minPtDifferenz ) {
179     minPtDifferenz = ptDiff;
180     pt = it->momentum.Et();
181     }
182     }
183    
184     // testing
185 kiesel 1.6 if( nearJets.size() > 1 && loggingVerbosity > 0 )
186 kiesel 1.4 std::cout << "There are several jets matching to this photon. "
187 kiesel 1.6 << "Please check if jet-matching is correct." << std::endl;
188 kiesel 1.4 return pt;
189     }
190    
191    
192 kiesel 1.1 void TreeWriter::Loop() {
193 kiesel 1.4
194 kiesel 1.1 // 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 kiesel 1.12 tree->Branch("electron", &electron);
211 kiesel 1.9 tree->Branch("muon", &muon);
212 kiesel 1.1 tree->Branch("met", &met, "met/F");
213 kiesel 1.11 tree->Branch("metPhi", &met_phi, "metPhi/F");
214     tree->Branch("type1met", &type1met, "type1met/F");
215     tree->Branch("type1metPhi", &type1met_phi, "type1metPhi/F");
216 kiesel 1.5 tree->Branch("ht", &ht, "ht/F");
217 kiesel 1.1 tree->Branch("nVertex", &nVertex, "nVertex/I");
218 kiesel 1.9 tree->Branch("pu_weight", &pu_weight, "pu_weight/F");
219 kiesel 1.1
220    
221 kiesel 1.9 for (long jentry=0; jentry < processNEvents; ++jentry) {
222 kiesel 1.4 if ( loggingVerbosity>0 && jentry%reportEvery==0 )
223 kiesel 1.1 std::cout << jentry << " / " << processNEvents << std :: endl;
224     inputTree->GetEntry(jentry);
225    
226     photon.clear();
227     jet.clear();
228 kiesel 1.9 electron.clear();
229     muon.clear();
230 kiesel 1.5 ht = 0;
231 kiesel 1.1
232 kiesel 1.9 // 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 kiesel 1.1 // photons
247 kiesel 1.5 if( loggingVerbosity > 1 )
248 kiesel 1.6 std::cout << "Process photons" << std::endl;
249 kiesel 1.1 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 kiesel 1.9 it != phoMap->second.end() && phoMap != event->photons.end(); ++it ) {
252 kiesel 1.12 if( !(it->isEB() || it->isEE()) && skim )
253 kiesel 1.1 continue;
254 kiesel 1.4 thisphoton->pt = getPtFromMatchedJet( *it, *event );
255 kiesel 1.12
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 kiesel 1.1 continue;
277     thisphoton->eta = it->momentum.Eta();
278 kiesel 1.4 thisphoton->phi = it->momentum.Phi();
279 kiesel 1.1 thisphoton->r9 = it->r9;
280     thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
281     thisphoton->hadTowOverEm = it->hadTowOverEm;
282     thisphoton->pixelseed = it->nPixelSeeds;
283 kiesel 1.11 thisphoton->conversionSafeVeto = it->passelectronveto;
284 kiesel 1.1 photon.push_back( *thisphoton );
285 kiesel 1.5 if( loggingVerbosity > 2 )
286 kiesel 1.6 std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
287 kiesel 1.1 }
288 kiesel 1.5
289 kiesel 1.4 if( photon.size() == 0 && skim )
290 kiesel 1.1 continue;
291     std::sort( photon.begin(), photon.end(), tree::EtGreater);
292 kiesel 1.5 if( loggingVerbosity > 1 )
293     std::cout << "Found " << photon.size() << " photons" << std::endl;
294 kiesel 1.1
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 kiesel 1.9 it != jetColl.end(); ++it) {
305 kiesel 1.1 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 kiesel 1.4 if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
314 kiesel 1.8 if(corrP4.Et() < 30 && skim ) continue;
315 kiesel 1.1 thisjet->pt = corrP4.Et();
316     thisjet->eta = corrP4.Eta();
317 kiesel 1.4 thisjet->phi = corrP4.Phi();
318 kiesel 1.9 thisjet->bCSV = it->bTagDiscriminators[susy::kCSV];
319 kiesel 1.12 // 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 kiesel 1.9 if( loggingVerbosity > 2 )
332     std::cout << " p_T, jet = " << thisjet->pt << std::endl;
333    
334 kiesel 1.1 jet.push_back( *thisjet );
335 kiesel 1.5 ht += thisjet->pt;
336 kiesel 1.1 }// for jet
337     }// if, else
338 kiesel 1.4 if( jet.size() < 2 && skim )
339     continue;
340     std::sort( jet.begin(), jet.end(), tree::EtGreater);
341 kiesel 1.9 if( loggingVerbosity > 1 )
342     std::cout << "Found " << jet.size() << " jets" << std::endl;
343    
344 kiesel 1.1
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 kiesel 1.9 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 kiesel 1.1
360 kiesel 1.4 // electrons
361 kiesel 1.9 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 kiesel 1.12 // 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 kiesel 1.9 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 kiesel 1.10 if( !( it->isPFMuon() && ( it->isGlobalMuon() || it->isTrackerMuon() ) ) )
417     continue; // see https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId#Loose_Muon
418 kiesel 1.9 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 kiesel 1.4
429 kiesel 1.1 // vertices
430     nVertex = event->vertices.size();
431    
432 kiesel 1.5 if( ht < 450 && skim)
433     continue;
434 kiesel 1.4
435 kiesel 1.1 tree->Fill();
436     } // for jentry
437    
438    
439 kiesel 1.9 outFile->cd();
440 kiesel 1.1 tree->Write();
441     outFile->Write();
442     outFile->Close();
443     }
444 kiesel 1.3