Loading tree from a file

Hello everyone; I’m trying to write a script to load a tree from a root file; the file has a few folder and each folder has a few tree; when I run the following code:

TFile *fp = new TFile("../Output/OutAna_500Events_100MeV.root");
 fp -> cd("flashmatchAnaArOnly")
TTree *T = (TTree*) fp -> Get("FlashMatchTree")

I get:

(TTree *) nullptr

but the tree FlashMatchTree does exist inside the folder flashmatchAnaArOnly and I’m not sure on how to solve this; any help is greatly appreciated


Please read tips for efficient and successful posting and posting code

ROOT Version: 6.22/00
Platform: Not Provided
Compiler: Not Provided


Try: fp->ls();

It gives this output

TFile**		../Output/OutAna_500Events_100MeV.root	
 TFile*		../Output/OutAna_500Events_100MeV.root	
  TDirectoryFile*		flashmatchAnaArOnly	flashmatchAnaArOnly (AnaCustom) folder
   KEY: TTree	FlashMatchTree;1	FlashMatchTree
   KEY: TTree	LargestFlashTree;1	LargestFlashTree
   KEY: TTree	SelectedFlashTree;1	SelectedFlashTree
   KEY: TTree	CountWaveforms;1	CountWaveforms
   KEY: TEfficiency	recoEfficiencyVsE;1	
   KEY: TEfficiency	recoEfficiencyVsX;1	
   KEY: TEfficiency	recoEfficiencyVsXandE;1	
   KEY: TEfficiency	largestEfficiencyVsE;1	
   KEY: TEfficiency	largestEfficiencyVsX;1	
   KEY: TEfficiency	largestEfficiencyVsXandE;1	
   KEY: TEfficiency	selectedEfficiencyVsE;1	
   KEY: TEfficiency	selectedEfficiencyVsX;1	
   KEY: TEfficiency	selectedEfficiencyVsXandE;1	
  KEY: TDirectoryFile	HitModuleLabel;1	HitModuleLabel (AnaCustom) folder
  KEY: TDirectoryFile	flashmatchAna10ppm;1	flashmatchAna10ppm (AnaCustom) folder
  KEY: TDirectoryFile	flashmatchAnaArOnly;1	flashmatchAnaArOnly (AnaCustom) folder
  KEY: TDirectoryFile	flashmatchAnaXeOnly;1	flashmatchAnaXeOnly (AnaCustom) folder

Try:
TTree *T; gDirectory->GetObject("FlashMatchTree", T);

This works, thank you!
But… why was what I was doing wrong?