From fa1664b2bc9ffd2c8b29e180f031b3772f5a803f Mon Sep 17 00:00:00 2001 From: Markus Frank <Markus.Frank@cern.ch> Date: Fri, 25 Nov 2022 20:14:25 +0100 Subject: [PATCH] Allow to change file on change-run --- DDG4/include/DDG4/Geant4Output2ROOT.h | 14 ++++++++--- DDG4/src/Geant4Output2ROOT.cpp | 35 ++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/DDG4/include/DDG4/Geant4Output2ROOT.h b/DDG4/include/DDG4/Geant4Output2ROOT.h index 46e7137e1..7cacc9cf5 100644 --- a/DDG4/include/DDG4/Geant4Output2ROOT.h +++ b/DDG4/include/DDG4/Geant4Output2ROOT.h @@ -42,18 +42,22 @@ namespace dd4hep { Sections m_sections; /// Branches in the event tree Branches m_branches; - /// name of the event tree - std::string m_section; /// Reference to the ROOT file to open TFile* m_file; /// Reference to the event data tree TTree* m_tree; - /// Flag if Monte-Carlo truth should be followed and checked - bool m_handleMCTruth; + /// File sequence number + int m_fseqNunmber { 0 }; + /// Property: name of the event tree + std::string m_section; /// Property: vector with disabled collections std::vector<std::string> m_disabledCollections; /// Property: vector with disabled collections bool m_disableParticles = false; + /// Property: Flag if Monte-Carlo truth should be followed and checked + bool m_handleMCTruth; + /// Property: Flag if Monte-Carlo truth should be followed and checked + bool m_filesByRun; public: /// Standard constructor @@ -65,6 +69,8 @@ namespace dd4hep { /// Fill single EVENT branch entry (Geant4 collection data) int fill(const std::string& nam, const ComponentCast& type, void* ptr); + /// Close current output file + virtual void closeOutput(); /// Callback to store the Geant4 run information virtual void beginRun(const G4Run* run); /// Callback to store each Geant4 hit collection diff --git a/DDG4/src/Geant4Output2ROOT.cpp b/DDG4/src/Geant4Output2ROOT.cpp index c43e58593..952020a72 100644 --- a/DDG4/src/Geant4Output2ROOT.cpp +++ b/DDG4/src/Geant4Output2ROOT.cpp @@ -23,6 +23,7 @@ // Geant4 include files #include <G4HCofThisEvent.hh> #include <G4ParticleTable.hh> +#include <G4Run.hh> // ROOT include files #include <TFile.h> @@ -36,18 +37,29 @@ using namespace std; /// Standard constructor Geant4Output2ROOT::Geant4Output2ROOT(Geant4Context* ctxt, const string& nam) : Geant4OutputAction(ctxt, nam), m_file(0), m_tree(0) { - declareProperty("Section", m_section = "EVENT"); - declareProperty("HandleMCTruth", m_handleMCTruth = true); + declareProperty("Section", m_section = "EVENT"); + declareProperty("HandleMCTruth", m_handleMCTruth = true); declareProperty("DisabledCollections", m_disabledCollections); declareProperty("DisableParticles", m_disableParticles); + declareProperty("FilesByRun", m_filesByRun = false); InstanceCount::increment(this); } /// Default destructor Geant4Output2ROOT::~Geant4Output2ROOT() { + closeOutput(); InstanceCount::decrement(this); +} + +/// Close current output file +void Geant4Output2ROOT::closeOutput() { if (m_file) { TDirectory::TContext ctxt(m_file); + Sections::iterator i = m_sections.find(m_section); + info("+++ Closing ROOT ourpur file %s", m_file->GetName()); + if ( i != m_sections.end() ) + m_sections.erase(i); + m_branches.clear(); m_tree->Write(); m_file->Close(); m_tree = 0; @@ -69,14 +81,25 @@ TTree* Geant4Output2ROOT::section(const string& nam) { /// Callback to store the Geant4 run information void Geant4Output2ROOT::beginRun(const G4Run* run) { - if (!m_file && !m_output.empty()) { + string fname = m_output; + if ( m_filesByRun ) { + size_t idx = m_output.rfind("."); + if ( m_file ) { + closeOutput(); + } + fname = m_output.substr(0, idx); + fname += _toString(run->GetRunID(), ".run%08d"); + if ( idx != string::npos ) + fname += m_output.substr(idx); + } + if ( !m_file && !fname.empty() ) { TDirectory::TContext ctxt(TDirectory::CurrentDirectory()); - m_file = TFile::Open(m_output.c_str(), "RECREATE", "dd4hep Simulation data"); + m_file = TFile::Open(fname.c_str(), "RECREATE", "dd4hep Simulation data"); if (m_file->IsZombie()) { detail::deletePtr (m_file); - throw runtime_error("Failed to open ROOT output file:'" + m_output + "'"); + except("Failed to open ROOT output file:'%s'", fname.c_str()); } - m_tree = section("EVENT"); + m_tree = section(m_section); } Geant4OutputAction::beginRun(run); } -- GitLab