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 "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 "RooArgList.h"
53 #include "RooSentinel.h"
54 #include "RooMsgService.h"
55 #include "ROOT/RMakeUnique.hxx"
56 #include "strlcpy.h"
57 
58 #include <iostream>
59 #include <fstream>
60 #include <iomanip>
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 ////////////////////////////////////////////////////////////////////////////////
155 /// Constructor from a RooArgList. If the list contains multiple
156 /// objects with the same name, only the first is store in the set.
157 /// Warning messages will be printed for dropped items.
158 
160  RooAbsCollection(list.GetName())
161 {
162  add(list,kTRUE) ; // verbose to catch duplicate errors
164 }
165 
166 
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Constructor from a RooArgList. If the list contains multiple
170 /// objects with the same name, only the first is store in the set.
171 /// Warning messages will be printed for dropped items.
172 
173 RooArgSet::RooArgSet(const RooArgList& list, const RooAbsArg* var1) :
174  RooAbsCollection(list.GetName())
175 {
176  if (var1 && !list.contains(*var1)) {
177  add(*var1,kTRUE) ;
178  }
179  add(list,kTRUE) ; // verbose to catch duplicate errors
181 }
182 
183 
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Empty set constructor
187 
190 {
192 }
193 
194 
195 
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Construct a set from two existing sets
199 
200 RooArgSet::RooArgSet(const RooArgSet& set1, const RooArgSet& set2, const char *name) : RooAbsCollection(name)
201 {
202  add(set1) ;
203  add(set2) ;
204  TRACE_CREATE
205 }
206 
207 
208 
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Constructor for set containing 1 initial object
212 
214  const char *name) :
216 {
217  add(var1);
219 }
220 
221 
222 
223 ////////////////////////////////////////////////////////////////////////////////
224 /// Constructor for set containing 2 initial objects
225 
226 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
227  const char *name) :
229 {
230  add(var1); add(var2);
232 }
233 
234 
235 
236 ////////////////////////////////////////////////////////////////////////////////
237 /// Constructor for set containing 3 initial objects
238 
239 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
240  const RooAbsArg& var3,
241  const char *name) :
243 {
244  add(var1); add(var2); add(var3);
246 }
247 
248 
249 
250 ////////////////////////////////////////////////////////////////////////////////
251 /// Constructor for set containing 4 initial objects
252 
253 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
254  const RooAbsArg& var3, const RooAbsArg& var4,
255  const char *name) :
257 {
258  add(var1); add(var2); add(var3); add(var4);
260 }
261 
262 
263 
264 ////////////////////////////////////////////////////////////////////////////////
265 /// Constructor for set containing 5 initial objects
266 
268  const RooAbsArg& var2, const RooAbsArg& var3,
269  const RooAbsArg& var4, const RooAbsArg& var5,
270  const char *name) :
272 {
273  add(var1); add(var2); add(var3); add(var4); add(var5);
275 }
276 
277 
278 
279 ////////////////////////////////////////////////////////////////////////////////
280 /// Constructor for set containing 6 initial objects
281 
282 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
283  const RooAbsArg& var3, const RooAbsArg& var4,
284  const RooAbsArg& var5, const RooAbsArg& var6,
285  const char *name) :
287 {
288  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6);
290 }
291 
292 
293 
294 ////////////////////////////////////////////////////////////////////////////////
295 /// Constructor for set containing 7 initial objects
296 
297 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
298  const RooAbsArg& var3, const RooAbsArg& var4,
299  const RooAbsArg& var5, const RooAbsArg& var6,
300  const RooAbsArg& var7,
301  const char *name) :
303 {
304  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;
306 }
307 
308 
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Constructor for set containing 8 initial objects
312 
313 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
314  const RooAbsArg& var3, const RooAbsArg& var4,
315  const RooAbsArg& var5, const RooAbsArg& var6,
316  const RooAbsArg& var7, const RooAbsArg& var8,
317  const char *name) :
319 {
320  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7) ;add(var8) ;
322 }
323 
324 
325 
326 ////////////////////////////////////////////////////////////////////////////////
327 /// Constructor for set containing 9 initial objects
328 
329 RooArgSet::RooArgSet(const RooAbsArg& var1, const RooAbsArg& var2,
330  const RooAbsArg& var3, const RooAbsArg& var4,
331  const RooAbsArg& var5, const RooAbsArg& var6,
332  const RooAbsArg& var7, const RooAbsArg& var8,
333  const RooAbsArg& var9, const char *name) :
335 {
336  add(var1); add(var2); add(var3); add(var4); add(var5); add(var6); add(var7); add(var8); add(var9);
338 }
339 
340 
341 
342 ////////////////////////////////////////////////////////////////////////////////
343 /// Constructor from a root TCollection. Elements in the collection that
344 /// do not inherit from RooAbsArg will be skipped. A warning message
345 /// will be printed for every skipped item.
346 
347 RooArgSet::RooArgSet(const TCollection& tcoll, const char* name) :
349 {
350  TIterator* iter = tcoll.MakeIterator() ;
351  TObject* obj ;
352  while((obj=iter->Next())) {
353  if (!dynamic_cast<RooAbsArg*>(obj)) {
354  coutW(InputArguments) << "RooArgSet::RooArgSet(TCollection) element " << obj->GetName()
355  << " is not a RooAbsArg, ignored" << endl ;
356  continue ;
357  }
358  add(*(RooAbsArg*)obj) ;
359  }
360  delete iter ;
362 }
363 
364 
365 
366 ////////////////////////////////////////////////////////////////////////////////
367 /// Copy constructor. Note that a copy of a set is always non-owning,
368 /// even the source set is owning. To create an owning copy of
369 /// a set (owning or not), use the snaphot() method.
370 
371 RooArgSet::RooArgSet(const RooArgSet& other, const char *name)
372  : RooAbsCollection(other,name)
373 {
375 }
376 
377 
378 
379 ////////////////////////////////////////////////////////////////////////////////
380 /// Destructor
381 
383 {
385 }
386 
387 
388 
389 ////////////////////////////////////////////////////////////////////////////////
390 /// Add element to non-owning set. The operation will fail if
391 /// a similarly named object already exists in the set, or
392 /// the set is specified to own its elements. Eventual error messages
393 /// can be suppressed with the silent flag
394 
395 Bool_t RooArgSet::add(const RooAbsArg& var, Bool_t silent)
396 {
397  return checkForDup(var,silent)? kFALSE : RooAbsCollection::add(var,silent) ;
398 }
399 
400 
401 
402 ////////////////////////////////////////////////////////////////////////////////
403 /// Add element to an owning set. The operation will fail if
404 /// a similarly named object already exists in the set, or
405 /// the set is not specified to own its elements. Eventual error messages
406 /// can be suppressed with the silent flag
407 
409 {
410  return checkForDup(var,silent)? kFALSE : RooAbsCollection::addOwned(var,silent) ;
411 }
412 
413 
414 
415 ////////////////////////////////////////////////////////////////////////////////
416 /// Add clone of specified element to an owning set. If sucessful, the
417 /// set will own the clone, not the original. The operation will fail if
418 /// a similarly named object already exists in the set, or
419 /// the set is not specified to own its elements. Eventual error messages
420 /// can be suppressed with the silent flag
421 
423 {
424  return checkForDup(var,silent)? 0 : RooAbsCollection::addClone(var,silent) ;
425 }
426 
427 
428 
429 ////////////////////////////////////////////////////////////////////////////////
430 /// Array operator. Named element must exist in set, otherwise
431 /// code will abort.
432 ///
433 /// When used as lvalue in assignment operations, the element contained in
434 /// the list will not be changed, only the value of the existing element!
435 
437 {
438  RooAbsArg* arg = find(name) ;
439  if (!arg) {
440  coutE(InputArguments) << "RooArgSet::operator[](" << GetName() << ") ERROR: no element named " << name << " in set" << endl ;
442  }
443  return *arg ;
444 }
445 
446 
447 
448 ////////////////////////////////////////////////////////////////////////////////
449 /// Check if element with var's name is already in set
450 
451 Bool_t RooArgSet::checkForDup(const RooAbsArg& var, Bool_t silent) const
452 {
453  RooAbsArg *other = find(var);
454  if (other) {
455  if (other != &var) {
456  if (!silent) {
457  // print a warning if this variable is not the same one we
458  // already have
459  coutE(InputArguments) << "RooArgSet::checkForDup: ERROR argument with name " << var.GetName() << " is already in this set" << endl;
460  }
461  }
462  // don't add duplicates
463  return kTRUE;
464  }
465  return kFALSE ;
466 }
467 
468 
469 
470 ////////////////////////////////////////////////////////////////////////////////
471 /// Get value of a RooAbsReal stored in set with given name. If none is found, value of defVal is returned.
472 /// No error messages are printed unless the verbose flag is set
473 
475 {
476  RooAbsArg* raa = find(name) ;
477  if (!raa) {
478  if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
479  return defVal ;
480  }
481  RooAbsReal* rar = dynamic_cast<RooAbsReal*>(raa) ;
482  if (!rar) {
483  if (verbose) coutE(InputArguments) << "RooArgSet::getRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsReal" << endl ;
484  return defVal ;
485  }
486  return rar->getVal() ;
487 }
488 
489 
490 
491 ////////////////////////////////////////////////////////////////////////////////
492 /// Set value of a RooAbsRealLValye stored in set with given name to newVal
493 /// No error messages are printed unless the verbose flag is set
494 
496 {
497  RooAbsArg* raa = find(name) ;
498  if (!raa) {
499  if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
500  return kTRUE ;
501  }
502  RooAbsRealLValue* rar = dynamic_cast<RooAbsRealLValue*>(raa) ;
503  if (!rar) {
504  if (verbose) coutE(InputArguments) << "RooArgSet::setRealValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsRealLValue" << endl ;
505  return kTRUE;
506  }
507  rar->setVal(newVal) ;
508  return kFALSE ;
509 }
510 
511 
512 
513 ////////////////////////////////////////////////////////////////////////////////
514 /// Get state name of a RooAbsCategory stored in set with given name. If none is found, value of defVal is returned.
515 /// No error messages are printed unless the verbose flag is set
516 
517 const char* RooArgSet::getCatLabel(const char* name, const char* defVal, Bool_t verbose) const
518 {
519  RooAbsArg* raa = find(name) ;
520  if (!raa) {
521  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
522  return defVal ;
523  }
524  RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
525  if (!rac) {
526  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
527  return defVal ;
528  }
529  return rac->getCurrentLabel() ;
530 }
531 
532 
533 
534 ////////////////////////////////////////////////////////////////////////////////
535 /// Set state name of a RooAbsCategoryLValue stored in set with given name to newVal.
536 /// No error messages are printed unless the verbose flag is set
537 
538 Bool_t RooArgSet::setCatLabel(const char* name, const char* newVal, Bool_t verbose)
539 {
540  RooAbsArg* raa = find(name) ;
541  if (!raa) {
542  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
543  return kTRUE ;
544  }
545  RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
546  if (!rac) {
547  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
548  return kTRUE ;
549  }
550  rac->setLabel(newVal) ;
551  return kFALSE ;
552 }
553 
554 
555 
556 ////////////////////////////////////////////////////////////////////////////////
557 /// Get index value of a RooAbsCategory stored in set with given name. If none is found, value of defVal is returned.
558 /// No error messages are printed unless the verbose flag is set
559 
561 {
562  RooAbsArg* raa = find(name) ;
563  if (!raa) {
564  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
565  return defVal ;
566  }
567  RooAbsCategory* rac = dynamic_cast<RooAbsCategory*>(raa) ;
568  if (!rac) {
569  if (verbose) coutE(InputArguments) << "RooArgSet::getCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
570  return defVal ;
571  }
572  return rac->getCurrentIndex() ;
573 }
574 
575 
576 
577 ////////////////////////////////////////////////////////////////////////////////
578 /// Set index value of a RooAbsCategoryLValue stored in set with given name to newVal.
579 /// No error messages are printed unless the verbose flag is set
580 
582 {
583  RooAbsArg* raa = find(name) ;
584  if (!raa) {
585  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
586  return kTRUE ;
587  }
588  RooAbsCategoryLValue* rac = dynamic_cast<RooAbsCategoryLValue*>(raa) ;
589  if (!rac) {
590  if (verbose) coutE(InputArguments) << "RooArgSet::setCatLabel(" << GetName() << ") ERROR object '" << name << "' is not of type RooAbsCategory" << endl ;
591  return kTRUE ;
592  }
593  rac->setIndex(newVal) ;
594  return kFALSE ;
595 }
596 
597 
598 
599 ////////////////////////////////////////////////////////////////////////////////
600 /// Get string value of a RooStringVar stored in set with given name. If none is found, value of defVal is returned.
601 /// No error messages are printed unless the verbose flag is set
602 
603 const char* RooArgSet::getStringValue(const char* name, const char* defVal, Bool_t verbose) const
604 {
605  RooAbsArg* raa = find(name) ;
606  if (!raa) {
607  if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
608  return defVal ;
609  }
610  auto ras = dynamic_cast<const RooStringVar*>(raa) ;
611  if (!ras) {
612  if (verbose) coutE(InputArguments) << "RooArgSet::getStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooStringVar" << endl ;
613  return defVal ;
614  }
615 
616  return ras->getVal() ;
617 }
618 
619 
620 
621 ////////////////////////////////////////////////////////////////////////////////
622 /// Set string value of a RooStringVar stored in set with given name to newVal.
623 /// No error messages are printed unless the verbose flag is set
624 
625 Bool_t RooArgSet::setStringValue(const char* name, const char* newVal, Bool_t verbose)
626 {
627  RooAbsArg* raa = find(name) ;
628  if (!raa) {
629  if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR no object with name '" << name << "' found" << endl ;
630  return kTRUE ;
631  }
632  auto ras = dynamic_cast<RooStringVar*>(raa);
633  if (!ras) {
634  if (verbose) coutE(InputArguments) << "RooArgSet::setStringValue(" << GetName() << ") ERROR object '" << name << "' is not of type RooStringVar" << endl ;
635  return kTRUE ;
636  }
637  ras->setVal(newVal);
638 
639  return false;
640 }
641 
642 
643 
644 ////////////////////////////////////////////////////////////////////////////////
645 /// Write contents of the argset to specified file.
646 /// See writeToStream() for details
647 
648 void RooArgSet::writeToFile(const char* fileName) const
649 {
650  ofstream ofs(fileName) ;
651  if (ofs.fail()) {
652  coutE(InputArguments) << "RooArgSet::writeToFile(" << GetName() << ") error opening file " << fileName << endl ;
653  return ;
654  }
655  writeToStream(ofs,kFALSE) ;
656 }
657 
658 
659 
660 ////////////////////////////////////////////////////////////////////////////////
661 /// Read contents of the argset from specified file.
662 /// See readFromStream() for details
663 
664 Bool_t RooArgSet::readFromFile(const char* fileName, const char* flagReadAtt, const char* section, Bool_t verbose)
665 {
666  ifstream ifs(fileName) ;
667  if (ifs.fail()) {
668  coutE(InputArguments) << "RooArgSet::readFromFile(" << GetName() << ") error opening file " << fileName << endl ;
669  return kTRUE ;
670  }
671  return readFromStream(ifs,kFALSE,flagReadAtt,section,verbose) ;
672 }
673 
674 
675 
676 
677 ////////////////////////////////////////////////////////////////////////////////
678 /// Write the contents of the argset in ASCII form to given stream.
679 ///
680 /// A line is written for each element contained in the form
681 /// `<argName> = <argValue>`
682 ///
683 /// The `<argValue>` part of each element is written by the arguments'
684 /// writeToStream() function.
685 /// \param os The stream to write to.
686 /// \param compact Write only the bare values, separated by ' '.
687 /// \note In compact mode, the stream cannot be read back into a RooArgSet,
688 /// but only into a RooArgList, because the variable names are lost.
689 /// \param section If non-null, add a section header like `[<section>]`.
690 void RooArgSet::writeToStream(ostream& os, Bool_t compact, const char* section) const
691 {
692  if (section && section[0] != '\0')
693  os << '[' << section << ']' << '\n';
694 
695  if (compact) {
696  for (const auto next : _list) {
697  next->writeToStream(os, true);
698  os << " ";
699  }
700  os << endl;
701  } else {
702  for (const auto next : _list) {
703  os << next->GetName() << " = " ;
704  next->writeToStream(os,kFALSE) ;
705  os << endl ;
706  }
707  }
708 }
709 
710 
711 
712 
713 ////////////////////////////////////////////////////////////////////////////////
714 /// Read the contents of the argset in ASCII form from given stream.
715 ///
716 /// The stream is read to end-of-file and each line is assumed to be
717 /// of the form
718 /// \code
719 /// <argName> = <argValue>
720 /// \endcode
721 /// Lines starting with argNames not matching any element in the list
722 /// will be ignored with a warning message. In addition limited C++ style
723 /// preprocessing and flow control is provided. The following constructions
724 /// are recognized:
725 /// \code
726 /// include "include.file"
727 /// \endcode
728 /// Include given file, recursive inclusion OK
729 /// \code
730 /// if (<boolean_expression>)
731 /// <name> = <value>
732 /// ....
733 /// else if (<boolean_expression>)
734 /// ....
735 /// else
736 /// ....
737 /// endif
738 /// \endcode
739 ///
740 /// All expressions are evaluated by RooFormula, and may involve any of
741 /// the sets variables.
742 /// \code
743 /// echo <Message>
744 /// \endcode
745 /// Print console message while reading from stream
746 /// \code
747 /// abort
748 /// \endcode
749 /// Force termination of read sequence with error status
750 ///
751 /// The value of each argument is read by the arguments readFromStream
752 /// function.
753 
754 Bool_t RooArgSet::readFromStream(istream& is, Bool_t compact, const char* flagReadAtt, const char* section, Bool_t verbose)
755 {
756  if (compact) {
757  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << ") compact mode not supported" << endl ;
758  return kTRUE ;
759  }
760 
761  RooStreamParser parser(is) ;
762  parser.setPunctuation("=") ;
763  TString token ;
764  Bool_t retVal(kFALSE) ;
765 
766  // Conditional stack and related state variables
767  // coverity[UNINIT]
768  Bool_t anyCondTrue[100] ;
769  Bool_t condStack[100] ;
770  Bool_t lastLineWasElse=kFALSE ;
771  Int_t condStackLevel=0 ;
772  condStack[0]=kTRUE ;
773 
774  // Prepare section processing
775  TString sectionHdr("[") ;
776  if (section) sectionHdr.Append(section) ;
777  sectionHdr.Append("]") ;
778  Bool_t inSection(section?kFALSE:kTRUE) ;
779 
780  Bool_t reprocessToken = kFALSE ;
781  while (1) {
782 
783  if (is.eof() || is.fail() || parser.atEOF()) {
784  break ;
785  }
786 
787  // Read next token until memEnd of file
788  if (!reprocessToken) {
789  token = parser.readToken() ;
790  }
791  reprocessToken = kFALSE ;
792 
793  // Skip empty lines
794  if (token.IsNull()) {
795  continue ;
796  }
797 
798  // Process include directives
799  if (!token.CompareTo("include")) {
800  if (parser.atEOL()) {
801  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
802  << "): no filename found after include statement" << endl ;
803  return kTRUE ;
804  }
805  TString filename = parser.readLine() ;
806  ifstream incfs(filename) ;
807  if (!incfs.good()) {
808  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): cannot open include file " << filename << endl ;
809  return kTRUE ;
810  }
811  coutI(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): processing include file "
812  << filename << endl ;
813  if (readFromStream(incfs,compact,flagReadAtt,inSection?0:section,verbose)) return kTRUE ;
814  continue ;
815  }
816 
817  // Process section headers if requested
818  if (*token.Data()=='[') {
819  TString hdr(token) ;
820  const char* last = token.Data() + token.Length() -1 ;
821  if (*last != ']') {
822  hdr.Append(" ") ;
823  hdr.Append(parser.readLine()) ;
824  }
825 // parser.putBackToken(token) ;
826 // token = parser.readLine() ;
827  if (section) {
828  inSection = !sectionHdr.CompareTo(hdr) ;
829  }
830  continue ;
831  }
832 
833  // If section is specified, ignore all data outside specified section
834  if (!inSection) {
835  parser.zapToEnd(kTRUE) ;
836  continue ;
837  }
838 
839  // Conditional statement evaluation
840  if (!token.CompareTo("if")) {
841 
842  // Extract conditional expressions and check validity
843  TString expr = parser.readLine() ;
844  RooFormula form(expr,expr,*this) ;
845  if (!form.ok()) return kTRUE ;
846 
847  // Evaluate expression
848  Bool_t status = form.eval()?kTRUE:kFALSE ;
849  if (lastLineWasElse) {
850  anyCondTrue[condStackLevel] |= status ;
851  lastLineWasElse=kFALSE ;
852  } else {
853  condStackLevel++ ;
854  anyCondTrue[condStackLevel] = status ;
855  }
856  condStack[condStackLevel] = status ;
857 
858  if (verbose) cxcoutD(Eval) << "RooArgSet::readFromStream(" << GetName()
859  << "): conditional expression " << expr << " = "
860  << (condStack[condStackLevel]?"true":"false") << endl ;
861  continue ; // go to next line
862  }
863 
864  if (!token.CompareTo("else")) {
865  // Must have seen an if statement before
866  if (condStackLevel==0) {
867  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'else'" << endl ;
868  }
869 
870  if (parser.atEOL()) {
871  // simple else: process if nothing else was true
872  condStack[condStackLevel] = !anyCondTrue[condStackLevel] ;
873  parser.zapToEnd(kFALSE) ;
874  continue ;
875  } else {
876  // if anything follows it should be 'if'
877  token = parser.readToken() ;
878  if (token.CompareTo("if")) {
879  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): syntax error: 'else " << token << "'" << endl ;
880  return kTRUE ;
881  } else {
882  if (anyCondTrue[condStackLevel]) {
883  // No need for further checking, true conditional already processed
884  condStack[condStackLevel] = kFALSE ;
885  parser.zapToEnd(kFALSE) ;
886  continue ;
887  } else {
888  // Process as normal 'if' no true conditional was encountered
889  reprocessToken = kTRUE ;
890  lastLineWasElse=kTRUE ;
891  continue ;
892  }
893  }
894  }
895  }
896 
897  if (!token.CompareTo("endif")) {
898  // Must have seen an if statement before
899  if (condStackLevel==0) {
900  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): unmatched 'endif'" << endl ;
901  return kTRUE ;
902  }
903 
904  // Decrease stack by one
905  condStackLevel-- ;
906  continue ;
907  }
908 
909  // If current conditional is true
910  if (condStack[condStackLevel]) {
911 
912  // Process echo statements
913  if (!token.CompareTo("echo")) {
914  TString message = parser.readLine() ;
915  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): >> " << message << endl ;
916  continue ;
917  }
918 
919  // Process abort statements
920  if (!token.CompareTo("abort")) {
921  TString message = parser.readLine() ;
922  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): USER ABORT" << endl ;
923  return kTRUE ;
924  }
925 
926  // Interpret the rest as <arg> = <value_expr>
927  RooAbsArg *arg ;
928 
929  if ((arg = find(token)) && !arg->getAttribute("Dynamic")) {
930  if (parser.expectToken("=",kTRUE)) {
931  parser.zapToEnd(kTRUE) ;
932  retVal=kTRUE ;
933  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName()
934  << "): missing '=' sign: " << arg << endl ;
935  continue ;
936  }
937  Bool_t argRet = arg->readFromStream(is,kFALSE,verbose) ;
938  if (!argRet && flagReadAtt) arg->setAttribute(flagReadAtt,kTRUE) ;
939  retVal |= argRet ;
940  } else {
941  if (verbose) {
942  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): argument "
943  << token << " not in list, ignored" << endl ;
944  }
945  parser.zapToEnd(kTRUE) ;
946  }
947  } else {
948  parser.readLine() ;
949  }
950  }
951 
952  // Did we fully unwind the conditional stack?
953  if (condStackLevel!=0) {
954  coutE(InputArguments) << "RooArgSet::readFromStream(" << GetName() << "): missing 'endif'" << endl ;
955  return kTRUE ;
956  }
957 
958  return retVal ;
959 }
960 
961 
962 Bool_t RooArgSet::isInRange(const char* rangeSpec)
963 {
964  char buf[1024] ;
965  strlcpy(buf,rangeSpec,1024) ;
966  char* token = strtok(buf,",") ;
967 
968  TIterator* iter = createIterator() ;
969 
970  while(token) {
971 
972  Bool_t accept=kTRUE ;
973  iter->Reset() ;
974  RooAbsArg* arg ;
975  while((arg=(RooAbsArg*)iter->Next())) {
976  RooAbsRealLValue* lvarg = dynamic_cast<RooAbsRealLValue*>(arg) ;
977  if (lvarg) {
978  if (!lvarg->inRange(token)) {
979  accept=kFALSE ;
980  break ;
981  }
982  }
983  // WVE MUST HANDLE RooAbsCategoryLValue ranges as well
984  }
985  if (accept) {
986  delete iter ;
987  return kTRUE ;
988  }
989 
990  token = strtok(0,",") ;
991  }
992 
993  delete iter ;
994  return kFALSE ;
995 }
RooArgSet::MemPool
MemPoolForRooSets< RooArgSet, 10 *600 > MemPool
Definition: RooArgSet.h:149
RooErrorHandler::softAbort
static void softAbort()
Definition: RooErrorHandler.h:44
RooStreamParser.h
RooFormula
Definition: RooFormula.h:28
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:495
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:648
RooStreamParser::atEOF
Bool_t atEOF()
Definition: RooStreamParser.h:58
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:474
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:451
RooArgSet.h
TString::Data
const char * Data() const
Definition: TString.h:369
RooArgSet::isInRange
Bool_t isInRange(const char *rangeSpec)
Definition: RooArgSet.cxx:962
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
Definition: RooArgList.h:21
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
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:625
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:538
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
TString::Length
Ssiz_t Length() const
Definition: TString.h:410
coutI
#define coutI(a)
Definition: RooMsgService.h:30
TClass.h
RooArgSet::add
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
RooFormula.h
RooAbsReal
Definition: RooAbsReal.h:61
RooStreamParser::readToken
TString readToken()
Read one token separated by any of the know punctuation characters This function recognizes and handl...
Definition: RooStreamParser.cxx:129
TString
Definition: TString.h:136
RooAbsCollection::GetName
const char * GetName() const
Returns name of object.
Definition: RooAbsCollection.h:226
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:517
RooAbsCategory::getCurrentLabel
virtual const char * getCurrentLabel() const
Return label string of current state.
Definition: RooAbsCategory.cxx:130
bool
TIterator
Definition: TIterator.h:30
operator+
TString operator+(const TString &s1, const TString &s2)
Use the special concatenation constructor.
Definition: TString.cxx:1474
RooArgSet::addOwned
virtual Bool_t addOwned(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:92
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:560
RooAbsCategory
Definition: RooAbsCategory.h:38
TObject::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:359
RooTrace.h
RooArgSet::~RooArgSet
virtual ~RooArgSet()
Destructor.
Definition: RooArgSet.cxx:382
RooAbsCollection::contains
Bool_t contains(const RooAbsArg &var) const
Check if collection contains an argument with the same name as var.
Definition: RooAbsCollection.h:102
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()
Definition: RooFormula.h:67
RooArgSet::operator[]
RooAbsArg & operator[](const char *name) const
Array operator.
Definition: RooArgSet.cxx:436
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:118
RooAbsCollection
Definition: RooAbsCollection.h:30
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
RooSentinel::activate
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:57
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:690
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: RooArgSet.h:26
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:110
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
Definition: RooStringVar.h:23
RooAbsCategoryLValue.h
TObject
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:259
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:664
name
char name[80]
Definition: TGX11.cxx:110
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooErrorHandler.h
RooAbsArg
Definition: RooAbsArg.h:73
RMakeUnique.hxx
TCollection
Definition: TCollection.h:63
RooArgSet::addClone
virtual void addClone(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:96
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
RooAbsCategoryLValue
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:603
RooAbsRealLValue::setVal
virtual void setVal(Double_t value)=0
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:340
RooAbsRealLValue
Definition: RooAbsRealLValue.h:31
RooArgList.h
RooSentinel.h
RooAbsRealLValue.h
RooAbsCollection::_list
Storage_t _list
Definition: RooAbsCollection.h:249
RooArgSet
Definition: RooArgSet.h:28
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:581
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:282