Roofit: TLegend

@couet I apologize for lond silence.
The enclosed file is the a macro I use. Unfortunately, I cannot enclose the root files the data is extracted from, but I hope the macro will be enough to understand what I am doing wrong.
Many-many thanks for your help.

FitData_17_compare.C (5.1 KB)

I made an example:

using namespace RooFit;

void roofit_legend()
{
   RooRealVar dt("dt", "dt", -20, 20);
   RooRealVar dm("dm", "dm", 0.472);
   RooRealVar tau("tau", "tau", 1.547);
   RooRealVar w("w", "mistag rate", 0.1);
   RooRealVar dw("dw", "delta mistag rate", 0.);
   RooCategory mixState("mixState", "B0/B0bar mixing state");
   mixState.defineType("mixed", -1);
   mixState.defineType("unmixed", 1);
   RooCategory tagFlav("tagFlav", "Flavour of the tagged B0");
   tagFlav.defineType("B0", 1);
   tagFlav.defineType("B0bar", -1);
   RooRealVar bias1("bias1", "bias1", 0);
   RooRealVar sigma1("sigma1", "sigma1", 0.1);
   RooGaussModel gm1("gm1", "gauss model 1", dt, bias1, sigma1);
   RooBMixDecay bmix("bmix", "decay", dt, mixState, tagFlav, tau, dm, w, dw, gm1, RooBMixDecay::DoubleSided);
   RooDataSet *data = bmix.generate(RooArgSet(dt, mixState, tagFlav), 2000);
   RooBinning abins(-10, 10);
   abins.addBoundary(0);
   abins.addBoundaryPair(1);
   abins.addBoundaryPair(2);
   abins.addBoundaryPair(3);
   abins.addBoundaryPair(4);
   abins.addBoundaryPair(6);

   // Create plot frame in dt
   RooPlot *aframe = dt.frame(Range(-10, 10));

   data->plotOn(aframe, Name("data1"), Asymmetry(mixState), Binning(abins));

   // Draw plots on canvas
   TCanvas *c = new TCanvas("rf108_plotbinning", "rf108_plotbinning", 400, 400);
   aframe->Draw();

   // Draw legend
   TLegend *leg = new TLegend(0.55,0.65,0.89,0.89);
   leg->AddEntry("data1", "This is data1", "p");
   leg->Draw();
}

1 Like

@couet Thank a lot for yout example! Although, my problem was in inability of a Legend to represent the colors of different datasets. In the example I shared I have several RooDataSets that I draw with the point of different color. But in the legegend they are all black. And I cannot match the color of points in the plot with the color of points in the Legend. I executed your example to make sure and can see that it doesn’t reproduce the problem I am facing.

Regards,
Dmytro.

How to you set the marker color in the example above ?

@couet I plot datasets as:
data2->plotOn(frame, MarkerColor(kGreen+3), MarkerSize(0.7), LineColor(kGreen+3));
And expect the defined color to be transfered to a Legend.

If it is wrong, could you please explain how I can plot data with a points if specific color and transfer the same color to a Legend.

It is properly transferred but only if you specify correctly the name of the data set as shown in the example I sent.

   data->plotOn(aframe,
                        Name("data1"), ///<<<<< The important part
                        MarkerColor(kGreen+3),
                        Asymmetry(mixState), 
                        Binning(abins));


   // Draw plots on canvas
   TCanvas *c = new TCanvas("rf108_plotbinning", "rf108_plotbinning", 400, 400);
   aframe->Draw();

   // Draw legend
   TLegend *leg = new TLegend(0.55,0.65,0.89,0.89);
   leg->AddEntry("data1", "This is data1", "p");
   leg->Draw();
1 Like

@couet, it works!!! I am sincerely grateful for your help. I also apologize for not mentioning this part in your previous reply.

Many-many thanks.