/
PHG4BackwardHcalSteppingAction.cc
303 lines (266 loc) · 10 KB
/
PHG4BackwardHcalSteppingAction.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
#include "PHG4BackwardHcalSteppingAction.h"
#include "PHG4BackwardHcalDetector.h"
#include <phparameter/PHParameters.h>
#include <g4main/PHG4Hit.h>
#include <g4main/PHG4HitContainer.h>
#include <g4main/PHG4Hitv1.h>
#include <g4main/PHG4Shower.h>
#include <g4main/PHG4SteppingAction.h> // for PHG4SteppingAction
#include <g4main/PHG4TrackUserInfoV1.h>
#include <phool/getClass.h>
#include <Geant4/G4IonisParamMat.hh> // for G4IonisParamMat
#include <Geant4/G4Material.hh> // for G4Material
#include <Geant4/G4MaterialCutsCouple.hh>
#include <Geant4/G4ParticleDefinition.hh> // for G4ParticleDefinition
#include <Geant4/G4ReferenceCountedHandle.hh> // for G4ReferenceCountedHandle
#include <Geant4/G4Step.hh>
#include <Geant4/G4StepPoint.hh> // for G4StepPoint
#include <Geant4/G4StepStatus.hh> // for fGeomBoundary, fAtRest...
#include <Geant4/G4SystemOfUnits.hh>
#include <Geant4/G4ThreeVector.hh> // for G4ThreeVector
#include <Geant4/G4TouchableHandle.hh> // for G4TouchableHandle
#include <Geant4/G4Track.hh> // for G4Track
#include <Geant4/G4TrackStatus.hh> // for fStopAndKill
#include <Geant4/G4Types.hh> // for G4double
#include <Geant4/G4VPhysicalVolume.hh> // for G4VPhysicalVolume
#include <Geant4/G4VTouchable.hh> // for G4VTouchable
#include <Geant4/G4VUserTrackInformation.hh> // for G4VUserTrackInformation
#include <iostream>
#include <string> // for basic_string, operator+
class PHCompositeNode;
//____________________________________________________________________________..
PHG4BackwardHcalSteppingAction::PHG4BackwardHcalSteppingAction(PHG4BackwardHcalDetector* detector, const PHParameters* parameters)
: PHG4SteppingAction(detector->GetName())
, m_Detector(detector)
, m_ActiveFlag(parameters->get_int_param("active"))
, m_AbsorberTruthFlag(parameters->get_int_param("absorberactive"))
, m_BlackHoleFlag(parameters->get_int_param("blackhole"))
{
}
PHG4BackwardHcalSteppingAction::~PHG4BackwardHcalSteppingAction()
{
// 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;
}
//____________________________________________________________________________..
bool PHG4BackwardHcalSteppingAction::UserSteppingAction(const G4Step* aStep, bool)
{
G4TouchableHandle touch = aStep->GetPreStepPoint()->GetTouchableHandle();
G4VPhysicalVolume* volume = touch->GetVolume();
// m_Detector->IsInBackwardHcal(volume)
// returns
// 0 is outside of Forward HCAL
// 1 is inside scintillator
// -1 is inside absorber (dead material)
int whichactive = m_Detector->IsInBackwardHcal(volume);
if (!whichactive)
{
return false;
}
int layer_id = m_Detector->get_Layer();
unsigned int icopy = touch->GetVolume(1)->GetCopyNo();
int idx_j = icopy >> 16;
int idx_k = icopy & 0xFFFF;
/* Get energy deposited by this step */
double edep = aStep->GetTotalEnergyDeposit() / GeV;
double eion = (aStep->GetTotalEnergyDeposit() - aStep->GetNonIonizingEnergyDeposit()) / GeV;
/* Get pointer to associated Geant4 track */
const G4Track* aTrack = aStep->GetTrack();
// if this block 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);
}
/* Make sure we are in a volume */
if (m_ActiveFlag)
{
/* Check if particle is 'geantino' */
bool geantino = false;
double light_yield = 0;
if (aTrack->GetParticleDefinition()->GetPDGEncoding() == 0 &&
aTrack->GetParticleDefinition()->GetParticleName().find("geantino") != std::string::npos)
{
geantino = true;
}
/* Get Geant4 pre- and post-step points */
G4StepPoint* prePoint = aStep->GetPreStepPoint();
G4StepPoint* postPoint = aStep->GetPostStepPoint();
switch (prePoint->GetStepStatus())
{
case fGeomBoundary:
case fUndefined:
if (!m_Hit)
{
m_Hit = new PHG4Hitv1();
}
/* Set hit location (space point) */
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);
/* Set hit time */
m_Hit->set_t(0, prePoint->GetGlobalTime() / nanosecond);
// set the tower index
m_Hit->set_index_j(idx_j);
m_Hit->set_index_k(idx_k);
//set the track ID
m_Hit->set_trkid(aTrack->GetTrackID());
/* set intial energy deposit */
m_Hit->set_edep(0);
/* Set hit location (tower index) */
/* Now add the hit to the hit collection */
if (whichactive > 0)
{
m_Hit->set_eion(0);
m_SaveHitContainer = m_HitContainer;
m_Hit->set_light_yield(0);
}
else
{
m_SaveHitContainer = m_AbsorberHitContainer;
}
if (G4VUserTrackInformation* p = aTrack->GetUserInformation())
{
if (PHG4TrackUserInfoV1* pp = dynamic_cast<PHG4TrackUserInfoV1*>(p))
{
m_Hit->set_trkid(pp->GetUserTrackId());
m_Hit->set_shower_id(pp->GetShower()->get_id());
m_SaveShower = pp->GetShower();
}
}
break;
default:
break;
}
if (whichactive > 0)
{
light_yield = GetVisibleEnergyDeposition(aStep); // for scintillator only, calculate light yields
static bool once = true;
if (once && edep > 0)
{
once = false;
if (Verbosity() > 0)
{
std::cout << "PHG4BackwardHcalSteppingAction::UserSteppingAction::"
//
<< m_Detector->GetName() << " - "
<< " use scintillating light model at each Geant4 steps. "
<< "First step: "
<< "Material = "
<< aTrack->GetMaterialCutsCouple()->GetMaterial()->GetName()
<< ", "
<< "Birk Constant = "
<< aTrack->GetMaterialCutsCouple()->GetMaterial()->GetIonisation()->GetBirksConstant()
<< ","
<< "edep = " << edep << ", "
<< "eion = " << eion
<< ", "
<< "light_yield = " << light_yield << std::endl;
}
}
}
/* Update exit values- will be overwritten with every step until
* we leave the volume or the particle ceases to exist */
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);
/* sum up the energy to get total deposited */
m_Hit->set_edep(m_Hit->get_edep() + edep);
if (whichactive > 0)
{
m_Hit->set_eion(m_Hit->get_eion() + eion);
m_Hit->set_light_yield(m_Hit->get_light_yield() + light_yield);
}
if (geantino)
{
m_Hit->set_edep(-1); // only energy=0 g4hits get dropped, this way geantinos survive the g4hit compression
if (whichactive > 0)
{
m_Hit->set_eion(-1);
m_Hit->set_light_yield(-1);
}
}
if (edep > 0 && (whichactive > 0 || m_AbsorberTruthFlag > 0))
{
if (G4VUserTrackInformation* p = aTrack->GetUserInformation())
{
if (PHG4TrackUserInfoV1* pp = dynamic_cast<PHG4TrackUserInfoV1*>(p))
{
pp->SetKeep(1); // we want to keep the track
}
}
}
// 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
// (not sure if this will ever be the case)
// 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 -1 for geantino)
if (m_Hit->get_edep())
{
m_SaveHitContainer->AddHit(layer_id, m_Hit);
if (m_SaveShower)
{
m_SaveShower->add_g4hit_id(m_SaveHitContainer->GetID(), m_Hit->get_hit_id());
}
// 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;
}
else
{
return false;
}
}
//____________________________________________________________________________..
void PHG4BackwardHcalSteppingAction::SetInterfacePointers(PHCompositeNode* topNode)
{
std::string hitnodename;
std::string absorbernodename;
if (m_Detector->SuperDetector() != "NONE")
{
hitnodename = "G4HIT_" + m_Detector->SuperDetector();
absorbernodename = "G4HIT_ABSORBER_" + m_Detector->SuperDetector();
}
else
{
hitnodename = "G4HIT_" + m_Detector->GetName();
absorbernodename = "G4HIT_ABSORBER_" + m_Detector->GetName();
}
//now look for the map and grab a pointer to it.
m_HitContainer = findNode::getClass<PHG4HitContainer>(topNode, hitnodename);
m_AbsorberHitContainer = findNode::getClass<PHG4HitContainer>(topNode, absorbernodename);
// if we do not find the node it's messed up.
if (!m_HitContainer)
{
std::cout << "PHG4BackwardHcalSteppingAction::SetTopNode - unable to find " << hitnodename << std::endl;
}
if (!m_AbsorberHitContainer)
{
if (Verbosity() > 0)
{
std::cout << "PHG4BackwardHcalSteppingAction::SetTopNode - unable to find " << absorbernodename << std::endl;
}
}
}