# Accessing data in root file

**URL:** <https://root-forum.cern.ch/t/accessing-data-in-root-file/60434>\
**Category:** Newbie\
**Created:** [August 6, 2024, 1:46pm UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434 "2024-08-06T13:46:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![liz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/liz/32/36077_2.png) [@liz](https://root-forum.cern.ch/u/liz)\
**Post date:** [August 6, 2024, 1:46pm UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/1 "2024-08-06T13:46:02Z")

</div>

Hello,  
first of all, sorry if I’m not using the proper terminology.  
I have a TTree file containing some branches, as shown in the attached image:

 ![Screenshot 2024-08-06 133005_1](https://root-forum.cern.ch/uploads/default/original/3X/0/e/0e8a5cd277c283bde686efc5dc6f91b4917aefb8.png)

For the first branch I managed to draw the histogram but I don’t know how to proceed for the other ones and obtain the correspondant graphs…

Thank you

---

<div class="post-metadata">

**Author:** ![dastudillo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/dastudillo/32/17280_2.png) [@dastudillo](https://root-forum.cern.ch/u/dastudillo)\
**Post date:** [August 6, 2024, 2:19pm UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/2 "2024-08-06T14:19:28Z")

</div>

If you are reading as TTree (not RDataFrame), see the `read` function in this tutorial:  
[https://root.cern/doc/master/hvector\_8C.html](https://root.cern/doc/master/hvector_8C.html)  
In short: create vector pointer(s), use SetBranchAddress, loop over the tree to get each event, and within each event, loop over the elements of the vector(s).

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 6, 2024, 2:22pm UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/3 "2024-08-06T14:22:20Z")

</div>

Hello @liz, thank you for posting your question.

In general, I would recommend going through [the manual](https://root.cern.ch/doc/master/classTTree.html) to get a better understanding of the TTree interface.

For your specific question: the recommended way to process and/or visualize the data inside a TTree is through [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html).  
For example, a simple way to visualize the data inside your TTree’s branches is:

```auto
ROOT::RDataFrame d("B5", "your_file.root");
auto myHisto = d.Histo1D("Etot"); // replace with whichever branch you want
myHisto->Draw();

```

RDataFrame also offers a number of other operations that you may want to perform on your data: refer to the documentation linked above if needed.

---

<div class="post-metadata">

**Author:** ![liz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/liz/32/36077_2.png) [@liz](https://root-forum.cern.ch/u/liz)\
**Post date:** [August 7, 2024, 8:25am UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/4 "2024-08-07T08:25:41Z")

</div>

Thank you very much!! I’ll defintely look a bit into RDataFrame…  
May I ask another question? I tried to follow the same method you told me about in order to plot a 2D histogram; this is what I did:

```auto
root [3] ROOT::RDataFrame d("B5", "B5ntuple.root");
root [4] auto myHisto2D = d.Histo2D({"histo", "XV vs YV;XV;YV", 100, 0, 100000000, 100, 0, 100000000}, "XV", "YV");
root [5] myHisto2D->Draw("COLZ")

```

XV and YV are the names of two branches containing vectors.  
However, after the “Draw” instruction, root just crashes (there are no errors apparently, it just makes me go back to my directory and I have to start over)  
I don’t know if I’m doing something wrong or if maybe it’s just a lot of data to process…

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 7, 2024, 9:22am UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/5 "2024-08-07T09:22:22Z")

</div>

Hi liz,  
the snippet you posted looks ok and it should work in principle. Could you please share the complete reproducer so we can have a look at it?

---

<div class="post-metadata">

**Author:** ![liz](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/liz/32/36077_2.png) [@liz](https://root-forum.cern.ch/u/liz)\
**Post date:** [August 7, 2024, 9:48am UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/6 "2024-08-07T09:48:12Z")

</div>

This is what I do:  
First of all,

```auto
root -l readNtuple.C

```

and then I use a loop in this way:

 ![image](https://root-forum.cern.ch/uploads/default/original/3X/1/e/1e75afd4a88f37afcf6bcce81c6c718d993c5f27.png)  
It goes through 100 events and it does take a little while to do it… then I use the instructions I wrote in my previous reply:  
 ![Screenshot 2024-08-07 114446](https://root-forum.cern.ch/uploads/default/original/3X/d/9/d906b913c0abb2d7e4e9d333ba35b9e7a9acec46.png)

For example, drawing a 1D histogram is quite time consuming but at the end I get the graph.  
 ![Screenshot 2024-08-07 114556](https://root-forum.cern.ch/uploads/default/original/3X/5/3/531fa67a7d07210491eca35bad2d89b362c7a76a.png)

But for the 2D one, it just crashes 🥲  
I’m sorry, I don’t think I can attach all the actual files…

---

<div class="post-metadata">

**Author:** ![silverweed](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/silverweed/32/36670_2.png) [@silverweed](https://root-forum.cern.ch/u/silverweed)\
**Post date:** [August 7, 2024, 11:12am UTC](https://root-forum.cern.ch/t/accessing-data-in-root-file/60434/7 "2024-08-07T11:12:34Z")

</div>

Thank you for providing more details.

I imagine you’re filling your TTree branches from `n.Loop()` and then reading them back with `RDataFrame` right after it. If that’s the case, there might be some resource management error in the `Loop()` function which causes errors when reading back the data (e.g. maybe the file doesn’t get closed properly before invoking `RDataFrame::Draw`).

Unfortunately it’s hard to tell what the problem is without knowing what `Loop()` does exactly. If you want a more thorough answer, you can send me at least the code for the `readNtuple` class and a small sample of the data you’re processing (you can do it via private message if you prefer).
