Belle II Software  release-06-02-00
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 <mdst/dataobjects/MCParticleGraph.h>
12 #include <framework/utilities/FileSystem.h>
13 
14 #include <framework/datastore/DataStore.h>
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/dataobjects/EventMetaData.h>
17 #include <framework/datastore/StoreObjPtr.h>
18 
19 #include <framework/logging/Logger.h>
20 #include <framework/utilities/IOIntercept.h>
21 
22 #include <boost/filesystem.hpp>
23 
24 #include <stdio.h>
25 
26 using namespace std;
27 using namespace Belle2;
28 
29 //-----------------------------------------------------------------
30 // Register the Module
31 //-----------------------------------------------------------------
32 REG_MODULE(KKGenInput)
33 
34 //-----------------------------------------------------------------
35 // Implementation
36 //-----------------------------------------------------------------
37 
38 KKGenInputModule::KKGenInputModule() : Module(), m_initial(BeamParameters::c_smearVertex)
39 {
40  //Set module properties
41  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.");
42  setPropertyFlags(c_Input);
43 
44  //Parameter definition
45  addParam("KKdefaultFile", m_KKdefaultFileName, "default KKMC setting filename",
46  FileSystem::findFile("/data/generators/kkmc/KK2f_defaults.dat"));
47  addParam("tauinputFile", m_tauinputFileName, "user-defined tau/mu/q-pairs generation setting",
48  FileSystem::findFile("/data/generators/kkmc/KK2f_defaults.dat"));
49  addParam("taudecaytableFile", m_taudecaytableFileName, "tau-decay-table file name",
50  FileSystem::findFile("/data/generators/kkmc/tau.input.dat"));
51  addParam("kkmcoutputfilename", m_KKMCOutputFileName, "KKMC output filename", string(""));
52 }
53 
54 
55 void KKGenInputModule::initialize()
56 {
57  //Initialize MCParticle collection
58  StoreArray<MCParticle> mcparticle;
59  mcparticle.registerInDataStore();
60 
61  //Initialize initial particle for beam parameters.
62  m_initial.initialize();
63 
64 }
65 
66 void KKGenInputModule::beginRun()
67 {
68 
69 }
70 
71 void KKGenInputModule::event()
72 {
73 
74  // Check if the BeamParameters have changed (if they do, abort the job! otherwise cross section calculation will be a nightmare.)
75  if (m_beamParams.hasChanged()) {
76  if (!m_initialized) {
77  initializeGenerator();
78  } else {
79  B2FATAL("KKGenInputModule::event(): BeamParameters have changed within a job, this is not supported for KKMC!");
80  }
81  }
82 
83  StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
84 
85  //generate an MCInitialEvent (for vertex smearing)
86  const MCInitialParticles& initial = m_initial.generate();
87  TVector3 vertex = initial.getVertex();
88 
89  mpg.clear();
90  int nPart = m_Ikkgen.simulateEvent(mpg, vertex);
91 
92  // to check surely generated events are received or not
93  for (int i = 0; i < nPart; ++i) {
94  MCParticleGraph::GraphParticle* p = &mpg[i];
95  int moID = 0;
96  char buf[200];
97  sprintf(buf, "IntC: %3d %4u %8d %4d %4d %4d %9.4f %9.4f %9.4f %9.4f",
98  p->getIndex(), p->getStatus(), p->getPDG(), moID,
99  p->getFirstDaughter(), p->getLastDaughter(),
100  p->get4Vector().Px(), p->get4Vector().Py(),
101  p->get4Vector().Pz(), p->get4Vector().E());
102  B2DEBUG(100, buf);
103  }
104 
105  B2DEBUG(150, "Generated event " << eventMetaDataPtr->getEvent() << " with " << nPart << " particles.");
106 }
107 
108 void KKGenInputModule::terminate()
109 {
110  m_Ikkgen.term();
111 }
112 
113 
114 void KKGenInputModule::initializeGenerator()
115 {
116  FILE* fp;
117 
118  if (m_KKMCOutputFileName.empty()) {
119  m_KKMCOutputFileName = boost::filesystem::unique_path("KKMC-%%%%%%%%%%.txt").native();
120  B2DEBUG(150, "Using KKMC output file " << m_KKMCOutputFileName);
121  }
122  if (FileSystem::fileExists(m_KKMCOutputFileName)) {
123  auto uniqueOutputFileName = boost::filesystem::unique_path(m_KKMCOutputFileName + "-%%%%%%%%%%").native();
124  B2WARNING("The KKMC output file " << m_KKMCOutputFileName << " already exists. Using " << uniqueOutputFileName << " instead.");
125  m_KKMCOutputFileName = uniqueOutputFileName;
126  }
127  fp = fopen(m_KKMCOutputFileName.c_str(), "w");
128  if (fp) {
129  fclose(fp);
130  remove(m_KKMCOutputFileName.c_str());
131  } else {
132  B2FATAL("KKGenInputModule::initializeGenerator(): Failed to open KKMC output file!");
133  }
134 
135  B2DEBUG(150, "m_KKdefaultFileName: " << m_KKdefaultFileName);
136  B2DEBUG(150, "m_tauinputFileName: " << m_tauinputFileName);
137  B2DEBUG(150, "m_taudecaytableFileName: " << m_taudecaytableFileName);
138  B2DEBUG(150, "m_KKMCOutputFileName: " << m_KKMCOutputFileName);
139 
140  // Ensure that files provided by user exists
141  if (!m_tauinputFileName.empty() && !FileSystem::fileExists(m_tauinputFileName)) {
142  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_tauinputFileName << " not found!");
143  }
144  if (!m_taudecaytableFileName.empty() && !FileSystem::fileExists(m_taudecaytableFileName)) {
145  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_taudecaytableFileName << " not found!");
146  }
147 
148  IOIntercept::OutputToLogMessages initLogCapture("EvtGen", LogConfig::c_Debug, LogConfig::c_Info, 100, 100);
149  initLogCapture.start();
150  m_Ikkgen.setup(m_KKdefaultFileName, m_tauinputFileName,
151  m_taudecaytableFileName, m_KKMCOutputFileName);
152 
153  const MCInitialParticles& initial = m_initial.generate();
154  TLorentzVector v_ler = initial.getLER();
155  TLorentzVector v_her = initial.getHER();
156 
157  //set the beam parameters, ignoring beam energy spread for the moment
158  m_Ikkgen.set_beam_info(v_ler, 0.0, v_her, 0.0);
159  initLogCapture.finish();
160 
161  m_initialized = true;
162 
163  B2DEBUG(150, "KKGenInputModule::initializeGenerator(): Finished initialising the KKGen Input Module. ");
164 
165 }
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
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:235
The KKGenInput module.
This class contains the initial state for the given event.
const TLorentzVector & getLER() const
Get 4vector of the low energy beam.
const TVector3 & getVertex() const
Get the position of the collision.
const TLorentzVector & getHER() const
Get 4vector of the high energy beam.
Class to represent Particle data in graph.
Base class for Modules.
Definition: Module.h:72
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:95
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
@ c_Input
Input Process.
Abstract base class for different kinds of events.