Logo ROOT   6.12/07
Reference Guide
List of all members | Public Types | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | Friends | List of all members
ROOT::Experimental::TDF::TResultProxy< T > Class Template Reference

template<typename T>
class ROOT::Experimental::TDF::TResultProxy< T >

Smart pointer for the return type of actions.

A wrapper around the result of TDataFrame actions able to trigger calculations lazily.

Template Parameters
TType of the action result

A smart pointer which allows to access the result of a TDataFrame action. The methods of the encapsulated object can be accessed via the arrow operator. Upon invocation of the arrow operator or dereferencing (operator*), the loop on the events and calculations of all scheduled actions are executed if needed. It is possible to iterate on the result proxy if the proxied object is a collection.

for (auto& myItem : myResultProxy) { ... };

If iteration is not supported by the type of the proxied object, a compilation error is thrown.

Definition at line 27 of file TResultProxy.hxx.

Public Types

using Value_t = T
 Convenience alias to simplify access to proxied type. More...
 

Public Member Functions

 TResultProxy ()=delete
 
 TResultProxy (const TResultProxy &)=default
 
 TResultProxy (TResultProxy &&)=default
 
TIterationHelper< T >::Iterator_t begin ()
 Return an iterator to the beginning of the contained object if this makes sense, throw a compilation error otherwise. More...
 
TIterationHelper< T >::Iterator_t end ()
 Return an iterator to the end of the contained object if this makes sense, throw a compilation error otherwise. More...
 
const T & GetValue ()
 Get a const reference to the encapsulated object. More...
 
TResultProxy< T > & OnPartialResult (ULong64_t everyNEvents, std::function< void(T &)> callback)
 Register a callback that TDataFrame will execute "everyNEvents" on a partial result. More...
 
TResultProxy< T > & OnPartialResultSlot (ULong64_t everyNEvents, std::function< void(unsigned int, T &)> callback)
 Register a callback that TDataFrame will execute in each worker thread concurrently on that thread's partial result. More...
 
T & operator* ()
 Get a pointer to the encapsulated object. More...
 
T * operator-> ()
 Get a pointer to the encapsulated object. More...
 

Static Public Attributes

static constexpr ULong64_t kOnce = 0ull
 Convenience definition to express a callback must be executed once. More...
 

Private Types

using ShrdPtrBool_t = std::shared_ptr< bool >
 
using SPT_t = std::shared_ptr< T >
 
using SPTLM_t = std::shared_ptr< TDFDetail::TLoopManager >
 
using WPTLM_t = std::weak_ptr< TDFDetail::TLoopManager >
 

Private Member Functions

 TResultProxy (const SPT_t &objPtr, const ShrdPtrBool_t &readiness, const SPTLM_t &loopManager, TDFInternal::TActionBase *actionPtr=nullptr)
 
T * Get ()
 Get the pointer to the encapsulated result. More...
 
std::shared_ptr< TDFInternal::TActionBase * > GetActionPtrPtr () const
 
void TriggerRun ()
 Triggers the event loop in the TLoopManager instance to which it's associated via the fImplWeakPtr. More...
 

Private Attributes

const std::shared_ptr< TDFInternal::TActionBase * > fActionPtrPtr
 Shared_ptr to a pointer to the TDF action that produces this result. More...
 
WPTLM_t fImplWeakPtr
 Points to the TLoopManager at the root of the functional graph. More...
 
const SPT_t fObjPtr
 Shared pointer encapsulating the wrapped result. More...
 
const ShrdPtrBool_t fReadiness = std::make_shared<bool>(false)
 State registered also in the TLoopManager until the event loop is executed. More...
 

Friends

template<typename W >
TResultProxy< W > TDFDetail::MakeResultProxy (const std::shared_ptr< W > &, const SPTLM_t &, TDFInternal::TActionBase *)
 
template<typename W >
std::pair< TResultProxy< W >, std::shared_ptr< TDFInternal::TActionBase * > > TDFDetail::MakeResultProxy (const std::shared_ptr< W > &, const SPTLM_t &)
 

#include <ROOT/TResultProxy.hxx>

Member Typedef Documentation

◆ ShrdPtrBool_t

template<typename T>
using ROOT::Experimental::TDF::TResultProxy< T >::ShrdPtrBool_t = std::shared_ptr<bool>
private

Definition at line 75 of file TResultProxy.hxx.

◆ SPT_t

template<typename T>
using ROOT::Experimental::TDF::TResultProxy< T >::SPT_t = std::shared_ptr<T>
private

Definition at line 72 of file TResultProxy.hxx.

◆ SPTLM_t

template<typename T>
using ROOT::Experimental::TDF::TResultProxy< T >::SPTLM_t = std::shared_ptr<TDFDetail::TLoopManager>
private

Definition at line 73 of file TResultProxy.hxx.

◆ Value_t

template<typename T>
using ROOT::Experimental::TDF::TResultProxy< T >::Value_t = T

Convenience alias to simplify access to proxied type.

Definition at line 137 of file TResultProxy.hxx.

◆ WPTLM_t

