/*****************************************************************************
 * 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)             *
 *****************************************************************************/

//////////////////////////////////////////////////////////////////////////////
//
// BEGIN_HTML
// RooSimultaneous facilitates simultaneous fitting of multiple PDFs
// to subsets of a given dataset.
// <p>
// The class takes an index category, which is interpreted as
// the data subset indicator, and a list of PDFs, each associated
// with a state of the index category. RooSimultaneous always returns
// the value of the PDF that is associated with the current value
// of the index category
// <p>
// Extended likelihood fitting is supported if all components support
// extended likelihood mode. The expected number of events by a RooSimultaneous
// is that of the component p.d.f. selected by the index category
// END_HTML
//


#include "RooNLLVar.h"

#include "RooStats/HistFactory/RooBarlowBeestonLL.h"
#include "RooStats/HistFactory/HistFactorySimultaneous.h"

using namespace std ;

ClassImp(RooStats::HistFactory::HistFactorySimultaneous)
;


//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::HistFactorySimultaneous(const char *name, const char *title, 
						 RooAbsCategoryLValue& inIndexCat) : 
  RooSimultaneous(name, title, inIndexCat ) {}


//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::HistFactorySimultaneous(const char *name, const char *title, 
				 const RooArgList& inPdfList, RooAbsCategoryLValue& inIndexCat) :
  RooSimultaneous(name, title, inPdfList, inIndexCat) {}


//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::HistFactorySimultaneous(const char *name, const char *title, 
				 map<string,RooAbsPdf*> pdfMap, RooAbsCategoryLValue& inIndexCat) :
  RooSimultaneous(name, title, pdfMap, inIndexCat) {}


//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::HistFactorySimultaneous(const HistFactorySimultaneous& other, const char* name) : 
  RooSimultaneous(other, name) {}

//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::HistFactorySimultaneous(const RooSimultaneous& other, const char* name) : 
  RooSimultaneous(other, name) {}

//_____________________________________________________________________________
RooStats::HistFactory::HistFactorySimultaneous::~HistFactorySimultaneous() 
{
  // Destructor
}


//_____________________________________________________________________________
RooAbsReal* RooStats::HistFactory::HistFactorySimultaneous::createNLL(RooAbsData& data, 
					       const RooCmdArg& arg1, const RooCmdArg& arg2, 
					       const RooCmdArg& arg3, const RooCmdArg& arg4, 
					       const RooCmdArg& arg5, const RooCmdArg& arg6, 
					       const RooCmdArg& arg7, const RooCmdArg& arg8) {
  
  // Probably not necessary because createNLL is virtual...

  RooLinkedList l ;
  l.Add((TObject*)&arg1) ;  l.Add((TObject*)&arg2) ;
  l.Add((TObject*)&arg3) ;  l.Add((TObject*)&arg4) ;
  l.Add((TObject*)&arg5) ;  l.Add((TObject*)&arg6) ;
  l.Add((TObject*)&arg7) ;  l.Add((TObject*)&arg8) ;
  return createNLL(data,l) ;
  
}


//_____________________________________________________________________________

RooAbsReal* RooStats::HistFactory::HistFactorySimultaneous::createNLL(RooAbsData& data, const RooLinkedList& cmdList) {
  
  // We want to overload the method createNLL so it return
  // a RooBarlow-Beeston NLL function, which can be used
  // in HistFactory to minimize statistical uncertainty analytically
  //
  // The only problem is one of ownership
  // This HistFactorySimultaneous and the RooAbsData& data must
  // exist for as long as the RooBarlowBeestonLL does
  //
  // This could be solved if we instead refer to the cloned
  // pdf's and data set in the nll that we create here, but
  // it's unclear how to do so
  //
  // Also, check for ownership/memory issue with the newly created nll
  // and whether RooBarlowBeestonLL owns it, etc

  // Create a standard nll
  RooNLLVar* nll = (RooNLLVar*) RooSimultaneous::createNLL( data, cmdList );

  RooBarlowBeestonLL* bbnll = new RooBarlowBeestonLL("bbnll", "bbnll", *nll); //, *observables);
  bbnll->setPdf( this );
  bbnll->setDataset( &data );
  bbnll->initializeBarlowCache(); 
  
  return bbnll;

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