Re: How to write out TMap objects?
Rene Brun (Rene.Brun@cern.ch)
Wed, 01 Jul 1998 14:17:58 +0200
meb@gorby.dataventures.com wrote:
>
> This might seem to be a really silly question. I am a new root user,
> having very little experience with OO programming. After reading all
> the documentation I can get my hands on, I find I need help.
>
> I have no difficulty creating a TMap object (map), and filling it with
> a small set of (key, value) pairs. For testing, the "key" and "value"
> objects are TObjString's. After opening a new file, I invoke the
> method map->Write(). A listing of the directory shows I saved a
> TObjString and not a TMap. What went wrong?
>
> root [0] TMap map(10)
> root [1] map.Add(new TObjString("Mike"), new TObjString("Beddo"))
> root [2] map
> (class TMap)542047224
> root [3] TFile db("test.root", "recreate")
> root [4] map->Write("a_map")
> root [5] .ls()
> TFile** test.root
> TFile* test.root
> KEY: TObjString a_map;1 Collectable string class
> root [6] .q
>
> - Mike Beddo
When writing objects derived from TCollection (case of TMap), the
default behaviour is to write each object in the collection
as a separate key on the file. (See TCollection::Write).
You can force all objects in the collection to be written as one single
key by specifying a second argument = 1).
In your case, simply do:
map->Write("a_map",1);
Rene Brun