Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooLinkedList.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
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
17/**
18\file RooLinkedList.cxx
19\class RooLinkedList
20\ingroup Roofitcore
21
22Collection class for internal use, storing a collection of TObject pointers
23in a vector. It can optionally add a hash table to speed up lookup by name in
24large collections.
25Use RooAbsCollection derived objects for public use
26(e.g. RooArgSet or RooArgList)
27**/
28
29#include "RooLinkedList.h"
30
31#include "RooLinkedListIter.h"
32#include "RooAbsArg.h"
33#include "RooAbsData.h"
34#include "RooMsgService.h"
35
36#include "TBuffer.h"
37#include "TROOT.h"
38
39#include <algorithm>
40#include <cstring>
41#include <iostream>
42
43namespace {
44
45// The name pointer of a RooAbsArg or RooAbsData, or nullptr for other objects.
46const TNamed *namePtr(TObject const *obj)
47{
48 if (auto arg = dynamic_cast<RooAbsArg const *>(obj))
49 return arg->namePtr();
50 if (auto data = dynamic_cast<RooAbsData const *>(obj))
51 return data->namePtr();
52 return nullptr;
53}
54
55} // namespace
56
57////////////////////////////////////////////////////////////////////////////////
58/// Copy constructor
59
61 : TObject(other), _hashThresh(other._hashThresh), _name(other._name), _useNptr(other._useNptr)
62{
63 if (other._htableName)
64 _htableName = std::make_unique<HashTableByName>();
65 for (TObject *obj : other._list)
66 Add(obj);
67}
68
69////////////////////////////////////////////////////////////////////////////////
70/// Assignment operator, copy contents from 'other'
71
73{
74 // Prevent self-assignment
75 if (&other==this) return *this ;
76
77 // remove old elements
78 Clear();
79 // Copy elements
80 for (TObject *obj : other._list)
81 Add(obj);
82
83 return *this ;
84}
85
86////////////////////////////////////////////////////////////////////////////////
87/// Change the threshold for hash-table use to given size.
88/// If a hash table exists when this method is called, it is regenerated.
89
91{
92 if (size < 0) {
93 coutE(InputArguments) << "RooLinkedList::setHashTable() ERROR size must be positive" << std::endl;
94 return;
95 }
96 if (size == 0) {
97 // Remove existing hash table
98 _htableName.reset();
99 return;
100 }
101
102 if (!_htableName) {
103 // (Re)create hash table
104 _htableName = std::make_unique<HashTableByName>(size);
105 for (TObject *obj : _list)
106 _htableName->insert({obj->GetName(), obj});
107 }
108
109 _htableName->reserve(size);
110}
111
112////////////////////////////////////////////////////////////////////////////////
113/// Destructor
114
116{
117 // Required since we overload TObject::Hash.
119}
120
121////////////////////////////////////////////////////////////////////////////////
122/// Insert object into collection
123
125{
126 if (!arg)
127 return;
128
129 // Only use RooAbsArg::namePtr() in lookup-by-name if all elements have it
130 if (!dynamic_cast<RooAbsArg *>(arg) && !dynamic_cast<RooAbsData *>(arg))
131 _useNptr = false;
132
133 if (!_htableName && _hashThresh > 0 && GetSize() > _hashThresh)
135
136 _list.push_back(arg);
137 if (_htableName)
138 _htableName->insert({arg->GetName(), arg});
139}
140
141////////////////////////////////////////////////////////////////////////////////
142/// Remove object from collection
143
145{
146 auto found = std::find(_list.begin(), _list.end(), arg);
147 if (found == _list.end())
148 return false;
149
150 if (_htableName)
151 _htableName->erase(arg->GetName());
152 _list.erase(found);
153 return true;
154}
155
156////////////////////////////////////////////////////////////////////////////////
157/// If one of the TObject we have a referenced to is deleted, remove the
158/// reference.
159
161{
162 Remove(obj); // This is a nop if the obj is not in the collection.
163}
164
165////////////////////////////////////////////////////////////////////////////////
166/// Return object stored in sequential position given by index.
167/// If index is out of range, a null pointer is returned.
168
170{
171 return index < 0 || index >= GetSize() ? nullptr : _list[index];
172}
173
174////////////////////////////////////////////////////////////////////////////////
175/// Replace object 'oldArg' in collection with new object 'newArg'.
176/// If 'oldArg' is not found in collection false is returned
177
178bool RooLinkedList::Replace(const TObject *oldArg, const TObject *newArg)
179{
180 auto found = std::find(_list.begin(), _list.end(), oldArg);
181 if (found == _list.end())
182 return false;
183
184 if (_htableName) {
185 _htableName->erase(oldArg->GetName());
186 _htableName->insert({newArg->GetName(), newArg});
187 }
188 *found = const_cast<TObject *>(newArg);
189 return true;
190}
191
192////////////////////////////////////////////////////////////////////////////////
193/// Return pointer to object with given name. If no such object
194/// is found return a null pointer.
195
197{
198 return find(name) ;
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Find object in list. If list contains object return
203/// (same) pointer to object, otherwise return null pointer
204
206{
207 return std::find(_list.begin(), _list.end(), obj) != _list.end() ? const_cast<TObject *>(obj) : nullptr;
208}
209
210////////////////////////////////////////////////////////////////////////////////
211/// Remove all elements from collection
212
214{
215 _list.clear();
216 if (_htableName)
217 _htableName->clear();
218}
219
220////////////////////////////////////////////////////////////////////////////////
221/// Remove all elements in collection and delete all elements
222/// NB: Collection does not own elements, this function should
223/// be used judiciously by caller.
224
226{
227 std::vector<TObject *> objects = std::move(_list);
228 Clear();
229 for (TObject *obj : objects)
230 delete obj;
231}
232
233////////////////////////////////////////////////////////////////////////////////
234/// Return pointer to object with given name in collection.
235/// If no such object is found, return null pointer.
236
238{
239 auto findByNamePtr = [&](const TNamed *nptr) -> TObject * {
240 auto found = std::find_if(_list.begin(), _list.end(), [&](TObject *obj) { return namePtr(obj) == nptr; });
241 return found != _list.end() ? *found : nullptr;
242 };
243
244 if (_htableName) {
245 auto found = _htableName->find(name);
246 if (found != _htableName->end())
247 return const_cast<TObject *>(found->second);
248 // The hash table misses elements that were renamed to 'name' after they were added
249 if (_useNptr) {
250 const TNamed *nptr = RooNameReg::known(name);
251 return nptr && nptr->TestBit(RooNameReg::kRenamedArg) ? findByNamePtr(nptr) : nullptr;
252 }
253 }
254
255 // The penalty for RooNameReg lookup seems to be outweighted by the faster search
256 // when the size list is longer than ~7, but let's be a bit conservative.
257 if (_useNptr && GetSize() > 9) {
258 const TNamed *nptr = RooNameReg::known(name);
259 return nptr ? findByNamePtr(nptr) : nullptr;
260 }
261
262 auto found = std::find_if(_list.begin(), _list.end(), [&](TObject *obj) { return !strcmp(obj->GetName(), name); });
263 return found != _list.end() ? *found : nullptr;
264}
265
266////////////////////////////////////////////////////////////////////////////////
267/// Return pointer to object with given name in collection.
268/// If no such object is found, return null pointer.
269
271{
272 if (_htableName) {
273 auto found = _htableName->find(arg->GetName());
274 if (found != _htableName->end())
275 return static_cast<RooAbsArg *>(const_cast<TObject *>(found->second));
276 // See if it might have been renamed
278 return nullptr;
279 }
280
281 for (TObject *obj : _list) {
282 if (static_cast<RooAbsArg *>(obj)->namePtr() == arg->namePtr())
283 return static_cast<RooAbsArg *>(obj);
284 }
285 return nullptr;
286}
287
288////////////////////////////////////////////////////////////////////////////////
289/// Return position of given object in list. If object
290/// is not contained in list, return -1
291
293{
294 auto found = std::find(_list.begin(), _list.end(), arg);
295 return found != _list.end() ? found - _list.begin() : -1;
296}
297
298////////////////////////////////////////////////////////////////////////////////
299/// Return position of given object in list. If object
300/// is not contained in list, return -1
301
303{
304 auto found = std::find_if(_list.begin(), _list.end(), [&](TObject *obj) { return !strcmp(obj->GetName(), name); });
305 return found != _list.end() ? found - _list.begin() : -1;
306}
307
308////////////////////////////////////////////////////////////////////////////////
309/// Print contents of list, defers to Print() function
310/// of contained objects
311
312void RooLinkedList::Print(const char *opt) const
313{
314 for (TObject *obj : _list) {
315 std::cout << obj << " : ";
316 obj->Print(opt);
317 }
318}
319
320////////////////////////////////////////////////////////////////////////////////
321/// Create a TIterator for this list.
322/// \param forward Run in forward direction (default).
323/// \return Pointer to a TIterator. The caller owns the pointer.
324
326 auto iterImpl = std::make_unique<RooLinkedListIterImpl>(this, forward);
327 return new RooLinkedListIter(std::move(iterImpl));
328}
329
330////////////////////////////////////////////////////////////////////////////////
331/// Create an iterator for this list.
332/// \param forward Run in forward direction (default).
333/// \return RooLinkedListIter (subclass of TIterator) over this list
334
336 auto iterImpl = std::make_unique<RooLinkedListIterImpl>(this, forward);
337 return RooLinkedListIter(std::move(iterImpl));
338}
339
340////////////////////////////////////////////////////////////////////////////////
341/// Create a one-time-use forward iterator for this list.
342/// \return RooFIter that only supports next()
343
345 auto iterImpl = std::make_unique<RooFIterForLinkedList>(this);
346 return RooFIter(std::move(iterImpl));
347}
348
350 return {this, true};
351}
352
354 return {this, -1, true};
355}
356
358 return {this, false};
359}
360
362 return {this, -1, false};
363}
364
365////////////////////////////////////////////////////////////////////////////////
366/// Sort the list with TObject::Compare(), keeping the order of equal elements.
367
368void RooLinkedList::Sort(bool ascend)
369{
370 std::stable_sort(_list.begin(), _list.end(),
371 [ascend](TObject *a, TObject *b) { return ascend ? a->Compare(b) < 0 : b->Compare(a) < 0; });
372}
373
374////////////////////////////////////////////////////////////////////////////////
375/// Custom streaming handling schema evolution w.r.t past implementations
376
378{
379 if (R__b.IsReading()) {
380
381 Version_t v = R__b.ReadVersion();
382 //R__b.ReadVersion();
383 TObject::Streamer(R__b);
384
385 Int_t size ;
386 TObject* arg ;
387
388 R__b >> size ;
389 while(size--) {
390 R__b >> arg ;
391 Add(arg) ;
392 }
393
394 if (v > 1 && v < 4) {
395 R__b >> _name;
396 }
397
398 } else {
400 TObject::Streamer(R__b);
401 R__b << GetSize() ;
402
403 for (TObject *arg : _list) {
404 R__b << arg ;
405 }
406
407 R__b << _name ;
408 }
409}
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define coutE(a)
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
const char Option_t
Option string (const char)
Definition RtypesCore.h:81
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
char name[80]
Definition TGX11.cxx:142
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
const TNamed * namePtr() const
De-duplicated pointer to this object's name.
Definition RooAbsArg.h:484
Abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:55
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
Implementation of the actual iterator on RooLinkedLists.
A wrapper around TIterator derivatives.
Collection class for internal use, storing a collection of TObject pointers in a vector.
Int_t GetSize() const
RooLinkedListIterImpl rend() const
TObject * At(int index) const
Return object stored in sequential position given by index.
RooLinkedListIter iterator(bool forward=true) const
Create an iterator for this list.
std::vector< TObject * > _list
! The stored objects
~RooLinkedList() override
Destructor.
RooLinkedListIterImpl end() const
std::unique_ptr< HashTableByName > _htableName
! Hash table by name
void RecursiveRemove(TObject *obj) override
If one of the TObject we have a referenced to is deleted, remove the reference.
bool Replace(const TObject *oldArg, const TObject *newArg)
Replace object 'oldArg' in collection with new object 'newArg'.
RooLinkedList(Int_t htsize=0)
void Print(const char *opt) const override
Print contents of list, defers to Print() function of contained objects.
RooFIter fwdIterator() const
Create a one-time-use forward iterator for this list.
void Streamer(TBuffer &) override
Custom streaming handling schema evolution w.r.t past implementations.
RooLinkedListIterImpl rbegin() const
std::size_t size() const
TClass * IsA() const override
virtual void Add(TObject *arg)
Insert object into collection.
Int_t _hashThresh
Size threshold for hashing.
RooAbsArg * findArg(const RooAbsArg *) const
Return pointer to object with given name in collection.
void Delete(Option_t *o=nullptr) override
Remove all elements in collection and delete all elements NB: Collection does not own elements,...
TObject * find(const char *name) const
Return pointer to object with given name in collection.
RooLinkedList & operator=(const RooLinkedList &other)
Assignment operator, copy contents from 'other'.
RooLinkedListIterImpl begin() const
void setHashTableSize(Int_t size)
Change the threshold for hash-table use to given size.
TObject * FindObject(const char *name) const override
Return pointer to object with given name.
TIterator * MakeIterator(bool forward=true) const
Create a TIterator for this list.
void Clear(Option_t *o=nullptr) override
Remove all elements from collection.
void Sort(bool ascend=true)
Sort the list with TObject::Compare(), keeping the order of equal elements.
Int_t IndexOf(const char *name) const
Return position of given object in list.
virtual bool Remove(TObject *arg)
Remove object from collection.
static const TNamed * known(const char *inStr)
If the name is already known, return its TNamed pointer. Otherwise return 0 (don't register the name)...
@ kRenamedArg
TNamed flag to indicate that some RooAbsArg has been renamed (flag set in new name)
Definition RooNameReg.h:46
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
Iterator abstract base class.
Definition TIterator.h:30
The TNamed class is the base class for all named ROOT classes.
Definition TNamed.h:29
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Mother of all ROOT objects.
Definition TObject.h:42
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:461
R__ALWAYS_INLINE Bool_t TestBit(UInt_t f) const
Definition TObject.h:204
virtual void Streamer(TBuffer &)
Stream an object of class TObject.
Definition TObject.cxx:995
virtual void Print(Option_t *option="") const
This method must be overridden when a class wants to print itself.
Definition TObject.cxx:660
void CallRecursiveRemoveIfNeeded(TObject &obj)
call RecursiveRemove for obj if gROOT is valid and obj.TestBit(kMustCleanup) is true.
Definition TROOT.h:406