Belle II Software  release-06-00-14
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 
17 using namespace std;
18 using namespace Belle2;
19 
20 //-----------------------------------------------------------------
21 // Register the Module
22 //-----------------------------------------------------------------
23 REG_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 
38 void HepMCOutputModule::initialize()
39 {
40  m_ascii_io = std::make_unique<HepMC::IO_GenEvent>(m_filename, std::ios::out);
41 }
42 
43 
44 void HepMCOutputModule::event()
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;
53  if (!m_storeVirtualParticles) {
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  TVector3 mom = mcPart.getMomentum();
77  TVector3 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,
101  mcPart.getProductionTime() * Const::speedOfLight * 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 
112 void HepMCOutputModule::terminate()
113 {
114 
115 }
The HepMCOutput 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.