Belle II Software  release-06-01-15
BelleMCOutputModule.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 /* Own header. */
10 #include <b2bii/modules/BelleMCOutput/BelleMCOutputModule.h>
11 
12 /* Belle2 headers. */
13 #include <framework/gearbox/Unit.h>
14 #include <framework/logging/Logger.h>
15 
16 /* Belle headers. */
17 #include <belle_legacy/tables/belletdf.h>
18 #include <belle_legacy/tables/hepevt.h>
19 #include <belle_legacy/tables/filespec.h>
20 
21 /* ROOT headers. */
22 #include <TMatrixD.h>
23 
24 using namespace Belle2;
25 
26 REG_MODULE(BelleMCOutput)
27 
29  Module()
30 {
31  setDescription("Output of MC particle list in Belle format.");
32  setPropertyFlags(c_Output);
33  addParam("outputFileName", m_OutputFileName,
34  "Output file name.", std::string("belle_mc.mdst"));
35  addParam("decayKsInGenerator", m_DecayKsInGenerator,
36  "Decay K_S0 in generator.", false);
37  addParam("decayLambdaInGenerator", m_DecayLambdaInGenerator,
38  "Decay Lambda in generator.", false);
39 }
40 
42 {
43 }
44 
46 {
47  m_BelleFile = new Belle::Panther_FileIO(m_OutputFileName.c_str(), BBS_WRITE);
48  m_BelleFile->write(BBS_FORMAT, 0);
49  m_BelleFile->save_br("BELLE_FILE_SPECIFICATION");
50  m_BelleFile->save_br("BELLE_RUNHEAD");
51  m_BelleFile->save_br("BELLE_NOMINAL_BEAM");
52  m_BelleFile->save("GEN_HEPEVT");
53  B2WARNING(
54  "************ ATTENTION! ************\n"
55  "\n"
56  " Belle MC generation differs from Belle II. In order to generate MC "
57  "correctly, you must do the following:\n"
58  "\n"
59  " 1. Use global tag with smearing of beam parameters and generation "
60  "flags: b2bii_beamParameters_with_smearing (there are no smearing data "
61  "or generation flags in the tags B2BII or B2BII_MC).\n"
62  " 2. Disable smearing in Belle simulation by removing the module "
63  "\"bpsmear\" from basf gsim scripts.\n"
64  "\n"
65  "It is also necessary to consider the following differences:\n"
66  "\n"
67  " 1. When converting the simulation result back to Belle II format, "
68  "it is recommended to disable conversion of beam parameters "
69  "(convertBeamParameters = False). Since the format of the table "
70  "BELLE_NOMINAL_BEAM is not sufficient to store an arbitrary covariance "
71  "matrix, the covariance matrix of IP position (BeamSpot) is not guaranteed "
72  "to be exactly the same after conversion.\n"
73  " 2. By default, decays of long-lived particles are removed and particles "
74  "are declared to be stable in generator (ISTHEP == 1 in basf) because "
75  "such decays are simulated by GEANT3 in basf. However, you may choose "
76  "to decay such particles via module parameters. This results in direct "
77  "passing of the decay products to simulation, i.e. material effects are "
78  "ignored. In this case, you may need to perform an additional study "
79  "of the difference between data and MC.\n"
80  "\n"
81  "************************************\n"
82  );
83 }
84 
86 {
87  Belle::Belle_file_specification_Manager& fileManager =
88  Belle::Belle_file_specification_Manager::get_manager();
89  fileManager.remove();
90  int position = 0;
91  int length = m_OutputFileName.size();
92  do {
93  Belle::Belle_file_specification& file = fileManager.add();
94  file.Spec(m_OutputFileName.substr(position, std::min(length, 8)).c_str());
95  position += 8;
96  length -= 8;
97  } while (length > 0);
98  Belle::Belle_runhead_Manager& runheadManager =
99  Belle::Belle_runhead_Manager::get_manager();
100  runheadManager.remove();
101  Belle::Belle_runhead& runhead = runheadManager.add();
102  runhead.ExpMC(2);
103  runhead.ExpNo(m_EventMetaData->getExperiment());
104  runhead.RunNo(m_EventMetaData->getRun());
105  runhead.Time(time(nullptr));
106  runhead.Type(0);
107  TLorentzVector momentumLER = m_BeamParameters->getLER();
108  TLorentzVector momentumHER = m_BeamParameters->getHER();
109  runhead.ELER(momentumLER.E());
110  runhead.EHER(momentumHER.E());
111  Belle::Belle_nominal_beam_Manager& beamManager =
112  Belle::Belle_nominal_beam_Manager::get_manager();
113  beamManager.remove();
114  Belle::Belle_nominal_beam& beam = beamManager.add();
115  beam.px_high(momentumHER.X());
116  beam.py_high(momentumHER.Y());
117  beam.pz_high(momentumHER.Z());
118  beam.px_low(momentumLER.X());
119  beam.py_low(momentumLER.Y());
120  beam.pz_low(momentumLER.Z());
121  TMatrixDSym herCovariance = m_BeamParameters->getCovHER();
122  beam.sigma_p_high(sqrt(herCovariance[0][0]));
123  TMatrixDSym lerCovariance = m_BeamParameters->getCovLER();
124  beam.sigma_p_low(sqrt(lerCovariance[0][0]));
125  /*
126  * The vertex parameters are in cm, as in basf2.
127  * The unit is not the same as for particles in GEN_HEPEVT.
128  */
129  TVector3 vertex = m_BeamParameters->getVertex();
130  beam.ip_x(vertex.X());
131  beam.ip_y(vertex.Y());
132  beam.ip_z(vertex.Z());
133  TMatrixDSym vertexCovariance = m_BeamParameters->getCovVertex();
134  beam.cang_high(momentumHER.Vect().Theta());
135  beam.cang_low(M_PI - momentumLER.Vect().Theta());
136  double angleIPZX = momentumHER.Vect().Theta() / 2;
137  beam.angle_ip_zx(angleIPZX);
138  /*
139  * Transformation of error matrix. It is inverse to the transformation in
140  * belle_legacy/ip/IpProfile.cc.
141  */
142  TRotation rotationY;
143  rotationY.RotateY(-angleIPZX);
144  TMatrixD rotationMatrix(3, 3);
145  rotationMatrix[0][0] = rotationY.XX();
146  rotationMatrix[0][1] = rotationY.XY();
147  rotationMatrix[0][2] = rotationY.XZ();
148  rotationMatrix[1][0] = rotationY.YX();
149  rotationMatrix[1][1] = rotationY.YY();
150  rotationMatrix[1][2] = rotationY.YZ();
151  rotationMatrix[2][0] = rotationY.ZX();
152  rotationMatrix[2][1] = rotationY.ZY();
153  rotationMatrix[2][2] = rotationY.ZZ();
154  TMatrixDSym vertexCovariance2 = vertexCovariance.Similarity(rotationMatrix);
155  beam.sigma_ip_x(sqrt(vertexCovariance2[0][0]));
156  beam.sigma_ip_y(sqrt(vertexCovariance2[1][1]));
157  beam.sigma_ip_z(sqrt(vertexCovariance2[2][2]));
158  m_BelleFile->write(BBS_BEGIN_RUN, 0);
159 }
160 
162  const MCParticle* particle, MCParticleGraph::GraphParticle* mother)
163 {
165  part = *particle;
166  if (mother != nullptr)
167  part.comesFrom(*mother);
168  int pdg = abs(particle->getPDG());
169  if ((pdg == Const::muon.getPDGCode()) ||
170  (pdg == Const::pion.getPDGCode()) ||
171  (pdg == Const::kaon.getPDGCode()) ||
172  ((pdg == Const::Kshort.getPDGCode()) && !m_DecayKsInGenerator) ||
173  (pdg == Const::Klong.getPDGCode()) ||
174  (pdg == Const::neutron.getPDGCode()) ||
175  ((pdg == Const::Lambda.getPDGCode()) && !m_DecayLambdaInGenerator) ||
176  (pdg == 3222) || // Sigma+
177  (pdg == 3112) || // Sigma-
178  (pdg == 3322) || // Xi0
179  (pdg == 3312) || // Xi-
180  (pdg == 3334)) { // Omega-
181  part.addStatus(MCParticle::c_StableInGenerator);
182  return;
183  }
184  std::vector<MCParticle*> daughters = particle->getDaughters();
185  for (const MCParticle* daughter : daughters)
186  addParticle(daughter, &part);
187 }
188 
190 {
191  Belle::Gen_hepevt_Manager& hepevtManager =
192  Belle::Gen_hepevt_Manager::get_manager();
193  hepevtManager.remove();
194  /*
195  * The time shift applied by basf module "evtgen" (file beam.cc) is
196  * vertex z coordinate [mm] / (2.0 * 2.99792458).
197  * The vertex coordinate is calculated relative to the IP position.
198  * The position correction happens at the simulation stage (bpsmear) in basf.
199  */
200  double timeShift = (m_MCInitialParticles->getVertex().Z() -
201  m_BeamParameters->getVertex().Z()) /
202  Unit::mm / (2.0 * 0.1 * Const::speedOfLight);
203  /*
204  * Regeneration of MCParticle array. It is necesary because in basf the
205  * long-lived particles (K_S0, K_L0, Lambda, neutron, pi, K, mu)
206  * are decayed by GEANT3.
207  */
209  for (const MCParticle& particle : m_MCParticles) {
210  if (particle.getMother() == nullptr)
211  addParticle(&particle, nullptr);
212  }
216  for (const MCParticle& particle : m_MCParticles) {
217  Belle::Gen_hepevt& hepevt = hepevtManager.add();
218  if (particle.hasStatus(MCParticle::c_Initial))
219  hepevt.isthep(3);
220  else if (particle.hasStatus(MCParticle::c_StableInGenerator))
221  hepevt.isthep(1);
222  else
223  hepevt.isthep(2);
224  hepevt.idhep(particle.getPDG());
225  hepevt.reset_mother();
226  const MCParticle* mother = particle.getMother();
227  int motherIndex = 0;
228  if (mother != nullptr)
229  motherIndex = mother->getIndex();
230  hepevt.moFirst(motherIndex);
231  hepevt.moLast(motherIndex);
232  hepevt.daFirst(particle.getFirstDaughter());
233  hepevt.daLast(particle.getLastDaughter());
234  TLorentzVector momentum = particle.get4Vector();
235  hepevt.PX(momentum.Px());
236  hepevt.PY(momentum.Py());
237  hepevt.PZ(momentum.Pz());
238  hepevt.E(momentum.E());
239  hepevt.M(particle.getMass());
240  TVector3 vertex = particle.getVertex();
241  hepevt.VX(vertex.X() / Unit::mm);
242  hepevt.VY(vertex.Y() / Unit::mm);
243  hepevt.VZ(vertex.Z() / Unit::mm);
244  hepevt.T(particle.getProductionTime() / Unit::mm * Const::speedOfLight +
245  timeShift);
246  }
247  m_BelleFile->write(BBS_EVENT, m_EventMetaData->getEvent());
248 }
249 
251 {
252 }
253 
255 {
256  delete m_BelleFile;
257 }
KLM digitization module.
void addParticle(const MCParticle *particle, MCParticleGraph::GraphParticle *mother)
Add particle to graph.
StoreObjPtr< MCInitialParticles > m_MCInitialParticles
Initial particles.
virtual void initialize() override
Initializer.
bool m_DecayKsInGenerator
Decay K_S0 in generator.
virtual void event() override
This method is called for each event.
Belle::Panther_FileIO * m_BelleFile
Belle file input-output handler.
virtual void endRun() override
This method is called if the current run ends.
virtual void terminate() override
This method is called at the end of the event processing.
virtual void beginRun() override
Called when entering a new run.
DBObjPtr< BeamParameters > m_BeamParameters
Beam parameters.
virtual ~BelleMCOutputModule()
Destructor.
bool m_DecayLambdaInGenerator
Decay Lambda in generator.
std::string m_OutputFileName
Output file name.
StoreObjPtr< EventMetaData > m_EventMetaData
Event metadata.
StoreArray< MCParticle > m_MCParticles
MC particles.
MCParticleGraph m_MCParticleGraph
MC particle graph.
static const ParticleType neutron
neutron particle
Definition: Const.h:556
static const ParticleType Lambda
Lambda particle.
Definition: Const.h:559
static const ChargedStable muon
muon particle
Definition: Const.h:541
static const ChargedStable pion
charged pion particle
Definition: Const.h:542
static const ParticleType Klong
K^0_L particle.
Definition: Const.h:558
static const double speedOfLight
[cm/ns]
Definition: Const.h:575
static const ParticleType Kshort
K^0_S particle.
Definition: Const.h:557
static const ChargedStable kaon
charged kaon particle
Definition: Const.h:543
Class to represent Particle data in graph.
@ c_checkCyclic
Check for cyclic dependencies.
@ c_clearParticles
Clear the particle list before adding the graph.
@ c_setDecayInfo
Set decay time and vertex.
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
@ c_Initial
bit 5: Particle is initial such as e+ or e- and not going to Geant4
Definition: MCParticle.h:57
@ c_StableInGenerator
bit 1: Particle is stable, i.e., not decaying in the generator.
Definition: MCParticle.h:49
Base class for Modules.
Definition: Module.h:72
static const double mm
[millimeters]
Definition: Unit.h:70
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
@ c_Output
Output Process.
void clear()
Reset particles and decay information to make the class reusable.
GraphParticle & addParticle()
Add new particle to the graph.
Abstract base class for different kinds of events.