/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id: RooAddPdf.h,v 1.46 2007/07/12 20:30:28 wouter 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_ADD_PDF
#define ROO_ADD_PDF

#include "RooAbsPdf.h"
#include "RooListProxy.h"
#include "RooSetProxy.h"
#include "RooAICRegistry.h"
#include "RooNormSetCache.h"
#include "RooNameSet.h"
#include "RooCacheManager.h"
#include "RooObjCacheManager.h"
#include "RooNameReg.h"

class RooAddPdf : public RooAbsPdf {
public:

  RooAddPdf() ;
  RooAddPdf(const char *name, const char *title=0);
  RooAddPdf(const char *name, const char *title,
	    RooAbsPdf& pdf1, RooAbsPdf& pdf2, RooAbsReal& coef1) ;
  RooAddPdf(const char *name, const char *title, const RooArgList& pdfList) ;
  RooAddPdf(const char *name, const char *title, const RooArgList& pdfList, const RooArgList& coefList, Bool_t recursiveFraction=kFALSE) ;
  
  RooAddPdf(const RooAddPdf& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooAddPdf(*this,newname) ; }
  virtual ~RooAddPdf() ;

  Double_t evaluate() const ;
  virtual Bool_t checkObservables(const RooArgSet* nset) const ;	

  virtual Bool_t forceAnalyticalInt(const RooAbsArg& /*dep*/) const { 
    // Force RooRealIntegral to offer all observables for internal integration
    return kTRUE ; 
  }
  Int_t getAnalyticalIntegralWN(RooArgSet& allVars, RooArgSet& numVars, const RooArgSet* normSet, const char* rangeName=0) const ;
  Double_t analyticalIntegralWN(Int_t code, const RooArgSet* normSet, const char* rangeName=0) const ;
  virtual Bool_t selfNormalized() const { 
    // P.d.f is self normalized
    return kTRUE ; 
  }

  virtual ExtendMode extendMode() const { 
    // Return extended mode capabilities
    return ((_haveLastCoef&&!_recursive) || _allExtendable) ? MustBeExtended : CanNotBeExtended; 
  }
  virtual Double_t expectedEvents(const RooArgSet* nset) const ;
  virtual Double_t expectedEvents(const RooArgSet& nset) const { 
    // Return expected number of events for extended likelihood calculation
    // which is the sum of all coefficients
    return expectedEvents(&nset) ; 
  }

  const RooArgList& pdfList() const { 
    // Return list of component p.d.fs
    return _pdfList ; 
  }
  const RooArgList& coefList() const { 
    // Return list of coefficients of component p.d.f.s
    return _coefList ; 
  }

  void fixCoefNormalization(const RooArgSet& refCoefNorm) ;  
  void fixCoefRange(const char* rangeName) ;
  
  const RooArgSet& getCoefNormalization() const { return _refCoefNorm ; }
  const char* getCoefRange() const { return _refCoefRangeName?RooNameReg::str(_refCoefRangeName):"" ; }

  virtual void resetErrorCounters(Int_t resetValue=10) ;

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

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

  virtual CacheMode canNodeBeCached() const { return RooAbsArg::NotAdvised ; } ;
  virtual void setCacheAndTrackHints(RooArgSet&) ;

protected:

  virtual void selectNormalization(const RooArgSet* depSet=0, Bool_t force=kFALSE) ;
  virtual void selectNormalizationRange(const char* rangeName=0, Bool_t force=kFALSE) ;

  mutable RooSetProxy _refCoefNorm ;   // Reference observable set for coefficient interpretation
  mutable TNamed* _refCoefRangeName ;  // Reference range name for coefficient interpreation

  Bool_t _projectCoefs ;         // If true coefficients need to be projected for use in evaluate()
  mutable Double_t* _coefCache ; //! Transiet cache with transformed values of coefficients


  class CacheElem : public RooAbsCacheElement {
  public:
    virtual ~CacheElem() {} ;

    RooArgList _suppNormList ; // Supplemental normalization list
    Bool_t    _needSupNorm ; // Does the above list contain any non-unit entries?

    RooArgList _projList ; // Projection integrals to be multiplied with coefficients
    RooArgList _suppProjList ; // Projection integrals to be multiplied with coefficients for supplemental normalization terms
    RooArgList _refRangeProjList ; // Range integrals to be multiplied with coefficients (reference range)
    RooArgList _rangeProjList ; // Range integrals to be multiplied with coefficients (target range)

    virtual RooArgList containedArgs(Action) ;

  } ;
  mutable RooObjCacheManager _projCacheMgr ;  // Manager of cache with coefficient projections and transformations
  CacheElem* getProjCache(const RooArgSet* nset, const RooArgSet* iset=0, const char* rangeName=0) const ;
  void updateCoefficients(CacheElem& cache, const RooArgSet* nset) const ;

  
  friend class RooAddGenContext ;
  virtual RooAbsGenContext* genContext(const RooArgSet &vars, const RooDataSet *prototype=0, 
                                       const RooArgSet* auxProto=0, Bool_t verbose= kFALSE) const ;


  mutable RooAICRegistry _codeReg ;  //! Registry of component analytical integration codes

  RooListProxy _pdfList ;   //  List of component PDFs
  RooListProxy _coefList ;  //  List of coefficients
  mutable RooArgList* _snormList ;  //!  List of supplemental normalization factors
  TIterator* _pdfIter ;     //! Iterator over PDF list
  TIterator* _coefIter ;    //! Iterator over coefficient list
  
