Inheriting from TDirectory

I’d like to create an object which is both a TDirectory and has other properties. So I thought to extend the TDirectory as follow in kk.C:

#include "TDirectory.h" class K: public TDirectory { public: K (const char *name): TDirectory (name, "junk", "K") { for (int i = 0; i < 100; i++) j[i] = i; } K (): TDirectory () {} int get (int i) { return j[i]; } ClassDef(K,1) private: int j[100]; }; ClassImp(K);
This is just a junk sample but I have a problem when using it. On root I first type:

.L kk.C+ TFile *f=new TFile ("j.root", "RECREATE") K* k1 = new K("k1") k1.Write () f.Save () f.Close ()
than when I try to read back the file from an other root session:

.L kk.C+ TFile *f=new TFile ("j.root") f.Get("k1") Error in <TClass>: class: TUUID, attempting to access a wrong version: 4268, object skipped at offset 82 SysError in <TFile>: cannot seek to position 58546795162370048 in file j.root, retpos=-1 (Invalid argument) Error in <TFile>: error reading all requested bytes from file j.root, got 170 of 2359356 (class TObject*)0x99bf780
Do you see where I’m wrong? Using (“k1”) gives the same error.
It is even strange that k1.Write() returns 0 instead of 400.
(root version 5.12)

Thanks in advance

Inheriting from TDirectory requires a bit more work … In particular you will need to overload WriteDirHeader (and possibly a few more place).

Out of curiosity, what are you trying to accomplish?


I only found in the documentation of TDirectory that I have to call the 3 argument ctor with the name of the derived class.
In any case I will try to overload the WriteDirHeader.

The application is the following: our root files contains the event data three and other run related data. These data are organized in a directory structure aside the three: lets say:

bxtree (the event three)

Since I have other data which are not related to any module, IMHO it was a good design to give these properties (like the detector parameters) directly to the bx_barn object, which in turn is a smarter objec than a simple directory. In this case one could do: bxbarn->get_property_a() and at the same time (“bx_barn/module1”) && histogram1->Draw ().
I could put a simpler object inside the bxbarn directory, but from the docs inheritiing from TDirectory did’nt look so hard. Maybe I will change my mind and move to a simpler design.