ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofit:$Id: RooHistFunc.cxx 39955 2011-06-24 20:52:32Z wouter $
 * Authors:                                                                  *
 *   WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu       *
 *   DK, David Kirkby,    UC Irvine,         dkirkby@uci.edu                 *
 *                                                                           *
 * Copyright (c) 2000-2005, Regents of the University of California          *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 * Redistribution and use in source and binary forms,                        *
 * with or without modification, are permitted according to the terms        *
 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooHistFunc implements a real-valued function sampled from a 
// multidimensional histogram. The histogram can have an arbitrary number of real or 
// discrete dimensions and may have negative values
// END_HTML
//

#include "RooFit.h"
#include "Riostream.h"

#include "RooHistFunc.h"
#include "RooDataHist.h"
#include "RooMsgService.h"
#include "RooRealVar.h"
#include "RooCategory.h"



ClassImp(RooHistFunc)
;



//_____________________________________________________________________________
RooHistFunc::RooHistFunc() :
  _dataHist(0),
  _intOrder(0),
  _cdfBoundaries(kFALSE),
  _totVolume(0),
  _unitNorm(kFALSE)
{
  // Default constructor
}


//_____________________________________________________________________________
RooHistFunc::RooHistFunc(const char *name, const char *title, const RooArgSet& vars, 
		       const RooDataHist& dhist, Int_t intOrder) :
  RooAbsReal(name,title), 
  _depList("depList","List of dependents",this),
  _dataHist((RooDataHist*)&dhist), 
  _codeReg(10),
  _intOrder(intOrder),
  _cdfBoundaries(kFALSE),
  _totVolume(0),
  _unitNorm(kFALSE)
{
  // Constructor from a RooDataHist. The variable listed in 'vars' control the dimensionality of the
  // function. Any additional dimensions present in 'dhist' will be projected out. RooDataHist dimensions
  // can be either real or discrete. See RooDataHist::RooDataHist for details on the binning.
  // RooHistFunc neither owns or clone 'dhist' and the user must ensure the input histogram exists
  // for the entire life span of this function.

  _depList.add(vars) ;

  // Verify that vars and dhist.get() have identical contents
  const RooArgSet* dvars = dhist.get() ;
  if (vars.getSize()!=dvars->getSize()) {
    coutE(InputArguments) << "RooHistFunc::ctor(" << GetName() 
			  << ") ERROR variable list and RooDataHist must contain the same variables." << endl ;
    assert(0) ;
  }
  TIterator* iter = vars.createIterator() ;
  RooAbsArg* arg ;
  while((arg=(RooAbsArg*)iter->Next())) {
    if (!dvars->find(arg->GetName())) {
      coutE(InputArguments) << "RooHistFunc::ctor(" << GetName() 
			    << ") ERROR variable list and RooDataHist must contain the same variables." << endl ;
      assert(0) ;
    }
  }
  delete iter ;
}



//_____________________________________________________________________________
RooHistFunc::RooHistFunc(const RooHistFunc& other, const char* name) :
  RooAbsReal(other,name), 
  _depList("depList",this,other._depList),
  _dataHist(other._dataHist),
  _codeReg(other._codeReg),
  _intOrder(other._intOrder),
  _cdfBoundaries(other._cdfBoundaries),
  _totVolume(other._totVolume),
  _unitNorm(other._unitNorm)
{
  // Copy constructor
}



//_____________________________________________________________________________
Double_t RooHistFunc::evaluate() const
{
  // Return the current value: The value of the bin enclosing the current coordinates
  // of the dependents, normalized by the histograms contents. Interpolation
  // is applied if the RooHistFunc is configured to do that

  Double_t ret =  _dataHist->weight(_depList,_intOrder,kFALSE,_cdfBoundaries) ;  
  return ret ;
}


