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.7 by kiesel, Tue Apr 9 12:13:55 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 69 | Line 55 | float TreeWriter::deltaR( TLorentzVector
55          return sqrt(pow(v1.Eta() - v2.Eta(), 2) + pow(v1.Phi() - v2.Phi(), 2) );
56   }
57  
58 < float TreeWriter::getPtFromMatchedJet( susy::Photon photon, susy::Event event ) {
58 > float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
59          /**
60           * \brief Takes jet p_T as photon p_T
61           *
62           * At first all jets with DeltaR < 0.3 (isolation cone) are searched.
63           * If several jets are found, take the one with the minimal pt difference
64 <         * compared to the photon. If no such jets are found, return 0
64 >         * compared to the photon. If no such jets are found, keep the photon_pt
65           * TODO: remove photon matched jet from jet-selection?
66           */
67          std::vector<susy::PFJet> nearJets;
68          nearJets.clear();
69  
70 <        std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event.pfJets.find("ak5");
71 <        if(pfJets_it == event.pfJets.end()){
72 <                if(event.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
70 >        std::map<TString,susy::PFJetCollection>::iterator pfJets_it = myEvent.pfJets.find("ak5");
71 >        if(pfJets_it == myEvent.pfJets.end()){
72 >                if(myEvent.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
73          } else {
74                  susy::PFJetCollection& jetColl = pfJets_it->second;
75                  for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
# Line 95 | Line 81 | float TreeWriter::getPtFromMatchedJet( s
81                          }
82                          float scale = s_it->second;
83                          TLorentzVector corrP4 = scale * it->momentum;
84 <                        float deltaR_ = deltaR(photon.momentum, corrP4 );
84 >                        float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
85                          if (deltaR_ > 0.3) continue;
86 +                        if( loggingVerbosity > 0 )
87 +                                std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
88                          nearJets.push_back( *it );
89                  }// for jet
90          }// if, else
91  
92          if ( nearJets.size() == 0 ) {
93 <                std::cout << "No jet with ΔR < .3 found, set p_T to 0" << std::endl;
94 <                return 0;
93 >                //std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
94 >                return myPhoton.momentum.Et();
95          }
96  
97          float pt = 0;
98          float minPtDifferenz = 1E20; // should be very high
99          for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
100                          it != jetEnd; it++ ) {
101 <                float ptDiff = fabs(photon.momentum.Et() - it->momentum.Et());
101 >                float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
102                  if (  ptDiff < minPtDifferenz ) {
103                          minPtDifferenz = ptDiff;
104                          pt = it->momentum.Et();
# Line 118 | Line 106 | float TreeWriter::getPtFromMatchedJet( s
106          }
107  
108          // testing
109 <        if( nearJets.size() > 1 )
109 >        if( nearJets.size() > 1 && loggingVerbosity > 0 )
110                  std::cout << "There are several jets matching to this photon. "
111 <                                        << "Please check if the upper assumtion is reasonable" << std::endl;
111 >                                        << "Please check if jet-matching is correct." << std::endl;
112          return pt;
113   }
114  
115  
116   void TreeWriter::Loop() {
129        // only for testing
130        bool skim = true;
117  
118          // here the event loop is implemented and the tree is filled
119          if (inputTree == 0) return;
# Line 146 | Line 132 | void TreeWriter::Loop() {
132          tree->Branch("photon", &photon);
133          tree->Branch("jet", &jet);
134          tree->Branch("met", &met, "met/F");
135 +        tree->Branch("ht", &ht, "ht/F");
136          tree->Branch("nVertex", &nVertex, "nVertex/I");
150        tree->Branch("weigth", &weight, "weight/F");
137          tree->Branch("nElectron", &nElectron, "nElectron/I");
138  
139  
# Line 158 | Line 144 | void TreeWriter::Loop() {
144  
145                  photon.clear();
146                  jet.clear();
147 +                ht = 0;
148  
149                  // photons
150 +                if( loggingVerbosity > 1 )
151 +                        std::cout << "Process photons" << std::endl;
152                  std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
153                  for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
154                                  it != phoMap->second.end() && phoMap != event->photons.end(); it++ ) {
# Line 180 | Line 169 | void TreeWriter::Loop() {
169                          thisphoton->hadTowOverEm = it->hadTowOverEm;
170                          thisphoton->pixelseed = it->nPixelSeeds;
171                          photon.push_back( *thisphoton );
172 +                        ht += thisphoton->pt;
173 +                        if( loggingVerbosity > 2 )
174 +                                std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
175                  }
176 +
177                  if( photon.size() == 0 && skim )
178                          continue;
179                  std::sort( photon.begin(), photon.end(), tree::EtGreater);
180 +                if( loggingVerbosity > 1 )
181 +                        std::cout << "Found " << photon.size() << " photons" << std::endl;
182  
183                  // jets
184                  std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event->pfJets.find("ak5");
# Line 208 | Line 203 | void TreeWriter::Loop() {
203                                  thisjet->eta = corrP4.Eta();
204                                  thisjet->phi = corrP4.Phi();
205                                  jet.push_back( *thisjet );
206 +                                ht += thisjet->pt;
207                          }// for jet
208                  }// if, else
209                  if( jet.size() < 2 && skim )
# Line 220 | Line 216 | void TreeWriter::Loop() {
216                  met = metobj->met();
217  
218                  // electrons
219 <                std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
220 <                nElectron = eVector.size();
219 >                //std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
220 >                //nElectron = eVector.size();
221  
222                  // vertices
223  
224                  nVertex = event->vertices.size();
229                weight = 1;
225  
226 <                // bjets
226 >                if( ht < 450 && skim)
227 >                        continue;
228  
229                  tree->Fill();
230          } // for jentry
# Line 241 | Line 237 | void TreeWriter::Loop() {
237          outFile->Close();
238   }
239  
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