# Writing linked TH2F and TVector3 into root file and reading them afterwards

**URL:** <https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780>\
**Category:** Newbie\
**Created:** [May 8, 2023, 9:24pm UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780 "2023-05-08T21:24:33Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 8, 2023, 9:24pm UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/1 "2023-05-08T21:24:33Z")

</div>

I am a bit lost in the documentation about read/write into root files.

I have:

1. TObjArray of TH2F … histList
2. TObjArray of TVector3 … vecList

Each TVector3 belongs to one TH2F.

Now suppose I somehow wrote them into root file.

Suppose I read the TObjArray of TH2F.  
Now when reading any TVector3 I want to know to which TH2F it belongs.  
Is it possible to somehow mark each TVector3 (say vecList(3) belongs to hisList(1)) when writing it into root file?

---

<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:** [May 9, 2023, 4:59am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/2 "2023-05-09T04:59:37Z")

</div>

> [@drsh2306](#):
>
> Each TVector3 belongs to one TH2F.

Can you explain a bit what you mean? some code example?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 9, 2023, 8:42am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/3 "2023-05-09T08:42:02Z")

</div>

Maybe a: [TMap](https://root.cern/doc/master/classTMap.html)

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 9, 2023, 8:48am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/4 "2023-05-09T08:48:30Z")

</div>

Tried to simplify it

```auto
// writing into .root file
TFile f("histvec.root", "recreate");
TRandom2 rng = new TRandom2();

int histNumber=100;
TObjArray histList(histNumber);
int vecNumber=250;
TObjArray vecList(vecNumber);

int vecCountInHist[histNumber]; // sum of all array elements = vecNumber

for (int iHist=0; iHist<histNumber; ++iHist)
{
 TString histName = "HistName " + std::to_string(iHist);
 TString histTitle = "HistTitle " + std::to_string(iHist);
 TH2F *hist = newTH2F(histName, histTitle, ...numerical hist parameters...);
 histList.Add(hist);
 
 for (int k=0; k<vecCountInHist[iHist]; ++k)
 {
  TVector3 *vec = new TVector3(rng->Rndm(),rng->Rndm(),rng->Rndm());
  vecList.Add(vec);
 }
}

histList.Write();
vecList.Write();
f.Close();

```

Now when I read this file somewhere else I get histList & vecList,  
but the linking between which “vec” belongs to which “hist” is lost.

Is there a way to label “vec” when adding to vecList, so that later when reading “vec” I know to which “hist” it belongs? Or structure the .root file in a way so that when reading particular “hist” I am automatically reading all “vec” that belong to it?

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 9, 2023, 9:15am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/5 "2023-05-09T09:15:38Z")

</div>

TMap might be actually the simpliest solution to what I need…, without explicitly structuring the .root file in the code by myself.

Edit: unfortunately TMap does not take integers as key… Even declaring RooInt object (which is integer that inherits from TObject) in code leads to segfault (I have v6.26/10).

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 9, 2023, 7:59pm UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/6 "2023-05-09T19:59:01Z")

</div>

Maybe a: [TExMap](https://root.cern/doc/master/classTExMap.html)

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 9, 2023, 8:13pm UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/7 "2023-05-09T20:13:21Z")

</div>

I am missing something. The documentation states:

> Blockquote  
> This class stores a (key,value) pair using an external hash.  
> The (key,value) are Long64\_t’s and therefore can contain object pointers or any longs. The map uses an open addressing hashing method (linear probing).

TExMap has values as Long64\_t, how do I convert TList\* to Long64\_t? It should be possible, but I have no idea how

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 9, 2023, 8:21pm UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/8 "2023-05-09T20:21:00Z")

</div>

`value = (Long64_t)pointer`  
`pointer = (TList*)value`

An example: [`montecarlo/eg/inc/TDatabasePDG.h`](https://github.com/root-project/root/blob/master/montecarlo/eg/inc/TDatabasePDG.h) + [`montecarlo/eg/src/TDatabasePDG.cxx`](https://github.com/root-project/root/blob/master/montecarlo/eg/src/TDatabasePDG.cxx)

BTW. Maybe you should also have a look at the [TRef](https://root.cern/doc/master/classTRef.html) and the [TRefArray](https://root.cern/doc/master/classTRefArray.html) classes (plus “`${ROOTSYS}/tutorials/tree/[jJ]et*`” files).

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 10, 2023, 3:33am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/9 "2023-05-10T03:33:16Z")

</div>

Tried TExMap, when executing this before writing the file

```auto
std::cout << "List size " << i << ": " << ((TList *)exMap->GetValue(i))->GetSize() << std::endl;

```

this gives 1 (and size of the map is 14)

When executing the same after reading the file

```auto
Error in <HandleInterpreterException>: Trying to access a pointer that points to an invalid memory address..
Execution of your code was aborted.
ROOT_prompt_7:1:7: warning: invalid memory pointer passed to a callee:
cout<<((TList *)exMap->GetValue(0))->GetSize()<<endl;
      ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

the size of the map after reading is correct though (14)

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 10, 2023, 7:52am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/10 "2023-05-10T07:52:14Z")

</div>

Is the “`i`” your “`key`”?  
I have the impression that you actually try to access the “`slot`” number.

I guess you maybe want to play with the “built-in” iterator: [TExMapIter](https://root.cern/doc/master/classTExMapIter.html)

I’m not very much familiar with these classes.  
@Axel It seems @rdm is the original author, so maybe someone who maintains it now could provide more help.

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 10, 2023, 9:22am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/11 "2023-05-10T09:22:05Z")

</div>

sorry, I forgot to write - it is a cycle so “i” is any index of TExMap. That line of code works, i.e. accessing the data from TExMap before they are written into root file.

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [May 10, 2023, 9:35am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/12 "2023-05-10T09:35:57Z")

</div>

I’m not sure what you mean by the “`index`”.  
In the most complex case, you may need to deal with four variables: [“`UInt_t slot`”, “`ULong64_t hash`”, “`Long64_t key`”, “`Long64_t value`”](https://root.cern/doc/master/classTExMap.html#ae1a10b5df6bc54119eb9fa296e429c34)

---

<div class="post-metadata">

**Author:** ![drsh2306](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/drsh2306/32/35369_2.png) [@drsh2306](https://root-forum.cern.ch/u/drsh2306)\
**Post date:** [May 10, 2023, 10:19am UTC](https://root-forum.cern.ch/t/writing-linked-th2f-and-tvector3-into-root-file-and-reading-them-afterwards/54780/13 "2023-05-10T10:19:20Z")

</div>

yes, “i” is key.

Actually, when I think of it, I could solve my problem, if I can make sure that

- TObjArray of TH2F, i.e. TObjArray plotList(100)
- TObjArray of TVector3, i.e. TObjArray vecList(250)

(or any other than TObjArray container that can store them) gets written in the order they are stored in TObjArray.  
Then I can simply write into root file a list of integers that will just do the mapping. (I really do not want to make a big programmatic deal out of this one)

Because now when I called

```auto
plotList.Write();
vecList.Write();

```

the TH2F histograms were stored in ascending order from 0 to 99, while the TVector3 vectors were stored in descending order from 249 to 0.

@pcanal Is there a way to guarantee the order of writing of the elements of these containers into file?
