Dear rooters, I have a directory tree in memory with several nested subdirectories and histograms (it’s created with the example attached below).
#include <iostream>
#include <map>
#include <vector>
#include <TFile>
#include <TH1F>
#include <TH2F>
#include <TH3F>
using namespace std ;
typedef vector<TH1> aHistogramList ;
typedef map<string> aDirectoryMap ;
typedef map<string> aHistogramMap ;
/*
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()
{
// TFile * file = new TFile("data.root", "RECREATE") ;
TDirectory * file = gROOT ;
aDirectoryMap dirMap ;
aHistogramMap hisMap ;
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++)
{
if( dirMap.find(his->first) == dirMap.end() )
{
cout << "No "
<<his>first
<< " directory found to place histograms supposed to belong there"
<<endl>first]->cd() ;
for(aHistogramList::iterator hisPtr=his->second.begin(); hisPtr!=his->second.end(); hisPtr++)
{
// (*hisPtr)->Write() ;
(*hisPtr)->SetDirectory(dirMap[his->first]) ;
}
}
gROOT->ls() ;
cout << "Swapping "
<<dirMap>GetName()
<< " from "
<<dirMap>GetMother()->GetName()
<< " to "
<<dirMap>GetName()
<<endl>SetMother(dirMap["dirSix"]) ;
gROOT->ls() ;
cout << "Now, ancestor of "
<<dirMap>GetName()
<< " is "
<<dirMap>GetMother()->GetName()
<<endl>ls() ;
// file->Close() ;
}
I would like to move one directory and all it’s content (subdirectories included) from one directory in the hierarchy to another. If I run the example below, where I (think) reassign the dirThree directory from belonging to the dirOne ancestor to the dirSix, the program apparently does what requested but the gROOT->ls() dump does not show that the hierarchy has been actually reshuffled.
[code] g++ -g -o createFile createFile.cpp root-config --cflags --libs
&& ./createFile
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: 0x9ae5570
OBJ: TH1F h4 h4 in dirFive : 0 at: 0x9ad8110
OBJ: TH2F h5 h5 in dirFive : 0 at: 0x9ad8630
OBJ: TH1F h2 h2 in dirTwo : 0 at: 0x9ad76d0
OBJ: TH1F h3 h3 in dirTwo : 0 at: 0x9ad7bf0
TDirectory* dirThree Second directory in dirOne
OBJ: TH1F h6 h6 in dirThree : 0 at: 0x9ae2c68
OBJ: TH1F h7 h7 in dirThree : 0 at: 0x9ae3188
OBJ: TH3F h8 h8 in dirThree : 0 at: 0x9ae36a8
OBJ: TH1F h1 h1 in dirOne : 0 at: 0x9ad7198
Swapping dirThree from dirOne to dirSix
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: 0x9ae5570
OBJ: TH1F h4 h4 in dirFive : 0 at: 0x9ad8110
OBJ: TH2F h5 h5 in dirFive : 0 at: 0x9ad8630
OBJ: TH1F h2 h2 in dirTwo : 0 at: 0x9ad76d0
OBJ: TH1F h3 h3 in dirTwo : 0 at: 0x9ad7bf0
TDirectory* dirThree Second directory in dirOne
OBJ: TH1F h6 h6 in dirThree : 0 at: 0x9ae2c68
OBJ: TH1F h7 h7 in dirThree : 0 at: 0x9ae3188
OBJ: TH3F h8 h8 in dirThree : 0 at: 0x9ae36a8
OBJ: TH1F h1 h1 in dirOne : 0 at: 0x9ad7198
Now, ancestor of dirThree is dirSix
[/code]
Even if I save the directory tree to file and reopen it, I do not see the reshuffling effect.
Am I maybe missing something? (This is root version 4.04/02b)
Thanks to everyone answering, as usual…