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 }
RooSuperCategory.h
kTRUE
const Bool_t kTRUE
Definition: RtypesCore.h:91
RooAbsReal.h
RooMsgService.h
RooAbsData
Definition: RooAbsData.h:46
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
Definition: RooAbsReal.h:61
RooDataProjBinding::_nset
const RooArgSet * _nset
Definition: RooDataProjBinding.h:37
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
Definition: TIterator.h:30
RooDataProjBinding::_catTable
Roo1DTable * _catTable
Definition: RooDataProjBinding.h:40
RooDataProjBinding::_first
Bool_t _first
Definition: RooDataProjBinding.h:34
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:749
RooRealBinding
Definition: RooRealBinding.h:26
RooDataProjBinding::_real
const RooAbsReal * _real
Definition: RooDataProjBinding.h:35
RooAbsPdf.h
kFALSE
const Bool_t kFALSE
Definition: RtypesCore.h:92
RooAbsData::get
virtual const RooArgSet * get() const
Definition: RooAbsData.h:89
RooAbsCollection::createIterator
TIterator * createIterator(Bool_t dir=kIterForward) const
TIterator-style iteration over contained elements.
Definition: RooAbsCollection.h:118
RooDataProjBinding::_data
const RooAbsData * _data
Definition: RooDataProjBinding.h:36
oodologW
#define oodologW(o, a)
Definition: RooMsgService.h:75
RooAbsData::numEntries
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:307
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:180
RooAbsFunc::isValid
Bool_t isValid() const
Definition: RooAbsFunc.h:33
RooAbsData.h
Double_t
double Double_t
Definition: RtypesCore.h:59
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
Definition: RooDataProjBinding.h:25
Roo1DTable.h
RooCategory
Definition: RooCategory.h:27
RooDataProjBinding::_superCat
RooSuperCategory * _superCat
Definition: RooDataProjBinding.h:39
RooSuperCategory
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
Definition: RooAbsArg.h:73
RooFit::Eval
@ Eval
Definition: RooGlobalFunc.h:68
RooArgSet
Definition: RooArgSet.h:28
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