Fork me on GitHub

source: svn/trunk/src/HepMCConverter.cc@ 584

Last change on this file since 584 was 574, checked in by cp3-support, 13 years ago

upgrade HepMC to version 2.06.05

File size: 11.8 KB
RevLine 
[350]1/***********************************************************************
2** **
3** /----------------------------------------------\ **
4** | Delphes, a framework for the fast simulation | **
5** | of a generic collider experiment | **
[443]6** \------------- arXiv:0903.2225v1 ------------/ **
[350]7** **
8** **
9** This package uses: **
10** ------------------ **
[443]11** ROOT: Nucl. Inst. & Meth. in Phys. Res. A389 (1997) 81-86 **
12** FastJet algorithm: Phys. Lett. B641 (2006) [hep-ph/0512210] **
13** Hector: JINST 2:P09005 (2007) [physics.acc-ph:0707.1198v2] **
[350]14** FROG: [hep-ex/0901.2718v1] **
[443]15** HepMC: Comput. Phys. Commun.134 (2001) 41 **
[350]16** **
17** ------------------------------------------------------------------ **
18** **
19** Main authors: **
20** ------------- **
21** **
[443]22** Severine Ovyn Xavier Rouby **
23** severine.ovyn@uclouvain.be xavier.rouby@cern **
[350]24** **
[443]25** Center for Particle Physics and Phenomenology (CP3) **
26** Universite catholique de Louvain (UCL) **
27** Louvain-la-Neuve, Belgium **
28** **
[350]29** Copyright (C) 2008-2009, **
[443]30** All rights reserved. **
[350]31** **
32***********************************************************************/
33
34#include <iostream>
35#include <fstream>
36#include "TLorentzVector.h"
37#include "BlockClasses.h"
38
[380]39#include "PdgParticle.h"
[350]40#include "ExRootTreeWriter.h"
41#include "ExRootTreeBranch.h"
42#include "HepMCConverter.h"
43
44#include "GenParticle.h"
45#include "GenVertex.h"
[574]46#include "IO_AsciiParticles.h"
[350]47#include "IO_GenEvent.h"
48
49//-------------------------------------------------------------------------
50int HepMCConverter::find_in_map( const std::map<HepMC::GenParticle*,int>& m, HepMC::GenParticle *p) const
51{
52 std::map<HepMC::GenParticle*,int>::const_iterator iter = m.find(p);
53 return (iter == m.end()) ? 0 : iter->second;
54}
55
56//--------------------------------------------------------------------------
57void HepMCConverter::ReadStats()
58{
59
60 unsigned int particle_counter=0;
61 index_to_particle.reserve(evt->particles_size());
62 index_to_particle[0] = 0;
63 HepMC::GenEvent::vertex_const_iterator v;
64 for (v = evt->vertices_begin(); v != evt->vertices_end(); ++v )
65 {
66 // making a list of incoming particles of the vertices
67 // so that the mother indices in HEPEVT can be filled properly
68 HepMC::GenVertex::particles_out_const_iterator p1;
69 for (p1 = (*v)->particles_in_const_begin();p1 != (*v)->particles_in_const_end(); ++p1 )
70 {
71
72 ++particle_counter;
73 //particle_counter can be very large for heavy ions
74 if(particle_counter >= index_to_particle.size() )
75 {
76 //make it large enough to hold up to this index
77 index_to_particle.resize(particle_counter+1);
78 }
79 index_to_particle[particle_counter] = *p1;
80 particle_to_index[*p1] = particle_counter;
81 }
82 // daughters are entered only if they aren't a mother of
83 // another vertex
84 HepMC::GenVertex::particles_out_const_iterator p2;
85 for (p2 = (*v)->particles_out_const_begin();p2 != (*v)->particles_out_const_end(); ++p2)
86 {
87 if (!(*p2)->end_vertex())
88 {
89 ++particle_counter;
90 //particle_counter can be very large for heavy ions
91 if(particle_counter >= index_to_particle.size() )
92 {
93 //make it large enough to hold up to this index
94 index_to_particle.resize(particle_counter+1);
95 }
96 index_to_particle[particle_counter] = *p2;
97 particle_to_index[*p2] = particle_counter;
98 }
99 }
100
101 }
102}
103
104
105//-------------------------------------------------------------------------
106void HepMCConverter::getStatsFromTuple(int &mo1, int &mo2, int &da1, int &da2, int &status, int &pid, int j) const
107{
108 if (!evt)
109 {
110 cout << "HepMCFileReader: Got no event :-( Game over already ?" <<endl;
111 }
112 else
113 {
114 status = index_to_particle[j]->status();
115 pid = index_to_particle[j]->pdg_id();
116 if ( index_to_particle[j]->production_vertex() )
117 {
118 int num_mothers = index_to_particle[j]->production_vertex()->particles_in_size();
[367]119 if (num_mothers ==0) {
120 mo1 = 0;
121 mo2 = 0;
122 }
123 else {
124 int first_mother = find_in_map( particle_to_index,*(index_to_particle[j]->production_vertex()->particles_in_const_begin()));
125 int last_mother = first_mother + num_mothers - 1;
126 if ( first_mother == 0 ) last_mother = 0;
127 mo1=first_mother;
128 mo2=last_mother;
129 } // if num_mothers !=0
[350]130 }
[367]131 else // no data on production_vertex
[350]132 {
133 mo1 =0;
134 mo2 =0;
135 }
136 if (index_to_particle[j]->end_vertex())
137 {
[574]138 // make sure first and last daughter are indeed the first and last
[350]139 int first_daughter = find_in_map( particle_to_index,*(index_to_particle[j]->end_vertex()->particles_begin(HepMC::children)));
[574]140 int last_daughter = 0;
141
[350]142 HepMC::GenVertex::particle_iterator ic;
[574]143 for (ic = index_to_particle[j]->end_vertex()->particles_begin(HepMC::children);ic != index_to_particle[j]->end_vertex()->particles_end(HepMC::children); ++ic) {
144 int current_daughter = find_in_map( particle_to_index,*ic) ;
145 if (current_daughter < first_daughter)
146 first_daughter = current_daughter;
147 if (current_daughter > last_daughter)
148 last_daughter = current_daughter;
149 }
[350]150 if (first_daughter== 0) last_daughter = 0;
151 da1=first_daughter;
152 da2=last_daughter;
[574]153
[350]154 }
155 else
156 {
157 da1=0;
158 da2=0;
159 }
160 }
161}
162
163
164
165//---------------------------------------------------------------------------
166
167void HepMCConverter::AnalyseEvent(ExRootTreeBranch *branch,HepMC::GenEvent& evt,const Long64_t eventNumber)
168{
169 TRootLHEFEvent *element;
170
171 element = static_cast<TRootLHEFEvent*>(branch->NewEntry());
172 element->Number = eventNumber;
173 element->Nparticles = evt.particles_size();
174 element->ProcessID = evt.signal_process_id();
175 // element->Weight = hepeup.XWGTUP;
176 element->ScalePDF = evt.event_scale();
177 element->CouplingQED = evt.alphaQED();
178 element->CouplingQCD = evt.alphaQCD();
179
180}
181
182//---------------------------------------------------------------------------
183
[367]184void HepMCConverter::AnalyseParticles(ExRootTreeBranch *branch, const HepMC::GenEvent& evt)
[350]185{
186 TRootC::GenParticle *element;
187
188 TLorentzVector momentum;
189 Double_t signPz;
190
191 ReadStats();
192 for(int n=1; n<=evt.particles_size(); n++)
193 {
194 getStatsFromTuple( mo1,mo2,da1,da2,status,pid,n);
195
196 element = static_cast<TRootC::GenParticle*>(branch->NewEntry());
197
198 element->PID = pid;
199 element->Status = status;
[574]200 element->M1 = mo1 - 1; // added -1 as the numbering in the tree starts from 0
201 element->M2 = mo2 - 1;
202 element->D1 = da1 - 1;
203 element->D2 = da2 - 1;
[350]204
205 element->E = index_to_particle[n]->momentum().e();
206 element->Px = index_to_particle[n]->momentum().px();
207 element->Py = index_to_particle[n]->momentum().py();
208 element->Pz = index_to_particle[n]->momentum().pz();
209
[367]210
[380]211 //cout << "element->PID = " << pid << "\t";
212 //PdgParticle pdg_part(PdgID[pid]);
213 //element->M = index_to_particle[n]->momentum().m(); // this is the particle virtuality, not its rest mass
214 //element->M = pdg_part.mass();
215 //element->Charge = pdg_part.charge();
216 //cout << "element->M = " << element->M << " \t element->Charge = " << element->Charge << endl;
217
218 element->PT = sqrt(pow(element->Px,2)+pow(element->Py,2));
219
[350]220 momentum.SetPxPyPzE(element->Px, element->Py, element->Pz, element->E);
221 signPz = (element->Pz >= 0.0) ? 1.0 : -1.0;
[367]222 //element->Eta = element->PT == 0.0 ? signPz*999.9 : momentum.Eta(); to avoid a warning from ROOT, replace the "==0" by "< 1e-6"
223 element->Eta = element->PT < 1e-6 ? signPz*999.9 : momentum.Eta();
[350]224 element->Phi = index_to_particle[n]->momentum().phi();
225
226 //In particle at vertex
227 HepMC::GenVertex* vrtI = (index_to_particle[n])->production_vertex();
228 HepMC::GenVertex::particles_in_const_iterator partI;
229
230 if(vrtI)
231 {
232 element->T = vrtI->position().t();
233 element->X = vrtI->position().x();
234 element->Y = vrtI->position().y();
235 element->Z = vrtI->position().z();
236 }
237 else
238 {
239 element->T = 0.;
240 element->X = 0.;
241 element->Y = 0.;
242 element->Z = 0.;
243 }
244 }
245}
246
247//
248
249//------------------------------------------------------------------------------
250
251HepMCConverter::~HepMCConverter()
252{
[359]253 delete evt;
254
255 /*cout << "delete index to particle" << endl;
256 std::vector<HepMC::GenParticle*>::iterator i;
257 / for (i=index_to_particle.begin();i != index_to_particle.end(); i++) delete (*i);
258 cout << "done" << endl;
259
260 std::map<HepMC::GenParticle*,int>::iterator j;
261 for (j=particle_to_index.begin(); j != particle_to_index.end(); j++) delete j->first;
262 */
263
[350]264}
265
266//------------------------------------------------------------------------------
267
[380]268HepMCConverter::HepMCConverter(const string& inputFileList, const string& outputFileName, const PdgTable& pdg, const int& Nevents) : DataConverter(pdg,Nevents)
[350]269{
270
271 ExRootTreeWriter *treeWriter = new ExRootTreeWriter(outputFileName, "GEN");
272 // information about generated event
273 ExRootTreeBranch *branchGenEvent = treeWriter->NewBranch("Event", TRootLHEFEvent::Class());
274 // generated particles from HEPEVT
275 ExRootTreeBranch *branchGenParticle = treeWriter->NewBranch("Particle", TRootC::GenParticle::Class());
[466]276
277 int nevt_already_processed=0;
278
[350]279 // Open a stream connected to an event file:
280 ifstream infile(inputFileList.c_str());
281 string filename;
282 if(!infile.is_open()) {
283 cerr << left << setw(30) <<"** ERROR: Can't open "<<""
284 << left << setw(20) << inputFileList <<""
285 << right << setw(19) <<"for input **"<<""<<endl;
286 exit(1);
287 }
288
[362]289 Long64_t entry = 0;
[350]290 while(1)
291 {
292 infile >> filename;
[466]293 if(!infile.good()) break; // end of listfile reached
294 if (Nevt>0 && nevt_already_processed >=Nevt) break; // enough events already processed
295
[350]296 ifstream checking_the_file(filename.c_str());
297 if(!checking_the_file.good())
298 {
299 cerr << left << setw(30) <<"** ERROR: Can't find file "<<""
[480]300 << left << setw(36) << filename <<""
301 << right << setw(3) <<" **"<<""<<endl;
[350]302 continue;
303 }
304 else checking_the_file.close();
305
306 //Open the file
307 HepMC::IO_GenEvent ascii_in(filename,std::ios::in);
308 // get the first event
[419]309
[350]310 evt = ascii_in.read_next_event();
311
312 while ( evt )
313 {
[419]314 if(Nevt>0 && entry>=Nevt) break;
[466]315 if(Nevt>0 && nevt_already_processed >=Nevt) break; // enough events already processed
[419]316
[350]317 treeWriter->Clear();
318 AnalyseEvent(branchGenEvent, *evt,entry+1);
319 AnalyseParticles(branchGenParticle, *evt);
320 delete evt;
321 // read the next event
322 ascii_in >> evt;
323 treeWriter->Fill();
324 ++entry;
[466]325 ++nevt_already_processed;
[350]326 }
327 }
328
329 treeWriter->Write();
330 delete treeWriter;
331
332}
333
Note: See TracBrowser for help on using the repository browser.