Belle II Software  release-08-01-10
KKGenInputModule.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 #include <generators/kkmc/KKGenInterface.h>
10 #include <generators/modules/kkgeninput/KKGenInputModule.h>
11 #include <framework/utilities/ConditionalGaussGenerator.h>
12 #include <mdst/dataobjects/MCParticleGraph.h>
13 #include <framework/utilities/FileSystem.h>
14 
15 #include <framework/datastore/DataStore.h>
16 #include <framework/datastore/StoreArray.h>
17 #include <framework/dataobjects/EventMetaData.h>
18 #include <framework/datastore/StoreObjPtr.h>
19 
20 #include <framework/logging/Logger.h>
21 #include <framework/utilities/IOIntercept.h>
22 
23 #include <boost/filesystem.hpp>
24 #include <Math/Vector3D.h>
25 
26 #include <stdio.h>
27 
28 using namespace std;
29 using namespace Belle2;
30 
31 //-----------------------------------------------------------------
32 // Register the Module
33 //-----------------------------------------------------------------
34 REG_MODULE(KKGenInput);
35 
36 //-----------------------------------------------------------------
37 // Implementation
38 //-----------------------------------------------------------------
39 
40 KKGenInputModule::KKGenInputModule() : GeneratorBaseModule(), m_initial(BeamParameters::c_smearVertex)
41 {
42  //Set module properties
43  setDescription("KKGenInput module. This an interface for KK2f Event Generator for basf2. The generated events are stored into MCParticles. You can find an expample of its decay file (tau_decaytable.dat) for tau-pair events at ${BELLE2_RELEASE_DIR}/data/generators/kkmc. On the other hand, when you like to generate mu-pair events, ${BELLE2_RELEASE_DIR}/data/generators/kkmc/mu.input.dat should be set to tauinputFile in your steering file.");
45 
46  //Parameter definition
47  addParam("KKdefaultFile", m_KKdefaultFileName, "default KKMC setting filename",
48  FileSystem::findFile("/data/generators/kkmc/KK2f_defaults.dat"));
49  addParam("tauinputFile", m_tauinputFileName, "user-defined tau/mu/q-pairs generation setting",
50  FileSystem::findFile("/data/generators/kkmc/KK2f_defaults.dat"));
51  addParam("taudecaytableFile", m_taudecaytableFileName, "tau-decay-table file name",
52  FileSystem::findFile("/data/generators/kkmc/tau.input.dat"));
53  addParam("kkmcoutputfilename", m_KKMCOutputFileName, "KKMC output filename", string(""));
54 
55 }
56 
57 
59 {
60  //Initialize MCParticle collection
61  StoreArray<MCParticle> mcparticle;
62  mcparticle.registerInDataStore();
63 
64  //Initialize initial particle for beam parameters.
66 
67 }
68 
70 {
71 
72 }
73 
75 {
76 
77  // Check if the BeamParameters have changed (if they do, abort the job! otherwise cross section calculation will be a nightmare.)
78  if (m_beamParams.hasChanged()) {
79  if (!m_initialized) {
81  } else {
82  B2FATAL("KKGenInputModule::event(): BeamParameters have changed within a job, this is not supported for KKMC!");
83  }
84  }
85 
86  StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
87 
88  //generate an MCInitialEvent (for vertex smearing)
89  ROOT::Math::XYZVector vertex = m_initial.getVertexConditional();
90 
91  const ConditionalGaussGenerator& lorentzGenerator = m_initial.getLorentzGenerator();
92 
93  mpg.clear();
94  int nPart = m_Ikkgen.simulateEvent(mpg, lorentzGenerator, vertex);
95 
96  // to check surely generated events are received or not
97  for (int i = 0; i < nPart; ++i) {
99  int moID = 0;
100  char buf[200];
101  sprintf(buf, "IntC: %3d %4u %8d %4d %4d %4d %9.4f %9.4f %9.4f %9.4f",
102  p->getIndex(), p->getStatus(), p->getPDG(), moID,
103  p->getFirstDaughter(), p->getLastDaughter(),
104  p->get4Vector().Px(), p->get4Vector().Py(),
105  p->get4Vector().Pz(), p->get4Vector().E());
106  B2DEBUG(100, buf);
107  }
108 
109  B2DEBUG(150, "Generated event " << eventMetaDataPtr->getEvent() << " with " << nPart << " particles.");
110 }
111 
113 {
114  m_Ikkgen.term();
115 }
116 
117 
119 {
120 
121  FILE* fp;
122 
123  if (m_KKMCOutputFileName.empty()) {
124  m_KKMCOutputFileName = boost::filesystem::unique_path("KKMC-%%%%%%%%%%.txt").native();
125  B2DEBUG(150, "Using KKMC output file " << m_KKMCOutputFileName);
126  }
128  auto uniqueOutputFileName = boost::filesystem::unique_path(m_KKMCOutputFileName + "-%%%%%%%%%%").native();
129  B2WARNING("The KKMC output file " << m_KKMCOutputFileName << " already exists. Using " << uniqueOutputFileName << " instead.");
130  m_KKMCOutputFileName = uniqueOutputFileName;
131  }
132  fp = fopen(m_KKMCOutputFileName.c_str(), "w");
133  if (fp) {
134  fclose(fp);
135  remove(m_KKMCOutputFileName.c_str());
136  } else {
137  B2FATAL("KKGenInputModule::initializeGenerator(): Failed to open KKMC output file!");
138  }
139 
140  B2DEBUG(150, "m_KKdefaultFileName: " << m_KKdefaultFileName);
141  B2DEBUG(150, "m_tauinputFileName: " << m_tauinputFileName);
142  B2DEBUG(150, "m_taudecaytableFileName: " << m_taudecaytableFileName);
143  B2DEBUG(150, "m_KKMCOutputFileName: " << m_KKMCOutputFileName);
144 
145  // Ensure that files provided by user exists
147  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_tauinputFileName << " not found!");
148  }
150  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_taudecaytableFileName << " not found!");
151  }
152 
153 
154 
155  //m_initial.initialize();
157  double E0cms = m_initial.getNominalEcms();
158  double E0cmsSpread = m_initial.getNominalEcmsSpread();
159 
160 
161 
162  IOIntercept::OutputToLogMessages initLogCapture("EvtGen", LogConfig::c_Debug, LogConfig::c_Info, 100, 100);
163  initLogCapture.start();
166 
167 
168 
169  //set the beam parameters, ignoring beam energy spread for the moment
170  m_Ikkgen.set_beam_info(E0cms, E0cmsSpread);
171  initLogCapture.finish();
172 
173  m_initialized = true;
174 
175  B2DEBUG(150, "KKGenInputModule::initializeGenerator(): Finished initialising the KKGen Input Module. ");
176 
177 }
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
Class implementing n-dimensional random number generator from Gaussian distribution where the first c...
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition: DataStore.h:59
static std::string findFile(const std::string &path, bool silent=false)
Search for given file or directory in local or central release directory, and return absolute path if...
Definition: FileSystem.cc:148
static bool fileExists(const std::string &filename)
Check if the file with given filename exists.
Definition: FileSystem.cc:32
The base module for generator modules, which sets the generator information as EventExtraInfo.
bool start()
Start intercepting the output.
Definition: IOIntercept.h:155
Capture stdout and stderr and convert into log messages.
Definition: IOIntercept.h:226
bool finish()
Finish the capture and emit the message if output has appeard on stdout or stderr.
Definition: IOIntercept.cc:236
double getNominalEcmsSpread()
Get spread of CMS collision energy calculated from beam parameters.
const ConditionalGaussGenerator & getLorentzGenerator()
Get the generator for the Lorentz transformation.
double getNominalEcms()
Get the CMS energy of collisions.
DBObjPtr< BeamParameters > m_beamParams
BeamParameter.
bool m_initialized
True if generator has been initialized.
std::string m_KKdefaultFileName
KKMC default setting file.
virtual void terminate() override
Method is called at the end of the event processing.
std::string m_tauinputFileName
KKMC setting file for generating fermions.
std::string m_KKMCOutputFileName
KKMC output file previously as set "fort.16".
virtual void generatorInitialize() override
Initializes the module.
virtual void beginRun() override
Method is called for each run.
InitialParticleGeneration m_initial
initial particle used by BeamParameter class
KKGenInterface m_Ikkgen
An instance of the KK2f MC Interface.
MCParticleGraph mpg
An instance of the MCParticle graph.
void initializeGenerator()
Method is called to initialize the generator.
std::string m_taudecaytableFileName
tau decay tables in PYTHIA6 manner.
virtual void generatorEvent() override
Method is called for each event.
int simulateEvent(MCParticleGraph &graph, const ConditionalGaussGenerator &lorentzGenerator, ROOT::Math::XYZVector vertex)
Simulate the events.
void term()
Terminate the generator.
int setup(const std::string &KKdefaultFileName, const std::string &tauinputFileName, const std::string &taudecaytableFileName, const std::string &KKMCOutputFileName)
Setup for KKMC and TAUOLA.
void set_beam_info(double Ecms0, double Ecms0Spread)
Setup for beams information.
@ c_Info
Info: for informational messages, e.g.
Definition: LogConfig.h:27
@ c_Debug
Debug: for code development.
Definition: LogConfig.h:26
Class to represent Particle data in graph.
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition: Module.cc:208
@ c_Input
This module is an input module (reads data).
Definition: Module.h:78
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
ROOT::Math::XYZVector getVertexConditional()
Generate vertex position and possibly update the generator of Lorentz transformation.
void initialize()
function to be executed on initialize()
void clear()
Reset particles and decay information to make the class reusable.
Abstract base class for different kinds of events.