Belle II Software  release-05-02-19
HepevtOutputModule.cc
1 /**************************************************************************
2  * Belle II detector background library *
3  * Copyright(C) 2011-2012 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Andreas Moll *
7  * Updated by R. Godang 11/26/13 *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <generators/modules/HepevtOutputModule.h>
12 
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/dataobjects/EventMetaData.h>
16 #include <mdst/dataobjects/MCParticle.h>
17 
18 using namespace std;
19 using namespace Belle2;
20 using namespace boost;
21 using boost::format;
22 
23 //-----------------------------------------------------------------
24 // Register the Module
25 //-----------------------------------------------------------------
26 REG_MODULE(HepevtOutput)
27 
28 //-----------------------------------------------------------------
29 // Implementation
30 //-----------------------------------------------------------------
31 
33 {
34  //Set module properties
35  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.");
36 
37  //Parameter definition
38  addParam("OutputFilename", m_filename, "The filename of the output file");
39  addParam("MirrorPz", m_mirrorPz, "If the directions of HER and LER are switched, mirror Pz.", false);
40  addParam("StoreVirtualParticles", m_storeVirtualParticles, "Store also virtual particles in the HePEvt file.", false);
41  addParam("FullFormat", m_fullFormat, "Write the full HepEvt format to file. Set it to false for a compact format.", true);
42 }
43 
44 
45 void HepevtOutputModule::initialize()
46 {
47  m_fileStream.open(m_filename.c_str());
48 }
49 
50 
51 void HepevtOutputModule::event()
52 {
53  StoreObjPtr<EventMetaData> eventMetaDataPtr;
54  StoreArray<MCParticle> mcPartCollection;
55 
56  int nPart = mcPartCollection.getEntries();
57 
58  //Find number of virtual particles
59  int nVirtualPart = 0;
60  if (!m_storeVirtualParticles) {
61  for (int iPart = 0; iPart < nPart; ++iPart) {
62  MCParticle& mcPart = *mcPartCollection[iPart];
63  if (mcPart.isVirtual()) nVirtualPart++;
64  }
65  }
66 
67  m_fileStream << format("%10d%10d\n") % eventMetaDataPtr->getEvent() % (nPart - nVirtualPart);
68 
69  for (int iPart = 0; iPart < nPart; ++iPart) {
70  MCParticle& mcPart = *mcPartCollection[iPart];
71  if (!m_storeVirtualParticles && mcPart.isVirtual()) continue;
72 
73  TVector3 mom = mcPart.getMomentum();
74  if (m_mirrorPz) mom.SetZ(-1.0 * mom.Z());
75 
76  if (m_fullFormat) {
77  int isthep = 1;
78  if (mcPart.getFirstDaughter() > 0) isthep = 2;
79  if (mcPart.isInitial()) isthep = 2;
80 
81  int motherIndex = 0;
82  if (mcPart.getMother() != NULL) motherIndex = mcPart.getMother()->getIndex();
83 
84  m_fileStream << format("%5i%12i%10i%10i%10i%10i") % isthep % mcPart.getPDG() % motherIndex % motherIndex % mcPart.getFirstDaughter()
85  % mcPart.getLastDaughter();
86  m_fileStream << format("%15.6f%15.6f%15.6f%15.6f%15.6f") % mom.X() % mom.Y() % mom.Z() % mcPart.getEnergy() % mcPart.getMass();
87  m_fileStream << format("%15.6f%15.6f%15.6f%15.6f\n") % mcPart.getVertex().X() % mcPart.getVertex().Y() % mcPart.getVertex().Z() %
88  mcPart.getProductionTime();
89  } else {
90  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()
91  % mcPart.getPDG();
92  }
93  }
94 }
95 
96 
97 void HepevtOutputModule::terminate()
98 {
99  m_fileStream.close();
100 }
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
Belle2::HepevtOutputModule
The HepevtOutput module.
Definition: HepevtOutputModule.h:39
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
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