51 ClassImp(RooStats::LikelihoodIntervalPlot);
53 using namespace RooStats;
59 LikelihoodIntervalPlot::LikelihoodIntervalPlot()
81 LikelihoodIntervalPlot::LikelihoodIntervalPlot(LikelihoodInterval* theInterval)
83 fInterval = theInterval;
84 fParamsPlot = fInterval->GetParameters();
85 fNdimPlot = fParamsPlot->getSize();
103 LikelihoodIntervalPlot::~LikelihoodIntervalPlot()
109 void LikelihoodIntervalPlot::SetLikelihoodInterval(LikelihoodInterval* theInterval)
111 fInterval = theInterval;
112 fParamsPlot = fInterval->GetParameters();
113 fNdimPlot = fParamsPlot->getSize();
120 void LikelihoodIntervalPlot::SetPlotParameters(
const RooArgSet *params)
122 fNdimPlot = params->getSize();
123 fParamsPlot = (RooArgSet*) params->clone((std::string(params->GetName())+
"_clone").c_str());
166 void LikelihoodIntervalPlot::Draw(
const Option_t *options)
168 TIter it = fParamsPlot->createIterator();
170 RooArgSet* intervalParams = fInterval->GetParameters();
172 RooArgSet extraParams;
173 while((arg=(RooAbsArg*)it.Next())) {
174 if (!intervalParams->contains(*arg) ) {
175 ccoutE(InputArguments) <<
"Parameter " << arg->GetName() <<
"is not in the list of LikelihoodInterval parameters "
176 <<
" - do not use for plotting " << std::endl;
178 extraParams.add(*arg);
181 if (extraParams.getSize() > 0)
182 fParamsPlot->remove(extraParams,
true,
true);
185 ccoutE(InputArguments) <<
"LikelihoodIntervalPlot::Draw(" << GetName()
186 <<
") ERROR: contours for more than 2 dimensions not implemented!" << std::endl;
193 RooAbsReal* newProfile = 0;
194 RooAbsReal* oldProfile = fInterval->GetLikelihoodRatio();
195 if (fNdimPlot != intervalParams->getSize() ) {
196 RooProfileLL * profilell =
dynamic_cast<RooProfileLL*
>(oldProfile);
197 if (!profilell)
return;
198 RooAbsReal & nll = profilell->nll();
199 newProfile = nll.createProfile(*fParamsPlot);
202 newProfile = oldProfile;
206 RooRealVar *myparam = (RooRealVar*) it.Next();
209 if (fInterval->GetBestFitParameters() ) {
210 *fParamsPlot = *fInterval->GetBestFitParameters();
211 newProfile->getVal();
215 TString opt = options;
218 TString title = GetTitle();
219 int nPoints = fNPoints;
226 bool useRooPlot = opt.Contains(
"rooplot") || ! (opt.Contains(
"tf1"));
227 opt.ReplaceAll(
"rooplot",
"");
228 opt.ReplaceAll(
"tf1",
"");
234 if (nPoints <=0) nPoints = 100;
236 const Double_t xcont_min = fInterval->LowerLimit(*myparam);
237 const Double_t xcont_max = fInterval->UpperLimit(*myparam);
239 RooRealVar* myarg = (RooRealVar *) newProfile->getVariables()->find(myparam->GetName());
240 double x1 = myarg->getMin();
241 double x2 = myarg->getMax();
244 if (fColor == 0) fColor = kBlue;
245 if (fLineColor == 0) fLineColor = kGreen;
253 double xmin = std::max( x1, 2*xcont_min - xcont_max);
254 double xmax = std::min( x2, 2*xcont_max - xcont_min);
255 if (fXmin < fXmax) { xmin = fXmin; xmax = fXmax; }
257 TF1 * tmp = newProfile->asTF(*myarg);
259 tmp->SetRange(xmin, xmax);
260 tmp->SetNpx(nPoints);
263 TF1 * f1 = (TF1*) tmp->Clone();
267 TString name = TString(GetName()) + TString(
"_PLL_") + TString(myarg->GetName());
273 x1 = xmin; x2 = xmax;
274 if (fMaximum > 0 && fXmin >= fXmax ) {
275 double x0 = f1->GetX(0, xmin, xmax);
277 if ( x0 > x1 && x0 < x2) {
278 x1 = f1->GetX(fMaximum, xmin, x0);
279 x2 = f1->GetX(fMaximum, x0, xmax);
280 f1->SetMaximum(fMaximum);
288 f1->SetLineColor(kBlue);
289 f1->GetXaxis()->SetTitle(myarg->GetName());
290 f1->GetYaxis()->SetTitle(Form(
"- log #lambda(%s)",myparam->GetName()));
292 fPlotObject = f1->GetHistogram();
297 double xmin = myparam->getMin();
double xmax = myparam->getMax();
298 if (fXmin < fXmax) { xmin = fXmin; xmax = fXmax; }
302 int prevBins = myarg->getBins();
303 myarg->setBins(fNPoints);
306 frame = myarg->frame(xmin,xmax,nPoints);
310 frame->SetTitle(title);
311 frame->GetYaxis()->SetTitle(Form(
"- log #lambda(%s)",myparam->GetName()));
317 if (fPrecision > 0) cmd = RooFit::Precision(fPrecision);
318 newProfile->plotOn(frame,cmd,RooFit::LineColor(fColor));
320 frame->SetMaximum(fMaximum);
321 frame->SetMinimum(0.);
323 myarg->setBins(prevBins);
330 Double_t Yat_Xmax = 0.5*ROOT::Math::chisquared_quantile(fInterval->ConfidenceLevel(),1);
332 TLine *Yline_cutoff =
new TLine(x1,Yat_Xmax,x2,Yat_Xmax);
333 TLine *Yline_min =
new TLine(xcont_min,0.,xcont_min,Yat_Xmax);
334 TLine *Yline_max =
new TLine(xcont_max,0.,xcont_max,Yat_Xmax);
336 Yline_cutoff->SetLineColor(fLineColor);
337 Yline_min->SetLineColor(fLineColor);
338 Yline_max->SetLineColor(fLineColor);
342 Yline_cutoff->Draw();
348 frame->addObject(Yline_min);
349 frame->addObject(Yline_max);
350 frame->addObject(Yline_cutoff);
360 else if(fNdimPlot == 2){
365 bool useMinuit = !opt.Contains(
"nominuit");
367 bool plotHist = !opt.Contains(
"nohist");
368 opt.ReplaceAll(
"nominuit",
"");
369 opt.ReplaceAll(
"nohist",
"");
370 if (opt.Contains(
"minuit") ) useMinuit=
true;
371 if (useMinuit) plotHist =
false;
372 if (opt.Contains(
"hist") ) plotHist=
true;
373 opt.ReplaceAll(
"minuit",
"");
374 opt.ReplaceAll(
"hist",
"");
376 RooRealVar *myparamY = (RooRealVar*)it.Next();
378 Double_t cont_level = ROOT::Math::chisquared_quantile(fInterval->ConfidenceLevel(),fNdimPlot);
379 cont_level = cont_level/2;
381 RooArgList params(*newProfile->getVariables());
383 for (
int i = 0; i < params.getSize(); ++i) {
384 RooRealVar & par = (RooRealVar &) params[i];
385 RooRealVar * fitPar = (RooRealVar *) (fInterval->GetBestFitParameters()->find(par.GetName() ) );
387 par.setVal( fitPar->getVal() );
391 newProfile->getVal();
393 if (title.Length() == 0)
394 title = TString(
"Contour of ") + TString(myparamY->GetName() ) + TString(
" vs ") + TString(myparam->GetName() );
396 title = TString::Format(
"%s;%s;%s",title.Data(),myparam->GetName(),myparamY->GetName());
398 if (nPoints <=0) nPoints = 40;
400 double xmin = myparam->getMin();
double xmax = myparam->getMax();
401 double ymin = myparamY->getMin();
double ymax = myparamY->getMax();
402 if (fXmin < fXmax) { xmin = fXmin; xmax = fXmax; }
403 if (fYmin < fYmax) { ymin = fYmin; ymax = fYmax; }
406 if (!useMinuit || plotHist) {
411 TString histName = TString::Format(
"_hist2D__%s_%s",myparam->GetName(),myparamY->GetName() );
412 int nBins = int( std::sqrt(
double(nPoints)) + 0.5 );
413 TH2* hist2D =
new TH2D(histName, title, nBins, xmin, xmax, nBins, ymin, ymax );
414 newProfile->fillHistogram(hist2D, RooArgList(*myparam,*myparamY), 1, 0,
false, 0,
false);
416 hist2D->SetTitle(title);
417 hist2D->SetStats(kFALSE);
422 const int nLevels = 51;
423 double contLevels[nLevels];
424 contLevels[0] = 0.01;
425 double maxVal = (fMaximum > 0) ? fMaximum : hist2D->GetMaximum();
426 for (
int k = 1; k < nLevels; ++k) {
427 contLevels[k] = k*maxVal/double(nLevels-1);
429 hist2D->SetContour(nLevels,contLevels);
431 if (fMaximum>0) hist2D->SetMaximum(fMaximum);
433 hist2D->DrawClone(
"COLZ");
439 const int nLevels = 8;
440 double contLevels[nLevels];
442 double confLevels[nLevels] = { 0.1,0.3,0.5,0.683,0.95,0.9973,0.9999366575,0.9999994267};
443 for (
int k = 0; k < nLevels; ++k) {
445 contLevels[k] = 0.5*ROOT::Math::chisquared_quantile(confLevels[k],2);
447 hist2D->SetContour(nLevels,contLevels);
448 if (fLineColor) hist2D->SetLineColor(fLineColor);
451 TString tmpOpt = opt;
452 tmpOpt.ReplaceAll(
"same",
"");
453 if (tmpOpt.Length() < 3) opt +=
"cont3";
455 if (plotHist) opt += TString(
" same");
456 hist2D->Draw(opt.Data());
463 TH2 * h = (TH2*) hist2D->Clone();
464 h->SetContour(1,&cont_level);
466 TVirtualPad * currentPad = gPad;
468 TCanvas * tmpCanvas =
new TCanvas(
"tmpCanvas",
"tmpCanvas");
469 h->Draw(
"CONT LIST");
473 TObjArray *contoursOrig = (TObjArray*) gROOT->GetListOfSpecials()->FindObject(
"contours");
475 TObjArray *contours = 0;
476 if (contoursOrig) contours = (TObjArray*) contoursOrig->Clone();
484 if (tmpOpt.Contains(
"cont4")) {
485 Double_t bm = gPad->GetBottomMargin();
486 Double_t lm = gPad->GetLeftMargin();
487 Double_t rm = gPad->GetRightMargin();
488 Double_t tm = gPad->GetTopMargin();
489 Double_t x1 = hist2D->GetXaxis()->GetXmin();
490 Double_t y1 = hist2D->GetYaxis()->GetXmin();
491 Double_t x2 = hist2D->GetXaxis()->GetXmax();
492 Double_t y2 = hist2D->GetYaxis()->GetXmax();
494 TPad *null=
new TPad(
"null",
"null",0,0,1,1);
495 null->SetFillStyle(0);
496 null->SetFrameFillStyle(0);
499 null->Range(x1-(x2-x1)*(lm/(1-rm-lm)),
500 y1-(y2-y1)*(bm/(1-tm-lm)),
501 x2+(x2-x1)*(rm/(1-rm-lm)),
502 y2+(y2-y1)*(tm/(1-tm-lm)));
509 int ncontours = contours->GetSize();
510 for (
int icont = 0; icont < ncontours; ++icont) {
511 TList * contourList = (TList*)contours->At(icont);
512 if (contourList && contourList->GetSize() > 0) {
513 TIterator * itgr = contourList->MakeIterator();
515 while( (gr = dynamic_cast<TGraph*>(itgr->Next()) ) ){
516 if (fLineColor) gr->SetLineColor(fLineColor);
517 gr->SetLineStyle(kDashed);
520 gr->SetFillColor(fColor);
531 ccoutE(InputArguments) <<
"LikelihoodIntervalPlot::Draw(" << GetName()
532 <<
") ERROR: no contours found in ListOfSpecial" << std::endl;
535 fPlotObject = hist2D;
542 TGraph * gr =
new TGraph(nPoints+1);
544 int ncp = fInterval->GetContourPoints(*myparam, *myparamY, gr->GetX(), gr->GetY(),nPoints);
546 if (
int(ncp) < nPoints) {
547 std::cout <<
"Warning - Less points calculated in contours np = " << ncp <<
" / " << nPoints << std::endl;
548 for (
int i = ncp; i < nPoints; ++i) gr->RemovePoint(i);
551 gr->SetPoint(ncp, gr->GetX()[0], gr->GetY()[0] );
552 if (!opt.Contains(
"c")) opt.Append(
"L");
554 if (!opt.Contains(
"same") && !plotHist) {
556 TH2F* hist2D =
new TH2F(
"_hist2D",title, nPoints, xmin, xmax, nPoints, ymin, ymax );
557 hist2D->GetXaxis()->SetTitle(myparam->GetName());
558 hist2D->GetYaxis()->SetTitle(myparamY->GetName());
559 hist2D->SetBit(TH1::kNoStats);
560 hist2D->SetFillStyle(fFillStyle);
561 hist2D->SetMaximum(1);
562 hist2D->Draw(
"AXIS");
564 if (fLineColor) gr->SetLineColor(fLineColor);
567 gr->SetFillColor(fColor);
571 if (opt.Contains(
"same")) gr->SetFillStyle(fFillStyle);
573 TString name = TString(
"Graph_of_") + TString(fInterval->GetName());
576 if (!fPlotObject) fPlotObject = gr;
577 else if (fPlotObject->IsA() != TH2D::Class() ) fPlotObject = gr;
582 const RooArgSet * bestFitParams = fInterval->GetBestFitParameters();
584 TGraph * gr0 =
new TGraph(1);
585 double x0 = bestFitParams->getRealValue(myparam->GetName());
586 double y0 = bestFitParams->getRealValue(myparamY->GetName());
587 gr0->SetPoint(0,x0,y0);
588 gr0->SetMarkerStyle(33);
590 if (fColor != kBlack) gr0->SetMarkerColor(fColor+4);
591 else gr0->SetMarkerColor(kGray);
594 delete bestFitParams;
602 if (newProfile != oldProfile)
delete newProfile;