PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/root-5.34.01/roofit/roofitcore/src/RooFormulaVar.cxx

#
C++ | 245 lines | 113 code | 64 blank | 68 comment | 18 complexity | 185295115b682ebf98578527b6153f0a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause
  1. /*****************************************************************************
  2. * Project: RooFit *
  3. * Package: RooFitCore *
  4. * @(#)root/roofitcore:$Id: RooFormulaVar.cxx 44982 2012-07-10 08:36:13Z moneta $
  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. // RooRealVar is a generic implementation of a real valued object
  19. // which takes a RooArgList of servers and a C++ expression string defining how
  20. // its value should be calculated from the given list of servers.
  21. // RooRealVar uses a RooFormula object to perform the expression evaluation.
  22. //
  23. // If RooAbsPdf objects are supplied to RooRealVar as servers, their
  24. // raw (unnormalized) values will be evaluated. Use RooGenericPdf, which
  25. // constructs generic PDF functions, to access their properly normalized
  26. // values.
  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:
  30. //
  31. // RooRealVar("gen","x*y",RooArgList(x,y)) or
  32. // RooRealVar("gen","@0*@1",RooArgList(x,y))
  33. //
  34. // The latter form, while slightly less readable, is more versatile because it
  35. // doesn't hardcode any of the variable names it expects
  36. //
  37. #include "RooFit.h"
  38. #include "Riostream.h"
  39. #include "RooFormulaVar.h"
  40. #include "RooFormulaVar.h"
  41. #include "RooStreamParser.h"
  42. #include "RooNLLVar.h"
  43. #include "RooChi2Var.h"
  44. #include "RooMsgService.h"
  45. using namespace std;
  46. ClassImp(RooFormulaVar)
  47. //_____________________________________________________________________________
  48. RooFormulaVar::RooFormulaVar(const char *name, const char *title, const char* inFormula, const RooArgList& dependents) :
  49. RooAbsReal(name,title),
  50. _actualVars("actualVars","Variables used by formula expression",this),
  51. _formula(0), _formExpr(inFormula)
  52. {
  53. // Constructor with formula expression and list of input variables
  54. _actualVars.add(dependents) ;
  55. if (_actualVars.getSize()==0) _value = traceEval(0) ;
  56. }
  57. //_____________________________________________________________________________
  58. RooFormulaVar::RooFormulaVar(const char *name, const char *title, const RooArgList& dependents) :
  59. RooAbsReal(name,title),
  60. _actualVars("actualVars","Variables used by formula expression",this),
  61. _formula(0), _formExpr(title)
  62. {
  63. // Constructor with formula expression, title and list of input variables
  64. _actualVars.add(dependents) ;
  65. if (_actualVars.getSize()==0) _value = traceEval(0) ;
  66. }
  67. //_____________________________________________________________________________
  68. RooFormulaVar::RooFormulaVar(const RooFormulaVar& other, const char* name) :
  69. RooAbsReal(other, name),
  70. _actualVars("actualVars",this,other._actualVars),
  71. _formula(0), _formExpr(other._formExpr)
  72. {
  73. // Copy constructor
  74. }
  75. //_____________________________________________________________________________
  76. RooFormulaVar::~RooFormulaVar()
  77. {
  78. // Destructor
  79. if (_formula) delete _formula ;
  80. }
  81. //_____________________________________________________________________________
  82. RooFormula& RooFormulaVar::formula() const
  83. {
  84. // Return reference to internal RooFormula object
  85. if (!_formula) {
  86. _formula = new RooFormula(GetName(),_formExpr,_actualVars) ;
  87. }
  88. return *_formula ;
  89. }
  90. //_____________________________________________________________________________
  91. Double_t RooFormulaVar::evaluate() const
  92. {
  93. // Calculate current value of object from internal formula
  94. return formula().eval(_lastNSet) ;
  95. }
  96. //_____________________________________________________________________________
  97. Bool_t RooFormulaVar::isValidReal(Double_t /*value*/, Bool_t /*printError*/) const
  98. {
  99. // Check if given value is valid
  100. return kTRUE ;
  101. }
  102. //_____________________________________________________________________________
  103. Bool_t RooFormulaVar::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t /*isRecursive*/)
  104. {
  105. // Propagate server change information to embedded RooFormula object
  106. return _formula ? _formula->changeDependents(newServerList,mustReplaceAll,nameChange) : kFALSE ;
  107. }
  108. //_____________________________________________________________________________
  109. void RooFormulaVar::printMultiline(ostream& os, Int_t contents, Bool_t verbose, TString indent) const
  110. {
  111. // Print info about this object to the specified stream.
  112. RooAbsReal::printMultiline(os,contents,verbose,indent);
  113. if(verbose) {
  114. indent.Append(" ");
  115. os << indent;
  116. formula().printMultiline(os,contents,verbose,indent);
  117. }
  118. }
  119. //_____________________________________________________________________________
  120. void RooFormulaVar::printMetaArgs(ostream& os) const
  121. {
  122. // Add formula expression as meta argument in printing interface
  123. os << "formula=\"" << _formExpr << "\" " ;
  124. }
  125. //_____________________________________________________________________________
  126. Bool_t RooFormulaVar::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
  127. {
  128. // Read object contents from given stream
  129. coutE(InputArguments) << "RooFormulaVar::readFromStream(" << GetName() << "): can't read" << endl ;
  130. return kTRUE ;
  131. }
  132. //_____________________________________________________________________________
  133. void RooFormulaVar::writeToStream(ostream& os, Bool_t compact) const
  134. {
  135. // Write object contents to given stream
  136. if (compact) {
  137. cout << getVal() << endl ;
  138. } else {
  139. os << GetTitle() ;
  140. }
  141. }
  142. //_____________________________________________________________________________
  143. Double_t RooFormulaVar::defaultErrorLevel() const
  144. {
  145. // Return the default error level for MINUIT error analysis
  146. // If the formula contains one or more RooNLLVars and
  147. // no RooChi2Vars, return the defaultErrorLevel() of
  148. // RooNLLVar. If the addition contains one ore more RooChi2Vars
  149. // and no RooNLLVars, return the defaultErrorLevel() of
  150. // RooChi2Var. If the addition contains neither or both
  151. // issue a warning message and return a value of 1
  152. RooAbsReal* nllArg(0) ;
  153. RooAbsReal* chi2Arg(0) ;
  154. TIterator* iter = _actualVars.createIterator() ;
  155. RooAbsArg* arg ;
  156. while((arg=(RooAbsArg*)iter->Next())) {
  157. if (dynamic_cast<RooNLLVar*>(arg)) {
  158. nllArg = (RooAbsReal*)arg ;
  159. }
  160. if (dynamic_cast<RooChi2Var*>(arg)) {
  161. chi2Arg = (RooAbsReal*)arg ;
  162. }
  163. }
  164. delete iter ;
  165. if (nllArg && !chi2Arg) {
  166. coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName()
  167. << ") Formula contains a RooNLLVar, using its error level" << endl ;
  168. return nllArg->defaultErrorLevel() ;
  169. } else if (chi2Arg && !nllArg) {
  170. coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName()
  171. << ") Formula contains a RooChi2Var, using its error level" << endl ;
  172. return chi2Arg->defaultErrorLevel() ;
  173. } else if (!nllArg && !chi2Arg) {
  174. coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
  175. << "Formula contains neither RooNLLVar nor RooChi2Var server, using default level of 1.0" << endl ;
  176. } else {
  177. coutI(Minimization) << "RooFormulaVar::defaultErrorLevel(" << GetName() << ") WARNING: "
  178. << "Formula contains BOTH RooNLLVar and RooChi2Var server, using default level of 1.0" << endl ;
  179. }
  180. return 1.0 ;
  181. }