# Histogram with \[x,y) bin and store in tree

**URL:** <https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045>\
**Category:** Newbie\
**Created:** [March 18, 2021, 5:25am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045 "2021-03-18T05:25:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 18, 2021, 5:25am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/1 "2021-03-18T05:25:54Z")

</div>

_ROOT Version:_ 6.18/00  
_Platform:_ Ubuntu 20.04.2 LTS  
_Compiler:_ C++/ROOT

* * *

Hello everyone , I’m new to root.  
I would like to make a histogram from data file (below is the images) and store in a tree in a ROOT file.

 ![hist](https://root-forum.cern.ch/uploads/default/original/3X/3/2/327f852f5f7ed1ff5f341cb7211813cb08bc3756.png)

Also, I wanted to plot a topographical contour as N(3rd column) = f(pz,pt) bins with pz along the horizontal and pt the vertical.  
Can anyone please help me ?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 18, 2021, 7:48am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/2 "2021-03-18T07:48:49Z")

</div>

You can use [TTree::ReadFile](https://root.cern/doc/master/classTTree.html#a9c8da1fbc68221b31c21e55bddf72ce7)

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 18, 2021, 9:42am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/3 "2021-03-18T09:42:50Z")

</div>

Thanks for the reply.

I have the idea with a bin centre value like  
pz pt  
0.3 0.10  
0.5 0.20  
0.32 0.12  
…  
…  
…  
.  
I’m not quite sure about the histogram with bin range.

Can I please get more clarification?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 18, 2021, 9:47am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/4 "2021-03-18T09:47:26Z")

</div>

If you create a TNuple or a TTree using ReadFile as I mentioned before. You will then have a tree with 3 columns pz pt and POT. If you want to see pz vs pt you then do:

```auto
root [0] yourtree->Draw("pz:pt");

```

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 18, 2021, 9:56am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/5 "2021-03-18T09:56:21Z")

</div>

Yes, I have the idea for that.  
Here is my code:

```auto
void dat_root()
  { 
   ifstream data;
   Float_t xF, pT, xsect, error;
   
   data.open("data.txt");
   TFile* file = new TFile("data.root","RECREATE");
 
  // TTree* tree = 0;  
   TNtuple* ntup = new TNtuple("ntup","import data file","xF:pT:xsect:error");
   
   while(1){ 
   data>>xF>>pT>>xsect>>error;
   if(!data.good()) break;
   
   ntup->Fill(xF,pT,xsect,error);
  // cout<<xF<<pT<<xsect<<error<<endl;
   }  
   data.close();
   file->Write();

```

And here is the text file ;

```auto
#xF pT xsect error
0.010 0.050 576.05603 9.82745
0.010 0.100 539.52253 8.51654
0.010 0.150 445.00001 6.66747
0.010 0.200 361.98333 5.64581
0.010 0.250 274.02531 4.79218
0.010 0.300 218.10507 4.38402
0.010 0.400 113.03824 2.17800
0.010 0.500 60.95881 1.57144
0.010 0.600 35.18787 1.18463
0.010 0.700 18.92297 0.85714
0.020 0.050 468.95175 10.15406
0.020 0.100 464.94718 7.17576
0.020 0.150 407.03352 6.65860
0.020 0.200 320.49617 5.40648
0.020 0.250 256.70166 4.78191
0.020 0.300 189.21733 3.91935
0.020 0.400 107.04824 1.97835
0.020 0.500 59.08772 1.57044
0.020 0.600 33.43162 1.18071
0.020 0.700 18.74202 0.87785
0.025 0.800 10.58136 0.41885
0.025 0.900 6.44480 0.33107
0.025 1.000 3.27737 0.21996
0.025 1.200 1.37635 0.10211
0.030 0.050 387.01476 8.26072
0.030 0.100 387.89989 6.96068

```

But, I’m confused with that range [0.30,0.50),…  
How can I do the same ?

Sorry for my ignorance.  
Thanks

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 18, 2021, 10:26am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/6 "2021-03-18T10:26:32Z")

</div>

try:

```auto
   TTree *T = new TTree("T","data from ascii file");
   Long64_t nlines = T->ReadFile("data.txt","xF:pT:xsect:error");
   T->Draw("xF");

```

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 18, 2021, 10:31am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/7 "2021-03-18T10:31:37Z")

</div>

