Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RColumnRegister.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, Danilo Piparo, Massimo Tumolo CERN 06/2018
2
3/*************************************************************************
4 * Copyright (C) 1995-2021, 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_RCOLUMNREGISTER
12#define ROOT_RDF_RCOLUMNREGISTER
13
14#include <TString.h>
15
16#include <algorithm>
17#include <unordered_map>
18#include <memory>
19#include <string>
20#include <vector>
21
22namespace ROOT {
23namespace Detail {
24namespace RDF {
25class RDefineBase;
26class RLoopManager;
27}
28} // namespace Detail
29
30namespace Internal {
31namespace RDF {
32
34
35class RVariationBase;
36
37/**
38 * \class ROOT::Internal::RDF::RColumnRegister
39 * \ingroup dataframe
40 * \brief A binder for user-defined columns and aliases.
41 * The storage is copy-on-write and shared between all instances of the class that have the same values.
42 */
44 using ColumnNames_t = std::vector<std::string>;
45 using DefinesMap_t = std::unordered_map<std::string, std::shared_ptr<RDFDetail::RDefineBase>>;
46 /// See fVariations for more information on this type.
47 using VariationsMap_t = std::unordered_multimap<std::string, std::shared_ptr<RVariationBase>>;
48
49 ////////////////////////////////////////////////////////////////////////////
50 /// \brief Add a new name to the list returned by `GetNames` without booking a new column.
51 ///
52 /// This is needed because we abuse fColumnNames to also keep track of the aliases defined
53 /// in each branch of the computation graph.
54 /// Internally it recreates the vector with the new name, and swaps it with the old one.
55 void AddName(std::string_view name);
56
57private:
58 // N.B. must come before fDefines, to be destructed after them
59 std::shared_ptr<RDFDetail::RLoopManager> fLoopManager;
60
61 /// Immutable map of Defines, can be shared among several nodes.
62 /// When a new define is added (through a call to RInterface::Define or similar) a new map with the extra element is
63 /// created.
64 std::shared_ptr<const DefinesMap_t> fDefines;
65 /// Immutable map of Aliases, can be shared among several nodes.
66 std::shared_ptr<const std::unordered_map<std::string, std::string>> fAliases;
67 /// Immutable multimap of Variations, can be shared among several nodes.
68 /// The key is the name of an existing column, the values are all variations that affect that column.
69 /// As a consequence, Variations that affect multiple columns are inserted multiple times, once per column.
70 std::shared_ptr<const VariationsMap_t> fVariations;
71 std::shared_ptr<const ColumnNames_t> fColumnNames; ///< Names of Defines and Aliases registered so far.
72
73public:
74 RColumnRegister(const RColumnRegister &) = default;
77
78 RColumnRegister(std::shared_ptr<RDFDetail::RLoopManager> lm)
79 : fLoopManager(lm), fDefines(std::make_shared<DefinesMap_t>()),
80 fAliases(std::make_shared<std::unordered_map<std::string, std::string>>()),
81 fVariations(std::make_shared<VariationsMap_t>()), fColumnNames(std::make_shared<ColumnNames_t>())
82 {
83 }
84
85 ////////////////////////////////////////////////////////////////////////////
86 /// \brief Returns the list of the names of the defined columns (Defines + Aliases).
87 ColumnNames_t GetNames() const { return *fColumnNames; }
88
89 ////////////////////////////////////////////////////////////////////////////
90 /// \brief Returns a map of pointers to the defined columns.
91 const DefinesMap_t &GetColumns() const { return *fDefines; }
92
93 ////////////////////////////////////////////////////////////////////////////
94 /// \brief Returns the multimap of systematic variations, see fVariations.
95 const VariationsMap_t &GetVariations() const { return *fVariations; }
96
97 ////////////////////////////////////////////////////////////////////////////
98 /// \brief Check if the provided name is tracked in the names list
99 bool HasName(std::string_view name) const;
100
101 ////////////////////////////////////////////////////////////////////////////
102 /// \brief Add a new booked column.
103 /// Internally it recreates the map with the new column, and swaps it with the old one.
104 void AddColumn(const std::shared_ptr<RDFDetail::RDefineBase> &column);
105
106 /// \brief Add a new alias to the ledger.
107 void AddAlias(std::string_view alias, std::string_view colName);
108
109 ////////////////////////////////////////////////////////////////////////////
110 /// \brief Return true if the given column name is an existing alias.
111 bool IsAlias(const std::string &name) const;
112
113 ////////////////////////////////////////////////////////////////////////////
114 /// \brief Return the actual column name that the alias resolves to.
115 /// Drills through multiple levels of aliasing if needed.
116 /// Returns the input in case it's not an alias.
117 /// Expands `#var` to `R_rdf_sizeof_var` (the #var columns are implicitly-defined aliases).
118 std::string ResolveAlias(std::string_view alias) const;
119
120 /// \brief Register a new systematic variation.
121 void AddVariation(const std::shared_ptr<RVariationBase> &variation);
122
123 ////////////////////////////////////////////////////////////////////////////
124 /// \brief Get the names of the variations that directly provide alternative values for this column.
125 std::vector<std::string> GetVariationsFor(const std::string &column) const;
126
127 ////////////////////////////////////////////////////////////////////////////
128 /// \brief Get the names of all variations that directly or indirectly affect a given column.
129 ///
130 /// This list includes variations applied to the column as well as variations applied to other
131 /// columns on which the value of this column depends (typically via a Define expression).
132 std::vector<std::string> GetVariationDeps(const std::string &column) const;
133
134 ////////////////////////////////////////////////////////////////////////////
135 /// \brief Get the names of all variations that directly or indirectly affect the specified columns.
136 ///
137 /// This list includes variations applied to the columns as well as variations applied to other
138 /// columns on which the value of any of these columns depend (typically via Define expressions).
139 std::vector<std::string> GetVariationDeps(const ColumnNames_t &columns) const;
140
141 ////////////////////////////////////////////////////////////////////////////
142 /// \brief Return the RVariation object that handles the specified variation of the specified column.
143 RVariationBase &FindVariation(const std::string &colName, const std::string &variationName) const;
144
145 ////////////////////////////////////////////////////////////////////////////
146 /// \brief Empty the contents of this ledger.
147 /// The only allowed operation on a RColumnRegister object after a call to Clear is its destruction.
148 void Clear();
149};
150
151} // Namespace RDF
152} // Namespace Internal
153} // Namespace ROOT
154
155#endif // ROOT_RDF_RCOLUMNREGISTER
char name[80]
Definition TGX11.cxx:110
A binder for user-defined columns and aliases.
std::unordered_multimap< std::string, std::shared_ptr< RVariationBase > > VariationsMap_t
See fVariations for more information on this type.
RColumnRegister & operator=(const RColumnRegister &)=default
std::shared_ptr< const std::unordered_map< std::string, std::string > > fAliases
Immutable map of Aliases, can be shared among several nodes.
void AddVariation(const std::shared_ptr< RVariationBase > &variation)
Register a new systematic variation.
RColumnRegister(std::shared_ptr< RDFDetail::RLoopManager > lm)
bool IsAlias(const std::string &name) const
Return true if the given column name is an existing alias.
std::vector< std::string > GetVariationsFor(const std::string &column) const
Get the names of the variations that directly provide alternative values for this column.
const DefinesMap_t & GetColumns() const
Returns a map of pointers to the defined columns.
std::shared_ptr< const VariationsMap_t > fVariations
Immutable multimap of Variations, can be shared among several nodes.
std::shared_ptr< const DefinesMap_t > fDefines
Immutable map of Defines, can be shared among several nodes.
bool HasName(std::string_view name) const
Check if the provided name is tracked in the names list.
void Clear()
Empty the contents of this ledger.
std::shared_ptr< const ColumnNames_t > fColumnNames
Names of Defines and Aliases registered so far.
std::string ResolveAlias(std::string_view alias) const
Return the actual column name that the alias resolves to.
void AddColumn(const std::shared_ptr< RDFDetail::RDefineBase > &column)
Add a new booked column.
const VariationsMap_t & GetVariations() const
Returns the multimap of systematic variations, see fVariations.
RVariationBase & FindVariation(const std::string &colName, const std::string &variationName) const
Return the RVariation object that handles the specified variation of the specified column.
RColumnRegister(const RColumnRegister &)=default
RColumnRegister(RColumnRegister &&)=default
std::unordered_map< std::string, std::shared_ptr< RDFDetail::RDefineBase > > DefinesMap_t
std::vector< std::string > GetVariationDeps(const std::string &column) const
Get the names of all variations that directly or indirectly affect a given column.
ColumnNames_t GetNames() const
Returns the list of the names of the defined columns (Defines + Aliases).
void AddAlias(std::string_view alias, std::string_view colName)
Add a new alias to the ledger.
std::shared_ptr< RDFDetail::RLoopManager > fLoopManager
std::vector< std::string > ColumnNames_t
void AddName(std::string_view name)
Add a new name to the list returned by GetNames without booking a new column.
This type includes all parts of RVariation that do not depend on the callable signature.
std::vector< std::string > ColumnNames_t
Definition Utils.hxx:35
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...