ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/kiesel/TreeWriter/treeWriter.cc
Revision: 1.8
Committed: Wed Apr 10 20:46:33 2013 UTC (12 years ago) by kiesel
Content type: text/plain
Branch: MAIN
Changes since 1.7: +64 -4 lines
Log Message:
rho correction for isolation

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
18 if (loggingVerbosity_ > 0)
19 std::cout << "Set Branch Address of susy::Event" << std::endl;
20 event = new susy::Event;
21 inputTree->SetBranchAddress("susyEvent", &event);
22
23 if (outputName == "") {
24 // Here the output filename is generated automaticly
25 try{
26 string fName = (string) inputName;
27 // Search for 'susyEvents_', and get the unique characters afterwards.
28 // eg /path/susyEvents_123_Myl.root -> susyTree_123_Mly.root
29 outputName = "susyTree_" + fName.substr(fName.find("susyEvents_")+11,-1);
30 } catch (int e ) {
31 outputName = "susyTree.root";
32 }
33 }
34
35 if (loggingVerbosity_>0)
36 std::cout << "Open file " << outputName << " for writing." << std::endl;
37 // open the output file
38 outFile = new TFile( outputName, "recreate" );
39 tree = new TTree("susyTree","Tree for single photon analysis");
40
41 // set default parameter
42 processNEvents = -1;
43 reportEvery = 1000;
44 loggingVerbosity = loggingVerbosity_;
45 skim = true;
46
47 }
48
49 TreeWriter::~TreeWriter() {
50 if (!inputTree) return;
51 delete inputTree->GetCurrentFile();
52 }
53
54 // useful functions
55 float TreeWriter::deltaR( TLorentzVector v1, TLorentzVector v2 ) {
56 return sqrt(pow(v1.Eta() - v2.Eta(), 2) + pow(v1.Phi() - v2.Phi(), 2) );
57 }
58
59 // correct iso, see https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedPhotonID2012
60 float chargedHadronIso_corrected(susy::Photon gamma, float rho) {
61 float eta = fabs(gamma.caloPosition.Eta());
62 float ea;
63
64 if(eta < 1.0) ea = 0.012;
65 else if(eta < 1.479) ea = 0.010;
66 else if(eta < 2.0) ea = 0.014;
67 else if(eta < 2.2) ea = 0.012;
68 else if(eta < 2.3) ea = 0.016;
69 else if(eta < 2.4) ea = 0.020;
70 else ea = 0.012;
71
72 float iso = gamma.chargedHadronIso;
73 iso = max(iso - rho*ea, (float)0.);
74
75 return iso;
76 }
77
78 float neutralHadronIso_corrected(susy::Photon gamma, float rho) {
79 float eta = fabs(gamma.caloPosition.Eta());
80 float ea;
81
82 if(eta < 1.0) ea = 0.030;
83 else if(eta < 1.479) ea = 0.057;
84 else if(eta < 2.0) ea = 0.039;
85 else if(eta < 2.2) ea = 0.015;
86 else if(eta < 2.3) ea = 0.024;
87 else if(eta < 2.4) ea = 0.039;
88 else ea = 0.072;
89
90 float iso = gamma.neutralHadronIso;
91 iso = max(iso - rho*ea, (float)0.);
92
93 return iso;
94 }
95
96 float photonIso_corrected(susy::Photon gamma, float rho) {
97 float eta = fabs(gamma.caloPosition.Eta());
98 float ea;
99
100 if(eta < 1.0) ea = 0.148;
101 else if(eta < 1.479) ea = 0.130;
102 else if(eta < 2.0) ea = 0.112;
103 else if(eta < 2.2) ea = 0.216;
104 else if(eta < 2.3) ea = 0.262;
105 else if(eta < 2.4) ea = 0.260;
106 else ea = 0.266;
107
108 float iso = gamma.photonIso;
109 iso = max(iso - rho*ea, (float)0.);
110
111 return iso;
112 }
113
114
115
116
117 float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
118 /**
119 * \brief Takes jet p_T as photon p_T
120 *
121 * At first all jets with DeltaR < 0.3 (isolation cone) are searched.
122 * If several jets are found, take the one with the minimal pt difference
123 * compared to the photon. If no such jets are found, keep the photon_pt
124 * TODO: remove photon matched jet from jet-selection?
125 */
126 std::vector<susy::PFJet> nearJets;
127 nearJets.clear();
128
129 std::map<TString,susy::PFJetCollection>::iterator pfJets_it = myEvent.pfJets.find("ak5");
130 if(pfJets_it == myEvent.pfJets.end()){
131 if(myEvent.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
132 } else {
133 susy::PFJetCollection& jetColl = pfJets_it->second;
134 for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
135 it != jetColl.end(); it++) {
136 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
137 if (s_it == it->jecScaleFactors.end()) {
138 std::cout << "JEC is not available for this jet!!!" << std::endl;
139 continue;
140 }
141 float scale = s_it->second;
142 TLorentzVector corrP4 = scale * it->momentum;
143 float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
144 if (deltaR_ > 0.3) continue;
145 if( loggingVerbosity > 0 )
146 std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
147 nearJets.push_back( *it );
148 }// for jet
149 }// if, else
150
151 if ( nearJets.size() == 0 ) {
152 //std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
153 return myPhoton.momentum.Et();
154 }
155
156 float pt = 0;
157 float minPtDifferenz = 1E20; // should be very high
158 for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
159 it != jetEnd; it++ ) {
160 float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
161 if ( ptDiff < minPtDifferenz ) {
162 minPtDifferenz = ptDiff;
163 pt = it->momentum.Et();
164 }
165 }
166
167 // testing
168 if( nearJets.size() > 1 && loggingVerbosity > 0 )
169 std::cout << "There are several jets matching to this photon. "
170 << "Please check if jet-matching is correct." << std::endl;
171 return pt;
172 }
173
174
175 void TreeWriter::Loop() {
176
177 // here the event loop is implemented and the tree is filled
178 if (inputTree == 0) return;
179
180 // get number of events to be proceeded
181 Long64_t nentries = inputTree->GetEntries();
182 if(processNEvents <= 0 || processNEvents > nentries) processNEvents = nentries;
183
184 if( loggingVerbosity > 0 )
185 std::cout << "Processing " << processNEvents << " ouf of "
186 << nentries << " events. " << std::endl;
187
188 tree::Photon *thisphoton = new tree::Photon();
189 tree::Jet *thisjet = new tree::Jet();
190
191 tree->Branch("photon", &photon);
192 tree->Branch("jet", &jet);
193 tree->Branch("met", &met, "met/F");
194 tree->Branch("ht", &ht, "ht/F");
195 tree->Branch("nVertex", &nVertex, "nVertex/I");
196 tree->Branch("nElectron", &nElectron, "nElectron/I");
197
198
199 for (long jentry=0; jentry < processNEvents; jentry++) {
200 if ( loggingVerbosity>0 && jentry%reportEvery==0 )
201 std::cout << jentry << " / " << processNEvents << std :: endl;
202 inputTree->GetEntry(jentry);
203
204 photon.clear();
205 jet.clear();
206 ht = 0;
207
208 // photons
209 if( loggingVerbosity > 1 )
210 std::cout << "Process photons" << std::endl;
211 std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
212 for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
213 it != phoMap->second.end() && phoMap != event->photons.end(); it++ ) {
214 if( ! it->isEB() && skim )
215 continue;
216 thisphoton->pt = getPtFromMatchedJet( *it, *event );
217 if( thisphoton->pt < 80 && skim )
218 continue;
219 thisphoton->eta = it->momentum.Eta();
220 thisphoton->phi = it->momentum.Phi();
221 thisphoton->chargedIso = chargedHadronIso_corrected(*it, event->rho25);
222 thisphoton->neutralIso = neutralHadronIso_corrected(*it, event->rho25);
223 thisphoton->photonIso = photonIso_corrected(*it, event->rho25);
224 if ( it->r9 >= 1 && skim )
225 continue;
226 thisphoton->r9 = it->r9;
227 thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
228 thisphoton->hadTowOverEm = it->hadTowOverEm;
229 thisphoton->pixelseed = it->nPixelSeeds;
230 photon.push_back( *thisphoton );
231 ht += thisphoton->pt;
232 if( loggingVerbosity > 2 )
233 std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
234 }
235
236 if( photon.size() == 0 && skim )
237 continue;
238 std::sort( photon.begin(), photon.end(), tree::EtGreater);
239 if( loggingVerbosity > 1 )
240 std::cout << "Found " << photon.size() << " photons" << std::endl;
241
242 // jets
243 std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event->pfJets.find("ak5");
244 if(pfJets_it == event->pfJets.end()){
245 if(event->pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
246 } else {
247
248 susy::PFJetCollection& jetColl = pfJets_it->second;
249
250 for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
251 it != jetColl.end(); it++) {
252 std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
253 if (s_it == it->jecScaleFactors.end()) {
254 std::cout << "JEC is not available for this jet!!!" << std::endl;
255 continue;
256 }
257 float scale = s_it->second;
258 TLorentzVector corrP4 = scale * it->momentum;
259
260 if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
261 if(corrP4.Et() < 30 && skim ) continue;
262 thisjet->pt = corrP4.Et();
263 thisjet->eta = corrP4.Eta();
264 thisjet->phi = corrP4.Phi();
265 jet.push_back( *thisjet );
266 ht += thisjet->pt;
267 }// for jet
268 }// if, else
269 if( jet.size() < 2 && skim )
270 continue;
271 std::sort( jet.begin(), jet.end(), tree::EtGreater);
272
273 // met
274 std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet");
275 susy::MET* metobj = &(met_it->second);
276 met = metobj->met();
277
278 // electrons
279 //std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
280 //nElectron = eVector.size();
281
282 // vertices
283
284 nVertex = event->vertices.size();
285
286 if( ht < 450 && skim)
287 continue;
288
289 tree->Fill();
290 } // for jentry
291
292
293
294 tree->Write();
295 outFile->cd();
296 outFile->Write();
297 outFile->Close();
298 }
299