```auto
void simple_tree()
{
   TFile *f = new TFile("data.root","RECREATE");
   
    Int_t nEntries = 270;
    Int_t nbins = 270;   
   
   TH2F* hist = new TH2F(""," ",nbins,0,0.5,nbins,0,1);
   TCanvas* c = new TCanvas("c","",800,600);
   
   TTree *tree = new TTree("data","data from ascii file");
   Long64_t nlines = tree->ReadFile("data.txt","xF:pT:xsect:error");
   printf(" found %lld pointsn",nlines);
   tree->Write();
   
   tree->Draw("pT:xF>>hist","xsect","colz");
  
   c->Update();
}

```

Thank you.  
Actually, I have this. But how will I write those range in text format?  
For example ;

```auto
pz pz
[0.30,0.50) as like this--> 0.30
[0.30,0.50) 0.30
... ...
.. ...    
[0.50,0.62) 0.50
..

```

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 18, 2021, 10:35am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/8 "2021-03-18T10:35:15Z")

</div>

> [@matt](#):
>
> But how will I write those range in text format?

what do you mean by “range” ?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [March 18, 2021, 10:40am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/9 "2021-03-18T10:40:56Z")

</div>

```cpp
{
  double pz_bins[] = {0.30, 0.50, 0.62, 0.75, 0.88, 1.00};
  unsigned long n_pz_bins = sizeof(pz_bins) / sizeof(double) - 1;
  double pT_bins[] = {0.00, 0.10, 0.20, 0.30, 0.40, 0.50};
  unsigned long n_pT_bins = sizeof(pT_bins) / sizeof(double) - 1;
  TH2D *h = new TH2D("h",
                     "N(#pi^{+}_{}) / POT;p_{z}^{} (GeV/c);p_{T}^{} (GeV/c)",
                     n_pz_bins, pz_bins, n_pT_bins, pT_bins);
  h->SetStats(kFALSE);
  double N[] = {3.32e-01, 3.25e-01, 1.93e-01, 1.06e-01, 0.0, // missing data!
                9.20e-02, 1.37e-01, 9.03e-02, 4.92e-02, 1.42e-02,
                6.02e-02, 1.11e-01, 8.26e-02, 4.32e-02, 2.19e-02,
                4.27e-02, 9.19e-02, 7.46e-02, 4.21e-02, 2.16e-02,
                3.42e-02, 7.50e-02, 6.68e-02, 3.82e-02, 1.99e-02};
  unsigned long n_N = sizeof(N) / sizeof(double);
  if (n_N != (n_pz_bins * n_pT_bins)) {
    std::cout << "Error: missing data." << std::endl;
    return;
  }
  for (int i = 1; i <= n_pz_bins; i++)
    for (int j = 1; j <= n_pT_bins; j++)
      h->SetBinContent(i, j, N[((i - 1) * n_pT_bins + (j - 1))]);
  h->Draw("COLZ TEXT");
}

```

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 18, 2021, 10:54am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/10 "2021-03-18T10:54:17Z")

</div>

Thank you very much .

I do really appreciate it for the help.

---

<div class="post-metadata">

**Author:** ![Cein](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/c/b2d939/32.png) [@Cein](https://root-forum.cern.ch/u/Cein)\
**Post date:** [March 19, 2021, 10:52pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/11 "2021-03-19T22:52:48Z")

</div>

Hi,

I have 1d histogram and I have saved it with “.root” extension. If I zoom it on the x axis to study a particular region, it does not look good unless I increase the bin size. But doing the analysis of ~1hr everytime just to change the x axis range and bin size does not make much sense. How can I change the bin size from the root file?

ps: I know I can change it from the .C file, but I was just curious if I can do it from the root file also as it is much easier.

Thanks for any suggestions 🙂

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 22, 2021, 8:51am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/12 "2021-03-22T08:51:48Z")

</div>

It is not a matter of “.root file” or “.C file”. We are talking about a 1D histogram here. You create it in a macro (the .C file) and save it for later use in a “.root” file. Once saved in a .root file (a `TFile`) you cannot change the booking parameters (limits, bin size etc…) like you would do at creation time in the ROOT macro. When you zoom a histogram on the X axis, you will reach a zoom level where there is only a few bins displayed and, at the extreme, only one. I do not understand how “increasing the bin size” would help. It is the opposite I would say. Can you provide some concret example ?

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 22, 2021, 11:05am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/13 "2021-03-22T11:05:05Z")

</div>

Hello, I would like to kindly ask a question in extension to this.  
I would like to access the bin center for each x (pz\_bins) and y (pT\_bins).

Also, plot the value(here : stats\_error) with a different marker style for different value range. I have searched in the past questions but couldn’t find what I exactly want.

