From 8a42ec7301dad0f6eed6e39bc9990c19861a05a9 Mon Sep 17 00:00:00 2001 From: Markus Frank Date: Thu, 18 Jan 2024 12:35:34 +0100 Subject: [PATCH] Make Geant4Output2ROOT more robust. --- DDG4/src/Geant4Output2ROOT.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/DDG4/src/Geant4Output2ROOT.cpp b/DDG4/src/Geant4Output2ROOT.cpp index f8119eb28..ffe5f89c8 100644 --- a/DDG4/src/Geant4Output2ROOT.cpp +++ b/DDG4/src/Geant4Output2ROOT.cpp @@ -36,7 +36,7 @@ using namespace std; /// Standard constructor Geant4Output2ROOT::Geant4Output2ROOT(Geant4Context* ctxt, const string& nam) - : Geant4OutputAction(ctxt, nam), m_file(0), m_tree(0) { + : Geant4OutputAction(ctxt, nam), m_file(nullptr), m_tree(nullptr) { declareProperty("Section", m_section = "EVENT"); declareProperty("HandleMCTruth", m_handleMCTruth = true); declareProperty("DisabledCollections", m_disabledCollections); @@ -94,11 +94,15 @@ void Geant4Output2ROOT::beginRun(const G4Run* run) { } if ( !m_file && !fname.empty() ) { TDirectory::TContext ctxt(TDirectory::CurrentDirectory()); - m_file = TFile::Open(fname.c_str(), "RECREATE", "dd4hep Simulation data"); - if (m_file->IsZombie()) { + std::unique_ptr file(TFile::Open(fname.c_str(), "RECREATE", "dd4hep Simulation data")); + if ( !file ) { + except("Failed to create ROOT output file:'%s'", fname.c_str()); + } + if (file->IsZombie()) { detail::deletePtr (m_file); except("Failed to open ROOT output file:'%s'", fname.c_str()); } + m_file = file.release(); m_tree = section(m_section); } Geant4OutputAction::beginRun(run);