Hello!
I realise this post is quite old but it helped me understand why my directory structure was not working now 7 years later and I wanted to add my findings.
What caused the problems with mkdir for me is shown with the following code:
//create test file
TFile *outFile = new TFile("dirTest.root","RECREATE");
//create test dir
TDirectory* d = outFile->mkdir("test");
//check if returned pointer points to test dir
std::cout << "test: " << d << ", " << d->GetName() << std::endl;
//move to test dir
d->cd();
std::cout << "Tried to move to test, moved to: " << gDirectory->GetDirectory(0)->GetPath() << std::endl;
//make test2 subdir
TDirectory* d2 = outFile->mkdir("test/test2");
//check if returned pointer points to test2 subdir
std::cout << "test2: " << d2 << ", " << d2->GetName() << std::endl;
//try to move to test2 subdir
d2->cd();
std::cout << "Tried to move to test2, moved to: " << gDirectory->GetDirectory(0)->GetPath() << std::endl;
//rebase (because paths in cd() are relative) and move to test2 subdir via gDirectory and explicit path
outFile->cd();
gDirectory->cd("test/test2");
//check location again
std::cout << "test2: " << gDirectory->GetDirectory(0) << ", " << gDirectory->GetDirectory(0)->GetName() << std::endl;
std::cout << "Tried to move to test2 via gDirectory and path, moved to: " << gDirectory->GetDirectory(0)->GetPath() << std::endl;
The output is then:
test: 0x4fa7e30, test
Tried to move to test, moved to: dirTest.root:/test
test2: 0x4fa7e30, test
Tried to move to test2, moved to: dirTest.root:/test
test2: 0x4c5b900, test2
Tried to move to test2 via gDirectory and path, moved to: dirTest.root:/test/test2
In the second mkdir call the sub-directory is created, but a pointer to the mother directory is returned. We can see this from the literal pointer being the same and from the output. Would it not make more sense if “mkdir” returned a pointer to the actually created directory? This would allways be the last directory in a hierarchy “a/b/c/…” anyway and no undefined behavior should result, right? And if that sub-directory cannot be created, return 0 and a warning?
Cheers,
Florian