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