Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 "RooGenericPdf.h"
50 #include "RooStreamParser.h"
51 #include "RooMsgService.h"
52 #include "RooArgList.h"
53 
54 
55 
56 using namespace std;
57 
58 ClassImp(RooGenericPdf);
59 
60 
61 
62 ////////////////////////////////////////////////////////////////////////////////
63 /// Constructor with formula expression and list of input variables
64 
65 RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList& dependents) :
66  RooAbsPdf(name,title),
67  _actualVars("actualVars","Variables used by PDF expression",this),
68  _formExpr(title)
69 {
70  _actualVars.add(dependents) ;
71  formula();
72 
73  if (_actualVars.getSize()==0) _value = traceEval(0) ;
74 }
75 
76 
77 
78 ////////////////////////////////////////////////////////////////////////////////
79 /// Constructor with a name, title, formula expression and a list of variables
80 
81 RooGenericPdf::RooGenericPdf(const char *name, const char *title,
82  const char* inFormula, const RooArgList& dependents) :
83  RooAbsPdf(name,title),
84  _actualVars("actualVars","Variables used by PDF expression",this),
85  _formExpr(inFormula)
86 {
87  _actualVars.add(dependents) ;
88  formula();
89 
90  if (_actualVars.getSize()==0) _value = traceEval(0) ;
91 }
92 
93 
94 
95 ////////////////////////////////////////////////////////////////////////////////
96 /// Copy constructor
97 
98 RooGenericPdf::RooGenericPdf(const RooGenericPdf& other, const char* name) :
99  RooAbsPdf(other, name),
100  _actualVars("actualVars",this,other._actualVars),
101  _formExpr(other._formExpr)
102 {
103  formula();
104 }
105 
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 
109 RooFormula& RooGenericPdf::formula() const
110 {
111  if (!_formula) {
112  const_cast<std::unique_ptr<RooFormula>&>(_formula).reset(
113  new RooFormula(GetName(),_formExpr.Data(),_actualVars));
114  const_cast<TString&>(_formExpr) = _formula->formulaString().c_str();
115  }
116  return *_formula ;
117 }
118 
119 
120 
121 ////////////////////////////////////////////////////////////////////////////////
122 /// Calculate current value of this object
123 
124 Double_t RooGenericPdf::evaluate() const
125 {
126  return formula().eval(_normSet) ;
127 }
128 
129 
130 
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Change formula expression to given expression
133 
134 Bool_t RooGenericPdf::setFormula(const char* inFormula)
135 {
136  if (formula().reCompile(inFormula)) return kTRUE ;
137 
138  _formExpr = inFormula ;
139  setValueDirty() ;
140  return kFALSE ;
141 }
142 
143 
144 
145 ////////////////////////////////////////////////////////////////////////////////
146 /// Check if given value is valid
147 
148 Bool_t RooGenericPdf::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const
149 {
150  return kTRUE ;
151 }
152 
153 
154 
155 ////////////////////////////////////////////////////////////////////////////////
156 /// Propagate server changes to embedded formula object
157 
158 Bool_t RooGenericPdf::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
159 {
160  if (_formula) {
161  return _formula->changeDependents(newServerList,mustReplaceAll,nameChange);
162  } else {
163  return kTRUE ;
164  }
165 }
166 
167 
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// Print info about this object to the specified stream.
171 
172 void RooGenericPdf::printMultiline(ostream& os, Int_t content, Bool_t verbose, TString indent) const
173 {
174  RooAbsPdf::printMultiline(os,content,verbose,indent);
175  if (verbose) {
176  os << " --- RooGenericPdf --- " << endl ;
177  indent.Append(" ");
178  os << indent ;
179  formula().printMultiline(os,content,verbose,indent);
180  }
181 }
182 
183 
184 
185 ////////////////////////////////////////////////////////////////////////////////
186 /// Add formula expression as meta argument in printing interface
187 
188 void RooGenericPdf::printMetaArgs(ostream& os) const
189 {
190  os << "formula=\"" << _formExpr << "\" " ;
191 }
192 
193 
194 
195 ////////////////////////////////////////////////////////////////////////////////
196 /// Read object contents from given stream
197 
198 Bool_t RooGenericPdf::readFromStream(istream& is, Bool_t compact, Bool_t /*verbose*/)
199 {
200  if (compact) {
201  coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read in compact mode" << endl ;
202  return kTRUE ;
203  } else {
204  RooStreamParser parser(is) ;
205  return setFormula(parser.readLine()) ;
206  }
207 }
208 
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Write object contents to given stream
212 
213 void RooGenericPdf::writeToStream(ostream& os, Bool_t compact) const
214 {
215  if (compact) {
216  os << getVal() << endl ;
217  } else {
218  os << GetTitle() ;
219  }
220 }
221 
222 
223