Logo ROOT  
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 "RooConstVar.h"
39 #include "RooArgList.h"
40 
41 using namespace std;
42 
44 
45 
46 
47 ////////////////////////////////////////////////////////////////////////////////
48 /// Return a constant value object with given value.
49 /// Return previously created object if available,
50 /// otherwise create a new one on the fly.
51 
53 {
54  // Lookup existing constant
55  for (auto varArg : constDB()) {
56  auto var = static_cast<RooConstVar*>(varArg);
57  if ((var->getVal()==value) && (!var->getAttribute("REMOVAL_DUMMY"))) return *var ;
58  }
59 
60  // Create new constant
61  std::ostringstream s ;
62  s << value ;
63 
64  auto var = new RooConstVar(s.str().c_str(),s.str().c_str(),value) ;
65  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
66  constDB().addOwned(*var) ;
67 
68  return *var ;
69 }
70 
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// Create a dummy node used in node-removal operations
74 
76 {
77  RooConstVar* var = new RooConstVar("REMOVAL_DUMMY","REMOVAL_DUMMY",1) ;
78  var->setAttribute("RooRealConstant_Factory_Object",kTRUE) ;
79  var->setAttribute("REMOVAL_DUMMY") ;
80  constDB().addOwned(*var) ;
81 
82  return *var ;
83 }
84 
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// One-time initialization of constants database
89 
91 {
92  static RooArgList constDB("RooRealVar Constants Database");
93  return constDB;
94 }
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooRealConstant
Definition: RooRealConstant.h:25
RooFit.h
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooArgList
Definition: RooArgList.h:21
RooRealConstant::value
static RooConstVar & value(Double_t value)
Return a constant value object with given value.
Definition: RooRealConstant.cxx:52
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:168
RooRealConstant::constDB
static RooArgList & constDB()
One-time initialization of constants database.
Definition: RooRealConstant.cxx:90
RooConstVar
Definition: RooConstVar.h:25
RooRealConstant::removalDummy
static RooConstVar & removalDummy()
Create a dummy node used in node-removal operations.
Definition: RooRealConstant.cxx:75
RooConstVar.h
Double_t
double Double_t
Definition: RtypesCore.h:59
RooAbsArg::setAttribute
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:259
RooArgList.h
RooRealConstant.h