Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RResultMap.hxx
Go to the documentation of this file.
1// Author: Enrico Guiraud, CERN 11/2021
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_RRESULTMAP
12#define ROOT_RDF_RRESULTMAP
13
16#include "ROOT/RDF/Utils.hxx" // Union
17
18#include <stdexcept>
19#include <string>
20#include <unordered_map>
21#include <vector>
22
23namespace ROOT {
24
25namespace RDF {
26
27namespace Experimental {
28// fwd decl for MakeResultMap
29template <typename T>
30class RResultMap;
31} // namespace Experimental
32} // namespace RDF
33
34namespace Internal {
35namespace RDF {
36template <typename T>
38MakeResultMap(std::shared_ptr<T> nominalResult, std::vector<std::shared_ptr<T>> &&variedResults,
39 std::vector<std::string> &&keys, RLoopManager &lm,
40 std::shared_ptr<ROOT::Internal::RDF::RActionBase> nominalAction,
41 std::shared_ptr<ROOT::Internal::RDF::RActionBase> variedAction)
42{
43 return ROOT::RDF::Experimental::RResultMap<T>(std::move(nominalResult), std::move(variedResults), std::move(keys),
44 lm, std::move(nominalAction), std::move(variedAction));
45}
46} // namespace RDF
47} // namespace Internal
48
49namespace RDF {
50
51namespace Experimental {
52
53template <typename T>
55
56 std::vector<std::string> fKeys; // values are the keys available in fMap
57 std::unordered_map<std::string, std::shared_ptr<T>> fMap; // shared_ptrs are never null
59 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fNominalAction; // never null
60 std::shared_ptr<ROOT::Internal::RDF::RActionBase> fVariedAction; // might be null if there are no variations
61
63 ROOT::Internal::RDF::MakeResultMap<T>(std::shared_ptr<T> nominalResult,
64 std::vector<std::shared_ptr<T>> &&variedResults,
65 std::vector<std::string> &&keys, ROOT::Detail::RDF::RLoopManager &lm,
66 std::shared_ptr<ROOT::Internal::RDF::RActionBase> nominalAction,
67 std::shared_ptr<ROOT::Internal::RDF::RActionBase> variedAction);
68
69 // The preconditions are that results and keys have the same size, are ordered the same way, and keys are unique.
70 RResultMap(std::shared_ptr<T> &&nominalResult, std::vector<std::shared_ptr<T>> &&variedResults,
71 std::vector<std::string> &&keys, ROOT::Detail::RDF::RLoopManager &lm,
72 std::shared_ptr<ROOT::Internal::RDF::RActionBase> nominalAction,
73 std::shared_ptr<ROOT::Internal::RDF::RActionBase> variedAction)
74 : fKeys{ROOT::Internal::RDF::Union({"nominal"}, keys)}, fLoopManager(&lm),
75 fNominalAction(std::move(nominalAction)), fVariedAction(std::move(variedAction))
76 {
77 R__ASSERT(variedResults.size() == keys.size() && "Keys and values have different sizes!");
78 std::size_t i = 0u;
79 fMap.insert({"nominal", std::move(nominalResult)});
80 for (const auto &k : keys) {
81 auto it = fMap.insert({k, variedResults[i++]});
82 R__ASSERT(it.second &&
83 "Failed to insert an element in RResultMap, maybe a duplicated key? This should never happen.");
84 }
85 }
86
87public:
88 // TODO: can we use a std::string_view here without having to create a temporary std::string anyway?
89 T &operator[](const std::string &key)
90 {
91 auto it = fMap.find(key);
92 if (it == fMap.end())
93 throw std::runtime_error("RResultMap: no result with key \"" + key + "\".");
94
95 if ((fVariedAction != nullptr && !fVariedAction->HasRun()) || !fNominalAction->HasRun())
96 fLoopManager->Run();
97 return *it->second;
98 }
99
100 const std::vector<std::string> &GetKeys() const { return fKeys; }
101};
102
103} // namespace Experimental
104} // namespace RDF
105} // namespace ROOT
106
107#endif
#define R__ASSERT(e)
Definition TError.h:118
The head node of a RDF computation graph.
T & operator[](const std::string &key)
ROOT::Detail::RDF::RLoopManager * fLoopManager
RResultMap(std::shared_ptr< T > &&nominalResult, std::vector< std::shared_ptr< T > > &&variedResults, std::vector< std::string > &&keys, ROOT::Detail::RDF::RLoopManager &lm, std::shared_ptr< ROOT::Internal::RDF::RActionBase > nominalAction, std::shared_ptr< ROOT::Internal::RDF::RActionBase > variedAction)
const std::vector< std::string > & GetKeys() const
std::shared_ptr< ROOT::Internal::RDF::RActionBase > fNominalAction
std::unordered_map< std::string, std::shared_ptr< T > > fMap
std::vector< std::string > fKeys
std::shared_ptr< ROOT::Internal::RDF::RActionBase > fVariedAction
ROOT::RDF::Experimental::RResultMap< T > MakeResultMap(std::shared_ptr< T > nominalResult, std::vector< std::shared_ptr< T > > &&variedResults, std::vector< std::string > &&keys, RLoopManager &lm, std::shared_ptr< ROOT::Internal::RDF::RActionBase > nominalAction, std::shared_ptr< ROOT::Internal::RDF::RActionBase > variedAction)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...