Belle II Software  release-08-01-10
KoralW.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 /* Own header. */
10 #include <generators/koralw/KoralW.h>
11 
12 /* Basf2 headers. */
13 #include <framework/gearbox/Unit.h>
14 #include <framework/logging/Logger.h>
15 
16 /* ROOT headers. */
17 #include <TRandom.h>
18 
19 using namespace std;
20 using namespace Belle2;
21 
22 //Declaration of the KoralW FORTRAN methods and common blocks.
23 extern"C" {
24 
25  extern struct {
26  double q1[4];
27  double q2[4];
28  double p1[4];
29  double p2[4];
30  double p3[4];
31  double p4[4];
32  } momdec_;
33 
34  extern struct {
35  double qeff1[4];
36  double qeff2[4];
37  double sphum[4];
38  double sphot[4][100];
39  int nphot;
40  } momset_;
41 
42  extern struct {
43  int nevhep;
44  int nhep;
45  int isthep[2000];
46  int idhep[2000];
47  int jmohep[2000][2];
48  int jdahep[2000][2];
49  float phep[2000][5];
50  float vhep[2000][4];
51  } hepevt_;
52 
53  void kw_setdatapath_(const char* filename, size_t* length);
54  void kw_readatax_(const char* filename, size_t* length, int* reset, const int* max, double* xpar);
55  void kw_initialize_(double* ecm, double* xpar);
56  void marini_(unsigned int* mar1, unsigned int* mar2, unsigned int* mar3);
57  void rmarin_(unsigned int* mar1, unsigned int* mar2, unsigned int* mar3);
58  void kw_make_();
59  void kw_finalize_();
60  void kw_getmomdec_(double* p1, double* p2, double* p3, double* p4);
61  void kw_getxsecmc_(double* xSecMC, double* xErrMC);
62 
63  void koralw_warning_ecm_(const double* ecmconfig, const double* ecm)
64  {
65  B2WARNING("KORALW: Different center of mass energy in config file (obsolete), E=" << *ecmconfig << ", and from beam parameters, E="
66  << *ecm);
67  }
68 
69 }
70 
71 void KoralW::init(const std::string& dataPath, const std::string& userDataFile)
72 {
73  if (dataPath.empty()) B2FATAL("KoralW: The specified data path is empty !");
74  if (userDataFile.empty()) B2FATAL("KoralW: The specified user data file is empty !");
75 
76  //Make sure the dataPath ends with an "/"
77  string dataPathNew = dataPath;
78  if (dataPath[dataPath.length() - 1] != '/') dataPathNew += "/";
79 
80  //Set the path to the data files
81  size_t pathLength = dataPathNew.size();
82  kw_setdatapath_(dataPathNew.c_str(), &pathLength);
83 
84  //Load the default KoralW input data
85  int reset = 1;
86  const string defaultDataFile = dataPathNew + "KoralW_Default.data";
87  pathLength = defaultDataFile.size();
88  kw_readatax_(defaultDataFile.c_str(), &pathLength, &reset, &m_numXPar, m_XPar);
89 
90  //Load the user KoralW input data
91  reset = 0;
92  pathLength = userDataFile.size();
93  kw_readatax_(userDataFile.c_str(), &pathLength, &reset, &m_numXPar, m_XPar);
94 
95  //Initialize KoralW
96  kw_initialize_(&m_cmsEnergy, m_XPar);
97 
98  //Initialize random number generator
99  unsigned int mar1 = gRandom->Integer(m_seed1); //The range for this seed seems to be [0, 900000000]
100  unsigned int mar2 = gRandom->Integer(m_seed2);
101  unsigned int mar3 = gRandom->Integer(m_seed3);
102  marini_(&mar1, &mar2, &mar3);
103  rmarin_(&mar1, &mar2, &mar3);
104 }
105 
106 
107 void KoralW::generateEvent(MCParticleGraph& mcGraph, ROOT::Math::XYZVector vertex, ROOT::Math::LorentzRotation boost)
108 {
109  kw_make_();
110 
111  //Store the particles with status id 3 and 2 as virtual particles, the particles with status id 1 as real particles
112  for (int iPart = 0; iPart < hepevt_.nhep; ++iPart) {
113  if (hepevt_.isthep[iPart] > 1) {
114  storeParticle(mcGraph, hepevt_.phep[iPart], hepevt_.vhep[iPart], hepevt_.idhep[iPart], vertex, boost, true);
115  } else {
116  storeParticle(mcGraph, hepevt_.phep[iPart], hepevt_.vhep[iPart], hepevt_.idhep[iPart], vertex, boost);
117  }
118  }
119 }
120 
121 
122 void KoralW::term()
123 {
124  kw_finalize_();
125 
126  //Get the cross section
127  kw_getxsecmc_(&m_crossSection, &m_crossSectionError);
128 }
129 
130 //=========================================================================
131 // Protected methods
132 //=========================================================================
133 
134 void KoralW::storeParticle(MCParticleGraph& mcGraph, const float* mom, const float* vtx, int pdg, ROOT::Math::XYZVector vertex,
135  ROOT::Math::LorentzRotation boost, bool isVirtual, bool isInitial)
136 {
137  // //Create particle
138  //MCParticleGraph::GraphParticle& part = mcGraph.addParticle();
139  //if (!isVirtual) {
140  // part.setStatus(MCParticle::c_PrimaryParticle);
141  //} else {
142  // part.setStatus(MCParticle::c_IsVirtual);
143  //}
144 
145  // RG 6/25/14 Add new flag for ISR "c_Initial"
147  if (isVirtual) {
148  part.setStatus(MCParticle::c_IsVirtual);
149  } else if (isInitial) {
150  part.setStatus(MCParticle::c_Initial);
151  }
152 
153  //every particle from a generator is primary, TF
154  part.addStatus(MCParticle::c_PrimaryParticle);
155  part.setPDG(pdg);
156  part.setFirstDaughter(0);
157  part.setLastDaughter(0);
158  part.setMomentum(ROOT::Math::XYZVector(mom[0], mom[1], mom[2]));
159  part.setEnergy(mom[3]);
160  part.setMass(mom[4]);
161  part.setProductionVertex(vtx[0]*Unit::mm, vtx[1]*Unit::mm, vtx[2]*Unit::mm);
162 
163  //boost, TF
164  ROOT::Math::PxPyPzEVector p4 = part.get4Vector();
165  p4 = boost * p4;
166  part.set4Vector(p4);
167 
168  //set vertex, TF
169  if (!isInitial) {
170  ROOT::Math::XYZVector v3 = part.getProductionVertex();
171  v3 = v3 + vertex;
172  part.setProductionVertex(v3);
173  part.setValidVertex(true);
174  }
175 }
Class to represent Particle data in graph.
Class to build, validate and sort a particle decay chain.
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.
int isthep[nmxhep]
status code.
double vhep[nmxhep][4]
vertex [mm].
double phep[nmxhep][5]
four-momentum, mass [GeV].
int idhep[nmxhep]
particle ident KF.
int nhep
number of particles.