Belle II Software  release-05-02-19
HepmcOutputModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2019 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Nikolai Hartmann *
7  * This software is provided "as is" without any warranty. *
8  **************************************************************************/
9 
10 #include <generators/modules/hepmcwriter/HepmcOutputModule.h>
11 
12 #include <framework/datastore/StoreArray.h>
13 #include <framework/dataobjects/EventMetaData.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/gearbox/Unit.h>
16 #include <mdst/dataobjects/MCParticle.h>
17 
18 using namespace std;
19 using namespace Belle2;
20 
21 //-----------------------------------------------------------------
22 // Register the Module
23 //-----------------------------------------------------------------
24 REG_MODULE(HepMCOutput)
25 
26 //-----------------------------------------------------------------
27 // Implementation
28 //-----------------------------------------------------------------
29 
31 {
32  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.");
33 
34  addParam("OutputFilename", m_filename, "The filename of the output file");
35  addParam("StoreVirtualParticles", m_storeVirtualParticles, "Store also virtual particles in the HepMC file.", false);
36 }
37 
38 
39 void HepMCOutputModule::initialize()
40 {
41  m_ascii_io = std::make_unique<HepMC::IO_GenEvent>(m_filename, std::ios::out);
42 }
43 
44 
45 void HepMCOutputModule::event()
46 {
47  StoreObjPtr<EventMetaData> eventMetaDataPtr;
48  StoreArray<MCParticle> mcPartCollection;
49 
50  int nPart = mcPartCollection.getEntries();
51 
52  //Find number of virtual particles
53  int nVirtualPart = 0;
54  if (!m_storeVirtualParticles) {
55  for (int iPart = 0; iPart < nPart; ++iPart) {
56  MCParticle& mcPart = *mcPartCollection[iPart];
57  if (mcPart.isVirtual()) nVirtualPart++;
58  }
59  }
60 
61  // The following fills values into the HEPEVT_Wrapper buffers
62  // The procedure is similar to the code in HepevtOutputModule
63 
64  HepMC::HEPEVT_Wrapper::zero_everything();
65  HepMC::HEPEVT_Wrapper::set_event_number(eventMetaDataPtr->getEvent());
66  HepMC::HEPEVT_Wrapper::set_number_entries(nPart - nVirtualPart);
67 
68  // Note: Particle numbering in HepMC starts at 1
69  for (int iPart = 1; iPart <= nPart; ++iPart) {
70  // but this is a normal array, starting at 0
71  MCParticle& mcPart = *mcPartCollection[iPart - 1];
72 
73  if (!m_storeVirtualParticles && mcPart.isVirtual()) {
74  continue;
75  }
76 
77  TVector3 mom = mcPart.getMomentum();
78  TVector3 vert = mcPart.getVertex();
79 
80  int isthep = 1;
81  if (mcPart.getFirstDaughter() > 0) isthep = 2;
82  if (mcPart.isInitial()) isthep = 2;
83 
84  int motherIndex = 0;
85 
86  if (mcPart.getMother() != NULL) motherIndex = mcPart.getMother()->getIndex();
87 
88  HepMC::HEPEVT_Wrapper::set_status(iPart, isthep);
89  HepMC::HEPEVT_Wrapper::set_id(iPart, mcPart.getPDG());
90  HepMC::HEPEVT_Wrapper::set_parents(iPart, motherIndex, motherIndex);
91  HepMC::HEPEVT_Wrapper::set_children(iPart, mcPart.getFirstDaughter(), mcPart.getLastDaughter());
92  HepMC::HEPEVT_Wrapper::set_momentum(iPart,
93  mom.X() * Unit::GeV,
94  mom.Y() * Unit::GeV,
95  mom.Z() * Unit::GeV,
96  mcPart.getEnergy() * Unit::GeV);
97  HepMC::HEPEVT_Wrapper::set_mass(iPart, mcPart.getMass() * Unit::GeV);
98  HepMC::HEPEVT_Wrapper::set_position(iPart,
99  vert.X() * Unit::cm,
100  vert.Y() * Unit::cm,
101  vert.Z() * Unit::cm,
102  mcPart.getProductionTime() * Const::speedOfLight * Unit::cm);
103  }
104 
105  // read from buffers and write event to disk
106  HepMC::GenEvent* evt = m_hepevtio.read_next_event();
107  evt->use_units(HepMC::Units::GEV, HepMC::Units::CM);
108  *m_ascii_io << evt;
109  delete evt;
110 
111 }
112 
113 void HepMCOutputModule::terminate()
114 {
115 
116 }
Belle2::MCParticle::getFirstDaughter
int getFirstDaughter() const
Get 1-based index of first daughter, 0 if no daughters.
Definition: MCParticle.h:262
Belle2::MCParticle::getIndex
int getIndex() const
Get 1-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:241
Belle2::MCParticle::getEnergy
float getEnergy() const
Return particle energy in GeV.
Definition: MCParticle.h:158
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::HepMCOutputModule
The HepMCOutput module.
Definition: HepmcOutputModule.h:44
Belle2::MCParticle::isInitial
bool isInitial() const
Check if particle is an initial particle such as ISR.
Definition: MCParticle.h:583
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::MCParticle::getVertex
TVector3 getVertex() const
Return production vertex position, shorthand for getProductionVertex().
Definition: MCParticle.h:194
Belle2::MCParticle::getPDG
int getPDG() const
Return PDG code of particle.
Definition: MCParticle.h:123
Belle2::MCParticle::getMass
float getMass() const
Return the particle mass in GeV.
Definition: MCParticle.h:146
Belle2::MCParticle::getMother
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition: MCParticle.h:593
Belle2::MCParticle::getLastDaughter
int getLastDaughter() const
Get 1-based index of last daughter, 0 if no daughters.
Definition: MCParticle.h:270
Belle2::MCParticle::isVirtual
bool isVirtual() const
Check if particle is virtual.
Definition: MCParticle.h:568
Belle2::MCParticle::getMomentum
TVector3 getMomentum() const
Return momentum.
Definition: MCParticle.h:209
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< MCParticle >
Belle2::StoreArray::getEntries
int getEntries() const
Get the number of objects in the array.
Definition: StoreArray.h:226
Belle2::MCParticle::getProductionTime
float getProductionTime() const
Return production time in ns.
Definition: MCParticle.h:170