Another question about directory hierarchies

Dear rooters, in the following snippet of code I’m creating a directory hierarchy in memory and then I pretend to remove an entire branch.
All members of the branch disappear, as expected, except the branch itself, which is counterintuitive.

Here is the code (part of a complex GUI I’m writing with Qt that allows users to move branches around using drag-and-drop).

[code]/*____________________________________________________________________________________
Author: D. Menasce

This program creates a file with a hierarchical structure of directories and
histograms.
To read back the content of this file, use readFile.cpp

g++ -g -o createHierarchyInMemory createHierarchyInMemory.cpp root-config --cflags --libs && ./createHierarchyInMemory


*/
#include
#include
#include

#include
#include
#include
#include
#include

using namespace std ;

typedef vector aHistogramList ;
typedef map aDirectoryMap ;
typedef map aHistogramMap ;

aDirectoryMap dirMap ;
aHistogramMap hisMap ;

/*
0 1 2 3 4–> directoryLevel

data.root
|
±-> dirOne
|
±-> h1
|
±-> dirTwo
| |
| ±-> h2
| |
| ±-> h3
| |
| ±-> dirFour
| | |
| | ±-> dirSix
| |
| ±-> dirFive
| |
| ±-> h4
| |
| ±-> h5
| |
| ±-> dirSeven
| |
| ±-> h9
|
±-> dirThree
|
±-> h6
|
±-> h7
|
±-> h8
*/

int main()
{

TDirectory * file = gROOT ;

dirMap[“dirOne”] = file->mkdir(“dirOne”, “First directory at top level”) ;
dirMap[“dirTwo”] = dirMap[“dirOne”]->mkdir(“dirTwo”, “First directory in dirOne” ) ;
dirMap[“dirThree”] = dirMap[“dirOne”]->mkdir(“dirThree”, “Second directory in dirOne” ) ;
dirMap[“dirFour”] = dirMap[“dirTwo”]->mkdir(“dirFour”, “First directory in dirTwo” ) ;
dirMap[“dirFive”] = dirMap[“dirTwo”]->mkdir(“dirFive”, “Second directory in dirTwo” ) ;
dirMap[“dirSix”] = dirMap[“dirFour”]->mkdir(“dirSix”, “First directory in dirFour” ) ;
dirMap[“dirSeven”] = dirMap[“dirFive”]->mkdir(“dirSeven”, “First directory in dirFive” ) ;

hisMap[“dirOne”] .push_back(new TH1F(“h1”,“h1 in dirOne”, 100, 0, 100 )) ;
hisMap[“dirTwo”] .push_back(new TH1F(“h2”,“h2 in dirTwo”, 100, 0, 100 )) ;
hisMap[“dirTwo”] .push_back(new TH1F(“h3”,“h3 in dirTwo”, 100, 0, 100 )) ;
hisMap[“dirFive”] .push_back(new TH1F(“h4”,“h4 in dirFive”, 100, 0, 100 )) ;
hisMap[“dirFive”] .push_back(new TH2F(“h5”,“h5 in dirFive”, 100, 0, 100, 100, 0, 100 )) ;
hisMap[“dirThree”].push_back(new TH1F(“h6”,“h6 in dirThree”, 100, 0, 100 )) ;
hisMap[“dirThree”].push_back(new TH1F(“h7”,“h7 in dirThree”, 100, 0, 100 )) ;
hisMap[“dirThree”].push_back(new TH3F(“h8”,“h8 in dirThree”, 10, 0, 100, 10, 0, 100, 10, 0, 100 )) ;
hisMap[“dirSeven”].push_back(new TH1F(“h9”,“h9 in dirSeven”, 100, 0, 100 )) ;

for(aHistogramMap::iterator his=hisMap.begin(); his!=hisMap.end(); his++)
{
for(aHistogramList::iterator hisPtr=his->second.begin(); hisPtr!=his->second.end(); hisPtr++)
{
(*hisPtr)->SetDirectory(dirMap[his->first]) ;
}
}

gROOT->ls() ;

string dirToDelete = “dirFive” ;

dirMap[dirToDelete]->GetList()->Delete(“T*;") ;
dirMap[dirToDelete]->Delete("T
;*”) ;

gROOT->ls() ;
}
[/code]

Running the code gives

[code]TROOT* root The ROOT of EVERYTHING
TDirectory* dirOne First directory at top level
TDirectory* dirTwo First directory in dirOne
TDirectory* dirFour First directory in dirTwo
TDirectory* dirSix First directory in dirFour
TDirectory* dirFive Second directory in dirTwo
TDirectory* dirSeven First directory in dirFive
OBJ: TH1F h9 h9 in dirSeven : 0 at: 0xa54c570
OBJ: TH1F h4 h4 in dirFive : 0 at: 0xa53f110
OBJ: TH2F h5 h5 in dirFive : 0 at: 0xa53f630
OBJ: TH1F h2 h2 in dirTwo : 0 at: 0xa53e6d0
OBJ: TH1F h3 h3 in dirTwo : 0 at: 0xa53ebf0
TDirectory* dirThree Second directory in dirOne
OBJ: TH1F h6 h6 in dirThree : 0 at: 0xa549c68
OBJ: TH1F h7 h7 in dirThree : 0 at: 0xa54a188
OBJ: TH3F h8 h8 in dirThree : 0 at: 0xa54a6a8
OBJ: TH1F h1 h1 in dirOne : 0 at: 0xa53e198

TROOT* root The ROOT of EVERYTHING
TDirectory* dirOne First directory at top level
TDirectory* dirTwo First directory in dirOne
TDirectory* dirFour First directory in dirTwo
TDirectory* dirSix First directory in dirFour
TDirectory* dirFive Second directory in dirTwo
OBJ: TH1F h2 h2 in dirTwo : 0 at: 0xa53e6d0
OBJ: TH1F h3 h3 in dirTwo : 0 at: 0xa53ebf0
TDirectory* dirThree Second directory in dirOne
OBJ: TH1F h6 h6 in dirThree : 0 at: 0xa549c68
OBJ: TH1F h7 h7 in dirThree : 0 at: 0xa54a188
OBJ: TH3F h8 h8 in dirThree : 0 at: 0xa54a6a8
OBJ: TH1F h1 h1 in dirOne : 0 at: 0xa53e198
[/code]

As expected, all descendents of dirFive have disappeared, but dirFive itself is still there. Once more I’m obviously missing something (which is not unusual… :wink: )

Thanks as usual

Dario,

As I already told you, this code cannot compile.

Rene