Logo ROOT  
Reference Guide
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(kTRUE), _real(&real), _data(&data), _nset(nset),
58 _superCat(0), _catTable(0)
59{
60 // Determine if dataset contains only categories
61 TIterator* iter = data.get()->createIterator() ;
62 Bool_t allCat(kTRUE) ;
63 RooAbsArg* arg ;
64 while((arg=(RooAbsArg*)iter->Next())) {
65 if (!dynamic_cast<RooCategory*>(arg)) allCat = kFALSE ;
66 }
67 delete iter ;
68
69 // Determine weights of various super categories fractions
70 if (allCat) {
71 _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
72 _catTable = data.table(*_superCat) ;
73 }
74}
75
76
77
78////////////////////////////////////////////////////////////////////////////////
79/// Destructor, delete owned objects
80
82{
83 if (_superCat) delete _superCat ;
84 if (_catTable) delete _catTable ;
85}
86
87
88
89////////////////////////////////////////////////////////////////////////////////
90/// Evaluate data-projected values of the bound real function.
91
93{
94 assert(isValid());
95 loadValues(xvector);
96
97 //RooAbsArg::setDirtyInhibit(kTRUE) ;
98
99 Double_t result(0) ;
100 Double_t wgtSum(0) ;
101
102 if (_catTable) {
103
104 // Data contains only categories, sum over weighted supercategory states
105 for (const auto& nameIdx : *_superCat) {
106 // Backprop state to data set so that _real takes appropriate value
107 _superCat->setIndex(nameIdx) ;
108
109 // Add weighted sum
110 Double_t wgt = _catTable->get(nameIdx.first.c_str());
111 if (wgt) {
112 result += wgt * _real->getVal(_nset) ;
113 wgtSum += wgt ;
114 }
115 }
116
117 } else {
118
119 // Data contains reals, sum over all entries
120 Int_t i ;
121 Int_t nEvt = _data->numEntries() ;
122
123 // Procedure might be lengthy, give some progress indication
124 if (_first) {
125 oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
126 _first = kFALSE ;
127 } else {
128 if (oodologW(_real,Eval)) {
129 ooccoutW(_real,Eval) << "." ; cout.flush() ;
130 }
131 }
132
133// _real->Print("v") ;
134// ((RooAbsReal*)_real)->printCompactTree() ;
135
136// RooArgSet* params = _real->getObservables(_data->get()) ;
137
138 for (i=0 ; i<nEvt ; i++) {
139 _data->get(i) ;
140
141 Double_t wgt = _data->weight() ;
142 Double_t ret ;
143 if (wgt) {
144 ret = _real->getVal(_nset) ;
145 result += wgt * ret ;
146// cout << "ret[" << i << "] = " ;
147// params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
148// cout << " = " << ret << endl ;
149 wgtSum += wgt ;
150 }
151 }
152 }
153
154 //RooAbsArg::setDirtyInhibit(kFALSE) ;
155
156 if (wgtSum==0) return 0 ;
157 return result / wgtSum ;
158}
159
160
161////////////////////////////////////////////////////////////////////////////////
162/// Evaluate the function at the specified values of the dependents.
164 assert(isValid());
165
166 if (!_batchBuffer)
167 _batchBuffer.reset(new std::vector<double>());
168 _batchBuffer->resize(coordinates.front().size());
169
170 std::unique_ptr<double[]> xVec( new double[coordinates.size()] );
171
172 for (std::size_t i=0; i < coordinates.front().size(); ++i) {
173 for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
174 xVec.get()[dim] = coordinates[dim][i];
175 }
176
177 (*_batchBuffer)[i] = this->operator()(xVec.get());
178 }
179
180 return {*_batchBuffer};
181}
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
Definition: RooMsgService.h:47
#define ooccoutW(o, a)
Definition: RooMsgService.h:55
#define oodologW(o, a)
Definition: RooMsgService.h:75
const Bool_t kFALSE
Definition: RtypesCore.h:101
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:375
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_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:245
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:78
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:61
virtual const RooArgSet * get() const
Definition: RooAbsData.h:105
virtual Double_t weight() const =0
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Definition: RooAbsData.cxx:381
Bool_t 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:63
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:93
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
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.
RooDataProjBinding(const RooAbsReal &real, const RooAbsData &data, const RooArgSet &vars, const RooArgSet *normSet=0)
Constructor of a data weighted average function binding with variables 'vars' for function 'real' and...
const RooArgSet * _nset
Normalization set for real function.
Bool_t _first
Bit indicating if operator() has been called yet.
const RooAbsReal * _real
Real function to be projected.
Roo1DTable * _catTable
Supercategory table generated from _data.
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_t operator()(const Double_t 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_t 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.
Iterator abstract base class.
Definition: TIterator.h:30
virtual TObject * Next()=0