Retrieve ArrayValue from a TTree

Hi to everyone,

as always, I’m a beginner in ROOT. And it is still a bit complicated to understand how it works for me, since it’s also a quite huge framework. However, as the title of my topic expresses, I have a root file (not made by myself but got it from other simulations in my collaboration), where there are TTrees with several branches. I need to retrieve the values of a particular branch, that I understood reading some posts in this forum, definition of classes etc (and making mistakes), that is an array. The branch is the following one:

*Br 42 :Event.fMCEvent.fRecParticles.fAzimuth :
| Double_t fAzimuth[Event.fMCEvent.fRecParticles_] *
*Entries : 1418 : Total Size= 6526 bytes File Size = 174 *
*Baskets : 1 : Basket Size= 16000 bytes Compression= 33.26 *

and I want to retrieve the fAzimuth values (or, however considering the complete extended name of the branch).
I tried to follow the first code in the TTreeReader ClassReference page on the ROOT website and it gives me this error:

Error in TTreeReaderValueBase::GetBranchDataType(): Must use TTreeReaderArray to access a member of an object that is stored in a collection.
Error in TTreeReaderValueBase::CreateProxy(): The branch Event.fMCEvent.fParticles.fAzimuth contains data of type {UNDETERMINED TYPE}, which does not have a dictionary.

(this suggested me that it is an Array :sweat_smile:) ). However, I tried to understand the TTreeReaderArray part (the second code on the same page) but I didn’t get much since, I know, I’m sure I’m missing basic things. I have no code to show since I tried to reproduce the things that I learned from these pages and trying to adapt to my case but I didn’t succeed.
Sorry if I didn’t put the reference of the cited pages but sieems that, as a beginner, I cannot do it.

I also look at the RDataFrame tutorial on the ROOT page and tried to get what I want from the RDataFrame, but I didn’t secceed also in this way.

I thanks in advance anyone that wants to help me.

_ROOT Version: 6.24/00
_Platform: Linux Mint 20.3, Cinnamon 64-bit
_Compiler: linuxx8664gcc


Hi,

try something like this (you should replace the name of the input file and the name of TTree with your own ones):

TFile* f = new TFile("myfile.root", "READ");
TTreeReader myReader("Events", f);
TTreeReaderArray<Double_t> azimuth(myReader, "fAzimuth");

Hi,

thanks for the reply. Yes, in the end I could make the following macro for it:

#include <TFile.h>
#include <TH1.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>
#include <vector>
#include <iostream>

void Extracting_Macro(){
  auto f=TFile::Open("file.root");

  TTreeReader myReader("AntData",f);
  TTreeReaderArray<double> myAzi(myReader,"fAzimuth");

  while(myReader.Next()){
    auto azimuth = *myAzi;
    for(auto&& Azimuth : azimuth){
      std::cout<<"Azimuth1: "<<azimuth <<std::endl;
      std::cout<<"Azimuth2: "<<Azimuth <<std::endl;                                                                                                        
    }
  }
}

But it gives me back the error:

16:20: error: indirection requires pointer operand ('TTreeReaderArray<double>' invalid)
    auto azimuth = *myAzi;

(Double_t is read as double)
I kept a similar exaple from the TTreeReader and TTreeReaderArray official page on ROOT website and I tried to modify for my case. I still don’t have clear why for the array they use the pointer “auto azimuth= *myAzi” and that type of structure of the for() cycle (I’ve never saw a structure for a for() cycle like that).

Hi,

try this instead:

#include <TFile.h>
#include <TH1.h>
#include <TTreeReader.h>
#include <TTreeReaderValue.h>
#include <TTreeReaderArray.h>
#include <vector>
#include <iostream>

void Extracting_Macro(){
  auto f=TFile::Open("file.root");

  TTreeReader myReader("AntData",f);
  TTreeReaderArray<Double_t> myAzi(myReader,"fAzimuth");

  while(myReader.Next())
    for (unsigned short myIterator = 0; myIterator < myAzi.GetSize(); myIterator++)
        printf("azimuth[%hu] = %f\n", myIterator, myAzi[myIterator]);
}

Indeed I understood that something was wrong in that for() cycle but I couldn’t manage how to do it. This works! Thanks much for the help!