# TMap object into TFile

**URL:** <https://root-forum.cern.ch/t/tmap-object-into-tfile/55769>\
**Category:** ROOT\
**Created:** [July 20, 2023, 6:21am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769 "2023-07-20T06:21:04Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 20, 2023, 6:21am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/1 "2023-07-20T06:21:04Z")

</div>

HEllo,

I think TMap is interesting to manipulate. I have a std::map\<TString, TString\> that I would like to convert into a TMap, save it into a root file (possibly not at the root of the file), and retrieve it later on.

Could you advice on this ?

Here is what I have done so far, but the TMap object is split into separate keys when saved into a root file. ( I guess this is somehow expected? [Read TMap from a file](https://root-forum.cern.ch/t/read-tmap-from-a-file/41286), @pcanal, @eguiraud would you maybe have an idea ?)

```auto
TFile *f = TFile::Open("test.root", "recreate");
TMap *m = new TMap();

m->Add(new TObjString("test:0"), TObjString("My Test 0"));
m->Add(new TObjString("test:1"), TObjString("My Test 1"));

m->Write();
f->Close();

```

Opening the saved file is returning this list, but I would like to store it as a single object.

```auto
Collection name='THashList', class='THashList', size=4
 TKey Name = test:0, Title = Collectable string class, Cycle = 1
 TKey Name = My Test 0, Title = Collectable string class, Cycle = 1
 TKey Name = test:1, Title = Collectable string class, Cycle = 1
 TKey Name = My Test 1, Title = Collectable string class, Cycle = 1

```

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 20, 2023, 8:36am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/2 "2023-07-20T08:36:07Z")

</div>

Dear @meyerma ,

If you already have a `std::map`, you could write that directly to your file:

```cpp
#include <iostream>
#include <map>
#include <string>

#include <TFile.h>

int main()
{
   std::map<std::string, std::string> mymap{{"a", "A"}, {"b", "B"}, {"c", "C"}};

   {
      TFile f{"myfile.root", "recreate"};
      f.WriteObject(&mymap, "mymap");
   }

   {
      TFile f{"myfile.root"};
      auto *mymap = f.Get<std::map<std::string, std::string>>("mymap");
      for (const auto &kv : *mymap) {
         std::cout << kv.first << ": " << kv.second << std::endl;
      }
   }
}

```

Does this serve your purpose better maybe?

Cheers,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 20, 2023, 9:23am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/3 "2023-07-20T09:23:21Z")

</div>

Definitely 🙂 is that possible to do the same with any object or just map ?

---

<div class="post-metadata">

**Author:** ![vpadulan](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/vpadulan/32/25315_2.png) [@vpadulan](https://root-forum.cern.ch/u/vpadulan)\
**Post date:** [July 20, 2023, 9:48am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/4 "2023-07-20T09:48:44Z")

</div>

Sure, ROOT I/O supports writing arbitrary C++ objects to TFile!  
For many standard C++ classes, you can use my example from above and just change `std::map` to the corresponding class.

You can also define your own custom C++ class and write such objects to file, you need a bit more work to generate information for ROOT to know how to write your class to the file. This is known as creating a class dictionary, see [I/O of custom classes - ROOT](https://root.cern/manual/io_custom_classes/#restrictions-on-types-root-io-can-handle) and [GitHub - eguiraud/root\_dictionaries\_tutorial: A tutorial on creating ROOT dictionaries to perform I/O of custom C++ classes](https://github.com/eguiraud/root_dictionaries_tutorial) for more info for this specific case

Best,  
Vincenzo

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 20, 2023, 10:10am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/5 "2023-07-20T10:10:42Z")

</div>

I was used to add my custom class, but all of them inherits from TObject/TNamed. I was assuming this was kind of mandatory.

I will check how vector of TObject are handled, because I would not like to have some randomly located object appearing in the root of my TFile

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 20, 2023, 5:00pm UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/6 "2023-07-20T17:00:02Z")

</div>

Also for the record:

> but the TMap object is split into separate keys

is solved by

```auto
m->Write(nullptr, TObject::kSingleKey, nullptr);

```

---

<div class="post-metadata">

**Author:** ![meyerma](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/meyerma/32/29724_2.png) [@meyerma](https://root-forum.cern.ch/u/meyerma)\
**Post date:** [July 21, 2023, 3:41am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/7 "2023-07-21T03:41:03Z")

</div>

> [@pcanal](#):
>
> `m->Write(nullptr, TObject::kSingleKey`

This is exactly what I needed ! I ended up using kSingleKey (altought I cannot browse with TBrowser because my map is too big 😅

Just a side comment, this works better when using compiled ROOT programs  
`m->Write(nullptr, TObject::kSingleKey, 0);`

Thank you so much guys, this was very fruitful discussion…  
M.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 4, 2023, 3:46am UTC](https://root-forum.cern.ch/t/tmap-object-into-tfile/55769/8 "2023-08-04T03:46:10Z")

</div>

Closed on August 4, 2023, 3:46am UTC

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