Openning a ROOT file in R

I have found that there is a library named ROOTR that allows to use R code inside ROOT.

I am looking for something that allows me to do the opposite.

There is any R library that allows me to open a ROOT File, extract a TTree, and associate to dataframe, or data table, in R?

Thanks!

Duplicates this posts: http://stackoverflow.com/questions/43988982/a-library-to-open-root-files-in-r

(Sorry I don’t know of a package, just wanted to link to your other posts in case it is answered there.)

Yes, there exists such a library, https://github.com/rmatev/RootTreeToR (which has some fixes on top of the official repository).

There exists also a package that allows reading a ROOT tree into a dplyr data_frame, https://github.com/rmatev/dplyr.root

You can install them via:

#Interface between ROOT and R
devtools::install_github("rmatev/RootTreeToR")
#Read ROOT ntuple into dplyr data_frame
devtools::install_github("rmatev/dplyr.root")

Here is some example usage using the hsimple.root file from the tutorials folder:

library(dplyr)
library(dplyr.root)

ntuple <- tbl_rootchain("hsimple.root", "ntuple")
events <- ntuple %>%
    mutate(P=px**2 + py**2 + pz**2) %>%
    filter(random > 0.2) %>%
    select(i, P) %>%
    collect()

> ntuple
Source: root file [25,000 x 5]

# S3: tbl_rootchain
            px          py           pz    random     i
         <dbl>       <dbl>        <dbl>     <dbl> <dbl>
1   0.01940945  0.01182548 0.0005165689 0.2826178     0
2   0.32718953  0.03787821 0.1084877476 0.4849736     1
3  -0.29461166 -0.01054588 0.0869072452 0.5400437     2
4  -0.77458960  0.04845844 0.6023373008 0.6586366     3
5  -1.43079531  0.50906366 2.3063209057 0.5196721     4
6   0.28898728 -1.86474288 3.5607798100 0.3923140     5
7   1.68962383  0.39789826 3.0131516457 0.0303352     6
8   1.39157462  0.50942063 2.1959893703 0.9437168     7
9  -0.62253845  0.83793545 1.0896899700 0.6655639     8
10 -0.43862954 -1.09520090 1.3918609619 0.1822846     9
# ... with 24,990 more rows

> events
# A tibble: 20,073 × 2
       i            P
*  <dbl>        <dbl>
1      0 5.168357e-04
2      1 1.202573e-01
3      2 9.446012e-02
4      3 9.651475e-01
5      4 7.625437e+00
6      5 1.623993e+01
7      7 7.018359e+00
8      8 2.277114e+00
9     11 1.619623e+00
10    12 1.128433e+00
# ... with 20,063 more rows

The tree will only be read after the call to collect. You need to use collect(protect=F) if the resulting dataframe is larger than 2GB, if I remember correctly.

As you can see, you can use the normal mutate, filter and select syntax of dplyr.

1 Like

The question on SO was closed, because it was asking for a recommendation of a software library…

I sometimes see people answer those kind of posts in comments. Thought it may be useful for others who find it. Your answer here was the most useful though.

Yes. Thats it! Thank you!

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