# Projecting from a TTree into a 2D histogram

Hello,

I have a tree from which I need to fill a 2-d histogram.
This is the tree content:

``tree->Scan();``

[ul][color=darkblue]

• Row * nsensor * temp *

• ``````   0 *         1 * 25.799999 *
``````
• ``````   1 *         2 * 26.799999 *
``````
• ``````   2 *         3 * 25.700000 *
``````
• ``````   3 *         4 * 23.899999 *
``````
• ``````   4 *         5 * 25.299999 *
``````
• ``````   5 *         6 * 24.100000 *
``````
• ``````   6 *         7 * 24.600000 *
``````
• ``````   7 *         8 * 25.100000 *
``````
• ``````   8 *         9 * 24.899999 *
``````
• ``````   9 *        10 * 23.899999 *
``````

[/color][/ul]

So, if you look at the “nsensor” variable, you’ll see that it assumes integer values from 1 to 10.
I project the contents of the tree in a histogram:

``````TH2F* h2 = new TH2F("h2","Temperature",12,0.,12.,50,22.,28.);
tree->Project("h2","temp:nsensor");
h2->SetMarkerStyle(28);
h2->Draw();``````

and this is what I get: Now, the values on the x axis should correspond to the (integer) values of nsensor, while they do not.
So I assume I am not doing the right thing. What should I do instead?
Maybe use TGraph? Then, how?

I can add that if I simply [color=green]tree->Draw(“temp:nsensor”)[/color], the plot looks correct.

[quote]I can add that if I simply tree->Draw(“temp:nsensor”), the plot looks correct[/quote]The different is that this command produce an exact plot (using a TGraph) while Project (by definition) uses an histogram which bins the result. If you do need the histogram, you can try to increase the number of bins and/or use TH2I.

Cheers,
Philippe.

[quote=“pcanal”]If you do need the histogram, you can try to increase the number of bins and/or use TH2I.
[/quote]

Philippe, thanks for your suggestion. Now the plot looks correct: But this procedure gives a little inconvenience: in fact, I’d like to set alphanumeric labels on the x axis.

How can I set labels just for the bins of interest and not for the 100 divisions I need to set in TH2I to obtain a good plot?

Thanks!

see example in tutorial labels1.C

Rene

What if I do not know the limits of the data in `TTree` in advance. How do I project the data on 2D histogram in this case?