# Getting histogram x axis value/plotting a TGraph

**URL:** <https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620>\
**Category:** Newbie\
**Created:** [March 30, 2020, 10:13am UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620 "2020-03-30T10:13:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KatR](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/d9b06d/32.png) [@KatR](https://root-forum.cern.ch/u/KatR)\
**Post date:** [March 30, 2020, 10:13am UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/1 "2020-03-30T10:13:33Z")

</div>

Hi all,

Apologies in advance, it’s probably a simple question but I don’t know much C++ and found the documentation hard to understand. I have a TH1F made like this:  
TH1F \*b = new TH1F(“b”, “NTrack”, 120, 0, 120);  
For a given bin number i, I want to check whether the x-axis value is \>k or \<k and I’m not sure how. I’ve tried this:

```auto
for (int k=0;k<randomBigNumber;k++){
for (int i=0; i<b->GetNbinsX(); i++){
        double_t a = static_cast<double>(i);
        if(b->GetXaxis()->GetCenter(a)>k){gluons+=b->GetBinContent(i);}
}
for (int i=0;i<g->GetNbinsX();i++){
        double_t a = static_cast<double>(i);
        double_t *ptr = &a;
        if(g->GetXaxis()->GetCenter(*ptr)<k){quarks+=g->GetBinContent(i);}
}

```

But it tells me \*\*cannot initialize a parameter of type 'Double\_t _’ (aka 'double ') with an lvalue of type ‘double\_t’ (aka ‘double’)_  
The same error for both for loops. Once I’ve done this I need to plot the resulting values of gluons vs quarks, and I don’t know how to do this either! I should fill an array and use that to plot a TGraph somehow but I’m not sure what the syntax should look like.

Thanks in advance for any help!

---

<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 30, 2020, 3:21pm UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/2 "2020-03-30T15:21:09Z")

</div>

According to the [doc of GetCenter](https://root.cern/doc/master/classTAxis.html#a00acd3d4caf7fb0268325fa671c87969), `a` should be an array

---

<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 30, 2020, 6:15pm UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/3 "2020-03-30T18:15:24Z")

</div>

```cpp
for (int i = 1; i <= b->GetNbinsX(); i++)
  if(b->GetBinCenter(i) > k) gluons += b->GetBinContent(i);

```

---

<div class="post-metadata">

**Author:** ![KatR](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/d9b06d/32.png) [@KatR](https://root-forum.cern.ch/u/KatR)\
**Post date:** [March 30, 2020, 6:38pm UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/4 "2020-03-30T18:38:10Z")

</div>

This triggers the same type of error, essentially that the argument of GetBinCenter can’t be an integer. I tried declaring it as a double but this just results in the error message in my first post. ☹

---

<div class="post-metadata">

**Author:** ![KatR](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/k/d9b06d/32.png) [@KatR](https://root-forum.cern.ch/u/KatR)\
**Post date:** [March 30, 2020, 6:39pm UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/5 "2020-03-30T18:39:45Z")

</div>

Thanks for the link! 🙂 But what should the array contain? I can’t give it the x, y values of the bin because that’s what I’m trying to find. Or does it mean just an uninitialised array?

---

<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 30, 2020, 6:41pm UTC](https://root-forum.cern.ch/t/getting-histogram-x-axis-value-plotting-a-tgraph/38620/6 "2020-03-30T18:41:44Z")

</div>

[TH1::GetBinCenter](https://root.cern/doc/master/classTH1.html#a0ee88e02b75d885985097dcde0a9eaa1)  
[TH1::GetBinContent](https://root.cern/doc/master/classTH1.html#ac70dcb4082bd510f5aa3e6efeb043f39)
