Belle II Software development
EventAction.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 <simulation/kernel/EventAction.h>
10#include <framework/core/Environment.h>
11#include <framework/datastore/RelationArray.h>
12#include <framework/logging/Logger.h>
13#include <mdst/dataobjects/MCParticleGraph.h>
14#include <simulation/kernel/SensitiveDetectorBase.h>
15
16#include <string>
17
18using namespace std;
19using namespace Belle2;
20using namespace Belle2::Simulation;
21
22
23EventAction::EventAction(const std::string& mcCollectionName, MCParticleGraph& mcParticleGraph):
24 G4UserEventAction(), m_mcCollectionName(mcCollectionName), m_mcParticleGraph(mcParticleGraph)
25{
26 if (false) {
27 G4Event* event;
28 BeginOfEventAction(event);
29 EndOfEventAction(event);
30 }
33 m_VREventStream = new std::ofstream;
34}
35
37{
38 if (m_writeSimSteps) {
39 // Just in case the file is still opened...
40 if (m_VREventStream->is_open())
41 m_VREventStream->close();
42 delete m_VREventStream;
43 }
44}
45
47{
48 //Enable recording of Hits
50 if (m_writeSimSteps) {
51 // Open a new output file for the VR event-history steps
52 if (m_VREventStream->is_open())
53 m_VREventStream->close();
54 if (not m_evtMetaData.isValid())
55 B2FATAL("EventMetaData is not valid.");
56 int eventNumber = m_evtMetaData->getEvent();
57 std::string filename = boost::str(boost::format("event%1%.csv") % eventNumber);
58 m_VREventStream->open(filename);
59 B2INFO("Opened VR event-history file " << filename);
60 }
61}
62
63
65{
66 //Disable recording of Hits
68
69 //Create the final MCParticle list and update the indices of the MCParticle graph particles.
71
72 //Build a TrackID to MCParticle index list
73 vector<std::pair<unsigned int, bool> > indices;
74 indices.resize(m_mcParticleGraph.size() + 1);
75 for (unsigned int iParticle = 0; iParticle < m_mcParticleGraph.size(); ++iParticle) {
76 MCParticleGraph::GraphParticle& currParticle = m_mcParticleGraph[iParticle];
77 //assert(currParticle.getTrackID()<indices.size());
78 indices[currParticle.getTrackID()] = std::make_pair(currParticle.getIndex() - 1, currParticle.getIgnore());
79 }
80 RelationArray::ReplaceVec<> indexReplacement(indices);
81
82 //Update all registered MCParticle Relations and replace the TrackID by the final MCParticle id
83 const std::map<std::string, RelationArray::EConsolidationAction>& relations = SensitiveDetectorBase::getMCParticleRelations();
84 for (std::map<std::string, RelationArray::EConsolidationAction>::const_iterator it = relations.begin(); it != relations.end();
85 ++it) {
86 RelationArray mcPartRelation(it->first);
87 if (mcPartRelation) mcPartRelation.consolidate(indexReplacement, RelationArray::Identity(), it->second);
88 }
89
90 if (m_writeSimSteps) {
91 // Close the VR event-history file
92 if (m_VREventStream->is_open())
93 m_VREventStream->close();
94 }
95}
bool getWriteSimSteps() const
Get the flag for writing the simulation steps into an output csv file.
Definition: Environment.h:219
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
Class to represent Particle data in graph.
int getTrackID() const
Returns the track ID assigned to this MCParticle.
bool getIgnore() const
Get the ignore flag.
Class to build, validate and sort a particle decay chain.
@ c_clearParticles
Clear the particle list before adding the graph.
size_t size() const
Return the number of particles in the graph.
void generateList(const std::string &name="", int options=c_setNothing)
Generates the MCParticle list and stores it in the StoreArray with the given name.
int getIndex() const
Get 1-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:230
Struct to replace indices based on a sequential container.
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:62
void consolidate()
Consolidate Relation Elements.
std::string m_mcCollectionName
The name of the MCParticle collection to which the MCParticles should be written.
Definition: EventAction.h:72
std::ofstream * m_VREventStream
Output stream for writing each step of event's history for the Virtual Reality.
Definition: EventAction.h:78
MCParticleGraph & m_mcParticleGraph
Reference to the MCParticle graph which is converted to a MCParticle list by this class.
Definition: EventAction.h:73
bool m_writeSimSteps
Flag for writing out the simulation steps.
Definition: EventAction.h:77
virtual ~EventAction()
Destructor.
Definition: EventAction.cc:36
StoreObjPtr< EventMetaData > m_evtMetaData
Event metadata.
Definition: EventAction.h:79
EventAction(const std::string &mcCollectionName, MCParticleGraph &mcParticleGraph)
Constructor.
Definition: EventAction.cc:23
void EndOfEventAction(const G4Event *event)
This method is invoked at the very end of event processing.
Definition: EventAction.cc:64
void BeginOfEventAction(const G4Event *event)
This method is invoked before converting the primary particles to G4Track objects.
Definition: EventAction.cc:46
static void setActive(bool activeStatus)
Enable/Disable all Sensitive Detectors.
static const std::map< std::string, RelationArray::EConsolidationAction > & getMCParticleRelations()
Return a list of all registered Relations with MCParticles.
Abstract base class for different kinds of events.
STL namespace.
Struct for identity transformation on indices.
Definition: RelationArray.h:94