Logo ROOT  
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 /// \class RooArgList
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 "RooArgList.h"
42 
43 #include "RooStreamParser.h"
44 #include "RooAbsRealLValue.h"
45 #include "RooAbsCategoryLValue.h"
46 #include "RooTrace.h"
47 #include "RooMsgService.h"
48 
49 #include <stdexcept>
50 
51 using namespace std;
52 
54 
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Default constructor
58 
61 {
63 }
64 
65 
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Constructor from a RooArgSet.
69 
71  RooAbsCollection(set.GetName())
72 {
73  add(set) ;
75 }
76 
77 
78 
79 ////////////////////////////////////////////////////////////////////////////////
80 /// Empty list constructor
81 
84 {
86 }
87 
88 
89 ////////////////////////////////////////////////////////////////////////////////
90 /// Constructor from a root TCollection. Elements in the collection that
91 /// do not inherit from RooAbsArg will be skipped. A warning message
92 /// will be printed for every skipped item.
93 
94 RooArgList::RooArgList(const TCollection& tcoll, const char* name) :
96 {
97  TIterator* iter = tcoll.MakeIterator() ;
98  TObject* obj ;
99  while((obj=iter->Next())) {
100  if (!dynamic_cast<RooAbsArg*>(obj)) {
101  coutW(InputArguments) << "RooArgList::RooArgList(TCollection) element " << obj->GetName()
102  << " is not a RooAbsArg, ignored" << endl ;
103  continue ;
104  }
105  add(*(RooAbsArg*)obj) ;
106  }
107  delete iter ;
109 }
110 
111 
112 
113 ////////////////////////////////////////////////////////////////////////////////
114 /// Copy constructor. Note that a copy of a list is always non-owning,
115 /// even the source list is owning. To create an owning copy of
116 /// a list (owning or not), use the snaphot() method.
117 
118 RooArgList::RooArgList(const RooArgList& other, const char *name)
119  : RooAbsCollection(other,name)
120 {
122 }
123 
124 
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// Destructor
128 
130 {
132 }
133 
134 
135 
136 ////////////////////////////////////////////////////////////////////////////////
137 /// Array operator. Element in slot `idx` must exist.
138 /// \throws std::invalid_argument if `idx` is out of range.
139 ///
140 /// When used as
141 /// ```
142 /// myArgList[4] = x;
143 /// ```
144 /// note that the element contained in the list will not be
145 /// replaced! Instead, `operator=` of the existing element is called.
147 {
148  RooAbsArg* arg = at(idx) ;
149  if (!arg) {
150  coutE(InputArguments) << "RooArgList::operator[](" << GetName() << ") ERROR: index "
151  << idx << " out of range (0," << getSize() << ")" << endl ;
152  throw std::invalid_argument(std::string("Index ") + to_string(idx) + " is out of range.");
153  }
154  return *arg ;
155 }
156 
157 
158 
159 ////////////////////////////////////////////////////////////////////////////////
160 /// Write the contents of the argset in ASCII form to given stream.
161 ///
162 /// All elements will be printed on a single line separated by a single
163 /// white space. The contents of each element is written by the arguments'
164 /// writeToStream() function
165 
166 void RooArgList::writeToStream(ostream& os, Bool_t compact)
167 {
168  if (!compact) {
169  coutE(InputArguments) << "RooArgList::writeToStream(" << GetName() << ") non-compact mode not supported" << endl ;
170  return ;
171  }
172 
173  for (const auto obj : _list) {
174  obj->writeToStream(os,kTRUE);
175  os << " " ;
176  }
177  os << endl ;
178 }
179 
180 
181 
182 ////////////////////////////////////////////////////////////////////////////////
183 /// Read the contents of the argset in ASCII form from given stream.
184 ///
185 /// A single line is read, and all elements are assumed to be separated
186 /// by white space. The value of each argument is read by the arguments
187 /// readFromStream function.
188 
190 {
191  if (!compact) {
192  coutE(InputArguments) << "RooArgList::readFromStream(" << GetName() << ") non-compact mode not supported" << endl ;
193  return kTRUE ;
194  }
195 
196  RooStreamParser parser(is) ;
197  for (auto next : _list) {
198  if (!next->getAttribute("Dynamic")) {
199  if (next->readFromStream(is,kTRUE,verbose)) {
200  parser.zapToEnd() ;
201 
202  return kTRUE ;
203  }
204  } else {
205  }
206  }
207 
208  if (!parser.atEOL()) {
209  TString rest = parser.readLine() ;
210  if (verbose) {
211  coutW(InputArguments) << "RooArgSet::readFromStream(" << GetName()
212  << "): ignoring extra characters at end of line: '" << rest << "'" << endl ;
213  }
214  }
215 
216  return kFALSE ;
217 }
218 
RooStreamParser.h
RooStreamParser::zapToEnd
void zapToEnd(Bool_t inclContLines=kFALSE)
Eat all characters up to and including then end of the current line.
Definition: RooStreamParser.cxx:361
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooMsgService.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooArgList::operator[]
RooAbsArg & operator[](Int_t idx) const
Array operator.
Definition: RooArgList.cxx:146
coutE
#define coutE(a)
Definition: RooMsgService.h:33
coutW
#define coutW(a)
Definition: RooMsgService.h:32
RooArgList
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooStreamParser::atEOL
Bool_t atEOL()
If true, parser is at end of line in stream.
Definition: RooStreamParser.cxx:88
RooArgList::at
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:70
TString
Basic string class.
Definition: TString.h:136
RooAbsCollection::GetName
const char * GetName() const
Returns name of object.
Definition: RooAbsCollection.h:227
bool
TIterator
Iterator abstract base class.
Definition: TIterator.h:30
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
RooTrace.h
RooStreamParser
Definition: RooStreamParser.h:21
TRACE_DESTROY
#define TRACE_DESTROY
Definition: RooTrace.h:24
RooArgList::RooArgList
RooArgList()
Default constructor.
Definition: RooArgList.cxx:59
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsCollection
RooAbsCollection is an abstract container object that can hold multiple RooAbsArg objects.
Definition: RooAbsCollection.h:31
TRACE_CREATE
#define TRACE_CREATE
Definition: RooTrace.h:23
RooAbsCollection::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:437
TCollection::MakeIterator
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const =0
TIterator::Next
virtual TObject * Next()=0
RooStreamParser::readLine
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of '\' in th...
Definition: RooStreamParser.cxx:309
RooArgList::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact)
Write the contents of the argset in ASCII form to given stream.
Definition: RooArgList.cxx:166
RooArgList::~RooArgList
virtual ~RooArgList()
Destructor.
Definition: RooArgList.cxx:129
RooAbsCategoryLValue.h
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
name
char name[80]
Definition: TGX11.cxx:110
RooArgList::readFromStream
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:189
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
TCollection
Collection abstract base class.
Definition: TCollection.h:63
RooArgList.h
RooAbsRealLValue.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:172
RooAbsCollection::_list
Storage_t _list
Definition: RooAbsCollection.h:250
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:29
int