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.1 by kiesel, Wed Mar 20 09:58:55 2013 UTC vs.
Revision 1.6 by kiesel, Tue Apr 9 08:24:40 2013 UTC

# Line 1 | Line 1
1 < #include "TFile.h"
2 < #include "TTree.h"
3 < #include "TString.h"
4 < #include "TLorentzVector.h"
5 <
6 < #include "SusyEvent.h"
7 <
8 <
9 < namespace tree {
10 < // In this namespace classes for the trees are defined.
11 <
12 < class Particle {
13 <        public:
14 <                float pt, eta, phi;
15 < };
16 <
17 < class Photon : public Particle {
18 <        public:
19 <                float r9, sigmaIetaIeta, hadTowOverEm, pixelseed;
20 <                float chargedIso, neutralIso, photonIso;
21 < };
22 <
23 < class Jet : public Particle{
24 <        public:
25 <                float pt, eta;
26 < };
1 > #include<iostream>
2 > #include<math.h>
3  
4 < bool EtGreater(const tree::Particle p1, const tree::Particle p2) {
29 <  return p1.pt > p2.pt;
30 < }
31 <
32 < } // end namespace definition
4 > #include "TSystem.h"
5  
6 < class TreeWriter {
35 <        public :
36 <                TreeWriter(TString inputName, TString outputName );
37 <                virtual ~TreeWriter();
38 <                virtual void Loop();
39 <
40 <                void SetProcessNEvents(int nEvents) { processNEvents = nEvents; }
41 <                void SetReportEvents(int nEvents) { reportEvery = nEvents; }
42 <
43 <                TFile *inputFile;
44 <                TTree *inputTree;
45 <                susy::Event *event;
46 <
47 <                TFile *outFile;
48 <                TTree *tree;
49 <
50 <        private:
51 <                int processNEvents; // number of events to be processed
52 <                int reportEvery;
53 <                int loggingVerbosity;
54 <
55 <                // variables which will be stored in the tree
56 <                std::vector<tree::Photon> photon;
57 <                std::vector<tree::Jet> jet;
58 <                float met;
59 <                int nVertex;
60 <                float weight;
61 < };
6 > #include "treeWriter.h"
7  
8 + using namespace std;
9  
10 < TreeWriter::TreeWriter(TString inputName, TString outputName) {
10 > TreeWriter::TreeWriter(TString inputName, TString outputName, int loggingVerbosity_ ) {
11          // read the input file
12 <        inputFile = new TFile( inputName, "read" );
13 <        inputTree = (TTree*) inputFile->Get("susyTree");
12 >        inputTree = new TChain("susyTree");
13 >        if (loggingVerbosity_ > 0)
14 >                std::cout << "Add files to chain" << std::endl;
15 >        inputTree->Add( inputName );
16 >
17 >        if (loggingVerbosity_ > 0)
18 >                std::cout << "Set Branch Address of susy::Event" << std::endl;
19          event = new susy::Event;
20          inputTree->SetBranchAddress("susyEvent", &event);
21  
# Line 75 | Line 26 | TreeWriter::TreeWriter(TString inputName
26          // set default parameter
27          processNEvents = -1;
28          reportEvery = 1000;
29 <        loggingVerbosity = 0;
29 >        loggingVerbosity = loggingVerbosity_;
30 >        skim = true;
31  
32   }
33  
# Line 84 | Line 36 | TreeWriter::~TreeWriter() {
36          delete inputTree->GetCurrentFile();
37   }
38  
39 + float TreeWriter::deltaR( TLorentzVector v1, TLorentzVector v2 ) {
40 +        return sqrt(pow(v1.Eta() - v2.Eta(), 2) + pow(v1.Phi() - v2.Phi(), 2) );
41 + }
42 +
43 + float TreeWriter::getPtFromMatchedJet( susy::Photon myPhoton, susy::Event myEvent ) {
44 +        /**
45 +         * \brief Takes jet p_T as photon p_T
46 +         *
47 +         * At first all jets with DeltaR < 0.3 (isolation cone) are searched.
48 +         * If several jets are found, take the one with the minimal pt difference
49 +         * compared to the photon. If no such jets are found, keep the photon_pt
50 +         * TODO: remove photon matched jet from jet-selection?
51 +         */
52 +        std::vector<susy::PFJet> nearJets;
53 +        nearJets.clear();
54 +
55 +        std::map<TString,susy::PFJetCollection>::iterator pfJets_it = myEvent.pfJets.find("ak5");
56 +        if(pfJets_it == myEvent.pfJets.end()){
57 +                if(myEvent.pfJets.size() > 0) std::cout << "JetCollection is not available!!!" << std::endl;
58 +        } else {
59 +                susy::PFJetCollection& jetColl = pfJets_it->second;
60 +                for(std::vector<susy::PFJet>::iterator it = jetColl.begin();
61 +                                it != jetColl.end(); it++) {
62 +                        std::map<TString,Float_t>::iterator s_it = it->jecScaleFactors.find("L2L3");
63 +                        if (s_it == it->jecScaleFactors.end()) {
64 +                                std::cout << "JEC is not available for this jet!!!" << std::endl;
65 +                                continue;
66 +                        }
67 +                        float scale = s_it->second;
68 +                        TLorentzVector corrP4 = scale * it->momentum;
69 +                        float deltaR_ = deltaR(myPhoton.momentum, corrP4 );
70 +                        if (deltaR_ > 0.3) continue;
71 +                        if( loggingVerbosity > 0 )
72 +                                std::cout << "gamma pt jet matching factor = " << it->momentum.Et() / myPhoton.momentum.Et() << std::endl;
73 +                        nearJets.push_back( *it );
74 +                }// for jet
75 +        }// if, else
76 +
77 +        if ( nearJets.size() == 0 ) {
78 +                //std::cout << "No jet with deltaR < .3 found, do not change photon_pt" << std::endl;
79 +                return myPhoton.momentum.Et();
80 +        }
81 +
82 +        float pt = 0;
83 +        float minPtDifferenz = 1E20; // should be very high
84 +        for( std::vector<susy::PFJet>::iterator it = nearJets.begin(), jetEnd = nearJets.end();
85 +                        it != jetEnd; it++ ) {
86 +                float ptDiff = fabs(myPhoton.momentum.Et() - it->momentum.Et());
87 +                if (  ptDiff < minPtDifferenz ) {
88 +                        minPtDifferenz = ptDiff;
89 +                        pt = it->momentum.Et();
90 +                }
91 +        }
92 +
93 +        // testing
94 +        if( nearJets.size() > 1 && loggingVerbosity > 0 )
95 +                std::cout << "There are several jets matching to this photon. "
96 +                                        << "Please check if jet-matching is correct." << std::endl;
97 +        return pt;
98 + }
99 +
100 +
101   void TreeWriter::Loop() {
102 +
103          // here the event loop is implemented and the tree is filled
104          if (inputTree == 0) return;
105  
# Line 102 | Line 117 | void TreeWriter::Loop() {
117          tree->Branch("photon", &photon);
118          tree->Branch("jet", &jet);
119          tree->Branch("met", &met, "met/F");
120 +        tree->Branch("ht", &ht, "ht/F");
121          tree->Branch("nVertex", &nVertex, "nVertex/I");
122 <        tree->Branch("weigth", &weight, "weight/F");
122 >        tree->Branch("nElectron", &nElectron, "nElectron/I");
123  
124  
125          for (long jentry=0; jentry < processNEvents; jentry++) {
126 <                if (jentry%reportEvery==0)
126 >                if ( loggingVerbosity>0 && jentry%reportEvery==0 )
127                          std::cout << jentry << " / " << processNEvents << std :: endl;
128                  inputTree->GetEntry(jentry);
129  
130                  photon.clear();
131                  jet.clear();
132 +                ht = 0;
133  
134                  // photons
135 +                if( loggingVerbosity > 1 )
136 +                        std::cout << "Process photons" << std::endl;
137                  std::map<TString, std::vector<susy::Photon> >::iterator phoMap = event->photons.find("photons");
138                  for(std::vector<susy::Photon>::iterator it = phoMap->second.begin();
139                                  it != phoMap->second.end() && phoMap != event->photons.end(); it++ ) {
140 <                        if( ! it->isEB() )
140 >                        if( ! it->isEB() && skim )
141                                  continue;
142 <                        thisphoton->pt = it->momentum.Et();
143 <                        if( thisphoton->pt < 80 )
142 >                        thisphoton->pt = getPtFromMatchedJet( *it, *event );
143 >                        if( thisphoton->pt < 80 && skim )
144                                  continue;
145                          thisphoton->eta = it->momentum.Eta();
146 +                        thisphoton->phi = it->momentum.Phi();
147                          thisphoton->chargedIso = it->chargedHadronIso;
148                          thisphoton->neutralIso = it->neutralHadronIso;
149                          thisphoton->photonIso = it->photonIso;
150 <                        if ( it->r9 > 1 ) // if == 1 ?
150 >                        if ( it->r9 > 1 && skim ) // if == 1 ?
151                                  continue;
152                          thisphoton->r9 = it->r9;
153                          thisphoton->sigmaIetaIeta = it->sigmaIetaIeta;
154                          thisphoton->hadTowOverEm = it->hadTowOverEm;
155                          thisphoton->pixelseed = it->nPixelSeeds;
156                          photon.push_back( *thisphoton );
157 +                        ht += thisphoton->pt;
158 +                        if( loggingVerbosity > 2 )
159 +                                std::cout << " p_T, gamma = " << thisphoton->pt << std::endl;
160                  }
161 <                if( photon.size() == 0 )
161 >
162 >                if( photon.size() == 0 && skim )
163                          continue;
164                  std::sort( photon.begin(), photon.end(), tree::EtGreater);
165 <
165 >                if( loggingVerbosity > 1 )
166 >                        std::cout << "Found " << photon.size() << " photons" << std::endl;
167  
168                  // jets
169                  std::map<TString,susy::PFJetCollection>::iterator pfJets_it = event->pfJets.find("ak5");
# Line 158 | Line 183 | void TreeWriter::Loop() {
183                                  float scale = s_it->second;
184                                  TLorentzVector corrP4 = scale * it->momentum;
185  
186 <                                if(std::abs(corrP4.Eta()) > 3.0) continue;
186 >                                if(std::abs(corrP4.Eta()) > 3.0 && skim ) continue;
187                                  thisjet->pt = corrP4.Et();
188                                  thisjet->eta = corrP4.Eta();
189 +                                thisjet->phi = corrP4.Phi();
190                                  jet.push_back( *thisjet );
191 +                                ht += thisjet->pt;
192                          }// for jet
193                  }// if, else
194 <                if( jet.size() == 0 )
195 <                        std::cout << "error, no jets found " << std::endl;
196 <                else
170 <                        std::sort( jet.begin(), jet.end(), tree::EtGreater);
194 >                if( jet.size() < 2 && skim )
195 >                        continue;
196 >                std::sort( jet.begin(), jet.end(), tree::EtGreater);
197  
198                  // met
199                  std::map<TString, susy::MET>::iterator met_it = event->metMap.find("pfMet");
200                  susy::MET* metobj = &(met_it->second);
201                  met = metobj->met();
202  
203 +                // electrons
204 +                //std::vector<susy::Electron> eVector = event->electrons["gsfElectrons"];
205 +                //nElectron = eVector.size();
206 +
207                  // vertices
208  
209                  nVertex = event->vertices.size();
210 <                weight = 1;
210 >
211 >                if( ht < 450 && skim)
212 >                        continue;
213  
214                  tree->Fill();
215          } // for jentry
# Line 189 | Line 221 | void TreeWriter::Loop() {
221          outFile->Write();
222          outFile->Close();
223   }
224 < /*
193 < int main(int argc, char** argv) {
194 <        TreeWriter *tw = new TreeWriter("qcd-1000-nTuple-test.root", "myQCDTree.root");
195 <        tw->SetProcessNEvents(10);
196 <        tw->Loop();
197 < }
198 < */
224 >

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines