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... 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 (void)entry; // avoid unused parameter warning (gcc 12.1)
79 }
80
81 template <typename... ColTypes, std::size_t... S>
82 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotTag)
83 {
84 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
85 fExpression(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
86 (void)entry; // avoid unused parameter warning (gcc 12.1)
87 }
88
89 template <typename... ColTypes, std::size_t... S>
90 void
91 UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotAndEntryTag)
92 {
93 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
94 fExpression(slot, entry, fValues[slot][S]->template Get<ColTypes>(entry)...);
95 }
96
97public:
98 RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns,
99 const RDFInternal::RColumnRegister &colRegister, RLoopManager &lm,
100 const std::string &variationName = "nominal")
101 : RDefineBase(name, type, colRegister, lm, columns, variationName), fExpression(std::move(expression)),
102 fLastResults(lm.GetNSlots() * RDFInternal::CacheLineStep<ret_type>()), fValues(lm.GetNSlots())
103 {
104 fLoopManager->Register(this);
105 }
106
107 RDefine(const RDefine &) = delete;
108 RDefine &operator=(const RDefine &) = delete;
109 ~RDefine() { fLoopManager->Deregister(this); }
110
111 void InitSlot(TTreeReader *r, unsigned int slot) final
112 {
113 RDFInternal::RColumnReadersInfo info{fColumnNames, fColRegister, fIsDefine.data(), *fLoopManager};
114 fValues[slot] = RDFInternal::GetColumnReaders(slot, r, ColumnTypes_t{}, info, fVariation);
115 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
116 }
117
118 /// Return the (type-erased) address of the Define'd value for the given processing slot.
119 void *GetValuePtr(unsigned int slot) final
120 {
121 return static_cast<void *>(&fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()]);
122 }
123
124 /// Update the value at the address returned by GetValuePtr with the content corresponding to the given entry
125 void Update(unsigned int slot, Long64_t entry) final
126 {
127 if (entry != fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()]) {
128 // evaluate this define expression, cache the result
129 UpdateHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{}, ExtraArgsTag{});
130 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
131 }
132 }
133
134 void Update(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &/*id*/) final {}
135
136 const std::type_info &GetTypeId() const final { return typeid(ret_type); }
137
138 /// Clean-up operations to be performed at the end of a task.
139 void FinalizeSlot(unsigned int slot) final
140 {
141 fValues[slot].fill(nullptr);
142
143 for (auto &e : fVariedDefines)
144 e.second->FinalizeSlot(slot);
145 }
146
147 /// Create clones of this Define that work with values in varied "universes".
148 void MakeVariations(const std::vector<std::string> &variations) final
149 {
150 for (const auto &variation : variations) {
151 if (std::find(fVariationDeps.begin(), fVariationDeps.end(), variation) == fVariationDeps.end()) {
152 // this Defined quantity does not depend on this variation, so no need to create a varied RDefine
153 continue;
154 }
155 if (fVariedDefines.find(variation) != fVariedDefines.end())
156 continue; // we already have this variation stored
157
158 // the varied defines get a copy of the callable object.
159 // TODO document this
160 auto variedDefine = std::unique_ptr<RDefineBase>(
161 new RDefine(fName, fType, fExpression, fColumnNames, fColRegister, *fLoopManager, variation));
162 // TODO switch to fVariedDefines.insert({variationName, std::move(variedDefine)}) when we drop gcc 5
163 fVariedDefines[variation] = std::move(variedDefine);
164 }
165 }
166
167 /// Return a clone of this Define that works with values in the variationName "universe".
168 RDefineBase &GetVariedDefine(const std::string &variationName) final
169 {
170 auto it = fVariedDefines.find(variationName);
171 if (it == fVariedDefines.end()) {
172 // We don't have a varied RDefine for this variation.
173 // This means we don't depend on it and we can return ourselves, i.e. the RDefine for the nominal universe.
174 assert(std::find(fVariationDeps.begin(), fVariationDeps.end(), variationName) == fVariationDeps.end());
175 return *this;
176 }
177
178 return *(it->second);
179 }
180};
181
182} // ns RDF
183} // ns Detail
184} // ns ROOT
185
186#endif // ROOT_RDF_RDEFINE
#define e(i)
Definition RSha256.hxx:103
long long Long64_t
Definition RtypesCore.h:80
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
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:91
void * GetValuePtr(unsigned int slot) final
Return the (type-erased) address of the Define'd value for the given processing slot.
Definition RDefine.hxx:119
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition RDefine.hxx:111
void MakeVariations(const std::vector< std::string > &variations) final
Create clones of this Define that work with values in varied "universes".
Definition RDefine.hxx:148
RDefineBase & GetVariedDefine(const std::string &variationName) final
Return a clone of this Define that works with values in the variationName "universe".
Definition RDefine.hxx:168
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:98
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:136
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:139
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:134
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotTag)
Definition RDefine.hxx:82
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:125
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:74
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:44
#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.