52
52
#include " ROOT/RNTupleDS.hxx"
53
53
#include " ROOT/RNTupleWriter.hxx" // for SnapshotRNTupleHelper
54
54
#endif
55
+ #include " ROOT/RTTreeDS.hxx"
55
56
56
57
#include < algorithm>
57
58
#include < functional>
@@ -1530,12 +1531,15 @@ class R__CLING_PTRCHECK(off) SnapshotTTreeHelper : public RActionImpl<SnapshotTT
1530
1531
std::vector<void *> fBranchAddresses ; // Addresses of objects associated to output branches
1531
1532
RBranchSet fOutputBranches ;
1532
1533
std::vector<bool > fIsDefine ;
1534
+ std::weak_ptr<ROOT::Detail::RDF::RLoopManager> fOutputLoopManager ;
1535
+ ROOT::RDF::RDataSource *fInputDataSource ;
1533
1536
1534
1537
public:
1535
1538
using ColumnTypes_t = TypeList<ColTypes...>;
1536
1539
SnapshotTTreeHelper (std::string_view filename, std::string_view dirname, std::string_view treename,
1537
1540
const ColumnNames_t &vbnames, const ColumnNames_t &bnames, const RSnapshotOptions &options,
1538
- std::vector<bool > &&isDefine)
1541
+ std::vector<bool > &&isDefine, std::weak_ptr<ROOT::Detail::RDF::RLoopManager> loopManager,
1542
+ ROOT::RDF::RDataSource *inputDataSource)
1539
1543
: fFileName (filename),
1540
1544
fDirName (dirname),
1541
1545
fTreeName (treename),
@@ -1544,7 +1548,9 @@ public:
1544
1548
fOutputBranchNames (ReplaceDotWithUnderscore (bnames)),
1545
1549
fBranches (vbnames.size (), nullptr ),
1546
1550
fBranchAddresses (vbnames.size (), nullptr ),
1547
- fIsDefine (std::move (isDefine))
1551
+ fIsDefine (std::move (isDefine)),
1552
+ fOutputLoopManager (loopManager),
1553
+ fInputDataSource (inputDataSource)
1548
1554
{
1549
1555
EnsureValidSnapshotTTreeOutput (fOptions , fTreeName , fFileName );
1550
1556
}
@@ -1571,6 +1577,8 @@ public:
1571
1577
{
1572
1578
if (r)
1573
1579
fInputTree = r->GetTree ();
1580
+ else if (auto treeDS = dynamic_cast <ROOT::Internal::RDF::RTTreeDS *>(fInputDataSource ))
1581
+ fInputTree = treeDS->GetTree ();
1574
1582
fBranchAddressesNeedReset = true ;
1575
1583
}
1576
1584
@@ -1650,6 +1658,11 @@ public:
1650
1658
// must destroy the TTree first, otherwise TFile will delete it too leading to a double delete
1651
1659
fOutputTree .reset ();
1652
1660
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
+ if (auto lmPtr = fOutputLoopManager .lock ())
1665
+ lmPtr->SetDataSource (std::make_unique<ROOT::Internal::RDF::RTTreeDS>(fullTreeName, fFileName ));
1653
1666
}
1654
1667
1655
1668
std::string GetActionName () { return " Snapshot" ; }
@@ -1673,8 +1686,15 @@ public:
1673
1686
SnapshotTTreeHelper MakeNew (void *newName, std::string_view /* variation*/ = " nominal" )
1674
1687
{
1675
1688
const std::string finalName = *reinterpret_cast <const std::string *>(newName);
1676
- return SnapshotTTreeHelper{
1677
- finalName, fDirName , fTreeName , fInputBranchNames , fOutputBranchNames , fOptions , std::vector<bool >(fIsDefine )};
1689
+ return SnapshotTTreeHelper{finalName,
1690
+ fDirName ,
1691
+ fTreeName ,
1692
+ fInputBranchNames ,
1693
+ fOutputBranchNames ,
1694
+ fOptions ,
1695
+ std::vector<bool >(fIsDefine ),
1696
+ fOutputLoopManager ,
1697
+ fInputDataSource };
1678
1698
}
1679
1699
};
1680
1700
@@ -1699,12 +1719,17 @@ class R__CLING_PTRCHECK(off) SnapshotTTreeHelperMT : public RActionImpl<Snapshot
1699
1719
std::vector<std::vector<void *>> fBranchAddresses ;
1700
1720
std::vector<RBranchSet> fOutputBranches ;
1701
1721
std::vector<bool > fIsDefine ;
1722
+ std::weak_ptr<ROOT::Detail::RDF::RLoopManager> fOutputLoopManager ;
1723
+ ROOT::RDF::RDataSource *fInputDataSource ;
1702
1724
1703
1725
public:
1704
1726
using ColumnTypes_t = TypeList<ColTypes...>;
1727
+
1705
1728
SnapshotTTreeHelperMT (const unsigned int nSlots, std::string_view filename, std::string_view dirname,
1706
1729
std::string_view treename, const ColumnNames_t &vbnames, const ColumnNames_t &bnames,
1707
- const RSnapshotOptions &options, std::vector<bool > &&isDefine)
1730
+ const RSnapshotOptions &options, std::vector<bool > &&isDefine,
1731
+ std::weak_ptr<ROOT::Detail::RDF::RLoopManager> loopManager,
1732
+ ROOT::RDF::RDataSource *inputDataSource)
1708
1733
: fNSlots (nSlots),
1709
1734
fOutputFiles (fNSlots ),
1710
1735
fOutputTrees (fNSlots ),
@@ -1719,7 +1744,9 @@ public:
1719
1744
fBranches (fNSlots , std::vector<TBranch *>(vbnames.size (), nullptr )),
1720
1745
fBranchAddresses (fNSlots , std::vector<void *>(vbnames.size (), nullptr )),
1721
1746
fOutputBranches (fNSlots ),
1722
- fIsDefine (std::move (isDefine))
1747
+ fIsDefine (std::move (isDefine)),
1748
+ fOutputLoopManager (loopManager),
1749
+ fInputDataSource (inputDataSource)
1723
1750
{
1724
1751
EnsureValidSnapshotTTreeOutput (fOptions , fTreeName , fFileName );
1725
1752
}
@@ -1766,7 +1793,9 @@ public:
1766
1793
if (r) {
1767
1794
// not an empty-source RDF
1768
1795
fInputTrees [slot] = r->GetTree ();
1769
- }
1796
+ } else if (auto treeDS = dynamic_cast <ROOT::Internal::RDF::RTTreeDS *>(fInputDataSource ))
1797
+ fInputTrees [slot] = treeDS->GetTree ();
1798
+
1770
1799
fBranchAddressesNeedReset [slot] = 1 ; // reset first event flag for this slot
1771
1800
}
1772
1801
@@ -1855,6 +1884,11 @@ public:
1855
1884
// flush all buffers to disk by destroying the TBufferMerger
1856
1885
fOutputFiles .clear ();
1857
1886
fMerger .reset ();
1887
+
1888
+ // Now connect the data source to the loop manager so it can be used for further processing
1889
+ auto fullTreeName = fDirName .empty () ? fTreeName : fDirName + ' /' + fTreeName ;
1890
+ if (auto lmPtr = fOutputLoopManager .lock ())
1891
+ lmPtr->SetDataSource (std::make_unique<ROOT::Internal::RDF::RTTreeDS>(fullTreeName, fFileName ));
1858
1892
}
1859
1893
1860
1894
std::string GetActionName () { return " Snapshot" ; }
@@ -1878,8 +1912,16 @@ public:
1878
1912
SnapshotTTreeHelperMT MakeNew (void *newName, std::string_view /* variation*/ = " nominal" )
1879
1913
{
1880
1914
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 )};
1915
+ return SnapshotTTreeHelperMT{fNSlots ,
1916
+ finalName,
1917
+ fDirName ,
1918
+ fTreeName ,
1919
+ fInputBranchNames ,
1920
+ fOutputBranchNames ,
1921
+ fOptions ,
1922
+ std::vector<bool >(fIsDefine ),
1923
+ fOutputLoopManager ,
1924
+ fInputDataSource };
1883
1925
}
1884
1926
};
1885
1927
@@ -1906,7 +1948,7 @@ class R__CLING_PTRCHECK(off) SnapshotRNTupleHelper : public RActionImpl<Snapshot
1906
1948
std::unique_ptr<TFile> fOutputFile {nullptr };
1907
1949
1908
1950
RSnapshotOptions fOptions ;
1909
- ROOT::Detail::RDF::RLoopManager * fLoopManager ;
1951
+ std::weak_ptr< ROOT::Detail::RDF::RLoopManager> fOutputLoopManager ;
1910
1952
ColumnNames_t fInputFieldNames ; // This contains the resolved aliases
1911
1953
ColumnNames_t fOutputFieldNames ;
1912
1954
std::unique_ptr<ROOT::Experimental::RNTupleWriter> fWriter {nullptr };
@@ -1919,11 +1961,11 @@ public:
1919
1961
using ColumnTypes_t = TypeList<ColTypes...>;
1920
1962
SnapshotRNTupleHelper (std::string_view filename, std::string_view ntuplename, const ColumnNames_t &vfnames,
1921
1963
const ColumnNames_t &fnames, const RSnapshotOptions &options,
1922
- ROOT::Detail::RDF::RLoopManager * lm, std::vector<bool > &&isDefine)
1964
+ std::weak_ptr< ROOT::Detail::RDF::RLoopManager> lm, std::vector<bool > &&isDefine)
1923
1965
: fFileName (filename),
1924
1966
fNTupleName (ntuplename),
1925
1967
fOptions (options),
1926
- fLoopManager (lm),
1968
+ fOutputLoopManager (lm),
1927
1969
fInputFieldNames (vfnames),
1928
1970
fOutputFieldNames (ReplaceDotWithUnderscore (fnames)),
1929
1971
fIsDefine (std::move (isDefine))
@@ -1937,7 +1979,7 @@ public:
1937
1979
SnapshotRNTupleHelper &operator =(SnapshotRNTupleHelper &&) = default ;
1938
1980
~SnapshotRNTupleHelper ()
1939
1981
{
1940
- if (!fNTupleName .empty () && !fLoopManager ->GetDataSource () && fOptions .fLazy )
1982
+ if (auto lm = fOutputLoopManager . lock (); lm && !fNTupleName .empty () && !lm ->GetDataSource () && fOptions .fLazy )
1941
1983
Warning (" Snapshot" , " A lazy Snapshot action was booked but never triggered." );
1942
1984
}
1943
1985
@@ -1993,7 +2035,8 @@ public:
1993
2035
{
1994
2036
fWriter .reset ();
1995
2037
// We can now set the data source of the loop manager for the RDataFrame that is returned by the Snapshot call.
1996
- fLoopManager ->SetDataSource (std::make_unique<ROOT::Experimental::RNTupleDS>(fNTupleName , fFileName ));
2038
+ if (auto lm = fOutputLoopManager .lock ())
2039
+ lm->SetDataSource (std::make_unique<ROOT::Experimental::RNTupleDS>(fNTupleName , fFileName ));
1997
2040
}
1998
2041
1999
2042
std::string GetActionName () { return " Snapshot" ; }
@@ -2022,7 +2065,7 @@ public:
2022
2065
fInputFieldNames ,
2023
2066
fOutputFieldNames ,
2024
2067
fOptions ,
2025
- fLoopManager ,
2068
+ fOutputLoopManager ,
2026
2069
std::vector<bool >(fIsDefine )};
2027
2070
}
2028
2071
};
0 commit comments