Skip to content

Commit

Permalink
ReadEDM4hep: add fileParameter, refactor parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
andresailer committed Jan 8, 2025
1 parent e77b06f commit 5906930
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 62 deletions.
23 changes: 21 additions & 2 deletions DDG4/edm4hep/EDM4hepFileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include <DDG4/EventParameters.h>
#include <DDG4/Factories.h>
#include <DDG4/FileParameters.h>
#include <DDG4/Geant4InputAction.h>
#include <DDG4/RunParameters.h>

Expand Down Expand Up @@ -77,6 +78,21 @@ namespace dd4hep::sim {
}
}

template <class T=podio::GenericParameters> void FileParameters::ingestParameters(T const& source) {
for(auto const& key: source.template getKeys<int>()) {
m_intValues[key] = source.template get<std::vector<int>>(key).value();
}
for(auto const& key: source.template getKeys<float>()) {
m_fltValues[key] = source.template get<std::vector<float>>(key).value();
}
for(auto const& key: source.template getKeys<double>()) {
m_dblValues[key] = source.template get<std::vector<double>>(key).value();
}
for(auto const& key: source.template getKeys<std::string>()) {
m_strValues[key] = source.template get<std::vector<std::string>>(key).value();
}
}

/// Class to read EDM4hep files
/**
* \version 1.0
Expand Down Expand Up @@ -127,15 +143,18 @@ namespace dd4hep::sim {
} catch(std::invalid_argument&) {
// we ignore if we do not have runs information
}
context()->run().addExtension<RunParameters>(parameters);

auto *fileParameters = new FileParameters();
try {
podio::Frame metaFrame = m_reader.readFrame("metadata", 0);
parameters->ingestParameters(metaFrame.getParameters());
fileParameters->ingestParameters(metaFrame.getParameters());
} catch (std::runtime_error& e) {
// we ignore if we do not have metadata information
} catch(std::invalid_argument&) {
// we ignore if we do not have metadata information
}
context()->run().addExtension<RunParameters>(parameters);
context()->run().addExtension<FileParameters>(fileParameters);
} catch(std::exception &e) {
printout(ERROR,"EDM4hepFileReader::registerRunParameters","Failed to register run parameters: %s", e.what());
}
Expand Down
44 changes: 37 additions & 7 deletions DDG4/edm4hep/Geant4Output2EDM4hep.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/// Framework include files
#include <DD4hep/Detector.h>
#include <DDG4/EventParameters.h>
#include <DDG4/FileParameters.h>
#include <DDG4/Geant4OutputAction.h>
#include <DDG4/RunParameters.h>

Expand Down Expand Up @@ -157,6 +158,27 @@ namespace dd4hep {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving run parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#endif
}
template <> void FileParameters::extractParameters(podio::Frame& frame) {
for(auto const& p: this->intParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->fltParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
for(auto const& p: this->strParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#if PODIO_BUILD_VERSION > PODIO_VERSION(0, 16, 2)
// This functionality is only present in podio > 0.16.2
for (auto const& p: this->dblParameters()) {
printout(DEBUG, "Geant4OutputEDM4hep", "Saving meta parameter: %s", p.first.c_str());
frame.putParameter(p.first, p.second);
}
#endif
}

Expand Down Expand Up @@ -303,7 +325,7 @@ void Geant4Output2EDM4hep::saveRun(const G4Run* run) {
// --- write an edm4hep::RunHeader ---------
// Runs are just Frames with different contents in EDM4hep / podio. We simply
// store everything as parameters for now
podio::Frame runHeader {};
podio::Frame runHeader {};
for (const auto& [key, value] : m_runHeader)
runHeader.putParameter(key, value);

Expand All @@ -312,13 +334,21 @@ void Geant4Output2EDM4hep::saveRun(const G4Run* run) {
runHeader.putParameter("GEANT4Version", G4Version);
runHeader.putParameter("DD4hepVersion", versionString());
runHeader.putParameter("detectorName", context()->detectorDescription().header().name());

RunParameters* parameters = context()->run().extension<RunParameters>(false);
if ( parameters ) {
parameters->extractParameters(runHeader);
{
RunParameters* parameters = context()->run().extension<RunParameters>(false);
if ( parameters ) {
parameters->extractParameters(runHeader);
}
m_file->writeFrame(runHeader, "runs");
}
{
podio::Frame metaFrame {};
FileParameters* parameters = context()->run().extension<FileParameters>(false);
if ( parameters ) {
parameters->extractParameters(metaFrame);
}
m_file->writeFrame(metaFrame, "meta");
}

m_file->writeFrame(runHeader, "runs");
}

void Geant4Output2EDM4hep::begin(const G4Event* event) {
Expand Down
31 changes: 4 additions & 27 deletions DDG4/include/DDG4/EventParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
#ifndef DDG4_EVENTPARAMETERS_H
#define DDG4_EVENTPARAMETERS_H

#include <map>
#include <string>
#include <vector>

#include <DDG4/ExtensionParameters.h>

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
Expand All @@ -28,43 +25,23 @@ namespace dd4hep {
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class EventParameters {
class EventParameters : public ExtensionParameters {
protected:
int m_runNumber = -1;
int m_eventNumber = -1;
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;
int m_eventNumber = -1;

public:
/// Initializing constructor
EventParameters() = default;
/// Default destructor
~EventParameters() = default;

/// Set the event parameters
void setRunNumber(int runNumber);
void setEventNumber(int eventNumber);
/// Get the run number
int runNumber() const { return m_runNumber; }
/// Get the event number
int eventNumber() const { return m_eventNumber; }

/// 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 event parameters
auto const& intParameters() const { return m_intValues; }
/// Get the float event parameters
auto const& fltParameters() const { return m_fltValues; }
/// Get the string event parameters
auto const& strParameters() const { return m_strValues; }
/// Get the double event parameters
auto const& dblParameters() const { return m_dblValues; }

};

} /* End namespace sim */
Expand Down
52 changes: 52 additions & 0 deletions DDG4/include/DDG4/ExtensionParameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//==========================================================================
// 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_EXTENSIONPARAMETERS_H
#define DDG4_EXTENSIONPARAMETERS_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 data to output data
/**
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class ExtensionParameters {
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 {};

public:
/// Get the int parameters
auto const& intParameters() const { return m_intValues; }
/// Get the float parameters
auto const& fltParameters() const { return m_fltValues; }
/// Get the string parameters
auto const& strParameters() const { return m_strValues; }
/// Get the double parameters
auto const& dblParameters() const { return m_dblValues; }

};

} /* End namespace sim */
} /* End namespace dd4hep */
#endif // DDG4_EXTENSIONPARAMETERS_H
38 changes: 38 additions & 0 deletions DDG4/include/DDG4/FileParameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//==========================================================================
// 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_FILEPARAMETERS_H
#define DDG4_FILEPARAMETERS_H

#include <DDG4/ExtensionParameters.h>

/// 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 FileParameters: public ExtensionParameters {
public:
/// Copy the parameters from source
template <class T> void ingestParameters(T const& source);
/// Put parameters into destination
template <class T> void extractParameters(T& destination);
};

} /* End namespace sim */
} /* End namespace dd4hep */
#endif // DDG4_FILEPARAMETERS_H
29 changes: 3 additions & 26 deletions DDG4/include/DDG4/RunParameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
#ifndef DDG4_RUNPARAMETERS_H
#define DDG4_RUNPARAMETERS_H

#include <map>
#include <string>
#include <vector>

#include <DDG4/ExtensionParameters.h>

/// Namespace for the AIDA detector description toolkit
namespace dd4hep {
Expand All @@ -28,39 +25,19 @@ namespace dd4hep {
* \version 1.0
* \ingroup DD4HEP_SIMULATION
*/
class RunParameters {
class RunParameters: ExtensionParameters {
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;
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 */
Expand Down

0 comments on commit 5906930

Please sign in to comment.