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#include <RooSpan.h>
18
19#include <TNamed.h>
20#include <TObject.h>
21
22#include <map>
23#include <stdexcept>
24#include <sstream>
25
26template <class T>
28
29/// \class RooFit::DataKey
30/// To use as a key type for RooFit data maps and containers. A RooFit::DataKey
31/// can be constructed with no runtime overhead from a RooAbsArg (or any
32/// templated RooFit proxy for convenience). Compared to using the RooAbsArg
33/// pointer directly, this has the advantage that one can easily change the way
34/// the key is constructed from the object, just by changing the implementation
35/// of the DataKey. For example, it is trivial to move from using the RooAbsArg
36/// pointer to using the unique name pointer retrieved by RooAbsArg::namePtr().
37
38namespace RooFit {
39namespace Detail {
40
41class DataKey {
42public:
43 inline DataKey(RooAbsArg const *arg) : _ptr{arg->namePtr()} {}
44 inline DataKey(TNamed const *arg) : _ptr{arg} {}
45 template <class T>
46 inline DataKey(RooTemplateProxy<T> const &proxy) : DataKey{&*proxy}
47 {
48 }
49
50 // Comparison operators that wrap the pointer comparisons.
51 friend inline bool operator==(const DataKey &k1, const DataKey &k2) { return k1._ptr == k2._ptr; }
52 friend inline bool operator!=(const DataKey &k1, const DataKey &k2) { return k1._ptr != k2._ptr; }
53 friend inline bool operator<(const DataKey &k1, const DataKey &k2) { return k1._ptr < k2._ptr; }
54
55 // Implementing pointer-style operators.
56 inline TObject const &operator*() const { return *_ptr; }
57 inline TObject const *operator->() const { return _ptr; }
58
59private:
60 TObject const *_ptr;
61};
62
63} // namespace Detail
64} // namespace RooFit
65
66namespace std {
67
68template <>
69struct hash<RooFit::Detail::DataKey> {
70 std::size_t operator()(const RooFit::Detail::DataKey &k) const { return hash<TObject const *>{}(&*k); }
71};
72
73} // namespace std
74
75namespace RooFit {
76namespace Detail {
77
78class DataMap {
79public:
80 auto empty() const { return _dataMap.empty(); }
81 auto begin() { return _dataMap.begin(); }
82 auto end() { return _dataMap.end(); }
83 auto begin() const { return _dataMap.begin(); }
84 auto end() const { return _dataMap.end(); }
85 auto size() const { return _dataMap.size(); }
86 auto resize(std::size_t n) { return _dataMap.resize(n); }
87
88 inline void set(RooAbsArg const *arg, RooSpan<const double> const &span)
89 {
90 if (!arg->hasDataToken())
91 return;
92 std::size_t idx = arg->dataToken();
93 _dataMap[idx] = span;
94 }
95
96 RooSpan<const double> at(RooAbsArg const *arg, RooAbsArg const * caller = nullptr);
97
98 inline RooSpan<const double> at(RooAbsArg const *arg, RooAbsArg const *caller = nullptr) const
99 {
100 return const_cast<DataMap *>(this)->at(arg, caller);
101 }
102
103 template <class T>
105 {
106 return at(&proxy.arg(), proxy.owner());
107 }
108
109 template <class T>
111 {
112 return at(&proxy.arg(), proxy.owner());
113 }
114
115private:
116 std::vector<RooSpan<const double>> _dataMap;
117};
118
119} // namespace Detail
120} // namespace RooFit
121
122#endif
TRObject operator()(const T1 &t1) const
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
bool hasDataToken() const
Definition RooAbsArg.h:702
std::size_t dataToken() const
Returns the token for retrieving results in the BatchMode. For internal use only.
Definition RooAbsArg.h:701
RooAbsArg * owner() const
Returns the owner of this proxy.
Definition RooArgProxy.h:58
DataKey(RooTemplateProxy< T > const &proxy)
Definition DataMap.h:46
friend bool operator!=(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:52
DataKey(RooAbsArg const *arg)
Definition DataMap.h:43
DataKey(TNamed const *arg)
Definition DataMap.h:44
TObject const * _ptr
Definition DataMap.h:60
TObject const * operator->() const
Definition DataMap.h:57
friend bool operator<(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:53
friend bool operator==(const DataKey &k1, const DataKey &k2)
Definition DataMap.h:51
TObject const & operator*() const
Definition DataMap.h:56
std::vector< RooSpan< const double > > _dataMap
Definition DataMap.h:116
auto empty() const
Definition DataMap.h:80
void set(RooAbsArg const *arg, RooSpan< const double > const &span)
Definition DataMap.h:88
auto size() const
Definition DataMap.h:85
RooSpan< const double > at(RooTemplateProxy< T > const &proxy)
Definition DataMap.h:104
RooSpan< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr) const
Definition DataMap.h:98
RooSpan< const double > at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr)
Definition DataMap.cxx:21
auto resize(std::size_t n)
Definition DataMap.h:86
auto end() const
Definition DataMap.h:84
RooSpan< const double > at(RooTemplateProxy< T > const &proxy) const
Definition DataMap.h:110
auto begin() const
Definition DataMap.h:83
A simple container to hold a batch of data values.
Definition RooSpan.h:34
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
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition Common.h:18