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