Put legend and text inside a box in ROOT

I want to draw a box in cern root which contains both the legend and the text (written through TLatex) . For this I tried TBox, but it is not showing the box.

here is the code snippet :

 TCanvas *cmean = new TCanvas("cmean", " ", 1053, 991);
    cmean->Divide(1, 2);
    TPad *pad1 = (TPad *)cmean->GetPad(1);
    TPad *pad2 = (TPad *)cmean->GetPad(2);

    pad1->SetBottomMargin(0.2);
    pad2->SetTopMargin(0.12);
    pad2->SetBottomMargin(0.18);
    pad1->SetGrid(1, 1);
    pad2->SetGrid(1, 1);

    pad1->cd();
    SetCanvasStyle(cmean, 0.15, 0.15, 0.15, 0.15);
    SetHistostyle2(meanped);

    meanped->SetTitle(0);
    meanped->SetMarkerStyle(8);
    meanped->Fit("fitgaus", "M");
    meanped->GetXaxis()->SetTitle("Pedestal Mean ADC");
    meanped->GetYaxis()->SetTitle("Counts");
    meanped->Draw("pe");

    TLegend *leg = new TLegend(0.16, 0.65, 0.45, 0.8);
    leg->AddEntry(meanped, "Data point", "lpe");
    leg->AddEntry(fitgaus, "Gauss fit", "l");
    leg->SetFillColor(0);
    leg->SetFillStyle(0);
    leg->SetTextFont(42);
    leg->SetTextSize(0.045);
    leg->SetLineColor(0);
    leg->SetShadowColor(0);
    leg->Draw();
    TLatex lat;
    lat.SetNDC();
    lat.SetTextSize(0.045);
    lat.SetTextFont(42);
    // lat.SetTextColor(4);
    lat.DrawLatex(0.16, 0.8, "Pedestal mean distribution");
    // lat.SetTextColor(1);
    lat.DrawLatex(0.16, 0.60, "Beam: OFF");
    lat.DrawLatex(0.16, 0.55, "Bias Voltage: 60V");
    // lat.DrawLatex(0.2,0.65,Form("Mean: %.2f",fitgaus->GetParameter(1)));
    // lat.DrawLatex(0.2,0.6,Form("Sigma: %.2f",fitgaus->GetParameter(2)));

    TBox *box = new TBox(0.14, 0.53, 0.5, 0.9); 
    box->SetFillColor(19);
    box->Draw();

    TBox *box2 = new TBox(0.14, 0.53, 0.5, 0.9);
    box2->SetFillStyle(0);
    box2->SetLineWidth(2);
    box2->SetLineColor(2);
    box2->Draw();

@swanski82 If you could provide more info or your code which we can run it’d be easier to help you out. From what I have understood if you want to make a box around legend you can do it by

leg->SetBorderSize(2);

and you don’t have to add latex text in your legend separately. you can do it like this

leg->AddEntry(meanped, "M_{R} = \\frac{m_1}{m_1+m_2} = \\frac{v_2}{v_1+v_2}", "lpe");
leg->AddEntry(fitgaus, "{}^{19}F +{}^{197}Au, Expt", "p");

Hope it helps.

To write about the data point, fitting which is in legend and something else using TText which is not a part of histogram, then how to put them is same box. Actually now my code is working, but the dimension of box is very strange. for pad 1 it is TBox *box0 = new TBox(124.5501,16.55008,152.6818,36.62952); and for pad 2 it is TBox *box = new TBox(0.5023204,23.36309,4.259675,51.34359);

Can you provide a running script showing the problem?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.