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
24#include "Riostream.h"
25#include <cmath>
26
27#include "RooMoment.h"
28#include "RooAbsReal.h"
29#include "RooAbsPdf.h"
30#include "RooErrorHandler.h"
31#include "RooArgSet.h"
32#include "RooMsgService.h"
33#include "RooRealVar.h"
34#include "RooFunctor.h"
35#include "RooFormulaVar.h"
36#include "RooGlobalFunc.h"
37#include "RooConstVar.h"
38#include "RooRealIntegral.h"
39#include "RooNumIntConfig.h"
40#include <string>
41
43
44////////////////////////////////////////////////////////////////////////////////
45
46RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, Int_t orderIn, bool centr, bool takeRoot) :
47 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
48 _xf("!xf","xf",this,false,false),
49 _ixf("!ixf","ixf",this),
50 _if("!if","if",this)
51{
53
54 std::string pname=Form("%s_product",name) ;
55
56 std::unique_ptr<RooFormulaVar> XF;
57 if (centr) {
58 std::string formula=Form("pow((@0-@1),%d)*@2",_order) ;
59 std::string m1name=Form("%s_moment1",GetName()) ;
60 RooAbsReal* mom1 = func.mean(x) ;
61 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
62 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
63 addOwnedComponents(*mom1) ;
64 _mean.setArg(*mom1) ;
65 } else {
66 std::string formula=Form("pow(@0,%d)*@1",_order) ;
67 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
68 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
69 }
70
71 if (func.isBinnedDistribution(x)) {
72 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
73 }
74
75 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
76 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
77 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
78 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
79
80 _xf.setArg(*XF) ;
81 _ixf.setArg(*intXF) ;
82 _if.setArg(*intF) ;
83 addOwnedComponents(std::move(XF)) ;
84 addOwnedComponents(std::move(intXF));
85 addOwnedComponents(std::move(intF));
86}
87
88////////////////////////////////////////////////////////////////////////////////
89
90RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
91 Int_t orderIn, bool centr, bool takeRoot, bool intNSet) :
92 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
93 _xf("!xf","xf",this,false,false),
94 _ixf("!ixf","ixf",this),
95 _if("!if","if",this)
96{
98
99 _nset.add(nset) ;
100
101 std::string pname=Form("%s_product",name) ;
102 std::unique_ptr<RooFormulaVar> XF;
103 if (centr) {
104 std::string formula=Form("pow((@0-@1),%d)*@2",_order) ;
105 std::string m1name=Form("%s_moment1",GetName()) ;
106 RooAbsReal* mom1 = func.mean(x,nset) ;
107 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
108 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
109 addOwnedComponents(*mom1) ;
110 _mean.setArg(*mom1) ;
111 } else {
112 std::string formula=Form("pow(@0,%d)*@1",_order) ;
113 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
114 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
115 }
116
117 if (func.isBinnedDistribution(x)) {
118 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
119 }
120
121 RooArgSet intSet(x) ;
122 if (intNSet) intSet.add(_nset,true) ;
123
124 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
125 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
126 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
127 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
128
129 _xf.setArg(*XF) ;
130 _ixf.setArg(*intXF) ;
131 _if.setArg(*intF) ;
132 addOwnedComponents(std::move(XF)) ;
133 addOwnedComponents(std::move(intXF));
134 addOwnedComponents(std::move(intF));
135}
136
137
138
139////////////////////////////////////////////////////////////////////////////////
140
141RooMoment::RooMoment(const RooMoment& other, const char* name) :
142 RooAbsMoment(other, name),
143 _xf("xf",this,other._xf),
144 _ixf("ixf",this,other._ixf),
145 _if("if",this,other._if)
146{
147}
148
149////////////////////////////////////////////////////////////////////////////////
150/// Calculate value
151
153{
154 double ratio = _ixf / _if ;
155 double ret = _takeRoot ? pow(ratio,1.0/_order) : ratio ;
156 return ret ;
157}
#define ClassImp(name)
Definition Rtypes.h:382
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:475
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:59
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 ...
RooAbsMoment * mean(RooRealVar &obs)
Definition RooAbsReal.h:360
virtual bool isBinnedDistribution(const RooArgSet &) const
Tests if the distribution is binned. Unless overridden by derived classes, this always returns false.
Definition RooAbsReal.h:347
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
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Double_t x[n]
Definition legend1.C:17