Logo ROOT  
Reference Guide
RooGenericPdf.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 RooGenericPdf.cxx
19 \class RooGenericPdf
20 \ingroup Roofitcore
21 
22 RooGenericPdf is a concrete implementation of a probability density function,
23 which takes a RooArgList of servers and a C++ expression string defining how
24 its value should be calculated from the given list of servers.
25 A fully numerical integration is automatically performed to normalize the given
26 expression. RooGenericPdf uses a RooFormula object to perform the expression evaluation.
27 
28 The string expression can be any valid TFormula expression referring to the
29 listed servers either by name or by their ordinal list position. These three are
30 equivalent:
31 ```
32  RooFormulaVar("gen", "x*y", RooArgList(x,y)) // reference by name
33  RooFormulaVar("gen", "@0*@1", RooArgList(x,y)) // reference by ordinal with @
34  RooFormulaVar("gen", "x[0]*x[1]", RooArgList(x,y)) // TFormula-builtin reference by ordinal
35 ```
36 Note that `x[i]` is an expression reserved for TFormula. All variable references
37 are automatically converted to the TFormula-native format. If a variable with
38 the name `x` is given, the RooFormula interprets `x[i]` as a list position,
39 but `x` without brackets as the name of a RooFit object.
40 
41 The last two versions, while slightly less readable, are more versatile because
42 the names of the arguments are not hard coded.
43 **/
44 
45 #include "RooFit.h"
46 #include "Riostream.h"
47 
48 #include "RooGenericPdf.h"
49 #include "RooStreamParser.h"
50 #include "RooMsgService.h"
51 #include "RooArgList.h"
52 
53 
54 
55 using namespace std;
56 
58 
59 
60 
61 ////////////////////////////////////////////////////////////////////////////////
62 /// Constructor with formula expression and list of input variables
63 
64 RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList& dependents) :
65  RooAbsPdf(name,title),
66  _actualVars("actualVars","Variables used by PDF expression",this),
67  _formExpr(title)
68 {
69  _actualVars.add(dependents) ;
70  formula();
71 
72  if (_actualVars.getSize()==0) _value = traceEval(0) ;
73 }
74 
75 
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// Constructor with a name, title, formula expression and a list of variables
79 
80 RooGenericPdf::RooGenericPdf(const char *name, const char *title,
81  const char* inFormula, const RooArgList& dependents) :
82  RooAbsPdf(name,title),
83  _actualVars("actualVars","Variables used by PDF expression",this),
84  _formExpr(inFormula)
85 {
86  _actualVars.add(dependents) ;
87  formula();
88 
89  if (_actualVars.getSize()==0) _value = traceEval(0) ;
90 }
91 
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Copy constructor
96 
97 RooGenericPdf::RooGenericPdf(const RooGenericPdf& other, const char* name) :
98  RooAbsPdf(other, name),
99  _actualVars("actualVars",this,other._actualVars),
100  _formExpr(other._formExpr)
101 {
102  formula();
103 }
104 
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 
109 {
110  if (!_formula) {
111  const_cast<std::unique_ptr<RooFormula>&>(_formula).reset(
113  const_cast<TString&>(_formExpr) = _formula->formulaString().c_str();
114  }
115  return *_formula ;
116 }
117 
118 
119 
120 ////////////////////////////////////////////////////////////////////////////////
121 /// Calculate current value of this object
122 
124 {
125  return formula().eval(_normSet) ;
126 }
127 
128 
129 
130 ////////////////////////////////////////////////////////////////////////////////
131 /// Change formula expression to given expression
132 
133 Bool_t RooGenericPdf::setFormula(const char* inFormula)
134 {
135  if (formula().reCompile(inFormula)) return kTRUE ;
136 
137  _formExpr = inFormula ;
138  setValueDirty() ;
139  return kFALSE ;
140 }
141 
142 
143 
144 ////////////////////////////////////////////////////////////////////////////////
145 /// Check if given value is valid
146 
147 Bool_t RooGenericPdf::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const
148 {
149  return kTRUE ;
150 }
151 
152 
153 
154 ////////////////////////////////////////////////////////////////////////////////
155 /// Propagate server changes to embedded formula object
156 
157 Bool_t RooGenericPdf::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
158 {
159  if (_formula) {
160  return _formula->changeDependents(newServerList,mustReplaceAll,nameChange);
161  } else {
162  return kTRUE ;
163  }
164 }
165 
166 
167 
168 ////////////////////////////////////////////////////////////////////////////////
169 /// Print info about this object to the specified stream.
170 
172 {
174  if (verbose) {
175  os << " --- RooGenericPdf --- " << endl ;
176  indent.Append(" ");
177  os << indent ;
178  formula().printMultiline(os,content,verbose,indent);
179  }
180 }
181 
182 
183 
184 ////////////////////////////////////////////////////////////////////////////////
185 /// Add formula expression as meta argument in printing interface
186 
187 void RooGenericPdf::printMetaArgs(ostream& os) const
188 {
189  os << "formula=\"" << _formExpr << "\" " ;
190 }
191 
192 
193 
194 ////////////////////////////////////////////////////////////////////////////////
195 /// Read object contents from given stream
196 
197 Bool_t RooGenericPdf::readFromStream(istream& is, Bool_t compact, Bool_t /*verbose*/)
198 {
199  if (compact) {
200  coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read in compact mode" << endl ;
201  return kTRUE ;
202  } else {
203  RooStreamParser parser(is) ;
204  return setFormula(parser.readLine()) ;
205  }
206 }
207 
208 
209 ////////////////////////////////////////////////////////////////////////////////
210 /// Write object contents to given stream
211 
212 void RooGenericPdf::writeToStream(ostream& os, Bool_t compact) const
213 {
214  if (compact) {
215  os << getVal() << endl ;
216  } else {
217  os << GetTitle() ;
218  }
219 }
220 
221 
222 
RooStreamParser.h
RooAbsPdf::_normSet
RooArgSet * _normSet
Normalization integral (owned by _normMgr)
Definition: RooAbsPdf.h:321
RooFormula
Definition: RooFormula.h:28
RooGenericPdf::_actualVars
RooListProxy _actualVars
Definition: RooGenericPdf.h:50
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooMsgService.h
RooGenericPdf::evaluate
virtual Double_t evaluate() const
Calculate current value of this object.
Definition: RooGenericPdf.cxx:123
RooFit.h
RooFit::InputArguments
@ InputArguments
Definition: RooGlobalFunc.h:68
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TNamed::GetTitle
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:54
coutE
#define coutE(a)
Definition: RooMsgService.h:33
RooArgList
Definition: RooArgList.h:21
RooGenericPdf::writeToStream
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
Definition: RooGenericPdf.cxx:212
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooGenericPdf::formula
RooFormula & formula() const
Definition: RooGenericPdf.cxx:108
RooAbsPdf::printMultiline
virtual void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Print multi line detailed information of this RooAbsPdf.
Definition: RooAbsPdf.cxx:1978
RooGenericPdf::_formExpr
TString _formExpr
Formula engine.
Definition: RooGenericPdf.h:61
RooGenericPdf::printMetaArgs
void printMetaArgs(std::ostream &os) const
Add formula expression as meta argument in printing interface.
Definition: RooGenericPdf.cxx:187
indent
static void indent(ostringstream &buf, int indent_level)
Definition: TClingCallFunc.cxx:87
TString
Definition: TString.h:136
RooGenericPdf.h
RooGenericPdf::printMultiline
void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
Definition: RooGenericPdf.cxx:171
RooGenericPdf::readFromStream
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
Definition: RooGenericPdf.cxx:197
bool
RooStreamParser
Definition: RooStreamParser.h:21
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooGenericPdf::setFormula
Bool_t setFormula(const char *formula)
Change formula expression to given expression.
Definition: RooGenericPdf.cxx:133
RooAbsCollection
Definition: RooAbsCollection.h:30
RooGenericPdf::RooGenericPdf
RooGenericPdf()
Definition: RooGenericPdf.h:28
RooListProxy::add
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
Definition: RooListProxy.cxx:104
RooAbsReal::_value
Double_t _value
Definition: RooAbsReal.h:450
RooGenericPdf::redirectServersHook
virtual Bool_t redirectServersHook(const RooAbsCollection &newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t isRecursive)
Propagate server changes to embedded formula object.
Definition: RooGenericPdf.cxx:157
RooStreamParser::readLine
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of '\' in th...
Definition: RooStreamParser.cxx:309
RooAbsReal::traceEval
Double_t traceEval(const RooArgSet *set) const
Calculate current value of object, with error tracing wrapper.
Definition: RooAbsReal.cxx:340
Double_t
double Double_t
Definition: RtypesCore.h:59
RooGenericPdf
Definition: RooGenericPdf.h:25
name
char name[80]
Definition: TGX11.cxx:110
RooGenericPdf::isValidReal
virtual Bool_t isValidReal(Double_t value, Bool_t printError) const
Check if given value is valid.
Definition: RooGenericPdf.cxx:147
genreflex::verbose
bool verbose
Definition: rootcling_impl.cxx:133
RooFormula::printMultiline
void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Printing interface.
Definition: RooFormula.cxx:369
RooAbsArg::setValueDirty
void setValueDirty()
Mark the element dirty. This forces a re-evaluation when a value is requested.
Definition: RooAbsArg.h:505
RooAbsPdf
Definition: RooAbsPdf.h:40
TNamed::GetName
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:53
Riostream.h
RooFormula::eval
Double_t eval(const RooArgSet *nset=0) const
Evalute all parameters/observables, and then evaluate formula.
Definition: RooFormula.cxx:340
RooArgList.h
RooAbsCollection::getSize
Int_t getSize() const
Definition: RooAbsCollection.h:171
int
RooGenericPdf::_formula
std::unique_ptr< RooFormula > _formula
Definition: RooGenericPdf.h:60