Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RResultHandle.hxx
Go to the documentation of this file.
1// Author: Stefan Wunsch, Enrico Guiraud CERN 09/2020
2
3/*************************************************************************
4 * Copyright (C) 1995-2020, Rene Brun and Fons Rademakers. *
5 * All rights reserved. *
6 * *
7 * For the licensing terms see $ROOTSYS/LICENSE. *
8 * For the list of contributors see $ROOTSYS/README/CREDITS. *
9 *************************************************************************/
10
11#ifndef ROOT_RDF_RRESULTHANDLE
12#define ROOT_RDF_RRESULTHANDLE
13
14#include "ROOT/RResultPtr.hxx"
17#include "ROOT/RDF/Utils.hxx" // TypeID2TypeName
18
19#include <memory>
20#include <sstream>
21#include <typeinfo>
22#include <stdexcept> // std::runtime_error
23
24namespace ROOT {
25namespace RDF {
26
27/// \brief A type-erased version of RResultPtr and RResultMap.
28/// RResultHandles are used to invoke ROOT::RDF::RunGraphs() and can also be useful
29/// to store result pointers of different types in the same collection. Knowledge
30/// about the actual result type will still be needed to access it.
31/// \note Varied results are stripped away when a RResultMap is converted to a RResultHandle:
32/// Only the nominal result will be accessible from the RResultHandle.
34 ROOT::Detail::RDF::RLoopManager *fLoopManager = nullptr; //< Pointer to the loop manager
35 /// Owning pointer to the action that will produce this result.
36 /// Ownership is shared with RResultPtrs and RResultHandles that refer to the same result.
37 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fActionPtr;
38 std::shared_ptr<void> fObjPtr; ///< Type erased shared pointer encapsulating the wrapped result
39 /// Owning pointer to the varied action that will produce these results if any.
40 /// Null if the RResultHandle was created from a RResultPtr, so no variations were present.
41 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fVariedActionPtr;
42 const std::type_info *fType = nullptr; ///< Type of the wrapped result
43
44 // The ROOT::RDF::RunGraphs helper has to access the loop manager to check whether two RResultHandles belong to the same computation graph
45 friend unsigned int RunGraphs(std::vector<RResultHandle>);
46
47 /// Get the pointer to the encapsulated result.
48 /// Ownership is not transferred to the caller.
49 /// Triggers event loop and execution of all actions booked in the associated RLoopManager.
50 void* Get()
51 {
52 if (!fActionPtr->HasRun())
54 return fObjPtr.get();
55 }
56
57 /// Compare given type to the type of the wrapped result and throw if the types don't match.
58 void CheckType(const std::type_info& type)
59 {
60 if (*fType != type) {
61 std::stringstream ss;
62 ss << "Got the type " << ROOT::Internal::RDF::TypeID2TypeName(type)
63 << " but the RResultHandle refers to a result of type " << ROOT::Internal::RDF::TypeID2TypeName(*fType)
64 << ".";
65 throw std::runtime_error(ss.str());
66 }
67 }
68
70 {
71 if (fObjPtr == nullptr)
72 throw std::runtime_error("Trying to access the contents of a null RResultHandle.");
73 }
74
75public:
76 template <class T>
77 RResultHandle(const RResultPtr<T> &resultPtr) : fLoopManager(resultPtr.fLoopManager), fActionPtr(resultPtr.fActionPtr), fObjPtr(resultPtr.fObjPtr), fType(&typeid(T)) {}
78 template <class T>
80 : fLoopManager(resultMap.fLoopManager),
81 fActionPtr(resultMap.fNominalAction),
82 fObjPtr(resultMap.fMap.at("nominal")),
83 fVariedActionPtr(resultMap.fVariedAction),
84 fType(&typeid(T))
85 {
86 }
87
88 RResultHandle(const RResultHandle&) = default;
90 RResultHandle() = default;
91
92 /// Get the pointer to the encapsulated object.
93 /// Triggers event loop and execution of all actions booked in the associated RLoopManager.
94 /// If the RResultHandle was created from a RResultMap, this returns a pointer to the nominal value.
95 /// \tparam T Type of the action result
96 template <class T>
97 T* GetPtr()
98 {
100 CheckType(typeid(T));
101 return static_cast<T*>(Get());
102 }
103
104 /// Get a const reference to the encapsulated object.
105 /// Triggers event loop and execution of all actions booked in the associated RLoopManager.
106 /// If the RResultHandle was created from a RResultMap, this returns a pointer to the nominal value.
107 /// \tparam T Type of the action result
108 template <class T>
109 const T& GetValue()
110 {
111 ThrowIfNull();
112 CheckType(typeid(T));
113 return *static_cast<T*>(Get());
114 }
115
116 /// Check whether the result has already been computed
117 ///
118 /// ~~~{.cpp}
119 /// std::vector<RResultHandle> results;
120 /// results.emplace_back(df.Mean<double>("var"));
121 /// res.IsReady(); // false, access will trigger event loop
122 /// std::cout << res.GetValue<double>() << std::endl; // triggers event loop
123 /// res.IsReady(); // true
124 /// ~~~
125 bool IsReady() const {
126 if (fActionPtr == nullptr)
127 return false;
128 return fActionPtr->HasRun();
129 }
130
131 bool operator==(const RResultHandle &rhs) const
132 {
133 return fObjPtr == rhs.fObjPtr && fVariedActionPtr == rhs.fVariedActionPtr;
134 }
135
136 bool operator!=(const RResultHandle &rhs) const
137 {
138 return !(fObjPtr == rhs.fObjPtr) && fVariedActionPtr == rhs.fVariedActionPtr;
139 }
140};
141
142} // namespace RDF
143} // namespace ROOT
144
145#endif // ROOT_RDF_RRESULTHANDLE
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
The head node of a RDF computation graph.
void Run(bool jit=true)
Start the event loop with a different mechanism depending on IMT/no IMT, data source/no data source.
A type-erased version of RResultPtr and RResultMap.
bool operator!=(const RResultHandle &rhs) const
T * GetPtr()
Get the pointer to the encapsulated object.
std::shared_ptr< void > fObjPtr
Type erased shared pointer encapsulating the wrapped result.
ROOT::Detail::RDF::RLoopManager * fLoopManager
friend unsigned int RunGraphs(std::vector< RResultHandle >)
Trigger the event loop of multiple RDataFrames concurrently.
void * Get()
Get the pointer to the encapsulated result.
bool IsReady() const
Check whether the result has already been computed.
std::shared_ptr< ROOT::Internal::RDF::RActionBase > fActionPtr
Owning pointer to the action that will produce this result.
void CheckType(const std::type_info &type)
Compare given type to the type of the wrapped result and throw if the types don't match.
RResultHandle(const RResultPtr< T > &resultPtr)
RResultHandle(const Experimental::RResultMap< T > &resultMap)
RResultHandle(const RResultHandle &)=default
std::shared_ptr< ROOT::Internal::RDF::RActionBase > fVariedActionPtr
Owning pointer to the varied action that will produce these results if any.
const T & GetValue()
Get a const reference to the encapsulated object.
RResultHandle(RResultHandle &&)=default
const std::type_info * fType
Type of the wrapped result.
bool operator==(const RResultHandle &rhs) const
Smart pointer for the return type of actions.
std::string TypeID2TypeName(const std::type_info &id)
Returns the name of a type starting from its type_info An empty string is returned in case of failure...
Definition RDFUtils.cxx:119
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...