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 std::string m1name=Form("%s_moment1",GetName()) ;
56 RooAbsReal* mom1 = func.mean(x) ;
57 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
58 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
60 _mean.setArg(*mom1) ;
61 } else {
62 std::string formula=Form("pow(@0,%d)*@1",_order) ;
63 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
64 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
65 }
66
67 if (func.isBinnedDistribution(x)) {
68 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
69 }
70
71 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(x)};
72 std::unique_ptr<RooAbsReal> intF{func.createIntegral(x)};
73 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
74 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
75
76 _xf.setArg(*XF) ;
77 _ixf.setArg(*intXF) ;
78 _if.setArg(*intF) ;
79 addOwnedComponents(std::move(XF)) ;
80 addOwnedComponents(std::move(intXF));
81 addOwnedComponents(std::move(intF));
82}
83
84////////////////////////////////////////////////////////////////////////////////
85
86RooMoment::RooMoment(const char* name, const char* title, RooAbsReal& func, RooRealVar& x, const RooArgSet& nset,
87 Int_t orderIn, bool centr, bool takeRoot, bool intNSet) :
88 RooAbsMoment(name, title,func,x,orderIn,takeRoot),
89 _xf("!xf","xf",this,false,false),
90 _ixf("!ixf","ixf",this),
91 _if("!if","if",this)
92{
94
95 _nset.add(nset) ;
96
97 std::string pname=Form("%s_product",name) ;
98 std::unique_ptr<RooFormulaVar> XF;
99 if (centr) {
100 std::string formula=Form("pow((@0-@1),%d)*@2",_order) ;
101 std::string m1name=Form("%s_moment1",GetName()) ;
102 RooAbsReal* mom1 = func.mean(x,nset) ;
103 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgList(x,*mom1,func)) ;
104 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
106 _mean.setArg(*mom1) ;
107 } else {
108 std::string formula=Form("pow(@0,%d)*@1",_order) ;
109 XF = std::make_unique<RooFormulaVar>(pname.c_str(),formula.c_str(),RooArgSet(x,func)) ;
110 XF->setExpensiveObjectCache(func.expensiveObjectCache()) ;
111 }
112
113 if (func.isBinnedDistribution(x)) {
114 XF->specialIntegratorConfig(true)->method1D().setLabel("RooBinIntegrator");
115 }
116
118 if (intNSet) intSet.add(_nset,true) ;
119
120 std::unique_ptr<RooAbsReal> intXF{XF->createIntegral(intSet, &_nset)};
121 std::unique_ptr<RooAbsReal> intF{func.createIntegral(intSet, &_nset)};
122 static_cast<RooRealIntegral&>(*intXF).setCacheNumeric(true) ;
123 static_cast<RooRealIntegral&>(*intF).setCacheNumeric(true) ;
124
125 _xf.setArg(*XF) ;
126 _ixf.setArg(*intXF) ;
127 _if.setArg(*intF) ;
128 addOwnedComponents(std::move(XF)) ;
129 addOwnedComponents(std::move(intXF));
130 addOwnedComponents(std::move(intF));
131}
132
133
134
135////////////////////////////////////////////////////////////////////////////////
136
139 _xf("xf",this,other._xf),
140 _ixf("ixf",this,other._ixf),
141 _if("if",this,other._if)
142{
143}
144
145////////////////////////////////////////////////////////////////////////////////
146/// Calculate value
147
149{
150 double ratio = _ixf / _if ;
151 double ret = _takeRoot ? pow(ratio,1.0/_order) : ratio ;
152 return ret ;
153}
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2570
RooExpensiveObjectCache & expensiveObjectCache() const
virtual void setExpensiveObjectCache(RooExpensiveObjectCache &cache)
Definition RooAbsArg.h:418
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
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.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:49
Double_t x[n]
Definition legend1.C:17