  Bool_t _haveLastCoef ;    //  Flag indicating if last PDFs coefficient was supplied in the ctor
  Bool_t _allExtendable ;   //  Flag indicating if all PDF components are extendable
  Bool_t _recursive ;       //  Flag indicating is fractions are treated recursively

  mutable Int_t _coefErrCount ; //! Coefficient error counter

private:

  ClassDef(RooAddPdf,3) // PDF representing a sum of PDFs
};

#endif
 RooAddPdf.h:1
 RooAddPdf.h:2
 RooAddPdf.h:3
 RooAddPdf.h:4
 RooAddPdf.h:5
 RooAddPdf.h:6
 RooAddPdf.h:7
 RooAddPdf.h:8
 RooAddPdf.h:9
 RooAddPdf.h:10
 RooAddPdf.h:11
 RooAddPdf.h:12
 RooAddPdf.h:13
 RooAddPdf.h:14
 RooAddPdf.h:15
 RooAddPdf.h:16
 RooAddPdf.h:17
 RooAddPdf.h:18
 RooAddPdf.h:19
 RooAddPdf.h:20
 RooAddPdf.h:21
 RooAddPdf.h:22
 RooAddPdf.h:23
 RooAddPdf.h:24
 RooAddPdf.h:25
 RooAddPdf.h:26
 RooAddPdf.h:27
 RooAddPdf.h:28
 RooAddPdf.h:29
 RooAddPdf.h:30
 RooAddPdf.h:31
 RooAddPdf.h:32
 RooAddPdf.h:33
 RooAddPdf.h:34
 RooAddPdf.h:35
 RooAddPdf.h:36
 RooAddPdf.h:37
 RooAddPdf.h:38
 RooAddPdf.h:39
 RooAddPdf.h:40
 RooAddPdf.h:41
 RooAddPdf.h:42
 RooAddPdf.h:43
 RooAddPdf.h:44
 RooAddPdf.h:45
 RooAddPdf.h:46
 RooAddPdf.h:47
 RooAddPdf.h:48
 RooAddPdf.h:49
 RooAddPdf.h:50
 RooAddPdf.h:51
 RooAddPdf.h:52
 RooAddPdf.h:53
 RooAddPdf.h:54
 RooAddPdf.h:55
 RooAddPdf.h:56
 RooAddPdf.h:57
 RooAddPdf.h:58
 RooAddPdf.h:59
 RooAddPdf.h:60
 RooAddPdf.h:61
 RooAddPdf.h:62
 RooAddPdf.h:63
 RooAddPdf.h:64
 RooAddPdf.h:65
 RooAddPdf.h:66
 RooAddPdf.h:67
 RooAddPdf.h:68
 RooAddPdf.h:69
 RooAddPdf.h:70
 RooAddPdf.h:71
 RooAddPdf.h:72
 RooAddPdf.h:73
 RooAddPdf.h:74
 RooAddPdf.h:75
 RooAddPdf.h:76
 RooAddPdf.h:77
 RooAddPdf.h:78
 RooAddPdf.h:79
 RooAddPdf.h:80
 RooAddPdf.h:81
 RooAddPdf.h:82
 RooAddPdf.h:83
 RooAddPdf.h:84
 RooAddPdf.h:85
 RooAddPdf.h:86
 RooAddPdf.h:87
 RooAddPdf.h:88
 RooAddPdf.h:89
 RooAddPdf.h:90
 RooAddPdf.h:91
 RooAddPdf.h:92
 RooAddPdf.h:93
 RooAddPdf.h:94
 RooAddPdf.h:95
 RooAddPdf.h:96
 RooAddPdf.h:97
 RooAddPdf.h:98
 RooAddPdf.h:99
 RooAddPdf.h:100
 RooAddPdf.h:101
 RooAddPdf.h:102
 RooAddPdf.h:103
 RooAddPdf.h:104
 RooAddPdf.h:105
 RooAddPdf.h:106
 RooAddPdf.h:107
 RooAddPdf.h:108
 RooAddPdf.h:109
 RooAddPdf.h:110
 RooAddPdf.h:111
 RooAddPdf.h:112
 RooAddPdf.h:113
 RooAddPdf.h:114
 RooAddPdf.h:115
 RooAddPdf.h:116
 RooAddPdf.h:117
 RooAddPdf.h:118
 RooAddPdf.h:119
 RooAddPdf.h:120
 RooAddPdf.h:121
 RooAddPdf.h:122
 RooAddPdf.h:123
 RooAddPdf.h:124
 RooAddPdf.h:125
 RooAddPdf.h:126
 RooAddPdf.h:127
 RooAddPdf.h:128
 RooAddPdf.h:129
 RooAddPdf.h:130
 RooAddPdf.h:131
 RooAddPdf.h:132
 RooAddPdf.h:133
 RooAddPdf.h:134
 RooAddPdf.h:135
 RooAddPdf.h:136
 RooAddPdf.h:137
 RooAddPdf.h:138
 RooAddPdf.h:139
 RooAddPdf.h:140
 RooAddPdf.h:141
 RooAddPdf.h:142
 RooAddPdf.h:143
 RooAddPdf.h:144
 RooAddPdf.h:145
 RooAddPdf.h:146
 RooAddPdf.h:147
 RooAddPdf.h:148
 RooAddPdf.h:149
 RooAddPdf.h:150