# Looping over multiple root files, merging them and plotting a particular tree

**URL:** <https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502>\
**Category:** ROOT\
**Tags:** tree, root\
**Created:** [September 8, 2022, 1:48pm UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502 "2022-09-08T13:48:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [September 8, 2022, 1:48pm UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502/1 "2022-09-08T13:48:23Z")

</div>

Hi all, I’m currently trying to plot a particular tree from several different root files placed inside a folder. To be more specific, my tree looks something likes this **Tree[i][j]** where i takes 10 values and for each i we have 3 values of j. So, what I wanna do is to merge all the root files placed inside a folder and then access the tree[0][0] and plot them and then on the same canvas plot tree[0][1], tree[0][2] and so on along the columns and along rows plot [0][0] and below that tree[1][0], below that tree[2][0] and so on. so eventually it will be 10 columns \* 3 rows canvas. What I’m doing now is to TChain all the root files and then plotting them individually in the same canvas one by one which is very time-consuming. Let me know if you have any solution to this and in case you need more info to assist me. Thanks again.

---

<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:** [September 9, 2022, 7:48am UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502/2 "2022-09-09T07:48:25Z")

</div>

Hi @thegame ,

if I understand correctly you have a dataset (split across multiple files) that consists of 10 columns, and column values are arrays of size 3.  
Assuming that what’s time consuming is looping over the data 30 times instead of just 1, you can use [RDataFrame](https://root.cern/doc/master/classROOT_1_1RDataFrame.html) to speed that up:

```cpp
ROOT::RDataFrame df(treename, "path/to/data/*.root")
// now we book 30 histograms
auto h00 = df.Define("value00", "column0[0]").Histo1D("value00");
auto h01 = df.Define("value01", "column0[1]").Histo1D("value01");
// ...
// and the event loop is run lazily, the first time you access one of the results, e.g. here:
h00->Draw();

```

Of course we can use a loop to book all histograms rather than manually writing one line per histogram:

```cpp
ROOT::RDataFrame df(treename, "path/to/data/*.root")
// now we book 30 histograms
std::vector<ROOT::RDF::RResultPtr<TH1D>> histos;
for (int col_idx = 0; col_idx < 10; ++col_idx) {
  for (int arr_idx = 0; arr_idx < 3; ++arr_idx) {
    const std::string col = std::to_string(col_idx);
    const std::string val = std::to_string(arr_idx);
    df.Define("tmp", "column" + col + '[' + val + ']')
      .Histo1D("tmp");
    histos.push_back();
  }
}

```

(I plotted the data as histograms but RDF can also produce TGraphs or any other custom data aggregation).

I hope this helps,  
Enrico

---

<div class="post-metadata">

**Author:** ![thegame](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/t/e495f1/32.png) [@thegame](https://root-forum.cern.ch/u/thegame)\
**Post date:** [September 15, 2022, 11:51pm UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502/3 "2022-09-15T23:51:33Z")

</div>

Hi enrico, thanks for your suggestion. getting too many errors while running this. Attached is the screenshot of the VStudio Code while running this. Thanks again.

---

<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:** [September 16, 2022, 6:53am UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502/4 "2022-09-16T06:53:07Z")

</div>

Hi @thegame ,

these are normal C++ compilation errors. As it often happens, the first one is causing the others here. You are missing `""` around `ROOT/RDataFrame.hxx`.

Note that I have not tested the code above (I’m missing the right input file), it was just meant to give you an idea of how you could implement what you need – there might be other minor issues.

Cheers,  
Enrico

---

<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:** [September 30, 2022, 6:53am UTC](https://root-forum.cern.ch/t/looping-over-multiple-root-files-merging-them-and-plotting-a-particular-tree/51502/5 "2022-09-30T06:53:12Z")

</div>

Closed on September 30, 2022, 6:53am UTC

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