Belle II Software  release-06-00-14
HepevtOutputModule.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/modules/HepevtOutputModule.h>
10 
11 #include <framework/datastore/StoreArray.h>
12 #include <framework/datastore/StoreObjPtr.h>
13 #include <framework/dataobjects/EventMetaData.h>
14 #include <mdst/dataobjects/MCParticle.h>
15 
16 using namespace std;
17 using namespace Belle2;
18 using namespace boost;
19 using boost::format;
20 
21 //-----------------------------------------------------------------
22 // Register the Module
23 //-----------------------------------------------------------------
24 REG_MODULE(HepevtOutput)
25 
26 //-----------------------------------------------------------------
27 // Implementation
28 //-----------------------------------------------------------------
29 
31 {
32  //Set module properties
33  setDescription("HepEvt file output. This module loads an event record from the MCParticle collection and store the content back into the HEPEVT format. HEPEVT format is a standard event record format to contain an event record in a Monte Carlo-independent format.");
34 
35  //Parameter definition
36  addParam("OutputFilename", m_filename, "The filename of the output file");
37  addParam("MirrorPz", m_mirrorPz, "If the directions of HER and LER are switched, mirror Pz.", false);
38  addParam("StoreVirtualParticles", m_storeVirtualParticles, "Store also virtual particles in the HePEvt file.", false);
39  addParam("FullFormat", m_fullFormat, "Write the full HepEvt format to file. Set it to false for a compact format.", true);
40 }
41 
42 
43 void HepevtOutputModule::initialize()
44 {
45  m_fileStream.open(m_filename.c_str());
46 }
47 
48 
49 void HepevtOutputModule::event()
50 {
51  StoreObjPtr<EventMetaData> eventMetaDataPtr;
52  StoreArray<MCParticle> mcPartCollection;
53 
54  int nPart = mcPartCollection.getEntries();
55 
56  //Find number of virtual particles
57  int nVirtualPart = 0;
58  if (!m_storeVirtualParticles) {
59  for (int iPart = 0; iPart < nPart; ++iPart) {
60  MCParticle& mcPart = *mcPartCollection[iPart];
61  if (mcPart.isVirtual()) nVirtualPart++;
62  }
63  }
64 
65  m_fileStream << format("%10d%10d\n") % eventMetaDataPtr->getEvent() % (nPart - nVirtualPart);
66 
67  for (int iPart = 0; iPart < nPart; ++iPart) {
68  MCParticle& mcPart = *mcPartCollection[iPart];
69  if (!m_storeVirtualParticles && mcPart.isVirtual()) continue;
70 
71  TVector3 mom = mcPart.getMomentum();
72  if (m_mirrorPz) mom.SetZ(-1.0 * mom.Z());
73 
74  if (m_fullFormat) {
75  int isthep = 1;
76  if (mcPart.getFirstDaughter() > 0) isthep = 2;
77  if (mcPart.isInitial()) isthep = 2;
78 
79  int motherIndex = 0;
80  if (mcPart.getMother() != NULL) motherIndex = mcPart.getMother()->getIndex();
81 
82  m_fileStream << format("%5i%12i%10i%10i%10i%10i") % isthep % mcPart.getPDG() % motherIndex % motherIndex % mcPart.getFirstDaughter()
83  % mcPart.getLastDaughter();
84  m_fileStream << format("%15.6f%15.6f%15.6f%15.6f%15.6f") % mom.X() % mom.Y() % mom.Z() % mcPart.getEnergy() % mcPart.getMass();
85  m_fileStream << format("%15.6f%15.6f%15.6f%15.6f\n") % mcPart.getVertex().X() % mcPart.getVertex().Y() % mcPart.getVertex().Z() %
86  mcPart.getProductionTime();
87  } else {
88  m_fileStream << format("%15.6f%15.6f%15.6f%15.6f%15.6f%15i\n") % mom.X() % mom.Y() % mom.Z() % mcPart.getEnergy() % mcPart.getMass()
89  % mcPart.getPDG();
90  }
91  }
92 }
93 
94 
95 void HepevtOutputModule::terminate()
96 {
97  m_fileStream.close();
98 }
The HepevtOutput module.
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:32
float getEnergy() const
Return particle energy in GeV.
Definition: MCParticle.h:147
int getIndex() const
Get 1-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:230
float getMass() const
Return the particle mass in GeV.
Definition: MCParticle.h:135
TVector3 getVertex() const
Return production vertex position, shorthand for getProductionVertex().
Definition: MCParticle.h:183
TVector3 getMomentum() const
Return momentum.
Definition: MCParticle.h:198
int getLastDaughter() const
Get 1-based index of last daughter, 0 if no daughters.
Definition: MCParticle.h:259
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:112
float getProductionTime() const
Return production time in ns.
Definition: MCParticle.h:159
int getFirstDaughter() const
Get 1-based index of first daughter, 0 if no daughters.
Definition: MCParticle.h:251
Base class for Modules.
Definition: Module.h:72
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:216
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
bool isInitial() const
Check if particle is an initial particle such as ISR.
Definition: MCParticle.h:572
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:582
bool isVirtual() const
Check if particle is virtual.
Definition: MCParticle.h:557
Abstract base class for different kinds of events.