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