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 auto &at(RooAbsArg const *arg, RooAbsArg const * /*caller*/ = nullptr)
89 {
90 std::size_t idx = arg->dataToken();
91 return _dataMap[idx];
92 }
93
94 inline auto &at(RooAbsArg const *arg, RooAbsArg const *caller = nullptr) const
95 {
96 return const_cast<DataMap *>(this)->at(arg, caller);
97 }
98
99 template <class T>
100 inline auto &at(RooTemplateProxy<T> const &proxy)
101 {
102 return at(&proxy.arg(), proxy.owner());
103 }
104
105 template <class T>
106 inline auto &at(RooTemplateProxy<T> const &proxy) const
107 {
108 return at(&proxy.arg(), proxy.owner());
109 }
110
111private:
112 std::vector<RooSpan<const double>> _dataMap;
113};
114
115} // namespace Detail
116} // namespace RooFit
117
118#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:69
std::size_t dataToken() const
Returns the token for retrieving results in the BatchMode. For internal use only.
Definition RooAbsArg.h:724
RooAbsArg * owner() const
Returns the owner of this proxy.
Definition RooArgProxy.h:49
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:112
auto empty() const
Definition DataMap.h:80
auto size() const
Definition DataMap.h:85
auto & at(RooTemplateProxy< T > const &proxy) const
Definition DataMap.h:106
auto & at(RooAbsArg const *arg, RooAbsArg const *=nullptr)
Definition DataMap.h:88
auto & at(RooTemplateProxy< T > const &proxy)
Definition DataMap.h:100
auto resize(std::size_t n)
Definition DataMap.h:86
auto end() const
Definition DataMap.h:84
auto begin() const
Definition DataMap.h:83
auto & at(RooAbsArg const *arg, RooAbsArg const *caller=nullptr) const
Definition DataMap.h:94
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