Logo ROOT   6.14/05
Reference Guide
RooArgList.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 // RooArgList is a container object that can hold multiple RooAbsArg objects.
20 // The container has list semantics which means that:
21 //
22 // - Contained objects are ordered, The iterator
23 // follows the object insertion order.
24 //
25 // - Objects can be retrieved by name and index
26 //
27 // - Multiple objects with the same name are allowed
28 //
29 // Ownership of contents.
30 //
31 // Unowned objects are inserted with the add() method. Owned objects
32 // are added with addOwned() or addClone(). A RooArgSet either owns all
33 // of it contents, or none, which is determined by the first <add>
34 // call. Once an ownership status is selected, inappropriate <add> calls
35 // will return error status. Clearing the list via removeAll() resets the
36 // ownership status. Arguments supplied in the constructor are always added
37 // as unowned elements.
38 //
39 //
40 
41 #include "Riostream.h"
42 #include <iomanip>
43 #include "TClass.h"
44 #include "RooArgList.h"
45 #include "RooErrorHandler.h"
46 #include "RooStreamParser.h"
47 #include "RooFormula.h"
48 #include "RooAbsRealLValue.h"
49 #include "RooAbsCategoryLValue.h"
50 #include "RooStringVar.h"
51 #include "RooTrace.h"
52 #include "RooMsgService.h"
53 
54 using namespace std;
55 
57  ;
58 
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Default constructor
62 
65 {
67 }
68 
69 
70 
71 ////////////////////////////////////////////////////////////////////////////////
72 /// Constructor from a RooArgSet.
73 
76 {
77  add(set) ;
79 }
80 
81 
82 
83 ////////////////////////////////////////////////////////////////////////////////
84 /// Empty list constructor
85 
87  RooAbsCollection(name)
88 {
90 }
91 
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Constructor for list containing 1 initial object
96 
98  const char *name) :
99  RooAbsCollection(name)
100 {
101  add(var1);
103 }
104 
105 
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Constructor for set containing 2 initial objects
109 
110 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
111  const char *name) :
112  RooAbsCollection(name)
113 {
114  add(var1); add(var2);
116 }
117 
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Constructor for set containing 3 initial objects
122 
123 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
124  const RooAbsArg& var3,
125  const char *name) :
126  RooAbsCollection(name)
127 {
128  add(var1); add(var2); add(var3);
130 }
131 
132 
133 
134 ////////////////////////////////////////////////////////////////////////////////
135 /// Constructor for set containing 4 initial objects
136 
137 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
138  const RooAbsArg& var3, const RooAbsArg& var4,
139  const char *name) :
140  RooAbsCollection(name)
141 {
142  add(var1); add(var2); add(var3); add(var4);
144 }
145 
146 
147 
148 ////////////////////////////////////////////////////////////////////////////////
149 /// Constructor for set containing 5 initial objects
150 
152  const RooAbsArg& var2, const RooAbsArg& var3,
153  const RooAbsArg& var4, const RooAbsArg& var5,
154  const char *name) :
155  RooAbsCollection(name)
156 {
157  add(var1); add(var2); add(var3); add(var4); add(var5);
159 }
160 
161 
162 
163 ////////////////////////////////////////////////////////////////////////////////
164 /// Constructor for set containing 6 initial objects
165 
166 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
167  const RooAbsArg& var3, const RooAbsArg& var4,
168  const RooAbsArg& var5, const RooAbsArg& var6,
169  const char *name) :
170  RooAbsCollection(name)
171 {
172  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6);
174 }
175 
176 
177 
178 ////////////////////////////////////////////////////////////////////////////////
179 /// Constructor for set containing 7 initial objects
180 
181 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
182  const RooAbsArg& var3, const RooAbsArg& var4,
183  const RooAbsArg& var5, const RooAbsArg& var6,
184  const RooAbsArg& var7,
185  const char *name) :
186  RooAbsCollection(name)
187 {
188  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;
190 }
191 
192 
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Constructor for set containing 8 initial objects
196 
197 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
198  const RooAbsArg& var3, const RooAbsArg& var4,
199  const RooAbsArg& var5, const RooAbsArg& var6,
200  const RooAbsArg& var7, const RooAbsArg& var8,
201  const char *name) :
202  RooAbsCollection(name)
203 {
204  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;add(var8) ;
206 }
207 
208 
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Constructor for set containing 9 initial objects
212 
213 RooArgList::RooArgList(const RooAbsArg& var1, const RooAbsArg& var2,
214  const RooAbsArg& var3, const RooAbsArg& var4,
215  const RooAbsArg& var5, const RooAbsArg& var6,
216  const RooAbsArg& var7, const RooAbsArg& var8,
217  const RooAbsArg& var9, const char *name) :
218  RooAbsCollection(name)
219 {
220  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7); add(var8); add(var9);
222 }
223 
224 
225 
226 ////////////////////////////////////////////////////////////////////////////////
227 /// Constructor from a root TCollection. Elements in the collection that
228 /// do not inherit from RooAbsArg will be skipped. A warning message
229 /// will be printed for every skipped item.
230 
231 RooArgList::RooArgList(const TCollection& tcoll, const char* name) :
232  RooAbsCollection(name)
233 {
234  TIterator* iter = tcoll.MakeIterator() ;
235  TObject* obj ;
236  while((obj=iter->Next())) {
237  if (!dynamic_cast<RooAbsArg*>(obj)) {
238  coutW(InputArguments) << "RooArgList::RooArgList(TCollection) element " << obj->GetName()
239  << " is not a RooAbsArg, ignored" << endl ;
240  continue ;
241  }
242  add(*(RooAbsArg*)obj) ;
243  }
244  delete iter ;
246 }
247 
248 
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// Copy constructor. Note that a copy of a list is always non-owning,
252 /// even the source list is owning. To create an owning copy of
253 /// a list (owning or not), use the snaphot() method.
254 
255 RooArgList::RooArgList(const RooArgList& other, const char *name)
256  : RooAbsCollection(other,name)
257 {
259 }
260 
261 
262 
263 ////////////////////////////////////////////////////////////////////////////////
264 /// Destructor
265 
267 {
269 }
270 
271 
272 
273 ////////////////////////////////////////////////////////////////////////////////
274 /// Array operator. Element in slot 'idx' must already exist, otherwise
275 /// code will abort.
276 ///
277 /// When used as lvalue in assignment operations, the element contained in
278 /// the list will not be changed, only the value of the existing element!
279 
281 {
282  RooAbsArg* arg = at(idx) ;
283  if (!arg) {
284  coutE(InputArguments) << "RooArgList::operator[](" << GetName() << ") ERROR: index "
285  << idx << " out of range (0," << getSize() << ")" << endl ;
287  }
288  return *arg ;
289 }
290 
291 
292 
293 ////////////////////////////////////////////////////////////////////////////////
294 /// Write the contents of the argset in ASCII form to given stream.
295 ///
296 /// All elements will be printed on a single line separated by a single
297 /// white space. The contents of each element is written by the arguments'
298 /// writeToStream() function
299 
300 void RooArgList::writeToStream(ostream& os, Bool_t compact)
301 {
302  if (!compact) {
303  coutE(InputArguments) << "RooArgList::writeToStream(" << GetName() << ") non-compact mode not supported" << endl ;
304  return ;
305  }
306 
307  TIterator *iterat= createIterator();
308  RooAbsArg *next = 0;
309  while((0 != (next= (RooAbsArg*)iterat->Next()))) {
310  next->writeToStream(os,kTRUE) ;
311  os << " " ;
312  }
313  delete iterat;
314  os << endl ;
315 }
316 
317 
318 
319 ////////////////////////////////////////////////////////////////////////////////
320 /// Read the contents of the argset in ASCII form from given stream.
321 ///
322 /// A single line is read, and all elements are assumed to be separated
323 /// by white space. The value of each argument is read by the arguments
324 /// readFromStream function.
325 
326 Bool_t RooArgList::readFromStream(istream& is, Bool_t compact, Bool_t verbose)
327 {
328  if (!compact) {
329  coutE(InputArguments) << "RooArgList::readFromStream(" << GetName() << ") non-compact mode not supported" << endl ;
330  return kTRUE ;
331  }
332 
333  TIterator *iterat= createIterator();
334  RooStreamParser parser(is) ;
335  RooAbsArg *next = 0;
336  while((0 != (next= (RooAbsArg*)iterat->Next()))) {
337  if (!next->getAttribute("Dynamic")) {
338  if (next->readFromStream(is,kTRUE,verbose)) {
339  parser.zapToEnd() ;
340 
341  delete iterat ;
342  return kTRUE ;
343  }
344  } else {
345  }
346  }
347 
348  if (!parser.atEOL()) {
349  TString rest = parser.readLine() ;
350  if (verbose) {
351  coutW(InputArguments) << "RooArgSet::readFromStream(" << GetName()
352  << "): ignoring extra characters at end of line: '" << rest << "'" << endl ;
353  }
354  }
355 
356  delete iterat;
357  return kFALSE ;
358 }
359 
TIterator * createIterator(Bool_t dir=kIterForward) const
#define coutE(a)
Definition: RooMsgService.h:34
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
static void softAbort()
virtual ~RooArgList()
Destructor.
Definition: RooArgList.cxx:266
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read the contents of the argset in ASCII form from given stream.
Definition: RooArgList.cxx:326
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
#define coutW(a)
Definition: RooMsgService.h:33
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)=0
Iterator abstract base class.
Definition: TIterator.h:30
#define TRACE_CREATE
Definition: RooTrace.h:22
virtual void writeToStream(std::ostream &os, Bool_t compact)
Write the contents of the argset in ASCII form to given stream.
Definition: RooArgList.cxx:300
Bool_t getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
Definition: RooAbsArg.cxx:263
Int_t getSize() const
RooAbsArg & operator[](Int_t idx) const
Array operator.
Definition: RooArgList.cxx:280
RooAbsArg * at(Int_t idx) const
Definition: RooArgList.h:84
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const =0
Collection abstract base class.
Definition: TCollection.h:63
void zapToEnd(Bool_t inclContLines=kFALSE)
Eat all characters up to and including then end of the current line.
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of &#39;\&#39; in th...
virtual void writeToStream(std::ostream &os, Bool_t compact) const =0
const Bool_t kFALSE
Definition: RtypesCore.h:88
#define ClassImp(name)
Definition: Rtypes.h:359
#define TRACE_DESTROY
Definition: RooTrace.h:23
RooArgList()
Default constructor.
Definition: RooArgList.cxx:63
const char * GetName() const
Returns name of object.
Mother of all ROOT objects.
Definition: TObject.h:37
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects...
virtual TObject * Next()=0
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:357
Bool_t atEOL()
If true, parser is at end of line in stream.
const Bool_t kTRUE
Definition: RtypesCore.h:87
char name[80]
Definition: TGX11.cxx:109