From ea7156ad04c4d483ed0b0963dc8d97818bbd3f88 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Thu, 18 Jan 2024 16:43:03 +0100
Subject: [PATCH] Make Geant4Output2ROOT more robust.

---
 DDG4/src/Geant4Output2ROOT.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/DDG4/src/Geant4Output2ROOT.cpp b/DDG4/src/Geant4Output2ROOT.cpp
index ffe5f89c8..9aae89e22 100644
--- a/DDG4/src/Geant4Output2ROOT.cpp
+++ b/DDG4/src/Geant4Output2ROOT.cpp
@@ -29,6 +29,8 @@
 #include <TFile.h>
 #include <TTree.h>
 #include <TBranch.h>
+#include <TSystem.h>
+
 
 using namespace dd4hep::sim;
 using namespace dd4hep;
@@ -94,7 +96,13 @@ void Geant4Output2ROOT::beginRun(const G4Run* run) {
   }
   if ( !m_file && !fname.empty() ) {
     TDirectory::TContext ctxt(TDirectory::CurrentDirectory());
+    if ( !gSystem->AccessPathName(fname.c_str()) )  {
+      gSystem->Unlink(fname.c_str());
+    }
     std::unique_ptr<TFile> file(TFile::Open(fname.c_str(), "RECREATE", "dd4hep Simulation data"));
+    if ( !file )  {
+      file.reset(TFile::Open((fname+".1").c_str(), "RECREATE", "dd4hep Simulation data"));
+    }
     if ( !file )  {
       except("Failed to create ROOT output file:'%s'", fname.c_str());
     }
-- 
GitLab