Logo ROOT   6.10/09
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 #include "Riostream.h"
34 
35 #include "RooDataProjBinding.h"
36 #include "RooAbsReal.h"
37 #include "RooAbsData.h"
38 #include "Roo1DTable.h"
39 #include "RooSuperCategory.h"
40 #include "RooCategory.h"
41 #include "RooAbsPdf.h"
42 #include "RooMsgService.h"
43 
44 #include <assert.h>
45 
46 
47 
48 using namespace std;
49 
51 ;
52 
53 
54 ////////////////////////////////////////////////////////////////////////////////
55 /// Constructor of a data weighted average function binding with
56 /// variables 'vars' for function 'real' and dataset 'data' with
57 /// weights.
58 
60  const RooArgSet &vars, const RooArgSet* nset) :
61  RooRealBinding(real,vars,0), _first(kTRUE), _real(&real), _data(&data), _nset(nset),
62  _superCat(0), _catTable(0)
63 {
64  // Determine if dataset contains only categories
65  TIterator* iter = data.get()->createIterator() ;
66  Bool_t allCat(kTRUE) ;
67  RooAbsArg* arg ;
68  while((arg=(RooAbsArg*)iter->Next())) {
69  if (!dynamic_cast<RooCategory*>(arg)) allCat = kFALSE ;
70  }
71  delete iter ;
72 
73  // Determine weights of various super categories fractions
74  if (allCat) {
75  _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
76  _catTable = data.table(*_superCat) ;
77  }
78 }
79 
80 
81 
82 ////////////////////////////////////////////////////////////////////////////////
83 /// Destructor, delete owned objects
84 
86 {
87  if (_superCat) delete _superCat ;
88  if (_catTable) delete _catTable ;
89 }
90 
91 
92 
93 ////////////////////////////////////////////////////////////////////////////////
94 /// Evaluate data-projected values of the bound real function.
95 
97 {
98  assert(isValid());
99  loadValues(xvector);
100 
101  //RooAbsArg::setDirtyInhibit(kTRUE) ;
102 
103  Double_t result(0) ;
104  Double_t wgtSum(0) ;
105 
106  if (_catTable) {
107 
108  // Data contains only categories, sum over weighted supercategory states
109  TIterator* iter = _superCat->typeIterator() ;
110  RooCatType* type ;
111  while((type=(RooCatType*)iter->Next())) {
112  // Backprop state to data set so that _real takes appropriate value
113  _superCat->setIndex(type->getVal()) ;
114 
115  // Add weighted sum
116  Double_t wgt = _catTable->get(type->GetName()) ;
117  if (wgt) {
118  result += wgt * _real->getVal(_nset) ;
119  wgtSum += wgt ;
120  }
121  }
122  delete iter ;
123 
124  } else {
125 
126  // Data contains reals, sum over all entries
127  Int_t i ;
128  Int_t nEvt = _data->numEntries() ;
129 
130  // Procedure might be lengthy, give some progress indication
131  if (_first) {
132  oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
133  _first = kFALSE ;
134  } else {
135  if (oodologW(_real,Eval)) {
136  ooccoutW(_real,Eval) << "." ; cout.flush() ;
137  }
138  }
139 
140 // _real->Print("v") ;
141 // ((RooAbsReal*)_real)->printCompactTree() ;
142 
143 // RooArgSet* params = _real->getObservables(_data->get()) ;
144 
145  for (i=0 ; i<nEvt ; i++) {
146  _data->get(i) ;
147 
148  Double_t wgt = _data->weight() ;
149  Double_t ret ;
150  if (wgt) {
151  ret = _real->getVal(_nset) ;
152  result += wgt * ret ;
153 // cout << "ret[" << i << "] = " ;
154 // params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
155 // cout << " = " << ret << endl ;
156  wgtSum += wgt ;
157  }
158  }
159  }
160 
161  //RooAbsArg::setDirtyInhibit(kFALSE) ;
162 
163  if (wgtSum==0) return 0 ;
164  return result / wgtSum ;
165 }
TIterator * createIterator(Bool_t dir=kIterForward) const
adaptor that projects a real function via summation of states provided in a dataset.
virtual const RooArgSet * get() const
Definition: RooAbsData.h:77
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named &#39;label&#39;.
Definition: Roo1DTable.cxx:244
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
STL namespace.
#define ooccoutW(o, a)
Definition: RooMsgService.h:53
Bool_t isValid() const
Definition: RooAbsFunc.h:33
RooDataProjBinding(const RooAbsReal &real, const RooAbsData &data, const RooArgSet &vars, const RooArgSet *normSet=0)
Constructor of a data weighted average function binding with variables &#39;vars&#39; for function &#39;real&#39; and...
RooSuperCategory * _superCat
Iterator abstract base class.
Definition: TIterator.h:30
const RooArgSet * _nset
RooCatType is an auxilary class for RooAbsCategory and defines a a single category state...
Definition: RooCatType.h:22
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate data-projected values of the bound real function.
#define oodologW(o, a)
Definition: RooMsgService.h:73
TIterator * typeIterator() const
Return iterator over all defined states.
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:44
virtual Bool_t setIndex(Int_t index, Bool_t printError=kTRUE)
Set the value of the super category by specifying the state index code by setting the states of the c...
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition: RooAbsData.h:37
const RooAbsData * _data
const Bool_t kFALSE
Definition: RtypesCore.h:92
#define ClassImp(name)
Definition: Rtypes.h:336
double Double_t
Definition: RtypesCore.h:55
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:53
int type
Definition: TGX11.cxx:120
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
#define oocoutW(o, a)
Definition: RooMsgService.h:46
Int_t getVal() const
Definition: RooCatType.h:79
virtual TObject * Next()=0
virtual Double_t weight() const =0
virtual ~RooDataProjBinding()
Destructor, delete owned objects.
const RooAbsReal * _real
RooSuperCategory consolidates several RooAbsCategoryLValue objects into a single category.
double result[121]
Lightweight interface adaptor that binds a RooAbsReal object to a subset of its servers and present i...
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:66
const Bool_t kTRUE
Definition: RtypesCore.h:91
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:269
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:752