Belle II Software development
SensitiveDetector.h
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#pragma once
10
11#include <vxd/simulation/SensitiveDetectorBase.h>
12#include <framework/datastore/StoreArray.h>
13#include <framework/datastore/RelationArray.h>
14#include <array>
15
16#ifdef VXD_SENSITIVEDETECTOR_DEBUG
17#include <vxd/simulation/SensitiveDetectorDebugHelper.h>
18#endif
19
20namespace Belle2 {
26 namespace VXD {
27
62 template <class SimHitClass, class TrueHitClass> class SensitiveDetector: public SensitiveDetectorBase {
63 public:
66
67 private:
73 int saveTrueHit(const SensorTraversal& traversal) override;
74
82 int saveSimHit(const SensorTraversal& traversal,
83 const SensorTraversal::range& points) override;
84
92 void saveRelations(const SensorTraversal& traversal, int trueHitIndex,
93 std::vector<std::pair<unsigned int, float>> simHitIndices) override;
94
100 std::array<float, 3> vecToFloat(const G4ThreeVector& vec)
101 {
102 return std::array<float, 3> {{(float)vec.x(), (float)vec.y(), (float)vec.z()}};
103 }
104
117 };
118
119 template <class SimHitClass, class TrueHitClass>
121 VXD::SensitiveDetectorBase(sensorInfo)
122 {
123 //Register output collections.
124 //m_mcparticles.isRequired();
125 m_simhits.registerInDataStore();
126 m_truehits.registerInDataStore();
127 m_relMCSimHits.registerInDataStore();
128 m_relMCTrueHits.registerInDataStore();
129 m_relTrueSimHits.registerInDataStore();
132 }
133
134 template <class SimHitClass, class TrueHitClass>
136 {
137 //We have the full sensor traversal information, we only lack the midpoint so lets get it
138 StepInformation midPoint = findMidPoint(traversal);
139
140 //By now everything is done: just convert the position and momenta in float[3] and create a truehit
141 auto posEntry = vecToFloat(traversal.front().position);
142 auto posMidPoint = vecToFloat(midPoint.position);
143 auto posExit = vecToFloat(traversal.back().position);
144 auto momEntry = vecToFloat(traversal.front().momentum);
145 auto momMidPoint = vecToFloat(midPoint.momentum);
146 auto momExit = vecToFloat(traversal.back().momentum);
147 //And we should convert the electrons back in energy ...
148 const double energyDep = traversal.getElectrons() * Const::ehEnergy;
149
150 //create the truehit
151 int trueHitIndex = m_truehits.getEntries();
152 m_truehits.appendNew(getSensorID(), posEntry.data(), posMidPoint.data(), posExit.data(),
153 momEntry.data(), momMidPoint.data(), momExit.data(), energyDep, midPoint.time);
154#ifdef VXD_SENSITIVEDETECTOR_DEBUG
156#endif
157 return trueHitIndex;
158 }
159
160 template <class SimHitClass, class TrueHitClass>
162 const SensorTraversal::range& points)
163 {
164 //Convert position to floats here
165 auto posIn = vecToFloat(points.first->position);
166 auto posOut = vecToFloat(points.second->position);
167 auto electronProfile = simplifyEnergyDeposit(points);
168
169 //Create the simhit
170 int simHitIndex = m_simhits.getEntries();
171 SimHitClass* simhit = m_simhits.appendNew(getSensorID(), traversal.getPDGCode(), points.first->time,
172 posIn.data(), posOut.data());
173 simhit->setEnergyDeposit(electronProfile);
174#ifdef VXD_SENSITIVEDETECTOR_DEBUG
175 int startPoint = std::distance(traversal.begin(), (SensorTraversal::const_iterator)points.first);
176 int endPoint = std::distance(traversal.begin(), (SensorTraversal::const_iterator)points.second);
177 SensitiveDetectorDebugHelper::getInstance().addSimHit(simhit, startPoint, endPoint);
178#endif
179 return simHitIndex;
180 }
181
182 template <class SimHitClass, class TrueHitClass>
184 int trueHitIndex, std::vector<std::pair<unsigned int, float>> simHitIndices)
185 {
186 m_relMCSimHits.add(traversal.getTrackID(), simHitIndices.begin(), simHitIndices.end());
187 //If there is no truehit there are obviously no relations ;)
188 if (trueHitIndex >= 0) {
189 m_relMCTrueHits.add(traversal.getTrackID(), trueHitIndex, traversal.getElectrons());
190 m_relTrueSimHits.add(trueHitIndex, simHitIndices.begin(), simHitIndices.end());
191 }
192 }
193
194 } //VXD Namespace
196} //Belle2 namespace
static const double ehEnergy
Energy needed to create an electron-hole pair in Si at std.
Definition Const.h:697
Low-level class to create/modify relations between StoreArrays.
@ c_negativeWeight
Flip the sign of the weight to become negative if the original element got re-attributed.
@ c_deleteElement
Delete the whole relation element if the original element got re-attributed.
Class to keep track of the traversal of the sensitive volume for one track.
int getPDGCode() const
get PDG code of the particle
double getElectrons() const
get total number of deposited electrons so far
int getTrackID() const
get Geant4 trackID
std::pair< iterator, iterator > range
Iterator pair for a set of points.
static void registerMCParticleRelation(const std::string &name, RelationArray::EConsolidationAction ignoreAction=RelationArray::c_negativeWeight)
Register an relation involving MCParticles.
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
SensitiveDetectorBase(SensorInfoBase *info)
Constructor.
StepInformation findMidPoint(const SensorTraversal &traversal)
Find the mid-point of the track traversal.
VxdID getSensorID() const
Return the VxdID belonging to this sensitive detector.
std::vector< unsigned int > simplifyEnergyDeposit(const SensorTraversal::range &points)
Simplify the energy deposition profile using Douglas-Peuker-Algorithm We normally force a Geant4 step...
static SensitiveDetectorDebugHelper & getInstance()
Singleton class: get instance.
void addSimHit(const VXDSimHit *simhit, int startPoint, int endPoint)
Write the information normally used when creating SimHits to the entry.
void addTrueHit(const VXDTrueHit *truehit)
Write the information normally used when creating TrueHits to the entry.
int saveSimHit(const SensorTraversal &traversal, const SensorTraversal::range &points) override
Save a SimHit for a track along the given points.
std::array< float, 3 > vecToFloat(const G4ThreeVector &vec)
Convert G4ThreeVector (aka Hep3Vector) to float array to store in TrueHit/SimHit classes.
int saveTrueHit(const SensorTraversal &traversal) override
Save the actual TrueHit for a given sensor traversal information.
void saveRelations(const SensorTraversal &traversal, int trueHitIndex, std::vector< std::pair< unsigned int, float > > simHitIndices) override
Save the relations between MCParticle, TrueHit and SimHits.
SensitiveDetector(VXD::SensorInfoBase *sensorInfo)
Construct instance and take over ownership of the sensorInfo pointer.
Base class to provide Sensor Information for PXD and SVD.
Namespace to provide code needed by both Vertex Detectors, PXD and SVD, and also testbeam telescopes.
Definition GeoCache.h:33
Abstract base class for different kinds of events.
Simple struct to keep information about steps in the sensitive detector.
G4ThreeVector momentum
Step momentum.
G4ThreeVector position
Step position.
double time
timestamp of the step