Logo ROOT   6.08/07
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 \file RooRealConstant.cxx
19 \class RooRealConstant
20 \ingroup Roofitcore
21 
22 RooRealConstant provides static functions to create and keep track
23 of RooRealVar constants. Instead of creating such constants by
24 hand (e.g. RooRealVar one("one","one",1)), simply use
25 ~~~{.cpp}
26  RooRealConstant::value(1.0)
27 ~~~
28 whenever a reference to RooRealVar with constant value 1.0 is needed.
29 RooRealConstant keeps an internal database of previously created
30 RooRealVar objects and will recycle them as appropriate.
31 **/
32 
33 #include "RooFit.h"
34 
35 #include <math.h>
36 #include <sstream>
37 #include "RooRealConstant.h"
38 #include "RooRealConstant.h"
39 #include "RooConstVar.h"
40 #include "RooArgList.h"
41 #include "RooSentinel.h"
42 
43 using namespace std;
44 
46 ;
47 
48 
51 
52 
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Cleanup function register with RooSentinel for cleanup in atexit()
56 
58 {
59  if (_constDB) {
60  delete _constDB ;
61  delete _constDBIter ;
62  _constDB = 0 ;
63  }
64 }
65 
66 
67 
68 ////////////////////////////////////////////////////////////////////////////////
69 /// Return a constant value object with given value.
70 /// Return previously created object if available,
71 /// otherwise create a new one on the fly.
72 
74 {
75  // Lookup existing constant
76  init() ;
77  RooConstVar* var ;
78  while((var=(RooConstVar*)_constDBIter->Next())) {
79  if ((var->getVal()==value) && (!var->getAttribute("REMOVAL_DUMMY"))) return *var ;
80  }
81 
82  // Create new constant
83  std::ostringstream s ;
84  s << value ;
85 
86  var = new RooConstVar(s.str().c_str(),s.str().c_str(),value) ;
87  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
88  _constDB->addOwned(*var) ;
89 
90  return *var ;
91 }
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Create a dummy node used in node-removal operations
96 
98 {
99  RooConstVar* var = new RooConstVar("REMOVAL_DUMMY","REMOVAL_DUMMY",1) ;
100  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
101  var->setAttribute("REMOVAL_DUMMY") ;
102  _constDB->addOwned(*var) ;
103 
104  return *var ;
105 }
106 
107 
108 
109 ////////////////////////////////////////////////////////////////////////////////
110 /// One-time initialization of constants database
111 
113 {
114  if (!_constDB) {
115  _constDB = new RooArgList("RooRealVar Constants Database") ;
116  _constDBIter = _constDB->createIterator() ;
118  } else {
119  _constDBIter->Reset() ;
120  }
121 }
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
static TIterator * _constDBIter
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
STL namespace.
RooRealConstant provides static functions to create and keep track of RooRealVar constants.
Iterator abstract base class.
Definition: TIterator.h:32
RooConstVar represent a constant real-valued object.
Definition: RooConstVar.h:25
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
static void activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:71
#define ClassImp(name)
Definition: Rtypes.h:279
static Int_t init()
double Double_t
Definition: RtypesCore.h:55
static RooArgList * _constDB
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
static void init()
One-time initialization of constants database.