1 //--------------------------------------------------------------------------
3 #ifndef HEPEVT_EntriesAllocation
4 #define HEPEVT_EntriesAllocation 50000
5 #endif // HEPEVT_EntriesAllocation
7 //--------------------------------------------------------------------------
8 #ifndef HEPMC_HEPEVT_COMMON_H
9 #define HEPMC_HEPEVT_COMMON_H
10 //////////////////////////////////////////////////////////////////////////
12 // PARAMETER (NMXHEP=2000)
13 // COMMON/HEPEVT/NEVHEP,NHEP,ISTHEP(NMXHEP),IDHEP(NMXHEP),
14 // & JMOHEP(2,NMXHEP),JDAHEP(2,NMXHEP),PHEP(5,NMXHEP),VHEP(4,NMXHEP)
15 /**********************************************************/
16 /* D E S C R I P T I O N : */
17 /*--------------------------------------------------------*/
18 /* NEVHEP - event number (or some special meaning*/
19 /* (see documentation for details) */
20 /* NHEP - actual number of entries in current */
22 /* ISTHEP[IHEP] - status code for IHEP'th entry - see */
23 /* documentation for details */
24 /* IDHEP [IHEP] - IHEP'th particle identifier according*/
26 /* JMOHEP[IHEP][0] - pointer to position of 1st mother */
27 /* JMOHEP[IHEP][1] - pointer to position of 2nd mother */
28 /* JDAHEP[IHEP][0] - pointer to position of 1st daughter */
29 /* JDAHEP[IHEP][1] - pointer to position of 2nd daughter */
30 /* PHEP [IHEP][0] - X momentum */
31 /* PHEP [IHEP][1] - Y momentum */
32 /* PHEP [IHEP][2] - Z momentum */
33 /* PHEP [IHEP][3] - Energy */
34 /* PHEP [IHEP][4] - Mass */
35 /* VHEP [IHEP][0] - X vertex */
36 /* VHEP [IHEP][1] - Y vertex */
37 /* VHEP [IHEP][2] - Z vertex */
38 /* VHEP [IHEP][3] - production time */
39 /*========================================================*/
40 // Remember, array(1) is the first entry in a fortran array, array[0] is the
41 // first entry in a C array.
43 // This interface to HEPEVT common block treats the block as
44 // an array of bytes --- the precision and number of entries
45 // is determined "on the fly" by the wrapper and used to decode
48 // HEPEVT_EntriesAllocation is the maximum size of the HEPEVT common block
49 // that can be interfaced.
50 // It is NOT the actual size of the HEPEVT common used in each
51 // individual application. The actual size can be changed on
52 // the fly using HEPEVT_Wrapper::set_max_number_entries().
53 // Thus HEPEVT_EntriesAllocation should typically be set
54 // to the maximum possible number of entries --- 10000 is a good choice
55 // (and is the number used by ATLAS versions of Pythia).
57 // Note: a statement like *( (int*)&hepevt.data[0] )
58 // takes the memory address of the first byte in HEPEVT,
59 // interprets it as an integer pointer,
60 // and dereferences the pointer.
61 // i.e. it returns an integer corresponding to nevhep
66 const unsigned int hepevt_bytes_allocation =
67 sizeof(long int) * ( 2 + 6 * HEPEVT_EntriesAllocation )
68 + sizeof(double) * ( 9 * HEPEVT_EntriesAllocation );
71 #ifdef _WIN32 // Platform: Windows MS Visual C++
73 char data[hepevt_bytes_allocation];
75 extern "C" HEPEVT_DEF HEPEVT;
81 char data[hepevt_bytes_allocation];
84 #define hepevt hepevt_
88 #endif // HEPMC_HEPEVT_COMMON_H
90 //--------------------------------------------------------------------------
91 #ifndef HEPMC_HEPEVT_WRAPPER_H
92 #define HEPMC_HEPEVT_WRAPPER_H
94 //////////////////////////////////////////////////////////////////////////
95 // Matt.Dobbs@Cern.CH, April 24, 2000, refer to:
96 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
97 // High Energy Physics", Computer Physics Communications (to be published).
99 // Generic Wrapper for the fortran HEPEVT common block
100 // This class is intended for static use only - it makes no sense to
102 // Updated: June 30, 2000 (static initialization moved to separate .cxx file)
103 //////////////////////////////////////////////////////////////////////////
105 // The index refers to the fortran style index:
106 // i.e. index=1 refers to the first entry in the HEPEVT common block.
107 // all indices must be >0
108 // number_entries --> integer between 0 and max_number_entries() giving total
109 // number of sequential particle indices
110 // first_parent/child --> index of first mother/child if there is one,
112 // last_parent/child --> if number children is >1, address of last parent/child
113 // if number of children is 1, same as first_parent/child
114 // if there are no children, returns zero.
115 // is_double_precision --> T or F depending if floating point variables
120 #include <cstdio> // needed for formatted output using sprintf
124 //! Generic Wrapper for the fortran HEPEVT common block
126 /// \class HEPEVT_Wrapper
127 /// This class is intended for static use only - it makes no sense to
130 class HEPEVT_Wrapper {
133 /// write information from HEPEVT common block
134 static void print_hepevt( std::ostream& ostr = std::cout );
135 /// write particle information to ostr
136 static void print_hepevt_particle( int index,
137 std::ostream& ostr = std::cout );
138 static bool is_double_precision(); //!< True if common block uses double
140 /// check for problems with HEPEVT common block
141 static bool check_hepevt_consistency( std::ostream& ostr = std::cout );
143 /// set all entries in HEPEVT to zero
144 static void zero_everything();
149 static int event_number(); //!< event number
150 static int number_entries(); //!< num entries in current evt
151 static int status( int index ); //!< status code
152 static int id( int index ); //!< PDG particle id
153 static int first_parent( int index ); //!< index of 1st mother
154 static int last_parent( int index ); //!< index of last mother
155 static int number_parents( int index ); //!< number of parents
156 static int first_child( int index ); //!< index of 1st daughter
157 static int last_child( int index ); //!< index of last daughter
158 static int number_children( int index ); //!< number of children
159 static double px( int index ); //!< X momentum
160 static double py( int index ); //!< Y momentum
161 static double pz( int index ); //!< Z momentum
162 static double e( int index ); //!< Energy
163 static double m( int index ); //!< generated mass
164 static double x( int index ); //!< X Production vertex
165 static double y( int index ); //!< Y Production vertex
166 static double z( int index ); //!< Z Production vertex
167 static double t( int index ); //!< production time
174 static void set_event_number( int evtno );
175 /// set number of entries in HEPEVT
176 static void set_number_entries( int noentries );
177 /// set particle status
178 static void set_status( int index, int status );
180 static void set_id( int index, int id );
181 /// define parents of a particle
182 static void set_parents( int index, int firstparent, int lastparent );
183 /// define children of a particle
184 static void set_children( int index, int firstchild, int lastchild );
185 /// set particle momentum
186 static void set_momentum( int index, double px, double py,
187 double pz, double e );
188 /// set particle mass
189 static void set_mass( int index, double mass );
190 /// set particle production vertex
191 static void set_position( int index, double x, double y, double z,
193 //////////////////////
194 // HEPEVT Floorplan //
195 //////////////////////
196 static unsigned int sizeof_int(); //!< size of integer in bytes
197 static unsigned int sizeof_real(); //!< size of real in bytes
198 static int max_number_entries(); //!< size of common block
199 static void set_sizeof_int(unsigned int); //!< define size of integer
200 static void set_sizeof_real(unsigned int); //!< define size of real
201 static void set_max_number_entries(unsigned int); //!< define size of common block
204 /// navigate a byte array
205 static double byte_num_to_double( unsigned int );
206 /// navigate a byte array
207 static int byte_num_to_int( unsigned int );
208 /// pretend common block is an array of bytes
209 static void write_byte_num( double, unsigned int );
210 /// pretend common block is an array of bytes
211 static void write_byte_num( int, unsigned int );
212 /// print output legend
213 static void print_legend( std::ostream& ostr = std::cout );
216 static unsigned int s_sizeof_int;
217 static unsigned int s_sizeof_real;
218 static unsigned int s_max_number_entries;
222 //////////////////////////////
223 // HEPEVT Floorplan Inlines //
224 //////////////////////////////
225 inline unsigned int HEPEVT_Wrapper::sizeof_int(){ return s_sizeof_int; }
227 inline unsigned int HEPEVT_Wrapper::sizeof_real(){ return s_sizeof_real; }
229 inline int HEPEVT_Wrapper::max_number_entries()
230 { return (int)s_max_number_entries; }
232 inline void HEPEVT_Wrapper::set_sizeof_int( unsigned int size )
234 if ( size != sizeof(short int) && size != sizeof(long int) && size != sizeof(int) ) {
235 std::cerr << "HepMC is not able to handle integers "
236 << " of size other than 2 or 4."
237 << " You requested: " << size << std::endl;
242 inline void HEPEVT_Wrapper::set_sizeof_real( unsigned int size ) {
243 if ( size != sizeof(float) && size != sizeof(double) ) {
244 std::cerr << "HepMC is not able to handle floating point numbers"
245 << " of size other than 4 or 8."
246 << " You requested: " << size << std::endl;
248 s_sizeof_real = size;
251 inline void HEPEVT_Wrapper::set_max_number_entries( unsigned int size ) {
252 s_max_number_entries = size;
255 inline double HEPEVT_Wrapper::byte_num_to_double( unsigned int b ) {
256 if ( b >= hepevt_bytes_allocation ) std::cerr
257 << "HEPEVT_Wrapper: requested hepevt data exceeds allocation"
259 if ( s_sizeof_real == sizeof(float) ) {
260 float* myfloat = (float*)&hepevt.data[b];
261 return (double)(*myfloat);
262 } else if ( s_sizeof_real == sizeof(double) ) {
263 double* mydouble = (double*)&hepevt.data[b];
267 << "HEPEVT_Wrapper: illegal floating point number length."
268 << s_sizeof_real << std::endl;
273 inline int HEPEVT_Wrapper::byte_num_to_int( unsigned int b ) {
274 if ( b >= hepevt_bytes_allocation ) std::cerr
275 << "HEPEVT_Wrapper: requested hepevt data exceeds allocation"
277 if ( s_sizeof_int == sizeof(short int) ) {
278 short int* myshortint = (short int*)&hepevt.data[b];
279 return (int)(*myshortint);
280 } else if ( s_sizeof_int == sizeof(long int) ) {
281 long int* mylongint = (long int*)&hepevt.data[b];
283 // on some 64 bit machines, int, short, and long are all different
284 } else if ( s_sizeof_int == sizeof(int) ) {
285 int* myint = (int*)&hepevt.data[b];
289 << "HEPEVT_Wrapper: illegal integer number length."
290 << s_sizeof_int << std::endl;
295 inline void HEPEVT_Wrapper::write_byte_num( double in, unsigned int b ) {
296 if ( b >= hepevt_bytes_allocation ) std::cerr
297 << "HEPEVT_Wrapper: requested hepevt data exceeds allocation"
299 if ( s_sizeof_real == sizeof(float) ) {
300 float* myfloat = (float*)&hepevt.data[b];
301 (*myfloat) = (float)in;
302 } else if ( s_sizeof_real == sizeof(double) ) {
303 double* mydouble = (double*)&hepevt.data[b];
304 (*mydouble) = (double)in;
307 << "HEPEVT_Wrapper: illegal floating point number length."
308 << s_sizeof_real << std::endl;
312 inline void HEPEVT_Wrapper::write_byte_num( int in, unsigned int b ) {
313 if ( b >= hepevt_bytes_allocation ) std::cerr
314 << "HEPEVT_Wrapper: requested hepevt data exceeds allocation"
316 if ( s_sizeof_int == sizeof(short int) ) {
317 short int* myshortint = (short int*)&hepevt.data[b];
318 (*myshortint) = (short int)in;
319 } else if ( s_sizeof_int == sizeof(long int) ) {
320 long int* mylongint = (long int*)&hepevt.data[b];
321 (*mylongint) = (int)in;
322 // on some 64 bit machines, int, short, and long are all different
323 } else if ( s_sizeof_int == sizeof(int) ) {
324 int* myint = (int*)&hepevt.data[b];
328 << "HEPEVT_Wrapper: illegal integer number length."
329 << s_sizeof_int << std::endl;
337 inline bool HEPEVT_Wrapper::is_double_precision()
339 // true if 8byte floating point numbers are used in the HepEVT common.
340 return ( sizeof(double) == sizeof_real() );
343 inline int HEPEVT_Wrapper::event_number()
344 { return byte_num_to_int(0); }
346 inline int HEPEVT_Wrapper::number_entries()
348 int nhep = byte_num_to_int( 1*sizeof_int() );
349 return ( nhep <= max_number_entries() ?
350 nhep : max_number_entries() );
353 inline int HEPEVT_Wrapper::status( int index )
354 { return byte_num_to_int( (2+index-1) * sizeof_int() ); }
356 inline int HEPEVT_Wrapper::id( int index )
358 return byte_num_to_int( (2+max_number_entries()+index-1)
362 inline int HEPEVT_Wrapper::first_parent( int index )
364 int parent = byte_num_to_int( (2+2*max_number_entries()+2*(index-1))
366 return ( parent > 0 && parent <= number_entries() ) ?
370 inline int HEPEVT_Wrapper::last_parent( int index )
372 // Returns the Index of the LAST parent in the HEPEVT record
373 // for particle with Index index.
374 // If there is only one parent, the last parent is forced to
375 // be the same as the first parent.
376 // If there are no parents for this particle, both the first_parent
377 // and the last_parent with return 0.
378 // Error checking is done to ensure the parent is always
379 // within range ( 0 <= parent <= nhep )
381 int firstparent = first_parent(index);
382 int parent = byte_num_to_int( (2+2*max_number_entries()+2*(index-1)+1)
384 return ( parent > firstparent && parent <= number_entries() )
385 ? parent : firstparent;
388 inline int HEPEVT_Wrapper::number_parents( int index ) {
389 int firstparent = first_parent(index);
390 return ( firstparent>0 ) ?
391 ( 1+last_parent(index)-firstparent ) : 0;
394 inline int HEPEVT_Wrapper::first_child( int index )
396 int child = byte_num_to_int( (2+4*max_number_entries()+2*(index-1))
398 return ( child > 0 && child <= number_entries() ) ?
402 inline int HEPEVT_Wrapper::last_child( int index )
404 // Returns the Index of the LAST child in the HEPEVT record
405 // for particle with Index index.
406 // If there is only one child, the last child is forced to
407 // be the same as the first child.
408 // If there are no children for this particle, both the first_child
409 // and the last_child with return 0.
410 // Error checking is done to ensure the child is always
411 // within range ( 0 <= parent <= nhep )
413 int firstchild = first_child(index);
414 int child = byte_num_to_int( (2+4*max_number_entries()+2*(index-1)+1)
416 return ( child > firstchild && child <= number_entries() )
417 ? child : firstchild;
420 inline int HEPEVT_Wrapper::number_children( int index )
422 int firstchild = first_child(index);
423 return ( firstchild>0 ) ?
424 ( 1+last_child(index)-firstchild ) : 0;
427 inline double HEPEVT_Wrapper::px( int index )
429 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
430 + (5*(index-1)+0) *sizeof_real() );
433 inline double HEPEVT_Wrapper::py( int index )
435 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
436 + (5*(index-1)+1) *sizeof_real() );
440 inline double HEPEVT_Wrapper::pz( int index )
442 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
443 + (5*(index-1)+2) *sizeof_real() );
446 inline double HEPEVT_Wrapper::e( int index )
448 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
449 + (5*(index-1)+3) *sizeof_real() );
452 inline double HEPEVT_Wrapper::m( int index )
454 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
455 + (5*(index-1)+4) *sizeof_real() );
458 inline double HEPEVT_Wrapper::x( int index )
460 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
461 + ( 5*max_number_entries()
462 + (4*(index-1)+0) ) *sizeof_real() );
465 inline double HEPEVT_Wrapper::y( int index )
467 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
468 + ( 5*max_number_entries()
469 + (4*(index-1)+1) ) *sizeof_real() );
472 inline double HEPEVT_Wrapper::z( int index )
474 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
475 + ( 5*max_number_entries()
476 + (4*(index-1)+2) ) *sizeof_real() );
479 inline double HEPEVT_Wrapper::t( int index )
481 return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
482 + ( 5*max_number_entries()
483 + (4*(index-1)+3) ) *sizeof_real() );
486 inline void HEPEVT_Wrapper::set_event_number( int evtno )
487 { write_byte_num( evtno, 0 ); }
489 inline void HEPEVT_Wrapper::set_number_entries( int noentries )
490 { write_byte_num( noentries, 1*sizeof_int() ); }
492 inline void HEPEVT_Wrapper::set_status( int index, int status )
494 if ( index <= 0 || index > max_number_entries() ) return;
495 write_byte_num( status, (2+index-1) * sizeof_int() );
498 inline void HEPEVT_Wrapper::set_id( int index, int id )
500 if ( index <= 0 || index > max_number_entries() ) return;
501 write_byte_num( id, (2+max_number_entries()+index-1) *sizeof_int() );
504 inline void HEPEVT_Wrapper::set_parents( int index, int firstparent,
507 if ( index <= 0 || index > max_number_entries() ) return;
508 write_byte_num( firstparent, (2+2*max_number_entries()+2*(index-1))
510 write_byte_num( lastparent, (2+2*max_number_entries()+2*(index-1)+1)
514 inline void HEPEVT_Wrapper::set_children( int index, int firstchild,
517 if ( index <= 0 || index > max_number_entries() ) return;
518 write_byte_num( firstchild, (2+4*max_number_entries()+2*(index-1))
520 write_byte_num( lastchild, (2+4*max_number_entries()+2*(index-1)+1)
524 inline void HEPEVT_Wrapper::set_momentum( int index, double px,
525 double py, double pz, double e )
527 if ( index <= 0 || index > max_number_entries() ) return;
528 write_byte_num( px, (2+6*max_number_entries()) *sizeof_int()
529 + (5*(index-1)+0) *sizeof_real() );
530 write_byte_num( py, (2+6*max_number_entries())*sizeof_int()
531 + (5*(index-1)+1) *sizeof_real() );
532 write_byte_num( pz, (2+6*max_number_entries())*sizeof_int()
533 + (5*(index-1)+2) *sizeof_real() );
534 write_byte_num( e, (2+6*max_number_entries())*sizeof_int()
535 + (5*(index-1)+3) *sizeof_real() );
538 inline void HEPEVT_Wrapper::set_mass( int index, double mass )
540 if ( index <= 0 || index > max_number_entries() ) return;
541 write_byte_num( mass, (2+6*max_number_entries())*sizeof_int()
542 + (5*(index-1)+4) *sizeof_real() );
545 inline void HEPEVT_Wrapper::set_position( int index, double x, double y,
548 if ( index <= 0 || index > max_number_entries() ) return;
549 write_byte_num( x, (2+6*max_number_entries())*sizeof_int()
550 + ( 5*max_number_entries()
551 + (4*(index-1)+0) ) *sizeof_real() );
552 write_byte_num( y, (2+6*max_number_entries())*sizeof_int()
553 + ( 5*max_number_entries()
554 + (4*(index-1)+1) ) *sizeof_real() );
555 write_byte_num( z, (2+6*max_number_entries())*sizeof_int()
556 + ( 5*max_number_entries()
557 + (4*(index-1)+2) ) *sizeof_real() );
558 write_byte_num( t, (2+6*max_number_entries())*sizeof_int()
559 + ( 5*max_number_entries()
560 + (4*(index-1)+3) ) *sizeof_real() );
565 #endif // HEPMC_HEPEVT_WRAPPER_H
566 //--------------------------------------------------------------------------