Logo ROOT   6.21/01
Reference Guide
RooNameSet.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 RooNameSet.cxx
19 \class RooNameSet
20 \ingroup Roofitcore
21 
22 RooNameSet is a utility class that stores the names the objects
23 in a RooArget. This allows to preserve the contents of a RooArgSet
24 in a specific use contents beyond the lifespan of the object in
25 the RooArgSet. A new RooArgSet can be created from a RooNameSet
26 by offering it a list of new RooAbsArg objects.
27 **/
28 
29 #include <cstring>
30 
31 #include "RooFit.h"
32 #include "Riostream.h"
33 
34 #include "TObjString.h"
35 #include "TClass.h"
36 #include "RooNameSet.h"
37 #include "RooArgSet.h"
38 #include "RooArgList.h"
39 
41 ;
42 
43 ////////////////////////////////////////////////////////////////////////////////
44 /// copy src to dst, keep dstlen up to date, make sure zero length strings
45 /// do not take memory
46 
47 void RooNameSet::strdup(Int_t& dstlen, char* &dstbuf, const char* src)
48 {
49  dstlen = src ? std::strlen(src) : 0;
50  if (dstlen) ++dstlen;
51  char *buf = dstlen ? new char[dstlen] : 0;
52  if (buf) std::strcpy(buf, src);
53  delete[] dstbuf;
54  dstbuf = buf;
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// Default constructor
59 
60 RooNameSet::RooNameSet() : _len(0), _nameList(0)
61 {
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Construct from RooArgSet
66 
67 RooNameSet::RooNameSet(const RooArgSet& argSet) : _len(0), _nameList(0)
68 {
69  refill(argSet);
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Copy constructor
74 
76  TObject(other), RooPrintable(other), _len(0), _nameList(0)
77 {
78  strdup(_len, _nameList, other._nameList);
79 }
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Destructor
83 
85 {
86  delete[] _nameList;
87 }
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Assignment operator
91 
93 {
94  // Check comparison against self
95  if (&other == this || _nameList == other._nameList) return *this;
96 
97  strdup(_len, _nameList, other._nameList);
98 
99  return *this;
100 }
101 
102 ////////////////////////////////////////////////////////////////////////////////
103 /// Comparison operator
104 
106 {
107  // Check comparison against self
108  if (&other == this || _nameList == other._nameList) return kTRUE;
109 
110  return _nameList && other._nameList &&
111  0 == std::strcmp(_nameList, other._nameList);
112 }
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 
117 {
118  if (&other == this) return kFALSE;
119  if (!_nameList) return other._nameList;
120  if (!other._nameList) return kFALSE;
121  return std::strcmp(_nameList, other._nameList) < 0;
122 }
123 
124 ////////////////////////////////////////////////////////////////////////////////
125 
127 {
128  if (!inc) return;
129  assert(inc > 0 || _len >= -inc);
130  int newsz = _len + inc;
131  if (newsz <= 1 || !_len) newsz = 0;
132  char* newbuf = newsz ? new char[newsz] : 0;
133  if (newbuf && _nameList) {
134  std::strncpy(newbuf, _nameList, std::min(_len, newsz));
135  newbuf[newsz - 1] = 0;
136  }
137  delete[] _nameList;
138  _nameList = newbuf;
139  _len = newsz;
140 }
141 
142 ////////////////////////////////////////////////////////////////////////////////
143 
144 void RooNameSet::setNameList(const char* givenList)
145 {
146  strdup(_len, _nameList, givenList);
147 }
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Refill internal contents from names in given argSet
151 
152 void RooNameSet::refill(const RooArgSet& argSet)
153 {
154  delete[] _nameList;
155  _nameList = 0;
156  _len = 0;
157  if (0 == argSet.getSize()) return;
158 
159  RooArgList tmp(argSet);
160  tmp.sort();
161  // figure out the length of the array we need
162  RooAbsArg* arg = 0;
163  for (RooFIter it = tmp.fwdIterator(); 0 != (arg = it.next());
164  _len += 1 + std::strlen(arg->GetName())) { }
165  if (_len <= 1) _len = 0;
166  // allocate it
167  _nameList = _len ? new char[_len] : 0;
168  if (_nameList) {
169  // copy in the names of the objects
170  char *p = _nameList;
171  for (RooFIter it = tmp.fwdIterator(); 0 != (arg = it.next()); ) {
172  const char *name = arg->GetName();
173  std::strcpy(p, name);
174  while (*p) ++p;
175  *p++ = ':';
176  }
177  // zero-terminate properly
178  *--p = 0;
179  }
180 }
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Construct a RooArgSet of objects in input 'list'
184 /// whose names match to those in the internal name
185 /// list of RooNameSet
186 
188 {
189  RooArgSet* output = new RooArgSet;
190  if (!_nameList || !std::strlen(_nameList)) return output;
191 
192  // need to copy _nameList because std::strtok modifies the string
193  char* tmp = 0;
194  int dummy = 0;
195  strdup(dummy, tmp, _nameList);
196 
197  char* token = std::strtok(tmp, ":");
198  while (token) {
199  RooAbsArg* arg = list.find(token);
200  if (arg) output->add(*arg);
201  token = std::strtok(0, ":");
202  }
203  delete[] tmp;
204 
205  return output;
206 }
207 
208 ////////////////////////////////////////////////////////////////////////////////
209 /// Print name of nameset
210 
211 void RooNameSet::printName(std::ostream& os) const
212 {
213  os << GetName();
214 }
215 
216 ////////////////////////////////////////////////////////////////////////////////
217 /// Print title of nameset
218 
219 void RooNameSet::printTitle(std::ostream& os) const
220 {
221  os << GetTitle();
222 }
223 
224 ////////////////////////////////////////////////////////////////////////////////
225 /// Print class name of nameset
226 
227 void RooNameSet::printClassName(std::ostream& os) const
228 {
229  os << IsA()->GetName();
230 }
231 
232 ////////////////////////////////////////////////////////////////////////////////
233 /// Print value of nameset, i.e the list of names
234 
235 void RooNameSet::printValue(std::ostream& os) const
236 {
237  os << content();
238 }
static void strdup(Int_t &dstlen, char *&dstbuf, const char *str)
copy src to dst, keep dstlen up to date, make sure zero length strings do not take memory ...
Definition: RooNameSet.cxx:47
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
virtual void printName(std::ostream &os) const
Print name of nameset.
Definition: RooNameSet.cxx:211
RooNameSet()
Default constructor.
Definition: RooNameSet.cxx:60
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
char * _nameList
Definition: RooNameSet.h:54
virtual void printTitle(std::ostream &os) const
Print title of nameset.
Definition: RooNameSet.cxx:219
virtual void printValue(std::ostream &os) const
Print value of nameset, i.e the list of names.
Definition: RooNameSet.cxx:235
void setNameList(const char *givenList)
Definition: RooNameSet.cxx:144
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const char * content() const
Definition: RooNameSet.h:50
RooNameSet & operator=(const RooNameSet &)
Assignment operator.
Definition: RooNameSet.cxx:92
RooPlotable is a &#39;mix-in&#39; base class that define the standard RooFit plotting and printing methods...
Definition: RooPrintable.h:25
virtual void printClassName(std::ostream &os) const
Print class name of nameset.
Definition: RooNameSet.cxx:227
void sort(Bool_t reverse=false)
Sort collection using std::sort and name comparison.
void extendBuffer(Int_t inc)
Definition: RooNameSet.cxx:126
virtual ~RooNameSet()
Destructor.
Definition: RooNameSet.cxx:84
RooNameSet is a utility class that stores the names the objects in a RooArget.
Definition: RooNameSet.h:24
Int_t getSize() const
Int_t _len
Definition: RooNameSet.h:53
Bool_t operator<(const RooNameSet &other) const
Definition: RooNameSet.cxx:116
const Bool_t kFALSE
Definition: RtypesCore.h:88
void refill(const RooArgSet &argSet)
Refill internal contents from names in given argSet.
Definition: RooNameSet.cxx:152
#define ClassImp(name)
Definition: Rtypes.h:365
RooAbsArg * find(const char *name) const
Find object with given name in list.
static RooMathCoreReg dummy
RooArgSet * select(const RooArgSet &list) const
Construct a RooArgSet of objects in input &#39;list&#39; whose names match to those in the internal name list...
Definition: RooNameSet.cxx:187
Bool_t operator==(const RooNameSet &other) const
Comparison operator.
Definition: RooNameSet.cxx:105
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetTitle() const
Returns title of object.
Definition: TObject.cxx:401
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:71
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
static void output(int code)
Definition: gifencode.c:226
RooFIter fwdIterator() const R__SUGGEST_ALTERNATIVE("begin()
One-time forward iterator.
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109
A one-time forward iterator working on RooLinkedList or RooAbsCollection.