HepMC3 event record library: HEPEVT_Wrapper_Template.h Source File

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-2023 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 */
27namespace HepMC3
28{
29
30template <int max_particles, typename momentum_type = double>
31 class HEPEVT_Wrapper_Template
32{
33//
34// Functions
35//
36public:
37 /** @brief Default constructor */
38 HEPEVT_Wrapper_Template() { m_hepevtptr=nullptr;};
39 /** @brief Default destructor */
40 ~HEPEVT_Wrapper_Template() {};
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;
55private:
56 /** @brief Internalstorage storage. Optional.*/
57 std::shared_ptr<struct HEPEVT_Templated<max_particles, momentum_type> > m_internal_storage;
58//
59// Accessors
60//
61public:
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 foreign 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 = reinterpret_cast<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//
101template <int max_particles, typename momentum_type>
102 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::print_hepevt( std::ostream& ostr ) const
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}
111template <int max_particles, typename momentum_type>
112 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::print_hepevt_particle( int index, std::ostream& ostr ) const
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
126template <int max_particles, typename momentum_type>
127 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::allocate_internal_storage()
128{
129 m_internal_storage = std::make_shared<struct HEPEVT_Templated<max_particles, momentum_type>>();
130 m_hepevtptr = m_internal_storage.get();
131}
132
133template <int max_particles, typename momentum_type>
134 void HEPEVT_Wrapper_Template<max_particles, momentum_type>::copy_to_internal_storage(char *c, int N)
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 = *reinterpret_cast<int*> ((x));
141 x += sizeof(int);
142 m_hepevtptr->nhep = *reinterpret_cast<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
157template <int max_particles, typename momentum_type>
158 inline void HEPEVT_Wrapper_Template<max_particles, momentum_type>::zero_everything()
159{
160 memset(m_hepevtptr, 0, sizeof(struct HEPEVT_Templated<max_particles, momentum_type>));
161}
162
163template <int max_particles, typename momentum_type>
164 inline int HEPEVT_Wrapper_Template<max_particles, momentum_type>::number_parents( const int index ) const
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
169template <int max_particles, typename momentum_type>
170 inline int HEPEVT_Wrapper_Template<max_particles, momentum_type>::number_children( const int index ) const
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
175template <int max_particles, typename momentum_type>
176 inline int HEPEVT_Wrapper_Template<max_particles, momentum_type>::number_children_exact( const int index ) const
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
184template <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
191template <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
198template <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
207template <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
213template <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
222template <int max_particles, typename momentum_type>
223 inline bool HEPEVT_Wrapper_Template<max_particles, momentum_type>::fix_daughters()
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
GenEvent.h
Definition of class GenEvent.
GenParticle.h
Definition of class GenParticle.
GenVertex.h
Definition of class GenVertex.
HEPEVT_Helpers.h
Helper functions used to manipulate with HEPEVT block.
HepMC3::GenEvent
Stores event-related information.
Definition GenEvent.h:47
HepMC3::HEPEVT_Wrapper_Template::first_child
int first_child(const int index) const
Get index of 1st daughter.
Definition HEPEVT_Wrapper_Template.h:73
HepMC3::HEPEVT_Wrapper_Template::last_parent
int last_parent(const int index) const
Get index of last mother.
Definition HEPEVT_Wrapper_Template.h:72
HepMC3::HEPEVT_Wrapper_Template::~HEPEVT_Wrapper_Template
~HEPEVT_Wrapper_Template()
Default destructor.
Definition HEPEVT_Wrapper_Template.h:40
HepMC3::HEPEVT_Wrapper_Template::set_id
void set_id(const int index, const int id)
Set PDG particle id.
Definition HEPEVT_Wrapper_Template.h:90
HepMC3::HEPEVT_Wrapper_Template::event_number
int event_number() const
Get event number.
Definition HEPEVT_Wrapper_Template.h:67
HepMC3::HEPEVT_Wrapper_Template::HEPEVT_to_GenEvent
bool HEPEVT_to_GenEvent(GenEvent *evt) const
Convert HEPEVT to GenEvent.
Definition HEPEVT_Wrapper_Template.h:50
HepMC3::HEPEVT_Wrapper_Template::GenEvent_to_HEPEVT
bool GenEvent_to_HEPEVT(const GenEvent *evt)
Convert GenEvent to HEPEVT.
Definition HEPEVT_Wrapper_Template.h:48
HepMC3::HEPEVT_Wrapper_Template::last_child
int last_child(const int index) const
Get index of last daughter.
Definition HEPEVT_Wrapper_Template.h:74
HepMC3::HEPEVT_Wrapper_Template::allocate_internal_storage
void allocate_internal_storage()
Allocates m_internal_storage storage in smart pointer to hold HEPEVT of fixed size.
Definition HEPEVT_Wrapper_Template.h:127
HepMC3::HEPEVT_Wrapper_Template::first_parent
int first_parent(const int index) const
Get index of 1st mother.
Definition HEPEVT_Wrapper_Template.h:71
HepMC3::HEPEVT_Wrapper_Template::pz
double pz(const int index) const
Get Z momentum.
Definition HEPEVT_Wrapper_Template.h:77
HepMC3::HEPEVT_Wrapper_Template::zero_everything
void zero_everything()
Set all entries in HEPEVT to zero.
Definition HEPEVT_Wrapper_Template.h:158
HepMC3::HEPEVT_Wrapper_Template::py
double py(const int index) const
Get Y momentum.
Definition HEPEVT_Wrapper_Template.h:76
HepMC3::HEPEVT_Wrapper_Template::set_parents
void set_parents(const int index, const int firstparent, const int lastparent)
Set parents.
Definition HEPEVT_Wrapper_Template.h:185
HepMC3::HEPEVT_Wrapper_Template::number_children_exact
int number_children_exact(const int index) const
Get number of children by counting.
Definition HEPEVT_Wrapper_Template.h:176
HepMC3::HEPEVT_Wrapper_Template::print_hepevt_particle
void print_hepevt_particle(int index, std::ostream &ostr=std::cout) const
Print particle information.
Definition HEPEVT_Wrapper_Template.h:112
HepMC3::HEPEVT_Wrapper_Template::id
int id(const int index) const
Get PDG particle id.
Definition HEPEVT_Wrapper_Template.h:70
HepMC3::HEPEVT_Wrapper_Template::t
double t(const int index) const
Get production time.
Definition HEPEVT_Wrapper_Template.h:83
HepMC3::HEPEVT_Wrapper_Template::set_momentum
void set_momentum(const int index, const double px, const double py, const double pz, const double e)
Set 4-momentum.
Definition HEPEVT_Wrapper_Template.h:199
HepMC3::HEPEVT_Wrapper_Template::set_children
void set_children(const int index, const int firstchild, const int lastchild)
Set children.
Definition HEPEVT_Wrapper_Template.h:192
HepMC3::HEPEVT_Wrapper_Template::number_parents
int number_parents(const int index) const
Get number of parents.
Definition HEPEVT_Wrapper_Template.h:164
HepMC3::HEPEVT_Wrapper_Template::set_position
void set_position(const int index, const double x, const double y, const double z, const double t)
Set position in time-space.
Definition HEPEVT_Wrapper_Template.h:214
HepMC3::HEPEVT_Wrapper_Template::y
double y(const int index) const
Get Y Production vertex.
Definition HEPEVT_Wrapper_Template.h:81
HepMC3::HEPEVT_Wrapper_Template::fix_daughters
bool fix_daughters()
Tries to fix list of daughters.
Definition HEPEVT_Wrapper_Template.h:223
HepMC3::HEPEVT_Wrapper_Template::set_max_number_entries
void set_max_number_entries(unsigned int size)
Set block size.
Definition HEPEVT_Wrapper_Template.h:64
HepMC3::HEPEVT_Wrapper_Template::number_entries
int number_entries() const
Get number of entries.
Definition HEPEVT_Wrapper_Template.h:68
HepMC3::HEPEVT_Wrapper_Template::set_hepevt_address
void set_hepevt_address(char *c)
Set Fortran block address.
Definition HEPEVT_Wrapper_Template.h:65
HepMC3::HEPEVT_Wrapper_Template::m
double m(const int index) const
Get generated mass.
Definition HEPEVT_Wrapper_Template.h:79
HepMC3::HEPEVT_Wrapper_Template::status
int status(const int index) const
Get status code.
Definition HEPEVT_Wrapper_Template.h:69
HepMC3::HEPEVT_Wrapper_Template::m_internal_storage
std::shared_ptr< struct HEPEVT_Templated< max_particles, momentum_type > > m_internal_storage
Internalstorage storage. Optional.
Definition HEPEVT_Wrapper_Template.h:57
HepMC3::HEPEVT_Wrapper_Template::px
double px(const int index) const
Get X momentum.
Definition HEPEVT_Wrapper_Template.h:75
HepMC3::HEPEVT_Wrapper_Template::number_children
int number_children(const int index) const
Get number of children from the range of daughters.
Definition HEPEVT_Wrapper_Template.h:170
HepMC3::HEPEVT_Wrapper_Template::z
double z(const int index) const
Get Z Production vertex.
Definition HEPEVT_Wrapper_Template.h:82
HepMC3::HEPEVT_Wrapper_Template::m_hepevtptr
struct HEPEVT_Templated< max_particles, momentum_type > * m_hepevtptr
Fortran common block HEPEVT.
Definition HEPEVT_Wrapper_Template.h:54
HepMC3::HEPEVT_Wrapper_Template::print_hepevt
void print_hepevt(std::ostream &ostr=std::cout) const
Print information from HEPEVT common block.
Definition HEPEVT_Wrapper_Template.h:102
HepMC3::HEPEVT_Wrapper_Template::set_mass
void set_mass(const int index, double mass)
Set mass.
Definition HEPEVT_Wrapper_Template.h:208
HepMC3::HEPEVT_Wrapper_Template::max_number_entries
int max_number_entries() const
Block size.
Definition HEPEVT_Wrapper_Template.h:66
HepMC3::HEPEVT_Wrapper_Template::x
double x(const int index) const
Get X Production vertex.
Definition HEPEVT_Wrapper_Template.h:80
HepMC3::HEPEVT_Wrapper_Template::set_event_number
void set_event_number(const int evtno)
Set event number.
Definition HEPEVT_Wrapper_Template.h:87
HepMC3::HEPEVT_Wrapper_Template::copy_to_internal_storage
void copy_to_internal_storage(char *c, int N)
Copies the content of foreign common block into the internal storage.
Definition HEPEVT_Wrapper_Template.h:134
HepMC3::HEPEVT_Wrapper_Template::set_status
void set_status(const int index, const int status)
Set status code.
Definition HEPEVT_Wrapper_Template.h:89
HepMC3::HEPEVT_Wrapper_Template::HEPEVT_Wrapper_Template
HEPEVT_Wrapper_Template()
Default constructor.
Definition HEPEVT_Wrapper_Template.h:38
HepMC3::HEPEVT_Wrapper_Template::set_number_entries
void set_number_entries(const int noentries)
Set number of entries.
Definition HEPEVT_Wrapper_Template.h:88
HepMC3::HEPEVT_Wrapper_Template::e
double e(const int index) const
Get Energy.
Definition HEPEVT_Wrapper_Template.h:78
HepMC3
HepMC3 main namespace.
Definition AnalysisExample.h:24
HepMC3::HEPEVT_to_GenEvent_nonstatic
bool HEPEVT_to_GenEvent_nonstatic(GenEvent *evt, T *A)
Converts HEPEVT into GenEvent.
Definition HEPEVT_Helpers.h:110
HepMC3::GenEvent_to_HEPEVT_nonstatic
bool GenEvent_to_HEPEVT_nonstatic(const GenEvent *evt, T *A)
Converts GenEvent into HEPEVT.
Definition HEPEVT_Helpers.h:181
HEPEVT_Templated
C structure representing Fortran common block HEPEVT T. Sjöstrand et al., "A proposed standard event ...