Fork me on GitHub

source: svn/trunk/src/HEPEVTConverter.cc@ 240

Last change on this file since 240 was 220, checked in by Xavier Rouby, 16 years ago

include statements have been cleaned

File size: 5.8 KB
Line 
1#include <iostream>
2#include <utility>
3#include <deque>
4#include <sstream>
5#include <fstream>
6
7#include "TROOT.h"
8#include "TTree.h"
9#include "TBranch.h"
10#include "TLeaf.h"
11#include "TString.h"
12#include "TLorentzVector.h"
13#include "TChain.h"
14
15#include "BlockClasses.h"
16#include "ExRootTreeWriter.h"
17#include "ExRootTreeBranch.h"
18
19#include "HEPEVTConverter.h"
20
21using namespace std;
22
23//------------------------------------------------------------------------------
24
25//------------------------------------------------------------------------------
26
27HEPTreeReader::HEPTreeReader(TTree *tree, HEPEvent *event) : fChain(tree), fCurrentTree(-1), fEvent(event)
28{
29 if(!fChain) return;
30
31 TBranch *branch;
32 TLeaf *leaf;
33 TString name;
34 Int_t i;
35
36 name = "Nhep";
37 branch = fChain->GetBranch(name);
38 branch->SetAddress(&(event->Nhep));
39 fBranches.push_back(make_pair(name, branch));
40
41 TString intNames[3] = {"Idhep", "Jsmhep", "Jsdhep"};
42 Int_t **intData[3] = {&event->Idhep, &event->Jsmhep, &event->Jsdhep};
43
44 for(i = 0; i < 3; ++i)
45 {
46 name = intNames[i];
47 branch = fChain->GetBranch(name);
48 leaf = branch->GetLeaf(name);
49 *intData[i] = new Int_t[leaf->GetNdata()];
50 branch->SetAddress(*intData[i]);
51 fBranches.push_back(make_pair(name, branch));
52 }
53
54 TString floatNames[2] = {"Phep", "Vhep"};
55 Float_t **floatData[2] = {&event->Phep, &event->Vhep};
56
57 for(i = 0; i < 2; ++i)
58 {
59 name = floatNames[i];
60 branch = fChain->GetBranch(name);
61 leaf = branch->GetLeaf(name);
62 *floatData[i] = new Float_t[leaf->GetNdata()];
63 branch->SetAddress(*floatData[i]);
64 fBranches.push_back(make_pair(name, branch));
65 }
66}
67
68//------------------------------------------------------------------------------
69
70HEPTreeReader::~HEPTreeReader()
71{
72 if(!fChain) return;
73
74 delete[] fEvent->Idhep;
75 delete[] fEvent->Jsmhep;
76 delete[] fEvent->Jsdhep;
77 delete[] fEvent->Phep;
78 delete[] fEvent->Vhep;
79}
80
81//------------------------------------------------------------------------------
82
83Bool_t HEPTreeReader::ReadEntry(Long64_t element)
84{
85 if(!fChain) return kFALSE;
86
87 Int_t treeEntry = fChain->LoadTree(element);
88 if(treeEntry < 0) return kFALSE;
89
90 if(fChain->IsA() == TChain::Class())
91 {
92 TChain *chain = static_cast<TChain*>(fChain);
93 if(chain->GetTreeNumber() != fCurrentTree)
94 {
95 fCurrentTree = chain->GetTreeNumber();
96 Notify();
97 }
98 }
99
100 deque< pair<TString, TBranch*> >::iterator it_deque;
101 TBranch *branch;
102
103 for(it_deque = fBranches.begin(); it_deque != fBranches.end(); ++it_deque)
104 {
105 branch = it_deque->second;
106 if(branch)
107 {
108 branch->GetEntry(treeEntry);
109 }
110 }
111
112 return kTRUE;
113}
114
115//------------------------------------------------------------------------------
116
117void HEPTreeReader::Notify()
118{
119 // Called when loading a new file.
120 // Get branch pointers.
121 if(!fChain) return;
122
123 deque< pair<TString, TBranch*> >::iterator it_deque;
124
125 for(it_deque = fBranches.begin(); it_deque != fBranches.end(); ++it_deque)
126 {
127 it_deque->second = fChain->GetBranch(it_deque->first);
128 if(!it_deque->second)
129 {
130 cout << "** WARNING: cannot get branch '" << it_deque->first << "'" << endl;
131 }
132 }
133}
134
135
136
137HEPEVTConverter::HEPEVTConverter(const string& inputFileList, const string& outputFileName)
138{
139 string buffer;
140
141 gROOT->SetBatch();
142
143 ExRootTreeWriter *treeWriter = new ExRootTreeWriter(outputFileName, "GEN");
144 ExRootTreeBranch *branchGen = treeWriter->NewBranch("Particle", TRootGenParticle::Class());
145
146 ifstream infile(inputFileList.c_str());
147 if(!infile.is_open())
148 {
149 cerr << "** ERROR: Can't open '" << inputFileList << "' for input" << endl;
150 exit(1);
151 }
152 while(1)
153 {
154
155 TChain *chain = new TChain("h101");
156 infile >> buffer;
157 ifstream checking_the_file(buffer.c_str());
158 if(!checking_the_file.good()) { cout << buffer << ": file not found\n"; continue;}
159 else checking_the_file.close();
160
161 if(!infile.good()) break;
162 chain->Add(buffer.c_str());
163
164 HEPEvent event;
165 HEPTreeReader *treeReader = new HEPTreeReader(chain, &event);
166
167 Long64_t entry, allEntries = treeReader->GetEntries();
168 Int_t address, particle;
169 Float_t signEta;
170
171 TRootGenParticle *element;
172
173 // Loop over all events
174 for(entry = 0; entry < allEntries; ++entry)
175 {
176 // Load selected branches with data from specified event
177 treeReader->ReadEntry(entry);
178 treeWriter->Clear();
179
180 for(particle = 0; particle < event.Nhep; ++particle)
181 {
182 element = (TRootGenParticle*) branchGen->NewEntry();
183
184 element->PID = event.Idhep[particle];
185 element->Status = event.Jsmhep[particle]/16000000
186 + event.Jsdhep[particle]/16000000*100;
187 element->M1 = (event.Jsmhep[particle]%16000000)/4000 -1;
188 element->M2 = event.Jsmhep[particle]%4000 -1;
189 element->D1 = (event.Jsdhep[particle]%16000000)/4000 -1;
190 element->D2 = event.Jsdhep[particle]%4000 -1;
191 address = particle*5;
192 element->E = event.Phep[address + 3];
193 element->Px = event.Phep[address + 0];
194 element->Py = event.Phep[address + 1];
195 element->Pz = event.Phep[address + 2];
196 element->M = event.Phep[address + 4];
197
198 TLorentzVector vector(element->Px, element->Py, element->Pz, element->E);
199 element->PT = vector.Perp();
200 signEta = (element->Pz >= 0.0) ? 1.0 : -1.0;
201 element->Eta = vector.CosTheta()*vector.CosTheta() == 1.0 ? signEta*999.9 : vector.Eta();
202 element->Phi = vector.Phi();
203
204 address = particle*4;
205 element->T = event.Vhep[address + 3];
206 element->X = event.Vhep[address + 0];
207 element->Y = event.Vhep[address + 1];
208 element->Z = event.Vhep[address + 2];
209 }
210 treeWriter->Fill();
211 }
212 delete chain;
213 delete treeReader;
214 }
215 treeWriter->Write();
216 cout << "** Exiting..." << endl;
217
218 delete treeWriter;
219
220}
221
222
223
224
Note: See TracBrowser for help on using the repository browser.