ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/kiesel/TreeWriter/treeWriter.cc
Revision: 1.11
Committed: Mon Apr 15 16:01:40 2013 UTC (12 years ago) by kiesel
Content type: text/plain
Branch: MAIN
Changes since 1.10: +4 -0 lines
Log Message:
added conversionSafeInfo for photon and put met info in tree

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.8 // correct iso, see https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedPhotonID2012
64     float chargedHadronIso_corrected(susy::Photon gamma, float rho) {
65     float eta = fabs(gamma.caloPosition.Eta());
66     float ea;
67    
68     if(eta < 1.0) ea = 0.012;
69     else if(eta < 1.479) ea = 0.010;
70     else if(eta < 2.0) ea = 0.014;
71     else if(eta < 2.2) ea = 0.012;
72     else if(eta < 2.3) ea = 0.016;
73     else if(eta < 2.4) ea = 0.020;
74     else ea = 0.012;
75    
76     float iso = gamma.chargedHadronIso;
77     iso = max(iso - rho*ea, (float)0.);
78    
79     return iso;
80     }
81    
82     float neutralHadronIso_corrected(susy::Photon gamma, float rho) {
83     float eta = fabs(gamma.caloPosition.Eta());
84     float ea;
85    
86     if(eta < 1.0) ea = 0.030;
87     else if(eta < 1.479) ea = 0.057;
88     else if(eta < 2.0) ea = 0.039;
89     else if(eta < 2.2) ea = 0.015;
90     else if(eta < 2.3) ea = 0.024;
91     else if(eta < 2.4) ea = 0.039;
92     else ea = 0.072;
93    
94     float iso = gamma.neutralHadronIso;
95     iso = max(iso - rho*ea, (float)0.);
96    
97     return iso;
98     }
99    
100     float photonIso_corrected(susy::Photon gamma, float rho) {
101     float eta = fabs(gamma.caloPosition.Eta());
102     float ea;
103    
104     if(eta < 1.0) ea = 0.148;
105     else if(eta < 1.479) ea = 0.130;
106     else if(eta < 2.0) ea = 0.112;
107     else if(eta < 2.2) ea = 0.216;
108     else if(eta < 2.3) ea = 0.262;
109     else if(eta < 2.4) ea = 0.260;
110     else ea = 0.266;
111    
112     float iso = gamma.photonIso;
113     iso = max(iso - rho*ea, (float)0.);
114    
115     return iso;
116     }
117    
118 kiesel 1.5 float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
119 kiesel 1.4 /**
120     * \brief Takes jet p_T as photon p_T
121     *
122     * At first all jets with DeltaR < 0.3 (isolation cone) are searched.
123     * If several jets are found, take the one with the minimal pt difference
124 kiesel 1.5 * compared to the photon. If no such jets are found, keep the photon_pt
125 kiesel 1.4 * TODO: remove photon matched jet from jet-selection?
126     */
127     std::vector<susy::PFJet> nearJets;
128     nearJets.clear();
129    
130 kiesel 1.5 std::map<TString,susy::PFJetCollection>::iterator pfJets_it = myEvent.pfJets.find("ak5");
131     if(pfJets_it == myEvent.pfJets.end()){
132     if(myEvent.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
133 kiesel 1.4 } else {
134     susy::PFJetCollection& jetColl = pfJets_it->second;
135     for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
136 kiesel 1.9 it != jetColl.end(); ++it) {
137 kiesel 1.4 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
138     if (s_it == it->jecScaleFactors.end()) {
139     std::cout << "JEC is not available for this jet!!!" << std::endl;
140     continue;
141     }
142     float scale = s_it->second;
143     TLorentzVector corrP4 = scale * it->momentum;
144 kiesel 1.5 float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
145 kiesel 1.4 if (deltaR_ > 0.3) continue;
146 kiesel 1.5 if( loggingVerbosity > 0 )
147     std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
148 kiesel 1.4 nearJets.push_back( *it );
149     }// for jet
150     }// if, else
151    
152     if ( nearJets.size() == 0 ) {
153 kiesel 1.5 //std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
154     return myPhoton.momentum.Et();
155 kiesel 1.4 }
156    
157     float pt = 0;
158     float minPtDifferenz = 1E20; // should be very high
159     for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
160 kiesel 1.9 it != jetEnd; ++it ) {
161 kiesel 1.5 float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
162 kiesel 1.4 if ( ptDiff < minPtDifferenz ) {
163     minPtDifferenz = ptDiff;
164     pt = it->momentum.Et();
165     }
166     }
167    
168     // testing
169 kiesel 1.6 if( nearJets.size() > 1 && loggingVerbosity > 0 )
170 kiesel 1.4 std::cout << "There are several jets matching to this photon. "
171 kiesel 1.6 << "Please check if jet-matching is correct." << std::endl;
172 kiesel 1.4 return pt;
173     }
174    
175    
176 kiesel 1.1 void TreeWriter::Loop() {
177 kiesel 1.4
178 kiesel 1.1 // here the event loop is implemented and the tree is filled
179     if (inputTree == 0) return;
180    
181     // get number of events to be proceeded
182     Long64_t nentries = inputTree->GetEntries();
183     if(processNEvents <= 0 || processNEvents > nentries) processNEvents = nentries;
184    
185     if( loggingVerbosity > 0 )
186     std::cout << "Processing " << processNEvents << " ouf of "
187     << nentries << " events. " << std::endl;
188    
189     tree::Photon *thisphoton = new tree::Photon();
190     tree::Jet *thisjet = new tree::Jet();
191    
192     tree->Branch("photon", &photon);
193     tree->Branch("jet", &jet);
194 kiesel 1.10 //tree->Branch("electron", &electron);
195 kiesel 1.9 tree->Branch("muon", &muon);
196 kiesel 1.1 tree->Branch("met", &met, "met/F");
197 kiesel 1.11 tree->Branch("metPhi", &met_phi, "metPhi/F");
198     tree->Branch("type1met", &type1met, "type1met/F");
199     tree->Branch("type1metPhi", &type1met_phi, "type1metPhi/F");
200 kiesel 1.5 tree->Branch("ht", &ht, "ht/F");
201 kiesel 1.1 tree->Branch("nVertex", &nVertex, "nVertex/I");
202 kiesel 1.9 tree->Branch("pu_weight", &pu_weight, "pu_weight/F");
203 kiesel 1.1
204    
205 kiesel 1.9 for (long jentry=0; jentry < processNEvents; ++jentry) {
206 kiesel 1.4 if ( loggingVerbosity>0 && jentry%reportEvery==0 )
207 kiesel 1.1 std::cout << jentry << " / " << processNEvents << std :: endl;
208     inputTree->GetEntry(jentry);
209    
210     photon.clear();
211     jet.clear();
212 kiesel 1.9 electron.clear();
213     muon.clear();
214 kiesel 1.5 ht = 0;
215 kiesel 1.1
216 kiesel 1.9 // weights
217     if (pileupHisto == 0) {
218     pu_weight = 1.;
219     } else {
220     float trueNumInteractions = -1;
221     for( susy::PUSummaryInfoCollection::const_iterator iBX = event->pu.begin();
222     iBX != event->pu.end() && trueNumInteractions < 0; ++iBX) {
223     if (iBX->BX == 0)
224     trueNumInteractions = iBX->trueNumInteractions;
225     }
226     pu_weight = pileupHisto->GetBinContent( pileupHisto->FindBin( trueNumInteractions ) );
227     }
228    
229    
230 kiesel 1.1 // photons
231 kiesel 1.5 if( loggingVerbosity > 1 )
232 kiesel 1.6 std::cout << "Process photons" << std::endl;
233 kiesel 1.1 std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
234     for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
235 kiesel 1.9 it != phoMap->second.end() && phoMap != event->photons.end(); ++it ) {
236 kiesel 1.4 if( ! it->isEB() && skim )
237 kiesel 1.1 continue;
238 kiesel 1.4 thisphoton->pt = getPtFromMatchedJet( *it, *event );
239     if( thisphoton->pt < 80 && skim )
240 kiesel 1.1 continue;
241     thisphoton->eta = it->momentum.Eta();
242 kiesel 1.4 thisphoton->phi = it->momentum.Phi();
243 kiesel 1.8 thisphoton->chargedIso = chargedHadronIso_corrected(*it, event->rho25);
244     thisphoton->neutralIso = neutralHadronIso_corrected(*it, event->rho25);
245     thisphoton->photonIso = photonIso_corrected(*it, event->rho25);
246 kiesel 1.1 thisphoton->r9 = it->r9;
247     thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
248     thisphoton->hadTowOverEm = it->hadTowOverEm;
249     thisphoton->pixelseed = it->nPixelSeeds;
250 kiesel 1.11 thisphoton->conversionSafeVeto = it->passelectronveto;
251 kiesel 1.1 photon.push_back( *thisphoton );
252 kiesel 1.5 if( loggingVerbosity > 2 )
253 kiesel 1.6 std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
254 kiesel 1.1 }
255 kiesel 1.5
256 kiesel 1.4 if( photon.size() == 0 && skim )
257 kiesel 1.1 continue;
258     std::sort( photon.begin(), photon.end(), tree::EtGreater);
259 kiesel 1.5 if( loggingVerbosity > 1 )
260     std::cout << "Found " << photon.size() << " photons" << std::endl;
261 kiesel 1.1
262     // jets
263     std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event->pfJets.find("ak5");
264     if(pfJets_it == event->pfJets.end()){
265     if(event->pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
266     } else {
267    
268     susy::PFJetCollection& jetColl = pfJets_it->second;
269    
270     for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
271 kiesel 1.9 it != jetColl.end(); ++it) {
272 kiesel 1.1 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
273     if (s_it == it->jecScaleFactors.end()) {
274     std::cout << "JEC is not available for this jet!!!" << std::endl;
275     continue;
276     }
277     float scale = s_it->second;
278     TLorentzVector corrP4 = scale * it->momentum;
279    
280 kiesel 1.4 if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
281 kiesel 1.8 if(corrP4.Et() < 30 && skim ) continue;
282 kiesel 1.1 thisjet->pt = corrP4.Et();
283     thisjet->eta = corrP4.Eta();
284 kiesel 1.4 thisjet->phi = corrP4.Phi();
285 kiesel 1.9 thisjet->bCSV = it->bTagDiscriminators[susy::kCSV];
286     if( loggingVerbosity > 2 )
287     std::cout << " p_T, jet = " << thisjet->pt << std::endl;
288    
289 kiesel 1.1 jet.push_back( *thisjet );
290 kiesel 1.5 ht += thisjet->pt;
291 kiesel 1.1 }// for jet
292     }// if, else
293 kiesel 1.4 if( jet.size() < 2 && skim )
294     continue;
295     std::sort( jet.begin(), jet.end(), tree::EtGreater);
296 kiesel 1.9 if( loggingVerbosity > 1 )
297     std::cout << "Found " << jet.size() << " jets" << std::endl;
298    
299 kiesel 1.1
300     // met
301     std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet");
302     susy::MET* metobj = &(met_it->second);
303     met = metobj->met();
304 kiesel 1.9 met_phi = metobj->mEt.Phi();
305     if( loggingVerbosity > 2 )
306     std::cout << " met = " << met << std::endl;
307    
308     std::map<TString, susy::MET>::iterator type1met_it = event->metMap.find("pfType1CorrectedMet");
309     susy::MET* type1metobj = &(type1met_it->second);
310     type1met = type1metobj->met();
311     type1met_phi = type1metobj->mEt.Phi();
312     if( loggingVerbosity > 2 )
313     std::cout << " type1met = " << type1met << std::endl;
314 kiesel 1.1
315 kiesel 1.4 // electrons
316 kiesel 1.10 /*
317 kiesel 1.9 tree::Particle* thiselectron = new tree::Particle();
318     map<TString, vector<susy::Electron> >::iterator eleMap = event->electrons.find("gsfElectrons");
319     if(eleMap == event->electrons.end() && loggingVerbosity > 0) {
320     cout << "gsfElectrons not found!" << endl;
321     } else {
322     for(vector<susy::Electron>::iterator it = eleMap->second.begin(); it < eleMap->second.end(); ++it) {
323     thiselectron->pt = it->momentum.Et();
324     if( thiselectron->pt < 20 )
325     continue;
326     if( loggingVerbosity > 2 )
327     std::cout << " p_T, electron = " << it->momentum.Et() << std::endl;
328     thiselectron->eta = it->momentum.Eta();
329     thiselectron->phi = it->momentum.Phi();
330     electron.push_back( *thiselectron );
331     }
332     }
333     if( loggingVerbosity > 1 )
334     std::cout << "Found " << electron.size() << " electrons" << std::endl;
335 kiesel 1.10 */
336 kiesel 1.9
337     // this seems not to work yet, where is the bug?
338     /*
339     std::vector<susy::Electron> eVector = event->electrons["gsfElectronsx"];
340     for( std::vector<susy::Electron>::iterator it = eVector.begin(); it != eVector.end(); ++it) {
341     thiselectron->pt = it->momentum.Et();
342     if( thiselectron->pt < 20 )
343     continue;
344     if( loggingVerbosity > 2 )
345     std::cout << " p_T, electron = " << it->momentum.Et() << std::endl;
346     thiselectron->eta = it->momentum.Eta();
347     thiselectron->phi = it->momentum.Phi();
348     electron.push_back( *thiselectron );
349     }
350     */
351    
352     // muons
353     std::vector<susy::Muon> mVector = event->muons["muons"];
354     tree::Particle* thismuon = new tree::Particle();
355     for( std::vector<susy::Muon>::iterator it = mVector.begin(); it != mVector.end(); ++it) {
356 kiesel 1.10 if( !( it->isPFMuon() && ( it->isGlobalMuon() || it->isTrackerMuon() ) ) )
357     continue; // see https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId#Loose_Muon
358 kiesel 1.9 thismuon->pt = it->momentum.Et();
359     if( thismuon->pt < 20 )
360     continue;
361     thismuon->eta = it->momentum.Eta();
362     thismuon->phi = it->momentum.Phi();
363     muon.push_back( *thismuon );
364     }
365     if( loggingVerbosity > 1 )
366     std::cout << "Found " << muon.size() << " muons" << std::endl;
367    
368 kiesel 1.4
369 kiesel 1.1 // vertices
370     nVertex = event->vertices.size();
371    
372 kiesel 1.5 if( ht < 450 && skim)
373     continue;
374 kiesel 1.4
375 kiesel 1.1 tree->Fill();
376     } // for jentry
377    
378    
379 kiesel 1.9 outFile->cd();
380 kiesel 1.1 tree->Write();
381     outFile->Write();
382     outFile->Close();
383     }
384 kiesel 1.3