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