//_____________________________________________________________________________
Double_t RooHistFunc::totVolume() const
{
  // Return the total volume spanned by the observables of the RooDataHist

  // Return previously calculated value, if any
  if (_totVolume>0) {
    return _totVolume ;
  }
  _totVolume = 1. ;
  TIterator* iter = _depList.createIterator() ;
  RooAbsArg* arg ;
  while((arg=(RooAbsArg*)iter->Next())) {
    RooRealVar* real = dynamic_cast<RooRealVar*>(arg) ;
    if (real) {
      _totVolume *= (real->getMax()-real->getMin()) ;
    } else {
      RooCategory* cat = dynamic_cast<RooCategory*>(arg) ;
      if (cat) {
	_totVolume *= cat->numTypes() ;
      }
    }
  }
  delete iter ;
  return _totVolume ;
}



//_____________________________________________________________________________
Int_t RooHistFunc::getAnalyticalIntegral(RooArgSet& allVars, RooArgSet& analVars, const char* rangeName) const 
{
  // Determine integration scenario. If no interpolation is used,
  // RooHistFunc can perform all integrals over its dependents
  // analytically via partial or complete summation of the input
  // histogram. If interpolation is used, only the integral
  // over all RooHistPdf observables is implemented.


  // Only analytical integrals over the full range are defined
  if (rangeName!=0) {
    return 0 ;
  }

  // Simplest scenario, integrate over all dependents
  RooAbsCollection *allVarsCommon = allVars.selectCommon(_depList) ;  
  Bool_t intAllObs = (allVarsCommon->getSize()==_depList.getSize()) ;  
  delete allVarsCommon ;
  if (intAllObs && matchArgs(allVars,analVars,_depList)) {
    return 1000 ;
  }

  // Disable partial analytical integrals if interpolation is used
  if (_intOrder>0) {
    return 0 ;
  }

  // Find subset of _depList that integration is requested over
  RooArgSet* allVarsSel = (RooArgSet*) allVars.selectCommon(_depList) ;
  if (allVarsSel->getSize()==0) {
    delete allVarsSel ;
    return 0 ;
  }

  // Partial integration scenarios.
  // Build unique code from bit mask of integrated variables in depList
  Int_t code(0),n(0) ;
  TIterator* iter = _depList.createIterator() ;
  RooAbsArg* arg ;
  while((arg=(RooAbsArg*)iter->Next())) {
    if (allVars.find(arg->GetName())) code |= (1<<n) ;
    n++ ;
  }
  delete iter ;
  analVars.add(*allVarsSel) ;

  return code ;

}



//_____________________________________________________________________________
Double_t RooHistFunc::analyticalIntegral(Int_t code, const char* /*rangeName*/) const 
{
  // Return integral identified by 'code'. The actual integration
  // is deferred to RooDataHist::sum() which implements partial
  // or complete summation over the histograms contents

  // WVE needs adaptation for rangeName feature

  // Simplest scenario, integration over all dependents
  if (code==1000) {
    return _dataHist->sum(kTRUE) ;
  }

  // Partial integration scenario, retrieve set of variables, calculate partial sum

  RooArgSet intSet ;
  TIterator* iter = _depList.createIterator() ;
  RooAbsArg* arg ;
  Int_t n(0) ;
  while((arg=(RooAbsArg*)iter->Next())) {
    if (code & (1<<n)) {
      intSet.add(*arg) ;
    }
    n++ ;
  }
  delete iter ;

  Double_t ret =  _dataHist->sum(intSet,_depList,kTRUE) ;
  return ret ;
}



//_____________________________________________________________________________
list<Double_t>* RooHistFunc::plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const
{
  // Return sampling hint for making curves of (projections) of this function
  // as the recursive division strategy of RooCurve cannot deal efficiently
  // with the vertical lines that occur in a non-interpolated histogram

  // No hints are required when interpolation is used
  if (_intOrder>0) {
    return 0 ;
  }

  // Check that observable is in dataset, if not no hint is generated
  RooAbsLValue* lvarg = dynamic_cast<RooAbsLValue*>(_dataHist->get()->find(obs.GetName())) ;
  if (!lvarg) {
    return 0 ;
  }

  // Retrieve position of all bin boundaries
  const RooAbsBinning* binning = lvarg->getBinningPtr(0) ;
  Double_t* boundaries = binning->array() ;

  list<Double_t>* hint = new list<Double_t> ;

  // Widen range slighty
  xlo = xlo - 0.01*(xhi-xlo) ;
  xhi = xhi + 0.01*(xhi-xlo) ;

  Double_t delta = (xhi-xlo)*1e-8 ;
 
  // Construct array with pairs of points positioned epsilon to the left and
  // right of the bin boundaries
  for (Int_t i=0 ; i<binning->numBoundaries() ; i++) {
    if (boundaries[i]>=xlo && boundaries[i]<=xhi) {
      hint->push_back(boundaries[i]-delta) ;
      hint->push_back(boundaries[i]+delta) ;
    }
  }

  return hint ;
}


