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 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 3)
36 #include <edm4hep/GeneratorEventParametersCollection.h>
40 #include <podio/CollectionBase.h>
41 #include <podio/podioVersion.h>
42 #include <podio/Frame.h>
43 #include <podio/FrameCategories.h>
44 #if PODIO_BUILD_VERSION >= PODIO_VERSION(0, 99, 0)
45 #include <podio/ROOTWriter.h>
47 #include <podio/ROOTFrameWriter.h>
49 using ROOTWriter = podio::ROOTFrameWriter;
77 using trackermap_t = std::map< std::string, edm4hep::SimTrackerHitCollection >;
78 using calorimeterpair_t = std::pair< edm4hep::SimCalorimeterHitCollection, edm4hep::CaloHitContributionCollection >;
80 std::unique_ptr<writer_t>
m_file { };
111 virtual void beginRun(
const G4Run* run);
113 virtual void endRun(
const G4Run* run);
116 virtual void saveRun(
const G4Run* run);
118 virtual void saveEvent( OutputContext<G4Event>& ctxt);
122 virtual void commit( OutputContext<G4Event>& ctxt);
125 virtual void begin(
const G4Event* event);
128 template <
typename T>
130 for(
const auto& p : parameters) {
131 std::stringstream output;
132 output <<
"Saving event parameter: "
133 << std::setw(32) << p.first
134 << std::setw(20) << p.second;
135 info(output.str().c_str());
136 m_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);
147 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
148 frame.putParameter(p.first, p.second);
151 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
152 frame.putParameter(p.first, p.second);
156 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving event parameter: %s", p.first.c_str());
157 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);
167 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
168 frame.putParameter(p.first, p.second);
171 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
172 frame.putParameter(p.first, p.second);
176 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving run parameter: %s", p.first.c_str());
177 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);
186 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
187 frame.putParameter(p.first, p.second);
190 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
191 frame.putParameter(p.first, p.second);
195 printout(DEBUG,
"Geant4OutputEDM4hep",
"Saving meta parameter: %s", p.first.c_str());
196 frame.putParameter(p.first, p.second);
202 #endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H
230 #include <G4Threading.hh>
231 #include <G4AutoLock.hh>
232 #include <G4Version.hh>
233 #include <G4ParticleDefinition.hh>
234 #include <G4VProcess.hh>
235 #include <G4Event.hh>
238 #include <CLHEP/Units/SystemOfUnits.h>
241 #include <edm4hep/EventHeaderCollection.h>
247 G4Mutex action_mutex = G4MUTEX_INITIALIZER;
255 :
Geant4OutputAction(ctxt,nam), m_runNo(0), m_runNumberOffset(0), m_eventNumberOffset(0)
257 declareProperty(
"RunHeader", m_runHeader);
258 declareProperty(
"EventParametersInt", m_eventParametersInt);
259 declareProperty(
"EventParametersFloat", m_eventParametersFloat);
260 declareProperty(
"EventParametersString", m_eventParametersString);
261 declareProperty(
"RunParametersInt", m_runParametersInt);
262 declareProperty(
"RunParametersFloat", m_runParametersFloat);
263 declareProperty(
"RunParametersString", m_runParametersString);
264 declareProperty(
"RunNumberOffset", m_runNumberOffset);
265 declareProperty(
"EventNumberOffset", m_eventNumberOffset);
266 declareProperty(
"SectionName", m_section_name);
267 declareProperty(
"FilesByRun", m_filesByRun);
268 info(
"Writer is now instantiated ..." );
274 G4AutoLock protection_lock(&action_mutex);
280 G4AutoLock protection_lock(&action_mutex);
284 std::size_t idx =
m_output.rfind(
".");
285 if ( idx != std::string::npos ) {
290 if ( !fname.empty() && !
m_file ) {
291 m_file = std::make_unique<podio::ROOTWriter>(fname);
293 fatal(
"+++ Failed to open output file: %s", fname.c_str());
295 printout( INFO,
"Geant4Output2EDM4hep" ,
"Opened %s for output", fname.c_str() ) ;
308 G4AutoLock protection_lock(&action_mutex);
317 podio::Frame metaFrame{};
319 metaFrame.putParameter(podio::collMetadataParamName(
name, CellIDEncoding), encodingStr);
321 G4AutoLock protection_lock(&action_mutex);
322 m_file->writeFrame(metaFrame,
"metadata");
328 G4AutoLock protection_lock(&action_mutex);
331 m_frame.put( std::move(it->second), it->first);
334 m_frame.put( std::move(calorimeterHits.first), colName);
335 m_frame.put( std::move(calorimeterHits.second), colName +
"Contributions");
344 except(
"+++ Failed to write output file. [Stream is not open]");
349 G4AutoLock protection_lock(&action_mutex);
353 podio::Frame runHeader {};
355 runHeader.putParameter(
key, value);
358 runHeader.putParameter(
key, value);
361 runHeader.putParameter(
key, value);
364 runHeader.putParameter(
key, value);
367 runHeader.putParameter(
"runNumber",
m_runNo);
368 runHeader.putParameter(
"GEANT4Version", G4Version);
370 runHeader.putParameter(
"detectorName",
context()->detectorDescription().header().
name());
373 if (
context()->runPtr() !=
nullptr) {
378 m_file->writeFrame(runHeader,
"runs");
384 podio::Frame metaFrame {};
389 m_file->writeFrame(metaFrame,
"meta");
405 typedef detail::ReferenceBitMask<const int>
PropertyMask;
410 if ( pm.size() > 0 ) {
413 std::map<int,int> p_ids;
414 std::vector<const Geant4Particle*> p_part;
415 p_part.reserve(pm.size());
417 for (
const auto& iParticle : pm) {
418 int id = iParticle.first;
422 const G4ParticleDefinition* def = p.
definition();
424 mcp.setPDG(p->
pdgID);
426 using MT = decltype(std::declval<edm4hep::MCParticle>().getMomentum().x);
427 mcp.setMomentum( {MT(p->
psx/CLHEP::GeV),MT(p->
psy/CLHEP::GeV),MT(p->
psz/CLHEP::GeV)} );
428 mcp.setMomentumAtEndpoint( {MT(p->
pex/CLHEP::GeV),MT(p->
pey/CLHEP::GeV),MT(p->
pez/CLHEP::GeV)} );
430 double vs_fa[3] = { p->
vsx/CLHEP::mm, p->
vsy/CLHEP::mm, p->
vsz/CLHEP::mm } ;
431 mcp.setVertex( vs_fa );
433 double ve_fa[3] = { p->
vex/CLHEP::mm, p->
vey/CLHEP::mm, p->
vez/CLHEP::mm } ;
434 mcp.setEndpoint( ve_fa );
436 mcp.setTime(p->
time/CLHEP::ns);
437 mcp.setMass(p->
mass/CLHEP::GeV);
438 mcp.setCharge(def ? def->GetPDGCharge() : 0);
441 mcp.setGeneratorStatus(0);
460 mcp.setOverlay(
false );
463 if( mcp.isCreatedInSimulation() )
464 mcp.setGeneratorStatus( 0 ) ;
466 #if EDM4HEP_MCPARTICLE_HAS_HELICITY
467 mcp.setHelicity(p->
spin[2]);
469 mcp.setSpin(p->
spin);
477 for(
size_t i=0; i < p_ids.size(); ++i) {
482 const auto k = p_ids.find(idau);
483 if (k == p_ids.end()) {
484 fatal(
"+++ Particle %d: FAILED to find daughter with ID:%d",p->
id,idau);
487 int iqdau = (*k).second;
489 q.addToDaughters(qdau);
492 for (
const auto& ipar : p->
parents) {
494 const auto k = p_ids.find(ipar);
495 if (k == p_ids.end()) {
496 fatal(
"+++ Particle %d: FAILED to find parent with ID:%d",p->
id,ipar);
499 int iqpar = (*k).second;
501 q.addToParents(qpar);
511 int runNumber(0), eventNumber(0);
514 double eventWeight{0};
517 runNumber = parameters->
runNumber() + runNumberOffset;
518 eventNumber = parameters->
eventNumber() + eventNumberOffset;
520 #if PODIO_BUILD_VERSION > PODIO_VERSION(0, 99, 0)
521 eventWeight =
m_frame.getParameter<
double>(
"EventWeights").value_or(0.0);
523 eventWeight =
m_frame.getParameter<
double>(
"EventWeights");
526 runNumber =
m_runNo + runNumberOffset;
527 eventNumber = ctxt.
context->GetEventID() + eventNumberOffset;
529 printout(INFO,
"Geant4Output2EDM4hep",
"+++ Saving EDM4hep event %d run %d.", eventNumber, runNumber);
532 edm4hep::EventHeaderCollection header_collection;
534 auto header = header_collection.create();
535 header.setRunNumber(runNumber);
536 header.setEventNumber(eventNumber);
537 header.setWeight(eventWeight);
539 header.setTimeStamp(std::time(
nullptr));
544 header.setTimeStamp(meh->getTimeStamp());
545 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 0)
546 for (
auto const& weight: meh->getWeights()) {
547 header.addToWeights(weight);
552 m_frame.put(std::move(header_collection),
"EventHeader");
554 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 3)
558 edm4hep::GeneratorEventParametersCollection genEvtParamsColl{};
559 genEvtParamsColl.push_back(*genEvtParams);
560 m_frame.put(std::move(genEvtParamsColl), edm4hep::labels::GeneratorEventParameters);
570 print(
"+++ Saving %d EDM4hep particles....",
int(part_map->
particleMap.size()));
587 operator std::string()
const {
588 const auto* sd = m_coll->sensitive();
599 std::string colName = collection->GetName();
600 if( coll ==
nullptr ){
601 error(
" no Geant4HitCollection: %s ", colName.c_str());
604 size_t nhits = collection->GetSize();
606 debug(
"+++ Saving EDM4hep collection %s with %d entries.", colName.c_str(),
int(nhits));
615 for(
unsigned i=0 ; i < nhits ; ++i){
616 auto sth = hits.create();
624 sth.setCellID( hit->
cellID ) ;
626 sth.setPathLength(hit->
length/CLHEP::mm);
628 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 10, 99)
629 sth.setParticle(mcp);
631 sth.setMCParticle(mcp);
633 sth.setPosition( {pos.x()/CLHEP::mm, pos.y()/CLHEP::mm, pos.z()/CLHEP::mm} );
634 sth.setMomentum( {float(mom.x()/CLHEP::GeV),float(mom.y()/CLHEP::GeV),float(mom.z()/CLHEP::GeV)} );
635 auto particleIt = pm->
particles().find(trackID);
636 if( ( particleIt != pm->
particles().end()) ){
640 sth.setProducedBySecondary( (particleIt->second->originalG4ID != t.
trackID) );
650 for(
unsigned i=0 ; i < nhits ; ++i){
651 auto sch = hits.first.create();
654 sch.setCellID( hit->
cellID );
655 sch.setPosition({float(pos.x()/CLHEP::mm), float(pos.y()/CLHEP::mm), float(pos.z()/CLHEP::mm)});
660 for(
auto ci=hit->
truth.begin(); ci != hit->
truth.end(); ++ci){
662 auto sCaloHitCont = hits.second.create();
663 sch.addToContributions( sCaloHitCont );
668 sCaloHitCont.setEnergy( c.
deposit/CLHEP::GeV );
669 sCaloHitCont.setTime( c.
time/CLHEP::ns );
670 sCaloHitCont.setParticle( mcp );
673 edm4hep::Vector3f p(c.
x/CLHEP::mm, c.
y/CLHEP::mm, c.
z/CLHEP::mm);
674 sCaloHitCont.setPDG( c.
pdgID );
675 sCaloHitCont.setStepPosition( p );
676 #if EDM4HEP_BUILD_VERSION >= EDM4HEP_VERSION(0, 99, 3)
677 sCaloHitCont.setStepLength(c.
length / CLHEP::mm);
684 error(
"+++ unknown type in Geant4HitCollection %s ", coll->
type().type().name());