13 #ifndef DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
14 #define DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
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;
33 using edm4hep::labels::CellIDEncoding;
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>
43 #include <podio/ROOTFrameWriter.h>
45 using ROOTWriter = podio::ROOTFrameWriter;
71 using trackermap_t = std::map< std::string, edm4hep::SimTrackerHitCollection >;
72 using calorimeterpair_t = std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection >;
74 std::unique_ptr<writer_t>
m_file { };
102 virtual void beginRun(
const G4Run* run);
104 virtual void endRun(
const G4Run* run);
107 virtual void saveRun(
const G4Run* run);
109 virtual void saveEvent( OutputContext<G4Event>& ctxt);
113 virtual void commit( OutputContext<G4Event>& ctxt);
116 virtual void begin(
const G4Event* event);
119 template <
typename T>
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);
130 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
131 frame.putParameter(p.first, p.second);
134 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
135 frame.putParameter(p.first, p.second);
138 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
139 frame.putParameter(p.first, p.second);
143 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
144 frame.putParameter(p.first, p.second);
150 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
151 frame.putParameter(p.first, p.second);
154 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
155 frame.putParameter(p.first, p.second);
158 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
159 frame.putParameter(p.first, p.second);
163 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
164 frame.putParameter(p.first, p.second);
169 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
170 frame.putParameter(p.first, p.second);
173 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
174 frame.putParameter(p.first, p.second);
177 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
178 frame.putParameter(p.first, p.second);
182 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
183 frame.putParameter(p.first, p.second);
189 #endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
217 #include <G4Threading.hh>
218 #include <G4AutoLock.hh>
219 #include <G4Version.hh>
220 #include <G4ParticleDefinition.hh>
221 #include <G4VProcess.hh>
222 #include <G4Event.hh>
225 #include <CLHEP/Units/SystemOfUnits.h>
228 #include <edm4hep/EventHeaderCollection.h>
234 G4Mutex action_mutex = G4MUTEX_INITIALIZER;
242 :
Geant4OutputAction(ctxt,nam), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
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 ..." );
258 G4AutoLock protection_lock(&action_mutex);
264 G4AutoLock protection_lock(&action_mutex);
268 std::size_t idx =
m_output.rfind(
".");
269 if ( idx != std::string::npos ) {
274 if ( !fname.empty() && !
m_file ) {
275 m_file = std::make_unique<podio::ROOTWriter>(fname);
277 fatal(
"+++ Failed to open output file: %s", fname.c_str());
279 printout( INFO,
"Geant4Output2EDM4hep" ,
"Opened %s for output", fname.c_str() ) ;
292 G4AutoLock protection_lock(&action_mutex);
301 podio::Frame metaFrame{};
303 metaFrame.putParameter(podio::collMetadataParamName(
name, CellIDEncoding), encodingStr);
305 G4AutoLock protection_lock(&action_mutex);
306 m_file->writeFrame(metaFrame,
"metadata");
312 G4AutoLock protection_lock(&action_mutex);
315 m_frame.put( std::move(it->second), it->first);
318 m_frame.put( std::move(calorimeterHits.first), colName);
319 m_frame.put( std::move(calorimeterHits.second), colName +
"Contributions");
328 except(
"+++ Failed to write output file. [Stream is not open]");
333 G4AutoLock protection_lock(&action_mutex);
337 podio::Frame runHeader {};
339 runHeader.putParameter(
key, value);
342 runHeader.putParameter(
"runNumber",
m_runNo);
343 runHeader.putParameter(
"GEANT4Version", G4Version);
345 runHeader.putParameter(
"detectorName",
context()->detectorDescription().header().
name());
348 if (
context()->runPtr() !=
nullptr) {
353 m_file->writeFrame(runHeader,
"runs");
359 podio::Frame metaFrame {};
364 m_file->writeFrame(metaFrame,
"meta");
380 typedef detail::ReferenceBitMask<const int>
PropertyMask;
385 if ( pm.size() > 0 ) {
388 std::map<int,int> p_ids;
389 std::vector<const Geant4Particle*> p_part;
390 p_part.reserve(pm.size());
392 for (
const auto& iParticle : pm) {
393 int id = iParticle.first;
397 const G4ParticleDefinition* def = p.
definition();
399 mcp.setPDG(p->
pdgID);
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)} );
405 double vs_fa[3] = { p->
vsx/CLHEP::mm, p->
vsy/CLHEP::mm, p->
vsz/CLHEP::mm } ;
406 mcp.setVertex( vs_fa );
408 double ve_fa[3] = { p->
vex/CLHEP::mm, p->
vey/CLHEP::mm, p->
vez/CLHEP::mm } ;
409 mcp.setEndpoint( ve_fa );
411 mcp.setTime(p->
time/CLHEP::ns);
412 mcp.setMass(p->
mass/CLHEP::GeV);
413 mcp.setCharge(def ? def->GetPDGCharge() : 0);
416 mcp.setGeneratorStatus(0);
435 mcp.setOverlay(
false );
438 if( mcp.isCreatedInSimulation() )
439 mcp.setGeneratorStatus( 0 ) ;
441 mcp.setSpin(p->
spin);
448 for(
size_t i=0; i < p_ids.size(); ++i) {
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);
458 int iqdau = (*k).second;
460 q.addToDaughters(qdau);
463 for (
const auto& ipar : p->
parents) {
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);
470 int iqpar = (*k).second;
472 q.addToParents(qpar);
482 int runNumber(0), eventNumber(0);
485 double eventWeight{0};
488 runNumber = parameters->
runNumber() + runNumberOffset;
489 eventNumber = parameters->
eventNumber() + eventNumberOffset;
491 #if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
492 eventWeight =
m_frame.getParameter<
double>(
"EventWeights").value_or(0.0);
494 eventWeight =
m_frame.getParameter<
double>(
"EventWeights");
497 runNumber =
m_runNo + runNumberOffset;
498 eventNumber = ctxt.
context->GetEventID() + eventNumberOffset;
500 printout(INFO,
"Geant4Output2EDM4hep",
"+++ Saving EDM4hep event %d run %d.", eventNumber, runNumber);
503 edm4hep::EventHeaderCollection header_collection;
505 auto header = header_collection.create();
506 header.setRunNumber(runNumber);
507 header.setEventNumber(eventNumber);
508 header.setWeight(eventWeight);
510 header.setTimeStamp(std::time(
nullptr));
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);
523 m_frame.put(std::move(header_collection),
"EventHeader");
530 print(
"+++ Saving %d EDM4hep particles....",
int(part_map->
particleMap.size()));
547 operator std::string()
const {
548 const auto* sd = m_coll->sensitive();
559 std::string colName = collection->GetName();
560 if( coll ==
nullptr ){
561 error(
" no Geant4HitCollection: %s ", colName.c_str());
564 size_t nhits = collection->GetSize();
566 debug(
"+++ Saving EDM4hep collection %s with %d entries.", colName.c_str(),
int(nhits));
575 for(
unsigned i=0 ; i < nhits ; ++i){
576 auto sth = hits->create();
584 sth.setCellID( hit->
cellID ) ;
586 sth.setPathLength(hit->
length/CLHEP::mm);
588 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 10, 99)
589 sth.setParticle(mcp);
591 sth.setMCParticle(mcp);
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()) ){
600 sth.setProducedBySecondary( (particleIt->second->originalG4ID != t.
trackID) );
610 for(
unsigned i=0 ; i < nhits ; ++i){
611 auto sch = hits.first->create();
614 sch.setCellID( hit->
cellID );
615 sch.setPosition({float(pos.x()/CLHEP::mm), float(pos.y()/CLHEP::mm), float(pos.z()/CLHEP::mm)});
620 for(
auto ci=hit->
truth.begin(); ci != hit->
truth.end(); ++ci){
622 auto sCaloHitCont = hits.second->create();
623 sch.addToContributions( sCaloHitCont );
628 sCaloHitCont.setEnergy( c.
deposit/CLHEP::GeV );
629 sCaloHitCont.setTime( c.
time/CLHEP::ns );
630 sCaloHitCont.setParticle( mcp );
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 );
641 error(
"+++ unknown type in Geant4HitCollection %s ", coll->
type().type().name());