Search code examples
c++root-framework

Empty TH2F histogram in Root


I'm trying to plot a 2-dimensional from a root file but for some reason instead of a nice plot I get a blank Canvas with 918 entries, mean x=0, mean y=47 and both of the standard deviations equal to 0. I have no idea why did it happen and apparently the problem is with the last line h1->Fill(xk,yk);, because the cout just before it works. Could you help me figure it out?

Here is my full code:

{
#include <iostream>
#include <vector>

using namespace std;

TFile f1("data1.root");
TFile f2("data2.root");

TTreeReader reader("T1", &f1);
TTreeReaderValue<Double_t> X(reader, "X"); 
TTreeReaderValue<Double_t> Y(reader, "Y");

TH2F* h1=new TH2F("h1"," ",10000,0,900,0,70);

vector<vector<Double_t>>v1;

while (reader.Next()) {
    //cout<<*X<<" "<<*Y<<endl;
    vector<Double_t>i;
    i.push_back(*X);
    i.push_back(*Y);
    v1.push_back(i);
}

for (int k=0;k<918;k++)
    {
        Double_t xk=v1[k][0];
        Double_t yk=v1[k][1];
        cout<<xk<<" "<<yk<<endl;
        h1->Fill(xk,yk);
    }

h1->Draw();
}

Solution

  • I suspect you're calling a wrong constructor in

    TH2F* h1=new TH2F("h1"," ",10000,0,900,0,70);
    

    The constructors of TH2F from here take 6 numeric arguments (3 for the x axis and 3 for the y axis) or pointers to arrays and you're providing 5.

    Inspecting the histogram you create:

    root [2] h->GetXaxis()->GetNbins()
    (int) 10000
    root [3] h->GetYaxis()->GetNbins()
    (int) 900
    
    root [6] h->GetXaxis()->GetBinLowEdge(1)
    (double) 0.0000000
    
    
    root [10] h->GetYaxis()->GetBinLowEdge(1)
    (double) 0.0000000
    root [11] h->GetYaxis()->GetBinUpEdge(900)
    (double) 70.000000
    
    root [13] h->GetXaxis()->GetBinUpEdge(10000)
    (double) 1.0000000
    
    

    it looks like the overload resolution picked the instantiation of the x-axis different from what you intended. I suspect (from the numbers you provided) you missed the number of bins for the y-axis:

    TH2F* h1=new TH2F("h1"," ",10000,0,900,10000,0,70);