How to access directories within root files

Dear root users,

Within a root file I have, there’s a histogram inside a directory, inside a directory. A screenshot of the way the directories are stacked is posted below:

I have been using root files with histograms stored in them ‘directly on top’, so not within a directory in the root file, and with those files I could access the histograms I wanted to draw in the following way:

#include "TCanvas.h"
#include "TH1D.h"
#include "TLegend.h"
#include "TH2D.h"

void smallmacro()
{

TFile *MyFile = TFile::Open("~/Documents/bonz_files/root_files/data10Mrebin.root");
if(!MyFile || MyFile->IsZombie())
  {
    cout << "Failed to load" << endl;
    return;
  }

 TH1D *ptPionH = (TH1D*) MyFile->Get("ptpionH;1");

 TH1D *ptSigmaH = (TH1D*) MyFile->Get("ptSigmacH;1");
 TH1D *ptPionSigmaH = (TH1D*) MyFile->Get("ptPionSigmaH;1");
 TH1D *ptLambdaSigmaH = (TH1D*) MyFile->Get("ptLambdacs;1");

TCanvas *c1 = new TCanvas("c1", "Canvas 1", 1000,1000);
c1->Divide(2,2);
c1->cd(1);
ptPionH->Draw();
c1->cd(2);
ptSigmaH->Draw();
c1->cd(3);
ptPionSigmaH->Draw();
c1->cd(4)
ptLambdaSigmaH->Draw();
}

I have tried to get to the desired directory in the root file in the following manner:

#include "TCanvas.h"
#include "TH1D.h"
#include "TLegend.h"
#include "TH2D.h"

void data()
{

TFile *f = TFile::Open("~/Documents/bonz_files/root_files/AnalysisResults-Dec3.root");
if(!f || f->IsZombie())
  {
    cout << "Failed to load" << endl;
    return;
  }

 printf("Open File %s\n",f->GetName());
 TDirectory *dir = (TDirectory*)f->Get("PWG3_D2H_InvMassLambdac"); //InvMassLambdac is the directory name
 TDirectory *dir2 = (TDirectory*)dir->Get("coutputLcppProd_Cuts"); //Same goes for coutputLcppProd_Cuts

 TH1D *ptPionSigmaData = (TH1D*) dir2->Get("hSoftPionSigmacPt0");
 ptPionSigmaData->Draw();
}

But this did not work. Can someone tell me what is the correct way of accessing directories within root files?!

Best regards,

Loek Meijers

Dear Loek Meijers,

You have to “cd()” into the directories. See tutorials/io/dirs.C .
Hope it helps.

G Ganis

Dear Ganis,

I have followed your instructions, and now my code looks like this:

#include "TCanvas.h"
#include "TH1D.h"
#include "TLegend.h"
#include "TH2D.h"

void data()
{

TFile *f = TFile::Open("~/Documents/bonz_files/root_files/AnalysisResults-Dec3.root");
if(!f || f->IsZombie())
  {
    cout << "Failed to load" << endl;
    return;
  }

 TDirectory *f2 = (TDirectory*)f->Get("PWG3_D2H_InvMassLambdac;1");
 f2->cd();
 TDirectory *f3 = (TDirectory*)f2->Get("coutputLcppProd_Cuts;1");
 f3->cd();
 TH1D *ptPionSigmaData = (TH1D*)f3->Get("hSoftPionSigmacPt0");
 ptPionSigmaData->Draw();
}

But I keep getting the following error:


There is still something I’m not doing right. I have taken a look at the root tutorial and didn’t see what I’m doing wrong. Would you happen to know where my mistake is?

Best regards,

Loek

Try:

TH1 *ptPionSigmaData;
f->GetObject("PWG3_D2H_InvMassLambdac/coutputLcppProd_Cuts/hSoftPionSigmacPt0", ptPionSigmaData);
1 Like

I found the problem. The last directory was not actually a directory but a list. The following worked:

void data()
{

TFile *f = TFile::Open("~/Documents/bonz_files/root_files/AnalysisResults-Dec3.root");
if(!f || f->IsZombie())
  {
    cout << "Failed to load" << endl;
    return;
  }

 Int_t n = 6; //Index of the wanted histogram from the list
 TDirectory* dir = (TDirectory*)f->Get("PWG3_D2H_InvMassLambdac");
 TList *hList = (TList*)dir->Get(Form("coutputLcppProd_Cuts"));
 
 TH1D*ptPionSigmaData= (TH1D*)hList->At(n);
 ptPionSigmaData->Draw();

 f->Close();