ROOT  6.06/09
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 //
19 // BEGIN_HTML
20 // RooGenericPdf is a concrete implementation of a probability density function,
21 // which takes a RooArgList of servers and a C++ expression string defining how
22 // its value should be calculated from the given list of servers.
23 // A fully numerical integration is automatically performed to normalize the given
24 // expression. RooGenericPdf uses a RooFormula object to perform the expression evaluation
25 //
26 // The string expression can be any valid TFormula expression referring to the
27 // listed servers either by name or by their ordinal list position:
28 //
29 // RooGenericPdf("gen","x*y",RooArgList(x,y)) or
30 // RooGenericPdf("gen","@0*@1",RooArgList(x,y))
31 //
32 // The latter form, while slightly less readable, is more versatile because it
33 // doesn't hardcode any of the variable names it expects
34 // END_HTML
35 //
36 
37 #include "RooFit.h"
38 #include "Riostream.h"
39 
40 #include "RooGenericPdf.h"
41 #include "RooGenericPdf.h"
42 #include "RooStreamParser.h"
43 #include "RooMsgService.h"
44 #include "RooArgList.h"
45 
46 
47 
48 using namespace std;
49 
51 
52 
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Constructor with formula expression and list of input variables
56 
57 RooGenericPdf::RooGenericPdf(const char *name, const char *title, const RooArgList& dependents) :
58  RooAbsPdf(name,title),
59  _actualVars("actualVars","Variables used by PDF expression",this),
60  _formula(0),
61  _formExpr(title)
62 {
63  _actualVars.add(dependents) ;
64 
65  if (_actualVars.getSize()==0) _value = traceEval(0) ;
66 }
67 
68 
69 
70 ////////////////////////////////////////////////////////////////////////////////
71 /// Constructor with a name, title, formula expression and a list of variables
72 
73 RooGenericPdf::RooGenericPdf(const char *name, const char *title,
74  const char* inFormula, const RooArgList& dependents) :
75  RooAbsPdf(name,title),
76  _actualVars("actualVars","Variables used by PDF expression",this),
77  _formula(0),
78  _formExpr(inFormula)
79 {
80  _actualVars.add(dependents) ;
81 
82  if (_actualVars.getSize()==0) _value = traceEval(0) ;
83 }
84 
85 
86 
87 ////////////////////////////////////////////////////////////////////////////////
88 /// Copy constructor
89 
90 RooGenericPdf::RooGenericPdf(const RooGenericPdf& other, const char* name) :
91  RooAbsPdf(other, name),
92  _actualVars("actualVars",this,other._actualVars),
93  _formula(0),
94  _formExpr(other._formExpr)
95 {
96 }
97 
98 
99 
100 ////////////////////////////////////////////////////////////////////////////////
101 /// Destructor
102 
104 {
105  if (_formula) delete _formula ;
106 }
107 
108 
109 
110 ////////////////////////////////////////////////////////////////////////////////
111 
113 {
114  if (!_formula) {
116  }
117  return *_formula ;
118 }
119 
120 
121 
122 ////////////////////////////////////////////////////////////////////////////////
123 /// Calculate current value of this object
124 
126 {
127  return formula().eval(_normSet) ;
128 }
129 
130 
131 
132 ////////////////////////////////////////////////////////////////////////////////
133 /// Change formula expression to given expression
134 
135 Bool_t RooGenericPdf::setFormula(const char* inFormula)
136 {
137  if (formula().reCompile(inFormula)) return kTRUE ;
138 
139  _formExpr = inFormula ;
140  setValueDirty() ;
141  return kFALSE ;
142 }
143 
144 
145 
146 ////////////////////////////////////////////////////////////////////////////////
147 /// Check if given value is valid
148 
149 Bool_t RooGenericPdf::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const
150 {
151  return kTRUE ;
152 }
153 
154 
155 
156 ////////////////////////////////////////////////////////////////////////////////
157 /// Propagate server changes to embedded formula object
158 
159 Bool_t RooGenericPdf::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
160 {
161  if (_formula) {
162  return _formula->changeDependents(newServerList,mustReplaceAll,nameChange) ;
163  } else {
164  return kTRUE ;
165  }
166 }
167 
168 
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Print info about this object to the specified stream.
172 
174 {
175  RooAbsPdf::printMultiline(os,content,verbose,indent);
176  if (verbose) {
177  os << " --- RooGenericPdf --- " << endl ;
178  indent.Append(" ");
179  os << indent ;
180  formula().printMultiline(os,content,verbose,indent);
181  }
182 }
183 
184 
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Add formula expression as meta argument in printing interface
188 
189 void RooGenericPdf::printMetaArgs(ostream& os) const
190 {
191  os << "formula=\"" << _formExpr << "\" " ;
192 }
193 
194 
195 
196 ////////////////////////////////////////////////////////////////////////////////
197 /// Read object contents from given stream
198 
199 Bool_t RooGenericPdf::readFromStream(istream& is, Bool_t compact, Bool_t /*verbose*/)
200 {
201  if (compact) {
202  coutE(InputArguments) << "RooGenericPdf::readFromStream(" << GetName() << "): can't read in compact mode" << endl ;
203  return kTRUE ;
204  } else {
205  RooStreamParser parser(is) ;
206  return setFormula(parser.readLine()) ;
207  }
208 }
209 
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// Write object contents to given stream
213 
214 void RooGenericPdf::writeToStream(ostream& os, Bool_t compact) const
215 {
216  if (compact) {
217  os << getVal() << endl ;
218  } else {
219  os << GetTitle() ;
220  }
221 }
222 
223 
224 
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:52
#define coutE(a)
Definition: RooMsgService.h:35
Basic string class.
Definition: TString.h:137
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
RooListProxy _actualVars
Definition: RooGenericPdf.h:51
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to given stream.
Double_t traceEval(const RooArgSet *set) const
Calculate current value of object, with error tracing wrapper.
Definition: RooAbsReal.cxx:288
TString _formExpr
Formula engine.
Definition: RooGenericPdf.h:62
const char * Data() const
Definition: TString.h:349
RooFormula * _formula
Definition: RooGenericPdf.h:61
TString & Append(const char *cs)
Definition: TString.h:492
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
void printMetaArgs(std::ostream &os) const
Add formula expression as meta argument in printing interface.
ClassImp(RooGenericPdf) RooGenericPdf
Constructor with formula expression and list of input variables.
Double_t eval(const RooArgSet *nset=0)
Evaluate ROOT::v5::TFormula using given normalization set to be used as observables definition passed...
Definition: RooFormula.cxx:233
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Reimplementation of standard RooArgList::add()
virtual Double_t evaluate() const
Calculate current value of this object.
virtual Bool_t isValidReal(Double_t value, Bool_t printError) const
Check if given value is valid.
void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print info about this object to the specified stream.
bool verbose
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:51
virtual ~RooGenericPdf()
Destructor.
TString readLine()
Read an entire line from the stream and return as TString This method recognizes the use of '\' in th...
static void indent(ostringstream &buf, int indent_level)
void printMultiline(std::ostream &os, Int_t contents, Bool_t verbose=kFALSE, TString indent="") const
Printing interface.
Definition: RooFormula.cxx:413
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:1611
double Double_t
Definition: RtypesCore.h:55
void setValueDirty() const
Definition: RooAbsArg.h:439
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from given stream.
#define name(a, b)
Definition: linkTestLib0.cpp:5
Double_t _value
Definition: RooAbsReal.h:389
RooAbsPdf is the abstract interface for all probability density functions The class provides hybrid a...
Definition: RooAbsPdf.h:41
virtual Bool_t redirectServersHook(const RooAbsCollection &newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t isRecursive)
Propagate server changes to embedded formula object.
Bool_t setFormula(const char *formula)
Change formula expression to given expression.
RooArgSet * _normSet
Normalization integral (owned by _normMgr)
Definition: RooAbsPdf.h:302
Bool_t changeDependents(const RooAbsCollection &newDeps, Bool_t mustReplaceAll, Bool_t nameChange)
Change used variables to those with the same name in given list If mustReplaceAll is true and error i...
Definition: RooFormula.cxx:190
RooFormula & formula() const
Int_t getSize() const
const Bool_t kTRUE
Definition: Rtypes.h:91