Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
22RooNameSet is a utility class that stores the names the objects
23in a RooArget. This allows to preserve the contents of a RooArgSet
24in a specific use contents beyond the lifespan of the object in
25the RooArgSet. A new RooArgSet can be created from a RooNameSet
26by 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
46void 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
59RooNameSet::RooNameSet() : _len(0), _nameList(0)
60{
61}
62
63////////////////////////////////////////////////////////////////////////////////
64/// Construct from RooArgSet
65
66RooNameSet::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{
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
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
143void RooNameSet::setNameList(const char* givenList)
144{
145 strdup(_len, _nameList, givenList);
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Refill internal contents from names in given argSet
150
151void 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{
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
210void RooNameSet::printName(std::ostream& os) const
211{
212 os << GetName();
213}
214
215////////////////////////////////////////////////////////////////////////////////
216/// Print title of nameset
217
218void RooNameSet::printTitle(std::ostream& os) const
219{
220 os << GetTitle();
221}
222
223////////////////////////////////////////////////////////////////////////////////
224/// Print class name of nameset
225
226void 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
234void RooNameSet::printValue(std::ostream& os) const
235{
236 os << content();
237}
const Bool_t kFALSE
Definition RtypesCore.h:101
const Bool_t kTRUE
Definition RtypesCore.h:100
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:69
Int_t getSize() const
void sort(Bool_t reverse=false)
Sort collection using std::sort and name comparison.
RooFIter fwdIterator() const
One-time forward iterator.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:35
A one-time forward iterator working on RooLinkedList or RooAbsCollection.
RooNameSet is a utility class that stores the names the objects in a RooArget.
Definition RooNameSet.h:26
virtual void printValue(std::ostream &os) const
Print value of nameset, i.e the list of names.
RooArgSet * select(const RooArgSet &list) const
Construct a RooArgSet of objects in input 'list' whose names match to those in the internal name list...
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
virtual void printTitle(std::ostream &os) const
Print title of nameset.
Bool_t operator<(const RooNameSet &other) const
void setNameList(const char *givenList)
void refill(const RooArgSet &argSet)
Refill internal contents from names in given argSet.
virtual void printName(std::ostream &os) const
Print name of nameset.
RooNameSet()
Default constructor.
const char * content() const
Definition RooNameSet.h:52
void extendBuffer(Int_t inc)
virtual void printClassName(std::ostream &os) const
Print class name of nameset.
Int_t _len
Definition RooNameSet.h:55
char * _nameList
Definition RooNameSet.h:56
RooNameSet & operator=(const RooNameSet &)
Assignment operator.
Bool_t operator==(const RooNameSet &other) const
Comparison operator.
virtual ~RooNameSet()
Destructor.
RooPlotable is a 'mix-in' base class that define the standard RooFit plotting and printing methods.
virtual const char * GetName() const
Returns name of object.
Definition TNamed.h:47
Mother of all ROOT objects.
Definition TObject.h:41
virtual const char * GetName() const
Returns name of object.
Definition TObject.cxx:429
virtual const char * GetTitle() const
Returns title of object.
Definition TObject.cxx:473
static void output(int code)
Definition gifencode.c:226