Logo ROOT  
Reference Guide
RooArgSet.cxx
Go to the documentation of this file.
1 /***************************************************************************** * Project: RooFit *
2  * Package: RooFitCore *
3  * @(#)root/roofitcore:$Id$
4  * Authors: *
5  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
6  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
7  * *
8  * Copyright (c) 2000-2005, Regents of the University of California *
9  * and Stanford University. All rights reserved. *
10  * *
11  * Redistribution and use in source and binary forms, *
12  * with or without modification, are permitted according to the terms *
13  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
14  *****************************************************************************/
15 
16 //////////////////////////////////////////////////////////////////////////////
17 /// \class RooArgSet
18 /// RooArgSet is a container object that can hold multiple RooAbsArg objects.
19 /// The container has set semantics which means that:
20 ///
21 /// - Every object it contains must have a unique name returned by GetName().
22 ///
23 /// - Contained objects are not ordered, although the set can be traversed
24 /// using an iterator returned by createIterator(). The iterator does not
25 /// necessarily follow the object insertion order.
26 ///
27 /// - Objects can be retrieved by name only, and not by index.
28 ///
29 ///
30 /// Ownership of contents
31 /// -------------------------
32 /// Unowned objects are inserted with the add() method. Owned objects
33 /// are added with addOwned() or addClone(). A RooArgSet either owns all
34 /// of it contents, or none, which is determined by the first `add`
35 /// call. Once an ownership status is selected, inappropriate `add` calls
36 /// will return error status. Clearing the list via removeAll() resets the
37 /// ownership status. Arguments supplied in the constructor are always added
38 /// as unowned elements.
39 ///
40 ///
41 
42 #include "RooArgSet.h"
43 
44 #include "TClass.h"
45 #include "RooStreamParser.h"
46 #include "RooFormula.h"
47 #include "RooAbsRealLValue.h"
48 #include "RooAbsCategoryLValue.h"
49 #include "RooStringVar.h"
50 #include "RooTrace.h"
51 #include "RooArgList.h"
52 #include "RooSentinel.h"
53 #include "RooMsgService.h"
54 #include "ROOT/RMakeUnique.hxx"
55 #include "strlcpy.h"
56 
57 #include <iostream>
58 #include <fstream>
59 #include <iomanip>
60 #include <stdexcept>
61 
62 using namespace std ;
63 
64 #if (__GNUC__==3&&__GNUC_MINOR__==2&&__GNUC_PATCHLEVEL__==3)
65 char* operator+( streampos&, char* );
66 #endif
67 
69 
70 
71 
72 #ifndef USEMEMPOOLFORARGSET
74 #else
75 
76 #include "MemPoolForRooSets.h"
77 
80  static auto * memPool = new RooArgSet::MemPool();
81  return memPool;
82 }
83 
84 ////////////////////////////////////////////////////////////////////////////////
85 /// Clear memory pool on exit to avoid reported memory leaks
86 
87 void RooArgSet::cleanup()
88 {
89  auto pool = memPool();
90  memPool()->teardown();
91 
92  //Here, the pool might have to leak if RooArgSets are still alive.
93  if (pool->empty())
94  delete pool;
95 }
96 
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 /// Overloaded new operator guarantees that all RooArgSets allocated with new
100 /// have a unique address, a property that is exploited in several places
101 /// in roofit to quickly index contents on normalization set pointers.
102 /// The memory pool only allocates space for the class itself. The elements
103 /// stored in the set are stored outside the pool.
104 
105 void* RooArgSet::operator new (size_t bytes)
106 {
107  //This will fail if a derived class uses this operator
108  assert(sizeof(RooArgSet) == bytes);
109 
110  return memPool()->allocate(bytes);
111 }
112 
113 
114 ////////////////////////////////////////////////////////////////////////////////
115 /// Overloaded new operator with placement does not guarante that all
116 /// RooArgSets allocated with new have a unique address, but uses the global
117 /// operator.
118 
119 void* RooArgSet::operator new (size_t bytes, void* ptr) noexcept
120 {
121  return ::operator new (bytes, ptr);
122 }
123 
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// Memory is owned by pool, we need to do nothing to release it
127 
128 void RooArgSet::operator delete (void* ptr)
129 {
130  // Decrease use count in pool that ptr is on
131  if (memPool()->deallocate(ptr))
132  return;
133 
134  std::cerr << __func__ << " " << ptr << " is not in any of the pools." << std::endl;
135 
136  // Not part of any pool; use global op delete:
137  ::operator delete(ptr);
138 }
139 
140 #endif
141 
142 
143 ////////////////////////////////////////////////////////////////////////////////
144 /// Default constructor
145 
148 {
150 }
151 
152 
153 ////////////////////////////////////////////////////////////////////////////////
154 /// Constructor from a RooArgList. If the list contains multiple
155 /// objects with the same name, only the first is store in the set.
156 /// Warning messages will be printed for dropped items.
158  RooAbsCollection(list.GetName())
159 {
160  add(list,kTRUE) ; // verbose to catch duplicate errors
162 }
163 
164 
165 ////////////////////////////////////////////////////////////////////////////////
166 /// Constructor from a RooArgSet / RooArgList and a pointer to another RooFit object.
167 ///
168 /// \param[in] collection Collection of RooFit objects to be added. If a list contains multiple
169 /// objects with the same name, only the first is stored in the set.
170 /// Warning messages will be printed for dropped items.
171 /// \param[in] var1 Further object to be added. If it is already in `collection`,
172 /// nothing happens, and the warning message is suppressed.
173 RooArgSet::RooArgSet(const RooAbsCollection& collection, const RooAbsArg* var1) :
174  RooAbsCollection(collection.GetName())
175 {
176  if (var1 && !collection.contains(*var1)) {
177  add(*var1,kTRUE) ;
178  }
179  add(collection,kTRUE) ; // verbose to catch duplicate errors
181 }
182 
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// Empty set constructor.
188 {
190 }
191 
192 
193 ////////////////////////////////////////////////////////////////////////////////
194 /// Construct a set from two existing sets. The new set will not own its
195 /// contents.
196 RooArgSet::RooArgSet(const RooArgSet& set1, const RooArgSet& set2, const char *name) : RooAbsCollection(name)
197 {
198  add(set1) ;
199  add(set2) ;
201 }
202 
203 
204 ////////////////////////////////////////////////////////////////////////////////
205 /// Constructor from a root TCollection. Elements in the collection that
206 /// do not inherit from RooAbsArg will be skipped. A warning message
207 /// will be printed for every skipped item.
208 
209 RooArgSet::RooArgSet(const TCollection& tcoll, const char* name) :
211 {
212  TIterator* iter = tcoll.MakeIterator() ;
213  TObject* obj ;
214  while((obj=iter->Next())) {
215  if (!dynamic_cast<RooAbsArg*>(obj)) {
216  coutW(InputArguments) << "RooArgSet::RooArgSet(TCollection) element " << obj->GetName()
217  << " is not a RooAbsArg, ignored" << endl ;
218  continue ;
219  }
220  add(*(RooAbsArg*)obj) ;
221  }
222  delete iter ;
224 }
225 
226 
227 ////////////////////////////////////////////////////////////////////////////////
228 /// Copy constructor. Note that a copy of a set is always non-owning,
229 /// even if the source set owns its contents. To create an owning copy of
230 /// a set (owning or not), use the snapshot() method.
231 RooArgSet::RooArgSet(const RooArgSet& other, const char *name)
232  : RooAbsCollection(other,name)
233 {
235 }
236 
237 
238 ////////////////////////////////////////////////////////////////////////////////
239 /// Destructor
240 
242 {
244 }
245 
246 
247 ////////////////////////////////////////////////////////////////////////////////
248 /// Add contents of a RooArgList to the set.
250  add(list);
251  if (_name.Length() == 0)
252  _name = list.GetName();
253 }
254 
255 ////////////////////////////////////////////////////////////////////////////////
256 /// Add element to non-owning set. The operation will fail if
257 /// a similarly named object already exists in the set, or
258 /// the set is specified to own its elements. Eventual error messages
259 /// can be suppressed with the silent flag
260 
261 Bool_t RooArgSet::add(const RooAbsArg& var, Bool_t silent)
262 {
263  return checkForDup(var,silent)? kFALSE : RooAbsCollection::add(var,silent) ;
264 }
265 
266 
267 
268 ////////////////////////////////////////////////////////////////////////////////
269 /// Add element to an owning set. The operation will fail if
270 /// a similarly named object already exists in the set, or
271 /// the set is not specified to own its elements. Eventual error messages
272 /// can be suppressed with the silent flag
273 
275 {
276  return checkForDup(var,silent)? kFALSE : RooAbsCollection::addOwned(var,silent) ;
277 }
278 
279 
280 
281 ////////////////////////////////////////////////////////////////////////////////
282 /// Add clone of specified element to an owning set. If sucessful, the
283 /// set will own the clone, not the original. The operation will fail if
284 /// a similarly named object already exists in the set, or
285 /// the set is not specified to own its elements. Eventual error messages
286 /// can be suppressed with the silent flag
287 
289 {
290  return checkForDup(var,silent)? 0 : RooAbsCollection::addClone(var,silent) ;
291 }
292 
293 
294 
295 ////////////////////////////////////////////////////////////////////////////////
296 /// Get reference to an element using its name. Named element must exist in set.
297 /// \throws invalid_argument if an element with the given name is not in the set.
298 ///
299 /// Note that since most RooFit objects use an assignment operator that copies
300 /// values, an expression like
301 /// ```
302 /// mySet["x"] = y;
303 /// ```
304 /// will not replace the element "x", it just assigns the values of y.
306 {
307  RooAbsArg* arg = find(name) ;
308  if (!arg) {
309  coutE(InputArguments) << "RooArgSet::operator[](" << GetName() << ") ERROR: no element named " << name << " in set" << endl ;
310  throw std::invalid_argument((TString("No element named '") + name + "' in set " + GetName()).Data());
311  }
312  return *arg ;
313 }
314 
315 
316 
317 ////////////////////////////////////////////////////////////////////////////////
318 /// Check if element with var's name is already in set
319 
320 Bool_t RooArgSet::checkForDup(const RooAbsArg& var, Bool_t silent) const
321 {
322  RooAbsArg *other = find(var);
323  if (other) {
324  if (other != &var) {
325  if (!silent) {
326  // print a warning if this variable is not the same one we
327  // already have
328  coutE(InputArguments) << "RooArgSet::checkForDup: ERROR argument with name " << var.GetName() << " is already in this set" << endl;
329  }
330  }
331  // don't add duplicates
332  return kTRUE;
333  }
334  return kFALSE ;
335 }
336 
337 
338 
339 ////////////////////////////////////////////////////////////////////////////////
340 /// Get value of a RooAbsReal stored in set with given name. If none is found, value of defVal is returned.
341 /// No error messages are printed unless the verbose flag is set
342 
344 {
345  RooAbsArg* raa = find(name) ;
346  if (!raa) {
347  if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
348  return defVal ;
349  }
350  RooAbsReal* rar = dynamic_cast<RooAbsReal*>(raa) ;
351  if (!rar) {
352  if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsReal" << endl ;
353  return defVal ;
354  }
355  return rar->getVal() ;
356 }
357 
358 
359 
360 ////////////////////////////////////////////////////////////////////////////////
361 /// Set value of a RooAbsRealLValye stored in set with given name to newVal
362 /// No error messages are printed unless the verbose flag is set
363 
365 {
366  RooAbsArg* raa = find(name) ;
367  if (!raa) {
368  if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
369  return kTRUE ;
370  }
371  RooAbsRealLValue* rar = dynamic_cast<RooAbsRealLValue*>(raa) ;
372  if (!rar) {
373  if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsRealLValue" << endl ;
374  return kTRUE;
375  }
376  rar->setVal(newVal) ;
377  return kFALSE ;
378 }
379 
380 
381 
382 ////////////////////////////////////////////////////////////////////////////////
383 /// Get state name of a RooAbsCategory stored in set with given name. If none is found, value of defVal is returned.
384 /// No error messages are printed unless the verbose flag is set
385 
386 const char* RooArgSet::getCatLabel(const char* name, const char* defVal, Bool_t verbose) const
387 {
388  RooAbsArg* raa = find(name) ;
389  if (!raa) {
390  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
391  return defVal ;
392  }
393  RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
394  if (!rac) {
395  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
396  return defVal ;
397  }
398  return rac->getCurrentLabel() ;
399 }
400 
401 
402 
403 ////////////////////////////////////////////////////////////////////////////////
404 /// Set state name of a RooAbsCategoryLValue stored in set with given name to newVal.
405 /// No error messages are printed unless the verbose flag is set
406 
407 Bool_t RooArgSet::setCatLabel(const char* name, const char* newVal, Bool_t verbose)
408 {
409  RooAbsArg* raa = find(name) ;
410  if (!raa) {
411  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
412  return kTRUE ;
413  }
414  RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
415  if (!rac) {
416  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
417  return kTRUE ;
418  }
419  rac->setLabel(newVal) ;
420  return kFALSE ;
421 }
422 
423 
424 
425 ////////////////////////////////////////////////////////////////////////////////
426 /// Get index value of a RooAbsCategory stored in set with given name. If none is found, value of defVal is returned.
427 /// No error messages are printed unless the verbose flag is set
428 
430 {
431  RooAbsArg* raa = find(name) ;
432  if (!raa) {
433  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
434  return defVal ;
435  }
436  RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
437  if (!rac) {
438  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
439  return defVal ;
440  }
441  return rac->getCurrentIndex() ;
442 }
443 
444 
445 
446 ////////////////////////////////////////////////////////////////////////////////
447 /// Set index value of a RooAbsCategoryLValue stored in set with given name to newVal.
448 /// No error messages are printed unless the verbose flag is set
449 
451 {
452  RooAbsArg* raa = find(name) ;
453  if (!raa) {
454  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
455  return kTRUE ;
456  }
457  RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
458  if (!rac) {
459  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
460  return kTRUE ;
461  }
462  rac->setIndex(newVal) ;
463  return kFALSE ;
464 }
465 
466 
467 
468 ////////////////////////////////////////////////////////////////////////////////
469 /// Get string value of a RooStringVar stored in set with given name. If none is found, value of defVal is returned.
470 /// No error messages are printed unless the verbose flag is set
471 
472 const char* RooArgSet::getStringValue(const char* name, const char* defVal, Bool_t verbose) const
473 {
474  RooAbsArg* raa = find(name) ;
475  if (!raa) {
476  if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
477  return defVal ;
478  }
479  auto ras = dynamic_cast<const RooStringVar*>(raa) ;
480  if (!ras) {
481  if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooStringVar" << endl ;
482  return defVal ;
483  }
484 
485  return ras->getVal() ;
486 }
487 
488 
489 
490 ////////////////////////////////////////////////////////////////////////////////
491 /// Set string value of a RooStringVar stored in set with given name to newVal.
492 /// No error messages are printed unless the verbose flag is set
493 
494 Bool_t RooArgSet::setStringValue(const char* name, const char* newVal, Bool_t verbose)
495 {
496  RooAbsArg* raa = find(name) ;
497  if (!raa) {
498  if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
499  return kTRUE ;
500  }
501  auto ras = dynamic_cast<RooStringVar*>(raa);
502  if (!ras) {
503  if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooStringVar" << endl ;
504  return kTRUE ;
505  }
506  ras->setVal(newVal);
507 
508  return false;
509 }
510 
511 
512 
513 ////////////////////////////////////////////////////////////////////////////////
514 /// Write contents of the argset to specified file.
515 /// See writeToStream() for details
516 
517 void RooArgSet::writeToFile(const char* fileName) const
518 {
519  ofstream ofs(fileName) ;
520  if (ofs.fail()) {
521  coutE(InputArguments) << "RooArgSet::writeToFile(" << GetName() << ") error opening file " << fileName << endl ;
522  return ;
523  }
524  writeToStream(ofs,kFALSE) ;
525 }
526 
527 
528 
529 ////////////////////////////////////////////////////////////////////////////////
530 /// Read contents of the argset from specified file.
531 /// See readFromStream() for details
532 
533 Bool_t RooArgSet::readFromFile(const char* fileName, const char* flagReadAtt, const char* section, Bool_t verbose)
534 {
535  ifstream ifs(fileName) ;
536  if (ifs.fail()) {
537  coutE(InputArguments) << "RooArgSet::readFromFile(" << GetName() << ") error opening file " << fileName << endl ;
538  return kTRUE ;
539  }
540  return readFromStream(ifs,kFALSE,flagReadAtt,section,verbose) ;
541 }
542 
543 
544 
545 
546 ////////////////////////////////////////////////////////////////////////////////
547 /// Write the contents of the argset in ASCII form to given stream.
548 ///
549 /// A line is written for each element contained in the form
550 /// `<argName> = <argValue>`
551 ///
552 /// The `<argValue>` part of each element is written by the arguments'
553 /// writeToStream() function.
554 /// \param os The stream to write to.
555 /// \param compact Write only the bare values, separated by ' '.
556 /// \note In compact mode, the stream cannot be read back into a RooArgSet,
557 /// but only into a RooArgList, because the variable names are lost.
558 /// \param section If non-null, add a section header like `[<section>]`.
559 void RooArgSet::writeToStream(ostream& os, Bool_t compact, const char* section) const
560 {
561  if (section && section[0] != '\0')
562  os << '[' << section << ']' << '\n';
563 
564  if (compact) {
565  for (const auto next : _list) {
566  next->writeToStream(os, true);
567  os << " ";
568  }
569  os << endl;
570  } else {
571  for (const auto next : _list) {
572  os << next->GetName() << " = " ;
573  next->writeToStream(os,kFALSE) ;
574  os << endl ;
575  }
576  }
577 }
578 
579 
580 
581 
582 ////////////////////////////////////////////////////////////////////////////////
583 /// Read the contents of the argset in ASCII form from given stream.
584 ///
585 /// The stream is read to end-of-file and each line is assumed to be
586 /// of the form
587 /// \code
588 /// <argName> = <argValue>
589 /// \endcode
590 /// Lines starting with argNames not matching any element in the list
591 /// will be ignored with a warning message. In addition limited C++ style
592 /// preprocessing and flow control is provided. The following constructions
593 /// are recognized:
594 /// \code
595 /// include "include.file"
596 /// \endcode
597 /// Include given file, recursive inclusion OK
598 /// \code
599 /// if (<boolean_expression>)
600 /// <name> = <value>
601 /// ....
602 /// else if (<boolean_expression>)
603 /// ....
604 /// else
605 /// ....
606 /// endif
607 /// \endcode
608 ///
609 /// All expressions are evaluated by RooFormula, and may involve any of
610 /// the sets variables.
611 /// \code
612 /// echo <Message>
613 /// \endcode
614 /// Print console message while reading from stream
615 /// \code
616 /// abort
617 /// \endcode
618 /// Force termination of read sequence with error status
619 ///
620 /// The value of each argument is read by the arguments readFromStream
621 /// function.
622 
623 Bool_t RooArgSet::readFromStream(istream& is, Bool_t compact, const char* flagReadAtt, const char* section, Bool_t verbose)
624 {
625  if (compact) {
626  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << ") compact mode not supported" << endl ;
627  return kTRUE ;
628  }
629 
630  RooStreamParser parser(is) ;
631  parser.setPunctuation("=") ;
632  TString token ;
633  Bool_t retVal(kFALSE) ;
634 
635  // Conditional stack and related state variables
636  // coverity[UNINIT]
637  Bool_t anyCondTrue[100] ;
638  Bool_t condStack[100] ;
639  Bool_t lastLineWasElse=kFALSE ;
640  Int_t condStackLevel=0 ;
641  condStack[0]=kTRUE ;
642 
643  // Prepare section processing
644  TString sectionHdr("[") ;
645  if (section) sectionHdr.Append(section) ;
646  sectionHdr.Append("]") ;
647  Bool_t inSection(section?kFALSE:kTRUE) ;
648 
649  Bool_t reprocessToken = kFALSE ;
650  while (1) {
651 
652  if (is.eof() || is.fail() || parser.atEOF()) {
653  break ;
654  }
655 
656  // Read next token until memEnd of file
657  if (!reprocessToken) {
658  token = parser.readToken() ;
659  }
660  reprocessToken = kFALSE ;
661 
662  // Skip empty lines
663  if (token.IsNull()) {
664  continue ;
665  }
666 
667  // Process include directives
668  if (!token.CompareTo("include")) {
669  if (parser.atEOL()) {
670  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
671  << "): no filename found after include statement" << endl ;
672  return kTRUE ;
673  }
674  TString filename = parser.readLine() ;
675  ifstream incfs(filename) ;
676  if (!incfs.good()) {
677  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): cannot open include file " << filename << endl ;
678  return kTRUE ;
679  }
680  coutI(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): processing include file "
681  << filename << endl ;
682  if (readFromStream(incfs,compact,flagReadAtt,inSection?0:section,verbose)) return kTRUE ;
683  continue ;
684  }
685 
686  // Process section headers if requested
687  if (*token.Data()=='[') {
688  TString hdr(token) ;
689  const char* last = token.Data() + token.Length() -1 ;
690  if (*last != ']') {
691  hdr.Append(" ") ;
692  hdr.Append(parser.readLine()) ;
693  }
694  // parser.putBackToken(token) ;
695  // token = parser.readLine() ;
696  if (section) {
697  inSection = !sectionHdr.CompareTo(hdr) ;
698  }
699  continue ;
700  }
701 
702  // If section is specified, ignore all data outside specified section
703  if (!inSection) {
704  parser.zapToEnd(kTRUE) ;
705  continue ;
706  }
707 
708  // Conditional statement evaluation
709  if (!token.CompareTo("if")) {
710 
711  // Extract conditional expressions and check validity
712  TString expr = parser.readLine() ;
713  RooFormula form(expr,expr,*this) ;
714  if (!form.ok()) return kTRUE ;
715 
716  // Evaluate expression
717  Bool_t status = form.eval()?kTRUE:kFALSE ;
718  if (lastLineWasElse) {
719  anyCondTrue[condStackLevel] |= status ;
720  lastLineWasElse=kFALSE ;
721  } else {
722  condStackLevel++ ;
723  anyCondTrue[condStackLevel] = status ;
724  }
725  condStack[condStackLevel] = status ;
726 
727  if (verbose) cxcoutD(Eval) << "RooArgSet::readFromStream(" << GetName()
728  << "): conditional expression " << expr << " = "
729  << (condStack[condStackLevel]?"true":"false") << endl ;
730  continue ; // go to next line
731  }
732 
733  if (!token.CompareTo("else")) {
734  // Must have seen an if statement before
735  if (condStackLevel==0) {
736  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'else'" << endl ;
737  }
738 
739  if (parser.atEOL()) {
740  // simple else: process if nothing else was true
741  condStack[condStackLevel] = !anyCondTrue[condStackLevel] ;
742  parser.zapToEnd(kFALSE) ;
743  continue ;
744  } else {
745  // if anything follows it should be 'if'
746  token = parser.readToken() ;
747  if (token.CompareTo("if")) {
748  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): syntax error: 'else " << token << "'" << endl ;
749  return kTRUE ;
750  } else {
751  if (anyCondTrue[condStackLevel]) {
752  // No need for further checking, true conditional already processed
753  condStack[condStackLevel] = kFALSE ;
754  parser.zapToEnd(kFALSE) ;
755  continue ;
756  } else {
757  // Process as normal 'if' no true conditional was encountered
758  reprocessToken = kTRUE ;
759  lastLineWasElse=kTRUE ;
760  continue ;
761  }
762  }
763  }
764  }
765 
766  if (!token.CompareTo("endif")) {
767  // Must have seen an if statement before
768  if (condStackLevel==0) {
769  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'endif'" << endl ;
770  return kTRUE ;
771  }
772 
773  // Decrease stack by one
774  condStackLevel-- ;
775  continue ;
776  }
777 
778  // If current conditional is true
779  if (condStack[condStackLevel]) {
780 
781  // Process echo statements
782  if (!token.CompareTo("echo")) {
783  TString message = parser.readLine() ;
784  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): >> " << message << endl ;
785  continue ;
786  }
787 
788  // Process abort statements
789  if (!token.CompareTo("abort")) {
790  TString message = parser.readLine() ;
791  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): USER ABORT" << endl ;
792  return kTRUE ;
793  }
794 
795  // Interpret the rest as <arg> = <value_expr>
796  RooAbsArg *arg ;
797 
798  if ((arg = find(token)) && !arg->getAttribute("Dynamic")) {
799  if (parser.expectToken("=",kTRUE)) {
800  parser.zapToEnd(kTRUE) ;
801  retVal=kTRUE ;
802  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
803  << "): missing '=' sign: " << arg << endl ;
804  continue ;
805  }
806  Bool_t argRet = arg->readFromStream(is,kFALSE,verbose) ;
807  if (!argRet && flagReadAtt) arg->setAttribute(flagReadAtt,kTRUE) ;
808  retVal |= argRet ;
809  } else {
810  if (verbose) {
811  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): argument "
812  << token << " not in list, ignored" << endl ;
813  }
814  parser.zapToEnd(kTRUE) ;
815  }
816  } else {
817  parser.readLine() ;
818  }
819  }
820 
821  // Did we fully unwind the conditional stack?
822  if (condStackLevel!=0) {
823  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): missing 'endif'" << endl ;
824  return kTRUE ;
825  }
826 
827  return retVal ;
828 }
829 
830 
831 Bool_t RooArgSet::isInRange(const char* rangeSpec)
832 {
833  char buf[1024] ;
834  strlcpy(buf,rangeSpec,1024) ;
835  char* token = strtok(buf,",") ;
836 
837  TIterator* iter = createIterator() ;
838 
839  while(token) {
840 
841  Bool_t accept=kTRUE ;
842  iter->Reset() ;
843  RooAbsArg* arg ;
844  while((arg=(RooAbsArg*)iter->Next())) {
845  RooAbsRealLValue* lvarg = dynamic_cast<RooAbsRealLValue*>(arg) ;
846  if (lvarg) {
847  if (!lvarg->inRange(token)) {
848  accept=kFALSE ;
849  break ;
850  }
851  }
852  // WVE MUST HANDLE RooAbsCategoryLValue ranges as well
853  }
854  if (accept) {
855  delete iter ;
856  return kTRUE ;
857  }
858 
859  token = strtok(0,",") ;
860  }
861 
862  delete iter ;
863  return kFALSE ;
864 }
RooArgSet::MemPool
MemPoolForRooSets< RooArgSet, 10 *600 > MemPool
Definition: RooArgSet.h:146
RooStreamParser.h
RooFormula
RooFormula internally uses ROOT's TFormula to compute user-defined expressions of RooAbsArgs.
Definition: RooFormula.h:34
RooAbsRealLValue::inRange
virtual Bool_t inRange(const char *name) const
Check if current value is inside range with given name.
Definition: RooAbsRealLValue.cxx:504
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
RooArgSet::setRealValue
Bool_t setRealValue(const char *name, Double_t newVal=0, Bool_t verbose=kFALSE)
Set value of a RooAbsRealLValye stored in set with given name to newVal No error messages are printed...
Definition: RooArgSet.cxx:364
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooStringVar.h
RooArgSet::writeToFile
void writeToFile(const char *fileName) const
Write contents of the argset to specified file.
Definition: RooArgSet.cxx:517
RooStreamParser::atEOF
Bool_t atEOF()
Definition: RooStreamParser.h:44
RooMsgService.h
RooStreamParser::expectToken
Bool_t expectToken(const TString &expected, Bool_t zapOnError=kFALSE)
Read the next token and return kTRUE if it is identical to the given 'expected' token.
Definition: RooStreamParser.cxx:397
RooAbsCategory::getCurrentIndex
virtual value_type getCurrentIndex() const
Return index number of current state.
Definition: RooAbsCategory.cxx:114
RooAbsCategoryLValue::setLabel
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
RooArgSet::getRealValue
Double_t getRealValue(const char *name, Double_t defVal=0, Bool_t verbose=kFALSE) const
Get value of a RooAbsReal stored in set with given name.
Definition: RooArgSet.cxx:343
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
RooArgSet::checkForDup
Bool_t checkForDup(const RooAbsArg &arg, Bool_t silent) const
Check if element with var's name is already in set.
Definition: RooArgSet.cxx:320
RooArgSet.h
TString::Data
const char * Data() const
Definition: TString.h:369
RooArgSet::isInRange
Bool_t isInRange(const char *rangeSpec)
Definition: RooArgSet.cxx:831
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooArgSet::RooArgSet
RooArgSet()
Default constructor.
Definition: RooArgSet.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
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:92
RooArgSet::setStringValue
Bool_t setStringValue(const char *name, const char *newVal="", Bool_t verbose=kFALSE)
Set string value of a RooStringVar stored in set with given name to newVal.
Definition: RooArgSet.cxx:494
RooArgSet::setCatLabel
Bool_t setCatLabel(const char *name, const char *newVal="", Bool_t verbose=kFALSE)
Set state name of a RooAbsCategoryLValue stored in set with given name to newVal.
Definition: RooArgSet.cxx:407
RooStreamParser::atEOL
Bool_t atEOL()
If true, parser is at end of line in stream.
Definition: RooStreamParser.cxx:88
RooAbsCollection::find
RooAbsArg * find(const char *name) const
Find object with given name in list.
Definition: RooAbsCollection.cxx:813
RooArgSet::processArg
void processArg(const RooAbsArg &var)
Definition: RooArgSet.h:139
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
coutI
#define coutI(a)
Definition: RooMsgService.h:30
TClass.h
RooFormula.h
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
RooArgSet::~RooArgSet
~RooArgSet() override
Destructor.
Definition: RooArgSet.cxx:241
RooStreamParser::readToken
TString readToken()
Read one token separated by any of the know punctuation characters This function recognizes and handl...
Definition: RooStreamParser.cxx:129
RooAbsCollection::_name
TString _name
Definition: RooAbsCollection.h:254
TString
Basic string class.
Definition: TString.h:136
RooAbsCollection::GetName
const char * GetName() const
Returns name of object.
Definition: RooAbsCollection.h:227
RooArgSet::getCatLabel
const char * getCatLabel(const char *name, const char *defVal="", Bool_t verbose=kFALSE) const
Get state name of a RooAbsCategory stored in set with given name.
Definition: RooArgSet.cxx:386
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:130
bool
TIterator
Iterator abstract base class.
Definition: TIterator.h:30
operator+
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
Definition: TString.cxx:1474
RooArgSet::getCatIndex
Int_t getCatIndex(const char *name, Int_t defVal=0, Bool_t verbose=kFALSE) const
Get index value of a RooAbsCategory stored in set with given name.
Definition: RooArgSet.cxx:429
RooAbsCategory
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
Definition: RooAbsCategory.h:38
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
RooTrace.h
RooAbsCollection::contains
Bool_t contains(const RooAbsArg &var) const
Check if collection contains an argument with the same name as var.
Definition: RooAbsCollection.h:103
RooStreamParser
Definition: RooStreamParser.h:21
TRACE_DESTROY
#define TRACE_DESTROY
Definition: RooTrace.h:24
RooAbsCategoryLValue::setIndex
virtual bool setIndex(value_type index, bool printError=true)=0
Change category state by specifying the index code of the desired state.
RooFormula::ok
Bool_t ok() const
Definition: RooFormula.h:59
RooAbsCollection::addClone
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add a clone of the specified argument to list.
Definition: RooAbsCollection.cxx:412
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
TString::Append
TString & Append(const char *cs)
Definition: TString.h:564
RooArgSet::memPool
static MemPool * memPool()
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:119
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
RooArgSet::addClone
virtual RooAbsArg * addClone(const RooAbsArg &var, Bool_t silent=kFALSE)
Add a clone of the specified argument to list.
Definition: RooAbsCollection.cxx:412
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
RooArgSet::addOwned
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:389
RooSentinel::activate
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:57
RooArgSet::operator[]
RooAbsArg & operator[](const TString &str) const
Get reference to an element using its name.
Definition: RooArgSet.cxx:305
RooArgSet::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact, const char *section=0) const
Write the contents of the argset in ASCII form to given stream.
Definition: RooArgSet.cxx:559
RooAbsCollection::addOwned
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:389
TIterator::Next
virtual TObject * Next()=0
MemPoolForRooSets
Memory pool for RooArgSet and RooDataSet.
Definition: MemPoolForRooSets.h:34
RooArgSet::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Shortcut for readFromStream(std::istream&, Bool_t, const char*, const char*, Bool_t),...
Definition: RooArgSet.h:102
TIterator::Reset
virtual void Reset()=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
RooArgSet::cleanup
static void cleanup()
Definition: RooArgSet.cxx:73
RooStreamParser::setPunctuation
void setPunctuation(const TString &punct)
Change list of characters interpreted as punctuation.
Definition: RooStreamParser.cxx:99
RooAbsArg::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)=0
TString::CompareTo
int CompareTo(const char *cs, ECaseCompare cmp=kExact) const
Compare a string to char *cs2.
Definition: TString.cxx:418
TString::IsNull
Bool_t IsNull() const
Definition: TString.h:407
Double_t
double Double_t
Definition: RtypesCore.h:59
MemPoolForRooSets.h
RooStringVar
RooStringVar is a RooAbsArg implementing string values.
Definition: RooStringVar.h:23
RooAbsCategoryLValue.h
RooArgSet::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
Definition: RooAbsCollection.cxx:437
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
RooAbsArg::setAttribute
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:291
RooArgSet::readFromFile
Bool_t readFromFile(const char *fileName, const char *flagReadAtt=0, const char *section=0, Bool_t verbose=kFALSE)
Read contents of the argset from specified file.
Definition: RooArgSet.cxx:533
name
char name[80]
Definition: TGX11.cxx:110
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
RMakeUnique.hxx
TCollection
Collection abstract base class.
Definition: TCollection.h:63
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
RooAbsCategoryLValue
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
Definition: RooAbsCategoryLValue.h:25
RooArgSet::getStringValue
const char * getStringValue(const char *name, const char *defVal="", Bool_t verbose=kFALSE) const
Get string value of a RooStringVar stored in set with given name.
Definition: RooArgSet.cxx:472
RooAbsRealLValue::setVal
virtual void setVal(Double_t value)=0
Set the current value of the object. Needs to be overridden by implementations.
RooFit::Eval
@ Eval
Definition: RooGlobalFunc.h:68
RooFormula::eval
Double_t eval(const RooArgSet *nset=0) const
Evalute all parameters/observables, and then evaluate formula.
Definition: RooFormula.cxx:344
RooAbsRealLValue
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
Definition: RooAbsRealLValue.h:31
RooArgList.h
RooSentinel.h
RooAbsRealLValue.h
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
cxcoutD
#define cxcoutD(a)
Definition: RooMsgService.h:81
int
RooArgSet::setCatIndex
Bool_t setCatIndex(const char *name, Int_t newVal=0, Bool_t verbose=kFALSE)
Set index value of a RooAbsCategoryLValue stored in set with given name to newVal.
Definition: RooArgSet.cxx:450
RooAbsArg::getAttribute
Bool_t getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
Definition: RooAbsArg.cxx:314