evtgen is hosted by Hepforge, IPPP Durham
EvtGen  2.0.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
EvtGen.cpp
Go to the documentation of this file.
1 
2 /***********************************************************************
3 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors *
4 * *
5 * This file is part of EvtGen. *
6 * *
7 * EvtGen is free software: you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation, either version 3 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * EvtGen is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with EvtGen. If not, see <https://www.gnu.org/licenses/>. *
19 ***********************************************************************/
20 
21 #include "EvtGen/EvtGen.hh"
22 
24 #include "EvtGenBase/EvtCPUtil.hh"
27 #include "EvtGenBase/EvtPDL.hh"
30 #include "EvtGenBase/EvtPatches.hh"
31 #include "EvtGenBase/EvtRadCorr.hh"
32 #include "EvtGenBase/EvtRandom.hh"
34 #include "EvtGenBase/EvtReport.hh"
36 #include "EvtGenBase/EvtStatus.hh"
38 
41 
42 #include <cstdlib>
43 #include <fstream>
44 #include <string>
45 
46 using std::endl;
47 using std::fstream;
48 using std::ifstream;
49 
51 {
52  //This is a bit ugly, should not do anything
53  //in a destructor. This will fail if EvtGen is made a static
54  //because then this destructor might be called _after_
55  //the destruction of objects that it depends on, e.g., EvtPDL.
56 
57  if ( getenv( "EVTINFO" ) ) {
59  }
60 }
61 
62 EvtGen::EvtGen( const std::string& decayName, const std::string& pdtTableName,
63  EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
64  const std::list<EvtDecayBase*>* extraModels, int mixingType,
65  bool useXml )
66 {
67  std::ifstream pdtIn( pdtTableName );
68  if ( !pdtIn ) {
69  EvtGenReport( EVTGEN_ERROR, "EvtGen" )
70  << "Could not open:" << pdtTableName << "EvtPDL" << endl;
71  return;
72  }
73  initialize( decayName, pdtIn, randomEngine, isrEngine, extraModels,
74  mixingType, useXml );
75  pdtIn.close();
76 }
77 
78 EvtGen::EvtGen( const std::string& decayName, std::istream& pdtTableData,
79  EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
80  const std::list<EvtDecayBase*>* extraModels, int mixingType,
81  bool useXml )
82 {
83  initialize( decayName, pdtTableData, randomEngine, isrEngine, extraModels,
84  mixingType, useXml );
85 }
86 
87 void EvtGen::initialize( const std::string& decayName, std::istream& pdtTable,
88  EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
89  const std::list<EvtDecayBase*>* extraModels,
90  int mixingType, bool useXml )
91 {
92  EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Initializing EvtGen" << endl;
93 
94  if ( randomEngine == 0 ) {
95  static EvtSimpleRandomEngine defaultRandomEngine;
96  EvtRandom::setRandomEngine( &defaultRandomEngine );
97  EvtGenReport( EVTGEN_INFO, "EvtGen" )
98  << "No random engine given in "
99  << "EvtGen::EvtGen constructor, "
100  << "will use default EvtSimpleRandomEngine." << endl;
101  } else {
102  EvtRandom::setRandomEngine( randomEngine );
103  }
104 
105  EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Storing known decay models" << endl;
106  EvtModelReg dummy( extraModels );
107 
108  EvtGenReport( EVTGEN_INFO, "EvtGen" )
109  << "Main decay file name :" << decayName << endl;
110 
111  _pdl.readPDT( pdtTable );
112 
113  if ( useXml ) {
114  EvtDecayTable::getInstance()->readXMLDecayFile( decayName, false );
115  } else {
116  EvtDecayTable::getInstance()->readDecayFile( decayName, false );
117  }
118 
119  _mixingType = mixingType;
120  EvtGenReport( EVTGEN_INFO, "EvtGen" )
121  << "Mixing type integer set to " << _mixingType << endl;
123 
124  // Set the radiative correction engine
125 
126  if ( isrEngine != 0 ) {
127  EvtRadCorr::setRadCorrEngine( isrEngine );
128 
129  } else {
130  // Owing to the pure abstract interface, we still need to define a concrete
131  // implementation of a radiative correction engine. Use one which does nothing.
132  EvtAbsRadCorr* noRadCorr = new EvtNoRadCorr();
133  EvtRadCorr::setRadCorrEngine( noRadCorr );
134  }
135 
136  EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Done initializing EvtGen" << endl;
137 }
138 
139 void EvtGen::readUDecay( const std::string& uDecayName, bool useXml )
140 {
141  ifstream indec;
142 
143  if ( uDecayName.size() == 0 ) {
144  EvtGenReport( EVTGEN_INFO, "EvtGen" )
145  << "Is not reading a user decay file!" << endl;
146  } else {
147  indec.open( uDecayName );
148  if ( indec ) {
149  if ( useXml ) {
150  EvtDecayTable::getInstance()->readXMLDecayFile( uDecayName, true );
151  } else {
152  EvtDecayTable::getInstance()->readDecayFile( uDecayName, true );
153  }
154  } else {
155  EvtGenReport( EVTGEN_INFO, "EvtGen" )
156  << "Can not find UDECAY file '" << uDecayName << "'. Stopping"
157  << endl;
158  ::abort();
159  }
160  }
161 }
162 
164  EvtVector4R translation,
165  EvtSpinDensity* spinDensity )
166 {
167  EvtParticle* theParticle( 0 );
168 
169  if ( spinDensity == 0 ) {
171  EvtPDL::evtIdFromStdHep( PDGId ), refFrameP4 );
172  } else {
174  EvtPDL::evtIdFromStdHep( PDGId ), refFrameP4, *spinDensity );
175  }
176 
177  generateDecay( theParticle );
178  EvtHepMCEvent* hepMCEvent = new EvtHepMCEvent();
179  hepMCEvent->constructEvent( theParticle, translation );
180 
181  theParticle->deleteTree();
182 
183  return hepMCEvent;
184 }
185 
187 {
188  int times = 0;
189  do {
190  times += 1;
192 
193  p->decay();
194  //ok then finish.
195  if ( EvtStatus::getRejectFlag() == 0 ) {
196  times = 0;
197  } else {
198  for ( size_t ii = 0; ii < p->getNDaug(); ii++ ) {
199  EvtParticle* temp = p->getDaug( ii );
200  temp->deleteTree();
201  }
202  p->resetFirstOrNot();
203  p->resetNDaug();
204  }
205 
206  if ( times == 10000 ) {
207  EvtGenReport( EVTGEN_ERROR, "EvtGen" )
208  << "Your event has been rejected 10000 times!" << endl;
209  EvtGenReport( EVTGEN_ERROR, "EvtGen" ) << "Will now abort." << endl;
210  ::abort();
211  times = 0;
212  }
213  } while ( times );
214 }
void initialize(const std::string &decayName, std::istream &pdtTable, EvtRandomEngine *randomEngine=0, EvtAbsRadCorr *isrEngine=0, const std::list< EvtDecayBase * > *extraModels=0, int mixingType=1, bool useXml=false)
Definition: EvtGen.cpp:87
void constructEvent(EvtParticle *baseParticle)
void resetFirstOrNot()
Definition: EvtParticle.cpp:81
static EvtDecayTable * getInstance()
std::ostream & EvtGenReport(EvtGenSeverity severity, const char *facility=0)
Definition: EvtReport.cpp:33
static void setRadCorrEngine(EvtAbsRadCorr *fsrEngine)
Definition: EvtRadCorr.cpp:49
void readPDT(std::istream &data)
Definition: EvtPDL.cpp:66
EvtPDL _pdl
Definition: EvtGen.hh:68
int _mixingType
Definition: EvtGen.hh:69
void setMixingType(int mixingType)
Definition: EvtCPUtil.hh:42
void resetNDaug()
Definition: EvtParticle.hh:286
size_t getNDaug() const
static void initRejectFlag()
Definition: EvtStatus.hh:32
static EvtParticle * particleFactory(EvtSpinType::spintype spinType)
EvtGen(const std::string &decayName, const std::string &pdtTableName, EvtRandomEngine *randomEngine=0, EvtAbsRadCorr *isrEngine=0, const std::list< EvtDecayBase * > *extraModels=0, int mixingType=1, bool useXml=false)
Definition: EvtGen.cpp:62
void deleteTree()
EvtHepMCEvent * generateDecay(int PDGid, EvtVector4R refFrameP4, EvtVector4R translation, EvtSpinDensity *spinDensity=0)
Definition: EvtGen.cpp:163
static EvtId evtIdFromStdHep(int stdhep)
Definition: EvtPDL.cpp:246
static int getRejectFlag()
Definition: EvtStatus.hh:43
void readUDecay(const std::string &udecay_name, bool useXml=false)
Definition: EvtGen.cpp:139
void readXMLDecayFile(const std::string dec_name, bool verbose=true)
static EvtCPUtil * getInstance()
Definition: EvtCPUtil.cpp:43
EvtParticle * getDaug(int i)
Definition: EvtParticle.cpp:91
void readDecayFile(const std::string dec_name, bool verbose=true)
~EvtGen()
Definition: EvtGen.cpp:50
static void setRandomEngine(EvtRandomEngine *randomEngine)
Definition: EvtRandom.cpp:37