Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 
50 ClassImp(RooDataProjBinding);
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 
59 RooDataProjBinding::RooDataProjBinding(const RooAbsReal &real, const RooAbsData& data,
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 
85 RooDataProjBinding::~RooDataProjBinding()
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 
96 Double_t RooDataProjBinding::operator()(const Double_t xvector[]) const
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 }