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_RCUSTOMCOLUMN
12#define ROOT_RCUSTOMCOLUMN
13
17#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 <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
53 RDFInternal::RemoveFirstParameterIf_t<std::is_same<ExtraArgsTag, SlotTag>::value, FunParamTypes_t>;
55 RDFInternal::RemoveFirstTwoParametersIf_t<std::is_same<ExtraArgsTag, SlotAndEntryTag>::value, ColumnTypesTmp_t>;
56 using TypeInd_t = std::make_index_sequence<ColumnTypes_t::list_size>;
58 // Avoid instantiating vector<bool> as `operator[]` returns temporaries in that case. Use std::deque instead.
60 typename std::conditional<std::is_same<ret_type, bool>::value, std::deque<ret_type>, std::vector<ret_type>>::type;
61
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 /// The nth flag signals whether the nth input column is a custom column or not.
70 std::array<bool, ColumnTypes_t::list_size> fIsDefine;
71
72 template <typename... ColTypes, std::size_t... S>
73 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, NoneTag)
74 {
75 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
76 fExpression(fValues[slot][S]->template Get<ColTypes>(entry)...);
77 // silence "unused parameter" warnings in gcc
78 (void)slot;
79 (void)entry;
80 }
81
82 template <typename... ColTypes, std::size_t... S>
83 void UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotTag)
84 {
85 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
86 fExpression(slot, fValues[slot][S]->template Get<ColTypes>(entry)...);
87 // silence "unused parameter" warnings in gcc
88 (void)slot;
89 (void)entry;
90 }
91
92 template <typename... ColTypes, std::size_t... S>
93 void
94 UpdateHelper(unsigned int slot, Long64_t entry, TypeList<ColTypes...>, std::index_sequence<S...>, SlotAndEntryTag)
95 {
96 fLastResults[slot * RDFInternal::CacheLineStep<ret_type>()] =
97 fExpression(slot, entry, fValues[slot][S]->template Get<ColTypes>(entry)...);
98 // silence "unused parameter" warnings in gcc
99 (void)slot;
100 (void)entry;
101 }
102
103public:
104 RDefine(std::string_view name, std::string_view type, F expression, const ColumnNames_t &columns,
105 unsigned int nSlots, const RDFInternal::RBookedDefines &defines,
106 const std::map<std::string, std::vector<void *>> &DSValuePtrs, ROOT::RDF::RDataSource *ds)
107 : RDefineBase(name, type, nSlots, defines, DSValuePtrs, ds), fExpression(std::move(expression)),
108 fColumnNames(columns), fLastResults(fNSlots * RDFInternal::CacheLineStep<ret_type>()), fValues(fNSlots),
109 fIsDefine()
110 {
111 const auto nColumns = fColumnNames.size();
112 for (auto i = 0u; i < nColumns; ++i)
113 fIsDefine[i] = fDefines.HasName(fColumnNames[i]);
114 }
115
116 RDefine(const RDefine &) = delete;
117 RDefine &operator=(const RDefine &) = delete;
118
119 void InitSlot(TTreeReader *r, unsigned int slot) final
120 {
121 if (!fIsInitialized[slot]) {
122 fIsInitialized[slot] = true;
123 RDFInternal::RColumnReadersInfo info{fColumnNames, fDefines, fIsDefine.data(), fDSValuePtrs, fDataSource};
124 fValues[slot] = RDFInternal::MakeColumnReaders(slot, r, ColumnTypes_t{}, info);
125 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = -1;
126 }
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 filter, cache the result
140 UpdateHelper(slot, entry, ColumnTypes_t{}, TypeInd_t{}, ExtraArgsTag{});
141 fLastCheckedEntry[slot * RDFInternal::CacheLineStep<Long64_t>()] = entry;
142 }
143 }
144
145 const std::type_info &GetTypeId() const { return typeid(ret_type); }
146
147 /// Clean-up operations to be performed at the end of a task.
148 void FinaliseSlot(unsigned int slot) final
149 {
150 if (fIsInitialized[slot]) {
151 for (auto &v : fValues[slot])
152 v.reset();
153 fIsInitialized[slot] = false;
154 }
155 }
156};
157
158} // ns RDF
159} // ns Detail
160} // ns ROOT
161
162#endif // ROOT_RCUSTOMCOLUMN
ROOT::R::TRInterface & r
Definition Object.C:4
long long Long64_t
Definition RtypesCore.h:73
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
typedef void((*Func_t)())
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:94
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:119
const std::type_info & GetTypeId() const
Definition RDefine.hxx:145
ValuesPerSlot_t fLastResults
Definition RDefine.hxx:64
typename CallableTraits< F >::ret_type ret_type
Definition RDefine.hxx:57
typename std::conditional< std::is_same< ret_type, bool >::value, std::deque< ret_type >, std::vector< ret_type > >::type ValuesPerSlot_t
Definition RDefine.hxx:60
RDefine(std::string_view name, std::string_view type, F expression, const 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:104
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
const ColumnNames_t fColumnNames
Definition RDefine.hxx:63
void FinaliseSlot(unsigned int slot) final
Clean-up operations to be performed at the end of a task.
Definition RDefine.hxx:148
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, SlotTag)
Definition RDefine.hxx:83
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:55
void UpdateHelper(unsigned int slot, Long64_t entry, TypeList< ColTypes... >, std::index_sequence< S... >, NoneTag)
Definition RDefine.hxx:73
std::array< bool, ColumnTypes_t::list_size > fIsDefine
The nth flag signals whether the nth input column is a custom column or not.
Definition RDefine.hxx:70
Encapsulates the columns defined by the user.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
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)
std::vector< std::string > ColumnNames_t
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.
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 InitColumnReaders.
Lightweight storage for a collection of types.