49 ClassImp(RooUnitTest);
54 TDirectory* RooUnitTest::gMemDir = 0 ;
59 RooUnitTest::RooUnitTest(
const char* name, TFile* refFile, Bool_t writeRef, Int_t verbose) : TNamed(name,name),
60 _refFile(refFile), _debug(kFALSE), _write(writeRef), _verb(verbose)
68 RooUnitTest::~RooUnitTest()
75 void RooUnitTest::regPlot(RooPlot* frame,
const char* refName)
78 string refNameStr(refName) ;
79 frame->SetName(refName) ;
80 _regPlots.push_back(make_pair(frame,refNameStr)) ;
89 void RooUnitTest::regResult(RooFitResult* r,
const char* refName)
92 string refNameStr(refName) ;
93 _regResults.push_back(make_pair(r,refNameStr)) ;
102 void RooUnitTest::regValue(Double_t d,
const char* refName)
105 string refNameStr(refName) ;
106 _regValues.push_back(make_pair(d,refNameStr)) ;
113 void RooUnitTest::regTable(RooTable* t,
const char* refName)
116 string refNameStr(refName) ;
117 _regTables.push_back(make_pair(t,refNameStr)) ;
126 void RooUnitTest::regWS(RooWorkspace* ws,
const char* refName)
129 string refNameStr(refName) ;
130 _regWS.push_back(make_pair(ws,refNameStr)) ;
139 void RooUnitTest::regTH(TH1* th,
const char* refName)
142 string refNameStr(refName) ;
143 _regTH.push_back(make_pair(th,refNameStr)) ;
152 RooWorkspace* RooUnitTest::getWS(
const char* refName)
154 RooWorkspace* ws =
dynamic_cast<RooWorkspace*
>(_refFile->Get(refName)) ;
156 cout <<
"RooUnitTest ERROR: cannot retrieve RooWorkspace " << refName
157 <<
" from reference file, skipping " << endl ;
167 Bool_t RooUnitTest::areTHidentical(TH1* htest, TH1* href)
169 if (htest->GetDimension() != href->GetDimension()) {
176 Double_t kmax = htest->KolmogorovTest(href,
"M") ;
180 cout <<
"KS distances = " << kmax << endl ;
182 Int_t ntest = htest->GetNbinsX() +2 ;
183 Int_t nref = href->GetNbinsX() +2 ;
184 if (htest->GetDimension()>1) {
185 ntest *= htest->GetNbinsY() + 2 ;
186 nref *= href->GetNbinsY() + 2 ;
188 if (htest->GetDimension()>2) {
189 ntest *= htest->GetNbinsZ() + 2 ;
190 nref *= href->GetNbinsZ() + 2 ;
197 for (Int_t i=0 ; i<ntest ; i++) {
198 if (fabs(htest->GetBinContent(i)-href->GetBinContent(i))>htol()) {
199 cout <<
"htest[" << i <<
"] = " << htest->GetBinContent(i) <<
" href[" << i <<
"] = " << href->GetBinContent(i) << endl;
213 Bool_t RooUnitTest::runCompTests()
217 list<pair<RooPlot*, string> >::iterator iter = _regPlots.begin() ;
218 while (iter!=_regPlots.end()) {
225 RooPlot* bmark =
dynamic_cast<RooPlot*
>(_refFile->Get(iter->second.c_str())) ;
227 cout <<
"RooUnitTest ERROR: cannot retrieve RooPlot " << iter->second <<
" from reference file, skipping " << endl ;
234 cout <<
"comparing RooPlot " << iter->first <<
" to benchmark " << iter->second <<
" = " << bmark << endl ;
235 cout <<
"reference: " ; iter->first->Print() ;
236 cout <<
"benchmark: " ; bmark->Print() ;
239 RooPlot* compPlot = _debug ? iter->first->emptyClone(Form(
"%s_comparison",iter->first->GetName())) : 0 ;
240 Bool_t anyFail=kFALSE ;
242 Stat_t nItems = iter->first->numItems() ;
243 for (Stat_t i=0 ; i<nItems ; i++) {
245 TObject* obj = iter->first->getObject((Int_t)i) ;
248 TObject* objRef = bmark->findObject(obj->GetName()) ;
251 cout <<
"RooUnitTest ERROR: cannot retrieve object " << obj->GetName() <<
" from reference RooPlot " << iter->second <<
", skipping" << endl ;
257 if (obj->IsA()==RooHist::Class()) {
258 RooHist* testHist =
static_cast<RooHist*
>(obj) ;
259 RooHist* refHist =
static_cast<RooHist*
>(objRef) ;
260 if (!testHist->isIdentical(*refHist,htol())) {
261 cout <<
"RooUnitTest ERROR: comparison of object " << obj->IsA()->GetName() <<
"::" << obj->GetName()
262 <<
" fails comparison with counterpart in reference RooPlot " << bmark->GetName() << endl ;
265 compPlot->addPlotable((RooHist*)testHist->Clone(),
"P") ;
266 compPlot->getAttLine()->SetLineColor(kRed) ;
267 compPlot->getAttMarker()->SetMarkerColor(kRed) ;
268 compPlot->getAttLine()->SetLineWidth(1) ;
270 compPlot->addPlotable((RooHist*)refHist->Clone(),
"P") ;
271 compPlot->getAttLine()->SetLineColor(kBlue) ;
272 compPlot->getAttMarker()->SetMarkerColor(kBlue) ;
273 compPlot->getAttLine()->SetLineWidth(1) ;
279 }
else if (obj->IsA()==RooCurve::Class()) {
280 RooCurve* testCurve =
static_cast<RooCurve*
>(obj) ;
281 RooCurve* refCurve =
static_cast<RooCurve*
>(objRef) ;
282 if (!testCurve->isIdentical(*refCurve,ctol())) {
283 cout <<
"RooUnitTest ERROR: comparison of object " << obj->IsA()->GetName() <<
"::" << obj->GetName()
284 <<
" fails comparison with counterpart in reference RooPlot " << bmark->GetName() << endl ;
287 compPlot->addPlotable((RooCurve*)testCurve->Clone()) ;
288 compPlot->getAttLine()->SetLineColor(kRed) ;
289 compPlot->getAttLine()->SetLineWidth(1) ;
290 compPlot->getAttLine()->SetLineStyle(kSolid) ;
292 compPlot->addPlotable((RooCurve*)refCurve->Clone()) ;
293 compPlot->getAttLine()->SetLineColor(kBlue) ;
294 compPlot->getAttLine()->SetLineWidth(1) ;
295 compPlot->getAttLine()->SetLineStyle(kDashed) ;
306 if (anyFail && compPlot) {
307 cout <<
"RooUnitTest INFO: writing comparison plot " << compPlot->GetName() <<
" of failed test to RooUnitTest_DEBUG.root" << endl ;
308 TFile fdbg(
"RooUnitTest_DEBUG.root",
"UPDATE") ;
322 cout <<
"RooUnitTest: Writing reference RooPlot " << iter->first <<
" as benchmark " << iter->second << endl ;
324 iter->first->Write(iter->second.c_str()) ;
332 list<pair<RooFitResult*, string> >::iterator iter2 = _regResults.begin() ;
333 while (iter2!=_regResults.end()) {
340 RooFitResult* bmark =
dynamic_cast<RooFitResult*
>(_refFile->Get(iter2->second.c_str())) ;
342 cout <<
"RooUnitTest ERROR: cannot retrieve RooFitResult " << iter2->second <<
" from reference file, skipping " << endl ;
349 cout <<
"comparing RooFitResult " << iter2->first <<
" to benchmark " << iter2->second <<
" = " << bmark << endl ;
352 if (!iter2->first->isIdentical(*bmark,fptol(),fctol())) {
353 cout <<
"RooUnitTest ERROR: comparison of object " << iter2->first->IsA()->GetName() <<
"::" << iter2->first->GetName()
354 <<
" from result " << iter2->second
355 <<
" fails comparison with counterpart in reference RooFitResult " << bmark->GetName() << endl ;
360 delete iter2->first ;
367 cout <<
"RooUnitTest: Writing reference RooFitResult " << iter2->first <<
" as benchmark " << iter2->second << endl ;
369 iter2->first->Write(iter2->second.c_str()) ;
376 list<pair<Double_t, string> >::iterator iter3 = _regValues.begin() ;
377 while (iter3!=_regValues.end()) {
384 RooDouble* ref =
dynamic_cast<RooDouble*
>(_refFile->Get(iter3->second.c_str())) ;
386 cout <<
"RooUnitTest ERROR: cannot retrieve RooDouble " << iter3->second <<
" from reference file, skipping " << endl ;
393 cout <<
"comparing value " << iter3->first <<
" to benchmark " << iter3->second <<
" = " << (Double_t)(*ref) << endl ;
396 if (fabs(iter3->first - (Double_t)(*ref))>vtol() ) {
397 cout <<
"RooUnitTest ERROR: comparison of value " << iter3->first <<
" fails comparison with reference " << ref->GetName() << endl ;
406 cout <<
"RooUnitTest: Writing reference Double_t " << iter3->first <<
" as benchmark " << iter3->second << endl ;
408 RooDouble* rd =
new RooDouble(iter3->first) ;
409 rd->Write(iter3->second.c_str()) ;
417 list<pair<RooTable*, string> >::iterator iter4 = _regTables.begin() ;
418 while (iter4!=_regTables.end()) {
425 RooTable* bmark =
dynamic_cast<RooTable*
>(_refFile->Get(iter4->second.c_str())) ;
427 cout <<
"RooUnitTest ERROR: cannot retrieve RooTable " << iter4->second <<
" from reference file, skipping " << endl ;
434 cout <<
"comparing RooTable " << iter4->first <<
" to benchmark " << iter4->second <<
" = " << bmark << endl ;
437 if (!iter4->first->isIdentical(*bmark)) {
438 cout <<
"RooUnitTest ERROR: comparison of object " << iter4->first->IsA()->GetName() <<
"::" << iter4->first->GetName()
439 <<
" fails comparison with counterpart in reference RooTable " << bmark->GetName() << endl ;
444 delete iter4->first ;
451 cout <<
"RooUnitTest: Writing reference RooTable " << iter4->first <<
" as benchmark " << iter4->second << endl ;
453 iter4->first->Write(iter4->second.c_str()) ;
461 list<pair<RooWorkspace*, string> >::iterator iter5 = _regWS.begin() ;
462 while (iter5!=_regWS.end()) {
468 cout <<
"RooUnitTest: Writing reference RooWorkspace " << iter5->first <<
" as benchmark " << iter5->second << endl ;
470 iter5->first->Write(iter5->second.c_str()) ;
478 list<pair<TH1*, string> >::iterator iter6 = _regTH.begin() ;
479 while (iter6!=_regTH.end()) {
486 TH1* bmark =
dynamic_cast<TH1*
>(_refFile->Get(iter6->second.c_str())) ;
488 cout <<
"RooUnitTest ERROR: cannot retrieve TH1 " << iter6->second <<
" from reference file, skipping " << endl ;
495 cout <<
"comparing TH1 " << iter6->first <<
" to benchmark " << iter6->second <<
" = " << bmark << endl ;
498 if (!areTHidentical(iter6->first,bmark)) {
500 cout <<
"RooUnitTest ERROR: comparison of object " << iter6->first->IsA()->GetName() <<
"::" << iter6->first->GetName()
501 <<
" fails comparison with counterpart in reference TH1 " << bmark->GetName() << endl ;
505 cout <<
"RooUnitTest INFO: writing THx " << iter6->first->GetName() <<
" and " << bmark->GetName()
506 <<
" of failed test to RooUnitTest_DEBUG.root" << endl ;
507 TFile fdbg(
"RooUnitTest_DEBUG.root",
"UPDATE") ;
508 iter6->first->SetName(Form(
"%s_test",iter6->first->GetName())) ;
509 iter6->first->Write() ;
510 bmark->SetName(Form(
"%s_ref",bmark->GetName())) ;
519 delete iter6->first ;
526 cout <<
"RooUnitTest: Writing reference TH1 " << iter6->first <<
" as benchmark " << iter6->second << endl ;
528 iter6->first->Write(iter6->second.c_str()) ;
544 void RooUnitTest::setSilentMode()
546 RooMsgService::instance().setSilentMode(kTRUE) ;
547 for (Int_t i=0 ; i<RooMsgService::instance().numStreams() ; i++) {
548 if (RooMsgService::instance().getStream(i).minLevel<RooFit::ERROR) {
549 RooMsgService::instance().setStreamStatus(i,kFALSE) ;
557 void RooUnitTest::clearSilentMode()
559 RooMsgService::instance().setSilentMode(kFALSE) ;
560 for (Int_t i=0 ; i<RooMsgService::instance().numStreams() ; i++) {
561 RooMsgService::instance().setStreamStatus(i,kTRUE) ;
569 Bool_t RooUnitTest::runTest()
576 cout <<
"*** Begin of output of Unit Test at normal verbosity *************" << endl ;
579 RooMsgService::instance().clearErrorCount() ;
582 gRandom->SetSeed(12345) ;
583 RooRandom::randomGenerator()->SetSeed(12345) ;
585 RooTrace::callgrind_zero() ;
586 if (!testCode())
return kFALSE ;
587 RooTrace::callgrind_dump() ;
592 cout <<
"*** End of output of Unit Test at normal verbosity ***************" << endl ;
595 if (RooMsgService::instance().errorCount()>0) {
596 cout <<
"RooUnitTest: ERROR messages were logged, failing test" << endl ;
600 return runCompTests() ;
607 void RooUnitTest::setMemDir(TDirectory* memDir) {