Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooMoment.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\file RooMoment.cxx
19\class RooMoment
20\ingroup Roofitcore
21
22RooMoment represents the first, second, or third order derivative
23of any RooAbsReal as calculated (numerically) by the MathCore Richardson
24derivator class.
25**/
26
27
28#include "Riostream.h"
29#include <math.h>
30
31#include "RooMoment.h"
32#include "RooAbsReal.h"
33#include "RooAbsPdf.h"
34#include "RooErrorHandler.h"
35#include "RooArgSet.h"
36#include "RooMsgService.h"
37#include "RooRealVar.h"
38#include "RooFunctor.h"
39#include "RooFormulaVar.h"
40#include "RooGlobalFunc.h"
41#include "RooConstVar.h"
42#include "RooRealIntegral.h"
43#include "RooNumIntConfig.h"
44#include <string>
45using namespace std;
46
47
49
50
51
52////////////////////////////////////////////////////////////////////////////////
53/// Default constructor
54
56{
57}
58
59
60
61////////////////////////////////////////////////////////////////////////////////
62
63RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, bool centr, bool takeRoot) :
64 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
65 _xf("!xf","xf",this,false,false),
66 _ixf("!ixf","ixf",this),
67 _if("!if","if",this)
68{
70
71 string pname=Form("%s_product",name) ;
72
73 std::unique_ptr<RooFormulaVar> XF;
74 if (centr) {
75 string formula=Form("pow((@0-@1),%d)*@2",_order) ;
76 string m1name=Form("%s_moment1",GetName()) ;
77 RooAbsReal* mom1 = func.mean(x) ;
78 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
79 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
80 addOwnedComponents(*mom1) ;
81 _mean.setArg(*mom1) ;
82 } else {
83 string formula=Form("pow(@0,%d)*@1",_order) ;
84 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
85 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
86 }
87
88 if (func.isBinnedDistribution(x)) {
89 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
90 }
91
92 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
93 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
94 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
95 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
96
97 _xf.setArg(*XF) ;
98 _ixf.setArg(*intXF) ;
99 _if.setArg(*intF) ;
100 addOwnedComponents(std::move(XF)) ;
101 addOwnedComponents(std::move(intXF));
102 addOwnedComponents(std::move(intF));
103}
104
105////////////////////////////////////////////////////////////////////////////////
106
107RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
108 Int_t orderIn, bool centr, bool takeRoot, bool intNSet) :
109 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
110 _xf("!xf","xf",this,false,false),
111 _ixf("!ixf","ixf",this),
112 _if("!if","if",this)
113{
115
116 _nset.add(nset) ;
117
118 string pname=Form("%s_product",name) ;
119 std::unique_ptr<RooFormulaVar> XF;
120 if (centr) {
121 string formula=Form("pow((@0-@1),%d)*@2",_order) ;
122 string m1name=Form("%s_moment1",GetName()) ;
123 RooAbsReal* mom1 = func.mean(x,nset) ;
124 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
125 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
126 addOwnedComponents(*mom1) ;
127 _mean.setArg(*mom1) ;
128 } else {
129 string formula=Form("pow(@0,%d)*@1",_order) ;
130 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
131 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
132 }
133
134 if (func.isBinnedDistribution(x)) {
135 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
136 }
137
138 RooArgSet intSet(x) ;
139 if (intNSet) intSet.add(_nset,true) ;
140
141 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
142 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
143 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
144 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
145
146 _xf.setArg(*XF) ;
147 _ixf.setArg(*intXF) ;
148 _if.setArg(*intF) ;
149 addOwnedComponents(std::move(XF)) ;
150 addOwnedComponents(std::move(intXF));
151 addOwnedComponents(std::move(intF));
152}
153
154
155
156////////////////////////////////////////////////////////////////////////////////
157
158RooMoment::RooMoment(const RooMoment& other, const char* name) :
159 RooAbsMoment(other, name),
160 _xf("xf",this,other._xf),
161 _ixf("ixf",this,other._ixf),
162 _if("if",this,other._if)
163{
164}
165
166
167
168////////////////////////////////////////////////////////////////////////////////
169/// Destructor
170
172{
173}
174
175
176
177////////////////////////////////////////////////////////////////////////////////
178/// Calculate value
179
181{
182 double ratio = _ixf / _if ;
183 double ret = _takeRoot ? pow(ratio,1.0/_order) : ratio ;
184 return ret ;
185}
186
187
#define ClassImp(name)
Definition Rtypes.h:377
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2467
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:500
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsMoment represents the first, second, or third order derivative of any RooAbsReal as calculated ...
RooRealProxy _mean
Mean (if calculated for central moment)
Int_t _order
Moment order.
Int_t _takeRoot
Return n-order root of moment.
RooSetProxy _nset
Normalization set (optional)
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:374
RooFit::OwningPtr< RooAbsReal > createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2=RooCmdArg::none(), const RooCmdArg &arg3=RooCmdArg::none(), const RooCmdArg &arg4=RooCmdArg::none(), const RooCmdArg &arg5=RooCmdArg::none(), const RooCmdArg &arg6=RooCmdArg::none(), const RooCmdArg &arg7=RooCmdArg::none(), const RooCmdArg &arg8=RooCmdArg::none()) const
Create an object that represents the integral of the function over one or more observables std::liste...
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:358
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
bool add(const RooAbsArg &var, bool valueServer, bool shapeServer, bool silent)
Overloaded RooCollection_t::add() method insert object into set and registers object as server to own...
RooMoment represents the first, second, or third order derivative of any RooAbsReal as calculated (nu...
Definition RooMoment.h:27
RooRealProxy _xf
X*F.
Definition RooMoment.h:45
~RooMoment() override
Destructor.
double evaluate() const override
Calculate value.
RooRealProxy _if
Int(F(x))dx ;.
Definition RooMoment.h:47
RooMoment()
Default constructor.
Definition RooMoment.cxx:55
RooRealProxy _ixf
Int(X*F(X))dx ;.
Definition RooMoment.h:46
RooRealIntegral performs hybrid numerical/analytical integrals of RooAbsReal objects.
void setCacheNumeric(bool flag)
RooRealVar represents a variable that can be changed from the outside.
Definition RooRealVar.h:40
bool setArg(T &newRef)
Change object held in proxy into newRef.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t x[n]
Definition legend1.C:17