Reading branches of TTree from root file

Hello,
I am a beginner with root and c++.
I am trying to read the TTree found in a root file. I built root from the source with -Druntime_cxxmodules=OFF from this blog post (How to run ROOT Macros in VS Code - ROOT). I am writing my scripts in visual studio code. To read the file I typed in my terminal.

root [6] TFile f("/Users/meerabharadwaj/Downloads/Dst--20_03_18--cutflow_mc--cocktail--2016--md.root");
root [7] f.ls()
TFile**		/Users/meerabharadwaj/Downloads/Dst--20_03_18--cutflow_mc--cocktail--2016--md.root	
 TFile*		/Users/meerabharadwaj/Downloads/Dst--20_03_18--cutflow_mc--cocktail--2016--md.root	
  KEY: TDirectoryFile	TupleB0;1	TupleB0
root [8] TupleB0->Print()
root [9] TupleB0->Print()

Nothing was printed in the terminal. How do I fix that?
I tried writing a script to read the Dst_2010_minus_PE/PX/PY/PZand muplus_PE/PX/PY/PZ branches of the TTree which is supposed to be in the TFile.
In vscode

#include <iostream>
#include "TFile.h"
#include "TCanvas.h"
#include "TTree.h"
#include "TH1F.h"
#include "TTreeReader.h"
#include "TTreeReaderValue.h"
#include "TNtuple.h"
int main()
{
void readfromfile();
{
std::cout<< "hello hello hello";
    TFile *f=new TFile("/Users/meerabharadwaj/Downloads/Dst--20_03_18--cutflow_mc--cocktail--2016--md.root");
    //TTree* my_tuple;f.GetObject("TupleB0",my_tuple);
    TTree *t1 = (TTree*)f->Get("TupleB0");
    float muplus, dstar;
    
    t1->SetBranchAddress("Dst_2010_minus_PE/PX/PY/PZ", &dstar);
    t1->SetBranchAddress("muplus_PE/PX/PY/PZ", &muplus);
    
    for (int irow=0;irow<t1->GetEntries();++irow){
        t1->GetEntry(irow);
        std::cout << muplus << " " << dstar <<std::endl;
    }
}
    
}


The script is compiling but while executing, I get errors.

meerabharadwaj@Meeras-MacBook-Pro root-on-vscode % g++ readfromfile.cpp $(root-config --cflags --libs)
meerabharadwaj@Meeras-MacBook-Pro root-on-vscode % ./a.out
hello hello hello
 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/meerabharadwaj/root-on-vscode/a.out] int TTree::SetBranchAddress<float>(char const*, float*, TBranch**) (no debug info)
[/Users/meerabharadwaj/root-on-vscode/a.out] main (no debug info)
[/usr/lib/dyld] start (no debug info)
meerabharadwaj@Meeras-MacBook-Pro root-on-vscode % g++ readfromfile.cpp $(root-config --cflags --libs)
meerabharadwaj@Meeras-MacBook-Pro root-on-vscode % ./a.out
hello hello hello
 *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[/Users/meerabharadwaj/build_root/lib/libRIO.so] TFileCacheRead::ReadBufferExtPrefetch(char*, long long, int, int&) (no debug info)
[/Users/meerabharadwaj/root-on-vscode/a.out] int TTree::SetBranchAddress<float>(char const*, float*, TBranch**) (no debug info)
[/Users/meerabharadwaj/root-on-vscode/a.out] main (no debug info)
[/usr/lib/dyld] start (no debug info)

What is this error, what is causing this error and how do I fix this?
Thank you,
Meera.

Please read tips for efficient and successful posting and posting code

_ROOT Version:6.28/04
Platform: macOS 14 on MacBook Pro M2
Compiler: Apple clang version 15.0.0 (clang-1500.0.40.1)


Hello, @bm5 !
TupleB0 is a TDirectoryFile. I suggest to use

TupleB0->ls()

to view its contents.

Regarding your script (or in other words macro), you can find out more here: ROOT macros and shared libraries - ROOT
Namely, try to run your macro via

root 'readfromfile.cpp'

instead of compiling it with gcc.

1 Like