/*****************************************************************************
 * 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
// RooGenFitStudy is an abstract base class for RooStudyManager modules
//
// END_HTML
//



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

#include "RooGenFitStudy.h"
#include "RooWorkspace.h"
#include "RooMsgService.h"
#include "RooDataSet.h"
#include "RooAbsPdf.h"
#include "RooRealVar.h"
#include "RooGlobalFunc.h"
#include "RooFitResult.h"


using namespace std ;

ClassImp(RooGenFitStudy)
  ;


//_____________________________________________________________________________
RooGenFitStudy::RooGenFitStudy(const char* name, const char* title) : 
  RooAbsStudy(name?name:"RooGenFitStudy",title?title:"RooGenFitStudy"), 
  _genPdf(0), 
  _fitPdf(0), 
  _genSpec(0),
  _nllVar(0),
  _ngenVar(0),
  _params(0),
  _initParams(0)
{  
  // Constructor
}



//_____________________________________________________________________________
RooGenFitStudy::RooGenFitStudy(const RooGenFitStudy& other) : 
  RooAbsStudy(other),
  _genPdfName(other._genPdfName),
  _genObsName(other._genObsName),
  _fitPdfName(other._fitPdfName),
  _fitObsName(other._fitObsName),
  _genPdf(0),
  _fitPdf(0),
  _genSpec(0),
  _nllVar(0),
  _ngenVar(0),
  _params(0),
  _initParams(0)
{  
  // Copy constructor
  TIterator* giter = other._genOpts.MakeIterator() ;
  TObject* o ;
  while((o=giter->Next())) {
    _genOpts.Add(o->Clone()) ;
  }
  delete giter ;

  TIterator* fiter = other._fitOpts.MakeIterator() ;
  while((o=fiter->Next())) {
    _fitOpts.Add(o->Clone()) ;
  }
  delete fiter ;

}



//_____________________________________________________________________________
RooGenFitStudy::~RooGenFitStudy()
{
  if (_params) delete _params ;
}



//_____________________________________________________________________________
Bool_t RooGenFitStudy::attach(RooWorkspace& w) 
{ 
  // Function called after insertion into workspace
  Bool_t ret = kFALSE ;

  RooAbsPdf* pdf = w.pdf(_genPdfName.c_str()) ;
  if (pdf) {
    _genPdf = pdf ;
  } else {
    coutE(InputArguments) << "RooGenFitStudy(" << GetName() << ") ERROR: generator p.d.f named " << _genPdfName << " not found in workspace " << w.GetName() << endl ;
    ret = kTRUE ;
  }

  _genObs.add(w.argSet(_genObsName.c_str())) ;
  if (_genObs.getSize()==0) {
    coutE(InputArguments) << "RooGenFitStudy(" << GetName() << ") ERROR: no generator observables defined" << endl ;
    ret = kTRUE ;
  }

  pdf = w.pdf(_fitPdfName.c_str()) ;
  if (pdf) {
    _fitPdf = pdf ;
  } else {
    coutE(InputArguments) << "RooGenFitStudy(" << GetName() << ") ERROR: fitting p.d.f named " << _fitPdfName << " not found in workspace " << w.GetName() << endl ;
    ret = kTRUE ;
  }

  _fitObs.add(w.argSet(_fitObsName.c_str())) ;
  if (_fitObs.getSize()==0) {
    coutE(InputArguments) << "RooGenFitStudy(" << GetName() << ") ERROR: no fitting observables defined" << endl ;
    ret = kTRUE ;
  }

  return ret ; 
} 



//_____________________________________________________________________________
void RooGenFitStudy::setGenConfig(const char* pdfName, const char* obsName, const RooCmdArg& arg1,const RooCmdArg& arg2,const RooCmdArg& arg3) 
{
  _genPdfName = pdfName ;
  _genObsName = obsName ;
  _genOpts.Add(arg1.Clone()) ;
  _genOpts.Add(arg2.Clone()) ;
  _genOpts.Add(arg3.Clone()) ;
}



//_____________________________________________________________________________
void RooGenFitStudy::setFitConfig(const char* pdfName, const char* obsName, const RooCmdArg& arg1,const RooCmdArg& arg2,const RooCmdArg& arg3) 
{
  _fitPdfName = pdfName ;
  _fitObsName = obsName ;
  _fitOpts.Add(arg1.Clone()) ;
  _fitOpts.Add(arg2.Clone()) ;
  _fitOpts.Add(arg3.Clone()) ;
}



//_____________________________________________________________________________
Bool_t RooGenFitStudy::initialize() 
{ 
  // One-time initialization of study 

  _nllVar = new RooRealVar("NLL","-log(Likelihood)",0) ;
  _ngenVar = new RooRealVar("ngen","number of generated events",0) ;
  
  _params = _fitPdf->getParameters(_genObs) ;
  RooArgSet modelParams(*_params) ;
  _initParams = (RooArgSet*) _params->snapshot() ;
  _params->add(*_nllVar) ;
  _params->add(*_ngenVar) ;

  _genSpec = _genPdf->prepareMultiGen(_genObs,(RooCmdArg&)*_genOpts.At(0),(RooCmdArg&)*_genOpts.At(1),(RooCmdArg&)*_genOpts.At(2)) ;

  registerSummaryOutput(*_params,modelParams) ;
  return kFALSE ;
} 



//_____________________________________________________________________________
Bool_t RooGenFitStudy::execute() 
{ 
  // Execute one study iteration
  *_params = *_initParams ;
  RooDataSet* data = _genPdf->generate(*_genSpec) ;
  RooFitResult* fr  = _fitPdf->fitTo(*data,RooFit::Save(kTRUE),(RooCmdArg&)*_fitOpts.At(0),(RooCmdArg&)*_fitOpts.At(1),(RooCmdArg&)*_fitOpts.At(2)) ;

  if (fr->status()==0) {
    _ngenVar->setVal(data->sumEntries()) ;
    _nllVar->setVal(fr->minNll()) ;
    storeSummaryOutput(*_params) ;
    storeDetailedOutput(*fr) ;
  }

  delete data ;
  return kFALSE ;
} 



//_____________________________________________________________________________
Bool_t RooGenFitStudy::finalize() 
{ 
  // Finalization of study
  delete _params ;
  delete _nllVar ;
  delete _ngenVar ;
  delete _initParams ;
  delete _genSpec ;
  _params = 0 ;
  _nllVar = 0 ;
  _ngenVar = 0 ;
  _initParams = 0 ;
  _genSpec = 0 ;
  

  return kFALSE ; 
} 


//_____________________________________________________________________________
void RooGenFitStudy::Print(Option_t* /*options*/) const
{
}


 RooGenFitStudy.cxx:1
 RooGenFitStudy.cxx:2
 RooGenFitStudy.cxx:3
 RooGenFitStudy.cxx:4
 RooGenFitStudy.cxx:5
 RooGenFitStudy.cxx:6
 RooGenFitStudy.cxx:7
 RooGenFitStudy.cxx:8
 RooGenFitStudy.cxx:9
 RooGenFitStudy.cxx:10
 RooGenFitStudy.cxx:11
 RooGenFitStudy.cxx:12
 RooGenFitStudy.cxx:13
 RooGenFitStudy.cxx:14
 RooGenFitStudy.cxx:15
 RooGenFitStudy.cxx:16
 RooGenFitStudy.cxx:17
 RooGenFitStudy.cxx:18
 RooGenFitStudy.cxx:19
 RooGenFitStudy.cxx:20
 RooGenFitStudy.cxx:21
 RooGenFitStudy.cxx:22
 RooGenFitStudy.cxx:23
 RooGenFitStudy.cxx:24
 RooGenFitStudy.cxx:25
 RooGenFitStudy.cxx:26
 RooGenFitStudy.cxx:27
 RooGenFitStudy.cxx:28
 RooGenFitStudy.cxx:29
 RooGenFitStudy.cxx:30
 RooGenFitStudy.cxx:31
 RooGenFitStudy.cxx:32
 RooGenFitStudy.cxx:33
 RooGenFitStudy.cxx:34
 RooGenFitStudy.cxx:35
 RooGenFitStudy.cxx:36
 RooGenFitStudy.cxx:37
 RooGenFitStudy.cxx:38
 RooGenFitStudy.cxx:39
 RooGenFitStudy.cxx:40
 RooGenFitStudy.cxx:41
 RooGenFitStudy.cxx:42
 RooGenFitStudy.cxx:43
 RooGenFitStudy.cxx:44
 RooGenFitStudy.cxx:45
 RooGenFitStudy.cxx:46
 RooGenFitStudy.cxx:47
 RooGenFitStudy.cxx:48
 RooGenFitStudy.cxx:49
 RooGenFitStudy.cxx:50
 RooGenFitStudy.cxx:51
 RooGenFitStudy.cxx:52
 RooGenFitStudy.cxx:53
 RooGenFitStudy.cxx:54
 RooGenFitStudy.cxx:55
 RooGenFitStudy.cxx:56
 RooGenFitStudy.cxx:57
 RooGenFitStudy.cxx:58
 RooGenFitStudy.cxx:59
 RooGenFitStudy.cxx:60
 RooGenFitStudy.cxx:61
 RooGenFitStudy.cxx:62
 RooGenFitStudy.cxx:63
 RooGenFitStudy.cxx:64
 RooGenFitStudy.cxx:65
 RooGenFitStudy.cxx:66
 RooGenFitStudy.cxx:67
 RooGenFitStudy.cxx:68
 RooGenFitStudy.cxx:69
 RooGenFitStudy.cxx:70
 RooGenFitStudy.cxx:71
 RooGenFitStudy.cxx:72
 RooGenFitStudy.cxx:73
 RooGenFitStudy.cxx:74
 RooGenFitStudy.cxx:75
 RooGenFitStudy.cxx:76
 RooGenFitStudy.cxx:77
 RooGenFitStudy.cxx:78
 RooGenFitStudy.cxx:79
 RooGenFitStudy.cxx:80
 RooGenFitStudy.cxx:81
 RooGenFitStudy.cxx:82
 RooGenFitStudy.cxx:83
 RooGenFitStudy.cxx:84
 RooGenFitStudy.cxx:85
 RooGenFitStudy.cxx:86
 RooGenFitStudy.cxx:87
 RooGenFitStudy.cxx:88
 RooGenFitStudy.cxx:89
 RooGenFitStudy.cxx:90
 RooGenFitStudy.cxx:91
 RooGenFitStudy.cxx:92
 RooGenFitStudy.cxx:93
 RooGenFitStudy.cxx:94
 RooGenFitStudy.cxx:95
 RooGenFitStudy.cxx:96
 RooGenFitStudy.cxx:97
 RooGenFitStudy.cxx:98
 RooGenFitStudy.cxx:99
 RooGenFitStudy.cxx:100
 RooGenFitStudy.cxx:101
 RooGenFitStudy.cxx:102
 RooGenFitStudy.cxx:103
 RooGenFitStudy.cxx:104
 RooGenFitStudy.cxx:105
 RooGenFitStudy.cxx:106
 RooGenFitStudy.cxx:107
 RooGenFitStudy.cxx:108
 RooGenFitStudy.cxx:109
 RooGenFitStudy.cxx:110
 RooGenFitStudy.cxx:111
 RooGenFitStudy.cxx:112
 RooGenFitStudy.cxx:113
 RooGenFitStudy.cxx:114
 RooGenFitStudy.cxx:115
 RooGenFitStudy.cxx:116
 RooGenFitStudy.cxx:117
 RooGenFitStudy.cxx:118
 RooGenFitStudy.cxx:119
 RooGenFitStudy.cxx:120
 RooGenFitStudy.cxx:121
 RooGenFitStudy.cxx:122
 RooGenFitStudy.cxx:123
 RooGenFitStudy.cxx:124
 RooGenFitStudy.cxx:125
 RooGenFitStudy.cxx:126
 RooGenFitStudy.cxx:127
 RooGenFitStudy.cxx:128
 RooGenFitStudy.cxx:129
 RooGenFitStudy.cxx:130
 RooGenFitStudy.cxx:131
 RooGenFitStudy.cxx:132
 RooGenFitStudy.cxx:133
 RooGenFitStudy.cxx:134
 RooGenFitStudy.cxx:135
 RooGenFitStudy.cxx:136
 RooGenFitStudy.cxx:137
 RooGenFitStudy.cxx:138
 RooGenFitStudy.cxx:139
 RooGenFitStudy.cxx:140
 RooGenFitStudy.cxx:141
 RooGenFitStudy.cxx:142
 RooGenFitStudy.cxx:143
 RooGenFitStudy.cxx:144
 RooGenFitStudy.cxx:145
 RooGenFitStudy.cxx:146
 RooGenFitStudy.cxx:147
 RooGenFitStudy.cxx:148
 RooGenFitStudy.cxx:149
 RooGenFitStudy.cxx:150
 RooGenFitStudy.cxx:151
 RooGenFitStudy.cxx:152
 RooGenFitStudy.cxx:153
 RooGenFitStudy.cxx:154
 RooGenFitStudy.cxx:155
 RooGenFitStudy.cxx:156
 RooGenFitStudy.cxx:157
 RooGenFitStudy.cxx:158
 RooGenFitStudy.cxx:159
 RooGenFitStudy.cxx:160
 RooGenFitStudy.cxx:161
 RooGenFitStudy.cxx:162
 RooGenFitStudy.cxx:163
 RooGenFitStudy.cxx:164
 RooGenFitStudy.cxx:165
 RooGenFitStudy.cxx:166
 RooGenFitStudy.cxx:167
 RooGenFitStudy.cxx:168
 RooGenFitStudy.cxx:169
 RooGenFitStudy.cxx:170
 RooGenFitStudy.cxx:171
 RooGenFitStudy.cxx:172
 RooGenFitStudy.cxx:173
 RooGenFitStudy.cxx:174
 RooGenFitStudy.cxx:175
 RooGenFitStudy.cxx:176
 RooGenFitStudy.cxx:177
 RooGenFitStudy.cxx:178
 RooGenFitStudy.cxx:179
 RooGenFitStudy.cxx:180
 RooGenFitStudy.cxx:181
 RooGenFitStudy.cxx:182
 RooGenFitStudy.cxx:183
 RooGenFitStudy.cxx:184
 RooGenFitStudy.cxx:185
 RooGenFitStudy.cxx:186
 RooGenFitStudy.cxx:187
 RooGenFitStudy.cxx:188
 RooGenFitStudy.cxx:189
 RooGenFitStudy.cxx:190
 RooGenFitStudy.cxx:191
 RooGenFitStudy.cxx:192
 RooGenFitStudy.cxx:193
 RooGenFitStudy.cxx:194
 RooGenFitStudy.cxx:195
 RooGenFitStudy.cxx:196
 RooGenFitStudy.cxx:197
 RooGenFitStudy.cxx:198
 RooGenFitStudy.cxx:199
 RooGenFitStudy.cxx:200
 RooGenFitStudy.cxx:201
 RooGenFitStudy.cxx:202
 RooGenFitStudy.cxx:203
 RooGenFitStudy.cxx:204
 RooGenFitStudy.cxx:205
 RooGenFitStudy.cxx:206
 RooGenFitStudy.cxx:207
 RooGenFitStudy.cxx:208
 RooGenFitStudy.cxx:209
 RooGenFitStudy.cxx:210
 RooGenFitStudy.cxx:211
 RooGenFitStudy.cxx:212
 RooGenFitStudy.cxx:213
 RooGenFitStudy.cxx:214
 RooGenFitStudy.cxx:215
 RooGenFitStudy.cxx:216
 RooGenFitStudy.cxx:217
 RooGenFitStudy.cxx:218
 RooGenFitStudy.cxx:219
 RooGenFitStudy.cxx:220
 RooGenFitStudy.cxx:221
 RooGenFitStudy.cxx:222
 RooGenFitStudy.cxx:223
 RooGenFitStudy.cxx:224
 RooGenFitStudy.cxx:225
 RooGenFitStudy.cxx:226
 RooGenFitStudy.cxx:227
 RooGenFitStudy.cxx:228
 RooGenFitStudy.cxx:229
 RooGenFitStudy.cxx:230
 RooGenFitStudy.cxx:231
 RooGenFitStudy.cxx:232
 RooGenFitStudy.cxx:233
 RooGenFitStudy.cxx:234