ROOT logo
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 * @(#)root/roofitcore:$Id: RooNLLVar.cxx 28963 2009-06-12 15:47:45Z wouter $
 * 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
// Class RooNLLVar implements a a -log(likelihood) calculation from a dataset
// and a PDF. The NLL is calculated as 
// <pre>
//  Sum[data] -log( pdf(x_data) )
// </pre>
// In extended mode, a (Nexpect - Nobserved*log(NExpected) term is added
// END_HTML
//

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

#include "RooNLLVar.h"
#include "RooAbsData.h"
#include "RooAbsPdf.h"
#include "RooCmdConfig.h"
#include "RooMsgService.h"

#include "RooRealVar.h"


ClassImp(RooNLLVar)
;

RooArgSet RooNLLVar::_emptySet ;


//_____________________________________________________________________________
RooNLLVar::RooNLLVar(const char *name, const char* title, RooAbsPdf& pdf, RooAbsData& indata,
		     const RooCmdArg& arg1, const RooCmdArg& arg2,const RooCmdArg& arg3,
		     const RooCmdArg& arg4, const RooCmdArg& arg5,const RooCmdArg& arg6,
		     const RooCmdArg& arg7, const RooCmdArg& arg8,const RooCmdArg& arg9) :
  RooAbsOptTestStatistic(name,title,pdf,indata,
			 *(const RooArgSet*)RooCmdConfig::decodeObjOnTheFly("RooNLLVar::RooNLLVar","ProjectedObservables",0,&_emptySet
									    ,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","RangeWithName",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 RooCmdConfig::decodeStringOnTheFly("RooNLLVar::RooNLLVar","AddCoefRange",0,"",arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","NumCPU",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 kFALSE,
			 RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","Verbose",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","SplitRange",0,0,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9),
			 RooCmdConfig::decodeIntOnTheFly("RooNLLVar::RooNLLVar","CloneData",0,1,arg1,arg2,arg3,arg4,arg5,arg6,arg7,arg8,arg9))             
{
  // Construct likelihood from given p.d.f and (binned or unbinned dataset)
  //
  //  Extended()     -- Include extended term in calculation
  //  NumCPU()       -- Activate parallel processing feature
  //  Range()        -- Fit only selected region
  //  SumCoefRange() -- Set the range in which to interpret the coefficients of RooAddPdf components 
  //  SplitRange()   -- Fit range is split by index catory of simultaneous PDF
  //  ConditionalObservables() -- Define conditional observables 
  //  Verbose()      -- Verbose output of GOF framework classes
  //  CloneData()    -- Clone input dataset for internal use (default is kTRUE)

  RooCmdConfig pc("RooNLLVar::RooNLLVar") ;
  pc.allowUndefined() ;
  pc.defineInt("extended","Extended",0,kFALSE) ;

  pc.process(arg1) ;  pc.process(arg2) ;  pc.process(arg3) ;
  pc.process(arg4) ;  pc.process(arg5) ;  pc.process(arg6) ;
  pc.process(arg7) ;  pc.process(arg8) ;  pc.process(arg9) ;

  _extended = pc.getInt("extended") ;
  _weightSq = kFALSE ;
}



//_____________________________________________________________________________
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
		     Bool_t extended, const char* rangeName, const char* addCoefRangeName,
		     Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitRange, Bool_t cloneData) : 
  RooAbsOptTestStatistic(name,title,pdf,indata,RooArgSet(),rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
  _extended(extended),
  _weightSq(kFALSE)
{
  // Construct likelihood from given p.d.f and (binned or unbinned dataset)
  // For internal use.

}



//_____________________________________________________________________________
RooNLLVar::RooNLLVar(const char *name, const char *title, RooAbsPdf& pdf, RooAbsData& indata,
		     const RooArgSet& projDeps, Bool_t extended, const char* rangeName,const char* addCoefRangeName, 
		     Int_t nCPU,Bool_t interleave,Bool_t verbose, Bool_t splitRange, Bool_t cloneData) : 
  RooAbsOptTestStatistic(name,title,pdf,indata,projDeps,rangeName,addCoefRangeName,nCPU,interleave,verbose,splitRange,cloneData),
  _extended(extended),
  _weightSq(kFALSE)
{
  // Construct likelihood from given p.d.f and (binned or unbinned dataset)
  // For internal use.  


}



//_____________________________________________________________________________
RooNLLVar::RooNLLVar(const RooNLLVar& other, const char* name) : 
  RooAbsOptTestStatistic(other,name),
  _extended(other._extended),
  _weightSq(other._weightSq)
{
  // Copy constructor
}




//_____________________________________________________________________________
RooNLLVar::~RooNLLVar()
{
  // Destructor
}



//_____________________________________________________________________________
Double_t RooNLLVar::evaluatePartition(Int_t firstEvent, Int_t lastEvent, Int_t stepSize) const 
{
  // Calculate and return likelihood on subset of data from firstEvent to lastEvent
  // processed with a step size of 'stepSize'. If this an extended likelihood and
  // and the zero event is processed the extended term is added to the return
  // likelihood.

  Int_t i ;
  Double_t result(0) ;
  
  RooAbsPdf* pdfClone = (RooAbsPdf*) _funcClone ;

  Double_t sumWeight(0) ;
  for (i=firstEvent ; i<lastEvent ; i+=stepSize) {
    
    // get the data values for this event
    _dataClone->get(i);

    if (!_dataClone->valid()) {
      continue ;
    }

    if (_dataClone->weight()==0) continue ;

    // cout << "evaluating nll for event #" << i << " of " << lastEvent-firstEvent << endl ;

    Double_t eventWeight = _dataClone->weight() ;
    if (_weightSq) eventWeight *= eventWeight ;

    Double_t term = eventWeight * pdfClone->getLogVal(_normSet);
    sumWeight += eventWeight ;

    //cout << "RooNLLVar term of event [" << i << "] is " << term << endl ; 

    result-= term;
  }
  
  // include the extended maximum likelihood term, if requested
  if(_extended && firstEvent==0) {
    result+= pdfClone->extendedTerm((Int_t)_dataClone->sumEntries(),_dataClone->get());
  }    

  // If part of simultaneous PDF normalize probability over 
  // number of simultaneous PDFs: -sum(log(p/n)) = -sum(log(p)) + N*log(n) 
  if (_simCount>1) {
    result += sumWeight*log(1.0*_simCount) ;
  }

  //cout << "RooNLLVar(first=" << firstEvent << ", last=" << lastEvent << ", step=" << stepSize << ") result = " << result << endl ;

  return result ;
}



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