Getting Histograms from a TDirectoryFile

Hello, I replicated the results in this paper and now I want to compare my results to theirs, I got their data from this website.

The problem arises when I try to load their histograms, every time I try to apply any method to them like ->Draw() I get segmentation violation.

I suppose that the problem is loading the files, they have this structure: a root file with Table of class TDirectoryFile and inside that Table are the histograms.

I have tried GetObject, FindObject, Lists, Directories, I have tried using the constructors in all the ways I can think of, am at the end of my rope here.

This iteration of my code (apparently) fails when I try to apply a method to the histogram:

//g++ -I $PYTHIA8/include -o comparison ./comparison.cpp `root-config --cflags --glibs` -l Pythia8 -L $PYTHIA8/lib

#include <iostream>
using namespace std;

#include "Pythia8/Pythia.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TF1.h"
#include "TFile.h"
#include "TList.h"

using namespace Pythia8;

int main() 
{
//get data
  TFile* proData1 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table1.root");
  TFile* proData2 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root");
  cout<<"Get data"<<endl;
//get histograms
  TH1F* proHist_eta_inel= (TH1F*)proData1->FindObject("Hist1D_y2");
  TH1F* proHist_eta_inel0= (TH1F*)proData2->FindObject("Hist1D_y1");
  TH1F* proHist_ptz_inel0= (TH1F*)proData2->FindObject("Hist1D_y1");
  cout<<"Get histograms"<<endl;
//Segmentation violation starts here
  int proBinNumber_eta_inel0 = proHist_eta_inel0->GetNbinsX();
  cout<<"This won't be printed";
  cout<<proBinNumber_eta_inel0;

    return 0;
}

This iteration of my code fails when getting the histograms:

//g++ -I $PYTHIA8/include -o comparison ./comparison.cpp `root-config --cflags --glibs` -l Pythia8 -L $PYTHIA8/lib

#include <iostream>
using namespace std;

#include "Pythia8/Pythia.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TF1.h"
#include "TFile.h"
#include "TDirectory.h"

using namespace Pythia8;

int main() 
{
  cout<<"1"<<endl;
//obtener datos profesionales
  TFile* proData1 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table1.root");
  cout<<"2"<<endl;
  TFile* proData2 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root");
  cout<<"Get data"<<endl;
//get directories
  TDirectory* dir1 = proData1 ->GetDirectory("Table1");
  TDirectory* dir2 = proData2 ->GetDirectory("Table2");
  cout<<"Get directories"<<endl;
//get histograms
  TH1F* proHist_eta_inel0;
  dir1->GetObject("Hist1D_y1", proHist_eta_inel0);
  TH1F* proHist_eta_inel;
  dir1->GetObject("Hist1D_y2", proHist_eta_inel);
  TH1F* proHist_ptz_inel0;
  dir2->GetObject("Hist1D_y2", proHist_ptz_inel0);
  cout<<"Get Histograms"<<endl;

  int proBinNumber_eta_inel0 = proHist_eta_inel0->GetNbinsX();
  cout<<"Get the number of bins: ";

  cout<<proBinNumber_eta_inel0;

  return 0;
}

Hi,

In first peace of code you should use:

TH1F* proHist_eta_inel= (TH1F*)proData1->Get("Table 1/Hist1D_y2");

In second peace of code you are missing space in “Table 1”

You also can browse content of ROOT objects, using JSROOT:

https://root.cern/js/latest/?file=https://hepdata.net/download/table/ins1395253/Table2/1/root+

Regards,
Sergey

Edit:
I can’t thank you enough, I am smiling so much right now… thank you thank you thank you…

All I had to do for this to work was to put “Table 1” and not “Table1”

Hi linev. I tried your advice:

//g++ -I $PYTHIA8/include -o comparison ./comparison.cpp `root-config --cflags --glibs` -l Pythia8 -L $PYTHIA8/lib

#include <iostream>
using namespace std;

#include "Pythia8/Pythia.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TF1.h"
#include "TFile.h"

using namespace Pythia8;

int main() 
{
  TFile* proData1 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table1.root");

  TH1F* proHist_eta_inel= (TH1F*)proData1->Get("Table1/Hist1D_y1");
  int proBinNumber_eta_inel0 = proHist_eta_inel->GetNbinsX();
  cout<<proBinNumber_eta_inel0;

return 0;
}

But sadly I got this message in the terminal

*** Break *** segmentation violation
 Generating stack trace...
 0x0000000000000001 in <unknown function>

use “Table 1” and not “Table1”

1 Like

Holly shit!, thanks you so much!, I’ve been hours workign on this I never imagined a freaking space was the solution… but all that matters is there is a solution. Thank you and Linev so much

Try:

rootls -h

Edit: I fixed it, sorry, sorry, it was a very silly mistake. It should have been Table 2 when talking about ptz variables

Terminal says “Undeclared Identifier”. But there’s something that worries me more.

I starting adding things to the code now that it works, and there is no specific line that causes a segmentation violation, and it is a line virtually identical to the ones before.

I put a “#######################” before that line

#include <iostream>
using namespace std;

#include "Pythia8/Pythia.h"
#include "TROOT.h"
#include "TCanvas.h"
#include "TH1F.h"
#include "TF1.h"
#include "TFile.h"
#include "TList.h"
#include "TDirectoryFile.h"
#include "TGraphAsymmErrors.h"

using namespace Pythia8;

int main() 
{
//obtener datos profesionales
  TFile* proData1 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table1.root");
  TFile* proData2 = TFile::Open("/Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root");
//crear los histogramas con los datos profesionales
  TH1F* proHist_eta_inel0= (TH1F*)proData1->Get("Table 1/Hist1D_y1");
  TH1F* proHist_eta_inel= (TH1F*)proData1->Get("Table 1/Hist1D_y1");
//No confundir, la variable se llama igual pero la sacamos de proData2
  TH1F* proHist_ptz_inel0= (TH1F*)proData2->Get("Table 1/Hist1D_y1");

//Obtenemos los datos de los histogramas para inicializar nuestros histogramas
  int proBinNumber_eta_inel0 = proHist_eta_inel0->GetNbinsX();
  int proBinNumber_eta_inel = proHist_eta_inel->GetNbinsX();
  //####################### If the next line is commented there will be no segmentation violation
  int proBinNumber_ptz_inel0 = proHist_ptz_inel0->GetNbinsX();

  cout<<proBinNumber_eta_inel0<<endl;

return 0;
}

Try:

rootls /Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root
rootls -l /Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root:"Table 2"
TGraphAsymmErrors  Jan 09 20:40  Graph1D_y1    "doi:10.17182/hepdata.70847.v1/t2"
TH1F               Jan 09 20:40  Hist1D_y1     "doi:10.17182/hepdata.70847.v1/t2"
TH1F               Jan 09 20:40  Hist1D_y1_e1  "doi:10.17182/hepdata.70847.v1/t2"
TH1F               Jan 09 20:40  Hist1D_y1_e2  "doi:10.17182/hepdata.70847.v1/t2"

That’s the same I can see in the TBrowser

So, try:

rootls -l /Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root:"Table 1"
WARNING:root:can't find Table 1 in /Users/Fer/Documents/Pythia/myhists/HEPData-ins1395253-v1-Table2.root

Can’t find what is not there, I understood that was the problem with the code, but I didn’t understand why was it happening

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