00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "RooFit.h"
00038 #include "Riostream.h"
00039
00040 #include "RooAbsTestStatistic.h"
00041 #include "RooAbsPdf.h"
00042 #include "RooSimultaneous.h"
00043 #include "RooAbsData.h"
00044 #include "RooArgSet.h"
00045 #include "RooRealVar.h"
00046 #include "RooNLLVar.h"
00047 #include "RooRealMPFE.h"
00048 #include "RooErrorHandler.h"
00049 #include "RooMsgService.h"
00050
00051 #include <string>
00052
00053 ClassImp(RooAbsTestStatistic)
00054 ;
00055
00056
00057
00058 RooAbsTestStatistic::RooAbsTestStatistic()
00059 {
00060
00061 _func = 0 ;
00062 _data = 0 ;
00063 _projDeps = 0 ;
00064 _init = kFALSE ;
00065 _gofArray = 0 ;
00066 _mpfeArray = 0 ;
00067 _projDeps = 0 ;
00068 }
00069
00070
00071
00072
00073 RooAbsTestStatistic::RooAbsTestStatistic(const char *name, const char *title, RooAbsReal& real, RooAbsData& data,
00074 const RooArgSet& projDeps, const char* rangeName, const char* addCoefRangeName,
00075 Int_t nCPU, Bool_t interleave, Bool_t verbose, Bool_t splitCutRange) :
00076 RooAbsReal(name,title),
00077 _paramSet("paramSet","Set of parameters",this),
00078 _func(&real),
00079 _data(&data),
00080 _projDeps((RooArgSet*)projDeps.Clone()),
00081 _rangeName(rangeName?rangeName:""),
00082 _addCoefRangeName(addCoefRangeName?addCoefRangeName:""),
00083 _splitRange(splitCutRange),
00084 _simCount(1),
00085 _verbose(verbose),
00086 _nGof(0),
00087 _gofArray(0),
00088 _nCPU(nCPU),
00089 _mpfeArray(0),
00090 _mpinterl(interleave)
00091 {
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107 RooArgSet* params = real.getParameters(&data) ;
00108 _paramSet.add(*params) ;
00109 delete params ;
00110
00111 if (_nCPU>1) {
00112
00113 _gofOpMode = MPMaster ;
00114
00115 } else {
00116
00117
00118 Bool_t simMode = dynamic_cast<RooSimultaneous*>(&real)?kTRUE:kFALSE ;
00119
00120 if (simMode) {
00121 _gofOpMode = SimMaster ;
00122 } else {
00123 _gofOpMode = Slave ;
00124 }
00125 }
00126
00127 _setNum = 0 ;
00128 _numSets = 1 ;
00129 _init = kFALSE ;
00130 _nEvents = data.numEntries() ;
00131 }
00132
00133
00134
00135
00136 RooAbsTestStatistic::RooAbsTestStatistic(const RooAbsTestStatistic& other, const char* name) :
00137 RooAbsReal(other,name),
00138 _paramSet("paramSet","Set of parameters",this),
00139 _func(other._func),
00140 _data(other._data),
00141 _projDeps((RooArgSet*)other._projDeps->Clone()),
00142 _rangeName(other._rangeName),
00143 _addCoefRangeName(other._addCoefRangeName),
00144 _splitRange(other._splitRange),
00145 _simCount(1),
00146 _verbose(other._verbose),
00147 _nGof(0),
00148 _gofArray(0),
00149 _nCPU(other._nCPU),
00150 _mpfeArray(0),
00151 _mpinterl(other._mpinterl)
00152 {
00153
00154
00155
00156 _paramSet.add(other._paramSet) ;
00157
00158 if (_nCPU>1) {
00159
00160 _gofOpMode = MPMaster ;
00161
00162 } else {
00163
00164
00165 Bool_t simMode = dynamic_cast<RooSimultaneous*>(_func)?kTRUE:kFALSE ;
00166
00167 if (simMode) {
00168 _gofOpMode = SimMaster ;
00169 } else {
00170 _gofOpMode = Slave ;
00171 }
00172 }
00173
00174 _setNum = 0 ;
00175 _numSets = 1 ;
00176 _init = kFALSE ;
00177 _nEvents = _data->numEntries() ;
00178
00179
00180 }
00181
00182
00183
00184
00185 RooAbsTestStatistic::~RooAbsTestStatistic()
00186 {
00187
00188
00189 if (_gofOpMode==MPMaster && _init) {
00190 Int_t i ;
00191 for (i=0 ; i<_nCPU ; i++) {
00192 delete _mpfeArray[i] ;
00193 }
00194 delete[] _mpfeArray ;
00195 }
00196
00197 if (_gofOpMode==SimMaster && _init) {
00198 Int_t i ;
00199 for (i=0 ; i<_nGof ; i++) {
00200 delete _gofArray[i] ;
00201 }
00202 delete[] _gofArray ;
00203 }
00204
00205 if (_projDeps) {
00206 delete _projDeps ;
00207 }
00208 }
00209
00210
00211
00212
00213 Double_t RooAbsTestStatistic::evaluate() const
00214 {
00215
00216
00217
00218
00219
00220
00221
00222 if (!_init) {
00223 const_cast<RooAbsTestStatistic*>(this)->initialize() ;
00224 }
00225
00226 if (_gofOpMode==SimMaster) {
00227
00228
00229 Double_t ret = combinedValue((RooAbsReal**)_gofArray,_nGof) ;
00230
00231
00232 if (numSets()==1) {
00233
00234 ret /= globalNormalization() ;
00235 }
00236
00237 return ret ;
00238
00239 } else if (_gofOpMode==MPMaster) {
00240
00241
00242 Int_t i ;
00243 for (i=0 ; i<_nCPU ; i++) {
00244 _mpfeArray[i]->calculate() ;
00245 }
00246 Double_t ret = combinedValue((RooAbsReal**)_mpfeArray,_nCPU)/globalNormalization() ;
00247 return ret ;
00248
00249 } else {
00250
00251
00252 Int_t nFirst, nLast, nStep ;
00253 if (_mpinterl) {
00254 nFirst = _setNum ;
00255 nLast = _nEvents ;
00256 nStep = _numSets ;
00257 } else {
00258 nFirst = _nEvents * _setNum / _numSets ;
00259 nLast = _nEvents * (_setNum+1) / _numSets ;
00260 nStep = 1 ;
00261 }
00262
00263
00264
00265
00266 Double_t ret = evaluatePartition(nFirst,nLast,nStep) ;
00267 if (numSets()==1) {
00268
00269 ret /= globalNormalization() ;
00270 }
00271
00272 return ret ;
00273
00274 }
00275 }
00276
00277
00278
00279
00280 Bool_t RooAbsTestStatistic::initialize()
00281 {
00282
00283
00284
00285
00286 if (_init) return kFALSE ;
00287
00288 if (_gofOpMode==MPMaster) {
00289 initMPMode(_func,_data,_projDeps,_rangeName.size()?_rangeName.c_str():0,_addCoefRangeName.size()?_addCoefRangeName.c_str():0) ;
00290 } else if (_gofOpMode==SimMaster) {
00291 initSimMode((RooSimultaneous*)_func,_data,_projDeps,_rangeName.size()?_rangeName.c_str():0,_addCoefRangeName.size()?_addCoefRangeName.c_str():0) ;
00292 }
00293 _init = kTRUE ;
00294 return kFALSE ;
00295 }
00296
00297
00298
00299
00300 Bool_t RooAbsTestStatistic::redirectServersHook(const RooAbsCollection& newServerList, Bool_t mustReplaceAll, Bool_t nameChange, Bool_t)
00301 {
00302
00303
00304 if (_gofOpMode==SimMaster && _gofArray) {
00305
00306 Int_t i ;
00307 for (i=0 ; i<_nGof ; i++) {
00308 if (_gofArray[i]) {
00309 _gofArray[i]->recursiveRedirectServers(newServerList,mustReplaceAll,nameChange) ;
00310 }
00311 }
00312 } else if (_gofOpMode==MPMaster && _mpfeArray) {
00313
00314
00315 Int_t i ;
00316 for (i=0 ; i<_nCPU ; i++) {
00317 if (_mpfeArray[i]) {
00318 _mpfeArray[i]->recursiveRedirectServers(newServerList,mustReplaceAll,nameChange) ;
00319
00320 }
00321 }
00322
00323 }
00324 return kFALSE ;
00325 }
00326
00327
00328
00329
00330 void RooAbsTestStatistic::printCompactTreeHook(ostream& os, const char* indent)
00331 {
00332
00333
00334
00335 if (_gofOpMode==SimMaster) {
00336
00337 Int_t i ;
00338 os << indent << "RooAbsTestStatistic begin GOF contents" << endl ;
00339 for (i=0 ; i<_nGof ; i++) {
00340 if (_gofArray[i]) {
00341 TString indent2(indent) ;
00342 indent2 += Form("[%d] ",i) ;
00343 _gofArray[i]->printCompactTreeHook(os,indent2) ;
00344 }
00345 }
00346 os << indent << "RooAbsTestStatistic end GOF contents" << endl ;
00347 } else if (_gofOpMode==MPMaster) {
00348
00349 }
00350 }
00351
00352
00353
00354
00355 void RooAbsTestStatistic::constOptimizeTestStatistic(ConstOpCode opcode)
00356 {
00357
00358
00359 Int_t i ;
00360 initialize() ;
00361 if (_gofOpMode==SimMaster) {
00362
00363 for (i=0 ; i<_nGof ; i++) {
00364 if (_gofArray[i]) _gofArray[i]->constOptimizeTestStatistic(opcode) ;
00365 }
00366 } else if (_gofOpMode==MPMaster) {
00367 for (i=0 ; i<_nCPU ; i++) {
00368 _mpfeArray[i]->constOptimizeTestStatistic(opcode) ;
00369 }
00370 }
00371 }
00372
00373
00374
00375
00376 void RooAbsTestStatistic::setMPSet(Int_t inSetNum, Int_t inNumSets)
00377 {
00378
00379
00380 _setNum = inSetNum ; _numSets = inNumSets ;
00381 if (_gofOpMode==SimMaster) {
00382
00383 initialize() ;
00384 Int_t i ;
00385 for (i=0 ; i<_nGof ; i++) {
00386 if (_gofArray[i]) _gofArray[i]->setMPSet(inSetNum,inNumSets) ;
00387 }
00388 }
00389 }
00390
00391
00392
00393
00394 void RooAbsTestStatistic::initMPMode(RooAbsReal* real, RooAbsData* data, const RooArgSet* projDeps, const char* rangeName, const char* addCoefRangeName)
00395 {
00396
00397
00398
00399 Int_t i ;
00400 _mpfeArray = new pRooRealMPFE[_nCPU] ;
00401
00402
00403 RooAbsTestStatistic* gof = create(GetName(),GetTitle(),*real,*data,*projDeps,rangeName,addCoefRangeName,1,_mpinterl,_verbose,_splitRange) ;
00404 gof->recursiveRedirectServers(_paramSet) ;
00405
00406 for (i=0 ; i<_nCPU ; i++) {
00407
00408 gof->setMPSet(i,_nCPU) ;
00409 gof->SetName(Form("%s_GOF%d",GetName(),i)) ;
00410 gof->SetTitle(Form("%s_GOF%d",GetTitle(),i)) ;
00411
00412 Bool_t doInline = (i==_nCPU-1) ;
00413 if (!doInline) coutI(Eval) << "RooAbsTestStatistic::initMPMode: starting remote server process #" << i << endl ;
00414 _mpfeArray[i] = new RooRealMPFE(Form("%s_%lx_MPFE%d",GetName(),(ULong_t)this,i),Form("%s_%lx_MPFE%d",GetTitle(),(ULong_t)this,i),*gof,doInline) ;
00415 _mpfeArray[i]->initialize() ;
00416 }
00417
00418 return ;
00419 }
00420
00421
00422
00423
00424 void RooAbsTestStatistic::initSimMode(RooSimultaneous* simpdf, RooAbsData* data,
00425 const RooArgSet* projDeps, const char* rangeName, const char* addCoefRangeName)
00426 {
00427
00428
00429
00430
00431
00432
00433 RooAbsCategoryLValue& simCat = (RooAbsCategoryLValue&) simpdf->indexCat() ;
00434
00435
00436 TString simCatName(simCat.GetName()) ;
00437 TList* dsetList = const_cast<RooAbsData*>(data)->split(simCat,processEmptyDataSets()) ;
00438 if (!dsetList) {
00439 coutE(Fitting) << "RooAbsTestStatistic::initSimMode(" << GetName() << ") ERROR: index category of simultaneous pdf is missing in dataset, aborting" << endl ;
00440 throw std::string("RooAbsTestStatistic::initSimMode() ERROR, index category of simultaneous pdf is missing in dataset, aborting") ;
00441 RooErrorHandler::softAbort() ;
00442 }
00443
00444
00445 Int_t n(0) ;
00446 _nGof = 0 ;
00447 RooCatType* type ;
00448 TIterator* catIter = simCat.typeIterator() ;
00449 while((type=(RooCatType*)catIter->Next())){
00450
00451
00452 RooAbsPdf* pdf = simpdf->getPdf(type->GetName()) ;
00453 RooAbsData* dset = (RooAbsData*) dsetList->FindObject(type->GetName()) ;
00454
00455 if (pdf && dset && (dset->sumEntries()!=0. || processEmptyDataSets() )) {
00456 _nGof++ ;
00457 }
00458 }
00459
00460
00461 _gofArray = new pRooAbsTestStatistic[_nGof] ;
00462
00463
00464 catIter->Reset() ;
00465 while((type=(RooCatType*)catIter->Next())){
00466
00467
00468 RooAbsPdf* pdf = simpdf->getPdf(type->GetName()) ;
00469 RooAbsData* dset = (RooAbsData*) dsetList->FindObject(type->GetName()) ;
00470
00471 if (pdf && dset && (dset->sumEntries()!=0. || processEmptyDataSets())) {
00472 coutI(Fitting) << "RooAbsTestStatistic::initSimMode: creating slave calculator #" << n << " for state " << type->GetName()
00473 << " (" << dset->numEntries() << " dataset entries)" << endl ;
00474
00475 if (_splitRange && rangeName) {
00476 _gofArray[n] = create(type->GetName(),type->GetName(),*pdf,*dset,*projDeps,
00477 Form("%s_%s",rangeName,type->GetName()),addCoefRangeName,_nCPU*(_mpinterl?-1:1),_mpinterl,_verbose,_splitRange) ;
00478 } else {
00479 _gofArray[n] = create(type->GetName(),type->GetName(),*pdf,*dset,*projDeps,
00480 rangeName,addCoefRangeName,_nCPU,_mpinterl,_verbose,_splitRange) ;
00481 }
00482 _gofArray[n]->setSimCount(_nGof) ;
00483
00484
00485 _gofArray[n]->recursiveRedirectServers(_paramSet) ;
00486 n++ ;
00487 } else {
00488 if ((!dset || (dset->sumEntries()==0. && !processEmptyDataSets()) ) && pdf) {
00489 if (_verbose) {
00490 coutI(Fitting) << "RooAbsTestStatistic::initSimMode: state " << type->GetName()
00491 << " has no data entries, no slave calculator created" << endl ;
00492 }
00493 }
00494 }
00495 }
00496
00497 dsetList->Delete() ;
00498 delete dsetList ;
00499 delete catIter ;
00500 }
00501
00502
00503
00504