Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooBinWidthFunction.cxx
Go to the documentation of this file.
1// Author Stephan Hageboeck, CERN, 10/2020
2/*****************************************************************************
3 * Project: RooFit *
4 * Package: RooFitCore *
5 * File: $Id$
6 * Authors: *
7 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
8 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
9 * *
10 * Copyright (c) 2000-2020, Regents of the University of California *
11 * and Stanford University. All rights reserved. *
12 * *
13 * Redistribution and use in source and binary forms, *
14 * with or without modification, are permitted according to the terms *
15 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
16 *****************************************************************************/
17
18
19/**
20 * \class RooBinWidthFunction
21 *
22 * RooBinWidthFunction is a class that returns the bin width (or volume) given a RooHistFunc.
23 * It can be used to normalise by bin width or to compute event densities. Using the extra
24 * argument of the constructor, it can also return the inverse of the bin width (or volume).
25 */
26
27#include "RooBinWidthFunction.h"
28
29#include "RooConstVar.h"
30#include "RooDataHist.h"
31#include "RooGlobalFunc.h"
32
34
35/// Globally enable bin-width corrections by this class.
37 _enabled = true;
38}
39
40/// Returns `true` if bin-width corrections by this class are globally enabled, `false` otherwise.
42 return _enabled;
43}
44
45/// Globally disable bin-width corrections by this class.
47 _enabled = false;
48}
49
50/// Compute current bin of observable, and return its volume or inverse volume, depending
51/// on configuration chosen in the constructor.
52/// If the bin is not valid, return a volume of 1.
54 if(!_enabled) return 1.;
55 const RooDataHist& dataHist = _histFunc->dataHist();
56 const auto idx = _histFunc->getBin();
57 auto volumes = dataHist.binVolumes(0, dataHist.numEntries());
58 const double volume = idx >= 0 ? volumes[idx] : 1.;
59
60 return _divideByBinWidth ? 1./volume : volume;
61}
62
63
64/// Compute bin index for all values of the observable(s) in `evalData`, and return their volumes or inverse volumes, depending
65/// on the configuration chosen in the constructor.
66/// If a bin is not valid, return a volume of 1.
67void RooBinWidthFunction::computeBatch(cudaStream_t*, double* output, size_t, RooFit::Detail::DataMap const& dataMap) const {
68 const RooDataHist& dataHist = _histFunc->dataHist();
69 std::vector<Int_t> bins = _histFunc->getBins(dataMap);
70 auto volumes = dataHist.binVolumes(0, dataHist.numEntries());
71
72 if(!_enabled){
73 for (std::size_t i=0; i < bins.size(); ++i) {
74 output[i] = 1.;
75 }
76 } else {
78 for (std::size_t i=0; i < bins.size(); ++i) {
79 output[i] = bins[i] >= 0 ? 1./volumes[bins[i]] : 1.;
80 }
81 } else {
82 for (std::size_t i=0; i < bins.size(); ++i) {
83 output[i] = bins[i] >= 0 ? volumes[bins[i]] : 1.;
84 }
85 }
86 }
87}
88
89
90std::unique_ptr<RooAbsArg>
92{
93 // If this is a binned likelihood, the pdf values can be directly
94 // interpreted as yields for Poisson terms in the NLL, and it doesn't make
95 // sense to divide them by the bin width to get a probability density. The
96 // NLL would only have to multiply by the bin with again.
97 if (ctx.binnedLikelihoodMode()) {
98 auto newArg = std::unique_ptr<RooAbsReal>{static_cast<RooAbsReal *>(RooFit::RooConst(1.0).Clone())};
99 ctx.markAsCompiled(*newArg);
100 // To propagate the information to the NLL that the pdf values can
101 // directly be interpreted as yields.
102 ctx.setBinWidthFuncFlag(true);
103 return newArg;
104 }
105 return RooAbsReal::compileForNormSet(normSet, ctx);
106}
virtual std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const
TObject * Clone(const char *newname=nullptr) const override
Make a clone of an object using the Streamer facility.
Definition RooAbsArg.h:86
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
std::unique_ptr< RooAbsArg > compileForNormSet(RooArgSet const &normSet, RooFit::Detail::CompileContext &ctx) const override
void computeBatch(cudaStream_t *, double *output, size_t size, RooFit::Detail::DataMap const &) const override
Compute bin index for all values of the observable(s) in evalData, and return their volumes or invers...
static void disableClass()
Globally disable bin-width corrections by this class.
double evaluate() const override
Compute current bin of observable, and return its volume or inverse volume, depending on configuratio...
RooTemplateProxy< const RooHistFunc > _histFunc
static bool isClassEnabled()
Returns true if bin-width corrections by this class are globally enabled, false otherwise.
static void enableClass()
Globally enable bin-width corrections by this class.
The RooDataHist is a container class to hold N-dimensional binned data.
Definition RooDataHist.h:39
RooSpan< const double > binVolumes(std::size_t first, std::size_t len) const
Retrieve all bin volumes. Bins are indexed according to getIndex().
Definition RooDataHist.h:89
void markAsCompiled(RooAbsArg &arg) const
std::vector< Int_t > getBins(RooFit::Detail::DataMap const &dataMap) const
Compute bin numbers corresponding to all coordinates in evalData.
Int_t getBin() const
Compute bin number corresponding to current coordinates.
RooDataHist & dataHist()
Return RooDataHist that is represented.
Definition RooHistFunc.h:45
RooConstVar & RooConst(double val)
static void output()