Serialization/Streaming of TDirectory

Hi!

I have a TDirectory (for example the gDirectory), which includes other TDirectories with some objects in them (for example histograms). It looks like this:

TDirectory* rootDir = gDirectory;
TDirectory* oneBelowDir = rootDir->mkdir("histograms");
oneBelowDir->cd();
Directory* testDir = oneBelowDir->mkdir("sub_directory");
testDir->cd();
TH1F th1("test1", "title", 10, 0, 5);
rootDir->cd();

Now I want to stream this directory structure (either the full gDirectory or at least the oneBelowDir). I tried streaming the directory itself or the list of it, but always end up with

Warning in <TMessage::WriteObjectAny>: since TROOT has no public constructor
	which can be called without argument, objects of this class
	can not be read with the current library. You will need to
	add a default constructor before attempting to read it.
Warning in <TStreamerInfo::Build>: atomic_flag: base class __atomic_flag_base has no streamer or dictionary it will not be saved

and a segfault when reading. Here is my streaming code:

write:

TMessage message(kMESS_OBJECT);
message.WriteObject(rootDir->GetList());

read:

TList* rootDir = (TList*)(message.ReadObject(message.GetClass()));

I am happy for any suggestions!

TROOT can not be stored and as you have it setup the storing of the child directory request it,
You can try

TDirectory* rootDir = new TDirectoryFile("rootDir", "");
TDirectory* oneBelowDir = rootDir->mkdir("histograms");
oneBelowDir->cd();
TDirectory* testDir = oneBelowDir->mkdir("sub_directory");
testDir->cd();
TH1F th1("test1", "title", 10, 0, 5);
rootDir->cd();
TMessage message(kMESS_OBJECT);
message.WriteObject(rootDir->GetList());

You can also look into using TFolder.

1 Like

Nice!
I didn’t knew TDirectoryFile can do this. Thanks!

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