Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RDefine.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo CERN 09/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2018, 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_RDEFINE
12#define ROOT_RDF_RDEFINE
13
18#include "ROOT/RDF/Utils.hxx"
19#include "ROOT/RStringView.hxx"
20#include "ROOT/TypeTraits.hxx"
21#include "RtypesCore.h"
22
23#include <array>
24#include <deque>
25#include <type_traits>
26#include <utility> // std::index_sequence
27#include <vector>
28
29class TTreeReader;
30
31namespace ROOT {
32namespace Detail {
33namespace RDF {
34
35using namespace ROOT::TypeTraits;
36
37// clang-format off
38namespace CustomColExtraArgs {
39struct None{};
40struct Slot{};
41struct SlotAndEntry{};
42}
43// clang-format on
44
45template <typename F, typename ExtraArgsTag = CustomColExtraArgs::None>
46class R__CLING_PTRCHECK(off) RDefine final : public RDefineBase {
47 // shortcuts
51 // other types
57 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
59 // Avoid instantiating vector<bool> as `operator[]` returns temporaries in that case. Use std::deque instead.
61 std::conditional_t<std::is_same<ret_type, bool>::value, std::deque<ret_type>, std::vector<ret_type>>;
62
65
66 /// Column readers per slot and per input column
67 std::vector<std::array<std::unique_ptr<RColumnReaderBase>, ColumnTypes_t::list_size>> fValues;
68
69 /// Define objects corresponding to systematic variations other than nominal for this defined column.
70 /// The map key is the full variation name, e.g. "pt:up".
71 std::unordered_map<std::string, std::unique_ptr<RDefineBase>> fVariedDefines;
72
73 template <typename... ColTypes, std::size_t... S>
74 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, NoneTag)
75 {
76 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
77 fExpression(fValues[slot][S]->template Get<ColTypes>(entry)...);
78 // silence "unused parameter" warnings in gcc
79 (void)slot;
80 (void)entry;
81 }
82
83 template <typename... ColTypes, std::size_t... S>
84 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotTag)
85 {
86 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
87 fExpression(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
88 // silence "unused parameter" warnings in gcc
89 (void)slot;
90 (void)entry;
91 }
92
93 template <typename... ColTypes, std::size_t... S>
94 void
95 UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotAndEntryTag)
96 {
97 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
98 fExpression(slot, entry, fValues[slot][S]->template Get<ColTypes>(entry)...);
99 // silence "unused parameter" warnings in gcc
100 (void)slot;
101 (void)entry;
102 }
103
104public:
105 RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,
106 const RDFInternal::RColumnRegister &colRegister, RLoopManager &lm,
107 const std::string &variationName = "nominal")
108 : RDefineBase(name, type, colRegister, lm, columns, variationName), fExpression(std::move(expression)),
109 fLastResults(lm.GetNSlots() * RDFInternal::CacheLineStep<ret_type>()), fValues(lm.GetNSlots())
110 {
111 fLoopManager->Book(this);
112 }
113
114 RDefine(const RDefine &) = delete;
115 RDefine &operator=(const RDefine &) = delete;
116 ~RDefine() { fLoopManager->Deregister(this); }
117
118 void InitSlot(TTreeReader *r, unsigned int slot) final
119 {
120 RDFInternal::RColumnReadersInfo info{fColumnNames, fColRegister, fIsDefine.data(), fLoopManager->GetDSValuePtrs(),
121 fLoopManager->GetDataSource()};
122 fValues[slot] = RDFInternal::MakeColumnReaders(slot, r, ColumnTypes_t{}, info, fVariation);
123 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
124
125 for (auto &e : fVariedDefines)
126 e.second->InitSlot(r, slot);
127 }
128
129 /// Return the (type-erased) address of the Define'd value for the given processing slot.
130 void *GetValuePtr(unsigned int slot) final
131 {
132 return static_cast<void *>(&fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()]);
133 }
134
135 /// Update the value at the address returned by GetValuePtr with the content corresponding to the given entry
136 void Update(unsigned int slot, Long64_t entry) final
137 {
138 if (entry != fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()]) {
139 // evaluate this define expression, cache the result
140 UpdateHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{}, ExtraArgsTag{});
141 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
142 }
143 }
144
145 void Update(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &/*id*/) final {}
146
147 const std::type_info &GetTypeId() const { return typeid(ret_type); }
148
149 /// Clean-up operations to be performed at the end of a task.
150 void FinaliseSlot(unsigned int slot) final
151 {
152 for (auto &v : fValues[slot])
153 v.reset();
154
155 for (auto &e : fVariedDefines)
156 e.second->FinaliseSlot(slot);
157 }
158
159 /// Create clones of this Define that work with values in varied "universes".
160 void MakeVariations(const std::vector<std::string> &variations) final
161 {
162 for (const auto &variation : variations) {
163 if (std::find(fVariationDeps.begin(), fVariationDeps.end(), variation) == fVariationDeps.end()) {
164 // this Defined quantity does not depend on this variation, so no need to create a varied RDefine
165 continue;
166 }
167 if (fVariedDefines.find(variation) != fVariedDefines.end())
168 continue; // we already have this variation stored
169
170 // the varied defines get a copy of the callable object.
171 // TODO document this
172 auto variedDefine = std::unique_ptr<RDefineBase>(
173 new RDefine(fName, fType, fExpression, fColumnNames, fColRegister, *fLoopManager, variation));
174 // TODO switch to fVariedDefines.insert({variationName, std::move(variedDefine)}) when we drop gcc 5
175 fVariedDefines[variation] = std::move(variedDefine);
176 }
177 }
178
179 /// Return a clone of this Define that works with values in the variationName "universe".
180 RDefineBase &GetVariedDefine(const std::string &variationName) final
181 {
182 auto it = fVariedDefines.find(variationName);
183 if (it == fVariedDefines.end()) {
184 // We don't have a varied RDefine for this variation.
185 // This means we don't depend on it and we can return ourselves, i.e. the RDefine for the nominal universe.
186 assert(std::find(fVariationDeps.begin(), fVariationDeps.end(), variationName) == fVariationDeps.end());
187 return *this;
188 }
189
190 return *(it->second);
191 }
192};
193
194} // ns RDF
195} // ns Detail
196} // ns ROOT
197
198#endif // ROOT_RDF_RDEFINE
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
ROOT::R::TRInterface & r
Definition Object.C:4
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:80
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
std::conditional_t< std::is_same< ret_type, bool >::value, std::deque< ret_type >, std::vector< ret_type > > ValuesPerSlot_t
Definition RDefine.hxx:61
RDFInternal::RemoveFirstParameterIf_t< std::is_same< ExtraArgsTag, SlotTag >::value, FunParamTypes_t > ColumnTypesTmp_t
Definition RDefine.hxx:54
RDefine(const RDefine &)=delete
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotAndEntryTag)
Definition RDefine.hxx:95
void * GetValuePtr(unsigned int slot) final
Return the (type-erased) address of the Define'd value for the given processing slot.
Definition RDefine.hxx:130
std::vector< std::array< std::unique_ptr< RColumnReaderBase >, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition RDefine.hxx:67
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RDefine.hxx:118
const std::type_info & GetTypeId() const
Definition RDefine.hxx:147
void MakeVariations(const std::vector< std::string > &variations) final
Create clones of this Define that work with values in varied "universes".
Definition RDefine.hxx:160
RDefineBase & GetVariedDefine(const std::string &variationName) final
Return a clone of this Define that works with values in the variationName "universe".
Definition RDefine.hxx:180
ValuesPerSlot_t fLastResults
Definition RDefine.hxx:64
RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns, const RDFInternal::RColumnRegister &colRegister, RLoopManager &lm, const std::string &variationName="nominal")
Definition RDefine.hxx:105
typename CallableTraits< F >::ret_type ret_type
Definition RDefine.hxx:58
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition RDefine.hxx:57
RDefine & operator=(const RDefine &)=delete
typename CallableTraits< F >::arg_types FunParamTypes_t
Definition RDefine.hxx:52
void Update(unsigned int, const ROOT::RDF::RSampleInfo &) final
Update function to be called once per sample, used if the derived type is a RDefinePerSample.
Definition RDefine.hxx:145
void FinaliseSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RDefine.hxx:150
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotTag)
Definition RDefine.hxx:84
void Update(unsigned int slot, Long64_t entry) final
Update the value at the address returned by GetValuePtr with the content corresponding to the given e...
Definition RDefine.hxx:136
RDFInternal::RemoveFirstTwoParametersIf_t< std::is_same< ExtraArgsTag, SlotAndEntryTag >::value, ColumnTypesTmp_t > ColumnTypes_t
Definition RDefine.hxx:56
std::unordered_map< std::string, std::unique_ptr< RDefineBase > > fVariedDefines
Define objects corresponding to systematic variations other than nominal for this defined column.
Definition RDefine.hxx:71
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, NoneTag)
Definition RDefine.hxx:74
The head node of a RDF computation graph.
A binder for user-defined columns and aliases.
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition TTreeReader.h:44
#define F(x, y, z)
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition Utils.hxx:149
std::array< std::unique_ptr< RDFDetail::RColumnReaderBase >, sizeof...(ColTypes)> MakeColumnReaders(unsigned int slot, TTreeReader *r, TypeList< ColTypes... >, const RColumnReadersInfo &colInfo, const std::string &variationName="nominal")
Create a group of column readers, one per type in the parameter pack.
typename RemoveFirstTwoParametersIf< MustRemove, TypeList >::type RemoveFirstTwoParametersIf_t
Definition Utils.hxx:163
std::vector< std::string > ColumnNames_t
Definition Utils.hxx:35
ROOT type_traits extensions.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Extract types from the signature of a callable object. See CallableTraits.
This type aggregates some of the arguments passed to MakeColumnReaders.
Lightweight storage for a collection of types.