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#include "RooAbsArg.h"
21#include "RooFit/UniqueId.h"
22
23
24class RooArgList ;
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#define USEMEMPOOLFORARGSET
32template <class RooSet_t, size_t>
34
36public:
37
38#ifdef USEMEMPOOLFORARGSET
39 void* operator new (size_t bytes);
40 void* operator new (size_t bytes, void* ptr) noexcept;
41 void operator delete (void *ptr);
42#endif
43
44 // Constructors, assignment etc.
45 RooArgSet();
46
47 /// Construct a (non-owning) RooArgSet from one or more
48 /// RooFit objects. The set will not own its contents.
49 /// \tparam Ts Parameter pack of objects that derive from RooAbsArg or RooFit collections; or a name.
50 /// \param arg A RooFit object.
51 /// Note that you can also pass a `double` as first argument
52 /// when constructing a RooArgSet, and another templated
53 /// constructor will be used where a RooConstVar is implicitly
54 /// created from the `double` value.
55 /// \param moreArgsOrName Arbitrary number of
56 /// - Further RooFit objects that derive from RooAbsArg
57 /// - RooFit collections of such objects
58 /// - `double`s from which a RooConstVar is implicitly created via `RooFit::RooConst`.
59 /// - A name for the set. Given multiple names, the last-given name prevails.
60 template<typename... Args_t>
61 RooArgSet(const RooAbsArg& arg, Args_t &&... moreArgsOrName)
62 /*NB: Making this a delegating constructor led to linker errors with MSVC*/
63 {
64 // This constructor should cause a failed static_assert if any of the input
65 // arguments is a temporary (r-value reference), which will be checked in
66 // processArg. This works statically because of the universal reference
67 // mechanism with templated functions.
68 // Unfortunately, we can't check the first arg, because it's type can't be
69 // a template parameter and hence a universal reference can't be used.
70 // This problem is solved by introducing another templated constructor below,
71 // which accepts a RooAbsArg && as the first argument which is forwarded to
72 // be the second argument for this constructor.
73 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
74 }
75
76 /// This constructor will provoke a `static_assert`, because passing a
77 /// RooAbsArg as r-value reference is not allowed.
78 template<typename... Args_t>
79 RooArgSet(RooAbsArg && arg, Args_t &&... moreArgsOrName)
80 : RooArgSet{arg, std::move(arg), std::forward<Args_t>(moreArgsOrName)...} {}
81
82 template<typename... Args_t>
83 explicit RooArgSet(double arg, Args_t &&... moreArgsOrName) {
84 processArgs(arg, std::forward<Args_t>(moreArgsOrName)...);
85 }
86
87 /// Construct a (non-owning) RooArgSet from iterators.
88 /// \tparam Iterator_t An iterator pointing to RooFit objects or to pointers/references of those.
89 /// \param beginIt Iterator to first element to add.
90 /// \param endIt Iterator to end of range to be added.
91 /// \param name Optional name of the collection.
92 template<typename Iterator_t,
93 typename value_type = typename std::remove_pointer<typename std::iterator_traits<Iterator_t>::value_type>::type,
94 typename = std::enable_if<std::is_convertible<const value_type*, const RooAbsArg*>::value> >
95 RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char* name="") :
97 for (auto it = beginIt; it != endIt; ++it) {
98 processArg(*it);
99 }
100 }
101
102 /// Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
103 /// This constructor is mainly intended for pyROOT. With cppyy, a Python list
104 /// or tuple can be implicitly converted to an std::vector, and by enabling
105 /// implicit construction of a RooArgSet from a std::vector, we indirectly
106 /// enable implicit conversion from a Python list/tuple to RooArgSets.
107 /// \param vec A vector with pointers to the arguments or doubles for RooFit::RooConst().
108 RooArgSet(std::vector<RooAbsArgPtrOrDouble> const& vec) {
109 for(auto const& arg : vec) {
110 if(arg.hasPtr) processArg(arg.ptr);
111 else processArg(arg.val);
112 }
113 }
114
115 RooArgSet(const RooArgSet& other, const char *name="");
116 /// Move constructor.
117 RooArgSet(RooArgSet && other) : RooAbsCollection(std::move(other)) {}
118
119 RooArgSet(const RooArgSet& set1, const RooArgSet& set2,
120 const char *name="");
121
122 RooArgSet(const RooAbsCollection& coll) ;
123 RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1);
124 explicit RooArgSet(const TCollection& tcoll, const char* name="") ;
125 explicit RooArgSet(const char *name);
126
127 ~RooArgSet() override;
128 TObject* clone(const char* newname) const override { return new RooArgSet(*this,newname); }
129 TObject* create(const char* newname) const override { return new RooArgSet(newname); }
130 RooArgSet& operator=(const RooArgSet& other) { RooAbsCollection::operator=(other) ; return *this ;}
131
132 using RooAbsCollection::operator[];
133 RooAbsArg& operator[](const TString& str) const;
134
135
136 /// Shortcut for readFromStream(std::istream&, Bool_t, const char*, const char*, Bool_t), setting
137 /// `flagReadAtt` and `section` to 0.
138 virtual bool readFromStream(std::istream& is, bool compact, bool verbose=false) {
139 // I/O streaming interface (machine readable)
140 return readFromStream(is, compact, 0, 0, verbose) ;
141 }
142 Bool_t readFromStream(std::istream& is, Bool_t compact, const char* flagReadAtt, const char* section, Bool_t verbose=kFALSE) ;
143 virtual void writeToStream(std::ostream& os, bool compact, const char* section=0) const;
144 void writeToFile(const char* fileName) const ;
145 Bool_t readFromFile(const char* fileName, const char* flagReadAtt=0, const char* section=0, Bool_t verbose=kFALSE) ;
146
147
148 /// Check if this exact instance is in this collection.
149 Bool_t containsInstance(const RooAbsArg& var) const override {
150 return find(var) == &var;
151 }
152
153 static void cleanup() ;
154
155 Bool_t isInRange(const char* rangeSpec) ;
156
157 /// Use RooAbsCollection::snapshot(), but return as RooArgSet.
158 RooArgSet * snapshot(bool deepCopy = true) const {
159 return static_cast<RooArgSet*>(RooAbsCollection::snapshot(deepCopy));
160 }
161
162 /// \copydoc RooAbsCollection::snapshot()
164 return RooAbsCollection::snapshot(output, deepCopy);
165 }
166
167 /// Returns a unique ID that is different for every instantiated RooArgSet.
168 /// This ID can be used to check whether two RooAbsData are the same object,
169 /// which is safer than memory address comparisons that might result in false
170 /// positives when memory is recycled.
172
173protected:
174 Bool_t checkForDup(const RooAbsArg& arg, Bool_t silent) const ;
175 virtual bool canBeAdded(const RooAbsArg& arg, bool silent) const override {
176 return !checkForDup(arg, silent);
177 }
178
179private:
180
181 template<typename... Args_t>
182 void processArgs(Args_t &&... args) {
183 // Expand parameter pack in C++ 11 way:
184 int dummy[] = { 0, (processArg(std::forward<Args_t>(args)), 0) ... };
185 (void)dummy;
186 }
187 void processArg(const RooAbsArg& arg) { add(arg); }
188 void processArg(const RooAbsArg* arg) { add(*arg); }
189 void processArg(RooAbsArg* var) { add(*var); }
190 template<class Arg_t>
191 void processArg(Arg_t && arg) {
192 assert_is_no_temporary(std::forward<Arg_t>(arg));
193 add(arg);
194 }
195 void processArg(const char* name) { _name = name; }
196 void processArg(double value);
197 void processArg(const RooAbsCollection& coll) { add(coll); if (_name.Length() == 0) _name = coll.GetName(); }
198 // this overload with r-value references is needed so we don't trigger the
199 // templated function with the failing static_assert for r-value references
200 void processArg(RooAbsCollection && coll) { processArg(coll); }
201 void processArg(const RooArgList& list);
202
203#ifdef USEMEMPOOLFORARGSET
204 typedef MemPoolForRooSets<RooArgSet, 10*600> MemPool; //600 = about 100 kb
205 //Initialise a static mem pool. It has to happen inside a function to solve the
206 //static initialisation order fiasco. At the end of the program, this might have
207 //to leak depending if RooArgSets are still alive. This depends on the order of destructions.
208 static MemPool* memPool();
209#endif
211
212 ClassDefOverride(RooArgSet,1) // Set of RooAbsArg objects
213};
214
215
216namespace RooFitShortHand {
217
218template<class... Args_t>
219RooArgSet S(Args_t&&... args) {
220 return {std::forward<Args_t>(args)...};
221}
222
223} // namespace RooFitShortHand
224
225
226#endif
typedef void(GLAPIENTRYP _GLUfuncptr)(void)
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassDefOverride(name, id)
Definition Rtypes.h:329
char name[80]
Definition TGX11.cxx:110
int type
Definition TGX11.cxx:121
Memory pool for RooArgSet and RooDataSet.
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
static void assert_is_no_temporary(T &&)
const char * GetName() const
Returns name of object.
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:35
void processArg(RooAbsCollection &&coll)
Definition RooArgSet.h:200
TObject * clone(const char *newname) const override
Definition RooArgSet.h:128
RooArgSet & operator=(const RooArgSet &other)
Definition RooArgSet.h:130
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:79
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition RooArgSet.h:158
void processArg(Arg_t &&arg)
Definition RooArgSet.h:191
RooArgSet()
Default constructor.
RooFit::UniqueId< RooArgSet > const & uniqueId() const
Returns a unique ID that is different for every instantiated RooArgSet.
Definition RooArgSet.h:171
RooArgSet(std::vector< RooAbsArgPtrOrDouble > const &vec)
Construct a non-owning RooArgSet from a vector of RooAbsArg pointers.
Definition RooArgSet.h:108
~RooArgSet() override
Destructor.
Bool_t snapshot(RooAbsCollection &output, Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents.
Definition RooArgSet.h:163
Bool_t readFromFile(const char *fileName, const char *flagReadAtt=0, const char *section=0, Bool_t verbose=kFALSE)
Read contents of the argset from specified file.
Bool_t isInRange(const char *rangeSpec)
virtual void writeToStream(std::ostream &os, bool compact, const char *section=0) const
Write the contents of the argset in ASCII form to given stream.
void processArg(const char *name)
Definition RooArgSet.h:195
RooArgSet(RooArgSet &&other)
Move constructor.
Definition RooArgSet.h:117
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
TObject * create(const char *newname) const override
Definition RooArgSet.h:129
static MemPool * memPool()
void processArgs(Args_t &&... args)
Definition RooArgSet.h:182
virtual 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:175
void processArg(const RooAbsArg *arg)
Definition RooArgSet.h:188
void processArg(const RooAbsCollection &coll)
Definition RooArgSet.h:197
Bool_t containsInstance(const RooAbsArg &var) const override
Check if this exact instance is in this collection.
Definition RooArgSet.h:149
RooArgSet(double arg, Args_t &&... moreArgsOrName)
Definition RooArgSet.h:83
MemPoolForRooSets< RooArgSet, 10 *600 > MemPool
Definition RooArgSet.h:204
void processArg(const RooArgList &list)
void processArg(RooAbsArg *var)
Definition RooArgSet.h:189
const RooFit::UniqueId< RooArgSet > _uniqueId
Definition RooArgSet.h:210
static void cleanup()
Definition RooArgSet.cxx:73
Bool_t checkForDup(const RooAbsArg &arg, Bool_t silent) const
Check if element with var's name is already in set.
virtual bool readFromStream(std::istream &is, bool compact, bool verbose=false)
Shortcut for readFromStream(std::istream&, Bool_t, const char*, const char*, Bool_t),...
Definition RooArgSet.h:138
RooArgSet(Iterator_t beginIt, Iterator_t endIt, const char *name="")
Construct a (non-owning) RooArgSet from iterators.
Definition RooArgSet.h:95
void processArg(const RooAbsArg &arg)
Definition RooArgSet.h:187
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:61
Collection abstract base class.
Definition TCollection.h:65
Mother of all ROOT objects.
Definition TObject.h:41
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
RooArgSet S(Args_t &&... args)
Definition RooArgSet.h:219
A UniqueId can be added as a class member to enhance any class with a unique identifier for each inst...
Definition UniqueId.h:39
static void output(int code)
Definition gifencode.c:226