Reading Tree failed using TTreeReader

Dear ROOT Experts,

I have a Tree with multiples branches. and I’m doing some track-cluster matching. I am using a TTreeReader to read my tree and looping over my cluster size which has a size equals to 497328

, but when I loop over it and print its size I got cluster size equals to 1. which causes a crash in my code Does it make since ? any help on that would be much appreciated. Thannks

PS: the root file was made using athena 21.9 in ATLAS

Try: your_tree->Scan("cluster_Eta");

What does

mean exactly? Do you loop over events and then, by looping over every cluster in event, you get clusters’ pseudorapidities? If so, how having exactly one cluster in every event crashes your code?

here it is

yes sure I loop over all entries and then over cluster size

TTreeReader R(ch1); p_R=&R;
TTreeReaderArray<Double_t>C_eta(R,"cluster_eta"); p_C_eta=&C_eta;

while (R.Next()==1){
for (int i=0; i<p_C_eta->GetSize(); {
}

this is the error I’m ending up with

Current Entry :0----
Current Entry :1----
Current Entry :2----
Current Entry :3----
i :0#####CLUSTER SIZE#####1
create data set info Default
Current Entry :4----
i :0#####CLUSTER SIZE#####1

*** Break *** segmentation violation

===========================================================
There was a crash.
This is the entire stack trace of all threads:

gdb.printing.register_pretty_printer(gdb.current_objfile(),
gdb.printing.register_pretty_printer(gdb.current_objfile(),

#0 0x00007f96a2b8b41c in waitpid () from /lib64/libc.so.6
#1 0x00007f96a2b08f12 in do_system () from /lib64/libc.so.6
#2 0x00007f96a366809c in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.6.20
#3 0x00007f96a366aa6c in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib64/root/libCore.so.6.20
#4
#5 0x00007f96a3d59fec in ?? ()
#6 0x0000000000000001 in ?? ()
#7 0x00007f96a33da7a8 in ?? () from /usr/lib64/root/libCore.so.6.20
#8 0x0000000000000002 in ?? ()
#9 0x0000000000000040 in ?? ()
#10 0x00000000269e693c in ?? ()
#11 0x00007ffd76a2c378 in ?? ()
#12 0x00007ffd76a2c360 in ?? ()
#13 0x00007ffd76a2c348 in ?? ()
#14 0x00007ffd76a2c330 in ?? ()
#15 0x00007ffd76a2c250 in ?? ()
#16 0x0000000004793d48 in ?? ()
#17 0x3fef7771561b5043 in ?? ()
#18 0x407a6cc50000003c in ?? ()
#19 0x00000000046e0870 in ?? ()
#20 0x00007ffd76a2cb10 in ?? ()
#21 0x0000000004793d48 in ?? ()
#22 0x3fef7771561b5043 in ?? ()
#23 0x0000000004793d48 in ?? ()
#24 0x3fef7771561b5043 in ?? ()
#25 0x407a6cc500000040 in ?? ()
#26 0x00000000046e0870 in ?? ()
#27 0x00007ffd76a2cb10 in ?? ()
#28 0x0000000004793d48 in ?? ()
#29 0x3fef7771561b5043 in ?? ()
#30 0x0000000004793d48 in ?? ()
#31 0x3fef7771561b5043 in ?? ()
#32 0x407a6cc576a2bdf0 in ?? ()
#33 0x00000000046e0870 in ?? ()
#34 0x00007ffd76a2cb10 in ?? ()
#35 0x0000000004793d48 in ?? ()
#36 0x3fef7771561b5043 in ?? ()
#37 0x407a6cc500000000 in ?? ()
#38 0x00000000046e0870 in ?? ()
#39 0x00007ffd76a2cb10 in ?? ()
#40 0x00007f96a34219f8 in ?? () from /usr/lib64/root/libCore.so.6.20
#41 0x00007f96939754e0 in ?? () from /usr/lib64/root/libTreePlayer.so.6.20.04
#42 0x407a6cc500000000 in ?? ()
#43 0x00000000046e0870 in ?? ()
#44 0x00007ffd76a2cb10 in ?? ()
#45 0x00007ffd76a2bea8 in ?? ()
#46 0x00007ffd76a2be80 in ?? ()
#47 0x00007f9693972390 in ?? () from /usr/lib64/root/libTreePlayer.so.6.20.04
#48 0x0000000000000031 in ?? ()
#49 0x407a6cc500000040 in ?? ()
#50 0x00000000046e0870 in ?? ()
#51 0x00007ffd76a2cb10 in ?? ()
#52 0x0000000000000000 in ?? ()

So, as you can see, for the first 14 entries (“Row” in the output) of your tree, the sizes of the “cluster_Eta” vector (“Instance” in the output) are probably 0 for entries (0 to 2), 1 for entries (3 to 8), 2 for events (9, 10, 14), 3 for entries (11, 12), 4 for entry (13).

Note: The “cluster_Eta” is a “vector<float>” but you try to use “TTreeReaderArray<Double_t>”. I don’t think this will work (you probably need to use “Float_t” instead of “Double_t”). Also, I think “cluster_eta” is NOT “cluster_Eta”.

I just checked with an old root files made with another version of athena and it works and has the same “Scan” as this new one

Yes thanks I already fixed that in my working code. this suppose to be the old version in GIT

I’d suggest to simplify the code a bit, replacing:

with just

TTreeReader R(ch1);
TTreeReaderArray<Float_t>C_eta(R,"cluster_eta");

while (R.Next()){
    for (int i=0; i<C_eta.GetSize(); i++) {
        // more code here
}

I don’t think that would change something. I pass the address of the Reader to the pointer.

In that case I guess I’d like to get hold of your file with TTree.

1 Like

Are you member in ATLAS. or do you have an lxplus account ?

Yes, both.

I really appreciate you help

~yelghaza/public/Forward_Electron_Identification/

you will find 3 root files. the code works with Zee.root (old version) but crashes with others.

Okay, so I wrote a small program to read in the cluster_eta values, and it works for all three files. I guess your main problem was that there are quite a few differences between the files. That is, in your Zee.root the TTree was named treeFwdElectronPerf, while in single_par_fwd_200evnt.root and in single_par10Gev_2000evnt.root it was named TRKTree. Also, the array you are interested in is called cluster_eta in the first file, but it’s cluster_Eta in the other two files. Finally, this array is of type double in the first file and float in the second and third files. After I figured all this out, I was able to read all pseudorapidity values of all clusters in all events in all three of your files.

Here is my program:

void yassine()
{
//      TFile *input = TFile::Open("~yelghaza/public/Forward_Electron_Identification/Zee.root", "READ"); // first file
//      TFile *input = TFile::Open("~yelghaza/public/Forward_Electron_Identification/single_par_fwd_200evnt.root", "READ"); // second file
        TFile *input = TFile::Open("~yelghaza/public/Forward_Electron_Identification/single_par10Gev_2000evnt.root", "READ"); // third file
        if (!input)
        {
                printf("Cannot open input file!\n");
                return;
        }

//      TTreeReader myReader("treeFwdElectronPerf", input); // for 1st file
        TTreeReader myReader("TRKTree", input); // for 2nd and 3rd files

//      TTreeReaderArray<double> cluster_pseudorapidity(myReader, "cluster_eta"); // for 1st file
        TTreeReaderArray<float> cluster_pseudorapidity(myReader, "cluster_Eta"); // for 2nd and 3rd files

        while (myReader.Next())
        {
                if (myReader.GetEntryStatus() != TTreeReader::kEntryValid)
                        printf("Something is wrong with this entry!!\n");
                else
                {
//                      if (myReader.GetCurrentEntry() % 10000 == 0)  // for 1st file
//                      if (myReader.GetCurrentEntry() % 100 == 0)  // for 2nd file
                        if (myReader.GetCurrentEntry() % 1000 == 0) // for 3rd file
                        {
                                printf("Processing event #%05lli with %02lu cluster(s):\n", myReader.GetCurrentEntry(), cluster_pseudorapidity.GetSize());
                                for (int clusterIt = 0; clusterIt < cluster_pseudorapidity.GetSize(); clusterIt++)
                                        printf("\tcluster eta = %f\n", cluster_pseudorapidity[clusterIt]);
                        }
                }
        }

        return;
}

2 Likes

thanks a lot
why did you put this line ```
if (myReader.GetCurrentEntry() % 1000 == 0)
``

This is to print limited info: I’m printing properties only of those events with their number multiple of 1000, that is #0, #1000, #2000 etc. You should delete this if you want to see pseudorapidity values of every cluster in exactly every event.

yes I see. Thanks a lot. I just realized that the segmentation violation was caused by this line

p_R->SetEntry(event);

I was also able to rease all eta values of all clusters with the code I sent as well by simply doing this.

void reader(){

TTreeReader *p_R;
TTreeReaderArray<Float_t> *p_C_eta;

TChain* ch1 = new TChain(“TRKTree”);
//ch1->Add(“single_par_fwd_200evnt.root”);
ch1->Add("~yelghaza/public/Forward_Electron_Identification/single_par10Gev_2000evnt.root");
TTreeReader R(ch1); p_R=&R;

TTreeReaderArray<Float_t>C_eta(R,“cluster_Eta”); p_C_eta=&C_eta;
while (R.Next()==1){ cout<<" Processing event: “<<p_R->GetCurrentEntry()<<”----"<<endl;
// if (p_R->GetCurrentEntry() % 1000 == 0){
for (int i=0; i<p_C_eta->GetSize(); i++){
cout<<"\tCluser eta: "<<p_C_eta->At(i)<<endl;

}

}
}

But I wasn’t able to figure out the source of error I sent you ? could you please have a look !

I don’t really understand the question. It works for you now, but you also want to know why you had the segfault with your previous code? Since I couldn’t reproduce the error, I’d need to look at your old code. Also, you wrote yourself

– isn’t it the source of your error?