Logo ROOT  
Reference Guide
RooUniform.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitModels *
4 * @(#)root/roofit:$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/** \class RooUniform
18 \ingroup Roofit
19
20Flat p.d.f. in N dimensions
21**/
22
23#include "RooArgSet.h"
24#include "RooRealVar.h"
25#include "RooUniform.h"
26#include "RunContext.h"
27
28
30
31////////////////////////////////////////////////////////////////////////////////
32
33RooUniform::RooUniform(const char *name, const char *title, const RooArgSet& _x) :
34 RooAbsPdf(name,title),
35 x("x","Observables",this,true,false)
36{
37 x.add(_x) ;
38}
39
40////////////////////////////////////////////////////////////////////////////////
41
42RooUniform::RooUniform(const RooUniform& other, const char* name) :
43 RooAbsPdf(other,name), x("x",this,other.x)
44{
45}
46
47////////////////////////////////////////////////////////////////////////////////
48
50{
51 return 1 ;
52}
53
54////////////////////////////////////////////////////////////////////////////////
55///Compute multiple values of the uniform distribution (effectively return a span with ones)
57{
58 size_t nEvents = 1;
59 for (auto elm : x) {
60 size_t nEventsCurrent = static_cast<const RooAbsReal*>(elm)->getValues(evalData).size();
61 if(nEventsCurrent != 1 && nEvents != 1 && nEventsCurrent != nEvents) {
62 auto errorMsg = std::string("RooUniform::evaluateSpan(): number of entries for input variables does not match")
63 + "in RooUniform with name \"" + GetName() + "\".";
64 coutE(FastEvaluations) << errorMsg << std::endl ;
65 throw std::runtime_error(errorMsg);
66 }
67 nEvents = std::max(nEvents, nEventsCurrent);
68 }
69 RooSpan<double> values = evalData.makeBatch(this, nEvents);
70 for (size_t i=0; i<nEvents; i++) {
71 values[i] = 1.0;
72 }
73 return values;
74}
75
76////////////////////////////////////////////////////////////////////////////////
77/// Advertise analytical integral
78
79Int_t RooUniform::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* /*rangeName*/) const
80{
81 Int_t nx = x.getSize() ;
82 if (nx>31) {
83 // Warn that analytical integration is only provided for the first 31 observables
84 coutW(Integration) << "RooUniform::getAnalyticalIntegral(" << GetName() << ") WARNING: p.d.f. has " << x.getSize()
85 << " observables, analytical integration is only implemented for the first 31 observables" << std::endl ;
86 nx=31 ;
87 }
88
89 Int_t code(0) ;
90 for (int i=0 ; i<x.getSize() ; i++) {
91 if (allVars.find(x.at(i)->GetName())) {
92 code |= (1<<i) ;
93 analVars.add(*allVars.find(x.at(i)->GetName())) ;
94 }
95 }
96 return code ;
97}
98
99////////////////////////////////////////////////////////////////////////////////
100/// Implement analytical integral
101
102double RooUniform::analyticalIntegral(Int_t code, const char* rangeName) const
103{
104 double ret(1) ;
105 for (int i=0 ; i<32 ; i++) {
106 if (code&(1<<i)) {
108 ret *= (var->getMax(rangeName) - var->getMin(rangeName)) ;
109 }
110 }
111 return ret ;
112}
113
114////////////////////////////////////////////////////////////////////////////////
115/// Advertise internal generator
116
117Int_t RooUniform::getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, bool /*staticInitOK*/) const
118{
119 Int_t nx = x.getSize() ;
120 if (nx>31) {
121 // Warn that analytical integration is only provided for the first 31 observables
122 coutW(Integration) << "RooUniform::getGenerator(" << GetName() << ") WARNING: p.d.f. has " << x.getSize()
123 << " observables, internal integrator is only implemented for the first 31 observables" << std::endl ;
124 nx=31 ;
125 }
126
127 Int_t code(0) ;
128 for (int i=0 ; i<x.getSize() ; i++) {
129 if (directVars.find(x.at(i)->GetName())) {
130 code |= (1<<i) ;
131 generateVars.add(*directVars.find(x.at(i)->GetName())) ;
132 }
133 }
134 return code ;
135 return 0 ;
136}
137
138////////////////////////////////////////////////////////////////////////////////
139/// Implement internal generator
140
142{
143 // Fast-track handling of one-observable case
144 if (code==1) {
145 ((RooAbsRealLValue*)x.at(0))->randomize() ;
146 return ;
147 }
148
149 for (int i=0 ; i<32 ; i++) {
150 if (code&(1<<i)) {
152 var->randomize() ;
153 }
154 }
155}
#define coutW(a)
Definition: RooMsgService.h:36
#define coutE(a)
Definition: RooMsgService.h:37
#define ClassImp(name)
Definition: Rtypes.h:375
char name[80]
Definition: TGX11.cxx:110
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsRealLValue is the common abstract base class for objects that represent a real value that may a...
virtual double getMax(const char *name=nullptr) const
Get maximum of currently defined range.
void randomize(const char *rangeName=nullptr) override
Set a new value sampled from a uniform distribution over the fit range.
virtual double getMin(const char *name=nullptr) const
Get minimum of currently defined range.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:60
virtual RooSpan< const double > getValues(RooBatchCompute::RunContext &evalData, const RooArgSet *normSet=nullptr) const
Definition: RooAbsReal.cxx:279
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:56
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...
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
Flat p.d.f.
Definition: RooUniform.h:24
Int_t getAnalyticalIntegral(RooArgSet &allVars, RooArgSet &analVars, const char *rangeName=nullptr) const override
Advertise analytical integral.
Definition: RooUniform.cxx:79
double evaluate() const override
Evaluate this PDF / function / constant. Needs to be overridden by all derived classes.
Definition: RooUniform.cxx:49
double analyticalIntegral(Int_t code, const char *rangeName=nullptr) const override
Implement analytical integral.
Definition: RooUniform.cxx:102
RooSpan< double > evaluateSpan(RooBatchCompute::RunContext &evalData, const RooArgSet *=nullptr) const override
Compute multiple values of the uniform distribution (effectively return a span with ones)
Definition: RooUniform.cxx:56
RooListProxy x
Definition: RooUniform.h:40
Int_t getGenerator(const RooArgSet &directVars, RooArgSet &generateVars, bool staticInitOK=true) const override
Advertise internal generator.
Definition: RooUniform.cxx:117
void generateEvent(Int_t code) override
Implement internal generator.
Definition: RooUniform.cxx:141
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
Double_t x[n]
Definition: legend1.C:17
@ FastEvaluations
Definition: RooGlobalFunc.h:64
@ Integration
Definition: RooGlobalFunc.h:62
This struct enables passing computation data around between elements of a computation graph.
Definition: RunContext.h:32
RooSpan< double > makeBatch(const RooAbsArg *owner, std::size_t size)
Create a writable batch.
Definition: RunContext.cxx:89