Need Intensity Values from 2D Histogram


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.

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.

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 (745.3 KB)

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.


EDIT: see also ROOT: TH2 Class Reference (including the methods publicly inherited from TH1).

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";
1 Like

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

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?

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

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