Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooArgSet.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooArgSet.h,v 1.45 2007/08/09 19:55:47 wouter Exp $
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16#ifndef ROO_ARG_SET
17#define ROO_ARG_SET
18
19#include "RooAbsCollection.h"
20
21class RooAbsArg ;
22class RooArgList ;
23
24// # Original comment on USEMEMPOOLFORARGSET:
25//
26// Use a memory pool for RooArgSet.
27// RooFit assumes (e.g. for caching results) that arg sets that have the same pointer have
28// the same contents. Trying to remove that memory pool lead to wrong results, because the
29// OS *occasionally* returns the same address, and the caching goes wrong.
30// It's hard to track down, so disable this only when e.g. looking for memory leaks!
31//
32// # Update April 2022:
33//
34// Using pointers comparisons for caching RooFit results caused too many bugs,
35// even with the memory pool. For example, if the RooArgSet is created on the
36// stack, there is no guarantee that memory is not reused. Also, pointer
37// comparisons still work if the RooArgSets for the cache entry are already out
38// of scope, which can also cause problems. Therefore, when RooArgSets are used
39// for caching, RooFit now uses the `RooArgSet::uniqueId()` as of PR [1].
40//
41// Since pointers are not used as cache keys anymore, the custom memory pool
42// is not necessary anymore. It was decided to deactivate it, because it also
43// caused quite some trouble on its own. It caused unexpected memory increases,
44// possibly because of heap fragmentation [2], and overloading `operator new`
45// and `delete` caused PyROOT issues on some platforms.
46//
47// [1] https://github.com/root-project/root/pull/10333
48// [2] https://github.com/root-project/root/issues/8323
49
50// #define USEMEMPOOLFORARGSET
51
52template <class RooSet_t, size_t>
54
56public:
57
58#ifdef USEMEMPOOLFORARGSET
59 void* operator new (size_t bytes);
60 void* operator new (size_t bytes, void* ptr) noexcept;
61 void operator delete (void *ptr);
62#endif
63
64 // Constructors, assignment etc.
65 RooArgSet();
66
67 /// Construct a (non-owning) RooArgSet from one or more
68 /// RooFit objects. The set will not own its contents.
69 /// \tparam Ts Parameter pack of objects that derive from RooAbsArg or RooFit collections; or a name.
70 /// \param arg A RooFit object.
71 /// Note that you can also pass a `double` as first argument
72 /// when constructing a RooArgSet, and another templated
73 /// constructor will be used where a RooConstVar is implicitly
74 /// created from the `double` value.
75 /// \param moreArgsOrName Arbitrary number of
76 /// - Further RooFit objects that derive from RooAbsArg
77 /// - RooFit collections of such objects
78 /// - `double`s from which a RooConstVar is implicitly created via `RooFit::RooConst`.
79 /// - A name for the set. Given multiple names, the last-given name prevails.
80 template<typename... Args_t>
81 RooArgSet(const RooAbsArg& arg, Args_t &&... moreArgsOrName)
82 /*NB: Making this a delegating constructor led to linker errors with MSVC*/
83 {
84 // This constructor should cause a failed static_assert if any of the input
85 // arguments is a temporary (r-value reference), which will be checked in
86 // processArg. This works statically because of the universal reference
87 // mechanism with templated functions.
88 // Unfortunately, we can't check the first arg, because it's type can't be
89 // a template parameter and hence a universal reference can't be used.
90 // This problem is solved by introducing another templated constructor below,
91 // which accepts a RooAbsArg && as the first argument which is forwarded to
92 // be the second argument for this constructor.
93 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
94 }
95
96 /// This constructor will provoke a `static_assert`, because passing a
97 /// RooAbsArg as r-value reference is not allowed.
98 template<typename... Args_t>
99 RooArgSet(RooAbsArg && arg, Args_t &&... moreArgsOrName)
100 : RooArgSet{arg, std::move(arg), std::forward<Args_t>(moreArgsOrName)...} {}
101
102 template<typename... Args_t>
103 explicit RooArgSet(double arg, Args_t &&... moreArgsOrName) {
104 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
105 }
106
107 /// Construct a (non-owning) RooArgSet from iterators.
108 /// \tparam Iterator_t An iterator pointing to RooFit objects or to pointers/references of those.
109 /// \param beginIt Iterator to first element to add.
110 /// \param endIt Iterator to end of range to be added.
111 /// \param name Optional name of the collection.
112 template<typename Iterator_t,
113 typename value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator_t>::value_type>::type,
114 typename = std::enable_if<std::is_convertible<const value_type*, const RooAbsArg*>::value> >
115 RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char* name="") :
116 RooArgSet(name) {
117 for (auto it = beginIt; it != endIt; ++it) {
118 processArg(*it);
119 }
120 }
121
122 /// Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
123 /// This constructor is mainly intended for pyROOT. With cppyy, a Python list
124 /// or tuple can be implicitly converted to an std::vector, and by enabling
125 /// implicit construction of a RooArgSet from a std::vector, we indirectly
126 /// enable implicit conversion from a Python list/tuple to RooArgSets.
127 /// \param vec A vector with pointers to the arguments or doubles for RooFit::RooConst().
128 RooArgSet(std::vector<RooAbsArgPtrOrDouble> const& vec) {
129 for(auto const& arg : vec) {
130 if(arg.hasPtr) processArg(arg.ptr);
131 else processArg(arg.val);
132 }
133 }
134
135 RooArgSet(const RooArgSet& other, const char *name="");
136 /// Move constructor.
137 RooArgSet(RooArgSet && other) : RooAbsCollection(std::move(other)) {}
138
139 RooArgSet(const RooArgSet& set1, const RooArgSet& set2,
140 const char *name="");
141
142 RooArgSet(const RooAbsCollection& coll) ;
143 RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1);
144 explicit RooArgSet(const TCollection& tcoll, const char* name="") ;
145 explicit RooArgSet(const char *name);
146
147 ~RooArgSet() override;
148 TObject* clone(const char* newname) const override { return new RooArgSet(*this,newname); }
149 TObject* create(const char* newname) const override { return new RooArgSet(newname); }
150 RooArgSet& operator=(const RooArgSet& other) { RooAbsCollection::operator=(other) ; return *this ;}
151
152 using RooAbsCollection::operator[];
153 RooAbsArg& operator[](const TString& str) const;
154
155
156 /// Shortcut for readFromStream(std::istream&, bool, const char*, const char*, bool), setting
157 /// `flagReadAtt` and `section` to 0.
158 virtual bool readFromStream(std::istream& is, bool compact, bool verbose=false) {
159 // I/O streaming interface (machine readable)
160 return readFromStream(is, compact, nullptr, nullptr, verbose) ;
161 }
162 bool readFromStream(std::istream& is, bool compact, const char* flagReadAtt, const char* section, bool verbose=false) ;
163 virtual void writeToStream(std::ostream& os, bool compact, const char* section=nullptr) const;
164 void writeToFile(const char* fileName) const ;
165 bool readFromFile(const char* fileName, const char* flagReadAtt=nullptr, const char* section=nullptr, bool verbose=false) ;
166
167
168 /// Check if this exact instance is in this collection.
169 bool containsInstance(const RooAbsArg& var) const override {
170 return find(var) == &var;
171 }
172
173 static void cleanup() ;
174
175 bool isInRange(const char* rangeSpec) ;
176
177 /// Use RooAbsCollection::snapshot(), but return as RooArgSet.
178 RooArgSet * snapshot(bool deepCopy = true) const {
179 return static_cast<RooArgSet*>(RooAbsCollection::snapshot(deepCopy));
180 }
181
182 /// \copydoc RooAbsCollection::snapshot()
183 bool snapshot(RooAbsCollection& output, bool deepCopy=true) const {
184 return RooAbsCollection::snapshot(output, deepCopy);
185 }
186
187protected:
188 bool checkForDup(const RooAbsArg& arg, bool silent) const ;
189 bool canBeAdded(const RooAbsArg& arg, bool silent) const override {
190 return !checkForDup(arg, silent);
191 }
192
193private:
194
195 template<typename... Args_t>
196 void processArgs(Args_t &&... args) {
197 // Expand parameter pack in C++ 11 way:
198 int dummy[] = { 0, (processArg(std::forward<Args_t>(args)), 0) ... };
199 (void)dummy;
200 }
201 void processArg(const RooAbsArg& arg) { add(arg); }
202 void processArg(const RooAbsArg* arg) { add(*arg); }
203 void processArg(RooAbsArg* var) { add(*var); }
204 template<class Arg_t>
205 void processArg(Arg_t && arg) {
206 assert_is_no_temporary(std::forward<Arg_t>(arg));
207 add(arg);
208 }
209 void processArg(const char* name) { _name = name; }
210 void processArg(double value);
211 void processArg(const RooAbsCollection& coll) { add(coll); if (_name.Length() == 0) _name = coll.GetName(); }
212 // this overload with r-value references is needed so we don't trigger the
213 // templated function with the failing static_assert for r-value references
214 void processArg(RooAbsCollection && coll) { processArg(coll); }
215 void processArg(const RooArgList& list);
216
217#ifdef USEMEMPOOLFORARGSET
218 typedef MemPoolForRooSets<RooArgSet, 10*600> MemPool; //600 = about 100 kb
219 //Initialise a static mem pool. It has to happen inside a function to solve the
220 //static initialisation order fiasco. At the end of the program, this might have
221 //to leak depending if RooArgSets are still alive. This depends on the order of destructions.
222 static MemPool* memPool();
223#endif
224
225 ClassDefOverride(RooArgSet,1) // Set of RooAbsArg objects
226};
227
228
229namespace RooFitShortHand {
230
231template<class... Args_t>
232RooArgSet S(Args_t&&... args) {
233 return {std::forward<Args_t>(args)...};
234}
235
236} // namespace RooFitShortHand
237
238
239#endif
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t bytes
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
Memory pool for RooArgSet and RooDataSet.
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:79
Abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * snapshot(bool deepCopy=true) const
Take a snap shot of current collection contents.
const char * GetName() const override
Returns name of object.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
TString _name
Our name.
static void assert_is_no_temporary(T &&)
RooAbsCollection & operator=(const RooAbsCollection &other)
Assign values from the elements in other to our elements.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
bool isInRange(const char *rangeSpec)
void processArg(RooAbsCollection &&coll)
Definition RooArgSet.h:214
TObject * clone(const char *newname) const override
Definition RooArgSet.h:148
RooArgSet & operator=(const RooArgSet &other)
Definition RooArgSet.h:150
bool checkForDup(const RooAbsArg &arg, bool silent) const
Check if element with var's name is already in set.
RooArgSet(RooAbsArg &&arg, Args_t &&... moreArgsOrName)
This constructor will provoke a static_assert, because passing a RooAbsArg as r-value reference is no...
Definition RooArgSet.h:99
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:178
void processArg(Arg_t &&arg)
Definition RooArgSet.h:205
RooArgSet()
Default constructor.
RooArgSet(std::vector< RooAbsArgPtrOrDouble > const &vec)
Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
Definition RooArgSet.h:128
~RooArgSet() override
Destructor.
bool containsInstance(const RooAbsArg &var) const override
Check if this exact instance is in this collection.
Definition RooArgSet.h:169
bool snapshot(RooAbsCollection &output, bool deepCopy=true) const
Take a snap shot of current collection contents.
Definition RooArgSet.h:183
void processArg(const char *name)
Definition RooArgSet.h:209
RooArgSet(RooArgSet &&other)
Move constructor.
Definition RooArgSet.h:137
bool canBeAdded(const RooAbsArg &arg, bool silent) const override
Determine whether it's possible to add a given RooAbsArg to the collection or not.
Definition RooArgSet.h:189
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
TObject * create(const char *newname) const override
Definition RooArgSet.h:149
void processArgs(Args_t &&... args)
Definition RooArgSet.h:196
void processArg(const RooAbsArg *arg)
Definition RooArgSet.h:202
void processArg(const RooAbsCollection &coll)
Definition RooArgSet.h:211
RooArgSet(double arg, Args_t &&... moreArgsOrName)
Definition RooArgSet.h:103
void processArg(const RooArgList &list)
bool readFromFile(const char *fileName, const char *flagReadAtt=nullptr, const char *section=nullptr, bool verbose=false)
Read contents of the argset from specified file.
void processArg(RooAbsArg *var)
Definition RooArgSet.h:203
static void cleanup()
Definition RooArgSet.cxx:80
virtual bool readFromStream(std::istream &is, bool compact, bool verbose=false)
Shortcut for readFromStream(std::istream&, bool, const char*, const char*, bool), setting flagReadAtt...
Definition RooArgSet.h:158
RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char *name="")
Construct a (non-owning) RooArgSet from iterators.
Definition RooArgSet.h:115
void processArg(const RooAbsArg &arg)
Definition RooArgSet.h:201
RooAbsArg & operator[](const TString &str) const
Get reference to an element using its name.
RooArgSet(const RooAbsArg &arg, Args_t &&... moreArgsOrName)
Construct a (non-owning) RooArgSet from one or more RooFit objects.
Definition RooArgSet.h:81
virtual void writeToStream(std::ostream &os, bool compact, const char *section=nullptr) const
Write the contents of the argset in ASCII form to given stream.
Collection abstract base class.
Definition TCollection.h:65
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:421
RooArgSet S(Args_t &&... args)
Definition RooArgSet.h:232
static void output()