-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathOpticsEvent.cc
More file actions
179 lines (148 loc) · 4.65 KB
/
OpticsEvent.cc
File metadata and controls
179 lines (148 loc) · 4.65 KB
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
#include "OpticsEvent.hh"
#include <DD4hep/InstanceCount.h>
#include <DDG4/Factories.h>
#include <DDG4/Geant4Data.h>
#include <DDG4/Geant4HitCollection.h>
#include <DDG4/Geant4SensDetAction.h>
#include <DDG4/Geant4Context.h>
#include <G4Event.hh>
#include <G4CXOpticks.hh>
#include <SEvt.hh>
#include <SComp.h>
#include <sphoton.h>
#include <NP.hh>
#include <NPFold.h>
namespace ddeicopticks
{
//---------------------------------------------------------------------------//
OpticsEvent::OpticsEvent(dd4hep::sim::Geant4Context* ctxt,
std::string const& name)
: dd4hep::sim::Geant4EventAction(ctxt, name)
{
dd4hep::InstanceCount::increment(this);
declareProperty("Verbose", verbose_);
}
//---------------------------------------------------------------------------//
OpticsEvent::~OpticsEvent()
{
dd4hep::InstanceCount::decrement(this);
}
//---------------------------------------------------------------------------//
void OpticsEvent::begin(G4Event const* event)
{
int eventID = event->GetEventID();
if (verbose_ > 0)
{
info("OpticsEvent::begin -- event #%d", eventID);
}
SEvt::CreateOrReuse_EGPU();
SEvt* sev = SEvt::Get_EGPU();
if (sev)
{
sev->beginOfEvent(eventID);
}
}
//---------------------------------------------------------------------------//
void OpticsEvent::end(G4Event const* event)
{
int eventID = event->GetEventID();
G4CXOpticks* gx = G4CXOpticks::Get();
if (!gx)
{
error("OpticsEvent::end -- G4CXOpticks not initialized");
return;
}
SEvt* sev = SEvt::Get_EGPU();
if (!sev)
{
error("OpticsEvent::end -- no EGPU SEvt instance");
return;
}
int64_t num_genstep = sev->getNumGenstepFromGenstep();
int64_t num_photon = sev->getNumPhotonFromGenstep();
if (verbose_ > 0 || num_genstep > 0)
{
info("Event #%d: %lld gensteps, %lld photons to simulate",
eventID,
static_cast<long long>(num_genstep),
static_cast<long long>(num_photon));
}
if (num_genstep > 0)
{
gx->simulate(eventID, /*reset=*/false);
unsigned num_hit = sev->getNumHit();
info("Event #%d: %u hits from GPU", eventID, num_hit);
// Inject GPU hits into DD4hep sensitive detector hit collections
if (num_hit > 0)
{
injectHits(event, sev, num_hit);
}
sev->endOfEvent(eventID);
gx->reset(eventID);
}
else
{
if (verbose_ > 0)
info("Event #%d: no gensteps, skipping GPU simulation", eventID);
sev->endOfEvent(eventID);
}
}
//---------------------------------------------------------------------------//
void OpticsEvent::injectHits(G4Event const* event,
SEvt* sev,
unsigned num_hit)
{
using dd4hep::sim::Geant4SensDetSequences;
using dd4hep::sim::Geant4HitCollection;
using dd4hep::sim::Geant4Tracker;
int eventID = event->GetEventID();
Geant4SensDetSequences& sens = context()->sensitiveActions();
auto const& seqs = sens.sequences();
if (seqs.empty())
{
warning("Event #%d: no sensitive detectors registered -- "
"call setupTracker() in steering script", eventID);
return;
}
for (auto const& [det_name, seq] : seqs)
{
Geant4HitCollection* coll = seq->collection(0);
if (!coll)
continue;
for (unsigned i = 0; i < num_hit; i++)
{
sphoton ph;
sev->getHit(ph, i);
coll->add(createTrackerHit(ph));
}
info("Event #%d: injected %u hits into '%s' collection",
eventID, num_hit, det_name.c_str());
}
}
//---------------------------------------------------------------------------//
dd4hep::sim::Geant4Tracker::Hit*
OpticsEvent::createTrackerHit(sphoton const& ph)
{
using dd4hep::sim::Geant4Tracker;
auto* hit = new Geant4Tracker::Hit();
hit->position = {ph.pos.x, ph.pos.y, ph.pos.z};
hit->momentum = {ph.mom.x, ph.mom.y, ph.mom.z};
hit->length = ph.wavelength;
hit->energyDeposit = 0;
hit->cellID = ph.identity;
hit->truth.trackID = ph.index;
hit->truth.pdgID = 0;
hit->truth.deposit = 0;
hit->truth.time = ph.time;
hit->truth.length = ph.wavelength;
hit->truth.x = ph.pos.x;
hit->truth.y = ph.pos.y;
hit->truth.z = ph.pos.z;
hit->truth.px = ph.mom.x;
hit->truth.py = ph.mom.y;
hit->truth.pz = ph.mom.z;
return hit;
}
//---------------------------------------------------------------------------//
} // namespace ddeicopticks
DECLARE_GEANT4ACTION_NS(ddeicopticks, OpticsEvent)