Logo ROOT   master
Reference Guide
RooAbsCollection.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id: RooAbsCollection.h,v 1.26 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_ABS_COLLECTION
17 #define ROO_ABS_COLLECTION
18 
19 #include "TString.h"
20 #include "RooAbsArg.h"
21 #include "RooPrintable.h"
22 #include "RooCmdArg.h"
23 #include "RooLinkedListIter.h"
24 #include <string>
25 
26 class RooCmdArg;
27 
28 class RooAbsCollection : public TObject, public RooPrintable {
29 public:
30  using Storage_t = std::vector<RooAbsArg*>;
31  using const_iterator = Storage_t::const_iterator;
32 
33 
34  // Constructors, assignment etc.
36  RooAbsCollection(const char *name);
37  virtual TObject* clone(const char* newname) const = 0 ;
38  virtual TObject* create(const char* newname) const = 0 ;
39  virtual TObject* Clone(const char* newname=0) const {
40  return clone(newname?newname:GetName()) ;
41  }
42  virtual ~RooAbsCollection();
43 
44  // Create a copy of an existing list. New variables cannot be added
45  // to a copied list. The variables in the copied list are independent
46  // of the original variables.
47  RooAbsCollection(const RooAbsCollection& other, const char *name="");
50  void assignFast(const RooAbsCollection& other, Bool_t setValDirty=kTRUE) ;
51 
52  // Copy list and contents (and optionally 'deep' servers)
53  RooAbsCollection *snapshot(Bool_t deepCopy=kTRUE) const ;
55 
56  /// \deprecated Without effect.
58  // Set size of internal hash table to i (should be a prime number)
59  }
60  /// \deprecated Without effect.
62  return 0;
63  }
64 
65  // List content management
66  virtual Bool_t add(const RooAbsArg& var, Bool_t silent=kFALSE) ;
67  virtual Bool_t addOwned(RooAbsArg& var, Bool_t silent=kFALSE);
68  virtual RooAbsArg *addClone(const RooAbsArg& var, Bool_t silent=kFALSE) ;
69  virtual Bool_t replace(const RooAbsArg& var1, const RooAbsArg& var2) ;
70  virtual Bool_t remove(const RooAbsArg& var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE) ;
71  virtual void removeAll() ;
72 
73  virtual Bool_t add(const RooAbsCollection& list, Bool_t silent=kFALSE) ;
74  virtual Bool_t addOwned(const RooAbsCollection& list, Bool_t silent=kFALSE);
75  virtual void addClone(const RooAbsCollection& list, Bool_t silent=kFALSE);
76  Bool_t replace(const RooAbsCollection &other);
77  Bool_t remove(const RooAbsCollection& list, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE) ;
78  template<class forwardIt>
79  void remove(forwardIt rangeBegin, forwardIt rangeEnd, Bool_t silent = kFALSE, Bool_t matchByNameOnly = kFALSE) {
80  for (forwardIt it = rangeBegin; it != rangeEnd; ++it) {
81  static_assert(std::is_same<
82  typename std::iterator_traits<forwardIt>::value_type,
83  RooAbsArg*>::value, "Can only remove lists of RooAbsArg*.");
84  auto castedElm = static_cast<RooAbsArg*>(*it);
85  remove(*castedElm, silent, matchByNameOnly);
86  }
87  }
88 
89  // Group operations on AbsArgs
90  void setAttribAll(const Text_t* name, Bool_t value=kTRUE) ;
91 
92  // List search methods
93  RooAbsArg *find(const char *name) const ;
94  RooAbsArg *find(const RooAbsArg&) const ;
95 
96  Bool_t contains(const RooAbsArg& var) const {
97  // Returns true if object with same name as var is contained in this collection
98  return (0 == find(var)) ? kFALSE:kTRUE;
99  }
100  Bool_t containsInstance(const RooAbsArg& var) const {
101  // Returns true if var is contained in this collection
102  return std::find(_list.begin(), _list.end(), &var) != _list.end();
103  }
104  RooAbsCollection* selectByAttrib(const char* name, Bool_t value) const ;
105  RooAbsCollection* selectCommon(const RooAbsCollection& refColl) const ;
106  RooAbsCollection* selectByName(const char* nameList, Bool_t verbose=kFALSE) const ;
107  Bool_t equals(const RooAbsCollection& otherColl) const ;
108  Bool_t overlaps(const RooAbsCollection& otherColl) const ;
109 
110  /// TIterator-style iteration over contained elements.
111  /// \note These iterators are slow. Use begin() and end() or
112  /// range-based for loop instead.
114  R__SUGGEST_ALTERNATIVE("begin(), end() and range-based for loops.") {
115  // Create and return an iterator over the elements in this collection
116  return new RooLinkedListIter(makeLegacyIterator(dir));
117  }
118 
119  /// TIterator-style iteration over contained elements.
120  /// \note This iterator is slow. Use begin() and end() or range-based for loop instead.
122  R__SUGGEST_ALTERNATIVE("begin(), end() and range-based for loops.") {
124  }
125 
126  /// One-time forward iterator.
127  /// \note Use begin() and end() or range-based for loop instead.
129  R__SUGGEST_ALTERNATIVE("begin(), end() and range-based for loops.") {
130  return RooFIter(makeLegacyIterator());
131  }
132 
134  return _list.begin();
135  }
136 
137  const_iterator end() const {
138  return _list.end();
139  }
140 
141  Storage_t::const_reverse_iterator rbegin() const {
142  return _list.rbegin();
143  }
144 
145  Storage_t::const_reverse_iterator rend() const {
146  return _list.rend();
147  }
148 
149  Storage_t::size_type size() const {
150  return _list.size();
151  }
152 
153  bool empty() const {
154  return _list.empty();
155  }
156 
157  void reserve(Storage_t::size_type count) {
158  _list.reserve(count);
159  }
160 
161  /// Clear contents. If the collection is owning, it will also delete the contents.
162  void clear() {
163  removeAll();
164  }
165 
166  inline Int_t getSize() const {
167  // Return the number of elements in the collection
168  return _list.size();
169  }
170 
171  inline RooAbsArg *first() const {
172  // Return the first element in this collection
173  return _list.front();
174  }
175 
176  RooAbsArg * operator[](Storage_t::size_type i) const {
177  return _list[i];
178  }
179 
180 
181  /// Returns index of given arg, or -1 if arg is not in the collection.
182  inline Int_t index(const RooAbsArg* arg) const {
183  auto item = std::find(_list.begin(), _list.end(), arg);
184  return item != _list.end() ? item - _list.begin() : -1;
185  }
186 
187  /// Returns index of given arg, or -1 if arg is not in the collection.
188  inline Int_t index(const RooAbsArg& arg) const {
189  return index(&arg);
190  }
191 
192  /// Returns index of arg with given name, or -1 if arg is not in the collection.
193  inline Int_t index(const char* name) const {
194  const std::string theName(name);
195  auto item = std::find_if(_list.begin(), _list.end(), [&theName](const RooAbsArg * elm){
196  return elm->GetName() == theName;
197  });
198  return item != _list.end() ? item - _list.begin() : -1;
199  }
200 
201  inline virtual void Print(Option_t *options= 0) const {
202  // Printing interface (human readable)
204  }
205  std::string contentsString() const ;
206 
207 
208  virtual void printName(std::ostream& os) const ;
209  virtual void printTitle(std::ostream& os) const ;
210  virtual void printClassName(std::ostream& os) const ;
211  virtual void printValue(std::ostream& os) const ;
212  virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
213 
214  virtual Int_t defaultPrintContents(Option_t* opt) const ;
215 
216  // Latex printing methods
217  void printLatex(const RooCmdArg& arg1=RooCmdArg(), const RooCmdArg& arg2=RooCmdArg(),
218  const RooCmdArg& arg3=RooCmdArg(), const RooCmdArg& arg4=RooCmdArg(),
219  const RooCmdArg& arg5=RooCmdArg(), const RooCmdArg& arg6=RooCmdArg(),
220  const RooCmdArg& arg7=RooCmdArg(), const RooCmdArg& arg8=RooCmdArg()) const ;
221  void printLatex(std::ostream& ofs, Int_t ncol, const char* option="NEYU", Int_t sigDigit=1,
222  const RooLinkedList& siblingLists=RooLinkedList(), const RooCmdArg* formatCmd=0) const ;
223 
224  void setName(const char *name) {
225  // Set name of collection
226  _name= name;
227  }
228  const char* GetName() const {
229  // Return namer of collection
230  return _name.Data() ;
231  }
232  Bool_t isOwning() const {
233  // Does collection own contents?
234  return _ownCont ;
235  }
236 
237  Bool_t allInRange(const char* rangeSpec) const ;
238 
239  void dump() const ;
240 
242  void takeOwnership() { _ownCont = kTRUE ; }
243 
244  void sort(Bool_t reverse = false);
245 
246  virtual void RecursiveRemove(TObject *obj);
247 
248 protected:
249  Storage_t _list; // Actual object storage
251 
252  Bool_t _ownCont; // Flag to identify a list that owns its contents.
253  TString _name; // Our name.
254  Bool_t _allRRV ; // All contents are RRV
255 
256  void safeDeleteList() ;
257 
258  // Support for snapshot method
260 
263 
264  mutable TNamed* _structureTag{nullptr}; //! Structure tag
265  mutable TNamed* _typedStructureTag{nullptr}; //! Typed structure tag
266 
268 
269  void makeStructureTag() ;
270  void makeTypedStructureTag() ;
271 
272 private:
273  std::unique_ptr<LegacyIterator_t> makeLegacyIterator (bool forward = true) const;
274 
275  ClassDef(RooAbsCollection,3) // Collection of RooAbsArg objects
276 };
277 
278 #endif
for(Int_t i=0;i< n;i++)
Definition: legend1.C:18
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
virtual void printStream(std::ostream &os, Int_t contents, StyleOption style, TString indent="") const
Print description of object on ostream, printing contents set by contents integer, which is interpreted as an OR of &#39;enum ContentsOptions&#39; values and in the style given by &#39;enum StyleOption&#39;.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
void printLatex(const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg()) const
Output content of collection as LaTex table.
Storage_t::size_type size() const
RooAbsCollection()
Default constructor.
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add a clone of the specified argument to list.
TNamed * typedStructureTag()
const char Option_t
Definition: RtypesCore.h:64
RooAbsCollection * selectCommon(const RooAbsCollection &refColl) const
Create a subset of the current collection, consisting only of those elements that are contained as we...
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Bool_t isOwning() const
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
virtual void printValue(std::ostream &os) const
Print value of collection, i.e.
const_iterator begin() const
TNamed * structureTag()
const_iterator end() const
Basic string class.
Definition: TString.h:131
virtual ~RooAbsCollection()
Destructor.
Bool_t equals(const RooAbsCollection &otherColl) const
Check if this and other collection have identically-named contents.
TIterator and GenericRooFIter front end with STL back end.
Iterator abstract base class.
Definition: TIterator.h:30
Storage_t::const_reverse_iterator rbegin() const
void safeDeleteList()
Examine client server dependencies in list and delete contents in safe order: any client is deleted b...
Int_t index(const char *name) const
Returns index of arg with given name, or -1 if arg is not in the collection.
#define ClassDef(name, id)
Definition: Rtypes.h:322
RooPlotable is a &#39;mix-in&#39; base class that define the standard RooFit plotting and printing methods...
Definition: RooPrintable.h:25
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
Int_t index(const RooAbsArg &arg) const
Returns index of given arg, or -1 if arg is not in the collection.
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
Bool_t allInRange(const char *rangeSpec) const
Return true if all contained object report to have their value inside the specified range...
void sort(Bool_t reverse=false)
Sort collection using std::sort and name comparison.
RooAbsCollection * selectByAttrib(const char *name, Bool_t value) const
Create a subset of the current collection, consisting only of those elements with the specified attri...
std::unique_ptr< LegacyIterator_t > makeLegacyIterator(bool forward=true) const
Factory for legacy iterators.
void reserve(Storage_t::size_type count)
const Bool_t kIterForward
Definition: TCollection.h:40
void setHashTableSize(Int_t)
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
void assignFast(const RooAbsCollection &other, Bool_t setValDirty=kTRUE)
Functional equivalent of operator=() but assumes this and other collection have same layout...
virtual TObject * clone(const char *newname) const =0
virtual void Print(Option_t *options=0) const
This method must be overridden when a class wants to print itself.
void clearStructureTags()
Typed structure tag.
#define R__SUGGEST_ALTERNATIVE(ALTERNATIVE)
Definition: RConfig.hxx:530
std::vector< RooAbsArg * > Storage_t
Bool_t overlaps(const RooAbsCollection &otherColl) const
Check if this and other collection have common entries.
virtual void printName(std::ostream &os) const
Return collection name.
Int_t getSize() const
virtual Int_t defaultPrintContents(Option_t *opt) const
Define default RooPrinable print options for given Print() flag string For inline printing only show ...
void setAttribAll(const Text_t *name, Bool_t value=kTRUE)
Set given attribute in each element of the collection by calling each elements setAttribute() functio...
RooAbsCollection * snapshot(Bool_t deepCopy=kTRUE) const
Take a snap shot of current collection contents.
std::string contentsString() const
Return comma separated list of contained object names as STL string.
TNamed * _typedStructureTag
Structure tag.
void clear()
Clear contents. If the collection is owning, it will also delete the contents.
RooAbsArg * first() const
virtual void RecursiveRemove(TObject *obj)
If one of the TObject we have a referenced to is deleted, remove the reference.
static void indent(ostringstream &buf, int indent_level)
const Bool_t kFALSE
Definition: RtypesCore.h:90
RooLinkedList is an collection class for internal use, storing a collection of RooAbsArg pointers in ...
Definition: RooLinkedList.h:35
static std::ostream & defaultPrintStream(std::ostream *os=0)
Return a reference to the current default stream to use in Print().
virtual void printClassName(std::ostream &os) const
Return collection class name.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsCollection & assignValueOnly(const RooAbsCollection &other, Bool_t oneSafe=kFALSE)
The assignment operator sets the value of any argument in our set that also appears in the other set...
RooFIter fwdIterator() const
One-time forward iterator.
RooAbsArg * operator[](Storage_t::size_type i) const
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Implement multiline printing of collection, one line for each contained object showing the requested ...
virtual StyleOption defaultPrintStyle(Option_t *opt) const
void forward(const LAYERDATA &prevLayerData, LAYERDATA &currLayerData)
apply the weights (and functions) in forward direction of the DNN
Definition: NeuralNet.icc:546
Storage_t::const_iterator const_iterator
Storage_t::const_reverse_iterator rend() const
const char * GetName() const
Returns name of object.
Mother of all ROOT objects.
Definition: TObject.h:37
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
Bool_t containsInstance(const RooAbsArg &var) const
bool empty() const
virtual void printTitle(std::ostream &os) const
Return collection title.
RooLinkedListIter iterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
void setName(const char *name)
Bool_t addServerClonesToList(const RooAbsArg &var)
Add clones of servers of given argument to end of list.
char Text_t
Definition: RtypesCore.h:60
RooAbsCollection * selectByName(const char *nameList, Bool_t verbose=kFALSE) const
Create a subset of the current collection, consisting only of those elements with names matching the ...
Bool_t contains(const RooAbsArg &var) const
RooAbsCollection & operator=(const RooAbsCollection &other)
The assignment operator sets the value of any argument in our set that also appears in the other set...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
static void output(int code)
Definition: gifencode.c:226
const Bool_t kTRUE
Definition: RtypesCore.h:89
Int_t getHashTableSize() const
A wrapper around TIterator derivatives.
char name[80]
Definition: TGX11.cxx:109
virtual TObject * create(const char *newname) const =0
void dump() const
Base contents dumper for debugging purposes.
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooCmdArg is a named container for two doubles, two integers two object points and three string point...
Definition: RooCmdArg.h:28
const char * Data() const
Definition: TString.h:364