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**/
22
23#include "RooMoment.h"
24#include "RooAbsReal.h"
25#include "RooAbsPdf.h"
26#include "RooErrorHandler.h"
27#include "RooArgSet.h"
28#include "RooMsgService.h"
29#include "RooRealVar.h"
30#include "RooFunctor.h"
31#include "RooFormulaVar.h"
32#include "RooGlobalFunc.h"
33#include "RooConstVar.h"
34#include "RooRealIntegral.h"
35#include "RooNumIntConfig.h"
36
37#include <cmath>
38#include <string>
39
40////////////////////////////////////////////////////////////////////////////////
41
42RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, bool centr, bool takeRoot) :
43 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
44 _xf("!xf","xf",this,false,false),
45 _ixf("!ixf","ixf",this),
46 _if("!if","if",this)
47{
49
50 std::string pname=Form("%s_product",name) ;
51
52 std::unique_ptr<RooFormulaVar> XF;
53 if (centr) {
54 std::string formula=Form("pow((@0-@1),%d)*@2",_order) ;
55 RooAbsReal* mom1 = func.mean(x) ;
56 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
57 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
58 addOwnedComponents(*mom1) ;
59 _mean.setArg(*mom1) ;
60 } else {
61 std::string formula=Form("pow(@0,%d)*@1",_order) ;
62 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
63 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
64 }
65
66 if (func.isBinnedDistribution(x)) {
67 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
68 }
69
70 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
71 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
72 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
73 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
74
75 _xf.setArg(*XF) ;
76 _ixf.setArg(*intXF) ;
77 _if.setArg(*intF) ;
78 addOwnedComponents(std::move(XF)) ;
79 addOwnedComponents(std::move(intXF));
80 addOwnedComponents(std::move(intF));
81}
82
83////////////////////////////////////////////////////////////////////////////////
84
85RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
86 Int_t orderIn, bool centr, bool takeRoot, bool intNSet) :
87 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
88 _xf("!xf","xf",this,false,false),
89 _ixf("!ixf","ixf",this),
90 _if("!if","if",this)
91{
93
94 _nset.add(nset) ;
95
96 std::string pname=Form("%s_product",name) ;
97 std::unique_ptr<RooFormulaVar> XF;
98 if (centr) {
99 std::string formula=Form("pow((@0-@1),%d)*@2",_order) ;
100 RooAbsReal* mom1 = func.mean(x,nset) ;
101 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
102 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
103 addOwnedComponents(*mom1) ;
104 _mean.setArg(*mom1) ;
105 } else {
106 std::string formula=Form("pow(@0,%d)*@1",_order) ;
107 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
108 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
109 }
110
111 if (func.isBinnedDistribution(x)) {
112 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
113 }
114
115 RooArgSet intSet(x) ;
116 if (intNSet) intSet.add(_nset,true) ;
117
118 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
119 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
120 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
121 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
122
123 _xf.setArg(*XF) ;
124 _ixf.setArg(*intXF) ;
125 _if.setArg(*intF) ;
126 addOwnedComponents(std::move(XF)) ;
127 addOwnedComponents(std::move(intXF));
128 addOwnedComponents(std::move(intF));
129}
130
131
132
133////////////////////////////////////////////////////////////////////////////////
134
135RooMoment::RooMoment(const RooMoment& other, const char* name) :
136 RooAbsMoment(other, name),
137 _xf("xf",this,other._xf),
138 _ixf("ixf",this,other._ixf),
139 _if("if",this,other._if)
140{
141}
142
143////////////////////////////////////////////////////////////////////////////////
144/// Calculate value
145
147{
148 double ratio = _ixf / _if ;
149 double ret = _takeRoot ? pow(ratio,1.0/_order) : ratio ;
150 return ret ;
151}
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:420
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.
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)
Abstract base class for objects that represent a real value and implements functionality common to al...
Definition RooAbsReal.h:63
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:356
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:343
RooFit::OwningPtr< RooAbsReal > createIntegral(const RooArgSet &iset, const RooCmdArg &arg1, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}) const
Create an object that represents the integral of the function over one or more observables listed in ...
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:24
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...
RooRealProxy _xf
X*F.
Definition RooMoment.h:43
RooMoment()=default
double evaluate() const override
Calculate value.
RooRealProxy _if
Int(F(x))dx ;.
Definition RooMoment.h:45
RooRealProxy _ixf
Int(X*F(X))dx ;.
Definition RooMoment.h:44
Performs hybrid numerical/analytical integrals of RooAbsReal objects.
void setCacheNumeric(bool flag)
Variable that can be changed from the outside.
Definition RooRealVar.h:37
bool setArg(T &newRef)
Change object held in proxy into newRef.
Double_t x[n]
Definition legend1.C:17