Fork me on GitHub

source: svn/trunk/src/LHCOConverter.cc@ 308

Last change on this file since 308 was 307, checked in by severine ovyn, 16 years ago

LHCO advanced

File size: 8.9 KB
Line 
1/***********************************************************************
2** **
3** /----------------------------------------------\ **
4** | Delphes, a framework for the fast simulation | **
5** | of a generic collider experiment | **
6** \----------------------------------------------/ **
7** **
8** **
9** This package uses: **
10** ------------------ **
11** FastJet algorithm: Phys. Lett. B641 (2006) [hep-ph/0512210] **
12** Hector: JINST 2:P09005 (2007) [physics.acc-ph:0707.1198v2] **
13** FROG: [hep-ex/0901.2718v1] **
14** **
15** ------------------------------------------------------------------ **
16** **
17** Main authors: **
18** ------------- **
19** **
20** Severine Ovyn Xavier Rouby **
21** severine.ovyn@uclouvain.be xavier.rouby@cern **
22** **
23** Center for Particle Physics and Phenomenology (CP3) **
24** Universite catholique de Louvain (UCL) **
25** Louvain-la-Neuve, Belgium **
26** **
27** Copyright (C) 2008-2009, **
28** All rights reserved. **
29** **
30***********************************************************************/
31
32// local includes
33#include "LHCOConverter.h"
34#include "ExRootTreeReader.h"
35#include "BlockClasses.h"
36
37// root includes
38#include "TFile.h"
39#include "TChain.h"
40#include "TClonesArray.h"
41
42// c++ includes
43#include <iostream>
44#include <string>
45#include <fstream>
46#include <sstream>
47#include <iomanip>
48using namespace std;
49
50
51//------------------------------------------------------------------------------
52extern const float UNDEFINED;
53
54LHCOConverter::LHCOConverter(const string& inputroot, const string& inputlog):
55 inputfilename_(inputroot), inputlogname_(inputlog), outputfilename_(inputroot), valid_(true) {
56 if (inputfilename_ == "") {valid_ = false;}
57 else {
58 TFile ftest(inputfilename_.c_str(),"READ");
59 if (!ftest.IsOpen()) { cout << "ERROR: file " << inputfilename_ << " could not be opened\n"; valid_=false;
60 }
61 else {
62 ftest.Close();
63 const unsigned int r_find = outputfilename_.rfind(".root");
64 if(r_find > outputfilename_.size()) {
65 cout << "ERROR: the extension of the input file is not '.root'. Exiting...\n"; valid_=false;
66 }
67 else {
68 outputfilename_.replace(r_find,5,"_events.lhco");
69 ofstream outfile( outputfilename_.c_str());
70 outfile.close();
71 cout << "INFO: " << outputfilename_ << " has been created\n";
72 }
73 }
74 }
75}
76
77
78
79void LHCOConverter::CopyRunLogFile() {
80 if(!valid_) {/*cout << "Nothing done\n";*/ return; }
81 if(inputlogname_ == "") {
82 // re-creates the logrun file name from the rootfile name
83 inputlogname_ = inputfilename_;
84 const unsigned int r_find = inputlogname_.rfind(".root");
85 inputlogname_.replace(r_find,5,"_run.log");
86 }
87 ifstream infile( inputlogname_.c_str() );
88 if (!infile.good()) { cout << "Warning: no input logfile found\n"; return; }
89
90 ofstream outfile( outputfilename_.c_str(),ios_base::app);
91
92 // else, if find is found
93 string linereader;
94 while ( getline(infile,linereader) ) {
95 outfile << " # " << linereader << endl;
96 }
97 outfile << " #" << endl;
98 infile.close();
99 outfile.close();
100}
101
102
103
104void LHCOConverter::ConvertExRootAnalysisToLHCO() {
105 if(!valid_) {/*cout << "Nothing done\n";*/ return; }
106
107 //output file
108 ofstream outfile( outputfilename_.c_str(),ios_base::app);
109 //# typ eta phi pt jmas ntrk btag had/em dum1 dum2
110 outfile << " ## More info on LHCO files: http://cp3wks05.fynu.ucl.ac.be/Manual/lhco.html\n #" << endl;
111 outfile << " # typ eta phi pt jmas ntrk btag had/em dum1 dum2" << endl;
112
113 // input files
114 TChain analysisChain("Analysis");
115 TChain triggerChain("Trigger");
116 analysisChain.Add(inputfilename_.c_str());
117 triggerChain.Add(inputfilename_.c_str());
118 ExRootTreeReader *analysisTree = new ExRootTreeReader(&analysisChain);
119 ExRootTreeReader *triggerTree = new ExRootTreeReader(&triggerChain);
120
121 TClonesArray *branchPhoton = analysisTree->UseBranch("Photon");
122 TClonesArray *branchElectron = analysisTree->UseBranch("Electron");
123 TClonesArray *branchMuon = analysisTree->UseBranch("Muon");
124 TClonesArray *branchTauJet = analysisTree->UseBranch("TauJet");
125 TClonesArray *branchJet = analysisTree->UseBranch("Jet");
126 TClonesArray *branchETmis = analysisTree->UseBranch("ETmis");
127
128 Long64_t Nevents = analysisTree->GetEntries();
129 cout << "** TTree 'Analysis' contains " << Nevents << " events" << endl;
130 Nevents = min(Nevents,triggerTree->GetEntries());
131 if (Nevents != analysisTree->GetEntries())
132 cout << "** WARNING: not the same number of entries in 'Analysis' and **\n** 'Trigger' trees\n";
133 for(Long64_t event = 0; event < Nevents; ++event) {
134 analysisTree->ReadEntry(event);
135 triggerTree->ReadEntry(event);
136 unsigned int triginfo = 0;
137 unsigned int line =0;
138
139 outfile << setw(3) << line++ << setw(4) << " " << setw(7) << event << setw(7) << triginfo << endl;
140
141 // 0 photon data
142 BranchReader(branchPhoton,line,lhcoPhotonID);
143
144 // 1 electron/positron data
145 BranchReader(branchElectron,line,lhcoElectronID);
146
147 // 2 muon data
148 BranchReader(branchMuon,line,lhcoMuonID);
149
150 // 3 tau-jets
151 BranchReader(branchTauJet,line,lhcoTauJetID);
152
153 // 4 jets
154 BranchReader(branchJet,line,lhcoJetID);
155
156 // 6 MET
157 BranchReader(branchETmis,line,lhcoETmisID);
158
159 } // event loop
160
161 outfile.close();
162 delete triggerTree;
163 delete analysisTree;
164}
165
166
167//ostringstream LHCOConverter::BranchReader(const TClonesArray* branch, unsigned int& line, unsigned short int lhcoID) const {
168void LHCOConverter::BranchReader(const TClonesArray* branch, unsigned int& line, unsigned short int lhcoID) const {
169 //ostringstream outfile;
170 ofstream outfile( outputfilename_.c_str(),ios_base::app);
171 unsigned int branch_size = branch->GetEntries();
172 TRootParticle * particle = 0;
173 for (unsigned int i=0; i< branch_size; i++) {
174 particle = (TRootParticle*) branch->At(i);
175 double temp = sqrt(particle->PT*particle->PT + particle->Pz * particle->Pz);
176 double jmass = (particle->E - temp) * (particle->E + temp); // prevents some numerical sensitivity
177 TLorentzVector pmu;
178 pmu.SetPtEtaPhiE(particle->PT, particle->Eta, particle->Phi, particle->E);
179 jmass = pmu.M();
180 float ntrk = 0.0;
181 float btag =0;
182 double ratioE = 0;
183
184 if(lhcoID == lhcoElectronID) { TRootElectron elec(*((TRootElectron*) branch->At(i))); ntrk = elec.Charge; }
185 else if (lhcoID == lhcoMuonID) { TRootMuon muon(*((TRootMuon*) branch->At(i))); ntrk = muon.Charge; }
186 else if (lhcoID == lhcoTauJetID) { TRootTauJet taujet(*((TRootTauJet*) branch->At(i))); ntrk = taujet.Charge; }
187 else if (lhcoID == lhcoJetID) { TRootJet jet(*((TRootJet*) branch->At(i))); ntrk = jet.NTracks; btag = jet.Btag;}
188 if(lhcoID != lhcoETmisID) {
189 outfile << fixed << setprecision(3)
190 << setw(3) << line++ // line counter
191 << setw(4) << lhcoID << " " // object ID in lhco format
192 << setw(7) << particle->Eta <<" "
193 << setw(7) << particle->Phi <<" "
194 << setw(7) << particle->PT <<" "
195 << setw(7) << jmass <<" " // invariant mass
196 << setw(7) << ntrk <<" " // number of tracks
197 << setw(7) << btag <<" "
198 << setw(7) << ratioE <<" " // E_had/E_em
199 << setw(7) << 0.0 << setw(7) << 0.0 // dummy/dummy
200 << endl;
201 } else {
202 outfile << fixed << setprecision(3)
203 << setw(3) << line++ // line counter
204 << setw(4) << lhcoID << " " // object ID in lhco format
205 << setw(7) << 0.000 <<" "
206 << setw(7) << particle->Phi <<" "
207 << setw(7) << particle->PT <<" "
208 << setw(7) << 0. <<" " // invariant mass
209 << setw(7) << 0 <<" " // number of tracks
210 << setw(7) << 0. <<" "
211 << setw(7) << 0. <<" " // E_had/E_em
212 << setw(7) << 0.0 << setw(7) << 0.0 // dummy/dummy
213 << endl;
214 }
215 }
216 outfile.close();
217 // return outfile;
218}
Note: See TracBrowser for help on using the repository browser.