source: trunk/test/MatchingTreeConverter.cpp@ 17

Last change on this file since 17 was 2, checked in by Pavel Demin, 16 years ago

first commit

File size: 8.3 KB
Line 
1
2#include <iostream>
3#include <fstream>
4#include <sstream>
5#include <map>
6
7#include "TROOT.h"
8#include "TApplication.h"
9
10#include "TFile.h"
11#include "TChain.h"
12#include "TString.h"
13
14#include "TH2.h"
15#include "THStack.h"
16#include "TLegend.h"
17#include "TPaveText.h"
18#include "TLorentzVector.h"
19
20#include "ExRootAnalysis/ExRootClasses.h"
21
22#include "ExRootAnalysis/ExRootTreeWriter.h"
23#include "ExRootAnalysis/ExRootTreeBranch.h"
24
25#include "ExRootAnalysis/ExRootUtilities.h"
26#include "ExRootAnalysis/ExRootProgressBar.h"
27
28using namespace std;
29
30struct MatchingTreeObject
31{
32 enum {maxDblParam = 58};
33 Double_t dblParam[maxDblParam];
34};
35
36//------------------------------------------------------------------------------
37
38class MatchingTreeConverter
39{
40public:
41 MatchingTreeConverter(const char *outputFileName);
42 ~MatchingTreeConverter();
43
44 void ProcessObject();
45 void Write();
46
47 Long64_t GetNumberOfObjects(ifstream &inputFileStream);
48 Bool_t ReadObject(ifstream &inputFileStream);
49
50private:
51
52 void AnalyseEvent(ExRootTreeBranch *branch);
53
54 istringstream fBufferStream;
55 string fBuffer;
56
57 MatchingTreeObject fCurrentObject;
58
59 TFile *fOutputFile;
60 ExRootTreeWriter *fTreeWriter;
61
62 ExRootTreeBranch *fBranchMatch;
63
64};
65
66//------------------------------------------------------------------------------
67
68MatchingTreeConverter::MatchingTreeConverter(const char *outputFileName) :
69 fOutputFile(0), fTreeWriter(0)
70{
71 fOutputFile = TFile::Open(outputFileName, "RECREATE");
72 fTreeWriter = new ExRootTreeWriter(fOutputFile, "Match");
73
74 // information about reconstructed event
75 fBranchMatch = fTreeWriter->NewBranch("Match", ExRootGenMatch::Class());
76}
77
78//------------------------------------------------------------------------------
79
80MatchingTreeConverter::~MatchingTreeConverter()
81{
82 if(fTreeWriter) delete fTreeWriter;
83 if(fOutputFile) delete fOutputFile;
84}
85
86//------------------------------------------------------------------------------
87
88Long64_t MatchingTreeConverter::GetNumberOfObjects(ifstream &inputFileStream)
89{
90 Long64_t counter = 0;
91 Bool_t canReadNumber, canReadFile = kTRUE;
92 Int_t number;
93 int position = inputFileStream.tellg();
94 inputFileStream.seekg(0, std::ios::beg);
95
96 inputFileStream.clear();
97
98 while(canReadFile)
99 {
100 do
101 {
102 getline(inputFileStream, fBuffer);
103
104 if(!inputFileStream.good())
105 {
106 canReadFile = kFALSE;
107 break;
108 }
109
110 fBufferStream.clear();
111 fBufferStream.str(fBuffer);
112
113 canReadNumber = (fBufferStream >> number);
114 }
115 while(!canReadNumber);
116
117 ++counter;
118 }
119
120 inputFileStream.clear();
121
122 inputFileStream.seekg(position, std::ios::beg);
123
124 return (counter - 1);
125}
126
127//------------------------------------------------------------------------------
128
129Bool_t MatchingTreeConverter::ReadObject(ifstream &inputFileStream)
130{
131 Int_t i;
132 Bool_t canReadNumber;
133
134 do
135 {
136 getline(inputFileStream, fBuffer);
137
138 if(!inputFileStream.good()) return kFALSE;
139
140 fBufferStream.clear();
141 fBufferStream.str(fBuffer);
142
143 canReadNumber = kTRUE;
144
145 for(i = 0; canReadNumber && i < MatchingTreeObject::maxDblParam; ++i)
146 {
147 canReadNumber = (fBufferStream >> fCurrentObject.dblParam[i]);
148 }
149 }
150 while(!canReadNumber);
151
152 return kTRUE;
153}
154
155//---------------------------------------------------------------------------
156
157void MatchingTreeConverter::Write()
158{
159 if(fTreeWriter) fTreeWriter->Write();
160}
161
162//---------------------------------------------------------------------------
163
164void MatchingTreeConverter::ProcessObject()
165{
166 fTreeWriter->Clear();
167
168 AnalyseEvent(fBranchMatch);
169
170 fTreeWriter->Fill();
171}
172
173
174//---------------------------------------------------------------------------
175
176void MatchingTreeConverter::AnalyseEvent(ExRootTreeBranch *branch)
177{
178 ExRootGenMatch *element;
179
180 element = static_cast<ExRootGenMatch*>(branch->NewEntry());
181
182 element->Npart = fCurrentObject.dblParam[0];
183 element->Qjet1 = fCurrentObject.dblParam[1];
184 element->Qjet2 = fCurrentObject.dblParam[2];
185 element->Qjet3 = fCurrentObject.dblParam[3];
186 element->Qjet4 = fCurrentObject.dblParam[4];
187 element->Ptcjet1 = fCurrentObject.dblParam[5];
188 element->Ptcjet2 = fCurrentObject.dblParam[6];
189 element->Ptcjet3 = fCurrentObject.dblParam[7];
190 element->Ptcjet4 = fCurrentObject.dblParam[8];
191 element->Etacjet1 = fCurrentObject.dblParam[9];
192 element->Etacjet2 = fCurrentObject.dblParam[10];
193 element->Etacjet3 = fCurrentObject.dblParam[11];
194 element->Etacjet4 = fCurrentObject.dblParam[12];
195 element->Phicjet1 = fCurrentObject.dblParam[13];
196 element->Phicjet2 = fCurrentObject.dblParam[14];
197 element->Phicjet3 = fCurrentObject.dblParam[15];
198 element->Phicjet4 = fCurrentObject.dblParam[16];
199 element->Ptjet1 = fCurrentObject.dblParam[17];
200 element->Ptjet2 = fCurrentObject.dblParam[18];
201 element->Ptjet3 = fCurrentObject.dblParam[19];
202 element->Ptjet4 = fCurrentObject.dblParam[20];
203 element->Etajet1 = fCurrentObject.dblParam[21];
204 element->Etajet2 = fCurrentObject.dblParam[22];
205 element->Etajet3 = fCurrentObject.dblParam[23];
206 element->Etajet4 = fCurrentObject.dblParam[24];
207 element->Phijet1 = fCurrentObject.dblParam[25];
208 element->Phijet2 = fCurrentObject.dblParam[26];
209 element->Phijet3 = fCurrentObject.dblParam[27];
210 element->Phijet4 = fCurrentObject.dblParam[28];
211 element->Idres1 = fCurrentObject.dblParam[29];
212 element->Ptres1 = fCurrentObject.dblParam[30];
213 element->Etares1 = fCurrentObject.dblParam[31];
214 element->Phires1 = fCurrentObject.dblParam[32];
215 element->Idres2 = fCurrentObject.dblParam[33];
216 element->Ptres2 = fCurrentObject.dblParam[34];
217 element->Etares2 = fCurrentObject.dblParam[35];
218 element->Phires2 = fCurrentObject.dblParam[36];
219 element->Ptlep1 = fCurrentObject.dblParam[37];
220 element->Etmiss = fCurrentObject.dblParam[38];
221 element->Htjets = fCurrentObject.dblParam[39];
222 element->Ptb = fCurrentObject.dblParam[40];
223 element->Etab = fCurrentObject.dblParam[41];
224 element->Ptbbar = fCurrentObject.dblParam[42];
225 element->Etabbar = fCurrentObject.dblParam[43];
226 element->Ptbj = fCurrentObject.dblParam[44];
227 element->Etabj = fCurrentObject.dblParam[45];
228 element->Qpar1 = fCurrentObject.dblParam[46];
229 element->Qpar2 = fCurrentObject.dblParam[47];
230 element->Qpar3 = fCurrentObject.dblParam[48];
231 element->Qpar4 = fCurrentObject.dblParam[49];
232 element->Ptpar1 = fCurrentObject.dblParam[50];
233 element->Ptpar2 = fCurrentObject.dblParam[51];
234 element->Ptpar3 = fCurrentObject.dblParam[52];
235 element->Ptpar4 = fCurrentObject.dblParam[53];
236 element->Ncjets = fCurrentObject.dblParam[54];
237 element->Njets = fCurrentObject.dblParam[55];
238 element->Nfile = fCurrentObject.dblParam[56];
239 element->Nclus = fCurrentObject.dblParam[57];
240}
241
242//---------------------------------------------------------------------------
243
244int main(int argc, char *argv[])
245{
246 char *appName = "ExRootLHEFConverter";
247
248 if(argc != 3)
249 {
250 cout << " Usage: " << appName << " input_file" << " output_file" << endl;
251 cout << " input_file - input file in LHEF format," << endl;
252 cout << " output_file - output file in ROOT format." << endl;
253 return 1;
254 }
255
256 gROOT->SetBatch();
257
258 int appargc = 1;
259 char *appargv[] = {appName};
260 TApplication app(appName, &appargc, appargv);
261
262 // Open a stream connected to an event file:
263 ifstream inputFileStream(argv[1]);
264
265 if(!inputFileStream.is_open())
266 {
267 cerr << "** ERROR: Can't open '" << argv[1] << "' for input" << endl;
268 return 1;
269 }
270
271 // Create LHC Olympics converter:
272 MatchingTreeConverter *converter = new MatchingTreeConverter(argv[2]);
273
274 cout << "** Calculating number of objects to process. Please wait..." << endl;
275 Long64_t allEntries = converter->GetNumberOfObjects(inputFileStream);
276 cout << "** Input file contains " << allEntries << " objects" << endl;
277
278 if(allEntries > 0)
279 {
280 ExRootProgressBar progressBar(allEntries);
281
282 // Loop over all objects
283 Long64_t entry = 0;
284 while(converter->ReadObject(inputFileStream))
285 {
286 converter->ProcessObject();
287
288 progressBar.Update(entry);
289
290 ++entry;
291 }
292 progressBar.Finish();
293
294 converter->Write();
295 }
296
297 cout << "** Exiting..." << endl;
298
299 delete converter;
300}
301
302
Note: See TracBrowser for help on using the repository browser.