# Read TMap from a file

**URL:** <https://root-forum.cern.ch/t/read-tmap-from-a-file/41286>\
**Category:** Newbie\
**Created:** [September 3, 2020, 4:11pm UTC](https://root-forum.cern.ch/t/read-tmap-from-a-file/41286 "2020-09-03T16:11:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibordule](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/i/aca169/32.png) [@ibordule](https://root-forum.cern.ch/u/ibordule)\
**Post date:** [September 3, 2020, 4:11pm UTC](https://root-forum.cern.ch/t/read-tmap-from-a-file/41286/1 "2020-09-03T16:11:14Z")

</div>

Hello!

I have a problem with reading the TMap object from a root file.  
Here is the simple code, how I create this TMap and save it to the file:

```auto
void TMap_structure() {
    TFile f("filespeed.root","recreate");
    TMap * tof = new TMap;
    TObjString *key = new TObjString("key!");
    TObjString *val = new TObjString("value!");

    tof->Add(key, val);
    tof->Write("tof");  
    f.Close();
}

```

But when I am trying to read this TMap from a file, I can’t do it. Here is the code, that I’m using to read TMap from root file:

```auto
void Read_root_file_TMap() {
    TMap * tof = new TMap;
    TFile *f=new TFile("filespeed.root");
    f->GetObject("tof", tof); // Here it just can't find "tof" and stores nothing in tof
    f->Close();
}

```

As I understood from the TBrowser, the Write() method doesn’t save the TMap structure to the root file, but it rather save all objects (keys and values) from the TMap as independent objects.  
So, is this a way to save a TMap in a file and then read it from file and use all its metods (e.g. GetValue, FindObject, etc)?

Best regards, Iurii

\_ROOT Version:\_6.14/06  
\_Platform:\_macosx64  
_Compiler:_ Not Provided

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [September 4, 2020, 6:52am UTC](https://root-forum.cern.ch/t/read-tmap-from-a-file/41286/2 "2020-09-04T06:52:09Z")

</div>

I guess @pcanal can help you.

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [October 6, 2020, 10:43am UTC](https://root-forum.cern.ch/t/read-tmap-from-a-file/41286/3 "2020-10-06T10:43:57Z")

</div>

Hi @ibordule,  
and welcome to the ROOT forum.  
I’m sorry you haven’t heard back from us, I hope you have resolved the issue in the meanwhile.

Writing individual elements of the TMap separately in the TFile is the default behavior, but [the TMap::Write](https://root.cern/doc/master/classTMap.html#aa2895d7b3b5bf0e81f82fd82c59c346a) docs suggest a way to write a single key instead.

Hope this helps!  
Enrico
