RooFormulaVar.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooFormulaVar.cxx 28259 2009-04-16 16:21:16Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 //
00019 // RooRealVar is a generic implementation of a real valued object
00020 // which takes a RooArgList of servers and a C++ expression string defining how
00021 // its value should be calculated from the given list of servers.
00022 // RooRealVar uses a RooFormula object to perform the expression evaluation.
00023 //
00024 // If RooAbsPdf objects are supplied to RooRealVar as servers, their
00025 // raw (unnormalized) values will be evaluated. Use RooGenericPdf, which
00026 // constructs generic PDF functions, to access their properly normalized
00027 // values.
00028 //
00029 // The string expression can be any valid TFormula expression referring to the
00030 // listed servers either by name or by their ordinal list position:
00031 //
00032 //   RooRealVar("gen","x*y",RooArgList(x,y))  or
00033 //   RooRealVar("gen","@0*@1",RooArgList(x,y)) 
00034 //
00035 // The latter form, while slightly less readable, is more versatile because it
00036 // doesn't hardcode any of the variable names it expects
00037 //
00038 
00039 
00040 #include "RooFit.h"
00041 #include "Riostream.h"
00042 
00043 #include "RooFormulaVar.h"
00044 #include "RooFormulaVar.h"
00045 #include "RooStreamParser.h"
00046 #include "RooNLLVar.h"
00047 #include "RooChi2Var.h"
00048 #include "RooMsgService.h"
00049 
00050 
00051 
00052 ClassImp(RooFormulaVar)
00053 
00054 
00055 
00056 //_____________________________________________________________________________
00057 RooFormulaVar::RooFormulaVar(const char *name, const char *title, const char* inFormula, const RooArgList& dependents) : 
00058   RooAbsReal(name,title), 
00059   _actualVars("actualVars","Variables used by formula expression",this),
00060   _formula(0), _formExpr(inFormula)
00061 {  
00062   // Constructor with formula expression and list of input variables
00063 
00064   _actualVars.add(dependents) ; 
00065 
00066   if (_actualVars.getSize()==0) _value = traceEval(0) ;
00067 }
00068 
00069 
00070 
00071 //_____________________________________________________________________________
00072 RooFormulaVar::RooFormulaVar(const char *name, const char *title, const RooArgList& dependents) : 
00073   RooAbsReal(name,title),
00074   _actualVars("actualVars","Variables used by formula expression",this),
00075   _formula(0), _formExpr(title)
00076 {  
00077   // Constructor with formula expression, title and list of input variables
00078 
00079   _actualVars.add(dependents) ; 
00080 
00081   if (_actualVars.getSize()==0) _value = traceEval(0) ;
00082 }
00083 
00084 
00085 
00086 //_____________________________________________________________________________
00087 RooFormulaVar::RooFormulaVar(const RooFormulaVar& other, const char* name) : 
00088   RooAbsReal(other, name), 
00089   _actualVars("actualVars",this,other._actualVars),
00090   _formula(0), _formExpr(other._formExpr)
00091 {
00092   // Copy constructor
00093 }
00094 
00095 
00096 
00097 //_____________________________________________________________________________
00098 RooFormulaVar::~RooFormulaVar() 
00099 {
00100   // Destructor
00101 
00102   if (_formula) delete _formula ;
00103 }
00104 
00105 
00106 
00107 //_____________________________________________________________________________
00108 RooFormula& RooFormulaVar::formula() const
00109 {
00110   // Return reference to internal RooFormula object
00111 
00112   if (!_formula) {
00113     _formula = new RooFormula(GetName(),_formExpr,_actualVars) ;    
00114   }
00115   return *_formula ;
00116 }
00117 
00118 
00119 
00120 //_____________________________________________________________________________
00121 Double_t RooFormulaVar::getVal(const RooArgSet* set) const 
00122 {
00123   // Overload RooAbsReal::getVal() to intercept nset pointer.
00124 
00125   _nset = (RooArgSet*) set ;
00126   return RooAbsReal::getVal(set) ;
00127 }
00128 
00129 
00130 
00131 //_____________________________________________________________________________
00132 Double_t RooFormulaVar::evaluate() const
00133 {
00134   // Calculate current value of object from internal formula
00135   return formula().eval(_nset) ;
00136 }
00137 
00138 
00139 
00140 //_____________________________________________________________________________
00141 Bool_t RooFormulaVar::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const 
00142 {
00143   // Check if given value is valid
00144   return kTRUE ;
00145 }
00146 
00147 
00148 
00149 //_____________________________________________________________________________
00150 Bool_t RooFormulaVar::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
00151 {
00152   // Propagate server change information to embedded RooFormula object
00153   return _formula ? _formula->changeDependents(newServerList,mustReplaceAll,nameChange) : kFALSE ;
00154 }
00155 
00156 
00157 
00158 //_____________________________________________________________________________
00159 void RooFormulaVar::printMultiline(ostream& os, Int_t contents, Bool_t verbose, TString indent) const
00160 {
00161   // Print info about this object to the specified stream.   
00162 
00163   RooAbsReal::printMultiline(os,contents,verbose,indent);
00164   if(verbose) {
00165     indent.Append("  ");
00166     os << indent;
00167     formula().printMultiline(os,contents,verbose,indent);
00168   }
00169 }
00170 
00171 
00172 
00173 //_____________________________________________________________________________
00174 void RooFormulaVar::printMetaArgs(ostream& os) const 
00175 {
00176   // Add formula expression as meta argument in printing interface
00177   os << "formula=\"" << _formExpr << "\" " ;
00178 }
00179 
00180 
00181 
00182 
00183 //_____________________________________________________________________________
00184 Bool_t RooFormulaVar::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
00185 {
00186   // Read object contents from given stream
00187 
00188   coutE(InputArguments) << "RooFormulaVar::readFromStream(" << GetName() << "): can't read" << endl ;
00189   return kTRUE ;
00190 }
00191 
00192 
00193 
00194 //_____________________________________________________________________________
00195 void RooFormulaVar::writeToStream(ostream& os, Bool_t compact) const
00196 {
00197   // Write object contents to given stream
00198 
00199   if (compact) {
00200     cout << getVal() << endl ;
00201   } else {
00202     os << GetTitle() ;
00203   }
00204 }
00205 
00206 
00207 //_____________________________________________________________________________
00208 Double_t RooFormulaVar::defaultErrorLevel() const 
00209 {
00210   // Return the default error level for MINUIT error analysis
00211   // If the formula contains one or more RooNLLVars and 
00212   // no RooChi2Vars, return the defaultErrorLevel() of
00213   // RooNLLVar. If the addition contains one ore more RooChi2Vars
00214   // and no RooNLLVars, return the defaultErrorLevel() of
00215   // RooChi2Var. If the addition contains neither or both
00216   // issue a warning message and return a value of 1
00217 
00218   RooAbsReal* nllArg(0) ;
00219   RooAbsReal* chi2Arg(0) ;
00220 
00221   TIterator* iter = _actualVars.createIterator() ;
00222   RooAbsArg* arg ;
00223   while((arg=(RooAbsArg*)iter->Next())) {
00224     if (dynamic_cast<RooNLLVar*>(arg)) {
00225       nllArg = (RooAbsReal*)arg ;
00226     }
00227     if (dynamic_cast<RooChi2Var*>(arg)) {
00228       chi2Arg = (RooAbsReal*)arg ;
00229     }
00230   }
00231   delete iter ;
00232 
00233   if (nllArg && !chi2Arg) {
00234     coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() 
00235                         << ") Formula contains a RooNLLVar, using its error level" << endl ;
00236     return nllArg->defaultErrorLevel() ;
00237   } else if (chi2Arg && !nllArg) {
00238     coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() 
00239          << ") Formula contains a RooChi2Var, using its error level" << endl ;
00240     return chi2Arg->defaultErrorLevel() ;
00241   } else if (!nllArg && !chi2Arg) {
00242     coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
00243                       << "Formula contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << endl ;
00244   } else {
00245     coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
00246                         << "Formula contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << endl ;
00247   }
00248 
00249   return 1.0 ;
00250 }
00251 
00252 
00253 
00254 

Generated on Tue Jul 5 15:06:40 2011 for ROOT_528-00b_version by  doxygen 1.5.1