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 <string_view>
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 ExtraArgsForDefine {
39struct None{};
40struct Slot{};
41struct SlotAndEntry{};
42}
43// clang-format on
44
45template <typename F, typename ExtraArgsTag = ExtraArgsForDefine::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<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 ColType>
74 auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
75 {
76 if (auto *val = fValues[slot][readerIdx]->template TryGet<ColType>(entry))
77 return *val;
78
79 throw std::out_of_range{"RDataFrame: Define could not retrieve value for column '" + fColumnNames[readerIdx] +
80 "' for entry " + std::to_string(entry) +
81 ". You can use the DefaultValueFor operation to provide a default value, or "
82 "FilterAvailable/FilterMissing to discard/keep entries with missing values instead."};
83 }
84
85 template <typename... ColTypes, std::size_t... S>
86 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, NoneTag)
87 {
88 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
89 fExpression(GetValueChecked<ColTypes>(slot, S, entry)...);
90 (void)entry; // avoid unused parameter warning (gcc 12.1)
91 }
92
93 template <typename... ColTypes, std::size_t... S>
94 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotTag)
95 {
96 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
97 fExpression(slot, GetValueChecked<ColTypes>(slot, S, entry)...);
98 (void)entry; // avoid unused parameter warning (gcc 12.1)
99 }
100
101 template <typename... ColTypes, std::size_t... S>
102 void
103 UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotAndEntryTag)
104 {
105 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
106 fExpression(slot, entry, GetValueChecked<ColTypes>(slot, S, entry)...);
107 }
108
109public:
110 RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,
111 const RDFInternal::RColumnRegister &colRegister, RLoopManager &lm,
112 const std::string &variationName = "nominal")
113 : RDefineBase(name, type, colRegister, lm, columns, variationName), fExpression(std::move(expression)),
114 fLastResults(lm.GetNSlots() * RDFInternal::CacheLineStep<ret_type>()), fValues(lm.GetNSlots())
115 {
116 fLoopManager->Register(this);
117 }
118
119 RDefine(const RDefine &) = delete;
120 RDefine &operator=(const RDefine &) = delete;
121 ~RDefine() { fLoopManager->Deregister(this); }
122
123 void InitSlot(TTreeReader *r, unsigned int slot) final
124 {
125 RDFInternal::RColumnReadersInfo info{fColumnNames, fColRegister, fIsDefine.data(), *fLoopManager};
126 fValues[slot] = RDFInternal::GetColumnReaders(slot, r, ColumnTypes_t{}, info, fVariation);
127 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
128 }
129
130 /// Return the (type-erased) address of the Define'd value for the given processing slot.
131 void *GetValuePtr(unsigned int slot) final
132 {
133 return static_cast<void *>(&fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()]);
134 }
135
136 /// Update the value at the address returned by GetValuePtr with the content corresponding to the given entry
137 void Update(unsigned int slot, Long64_t entry) final
138 {
139 if (entry != fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()]) {
140 // evaluate this define expression, cache the result
141 UpdateHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{}, ExtraArgsTag{});
142 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
143 }
144 }
145
146 void Update(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &/*id*/) final {}
147
148 const std::type_info &GetTypeId() const final { return typeid(ret_type); }
149
150 /// Clean-up operations to be performed at the end of a task.
151 void FinalizeSlot(unsigned int slot) final
152 {
153 fValues[slot].fill(nullptr);
154
155 for (auto &e : fVariedDefines)
156 e.second->FinalizeSlot(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
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:69
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 r
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
char name[80]
Definition TGX11.cxx:110
auto GetValueChecked(unsigned int slot, std::size_t readerIdx, Long64_t entry) -> ColType &
Definition RDefine.hxx:74
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:103
void * GetValuePtr(unsigned int slot) final
Return the (type-erased) address of the Define'd value for the given processing slot.
Definition RDefine.hxx:131
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RDefine.hxx:123
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:110
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
const std::type_info & GetTypeId() const final
Definition RDefine.hxx:148
RDefine & operator=(const RDefine &)=delete
typename CallableTraits< F >::arg_types FunParamTypes_t
Definition RDefine.hxx:52
void FinalizeSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RDefine.hxx:151
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:146
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotTag)
Definition RDefine.hxx:94
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:137
RDFInternal::RemoveFirstTwoParametersIf_t< std::is_same< ExtraArgsTag, SlotAndEntryTag >::value, ColumnTypesTmp_t > ColumnTypes_t
Definition RDefine.hxx:56
std::vector< std::array< RColumnReaderBase *, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition RDefine.hxx:67
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:86
The head node of a RDF computation graph.
A binder for user-defined columns, variations 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:46
#define F(x, y, z)
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition Utils.hxx:151
std::array< RDFDetail::RColumnReaderBase *, sizeof...(ColTypes)> GetColumnReaders(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:165
std::vector< std::string > ColumnNames_t
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 GetColumnReaders.
Lightweight storage for a collection of types.