ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/kiesel/TreeWriter/treeWriter.cc
(Generate patch)

Comparing UserCode/kiesel/TreeWriter/treeWriter.cc (file contents):
Revision 1.8 by kiesel, Wed Apr 10 20:46:33 2013 UTC vs.
Revision 1.14 by kiesel, Wed Apr 17 09:57:52 2013 UTC

# Line 8 | Line 8
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 == "") {
34 <                // Here the output filename is generated automaticly
35 <                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 <        }
33 >        // Here the number of proceeded events will be stored. For plotting, simply use L*sigma/eventNumber
34 >        eventNumbers = new TH1F("eventNumbers", "Histogram containing number of generated Events", 1, 0, 1);
35 >        eventNumbers->GetXaxis()->SetBinLabel(1,"Number of generated Events");
36  
37 +        // open the output file
38          if (loggingVerbosity_>0)
39                  std::cout << "Open file " << outputName << " for writing." << std::endl;
37        // open the output file
40          outFile = new TFile( outputName, "recreate" );
41          tree = new TTree("susyTree","Tree for single photon analysis");
42  
# Line 43 | Line 45 | TreeWriter::TreeWriter(TString inputName
45          reportEvery = 1000;
46          loggingVerbosity = loggingVerbosity_;
47          skim = true;
48 +        pileupHisto = 0;
49 + }
50  
51 + void TreeWriter::PileUpWeightFile( string pileupFileName ) {
52 +        TFile *puFile = new TFile( pileupFileName.c_str() );
53 +        pileupHisto = (TH1F*) puFile->Get("pileup");
54   }
55  
56   TreeWriter::~TreeWriter() {
57 <        if (!inputTree) return;
58 <        delete inputTree->GetCurrentFile();
57 >        if (pileupHisto != 0 )
58 >                delete pileupHisto;
59 >        inputTree->GetCurrentFile()->Close();
60   }
61  
62   // useful functions
# Line 56 | Line 64 | float TreeWriter::deltaR( TLorentzVector
64          return sqrt(pow(v1.Eta() - v2.Eta(), 2) + pow(v1.Phi() - v2.Phi(), 2) );
65   }
66  
67 + float effectiveAreaElectron( float eta ) {
68 +        // see https://twiki.cern.ch/twiki/bin/view/CMS/EgammaEARhoCorrection
69 +        // only for Delta R = 0.3 on 2012 Data
70 +        eta = fabs( eta );
71 +        float ea;
72 +        if( eta < 1.0 ) ea = 0.13;
73 +        else if( eta < 1.479 ) ea = 0.14;
74 +        else if( eta < 2.0 ) ea = 0.07;
75 +        else if( eta < 2.2 ) ea = 0.09;
76 +        else if( eta < 2.3 ) ea = 0.11;
77 +        else if( eta < 2.4 ) ea = 0.11;
78 +        else ea = 0.14;
79 +        return ea;
80 + }
81 +
82   // correct iso, see https://twiki.cern.ch/twiki/bin/view/CMS/CutBasedPhotonID2012
83   float chargedHadronIso_corrected(susy::Photon gamma, float rho) {
84 <  float eta = fabs(gamma.caloPosition.Eta());
85 <  float ea;
84 >        float eta = fabs(gamma.caloPosition.Eta());
85 >        float ea;
86  
87 <  if(eta < 1.0) ea = 0.012;
88 <  else if(eta < 1.479) ea = 0.010;
89 <  else if(eta < 2.0) ea = 0.014;
90 <  else if(eta < 2.2) ea = 0.012;
91 <  else if(eta < 2.3) ea = 0.016;
92 <  else if(eta < 2.4) ea = 0.020;
93 <  else ea = 0.012;
87 >        if(eta < 1.0) ea = 0.012;
88 >        else if(eta < 1.479) ea = 0.010;
89 >        else if(eta < 2.0) ea = 0.014;
90 >        else if(eta < 2.2) ea = 0.012;
91 >        else if(eta < 2.3) ea = 0.016;
92 >        else if(eta < 2.4) ea = 0.020;
93 >        else ea = 0.012;
94  
95 <  float iso = gamma.chargedHadronIso;
96 <  iso = max(iso - rho*ea, (float)0.);
95 >        float iso = gamma.chargedHadronIso;
96 >        iso = max(iso - rho*ea, (float)0.);
97  
98 <  return iso;
98 >        return iso;
99   }
100  
101   float neutralHadronIso_corrected(susy::Photon gamma, float rho) {
102 <  float eta = fabs(gamma.caloPosition.Eta());
103 <  float ea;
102 >        float eta = fabs(gamma.caloPosition.Eta());
103 >        float ea;
104  
105 <  if(eta < 1.0) ea = 0.030;
106 <  else if(eta < 1.479) ea = 0.057;
107 <  else if(eta < 2.0) ea = 0.039;
108 <  else if(eta < 2.2) ea = 0.015;
109 <  else if(eta < 2.3) ea = 0.024;
110 <  else if(eta < 2.4) ea = 0.039;
111 <  else ea = 0.072;
105 >        if(eta < 1.0) ea = 0.030;
106 >        else if(eta < 1.479) ea = 0.057;
107 >        else if(eta < 2.0) ea = 0.039;
108 >        else if(eta < 2.2) ea = 0.015;
109 >        else if(eta < 2.3) ea = 0.024;
110 >        else if(eta < 2.4) ea = 0.039;
111 >        else ea = 0.072;
112  
113 <  float iso = gamma.neutralHadronIso;
114 <  iso = max(iso - rho*ea, (float)0.);
113 >        float iso = gamma.neutralHadronIso;
114 >        iso = max(iso - rho*ea, (float)0.);
115  
116 <  return iso;
116 >        return iso;
117   }
118  
119   float photonIso_corrected(susy::Photon gamma, float rho) {
120 <  float eta = fabs(gamma.caloPosition.Eta());
121 <  float ea;
120 >        float eta = fabs(gamma.caloPosition.Eta());
121 >        float ea;
122  
123 <  if(eta < 1.0) ea = 0.148;
124 <  else if(eta < 1.479) ea = 0.130;
125 <  else if(eta < 2.0) ea = 0.112;
126 <  else if(eta < 2.2) ea = 0.216;
127 <  else if(eta < 2.3) ea = 0.262;
128 <  else if(eta < 2.4) ea = 0.260;
129 <  else ea = 0.266;
123 >        if(eta < 1.0) ea = 0.148;
124 >        else if(eta < 1.479) ea = 0.130;
125 >        else if(eta < 2.0) ea = 0.112;
126 >        else if(eta < 2.2) ea = 0.216;
127 >        else if(eta < 2.3) ea = 0.262;
128 >        else if(eta < 2.4) ea = 0.260;
129 >        else ea = 0.266;
130  
131 <  float iso = gamma.photonIso;
132 <  iso = max(iso - rho*ea, (float)0.);
131 >        float iso = gamma.photonIso;
132 >        iso = max(iso - rho*ea, (float)0.);
133  
134 <  return iso;
134 >        return iso;
135   }
136  
137 + float d0correction( susy::Electron electron, susy::Event event ) {
138 +        TVector3 beamspot = event.vertices[0].position;
139 +        susy::Track track = event.tracks[electron.gsfTrackIndex];
140 +        float d0 = track.d0() - beamspot.X()*sin(track.phi()) + beamspot.Y()*cos(track.phi());
141 +        cout << "return " << d0 << endl;
142 +        return d0;
143 + }
144  
145 <
145 > float dZcorrection( susy::Electron electron, susy::Event event ) {
146 >        TVector3 beamspot = event.vertices[0].position;
147 >        susy::Track track = event.tracks[electron.gsfTrackIndex];
148 >
149 >        if(track.momentum.Pt() == 0.) return 1.e6;
150 >        float dz = (track.vertex.Z() - beamspot.Z()) - ((track.vertex.X() - beamspot.X())*track.momentum.Px() + (track.vertex.Y() - beamspot.Y())*track.momentum.Py()) / track.momentum.Pt() * (track.momentum.Pz() / track.momentum.Pt());
151 >        return dz;
152 > }
153  
154   float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
155          /**
# Line 132 | Line 169 | float TreeWriter::getPtFromMatchedJet( s
169          } else {
170                  susy::PFJetCollection& jetColl = pfJets_it->second;
171                  for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
172 <                                it != jetColl.end(); it++) {
172 >                                it != jetColl.end(); ++it) {
173                          std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
174                          if (s_it == it->jecScaleFactors.end()) {
175                                  std::cout << "JEC is not available for this jet!!!" << std::endl;
# Line 149 | Line 186 | float TreeWriter::getPtFromMatchedJet( s
186          }// if, else
187  
188          if ( nearJets.size() == 0 ) {
189 <                //std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
189 >                if( loggingVerbosity > 1 )
190 >                        std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
191                  return myPhoton.momentum.Et();
192          }
193  
194          float pt = 0;
195          float minPtDifferenz = 1E20; // should be very high
196          for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
197 <                        it != jetEnd; it++ ) {
197 >                        it != jetEnd; ++it ) {
198                  float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
199                  if (  ptDiff < minPtDifferenz ) {
200                          minPtDifferenz = ptDiff;
# Line 179 | Line 217 | void TreeWriter::Loop() {
217  
218          // get number of events to be proceeded
219          Long64_t nentries = inputTree->GetEntries();
220 +        // store them in histo
221 +        eventNumbers->Fill( "Number of generated Events", nentries );
222          if(processNEvents <= 0 || processNEvents > nentries) processNEvents = nentries;
223  
224          if( loggingVerbosity > 0 )
# Line 190 | Line 230 | void TreeWriter::Loop() {
230  
231          tree->Branch("photon", &photon);
232          tree->Branch("jet", &jet);
233 +        tree->Branch("electron", &electron);
234 +        tree->Branch("muon", &muon);
235          tree->Branch("met", &met, "met/F");
236 +        tree->Branch("metPhi", &met_phi, "metPhi/F");
237 +        tree->Branch("type1met", &type1met, "type1met/F");
238 +        tree->Branch("type1metPhi", &type1met_phi, "type1metPhi/F");
239          tree->Branch("ht", &ht, "ht/F");
240          tree->Branch("nVertex", &nVertex, "nVertex/I");
241 <        tree->Branch("nElectron", &nElectron, "nElectron/I");
241 >        tree->Branch("pu_weight", &pu_weight, "pu_weight/F");
242  
243  
244 <        for (long jentry=0; jentry < processNEvents; jentry++) {
244 >        for (long jentry=0; jentry < processNEvents; ++jentry) {
245                  if ( loggingVerbosity>0 && jentry%reportEvery==0 )
246                          std::cout << jentry << " / " << processNEvents << std :: endl;
247                  inputTree->GetEntry(jentry);
248  
249                  photon.clear();
250                  jet.clear();
251 +                electron.clear();
252 +                muon.clear();
253                  ht = 0;
254  
255 +                // weights
256 +                if (pileupHisto == 0) {
257 +                        pu_weight = 1.;
258 +                } else {
259 +                        float trueNumInteractions = -1;
260 +                        for( susy::PUSummaryInfoCollection::const_iterator iBX = event->pu.begin();
261 +                                        iBX != event->pu.end() && trueNumInteractions < 0; ++iBX) {
262 +                                if (iBX->BX == 0)
263 +                                        trueNumInteractions = iBX->trueNumInteractions;
264 +                        }
265 +                        pu_weight = pileupHisto->GetBinContent( pileupHisto->FindBin( trueNumInteractions ) );
266 +                }
267 +
268 +
269                  // photons
270                  if( loggingVerbosity > 1 )
271                          std::cout << "Process photons" << std::endl;
272                  std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
273                  for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
274 <                                it != phoMap->second.end() && phoMap != event->photons.end(); it++ ) {
275 <                        if( ! it->isEB() && skim )
274 >                                it != phoMap->second.end() && phoMap != event->photons.end(); ++it ) {
275 >                        if( !(it->isEB() || it->isEE()) && skim )
276                                  continue;
277                          thisphoton->pt = getPtFromMatchedJet( *it, *event );
278 <                        if( thisphoton->pt < 80 && skim )
218 <                                continue;
219 <                        thisphoton->eta = it->momentum.Eta();
220 <                        thisphoton->phi = it->momentum.Phi();
278 >
279                          thisphoton->chargedIso = chargedHadronIso_corrected(*it, event->rho25);
280                          thisphoton->neutralIso = neutralHadronIso_corrected(*it, event->rho25);
281                          thisphoton->photonIso = photonIso_corrected(*it, event->rho25);
282 <                        if ( it->r9 >= 1 && skim )
282 >
283 >                        bool loose_photon_barrel = thisphoton->pt>20
284 >                                && it->isEB()
285 >                                && it->passelectronveto
286 >                                && it->hadTowOverEm<0.05
287 >                                && it->sigmaIetaIeta<0.012
288 >                                && thisphoton->chargedIso<2.6
289 >                                && thisphoton->neutralIso<3.5+0.04*thisphoton->pt
290 >                                && thisphoton->photonIso<1.3+0.005*thisphoton->pt;
291 >                        bool loose_photon_endcap = thisphoton->pt > 20
292 >                                && it->isEE()
293 >                                && it->passelectronveto
294 >                                && it->hadTowOverEm<0.05
295 >                                && it->sigmaIetaIeta<0.034
296 >                                && thisphoton->chargedIso<2.3
297 >                                && thisphoton->neutralIso<2.9+0.04*thisphoton->pt;
298 >                        if(!(loose_photon_endcap || loose_photon_barrel || thisphoton->pt > 75 ) && skim )
299                                  continue;
300 +                        thisphoton->eta = it->momentum.Eta();
301 +                        thisphoton->phi = it->momentum.Phi();
302                          thisphoton->r9 = it->r9;
303                          thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
304                          thisphoton->hadTowOverEm = it->hadTowOverEm;
305                          thisphoton->pixelseed = it->nPixelSeeds;
306 +                        thisphoton->conversionSafeVeto = it->passelectronveto;
307                          photon.push_back( *thisphoton );
231                        ht += thisphoton->pt;
308                          if( loggingVerbosity > 2 )
309                                  std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
310                  }
# Line 248 | Line 324 | void TreeWriter::Loop() {
324                          susy::PFJetCollection& jetColl = pfJets_it->second;
325  
326                          for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
327 <                                        it != jetColl.end(); it++) {
327 >                                        it != jetColl.end(); ++it) {
328                                  std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
329                                  if (s_it == it->jecScaleFactors.end()) {
330                                          std::cout << "JEC is not available for this jet!!!" << std::endl;
# Line 257 | Line 333 | void TreeWriter::Loop() {
333                                  float scale = s_it->second;
334                                  TLorentzVector corrP4 = scale * it->momentum;
335  
336 <                                if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
336 >                                if(std::fabs(corrP4.Eta()) > 3.0 && skim ) continue;
337                                  if(corrP4.Et() < 30 && skim ) continue;
338                                  thisjet->pt = corrP4.Et();
339                                  thisjet->eta = corrP4.Eta();
340                                  thisjet->phi = corrP4.Phi();
341 +                                thisjet->bCSV = it->bTagDiscriminators[susy::kCSV];
342 +                                // jet composition
343 +                                thisjet->chargedHadronEnergy = it->chargedHadronEnergy;
344 +                                thisjet->neutralHadronEnergy = it->neutralHadronEnergy;
345 +                                thisjet->photonEnergy = it->photonEnergy;
346 +                                thisjet->electronEnergy = it->electronEnergy;
347 +                                thisjet->muonEnergy = it->muonEnergy;
348 +                                thisjet->HFHadronEnergy = it->HFHadronEnergy;
349 +                                thisjet->HFEMEnergy = it->HFEMEnergy;
350 +                                thisjet->chargedEmEnergy = it->chargedEmEnergy;
351 +                                thisjet->chargedMuEnergy = it->chargedMuEnergy;
352 +                                thisjet->neutralEmEnergy = it->neutralEmEnergy;
353 +
354 +                                if( loggingVerbosity > 2 )
355 +                                        std::cout << " p_T, jet = " << thisjet->pt << std::endl;
356 +
357                                  jet.push_back( *thisjet );
358                                  ht += thisjet->pt;
359                          }// for jet
# Line 269 | Line 361 | void TreeWriter::Loop() {
361                  if( jet.size() < 2 && skim )
362                          continue;
363                  std::sort( jet.begin(), jet.end(), tree::EtGreater);
364 +                if( loggingVerbosity > 1 )
365 +                        std::cout << "Found " << jet.size() << " jets" << std::endl;
366 +
367  
368                  // met
369                  std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet");
370                  susy::MET* metobj = &(met_it->second);
371                  met = metobj->met();
372 +                met_phi = metobj->mEt.Phi();
373 +                if( loggingVerbosity > 2 )
374 +                        std::cout << " met = " << met << std::endl;
375 +
376 +                std::map<TString, susy::MET>::iterator type1met_it = event->metMap.find("pfType1CorrectedMet");
377 +                susy::MET* type1metobj = &(type1met_it->second);
378 +                type1met = type1metobj->met();
379 +                type1met_phi = type1metobj->mEt.Phi();
380 +                if( loggingVerbosity > 2 )
381 +                        std::cout << " type1met = " << type1met << std::endl;
382  
383                  // electrons
384 <                //std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
385 <                //nElectron = eVector.size();
384 >                tree::Particle* thiselectron = new tree::Particle();
385 >                map<TString, vector<susy::Electron> >::iterator eleMap = event->electrons.find("gsfElectrons");
386 >                if(eleMap == event->electrons.end() && loggingVerbosity > 0) {
387 >                        cout << "gsfElectrons not found!" << endl;
388 >                } else {
389 >                        cout << "now begin electrons " << endl;
390 >                        for(vector<susy::Electron>::iterator it = eleMap->second.begin(); it < eleMap->second.end(); ++it) {
391 >                                // for cuts see https://twiki.cern.ch/twiki/bin/viewauth/CMS/EgammaCutBasedIdentification for veto electrons
392 >                                if( it->momentum.Pt() < 1  || it->momentum.Pt() > 1e6 )
393 >                                        continue; // spike rejection
394 >                                float iso = ( it->chargedHadronIso + max(it->neutralHadronIso+it->photonIso
395 >                                                                                                                        - effectiveAreaElectron(it->momentum.Eta())*event->rho25, (Float_t)0. )
396 >                                                        ) / it->momentum.Pt();
397 >                                cout << iso << endl;
398 >                                cout << d0correction( *it, *event ) << endl;
399 >                                float test = d0correction( *it, *event );
400 >                                cout << test << endl;
401 >                                float dZ = std::fabs( dZcorrection( *it, *event ) );
402 >                                cout <<" find e" << endl;
403 >                                float d0 = 0.;
404 >                                if ( it->isEB() ){
405 >                                        if ( fabs(it->deltaEtaSuperClusterTrackAtVtx) > 0.007
406 >                                                        || fabs(it->deltaPhiSuperClusterTrackAtVtx) > 0.8
407 >                                                        || it->sigmaIetaIeta > 0.01
408 >                                                        || it->hcalOverEcalBc > 0.15
409 >                                                        || d0 > 0.04
410 >                                                        || dZ > 0.2
411 >                                                        || iso > 0.15 )
412 >                                                continue;
413 >                                        }
414 >                                else if( it->isEE() ) {
415 >                                        if ( fabs(it->deltaEtaSuperClusterTrackAtVtx) > 0.01
416 >                                                        || fabs(it->deltaPhiSuperClusterTrackAtVtx) > 0.7
417 >                                                        || it->sigmaIetaIeta > 0.03
418 >                                                        || d0 > 0.04
419 >                                                        || dZ > 0.2
420 >                                                        || iso > 0.15 )
421 >                                                continue;
422 >                                        }
423 >                                else // not in barrel nor in endcap
424 >                                        continue;
425 >                                // TODO: conversion rejection information not implemented yet, see twiki for more details
426 >
427 >                                thiselectron->pt = it->momentum.Pt();
428 >                                if( thiselectron->pt < 20 )
429 >                                        continue;
430 >                                if( loggingVerbosity > 2 )
431 >                                        std::cout << " p_T, electron = " << it->momentum.Et() << std::endl;
432 >                                thiselectron->eta = it->momentum.Eta();
433 >                                thiselectron->phi = it->momentum.Phi();
434 >                                electron.push_back( *thiselectron );
435 >                        }
436 >                }
437 >                if( loggingVerbosity > 1 )
438 >                        std::cout << "Found " << electron.size() << " electrons" << std::endl;
439 >
440 >                // muons
441 >                std::vector<susy::Muon> mVector = event->muons["muons"];
442 >                tree::Particle* thismuon = new tree::Particle();
443 >                for( std::vector<susy::Muon>::iterator it = mVector.begin(); it != mVector.end(); ++it) {
444 >                        if( !( it->isPFMuon() && ( it->isGlobalMuon() || it->isTrackerMuon() ) ) )
445 >                                continue; // see https://twiki.cern.ch/twiki/bin/view/CMSPublic/SWGuideMuonId#Loose_Muon
446 >                        thismuon->pt = it->momentum.Et();
447 >                        if( thismuon->pt < 20 )
448 >                                continue;
449 >                        thismuon->eta = it->momentum.Eta();
450 >                        thismuon->phi = it->momentum.Phi();
451 >                        muon.push_back( *thismuon );
452 >                }
453 >                if( loggingVerbosity > 1 )
454 >                        std::cout << "Found " << muon.size() << " muons" << std::endl;
455  
282                // vertices
456  
457 +                // vertices
458                  nVertex = event->vertices.size();
459  
460                  if( ht < 450 && skim)
# Line 290 | Line 464 | void TreeWriter::Loop() {
464          } // for jentry
465  
466  
293
294        tree->Write();
467          outFile->cd();
468 +        eventNumbers->Write();
469 +        tree->Write();
470          outFile->Write();
471          outFile->Close();
472   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines