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

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.

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 to speed that up:

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:

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

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.

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

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