Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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.
35  RooAbsCollection();
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="");
48  RooAbsCollection& operator=(const RooAbsCollection& other);
49  RooAbsCollection& assignValueOnly(const RooAbsCollection& other, Bool_t oneSafe=kFALSE) ;
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 ;
54  Bool_t snapshot(RooAbsCollection& output, Bool_t deepCopy=kTRUE) const ;
55 
56  /// \deprecated Without effect.
57  void setHashTableSize(Int_t) {
58  // Set size of internal hash table to i (should be a prime number)
59  }
60  /// \deprecated Without effect.
61  Int_t getHashTableSize() const {
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.
113  inline TIterator* createIterator(Bool_t dir = kIterForward) const
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.
121  RooLinkedListIter iterator(Bool_t dir = kIterForward) const
122  R__SUGGEST_ALTERNATIVE("begin(), end() and range-based for loops.") {
123  return RooLinkedListIter(makeLegacyIterator(dir));
124  }
125 
126  /// One-time forward iterator.
127  /// \note Use begin() and end() or range-based for loop instead.
128  RooFIter fwdIterator() const
129  R__SUGGEST_ALTERNATIVE("begin(), end() and range-based for loops.") {
130  return RooFIter(makeLegacyIterator());
131  }
132 
133  const_iterator begin() const {
134  return _list.begin();
135  }
136 
137  const_iterator end() const {
138  return _list.end();
139  }
140 
141  Storage_t::size_type size() const {
142  return _list.size();
143  }
144 
145  bool empty() const {
146  return _list.empty();
147  }
148 
149  void reserve(Storage_t::size_type count) {
150  _list.reserve(count);
151  }
152 
153  /// Clear contents. If the collection is owning, it will also delete the contents.
154  void clear() {
155  removeAll();
156  }
157 
158  inline Int_t getSize() const {
159  // Return the number of elements in the collection
160  return _list.size();
161  }
162 
163  inline RooAbsArg *first() const {
164  // Return the first element in this collection
165  return _list.front();
166  }
167 
168  RooAbsArg * operator[](Storage_t::size_type i) const {
169  return _list[i];
170  }
171 
172 
173  /// Returns index of given arg, or -1 if arg is not in the collection.
174  inline Int_t index(const RooAbsArg* arg) const {
175  auto item = std::find(_list.begin(), _list.end(), arg);
176  return item != _list.end() ? item - _list.begin() : -1;
177  }
178 
179  /// Returns index of given arg, or -1 if arg is not in the collection.
180  inline Int_t index(const RooAbsArg& arg) const {
181  return index(&arg);
182  }
183 
184  /// Returns index of arg with given name, or -1 if arg is not in the collection.
185  inline Int_t index(const char* name) const {
186  const std::string theName(name);
187  auto item = std::find_if(_list.begin(), _list.end(), [&theName](const RooAbsArg * elm){
188  return elm->GetName() == theName;
189  });
190  return item != _list.end() ? item - _list.begin() : -1;
191  }
192 
193  inline virtual void Print(Option_t *options= 0) const {
194  // Printing interface (human readable)
195  printStream(defaultPrintStream(),defaultPrintContents(options),defaultPrintStyle(options));
196  }
197  std::string contentsString() const ;
198 
199 
200  virtual void printName(std::ostream& os) const ;
201  virtual void printTitle(std::ostream& os) const ;
202  virtual void printClassName(std::ostream& os) const ;
203  virtual void printValue(std::ostream& os) const ;
204  virtual void printMultiline(std::ostream& os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const ;
205 
206  virtual Int_t defaultPrintContents(Option_t* opt) const ;
207 
208  // Latex printing methods
209  void printLatex(const RooCmdArg& arg1=RooCmdArg(), const RooCmdArg& arg2=RooCmdArg(),
210  const RooCmdArg& arg3=RooCmdArg(), const RooCmdArg& arg4=RooCmdArg(),
211  const RooCmdArg& arg5=RooCmdArg(), const RooCmdArg& arg6=RooCmdArg(),
212  const RooCmdArg& arg7=RooCmdArg(), const RooCmdArg& arg8=RooCmdArg()) const ;
213  void printLatex(std::ostream& ofs, Int_t ncol, const char* option="NEYU", Int_t sigDigit=1,
214  const RooLinkedList& siblingLists=RooLinkedList(), const RooCmdArg* formatCmd=0) const ;
215 
216  void setName(const char *name) {
217  // Set name of collection
218  _name= name;
219  }
220  const char* GetName() const {
221  // Return namer of collection
222  return _name.Data() ;
223  }
224  Bool_t isOwning() const {
225  // Does collection own contents?
226  return _ownCont ;
227  }
228 
229  Bool_t allInRange(const char* rangeSpec) const ;
230 
231  void dump() const ;
232 
233  void releaseOwnership() { _ownCont = kFALSE ; }
234  void takeOwnership() { _ownCont = kTRUE ; }
235 
236  void sort(Bool_t reverse = false);
237 
238  virtual void RecursiveRemove(TObject *obj);
239 
240 protected:
241 
242  friend class RooMultiCatIter ;
243 
244  Storage_t _list; // Actual object storage
245  using LegacyIterator_t = TIteratorToSTLInterface<Storage_t>;
246 
247  Bool_t _ownCont; // Flag to identify a list that owns its contents.
248  TString _name; // Our name.
249  Bool_t _allRRV ; // All contents are RRV
250 
251  void safeDeleteList() ;
252 
253  // Support for snapshot method
254  Bool_t addServerClonesToList(const RooAbsArg& var) ;
255 
256  inline TNamed* structureTag() { if (_structureTag==0) makeStructureTag() ; return _structureTag ; }
257  inline TNamed* typedStructureTag() { if (_typedStructureTag==0) makeTypedStructureTag() ; return _typedStructureTag ; }
258 
259  mutable TNamed* _structureTag{nullptr}; //! Structure tag
260  mutable TNamed* _typedStructureTag{nullptr}; //! Typed structure tag
261 
262  inline void clearStructureTags() { _structureTag=0 ; _typedStructureTag = 0 ; }
263 
264  void makeStructureTag() ;
265  void makeTypedStructureTag() ;
266 
267 private:
268  std::unique_ptr<LegacyIterator_t> makeLegacyIterator (bool forward = true) const;
269 
270  ClassDef(RooAbsCollection,3) // Collection of RooAbsArg objects
271 };
272 
273 #endif