LCIO  02.17
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
HEPEVT.cc
Go to the documentation of this file.
1 
2 #include "CPPFORT/HEPEVT.h"
3 
4 #include <cstdlib>
5 #include <cmath>
6 #include <iostream>
7 using namespace std ;
8 
9 using namespace lcio ;
10 
11 namespace HEPEVTIMPL{
12 
13  void HEPEVT::fromHepEvt(LCEvent * evt, const char* mcpColName){
14 
15  float* p = new float[3] ;
16 
17  // create and add mc particles from stdhep COMMON
18  LCCollectionVec* mcVec = new LCCollectionVec( LCIO::MCPARTICLE ) ;
19 
20  // create a particle instance for each HEPEVT entry
21  // and add it to the collection - leave parent relations empty
22  int* NMCPART = &FTNhep.nhep;
23  for(int j=0;j < *NMCPART;j++)
24  {
25  MCParticleImpl* mcp = new MCParticleImpl ;
26  mcp->setPDG( FTNhep.idhep[j] ) ;
27  mcp->setGeneratorStatus( FTNhep.isthep[j] ) ;
28  mcp->setSimulatorStatus( 0 ) ;
29 
30  // now momentum, vertex, charge
31  for(int k=0;k<3;k++) p[k] = (float)FTNhep.phep[j][k];
32  mcp->setMomentum( p );
33  mcp->setMass( (float)FTNhep.phep[j][4] ) ;
34  mcp->setVertex( FTNhep.vhep[j] ) ;
35 
36  // finally store pointer and set charge
37  mcp->setCharge( FTNhep1.mcchargev[j] ) ;
38 
39  mcVec->push_back( mcp ) ;
40  }
41 
42  // now loop one more time and add parent relations
43  // NB: this assumes that the parent relations are consistent, i.e. any particle
44  // referred to as daughter in the hepevt common block refers to the corresponding
45  // particle as mother
46  for(int j=0;j < *NMCPART;j++) {
47  MCParticleImpl* mcp = dynamic_cast<MCParticleImpl*>( mcVec->getElementAt( j ) ) ;
48 
49  // now get parents if required and set daughter if parent1 is defined
50  int parent1 = FTNhep.jmohep[j][0] ;
51  int parent2 = FTNhep.jmohep[j][1] ;
52 
53  if( parent1 > 0 ) {
54  MCParticle* mom = dynamic_cast<MCParticle*>( mcVec->getElementAt( parent1-1 ) ) ;
55  mcp->addParent( mom ) ;
56  if( parent2 > 0 )
57  for(int i = parent1 ; i < parent2 ; i++ ){
58  MCParticle* mom2 = dynamic_cast<MCParticle*>( mcVec->getElementAt( i ) ) ;
59  mcp->addParent( mom2 ) ;
60  }
61  }
62  }
63 
64  std::string colName("MCParticle") ;
65  if( mcpColName != 0 )
66  colName = mcpColName ;
67 
68  // add all collection to the event
69  evt->addCollection( (LCCollection*) mcVec , colName ) ;
70 
71  // now fill pointers for MCParticle collection
72  LCEventImpl* evtimpl = reinterpret_cast<LCEventImpl*>(evt) ;
73  LCCollection* getmcVec = evtimpl->getCollection( "MCParticle" ) ;
74  int nelem = getmcVec->getNumberOfElements() ;
75  for(int j=0;j < nelem; j++)
76  {
77  FTNhep1.mcpointerv[j] = reinterpret_cast<PTRTYPE>( getmcVec->getElementAt( j ) ) ;
78  }
79 
80  delete[] p ;
81  } // end of fromHepEvt
82 
83 /* ============================================================================================================= */
84 
85  void HEPEVT::toHepEvt(const LCEvent* evt, const char* mcpColName){
86 
87  int* kmax = new int ;
88  double* maxxyz = new double;
89 
90 
91  // set event number in stdhep COMMON
92  FTNhep.nevhep = evt->getEventNumber() ;
93 
94  std::string colName("MCParticle") ;
95  if( mcpColName != 0 )
96  colName = mcpColName ;
97 
98  // fill MCParticles into stdhep COMMON
99  LCCollection* mcVec = evt->getCollection( colName ) ;
100  FTNhep.nhep = mcVec->getNumberOfElements() ;
101  int* NMCPART = &FTNhep.nhep ;
102 
103  for(int j=0;j < *NMCPART;j++)
104  {
105 
106  //for each MCParticle fill hepevt common line
107  const MCParticle* mcp =
108  dynamic_cast<const MCParticle*>( mcVec->getElementAt( j ) ) ;
109 
110  FTNhep.idhep[j] = mcp->getPDG() ;
111  FTNhep.isthep[j] = mcp->getGeneratorStatus() ;
112 
113  // store mother indices
114  FTNhep.jmohep[j][0] = 0 ;
115  FTNhep.jmohep[j][1] = 0 ;
116  const MCParticle* mcpp = 0 ;
117  int nparents = mcp->getParents().size() ;
118  if( nparents > 0 ) mcpp = mcp->getParents()[0] ;
119 
120  try{
121  for(int jjm=0;jjm < *NMCPART;jjm++)
122  {
123  if (mcpp == dynamic_cast<const MCParticle*>(mcVec->getElementAt( jjm )) ){
124  FTNhep.jmohep[j][0] = jjm + 1 ;
125  break ;
126  }
127  }
128  }catch(exception& e){
129  }
130  if ( FTNhep.jmohep[j][0] > 0 )
131  {
132  try{
133  const MCParticle* mcpsp = 0 ;
134  if( mcp->getParents().size() > 1 ) mcpsp = mcp->getParents()[ nparents-1 ] ;
135 
136  for(int jjj=0;jjj < *NMCPART;jjj++)
137  {
138 
139  if (mcpsp == dynamic_cast<const MCParticle*>(mcVec->getElementAt( jjj )) ){
140  FTNhep.jmohep[j][1] = jjj + 1 ;
141  break ;
142  }
143  }
144  }catch(exception& e){
145  FTNhep.jmohep[j][1] = 0 ;
146  }
147  }
148 
149 
150  // store daugther indices
151  FTNhep.jdahep[j][0] = 0 ;
152  FTNhep.jdahep[j][1] = 0 ;
153  // for the StdHep convention particles with GeneratorStatus = 3 have no daughters
154  if ( FTNhep.isthep[j] != 3 )
155  {
156  int ndaugthers = mcp->getDaughters().size() ;
157 
158  if (ndaugthers > 0)
159  {
160  const MCParticle* mcpd = mcp->getDaughters()[0] ;
161  for (int jjj=0; jjj < *NMCPART; jjj++)
162  {
163  const MCParticle* mcpdtest = dynamic_cast<const MCParticle*>(mcVec->getElementAt( jjj )) ;
164  if ( mcpd == mcpdtest )
165  {
166  FTNhep.jdahep[j][0] = jjj + 1 ;
167  FTNhep.jdahep[j][1] = FTNhep.jdahep[j][0] + ndaugthers -1 ;
168  break ;
169  }
170  }
171  }
172  }
173 
174  // now momentum, energy, and mass
175  for(int k=0;k<3;k++) FTNhep.phep[j][k] = (double)mcp->getMomentum()[k] ;
176  FTNhep.phep[j][3] = (double)mcp->getEnergy() ;
177  FTNhep.phep[j][4] = (double)mcp->getMass() ;
178 
179  // get vertex and production time
180  *kmax = 0 ;
181  *maxxyz = 0. ;
182  for(int k=0;k<3;k++){
183  FTNhep.vhep[j][k] = mcp->getVertex()[k] ;
184  if ( fabs( FTNhep.vhep[j][k] ) > *maxxyz ){
185  *maxxyz = fabs( FTNhep.vhep[j][k] ) ;
186  *kmax = k ;
187  }
188  }
189  if ( mcpp != 0 && *maxxyz > 0. )
190  {
191  FTNhep.vhep[j][3] = FTNhep.vhep[FTNhep.jmohep[j][0]-1][3]
192  + (mcp->getVertex()[*kmax] - mcpp->getVertex()[*kmax]) * mcpp->getEnergy()
193  / mcpp->getMomentum()[*kmax] ;
194  }
195  else
196  {
197  FTNhep.vhep[j][3] = 0. ; // no production time for MCParticle
198  }
199 
200  // finally store pointer and get charge
201  FTNhep1.mcpointerv[j] = reinterpret_cast<PTRTYPE>( (mcp) ) ;
202  FTNhep1.mcchargev[j] = mcp->getCharge() ;
203  }
204 
205  delete kmax ;
206  delete maxxyz ;
207  } // end of toHepEvt
208 
209 } //namespace HEPEVTIMPL
210 
STL class.
static const int NMCPART
long PTRTYPE
Fortran interface - define the length of pointers this has to made machine independent ...
Definition: cpointer.h:12
T fabs(T...args)
STL class.
#define FTNhep
Definition: hepevt0.h:64
#define FTNhep1
Definition: hepevt1.h:41