/
AllSiliconTrackerSteppingAction.cc
305 lines (289 loc) · 12 KB
/
AllSiliconTrackerSteppingAction.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "AllSiliconTrackerSteppingAction.h"
#include "AllSiliconTrackerDetector.h"
#include <phparameter/PHParameters.h>
#include <g4detectors/PHG4StepStatusDecode.h>
#include <g4main/PHG4Hit.h>
#include <g4main/PHG4HitContainer.h>
#include <g4main/PHG4Hitv1.h>
#include <g4main/PHG4Shower.h>
#include <g4main/PHG4SteppingAction.h>
#include <g4main/PHG4TrackUserInfoV1.h>
#include <phool/getClass.h>
#include <TSystem.h>
#include <Geant4/G4ParticleDefinition.hh>
#include <Geant4/G4ReferenceCountedHandle.hh>
#include <Geant4/G4Step.hh>
#include <Geant4/G4StepPoint.hh>
#include <Geant4/G4StepStatus.hh>
#include <Geant4/G4String.hh>
#include <Geant4/G4SystemOfUnits.hh>
#include <Geant4/G4ThreeVector.hh>
#include <Geant4/G4TouchableHandle.hh>
#include <Geant4/G4Track.hh>
#include <Geant4/G4TrackStatus.hh>
#include <Geant4/G4Types.hh>
#include <Geant4/G4VPhysicalVolume.hh>
#include <Geant4/G4VTouchable.hh>
#include <Geant4/G4VUserTrackInformation.hh>
#include <cmath>
#include <iostream>
#include <string>
class PHCompositeNode;
using namespace std;
//____________________________________________________________________________..
AllSiliconTrackerSteppingAction::AllSiliconTrackerSteppingAction(AllSiliconTrackerDetector *detector, const PHParameters *parameters)
: PHG4SteppingAction(detector->GetName())
, m_Detector(detector)
, m_Params(parameters)
, m_HitContainer(nullptr)
, m_AbsorberHitContainer(nullptr)
, m_Hit(nullptr)
, m_SaveHitContainer(nullptr)
, m_SaveVolPre(nullptr)
, m_SaveVolPost(nullptr)
, m_SaveTrackId(-1)
, m_SavePreStepStatus(-1)
, m_SavePostStepStatus(-1)
, m_BlackHoleFlag(m_Params->get_int_param("blackhole"))
, m_EdepSum(0)
{
}
//____________________________________________________________________________..
AllSiliconTrackerSteppingAction::~AllSiliconTrackerSteppingAction()
{
// if the last hit was a zero energie deposit hit, it is just reset
// and the memory is still allocated, so we need to delete it here
// if the last hit was saved, hit is a nullptr pointer which are
// legal to delete (it results in a no operation)
delete m_Hit;
}
//____________________________________________________________________________..
// This is the implementation of the G4 UserSteppingAction
bool AllSiliconTrackerSteppingAction::UserSteppingAction(const G4Step *aStep, bool was_used)
{
G4TouchableHandle touch = aStep->GetPreStepPoint()->GetTouchableHandle();
G4TouchableHandle touchpost = aStep->GetPostStepPoint()->GetTouchableHandle();
// get volume of the current step
G4VPhysicalVolume *volume = touch->GetVolume();
// IsInDetector(volume) returns
// == 0 outside of detector
// > 0 for hits in active volume
// < 0 for hits in passive material
int whichactive = m_Detector->IsInDetector(volume);
if (!whichactive)
{
return false;
}
// collect energy and track length step by step
G4double edep = aStep->GetTotalEnergyDeposit() / GeV;
const G4Track *aTrack = aStep->GetTrack();
// if this detector stops everything, just put all kinetic energy into edep
if (m_BlackHoleFlag)
{
edep = aTrack->GetKineticEnergy() / GeV;
G4Track *killtrack = const_cast<G4Track *>(aTrack);
killtrack->SetTrackStatus(fStopAndKill);
}
int detector_id = m_Detector->get_detid(volume, whichactive); // the detector id is coded into the IsInDetector return
// cout << "Name: " << volume->GetName() << endl;
// cout << "det id: " << whichactive << endl;
bool geantino = false;
// the check for the pdg code speeds things up, I do not want to make
// an expensive string compare for every track when we know
// geantino or chargedgeantino has pid=0
if (aTrack->GetParticleDefinition()->GetPDGEncoding() == 0 &&
aTrack->GetParticleDefinition()->GetParticleName().find("geantino") !=
string::npos) // this also accounts for "chargedgeantino"
{
geantino = true;
}
G4StepPoint *prePoint = aStep->GetPreStepPoint();
G4StepPoint *postPoint = aStep->GetPostStepPoint();
// Here we have to decide if we need to create a new hit. Normally this should
// only be neccessary if a G4 Track enters a new volume or is freshly created
// For this we look at the step status of the prePoint (beginning of the G4 Step).
// This should be either fGeomBoundary (G4 Track crosses into volume) or
// fUndefined (G4 Track newly created)
// Sadly over the years with different G4 versions we have observed cases where
// G4 produces "impossible hits" which we try to catch here
// These errors were always rare and it is not clear if they still exist but we
// still check for them for safety. We can reproduce G4 runs identically (if given
// the sequence of random number seeds you find in the log), the printouts help
// us giving the G4 support information about those failures
//
switch (prePoint->GetStepStatus())
{
case fPostStepDoItProc:
if (m_SavePostStepStatus != fGeomBoundary)
{
// this is the okay case, fPostStepDoItProc called in a volume, not first thing inside
// a new volume, just proceed here
break;
}
else
{
// this is an impossible G4 Step print out diagnostic to help debug, not sure if
// this is still with us
cout << GetName() << ": New Hit for " << endl;
cout << "prestep status: "
<< PHG4StepStatusDecode::GetStepStatus(prePoint->GetStepStatus())
<< ", poststep status: "
<< PHG4StepStatusDecode::GetStepStatus(postPoint->GetStepStatus())
<< ", last pre step status: "
<< PHG4StepStatusDecode::GetStepStatus(m_SavePreStepStatus)
<< ", last post step status: "
<< PHG4StepStatusDecode::GetStepStatus(m_SavePostStepStatus) << endl;
cout << "last track: " << m_SaveTrackId
<< ", current trackid: " << aTrack->GetTrackID() << endl;
cout << "phys pre vol: " << volume->GetName()
<< " post vol : " << touchpost->GetVolume()->GetName() << endl;
cout << " previous phys pre vol: " << m_SaveVolPre->GetName()
<< " previous phys post vol: " << m_SaveVolPost->GetName() << endl;
}
// These are the normal cases
case fGeomBoundary:
case fUndefined:
if (!m_Hit)
{
m_Hit = new PHG4Hitv1();
}
m_Hit->set_layer(detector_id);
// here we set the entrance values in cm
m_Hit->set_x(0, prePoint->GetPosition().x() / cm);
m_Hit->set_y(0, prePoint->GetPosition().y() / cm);
m_Hit->set_z(0, prePoint->GetPosition().z() / cm);
// time in ns
m_Hit->set_t(0, prePoint->GetGlobalTime() / nanosecond);
// set the track ID
m_Hit->set_trkid(aTrack->GetTrackID());
m_SaveTrackId = aTrack->GetTrackID();
// set the initial energy deposit
m_EdepSum = 0;
// implement your own here://
// add the properties you are interested in via set_XXX methods
// you can find existing set methods in $OFFLINE_MAIN/include/g4main/PHG4Hit.h
// this is initialization of your value. This is not needed you can just set the final
// value at the last step in this volume later one
if (whichactive > 0)
{
m_SaveHitContainer = m_Detector->get_hitcontainer(detector_id);
}
else
{
// all absorber hits go into one node
m_SaveHitContainer = m_Detector->get_hitcontainer(-1);
}
// this is for the tracking of the truth info
if (G4VUserTrackInformation *p = aTrack->GetUserInformation())
{
if (PHG4TrackUserInfoV1 *pp = dynamic_cast<PHG4TrackUserInfoV1 *>(p))
{
m_Hit->set_trkid(pp->GetUserTrackId());
pp->GetShower()->add_g4hit_id(m_SaveHitContainer->GetID(), m_Hit->get_hit_id());
}
}
break;
default:
break;
}
// This section is called for every step
// some sanity checks for inconsistencies (aka bugs) we have seen over the years
// check if this hit was created, if not print out last post step status
if (!m_Hit || !isfinite(m_Hit->get_x(0)))
{
cout << GetName() << ": hit was not created" << endl;
cout << "prestep status: "
<< PHG4StepStatusDecode::GetStepStatus(prePoint->GetStepStatus())
<< ", poststep status: "
<< PHG4StepStatusDecode::GetStepStatus(postPoint->GetStepStatus())
<< ", last pre step status: "
<< PHG4StepStatusDecode::GetStepStatus(m_SavePreStepStatus)
<< ", last post step status: "
<< PHG4StepStatusDecode::GetStepStatus(m_SavePostStepStatus) << endl;
cout << "last track: " << m_SaveTrackId
<< ", current trackid: " << aTrack->GetTrackID() << endl;
cout << "phys pre vol: " << volume->GetName()
<< " post vol : " << touchpost->GetVolume()->GetName() << endl;
cout << " previous phys pre vol: " << m_SaveVolPre->GetName()
<< " previous phys post vol: " << m_SaveVolPost->GetName() << endl;
// This is fatal - a hit from nowhere. This needs to be looked at and fixed
gSystem->Exit(1);
}
// check if track id matches the initial one when the hit was created
if (aTrack->GetTrackID() != m_SaveTrackId)
{
cout << GetName() << ": hits do not belong to the same track" << endl;
cout << "saved track: " << m_SaveTrackId
<< ", current trackid: " << aTrack->GetTrackID()
<< ", prestep status: " << prePoint->GetStepStatus()
<< ", previous post step status: " << m_SavePostStepStatus << endl;
// This is fatal - a hit from nowhere. This needs to be looked at and fixed
gSystem->Exit(1);
}
// We need to cache a few things from one step to the next
// to identify impossible hits and subsequent debugging printout
m_SavePreStepStatus = prePoint->GetStepStatus();
m_SavePostStepStatus = postPoint->GetStepStatus();
m_SaveVolPre = volume;
m_SaveVolPost = touchpost->GetVolume();
// here we just update the exit values, it will be overwritten
// for every step until we leave the volume or the particle
// ceases to exist
// sum up the energy to get total deposited
m_EdepSum += edep;
// if any of these conditions is true this is the last step in
// this volume and we need to save the hit
// postPoint->GetStepStatus() == fGeomBoundary: track leaves this volume
// postPoint->GetStepStatus() == fWorldBoundary: track leaves this world
// (happens when your detector goes outside world volume)
// postPoint->GetStepStatus() == fAtRestDoItProc: track stops (typically
// aTrack->GetTrackStatus() == fStopAndKill is also set)
// aTrack->GetTrackStatus() == fStopAndKill: track ends
if (postPoint->GetStepStatus() == fGeomBoundary ||
postPoint->GetStepStatus() == fWorldBoundary ||
postPoint->GetStepStatus() == fAtRestDoItProc ||
aTrack->GetTrackStatus() == fStopAndKill)
{
// save only hits with energy deposit (or geantino)
if (m_EdepSum > 0 || geantino)
{
// update values at exit coordinates and set keep flag
// of track to keep
m_Hit->set_x(1, postPoint->GetPosition().x() / cm);
m_Hit->set_y(1, postPoint->GetPosition().y() / cm);
m_Hit->set_z(1, postPoint->GetPosition().z() / cm);
m_Hit->set_t(1, postPoint->GetGlobalTime() / nanosecond);
if (G4VUserTrackInformation *p = aTrack->GetUserInformation())
{
if (PHG4TrackUserInfoV1 *pp = dynamic_cast<PHG4TrackUserInfoV1 *>(p))
{
pp->SetKeep(1); // we want to keep the track
}
}
if (geantino)
{
//implement your own here://
// if you want to do something special for geantinos (normally you do not)
m_Hit->set_edep(-1); // only energy=0 g4hits get dropped, this way
// geantinos survive the g4hit compression
}
else
{
m_Hit->set_edep(m_EdepSum);
}
m_SaveHitContainer->AddHit(detector_id, m_Hit);
// ownership has been transferred to container, set to null
// so we will create a new hit for the next track
m_Hit = nullptr;
}
else
{
// if this hit has no energy deposit, just reset it for reuse
// this means we have to delete it in the dtor. If this was
// the last hit we processed the memory is still allocated
m_Hit->Reset();
}
}
// return true to indicate the hit was used
return true;
}