RooDataProjBinding.cxx

Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Project: RooFit                                                           *
00003  * Package: RooFitCore                                                       *
00004  * @(#)root/roofitcore:$Id: RooDataProjBinding.cxx 24269 2008-06-13 15:37:03Z wouter $
00005  * Authors:                                                                  *
00006  *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
00007  *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
00008  *                                                                           *
00009  * Copyright (c) 2000-2005, Regents of the University of California          *
00010  *                          and Stanford University. All rights reserved.    *
00011  *                                                                           *
00012  * Redistribution and use in source and binary forms,                        *
00013  * with or without modification, are permitted according to the terms        *
00014  * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
00015  *****************************************************************************/
00016 
00017 //////////////////////////////////////////////////////////////////////////////
00018 // 
00019 // BEGIN_HTML Class RooDataProjBinding is a lightweight interface
00020 // adaptor that projects a real function via summation of states
00021 // provided in a dataset. The real function must be attached to the
00022 // dataset before creating this binding object.
00023 //
00024 // If the dataset only contains category variables, the summation is optimized
00025 // performing a weighted sum over the states of a RooSuperCategory that is
00026 // constructed from all the categories in the dataset
00027 //
00028 // END_HTML
00029 //
00030 
00031 #include "RooFit.h"
00032 #include "Riostream.h"
00033 
00034 #include "RooDataProjBinding.h"
00035 #include "RooAbsReal.h"
00036 #include "RooAbsData.h"
00037 #include "Roo1DTable.h"
00038 #include "RooSuperCategory.h"
00039 #include "RooCategory.h"
00040 #include "RooAbsPdf.h"
00041 #include "RooMsgService.h"
00042 
00043 #include <assert.h>
00044 
00045 
00046 
00047 ClassImp(RooDataProjBinding)
00048 ;
00049 
00050 
00051 //_____________________________________________________________________________
00052 RooDataProjBinding::RooDataProjBinding(const RooAbsReal &real, const RooAbsData& data, 
00053                                        const RooArgSet &vars, const RooArgSet* nset) :
00054   RooRealBinding(real,vars,0), _first(kTRUE), _real(&real), _data(&data), _nset(nset), 
00055   _superCat(0), _catTable(0)
00056 {  
00057   // Constructor of a data weighted average function binding with
00058   // variables 'vars' for function 'real' and dataset 'data' with
00059   // weights.
00060 
00061   // Determine if dataset contains only categories
00062   TIterator* iter = data.get()->createIterator() ;
00063   Bool_t allCat(kTRUE) ;
00064   RooAbsArg* arg ;
00065   while((arg=(RooAbsArg*)iter->Next())) {
00066     if (!dynamic_cast<RooCategory*>(arg)) allCat = kFALSE ;
00067   }
00068   delete iter ;
00069 
00070   // Determine weights of various super categories fractions
00071   if (allCat) {
00072      _superCat = new RooSuperCategory("superCat","superCat",*data.get()) ;
00073      _catTable = data.table(*_superCat) ;
00074   }
00075 }
00076 
00077 
00078 
00079 //_____________________________________________________________________________
00080 RooDataProjBinding::~RooDataProjBinding() 
00081 {
00082   // Destructor, delete owned objects
00083   if (_superCat) delete _superCat ;
00084   if (_catTable) delete _catTable ;
00085 }
00086 
00087 
00088 
00089 //_____________________________________________________________________________
00090 Double_t RooDataProjBinding::operator()(const Double_t xvector[]) const 
00091 {
00092   // Evaluate data-projected values of the bound real function.
00093 
00094   assert(isValid());
00095   loadValues(xvector);    
00096 
00097   //RooAbsArg::setDirtyInhibit(kTRUE) ;
00098 
00099   Double_t result(0) ;
00100   Double_t wgtSum(0) ;  
00101 
00102   if (_catTable) {
00103 
00104     // Data contains only categories, sum over weighted supercategory states
00105     TIterator* iter = _superCat->typeIterator() ;
00106     RooCatType* type ;
00107     while((type=(RooCatType*)iter->Next())) {
00108       // Backprop state to data set so that _real takes appropriate value
00109       _superCat->setIndex(type->getVal()) ;
00110 
00111       // Add weighted sum
00112       Double_t wgt = _catTable->get(type->GetName()) ;
00113       if (wgt) {
00114         result += wgt * _real->getVal(_nset) ;
00115         wgtSum += wgt ;
00116       }
00117     }
00118     delete iter ;
00119     
00120   } else {
00121 
00122     // Data contains reals, sum over all entries
00123     Int_t i ;
00124     Int_t nEvt = _data->numEntries() ;
00125 
00126     // Procedure might be lengthy, give some progress indication
00127     if (_first) {
00128       oocoutW(_real,Eval) << "RooDataProjBinding::operator() projecting over " << nEvt << " events" << endl ;
00129       _first = kFALSE ;
00130     } else {
00131       if (oodologW(_real,Eval)) {
00132         ooccoutW(_real,Eval) << "." ; cout.flush() ;
00133       }
00134     }
00135 
00136 //     _real->Print("v") ;
00137 //     ((RooAbsReal*)_real)->printCompactTree() ;
00138 
00139 //     RooArgSet* params = _real->getObservables(_data->get()) ;
00140 
00141     for (i=0 ; i<nEvt ; i++) {
00142       _data->get(i) ;
00143 
00144       Double_t wgt = _data->weight() ;
00145       Double_t ret ;
00146       if (wgt) {        
00147         ret = _real->getVal(_nset) ;
00148         result += wgt * ret ;
00149 //      cout << "ret[" << i << "] = " ;
00150 //      params->printStream(cout,RooPrintable::kName|RooPrintable::kValue,RooPrintable::kStandard) ;
00151 //      cout << " = " << ret << endl ;
00152         wgtSum += wgt ;
00153       }      
00154     }
00155   }
00156 
00157   //RooAbsArg::setDirtyInhibit(kFALSE) ;
00158 
00159   if (wgtSum==0) return 0 ;
00160   return result / wgtSum ;
00161 }

Generated on Tue Jul 5 15:06:27 2011 for ROOT_528-00b_version by  doxygen 1.5.1