/*****************************************************************************
 * Project: RooFit                                                           *
 *                                                                           *
 * 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 ROOPROJECTEDPDF
#define ROOPROJECTEDPDF

#include "RooAbsPdf.h"
#include "RooRealProxy.h"
#include "RooAbsReal.h"
#include "RooObjCacheManager.h"
#include "RooSetProxy.h" 

class RooProjectedPdf : public RooAbsPdf {
public:

  RooProjectedPdf() ;
  RooProjectedPdf(const char *name, const char *title,  RooAbsReal& _intpdf, const RooArgSet& intObs);
  RooProjectedPdf(const RooProjectedPdf& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooProjectedPdf(*this,newname); }
  inline virtual ~RooProjectedPdf() { }

  // Analytical integration support
  virtual Int_t getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& analVars, const RooArgSet* normSet, const char* rangeName=0) const ;
  virtual Double_t analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName=0) const ;
  virtual Bool_t forceAnalyticalInt(const RooAbsArg& dep) const ;

  virtual Double_t getValV(const RooArgSet* set=0) const ;

  Int_t getGenerator(const RooArgSet& directVars, RooArgSet &generateVars, Bool_t staticInitOK=kTRUE) const;
  void initGenerator(Int_t /*code*/) {} ; // optional pre-generation initialization
  void generateEvent(Int_t code);

  virtual Bool_t selfNormalized() const { return kTRUE ; }

  // Handle projection of projection explicitly
  virtual RooAbsPdf* createProjection(const RooArgSet& iset) ;  

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


protected:

  RooRealProxy intpdf ; // p.d.f that is integrated
  RooSetProxy intobs ;  // observables that p.d.f is integrated over
  RooSetProxy deps ;    // dependents of this p.d.f

  class CacheElem : public RooAbsCacheElement {
  public:
    virtual ~CacheElem() { delete _projection ; } ;
    // Payload
    RooAbsReal* _projection ;
    // Cache management functions
    virtual RooArgList containedArgs(Action) ; 
    virtual void printCompactTreeHook(std::ostream&, const char *, Int_t, Int_t) ;
  } ;
  mutable RooObjCacheManager _cacheMgr ; //! The cache manager

  Bool_t redirectServersHook(const RooAbsCollection& newServerList, Bool_t /*mustReplaceAll*/, Bool_t /*nameChange*/, Bool_t /*isRecursive*/) ;
  
  mutable RooArgSet* _curNormSet ; //!

  const RooAbsReal* getProjection(const RooArgSet* iset, const RooArgSet* nset, const char* rangeName, int& code) const ;
  Double_t evaluate() const ;

private:

  ClassDef(RooProjectedPdf,1) // Operator p.d.f calculating projection of another p.d.f
};
 
#endif
 RooProjectedPdf.h:1
 RooProjectedPdf.h:2
 RooProjectedPdf.h:3
 RooProjectedPdf.h:4
 RooProjectedPdf.h:5
 RooProjectedPdf.h:6
 RooProjectedPdf.h:7
 RooProjectedPdf.h:8
 RooProjectedPdf.h:9
 RooProjectedPdf.h:10
 RooProjectedPdf.h:11
 RooProjectedPdf.h:12
 RooProjectedPdf.h:13
 RooProjectedPdf.h:14
 RooProjectedPdf.h:15
 RooProjectedPdf.h:16
 RooProjectedPdf.h:17
 RooProjectedPdf.h:18
 RooProjectedPdf.h:19
 RooProjectedPdf.h:20
 RooProjectedPdf.h:21
 RooProjectedPdf.h:22
 RooProjectedPdf.h:23
 RooProjectedPdf.h:24
 RooProjectedPdf.h:25
 RooProjectedPdf.h:26
 RooProjectedPdf.h:27
 RooProjectedPdf.h:28
 RooProjectedPdf.h:29
 RooProjectedPdf.h:30
 RooProjectedPdf.h:31
 RooProjectedPdf.h:32
 RooProjectedPdf.h:33
 RooProjectedPdf.h:34
 RooProjectedPdf.h:35
 RooProjectedPdf.h:36
 RooProjectedPdf.h:37
 RooProjectedPdf.h:38
 RooProjectedPdf.h:39
 RooProjectedPdf.h:40
 RooProjectedPdf.h:41
 RooProjectedPdf.h:42
 RooProjectedPdf.h:43
 RooProjectedPdf.h:44
 RooProjectedPdf.h:45
 RooProjectedPdf.h:46
 RooProjectedPdf.h:47
 RooProjectedPdf.h:48
 RooProjectedPdf.h:49
 RooProjectedPdf.h:50
 RooProjectedPdf.h:51
 RooProjectedPdf.h:52
 RooProjectedPdf.h:53
 RooProjectedPdf.h:54
 RooProjectedPdf.h:55
 RooProjectedPdf.h:56
 RooProjectedPdf.h:57
 RooProjectedPdf.h:58
 RooProjectedPdf.h:59
 RooProjectedPdf.h:60
 RooProjectedPdf.h:61
 RooProjectedPdf.h:62
 RooProjectedPdf.h:63
 RooProjectedPdf.h:64
 RooProjectedPdf.h:65
 RooProjectedPdf.h:66
 RooProjectedPdf.h:67
 RooProjectedPdf.h:68
 RooProjectedPdf.h:69
 RooProjectedPdf.h:70
 RooProjectedPdf.h:71
 RooProjectedPdf.h:72
 RooProjectedPdf.h:73
 RooProjectedPdf.h:74
 RooProjectedPdf.h:75
 RooProjectedPdf.h:76
 RooProjectedPdf.h:77
 RooProjectedPdf.h:78