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