template<typename T>
using ROOT::Experimental::TDF::TResultProxy< T >::WPTLM_t = std::weak_ptr<TDFDetail::TLoopManager>
private

Definition at line 74 of file TResultProxy.hxx.

Constructor & Destructor Documentation

◆ TResultProxy() [1/4]

template<typename T>
ROOT::Experimental::TDF::TResultProxy< T >::TResultProxy ( const SPT_t objPtr,
const ShrdPtrBool_t readiness,
const SPTLM_t loopManager,
TDFInternal::TActionBase actionPtr = nullptr 
)
inlineprivate

Definition at line 127 of file TResultProxy.hxx.

◆ TResultProxy() [2/4]

template<typename T>
ROOT::Experimental::TDF::TResultProxy< T >::TResultProxy ( )
delete

◆ TResultProxy() [3/4]

template<typename T>
ROOT::Experimental::TDF::TResultProxy< T >::TResultProxy ( const TResultProxy< T > &  )
default

◆ TResultProxy() [4/4]

template<typename T>
ROOT::Experimental::TDF::TResultProxy< T >::TResultProxy ( TResultProxy< T > &&  )
default

Member Function Documentation

◆ begin()

template<typename T>
TIterationHelper<T>::Iterator_t ROOT::Experimental::TDF::TResultProxy< T >::begin ( )
inline

Return an iterator to the beginning of the contained object if this makes sense, throw a compilation error otherwise.

Definition at line 159 of file TResultProxy.hxx.

◆ end()

template<typename T>
TIterationHelper<T>::Iterator_t ROOT::Experimental::TDF::TResultProxy< T >::end ( )
inline

Return an iterator to the end of the contained object if this makes sense, throw a compilation error otherwise.

Definition at line 168 of file TResultProxy.hxx.

◆ Get()

template<typename T>
T* ROOT::Experimental::TDF::TResultProxy< T >::Get ( )
inlineprivate

Get the pointer to the encapsulated result.

Ownership is not transferred to the caller. Triggers event loop and execution of all actions booked in the associated TLoopManager.

Definition at line 120 of file TResultProxy.hxx.

◆ GetActionPtrPtr()

template<typename T>
std::shared_ptr<TDFInternal::TActionBase *> ROOT::Experimental::TDF::TResultProxy< T >::GetActionPtrPtr ( ) const
inlineprivate

Definition at line 134 of file TResultProxy.hxx.

◆ GetValue()

template<typename T>
const T& ROOT::Experimental::TDF::TResultProxy< T >::GetValue ( )
inline

Get a const reference to the encapsulated object.

Triggers event loop and execution of all actions booked in the associated TLoopManager.

Definition at line 146 of file TResultProxy.hxx.

◆ OnPartialResult()

template<typename T>
TResultProxy<T>& ROOT::Experimental::TDF::TResultProxy< T >::OnPartialResult ( ULong64_t  everyNEvents,
std::function< void(T &)>  callback 
)
inline

Register a callback that TDataFrame will execute "everyNEvents" on a partial result.

Parameters
[in]everyNEventsFrequency at which the callback will be called, as a number of events processed
[in]callbacka callable with signature void(Value_t&) where Value_t is the type of the value contained in this TResultProxy
Returns
this TResultProxy, to allow chaining of OnPartialResultSlot with other calls

The callback must be a callable (lambda, function, functor class...) that takes a reference to the result type as argument and returns nothing. TDataFrame will invoke registered callbacks passing partial action results as arguments to them (e.g. a histogram filled with a part of the selected events, a counter incremented only up to a certain point, a mean over a subset of the events and so forth).

Callbacks can be used e.g. to inspect partial results of the analysis while the event loop is running. For example one can draw an up-to-date version of a result histogram every 100 entries like this:

auto h = tdf.Histo1D("x");
TCanvas c("c","x hist");
h.OnPartialResult(100, [&c](TH1D &h_) { c.cd(); h_.Draw(); c.Update(); });
h->Draw(); // event loop runs here, this `Draw` is executed after the event loop is finished

A value of 0 for everyNEvents indicates the callback must be executed only once, before running the event loop. A conveniece definition kOnce is provided to make this fact more expressive in user code (see snippet below). Multiple callbacks can be registered with the same TResultProxy (i.e. results of TDataFrame actions) and will be executed sequentially. Callbacks are executed in the order they were registered. The type of the value contained in a TResultProxy is also available as TResultProxy<T>::Value_t, e.g.

auto h = tdf.Histo1D("x");
// h.kOnce is 0
// decltype(h)::Value_t is TH1D

When implicit multi-threading is enabled, the callback:

  • will never be executed by multiple threads concurrently: it needs not be thread-safe. For example the snippet above that draws the partial histogram on a canvas works seamlessly in multi-thread event loops.
  • will always be executed "everyNEvents": partial results will "contain" that number of events more from one call to the next
  • might be executed by a different worker thread at different times: the value of std::this_thread::get_id() might change between calls To register a callback that is called by each worker thread (concurrently) every N events one can use OnPartialResultSlot.

Definition at line 215 of file TResultProxy.hxx.

◆ OnPartialResultSlot()