//______________________________________________________________________________
void RooHistFunc::Streamer(TBuffer &R__b)
{
   // Stream an object of class RooHistFunc.

   if (R__b.IsReading()) {
      R__b.ReadClassBuffer(RooHistFunc::Class(),this);
      // WVE - interim solution - fix proxies here
      _proxyList.Clear() ;
      registerProxy(_depList) ;
   } else {
      R__b.WriteClassBuffer(RooHistFunc::Class(),this);
   }
}

 RooHistFunc.cxx:1
 RooHistFunc.cxx:2
 RooHistFunc.cxx:3
 RooHistFunc.cxx:4
 RooHistFunc.cxx:5
 RooHistFunc.cxx:6
 RooHistFunc.cxx:7
 RooHistFunc.cxx:8
 RooHistFunc.cxx:9
 RooHistFunc.cxx:10
 RooHistFunc.cxx:11
 RooHistFunc.cxx:12
 RooHistFunc.cxx:13
 RooHistFunc.cxx:14
 RooHistFunc.cxx:15
 RooHistFunc.cxx:16
 RooHistFunc.cxx:17
 RooHistFunc.cxx:18
 RooHistFunc.cxx:19
 RooHistFunc.cxx:20
 RooHistFunc.cxx:21
 RooHistFunc.cxx:22
 RooHistFunc.cxx:23
 RooHistFunc.cxx:24
 RooHistFunc.cxx:25
 RooHistFunc.cxx:26
 RooHistFunc.cxx:27
 RooHistFunc.cxx:28
 RooHistFunc.cxx:29
 RooHistFunc.cxx:30
 RooHistFunc.cxx:31
 RooHistFunc.cxx:32
 RooHistFunc.cxx:33
 RooHistFunc.cxx:34
 RooHistFunc.cxx:35
 RooHistFunc.cxx:36
 RooHistFunc.cxx:37
 RooHistFunc.cxx:38
 RooHistFunc.cxx:39
 RooHistFunc.cxx:40
 RooHistFunc.cxx:41
 RooHistFunc.cxx:42
 RooHistFunc.cxx:43
 RooHistFunc.cxx:44
 RooHistFunc.cxx:45
 RooHistFunc.cxx:46
 RooHistFunc.cxx:47
 RooHistFunc.cxx:48
 RooHistFunc.cxx:49
 RooHistFunc.cxx:50
 RooHistFunc.cxx:51
 RooHistFunc.cxx:52
 RooHistFunc.cxx:53
 RooHistFunc.cxx:54
 RooHistFunc.cxx:55
 RooHistFunc.cxx:56
 RooHistFunc.cxx:57
 RooHistFunc.cxx:58
 RooHistFunc.cxx:59
 RooHistFunc.cxx:60
 RooHistFunc.cxx:61
 RooHistFunc.cxx:62
 RooHistFunc.cxx:63
 RooHistFunc.cxx:64
 RooHistFunc.cxx:65
 RooHistFunc.cxx:66
 RooHistFunc.cxx:67
 RooHistFunc.cxx:68
 RooHistFunc.cxx:69
 RooHistFunc.cxx:70
 RooHistFunc.cxx:71
 RooHistFunc.cxx:72
 RooHistFunc.cxx:73
 RooHistFunc.cxx:74
 RooHistFunc.cxx:75
 RooHistFunc.cxx:76
 RooHistFunc.cxx:77
 RooHistFunc.cxx:78
 RooHistFunc.cxx:79
 RooHistFunc.cxx:80
 RooHistFunc.cxx:81
 RooHistFunc.cxx:82
 RooHistFunc.cxx:83
 RooHistFunc.cxx:84
 RooHistFunc.cxx:85
 RooHistFunc.cxx:86
 RooHistFunc.cxx:87
 RooHistFunc.cxx:88
 RooHistFunc.cxx:89
 RooHistFunc.cxx:90
 RooHistFunc.cxx:91
 RooHistFunc.cxx:92
 RooHistFunc.cxx:93
 RooHistFunc.cxx:94
 RooHistFunc.cxx:95
 RooHistFunc.cxx:96
 RooHistFunc.cxx:97
 RooHistFunc.cxx:98
 RooHistFunc.cxx:99
 RooHistFunc.cxx:100
 RooHistFunc.cxx:101
 RooHistFunc.cxx:102
 RooHistFunc.cxx:103
 RooHistFunc.cxx:104
 RooHistFunc.cxx:105
 RooHistFunc.cxx:106
 RooHistFunc.cxx:107
 RooHistFunc.cxx:108
 RooHistFunc.cxx:109
 RooHistFunc.cxx:110
 RooHistFunc.cxx:111
 RooHistFunc.cxx:112
 RooHistFunc.cxx:113
 RooHistFunc.cxx:114
 RooHistFunc.cxx:115
 RooHistFunc.cxx:116
 RooHistFunc.cxx:117
 RooHistFunc.cxx:118
 RooHistFunc.cxx:119
 RooHistFunc.cxx:120
 RooHistFunc.cxx:121
 RooHistFunc.cxx:122
 RooHistFunc.cxx:123
 RooHistFunc.cxx:124
 RooHistFunc.cxx:125
 RooHistFunc.cxx:126
 RooHistFunc.cxx:127
 RooHistFunc.cxx:128
 RooHistFunc.cxx:129
 RooHistFunc.cxx:130
 RooHistFunc.cxx:131
 RooHistFunc.cxx:132
 RooHistFunc.cxx:133
 RooHistFunc.cxx:134
 RooHistFunc.cxx:135
 RooHistFunc.cxx:136
 RooHistFunc.cxx:137
 RooHistFunc.cxx:138
 RooHistFunc.cxx:139
 RooHistFunc.cxx:140
 RooHistFunc.cxx:141
 RooHistFunc.cxx:142
 RooHistFunc.cxx:143
 RooHistFunc.cxx:144
 RooHistFunc.cxx:145
 RooHistFunc.cxx:146
 RooHistFunc.cxx:147
 RooHistFunc.cxx:148
 RooHistFunc.cxx:149
 RooHistFunc.cxx:150
 RooHistFunc.cxx:151
 RooHistFunc.cxx:152
 RooHistFunc.cxx:153
 RooHistFunc.cxx:154
 RooHistFunc.cxx:155
 RooHistFunc.cxx:156
 RooHistFunc.cxx:157
 RooHistFunc.cxx:158
 RooHistFunc.cxx:159
 RooHistFunc.cxx:160
 RooHistFunc.cxx:161
 RooHistFunc.cxx:162
 RooHistFunc.cxx:163
 RooHistFunc.cxx:164
 RooHistFunc.cxx:165
 RooHistFunc.cxx:166
 RooHistFunc.cxx:167
 RooHistFunc.cxx:168
 RooHistFunc.cxx:169
 RooHistFunc.cxx:170
 RooHistFunc.cxx:171
 RooHistFunc.cxx:172
 RooHistFunc.cxx:173
 RooHistFunc.cxx:174
 RooHistFunc.cxx:175
 RooHistFunc.cxx:176
 RooHistFunc.cxx:177
 RooHistFunc.cxx:178
 RooHistFunc.cxx:179
 RooHistFunc.cxx:180
 RooHistFunc.cxx:181
 RooHistFunc.cxx:182
 RooHistFunc.cxx:183
 RooHistFunc.cxx:184
 RooHistFunc.cxx:185
 RooHistFunc.cxx:186
 RooHistFunc.cxx:187
 RooHistFunc.cxx:188
 RooHistFunc.cxx:189
 RooHistFunc.cxx:190
 RooHistFunc.cxx:191
 RooHistFunc.cxx:192
 RooHistFunc.cxx:193
 RooHistFunc.cxx:194
 RooHistFunc.cxx:195
 RooHistFunc.cxx:196
 RooHistFunc.cxx:197
 RooHistFunc.cxx:198
 RooHistFunc.cxx:199
 RooHistFunc.cxx:200
 RooHistFunc.cxx:201
 RooHistFunc.cxx:202
 RooHistFunc.cxx:203
 RooHistFunc.cxx:204
 RooHistFunc.cxx:205
 RooHistFunc.cxx:206
 RooHistFunc.cxx:207
 RooHistFunc.cxx:208
 RooHistFunc.cxx:209
 RooHistFunc.cxx:210
 RooHistFunc.cxx:211
 RooHistFunc.cxx:212
 RooHistFunc.cxx:213
 RooHistFunc.cxx:214
 RooHistFunc.cxx:215
 RooHistFunc.cxx:216
 RooHistFunc.cxx:217
 RooHistFunc.cxx:218
 RooHistFunc.cxx:219
 RooHistFunc.cxx:220
 RooHistFunc.cxx:221
 RooHistFunc.cxx:222
 RooHistFunc.cxx:223
 RooHistFunc.cxx:224
 RooHistFunc.cxx:225
 RooHistFunc.cxx:226
 RooHistFunc.cxx:227
 RooHistFunc.cxx:228
 RooHistFunc.cxx:229
 RooHistFunc.cxx:230
 RooHistFunc.cxx:231
 RooHistFunc.cxx:232
 RooHistFunc.cxx:233
 RooHistFunc.cxx:234
 RooHistFunc.cxx:235
 RooHistFunc.cxx:236
 RooHistFunc.cxx:237
 RooHistFunc.cxx:238
 RooHistFunc.cxx:239
 RooHistFunc.cxx:240
 RooHistFunc.cxx:241
 RooHistFunc.cxx:242
 RooHistFunc.cxx:243
 RooHistFunc.cxx:244
 RooHistFunc.cxx:245
 RooHistFunc.cxx:246
 RooHistFunc.cxx:247
 RooHistFunc.cxx:248
 RooHistFunc.cxx:249
 RooHistFunc.cxx:250
 RooHistFunc.cxx:251
 RooHistFunc.cxx:252
 RooHistFunc.cxx:253
 RooHistFunc.cxx:254
 RooHistFunc.cxx:255
 RooHistFunc.cxx:256
 RooHistFunc.cxx:257
 RooHistFunc.cxx:258
 RooHistFunc.cxx:259
 RooHistFunc.cxx:260
 RooHistFunc.cxx:261
 RooHistFunc.cxx:262
 RooHistFunc.cxx:263
 RooHistFunc.cxx:264
 RooHistFunc.cxx:265
 RooHistFunc.cxx:266
 RooHistFunc.cxx:267
 RooHistFunc.cxx:268
 RooHistFunc.cxx:269
 RooHistFunc.cxx:270
 RooHistFunc.cxx:271
 RooHistFunc.cxx:272
 RooHistFunc.cxx:273
 RooHistFunc.cxx:274
 RooHistFunc.cxx:275
 RooHistFunc.cxx:276
 RooHistFunc.cxx:277
 RooHistFunc.cxx:278
 RooHistFunc.cxx:279
 RooHistFunc.cxx:280
 RooHistFunc.cxx:281
 RooHistFunc.cxx:282
 RooHistFunc.cxx:283
 RooHistFunc.cxx:284
 RooHistFunc.cxx:285
 RooHistFunc.cxx:286
 RooHistFunc.cxx:287
 RooHistFunc.cxx:288
 RooHistFunc.cxx:289
 RooHistFunc.cxx:290
 RooHistFunc.cxx:291
 RooHistFunc.cxx:292
 RooHistFunc.cxx:293
 RooHistFunc.cxx:294
 RooHistFunc.cxx:295
 RooHistFunc.cxx:296