Problem in reading ROOT Trees and Branches

I’m writing a ROOT macro to read a tree, this tree contains many branches but I’m only interested in three particular ones. The problem is that when I try to read them I get wrong values, in fact, e.g., if I type “h76->Show(1)” it prints out the value of the branches for the entry 1. When I try to read it from my macro and print, it gives me a wrong value. This is my code:

#define N_PID 10000

void pTdist()
{
    Double_t pT;
        int i;
    Int_t PID[N_PID];
    Int_t ntrack; //Number of particles in one event
    TFile *file = new TFile("PROVA0.root","read");
    file->ls();
    TTree * Tout= (TTree*)file->Get("h76");
    Tout->Print();
    Int_t nentries=Tout->GetEntries();
    Tout->SetBranchStatus("*", false);
    //Tout->SetBranchStatus("ntrack", true); //In the ROOT tutorial it says that this operation is necessary but if I do it gives me error
    //Tout->SetBranchStatus("PID", true);
    //Tout->SetBranchStatus("pT", true);

    cout << " nentries = " << nentries << endl; //Here prints the correct number of entries
    Tout->SetBranchAddress("ntrack",&ntrack);
    Tout->SetBranchAddress("pT",&pT);
    Tout->SetBranchAddress("PID",&PID); //Fino a qui funziona tutto in teoria
    for( i=0;i< 10;i++)
    {
        //cout << i << endl;
        Tout->GetEntry(i); //
                printf("%d %d\n",ntrack,i);
    }
}

What am I missing here? Thanks to everyone who will answer.
ROOT Version: 6.28/06
Platform: MacOs

//In the ROOT tutorial it says that this operation is necessary but if I do it gives me error

They are necessary. The line:

Tout->SetBranchStatus("*", false);

tells the TTree to NOT read anything and hence the wrong results.

but if I do it gives me error

What error do you see?

1 Like

Hi, thanks for your answer, if I enable the Branches with the commands that are now commented it says:
" *** Break *** segmentation violation
[/usr/lib/system/libsystem_platform.dylib] _sigtramp (no debug info)
[] (no debug info)
[] (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::IncrementalExecutor::executeWrapper(llvm::StringRef, cling::Value*) const (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::Interpreter::EvaluateInternal(std::__1::basic_string<char, std::__1::char_traits, std::__1::allocator > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**, unsigned long) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] cling::MetaProcessor::process(llvm::StringRef, cling::Interpreter::CompilationResult&, cling::Value*, bool) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] HandleInterpreterException(cling::MetaProcessor*, char const*, cling::Interpreter::CompilationResult&, cling::Value*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] TCling::ProcessLine(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCling.so] TCling::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libCore.so] TApplication::ExecuteFile(char const*, int*, bool) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libRint.so] TRint::ProcessLineNr(char const*, char const*, int*) (no debug info)
[/usr/local/Cellar/root/6.28.06/lib/root/libRint.so] TRint::Run(bool) (no debug info)
[/usr/local/Cellar/root/6.28.06/bin/root.exe] main (no debug info)
[/usr/lib/dyld] start (no debug info)

What do you think is wrong with this? I can’t interpret well this kind of error. Thanks again for helping me

Hi,

I realise this is an initial attempt to do something more sophisticated with this data. Would it be perhaps an option to go for RDataFrame, which would allow to abstract from the powerful and highly configurable interfaces of TTree?
All the code above can be expressed by these two lines:

    ROOT::RDataFrame df("h76", "PROVA0.root");
    df.ForEach([](int ntrack, ULong64_t i){printf("%d %d\n",ntrack,i);}, {"ntrack", "rdfentry_"});

The RDataFrame crash course can be found here and the code examples here.

Best,
Danilo

Hi Danilo, this my first time using ROOT and I still don’t know many options this framework offers, thank you very much for your suggestion! I’ll try to use the RDataFrame.
Best,
Niccolò

Hi Niccolò,

One has to start from something - nothing wrong with that.
Let us know how it goes and don’t hesitate to come back with questions.

Cheers,
Danilo