ROOT  6.06/09
Reference Guide
RooRealConstant.cxx
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id$
5  * Authors: *
6  * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7  * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8  * *
9  * Copyright (c) 2000-2005, Regents of the University of California *
10  * and Stanford University. All rights reserved. *
11  * *
12  * Redistribution and use in source and binary forms, *
13  * with or without modification, are permitted according to the terms *
14  * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15  *****************************************************************************/
16 
17 //////////////////////////////////////////////////////////////////////////////
18 //
19 // BEGIN_HTML
20 // RooRealConstant provides static functions to create and keep track
21 // of RooRealVar constants. Instead of creating such constants by
22 // hand (e.g. RooRealVar one("one","one",1)), simply use
23 // <pre>
24 // RooRealConstant::value(1.0)
25 // </pre>
26 // whenever a reference to RooRealVar with constant value 1.0 is needed.
27 // RooRealConstant keeps an internal database of previously created
28 // RooRealVar objects and will recycle them as appropriate.
29 // END_HTML
30 //
31 
32 #include "RooFit.h"
33 
34 #include <math.h>
35 #include <sstream>
36 #include "RooRealConstant.h"
37 #include "RooRealConstant.h"
38 #include "RooConstVar.h"
39 #include "RooArgList.h"
40 #include "RooSentinel.h"
41 
42 using namespace std;
43 
45 ;
46 
47 
48 RooArgList* RooRealConstant::_constDB = 0;
49 TIterator* RooRealConstant::_constDBIter = 0;
50 
51 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Cleanup function register with RooSentinel for cleanup in atexit()
55 
57 {
58  if (_constDB) {
59  delete _constDB ;
60  delete _constDBIter ;
61  _constDB = 0 ;
62  }
63 }
64 
65 
66 
67 ////////////////////////////////////////////////////////////////////////////////
68 /// Return a constant value object with given value.
69 /// Return previously created object if available,
70 /// otherwise create a new one on the fly.
71 
73 {
74  // Lookup existing constant
75  init() ;
76  RooConstVar* var ;
77  while((var=(RooConstVar*)_constDBIter->Next())) {
78  if ((var->getVal()==value) && (!var->getAttribute("REMOVAL_DUMMY"))) return *var ;
79  }
80 
81  // Create new constant
82  std::ostringstream s ;
83  s << value ;
84 
85  var = new RooConstVar(s.str().c_str(),s.str().c_str(),value) ;
86  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
87  _constDB->addOwned(*var) ;
88 
89  return *var ;
90 }
91 
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Create a dummy node used in node-removal operations
95 
97 {
98  RooConstVar* var = new RooConstVar("REMOVAL_DUMMY","REMOVAL_DUMMY",1) ;
99  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
100  var->setAttribute("REMOVAL_DUMMY") ;
101  _constDB->addOwned(*var) ;
102 
103  return *var ;
104 }
105 
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// One-time initialization of constants database
110 
112 {
113  if (!_constDB) {
114  _constDB = new RooArgList("RooRealVar Constants Database") ;
115  _constDBIter = _constDB->createIterator() ;
117  } else {
118  _constDBIter->Reset() ;
119  }
120 }
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:265
STL namespace.
Bool_t getAttribute(const Text_t *name) const
Check if a named attribute is set. By default, all attributes are unset.
Definition: RooAbsArg.cxx:288
Iterator abstract base class.
Definition: TIterator.h:32
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
ClassImp(RooRealConstant)
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:70
static Int_t init()
double Double_t
Definition: RtypesCore.h:55
static RooConstVar & removalDummy()
Create a dummy node used in node-removal operations.
static RooConstVar & value(Double_t value)
Return a constant value object with given value.
static void cleanup()
Cleanup function register with RooSentinel for cleanup in atexit()
const Bool_t kTRUE
Definition: Rtypes.h:91
float value
Definition: math.cpp:443
static void init()
One-time initialization of constants database.