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