CMS 3D CMS Logo

EPOS_Wrapper.h
Go to the documentation of this file.
1 //--------------------------------------------------------------------------
2 //THIS IS A BRUTAL COPY OF HEPEVT_Wrapper from HEPMC
3 //We need it because the EPOS generator needs a largeer version of HEPEVT to store the event
4 #ifndef EPOS_EntriesAllocation
5 #define EPOS_EntriesAllocation 99900
6 #endif // EPOS_EntriesAllocation
7 
8 //--------------------------------------------------------------------------
9 #ifndef HEPMC_EPOS_COMMON_H
10 #define HEPMC_EPOS_COMMON_H
11 //
13 // PARAMETER (NMXHEP=2000)
14 // COMMON/HEPCOM/NEVHEP,NHEP,ISTHEP(NMXHEP),IDHEP(NMXHEP),
15 // & JMOHEP(2,NMXHEP),JDAHEP(2,NMXHEP),PHEP(5,NMXHEP),VHEP(4,NMXHEP)
16 /**********************************************************/
17 /* D E S C R I P T I O N : */
18 /*--------------------------------------------------------*/
19 /* NEVHEP - event number (or some special meaning*/
20 /* (see documentation for details) */
21 /* NHEP - actual number of entries in current */
22 /* event. */
23 /* ISTHEP[IHEP] - status code for IHEP'th entry - see */
24 /* documentation for details */
25 /* IDHEP [IHEP] - IHEP'th particle identifier according*/
26 /* to PDG. */
27 /* JMOHEP[IHEP][0] - pointer to position of 1st mother */
28 /* JMOHEP[IHEP][1] - pointer to position of 2nd mother */
29 /* JDAHEP[IHEP][0] - pointer to position of 1st daughter */
30 /* JDAHEP[IHEP][1] - pointer to position of 2nd daughter */
31 /* PHEP [IHEP][0] - X momentum */
32 /* PHEP [IHEP][1] - Y momentum */
33 /* PHEP [IHEP][2] - Z momentum */
34 /* PHEP [IHEP][3] - Energy */
35 /* PHEP [IHEP][4] - Mass */
36 /* VHEP [IHEP][0] - X vertex */
37 /* VHEP [IHEP][1] - Y vertex */
38 /* VHEP [IHEP][2] - Z vertex */
39 /* VHEP [IHEP][3] - production time */
40 /*========================================================*/
41 // Remember, array(1) is the first entry in a fortran array, array[0] is the
42 // first entry in a C array.
43 //
44 // This interface to EPOS common block treats the block as
45 // an array of bytes --- the precision and number of entries
46 // is determined "on the fly" by the wrapper and used to decode
47 // each entry.
48 //
49 // EPOS_EntriesAllocation is the maximum size of the EPOS common block
50 // that can be interfaced.
51 // It is NOT the actual size of the EPOS common used in each
52 // individual application. The actual size can be changed on
53 // the fly using EPOS_Wrapper::set_max_number_entries().
54 // Thus EPOS_EntriesAllocation should typically be set
55 // to the maximum possible number of entries --- 10000 is a good choice
56 // (and is the number used by ATLAS versions of Pythia).
57 //
58 // Note: a statement like *( (int*)&hepcom.data[0] )
59 // takes the memory address of the first byte in EPOS,
60 // interprets it as an integer pointer,
61 // and dereferences the pointer.
62 // i.e. it returns an integer corresponding to nevhep
63 //
64 
65 #include <ctype.h>
66 
67  const unsigned int epos_bytes_allocation =
68  sizeof(long int) * ( 2 + 6 * EPOS_EntriesAllocation )
69  + sizeof(double) * ( 9 * EPOS_EntriesAllocation );
70 
71 
72 #ifdef _WIN32 // Platform: Windows MS Visual C++
73 struct HEPCOM_DEF{
75  };
76 extern "C" HEPCOM_DEF HEPCOM;
77 #define hepcom HEPCOM
78 
79 #else
80 extern "C" {
81  extern struct {
83  } hepcom_;
84 }
85 #define hepcom hepcom_
86 
87 #endif // Platform
88 
89 #endif // HEPMC_EPOS_COMMON_H
90 
91 //--------------------------------------------------------------------------
92 #ifndef HEPMC_EPOS_WRAPPER_H
93 #define HEPMC_EPOS_WRAPPER_H
94 
96 // Matt.Dobbs@Cern.CH, April 24, 2000, refer to:
97 // M. Dobbs and J.B. Hansen, "The HepMC C++ Monte Carlo Event Record for
98 // High Energy Physics", Computer Physics Communications (to be published).
99 //
100 // Generic Wrapper for the fortran EPOS common block
101 // This class is intended for static use only - it makes no sense to
102 // instantiate it.
103 // Updated: June 30, 2000 (static initialization moved to separate .cxx file)
105 //
106 // The index refers to the fortran style index:
107 // i.e. index=1 refers to the first entry in the EPOS common block.
108 // all indices must be >0
109 // number_entries --> integer between 0 and max_number_entries() giving total
110 // number of sequential particle indices
111 // first_parent/child --> index of first mother/child if there is one,
112 // zero otherwise
113 // last_parent/child --> if number children is >1, address of last parent/child
114 // if number of children is 1, same as first_parent/child
115 // if there are no children, returns zero.
116 // is_double_precision --> T or F depending if floating point variables
117 // are 8 or 4 bytes
118 //
119 
120 #include <iostream>
121 #include <cstdio> // needed for formatted output using sprintf
122 
123 namespace EPOS {
124 
126 
131  class EPOS_Wrapper {
132  public:
133 
135  static void print_hepcom( std::ostream& ostr = std::cout );
137  static void print_hepcom_particle( int index,
138  std::ostream& ostr = std::cout );
140  static void zero_everything();
141 
143  // Access Methods //
145  static int event_number();
146  static int number_entries();
147  static int status( int index );
148  static int id( int index );
149  static int first_parent( int index );
150  static int last_parent( int index );
151  static int number_parents( int index );
152  static int first_child( int index );
153  static int last_child( int index );
154  static int number_children( int index );
155  static double px( int index );
156  static double py( int index );
157  static double pz( int index );
158  static double e( int index );
159  static double m( int index );
160  static double x( int index );
161  static double y( int index );
162  static double z( int index );
163  static double t( int index );
164 
166  // Set Methods //
168 
170  static void set_event_number( int evtno );
172  static void set_number_entries( int noentries );
174  static void set_status( int index, int status );
176  static void set_id( int index, int id );
178  static void set_parents( int index, int firstparent, int lastparent );
180  static void set_children( int index, int firstchild, int lastchild );
182  static void set_momentum( int index, double px, double py,
183  double pz, double e );
185  static void set_mass( int index, double mass );
187  static void set_position( int index, double x, double y, double z,
188  double t );
190  // EPOS Floorplan //
192  static unsigned int sizeof_int();
193  static unsigned int sizeof_real();
194  static int max_number_entries();
195  static void set_sizeof_int(unsigned int);
196  static void set_sizeof_real(unsigned int);
197  static void set_max_number_entries(unsigned int);
198 
199  protected:
201  static double byte_num_to_double( unsigned int );
203  static int byte_num_to_int( unsigned int );
205  static void write_byte_num( double, unsigned int );
207  static void write_byte_num( int, unsigned int );
209  static void print_legend( std::ostream& ostr = std::cout );
210 
211  private:
212  static unsigned int s_sizeof_int;
213  static unsigned int s_sizeof_real;
214  static unsigned int s_max_number_entries;
215 
216  };
217 
219  // EPOS Floorplan Inlines //
221  inline unsigned int EPOS_Wrapper::sizeof_int(){ return s_sizeof_int; }
222 
223  inline unsigned int EPOS_Wrapper::sizeof_real(){ return s_sizeof_real; }
224 
225  inline int EPOS_Wrapper::max_number_entries()
226  { return (int)s_max_number_entries; }
227 
228  inline void EPOS_Wrapper::set_sizeof_int( unsigned int size )
229  {
230  if ( size != sizeof(short int) && size != sizeof(long int) && size != sizeof(int) ) {
231  std::cerr << "HepMC is not able to handle integers "
232  << " of size other than 2 or 4."
233  << " You requested: " << size << std::endl;
234  }
235  s_sizeof_int = size;
236  }
237 
238  inline void EPOS_Wrapper::set_sizeof_real( unsigned int size ) {
239  if ( size != sizeof(float) && size != sizeof(double) ) {
240  std::cerr << "HepMC is not able to handle floating point numbers"
241  << " of size other than 4 or 8."
242  << " You requested: " << size << std::endl;
243  }
244  s_sizeof_real = size;
245  }
246 
247  inline void EPOS_Wrapper::set_max_number_entries( unsigned int size ) {
248  s_max_number_entries = size;
249  }
250 
251  inline double EPOS_Wrapper::byte_num_to_double( unsigned int b ) {
252  if ( b >= epos_bytes_allocation ) std::cerr
253  << "EPOS_Wrapper: requested hepcom data exceeds allocation"
254  << std::endl;
255  if ( s_sizeof_real == sizeof(float) ) {
256  float* myfloat = (float*)&hepcom.data[b];
257  return (double)(*myfloat);
258  } else if ( s_sizeof_real == sizeof(double) ) {
259  double* mydouble = (double*)&hepcom.data[b];
260  return (*mydouble);
261  } else {
262  std::cerr
263  << "EPOS_Wrapper: illegal floating point number length."
264  << s_sizeof_real << std::endl;
265  }
266  return 0;
267  }
268 
269  inline int EPOS_Wrapper::byte_num_to_int( unsigned int b ) {
270  if ( b >= epos_bytes_allocation ) std::cerr
271  << "EPOS_Wrapper: requested hepcom data exceeds allocation"
272  << std::endl;
273  if ( s_sizeof_int == sizeof(short int) ) {
274  short int* myshortint = (short int*)&hepcom.data[b];
275  return (int)(*myshortint);
276  } else if ( s_sizeof_int == sizeof(long int) ) {
277  long int* mylongint = (long int*)&hepcom.data[b];
278  return (*mylongint);
279  // on some 64 bit machines, int, short, and long are all different
280  } else if ( s_sizeof_int == sizeof(int) ) {
281  int* myint = (int*)&hepcom.data[b];
282  return (*myint);
283  } else {
284  std::cerr
285  << "EPOS_Wrapper: illegal integer number length."
286  << s_sizeof_int << std::endl;
287  }
288  return 0;
289  }
290 
291  inline void EPOS_Wrapper::write_byte_num( double in, unsigned int b ) {
292  if ( b >= epos_bytes_allocation ) std::cerr
293  << "EPOS_Wrapper: requested hepcom data exceeds allocation"
294  << std::endl;
295  if ( s_sizeof_real == sizeof(float) ) {
296  float* myfloat = (float*)&hepcom.data[b];
297  (*myfloat) = (float)in;
298  } else if ( s_sizeof_real == sizeof(double) ) {
299  double* mydouble = (double*)&hepcom.data[b];
300  (*mydouble) = (double)in;
301  } else {
302  std::cerr
303  << "EPOS_Wrapper: illegal floating point number length."
304  << s_sizeof_real << std::endl;
305  }
306  }
307 
308  inline void EPOS_Wrapper::write_byte_num( int in, unsigned int b ) {
309  if ( b >= epos_bytes_allocation ) std::cerr
310  << "EPOS_Wrapper: requested hepcom data exceeds allocation"
311  << std::endl;
312  if ( s_sizeof_int == sizeof(short int) ) {
313  short int* myshortint = (short int*)&hepcom.data[b];
314  (*myshortint) = (short int)in;
315  } else if ( s_sizeof_int == sizeof(long int) ) {
316  long int* mylongint = (long int*)&hepcom.data[b];
317  (*mylongint) = (int)in;
318  // on some 64 bit machines, int, short, and long are all different
319  } else if ( s_sizeof_int == sizeof(int) ) {
320  int* myint = (int*)&hepcom.data[b];
321  (*myint) = (int)in;
322  } else {
323  std::cerr
324  << "EPOS_Wrapper: illegal integer number length."
325  << s_sizeof_int << std::endl;
326  }
327  }
328 
330  // INLINES //
332 
333  inline int EPOS_Wrapper::event_number()
334  { return byte_num_to_int(0); }
335 
336  inline int EPOS_Wrapper::number_entries()
337  {
338  int nhep = byte_num_to_int( 1*sizeof_int() );
339  return ( nhep <= max_number_entries() ?
340  nhep : max_number_entries() );
341  }
342 
343  inline int EPOS_Wrapper::status( int index )
344  { return byte_num_to_int( (2+index-1) * sizeof_int() ); }
345 
346  inline int EPOS_Wrapper::id( int index )
347  {
348  return byte_num_to_int( (2+max_number_entries()+index-1)
349  * sizeof_int() );
350  }
351 
352  inline int EPOS_Wrapper::first_parent( int index )
353  {
354  int parent = byte_num_to_int( (2+2*max_number_entries()+2*(index-1))
355  * sizeof_int() );
356  return ( parent > 0 && parent <= number_entries() ) ?
357  parent : 0;
358  }
359 
360  inline int EPOS_Wrapper::last_parent( int index )
361  {
362  // Returns the Index of the LAST parent in the EPOS record
363  // for particle with Index index.
364  // If there is only one parent, the last parent is forced to
365  // be the same as the first parent.
366  // If there are no parents for this particle, both the first_parent
367  // and the last_parent with return 0.
368  // Error checking is done to ensure the parent is always
369  // within range ( 0 <= parent <= nhep )
370  //
371  int firstparent = first_parent(index);
372  int parent = byte_num_to_int( (2+2*max_number_entries()+2*(index-1)+1)
373  * sizeof_int() );
374  return ( parent > firstparent && parent <= number_entries() )
375  ? parent : firstparent;
376  }
377 
378  inline int EPOS_Wrapper::number_parents( int index ) {
379  int firstparent = first_parent(index);
380  return ( firstparent>0 ) ?
381  ( 1+last_parent(index)-firstparent ) : 0;
382  }
383 
384  inline int EPOS_Wrapper::first_child( int index )
385  {
386  int child = byte_num_to_int( (2+4*max_number_entries()+2*(index-1))
387  * sizeof_int() );
388  return ( child > 0 && child <= number_entries() ) ?
389  child : 0;
390  }
391 
392  inline int EPOS_Wrapper::last_child( int index )
393  {
394  // Returns the Index of the LAST child in the EPOS record
395  // for particle with Index index.
396  // If there is only one child, the last child is forced to
397  // be the same as the first child.
398  // If there are no children for this particle, both the first_child
399  // and the last_child with return 0.
400  // Error checking is done to ensure the child is always
401  // within range ( 0 <= parent <= nhep )
402  //
403  int firstchild = first_child(index);
404  int child = byte_num_to_int( (2+4*max_number_entries()+2*(index-1)+1)
405  * sizeof_int() );
406  return ( child > firstchild && child <= number_entries() )
407  ? child : firstchild;
408  }
409 
410  inline int EPOS_Wrapper::number_children( int index )
411  {
412  int firstchild = first_child(index);
413  return ( firstchild>0 ) ?
414  ( 1+last_child(index)-firstchild ) : 0;
415  }
416 
417  inline double EPOS_Wrapper::px( int index )
418  {
419  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
420  + (5*(index-1)+0) *sizeof_real() );
421  }
422 
423  inline double EPOS_Wrapper::py( int index )
424  {
425  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
426  + (5*(index-1)+1) *sizeof_real() );
427  }
428 
429 
430  inline double EPOS_Wrapper::pz( int index )
431  {
432  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
433  + (5*(index-1)+2) *sizeof_real() );
434  }
435 
436  inline double EPOS_Wrapper::e( int index )
437  {
438  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
439  + (5*(index-1)+3) *sizeof_real() );
440  }
441 
442  inline double EPOS_Wrapper::m( int index )
443  {
444  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
445  + (5*(index-1)+4) *sizeof_real() );
446  }
447 
448  inline double EPOS_Wrapper::x( int index )
449  {
450  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
451  + ( 5*max_number_entries()
452  + (4*(index-1)+0) ) *sizeof_real() );
453  }
454 
455  inline double EPOS_Wrapper::y( int index )
456  {
457  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
458  + ( 5*max_number_entries()
459  + (4*(index-1)+1) ) *sizeof_real() );
460  }
461 
462  inline double EPOS_Wrapper::z( int index )
463  {
464  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
465  + ( 5*max_number_entries()
466  + (4*(index-1)+2) ) *sizeof_real() );
467  }
468 
469  inline double EPOS_Wrapper::t( int index )
470  {
471  return byte_num_to_double( (2+6*max_number_entries())*sizeof_int()
472  + ( 5*max_number_entries()
473  + (4*(index-1)+3) ) *sizeof_real() );
474  }
475 
476  inline void EPOS_Wrapper::set_event_number( int evtno )
477  { write_byte_num( evtno, 0 ); }
478 
479  inline void EPOS_Wrapper::set_number_entries( int noentries )
480  { write_byte_num( noentries, 1*sizeof_int() ); }
481 
482  inline void EPOS_Wrapper::set_status( int index, int status )
483  {
484  if ( index <= 0 || index > max_number_entries() ) return;
485  write_byte_num( status, (2+index-1) * sizeof_int() );
486  }
487 
488  inline void EPOS_Wrapper::set_id( int index, int id )
489  {
490  if ( index <= 0 || index > max_number_entries() ) return;
491  write_byte_num( id, (2+max_number_entries()+index-1) *sizeof_int() );
492  }
493 
494  inline void EPOS_Wrapper::set_parents( int index, int firstparent,
495  int lastparent )
496  {
497  if ( index <= 0 || index > max_number_entries() ) return;
498  write_byte_num( firstparent, (2+2*max_number_entries()+2*(index-1))
499  *sizeof_int() );
500  write_byte_num( lastparent, (2+2*max_number_entries()+2*(index-1)+1)
501  * sizeof_int() );
502  }
503 
504  inline void EPOS_Wrapper::set_children( int index, int firstchild,
505  int lastchild )
506  {
507  if ( index <= 0 || index > max_number_entries() ) return;
508  write_byte_num( firstchild, (2+4*max_number_entries()+2*(index-1))
509  *sizeof_int() );
510  write_byte_num( lastchild, (2+4*max_number_entries()+2*(index-1)+1)
511  *sizeof_int() );
512  }
513 
514  inline void EPOS_Wrapper::set_momentum( int index, double px,
515  double py, double pz, double e )
516  {
517  if ( index <= 0 || index > max_number_entries() ) return;
518  write_byte_num( px, (2+6*max_number_entries()) *sizeof_int()
519  + (5*(index-1)+0) *sizeof_real() );
520  write_byte_num( py, (2+6*max_number_entries())*sizeof_int()
521  + (5*(index-1)+1) *sizeof_real() );
522  write_byte_num( pz, (2+6*max_number_entries())*sizeof_int()
523  + (5*(index-1)+2) *sizeof_real() );
524  write_byte_num( e, (2+6*max_number_entries())*sizeof_int()
525  + (5*(index-1)+3) *sizeof_real() );
526  }
527 
528  inline void EPOS_Wrapper::set_mass( int index, double mass )
529  {
530  if ( index <= 0 || index > max_number_entries() ) return;
531  write_byte_num( mass, (2+6*max_number_entries())*sizeof_int()
532  + (5*(index-1)+4) *sizeof_real() );
533  }
534 
535  inline void EPOS_Wrapper::set_position( int index, double x, double y,
536  double z, double t )
537  {
538  if ( index <= 0 || index > max_number_entries() ) return;
539  write_byte_num( x, (2+6*max_number_entries())*sizeof_int()
540  + ( 5*max_number_entries()
541  + (4*(index-1)+0) ) *sizeof_real() );
542  write_byte_num( y, (2+6*max_number_entries())*sizeof_int()
543  + ( 5*max_number_entries()
544  + (4*(index-1)+1) ) *sizeof_real() );
545  write_byte_num( z, (2+6*max_number_entries())*sizeof_int()
546  + ( 5*max_number_entries()
547  + (4*(index-1)+2) ) *sizeof_real() );
548  write_byte_num( t, (2+6*max_number_entries())*sizeof_int()
549  + ( 5*max_number_entries()
550  + (4*(index-1)+3) ) *sizeof_real() );
551  }
552 
553  inline void EPOS_Wrapper::zero_everything()
554  {
555  set_event_number( 0 );
556  set_number_entries( 0 );
557  for ( int i = 1; i<=max_number_entries(); ++i ) {
558  set_status( i, 0 );
559  set_id( i, 0 );
560  set_parents( i, 0, 0 );
561  set_children( i, 0, 0 );
562  set_momentum( i, 0, 0, 0, 0 );
563  set_mass( i, 0 );
564  set_position( i, 0, 0, 0, 0 );
565  }
566  }
567 
568  inline void EPOS_Wrapper::print_hepcom( std::ostream& ostr )
569  {
570  ostr << "________________________________________"
571  << "________________________________________" << std::endl;
572  ostr << "***** HEPEVT Common Event#: "
573  << event_number()
574  << ", " << number_entries() << " particles (max "
575  << max_number_entries() << ") *****";
576  ostr << sizeof_int() << "-byte integers, "
577  << sizeof_real() << "-byte floating point numbers, "
578  << max_number_entries() << "-allocated entries."
579  << std::endl;
580  print_legend(ostr);
581  ostr << "________________________________________"
582  << "________________________________________" << std::endl;
583  for ( int i=1; i <= number_entries(); ++i ) {
584  print_hepcom_particle( i, ostr );
585  }
586  ostr << "________________________________________"
587  << "________________________________________" << std::endl;
588  }
589 
590  inline void EPOS_Wrapper::print_hepcom_particle( int i, std::ostream& ostr )
591  {
592  char outline[81];
593  sprintf( outline,
594  "%4d %+4d %4d %4d (%9.3g, %9.3g, %9.3g, %9.3g, %9.3g)"
595  ,i, status(i), first_parent(i), first_child(i),
596  px(i), py(i), pz(i), e(i), m(i) );
597  ostr << outline << "\n";
598  sprintf( outline,"%+9d %4d %4d (%9.3g, %9.3g, %9.3g, %9.3g)",
599  // old version was:" (%+9.2e, %+9.2e, %+9.2e, %+9.2e)"
600  id(i), last_parent(i), last_child(i),
601  x(i), y(i), z(i), t(i) );
602  ostr << outline << std::endl;
603  }
604 
605  inline void EPOS_Wrapper::print_legend( std::ostream& ostr )
606  {
607  char outline[81];
608  sprintf( outline,"%4s %4s %4s %5s %10s, %9s, %9s, %9s, %10s",
609  "Indx","Stat","Par-","chil-",
610  "( P_x","P_y","P_z","Energy","M ) ");
611  ostr << outline << std::endl;
612  sprintf( outline,"%9s %4s %4s %10s, %9s, %9s, %9s) %9s",
613  "ID ","ents","dren",
614  "Prod ( X","Y","Z","cT", "[mm]");
615  ostr << outline << std::endl;
616  }
617 
618 } // HepMC
619 
620 #endif // HEPMC_EPOS_WRAPPER_H
621 //--------------------------------------------------------------------------
622 
size
Write out results.
int i
Definition: DBlmapReader.cc:9
#define EPOS_EntriesAllocation
Definition: EPOS_Wrapper.h:5
float float float z
T x() const
Cartesian x coordinate.
#define hepcom
Definition: EPOS_Wrapper.h:85
Generic Wrapper for the fortran EPOS common block.
Definition: EPOS_Wrapper.h:131
static unsigned int s_max_number_entries
Definition: EPOS_Wrapper.h:214
double b
Definition: hdecay.h:120
static unsigned int s_sizeof_real
Definition: EPOS_Wrapper.h:213
return(e1-e2)*(e1-e2)+dp *dp
const unsigned int epos_bytes_allocation
Definition: EPOS_Wrapper.h:67
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
struct @591 hepcom_
static unsigned int s_sizeof_int
Definition: EPOS_Wrapper.h:212