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