Fork me on GitHub

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

Last change on this file since 419 was 419, checked in by Xavier Rouby, 15 years ago

Nevent implemented in DetectorCard

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