you are right. In the current implementation an objects uniqueness is
determined by the values returned by its Hash() and IsEqual() methods.
In the case of TObjString's both the Hash() and IsEqual() are overridden
to only take the string into account. For most in-memory purposes this is
good enough and exactly what the user want when he decides to override the
TObject provided defaults. However, to make the I/O mechanism independent
of the overridden Hash() and IsEqual() methods I've modified the I/O
algorithm to only depend on the objects address for the determination of its
uniqueness. In that case the I/O of your example below works as expected.
Cheers, Fons.
Tioukov Valeri wrote:
>
> Hi Fons,
>
> In your macro the contents of the object (string) is equivalent to
> its Hash() value, so it could not demonstrate the problem.
>
> I attached to this mail the modified macro where I fill for each string
> also fUniqueID to have in the object something different from it's Hash
> parameter.
>
> the output of the macro is:
>
> root [0] .L f.C
> root [1] hhh()
> It was in the memory before storing to file
> ID = 0 TObjString = aap 0
> ID = 1 TObjString = aap 1
> ID = 100 TObjString = aap 0
> ID = 101 TObjString = aap 1
> It is in the memory after restoring
> ID = 0 TObjString = aap 0
> ID = 1 TObjString = aap 1
> ID = 0 TObjString = aap 0
> ID = 1 TObjString = aap 1
>
> As far as I understand there is no any bug here - it's just a mechanizm
> how you store the objects to file: the objects of the same type with the
> same Hash() parameter are always considered the same.
>
> The question: if you have more then 1 hash tables with different objects
> of the same type and Hash() is unique inside each table but could be the
> same for objects from different tables how you store it?
>
> Regards
> Valeri
>
> On Tue, 6 Jul 1999, Fons Rademakers wrote:
>
> > Hi Valeri,
> >
> > I tried the following macro:
> >
> > {
> > gROOT->Reset();
> >
> > char s[32];
> >
> > THashList *l = new THashList;
> >
> > for (int i = 0; i < 10; i++) {
> > THashList *h = new THashList;
> > l->Add(h);
> > for (int j = 0; j < 10; j++) {
> > sprintf(s, "aap %d", j);
> > h->Add(new TObjString(s));
> > }
> > }
> > }
> >
> > is that something like what you try doing?
> > To store such a collection use something like:
> >
> > TFile x("hash.root","recreate")
> > l.Write("l",TObject::kSingleKey)
> >
> > and to read it do:
> >
> > TFile x("hash1.root")
> > THashList *l = (THashList*)x.Get("l")
> > l->Print();
> >
> > or is this not what you want?
> >
> >
> > Cheers, Fons.
> >
> >
> >
> >
> > Tioukov Valeri wrote:
> > >
> > > Hi rooters,
> > >
> > > I have data structure organized as following:
> > >
> > > THashList of objects type A
> > > where A is
> > > THashList of objects type B
> > >
> > > The Hash() of objects B is unic inside A but could be the same for
> > > B's from different A's.
> > >
> > > It worked perfectly before I tried to store and restore this staff.
> > >
> > > I note that after restoring the objects B with the same Hash() becomes
> > > the first stored B.
> > > So Hash() of any objects treated as the global one during the storing.
> > >
> > > But if I'd like to have the structure as described above and I want to
> > > store it. Is it possible to do in the correct way?
> > >
> > > Best regards
> > > Valeri
> >
> > --
> > Org: CERN, European Laboratory for Particle Physics.
> > Mail: 1211 Geneve 23, Switzerland
> > E-Mail: Fons.Rademakers@cern.ch Phone: +41 22 7679248
> > WWW: http://root.cern.ch/~rdm/ Fax: +41 22 7677910
> >
>
> --------------------------------------------------------------------------------
> void hhh()
> {
> w();
> r();
> }
>
> void w()
> {
> gROOT->Reset();
>
> char s[32];
> TObjString *os=0;
>
> THashList *l = new THashList;
>
> for (int i = 0; i < 2; i++) {
> THashList *h = new THashList;
> l->Add(h);
> for (int j = 0; j < 2; j++) {
> sprintf(s, "aap %d", j);
> os = new TObjString(s);
> os->SetUniqueID(100*i+j);
> h->Add(os);
> }
> }
>
> printf("It was in the memory before storing to file\n");
> for (int i = 0; i < 2; i++) {
> h = (THashList*)(l->At(i));
> for (int j = 0; j < 2; j++) {
> os = (TObjString*)( h->At(j) );
> printf("ID = %d ", os->GetUniqueID()); os->Print();
> }
> }
>
> TFile f("f.root","RECREATE");
> l->Write("l",1);
> f.Close();
> }
>
> void r()
> {
> gROOT->Reset();
> TFile f("f.root");
>
> THashList *h;
> TObjString os;
>
> printf("It is in the memory after restoring\n");
> for (int i = 0; i < 2; i++) {
> h = (THashList*)(l->At(i));
> for (int j = 0; j < 2; j++) {
> os = (TObjString*)( h->At(j) );
> printf("ID = %d ", os->GetUniqueID()); os->Print();
> }
> }
> }
-- Org: CERN, European Laboratory for Particle Physics. Mail: 1211 Geneve 23, Switzerland E-Mail: Fons.Rademakers@cern.ch Phone: +41 22 7679248 WWW: http://root.cern.ch/~rdm/ Fax: +41 22 7677910