Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
DataMap.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 12/2021
5 *
6 * Copyright (c) 2022, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#ifndef RooFit_Detail_DataMap_h
14#define RooFit_Detail_DataMap_h
15
16#include <RooAbsArg.h>
17
18#include <ROOT/RSpan.hxx>
19
20#include <TNamed.h>
21#include <TObject.h>
22
23#include <map>
24#include <stdexcept>
25#include <sstream>
26
27template <class T>
29
30namespace RooBatchCompute {
31class Config;
32}
33
34/// \class RooFit::DataKey
35/// To use as a key type for RooFit data maps and containers. A RooFit::DataKey
36/// can be constructed with no runtime overhead from a RooAbsArg (or any
37/// templated RooFit proxy for convenience). Compared to using the RooAbsArg
38/// pointer directly, this has the advantage that one can easily change the way
39/// the key is constructed from the object, just by changing the implementation
40/// of the DataKey. For example, it is trivial to move from using the RooAbsArg
41/// pointer to using the unique name pointer retrieved by RooAbsArg::namePtr().
42
43namespace RooFit {
44namespace Detail {
45
46class DataKey {
47public:
48 inline DataKey(RooAbsArg const *arg) : _ptr{arg->namePtr()} {}
49 inline DataKey(TNamed const *arg) : _ptr{arg} {}
50 template <class T>
51 inline DataKey(RooTemplateProxy<T> const &proxy) : DataKey{&*proxy}
52 {
53 }
54
55 // Comparison operators that wrap the pointer comparisons.
56 friend inline bool operator==(const DataKey &k1, const DataKey &k2) { return k1._ptr == k2._ptr; }
57 friend inline bool operator!=(const DataKey &k1, const DataKey &k2) { return k1._ptr != k2._ptr; }
58 friend inline bool operator<(const DataKey &k1, const DataKey &k2) { return k1._ptr < k2._ptr; }
59
60 // Implementing pointer-style operators.
61 inline TObject const &operator*() const { return *_ptr; }
62 inline TObject const *operator->() const { return _ptr; }
63
64private:
65 TObject const *_ptr;
66};
67
68} // namespace Detail
69} // namespace RooFit
70
71namespace std {
72
73template <>
74struct hash<RooFit::Detail::DataKey> {
75 std::size_t operator()(const RooFit::Detail::DataKey &k) const { return hash<TObject const *>{}(&*k); }
76};
77
78} // namespace std
79
80namespace RooFit {
81namespace Detail {
82
83class DataMap {
84public:
85 auto size() const
86 {
87 return _dataMap.size();
88 }
89 void resize(std::size_t n);
90
91 inline void set(RooAbsArg const *arg, std::span<const double> const &span)
92 {
93 if (!arg->hasDataToken())
94 return;
95 std::size_t idx = arg->dataToken();
96 _dataMap[idx] = span;
97 }
98
99 void setConfig(RooAbsArg const *arg, RooBatchCompute::Config const &config);
100
101 std::span<const double> at(RooAbsArg const *arg, RooAbsArg const *caller = nullptr);
102
103 inline std::span<const double> at(RooAbsArg const *arg, RooAbsArg const *caller = nullptr) const
104 {
105 return const_cast<DataMap *>(this)->at(arg, caller);
106 }
107
108 template <class T>
109 inline std::span<const double> at(RooTemplateProxy<T> const &proxy)
110 {
111 return at(&proxy.arg(), proxy.owner());
112 }
113
114 template <class T>
115 inline std::span<const double> at(RooTemplateProxy<T> const &proxy) const
116 {
117 return at(&proxy.arg(), proxy.owner());
118 }
119
120 RooBatchCompute::Config config(RooAbsArg const *arg) const;
121
122private:
123 std::vector<std::span<const double>> _dataMap;
124 std::vector<RooBatchCompute::Config> _cfgs;
125};
126
127} // namespace Detail
128} // namespace RooFit
129
130#endif
TRObject operator()(const T1 &t1) const
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
bool hasDataToken() const
Definition RooAbsArg.h:707
std::size_t dataToken() const
Returns the token for retrieving results in the BatchMode. For internal use only.
Definition RooAbsArg.h:706
RooAbsArg * owner() const
Returns the owner of this proxy.
Definition RooArgProxy.h:57
Minimal configuration struct to steer the evaluation of a single node with the RooBatchCompute librar...
DataKey(RooTemplateProxy< T > const &proxy)
Definition DataMap.h:51
friend bool operator!=(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:57
DataKey(RooAbsArg const *arg)
Definition DataMap.h:48
DataKey(TNamed const *arg)
Definition DataMap.h:49
TObject const * _ptr
Definition DataMap.h:65
TObject const * operator->() const
Definition DataMap.h:62
friend bool operator<(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:58
friend bool operator==(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:56
TObject const & operator*() const
Definition DataMap.h:61
std::span< const double > at(RooTemplateProxy< T > const &proxy)
Definition DataMap.h:109
std::vector< RooBatchCompute::Config > _cfgs
Definition DataMap.h:124
auto size() const
Definition DataMap.h:85
RooBatchCompute::Config config(RooAbsArg const *arg) const
Definition DataMap.cxx:40
std::vector< std::span< const double > > _dataMap
Definition DataMap.h:123
std::span< const double > at(RooTemplateProxy< T > const &proxy) const
Definition DataMap.h:115
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:22
void set(RooAbsArg const *arg, std::span< const double > const &span)
Definition DataMap.h:91
void setConfig(RooAbsArg const *arg, RooBatchCompute::Config const &config)
Definition DataMap.cxx:32
std::span< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr) const
Definition DataMap.h:103
void resize(std::size_t n)
Definition DataMap.cxx:49
const T & arg() const
Return reference to object held in proxy.
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
Mother of all ROOT objects.
Definition TObject.h:41
const Int_t n
Definition legend1.C:16
Namespace for dispatching RooFit computations to various backends.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition JSONIO.h:26