Belle II Software  release-05-02-19
SensitiveDetector.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter, Igal Jaegle, Jerome Baudot *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <beast/plume/simulation/SensitiveDetector.h>
12 #include <beast/plume/dataobjects/PlumeSimHit.h>
13 #include <beast/plume/dataobjects/PlumeHit.h>
14 
15 #include <framework/datastore/StoreArray.h>
16 #include <framework/datastore/RelationArray.h>
17 
18 #include <G4Track.hh>
19 #include <G4Step.hh>
20 
21 #include <iostream>
22 #define _USE_MATH_DEFINES
23 #include <cmath>
24 using namespace std;
25 
26 
27 namespace Belle2 {
33  namespace plume {
34 
35  SensitiveDetector::SensitiveDetector():
36  Simulation::SensitiveDetectorBase("PlumeSensitiveDetector", Const::invalidDetector)
37  {
38  current_trackID = 0;
42  current_pdgID = 0;
43  current_sensorID = 0;
44  current_posIN_u = 0.;
45  current_posIN_v = 0.;
46  current_posIN_w = 0.;
47  current_posIN_x = 0.;
48  current_posIN_y = 0.;
49  current_posIN_z = 0.;
50  current_posOUT_u = 0.;
51  current_posOUT_v = 0.;
52  current_posOUT_w = 0.;
53  current_posOUT_x = 0.;
54  current_posOUT_y = 0.;
55  current_posOUT_z = 0.;
56  current_momentum_x = 0.;
57  current_momentum_y = 0.;
58  current_momentum_z = 0.;
59  current_energyDep = 0.;
60  current_nielDep = 0.;
61  current_thetaAngle = 0.; // local sensor frame
62  current_phiAngle = 0.; // local sensor frame
63  current_globalTime = 0.;
64 
65  //Make sure all collections are registered
66  StoreArray<MCParticle> mcParticles;
68  StoreArray<PlumeHit> plumeHits;
69 
70  RelationArray relMCSimHit(mcParticles, simHits);
71  RelationArray relMCplumeHit(mcParticles, plumeHits);
72 
73  //Register all collections we want to modify and require those we want to use
74  mcParticles.registerInDataStore();
75  simHits.registerInDataStore();
76  plumeHits.registerInDataStore();
77  relMCSimHit.registerInDataStore();
78  relMCplumeHit.registerInDataStore();
79 
80  //Register the Relation so that the TrackIDs get replaced by the actual
81  //MCParticle indices after simulating the events. This is needed as
82  //secondary particles might not be stored so everything relating to those
83  //particles will be attributed to the last saved mother particle
84  registerMCParticleRelation(relMCSimHit);
85  registerMCParticleRelation(relMCplumeHit);
86  }
87 
89  {
90 
91  }
92 
93 
94  bool SensitiveDetector::step(G4Step* step, G4TouchableHistory*)
95  {
96 
97  //Get track information, and pre- and post-step
98  const G4Track& track = *step->GetTrack();
99  const G4StepPoint& preStepPoint = *step->GetPreStepPoint();
100  const G4StepPoint& postStepPoint = *step->GetPostStepPoint();
101 
102 
103  // If new track, store general information from this first step
104  if (current_trackID != track.GetTrackID()) { // if new track
105  current_trackID = track.GetTrackID();
106  current_trackVertex_x = track.GetVertexPosition().x();
107  current_trackVertex_y = track.GetVertexPosition().y();
108  current_trackVertex_z = track.GetVertexPosition().z();
109  current_pdgID = track.GetDefinition()->GetPDGEncoding();
110  current_sensorID = preStepPoint.GetTouchableHandle()->GetReplicaNumber(1);
111 
112  // since this is first step in volume store track incidence and momentum and reset energy loss
113  G4ThreeVector preStepPointPosition = preStepPoint.GetPosition();
114  G4ThreeVector trackMomentum = track.GetMomentum();
115  current_energyDep = 0.;
116  current_nielDep = 0.;
117 
118  current_posIN_x = preStepPointPosition.x();
119  current_posIN_y = preStepPointPosition.y();
120  current_posIN_z = preStepPointPosition.z();
121  current_momentum_x = trackMomentum.x();
122  current_momentum_y = trackMomentum.y();
123  current_momentum_z = trackMomentum.z();
124 
125  // We want the track angles of incidence on the local volume and the local position
126  G4TouchableHandle theTouchable = preStepPoint.GetTouchableHandle();
127  G4ThreeVector worldDirection = preStepPoint.GetMomentumDirection();
128  G4ThreeVector localDirection = theTouchable->GetHistory()->GetTopTransform().TransformAxis(worldDirection);
129 
130  if (localDirection.z() < 0.) {
131  current_thetaAngle = M_PI - acos(-1. * localDirection.z());
132  } else {
133  current_thetaAngle = acos(localDirection.z());
134  }
135 
136 
137  if (localDirection.x() < 0. && localDirection.y() > 0.) {
138  current_phiAngle = atan(-1. * localDirection.x() / localDirection.y());
139  } else if (localDirection.x() < 0. && localDirection.y() < 0.) {
140  current_phiAngle = M_PI - atan(localDirection.x() / localDirection.y());
141  } else if (localDirection.x() > 0. && localDirection.y() < 0.) {
142  current_phiAngle = M_PI + atan(-1. * localDirection.x() / localDirection.y());
143  } else if (localDirection.x() > 0. && localDirection.y() > 0.) {
144  current_phiAngle = 2. * M_PI - atan(localDirection.x() / localDirection.y());
145  } else if (localDirection.x() < 0. && localDirection.y() == 0.) {
146  current_phiAngle = M_PI / 2.;
147  } else if (localDirection.x() > 0. && localDirection.y() == 0.) {
148  current_phiAngle = 3.* M_PI / 2.;
149  } else if (localDirection.x() == 0. && localDirection.y() > 0.) {
150  current_phiAngle = 0.;
151  } else if (localDirection.x() == 0. && localDirection.y() < 0.) {
152  current_phiAngle = M_PI;
153  }
154 
155  G4ThreeVector localINPosition = theTouchable->GetHistory()->GetTopTransform().TransformPoint(preStepPointPosition);
156  current_posIN_u = localINPosition.y();
157  current_posIN_v = - localINPosition.x();
158  current_posIN_w = localINPosition.z();
159 
160  current_globalTime = step->GetPreStepPoint()->GetGlobalTime();
161 
162  } // end if new track
163 
164  // Update information at each step
165  current_energyDep += step->GetTotalEnergyDeposit();
166  current_nielDep += step->GetNonIonizingEnergyDeposit();
167 
168  // If track leaves volume or is killed, store final step info and save simHit
169  if (track.GetNextVolume() != track.GetVolume() || track.GetTrackStatus() >= fStopAndKill) { // if last step
170 
171  G4ThreeVector postStepPointPosition = postStepPoint.GetPosition();
172 
173  current_posOUT_x = postStepPointPosition.x();
174  current_posOUT_y = postStepPointPosition.y();
175  current_posOUT_z = postStepPointPosition.z();
176  G4ThreeVector localOUTPosition = preStepPoint.GetTouchableHandle()->GetHistory()->GetTopTransform().TransformPoint(
177  postStepPointPosition);
178  current_posOUT_u = localOUTPosition.y();
179  current_posOUT_v = - localOUTPosition.x();
180  current_posOUT_w = localOUTPosition.z();
181 
182  //Get the datastore arrays
183  StoreArray<MCParticle> mcParticles;
184  StoreArray<PlumeSimHit> simHits;
185 
186  if (current_energyDep > CLHEP::eV) {
187 
188  RelationArray relMCSimHit(mcParticles, simHits);
189  PlumeSimHit* hit = simHits.appendNew(
193  current_trackVertex_x / CLHEP::mm,
194  current_trackVertex_y / CLHEP::mm,
195  current_trackVertex_z / CLHEP::mm,
196  current_energyDep / CLHEP::MeV,
197  current_nielDep / CLHEP::MeV,
198  current_posIN_x / CLHEP::mm,
199  current_posIN_y / CLHEP::mm,
200  current_posIN_z / CLHEP::mm,
201  current_posIN_u / CLHEP::mm,
202  current_posIN_v / CLHEP::mm,
203  current_posIN_w / CLHEP::mm,
204  current_posOUT_u / CLHEP::mm,
205  current_posOUT_v / CLHEP::mm,
206  current_posOUT_w / CLHEP::mm,
207  current_momentum_x / CLHEP::GeV,
208  current_momentum_y / CLHEP::GeV,
209  current_momentum_z / CLHEP::GeV,
210  current_thetaAngle / CLHEP::degree,
211  current_phiAngle / CLHEP::degree,
212  current_globalTime / CLHEP::nanosecond
213  );
214 
215  //Add Relation between SimHit and MCParticle with a weight of 1. Since
216  //the MCParticle index is not yet defined we use the trackID from Geant4
217  relMCSimHit.add(current_trackID, hit->getArrayIndex(), 1.0);
218  }
219 
220  //Reset TrackID
221  current_trackID = 0;
222 
223  } // end if last
224 
225 
226  //Ignore everything below 1eV
227  if (current_energyDep < CLHEP::eV) return false;
228 
229 
230  return true;
231  }
232 
233  } //plume namespace
235 } //Belle2 namespace
Belle2::StoreArray::appendNew
T * appendNew()
Construct a new T object at the end of the array.
Definition: StoreArray.h:256
Belle2::plume::SensitiveDetector::current_trackID
int current_trackID
track ID
Definition: SensitiveDetector.h:52
Belle2::RelationArray
Low-level class to create/modify relations between StoreArrays.
Definition: RelationArray.h:72
Belle2::plume::SensitiveDetector::current_thetaAngle
float current_thetaAngle
local (sensor ref) theta angle, out of sensor plane, in degree
Definition: SensitiveDetector.h:94
Belle2::plume::SensitiveDetector::current_posOUT_x
float current_posOUT_x
outcoming track position x in sensor ref, in mm
Definition: SensitiveDetector.h:82
Belle2::plume::SensitiveDetector::current_posIN_v
float current_posIN_v
incoming track position v in sensor ref, in mm
Definition: SensitiveDetector.h:72
Belle2::PlumeSimHit
Class PlumeSimHit - Geant4 simulated hit for the PLUME detector.
Definition: PlumeSimHit.h:37
Belle2::plume::SensitiveDetector::current_momentum_y
float current_momentum_y
incoming track momentum, y coordinates in G4 ref, in GeV
Definition: SensitiveDetector.h:90
Belle2::plume::SensitiveDetector::current_nielDep
float current_nielDep
non ionizing deposited energy
Definition: SensitiveDetector.h:62
Belle2::plume::SensitiveDetector::~SensitiveDetector
~SensitiveDetector()
Destructor.
Definition: SensitiveDetector.cc:88
Belle2::StoreAccessorBase::registerInDataStore
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Definition: StoreAccessorBase.h:54
Belle2::plume::SensitiveDetector::current_posOUT_z
float current_posOUT_z
outcoming track position z in sensor ref, in mm
Definition: SensitiveDetector.h:86
Belle2::Simulation::SensitiveDetectorBase::registerMCParticleRelation
static void registerMCParticleRelation(const std::string &name, RelationArray::EConsolidationAction ignoreAction=RelationArray::c_negativeWeight)
Register an relation involving MCParticles.
Definition: SensitiveDetectorBase.cc:24
Belle2::plume::SensitiveDetector::current_posOUT_v
float current_posOUT_v
outcoming track position v in sensor ref, in mm
Definition: SensitiveDetector.h:78
Belle2::plume::SensitiveDetector::current_posOUT_y
float current_posOUT_y
outcoming track position y in sensor ref, in mm
Definition: SensitiveDetector.h:84
Belle2::plume::SensitiveDetector::current_trackVertex_y
float current_trackVertex_y
track production vertex y coordinates in G4 ref
Definition: SensitiveDetector.h:56
Belle2::plume::SensitiveDetector::current_trackVertex_x
float current_trackVertex_x
track production vertex x coordinates in G4 ref
Definition: SensitiveDetector.h:54
Belle2::plume::SensitiveDetector::current_energyDep
float current_energyDep
deposited energy in MeV
Definition: SensitiveDetector.h:60
Belle2::plume::SensitiveDetector::current_momentum_z
float current_momentum_z
incoming track momentum, z coordinates in G4 ref, in GeV
Definition: SensitiveDetector.h:92
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::RelationArray::add
void add(index_type from, index_type to, weight_type weight=1.0)
Add a new element to the relation.
Definition: RelationArray.h:291
Belle2::plume::SensitiveDetector::current_sensorID
int current_sensorID
sensor ID
Definition: SensitiveDetector.h:50
Belle2::plume::SensitiveDetector::current_posOUT_u
float current_posOUT_u
outcoming track position u in sensor ref, in mm
Definition: SensitiveDetector.h:76
Belle2::plume::SensitiveDetector::current_pdgID
int current_pdgID
particle PDG id
Definition: SensitiveDetector.h:48
Belle2::plume::SensitiveDetector::current_momentum_x
float current_momentum_x
incoming track momentum, x coordinates in G4 ref, in GeV
Definition: SensitiveDetector.h:88
Belle2::plume::SensitiveDetector::current_trackVertex_z
float current_trackVertex_z
track production vertex z coordinates in G4 ref
Definition: SensitiveDetector.h:58
Belle2::plume::SensitiveDetector::current_posOUT_w
float current_posOUT_w
outcoming track position w in sensor ref, in mm
Definition: SensitiveDetector.h:80
Belle2::plume::SensitiveDetector::current_posIN_y
float current_posIN_y
incoming track position y in G4 ref, in mm
Definition: SensitiveDetector.h:66
Belle2::StoreArray< MCParticle >
Belle2::Const
This class provides a set of constants for the framework.
Definition: Const.h:36
Belle2::plume::SensitiveDetector::current_posIN_u
float current_posIN_u
incoming track position u in sensor ref, in mm
Definition: SensitiveDetector.h:70
Belle2::plume::SensitiveDetector::current_globalTime
float current_globalTime
global time
Definition: SensitiveDetector.h:98
Belle2::plume::SensitiveDetector::current_posIN_w
float current_posIN_w
incoming track position w in sensor ref, in mm
Definition: SensitiveDetector.h:74
Belle2::plume::SensitiveDetector::current_posIN_x
float current_posIN_x
incoming track position x in G4 ref, in mm
Definition: SensitiveDetector.h:64
Belle2::plume::SensitiveDetector::current_posIN_z
float current_posIN_z
incoming track position z in G4 ref, in mm
Definition: SensitiveDetector.h:68
Belle2::plume::SensitiveDetector::step
bool step(G4Step *step, G4TouchableHistory *) override
Step processing method.
Definition: SensitiveDetector.cc:94
Belle2::plume::SensitiveDetector::current_phiAngle
float current_phiAngle
local (sensor ref) phi angle, in sensor plane, in degree
Definition: SensitiveDetector.h:96