From a264eea63c2a669255722d38968024b64867ecf2 Mon Sep 17 00:00:00 2001 From: Gordon McCann Date: Mon, 16 Jan 2023 11:32:00 -0500 Subject: [PATCH] Fix bug with merging where TChain::Merge implicitly took ownership of TFile and auto close and deleted --- src/evb/EVBWorkspace.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/evb/EVBWorkspace.cpp b/src/evb/EVBWorkspace.cpp index 1455bb6..213ce11 100644 --- a/src/evb/EVBWorkspace.cpp +++ b/src/evb/EVBWorkspace.cpp @@ -191,7 +191,7 @@ namespace EventBuilder { return false; TFile* output = TFile::Open(outputname.c_str(), "RECREATE"); - if(!output->IsOpen()) + if(!output || !output->IsOpen()) { EVB_ERROR("Could not open output file {0} for merge", outputname); output->Close(); @@ -200,8 +200,15 @@ namespace EventBuilder { TChain* chain = new TChain("SPSTree", "SPSTree"); for(auto& entry : files) + { + EVB_INFO("Merging file: {0}", entry); chain->Add(entry.c_str()); - chain->Merge(output, 0, "fast"); + } + //Note: TChain merge is kinda fucked. It requires a file passed in, which it then implicitly takes ownership of. + //By passing the keep option, we tell TChain we want to keep ownership of the file (otherwise it will automatically close and delete) + //We need ownership because the Merge function does not do a good job of handling errors explicitly. + chain->Merge(output, 0, "fast|keep"); + output->Close(); return true; }