template<typename T>
TResultProxy<T>& ROOT::Experimental::TDF::TResultProxy< T >::OnPartialResultSlot ( ULong64_t  everyNEvents,
std::function< void(unsigned int, T &)>  callback 
)
inline

Register a callback that TDataFrame will execute in each worker thread concurrently on that thread's partial result.

Parameters
[in]everyNEventsFrequency at which the callback will be called by each thread, as a number of events processed
[in]acallable with signature void(unsigned int, Value_t&) where Value_t is the type of the value contained in this TResultProxy
Returns
this TResultProxy, to allow chaining of OnPartialResultSlot with other calls

See OnPartialResult for a generic explanation of the callback mechanism. Compared to OnPartialResult, this method has two major differences:

  • all worker threads invoke the callback once every specified number of events. The event count is per-thread, and callback invocation might happen concurrently (i.e. the callback must be thread-safe)
  • the callable must take an extra unsigned int parameter corresponding to a multi-thread "processing slot": this is a "helper value" to simplify writing thread-safe callbacks: different worker threads might invoke the callback concurrently but always with different slot numbers.
  • a value of 0 for everyNEvents indicates the callback must be executed once per slot.

For example, the following snippet prints out a thread-safe progress bar of the events processed by TDataFrame

auto c = tdf.Count(); // any action would do, but `Count` is the most lightweight
std::string progress;
std::mutex bar_mutex;
c.OnPartialResultSlot(nEvents / 100, [&progress, &bar_mutex](unsigned int, ULong64_t &) {
std::lock_guard<std::mutex> lg(bar_mutex);
progress.push_back('#');
std::cout << "\r[" << std::left << std::setw(100) << progress << ']' << std::flush;
});
std::cout << "Analysis running..." << std::endl;
*c; // trigger the event loop by accessing an action's result
std::cout << "\nDone!" << std::endl;

Definition at line 261 of file TResultProxy.hxx.

◆ operator*()

template<typename T>
T& ROOT::Experimental::TDF::TResultProxy< T >::operator* ( )
inline

Get a pointer to the encapsulated object.

Triggers event loop and execution of all actions booked in the associated TLoopManager.

Definition at line 150 of file TResultProxy.hxx.

◆ operator->()

template<typename T>
T* ROOT::Experimental::TDF::TResultProxy< T >::operator-> ( )
inline

Get a pointer to the encapsulated object.

Ownership is not transferred to the caller. Triggers event loop and execution of all actions booked in the associated TLoopManager.

Definition at line 155 of file TResultProxy.hxx.

◆ TriggerRun()

template<typename T >
void ROOT::Experimental::TDF::TResultProxy< T >::TriggerRun ( )
private

Triggers the event loop in the TLoopManager instance to which it's associated via the fImplWeakPtr.

Definition at line 277 of file TResultProxy.hxx.

Friends And Related Function Documentation

◆ TDFDetail::MakeResultProxy [1/2]

template<typename T>
template<typename W >
TResultProxy<W> TDFDetail::MakeResultProxy ( const std::shared_ptr< W > &  ,
const SPTLM_t ,
TDFInternal::TActionBase  
)
friend

◆ TDFDetail::MakeResultProxy [2/2]

template<typename T>
template<typename W >
std::pair<TResultProxy<W>, std::shared_ptr<TDFInternal::TActionBase *> > TDFDetail::MakeResultProxy ( const std::shared_ptr< W > &  ,
const SPTLM_t  
)
friend

Member Data Documentation

◆ fActionPtrPtr

template<typename T>
const std::shared_ptr<TDFInternal::TActionBase *> ROOT::Experimental::TDF::TResultProxy< T >::fActionPtrPtr
private

Shared_ptr to a pointer to the TDF action that produces this result.

It is set at construction time for non-jitted actions, and at jitting time for jitted actions (at the time of writing, this means right before the event-loop).

Definition at line 112 of file TResultProxy.hxx.

◆ fImplWeakPtr

template<typename T>
WPTLM_t ROOT::Experimental::TDF::TResultProxy< T >::fImplWeakPtr
private

Points to the TLoopManager at the root of the functional graph.

Definition at line 103 of file TResultProxy.hxx.

◆ fObjPtr

template<typename T>
const SPT_t ROOT::Experimental::TDF::TResultProxy< T >::fObjPtr
private

Shared pointer encapsulating the wrapped result.

Definition at line 104 of file TResultProxy.hxx.

◆ fReadiness

template<typename T>
const ShrdPtrBool_t ROOT::Experimental::TDF::TResultProxy< T >::fReadiness = std::make_shared<bool>(false)
private

State registered also in the TLoopManager until the event loop is executed.

Definition at line 102 of file TResultProxy.hxx.

◆ kOnce

template<typename T>
constexpr ULong64_t ROOT::Experimental::TDF::TResultProxy< T >::kOnce = 0ull
static

Convenience definition to express a callback must be executed once.

Definition at line 138 of file TResultProxy.hxx.

Libraries for ROOT::Experimental::TDF::TResultProxy< T >:
[legend]

The documentation for this class was generated from the following file: