EvtGen 2.2.0
Monte Carlo generator of particle decays, in particular the weak decays of heavy flavour particles such as B mesons.
Loading...
Searching...
No Matches
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
27#include "EvtGenBase/EvtPDL.hh"
37
40
41#include <cstdlib>
42#include <fstream>
43#include <string>
44
45using std::endl;
46using std::fstream;
47using std::ifstream;
48
50{
51 //This is a bit ugly, should not do anything
52 //in a destructor. This will fail if EvtGen is made a static
53 //because then this destructor might be called _after_
54 //the destruction of objects that it depends on, e.g., EvtPDL.
55
56 if ( getenv( "EVTINFO" ) ) {
58 }
59}
60
61EvtGen::EvtGen( const std::string& decayName, const std::string& pdtTableName,
62 EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
63 const std::list<EvtDecayBase*>* extraModels, int mixingType,
64 bool useXml )
65{
66 std::ifstream pdtIn( pdtTableName );
67 if ( !pdtIn ) {
68 EvtGenReport( EVTGEN_ERROR, "EvtGen" )
69 << "Could not open:" << pdtTableName << "EvtPDL" << endl;
70 return;
71 }
72 initialize( decayName, pdtIn, randomEngine, isrEngine, extraModels,
73 mixingType, useXml );
74 pdtIn.close();
75}
76
77EvtGen::EvtGen( const std::string& decayName, std::istream& pdtTableData,
78 EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
79 const std::list<EvtDecayBase*>* extraModels, int mixingType,
80 bool useXml )
81{
82 initialize( decayName, pdtTableData, randomEngine, isrEngine, extraModels,
83 mixingType, useXml );
84}
85
86void EvtGen::initialize( const std::string& decayName, std::istream& pdtTable,
87 EvtRandomEngine* randomEngine, EvtAbsRadCorr* isrEngine,
88 const std::list<EvtDecayBase*>* extraModels,
89 int mixingType, bool useXml )
90{
91 EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Initializing EvtGen" << endl;
92
93 if ( !randomEngine ) {
94 static thread_local EvtSimpleRandomEngine defaultRandomEngine;
95 EvtRandom::setRandomEngine( &defaultRandomEngine );
96 EvtGenReport( EVTGEN_INFO, "EvtGen" )
97 << "No random engine given in "
98 << "EvtGen::EvtGen constructor, "
99 << "will use default EvtSimpleRandomEngine." << endl;
100 } else {
101 EvtRandom::setRandomEngine( randomEngine );
102 }
103
104 EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Storing known decay models" << endl;
105 EvtModelReg dummy( extraModels );
106
107 EvtGenReport( EVTGEN_INFO, "EvtGen" )
108 << "Main decay file name :" << decayName << endl;
109
110 EvtPDL::readPDT( pdtTable );
111
112 if ( useXml ) {
113 EvtDecayTable::getInstance().readXMLDecayFile( decayName, false );
114 } else {
115 EvtDecayTable::getInstance().readDecayFile( decayName, false );
116 }
117
118 m_mixingType = mixingType;
119 EvtGenReport( EVTGEN_INFO, "EvtGen" )
120 << "Mixing type integer set to " << m_mixingType << endl;
122
123 // Set the radiative correction engine
124
125 if ( isrEngine != nullptr ) {
126 EvtRadCorr::setRadCorrEngine( isrEngine );
127
128 } else {
129 // Owing to the pure abstract interface, we still need to define a concrete
130 // implementation of a radiative correction engine. Use one which does nothing.
131 EvtAbsRadCorr* noRadCorr = new EvtNoRadCorr();
132 EvtRadCorr::setRadCorrEngine( noRadCorr );
133 }
134
135 EvtGenReport( EVTGEN_INFO, "EvtGen" ) << "Done initializing EvtGen" << endl;
136}
137
138void EvtGen::readUDecay( const std::string& uDecayName, bool useXml )
139{
140 ifstream indec;
141
142 if ( uDecayName.size() == 0 ) {
143 EvtGenReport( EVTGEN_INFO, "EvtGen" )
144 << "Is not reading a user decay file!" << endl;
145 } else {
146 indec.open( uDecayName );
147 if ( indec ) {
148 if ( useXml ) {
149 EvtDecayTable::getInstance().readXMLDecayFile( uDecayName, true );
150 } else {
151 EvtDecayTable::getInstance().readDecayFile( uDecayName, true );
152 }
153 } else {
154 EvtGenReport( EVTGEN_INFO, "EvtGen" )
155 << "Can not find UDECAY file '" << uDecayName << "'. Stopping"
156 << endl;
157 ::abort();
158 }
159 }
160}
161
163 EvtVector4R translation,
164 EvtSpinDensity* spinDensity )
165{
166 EvtParticle* theParticle( nullptr );
167
168 if ( spinDensity == nullptr ) {
170 EvtPDL::evtIdFromStdHep( PDGId ), refFrameP4 );
171 } else {
173 EvtPDL::evtIdFromStdHep( PDGId ), refFrameP4, *spinDensity );
174 }
175
176 generateDecay( theParticle );
177 EvtHepMCEvent* hepMCEvent = new EvtHepMCEvent();
178 hepMCEvent->constructEvent( theParticle, translation );
179
180 theParticle->deleteTree();
181
182 return hepMCEvent;
183}
184
186{
187 int times = 0;
188 do {
189 times += 1;
191
192 p->decay();
193 //ok then finish.
194 if ( EvtStatus::getRejectFlag() == 0 ) {
195 times = 0;
196 } else {
197 for ( size_t ii = 0; ii < p->getNDaug(); ii++ ) {
198 EvtParticle* temp = p->getDaug( ii );
199 temp->deleteTree();
200 }
201 p->resetFirstOrNot();
202 p->resetNDaug();
203 }
204
205 if ( times == 10000 ) {
206 EvtGenReport( EVTGEN_ERROR, "EvtGen" )
207 << "Your event has been rejected 10000 times!" << endl;
208 EvtGenReport( EVTGEN_ERROR, "EvtGen" ) << "Will now abort." << endl;
209 ::abort();
210 times = 0;
211 }
212 } while ( times );
213}
std::ostream & EvtGenReport(EvtGenSeverity severity, const char *facility=nullptr)
Definition EvtReport.cpp:32
@ EVTGEN_INFO
Definition EvtReport.hh:52
@ EVTGEN_ERROR
Definition EvtReport.hh:49
static EvtCPUtil * getInstance()
Definition EvtCPUtil.cpp:42
void setMixingType(int mixingType)
Definition EvtCPUtil.hh:41
void readXMLDecayFile(const std::string dec_name, bool verbose=true)
void printSummary() const
static EvtDecayTable & getInstance()
void readDecayFile(const std::string dec_name, bool verbose=true)
int m_mixingType
Definition EvtGen.hh:67
~EvtGen()
Definition EvtGen.cpp:49
void readUDecay(const std::string &udecay_name, bool useXml=false)
Definition EvtGen.cpp:138
void initialize(const std::string &decayName, std::istream &pdtTable, EvtRandomEngine *randomEngine=nullptr, EvtAbsRadCorr *isrEngine=nullptr, const std::list< EvtDecayBase * > *extraModels=nullptr, int mixingType=1, bool useXml=false)
Definition EvtGen.cpp:86
EvtHepMCEvent * generateDecay(int PDGid, EvtVector4R refFrameP4, EvtVector4R translation, EvtSpinDensity *spinDensity=nullptr)
Definition EvtGen.cpp:162
EvtGen(const std::string &decayName, const std::string &pdtTableName, EvtRandomEngine *randomEngine=nullptr, EvtAbsRadCorr *isrEngine=nullptr, const std::list< EvtDecayBase * > *extraModels=nullptr, int mixingType=1, bool useXml=false)
Definition EvtGen.cpp:61
void constructEvent(EvtParticle *baseParticle)
static void readPDT(std::istream &data)
Definition EvtPDL.cpp:59
static EvtId evtIdFromStdHep(int stdhep)
Definition EvtPDL.cpp:241
static EvtParticle * particleFactory(EvtSpinType::spintype spinType)
void resetNDaug()
void resetFirstOrNot()
EvtParticle * getDaug(const int i)
size_t getNDaug() const
void deleteTree()
static void setRadCorrEngine(EvtAbsRadCorr *fsrEngine)
static void setRandomEngine(EvtRandomEngine *randomEngine)
Definition EvtRandom.cpp:36
static void initRejectFlag()
Definition EvtStatus.hh:32
static int getRejectFlag()
Definition EvtStatus.hh:43