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