Add legend in roofit

Dear experts,

I am trying to to add a legend in a roofit code,but I am not sure why this doesn’t work. I am using the lines:

> TLegend *leg = new TLegend(0.57,0.73,0.78,0.93);
>   leg->SetBorderSize(0);  // no border
>   leg->SetFillStyle(0);
>   leg->AddEntry("deltaM_kspipi","Data","pe");
>   leg->AddEntry("pdf","Total Fit","l");
>   leg->AddEntry("gauss","Gaussian","l");
>   leg->AddEntry("johnson","Johnson","l");

Pleas have a look at my code in the directory: legend_roofit.zip (1.8 MB)

Please read tips for efficient and successful posting and posting code

_ROOT Version:_6.24/06
_Platform:_ubuntu 20

Compiler: Not Provided


This was already asked on this forum:

Dear @couet

Thank you very much for your response. Here’s the solution. It might not be straight forward to guess the names of the PDFs.

TLegend *leg = new TLegend(0.57,0.73,0.78,0.93);
  leg->SetBorderSize(0);  // no border
  leg->SetFillStyle(0);
  leg->AddEntry("h_data","Data","pe");
  leg->AddEntry("pdf_Norm[deltaM_kspipi]","Total Fit","l");
  leg->AddEntry("pdf_Norm[deltaM_kspipi]_Comp[gauss]","Gaussian","l");
  leg->AddEntry("pdf_Norm[deltaM_kspipi]_Comp[johnson]","Johnson","l");
  leg->Draw();

Here’s what helped.

1 Like

Dear @couet,

I tried to use the same method for a 2D simultaneous fit but it does not work in that case. I have also tried:

 TLegend *leg_deltaM_d0 = new TLegend(0.54,0.32,0.84,0.52);
  leg_deltaM_d0->SetBorderSize(0);  // no border
  leg_deltaM_d0->AddEntry(deltaM_d0->findObject("deltaM"),"Data","pe");
  leg_deltaM_d0->AddEntry(deltaM_d0->findObject("simpdf"),"Total Fit","l");
  leg_deltaM_d0->AddEntry(deltaM_d0->findObject("signal_peak_d0"),"Signal","l");
  leg_deltaM_d0->AddEntry(deltaM_d0->findObject("kspipi_peak_d0"),"K_{s}#pi^{+}#pi^{-} background","l");
  leg_deltaM_d0->AddEntry(deltaM_d0->findObject("non_peaking_d0"),"Other backgrounds","l");
  leg_deltaM_d0->Draw();

Here’s the directory in which I am working:
legend_simfit.zip (1.8 MB)

Will you please have a a look at it.

it does not work:

root [0] .x mc15_asymmetry_fit.C 
Processing mc15_asymmetry_fit.C...
In file included from input_line_9:1:
/Users/couet/Downloads/legend_simfit/mc15_asymmetry_fit.C:315:10: error: no matching member function for call to 'paramOn'
  simPdf.paramOn(deltaM_d0,dataxy_d0);
  ~~~~~~~^~~~~~~
/Users/couet/git/couet-root-bin/include/RooAbsPdf.h:138:20: note: candidate function not viable: no known conversion from 'RooDataSet *' to 'const RooCmdArg' for 2nd argument
  virtual RooPlot* paramOn(RooPlot* frame,
                   ^
/Users/couet/git/couet-root-bin/include/RooAbsPdf.h:327:20: note: candidate function not viable: no known conversion from 'RooDataSet *' to 'const RooArgSet' for 2nd argument
  virtual RooPlot* paramOn(RooPlot* frame, const RooArgSet& params, bool showConstants=false,
                   ^
In file included from input_line_9:1:
/Users/couet/Downloads/legend_simfit/mc15_asymmetry_fit.C:446:10: error: no matching member function for call to 'paramOn'
  simPdf.paramOn(log_min_significance_d0bar,dataxy_d0bar);
  ~~~~~~~^~~~~~~
/Users/couet/git/couet-root-bin/include/RooAbsPdf.h:138:20: note: candidate function not viable: no known conversion from 'RooDataSet *' to 'const RooCmdArg' for 2nd argument
  virtual RooPlot* paramOn(RooPlot* frame,
                   ^
/Users/couet/git/couet-root-bin/include/RooAbsPdf.h:327:20: note: candidate function not viable: no known conversion from 'RooDataSet *' to 'const RooArgSet' for 2nd argument
  virtual RooPlot* paramOn(RooPlot* frame, const RooArgSet& params, bool showConstants=false,
                   ^
root [1] 

I am not sure why. I did check the code before sending it. I have checked it again. Could you try this:
mc15_asymmetry_fit_check.C (32.0 KB)

still:

Processing mc15_asymmetry_fit_check.C...
In file included from input_line_9:1:
/Users/couet/Downloads/legend_simfit/mc15_asymmetry_fit_check.C:315:10: error: no matching member function for call to 'paramOn'
  simPdf.paramOn(deltaM_d0,dataxy_d0);
  ~~~~~~~^~~~~~~

I am not sure why this is happening… The code works for me. Could you try *dataxy_d0, although it does not work in my case.

Sorry, I know. I do not have roofit installed in my ROOT…
I think the answer of you problem is here:

Try to fine the name of the real object as I did in that example.

I did try gPad->ls(). It just gives me the following and the names are not very clear I . I am not sure if this the same problem as the one you solved before.

TPad fXlowNDC=0 fYlowNDC=0 fWNDC=1 fHNDC=0.25 Name= c4_2 Title= c4_2 Option=
 OBJ: TList	TList	Doubly linked list : 0
  TFrame  X1= -6.000000 Y1=-4.000000 X2=6.700000 Y2=4.000000
  OBJ: TH1D	frame_557bcf9d35d0	  : 0 at: 0x557bcf532160
  OBJ: RooHist	pull_h_combData_Cut[sample==sample::D0bar]_model_d0bar_Int[deltaM]_Norm[deltaM,log_min_significance]	Pull of Histogram of combData_plot__log_min_significance and Projection of  : 0 at: 0x557bcf518800
  OBJ: TH1D	frame_557bcf9d35d0	  : 0 at: 0x557bcf532160
  OBJ: TPaveText	title  	X1= 0.145763 Y1=4.400000 X2=0.554237 Y2=4.950000

the name is:

pull_h_combData_Cut[sample==sample::D0bar]_model_d0bar_Int[deltaM]_Norm[deltaM,log_min_significance]

This is how. it works. One has to assign names.

>  pdf.plotOn(d0plot,Components(RooArgSet(pdf)),Name("pdf"));  
> 
>   TLegend *legend = new TLegend(0.54,0.32,0.84,0.52);
>   legend->AddEntry(d0plot->findObject("data"),"Data","pe");
>   legend->AddEntry(d0plot->findObject("pdf"),"Total Fit","l");