Home | News | Documentation | Download

Scanning a TTree does not display the array elements

So I have the following macro where I am practicing how to use TTree class for an array of variable size. When I run the macro in the root terminal, there is no error or anything and it loads perfectly. However, when I try t->Scan() on the root terminal, it gives me the following output, and it has clearly no information regarding the array elements that I fed in the macro. Can someone help me understand what is going on and how to solve the issue?

ttree1

Also, interestingly, if I comment out the saving to a root file, part, it gives me following:

ttree2

which is fine, but the problem is it is exiting root completely.
introttree3.cpp (1.3 KB)

Any help would be appreciated.
ROOT Version: 5.18/34
Platform: Ubuntu 18.04
Compiler: Not Provided


Before exiting the macro, you need:
t->ResetBranchAddresses(); // disconnect from local variables

BTW. instead of f->Close();, use delete f;

1 Like

Thank you. That solved the issue. Just curious why f->Close() is not preferred over delete f?

Actually, what you do is not correct. You should have:

TFile *f = TFile::Open(..., "recreate"); // FIRST open a file for writing
TTree *t = new TTree(...); // THEN create a tree (file resident)
// ... fill the tree in a loop ...
// f->cd(); // just a precaution
t->Write(); // make sure everything is saved
delete f; // automatically deletes t, too

Doing so gives me a segmentation violation

Then you have a bug somewhere (just to make sure … you do understand that delete f; AUTOMATICALLY deletes “t”, too, so you must not use it any more).

1 Like

Yes, that is what I realized. Inititally, I had t->ResetBranchAddresses(); at the very end of the code, and when I added delete f; I forgot to move it before that. Hence the error. Now it seems to be fixed.

You do not need this line, if the tree gets deleted in this macro (you would only need it, if you did not close the file).

1 Like

Ok. that makes sense. Thank you.

Also, getting rid of that line and using just delete f; seems to be not working cause the root says that “Symbol t is not defined in current scope.”

I changed delete f; to f->Delete(); and it seems to work fine. So a bit confused about your suggestion.

Then you have a bug somewhere.

If you want to use ‘t’ afterward, you *****must not delete nor close the file ****** (or you need to reopen it and re-fetch the TTree object from it) Note delete f; and f->Delete() are two completely distinct operations. The former ‘just’ remove the TFile object (and its associated object like TTrees and Histograms from memory). The later instruct the TFile object to “remove” all its content (make the file essentially empty).

1 Like

A bit confused about what you mean by “If you want to use ‘t’ afterward”. What does “afterward” mean? Like re-run the macro multiple times?

The original post contains:

.x introttree3.cpp 
t->Scan();

For the 2nd line to work, the first line must not delete the file nor the tree (i.e. for t to be valid after the execution for introttree3.cpp )

1 Like

Ok, thank you. I think I got it now. So I changed my file such that I now have the line

t->Scan();

before

delete f;

Earlier, I did not have that and what I was doing is loading the macro and when I called t->Scan(); it said that symbol t is not defined in the scope. It all makes sense now. Thank you.

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