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 
22 adaptor that projects a real function via summation of states
23 provided in a dataset. The real function must be attached to the
24 dataset before creating this binding object.
25 
26 If the dataset only contains category variables, the summation is optimized
27 performing a weighted sum over the states of a RooSuperCategory that is
28 constructed 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 
46 using 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 }
RooSuperCategory.h
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:100
RooAbsReal.h
RooMsgService.h
RooDataProjBinding::_batchBuffer
std::unique_ptr< std::vector< double > > _batchBuffer
Definition: RooDataProjBinding.h:43
RooAbsData
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:49
RooFit.h
ooccoutW
#define ooccoutW(o, a)
Definition: RooMsgService.h:55
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
RooAbsData::weight
virtual Double_t weight() const =0
RooAbsReal::getVal
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooDataProjBinding.h
RooAbsReal
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:61
RooDataProjBinding::_nset
const RooArgSet * _nset
Definition: RooDataProjBinding.h:39
RooSuperCategory::setIndex
virtual bool setIndex(value_type index, bool printError=true) override
Set the value of the super category to the specified index.
Definition: RooSuperCategory.cxx:102
RooDataProjBinding::~RooDataProjBinding
virtual ~RooDataProjBinding()
Destructor, delete owned objects.
Definition: RooDataProjBinding.cxx:83
bool
TIterator
Iterator abstract base class.
Definition: TIterator.h:30
RooDataProjBinding::_catTable
Roo1DTable * _catTable
Definition: RooDataProjBinding.h:42
RooDataProjBinding::_first
Bool_t _first
Definition: RooDataProjBinding.h:36
RooAbsData::table
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:745
RooRealBinding
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
Definition: RooRealBinding.h:29
RooDataProjBinding::_real
const RooAbsReal * _real
Definition: RooDataProjBinding.h:37
size
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
RooAbsPdf.h
Double_t
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:101
RooAbsData::get
virtual const RooArgSet * get() const
Definition: RooAbsData.h:92
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:178
RooDataProjBinding::_data
const RooAbsData * _data
Definition: RooDataProjBinding.h:38
oodologW
#define oodologW(o, a)
Definition: RooMsgService.h:75
RooAbsData::numEntries
virtual Int_t numEntries() const
Return number of entries in dataset, i.e., count unweighted entries.
Definition: RooAbsData.cxx:304
RooCategory.h
oocoutW
#define oocoutW(o, a)
Definition: RooMsgService.h:47
TIterator::Next
virtual TObject * Next()=0
RooRealBinding::loadValues
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
Definition: RooRealBinding.cxx:171
RooAbsFunc::isValid
Bool_t isValid() const
Definition: RooAbsFunc.h:37
RooAbsData.h
RooDataProjBinding::getValues
RooSpan< const double > getValues(std::vector< RooSpan< const double >> coordinates) const
Evaluate the function at the specified values of the dependents.
Definition: RooDataProjBinding.cxx:165
RooDataProjBinding::RooDataProjBinding
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...
Definition: RooDataProjBinding.cxx:57
RooDataProjBinding
adaptor that projects a real function via summation of states provided in a dataset.
Definition: RooDataProjBinding.h:25
Roo1DTable.h
RooCategory
RooCategory is an object to represent discrete states.
Definition: RooCategory.h:27
RooDataProjBinding::_superCat
RooSuperCategory * _superCat
Definition: RooDataProjBinding.h:41
RooSuperCategory
The RooSuperCategory can join several RooAbsCategoryLValue objects into a single category.
Definition: RooSuperCategory.h:27
Roo1DTable::get
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:246
RooAbsArg
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
Definition: RooAbsArg.h:72
RooFit::Eval
@ Eval
Definition: RooGlobalFunc.h:61
RooSpan
A simple container to hold a batch of data values.
Definition: RooSpan.h:34
RooArgSet
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:33
int
RooDataProjBinding::operator()
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate data-projected values of the bound real function.
Definition: RooDataProjBinding.cxx:94