Natilus
September 6, 2023, 8:06am
1
Dear experts,
I have a plot and I create a legend for it including some numbers (yields). the numbers are shown with 6 digits after the decimal point. I would like to format the numbers shown in my Legend to show one digit after decimal point.
example: 152.600000 → 152.6
Thank you for the help
couet
September 6, 2023, 8:40am
2
Can you post the code creating the legend ?
Natilus
September 6, 2023, 9:01am
3
Hi @couet
Thanks for the quick response. Here is the code:
std::string BMassCTitle = "B-Mass Corresponding to Different B-Vertex Probabilities";
TCanvas* BMass_canv = new TCanvas("BVertexProbability_BMass",BMassCTitle.c_str());
BMass_canv->SetWindowSize(WW, WH);
BMass_canv->SetGridx();
BMass_canv->SetGridy();
gStyle->SetOptStat(0);
Functions[0]->SetTitle("B-Vertex Probabilities");
Functions[0]->GetXaxis()->SetTitle("M_{B}(GeV)");
Functions[0]->GetXaxis()->SetRangeUser(5.08, 5.48);
Functions[0]->GetXaxis()->SetNdivisions(8);
Functions[0]->GetYaxis()->SetRangeUser(0,(Functions[0]->GetMaximum()+0.1*(Functions[0]->GetMaximum())));
Functions[0]->SetLineColor(1);
Functions[0]->SetFillColor(1);
Functions[0]->SetFillStyle(3001);
Functions[0]->Draw("LF2");
for (int i=1; i<(int)Functions.size(); i++) {
int Color = i+1;
if (Color==10)
Color = 46;
Functions[i]->SetLineColor(Color);
Functions[i]->SetFillColor(Color);
Functions[i]->SetFillStyle(3001);
Functions[i]->Draw("LF2,SAME");
}
TLegend* BMass_l = new TLegend(0.75,0.60,0.95,0.85);
BMass_l->SetTextSize(0.015);
BMass_l->SetNColumns(3);
BMass_l->SetHeader(BMassCTitle.c_str(),"C");
for (int j=0; j<(int)Functions.size(); j++) {
BMass_l->AddEntry(Functions[j],("VtxProb_{B} > "+to_string(BVtxStart+(j*BVtxStep))).c_str(),"l");
BMass_l->AddEntry((TObject*)0,("SY = "+to_string(Yields[j].first.first)+" +/- "+to_string(Yields[j].first.second)).c_str(),"");
BMass_l->AddEntry((TObject*)0,("BY = "+to_string(Yields[j].second.first)+" +/- "+to_string(Yields[j].second.second)).c_str(),"");
}
BMass_l->Draw();
BMass_canv->Update();
In the above code, Functions is an array that contains TF1* objects.
couet
September 6, 2023, 9:42am
4
in your example to_string
does the conversion to string. I am not sure you can specify the formatting for to_string
ferhue
September 6, 2023, 9:51am
5
Natilus
September 6, 2023, 9:56am
6
The reason I used to_string() is because I thought I can only use strings in the AddEntry method.
Is it possible to format before turning into string? or just not use strings?
Try what ferhue suggested, or you can also use Form; e.g. if val
is a double and you want 1 decimal:
BMass_l->AddEntry(mygraph,Form("%.1f",val),"l");
Natilus
September 6, 2023, 10:34am
8
Hi @dastudillo ,
That worked perfectly!
Thank you all for your help.