Logo ROOT   6.18/05
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
22RooRealConstant provides static functions to create and keep track
23of RooRealVar constants. Instead of creating such constants by
24hand (e.g. RooRealVar one("one","one",1)), simply use
25~~~{.cpp}
26 RooRealConstant::value(1.0)
27~~~
28whenever a reference to RooRealVar with constant value 1.0 is needed.
29RooRealConstant keeps an internal database of previously created
30RooRealVar 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
43using namespace std;
44
46;
47
48
50
51
52
53////////////////////////////////////////////////////////////////////////////////
54/// Cleanup function register with RooSentinel for cleanup in atexit()
55
57{
58 if (_constDB) {
59 delete _constDB ;
60 _constDB = 0 ;
61 }
62}
63
64
65
66////////////////////////////////////////////////////////////////////////////////
67/// Return a constant value object with given value.
68/// Return previously created object if available,
69/// otherwise create a new one on the fly.
70
72{
73 // Lookup existing constant
74 init() ;
75 for (auto varArg : *_constDB) {
76 auto var = static_cast<RooConstVar*>(varArg);
77 if ((var->getVal()==value) && (!var->getAttribute("REMOVAL_DUMMY"))) return *var ;
78 }
79
80 // Create new constant
81 std::ostringstream s ;
82 s << value ;
83
84 auto var = new RooConstVar(s.str().c_str(),s.str().c_str(),value) ;
85 var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
86 _constDB->addOwned(*var) ;
87
88 return *var ;
89}
90
91
92////////////////////////////////////////////////////////////////////////////////
93/// Create a dummy node used in node-removal operations
94
96{
97 RooConstVar* var = new RooConstVar("REMOVAL_DUMMY","REMOVAL_DUMMY",1) ;
98 var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
99 var->setAttribute("REMOVAL_DUMMY") ;
100 _constDB->addOwned(*var) ;
101
102 return *var ;
103}
104
105
106
107////////////////////////////////////////////////////////////////////////////////
108/// One-time initialization of constants database
109
111{
112 if (!_constDB) {
113 _constDB = new RooArgList("RooRealVar Constants Database") ;
115 }
116}
double Double_t
Definition: RtypesCore.h:55
const Bool_t kTRUE
Definition: RtypesCore.h:87
#define ClassImp(name)
Definition: Rtypes.h:365
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:256
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:21
RooConstVar represent a constant real-valued object.
Definition: RooConstVar.h:25
RooRealConstant provides static functions to create and keep track of RooRealVar constants.
static RooArgList * _constDB
static void init()
One-time initialization of constants database.
static void cleanup()
Cleanup function register with RooSentinel for cleanup in atexit()
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 activate()
Install atexit handler that calls CleanupRooFitAtExit() on program termination.
Definition: RooSentinel.cxx:73
static constexpr double s