Saving Histogram Into Sub-Sub Directory

Hi, I’m trying to save histograms into different sub-sub directories. Previously I managed to save histograms into subdirectories as such:

TFile outputFile("OutputFileName","RECREATE");
TDirectory* subD1 = outputFile.mkdir("SubDir1");
TDirectory* subD2 = outputFile.mkdir("SubDir2");

subD1->cd();
someHistInSubD1->Write();
subD2->cd();
someHistInSubD1->Write();

Which would successfully write the histograms into the correct subdirectories. However doing a similar process to write histograms into sub-sub directories is not working for me.

I am trying:

TFile outputFile("OutputFileName","RECREATE");
TDirectory* subD1 = outputFile.mkdir("SubDir1");
TDirectory* subSubD1 = outputFile.mkdir("SubDir1/SubSubDir1");

SubSubD1->cd();
someHistInSubSubD1->Write();

However doing this creates SubSubDir1 as a subdirectory of SubDir1, however it saves someHistInSubSubD1 in SubDir1, not in SubSubDir1

I figured it out. Needed to do

TDirectory* subSubD1 = subD1->mkdir(SubDir1/SubSubDir1");

May I suggest that the bug where TDirectory* subSubD1 = outputFile.mkdir(“SubDir1/SubSubDir1”); correctly produces the subsubdirectory but does not direct to it when doing subSubD1->cd() and instead directs to the upper directory be fixed?

Hello,

As documented at https://root.cern.ch/doc/master/classTDirectory.html#aefc863f608016f7d05ab1a6d4ba5688d
after the following

 TDirectory* subSubD1 = outputFile.mkdir("SubDir1/SubSubDir1");

subsubDir1 points to "SubDir1", so it is the same as subDir1 .
So the behaviour that you experience is expected.
Alternative to your solution would be something like:

 TDirectory* subD1 = outputFile.mkdir("SubDir1/SubSubDir1");
 // Both "SubDir1" and "SubDir1/SubSubDir1" are now created;
 // now get a pointer to the subdirectory
 TDirectory* subSubD1 = subD1->GetDirectory("SubSubDir1");

G Ganis

Dear Ganis,

Thank you for the message, however this is not behaving as expected, and the documentation you have directed to contradicts itself.

"Create a sub-directory and return a pointer to the created directory."
The sub-directory SubDir1/SubSubDir1 is created, however the pointer is not to the created directory. The documentation then goes on to say that in the case SubDir1/SubSubDir1 it will point to SubDir1, however that is in direct contradiction to the claim in the documentation just before that it will return a pointer to the created directory (and also is not what someone would expect). That the pointer returned points to the created directory is what someone would expect, and is what the documentation claims. That the pointer returned points to an already existing directory that is not at all affected by the mkdir in this case is very contrary to what one would reasonably expect, and contrary to the original claim of the documentation.

Dear JackLindon,

I agree that it may not be expected, but it comes form the recursive implementation which leaves access to the top pointer for the return. This was there from the beginning, so it is not possible the change the behaviour.

The first statement of the doc can be be rephrased, though. This will be done asap.
Thanks for reporting.

G Ganis

Thanks very much for the help and clarifying the documentation

Jack

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