ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/kiesel/TreeWriter/treeWriter.cc
Revision: 1.5
Committed: Tue Apr 9 07:28:02 2013 UTC (12 years ago) by kiesel
Content type: text/plain
Branch: MAIN
Changes since 1.4: +36 -64 lines
Log Message:
Working with root macro now, not with executable

File Contents

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