Seg Fault while extracting data from TH2D histo


I have this strange problem: I want to extract data from the TH2D histogram (detTimeProf) that I read from the root file. I know that histogram exists and is read from the file but while trying to extract data from the histo the segfault occurs. For example, while line #1 works perfectly line #2 returns segFault.

1 const int nRuns = detTimeProf->GetXaxis()->GetNbins();
2 const int nRuns = detTimeProf->GetNbinsX();

Also all my attempts to extract histo integral (GetIntegral) or actually any other information straight from histo fail.

Here is a short sample of my code:

#include <TH2D.h>
#include <TFile.h>

 int main()
   TH2D *detTimeProf;
   detTimeProf = openDetTimeProfile( detId );

   if( detTimeProf == nullptr )
     return 1;

   const int nRuns = detTimeProf->GetXaxis()->GetNbins();  //this works
   const int nChns = detTimeProf->GetYaxis()->GetNbins();  //this works
   const double content = detTimeProf->GetBinContent( 1, 1 ); //here I have segFault

  return 0;
TH2D* openDetTimeProfile( const int detId )
  TFile rootFile( "detTimeProf.root" );
  return (TH2D*)rootFile->Get( "histoTest" );

ROOT Version: 6.18/04
Thx for help in advance.

TFile *rootFile = TFile::Open( "detTimeProf.root" );

Thank you for reply. I assume that the problem occured because the file in which histo exists was closed and the pointer become dangling pointer. But would you be so kind and explain to me why line #1 worked? It seems strange - I assume that I should not be able to call any function on this object as it is not available for me any more.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.