-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathGeant4Output2EDM4hep.cpp
573 lines (511 loc) · 22.7 KB
/
Geant4Output2EDM4hep.cpp
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : F.Gaede, DESY
//
//==========================================================================
#ifndef DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
#define DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
/// Framework include files
#include <DD4hep/Detector.h>
#include <DDG4/EventParameters.h>
#include <DDG4/Geant4OutputAction.h>
#include <DDG4/RunParameters.h>
/// edm4hep include files
#include <edm4hep/MCParticleCollection.h>
#include <edm4hep/SimTrackerHitCollection.h>
#include <edm4hep/CaloHitContributionCollection.h>
#include <edm4hep/SimCalorimeterHitCollection.h>
/// podio include files
#include <podio/Frame.h>
#include <podio/ROOTFrameWriter.h>
#include <podio/podioVersion.h>
/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
class ComponentCast;
/// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit
namespace sim {
class Geant4ParticleMap;
/// Base class to output Geant4 event data to EDM4hep
/**
* \author F.Gaede
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class Geant4Output2EDM4hep : public Geant4OutputAction {
protected:
using writer_t = podio::ROOTFrameWriter;
using stringmap_t = std::map< std::string, std::string >;
using trackermap_t = std::map< std::string, edm4hep::SimTrackerHitCollection >;
using calorimeterpair_t = std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection >;
using calorimetermap_t = std::map< std::string, calorimeterpair_t >;
std::unique_ptr<writer_t> m_file { };
podio::Frame m_frame { };
edm4hep::MCParticleCollection m_particles { };
trackermap_t m_trackerHits;
calorimetermap_t m_calorimeterHits;
stringmap_t m_runHeader;
stringmap_t m_eventParametersInt;
stringmap_t m_eventParametersFloat;
stringmap_t m_eventParametersString;
stringmap_t m_cellIDEncodingStrings{};
std::string m_section_name { "events" };
int m_runNo { 0 };
int m_runNumberOffset { 0 };
int m_eventNo { 0 };
int m_eventNumberOffset { 0 };
bool m_filesByRun { false };
/// Data conversion interface for MC particles to EDM4hep format
void saveParticles(Geant4ParticleMap* particles);
/// Store the metadata frame with e.g. the cellID encoding strings
void saveFileMetaData();
public:
/// Standard constructor
Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam);
/// Default destructor
virtual ~Geant4Output2EDM4hep();
/// Callback to store the Geant4 run information
virtual void beginRun(const G4Run* run);
/// Callback to store the Geant4 run information
virtual void endRun(const G4Run* run);
/// Callback to store the Geant4 run information
virtual void saveRun(const G4Run* run);
/// Callback to store the Geant4 event
virtual void saveEvent( OutputContext<G4Event>& ctxt);
/// Callback to store each Geant4 hit collection
virtual void saveCollection( OutputContext<G4Event>& ctxt, G4VHitsCollection* collection);
/// Commit data at end of filling procedure
virtual void commit( OutputContext<G4Event>& ctxt);
/// begin-of-event callback - creates EDM4hep event and adds it to the event context
virtual void begin(const G4Event* event);
protected:
/// Fill event parameters in EDM4hep event
template <typename T>
void saveEventParameters(const std::map<std::string, std::string >& parameters) {
for(const auto& p : parameters) {
info("Saving event parameter: %-32s = %s", p.first.c_str(), p.second.c_str());
m_frame.putParameter(p.first, p.second);
}
}
};
template <> void EventParameters::extractParameters(podio::Frame& frame) {
for(auto const& p: this->intParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->fltParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->strParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#if podio_VERSION_MAJOR > 0 || podio_VERSION_MINOR > 16 || podio_VERSION_PATCH > 2
// This functionality is only present in podio > 0.16.2
for (auto const& p: this->dblParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#endif
}
template <> void RunParameters::extractParameters(podio::Frame& frame) {
for(auto const& p: this->intParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->fltParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->strParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#if podio_VERSION_MAJOR > 0 || podio_VERSION_MINOR > 16 || podio_VERSION_PATCH > 2
// This functionality is only present in podio > 0.16.2
for (auto const& p: this->dblParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#endif
}
} // End namespace sim
} // End namespace dd4hep
#endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
//==========================================================================
// AIDA Detector description implementation
//--------------------------------------------------------------------------
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// All rights reserved.
//
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
//
// Author : F.Gaede, DESY
//
//==========================================================================
/// Framework include files
#include <DD4hep/InstanceCount.h>
#include <DD4hep/VolumeManager.h>
#include <DDG4/Geant4HitCollection.h>
#include <DDG4/Geant4DataConversion.h>
#include <DDG4/Geant4SensDetAction.h>
#include <DDG4/Geant4Context.h>
#include <DDG4/Geant4Particle.h>
#include <DDG4/Geant4Data.h>
///#include <DDG4/Geant4Output2EDM4hep.h>
/// Geant4 headers
#include <G4Threading.hh>
#include <G4AutoLock.hh>
#include <G4Version.hh>
#include <G4ParticleDefinition.hh>
#include <G4VProcess.hh>
#include <G4Event.hh>
#include <G4Run.hh>
/// use the Geant4 units in namespace CLHEP
#include <CLHEP/Units/SystemOfUnits.h>
/// edm4hep include files
#include <edm4hep/EventHeaderCollection.h>
using namespace dd4hep::sim;
using namespace dd4hep;
namespace {
G4Mutex action_mutex = G4MUTEX_INITIALIZER;
}
#include <DDG4/Factories.h>
DECLARE_GEANT4ACTION(Geant4Output2EDM4hep)
/// Standard constructor
Geant4Output2EDM4hep::Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam)
: Geant4OutputAction(ctxt,nam), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
{
declareProperty("RunHeader", m_runHeader);
declareProperty("EventParametersInt", m_eventParametersInt);
declareProperty("EventParametersFloat", m_eventParametersFloat);
declareProperty("EventParametersString", m_eventParametersString);
declareProperty("RunNumberOffset", m_runNumberOffset);
declareProperty("EventNumberOffset", m_eventNumberOffset);
declareProperty("SectionName", m_section_name);
declareProperty("FilesByRun", m_filesByRun);
info("Writer is now instantiated ..." );
InstanceCount::increment(this);
}
/// Default destructor
Geant4Output2EDM4hep::~Geant4Output2EDM4hep() {
G4AutoLock protection_lock(&action_mutex);
m_file.reset();
InstanceCount::decrement(this);
}
// Callback to store the Geant4 run information
void Geant4Output2EDM4hep::beginRun(const G4Run* run) {
G4AutoLock protection_lock(&action_mutex);
std::string fname = m_output;
m_runNo = run->GetRunID();
if ( m_filesByRun ) {
std::size_t idx = m_output.rfind(".");
if ( idx != std::string::npos ) {
fname = m_output.substr(0, idx) + _toString(m_runNo, ".run%08d") + m_output.substr(idx);
}
}
if ( !fname.empty() ) {
m_file = std::make_unique<podio::ROOTFrameWriter>(fname);
if ( !m_file ) {
fatal("+++ Failed to open output file: %s", fname.c_str());
}
printout( INFO, "Geant4Output2EDM4hep" ,"Opened %s for output", fname.c_str() ) ;
}
}
/// Callback to store the Geant4 run information
void Geant4Output2EDM4hep::endRun(const G4Run* run) {
saveRun(run);
saveFileMetaData();
if ( m_file ) {
m_file->finish();
m_file.reset();
}
}
void Geant4Output2EDM4hep::saveFileMetaData() {
podio::Frame metaFrame{};
for (const auto& [name, encodingStr] : m_cellIDEncodingStrings) {
metaFrame.putParameter(name + "__CellIDEncoding", encodingStr);
}
m_file->writeFrame(metaFrame, "metadata");
}
/// Commit data at end of filling procedure
void Geant4Output2EDM4hep::commit( OutputContext<G4Event>& /* ctxt */) {
if ( m_file ) {
G4AutoLock protection_lock(&action_mutex);
m_frame.put( std::move(m_particles), "MCParticles");
for (auto it = m_trackerHits.begin(); it != m_trackerHits.end(); ++it) {
m_frame.put( std::move(it->second), it->first);
}
for (auto& [colName, calorimeterHits] : m_calorimeterHits) {
m_frame.put( std::move(calorimeterHits.first), colName);
m_frame.put( std::move(calorimeterHits.second), colName + "Contributions");
}
m_file->writeFrame(m_frame, m_section_name);
m_particles.clear();
m_trackerHits.clear();
m_calorimeterHits.clear();
m_frame = {};
return;
}
except("+++ Failed to write output file. [Stream is not open]");
}
/// Callback to store the Geant4 run information
void Geant4Output2EDM4hep::saveRun(const G4Run* run) {
G4AutoLock protection_lock(&action_mutex);
// --- write an edm4hep::RunHeader ---------
// Runs are just Frames with different contents in EDM4hep / podio. We simply
// store everything as parameters for now
podio::Frame runHeader {};
for (const auto& [key, value] : m_runHeader)
runHeader.putParameter(key, value);
m_runNo = m_runNumberOffset > 0 ? m_runNumberOffset + run->GetRunID() : run->GetRunID();
runHeader.putParameter("runNumber", m_runNo);
runHeader.putParameter("GEANT4Version", G4Version);
runHeader.putParameter("DD4hepVersion", versionString());
runHeader.putParameter("detectorName", context()->detectorDescription().header().name());
RunParameters* parameters = context()->run().extension<RunParameters>(false);
if ( parameters ) {
parameters->extractParameters(runHeader);
}
m_file->writeFrame(runHeader, "runs");
}
void Geant4Output2EDM4hep::begin(const G4Event* event) {
/// Create event frame object
m_eventNo = event->GetEventID();
m_frame = {};
m_particles = {};
m_trackerHits.clear();
m_calorimeterHits.clear();
}
/// Data conversion interface for MC particles to EDM4hep format
void Geant4Output2EDM4hep::saveParticles(Geant4ParticleMap* particles) {
typedef detail::ReferenceBitMask<const int> PropertyMask;
typedef Geant4ParticleMap::ParticleMap ParticleMap;
const ParticleMap& pm = particles->particleMap;
m_particles.clear();
if ( pm.size() > 0 ) {
size_t cnt = 0;
// Mapping of ids in the ParticleMap to indices in the MCParticle collection
std::map<int,int> p_ids;
std::vector<const Geant4Particle*> p_part;
p_part.reserve(pm.size());
// First create the particles
for (const auto& iParticle : pm) {
int id = iParticle.first;
const Geant4ParticleHandle p = iParticle.second;
PropertyMask mask(p->status);
// std::cout << " ********** mcp status : 0x" << std::hex << p->status << ", mask.isSet(G4PARTICLE_GEN_STABLE) x" << std::dec << mask.isSet(G4PARTICLE_GEN_STABLE) <<std::endl ;
const G4ParticleDefinition* def = p.definition();
auto mcp = m_particles.create();
mcp.setPDG(p->pdgID);
float ps_fa[3] = {float(p->psx/CLHEP::GeV),float(p->psy/CLHEP::GeV),float(p->psz/CLHEP::GeV)};
mcp.setMomentum( ps_fa );
float pe_fa[3] = {float(p->pex/CLHEP::GeV),float(p->pey/CLHEP::GeV),float(p->pez/CLHEP::GeV)};
mcp.setMomentumAtEndpoint( pe_fa );
double vs_fa[3] = { p->vsx/CLHEP::mm, p->vsy/CLHEP::mm, p->vsz/CLHEP::mm } ;
mcp.setVertex( vs_fa );
double ve_fa[3] = { p->vex/CLHEP::mm, p->vey/CLHEP::mm, p->vez/CLHEP::mm } ;
mcp.setEndpoint( ve_fa );
mcp.setTime(p->time/CLHEP::ns);
mcp.setMass(p->mass/CLHEP::GeV);
mcp.setCharge(def ? def->GetPDGCharge() : 0); // Charge(e+) = 1 !
// Set generator status
mcp.setGeneratorStatus(0);
if( p->genStatus ) {
mcp.setGeneratorStatus( p->genStatus ) ;
} else {
if ( mask.isSet(G4PARTICLE_GEN_STABLE) ) mcp.setGeneratorStatus(1);
else if ( mask.isSet(G4PARTICLE_GEN_DECAYED) ) mcp.setGeneratorStatus(2);
else if ( mask.isSet(G4PARTICLE_GEN_DOCUMENTATION) ) mcp.setGeneratorStatus(3);
else if ( mask.isSet(G4PARTICLE_GEN_BEAM) ) mcp.setGeneratorStatus(4);
else if ( mask.isSet(G4PARTICLE_GEN_OTHER) ) mcp.setGeneratorStatus(9);
}
// Set simulation status
mcp.setCreatedInSimulation( mask.isSet(G4PARTICLE_SIM_CREATED) );
mcp.setBackscatter( mask.isSet(G4PARTICLE_SIM_BACKSCATTER) );
mcp.setVertexIsNotEndpointOfParent( mask.isSet(G4PARTICLE_SIM_PARENT_RADIATED) );
mcp.setDecayedInTracker( mask.isSet(G4PARTICLE_SIM_DECAY_TRACKER) );
mcp.setDecayedInCalorimeter( mask.isSet(G4PARTICLE_SIM_DECAY_CALO) );
mcp.setHasLeftDetector( mask.isSet(G4PARTICLE_SIM_LEFT_DETECTOR) );
mcp.setStopped( mask.isSet(G4PARTICLE_SIM_STOPPED) );
mcp.setOverlay( false );
//fg: if simstatus !=0 we have to set the generator status to 0:
if( mcp.isCreatedInSimulation() )
mcp.setGeneratorStatus( 0 ) ;
mcp.setSpin(p->spin);
mcp.setColorFlow(p->colorFlow);
p_ids[id] = cnt++;
p_part.push_back(p);
}
// Now establish parent-daughter relationships
for(size_t i=0; i < p_ids.size(); ++i) {
const Geant4Particle* p = p_part[i];
auto q = m_particles[i];
for (const auto& idau : p->daughters) {
const auto k = p_ids.find(idau);
if (k == p_ids.end()) {
fatal("+++ Particle %d: FAILED to find daughter with ID:%d",p->id,idau);
continue;
}
int iqdau = (*k).second;
auto qdau = m_particles[iqdau];
q.addToDaughters(qdau);
}
for (const auto& ipar : p->parents) {
if (ipar >= 0) { // A parent ID of -1 means NO parent, because a base of 0 is perfectly legal
const auto k = p_ids.find(ipar);
if (k == p_ids.end()) {
fatal("+++ Particle %d: FAILED to find parent with ID:%d",p->id,ipar);
continue;
}
int iqpar = (*k).second;
auto qpar = m_particles[iqpar];
q.addToParents(qpar);
}
}
}
}
}
/// Callback to store the Geant4 event
void Geant4Output2EDM4hep::saveEvent(OutputContext<G4Event>& ctxt) {
EventParameters* parameters = context()->event().extension<EventParameters>(false);
int runNumber(0), eventNumber(0);
const int eventNumberOffset(m_eventNumberOffset > 0 ? m_eventNumberOffset : 0);
const int runNumberOffset(m_runNumberOffset > 0 ? m_runNumberOffset : 0);
double eventWeight{0};
// Get event number, run number and parameters from extension ...
if ( parameters ) {
runNumber = parameters->runNumber() + runNumberOffset;
eventNumber = parameters->eventNumber() + eventNumberOffset;
parameters->extractParameters(m_frame);
#if podio_VERSION_MAJOR > 0 || podio_VERSION_MINOR > 16 || podio_VERSION_PATCH > 2
// This functionality is only present in podio > 0.16.2
eventWeight = m_frame.getParameter<double>("EventWeights");
#endif
} else { // ... or from DD4hep framework
runNumber = m_runNo + runNumberOffset;
eventNumber = ctxt.context->GetEventID() + eventNumberOffset;
}
printout(INFO,"Geant4Output2EDM4hep","+++ Saving EDM4hep event %d run %d.", eventNumber, runNumber);
// this does not compile as create() is we only get a const ref - need to review PODIO EventStore API
edm4hep::EventHeaderCollection header_collection;
auto header = header_collection.create();
header.setRunNumber(runNumber);
header.setEventNumber(eventNumber);
header.setWeight(eventWeight);
//not implemented in EDM4hep ? header.setDetectorName(context()->detectorDescription().header().name());
header.setTimeStamp( std::time(nullptr) ) ;
m_frame.put( std::move(header_collection), "EventHeader");
saveEventParameters<int>(m_eventParametersInt);
saveEventParameters<float>(m_eventParametersFloat);
saveEventParameters<std::string>(m_eventParametersString);
Geant4ParticleMap* part_map = context()->event().extension<Geant4ParticleMap>(false);
if ( part_map ) {
print("+++ Saving %d EDM4hep particles....",int(part_map->particleMap.size()));
if ( part_map->particleMap.size() > 0 ) {
saveParticles(part_map);
}
}
}
/**
* Helper struct that can be used together with map::try_emplace to construct
* the encoding only once per collection (name).
*/
struct LazyEncodingExtraction {
/// Constructor that does effectively nothing. This will be called in every
/// try_emplace call
LazyEncodingExtraction(Geant4HitCollection* coll) : m_coll(coll) {}
/// Defer the real work to the implicit conversion to std::string that will
/// only be called if the value is actually emplaced into the map
operator std::string() const {
const auto* sd = m_coll->sensitive();
return dd4hep::sim::Geant4ConversionHelper::encoding(sd->sensitiveDetector());
}
private:
Geant4HitCollection* m_coll{nullptr};
};
/// Callback to store each Geant4 hit collection
void Geant4Output2EDM4hep::saveCollection(OutputContext<G4Event>& /*ctxt*/, G4VHitsCollection* collection) {
Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
std::string colName = collection->GetName();
if( coll == nullptr ){
error(" no Geant4HitCollection: %s ", colName.c_str());
return ;
}
size_t nhits = collection->GetSize();
Geant4ParticleMap* pm = context()->event().extension<Geant4ParticleMap>(false);
debug("+++ Saving EDM4hep collection %s with %d entries.", colName.c_str(), int(nhits));
// Using try_emplace here to only fill this the first time we come across
m_cellIDEncodingStrings.try_emplace(colName, LazyEncodingExtraction{coll});
//-------------------------------------------------------------------
if( typeid( Geant4Tracker::Hit ) == coll->type().type() ){
// Create the hit container even if there are no entries!
auto& hits = m_trackerHits[colName];
for(unsigned i=0 ; i < nhits ; ++i){
auto sth = hits->create();
const Geant4Tracker::Hit* hit = coll->hit(i);
const Geant4Tracker::Hit::Contribution& t = hit->truth;
int trackID = pm->particleID(t.trackID);
auto mcp = m_particles.at(trackID);
const auto& mom = hit->momentum;
const auto& pos = hit->position;
edm4hep::Vector3f();
sth.setCellID( hit->cellID ) ;
sth.setEDep(hit->energyDeposit/CLHEP::GeV);
sth.setPathLength(hit->length/CLHEP::mm);
sth.setTime(hit->truth.time/CLHEP::ns);
sth.setMCParticle(mcp);
sth.setPosition( {pos.x()/CLHEP::mm, pos.y()/CLHEP::mm, pos.z()/CLHEP::mm} );
sth.setMomentum( {float(mom.x()/CLHEP::GeV),float(mom.y()/CLHEP::GeV),float(mom.z()/CLHEP::GeV)} );
auto particleIt = pm->particles().find(trackID);
if( ( particleIt != pm->particles().end()) ){
// if the original track ID of the particle is not the same as the
// original track ID of the hit it was produced by an MCParticle that
// is no longer stored
sth.setProducedBySecondary( (particleIt->second->originalG4ID != t.trackID) );
}
}
//-------------------------------------------------------------------
}
else if( typeid( Geant4Calorimeter::Hit ) == coll->type().type() ){
Geant4Sensitive* sd = coll->sensitive();
int hit_creation_mode = sd->hitCreationMode();
// Create the hit container even if there are no entries!
auto& hits = m_calorimeterHits[colName];
for(unsigned i=0 ; i < nhits ; ++i){
auto sch = hits.first->create();
const Geant4Calorimeter::Hit* hit = coll->hit(i);
const auto& pos = hit->position;
sch.setCellID( hit->cellID );
sch.setPosition({float(pos.x()/CLHEP::mm), float(pos.y()/CLHEP::mm), float(pos.z()/CLHEP::mm)});
sch.setEnergy( hit->energyDeposit/CLHEP::GeV );
// now add the individual step contributions
for(auto ci=hit->truth.begin(); ci != hit->truth.end(); ++ci){
auto sCaloHitCont = hits.second->create();
sch.addToContributions( sCaloHitCont );
const Geant4HitData::Contribution& c = *ci;
int trackID = pm->particleID(c.trackID);
auto mcp = m_particles.at(trackID);
sCaloHitCont.setEnergy( c.deposit/CLHEP::GeV );
sCaloHitCont.setTime( c.time/CLHEP::ns );
sCaloHitCont.setParticle( mcp );
if ( hit_creation_mode == Geant4Sensitive::DETAILED_MODE ) {
edm4hep::Vector3f p(c.x/CLHEP::mm, c.y/CLHEP::mm, c.z/CLHEP::mm);
sCaloHitCont.setPDG( c.pdgID );
sCaloHitCont.setStepPosition( p );
}
}
}
//-------------------------------------------------------------------
} else {
error("+++ unknown type in Geant4HitCollection %s ", coll->type().type().name());
}
}