I have attached the plot which I attempts along with the code ([ask.C](https://root-forum.cern.ch/uploads/short-url/4EtnPlYllOYYklsOlA1aD3iN6uK.C) (3.2 KB) ) .

 ![plot](https://root-forum.cern.ch/uploads/default/original/3X/1/4/14930ed53d65400318aff61b55013242f373d91a.png)

As seen from the plot, it stores the value in all the bins. I would like to store only in the bin center of (pz,pT).  
If you could help me I would be very grateful.

Sorry for my ignorance.  
Thanks again in advance.

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 22, 2021, 11:26am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/14 "2021-03-22T11:26:31Z")

</div>

> [@matt](#):
>
> access the bin center

[https://root-forum.cern.ch/search?q=TH2D%20getbincenter](https://root-forum.cern.ch/search?q=TH2D%20getbincenter)

> [@matt](#):
>
> it stores the value in all the bins. I would like to store only in the bin center of (pz,pT).

What do you mean?

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 22, 2021, 11:58am UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/15 "2021-03-22T11:58:55Z")

</div>

Thanks for the reply.

Statistical error/uncertainty ( stats\_error[] in my code [ask.C](https://root-forum.cern.ch/uploads/short-url/4EtnPlYllOYYklsOlA1aD3iN6uK.C) (3.2 KB) ) on measurement of the number of pions in (pz,pT) phase space varies from 0 to 10 (or more) percentage.  
I wanted to plot them using different marker style i.e, \< 2.5 % (as dot) , between 2.5% and 5% (as circle) and \>5% (as plus) in (pz,pT) bins.

I’m sorry if my explanation is not clear.(Please kindly correct me if I’m wrong)

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 22, 2021, 12:18pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/16 "2021-03-22T12:18:35Z")

</div>

Not sure I understand correctly, but I guess:

- you want to plot “he” as colour (“colz”)
- for “he\_a/b/c”, you only want the marker at the corresponding bin; then we just need any value in the bin (the value is not important), to plot the marker. Maybe something like this:

```auto
for i in x bins {
  for j in y bins {
    he -> setbincontent(i,j,error); // so the values are shown as colour
    if (error < 2.5%) he_a -> setbincontent(i,j,1);
    else if (error < 5%) he_b -> setbincontent(i,j,1);
    else he_c -> setbincontent(i,j,1);
  }
}
// set the marker styles you want for a, b, c:
he_a->SetMarkerStyle(...);
he_b->SetMarkerStyle(...);
he_c...
// draw
he->Draw("colz");
he_a->Draw("same");
he_b->Draw("same");
he_c->Draw("same");

```

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 22, 2021, 12:30pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/17 "2021-03-22T12:30:31Z")

</div>

Yes, This is what I want.

But, for this case "i " will be 0.30 , 0.5, 0.62, …and so on while “j” will be 0.00, 0.10, 0.20 …(if I’m not wrong).  
I would want my “i” to be the center of 0.30 and 0.50, 0.50 and 0.62 , 0.62 and … and “j” to be center of "0.00 and 0.10, 0.10 and 0.20, and so on.

Is it possible?

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [March 22, 2021, 12:44pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/19 "2021-03-22T12:44:18Z")

</div>

May be:

```auto
x = h.GetXaxis()->GetBinCenter(i);
y = h.GetYaxis()->GetBinCenter(j);

```

?

---

<div class="post-metadata">

**Author:** ![matt](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/matt/32/30804_2.png) [@matt](https://root-forum.cern.ch/u/matt)\
**Post date:** [March 22, 2021, 12:52pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/20 "2021-03-22T12:52:24Z")

</div>

I think this works when a histogram is stored in a root file.  
you mean like this?

for ( int k = 1; k\<=n\_pz\_bins; k++)  
for ( int l = 1; l \<= n\_pT\_bins; l++)  
he-\>SetBinContent(k, l, stats\_error[((k - 1) \* n\_pT\_bins + (l - 1))]);  
double x = ((TAxis\*)he-\>GetXaxis())-\>GetBinCenter(k);  
double y = ((TAxis\*)he-\>GetYaxis())-\>GetBinCenter(l);

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [March 22, 2021, 12:57pm UTC](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045/21 "2021-03-22T12:57:52Z")

</div>

I see, the markers are not drawn at the bin centres. I don’t know if there’s an option to fix that, but one workaround coule be the “text” option, which is centred; since this shows the value of the bin, and you need 3 markers, you could use values of 1, 0 and -1 in the SetBinContent lines, instead of 1 (for a, b, and c, resp.), then draw with `he_a->Draw("same,text");` and so on.  
(or use GetBinCenter on “he” and define new histos for a/b/c with these new bins)

[Next page](https://root-forum.cern.ch/t/histogram-with-x-y-bin-and-store-in-tree/44045.md?page=2)
