Belle II Software  release-05-02-19
KKGenInputModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Kiyoshi Hayasaka, Torben Ferber *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <generators/kkmc/KKGenInterface.h>
12 #include <generators/modules/kkgeninput/KKGenInputModule.h>
13 #include <mdst/dataobjects/MCParticleGraph.h>
14 #include <framework/utilities/FileSystem.h>
15 
16 #include <framework/datastore/DataStore.h>
17 #include <framework/datastore/StoreArray.h>
18 #include <framework/dataobjects/EventMetaData.h>
19 #include <framework/datastore/StoreObjPtr.h>
20 
21 #include <framework/logging/Logger.h>
22 #include <framework/utilities/IOIntercept.h>
23 
24 #include <boost/filesystem.hpp>
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() : Module(), 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.");
44  setPropertyFlags(c_Input);
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 void KKGenInputModule::initialize()
58 {
59  //Initialize MCParticle collection
60  StoreArray<MCParticle> mcparticle;
61  mcparticle.registerInDataStore();
62 
63  //Initialize initial particle for beam parameters.
64  m_initial.initialize();
65 
66 }
67 
68 void KKGenInputModule::beginRun()
69 {
70 
71 }
72 
73 void KKGenInputModule::event()
74 {
75 
76  // Check if the BeamParameters have changed (if they do, abort the job! otherwise cross section calculation will be a nightmare.)
77  if (m_beamParams.hasChanged()) {
78  if (!m_initialized) {
79  initializeGenerator();
80  } else {
81  B2FATAL("KKGenInputModule::event(): BeamParameters have changed within a job, this is not supported for KKMC!");
82  }
83  }
84 
85  StoreObjPtr<EventMetaData> eventMetaDataPtr("EventMetaData", DataStore::c_Event);
86 
87  //generate an MCInitialEvent (for vertex smearing)
88  const MCInitialParticles& initial = m_initial.generate();
89  TVector3 vertex = initial.getVertex();
90 
91  mpg.clear();
92  int nPart = m_Ikkgen.simulateEvent(mpg, vertex);
93 
94  // to check surely generated events are received or not
95  for (int i = 0; i < nPart; ++i) {
96  MCParticleGraph::GraphParticle* p = &mpg[i];
97  int moID = 0;
98  char buf[200];
99  sprintf(buf, "IntC: %3d %4u %8d %4d %4d %4d %9.4f %9.4f %9.4f %9.4f",
100  p->getIndex(), p->getStatus(), p->getPDG(), moID,
101  p->getFirstDaughter(), p->getLastDaughter(),
102  p->get4Vector().Px(), p->get4Vector().Py(),
103  p->get4Vector().Pz(), p->get4Vector().E());
104  B2DEBUG(100, buf);
105  }
106 
107  B2DEBUG(150, "Generated event " << eventMetaDataPtr->getEvent() << " with " << nPart << " particles.");
108 }
109 
110 void KKGenInputModule::terminate()
111 {
112  m_Ikkgen.term();
113 }
114 
115 
116 void KKGenInputModule::initializeGenerator()
117 {
118  FILE* fp;
119 
120  if (m_KKMCOutputFileName.empty()) {
121  m_KKMCOutputFileName = boost::filesystem::unique_path("KKMC-%%%%%%%%%%.txt").native();
122  B2DEBUG(150, "Using KKMC output file " << m_KKMCOutputFileName);
123  }
124  if (FileSystem::fileExists(m_KKMCOutputFileName)) {
125  auto uniqueOutputFileName = boost::filesystem::unique_path(m_KKMCOutputFileName + "-%%%%%%%%%%").native();
126  B2WARNING("The KKMC output file " << m_KKMCOutputFileName << " already exists. Using " << uniqueOutputFileName << " instead.");
127  m_KKMCOutputFileName = uniqueOutputFileName;
128  }
129  fp = fopen(m_KKMCOutputFileName.c_str(), "w");
130  if (fp) {
131  fclose(fp);
132  remove(m_KKMCOutputFileName.c_str());
133  } else {
134  B2FATAL("KKGenInputModule::initializeGenerator(): Failed to open KKMC output file!");
135  }
136 
137  B2DEBUG(150, "m_KKdefaultFileName: " << m_KKdefaultFileName);
138  B2DEBUG(150, "m_tauinputFileName: " << m_tauinputFileName);
139  B2DEBUG(150, "m_taudecaytableFileName: " << m_taudecaytableFileName);
140  B2DEBUG(150, "m_KKMCOutputFileName: " << m_KKMCOutputFileName);
141 
142  // Ensure that files provided by user exists
143  if (!m_tauinputFileName.empty() && !FileSystem::fileExists(m_tauinputFileName)) {
144  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_tauinputFileName << " not found!");
145  }
146  if (!m_taudecaytableFileName.empty() && !FileSystem::fileExists(m_taudecaytableFileName)) {
147  B2FATAL("KKGenInputModule::initializeGenerator(): " << m_taudecaytableFileName << " not found!");
148  }
149 
150  IOIntercept::OutputToLogMessages initLogCapture("EvtGen", LogConfig::c_Debug, LogConfig::c_Info, 100, 100);
151  initLogCapture.start();
152  m_Ikkgen.setup(m_KKdefaultFileName, m_tauinputFileName,
153  m_taudecaytableFileName, m_KKMCOutputFileName);
154 
155  const MCInitialParticles& initial = m_initial.generate();
156  TLorentzVector v_ler = initial.getLER();
157  TLorentzVector v_her = initial.getHER();
158 
159  //set the beam parameters, ignoring beam energy spread for the moment
160  m_Ikkgen.set_beam_info(v_ler, 0.0, v_her, 0.0);
161  initLogCapture.finish();
162 
163  m_initialized = true;
164 
165  B2DEBUG(150, "KKGenInputModule::initializeGenerator(): Finished initialising the KKGen Input Module. ");
166 
167 }
Belle2::MCInitialParticles::getLER
const TLorentzVector & getLER() const
Get 4vector of the low energy beam.
Definition: MCInitialParticles.h:140
Belle2::KKGenInputModule
The KKGenInput module.
Definition: KKGenInputModule.h:41
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::IOIntercept::OutputToLogMessages::finish
bool finish()
Finish the capture and emit the message if output has appeard on stdout or stderr.
Definition: IOIntercept.cc:245
Belle2::MCInitialParticles::getHER
const TLorentzVector & getHER() const
Get 4vector of the high energy beam.
Definition: MCInitialParticles.h:137
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::ProcType::c_Input
@ c_Input
Input Process.
Belle2::MCInitialParticles
This class contains the initial state for the given event.
Definition: MCInitialParticles.h:35
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::IOIntercept::InterceptOutput::start
bool start()
Start intercepting the output.
Definition: IOIntercept.h:165
Belle2::MCInitialParticles::getVertex
const TVector3 & getVertex() const
Get the position of the collision.
Definition: MCInitialParticles.h:143
Belle2::IOIntercept::OutputToLogMessages
Capture stdout and stderr and convert into log messages.
Definition: IOIntercept.h:236
Belle2::StoreArray< MCParticle >
Belle2::BeamParameters
This class contains the nominal beam parameters and the parameters used for smearing of the primary v...
Definition: BeamParameters.h:33
Belle2::MCParticleGraph::GraphParticle
Class to represent Particle data in graph.
Definition: MCParticleGraph.h:86