Using TTreereader to access a folder inside a root file

Hi all, I’ve written this simple script that should let me plot variables from a TTree (anatree;1) contained inside a folder (analysistree;1) in a root file (hist.root). The Macro doesn’t give any error but the histograms turn out empty. Any idea why?

#include "TFile.h"
#include "TDirectoryFile.h"
#include "TTree.h"
#include "TBrowser.h"
#include "TH2.h"
#include "TRandom.h"
#include "TCanvas.h"
#include "TMath.h"
#include "TROOT.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"



void bigpm(){
   TH2F *hxy = new TH2F("hxy","hxy",1000,-2000,2000,1000,-2000,2000);
   TH2F *hyz = new TH2F("hyz","hyz",1000,-2000,2000,1000,-2000,2000);
   TH2F *hxz = new TH2F("hxz","hxz",1000,-2000,2000,1000,-2000,2000);
   TH1F *hlen = new TH1F("hlen","hlen",1000,-2000,2000);
   
   TFile *fileIn= new TFile("hist.root"); 
   fileIn->cd("analysistree;1");  
   
   TTreeReader theReader("anatree;1",fileIn);
   TTreeReaderValue<double> RVvtxx(theReader, "trkstartx");
   TTreeReaderValue<double> RVvtxy(theReader, "trkstarty");
   TTreeReaderValue<double> RVvtxz(theReader, "trkstartz");
   TTreeReaderValue<double> RVlen(theReader, "trklen");


   while(theReader.Next()){
     
      hxy->Fill(*RVvtxx,*RVvtxy);
      hyz->Fill(*RVvtxy,*RVvtxz); 
      hxz->Fill(*RVvtxx,*RVvtxz); 
      hlen->Fill(*RVlen);   
      
   }

   TCanvas *c1 = new TCanvas("c1","c1", 750, 500);
   hxy->Draw();
   TCanvas *c2 = new TCanvas("c2","c2",500,750);
   hyz->Draw();
   TCanvas *c3 = new TCanvas("c3","c3",500,750);
   hxz->Draw();
   TCanvas *c4 = new TCanvas("c4","c4",500,750);
   hlen->Draw();

}

_
ROOT Version: vo6-12-06


Are you sure your while loop is correct ? can you put some printout inside to check if the Fill methods are actually called ?

Are you sure the “;1” in the names of the folder and the TTree are absolutely needed?

Ok guys, thanks for the help but I actually managed to solve the problem using a TDirectory pointer

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