Logo ROOT  
Reference Guide
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
17#include "ROOT/RDF/Utils.hxx"
18#include "ROOT/RStringView.hxx"
19#include "ROOT/TypeTraits.hxx"
20#include "RtypesCore.h"
21
22#include <array>
23#include <deque>
24#include <type_traits>
25#include <utility> // std::index_sequence
26#include <vector>
27
28class TTreeReader;
29
30namespace ROOT {
31namespace Detail {
32namespace RDF {
33
34using namespace ROOT::TypeTraits;
35
36// clang-format off
37namespace CustomColExtraArgs {
38struct None{};
39struct Slot{};
40struct SlotAndEntry{};
41}
42// clang-format on
43
44template <typename F, typename ExtraArgsTag = CustomColExtraArgs::None>
45class R__CLING_PTRCHECK(off) RDefine final : public RDefineBase {
46 // shortcuts
50 // other types
51 using FunParamTypes_t = typename CallableTraits<F>::arg_types;
56 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
57 using ret_type = typename CallableTraits<F>::ret_type;
58 // Avoid instantiating vector<bool> as `operator[]` returns temporaries in that case. Use std::deque instead.
60 std::conditional_t<std::is_same<ret_type, bool>::value, std::deque<ret_type>, std::vector<ret_type>>;
61
64
65 /// Column readers per slot and per input column
66 std::vector<std::array<std::unique_ptr<RColumnReaderBase>, ColumnTypes_t::list_size>> fValues;
67
68 template <typename... ColTypes, std::size_t... S>
69 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, NoneTag)
70 {
71 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
72 fExpression(fValues[slot][S]->template Get<ColTypes>(entry)...);
73 // silence "unused parameter" warnings in gcc
74 (void)slot;
75 (void)entry;
76 }
77
78 template <typename... ColTypes, std::size_t... S>
79 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotTag)
80 {
81 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
82 fExpression(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
83 // silence "unused parameter" warnings in gcc
84 (void)slot;
85 (void)entry;
86 }
87
88 template <typename... ColTypes, std::size_t... S>
89 void
90 UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotAndEntryTag)
91 {
92 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
93 fExpression(slot, entry, fValues[slot][S]->template Get<ColTypes>(entry)...);
94 // silence "unused parameter" warnings in gcc
95 (void)slot;
96 (void)entry;
97 }
98
99public:
101 unsigned int nSlots, const RDFInternal::RBookedDefines &defines,
102 const std::map<std::string, std::vector<void *>> &DSValuePtrs, ROOT::RDF::RDataSource *ds)
103 : RDefineBase(name, type, nSlots, defines, DSValuePtrs, ds, columns), fExpression(std::move(expression)),
104 fLastResults(nSlots * RDFInternal::CacheLineStep<ret_type>()), fValues(nSlots)
105 {
106 }
107
108 RDefine(const RDefine &) = delete;
109 RDefine &operator=(const RDefine &) = delete;
110
111 void InitSlot(TTreeReader *r, unsigned int slot) final
112 {
113 if (!fIsInitialized[slot]) {
114 for (auto &define : fDefines.GetColumns())
115 define.second->InitSlot(r, slot);
116 fIsInitialized[slot] = true;
117 RDFInternal::RColumnReadersInfo info{fColumnNames, fDefines, fIsDefine.data(), fDSValuePtrs, fDataSource};
118 fValues[slot] = RDFInternal::MakeColumnReaders(slot, r, ColumnTypes_t{}, info);
119 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
120 }
121 }
122
123 /// Return the (type-erased) address of the Define'd value for the given processing slot.
124 void *GetValuePtr(unsigned int slot) final
125 {
126 return static_cast<void *>(&fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()]);
127 }
128
129 /// Update the value at the address returned by GetValuePtr with the content corresponding to the given entry
130 void Update(unsigned int slot, Long64_t entry) final
131 {
132 if (entry != fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()]) {
133 // evaluate this define expression, cache the result
134 UpdateHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{}, ExtraArgsTag{});
135 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
136 }
137 }
138
139 void Update(unsigned int /*slot*/, const ROOT::RDF::RSampleInfo &/*id*/) final {}
140
141 const std::type_info &GetTypeId() const { return typeid(ret_type); }
142
143 /// Clean-up operations to be performed at the end of a task.
144 void FinaliseSlot(unsigned int slot) final
145 {
146 if (fIsInitialized[slot]) {
147 for (auto &v : fValues[slot])
148 v.reset();
149 fIsInitialized[slot] = false;
150 }
151 }
152};
153
154} // ns RDF
155} // ns Detail
156} // ns ROOT
157
158#endif // ROOT_RDF_RDEFINE
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
ROOT::R::TRInterface & r
Definition: Object.C:4
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:60
RDFInternal::RemoveFirstParameterIf_t< std::is_same< ExtraArgsTag, SlotTag >::value, FunParamTypes_t > ColumnTypesTmp_t
Definition: RDefine.hxx:53
RDefine(const RDefine &)=delete
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotAndEntryTag)
Definition: RDefine.hxx:90
void * GetValuePtr(unsigned int slot) final
Return the (type-erased) address of the Define'd value for the given processing slot.
Definition: RDefine.hxx:124
std::vector< std::array< std::unique_ptr< RColumnReaderBase >, ColumnTypes_t::list_size > > fValues
Column readers per slot and per input column.
Definition: RDefine.hxx:66
void InitSlot(TTreeReader *r, unsigned int slot) final
Definition: RDefine.hxx:111
const std::type_info & GetTypeId() const
Definition: RDefine.hxx:141
RDefine(std::string_view name, std::string_view type, F expression, const ROOT::RDF::ColumnNames_t &columns, unsigned int nSlots, const RDFInternal::RBookedDefines &defines, const std::map< std::string, std::vector< void * > > &DSValuePtrs, ROOT::RDF::RDataSource *ds)
Definition: RDefine.hxx:100
ValuesPerSlot_t fLastResults
Definition: RDefine.hxx:63
typename CallableTraits< F >::ret_type ret_type
Definition: RDefine.hxx:57
std::make_index_sequence< ColumnTypes_t::list_size > TypeInd_t
Definition: RDefine.hxx:56
RDefine & operator=(const RDefine &)=delete
typename CallableTraits< F >::arg_types FunParamTypes_t
Definition: RDefine.hxx:51
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:139
void FinaliseSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition: RDefine.hxx:144
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotTag)
Definition: RDefine.hxx:79
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:130
RDFInternal::RemoveFirstTwoParametersIf_t< std::is_same< ExtraArgsTag, SlotAndEntryTag >::value, ColumnTypesTmp_t > ColumnTypes_t
Definition: RDefine.hxx:55
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, NoneTag)
Definition: RDefine.hxx:69
Encapsulates the columns defined by the user.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
This type represents a sample identifier, to be used in conjunction with RDataFrame features such as ...
Definition: RSampleInfo.hxx:32
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:44
basic_string_view< char > string_view
#define F(x, y, z)
std::array< std::unique_ptr< RDFDetail::RColumnReaderBase >, sizeof...(ColTypes)> MakeColumnReaders(unsigned int slot, TTreeReader *r, TypeList< ColTypes... >, const RColumnReadersInfo &colInfo)
Create a group of column readers, one per type in the parameter pack.
constexpr std::size_t CacheLineStep()
Stepping through CacheLineStep<T> values in a vector<T> brings you to a new cache line.
Definition: Utils.hxx:220
typename RemoveFirstParameterIf< MustRemove, TypeList >::type RemoveFirstParameterIf_t
Definition: Utils.hxx:149
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.
Definition: TypeTraits.hxx:21
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
RooArgSet S(Args_t &&... args)
Definition: RooArgSet.h:215
This type aggregates some of the arguments passed to MakeColumnReaders.
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25