# How to access directories within root files

**URL:** <https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866>\
**Category:** Newbie\
**Created:** [December 3, 2018, 4:16pm UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866 "2018-12-03T16:16:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![LoekMeijers](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@LoekMeijers](https://root-forum.cern.ch/u/LoekMeijers)\
**Post date:** [December 3, 2018, 4:16pm UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866/1 "2018-12-03T16:16:29Z")

</div>

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:

 ![Screenshot%20from%202018-12-03%2017-35-32](https://root-forum.cern.ch/uploads/default/original/3X/4/e/4e80f93ac227ae2b9e06a0b579c9dbdcefec477e.png)

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:

```auto
#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:

```auto
#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

---

<div class="post-metadata">

**Author:** ![ganis](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ganis/32/12484_2.png) [@ganis](https://root-forum.cern.ch/u/ganis)\
**Post date:** [December 4, 2018, 2:50pm UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866/2 "2018-12-04T14:50:19Z")

</div>

Dear Loek Meijers,

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

G Ganis

---

<div class="post-metadata">

**Author:** ![LoekMeijers](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@LoekMeijers](https://root-forum.cern.ch/u/LoekMeijers)\
**Post date:** [December 4, 2018, 4:18pm UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866/3 "2018-12-04T16:18:45Z")

</div>

Dear Ganis,

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

```auto
#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:

 ![Screenshot%20from%202018-12-04%2017-17-04](https://root-forum.cern.ch/uploads/default/original/3X/8/b/8be20146707816dd08f18f8c590c00deaf1f488f.png)  
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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [December 4, 2018, 4:22pm UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866/4 "2018-12-04T16:22:30Z")

</div>

Try:

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

```

---

<div class="post-metadata">

**Author:** ![LoekMeijers](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/ad7895/32.png) [@LoekMeijers](https://root-forum.cern.ch/u/LoekMeijers)\
**Post date:** [December 5, 2018, 9:31am UTC](https://root-forum.cern.ch/t/how-to-access-directories-within-root-files/31866/5 "2018-12-05T09:31:11Z")

</div>

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

```auto
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();

```
