From a3fc61478495f80cbc55418627832f8287e3b423 Mon Sep 17 00:00:00 2001
From: Florine de Geus <florine.willemijn.de.geus@cern.ch>
Date: Mon, 13 Mar 2023 14:39:50 +0100
Subject: [PATCH] [ntuple] Re-add fSourceTree and reduce duplication in
 Create()

---
 .../v7/inc/ROOT/RNTupleImporter.hxx           |  3 ++
 tree/ntupleutil/v7/src/RNTupleImporter.cxx    | 32 ++++++++++---------
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/tree/ntupleutil/v7/inc/ROOT/RNTupleImporter.hxx b/tree/ntupleutil/v7/inc/ROOT/RNTupleImporter.hxx
index 5941af251af..3b7a0acc5bf 100644
--- a/tree/ntupleutil/v7/inc/ROOT/RNTupleImporter.hxx
+++ b/tree/ntupleutil/v7/inc/ROOT/RNTupleImporter.hxx
@@ -208,6 +208,7 @@ private:
 
    RNTupleImporter() = default;
 
+   std::unique_ptr<TFile> fSourceFile;
    std::unique_ptr<TTree> fSourceTree;
 
    std::string fDestFileName;
@@ -231,6 +232,8 @@ private:
    std::unique_ptr<RNTupleModel> fModel;
    std::unique_ptr<REntry> fEntry;
 
+   ROOT::Experimental::RResult<void> SetupDestination(std::string_view destFileName);
+
    void ResetSchema();
    /// Sets up the connection from TTree branches to RNTuple fields, including initialization of the memory
    /// buffers used for reading and writing.
diff --git a/tree/ntupleutil/v7/src/RNTupleImporter.cxx b/tree/ntupleutil/v7/src/RNTupleImporter.cxx
index f5ca2bd7a37..e593c53c6a0 100644
--- a/tree/ntupleutil/v7/src/RNTupleImporter.cxx
+++ b/tree/ntupleutil/v7/src/RNTupleImporter.cxx
@@ -86,23 +86,18 @@ ROOT::Experimental::RNTupleImporter::Create(std::string_view sourceFileName, std
 {
    auto importer = std::unique_ptr<RNTupleImporter>(new RNTupleImporter());
    importer->fNTupleName = treeName;
-   auto sourceFile = TFile::Open(std::string(sourceFileName).c_str());
-   if (!sourceFile || sourceFile->IsZombie()) {
+   importer->fSourceFile = std::unique_ptr<TFile>(TFile::Open(std::string(sourceFileName).c_str()));
+   if (!importer->fSourceFile || importer->fSourceFile->IsZombie()) {
       return R__FAIL("cannot open source file " + std::string(sourceFileName));
    }
-   importer->fSourceTree = std::unique_ptr<TTree>(sourceFile->Get<TTree>(std::string(treeName).c_str()));
+   importer->fSourceTree = std::unique_ptr<TTree>(importer->fSourceFile->Get<TTree>(std::string(treeName).c_str()));
    if (!importer->fSourceTree) {
       return R__FAIL("cannot read TTree " + std::string(treeName) + " from " + std::string(sourceFileName));
    }
    // If we have IMT enabled, its best use is for parallel page compression
    importer->fSourceTree->SetImplicitMT(false);
 
-   importer->fDestFileName = destFileName;
-   importer->fWriteOptions.SetCompression(kDefaultCompressionSettings);
-   importer->fDestFile = std::unique_ptr<TFile>(TFile::Open(importer->fDestFileName.c_str(), "UPDATE"));
-   if (!importer->fDestFile || importer->fDestFile->IsZombie()) {
-      return R__FAIL("cannot open dest file " + std::string(importer->fDestFileName));
-   }
+   importer->SetupDestination(destFileName);
 
    return importer;
 }
@@ -117,16 +112,23 @@ ROOT::Experimental::RNTupleImporter::Create(TTree *sourceTree, std::string_view
    // If we have IMT enabled, its best use is for parallel page compression
    importer->fSourceTree->SetImplicitMT(false);
 
-   importer->fDestFileName = destFileName;
-   importer->fWriteOptions.SetCompression(kDefaultCompressionSettings);
-   importer->fDestFile = std::unique_ptr<TFile>(TFile::Open(importer->fDestFileName.c_str(), "UPDATE"));
-   if (!importer->fDestFile || importer->fDestFile->IsZombie()) {
-      return R__FAIL("cannot open dest file " + std::string(importer->fDestFileName));
-   }
+   importer->SetupDestination(destFileName);
 
    return importer;
 }
 
+ROOT::Experimental::RResult<void> ROOT::Experimental::RNTupleImporter::SetupDestination(std::string_view destFileName)
+{
+   fDestFileName = destFileName;
+   fWriteOptions.SetCompression(kDefaultCompressionSettings);
+   fDestFile = std::unique_ptr<TFile>(TFile::Open(fDestFileName.c_str(), "UPDATE"));
+   if (!fDestFile || fDestFile->IsZombie()) {
+      return R__FAIL("cannot open dest file " + std::string(fDestFileName));
+   }
+
+   return RResult<void>::Success();
+}
+
 void ROOT::Experimental::RNTupleImporter::ReportSchema()
 {
    for (const auto &f : fImportFields) {
-- 
GitLab