8 |
|
|
9 |
|
using namespace std; |
10 |
|
|
11 |
< |
TreeWriter::TreeWriter(TString inputName, TString outputName, int loggingVerbosity_ ) { |
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 |
< |
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 |
< |
|
33 |
> |
// open the output file |
34 |
|
if (loggingVerbosity_>0) |
35 |
|
std::cout << "Open file " << outputName << " for writing." << std::endl; |
37 |
– |
// open the output file |
36 |
|
outFile = new TFile( outputName, "recreate" ); |
37 |
|
tree = new TTree("susyTree","Tree for single photon analysis"); |
38 |
|
|
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 (!inputTree) return; |
54 |
< |
delete inputTree->GetCurrentFile(); |
53 |
> |
if (pileupHisto != 0 ) |
54 |
> |
delete pileupHisto; |
55 |
> |
inputTree->GetCurrentFile()->Close(); |
56 |
|
} |
57 |
|
|
58 |
|
// useful functions |
115 |
|
return iso; |
116 |
|
} |
117 |
|
|
114 |
– |
|
115 |
– |
|
116 |
– |
|
118 |
|
float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) { |
119 |
|
/** |
120 |
|
* \brief Takes jet p_T as photon p_T |
133 |
|
} else { |
134 |
|
susy::PFJetCollection& jetColl = pfJets_it->second; |
135 |
|
for(std::vector<susy::PFJet>::iterator it = jetColl.begin(); |
136 |
< |
it != jetColl.end(); it++) { |
136 |
> |
it != jetColl.end(); ++it) { |
137 |
|
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; |
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 |
< |
it != jetEnd; it++ ) { |
160 |
> |
it != jetEnd; ++it ) { |
161 |
|
float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et()); |
162 |
|
if ( ptDiff < minPtDifferenz ) { |
163 |
|
minPtDifferenz = ptDiff; |
191 |
|
|
192 |
|
tree->Branch("photon", &photon); |
193 |
|
tree->Branch("jet", &jet); |
194 |
+ |
//tree->Branch("electron", &electron); |
195 |
+ |
tree->Branch("muon", &muon); |
196 |
|
tree->Branch("met", &met, "met/F"); |
197 |
|
tree->Branch("ht", &ht, "ht/F"); |
198 |
|
tree->Branch("nVertex", &nVertex, "nVertex/I"); |
199 |
< |
tree->Branch("nElectron", &nElectron, "nElectron/I"); |
199 |
> |
tree->Branch("pu_weight", &pu_weight, "pu_weight/F"); |
200 |
|
|
201 |
|
|
202 |
< |
for (long jentry=0; jentry < processNEvents; jentry++) { |
202 |
> |
for (long jentry=0; jentry < processNEvents; ++jentry) { |
203 |
|
if ( loggingVerbosity>0 && jentry%reportEvery==0 ) |
204 |
|
std::cout << jentry << " / " << processNEvents << std :: endl; |
205 |
|
inputTree->GetEntry(jentry); |
206 |
|
|
207 |
|
photon.clear(); |
208 |
|
jet.clear(); |
209 |
+ |
electron.clear(); |
210 |
+ |
muon.clear(); |
211 |
|
ht = 0; |
212 |
|
|
213 |
+ |
// weights |
214 |
+ |
if (pileupHisto == 0) { |
215 |
+ |
pu_weight = 1.; |
216 |
+ |
} else { |
217 |
+ |
float trueNumInteractions = -1; |
218 |
+ |
for( susy::PUSummaryInfoCollection::const_iterator iBX = event->pu.begin(); |
219 |
+ |
iBX != event->pu.end() && trueNumInteractions < 0; ++iBX) { |
220 |
+ |
if (iBX->BX == 0) |
221 |
+ |
trueNumInteractions = iBX->trueNumInteractions; |
222 |
+ |
} |
223 |
+ |
pu_weight = pileupHisto->GetBinContent( pileupHisto->FindBin( trueNumInteractions ) ); |
224 |
+ |
} |
225 |
+ |
|
226 |
+ |
|
227 |
|
// photons |
228 |
|
if( loggingVerbosity > 1 ) |
229 |
|
std::cout << "Process photons" << std::endl; |
230 |
|
std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons"); |
231 |
|
for(std::vector<susy::Photon>::iterator it = phoMap->second.begin(); |
232 |
< |
it != phoMap->second.end() && phoMap != event->photons.end(); it++ ) { |
232 |
> |
it != phoMap->second.end() && phoMap != event->photons.end(); ++it ) { |
233 |
|
if( ! it->isEB() && skim ) |
234 |
|
continue; |
235 |
|
thisphoton->pt = getPtFromMatchedJet( *it, *event ); |
240 |
|
thisphoton->chargedIso = chargedHadronIso_corrected(*it, event->rho25); |
241 |
|
thisphoton->neutralIso = neutralHadronIso_corrected(*it, event->rho25); |
242 |
|
thisphoton->photonIso = photonIso_corrected(*it, event->rho25); |
224 |
– |
if ( it->r9 >= 1 && skim ) |
225 |
– |
continue; |
243 |
|
thisphoton->r9 = it->r9; |
244 |
|
thisphoton->sigmaIetaIeta = it->sigmaIetaIeta; |
245 |
|
thisphoton->hadTowOverEm = it->hadTowOverEm; |
246 |
|
thisphoton->pixelseed = it->nPixelSeeds; |
247 |
|
photon.push_back( *thisphoton ); |
231 |
– |
ht += thisphoton->pt; |
248 |
|
if( loggingVerbosity > 2 ) |
249 |
|
std::cout << " p_T, gamma = " << thisphoton->pt << std::endl; |
250 |
|
} |
264 |
|
susy::PFJetCollection& jetColl = pfJets_it->second; |
265 |
|
|
266 |
|
for(std::vector<susy::PFJet>::iterator it = jetColl.begin(); |
267 |
< |
it != jetColl.end(); it++) { |
267 |
> |
it != jetColl.end(); ++it) { |
268 |
|
std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3"); |
269 |
|
if (s_it == it->jecScaleFactors.end()) { |
270 |
|
std::cout << "JEC is not available for this jet!!!" << std::endl; |
278 |
|
thisjet->pt = corrP4.Et(); |
279 |
|
thisjet->eta = corrP4.Eta(); |
280 |
|
thisjet->phi = corrP4.Phi(); |
281 |
+ |
thisjet->bCSV = it->bTagDiscriminators[susy::kCSV]; |
282 |
+ |
if( loggingVerbosity > 2 ) |
283 |
+ |
std::cout << " p_T, jet = " << thisjet->pt << std::endl; |
284 |
+ |
|
285 |
|
jet.push_back( *thisjet ); |
286 |
|
ht += thisjet->pt; |
287 |
|
}// for jet |
289 |
|
if( jet.size() < 2 && skim ) |
290 |
|
continue; |
291 |
|
std::sort( jet.begin(), jet.end(), tree::EtGreater); |
292 |
+ |
if( loggingVerbosity > 1 ) |
293 |
+ |
std::cout << "Found " << jet.size() << " jets" << std::endl; |
294 |
+ |
|
295 |
|
|
296 |
|
// met |
297 |
|
std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet"); |
298 |
|
susy::MET* metobj = &(met_it->second); |
299 |
|
met = metobj->met(); |
300 |
+ |
met_phi = metobj->mEt.Phi(); |
301 |
+ |
if( loggingVerbosity > 2 ) |
302 |
+ |
std::cout << " met = " << met << std::endl; |
303 |
+ |
|
304 |
+ |
std::map<TString, susy::MET>::iterator type1met_it = event->metMap.find("pfType1CorrectedMet"); |
305 |
+ |
susy::MET* type1metobj = &(type1met_it->second); |
306 |
+ |
type1met = type1metobj->met(); |
307 |
+ |
type1met_phi = type1metobj->mEt.Phi(); |
308 |
+ |
if( loggingVerbosity > 2 ) |
309 |
+ |
std::cout << " type1met = " << type1met << std::endl; |
310 |
|
|
311 |
|
// electrons |
312 |
< |
//std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"]; |
313 |
< |
//nElectron = eVector.size(); |
312 |
> |
/* |
313 |
> |
tree::Particle* thiselectron = new tree::Particle(); |
314 |
> |
map<TString, vector<susy::Electron> >::iterator eleMap = event->electrons.find("gsfElectrons"); |
315 |
> |
if(eleMap == event->electrons.end() && loggingVerbosity > 0) { |
316 |
> |
cout << "gsfElectrons not found!" << endl; |
317 |
> |
} else { |
318 |
> |
for(vector<susy::Electron>::iterator it = eleMap->second.begin(); it < eleMap->second.end(); ++it) { |
319 |
> |
thiselectron->pt = it->momentum.Et(); |
320 |
> |
if( thiselectron->pt < 20 ) |
321 |
> |
continue; |
322 |
> |
if( loggingVerbosity > 2 ) |
323 |
> |
std::cout << " p_T, electron = " << it->momentum.Et() << std::endl; |
324 |
> |
thiselectron->eta = it->momentum.Eta(); |
325 |
> |
thiselectron->phi = it->momentum.Phi(); |
326 |
> |
electron.push_back( *thiselectron ); |
327 |
> |
} |
328 |
> |
} |
329 |
> |
if( loggingVerbosity > 1 ) |
330 |
> |
std::cout << "Found " << electron.size() << " electrons" << std::endl; |
331 |
> |
*/ |
332 |
|
|
333 |
< |
// vertices |
333 |
> |
// this seems not to work yet, where is the bug? |
334 |
> |
/* |
335 |
> |
std::vector<susy::Electron> eVector = event->electrons["gsfElectronsx"]; |
336 |
> |
for( std::vector<susy::Electron>::iterator it = eVector.begin(); it != eVector.end(); ++it) { |
337 |
> |
thiselectron->pt = it->momentum.Et(); |
338 |
> |
if( thiselectron->pt < 20 ) |
339 |
> |
continue; |
340 |
> |
if( loggingVerbosity > 2 ) |
341 |
> |
std::cout << " p_T, electron = " << it->momentum.Et() << std::endl; |
342 |
> |
thiselectron->eta = it->momentum.Eta(); |
343 |
> |
thiselectron->phi = it->momentum.Phi(); |
344 |
> |
electron.push_back( *thiselectron ); |
345 |
> |
} |
346 |
> |
*/ |
347 |
> |
|
348 |
> |
// muons |
349 |
> |
std::vector<susy::Muon> mVector = event->muons["muons"]; |
350 |
> |
tree::Particle* thismuon = new tree::Particle(); |
351 |
> |
for( std::vector<susy::Muon>::iterator it = mVector.begin(); it != mVector.end(); ++it) { |
352 |
> |
if( !( it->isPFMuon() && ( it->isGlobalMuon() || it->isTrackerMuon() ) ) ) |
353 |
> |
continue; // see https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId#Loose_Muon |
354 |
> |
thismuon->pt = it->momentum.Et(); |
355 |
> |
if( thismuon->pt < 20 ) |
356 |
> |
continue; |
357 |
> |
thismuon->eta = it->momentum.Eta(); |
358 |
> |
thismuon->phi = it->momentum.Phi(); |
359 |
> |
muon.push_back( *thismuon ); |
360 |
> |
} |
361 |
> |
if( loggingVerbosity > 1 ) |
362 |
> |
std::cout << "Found " << muon.size() << " muons" << std::endl; |
363 |
|
|
364 |
+ |
|
365 |
+ |
// vertices |
366 |
|
nVertex = event->vertices.size(); |
367 |
|
|
368 |
|
if( ht < 450 && skim) |
372 |
|
} // for jentry |
373 |
|
|
374 |
|
|
293 |
– |
|
294 |
– |
tree->Write(); |
375 |
|
outFile->cd(); |
376 |
+ |
tree->Write(); |
377 |
|
outFile->Write(); |
378 |
|
outFile->Close(); |
379 |
|
} |