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