Logo ROOT  
Reference Guide
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 "RooDataHist.h"
30#include "RunContext.h"
31
33
34/// Globally enable bin-width corrections by this class.
36 _enabled = true;
37}
38
39/// Returns `true` if bin-width corrections by this class are globally enabled, `false` otherwise.
41 return _enabled;
42}
43
44/// Globally disable bin-width corrections by this class.
46 _enabled = false;
47}
48
49/// Compute current bin of observable, and return its volume or inverse volume, depending
50/// on configuration chosen in the constructor.
51/// If the bin is not valid, return a volume of 1.
53 if(!_enabled) return 1.;
54 const RooDataHist& dataHist = _histFunc->dataHist();
55 const auto idx = _histFunc->getBin();
56 auto volumes = dataHist.binVolumes(0, dataHist.numEntries());
57 const double volume = idx >= 0 ? volumes[idx] : 1.;
58
59 return _divideByBinWidth ? 1./volume : volume;
60}
61
62
63/// Compute bin index for all values of the observable(s) in `evalData`, and return their volumes or inverse volumes, depending
64/// on the configuration chosen in the constructor.
65/// If a bin is not valid, return a volume of 1.
66void RooBinWidthFunction::computeBatch(cudaStream_t*, double* output, size_t, RooBatchCompute::DataMap& dataMap) const {
67 const RooDataHist& dataHist = _histFunc->dataHist();
68 std::vector<Int_t> bins = _histFunc->getBins(dataMap);
69 auto volumes = dataHist.binVolumes(0, dataHist.numEntries());
70
71 if(!_enabled){
72 for (std::size_t i=0; i < bins.size(); ++i) {
73 output[i] = 1.;
74 }
75 } else {
77 for (std::size_t i=0; i < bins.size(); ++i) {
78 output[i] = bins[i] >= 0 ? 1./volumes[bins[i]] : 1.;
79 }
80 } else {
81 for (std::size_t i=0; i < bins.size(); ++i) {
82 output[i] = bins[i] >= 0 ? volumes[bins[i]] : 1.;
83 }
84 }
85 }
86}
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.
void computeBatch(cudaStream_t *, double *output, size_t size, RooBatchCompute::DataMap &) const override
Compute bin index for all values of the observable(s) in evalData, and return their volumes or invers...
The RooDataHist is a container class to hold N-dimensional binned data.
Definition: RooDataHist.h:45
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:96
Int_t numEntries() const override
Return the number of bins.
Int_t getBin() const
Compute bin number corresponding to current coordinates.
std::vector< Int_t > getBins(RooBatchCompute::DataMap &dataMap) const
Compute bin numbers corresponding to all coordinates in evalData.
RooDataHist & dataHist()
Return RooDataHist that is represented.
Definition: RooHistFunc.h:40
std::map< DataKey, RooSpan< const double > > DataMap
static void output()