Error Reading a tree file in root

ROOT VERSION 5.34.36
WINDOWS 10

I have a tree in a rootfile under a subdirectory

rootfile.root/dir/tree

I use

TFile *f = new TFile("ggtree_mc.root");
	f -> Cd("ggNtuplizer");
	cout << gDirectory -> pwd() << endl;
	TTree *t = (TTree*)f->Get("EventTree");

but i receive an error :

Error: illegal pointer to class object t 0x0 3116 D:\debajyoti_project\tot.C(19)
*** Interpreter error recovered ***

Here is the entire code (followed by the output) :


#include <iostream>
#include <TObject.h>
#include <TROOT.h>
#include<TFile.h>
#include <TTree.h>
using namespace std;
int tot ()

{
	cout << "Hellow World" << endl;
	TFile *f = new TFile("ggtree_mc.root");
	f -> Cd("ggNtuplizer");
	cout << gDirectory -> pwd() << endl;
	TTree *t = (TTree*)f->Get("EventTree");
	
	// t.Print();
	Float_t mcPt, mcVtz, elePhi;
	t->SetBranchAddress("mcPt",&mcPt);
    t->SetBranchAddress("mcVtz",&mcVtz);
    t->SetBranchAddress("elePhi",&elePhi);
    
	return 1;
}

Output :

Processing tot.C…
Hellow World
ggtree_mc.root:/ggNtuplizer
0
Error: illegal pointer to class object t 0x0 3116 D:\debajyoti_project\tot.C(19)
*** Interpreter error recovered ***

Hi,
my guess is that f->Get("EventTree") returns a nullptr (i.e. t is invalid).
Can you verify this is the case?
If yes, are you sure you have a TTree named EventTree in that file?
Can you post the output of f->ls()?

Also it seems there are two couts in your code but three printouts in your output, so there’s something not matching.

Cheers,
Enrico

HI Enrico

I updated the code as advised :


#include <iostream>
#include <TObject.h>
#include <TROOT.h>
#include<TFile.h>
#include <TTree.h>
using namespace std;
int tot ()

{
	cout << "Hellow World" << endl;
	TFile f("ggtree_mc.root");
	f.Cd("ggNtuplizer");
	cout << gDirectory -> pwd() << endl;
	cout << f.ls() << endl;
	TTree *t1 = (TTree*)f.Get("EventTree");
	
	return 0;
}

Here’s the output

Processing tot.C...
Hellow World
ggtree_mc.root:/ggNtuplizer
0
TFile**         ggtree_mc.root
 TFile*         ggtree_mc.root
  TDirectoryFile*               ggNtuplizer     ggNtuplizer
   KEY: TTree   EventTree;3     Event data (tag V08_00_26_03)
   KEY: TTree   EventTree;2     Event data (tag V08_00_26_03)
   KEY: TH1F    hEvents;1       total processed and skimmed events
   KEY: TH1F    hPU;1   number of pileup
   KEY: TH1F    hPUTrue;1       number of true pilepu
   KEY: TH1F    hGenWeight;1    Gen weights
   KEY: TH1F    hSumGenWeight;1 Sum of Gen weights
  KEY: TDirectoryFile   ggNtuplizer;1   ggNtuplizer
0
(int)0

Try:

#include "TFile.h"
#include "TDirectory.h"
#include "TTree.h"
int tot(void) {
  TFile *f = TFile:Open("ggtree_mc.root");
  if ((!f) || f->IsZombie()) { delete f; return 1; } // input file problems
  if (!(f->cd("ggNtuplizer"))) { delete f; return 2; } // directory not found
  TTree *t; gDirectory->GetObject("EventTree", t);
  if (!t) { delete f; return 3; } // tree not found
  // ... do whatever you need ...
  // t->ResetBranchAddresses(); // "disconnect" from local variables
  delete f; // automatically deletes "t", too
  return 0;
}

Hi,
ok, please try to remove the f.Cd call and do instead f.Get("ggNtuplizer/EventTree") to retrieve the TTree.

Thank you good sir :smiley:
It worked.
Although I wonder why the .Cd call didn’t work?

I really could not say.
Maybe @pcanal or @Wile_E_Coyote know: given the attached file, why does TFile f("f.root"); f.Cd("NNtuplizer"); f.Get("EventTree"); return a nullptr while f.Get("NNtuplizer/EventTree") returns the tree as expected?

f.root (6.6 KB)

TFile f("f.root"); f.Cd("NNtuplizer"); f.Get("EventTree");

you meant

TFile f("f.root"); f.Cd("NNtuplizer"); gDirectory->Get("EventTree");

I.e. TFile::Get looks only in the top directory for the name.

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