Logo ROOT   6.10/09
Reference Guide
RooLinearVar.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 // RooLinearVar is the most general form of a derived real-valued object that can
20 // be used by RooRealIntegral to integrate over. The requirements for this are
21 //
22 // - Can be modified directly (i.e. invertible formula)
23 // - Jacobian term in integral is constant (but not necessarily 1)
24 //
25 // This class implements the most general form that satisfy these requirement
26 //
27 // RLV = (slope)*x + (offset)
28 //
29 // X is required to be a RooRealVar to meet the invertibility criterium
30 // (slope) and (offset) is are RooAbsReals, but may not overlap with x,
31 // i.e. x may not be a server of (slope) and (offset)
32 //
33 // In the context of a dataset, (slope) may not contain any real-valued dependents
34 // (satisfied constant Jacobian requirement). This check cannot be enforced at
35 // construction time, but can be performed at run time through the isJacobianOK(depList)
36 // member function.
37 //
38 //
39 
40 #include "RooFit.h"
41 #include "Riostream.h"
42 
43 #include <math.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <ctype.h>
47 #include "TClass.h"
48 #include "TObjString.h"
49 #include "TTree.h"
50 #include "RooLinearVar.h"
51 #include "RooStreamParser.h"
52 #include "RooArgSet.h"
53 #include "RooRealVar.h"
54 #include "RooNumber.h"
55 #include "RooBinning.h"
56 #include "RooMsgService.h"
57 
58 
59 
60 using namespace std;
61 
63 
64 
65 ////////////////////////////////////////////////////////////////////////////////
66 /// Constructor with RooAbsRealLValue variable and RooAbsReal slope and offset
67 
68 RooLinearVar::RooLinearVar(const char *name, const char *title, RooAbsRealLValue& variable,
69  const RooAbsReal& slope, const RooAbsReal& offs, const char *unit) :
70  RooAbsRealLValue(name, title, unit),
71  _binning(variable.getBinning(),slope.getVal(),offs.getVal()),
72  _var("var","variable",this,variable,kTRUE,kTRUE),
73  _slope("slope","slope",this,(RooAbsReal&)slope),
74  _offset("offset","offset",this,(RooAbsReal&)offs)
75 {
76  // Slope and offset may not depend on variable
77  if (slope.dependsOnValue(variable) || offs.dependsOnValue(variable)) {
78  coutE(InputArguments) << "RooLinearVar::RooLinearVar(" << GetName()
79  << "): ERROR, slope(" << slope.GetName() << ") and offset("
80  << offs.GetName() << ") may not depend on variable("
81  << variable.GetName() << ")" << endl ;
82  assert(0) ;
83  }
84 
85  // Initial plot range and number of bins from dependent variable
86 // setPlotRange(variable.getPlotMin()*_slope + _offset,
87 // variable.getPlotMax()*_slope + _offset) ;
88 // setPlotBins(variable.getPlotBins()) ;
89 
90 }
91 
92 
93 
94 ////////////////////////////////////////////////////////////////////////////////
95 /// Copy constructor
96 
97 RooLinearVar::RooLinearVar(const RooLinearVar& other, const char* name) :
98  RooAbsRealLValue(other,name),
99  _binning(other._binning),
100  _var("var",this,other._var),
101  _slope("slope",this,other._slope),
102  _offset("offset",this,other._offset)
103 {
104 }
105 
106 
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Destructor
110 
112 {
113  _altBinning.Delete() ;
114 }
115 
116 
117 
118 ////////////////////////////////////////////////////////////////////////////////
119 /// Calculate current value of this object
120 
122 {
123  return _offset + _var * _slope ;
124 }
125 
126 
127 
128 ////////////////////////////////////////////////////////////////////////////////
129 /// Assign given value to linear transformation: sets input variable to (value-offset)/slope
130 /// If slope is zerom an error message is printed and no assignment is made
131 
133 {
134  //cout << "RooLinearVar::setVal(" << GetName() << "): new value = " << value << endl ;
135 
136  // Prevent DIV0 problems
137  if (_slope == 0.) {
138  coutE(Eval) << "RooLinearVar::setVal(" << GetName() << "): ERROR: slope is zero, cannot invert relation" << endl ;
139  return ;
140  }
141 
142  // Invert formula 'value = offset + slope*var'
143  ((RooRealVar&)_var.arg()).setVal((value - _offset) / _slope) ;
144 
145 }
146 
147 
148 
149 ////////////////////////////////////////////////////////////////////////////////
150 /// Returns true if Jacobian term associated with current
151 /// expression tree is indeed constant.
152 
154 {
155  if (!((RooAbsRealLValue&)_var.arg()).isJacobianOK(depList)) {
156  return kFALSE ;
157  }
158 
159  // Check if jacobian has no real-valued dependents
160  RooAbsArg* arg ;
161  TIterator* dIter = depList.createIterator() ;
162  while ((arg=(RooAbsArg*)dIter->Next())) {
163  if (arg->IsA()->InheritsFrom(RooAbsReal::Class())) {
164  if (_slope.arg().dependsOnValue(*arg)) {
165 // cout << "RooLinearVar::isJacobianOK(" << GetName() << ") return kFALSE because slope depends on value of " << arg->GetName() << endl ;
166  return kFALSE ;
167  }
168  }
169  }
170  delete dIter ;
171 // cout << "RooLinearVar::isJacobianOK(" << GetName() << ") return kTRUE" << endl ;
172  return kTRUE ;
173 }
174 
175 
176 
177 ////////////////////////////////////////////////////////////////////////////////
178 /// Return value of Jacobian associated with the transformation
179 
181 {
182  return _slope*((RooAbsRealLValue&)_var.arg()).jacobian() ;
183 }
184 
185 
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// Read object contents from stream
189 
190 Bool_t RooLinearVar::readFromStream(istream& /*is*/, Bool_t /*compact*/, Bool_t /*verbose*/)
191 {
192  return kTRUE ;
193 }
194 
195 
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// Write object contents to stream
199 
200 void RooLinearVar::writeToStream(ostream& os, Bool_t compact) const
201 {
202  if (compact) {
203  os << getVal() ;
204  } else {
205  os << _slope.arg().GetName() << " * " << _var.arg().GetName() << " + " << _offset.arg().GetName() ;
206  }
207 }
208 
209 
210 
211 ////////////////////////////////////////////////////////////////////////////////
212 /// Retrieve binning of this linear transformation. A RooLinearVar does not have its own
213 /// binnings but uses linearly transformed binnings of teh input variable. If a given
214 /// binning exists on the input variable, it will also exists on this linear transformation
215 /// and a binning adaptor object is created on the fly.
216 
218 {
219  // Normalization binning
220  if (name==0) {
222  return _binning ;
223  }
224 
225  // Alternative named range binnings, look for existing translator binning first
227  if (altBinning) {
228  altBinning->updateInput(((RooAbsRealLValue&)_var.arg()).getBinning(name,verbose),_slope,_offset) ;
229  return *altBinning ;
230  }
231 
232  // If binning is not found return default binning, if creation is not requested
233  if (!_var.arg().hasRange(name) && !createOnTheFly) {
234  return _binning ;
235  }
236 
237  // Create translator binning on the fly
238  RooAbsBinning& sourceBinning = ((RooAbsRealLValue&)_var.arg()).getBinning(name,verbose) ;
239  RooLinTransBinning* transBinning = new RooLinTransBinning(sourceBinning,_slope,_offset) ;
240  _altBinning.Add(transBinning) ;
241 
242  return *transBinning ;
243 }
244 
245 
246 ////////////////////////////////////////////////////////////////////////////////
247 /// Const version of getBinning()
248 
249 const RooAbsBinning& RooLinearVar::getBinning(const char* name, Bool_t verbose, Bool_t createOnTheFly) const
250 {
251  return const_cast<RooLinearVar*>(this)->getBinning(name,verbose,createOnTheFly) ;
252 }
253 
254 ////////////////////////////////////////////////////////////////////////////////
255 /// Get a list of all binning names. An empty name implies the default binning.
256 /// A 0 pointer should be passed to getBinning in this case.
257 
258 std::list<std::string> RooLinearVar::getBinningNames() const
259 {
260  std::list<std::string> binningNames(1, "");
261 
263  const RooAbsArg* binning = 0;
264  while((binning = iter.next())) {
265  const char* name = binning->GetName();
266  binningNames.push_back(name);
267  }
268 
269  return binningNames;
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// Returns true if binning with given name exists.If a given binning
274 /// exists on the input variable, it will also exists on this linear
275 /// transformation.
276 
278 {
279  return ((RooAbsRealLValue&)_var.arg()).hasBinning(name) ;
280 }
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
std::string GetName(const std::string &scope_name)
Definition: Cppyy.cxx:145
virtual ~RooLinearVar()
Destructor.
TIterator * createIterator(Bool_t dir=kIterForward) const
#define coutE(a)
Definition: RooMsgService.h:34
TObject * FindObject(const char *name) const
Return pointer to obejct with given name.
RooRealProxy _slope
Definition: RooLinearVar.h:66
void updateInput(const RooAbsBinning &input, Double_t slope=1.0, Double_t offset=0.0)
Update the slope and offset parameters and the pointer to the input binning.
virtual Double_t evaluate() const
Calculate current value of this object.
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual Bool_t readFromStream(std::istream &is, Bool_t compact, Bool_t verbose=kFALSE)
Read object contents from stream.
RooFIter fwdIterator() const
bool Bool_t
Definition: RtypesCore.h:59
virtual Bool_t isJacobianOK(const RooArgSet &depList) const
Returns true if Jacobian term associated with current expression tree is indeed constant.
STL namespace.
Iterator abstract base class.
Definition: TIterator.h:30
void Class()
Definition: Class.C:29
virtual void writeToStream(std::ostream &os, Bool_t compact) const
Write object contents to stream.
RooRealVar represents a fundamental (non-derived) real valued object.
Definition: RooRealVar.h:36
RooLinkedList _altBinning
Definition: RooLinearVar.h:64
virtual void Add(TObject *arg)
Definition: RooLinkedList.h:62
virtual const RooAbsBinning & getBinning(const char *name=0, Bool_t verbose=kTRUE, Bool_t createOnTheFly=kFALSE) const
Const version of getBinning()
RooLinTransBinning is a special binning implementation for RooLinearVar that transforms the binning o...
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:436
bool verbose
virtual void setVal(Double_t value)
Assign given value to linear transformation: sets input variable to (value-offset)/slope If slope is ...
virtual Double_t jacobian() const
Return value of Jacobian associated with the transformation.
virtual Bool_t hasRange(const char *) const
Definition: RooAbsArg.h:293
RooRealProxy _offset
Definition: RooLinearVar.h:67
RooAbsArg * next()
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsBinning is the abstract base class for RooRealVar binning definitions This class defines the in...
Definition: RooAbsBinning.h:26
void Delete(Option_t *o=0)
Remove all elements in collection and delete all elements NB: Collection does not own elements...
#define ClassImp(name)
Definition: Rtypes.h:336
RooLinTransBinning _binning
Definition: RooLinearVar.h:63
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
Bool_t dependsOnValue(const RooAbsCollection &serverList, const RooAbsArg *ignoreArg=0) const
Definition: RooAbsArg.h:88
virtual std::list< std::string > getBinningNames() const
Get a list of all binning names.
RooRealProxy _var
Definition: RooLinearVar.h:65
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual TObject * Next()=0
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const RooAbsReal & arg() const
Definition: RooRealProxy.h:43
virtual Bool_t hasBinning(const char *name) const
Returns true if binning with given name exists.If a given binning exists on the input variable...
const Bool_t kTRUE
Definition: RtypesCore.h:91