DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
Geant4Output2EDM4hep.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : F.Gaede, DESY
11 //
12 //==========================================================================
13 #ifndef DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
14 #define DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
15 
17 #include <DD4hep/Detector.h>
18 #include <DDG4/EventParameters.h>
19 #include <DDG4/FileParameters.h>
21 #include <DDG4/RunParameters.h>
22 
24 #include <edm4hep/MCParticleCollection.h>
25 #include <edm4hep/SimTrackerHitCollection.h>
26 #include <edm4hep/CaloHitContributionCollection.h>
27 #include <edm4hep/SimCalorimeterHitCollection.h>
28 #include <edm4hep/EDM4hepVersion.h>
29 #include <edm4hep/Constants.h>
30 #if EDM4HEP_BUILD_VERSION < EDM4HEP_VERSION(0, 99, 0)
31  using edm4hep::CellIDEncoding;
32 #else
33  using edm4hep::labels::CellIDEncoding;
34 #endif
35 #include <podio/CollectionBase.h>
37 #include <podio/podioVersion.h>
38 #include <podio/Frame.h>
39 #include <podio/FrameCategories.h>
40 #if PODIO_BUILD_VERSION >= PODIO_VERSION(0, 99, 0)
41 #include <podio/ROOTWriter.h>
42 #else
43 #include <podio/ROOTFrameWriter.h>
44 namespace podio {
45  using ROOTWriter = podio::ROOTFrameWriter;
46 }
47 #endif
48 
50 namespace dd4hep {
51 
52  class ComponentCast;
53 
55  namespace sim {
56 
57  class Geant4ParticleMap;
58 
60 
66  protected:
67  using writer_t = podio::ROOTWriter;
68  using stringmap_t = std::map< std::string, std::string >;
69  using trackermap_t = std::map< std::string, edm4hep::SimTrackerHitCollection >;
70  using calorimeterpair_t = std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection >;
71  using calorimetermap_t = std::map< std::string, calorimeterpair_t >;
72  std::unique_ptr<writer_t> m_file { };
73  podio::Frame m_frame { };
74  edm4hep::MCParticleCollection m_particles { };
82  std::string m_section_name { "events" };
83  int m_runNo { 0 };
84  int m_runNumberOffset { 0 };
85  int m_eventNo { 0 };
87  bool m_filesByRun { false };
88 
90  void saveParticles(Geant4ParticleMap* particles);
92  void saveFileMetaData();
93  public:
95  Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam);
97  virtual ~Geant4Output2EDM4hep();
99  virtual void beginRun(const G4Run* run);
101  virtual void endRun(const G4Run* run);
102 
104  virtual void saveRun(const G4Run* run);
106  virtual void saveEvent( OutputContext<G4Event>& ctxt);
108  virtual void saveCollection( OutputContext<G4Event>& ctxt, G4VHitsCollection* collection);
110  virtual void commit( OutputContext<G4Event>& ctxt);
111 
113  virtual void begin(const G4Event* event);
114  protected:
116  template <typename T>
117  void saveEventParameters(const std::map<std::string, std::string >& parameters) {
118  for(const auto& p : parameters) {
119  info("Saving event parameter: %-32s = %s", p.first.c_str(), p.second.c_str());
120  m_frame.putParameter(p.first, p.second);
121  }
122  }
123  };
124 
125  template <> void EventParameters::extractParameters(podio::Frame& frame) {
126  for(auto const& p: this->intParameters()) {
127  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
128  frame.putParameter(p.first, p.second);
129  }
130  for(auto const& p: this->fltParameters()) {
131  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
132  frame.putParameter(p.first, p.second);
133  }
134  for(auto const& p: this->strParameters()) {
135  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
136  frame.putParameter(p.first, p.second);
137  }
138  // This functionality is only present in podio > 0.16.2
139  for (auto const& p: this->dblParameters()) {
140  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
141  frame.putParameter(p.first, p.second);
142  }
143  }
144 
145  template <> void RunParameters::extractParameters(podio::Frame& frame) {
146  for(auto const& p: this->intParameters()) {
147  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
148  frame.putParameter(p.first, p.second);
149  }
150  for(auto const& p: this->fltParameters()) {
151  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
152  frame.putParameter(p.first, p.second);
153  }
154  for(auto const& p: this->strParameters()) {
155  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
156  frame.putParameter(p.first, p.second);
157  }
158  // This functionality is only present in podio > 0.16.2
159  for (auto const& p: this->dblParameters()) {
160  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
161  frame.putParameter(p.first, p.second);
162  }
163  }
164  template <> void FileParameters::extractParameters(podio::Frame& frame) {
165  for(auto const& p: this->intParameters()) {
166  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
167  frame.putParameter(p.first, p.second);
168  }
169  for(auto const& p: this->fltParameters()) {
170  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
171  frame.putParameter(p.first, p.second);
172  }
173  for(auto const& p: this->strParameters()) {
174  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
175  frame.putParameter(p.first, p.second);
176  }
177  // This functionality is only present in podio > 0.16.2
178  for (auto const& p: this->dblParameters()) {
179  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
180  frame.putParameter(p.first, p.second);
181  }
182  }
183 
184  } // End namespace sim
185 } // End namespace dd4hep
186 #endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
187 
188 //==========================================================================
189 // AIDA Detector description implementation
190 //--------------------------------------------------------------------------
191 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
192 // All rights reserved.
193 //
194 // For the licensing terms see $DD4hepINSTALL/LICENSE.
195 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
196 //
197 // Author : F.Gaede, DESY
198 //
199 //==========================================================================
200 
202 #include <DD4hep/InstanceCount.h>
203 #include <DD4hep/VolumeManager.h>
204 
208 #include <DDG4/Geant4Context.h>
209 #include <DDG4/Geant4Particle.h>
210 #include <DDG4/Geant4Data.h>
211 
214 #include <G4Threading.hh>
215 #include <G4AutoLock.hh>
216 #include <G4Version.hh>
217 #include <G4ParticleDefinition.hh>
218 #include <G4VProcess.hh>
219 #include <G4Event.hh>
220 #include <G4Run.hh>
222 #include <CLHEP/Units/SystemOfUnits.h>
223 
225 #include <edm4hep/EventHeaderCollection.h>
226 
227 using namespace dd4hep::sim;
228 using namespace dd4hep;
229 
230 namespace {
231  G4Mutex action_mutex = G4MUTEX_INITIALIZER;
232 }
233 
234 #include <DDG4/Factories.h>
236 
237 Geant4Output2EDM4hep::Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam)
239 : Geant4OutputAction(ctxt,nam), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
240 {
241  declareProperty("RunHeader", m_runHeader);
242  declareProperty("EventParametersInt", m_eventParametersInt);
243  declareProperty("EventParametersFloat", m_eventParametersFloat);
244  declareProperty("EventParametersString", m_eventParametersString);
245  declareProperty("RunNumberOffset", m_runNumberOffset);
246  declareProperty("EventNumberOffset", m_eventNumberOffset);
247  declareProperty("SectionName", m_section_name);
248  declareProperty("FilesByRun", m_filesByRun);
249  info("Writer is now instantiated ..." );
251 }
252 
255  G4AutoLock protection_lock(&action_mutex);
256  m_file.reset();
258 }
259 
260 // Callback to store the Geant4 run information
261 void Geant4Output2EDM4hep::beginRun(const G4Run* run) {
262  G4AutoLock protection_lock(&action_mutex);
263  std::string fname = m_output;
264  m_runNo = run->GetRunID();
265  if ( m_filesByRun ) {
266  std::size_t idx = m_output.rfind(".");
267  if ( idx != std::string::npos ) {
268  fname = m_output.substr(0, idx) + _toString(m_runNo, ".run%08d") + m_output.substr(idx);
269  }
270  }
271  if ( !fname.empty() ) {
272  m_file = std::make_unique<podio::ROOTWriter>(fname);
273  if ( !m_file ) {
274  fatal("+++ Failed to open output file: %s", fname.c_str());
275  }
276  printout( INFO, "Geant4Output2EDM4hep" ,"Opened %s for output", fname.c_str() ) ;
277  }
278 }
279 
281 void Geant4Output2EDM4hep::endRun(const G4Run* run) {
282  saveRun(run);
284  if ( m_file ) {
285  m_file->finish();
286  m_file.reset();
287  }
288 }
289 
291  podio::Frame metaFrame{};
292  for (const auto& [name, encodingStr] : m_cellIDEncodingStrings) {
293  metaFrame.putParameter(podio::collMetadataParamName(name, CellIDEncoding), encodingStr);
294  }
295 
296  m_file->writeFrame(metaFrame, "metadata");
297 }
298 
301  if ( m_file ) {
302  G4AutoLock protection_lock(&action_mutex);
303  m_frame.put( std::move(m_particles), "MCParticles");
304  for (auto it = m_trackerHits.begin(); it != m_trackerHits.end(); ++it) {
305  m_frame.put( std::move(it->second), it->first);
306  }
307  for (auto& [colName, calorimeterHits] : m_calorimeterHits) {
308  m_frame.put( std::move(calorimeterHits.first), colName);
309  m_frame.put( std::move(calorimeterHits.second), colName + "Contributions");
310  }
311  m_file->writeFrame(m_frame, m_section_name);
312  m_particles.clear();
313  m_trackerHits.clear();
314  m_calorimeterHits.clear();
315  m_frame = {};
316  return;
317  }
318  except("+++ Failed to write output file. [Stream is not open]");
319 }
320 
322 void Geant4Output2EDM4hep::saveRun(const G4Run* run) {
323  G4AutoLock protection_lock(&action_mutex);
324  // --- write an edm4hep::RunHeader ---------
325  // Runs are just Frames with different contents in EDM4hep / podio. We simply
326  // store everything as parameters for now
327  podio::Frame runHeader {};
328  for (const auto& [key, value] : m_runHeader)
329  runHeader.putParameter(key, value);
330 
331  m_runNo = m_runNumberOffset > 0 ? m_runNumberOffset + run->GetRunID() : run->GetRunID();
332  runHeader.putParameter("runNumber", m_runNo);
333  runHeader.putParameter("GEANT4Version", G4Version);
334  runHeader.putParameter("DD4hepVersion", versionString());
335  runHeader.putParameter("detectorName", context()->detectorDescription().header().name());
336  {
337  RunParameters* parameters = context()->run().extension<RunParameters>(false);
338  if ( parameters ) {
339  parameters->extractParameters(runHeader);
340  }
341  m_file->writeFrame(runHeader, "runs");
342  }
343  {
344  podio::Frame metaFrame {};
345  FileParameters* parameters = context()->run().extension<FileParameters>(false);
346  if ( parameters ) {
347  parameters->extractParameters(metaFrame);
348  }
349  m_file->writeFrame(metaFrame, "meta");
350  }
351 }
352 
353 void Geant4Output2EDM4hep::begin(const G4Event* event) {
355  m_eventNo = event->GetEventID();
356  m_frame = {};
357  m_particles = {};
358  m_trackerHits.clear();
359  m_calorimeterHits.clear();
360 }
361 
364  typedef detail::ReferenceBitMask<const int> PropertyMask;
365  typedef Geant4ParticleMap::ParticleMap ParticleMap;
366  const ParticleMap& pm = particles->particleMap;
367 
368  m_particles.clear();
369  if ( pm.size() > 0 ) {
370  size_t cnt = 0;
371  // Mapping of ids in the ParticleMap to indices in the MCParticle collection
372  std::map<int,int> p_ids;
373  std::vector<const Geant4Particle*> p_part;
374  p_part.reserve(pm.size());
375  // First create the particles
376  for (const auto& iParticle : pm) {
377  int id = iParticle.first;
378  const Geant4ParticleHandle p = iParticle.second;
379  PropertyMask mask(p->status);
380  // std::cout << " ********** mcp status : 0x" << std::hex << p->status << ", mask.isSet(G4PARTICLE_GEN_STABLE) x" << std::dec << mask.isSet(G4PARTICLE_GEN_STABLE) <<std::endl ;
381  const G4ParticleDefinition* def = p.definition();
382  auto mcp = m_particles.create();
383  mcp.setPDG(p->pdgID);
384  // Because EDM4hep is switching between vector3f[loat] and vector3d[ouble]
385  using MT = decltype(std::declval<edm4hep::MCParticle>().getMomentum().x);
386  mcp.setMomentum( {MT(p->psx/CLHEP::GeV),MT(p->psy/CLHEP::GeV),MT(p->psz/CLHEP::GeV)} );
387  mcp.setMomentumAtEndpoint( {MT(p->pex/CLHEP::GeV),MT(p->pey/CLHEP::GeV),MT(p->pez/CLHEP::GeV)} );
388 
389  double vs_fa[3] = { p->vsx/CLHEP::mm, p->vsy/CLHEP::mm, p->vsz/CLHEP::mm } ;
390  mcp.setVertex( vs_fa );
391 
392  double ve_fa[3] = { p->vex/CLHEP::mm, p->vey/CLHEP::mm, p->vez/CLHEP::mm } ;
393  mcp.setEndpoint( ve_fa );
394 
395  mcp.setTime(p->time/CLHEP::ns);
396  mcp.setMass(p->mass/CLHEP::GeV);
397  mcp.setCharge(def ? def->GetPDGCharge() : 0); // Charge(e+) = 1 !
398 
399  // Set generator status
400  mcp.setGeneratorStatus(0);
401  if( p->genStatus ) {
402  mcp.setGeneratorStatus( p->genStatus ) ;
403  } else {
404  if ( mask.isSet(G4PARTICLE_GEN_STABLE) ) mcp.setGeneratorStatus(1);
405  else if ( mask.isSet(G4PARTICLE_GEN_DECAYED) ) mcp.setGeneratorStatus(2);
406  else if ( mask.isSet(G4PARTICLE_GEN_DOCUMENTATION) ) mcp.setGeneratorStatus(3);
407  else if ( mask.isSet(G4PARTICLE_GEN_BEAM) ) mcp.setGeneratorStatus(4);
408  else if ( mask.isSet(G4PARTICLE_GEN_OTHER) ) mcp.setGeneratorStatus(9);
409  }
410 
411  // Set simulation status
412  mcp.setCreatedInSimulation( mask.isSet(G4PARTICLE_SIM_CREATED) );
413  mcp.setBackscatter( mask.isSet(G4PARTICLE_SIM_BACKSCATTER) );
414  mcp.setVertexIsNotEndpointOfParent( mask.isSet(G4PARTICLE_SIM_PARENT_RADIATED) );
415  mcp.setDecayedInTracker( mask.isSet(G4PARTICLE_SIM_DECAY_TRACKER) );
416  mcp.setDecayedInCalorimeter( mask.isSet(G4PARTICLE_SIM_DECAY_CALO) );
417  mcp.setHasLeftDetector( mask.isSet(G4PARTICLE_SIM_LEFT_DETECTOR) );
418  mcp.setStopped( mask.isSet(G4PARTICLE_SIM_STOPPED) );
419  mcp.setOverlay( false );
420 
421  //fg: if simstatus !=0 we have to set the generator status to 0:
422  if( mcp.isCreatedInSimulation() )
423  mcp.setGeneratorStatus( 0 ) ;
424 
425  mcp.setSpin(p->spin);
426 
427  p_ids[id] = cnt++;
428  p_part.push_back(p);
429  }
430 
431  // Now establish parent-daughter relationships
432  for(size_t i=0; i < p_ids.size(); ++i) {
433  const Geant4Particle* p = p_part[i];
434  auto q = m_particles[i];
435 
436  for (const auto& idau : p->daughters) {
437  const auto k = p_ids.find(idau);
438  if (k == p_ids.end()) {
439  fatal("+++ Particle %d: FAILED to find daughter with ID:%d",p->id,idau);
440  continue;
441  }
442  int iqdau = (*k).second;
443  auto qdau = m_particles[iqdau];
444  q.addToDaughters(qdau);
445  }
446 
447  for (const auto& ipar : p->parents) {
448  if (ipar >= 0) { // A parent ID of -1 means NO parent, because a base of 0 is perfectly legal
449  const auto k = p_ids.find(ipar);
450  if (k == p_ids.end()) {
451  fatal("+++ Particle %d: FAILED to find parent with ID:%d",p->id,ipar);
452  continue;
453  }
454  int iqpar = (*k).second;
455  auto qpar = m_particles[iqpar];
456  q.addToParents(qpar);
457  }
458  }
459  }
460  }
461 }
462 
465  EventParameters* parameters = context()->event().extension<EventParameters>(false);
466  int runNumber(0), eventNumber(0);
467  const int eventNumberOffset(m_eventNumberOffset > 0 ? m_eventNumberOffset : 0);
468  const int runNumberOffset(m_runNumberOffset > 0 ? m_runNumberOffset : 0);
469  double eventWeight{0};
470  // Get event number, run number and parameters from extension ...
471  if ( parameters ) {
472  runNumber = parameters->runNumber() + runNumberOffset;
473  eventNumber = parameters->eventNumber() + eventNumberOffset;
474  parameters->extractParameters(m_frame);
475 #if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
476  eventWeight = m_frame.getParameter<double>("EventWeights").value_or(0.0);
477 #else
478  eventWeight = m_frame.getParameter<double>("EventWeights");
479 #endif
480  } else { // ... or from DD4hep framework
481  runNumber = m_runNo + runNumberOffset;
482  eventNumber = ctxt.context->GetEventID() + eventNumberOffset;
483  }
484  printout(INFO,"Geant4Output2EDM4hep","+++ Saving EDM4hep event %d run %d.", eventNumber, runNumber);
485 
486  // this does not compile as create() is we only get a const ref - need to review PODIO EventStore API
487  edm4hep::EventHeaderCollection header_collection;
488 
489  auto header = header_collection.create();
490  header.setRunNumber(runNumber);
491  header.setEventNumber(eventNumber);
492  header.setWeight(eventWeight);
493  //not implemented in EDM4hep ? header.setDetectorName(context()->detectorDescription().header().name());
494  header.setTimeStamp(std::time(nullptr));
495 
496  // extract event header, in case we come from edm4hep input
497  auto* meh = context()->event().extension<edm4hep::MutableEventHeader>(false);
498  if(meh) {
499  header.setTimeStamp(meh->getTimeStamp());
500 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 0)
501  for (auto const& weight: meh->getWeights()) {
502  header.addToWeights(weight);
503  }
504 #endif
505  }
506 
507  m_frame.put(std::move(header_collection), "EventHeader");
508  saveEventParameters<int>(m_eventParametersInt);
509  saveEventParameters<float>(m_eventParametersFloat);
510  saveEventParameters<std::string>(m_eventParametersString);
511 
512  Geant4ParticleMap* part_map = context()->event().extension<Geant4ParticleMap>(false);
513  if ( part_map ) {
514  print("+++ Saving %d EDM4hep particles....",int(part_map->particleMap.size()));
515  if ( part_map->particleMap.size() > 0 ) {
516  saveParticles(part_map);
517  }
518  }
519 }
520 
531  operator std::string() const {
532  const auto* sd = m_coll->sensitive();
533  return dd4hep::sim::Geant4ConversionHelper::encoding(sd->sensitiveDetector());
534  }
535 private:
536  Geant4HitCollection* m_coll{nullptr};
537 };
538 
539 
542  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
543  std::string colName = collection->GetName();
544  if( coll == nullptr ){
545  error(" no Geant4HitCollection: %s ", colName.c_str());
546  return ;
547  }
548  size_t nhits = collection->GetSize();
550  debug("+++ Saving EDM4hep collection %s with %d entries.", colName.c_str(), int(nhits));
551 
552  // Using try_emplace here to only fill this the first time we come across
553  m_cellIDEncodingStrings.try_emplace(colName, LazyEncodingExtraction{coll});
554 
555  //-------------------------------------------------------------------
556  if( typeid( Geant4Tracker::Hit ) == coll->type().type() ){
557  // Create the hit container even if there are no entries!
558  auto& hits = m_trackerHits[colName];
559  for(unsigned i=0 ; i < nhits ; ++i){
560  auto sth = hits->create();
561  const Geant4Tracker::Hit* hit = coll->hit(i);
562  const Geant4Tracker::Hit::Contribution& t = hit->truth;
563  int trackID = pm->particleID(t.trackID);
564  auto mcp = m_particles.at(trackID);
565  const auto& mom = hit->momentum;
566  const auto& pos = hit->position;
567  edm4hep::Vector3f();
568  sth.setCellID( hit->cellID ) ;
569  sth.setEDep(hit->energyDeposit/CLHEP::GeV);
570  sth.setPathLength(hit->length/CLHEP::mm);
571  sth.setTime(hit->truth.time/CLHEP::ns);
572 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 10, 99)
573  sth.setParticle(mcp);
574 #else
575  sth.setMCParticle(mcp);
576 #endif
577  sth.setPosition( {pos.x()/CLHEP::mm, pos.y()/CLHEP::mm, pos.z()/CLHEP::mm} );
578  sth.setMomentum( {float(mom.x()/CLHEP::GeV),float(mom.y()/CLHEP::GeV),float(mom.z()/CLHEP::GeV)} );
579  auto particleIt = pm->particles().find(trackID);
580  if( ( particleIt != pm->particles().end()) ){
581  // if the original track ID of the particle is not the same as the
582  // original track ID of the hit it was produced by an MCParticle that
583  // is no longer stored
584  sth.setProducedBySecondary( (particleIt->second->originalG4ID != t.trackID) );
585  }
586  }
587  //-------------------------------------------------------------------
588  }
589  else if( typeid( Geant4Calorimeter::Hit ) == coll->type().type() ){
590  Geant4Sensitive* sd = coll->sensitive();
591  int hit_creation_mode = sd->hitCreationMode();
592  // Create the hit container even if there are no entries!
593  auto& hits = m_calorimeterHits[colName];
594  for(unsigned i=0 ; i < nhits ; ++i){
595  auto sch = hits.first->create();
596  const Geant4Calorimeter::Hit* hit = coll->hit(i);
597  const auto& pos = hit->position;
598  sch.setCellID( hit->cellID );
599  sch.setPosition({float(pos.x()/CLHEP::mm), float(pos.y()/CLHEP::mm), float(pos.z()/CLHEP::mm)});
600  sch.setEnergy( hit->energyDeposit/CLHEP::GeV );
601 
602 
603  // now add the individual step contributions
604  for(auto ci=hit->truth.begin(); ci != hit->truth.end(); ++ci){
605 
606  auto sCaloHitCont = hits.second->create();
607  sch.addToContributions( sCaloHitCont );
608 
609  const Geant4HitData::Contribution& c = *ci;
610  int trackID = pm->particleID(c.trackID);
611  auto mcp = m_particles.at(trackID);
612  sCaloHitCont.setEnergy( c.deposit/CLHEP::GeV );
613  sCaloHitCont.setTime( c.time/CLHEP::ns );
614  sCaloHitCont.setParticle( mcp );
615 
616  if ( hit_creation_mode == Geant4Sensitive::DETAILED_MODE ) {
617  edm4hep::Vector3f p(c.x/CLHEP::mm, c.y/CLHEP::mm, c.z/CLHEP::mm);
618  sCaloHitCont.setPDG( c.pdgID );
619  sCaloHitCont.setStepPosition( p );
620  }
621  }
622  }
623  //-------------------------------------------------------------------
624  } else {
625  error("+++ unknown type in Geant4HitCollection %s ", coll->type().type().name());
626  }
627 }
dd4hep::sim::Geant4Output2EDM4hep::m_eventParametersFloat
stringmap_t m_eventParametersFloat
Definition: Geant4Output2EDM4hep.cpp:79
Geant4DataConversion.h
dd4hep::sim::G4PARTICLE_SIM_CREATED
@ G4PARTICLE_SIM_CREATED
Definition: Geant4Particle.h:85
dd4hep::sim::Geant4Output2EDM4hep::m_eventParametersInt
stringmap_t m_eventParametersInt
Definition: Geant4Output2EDM4hep.cpp:78
dd4hep::sim::Geant4Tracker::Hit::momentum
Direction momentum
Hit direction.
Definition: Geant4Data.h:270
dd4hep::sim::Geant4Output2EDM4hep::beginRun
virtual void beginRun(const G4Run *run)
Callback to store the Geant4 run information.
Definition: Geant4Output2EDM4hep.cpp:261
dd4hep::sim::Geant4Tracker::Hit::position
Position position
Hit position.
Definition: Geant4Data.h:268
dd4hep::sim::Geant4Output2EDM4hep::m_eventParametersString
stringmap_t m_eventParametersString
Definition: Geant4Output2EDM4hep.cpp:80
Geant4HitCollection.h
dd4hep::sim::Geant4Output2EDM4hep::m_eventNo
int m_eventNo
Definition: Geant4Output2EDM4hep.cpp:85
dd4hep::sim::Geant4Particle::vsz
double vsz
Definition: Geant4Particle.h:124
dd4hep::sim::ExtensionParameters::dblParameters
auto const & dblParameters() const
Get the double parameters.
Definition: ExtensionParameters.h:46
dd4hep::sim::Geant4Output2EDM4hep::m_runHeader
stringmap_t m_runHeader
Definition: Geant4Output2EDM4hep.cpp:77
dd4hep::sim::Geant4HitData::MonteCarloContrib::pdgID
int pdgID
Particle ID from the PDG table.
Definition: Geant4Data.h:143
dd4hep::sim::Geant4Output2EDM4hep::saveRun
virtual void saveRun(const G4Run *run)
Callback to store the Geant4 run information.
Definition: Geant4Output2EDM4hep.cpp:322
dd4hep::sim::G4PARTICLE_GEN_STABLE
@ G4PARTICLE_GEN_STABLE
Definition: Geant4Particle.h:71
dd4hep::sim::Geant4Output2EDM4hep::commit
virtual void commit(OutputContext< G4Event > &ctxt)
Commit data at end of filling procedure.
Definition: Geant4Output2EDM4hep.cpp:300
dd4hep::sim::G4PARTICLE_SIM_PARENT_RADIATED
@ G4PARTICLE_SIM_PARENT_RADIATED
Definition: Geant4Particle.h:91
dd4hep::sim::Geant4Particle::mass
double mass
Particle mass.
Definition: Geant4Particle.h:132
dd4hep::sim::Geant4Particle::vey
double vey
Definition: Geant4Particle.h:126
dd4hep::sim::Geant4HitData::MonteCarloContrib::y
float y
Definition: Geant4Data.h:151
Detector.h
dd4hep::sim::Geant4Output2EDM4hep::endRun
virtual void endRun(const G4Run *run)
Callback to store the Geant4 run information.
Definition: Geant4Output2EDM4hep.cpp:281
dd4hep::info
std::size_t info(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:65
dd4hep::sim::Geant4Particle::pez
double pez
Definition: Geant4Particle.h:130
dd4hep::sim::Geant4ParticleMap::particles
const ParticleMap & particles() const
Access the particle map.
Definition: Geant4Particle.h:361
dd4hep::sim::Geant4Output2EDM4hep::saveCollection
virtual void saveCollection(OutputContext< G4Event > &ctxt, G4VHitsCollection *collection)
Callback to store each Geant4 hit collection.
Definition: Geant4Output2EDM4hep.cpp:541
dd4hep::sim::Geant4Context::run
Geant4Run & run() const
Access the geant4 run – valid only between BeginRun() and EndRun()!
Definition: Geant4Context.cpp:72
dd4hep::sim::Geant4Output2EDM4hep::m_runNo
int m_runNo
Definition: Geant4Output2EDM4hep.cpp:83
dd4hep::sim::Geant4Particle::id
int id
not persistent
Definition: Geant4Particle.h:108
Geant4SensDetAction.h
dd4hep::sim::Geant4HitCollection
Generic hit container class using Geant4HitWrapper objects.
Definition: Geant4HitCollection.h:201
dd4hep::versionString
std::string versionString()
return a string with the current dd4hep version in the form vXX-YY.
Definition: DetectorImp.cpp:139
dd4hep::sim::Geant4Particle::pdgID
int pdgID
Definition: Geant4Particle.h:115
dd4hep::sim::Geant4HitCollection::hit
Geant4HitWrapper & hit(size_t which)
Access the hit wrapper.
Definition: Geant4HitCollection.h:325
dd4hep::_toString
std::string _toString(bool value)
String conversions: boolean value to string.
Definition: Handle.cpp:332
G4VHitsCollection
Class of the Geant4 toolkit. See http://www-geant4.kek.jp/Reference.
Definition: Geant4Classes.h:47
dd4hep::sim::Geant4OutputAction
Base class to output Geant4 event data to persistent media.
Definition: Geant4OutputAction.h:40
dd4hep::InstanceCount::increment
static void increment(T *)
Increment count according to type information.
Definition: InstanceCount.h:98
dd4hep::sim::Geant4Particle::psz
double psz
Definition: Geant4Particle.h:128
LazyEncodingExtraction
Definition: Geant4Output2EDM4hep.cpp:525
Geant4Data.h
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
dd4hep::sim::Geant4Output2EDM4hep::saveEvent
virtual void saveEvent(OutputContext< G4Event > &ctxt)
Callback to store the Geant4 event.
Definition: Geant4Output2EDM4hep.cpp:464
DECLARE_GEANT4ACTION
#define DECLARE_GEANT4ACTION(name)
Plugin defintion to create Geant4Action objects.
Definition: Factories.h:210
dd4hep::sim::Geant4HitData::cellID
long long int cellID
cellID
Definition: Geant4Data.h:124
dd4hep::sim::Geant4Output2EDM4hep::saveParticles
void saveParticles(Geant4ParticleMap *particles)
Data conversion interface for MC particles to EDM4hep format.
Definition: Geant4Output2EDM4hep.cpp:363
dd4hep::sim::G4PARTICLE_GEN_DOCUMENTATION
@ G4PARTICLE_GEN_DOCUMENTATION
Definition: Geant4Particle.h:73
dd4hep::sim::G4PARTICLE_SIM_BACKSCATTER
@ G4PARTICLE_SIM_BACKSCATTER
Definition: Geant4Particle.h:86
dd4hep::sim::Geant4Action::fatal
void fatal(const char *fmt,...) const
Support of fatal messages. Throws exception.
Definition: Geant4Action.cpp:248
dd4hep::sim::Geant4Output2EDM4hep::m_frame
podio::Frame m_frame
Definition: Geant4Output2EDM4hep.cpp:73
RunParameters.h
dd4hep::sim::Geant4Output2EDM4hep
Base class to output Geant4 event data to EDM4hep.
Definition: Geant4Output2EDM4hep.cpp:65
dd4hep::sim::RunParameters
Extension to pass input run data to output run data.
Definition: RunParameters.h:28
dd4hep::sim::Geant4Output2EDM4hep::calorimetermap_t
std::map< std::string, calorimeterpair_t > calorimetermap_t
Definition: Geant4Output2EDM4hep.cpp:71
dd4hep::sim::Geant4Sensitive::DETAILED_MODE
@ DETAILED_MODE
Definition: Geant4SensDetAction.h:126
VolumeManager.h
dd4hep::sim::Geant4OutputAction::OutputContext
Helper class for thread savety.
Definition: Geant4OutputAction.h:43
dd4hep::sim::Geant4ParticleMap
Data structure to map particles produced during the generation and the simulation.
Definition: Geant4Particle.h:337
dd4hep::sim::Geant4Action::info
void info(const char *fmt,...) const
Support of info messages.
Definition: Geant4Action.cpp:215
dd4hep::sim::Geant4Event::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:151
dd4hep::sim::Geant4Action::except
void except(const char *fmt,...) const
Support of exceptions: Print fatal message and throw runtime_error.
Definition: Geant4Action.cpp:256
FileParameters.h
dd4hep::sim::G4PARTICLE_SIM_STOPPED
@ G4PARTICLE_SIM_STOPPED
Definition: Geant4Particle.h:89
Geant4OutputAction.h
dd4hep::sim::Geant4Output2EDM4hep::stringmap_t
std::map< std::string, std::string > stringmap_t
Definition: Geant4Output2EDM4hep.cpp:68
EventParameters.h
dd4hep::sim::Geant4HitCollection::sensitive
Geant4Sensitive * sensitive() const
Access the sensitive detector.
Definition: Geant4HitCollection.h:313
dd4hep::sim::Geant4Output2EDM4hep::m_section_name
std::string m_section_name
Definition: Geant4Output2EDM4hep.cpp:82
dd4hep::sim::EventParameters::extractParameters
void extractParameters(T &destination)
Put parameters into destination.
Definition: Geant4Output2LCIO.cpp:46
dd4hep::sim::Geant4Action::error
void error(const char *fmt,...) const
Support of error messages.
Definition: Geant4Action.cpp:231
dd4hep::sim::Geant4Output2EDM4hep::m_trackerHits
trackermap_t m_trackerHits
Definition: Geant4Output2EDM4hep.cpp:75
dd4hep::sim::Geant4Particle::vez
double vez
Definition: Geant4Particle.h:126
dd4hep::sim::G4PARTICLE_SIM_DECAY_CALO
@ G4PARTICLE_SIM_DECAY_CALO
Definition: Geant4Particle.h:87
dd4hep::sim::Geant4Output2EDM4hep::saveEventParameters
void saveEventParameters(const std::map< std::string, std::string > &parameters)
Fill event parameters in EDM4hep event.
Definition: Geant4Output2EDM4hep.cpp:117
dd4hep::sim::Geant4Tracker::Hit
DDG4 tracker hit class used by the generic DDG4 tracker sensitive detector.
Definition: Geant4Data.h:263
dd4hep::sim::Geant4Tracker::Hit::length
double length
Length of the track segment contributing to this hit.
Definition: Geant4Data.h:272
dd4hep::sim::Geant4ParticleMap::ParticleMap
std::map< int, Particle * > ParticleMap
Definition: Geant4Particle.h:340
dd4hep::sim::Geant4Output2EDM4hep::calorimeterpair_t
std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection > calorimeterpair_t
Definition: Geant4Output2EDM4hep.cpp:70
dd4hep::sim::Geant4Output2EDM4hep::m_calorimeterHits
calorimetermap_t m_calorimeterHits
Definition: Geant4Output2EDM4hep.cpp:76
dd4hep::sim::Geant4HitData::MonteCarloContrib::deposit
double deposit
Total energy deposit in this hit.
Definition: Geant4Data.h:145
dd4hep::sim::Geant4Particle::spin
float spin[3]
Definition: Geant4Particle.h:121
dd4hep::sim::ExtensionParameters::fltParameters
auto const & fltParameters() const
Get the float parameters.
Definition: ExtensionParameters.h:42
dd4hep::InstanceCount::decrement
static void decrement(T *)
Decrement count according to type information.
Definition: InstanceCount.h:102
dd4hep::sim::Geant4Particle::genStatus
unsigned short genStatus
Definition: Geant4Particle.h:118
dd4hep::sim::Geant4Particle::status
int status
Definition: Geant4Particle.h:116
dd4hep::sim::G4PARTICLE_GEN_DECAYED
@ G4PARTICLE_GEN_DECAYED
Definition: Geant4Particle.h:72
dd4hep::sim::Geant4Particle::daughters
Particles daughters
The list of daughters of this MC particle.
Definition: Geant4Particle.h:140
dd4hep::sim::G4PARTICLE_SIM_DECAY_TRACKER
@ G4PARTICLE_SIM_DECAY_TRACKER
Definition: Geant4Particle.h:88
dd4hep::sim::Geant4Output2EDM4hep::~Geant4Output2EDM4hep
virtual ~Geant4Output2EDM4hep()
Default destructor.
Definition: Geant4Output2EDM4hep.cpp:254
dd4hep::sim::FileParameters
Extension to pass input run data to output run data.
Definition: FileParameters.h:28
dd4hep::sim::Geant4Tracker::Hit::truth
Contribution truth
Monte Carlo / Geant4 information.
Definition: Geant4Data.h:276
LazyEncodingExtraction::LazyEncodingExtraction
LazyEncodingExtraction(Geant4HitCollection *coll)
Definition: Geant4Output2EDM4hep.cpp:528
dd4hep::sim::Geant4Output2EDM4hep::m_cellIDEncodingStrings
stringmap_t m_cellIDEncodingStrings
Definition: Geant4Output2EDM4hep.cpp:81
dd4hep::sim::Geant4Output2EDM4hep::saveFileMetaData
void saveFileMetaData()
Store the metadata frame with e.g. the cellID encoding strings.
Definition: Geant4Output2EDM4hep.cpp:290
dd4hep::sim::Geant4Action::name
const std::string & name() const
Access name of the action.
Definition: Geant4Action.h:280
dd4hep::sim::Geant4Output2EDM4hep::m_file
std::unique_ptr< writer_t > m_file
Definition: Geant4Output2EDM4hep.cpp:72
dd4hep::sim::Geant4Particle::time
double time
Particle creation time.
Definition: Geant4Particle.h:134
dd4hep::sim::Geant4Calorimeter::Hit::energyDeposit
double energyDeposit
Total energy deposit.
Definition: Geant4Data.h:332
dd4hep::sim::Geant4Calorimeter::Hit
DDG4 calorimeter hit class used by the generic DDG4 calorimeter sensitive detector.
Definition: Geant4Data.h:323
dd4hep::sim::FileParameters::extractParameters
void extractParameters(T &destination)
Put parameters into destination.
dd4hep::sim::Geant4Particle::pex
double pex
The track momentum at the end vertex.
Definition: Geant4Particle.h:130
dd4hep::sim::Geant4Output2EDM4hep::m_runNumberOffset
int m_runNumberOffset
Definition: Geant4Output2EDM4hep.cpp:84
dd4hep::sim::Geant4HitCollection::type
const ComponentCast & type() const
Type information of the object stored.
Definition: Geant4HitCollection.cpp:87
dd4hep::sim::Geant4ParticleMap::particleID
int particleID(int track, bool throw_if_not_found=true) const
Access the equivalent track id (shortcut to the usage of TrackEquivalents)
Definition: Geant4Particle.cpp:541
dd4hep::sim::Geant4Action::print
void print(const char *fmt,...) const
Support for messages with variable output level using output level.
Definition: Geant4Action.cpp:144
dd4hep::sim::Geant4HitData::MonteCarloContrib::trackID
int trackID
Geant 4 Track identifier.
Definition: Geant4Data.h:141
dd4hep::sim::Geant4Particle::vex
double vex
The end vertex.
Definition: Geant4Particle.h:126
dd4hep::sim::Geant4Run::extension
T * extension(bool alert=true)
Access to type safe extension object. Exception is thrown if the object is invalid.
Definition: Geant4Context.h:95
dd4hep::sim::Geant4Particle::vsx
double vsx
The starting vertex.
Definition: Geant4Particle.h:124
dd4hep::sim::Geant4Particle::psx
double psx
The track momentum at the start vertex.
Definition: Geant4Particle.h:128
dd4hep::sim::G4PARTICLE_GEN_BEAM
@ G4PARTICLE_GEN_BEAM
Definition: Geant4Particle.h:74
dd4hep::sim::Geant4ParticleHandle::definition
const G4ParticleDefinition * definition() const
Access the Geant4 particle definition object (expensive!)
Definition: Geant4Particle.cpp:116
dd4hep::sim::Geant4Particle::pey
double pey
Definition: Geant4Particle.h:130
dd4hep::sim::ExtensionParameters::intParameters
auto const & intParameters() const
Get the int parameters.
Definition: ExtensionParameters.h:40
dd4hep::sim::Geant4Output2EDM4hep::m_filesByRun
bool m_filesByRun
Definition: Geant4Output2EDM4hep.cpp:87
Factories.h
dd4hep::sim::Geant4OutputAction::m_output
std::string m_output
Property: "Output" output destination.
Definition: Geant4OutputAction.h:56
dd4hep::sim::Geant4ParticleMap::particleMap
ParticleMap particleMap
Mapping of particles of this event.
Definition: Geant4Particle.h:343
dd4hep::sim::G4PARTICLE_SIM_LEFT_DETECTOR
@ G4PARTICLE_SIM_LEFT_DETECTOR
Definition: Geant4Particle.h:90
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: EDM4hepFileReader.cpp:41
dd4hep::sim::RunParameters::extractParameters
void extractParameters(T &destination)
Put parameters into destination.
Definition: Geant4Output2LCIO.cpp:66
dd4hep::sim::Geant4Output2EDM4hep::m_particles
edm4hep::MCParticleCollection m_particles
Definition: Geant4Output2EDM4hep.cpp:74
dd4hep::sim::Geant4HitData::MonteCarloContrib::x
float x
Proper position of the hit contribution.
Definition: Geant4Data.h:151
dd4hep::sim::Geant4OutputAction::OutputContext::context
const T * context
Definition: Geant4OutputAction.h:45
dd4hep::sim::EventParameters
Event extension to pass input event data to output event.
Definition: EventParameters.h:28
dd4hep::sim::Geant4Calorimeter::Hit::truth
Contributions truth
Hit contributions by individual particles.
Definition: Geant4Data.h:330
dd4hep::sim::Geant4Sensitive::hitCreationMode
int hitCreationMode() const
Property access to the hit creation mode.
Definition: Geant4SensDetAction.h:173
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::Geant4ParticleHandle
Data structure to access derived MC particle information.
Definition: Geant4Particle.h:181
dd4hep::sim::Geant4Output2EDM4hep::m_eventNumberOffset
int m_eventNumberOffset
Definition: Geant4Output2EDM4hep.cpp:86
dd4hep::sim::Geant4Particle::parents
Particles parents
The list of parents of this MC particle.
Definition: Geant4Particle.h:138
dd4hep::sim::Geant4Output2EDM4hep::Geant4Output2EDM4hep
Geant4Output2EDM4hep(Geant4Context *ctxt, const std::string &nam)
Standard constructor.
Definition: Geant4Output2EDM4hep.cpp:238
Geant4Particle.h
dd4hep::sim::Geant4Particle::vsy
double vsy
Definition: Geant4Particle.h:124
dd4hep::sim::Geant4Action::debug
void debug(const char *fmt,...) const
Support of debug messages.
Definition: Geant4Action.cpp:207
dd4hep::sim::Geant4Output2EDM4hep::trackermap_t
std::map< std::string, edm4hep::SimTrackerHitCollection > trackermap_t
Definition: Geant4Output2EDM4hep.cpp:69
dd4hep::sim::Geant4Output2EDM4hep::begin
virtual void begin(const G4Event *event)
begin-of-event callback - creates EDM4hep event and adds it to the event context
Definition: Geant4Output2EDM4hep.cpp:353
dd4hep::sim::Geant4HitData::MonteCarloContrib
Utility class describing the monte carlo contribution of a given particle to a hit.
Definition: Geant4Data.h:138
dd4hep::sim::Geant4Sensitive
The base class for Geant4 sensitive detector actions implemented by users.
Definition: Geant4SensDetAction.h:121
dd4hep::sim::Geant4Tracker::Hit::energyDeposit
double energyDeposit
Energy deposit in the tracker hit.
Definition: Geant4Data.h:274
dd4hep::sim::Geant4Particle
Data structure to store the MC particle information.
Definition: Geant4Particle.h:103
PropertyMask
dd4hep::detail::ReferenceBitMask< int > PropertyMask
Definition: EDM4hepFileReader.cpp:38
dd4hep::sim::ExtensionParameters::strParameters
auto const & strParameters() const
Get the string parameters.
Definition: ExtensionParameters.h:44
InstanceCount.h
dd4hep::sim::Geant4ConversionHelper::encoding
static std::string encoding(VolumeManager vm, VolumeID vid)
Access to the data encoding using the volume manager and a specified volume id.
Definition: Geant4DataConversion.cpp:28
dd4hep::sim::Geant4Particle::psy
double psy
Definition: Geant4Particle.h:128
dd4hep::sim::Geant4HitData::MonteCarloContrib::z
float z
Definition: Geant4Data.h:151
dd4hep::sim::EventParameters::eventNumber
int eventNumber() const
Get the event number.
Definition: EventParameters.h:40
dd4hep::sim::G4PARTICLE_GEN_OTHER
@ G4PARTICLE_GEN_OTHER
Definition: Geant4Particle.h:76
Geant4Context.h
dd4hep::sim::Geant4HitData::MonteCarloContrib::time
double time
Timestamp when this energy was deposited.
Definition: Geant4Data.h:147
dd4hep::sim::EventParameters::runNumber
int runNumber() const
Get the run number.
Definition: EventParameters.h:38
dd4hep::sim::Geant4Calorimeter::Hit::position
Position position
Hit position.
Definition: Geant4Data.h:328
dd4hep::sim::Geant4Output2EDM4hep::writer_t
podio::ROOTWriter writer_t
Definition: Geant4Output2EDM4hep.cpp:67
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201
dd4hep::sim::Geant4Action::context
Geant4Context * context() const
Access the context.
Definition: Geant4Action.h:270