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 -
Cara

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

Rene

[code]{
TH1F *h = new TH1F(“h”,“test”,100,-3,3);
h->FillRandom(“gaus”,2000);
TCanvas *c1 = new TCanvas(“c1”);
h->Draw();
//draw a TText in pad/user coordinates
TText *t = new TText(0,10,“I am a TText”);
t->SetTextSize(0.04);
t->Draw();
//draw a Tlatex in NDC
TLatex *l = new TLatex(0.2,0.7,“I am a TLatex”);
l->SetNDC();
l->SetTextSize(0.04);
l->Draw();

}
[/code]

Thanks!

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.

Cara

Hi,

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.

Cheers,
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

Rene

1 Like

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

Cara