# Need Intensity Values from 2D Histogram

**URL:** <https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503>\
**Category:** Newbie\
**Created:** [September 9, 2022, 2:32am UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503 "2022-09-09T02:32:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![YanPan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yanpan/32/31348_2.png) [@YanPan](https://root-forum.cern.ch/u/YanPan)\
**Post date:** [September 9, 2022, 2:32am UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/1 "2022-09-09T02:32:55Z")

</div>

Hello,

I ran a SPECT simulation in Gate and received \*.root output Singles data containing events, globalPosX,Y,Z, time & energy, etc. From the output, I was able to plot this 2D histogram with the following code. However, I can’t seem to extract the intensity/counts data of the plot for post-analysis.

> Blockquote  
> `TCanvas *c1 = new TCanvas("c1","globalPos_ZX",870,0,400,400); TH2F *globalPos_ZX = new TH2F("globalPos_ZX","",100,-150,150,100,-150,150); Singles->Project("globalPos_ZX","globalPosZ:globalPosX"); c1->cd(); globalPos_ZX->Draw("contZ"); gStyle->SetOptFit(1111); Blockquote`

I have attached the root file. I would appreciate some advice or code to get the counts or intensity values. Ideally, I need an array with globalPosZ, globalPosX, Counts.

> **[Geo1Canvas.png](https://drive.google.com/file/d/1fAlM0V_MUMCcNYcbl7d89fTdL-ysEv_J/view?usp=sharing)**
>
> Google Drive file.

Btw I’m running root v6.14 (built for linuxx8664gcc) inside vGate v9.0 on Oracle VM VirtualBox.

[SPECT\_Geometry1\_outout\_75000Bq\_600s\_All43\_Aug27.root](https://root-forum.cern.ch/uploads/short-url/dlY87PLnXPHyEJqfkFyoKTb2eC4.root) (745.3 KB)

* * *

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [September 9, 2022, 7:32am UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/2 "2022-09-09T07:32:10Z")

</div>

Hi @YanPan ,

you can access the contents of the 2D histogram with `histogram.GetBinContent(binx, biny)`. You can also access the full array of values, it’s returned by `histogram.GetBuffer()` and it will be a flattened version of the 2D array of bin contents.

Cheers,  
Enrico

EDIT: see also [ROOT: TH2 Class Reference](https://root.cern/doc/master/classTH2.html) (including the methods publicly inherited from TH1).

---

<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:** [September 9, 2022, 7:35am UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/3 "2022-09-09T07:35:45Z")

</div>

```cpp
for (int i = 1; i <= globalPos_ZX->GetNbinsX(); i++) {
  for (int j = 1; j <= globalPos_ZX->GetNbinsY(); j++) {
    std::cout << "x = " << globalPos_ZX->GetXaxis()->GetBinCenter(i) << " , "
              << "y = " << globalPos_ZX->GetYaxis()->GetBinCenter(j) << " , "
              << "content = " << globalPos_ZX->GetBinContent(i, j)
              << " +- " << globalPos_ZX->GetBinError(i, j) << "\n";
  }
}

```

---

<div class="post-metadata">

**Author:** ![YanPan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yanpan/32/31348_2.png) [@YanPan](https://root-forum.cern.ch/u/YanPan)\
**Post date:** [September 9, 2022, 6:50pm UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/4 "2022-09-09T18:50:55Z")

</div>

Hi @Wile_E_Coyote and @eguiraud thank you for your responses.

I’m getting this error message. It seems it’s not picking up on the definitions

 ![errorMessage](https://root-forum.cern.ch/uploads/default/original/3X/8/2/822fa70535ce7e01077afbaed3e200f05021ac7e.png)

The second error cannot identify ‘histogram’, ‘dinx’ or ‘biny’

Am I going about this correctly? Would be better if I created a \*.cc file to run? If so, could you point me to the execution command?

---

<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:** [September 9, 2022, 7:09pm UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/5 "2022-09-09T19:09:51Z")

</div>

I modified my example source code so that you can use it from the “root” command line, too.

In general, for more complex tasks, you’d better create and use ROOT C++ macro files: [ROOT Primer → ROOT Macros](https://root.cern/primer/#root-macros)

---

<div class="post-metadata">

**Author:** ![YanPan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yanpan/32/31348_2.png) [@YanPan](https://root-forum.cern.ch/u/YanPan)\
**Post date:** [September 9, 2022, 7:38pm UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/6 "2022-09-09T19:38:00Z")

</div>

It works! Thank you a million for your help & the tutorial links.

---

<div class="post-metadata">

**Author:** ![YanPan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/yanpan/32/31348_2.png) [@YanPan](https://root-forum.cern.ch/u/YanPan)\
**Post date:** [April 20, 2023, 5:11pm UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/7 "2023-04-20T17:11:38Z")

</div>

Hello @Wile_E_Coyote, I have been using this code for months until yesterday, it stopped working. After I paste this code I get " \*\*\*\* Break \*\*\*\* segmentation violation " then a crash. I haven’t changed anything in the code and even retraced my steps, I don’t know what went wrong. I have attached the full crash info.  
[Root Crash.txt](https://root-forum.cern.ch/uploads/short-url/mmXgAcmLJvyGmyk9SEuUYytMwG4.txt) (9.6 KB)

---

<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:** [April 20, 2023, 5:27pm UTC](https://root-forum.cern.ch/t/need-intensity-values-from-2d-histogram/51503/8 "2023-04-20T17:27:59Z")

</div>

Probably the “`globalPos_ZX`” was not defined.
