ROOT  6.06/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 //
19 // BEGIN_HTML Class RooDataProjBinding is a lightweight interface
20 // adaptor that projects a real function via summation of states
21 // provided in a dataset. The real function must be attached to the
22 // dataset before creating this binding object.
23 //
24 // If the dataset only contains category variables, the summation is optimized
25 // performing a weighted sum over the states of a RooSuperCategory that is
26 // constructed from all the categories in the dataset
27 //
28 // END_HTML
29 //
30 
31 #include "RooFit.h"
32 #include "Riostream.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 <assert.h>
44 
45 
46 
47 using namespace std;
48 
50 ;
51 
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// Constructor of a data weighted average function binding with
55 /// variables 'vars' for function 'real' and dataset 'data' with
56 /// weights.
57 
59  const RooArgSet &vars, const RooArgSet* nset) :
60  RooRealBinding(real,vars,0), _first(kTRUE), _real(&real), _data(&data), _nset(nset),
61  _superCat(0), _catTable(0)
62 {
63  // Determine if dataset contains only categories
64  TIterator* iter = data.get()->createIterator() ;
65  Bool_t allCat(kTRUE) ;
66  RooAbsArg* arg ;
67  while((arg=(RooAbsArg*)iter->Next())) {
68  if (!dynamic_cast<RooCategory*>(arg)) allCat = kFALSE ;
69  }
70  delete iter ;
71 
72  // Determine weights of various super categories fractions
73  if (allCat) {
74  _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
75  _catTable = data.table(*_superCat) ;
76  }
77 }
78 
79 
80 
81 ////////////////////////////////////////////////////////////////////////////////
82 /// Destructor, delete owned objects
83 
85 {
86  if (_superCat) delete _superCat ;
87  if (_catTable) delete _catTable ;
88 }
89 
90 
91 
92 ////////////////////////////////////////////////////////////////////////////////
93 /// Evaluate data-projected values of the bound real function.
94 
96 {
97  assert(isValid());
98  loadValues(xvector);
99 
100  //RooAbsArg::setDirtyInhibit(kTRUE) ;
101 
102  Double_t result(0) ;
103  Double_t wgtSum(0) ;
104 
105  if (_catTable) {
106 
107  // Data contains only categories, sum over weighted supercategory states
109  RooCatType* type ;
110  while((type=(RooCatType*)iter->Next())) {
111  // Backprop state to data set so that _real takes appropriate value
112  _superCat->setIndex(type->getVal()) ;
113 
114  // Add weighted sum
115  Double_t wgt = _catTable->get(type->GetName()) ;
116  if (wgt) {
117  result += wgt * _real->getVal(_nset) ;
118  wgtSum += wgt ;
119  }
120  }
121  delete iter ;
122 
123  } else {
124 
125  // Data contains reals, sum over all entries
126  Int_t i ;
127  Int_t nEvt = _data->numEntries() ;
128 
129  // Procedure might be lengthy, give some progress indication
130  if (_first) {
131  oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
132  _first = kFALSE ;
133  } else {
134  if (oodologW(_real,Eval)) {
135  ooccoutW(_real,Eval) << "." ; cout.flush() ;
136  }
137  }
138 
139 // _real->Print("v") ;
140 // ((RooAbsReal*)_real)->printCompactTree() ;
141 
142 // RooArgSet* params = _real->getObservables(_data->get()) ;
143 
144  for (i=0 ; i<nEvt ; i++) {
145  _data->get(i) ;
146 
147  Double_t wgt = _data->weight() ;
148  Double_t ret ;
149  if (wgt) {
150  ret = _real->getVal(_nset) ;
151  result += wgt * ret ;
152 // cout << "ret[" << i << "] = " ;
153 // params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
154 // cout << " = " << ret << endl ;
155  wgtSum += wgt ;
156  }
157  }
158  }
159 
160  //RooAbsArg::setDirtyInhibit(kFALSE) ;
161 
162  if (wgtSum==0) return 0 ;
163  return result / wgtSum ;
164 }
ClassImp(RooDataProjBinding)
virtual Roo1DTable * table(const RooArgSet &catSet, const char *cuts="", const char *opts="") const
Construct table for product of categories in catSet.
Definition: RooAbsData.cxx:833
#define assert(cond)
Definition: unittest.h:542
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
virtual Double_t operator()(const Double_t xvector[]) const
Evaluate data-projected values of the bound real function.
#define ooccoutW(o, a)
Definition: RooMsgService.h:54
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...
RooSuperCategory * _superCat
Iterator abstract base class.
Definition: TIterator.h:32
const RooArgSet * _nset
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
TIterator * createIterator(Bool_t dir=kIterForward) const
Double_t getVal(const RooArgSet *set=0) const
Definition: RooAbsReal.h:64
virtual const RooArgSet * get() const
Definition: RooAbsData.h:77
void loadValues(const Double_t xvector[]) const
Load the vector of variable values into the RooRealVars associated as variables with the bound RooAbs...
#define oodologW(o, a)
Definition: RooMsgService.h:74
virtual Int_t numEntries() const
Definition: RooAbsData.cxx:291
Bool_t isValid() const
Definition: RooAbsFunc.h:33
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...
const RooAbsData * _data
Double_t get(const char *label, Bool_t silent=kFALSE) const
Return the table entry named 'label'.
Definition: Roo1DTable.cxx:243
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
Int_t getVal() const
Definition: RooCatType.h:80
#define oocoutW(o, a)
Definition: RooMsgService.h:47
virtual TObject * Next()=0
virtual Double_t weight() const =0
virtual ~RooDataProjBinding()
Destructor, delete owned objects.
const RooAbsReal * _real
double result[121]
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: Rtypes.h:91
virtual const Text_t * GetName() const
Returns name of object.
Definition: RooCatType.h:45
TIterator * typeIterator() const
Return iterator over all defined states.