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