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 "RooFit.h"
33
34#include "RooDataProjBinding.h"
35#include "RooAbsReal.h"
36#include "RooAbsData.h"
37#include "Roo1DTable.h"
38#include "RooSuperCategory.h"
39#include "RooCategory.h"
40#include "RooAbsPdf.h"
41#include "RooMsgService.h"
42
43#include <iostream>
44#include <cassert>
45
46using namespace std;
47
49;
50
51
52////////////////////////////////////////////////////////////////////////////////
53/// Constructor of a data weighted average function binding with
54/// variables 'vars' for function 'real' and dataset 'data' with
55/// weights.
56
58 const RooArgSet &vars, const RooArgSet* nset) :
59 RooRealBinding(real,vars,0), _first(kTRUE), _real(&real), _data(&data), _nset(nset),
60 _superCat(0), _catTable(0)
61{
62 // Determine if dataset contains only categories
63 TIterator* iter = data.get()->createIterator() ;
64 Bool_t allCat(kTRUE) ;
65 RooAbsArg* arg ;
66 while((arg=(RooAbsArg*)iter->Next())) {
67 if (!dynamic_cast<RooCategory*>(arg)) allCat = kFALSE ;
68 }
69 delete iter ;
70
71 // Determine weights of various super categories fractions
72 if (allCat) {
73 _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
74 _catTable = data.table(*_superCat) ;
75 }
76}
77
78
79
80////////////////////////////////////////////////////////////////////////////////
81/// Destructor, delete owned objects
82
84{
85 if (_superCat) delete _superCat ;
86 if (_catTable) delete _catTable ;
87}
88
89
90
91////////////////////////////////////////////////////////////////////////////////
92/// Evaluate data-projected values of the bound real function.
93
95{
96 assert(isValid());
97 loadValues(xvector);
98
99 //RooAbsArg::setDirtyInhibit(kTRUE) ;
100
101 Double_t result(0) ;
102 Double_t wgtSum(0) ;
103
104 if (_catTable) {
105
106 // Data contains only categories, sum over weighted supercategory states
107 for (const auto& nameIdx : *_superCat) {
108 // Backprop state to data set so that _real takes appropriate value
109 _superCat->setIndex(nameIdx) ;
110
111 // Add weighted sum
112 Double_t wgt = _catTable->get(nameIdx.first.c_str());
113 if (wgt) {
114 result += wgt * _real->getVal(_nset) ;
115 wgtSum += wgt ;
116 }
117 }
118
119 } else {
120
121 // Data contains reals, sum over all entries
122 Int_t i ;
123 Int_t nEvt = _data->numEntries() ;
124
125 // Procedure might be lengthy, give some progress indication
126 if (_first) {
127 oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
128 _first = kFALSE ;
129 } else {
130 if (oodologW(_real,Eval)) {
131 ooccoutW(_real,Eval) << "." ; cout.flush() ;
132 }
133 }
134
135// _real->Print("v") ;
136// ((RooAbsReal*)_real)->printCompactTree() ;
137
138// RooArgSet* params = _real->getObservables(_data->get()) ;
139
140 for (i=0 ; i<nEvt ; i++) {
141 _data->get(i) ;
142
143 Double_t wgt = _data->weight() ;
144 Double_t ret ;
145 if (wgt) {
146 ret = _real->getVal(_nset) ;
147 result += wgt * ret ;
148// cout << "ret[" << i << "] = " ;
149// params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
150// cout << " = " << ret << endl ;
151 wgtSum += wgt ;
152 }
153 }
154 }
155
156 //RooAbsArg::setDirtyInhibit(kFALSE) ;
157
158 if (wgtSum==0) return 0 ;
159 return result / wgtSum ;
160}
161
162
163////////////////////////////////////////////////////////////////////////////////
164/// Evaluate the function at the specified values of the dependents.
166 assert(isValid());
167
168 if (!_batchBuffer)
169 _batchBuffer.reset(new std::vector<double>());
170 _batchBuffer->resize(coordinates.front().size());
171
172 std::unique_ptr<double[]> xVec( new double[coordinates.size()] );
173
174 for (std::size_t i=0; i < coordinates.front().size(); ++i) {
175 for (unsigned int dim=0; dim < coordinates.size(); ++dim) {
176 xVec.get()[dim] = coordinates[dim][i];
177 }
178
179 (*_batchBuffer)[i] = this->operator()(xVec.get());
180 }
181
182 return {*_batchBuffer};
183}
#define oocoutW(o, a)
#define ooccoutW(o, a)
#define oodologW(o, a)
const Bool_t kFALSE
Definition RtypesCore.h:92
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
Double_t get(const char *label, Bool_t silent=kFALSE) 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:72
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:49
virtual const RooArgSet * get() const
Definition RooAbsData.h:92
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
virtual Double_t weight() const =0
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
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:61
Double_t 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:29
RooCategory is an object to represent discrete states.
Definition RooCategory.h:27
adaptor that projects a real function via summation of states provided in a dataset.
virtual ~RooDataProjBinding()
Destructor, delete owned objects.
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
const RooAbsReal * _real
std::unique_ptr< std::vector< double > > _batchBuffer
RooSpan< const double > getValues(std::vector< RooSpan< const double > > coordinates) const
Evaluate the function at the specified values of the dependents.
RooSuperCategory * _superCat
const RooAbsData * _data
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate data-projected values of the bound real function.
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.
virtual 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