/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id$
 * 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)             *
 *****************************************************************************/

///////////////////////////////////////////////////////////////////////////
//  RooExtendPdf is a wrappper around an existing PDF that adds a 
//  parameteric extended likelihood term to the PDF, optionally divided by a 
//  fractional term from a partial normalization of the PDF:
//
//  nExpected = N   _or Expected = N / frac 
//
//  where N is supplied as a RooAbsReal to RooExtendPdf.
//  The fractional term is defined as
//                          _       _ _   _  _
//            Int(cutRegion[x]) pdf(x,y) dx dy 
//     frac = ---------------_-------_-_---_--_ 
//            Int(normRegion[x]) pdf(x,y) dx dy 
//
//        _                                                               _
//  where x is the set of dependents involved in the selection region and y
//  is the set of remaining dependents.
//            _
//  cutRegion[x] is an limited integration range that is contained in
//  the nominal integration range normRegion[x[]
//

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

#include "RooExtendPdf.h"
#include "RooExtendPdf.h"
#include "RooArgList.h"
#include "RooRealVar.h"
#include "RooFormulaVar.h"
#include "RooNameReg.h"
#include "RooMsgService.h"



using namespace std;

ClassImp(RooExtendPdf)
;


RooExtendPdf::RooExtendPdf() : _rangeName(0)
{
  // Default constructor
}

RooExtendPdf::RooExtendPdf(const char *name, const char *title, const RooAbsPdf& pdf,
			   const RooAbsReal& norm, const char* rangeName) :
  RooAbsPdf(name,title),
  _pdf("pdf","PDF",this,(RooAbsReal&)pdf),
  _n("n","Normalization",this,(RooAbsReal&)norm),
  _rangeName(RooNameReg::ptr(rangeName))
{
  // Constructor. The ExtendedPdf behaves identical to the supplied input pdf,
  // but adds an extended likelihood term. The expected number of events return
  // is 'norm'. If a rangename is given, the number of events is interpreted as
#  // the number of events in the given range

  // Copy various setting from pdf
  setUnit(_pdf.arg().getUnit()) ;
  setPlotLabel(_pdf.arg().getPlotLabel()) ;
}



RooExtendPdf::RooExtendPdf(const RooExtendPdf& other, const char* name) :
  RooAbsPdf(other,name),
  _pdf("pdf",this,other._pdf),
  _n("n",this,other._n),
  _rangeName(other._rangeName)
{
  // Copy constructor
}


RooExtendPdf::~RooExtendPdf() 
{
  // Destructor

}



Double_t RooExtendPdf::expectedEvents(const RooArgSet* nset) const 
{
  // Return the number of expected events, which is
  //
  // n / [ Int(xC,yF) pdf(x,y) / Int(xF,yF) pdf(x,y) ]
  //
  // Where x is the set of dependents with cuts defined
  // and y are the other dependents. xC is the integration
  // of x over the cut range, xF is the integration of
  // x over the full range.

  RooAbsPdf& pdf = (RooAbsPdf&)_pdf.arg() ;

  if (_rangeName && (!nset || nset->getSize()==0)) {
    coutW(InputArguments) << "RooExtendPdf::expectedEvents(" << GetName() << ") WARNING: RooExtendPdf needs non-null normalization set to calculate fraction in range " 
			  << _rangeName << ".  Results may be nonsensical" << endl ;  
  }

  Double_t nExp = _n ;

  // Optionally multiply with fractional normalization
  if (_rangeName) {

    globalSelectComp(kTRUE) ;
    Double_t fracInt = pdf.getNormObj(nset,nset,_rangeName)->getVal() ;
    globalSelectComp(kFALSE) ;


    if ( fracInt == 0. || _n == 0.) {
      coutW(Eval) << "RooExtendPdf(" << GetName() << ") WARNING: nExpected = " << _n << " / " 
		  << fracInt << " for nset = " << (nset?*nset:RooArgSet()) << endl ;
    }

    nExp /= fracInt ;    


    // cout << "RooExtendPdf::expectedEvents(" << GetName() << ") fracInt = " << fracInt << " _n = " << _n << " nExpect = " << nExp << endl ;

  }

  // Multiply with original Nexpected, if defined
  if (pdf.canBeExtended()) nExp *= pdf.expectedEvents(nset) ;

  return nExp ;
}



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