CMS 3D CMS Logo

IO_EPOS.cc
Go to the documentation of this file.
1 
2 // EPOS IO class
3 
5 #include "HepMC/GenEvent.h"
6 #include <cstdio> // needed for formatted output using sprintf
7 
8 using namespace HepMC;
9 
10 namespace EPOS {
11 
12  unsigned int EPOS_Wrapper::s_sizeof_int = 4;
13  unsigned int EPOS_Wrapper::s_sizeof_real = sizeof(double);
14  unsigned int EPOS_Wrapper::s_max_number_entries = 99900;
15 
16  IO_EPOS::IO_EPOS() : m_trust_mothers_before_daughters(true),
17  m_trust_both_mothers_and_daughters(false),
18  m_print_inconsistency_errors(true),
19  m_trust_beam_particles(true),
20  m_skip_nucl_frag(false)
21  {}
22 
24 
25  void IO_EPOS::print( std::ostream& ostr ) const {
26  ostr << "IO_EPOS: reads an event from the FORTRAN EPOS g "
27  << "common block. \n"
28  << " trust_mothers_before_daughters = "
30  << " trust_both_mothers_and_daughters = "
32  << ", print_inconsistency_errors = "
33  << m_print_inconsistency_errors << std::endl;
34  }
35 
36  bool IO_EPOS::fill_next_event( HepMC::GenEvent* evt ) {
37  //
38  // 1. test that evt pointer is not null and set event number
39  if ( !evt ) {
40  std::cerr
41  << "IO_EPOS::fill_next_event error - passed null event."
42  << std::endl;
43  return false;
44  }
45  evt->set_event_number( EPOS_Wrapper::event_number() );
46  //
47  // 2. create a particle instance for each EPOS entry and fill a map
48  // create a vector which maps from the EPOS particle index to the
49  // GenParticle address
50  // (+1 in size accounts for hepevt_particle[0] which is unfilled)
51  std::vector<HepMC::GenParticle*> hepevt_particle(
53  hepevt_particle[0] = nullptr;
54  for ( int i1 = 1; i1 <= EPOS_Wrapper::number_entries(); ++i1 ) {
55  hepevt_particle[i1] = build_particle(i1);
56  }
57 
58  HepMC::GenVertex* primaryVertex = new HepMC::GenVertex(HepMC::FourVector(0,0,0,0),0);
59  evt->add_vertex(primaryVertex);
60  if(!evt->signal_process_vertex()) evt->set_signal_process_vertex(primaryVertex);
61 
62  std::set<HepMC::GenVertex*> new_vertices;
63  //
64  // Here we assume that the first two particles in the list
65  // are the incoming beam particles.
66  if( trust_beam_particles() ) {
67  evt->set_beam_particles( hepevt_particle[1], hepevt_particle[2] );
68  }
69  //
70  // 3.+4. loop over EPOS particles AGAIN, this time creating vertices
71  //MODIFICATION FROM HEPMC!! skipping nuclear fragments in event if option is set
72  for ( int i = 1; i <= EPOS_Wrapper::number_entries(); ++i ) {
73  if (m_skip_nucl_frag && abs(hepevt_particle[i]->pdg_id())>=1000000000)
74  continue;
75  // We go through and build EITHER the production or decay
76  // vertex for each entry in hepevt, depending on the switch
77  // m_trust_mothers_before_daughters (new 2001-02-28)
78  // Note: since the EPOS pointers are bi-directional, it is
79  //
80  // 3. Build the production_vertex (if necessary)
83  build_production_vertex( i, hepevt_particle, evt );
84  }
85  //
86  // 4. Build the end_vertex (if necessary)
87  // Identical steps as for production vertex
90  build_end_vertex( i, hepevt_particle, evt );
91  }
92  }
93  // 5. 01.02.2000
94  // handle the case of particles in EPOS which come from nowhere -
95  // i.e. particles without mothers or daughters.
96  // These particles need to be attached to a vertex, or else they
97  // will never become part of the event. check for this situation
98  //MODIFICATION FROM HEPMC!! skipping nuclear fragments in event if option is set
99  for ( int i3 = 1; i3 <= EPOS_Wrapper::number_entries(); ++i3 ) {
100  if (m_skip_nucl_frag && abs(hepevt_particle[i3]->pdg_id())>=1000000000)
101  continue;
102  if ( !hepevt_particle[i3]->end_vertex() &&
103  !hepevt_particle[i3]->production_vertex() ) {
104  HepMC::GenVertex* prod_vtx = new GenVertex();
105  prod_vtx->add_particle_out( hepevt_particle[i3] );
106  evt->add_vertex( prod_vtx );
107  }
108  }
109  return true;
110  }
111 
112  void IO_EPOS::write_event( const GenEvent* evt ) {
113  //
114  if ( !evt ) return;
115  //
116  // map all particles onto a unique index
117  std::vector<HepMC::GenParticle*> index_to_particle(
119  index_to_particle[0]=nullptr;
120  std::map<HepMC::GenParticle*,int> particle_to_index;
121  int particle_counter=0;
122  for ( HepMC::GenEvent::vertex_const_iterator v = evt->vertices_begin();
123  v != evt->vertices_end(); ++v ) {
124  // all "mothers" or particles_in are kept adjacent in the list
125  // so that the mother indices in hepevt can be filled properly
126  for ( HepMC::GenVertex::particles_in_const_iterator p1
127  = (*v)->particles_in_const_begin();
128  p1 != (*v)->particles_in_const_end(); ++p1 ) {
129  ++particle_counter;
130  if ( particle_counter >
132  index_to_particle[particle_counter] = *p1;
133  particle_to_index[*p1] = particle_counter;
134  }
135  // daughters are entered only if they aren't a mother of
136  // another vtx
137  for ( HepMC::GenVertex::particles_out_const_iterator p2
138  = (*v)->particles_out_const_begin();
139  p2 != (*v)->particles_out_const_end(); ++p2 ) {
140  if ( !(*p2)->end_vertex() ) {
141  ++particle_counter;
142  if ( particle_counter >
144  break;
145  }
146  index_to_particle[particle_counter] = *p2;
147  particle_to_index[*p2] = particle_counter;
148  }
149  }
150  }
151  if ( particle_counter > EPOS_Wrapper::max_number_entries() ) {
152  particle_counter = EPOS_Wrapper::max_number_entries();
153  }
154  //
155  // fill the EPOS event record
156  EPOS_Wrapper::set_event_number( evt->event_number() );
157  EPOS_Wrapper::set_number_entries( particle_counter );
158  for ( int i = 1; i <= particle_counter; ++i ) {
159  EPOS_Wrapper::set_status( i, index_to_particle[i]->status() );
160  EPOS_Wrapper::set_id( i, index_to_particle[i]->pdg_id() );
161  FourVector m = index_to_particle[i]->momentum();
162  EPOS_Wrapper::set_momentum( i, m.px(), m.py(), m.pz(), m.e() );
163  EPOS_Wrapper::set_mass( i, index_to_particle[i]->generatedMass() );
164  // there should ALWAYS be particles in any vertex, but some generators
165  // are making non-kosher HepMC events
166  if ( index_to_particle[i]->production_vertex() &&
167  index_to_particle[i]->production_vertex()->particles_in_size()) {
168  FourVector p = index_to_particle[i]->
169  production_vertex()->position();
170  EPOS_Wrapper::set_position( i, p.x(), p.y(), p.z(), p.t() );
171  int num_mothers = index_to_particle[i]->production_vertex()->
172  particles_in_size();
173  int first_mother = find_in_map( particle_to_index,
174  *(index_to_particle[i]->
175  production_vertex()->
176  particles_in_const_begin()));
177  int last_mother = first_mother + num_mothers - 1;
178  if ( first_mother == 0 ) last_mother = 0;
179  EPOS_Wrapper::set_parents( i, first_mother, last_mother );
180  } else {
181  EPOS_Wrapper::set_position( i, 0, 0, 0, 0 );
182  EPOS_Wrapper::set_parents( i, 0, 0 );
183  }
184  EPOS_Wrapper::set_children( i, 0, 0 );
185  }
186  }
187 
189  std::vector<HepMC::GenParticle*>&
190  hepevt_particle,
191  GenEvent* evt ) {
192  HepMC::GenParticle* p = hepevt_particle[i];
193  // a. search to see if a production vertex already exists
194  int mother = EPOS_Wrapper::first_parent(i);
195  HepMC::GenVertex* prod_vtx = p->production_vertex();
196  while ( !prod_vtx && mother > 0 ) {
197  prod_vtx = hepevt_particle[mother]->end_vertex();
198  if ( prod_vtx ) prod_vtx->add_particle_out( p );
199  // increment mother for next iteration
200  if ( ++mother > EPOS_Wrapper::last_parent(i) ) mother = 0;
201  }
202  // b. if no suitable production vertex exists - and the particle
203  // has atleast one mother or position information to store -
204  // make one
205  HepMC::FourVector prod_pos( EPOS_Wrapper::x(i), EPOS_Wrapper::y(i),
207  );
208  if ( !prod_vtx && (EPOS_Wrapper::number_parents(i)>0
209  || prod_pos!=FourVector(0,0,0,0)) )
210  {
211  prod_vtx = new HepMC::GenVertex();
212  prod_vtx->add_particle_out( p );
213  evt->add_vertex( prod_vtx );
214  }
215  // c. if prod_vtx doesn't already have position specified, fill it
216  if ( prod_vtx && prod_vtx->position()==FourVector(0,0,0,0) ) {
217  prod_vtx->set_position( prod_pos );
218  }
219  // d. loop over mothers to make sure their end_vertices are
220  // consistent
221  mother = EPOS_Wrapper::first_parent(i);
222  while ( prod_vtx && mother > 0 ) {
223  if ( !hepevt_particle[mother]->end_vertex() ) {
224  // if end vertex of the mother isn't specified, do it now
225  prod_vtx->add_particle_in( hepevt_particle[mother] );
226  } else if (hepevt_particle[mother]->end_vertex() != prod_vtx ) {
227  // problem scenario --- the mother already has a decay
228  // vertex which differs from the daughter's produciton
229  // vertex. This means there is internal
230  // inconsistency in the EPOS event record. Print an
231  // error
232  // Note: we could provide a fix by joining the two
233  // vertices with a dummy particle if the problem
234  // arrises often with any particular generator.
236  << "HepMC::IO_EPOS: inconsistent mother/daugher "
237  << "information in EPOS event "
239  << ". \n I recommend you try "
240  << "inspecting the event first with "
241  << "\n\tEPOS_Wrapper::check_hepevt_consistency()"
242  << "\n This warning can be turned off with the "
243  << "IO_EPOS::print_inconsistency_errors switch."
244  << std::endl;
245  }
246  if ( ++mother > EPOS_Wrapper::last_parent(i) ) mother = 0;
247  }
248  }
249 
251  ( int i, std::vector<HepMC::GenParticle*>& hepevt_particle, GenEvent* evt )
252  {
253  // Identical steps as for build_production_vertex
254  HepMC::GenParticle* p = hepevt_particle[i];
255  // a.
256  int daughter = EPOS_Wrapper::first_child(i);
257  HepMC::GenVertex* end_vtx = p->end_vertex();
258  while ( !end_vtx && daughter > 0 ) {
259  end_vtx = hepevt_particle[daughter]->production_vertex();
260  if ( end_vtx ) end_vtx->add_particle_in( p );
261  if ( ++daughter > EPOS_Wrapper::last_child(i) ) daughter = 0;
262  }
263  // b. (different from 3c. because EPOS particle can not know its
264  // decay position )
265  if ( !end_vtx && EPOS_Wrapper::number_children(i)>0 ) {
266  end_vtx = new GenVertex();
267  end_vtx->add_particle_in( p );
268  evt->add_vertex( end_vtx );
269  }
270  // c+d. loop over daughters to make sure their production vertices
271  // point back to the current vertex.
272  // We get the vertex position from the daughter as well.
273  daughter = EPOS_Wrapper::first_child(i);
274  while ( end_vtx && daughter > 0 ) {
275  if ( !hepevt_particle[daughter]->production_vertex() ) {
276  // if end vertex of the mother isn't specified, do it now
277  end_vtx->add_particle_out( hepevt_particle[daughter] );
278  //
279  // 2001-03-29 M.Dobbs, fill vertex the position.
280  if ( end_vtx->position()==FourVector(0,0,0,0) ) {
281  // again mm to cm conversion
282  FourVector prod_pos( EPOS_Wrapper::x(daughter),
283  EPOS_Wrapper::y(daughter),
284  EPOS_Wrapper::z(daughter),
285  EPOS_Wrapper::t(daughter)
286  );
287  if ( prod_pos != FourVector(0,0,0,0) ) {
288  end_vtx->set_position( prod_pos );
289  }
290  }
291  } else if (hepevt_particle[daughter]->production_vertex()
292  != end_vtx){
293  // problem scenario --- the daughter already has a prod
294  // vertex which differs from the mother's end
295  // vertex. This means there is internal
296  // inconsistency in the EPOS event record. Print an
297  // error
299  << "HepMC::IO_EPOS: inconsistent mother/daugher "
300  << "information in EPOS event "
302  << ". \n I recommend you try "
303  << "inspecting the event first with "
304  << "\n\tEPOS_Wrapper::check_hepevt_consistency()"
305  << "\n This warning can be turned off with the "
306  << "IO_EPOS::print_inconsistency_errors switch."
307  << std::endl;
308  }
309  if ( ++daughter > EPOS_Wrapper::last_child(i) ) daughter = 0;
310  }
311  if ( !p->end_vertex() && !p->production_vertex() ) {
312  // Added 2001-11-04, to try and handle Isajet problems.
313  build_production_vertex( i, hepevt_particle, evt );
314  }
315  }
316 
318  //
320  = new GenParticle( FourVector( EPOS_Wrapper::px(index),
321  EPOS_Wrapper::py(index),
322  EPOS_Wrapper::pz(index),
323  EPOS_Wrapper::e(index) ),
324  EPOS_Wrapper::id(index),
325  EPOS_Wrapper::status(index) );
326  p->setGeneratedMass( EPOS_Wrapper::m(index) );
327  p->suggest_barcode( index );
328  return p;
329  }
330 
331  int IO_EPOS::find_in_map( const std::map<HepMC::GenParticle*,int>& m,
332  HepMC::GenParticle* p) const {
333  std::map<HepMC::GenParticle*,int>::const_iterator iter = m.find(p);
334  if ( iter == m.end() ) return 0;
335  return iter->second;
336  }
337 
338 } // HepMC
339 
340 
341 
bool m_skip_nucl_frag
Definition: IO_EPOS.h:57
bool fill_next_event(HepMC::GenEvent *) override
Definition: IO_EPOS.cc:36
static void set_children(int index, int firstchild, int lastchild)
define children of a particle
Definition: EPOS_Wrapper.h:504
static void set_mass(int index, double mass)
set particle mass
Definition: EPOS_Wrapper.h:528
HepMC::GenParticle * build_particle(int index)
Definition: IO_EPOS.cc:317
static double m(int index)
generated mass
Definition: EPOS_Wrapper.h:442
static void set_status(int index, int status)
set particle status
Definition: EPOS_Wrapper.h:482
static void set_id(int index, int id)
set particle ID
Definition: EPOS_Wrapper.h:488
static double e(int index)
Energy.
Definition: EPOS_Wrapper.h:436
void build_end_vertex(int i, std::vector< HepMC::GenParticle * > &hepevt_particle, HepMC::GenEvent *evt)
Definition: IO_EPOS.cc:251
static double t(int index)
production time
Definition: EPOS_Wrapper.h:469
static void set_number_entries(int noentries)
set number of entries in EPOS
Definition: EPOS_Wrapper.h:479
static int number_parents(int index)
number of parents
Definition: EPOS_Wrapper.h:378
static void set_event_number(int evtno)
set event number
Definition: EPOS_Wrapper.h:476
static void set_momentum(int index, double px, double py, double pz, double e)
set particle momentum
Definition: EPOS_Wrapper.h:514
static int first_child(int index)
index of 1st daughter
Definition: EPOS_Wrapper.h:384
static int first_parent(int index)
index of 1st mother
Definition: EPOS_Wrapper.h:352
void build_production_vertex(int i, std::vector< HepMC::GenParticle * > &hepevt_particle, HepMC::GenEvent *evt)
Definition: IO_EPOS.cc:188
~IO_EPOS() override
Definition: IO_EPOS.cc:23
bool m_print_inconsistency_errors
Definition: IO_EPOS.h:55
bool m_trust_mothers_before_daughters
Definition: IO_EPOS.h:53
bool m_trust_both_mothers_and_daughters
Definition: IO_EPOS.h:54
static double y(int index)
Y Production vertex.
Definition: EPOS_Wrapper.h:455
void write_event(const HepMC::GenEvent *) override
Definition: IO_EPOS.cc:112
static double py(int index)
Y momentum.
Definition: EPOS_Wrapper.h:423
bool trust_beam_particles() const
Definition: IO_EPOS.h:79
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
static int event_number()
event number
Definition: EPOS_Wrapper.h:333
static int status(int index)
status code
Definition: EPOS_Wrapper.h:343
double p2[4]
Definition: TauolaWrapper.h:90
static void set_position(int index, double x, double y, double z, double t)
set particle production vertex
Definition: EPOS_Wrapper.h:535
static int number_entries()
num entries in current evt
Definition: EPOS_Wrapper.h:336
static double pz(int index)
Z momentum.
Definition: EPOS_Wrapper.h:430
void print(std::ostream &ostr=std::cout) const override
Definition: IO_EPOS.cc:25
static double z(int index)
Z Production vertex.
Definition: EPOS_Wrapper.h:462
static int number_children(int index)
number of children
Definition: EPOS_Wrapper.h:410
static int id(int index)
PDG particle id.
Definition: EPOS_Wrapper.h:346
static int max_number_entries()
size of common block
Definition: EPOS_Wrapper.h:225
double p1[4]
Definition: TauolaWrapper.h:89
static int last_child(int index)
index of last daughter
Definition: EPOS_Wrapper.h:392
static int last_parent(int index)
index of last mother
Definition: EPOS_Wrapper.h:360
int find_in_map(const std::map< HepMC::GenParticle *, int > &m, HepMC::GenParticle *p) const
Definition: IO_EPOS.cc:331
static double x(int index)
X Production vertex.
Definition: EPOS_Wrapper.h:448
static double px(int index)
X momentum.
Definition: EPOS_Wrapper.h:417
static void set_parents(int index, int firstparent, int lastparent)
define parents of a particle
Definition: EPOS_Wrapper.h:494