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