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