13#ifndef ROOT_RDF_HELPERS
14#define ROOT_RDF_HELPERS
22#include "RConfigure.h"
42template <
typename... ArgTypes,
typename F>
45 return std::function<
bool(ArgTypes...)>([=](ArgTypes... args)
mutable {
return !
f(args...); });
48template <
typename... ArgTypes,
typename Ret,
typename... Args>
51 return std::function<
bool(ArgTypes...)>([=](ArgTypes... args)
mutable {
return !
f(args...); });
54template <
typename I,
typename T,
typename F>
57template <std::size_t...
N,
typename T,
typename F>
59 template <std::
size_t Idx>
61 std::decay_t<F> fFunc;
65 auto operator()(AlwaysT<N>... args) ->
decltype(fFunc({args...})) {
return fFunc({args...}); }
68template <std::
size_t N,
typename T,
typename F>
86 return std::make_shared<T>(obj);
95 return std::make_shared<ROOT::Experimental::RHist<B>>(obj.
Clone());
103 return std::make_shared<ROOT::Experimental::RHistEngine<B>>(obj.
Clone());
120 typename Args =
typename ROOT::TypeTraits::CallableTraits<std::decay_t<F>>::arg_types_nodecay,
121 typename Ret =
typename ROOT::TypeTraits::CallableTraits<std::decay_t<F>>::ret_type>
124 static_assert(std::is_same<Ret, bool>::value,
"RDF::Not requires a callable that returns a bool.");
141template <std::
size_t N,
typename T,
typename F>
158template <
typename NodeType>
177template <
typename NodeType>
178void SaveGraph(NodeType node,
const std::string &outputFile)
183 std::ofstream out(outputFile);
184 if (!out.is_open()) {
185 throw std::runtime_error(
"Could not open output file \"" + outputFile +
"\"for reading");
196template <
typename NodeType>
228unsigned int RunGraphs(std::vector<RResultHandle> handles);
230namespace Experimental {
267 static_assert(!std::is_same_v<T, SnapshotResult_t>,
268 "Snapshot with variations can only be enabled via RSnapshotOptions.");
270 R__ASSERT(resPtr !=
nullptr &&
"Calling VariationsFor on an empty RResultPtr");
276 std::unique_ptr<RDFInternal::RActionBase> variedAction;
277 std::vector<std::shared_ptr<T>> variedResults;
279 std::shared_ptr<RDFInternal::RActionBase> nominalAction = resPtr.
fActionPtr;
280 std::vector<std::string> variations = nominalAction->GetVariations();
281 const auto nVariations = variations.size();
283 if (nVariations > 0) {
285 variedResults.reserve(nVariations);
286 for (
auto i = 0u; i < nVariations; ++i){
292 if constexpr (std::is_base_of<TNamed, T>::value) {
294 std::string variationName = variations[i];
296 std::replace(variationName.begin(), variationName.end(),
':',
'_');
298 auto &variedResult = variedResults.back();
300 variedResult->SetName((std::string(variedResult->GetName()) +
"_" + variationName).c_str());
304 std::vector<void *> typeErasedResults;
305 typeErasedResults.reserve(variedResults.size());
306 for (
auto &res : variedResults)
307 typeErasedResults.emplace_back(&res);
311 variedAction = nominalAction->MakeVariedAction(std::move(typeErasedResults));
314 return RDFInternal::MakeResultMap<T>(resPtr.
fObjPtr, std::move(variedResults), std::move(variations),
315 *resPtr.
fLoopManager, std::move(nominalAction), std::move(variedAction));
378 std::chrono::seconds totalElapsedSeconds)
const;
394 std::chrono::time_point<std::chrono::system_clock>
const fBeginTime = std::chrono::system_clock::now();
412 ProgressHelper(std::size_t increment,
unsigned int totalFiles,
unsigned int printInterval = 0,
413 bool useColors =
true);
425 template <
typename T>
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
void Jit()
Add RDF nodes that require just-in-time compilation to the computation graph.
A histogram data structure to bin data along multiple dimensions.
RHistEngine Clone() const
Clone this histogram engine.
A histogram for aggregation of data along multiple dimensions.
RHist Clone() const
Clone this histogram.
std::string RepresentGraph(ROOT::RDataFrame &rDataFrame)
Starting from the root node, prints the entire graph.
unsigned int const fTotalFiles
std::size_t ComputeTotalEvents() const
Compute total events in all open files.
~ProgressHelper()=default
ProgressHelper(std::size_t increment, unsigned int totalFiles, unsigned int printInterval=0, bool useColors=true)
Create a progress helper.
void PrintStatsFinal(std::size_t exactTotalEvents) const
std::pair< std::size_t, std::chrono::seconds > RecordEvtCountAndTime()
Record current event counts and time stamp, populate evts/s statistics array.
void RegisterNewSample(unsigned int, const ROOT::RDF::RSampleInfo &id)
Register a new sample for completion statistics.
unsigned int const fNColumns
unsigned int fEventsPerSecondStatisticsCounter
std::chrono::time_point< std::chrono::system_clock > const fBeginTime
void Update()
Record number of events processed and update progress bar.
std::map< std::string, ULong64_t > fSampleNameToEventEntries
ProgressHelper(ProgressHelper const &)=delete
ProgressHelper & operator=(ProgressHelper &&)=delete
std::size_t fLastProcessedEvents
ProgressHelper(ProgressHelper &&)=delete
std::mutex fSampleNameToEventEntriesMutex
std::chrono::seconds const fPrintInterval
double EvtPerSec() const
Compute a running mean of events/s.
std::atomic< std::size_t > fProcessedEvents
std::array< double, 10 > fEventsPerSecondStatistics
std::chrono::time_point< std::chrono::system_clock > fLastPrintTime
void operator()(unsigned int, T &)
Thread-safe callback for RDataFrame.
std::size_t const fIncrement
bool const fUseShellColours
void PrintProgressAndStats(std::ostream &stream, std::size_t currentEventCount, std::chrono::seconds totalElapsedSeconds) const
Print event and time statistics.
ProgressHelper & operator=(ProgressHelper const &)=delete
The public interface to the RDataFrame federation of classes.
Smart pointer for the return type of actions.
std::shared_ptr< T > fObjPtr
Shared pointer encapsulating the wrapped result.
RDFDetail::RLoopManager * fLoopManager
Non-owning pointer to the RLoopManager at the root of this computation graph.
std::shared_ptr< RDFInternal::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
ROOT's RDataFrame offers a modern, high-level interface for analysis of data stored in TTree ,...
std::function< bool(ArgTypes...)> NotHelper(ROOT::TypeTraits::TypeList< ArgTypes... >, F &&f)
auto PassAsVec(F &&f) -> PassAsVecHelper< std::make_index_sequence< N >, T, F >
std::shared_ptr< T > CopyForVariations(const T &obj)
Helper function to add a copy of an object to a vector of shared_ptrs, used in the implementation of ...
void ThreadsPerTH3(unsigned int nThread=1)
Set the number of threads sharing one TH3 in RDataFrame.
RResultMap< T > VariationsFor(RResultPtr< T > resPtr)
Produce all required systematic variations for the given result.
void AddProgressBar(ROOT::RDF::RNode df)
Add ProgressBar to a ROOT::RDF::RNode.
auto Not(F &&f) -> decltype(RDFInternal::NotHelper(Args(), std::forward< F >(f)))
Given a callable with signature bool(T1, T2, ...) return a callable with same signature that returns ...
std::string SaveGraph(NodeType node)
Create a graphviz representation of the dataframe computation graph, return it as a string.
RNode AsRNode(NodeType node)
Cast a RDataFrame node to the common type ROOT::RDF::RNode.
Lightweight storage for a collection of types.