/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id: PiecewiseInterpolation.h,v 1.3 2007/05/11 09:11:30 verkerke Exp $
 * 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)             *
 *****************************************************************************/
#ifndef ROO_PIECEWISEINTERPOLATION
#define ROO_PIECEWISEINTERPOLATION

#include "RooAbsReal.h"
#include "RooRealProxy.h"
#include "RooListProxy.h"

#include "RooObjCacheManager.h"

class RooRealVar;
class RooArgList ;

class PiecewiseInterpolation : public RooAbsReal {
public:

  PiecewiseInterpolation() ;
  PiecewiseInterpolation(const char *name, const char *title, const RooAbsReal& nominal, const RooArgList& lowSet, const RooArgList& highSet, const RooArgList& paramSet, Bool_t takeOwnerShip=kFALSE) ;
  virtual ~PiecewiseInterpolation() ;

  PiecewiseInterpolation(const PiecewiseInterpolation& other, const char* name = 0);
  virtual TObject* clone(const char* newname) const { return new PiecewiseInterpolation(*this, newname); }

  //  virtual Double_t defaultErrorLevel() const ;

  //  void printMetaArgs(std::ostream& os) const ;

  const RooArgList& lowList() const { return _lowSet ; }
  const RooArgList& highList() const { return _highSet ; }
  const RooArgList& paramList() const { return _paramSet ; }

  //virtual Bool_t forceAnalyticalInt(const RooAbsArg&) const { return kTRUE ; }
  Bool_t setBinIntegrator(RooArgSet& allVars) ;

  Int_t getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet,const char* rangeName=0) const ;
  Double_t analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName=0) const ;

  void setPositiveDefinite(bool flag=true){_positiveDefinite=flag;}

  void setInterpCode(RooAbsReal& param, int code);
  void setAllInterpCodes(int code);
  void printAllInterpCodes();

  virtual std::list<Double_t>* binBoundaries(RooAbsRealLValue& /*obs*/, Double_t /*xlo*/, Double_t /*xhi*/) const ;
  virtual std::list<Double_t>* plotSamplingHint(RooAbsRealLValue& obs, Double_t xlo, Double_t xhi) const ; 
  virtual Bool_t isBinnedDistribution(const RooArgSet& obs) const ;

protected:

  class CacheElem : public RooAbsCacheElement {
  public:
    CacheElem()  {} ;
    virtual ~CacheElem() {} ; 
    virtual RooArgList containedArgs(Action) { 
      RooArgList ret(_funcIntList) ; 
      ret.add(_lowIntList); 
      ret.add(_highIntList);
      return ret ; 
    }
    RooArgList _funcIntList ;
    RooArgList _lowIntList ;
    RooArgList _highIntList ;
    // will want std::vector<RooRealVar*> for low and high also
  } ;
  mutable RooObjCacheManager _normIntMgr ; // The integration cache manager

  RooRealProxy _nominal;           // The nominal value
  RooArgList   _ownedList ;       // List of owned components
  RooListProxy _lowSet ;            // Low-side variation
  RooListProxy _highSet ;            // High-side varaition
  RooListProxy _paramSet ;            // interpolation parameters
  RooListProxy _normSet ;            // interpolation parameters
  Bool_t _positiveDefinite; // protect against negative and 0 bins.

  std::vector<int> _interpCode;

  Double_t evaluate() const;

  ClassDef(PiecewiseInterpolation,3) // Sum of RooAbsReal objects
};

#endif
 PiecewiseInterpolation.h:1
 PiecewiseInterpolation.h:2
 PiecewiseInterpolation.h:3
 PiecewiseInterpolation.h:4
 PiecewiseInterpolation.h:5
 PiecewiseInterpolation.h:6
 PiecewiseInterpolation.h:7
 PiecewiseInterpolation.h:8
 PiecewiseInterpolation.h:9
 PiecewiseInterpolation.h:10
 PiecewiseInterpolation.h:11
 PiecewiseInterpolation.h:12
 PiecewiseInterpolation.h:13
 PiecewiseInterpolation.h:14
 PiecewiseInterpolation.h:15
 PiecewiseInterpolation.h:16
 PiecewiseInterpolation.h:17
 PiecewiseInterpolation.h:18
 PiecewiseInterpolation.h:19
 PiecewiseInterpolation.h:20
 PiecewiseInterpolation.h:21
 PiecewiseInterpolation.h:22
 PiecewiseInterpolation.h:23
 PiecewiseInterpolation.h:24
 PiecewiseInterpolation.h:25
 PiecewiseInterpolation.h:26
 PiecewiseInterpolation.h:27
 PiecewiseInterpolation.h:28
 PiecewiseInterpolation.h:29
 PiecewiseInterpolation.h:30
 PiecewiseInterpolation.h:31
 PiecewiseInterpolation.h:32
 PiecewiseInterpolation.h:33
 PiecewiseInterpolation.h:34
 PiecewiseInterpolation.h:35
 PiecewiseInterpolation.h:36
 PiecewiseInterpolation.h:37
 PiecewiseInterpolation.h:38
 PiecewiseInterpolation.h:39
 PiecewiseInterpolation.h:40
 PiecewiseInterpolation.h:41
 PiecewiseInterpolation.h:42
 PiecewiseInterpolation.h:43
 PiecewiseInterpolation.h:44
 PiecewiseInterpolation.h:45
 PiecewiseInterpolation.h:46
 PiecewiseInterpolation.h:47
 PiecewiseInterpolation.h:48
 PiecewiseInterpolation.h:49
 PiecewiseInterpolation.h:50
 PiecewiseInterpolation.h:51
 PiecewiseInterpolation.h:52
 PiecewiseInterpolation.h:53
 PiecewiseInterpolation.h:54
 PiecewiseInterpolation.h:55
 PiecewiseInterpolation.h:56
 PiecewiseInterpolation.h:57
 PiecewiseInterpolation.h:58
 PiecewiseInterpolation.h:59
 PiecewiseInterpolation.h:60
 PiecewiseInterpolation.h:61
 PiecewiseInterpolation.h:62
 PiecewiseInterpolation.h:63
 PiecewiseInterpolation.h:64
 PiecewiseInterpolation.h:65
 PiecewiseInterpolation.h:66
 PiecewiseInterpolation.h:67
 PiecewiseInterpolation.h:68
 PiecewiseInterpolation.h:69
 PiecewiseInterpolation.h:70
 PiecewiseInterpolation.h:71
 PiecewiseInterpolation.h:72
 PiecewiseInterpolation.h:73
 PiecewiseInterpolation.h:74
 PiecewiseInterpolation.h:75
 PiecewiseInterpolation.h:76
 PiecewiseInterpolation.h:77
 PiecewiseInterpolation.h:78
 PiecewiseInterpolation.h:79
 PiecewiseInterpolation.h:80
 PiecewiseInterpolation.h:81
 PiecewiseInterpolation.h:82
 PiecewiseInterpolation.h:83
 PiecewiseInterpolation.h:84
 PiecewiseInterpolation.h:85
 PiecewiseInterpolation.h:86
 PiecewiseInterpolation.h:87
 PiecewiseInterpolation.h:88
 PiecewiseInterpolation.h:89
 PiecewiseInterpolation.h:90
 PiecewiseInterpolation.h:91
 PiecewiseInterpolation.h:92
 PiecewiseInterpolation.h:93
 PiecewiseInterpolation.h:94
 PiecewiseInterpolation.h:95
 PiecewiseInterpolation.h:96