DD4hep  1.31.0
Detector Description Toolkit for High Energy Physics
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
49 #include <atomic>
50 
52 namespace dd4hep {
53 
54  class ComponentCast;
55 
57  namespace sim {
58 
59  class Geant4ParticleMap;
60 
62 
68  protected:
69  using writer_t = podio::ROOTWriter;
70  using stringmap_t = std::map< std::string, std::string >;
71  using trackermap_t = std::map< std::string, edm4hep::SimTrackerHitCollection >;
72  using calorimeterpair_t = std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection >;
73  using calorimetermap_t = std::map< std::string, calorimeterpair_t >;
74  std::unique_ptr<writer_t> m_file { };
75  std::atomic_size_t m_fileUseCount { 0 };
76  podio::Frame m_frame { };
77  edm4hep::MCParticleCollection m_particles { };
85  std::string m_section_name { "events" };
86  int m_runNo { 0 };
87  int m_runNumberOffset { 0 };
88  int m_eventNo { 0 };
90  bool m_filesByRun { false };
91 
93  void saveParticles(Geant4ParticleMap* particles);
95  void saveFileMetaData();
96  public:
98  Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam);
100  virtual ~Geant4Output2EDM4hep();
102  virtual void beginRun(const G4Run* run);
104  virtual void endRun(const G4Run* run);
105 
107  virtual void saveRun(const G4Run* run);
109  virtual void saveEvent( OutputContext<G4Event>& ctxt);
111  virtual void saveCollection( OutputContext<G4Event>& ctxt, G4VHitsCollection* collection);
113  virtual void commit( OutputContext<G4Event>& ctxt);
114 
116  virtual void begin(const G4Event* event);
117  protected:
119  template <typename T>
120  void saveEventParameters(const std::map<std::string, std::string >& parameters) {
121  for(const auto& p : parameters) {
122  info("Saving event parameter: %-32s = %s", p.first.c_str(), p.second.c_str());
123  m_frame.putParameter(p.first, p.second);
124  }
125  }
126  };
127 
128  template <> void EventParameters::extractParameters(podio::Frame& frame) {
129  for(auto const& p: this->intParameters()) {
130  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
131  frame.putParameter(p.first, p.second);
132  }
133  for(auto const& p: this->fltParameters()) {
134  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
135  frame.putParameter(p.first, p.second);
136  }
137  for(auto const& p: this->strParameters()) {
138  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
139  frame.putParameter(p.first, p.second);
140  }
141  // This functionality is only present in podio > 0.16.2
142  for (auto const& p: this->dblParameters()) {
143  printout(DEBUG, "Geant4OutputEDM4hep", "Saving event parameter: %s", p.first.c_str());
144  frame.putParameter(p.first, p.second);
145  }
146  }
147 
148  template <> void RunParameters::extractParameters(podio::Frame& frame) {
149  for(auto const& p: this->intParameters()) {
150  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
151  frame.putParameter(p.first, p.second);
152  }
153  for(auto const& p: this->fltParameters()) {
154  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
155  frame.putParameter(p.first, p.second);
156  }
157  for(auto const& p: this->strParameters()) {
158  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
159  frame.putParameter(p.first, p.second);
160  }
161  // This functionality is only present in podio > 0.16.2
162  for (auto const& p: this->dblParameters()) {
163  printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
164  frame.putParameter(p.first, p.second);
165  }
166  }
167  template <> void FileParameters::extractParameters(podio::Frame& frame) {
168  for(auto const& p: this->intParameters()) {
169  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
170  frame.putParameter(p.first, p.second);
171  }
172  for(auto const& p: this->fltParameters()) {
173  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
174  frame.putParameter(p.first, p.second);
175  }
176  for(auto const& p: this->strParameters()) {
177  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
178  frame.putParameter(p.first, p.second);
179  }
180  // This functionality is only present in podio > 0.16.2
181  for (auto const& p: this->dblParameters()) {
182  printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
183  frame.putParameter(p.first, p.second);
184  }
185  }
186 
187  } // End namespace sim
188 } // End namespace dd4hep
189 #endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
190 
191 //==========================================================================
192 // AIDA Detector description implementation
193 //--------------------------------------------------------------------------
194 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
195 // All rights reserved.
196 //
197 // For the licensing terms see $DD4hepINSTALL/LICENSE.
198 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
199 //
200 // Author : F.Gaede, DESY
201 //
202 //==========================================================================
203 
205 #include <DD4hep/InstanceCount.h>
206 #include <DD4hep/VolumeManager.h>
207 
211 #include <DDG4/Geant4Context.h>
212 #include <DDG4/Geant4Particle.h>
213 #include <DDG4/Geant4Data.h>
214 
217 #include <G4Threading.hh>
218 #include <G4AutoLock.hh>
219 #include <G4Version.hh>
220 #include <G4ParticleDefinition.hh>
221 #include <G4VProcess.hh>
222 #include <G4Event.hh>
223 #include <G4Run.hh>
225 #include <CLHEP/Units/SystemOfUnits.h>
226 
228 #include <edm4hep/EventHeaderCollection.h>
229 
230 using namespace dd4hep::sim;
231 using namespace dd4hep;
232 
233 namespace {
234  G4Mutex action_mutex = G4MUTEX_INITIALIZER;
235 }
236 
237 #include <DDG4/Factories.h>
239 
240 Geant4Output2EDM4hep::Geant4Output2EDM4hep(Geant4Context* ctxt, const std::string& nam)
242 : Geant4OutputAction(ctxt,nam), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
243 {
244  declareProperty("RunHeader", m_runHeader);
245  declareProperty("EventParametersInt", m_eventParametersInt);
246  declareProperty("EventParametersFloat", m_eventParametersFloat);
247  declareProperty("EventParametersString", m_eventParametersString);
248  declareProperty("RunNumberOffset", m_runNumberOffset);
249  declareProperty("EventNumberOffset", m_eventNumberOffset);
250  declareProperty("SectionName", m_section_name);
251  declareProperty("FilesByRun", m_filesByRun);
252  info("Writer is now instantiated ..." );
254 }
255 
258  G4AutoLock protection_lock(&action_mutex);
260 }
261 
262 // Callback to store the Geant4 run information
263 void Geant4Output2EDM4hep::beginRun(const G4Run* run) {
264  G4AutoLock protection_lock(&action_mutex);
265  std::string fname = m_output;
266  m_runNo = run->GetRunID();
267  if ( m_filesByRun ) {
268  std::size_t idx = m_output.rfind(".");
269  if ( idx != std::string::npos ) {
270  fname = m_output.substr(0, idx) + _toString(m_runNo, ".run%08d") + m_output.substr(idx);
271  }
272  }
273  // Create the file only when it has not yet beeen created in another thread
274  if ( !fname.empty() && !m_file ) {
275  m_file = std::make_unique<podio::ROOTWriter>(fname);
276  if ( !m_file ) {
277  fatal("+++ Failed to open output file: %s", fname.c_str());
278  }
279  printout( INFO, "Geant4Output2EDM4hep" ,"Opened %s for output", fname.c_str() ) ;
280  }
281  m_fileUseCount++;
282 }
283 
285 void Geant4Output2EDM4hep::endRun(const G4Run* run) {
286  saveRun(run);
288 
289  // Close the file only when this is the last thread using it.
290  // Note: Although the use count is atomic, the file pointer is not,
291  // and testing it requires locking.
292  G4AutoLock protection_lock(&action_mutex);
293  if ( m_file && m_fileUseCount == 1 ) {
294  m_file->finish();
295  m_file.reset();
296  }
297  m_fileUseCount--;
298 }
299 
301  podio::Frame metaFrame{};
302  for (const auto& [name, encodingStr] : m_cellIDEncodingStrings) {
303  metaFrame.putParameter(podio::collMetadataParamName(name, CellIDEncoding), encodingStr);
304  }
305  G4AutoLock protection_lock(&action_mutex);
306  m_file->writeFrame(metaFrame, "metadata");
307 }
308 
311  if ( m_file ) {
312  G4AutoLock protection_lock(&action_mutex);
313  m_frame.put( std::move(m_particles), "MCParticles");
314  for (auto it = m_trackerHits.begin(); it != m_trackerHits.end(); ++it) {
315  m_frame.put( std::move(it->second), it->first);
316  }
317  for (auto& [colName, calorimeterHits] : m_calorimeterHits) {
318  m_frame.put( std::move(calorimeterHits.first), colName);
319  m_frame.put( std::move(calorimeterHits.second), colName + "Contributions");
320  }
321  m_file->writeFrame(m_frame, m_section_name);
322  m_particles = { };
323  m_trackerHits.clear();
324  m_calorimeterHits.clear();
325  m_frame = {};
326  return;
327  }
328  except("+++ Failed to write output file. [Stream is not open]");
329 }
330 
332 void Geant4Output2EDM4hep::saveRun(const G4Run* run) {
333  G4AutoLock protection_lock(&action_mutex);
334  // --- write an edm4hep::RunHeader ---------
335  // Runs are just Frames with different contents in EDM4hep / podio. We simply
336  // store everything as parameters for now
337  podio::Frame runHeader {};
338  for (const auto& [key, value] : m_runHeader)
339  runHeader.putParameter(key, value);
340 
341  m_runNo = m_runNumberOffset > 0 ? m_runNumberOffset + run->GetRunID() : run->GetRunID();
342  runHeader.putParameter("runNumber", m_runNo);
343  runHeader.putParameter("GEANT4Version", G4Version);
344  runHeader.putParameter("DD4hepVersion", versionString());
345  runHeader.putParameter("detectorName", context()->detectorDescription().header().name());
346  {
347  // In multithreaded running, the run is present in only one of the contexts
348  if (context()->runPtr() != nullptr) {
349  RunParameters* parameters = context()->run().extension<RunParameters>(false);
350  if ( parameters ) {
351  parameters->extractParameters(runHeader);
352  }
353  m_file->writeFrame(runHeader, "runs");
354  }
355  }
356  {
357  // In multithreaded running, the run is present in only one of the contexts
358  if (context()->runPtr() != nullptr) {
359  podio::Frame metaFrame {};
360  FileParameters* parameters = context()->run().extension<FileParameters>(false);
361  if ( parameters ) {
362  parameters->extractParameters(metaFrame);
363  }
364  m_file->writeFrame(metaFrame, "meta");
365  }
366  }
367 }
368 
369 void Geant4Output2EDM4hep::begin(const G4Event* event) {
371  m_eventNo = event->GetEventID();
372  m_frame = {};
373  m_particles = {};
374  m_trackerHits.clear();
375  m_calorimeterHits.clear();
376 }
377 
380  typedef detail::ReferenceBitMask<const int> PropertyMask;
381  typedef Geant4ParticleMap::ParticleMap ParticleMap;
382  const ParticleMap& pm = particles->particleMap;
383 
384  m_particles.clear();
385  if ( pm.size() > 0 ) {
386  size_t cnt = 0;
387  // Mapping of ids in the ParticleMap to indices in the MCParticle collection
388  std::map<int,int> p_ids;
389  std::vector<const Geant4Particle*> p_part;
390  p_part.reserve(pm.size());
391  // First create the particles
392  for (const auto& iParticle : pm) {
393  int id = iParticle.first;
394  const Geant4ParticleHandle p = iParticle.second;
395  PropertyMask mask(p->status);
396  // std::cout << " ********** mcp status : 0x" << std::hex << p->status << ", mask.isSet(G4PARTICLE_GEN_STABLE) x" << std::dec << mask.isSet(G4PARTICLE_GEN_STABLE) <<std::endl ;
397  const G4ParticleDefinition* def = p.definition();
398  auto mcp = m_particles.create();
399  mcp.setPDG(p->pdgID);
400  // Because EDM4hep is switching between vector3f[loat] and vector3d[ouble]
401  using MT = decltype(std::declval<edm4hep::MCParticle>().getMomentum().x);
402  mcp.setMomentum( {MT(p->psx/CLHEP::GeV),MT(p->psy/CLHEP::GeV),MT(p->psz/CLHEP::GeV)} );
403  mcp.setMomentumAtEndpoint( {MT(p->pex/CLHEP::GeV),MT(p->pey/CLHEP::GeV),MT(p->pez/CLHEP::GeV)} );
404 
405  double vs_fa[3] = { p->vsx/CLHEP::mm, p->vsy/CLHEP::mm, p->vsz/CLHEP::mm } ;
406  mcp.setVertex( vs_fa );
407 
408  double ve_fa[3] = { p->vex/CLHEP::mm, p->vey/CLHEP::mm, p->vez/CLHEP::mm } ;
409  mcp.setEndpoint( ve_fa );
410 
411  mcp.setTime(p->time/CLHEP::ns);
412  mcp.setMass(p->mass/CLHEP::GeV);
413  mcp.setCharge(def ? def->GetPDGCharge() : 0); // Charge(e+) = 1 !
414 
415  // Set generator status
416  mcp.setGeneratorStatus(0);
417  if( p->genStatus ) {
418  mcp.setGeneratorStatus( p->genStatus ) ;
419  } else {
420  if ( mask.isSet(G4PARTICLE_GEN_STABLE) ) mcp.setGeneratorStatus(1);
421  else if ( mask.isSet(G4PARTICLE_GEN_DECAYED) ) mcp.setGeneratorStatus(2);
422  else if ( mask.isSet(G4PARTICLE_GEN_DOCUMENTATION) ) mcp.setGeneratorStatus(3);
423  else if ( mask.isSet(G4PARTICLE_GEN_BEAM) ) mcp.setGeneratorStatus(4);
424  else if ( mask.isSet(G4PARTICLE_GEN_OTHER) ) mcp.setGeneratorStatus(9);
425  }
426 
427  // Set simulation status
428  mcp.setCreatedInSimulation( mask.isSet(G4PARTICLE_SIM_CREATED) );
429  mcp.setBackscatter( mask.isSet(G4PARTICLE_SIM_BACKSCATTER) );
430  mcp.setVertexIsNotEndpointOfParent( mask.isSet(G4PARTICLE_SIM_PARENT_RADIATED) );
431  mcp.setDecayedInTracker( mask.isSet(G4PARTICLE_SIM_DECAY_TRACKER) );
432  mcp.setDecayedInCalorimeter( mask.isSet(G4PARTICLE_SIM_DECAY_CALO) );
433  mcp.setHasLeftDetector( mask.isSet(G4PARTICLE_SIM_LEFT_DETECTOR) );
434  mcp.setStopped( mask.isSet(G4PARTICLE_SIM_STOPPED) );
435  mcp.setOverlay( false );
436 
437  //fg: if simstatus !=0 we have to set the generator status to 0:
438  if( mcp.isCreatedInSimulation() )
439  mcp.setGeneratorStatus( 0 ) ;
440 
441  mcp.setSpin(p->spin);
442 
443  p_ids[id] = cnt++;
444  p_part.push_back(p);
445  }
446 
447  // Now establish parent-daughter relationships
448  for(size_t i=0; i < p_ids.size(); ++i) {
449  const Geant4Particle* p = p_part[i];
450  auto q = m_particles[i];
451 
452  for (const auto& idau : p->daughters) {
453  const auto k = p_ids.find(idau);
454  if (k == p_ids.end()) {
455  fatal("+++ Particle %d: FAILED to find daughter with ID:%d",p->id,idau);
456  continue;
457  }
458  int iqdau = (*k).second;
459  auto qdau = m_particles[iqdau];
460  q.addToDaughters(qdau);
461  }
462 
463  for (const auto& ipar : p->parents) {
464  if (ipar >= 0) { // A parent ID of -1 means NO parent, because a base of 0 is perfectly legal
465  const auto k = p_ids.find(ipar);
466  if (k == p_ids.end()) {
467  fatal("+++ Particle %d: FAILED to find parent with ID:%d",p->id,ipar);
468  continue;
469  }
470  int iqpar = (*k).second;
471  auto qpar = m_particles[iqpar];
472  q.addToParents(qpar);
473  }
474  }
475  }
476  }
477 }
478 
481  EventParameters* parameters = context()->event().extension<EventParameters>(false);
482  int runNumber(0), eventNumber(0);
483  const int eventNumberOffset(m_eventNumberOffset > 0 ? m_eventNumberOffset : 0);
484  const int runNumberOffset(m_runNumberOffset > 0 ? m_runNumberOffset : 0);
485  double eventWeight{0};
486  // Get event number, run number and parameters from extension ...
487  if ( parameters ) {
488  runNumber = parameters->runNumber() + runNumberOffset;
489  eventNumber = parameters->eventNumber() + eventNumberOffset;
490  parameters->extractParameters(m_frame);
491 #if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
492  eventWeight = m_frame.getParameter<double>("EventWeights").value_or(0.0);
493 #else
494  eventWeight = m_frame.getParameter<double>("EventWeights");
495 #endif
496  } else { // ... or from DD4hep framework
497  runNumber = m_runNo + runNumberOffset;
498  eventNumber = ctxt.context->GetEventID() + eventNumberOffset;
499  }
500  printout(INFO,"Geant4Output2EDM4hep","+++ Saving EDM4hep event %d run %d.", eventNumber, runNumber);
501 
502  // this does not compile as create() is we only get a const ref - need to review PODIO EventStore API
503  edm4hep::EventHeaderCollection header_collection;
504 
505  auto header = header_collection.create();
506  header.setRunNumber(runNumber);
507  header.setEventNumber(eventNumber);
508  header.setWeight(eventWeight);
509  //not implemented in EDM4hep ? header.setDetectorName(context()->detectorDescription().header().name());
510  header.setTimeStamp(std::time(nullptr));
511 
512  // extract event header, in case we come from edm4hep input
513  auto* meh = context()->event().extension<edm4hep::MutableEventHeader>(false);
514  if(meh) {
515  header.setTimeStamp(meh->getTimeStamp());
516 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 0)
517  for (auto const& weight: meh->getWeights()) {
518  header.addToWeights(weight);
519  }
520 #endif
521  }
522 
523  m_frame.put(std::move(header_collection), "EventHeader");
524  saveEventParameters<int>(m_eventParametersInt);
525  saveEventParameters<float>(m_eventParametersFloat);
526  saveEventParameters<std::string>(m_eventParametersString);
527 
528  Geant4ParticleMap* part_map = context()->event().extension<Geant4ParticleMap>(false);
529  if ( part_map ) {
530  print("+++ Saving %d EDM4hep particles....",int(part_map->particleMap.size()));
531  if ( part_map->particleMap.size() > 0 ) {
532  saveParticles(part_map);
533  }
534  }
535 }
536 
547  operator std::string() const {
548  const auto* sd = m_coll->sensitive();
549  return dd4hep::sim::Geant4ConversionHelper::encoding(sd->sensitiveDetector());
550  }
551 private:
552  Geant4HitCollection* m_coll{nullptr};
553 };
554 
555 
558  Geant4HitCollection* coll = dynamic_cast<Geant4HitCollection*>(collection);
559  std::string colName = collection->GetName();
560  if( coll == nullptr ){
561  error(" no Geant4HitCollection: %s ", colName.c_str());
562  return ;
563  }
564  size_t nhits = collection->GetSize();
566  debug("+++ Saving EDM4hep collection %s with %d entries.", colName.c_str(), int(nhits));
567 
568  // Using try_emplace here to only fill this the first time we come across
569  m_cellIDEncodingStrings.try_emplace(colName, LazyEncodingExtraction{coll});
570 
571  //-------------------------------------------------------------------
572  if( typeid( Geant4Tracker::Hit ) == coll->type().type() ){
573  // Create the hit container even if there are no entries!
574  auto& hits = m_trackerHits[colName];
575  for(unsigned i=0 ; i < nhits ; ++i){
576  auto sth = hits->create();
577  const Geant4Tracker::Hit* hit = coll->hit(i);
578  const Geant4Tracker::Hit::Contribution& t = hit->truth;
579  int trackID = pm->particleID(t.trackID);
580  auto mcp = m_particles.at(trackID);
581  const auto& mom = hit->momentum;
582  const auto& pos = hit->position;
583  edm4hep::Vector3f();
584  sth.setCellID( hit->cellID ) ;
585  sth.setEDep(hit->energyDeposit/CLHEP::GeV);
586  sth.setPathLength(hit->length/CLHEP::mm);
587  sth.setTime(hit->truth.time/CLHEP::ns);
588 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 10, 99)
589  sth.setParticle(mcp);
590 #else
591  sth.setMCParticle(mcp);
592 #endif
593  sth.setPosition( {pos.x()/CLHEP::mm, pos.y()/CLHEP::mm, pos.z()/CLHEP::mm} );
594  sth.setMomentum( {float(mom.x()/CLHEP::GeV),float(mom.y()/CLHEP::GeV),float(mom.z()/CLHEP::GeV)} );
595  auto particleIt = pm->particles().find(trackID);
596  if( ( particleIt != pm->particles().end()) ){
597  // if the original track ID of the particle is not the same as the
598  // original track ID of the hit it was produced by an MCParticle that
599  // is no longer stored
600  sth.setProducedBySecondary( (particleIt->second->originalG4ID != t.trackID) );
601  }
602  }
603  //-------------------------------------------------------------------
604  }
605  else if( typeid( Geant4Calorimeter::Hit ) == coll->type().type() ){
606  Geant4Sensitive* sd = coll->sensitive();
607  int hit_creation_mode = sd->hitCreationMode();
608  // Create the hit container even if there are no entries!
609  auto& hits = m_calorimeterHits[colName];
610  for(unsigned i=0 ; i < nhits ; ++i){
611  auto sch = hits.first->create();
612  const Geant4Calorimeter::Hit* hit = coll->hit(i);
613  const auto& pos = hit->position;
614  sch.setCellID( hit->cellID );
615  sch.setPosition({float(pos.x()/CLHEP::mm), float(pos.y()/CLHEP::mm), float(pos.z()/CLHEP::mm)});
616  sch.setEnergy( hit->energyDeposit/CLHEP::GeV );
617 
618 
619  // now add the individual step contributions
620  for(auto ci=hit->truth.begin(); ci != hit->truth.end(); ++ci){
621 
622  auto sCaloHitCont = hits.second->create();
623  sch.addToContributions( sCaloHitCont );
624 
625  const Geant4HitData::Contribution& c = *ci;
626  int trackID = pm->particleID(c.trackID);
627  auto mcp = m_particles.at(trackID);
628  sCaloHitCont.setEnergy( c.deposit/CLHEP::GeV );
629  sCaloHitCont.setTime( c.time/CLHEP::ns );
630  sCaloHitCont.setParticle( mcp );
631 
632  if ( hit_creation_mode == Geant4Sensitive::DETAILED_MODE ) {
633  edm4hep::Vector3f p(c.x/CLHEP::mm, c.y/CLHEP::mm, c.z/CLHEP::mm);
634  sCaloHitCont.setPDG( c.pdgID );
635  sCaloHitCont.setStepPosition( p );
636  }
637  }
638  }
639  //-------------------------------------------------------------------
640  } else {
641  error("+++ unknown type in Geant4HitCollection %s ", coll->type().type().name());
642  }
643 }
dd4hep::sim::Geant4Output2EDM4hep::m_eventParametersFloat
stringmap_t m_eventParametersFloat
Definition: Geant4Output2EDM4hep.cpp:82
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:81
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:263
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:83
Geant4HitCollection.h
dd4hep::sim::Geant4Output2EDM4hep::m_eventNo
int m_eventNo
Definition: Geant4Output2EDM4hep.cpp:88
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:80
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:332
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:310
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:285
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:557
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:86
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:541
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:480
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:379
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:76
RunParameters.h
dd4hep::sim::Geant4Output2EDM4hep
Base class to output Geant4 event data to EDM4hep.
Definition: Geant4Output2EDM4hep.cpp:67
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:73
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:70
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:85
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:78
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:120
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:72
dd4hep::sim::Geant4Output2EDM4hep::m_calorimeterHits
calorimetermap_t m_calorimeterHits
Definition: Geant4Output2EDM4hep.cpp:79
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:257
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:544
dd4hep::sim::Geant4Output2EDM4hep::m_cellIDEncodingStrings
stringmap_t m_cellIDEncodingStrings
Definition: Geant4Output2EDM4hep.cpp:84
dd4hep::sim::Geant4Output2EDM4hep::saveFileMetaData
void saveFileMetaData()
Store the metadata frame with e.g. the cellID encoding strings.
Definition: Geant4Output2EDM4hep.cpp:300
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:74
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:87
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:537
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:112
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:90
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::Geant4Context::runPtr
Geant4Run * runPtr() const
Access the geant4 run by ptr. Must be checked by clients!
Definition: Geant4Context.h:225
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:77
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:89
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:241
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:71
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:369
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
dd4hep::sim::Geant4Output2EDM4hep::m_fileUseCount
std::atomic_size_t m_fileUseCount
Definition: Geant4Output2EDM4hep.cpp:75
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:69
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