Belle II Software development
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
16using namespace std;
17using namespace Belle2;
18using namespace boost;
19using boost::format;
20
21//-----------------------------------------------------------------
22// Register the Module
23//-----------------------------------------------------------------
24REG_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
44{
45 m_fileStream.open(m_filename.c_str());
46}
47
48
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;
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 ROOT::Math::XYZVector 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
96{
97 m_fileStream.close();
98}
bool m_mirrorPz
If the directions of HER and LER are switched, mirror Pz.
virtual void initialize() override
Initializes the module.
bool m_storeVirtualParticles
Flag which specifies if virtual particles are stored in the HEPEvt file.
virtual void event() override
Method is called for each event.
virtual void terminate() override
Terminates the module.
bool m_fullFormat
Flag which specifies if the full HepEvt format should be written (true), or a compact format (false).
std::ofstream m_fileStream
The text file stream.
std::string m_filename
The output filename.
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
ROOT::Math::XYZVector getVertex() const
Return production vertex position, shorthand for getProductionVertex().
Definition: MCParticle.h:183
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
ROOT::Math::XYZVector getMomentum() const
Return momentum.
Definition: MCParticle.h:198
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
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
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: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
bool isInitial() const
Check if particle is an initial particle such as ISR.
Definition: MCParticle.h:590
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:600
bool isVirtual() const
Check if particle is virtual.
Definition: MCParticle.h:575
Abstract base class for different kinds of events.
STL namespace.