I have a script in which I create a TTree: TTree* myTree = new TTree("myTree", "C");
then fill it, and then - in the same script - make calls to “Draw”, to draw branches of the tree.
This works (I get filled plots on my canvas), but I get warnings like this:
Error in <TBranch::WriteBasketImpl>: basket's WriteBuffer failed.
Error in <TBranch::TBranch::Fill>: Failed to write out basket.
Error in <TTree::Fill>: Failed filling branch:coincTree.tentry, nbytes=-1, entry=3990
This error is symptomatic of a Tree created as a memory-resident Tree
So, then I tried this:
TFile outRootFile("myTREE.root_NEW", "RECREATE");
TTree* myTree = new TTree("myTree", "C");
And then fill the TTree, but now, when I make calls to “myTree->Draw()”, nothing shows up.
I have tried putting “myTree->GetCurrentFile()->cd();” in some places in my code, but it doesn’t help.
So, without the call to TFile, I get the correct plots, but warnings. And with the call to TFile, I get rid of the warnings, but no plots show up. How do I fix this?
Hi,
Thanks for your reply. The problem is, it’s a very long script with a lot going on. I should first simply it. Maybe part of the problem is that I also open two TFiles (containing TTrees) as input. So there are two input TFiles and one output TFile. So, my guess is that it’s a matter of “directories” (I am not sure that’s the right word). I can’t find how to do something similar to “ls” and “cd” in a ROOT C++ script.
I tried playing with “myTree->GetCurrentFile()->cd();” but that doesn’t help.
By the way, even my histograms “disappear” (when I try drawing them), as soon as I use the TFile line before creating my TTree.
The only solution I see, for the moment, is only drawing histograms and not creating a TTree.
You were right. The problem is something else. Actually, the “TTree” does not disappear, it’s the histograms that disappear. Either when I project a TTree draw onto a histogram (myTree->Draw("aValue >> h1");) or when plotting a histogram directly.
So, I am guessing that since the histograms are not “TFile” located (?), they cannot be plotted?
How can I fix this? I included the simplified code and two plots as example.
The problem is caused by the TFile taking ownership of the histograms. At the end of the macro, the file is destructed and takes the histograms with it. A more detailed explanation of ROOT’s object ownership model can be found here: Object ownership - ROOT
You can insert at the beginning of the macro TH1::AddDirectory(false). This changes the ownership behavior for all histograms so that the caller of new TH1D owns it, and also is in charge of deleting it.