/*****************************************************************************
 * Project: RooFit                                                           *
 *                                                                           *
 * Copyright (c) 2000-2007, 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 ROOLINEARMORPH
#define ROOLINEARMORPH

#include "RooAbsCachedPdf.h"
#include "RooRealProxy.h"
#include "RooCategoryProxy.h"
#include "RooAbsReal.h"
#include "RooAbsCategory.h"
class RooBrentRootFinder ;

class TH1D ;
 
class RooIntegralMorph : public RooAbsCachedPdf {
public:
  RooIntegralMorph() {
    // coverity[UNINIT_CTOR]
  } ; 
  RooIntegralMorph(const char *name, const char *title,
	      RooAbsReal& _pdf1,
	      RooAbsReal& _pdf2,
  	      RooAbsReal& _x,
	      RooAbsReal& _alpha, Bool_t cacheAlpha=kFALSE);
  RooIntegralMorph(const RooIntegralMorph& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooIntegralMorph(*this,newname); }
  inline virtual ~RooIntegralMorph() { }

  Bool_t selfNormalized() const { 
    // P.d.f is self normalized
    return kTRUE ; 
  }
  void setCacheAlpha(Bool_t flag) { 
    // Activate caching of p.d.f. shape for all values of alpha as well
    _cacheMgr.sterilize() ; _cacheAlpha = flag ; 
  }
  Bool_t cacheAlpha() const { 
    // If true caching of p.d.f for all alpha values is active
    return _cacheAlpha ; 
  }

  virtual void preferredObservableScanOrder(const RooArgSet& obs, RooArgSet& orderedObs) const ;

  class MorphCacheElem : public PdfCacheElem {
  public:
    MorphCacheElem(RooIntegralMorph& self, const RooArgSet* nset) ;
    ~MorphCacheElem() ;
    void calculate(TIterator* iter) ;
    virtual RooArgList containedArgs(Action) ;

  protected:
    
    void findRange() ;
    Double_t calcX(Double_t y, Bool_t& ok) ;
    Int_t binX(Double_t x) ;
    void fillGap(Int_t ixlo, Int_t ixhi,Double_t splitPoint=0.5) ;
    void interpolateGap(Int_t ixlo, Int_t ixhi) ;

    RooIntegralMorph* _self ; //
    RooArgSet* _nset ; 
    RooAbsPdf* _pdf1 ; // PDF1
    RooAbsPdf* _pdf2 ; // PDF2
    RooRealVar* _x   ; // X
    RooAbsReal* _alpha ; // ALPHA 
    RooAbsReal* _c1 ; // CDF of PDF 1
    RooAbsReal* _c2 ; // CDF of PDF 2
    RooAbsFunc* _cb1 ; // Binding of CDF1
    RooAbsFunc* _cb2 ; // Binding of CDF2
    RooBrentRootFinder* _rf1 ; // ROOT finder on CDF1
    RooBrentRootFinder* _rf2 ; // ROOT finder of CDF2 ;

    Double_t* _yatX ; //
    Double_t* _calcX; //
    Int_t _yatXmin, _yatXmax ;
    Int_t _ccounter ;

    Double_t _ycutoff ;

  } ;

protected:

  friend class MorphCacheElem ;  
  virtual PdfCacheElem* createCache(const RooArgSet* nset) const ;
  virtual const char* inputBaseName() const ;
  virtual RooArgSet* actualObservables(const RooArgSet& nset) const ;
  virtual RooArgSet* actualParameters(const RooArgSet& nset) const ;
  virtual void fillCacheObject(PdfCacheElem& cache) const ;
  
  RooRealProxy pdf1 ; // First input shape
  RooRealProxy pdf2 ; // Second input shape
  RooRealProxy x ;    // Observable
  RooRealProxy alpha ; // Interpolation parameter
  Bool_t _cacheAlpha ; // If true, both (x,alpha) are cached
  mutable MorphCacheElem* _cache ; // Current morph cache element in use

 
  Double_t evaluate() const ;

private:

  ClassDef(RooIntegralMorph,1) // Linear shape interpolation operator p.d.f
};
 
#endif
 RooIntegralMorph.h:1
 RooIntegralMorph.h:2
 RooIntegralMorph.h:3
 RooIntegralMorph.h:4
 RooIntegralMorph.h:5
 RooIntegralMorph.h:6
 RooIntegralMorph.h:7
 RooIntegralMorph.h:8
 RooIntegralMorph.h:9
 RooIntegralMorph.h:10
 RooIntegralMorph.h:11
 RooIntegralMorph.h:12
 RooIntegralMorph.h:13
 RooIntegralMorph.h:14
 RooIntegralMorph.h:15
 RooIntegralMorph.h:16
 RooIntegralMorph.h:17
 RooIntegralMorph.h:18
 RooIntegralMorph.h:19
 RooIntegralMorph.h:20
 RooIntegralMorph.h:21
 RooIntegralMorph.h:22
 RooIntegralMorph.h:23
 RooIntegralMorph.h:24
 RooIntegralMorph.h:25
 RooIntegralMorph.h:26
 RooIntegralMorph.h:27
 RooIntegralMorph.h:28
 RooIntegralMorph.h:29
 RooIntegralMorph.h:30
 RooIntegralMorph.h:31
 RooIntegralMorph.h:32
 RooIntegralMorph.h:33
 RooIntegralMorph.h:34
 RooIntegralMorph.h:35
 RooIntegralMorph.h:36
 RooIntegralMorph.h:37
 RooIntegralMorph.h:38
 RooIntegralMorph.h:39
 RooIntegralMorph.h:40
 RooIntegralMorph.h:41
 RooIntegralMorph.h:42
 RooIntegralMorph.h:43
 RooIntegralMorph.h:44
 RooIntegralMorph.h:45
 RooIntegralMorph.h:46
 RooIntegralMorph.h:47
 RooIntegralMorph.h:48
 RooIntegralMorph.h:49
 RooIntegralMorph.h:50
 RooIntegralMorph.h:51
 RooIntegralMorph.h:52
 RooIntegralMorph.h:53
 RooIntegralMorph.h:54
 RooIntegralMorph.h:55
 RooIntegralMorph.h:56
 RooIntegralMorph.h:57
 RooIntegralMorph.h:58
 RooIntegralMorph.h:59
 RooIntegralMorph.h:60
 RooIntegralMorph.h:61
 RooIntegralMorph.h:62
 RooIntegralMorph.h:63
 RooIntegralMorph.h:64
 RooIntegralMorph.h:65
 RooIntegralMorph.h:66
 RooIntegralMorph.h:67
 RooIntegralMorph.h:68
 RooIntegralMorph.h:69
 RooIntegralMorph.h:70
 RooIntegralMorph.h:71
 RooIntegralMorph.h:72
 RooIntegralMorph.h:73
 RooIntegralMorph.h:74
 RooIntegralMorph.h:75
 RooIntegralMorph.h:76
 RooIntegralMorph.h:77
 RooIntegralMorph.h:78
 RooIntegralMorph.h:79
 RooIntegralMorph.h:80
 RooIntegralMorph.h:81
 RooIntegralMorph.h:82
 RooIntegralMorph.h:83
 RooIntegralMorph.h:84
 RooIntegralMorph.h:85
 RooIntegralMorph.h:86
 RooIntegralMorph.h:87
 RooIntegralMorph.h:88
 RooIntegralMorph.h:89
 RooIntegralMorph.h:90
 RooIntegralMorph.h:91
 RooIntegralMorph.h:92
 RooIntegralMorph.h:93
 RooIntegralMorph.h:94
 RooIntegralMorph.h:95
 RooIntegralMorph.h:96
 RooIntegralMorph.h:97
 RooIntegralMorph.h:98
 RooIntegralMorph.h:99
 RooIntegralMorph.h:100
 RooIntegralMorph.h:101
 RooIntegralMorph.h:102
 RooIntegralMorph.h:103
 RooIntegralMorph.h:104
 RooIntegralMorph.h:105
 RooIntegralMorph.h:106
 RooIntegralMorph.h:107
 RooIntegralMorph.h:108
 RooIntegralMorph.h:109
 RooIntegralMorph.h:110
 RooIntegralMorph.h:111
 RooIntegralMorph.h:112
 RooIntegralMorph.h:113
 RooIntegralMorph.h:114