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.4 by kiesel, Mon Mar 25 12:00:06 2013 UTC vs.
Revision 1.8 by kiesel, Wed Apr 10 20:46:33 2013 UTC

# Line 1 | Line 1
1   #include<iostream>
2   #include<math.h>
3 + #include<string>
4  
5 < #include "TFile.h"
5 < #include "TTree.h"
6 < #include "TString.h"
7 <
8 < #include "SusyEvent.h"
9 < #include "TreeObjects.h"
10 <
11 <
12 < class TreeWriter {
13 <        public :
14 <                TreeWriter(TString inputName, TString outputName );
15 <                virtual ~TreeWriter();
16 <                virtual void Loop();
17 <
18 <                void SetProcessNEvents(int nEvents) { processNEvents = nEvents; }
19 <                void SetReportEvents(int nEvents) { reportEvery = nEvents; }
20 <
21 <                TFile *inputFile;
22 <                TTree *inputTree;
23 <                susy::Event *event;
24 <
25 <                TFile *outFile;
26 <                TTree *tree;
27 <
28 <                float getPtFromMatchedJet( susy::Photon, susy::Event );
29 <                float deltaR( TLorentzVector, TLorentzVector );
30 <
31 <        private:
32 <                int processNEvents; // number of events to be processed
33 <                int reportEvery;
34 <                int loggingVerbosity;
35 <
36 <                // variables which will be stored in the tree
37 <                std::vector<tree::Photon> photon;
38 <                std::vector<tree::Jet> jet;
39 <                float met;
40 <                int nVertex;
41 <                int nElectron;
42 <                float weight;
43 < };
5 > #include "TSystem.h"
6  
7 < TreeWriter::TreeWriter(TString inputName, TString outputName) {
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 <        inputFile = new TFile( inputName, "read" );
14 <        inputTree = (TTree*) inputFile->Get("susyTree");
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");
# Line 56 | Line 41 | TreeWriter::TreeWriter(TString inputName
41          // set default parameter
42          processNEvents = -1;
43          reportEvery = 1000;
44 <        loggingVerbosity = 0;
44 >        loggingVerbosity = loggingVerbosity_;
45 >        skim = true;
46  
47   }
48  
# Line 65 | Line 51 | TreeWriter::~TreeWriter() {
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 < float TreeWriter::getPtFromMatchedJet( susy::Photon photon, susy::Event event ) {
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, return 0
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 = event.pfJets.find("ak5");
130 <        if(pfJets_it == event.pfJets.end()){
131 <                if(event.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
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();
# Line 95 | Line 140 | float TreeWriter::getPtFromMatchedJet( s
140                          }
141                          float scale = s_it->second;
142                          TLorentzVector corrP4 = scale * it->momentum;
143 <                        float deltaR_ = deltaR(photon.momentum, corrP4 );
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 ΔR < .3 found, set p_T to 0" << std::endl;
153 <                return 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(photon.momentum.Et() - it->momentum.Et());
160 >                float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
161                  if (  ptDiff < minPtDifferenz ) {
162                          minPtDifferenz = ptDiff;
163                          pt = it->momentum.Et();
# Line 118 | Line 165 | float TreeWriter::getPtFromMatchedJet( s
165          }
166  
167          // testing
168 <        if( nearJets.size() > 1 )
168 >        if( nearJets.size() > 1 && loggingVerbosity > 0 )
169                  std::cout << "There are several jets matching to this photon. "
170 <                                        << "Please check if the upper assumtion is reasonable" << std::endl;
170 >                                        << "Please check if jet-matching is correct." << std::endl;
171          return pt;
172   }
173  
174  
175   void TreeWriter::Loop() {
129        // only for testing
130        bool skim = true;
176  
177          // here the event loop is implemented and the tree is filled
178          if (inputTree == 0) return;
# Line 146 | Line 191 | void TreeWriter::Loop() {
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");
150        tree->Branch("weigth", &weight, "weight/F");
196          tree->Branch("nElectron", &nElectron, "nElectron/I");
197  
198  
# Line 158 | Line 203 | void TreeWriter::Loop() {
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++ ) {
# Line 170 | Line 218 | void TreeWriter::Loop() {
218                                  continue;
219                          thisphoton->eta = it->momentum.Eta();
220                          thisphoton->phi = it->momentum.Phi();
221 <                        thisphoton->chargedIso = it->chargedHadronIso;
222 <                        thisphoton->neutralIso = it->neutralHadronIso;
223 <                        thisphoton->photonIso = it->photonIso;
224 <                        if ( it->r9 > 1 && skim ) // if == 1 ?
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");
# Line 204 | Line 258 | void TreeWriter::Loop() {
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 )
# Line 220 | Line 276 | void TreeWriter::Loop() {
276                  met = metobj->met();
277  
278                  // electrons
279 <                std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
280 <                nElectron = eVector.size();
279 >                //std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
280 >                //nElectron = eVector.size();
281  
282                  // vertices
283  
284                  nVertex = event->vertices.size();
229                weight = 1;
285  
286 <                // bjets
286 >                if( ht < 450 && skim)
287 >                        continue;
288  
289                  tree->Fill();
290          } // for jentry
# Line 241 | Line 297 | void TreeWriter::Loop() {
297          outFile->Close();
298   }
299  
244 int main(int argc, char** argv) {
245        TreeWriter *tw = new TreeWriter("../root-files/susyEvents_qcd_1000-inf_part.root", "myTree.root");
246        tw->SetProcessNEvents(-1);
247        tw->Loop();
248 }
249

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines