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