diff --git a/DDG4/edm4hep/Geant4Output2EDM4hep.cpp b/DDG4/edm4hep/Geant4Output2EDM4hep.cpp index f043add6133e44b2e2fc5ffddd8ba49bcc21a7ff..6d4acdcc26cd5c69f265ae818a47a45622d6b7c7 100644 --- a/DDG4/edm4hep/Geant4Output2EDM4hep.cpp +++ b/DDG4/edm4hep/Geant4Output2EDM4hep.cpp @@ -17,6 +17,7 @@ #include <DD4hep/Detector.h> #include <DDG4/EventParameters.h> #include <DDG4/Geant4OutputAction.h> +#include <DDG4/RunParameters.h> /// edm4hep include files #include <edm4hep/MCParticleCollection.h> @@ -125,6 +126,29 @@ namespace dd4hep { } #endif } + + template <> void RunParameters::extractParameters(podio::Frame& frame) { + for(auto const& p: this->intParameters()) { + printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str()); + frame.putParameter(p.first, p.second); + } + for(auto const& p: this->fltParameters()) { + printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str()); + frame.putParameter(p.first, p.second); + } + for(auto const& p: this->strParameters()) { + printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str()); + frame.putParameter(p.first, p.second); + } +#if podio_VERSION_MAJOR > 0 || podio_VERSION_MINOR > 16 || podio_VERSION_PATCH > 2 + // This functionality is only present in podio > 0.16.2 + for (auto const& p: this->dblParameters()) { + printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str()); + frame.putParameter(p.first, p.second); + } +#endif + } + } // End namespace sim } // End namespace dd4hep #endif // DD4HEP_DDG4_GEANT4OUTPUT2EDM4hep_H @@ -278,6 +302,11 @@ void Geant4Output2EDM4hep::saveRun(const G4Run* run) { runHeader.putParameter("DD4hepVersion", versionString()); runHeader.putParameter("detectorName", context()->detectorDescription().header().name()); + RunParameters* parameters = context()->run().extension<RunParameters>(false); + if ( parameters ) { + parameters->extractParameters(runHeader); + } + m_file->writeFrame(runHeader, "runs"); } diff --git a/DDG4/hepmc/HepMC3FileReader.cpp b/DDG4/hepmc/HepMC3FileReader.cpp index 5bbbf6e679121adb99d626d639f4155c45a1ca9e..9d03040d348c662c408bfebf7d2d087730acce50 100644 --- a/DDG4/hepmc/HepMC3FileReader.cpp +++ b/DDG4/hepmc/HepMC3FileReader.cpp @@ -72,7 +72,6 @@ namespace dd4hep { template <class T=HepMC3::GenRunInfo> void RunParameters::ingestParameters(T const& runInfo) { // This attributes is not the same return type as for GenEvent! for(auto const& attr: runInfo.attributes()){ - std::stringstream strstr; if(auto int_attr = std::dynamic_pointer_cast<HepMC3::IntAttribute>(attr.second)) { m_intValues[attr.first] = {int_attr->value()}; } else if(auto flt_attr = std::dynamic_pointer_cast<HepMC3::FloatAttribute>(attr.second)) { @@ -128,6 +127,10 @@ HEPMC3FileReader::HEPMC3FileReader(const std::string& nam) { m_reader = HepMC3::deduce_reader(nam); printout(INFO,"HEPMC3FileReader","Created file reader. Try to open input %s", nam.c_str()); + // to read potential run_info in HepMC3 ASCII files, we have to call skip(-1), otherwise run_info is only read when we + // read the first event. This is different for different readers, and may or may not work :shrug: + // skip(-1) is also a no-op for RootReader, and for RootReader the RunInfo is read when the file is opened + m_reader->skip(-1); m_directAccess = false; } @@ -136,7 +139,8 @@ void HEPMC3FileReader::registerRunParameters() { auto *parameters = new RunParameters(); parameters->ingestParameters(*(m_reader->run_info())); context()->run().addExtension<RunParameters>(parameters); - } catch(std::exception &) { + } catch(std::exception &e) { + printout(ERROR,"HEPMC3FileReader::registerRunParameters","Failed to register run parameters: %s", e.what()); } } diff --git a/DDG4/include/DDG4/RunParameters.h b/DDG4/include/DDG4/RunParameters.h new file mode 100644 index 0000000000000000000000000000000000000000..05557f18e3315f99e8c2b994d2f92556592380fb --- /dev/null +++ b/DDG4/include/DDG4/RunParameters.h @@ -0,0 +1,68 @@ +//========================================================================== +// AIDA Detector description implementation +//-------------------------------------------------------------------------- +// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN) +// All rights reserved. +// +// For the licensing terms see $DD4hepINSTALL/LICENSE. +// For the list of contributors see $DD4hepINSTALL/doc/CREDITS. +// +// +//========================================================================== +#ifndef DDG4_RUNPARAMETERS_H +#define DDG4_RUNPARAMETERS_H + +#include <map> +#include <string> +#include <vector> + + +/// Namespace for the AIDA detector description toolkit +namespace dd4hep { + + /// Namespace for the Geant4 based simulation part of the AIDA detector description toolkit + namespace sim { + + /// Extension to pass input run data to output run data + /** + * \version 1.0 + * \ingroup DD4HEP_SIMULATION + */ + class RunParameters { + protected: + std::map<std::string, std::vector<int>> m_intValues {}; + std::map<std::string, std::vector<float>> m_fltValues {}; + std::map<std::string, std::vector<std::string>> m_strValues {}; + std::map<std::string, std::vector<double>> m_dblValues {}; + int m_runNumber = -1; + + public: + /// Initializing constructor + RunParameters() = default; + /// Default destructor + ~RunParameters() = default; + + /// Set the Run parameters + void setRunNumber(int runNumber); + /// Get the run number + int runNumber() const { return m_runNumber; } + + /// Copy the parameters from source + template <class T> void ingestParameters(T const& source); + /// Put parameters into destination + template <class T> void extractParameters(T& destination); + + /// Get the int Run parameters + auto const& intParameters() const { return m_intValues; } + /// Get the float Run parameters + auto const& fltParameters() const { return m_fltValues; } + /// Get the string Run parameters + auto const& strParameters() const { return m_strValues; } + /// Get the double Run parameters + auto const& dblParameters() const { return m_dblValues; } + + }; + + } /* End namespace sim */ +} /* End namespace dd4hep */ +#endif // DDG4_RUNPARAMETERS_H