Logo ROOT  
Reference Guide
ColumnReaderUtils.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud CERN 09/2020
2
3/*************************************************************************
4 * Copyright (C) 1995-2020, 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_COLUMNREADERUTILS
12#define ROOT_RDF_COLUMNREADERUTILS
13
14#include "RColumnReaderBase.hxx"
15#include "RColumnRegister.hxx"
16#include "RDefineBase.hxx"
17#include "RDefineReader.hxx"
18#include "RDSColumnReader.hxx"
19#include "RTreeColumnReader.hxx"
20
21#include <ROOT/RDataSource.hxx>
22#include <ROOT/TypeTraits.hxx>
23#include <TTreeReader.h>
24
25#include <array>
26#include <cassert>
27#include <map>
28#include <memory>
29#include <string>
30#include <typeinfo> // for typeid
31#include <vector>
32
33namespace ROOT {
34namespace Internal {
35namespace RDF {
36
37using namespace ROOT::TypeTraits;
39
40template <typename T>
41std::unique_ptr<RDFDetail::RColumnReaderBase>
42MakeColumnReader(unsigned int slot, RDefineBase *define,
43 const std::map<std::string, std::vector<void *>> &DSValuePtrsMap, TTreeReader *r,
44 ROOT::RDF::RDataSource *ds, const std::string &colName)
45{
46 using Ret_t = std::unique_ptr<RDFDetail::RColumnReaderBase>;
47
48 // this check must come first!
49 // so that Redefine'd columns have precedence over the original columns
50 if (define != nullptr)
51 return Ret_t{new RDefineReader(slot, *define, typeid(T))};
52
53 const auto DSValuePtrsIt = DSValuePtrsMap.find(colName);
54 if (DSValuePtrsIt != DSValuePtrsMap.end()) {
55 // reading from a RDataSource with the old column reader interface
56 const std::vector<void *> &DSValuePtrs = DSValuePtrsIt->second;
57 return Ret_t(new RDSColumnReader<T>(DSValuePtrs[slot]));
58 }
59
60 if (ds != nullptr) {
61 // reading from a RDataSource with the new column reader interface
62 return ds->GetColumnReaders(slot, colName, typeid(T));
63 }
64
65 assert(r != nullptr && "We could not find a reader for this column, this should never happen at this point.");
66
67 // reading from a TTree
68 return Ret_t{new RTreeColumnReader<T>(*r, colName)};
69}
70
71/// This type aggregates some of the arguments passed to MakeColumnReaders.
72/// We need to pass a single RColumnReadersInfo object rather than each argument separately because with too many
73/// arguments passed, gcc 7.5.0 and cling disagree on the ABI, which leads to the last function argument being read
74/// incorrectly from a compiled MakeColumnReaders symbols when invoked from a jitted symbol.
76 const std::vector<std::string> &fColNames;
78 const bool *fIsDefine;
79 const std::map<std::string, std::vector<void *>> &fDSValuePtrsMap;
81};
82
83/// Create a group of column readers, one per type in the parameter pack.
84/// colInfo.fColNames and colInfo.fIsDefine are expected to have size equal to the parameter pack, and elements ordered
85/// accordingly, i.e. fIsDefine[0] refers to fColNames[0] which is of type "ColTypes[0]".
86///
87/// Pre-condition: colInfo.isDefine must not be null.
88template <typename... ColTypes>
89std::array<std::unique_ptr<RDFDetail::RColumnReaderBase>, sizeof...(ColTypes)>
91{
92 // see RColumnReadersInfo for why we pass these arguments like this rather than directly as function arguments
93 const auto &colNames = colInfo.fColNames;
94 const auto &defines = colInfo.fCustomCols.GetColumns();
95 const bool *isDefine = colInfo.fIsDefine;
96 const auto &DSValuePtrsMap = colInfo.fDSValuePtrsMap;
97 auto *ds = colInfo.fDataSource;
98
99 int i = -1;
100 std::array<std::unique_ptr<RDFDetail::RColumnReaderBase>, sizeof...(ColTypes)> ret{
101 {{(++i, MakeColumnReader<ColTypes>(slot, isDefine[i] ? defines.at(colNames[i]).get() : nullptr, DSValuePtrsMap, r,
102 ds, colNames[i]))}...}};
103 return ret;
104
105 // avoid bogus "unused variable" warnings
106 (void)ds;
107 (void)slot;
108 (void)r;
109}
110
111} // namespace RDF
112} // namespace Internal
113} // namespace ROOT
114
115#endif // ROOT_RDF_COLUMNREADERS
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
ROOT::R::TRInterface & r
Definition: Object.C:4
A binder for user-defined columns and aliases.
const RDefineBasePtrMap_t & GetColumns() const
Returns the list of the pointers to the defined columns.
Column reader type that deals with values read from RDataSources.
Column reader for defined (aka custom) columns.
RTreeColumnReader specialization for TTree values read via TTreeReaderValues.
RDataSource defines an API that RDataFrame can use to read arbitrary data formats.
std::vector< T ** > GetColumnReaders(std::string_view columnName)
Called at most once per column by RDF.
A simple, robust and fast interface to read values from ROOT columnar datasets such as TTree,...
Definition: TTreeReader.h:44
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.
std::unique_ptr< RDFDetail::RColumnReaderBase > MakeColumnReader(unsigned int slot, RDefineBase *define, const std::map< std::string, std::vector< void * > > &DSValuePtrsMap, TTreeReader *r, ROOT::RDF::RDataSource *ds, const std::string &colName)
double T(double x)
Definition: ChebyshevPol.h:34
ROOT type_traits extensions.
Definition: TypeTraits.hxx:21
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.
This type aggregates some of the arguments passed to MakeColumnReaders.
const std::map< std::string, std::vector< void * > > & fDSValuePtrsMap
const std::vector< std::string > & fColNames
Lightweight storage for a collection of types.
Definition: TypeTraits.hxx:25