Belle II Software development
HepmcOutputModule.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/hepmcwriter/HepmcOutputModule.h>
10
11#include <framework/datastore/StoreArray.h>
12#include <framework/dataobjects/EventMetaData.h>
13#include <framework/datastore/StoreObjPtr.h>
14#include <framework/gearbox/Unit.h>
15#include <mdst/dataobjects/MCParticle.h>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23REG_MODULE(HepMCOutput);
24
25//-----------------------------------------------------------------
26// Implementation
27//-----------------------------------------------------------------
28
30{
31 setDescription("HepMC file output. This module loads an event record from the MCParticle collection and store the content back into the HepMC (2) format. HepMC format is a standard event record format to contain an event record in a Monte Carlo-independent format.");
32
33 addParam("OutputFilename", m_filename, "The filename of the output file");
34 addParam("StoreVirtualParticles", m_storeVirtualParticles, "Store also virtual particles in the HepMC file.", false);
35}
36
37
39{
40 m_ascii_io = std::make_unique<HepMC::IO_GenEvent>(m_filename, std::ios::out);
41}
42
43
45{
46 StoreObjPtr<EventMetaData> eventMetaDataPtr;
47 StoreArray<MCParticle> mcPartCollection;
48
49 int nPart = mcPartCollection.getEntries();
50
51 //Find number of virtual particles
52 int nVirtualPart = 0;
54 for (int iPart = 0; iPart < nPart; ++iPart) {
55 MCParticle& mcPart = *mcPartCollection[iPart];
56 if (mcPart.isVirtual()) nVirtualPart++;
57 }
58 }
59
60 // The following fills values into the HEPEVT_Wrapper buffers
61 // The procedure is similar to the code in HepevtOutputModule
62
63 HepMC::HEPEVT_Wrapper::zero_everything();
64 HepMC::HEPEVT_Wrapper::set_event_number(eventMetaDataPtr->getEvent());
65 HepMC::HEPEVT_Wrapper::set_number_entries(nPart - nVirtualPart);
66
67 // Note: Particle numbering in HepMC starts at 1
68 for (int iPart = 1; iPart <= nPart; ++iPart) {
69 // but this is a normal array, starting at 0
70 MCParticle& mcPart = *mcPartCollection[iPart - 1];
71
72 if (!m_storeVirtualParticles && mcPart.isVirtual()) {
73 continue;
74 }
75
76 ROOT::Math::XYZVector mom = mcPart.getMomentum();
77 ROOT::Math::XYZVector vert = mcPart.getVertex();
78
79 int isthep = 1;
80 if (mcPart.getFirstDaughter() > 0) isthep = 2;
81 if (mcPart.isInitial()) isthep = 2;
82
83 int motherIndex = 0;
84
85 if (mcPart.getMother() != NULL) motherIndex = mcPart.getMother()->getIndex();
86
87 HepMC::HEPEVT_Wrapper::set_status(iPart, isthep);
88 HepMC::HEPEVT_Wrapper::set_id(iPart, mcPart.getPDG());
89 HepMC::HEPEVT_Wrapper::set_parents(iPart, motherIndex, motherIndex);
90 HepMC::HEPEVT_Wrapper::set_children(iPart, mcPart.getFirstDaughter(), mcPart.getLastDaughter());
91 HepMC::HEPEVT_Wrapper::set_momentum(iPart,
92 mom.X() * Unit::GeV,
93 mom.Y() * Unit::GeV,
94 mom.Z() * Unit::GeV,
95 mcPart.getEnergy() * Unit::GeV);
96 HepMC::HEPEVT_Wrapper::set_mass(iPart, mcPart.getMass() * Unit::GeV);
97 HepMC::HEPEVT_Wrapper::set_position(iPart,
98 vert.X() * Unit::cm,
99 vert.Y() * Unit::cm,
100 vert.Z() * Unit::cm,
102 }
103
104 // read from buffers and write event to disk
105 HepMC::GenEvent* evt = m_hepevtio.read_next_event();
106 evt->use_units(HepMC::Units::GEV, HepMC::Units::CM);
107 *m_ascii_io << evt;
108 delete evt;
109
110}
111
113{
114
115}
static const double speedOfLight
[cm/ns]
Definition: Const.h:695
std::unique_ptr< HepMC::IO_GenEvent > m_ascii_io
The text file stream.
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
method is called at end
std::string m_filename
The output filename.
HepMC::IO_HEPEVT m_hepevtio
wrapper/buffer for hepevt conversion.
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
static const double cm
Standard units with the value = 1.
Definition: Unit.h:47
static const double GeV
Standard of [energy, momentum, mass].
Definition: Unit.h:51
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.