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?
Also, interestingly, if I comment out the saving to a root file, part, it gives me following:
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
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
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.
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).
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.