Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooDataProjBinding.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 RooDataProjBinding.cxx
19\class RooDataProjBinding
20\ingroup Roofitcore
21
22adaptor that projects a real function via summation of states
23provided in a dataset. The real function must be attached to the
24dataset before creating this binding object.
25
26If the dataset only contains category variables, the summation is optimized
27performing a weighted sum over the states of a RooSuperCategory that is
28constructed from all the categories in the dataset
29
30**/
31
32#include "RooDataProjBinding.h"
33#include "RooAbsReal.h"
34#include "RooAbsData.h"
35#include "Roo1DTable.h"
36#include "RooSuperCategory.h"
37#include "RooCategory.h"
38#include "RooAbsPdf.h"
39#include "RooMsgService.h"
40
41#include <iostream>
42#include <cassert>
43
44using namespace std;
45
47;
48
49
50////////////////////////////////////////////////////////////////////////////////
51/// Constructor of a data weighted average function binding with
52/// variables 'vars' for function 'real' and dataset 'data' with
53/// weights.
54
56 const RooArgSet &vars, const RooArgSet* nset) :
57 RooRealBinding(real,vars,0), _first(true), _real(&real), _data(&data), _nset(nset),
58 _superCat(0), _catTable(0)
59{
60 // Determine if dataset contains only categories
61 bool allCat(true) ;
62 for(RooAbsArg * arg : *data.get()) {
63 if (!dynamic_cast<RooCategory*>(arg)) allCat = false ;
64 }
65
66 // Determine weights of various super categories fractions
67 if (allCat) {
68 _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
69 _catTable = data.table(*_superCat) ;
70 }
71}
72
73
74
75////////////////////////////////////////////////////////////////////////////////
76/// Destructor, delete owned objects
77
79{
80 if (_superCat) delete _superCat ;
81 if (_catTable) delete _catTable ;
82}
83
84
85
86////////////////////////////////////////////////////////////////////////////////
87/// Evaluate data-projected values of the bound real function.
88
89double RooDataProjBinding::operator()(const double xvector[]) const
90{
91 assert(isValid());
92 loadValues(xvector);
93
94 //RooAbsArg::setDirtyInhibit(true) ;
95
96 double result(0) ;
97 double wgtSum(0) ;
98
99 if (_catTable) {
100
101 // Data contains only categories, sum over weighted supercategory states
102 for (const auto& nameIdx : *_superCat) {
103 // Backprop state to data set so that _real takes appropriate value
104 _superCat->setIndex(nameIdx) ;
105
106 // Add weighted sum
107 double wgt = _catTable->get(nameIdx.first.c_str());
108 if (wgt) {
109 result += wgt * _real->getVal(_nset) ;
110 wgtSum += wgt ;
111 }
112 }
113
114 } else {
115
116 // Data contains reals, sum over all entries
117 Int_t i ;
118 Int_t nEvt = _data->numEntries() ;
119
120 // Procedure might be lengthy, give some progress indication
121 if (_first) {
122 oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
123 _first = false ;
124 } else {
125 if (oodologW(_real,Eval)) {
126 ooccoutW(_real,Eval) << "." ; cout.flush() ;
127 }
128 }
129
130// _real->Print("v") ;
131// ((RooAbsReal*)_real)->printCompactTree() ;
132
133// RooArgSet* params = _real->getObservables(_data->get()) ;
134
135 for (i=0 ; i<nEvt ; i++) {
136 _data->get(i) ;
137
138 double wgt = _data->weight() ;
139 double ret ;
140 if (wgt) {
141 ret = _real->getVal(_nset) ;
142 result += wgt * ret ;
143// cout << "ret[" << i << "] = " ;
144// params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
145// cout << " = " << ret << endl ;
146 wgtSum += wgt ;
147 }
148 }
149 }
150
151 //RooAbsArg::setDirtyInhibit(false) ;
152
153 if (wgtSum==0) return 0 ;
154 return result / wgtSum ;
155}
156
157
158////////////////////////////////////////////////////////////////////////////////
159/// Evaluate the function at the specified values of the dependents.
161 assert(isValid());
162
163 if (!_batchBuffer)
164 _batchBuffer = std::make_unique<std::vector<double>>();
165 _batchBuffer->resize(coordinates.front().size());
166
167 std::vector<double> xVec(coordinates.size());
168
169 for (std::size_t i=0; i < coordinates.front().size(); ++i) {
170 for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
171 xVec[dim] = coordinates[dim][i];
172 }
173
174 (*_batchBuffer)[i] = this->operator()(xVec.data());
175 }
176
177 return {*_batchBuffer};
178}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
#define ooccoutW(o, a)
#define oodologW(o, a)
#define ClassImp(name)
Definition Rtypes.h:377
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
double get(const char *label, bool silent=false) const
Return the table entry named 'label'.
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition RooAbsArg.h:74
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
virtual double weight() const =0
virtual const RooArgSet * get() const
Definition RooAbsData.h:103
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
bool isValid() const
Definition RooAbsFunc.h:37
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition RooAbsReal.h:91
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooCategory is an object to represent discrete states.
Definition RooCategory.h:28
adaptor that projects a real function via summation of states provided in a dataset.
const RooArgSet * _nset
Normalization set for real function.
bool _first
Bit indicating if operator() has been called yet.
const RooAbsReal * _real
Real function to be projected.
Roo1DTable * _catTable
Supercategory table generated from _data.
RooDataProjBinding(const RooAbsReal &real, const RooAbsData &data, const RooArgSet &vars, const RooArgSet *normSet=nullptr)
Constructor of a data weighted average function binding with variables 'vars' for function 'real' and...
std::unique_ptr< std::vector< double > > _batchBuffer
! Storage for handing out spans.
~RooDataProjBinding() override
Destructor, delete owned objects.
RooSuperCategory * _superCat
Supercategory constructed from _data's category variables.
const RooAbsData * _data
Dataset used for projection.
double operator()(const double xvector[]) const override
Evaluate data-projected values of the bound real function.
RooSpan< const double > getValues(std::vector< RooSpan< const double > > coordinates) const override
Evaluate the function at the specified values of the dependents.
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
void loadValues(const double xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
A simple container to hold a batch of data values.
Definition RooSpan.h:34
The RooSuperCategory can join several RooAbsCategoryLValue objects into a single category.
bool setIndex(value_type index, bool printError=true) override
Set the value of the super category to the specified index.