5252#include " ROOT/RNTupleDS.hxx"
5353#include " ROOT/RNTupleWriter.hxx" // for SnapshotRNTupleHelper
5454#endif
55+ #include " ROOT/RTTreeDS.hxx"
5556
5657#include < algorithm>
5758#include < functional>
@@ -1530,12 +1531,15 @@ class R__CLING_PTRCHECK(off) SnapshotTTreeHelper : public RActionImpl<SnapshotTT
15301531 std::vector<void *> fBranchAddresses ; // Addresses of objects associated to output branches
15311532 RBranchSet fOutputBranches ;
15321533 std::vector<bool > fIsDefine ;
1534+ ROOT::Detail::RDF::RLoopManager *fOutputLoopManager ;
1535+ ROOT::RDF::RDataSource *fInputDataSource ;
15331536
15341537public:
15351538 using ColumnTypes_t = TypeList<ColTypes...>;
15361539 SnapshotTTreeHelper (std::string_view filename, std::string_view dirname, std::string_view treename,
15371540 const ColumnNames_t &vbnames, const ColumnNames_t &bnames, const RSnapshotOptions &options,
1538- std::vector<bool > &&isDefine)
1541+ std::vector<bool > &&isDefine, ROOT::Detail::RDF::RLoopManager *loopManager,
1542+ ROOT::RDF::RDataSource *inputDataSource)
15391543 : fFileName (filename),
15401544 fDirName (dirname),
15411545 fTreeName (treename),
@@ -1544,7 +1548,9 @@ public:
15441548 fOutputBranchNames (ReplaceDotWithUnderscore (bnames)),
15451549 fBranches (vbnames.size (), nullptr ),
15461550 fBranchAddresses (vbnames.size (), nullptr ),
1547- fIsDefine (std::move (isDefine))
1551+ fIsDefine (std::move (isDefine)),
1552+ fOutputLoopManager (loopManager),
1553+ fInputDataSource (inputDataSource)
15481554 {
15491555 EnsureValidSnapshotTTreeOutput (fOptions , fTreeName , fFileName );
15501556 }
@@ -1571,6 +1577,8 @@ public:
15711577 {
15721578 if (r)
15731579 fInputTree = r->GetTree ();
1580+ else if (auto treeDS = dynamic_cast <ROOT::Internal::RDF::RTTreeDS *>(fInputDataSource ))
1581+ fInputTree = treeDS->GetTree ();
15741582 fBranchAddressesNeedReset = true ;
15751583 }
15761584
@@ -1650,6 +1658,10 @@ public:
16501658 // must destroy the TTree first, otherwise TFile will delete it too leading to a double delete
16511659 fOutputTree .reset ();
16521660 fOutputFile ->Close ();
1661+
1662+ // Now connect the data source to the loop manager so it can be used for further processing
1663+ auto fullTreeName = fDirName .empty () ? fTreeName : fDirName + ' /' + fTreeName ;
1664+ fOutputLoopManager ->SetDataSource (std::make_unique<ROOT::Internal::RDF::RTTreeDS>(fullTreeName, fFileName ));
16531665 }
16541666
16551667 std::string GetActionName () { return " Snapshot" ; }
@@ -1673,8 +1685,15 @@ public:
16731685 SnapshotTTreeHelper MakeNew (void *newName, std::string_view /* variation*/ = " nominal" )
16741686 {
16751687 const std::string finalName = *reinterpret_cast <const std::string *>(newName);
1676- return SnapshotTTreeHelper{
1677- finalName, fDirName , fTreeName , fInputBranchNames , fOutputBranchNames , fOptions , std::vector<bool >(fIsDefine )};
1688+ return SnapshotTTreeHelper{finalName,
1689+ fDirName ,
1690+ fTreeName ,
1691+ fInputBranchNames ,
1692+ fOutputBranchNames ,
1693+ fOptions ,
1694+ std::vector<bool >(fIsDefine ),
1695+ fOutputLoopManager ,
1696+ fInputDataSource };
16781697 }
16791698};
16801699
@@ -1699,12 +1718,16 @@ class R__CLING_PTRCHECK(off) SnapshotTTreeHelperMT : public RActionImpl<Snapshot
16991718 std::vector<std::vector<void *>> fBranchAddresses ;
17001719 std::vector<RBranchSet> fOutputBranches ;
17011720 std::vector<bool > fIsDefine ;
1721+ ROOT::Detail::RDF::RLoopManager *fOutputLoopManager ;
1722+ ROOT::RDF::RDataSource *fInputDataSource ;
17021723
17031724public:
17041725 using ColumnTypes_t = TypeList<ColTypes...>;
1726+
17051727 SnapshotTTreeHelperMT (const unsigned int nSlots, std::string_view filename, std::string_view dirname,
17061728 std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
1707- const RSnapshotOptions &options, std::vector<bool > &&isDefine)
1729+ const RSnapshotOptions &options, std::vector<bool > &&isDefine,
1730+ ROOT::Detail::RDF::RLoopManager *loopManager, ROOT::RDF::RDataSource *inputDataSource)
17081731 : fNSlots (nSlots),
17091732 fOutputFiles (fNSlots ),
17101733 fOutputTrees (fNSlots ),
@@ -1719,7 +1742,9 @@ public:
17191742 fBranches (fNSlots , std::vector<TBranch *>(vbnames.size (), nullptr )),
17201743 fBranchAddresses (fNSlots , std::vector<void *>(vbnames.size (), nullptr )),
17211744 fOutputBranches (fNSlots ),
1722- fIsDefine (std::move (isDefine))
1745+ fIsDefine (std::move (isDefine)),
1746+ fOutputLoopManager (loopManager),
1747+ fInputDataSource (inputDataSource)
17231748 {
17241749 EnsureValidSnapshotTTreeOutput (fOptions , fTreeName , fFileName );
17251750 }
@@ -1766,7 +1791,9 @@ public:
17661791 if (r) {
17671792 // not an empty-source RDF
17681793 fInputTrees [slot] = r->GetTree ();
1769- }
1794+ } else if (auto treeDS = dynamic_cast <ROOT::Internal::RDF::RTTreeDS *>(fInputDataSource ))
1795+ fInputTrees [slot] = treeDS->GetTree ();
1796+
17701797 fBranchAddressesNeedReset [slot] = 1 ; // reset first event flag for this slot
17711798 }
17721799
@@ -1855,6 +1882,10 @@ public:
18551882 // flush all buffers to disk by destroying the TBufferMerger
18561883 fOutputFiles .clear ();
18571884 fMerger .reset ();
1885+
1886+ // Now connect the data source to the loop manager so it can be used for further processing
1887+ auto fullTreeName = fDirName .empty () ? fTreeName : fDirName + ' /' + fTreeName ;
1888+ fOutputLoopManager ->SetDataSource (std::make_unique<ROOT::Internal::RDF::RTTreeDS>(fullTreeName, fFileName ));
18581889 }
18591890
18601891 std::string GetActionName () { return " Snapshot" ; }
@@ -1878,8 +1909,16 @@ public:
18781909 SnapshotTTreeHelperMT MakeNew (void *newName, std::string_view /* variation*/ = " nominal" )
18791910 {
18801911 const std::string finalName = *reinterpret_cast <const std::string *>(newName);
1881- return SnapshotTTreeHelperMT{fNSlots , finalName, fDirName , fTreeName ,
1882- fInputBranchNames , fOutputBranchNames , fOptions , std::vector<bool >(fIsDefine )};
1912+ return SnapshotTTreeHelperMT{fNSlots ,
1913+ finalName,
1914+ fDirName ,
1915+ fTreeName ,
1916+ fInputBranchNames ,
1917+ fOutputBranchNames ,
1918+ fOptions ,
1919+ std::vector<bool >(fIsDefine ),
1920+ fOutputLoopManager ,
1921+ fInputDataSource };
18831922 }
18841923};
18851924
@@ -1907,7 +1946,7 @@ class R__CLING_PTRCHECK(off) SnapshotRNTupleHelper : public RActionImpl<Snapshot
19071946 std::unique_ptr<TFile> fOutputFile {nullptr };
19081947
19091948 RSnapshotOptions fOptions ;
1910- ROOT::Detail::RDF::RLoopManager *fLoopManager ;
1949+ ROOT::Detail::RDF::RLoopManager *fOutputLoopManager ;
19111950 ColumnNames_t fInputFieldNames ; // This contains the resolved aliases
19121951 ColumnNames_t fOutputFieldNames ;
19131952 std::unique_ptr<ROOT::Experimental::RNTupleWriter> fWriter {nullptr };
@@ -1925,7 +1964,7 @@ public:
19251964 fDirName (dirname),
19261965 fNTupleName (ntuplename),
19271966 fOptions (options),
1928- fLoopManager (lm),
1967+ fOutputLoopManager (lm),
19291968 fInputFieldNames (vfnames),
19301969 fOutputFieldNames (ReplaceDotWithUnderscore (fnames)),
19311970 fIsDefine (std::move (isDefine))
@@ -1939,7 +1978,7 @@ public:
19391978 SnapshotRNTupleHelper &operator =(SnapshotRNTupleHelper &&) = default ;
19401979 ~SnapshotRNTupleHelper ()
19411980 {
1942- if (!fNTupleName .empty () && !fLoopManager ->GetDataSource () && fOptions .fLazy )
1981+ if (!fNTupleName .empty () && !fOutputLoopManager ->GetDataSource () && fOptions .fLazy )
19431982 Warning (" Snapshot" , " A lazy Snapshot action was booked but never triggered." );
19441983 }
19451984
@@ -1999,7 +2038,7 @@ public:
19992038 {
20002039 fWriter .reset ();
20012040 // We can now set the data source of the loop manager for the RDataFrame that is returned by the Snapshot call.
2002- fLoopManager ->SetDataSource (
2041+ fOutputLoopManager ->SetDataSource (
20032042 std::make_unique<ROOT::Experimental::RNTupleDS>(fDirName + " /" + fNTupleName , fFileName ));
20042043 }
20052044
@@ -2029,7 +2068,7 @@ public:
20292068 fInputFieldNames ,
20302069 fOutputFieldNames ,
20312070 fOptions ,
2032- fLoopManager ,
2071+ fOutputLoopManager ,
20332072 std::vector<bool >(fIsDefine )};
20342073 }
20352074};
0 commit comments