Displaying the content of a variable on a histogram

Hi -

I am having trouble displaying something I calculated (as a double) on my histogram. I have converted the double to a string, but this also doesn’t seem to help much.

Is there a way to either display a number directly or display a string using SetTitle or TLegend or anything?

Thanks -

You can draw a TText or TLatex object in your pad using either pad coordinates or NDC coordinates. see example below


TH1F *h = new TH1F(“h”,“test”,100,-3,3);
TCanvas *c1 = new TCanvas(“c1”);
//draw a TText in pad/user coordinates
TText *t = new TText(0,10,“I am a TText”);
//draw a Tlatex in NDC
TLatex *l = new TLatex(0.2,0.7,“I am a TLatex”);



I still ended up needing to just recast my number into a character array in order for root to accept it as something which could be printed to the screen. If there happens to be some other way to display calculated values (doubles) directly onto a canvas it would be great to know.



I’m not a ROOT developer or anything, but I’ve had exactly this problem in the past. I know two good methods of doing this: one uses TString, the other uses std::ostringstream. Either can provide input into a TLatex object for fancy printing.

With a TString, you can use TString::operator+=(…)

TString d;
double var = 9.05;
d = "A string";
d += var;
d += " in some units";
TLatex *l = new TLatex(0.2,0.7,d.Data());
// and so on

std::ostringstream works in practice just like cout (this is because they’re both output streams). I find this particularly useful for formatting the output. So you just do something like (note the #include statements)

#include <iostream>
#include <sstream>
ostringstream d;
double var = 9.05;
d << "A string " << var << " in some units";
TLatex *l = new TLatex(0.2,0.7,d.str().c_str());

Have a look at cplusplus.com/reference/iost … ingstream/ for more on how to use this.

Mike Flowerdew

or much simpler:

double var = 9.05; TLatex *l = new TLatex(0.2,0.7,Form(A string %g in some units",var)); // and so on


1 Like

Awesome! Thanks to both of you.
That actually solved another problem I was having as well :smiley: