HepMC3 event record library
HEPEVT_Wrapper_Template.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of HepMC
4 // Copyright (C) 2014-2021 The HepMC collaboration (see AUTHORS for details)
5 //
6 #ifndef HEPMC3_HEPEVT_WRAPPER_TEMPLATE_H
7 #define HEPMC3_HEPEVT_WRAPPER_TEMPLATE_H
8 #include <iostream>
9 #include <cstdio>
10 #include <set>
11 #include <map>
12 #include <cstring> // memset
13 #include <cassert>
14 #include <algorithm> //min max for VS2017
15 #include "HepMC3/GenEvent.h"
16 #include "HepMC3/GenParticle.h"
17 #include "HepMC3/GenVertex.h"
18 #include "HepMC3/HEPEVT_Helpers.h"
19 
20 /**
21  * @file HEPEVT_Wrapper_Template.h
22  * @brief Definition of \b class HEPEVT_Wrapper_Template
23  *
24  * @class HepMC3::HEPEVT_Wrapper_Template
25  * @brief An interface to HEPEVT common block implemented as template class
26  */
27 namespace HepMC3
28 {
29 
30 template <int max_particles, typename momentum_type = double>
32 {
33 //
34 // Functions
35 //
36 public:
37  /** @brief Default constructor */
39  /** @brief Default destructor */
41  /** @brief Print information from HEPEVT common block */
42  void print_hepevt( std::ostream& ostr = std::cout ) const;
43  /** @brief Print particle information */
44  void print_hepevt_particle( int index, std::ostream& ostr = std::cout ) const;
45  /** @brief Set all entries in HEPEVT to zero */
46  void zero_everything();
47  /** @brief Convert GenEvent to HEPEVT*/
48  bool GenEvent_to_HEPEVT( const GenEvent* evt ) { return GenEvent_to_HEPEVT_nonstatic(evt, this);};
49  /** @brief Convert HEPEVT to GenEvent*/
50  bool HEPEVT_to_GenEvent( GenEvent* evt ) const { return HEPEVT_to_GenEvent_nonstatic(evt, this);};
51  /** @brief Tries to fix list of daughters */
52  bool fix_daughters();
53  /** @brief Fortran common block HEPEVT */
54  struct HEPEVT_Templated<max_particles, momentum_type>* m_hepevtptr;
55 private:
56  /** @brief Internalstorage storage. Optional.*/
57  std::shared_ptr<struct HEPEVT_Templated<max_particles, momentum_type> > m_internal_storage;
58 //
59 // Accessors
60 //
61 public:
62  void allocate_internal_storage(); //!< Allocates m_internal_storage storage in smart pointer to hold HEPEVT of fixed size
63  void copy_to_internal_storage( char *c, int N ); //!< Copies the content of foreight common block into the internal storage
64  void set_max_number_entries( unsigned int size ) { if (size != max_particles) printf("This implementation does not support change of the block size.\n"); assert(size == max_particles); }//!< Set block size
65  void set_hepevt_address(char *c) { m_hepevtptr = (struct HEPEVT_Templated<max_particles, momentum_type>*)c; } //!< Set Fortran block address
66  int max_number_entries() const { return max_particles; } //!< Block size
67  int event_number() const { return m_hepevtptr->nevhep; } //!< Get event number
68  int number_entries() const { return m_hepevtptr->nhep; } //!< Get number of entries
69  int status(const int index ) const { return m_hepevtptr->isthep[index-1]; } //!< Get status code
70  int id(const int index ) const { return m_hepevtptr->idhep[index-1]; } //!< Get PDG particle id
71  int first_parent(const int index ) const { return m_hepevtptr->jmohep[index-1][0]; } //!< Get index of 1st mother
72  int last_parent(const int index ) const { return m_hepevtptr->jmohep[index-1][1]; } //!< Get index of last mother
73  int first_child(const int index ) const { return m_hepevtptr->jdahep[index-1][0]; } //!< Get index of 1st daughter
74  int last_child(const int index ) const { return m_hepevtptr->jdahep[index-1][1]; } //!< Get index of last daughter
75  double px(const int index ) const { return m_hepevtptr->phep[index-1][0]; } //!< Get X momentum
76  double py(const int index ) const { return m_hepevtptr->phep[index-1][1]; } //!< Get Y momentum
77  double pz(const int index ) const { return m_hepevtptr->phep[index-1][2]; } //!< Get Z momentum
78  double e(const int index ) const { return m_hepevtptr->phep[index-1][3]; } //!< Get Energy
79  double m(const int index ) const { return m_hepevtptr->phep[index-1][4]; } //!< Get generated mass
80  double x(const int index ) const { return m_hepevtptr->vhep[index-1][0]; } //!< Get X Production vertex
81  double y(const int index ) const { return m_hepevtptr->vhep[index-1][1]; } //!< Get Y Production vertex
82  double z(const int index ) const { return m_hepevtptr->vhep[index-1][2]; } //!< Get Z Production vertex
83  double t(const int index ) const { return m_hepevtptr->vhep[index-1][3]; } //!< Get production time
84  int number_parents(const int index ) const; //!< Get number of parents
85  int number_children(const int index ) const; //!< Get number of children from the range of daughters
86  int number_children_exact(const int index ) const; //!< Get number of children by counting
87  void set_event_number( const int evtno ) { m_hepevtptr->nevhep = evtno; } //!< Set event number
88  void set_number_entries( const int noentries ) { m_hepevtptr->nhep = noentries; } //!< Set number of entries
89  void set_status( const int index, const int status ) { m_hepevtptr->isthep[index-1] = status; } //!< Set status code
90  void set_id(const int index, const int id ) { m_hepevtptr->idhep[index-1] = id; } //!< Set PDG particle id
91  void set_parents( const int index, const int firstparent, const int lastparent ); //!< Set parents
92  void set_children( const int index, const int firstchild, const int lastchild ); //!< Set children
93  void set_momentum( const int index, const double px, const double py, const double pz, const double e ); //!< Set 4-momentum
94  void set_mass( const int index, double mass ); //!< Set mass
95  void set_position( const int index, const double x, const double y, const double z, const double t ); //!< Set position in time-space
96 };
97 
98 //
99 // inline definitions
100 //
101 template <int max_particles, typename momentum_type>
103 {
104  ostr << " Event No.: " << m_hepevtptr->nevhep << std::endl;
105  ostr << " Nr Type Parent(s) Daughter(s) Px Py Pz E Inv. M." << std::endl;
106  for ( int i = 1; i <= m_hepevtptr->nhep; ++i )
107  {
108  print_hepevt_particle( i, ostr );
109  }
110 }
111 template <int max_particles, typename momentum_type>
113 {
114  char buf[255];//Note: the format is fixed, so no reason for complicated treatment
115 
116  sprintf(buf, "%5i %6i", index, m_hepevtptr->idhep[index-1]);
117  ostr << buf;
118  sprintf(buf, "%4i - %4i ", m_hepevtptr->jmohep[index-1][0], m_hepevtptr->jmohep[index-1][1]);
119  ostr << buf;
120  sprintf(buf, "%4i - %4i ", m_hepevtptr->jdahep[index-1][0], m_hepevtptr->jdahep[index-1][1]);
121  ostr << buf;
122  sprintf(buf, "%8.2f %8.2f %8.2f %8.2f %8.2f", m_hepevtptr->phep[index-1][0], m_hepevtptr->phep[index-1][1], m_hepevtptr->phep[index-1][2], m_hepevtptr->phep[index-1][3], m_hepevtptr->phep[index-1][4]);
123  ostr << buf << std::endl;
124 }
125 
126 template <int max_particles, typename momentum_type>
128 {
129  m_internal_storage = std::make_shared<struct HEPEVT_Templated<max_particles, momentum_type>>();
130  m_hepevtptr = m_internal_storage.get();
131 }
132 
133 template <int max_particles, typename momentum_type>
135 {
136  if ( N < 1 || N > max_particles) return;
137  m_internal_storage = std::make_shared<struct HEPEVT_Templated<max_particles, momentum_type>>();
138  m_hepevtptr = m_internal_storage.get();
139  char* x = c;
140  m_hepevtptr->nevhep = *((int*)x);
141  x += sizeof(int);
142  m_hepevtptr->nhep = *((int*)x);
143  x += sizeof(int);
144  memcpy(m_hepevtptr->isthep, x, N*sizeof(int));
145  x += sizeof(int)*N;
146  memcpy(m_hepevtptr->idhep, x, N*sizeof(int));
147  x += sizeof(int)*N;
148  memcpy(m_hepevtptr->jmohep, x, 2*N*sizeof(int));
149  x += sizeof(int)*N*2;
150  memcpy(m_hepevtptr->jdahep, x, 2*N*sizeof(int));
151  x += sizeof(int)*N*2;
152  memcpy(m_hepevtptr->phep, x, 5*N*sizeof(momentum_type));
153  x += sizeof(momentum_type)*N*5;
154  memcpy(m_hepevtptr->vhep, x, 4*N*sizeof(momentum_type));
155 }
156 
157 template <int max_particles, typename momentum_type>
159 {
160  memset(m_hepevtptr, 0, sizeof(struct HEPEVT_Templated<max_particles, momentum_type>));
161 }
162 
163 template <int max_particles, typename momentum_type>
165 {
166  return (m_hepevtptr->jmohep[index-1][0]) ? (m_hepevtptr->jmohep[index-1][1]) ? m_hepevtptr->jmohep[index-1][1]-m_hepevtptr->jmohep[index-1][0] : 1 : 0;
167 }
168 
169 template <int max_particles, typename momentum_type>
171 {
172  return (m_hepevtptr->jdahep[index-1][0]) ? (m_hepevtptr->jdahep[index-1][1]) ? m_hepevtptr->jdahep[index-1][1]-m_hepevtptr->jdahep[index-1][0] : 1 : 0;
173 }
174 
175 template <int max_particles, typename momentum_type>
177 {
178  int nc = 0;
179  for ( int i = 1; i <= m_hepevtptr->nhep; ++i )
180  if (((m_hepevtptr->jmohep[i-1][0] <= index && m_hepevtptr->jmohep[i-1][1] >= index)) || (m_hepevtptr->jmohep[i-1][0] == index) || (m_hepevtptr->jmohep[i-1][1]==index)) nc++;
181  return nc;
182 }
183 
184 template <int max_particles, typename momentum_type>
185 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::set_parents( const int index, const int firstparent, const int lastparent )
186 {
187  m_hepevtptr->jmohep[index-1][0] = firstparent;
188  m_hepevtptr->jmohep[index-1][1] = lastparent;
189 }
190 
191 template <int max_particles, typename momentum_type>
192 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::set_children( const int index, const int firstchild, const int lastchild )
193 {
194  m_hepevtptr->jdahep[index-1][0] = firstchild;
195  m_hepevtptr->jdahep[index-1][1] = lastchild;
196 }
197 
198 template <int max_particles, typename momentum_type>
199 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::set_momentum( const int index, const double px, const double py, const double pz, const double e )
200 {
201  m_hepevtptr->phep[index-1][0] = px;
202  m_hepevtptr->phep[index-1][1] = py;
203  m_hepevtptr->phep[index-1][2] = pz;
204  m_hepevtptr->phep[index-1][3] = e;
205 }
206 
207 template <int max_particles, typename momentum_type>
208 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::set_mass( const int index, double mass )
209 {
210  m_hepevtptr->phep[index-1][4] = mass;
211 }
212 
213 template <int max_particles, typename momentum_type>
214 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::set_position( const int index, const double x, const double y, const double z, const double t )
215 {
216  m_hepevtptr->vhep[index-1][0] = x;
217  m_hepevtptr->vhep[index-1][1] = y;
218  m_hepevtptr->vhep[index-1][2] = z;
219  m_hepevtptr->vhep[index-1][3] = t;
220 }
221 
222 template <int max_particles, typename momentum_type>
224 {
225  /*AV The function should be called for a record that has correct particle ordering and mother ids.
226  As a result it produces a record with ranges where the daughters can be found.
227  Not every particle in the range will be a daughter. It is true only for proper events.
228  The return tells if the record was fixed succesfully.
229  */
230  for ( int i = 1; i <= number_entries(); i++ )
231  for ( int k=1; k <= number_entries(); k++ ) if (i != k)
232  if ((first_parent(k) <= i) && (i <= last_parent(k)))
233  set_children(i, (first_child(i) == 0 ? k : std::min(first_child(i), k)), (last_child(i) == 0 ? k : std::max(last_child(i), k)));
234  bool is_fixed = true;
235  for ( int i = 1; i <= number_entries(); i++ )
236  is_fixed = (is_fixed && (number_children_exact(i) == number_children(i)));
237  return is_fixed;
238 }
239 
240 } // namespace HepMC3
241 #endif
void set_mass(const int index, double mass)
Set mass.
int status(const int index) const
Get status code.
double z(const int index) const
Get Z Production vertex.
void copy_to_internal_storage(char *c, int N)
Copies the content of foreight common block into the internal storage.
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., &quot;A proposed standard event...
void set_id(const int index, const int id)
Set PDG particle id.
void set_max_number_entries(unsigned int size)
Set block size.
void set_momentum(const int index, const double px, const double py, const double pz, const double e)
Set 4-momentum.
Definition of class GenParticle.
bool HEPEVT_to_GenEvent_nonstatic(GenEvent *evt, T *A)
Converts HEPEVT into GenEvent.
void allocate_internal_storage()
Allocates m_internal_storage storage in smart pointer to hold HEPEVT of fixed size.
bool HEPEVT_to_GenEvent(GenEvent *evt) const
Convert HEPEVT to GenEvent.
int id(const int index) const
Get PDG particle id.
int event_number() const
Get event number.
void set_parents(const int index, const int firstparent, const int lastparent)
Set parents.
Definition of class GenVertex.
int last_child(const int index) const
Get index of last daughter.
double x(const int index) const
Get X Production vertex.
Helper functions used to manipulate with HEPEVT block.
void set_hepevt_address(char *c)
Set Fortran block address.
void set_event_number(const int evtno)
Set event number.
void print_hepevt_particle(int index, std::ostream &ostr=std::cout) const
Print particle information.
Stores event-related information.
Definition: GenEvent.h:41
double e(const int index) const
Get Energy.
double m(const int index) const
Get generated mass.
bool GenEvent_to_HEPEVT(const GenEvent *evt)
Convert GenEvent to HEPEVT.
double py(const int index) const
Get Y momentum.
~HEPEVT_Wrapper_Template()
Default destructor.
double pz(const int index) const
Get Z momentum.
int last_parent(const int index) const
Get index of last mother.
bool fix_daughters()
Tries to fix list of daughters.
double y(const int index) const
Get Y Production vertex.
int number_children_exact(const int index) const
Get number of children by counting.
An interface to HEPEVT common block implemented as template class.
int max_number_entries() const
Block size.
int first_child(const int index) const
Get index of 1st daughter.
HEPEVT_Wrapper_Template()
Default constructor.
void print_hepevt(std::ostream &ostr=std::cout) const
Print information from HEPEVT common block.
void set_number_entries(const int noentries)
Set number of entries.
bool GenEvent_to_HEPEVT_nonstatic(const GenEvent *evt, T *A)
Converts GenEvent into HEPEVT.
Definition of class GenEvent.
int number_children(const int index) const
Get number of children from the range of daughters.
int number_entries() const
Get number of entries.
void set_children(const int index, const int firstchild, const int lastchild)
Set children.
void set_position(const int index, const double x, const double y, const double z, const double t)
Set position in time-space.
double t(const int index) const
Get production time.
struct HEPEVT_Templated< max_particles, momentum_type > * m_hepevtptr
Fortran common block HEPEVT.
double px(const int index) const
Get X momentum.
void set_status(const int index, const int status)
Set status code.
void zero_everything()
Set all entries in HEPEVT to zero.
int first_parent(const int index) const
Get index of 1st mother.
std::shared_ptr< struct HEPEVT_Templated< max_particles, momentum_type > > m_internal_storage
Internalstorage storage. Optional.
int number_parents(const int index) const
Get number of parents.