# How to access the information contained in branches in RDataFrame

**URL:** <https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712>\
**Category:** ROOT\
**Tags:** tree, python, rdataframe\
**Created:** [July 29, 2020, 10:38am UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712 "2020-07-29T10:38:36Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [July 29, 2020, 10:38am UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/1 "2020-07-29T10:38:36Z")

</div>

Dear experts,  
From tutorials, I know how to interact with the data contained in a tree in RDataFrame:

```auto
d = ROOT.RDataFrame(treeName, fileName)

```

However, I do not kwow how to proceed in case of a more complex tree structure. For instance, I do not know how to access the individual leaves of each branch of the tree depicted in the attached figure.  
For example, how can I access the hcalTPieta leaf?  
Thank you for your time.  
Cheers,  
Nathanael  
 ![tree](https://root-forum.cern.ch/uploads/default/original/3X/5/b/5b3b3f7fd01058db303dd4733d884f046b4bb89f.png)

---

<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:** [July 29, 2020, 12:46pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/2 "2020-07-29T12:46:33Z")

</div>

Hi @Nathanael,  
what does `d.GetColumnNames()` return for that TTree?

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [July 29, 2020, 2:24pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/3 "2020-07-29T14:24:45Z")

</div>

Hi @eguiraud,  
Thank you for your reply.  
I tried this piece of code

```auto
import ROOT
fileName = "ZToMuMu_mc_off_MET_PU_mit_392SF.root"
treeName = "l1CaloTowerTree"
d = ROOT.RDataFrame(treeName, fileName)
d.GetColumnNames()

```

and I get segmentation violation.  
Please see attached the test root file [ZToMuMu\_mc\_off\_MET\_PU\_mit\_392SF.root](https://root-forum.cern.ch/uploads/short-url/ir7JsnrOrdIIHzpRN1VPoJYBE93.root) (2.3 MB)

---

<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:** [July 29, 2020, 3:12pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/4 "2020-07-29T15:12:54Z")

</div>

Uhm a segmentation violation seems like an overreaction on the part of the script 😃

What ROOT version are you on? Here’s what I get with v6.22:

```auto
$ python read_cols.py (cern-root) 
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisEventDataFormat is available                                                                    
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisCaloTPDataFormat is available                                                                   
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisL1CaloTowerDataFormat is available                                                              
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisL1UpgradeTfMuonDataFormat is available                                                          
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisBMTFInputsDataFormat is available                                                               
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisL1UpgradeDataFormat is available                                                                
TClass::Init:0: RuntimeWarning: no dictionary for class GlobalAlgBlk is available                                                                                             
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisL1HODataFormat is available                                                                     
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisL1CaloClusterDataFormat is available                                                            
TClass::Init:0: RuntimeWarning: no dictionary for class L1Analysis::L1AnalysisGeneratorDataFormat is available                                                                
Error in <TChain::LoadTree>: Cannot find tree with name l1CaloTowerTree in file ZToMuMu_mc_off_MET_PU_mit_392SF.root                                                          
Traceback (most recent call last):                                                                                                                                            
  File "read_cols.py", line 5, in <module>                                                                                                                                          
    d.GetColumnNames()                                                                                                                                                        
cppyy.gbl.std.runtime_error: vector<string> ROOT::RDF::RInterface<ROOT::Detail::RDF::RLoopManager,void>::GetColumnNames() =>                                                  
    runtime_error: GetBranchNames: error in opening the tree l1CaloTowerTree                                                                                                  

```

So I inspected the file and indeed, like your screenshot above shows, `l1CaloTowerTree` is not a TTree but a directory (with a TTree called `L1CaloTowerTree` – with a capital L! – inside).

This works:

```python
import ROOT
fileName = "ZToMuMu_mc_off_MET_PU_mit_392SF.root"
treeName = "l1CaloTowerTree/L1CaloTowerTree"
print(ROOT.RDataFrame(treeName, fileName).GetColumnNames())

```

and prints

```auto
{ "CaloTP.nHCALTP", "CaloTP.hcalTPieta", "CaloTP.hcalTPiphi", "CaloTP.hcalTPCaliphi", "CaloTP.hcalTPet", "CaloTP.hcalTPcompEt", "CaloTP.hcalTPfineGrain", "CaloTP.nECALTP", "C
aloTP.ecalTPieta", "CaloTP.ecalTPiphi", "CaloTP.ecalTPCaliphi", "CaloTP.ecalTPet", "CaloTP.ecalTPcompEt", "CaloTP.ecalTPfineGrain", "CaloTP", "L1CaloTower.nTower", "L1CaloTow
er.ieta", "L1CaloTower.iphi", "L1CaloTower.iet", "L1CaloTower.iem", "L1CaloTower.ihad", "L1CaloTower.iratio", "L1CaloTower.iqual", "L1CaloTower.et", "L1CaloTower.eta", "L1Cal
oTower.phi", "L1CaloTower" }

```

I tried to plot `CaloTP.hcalTPieta`. This also works:

```auto
import ROOT                                                                                                                                                                   
fileName = "ZToMuMu_mc_off_MET_PU_mit_392SF.root"                                                                                                                             
treeName = "l1CaloTowerTree/L1CaloTowerTree"                                                                                                                                  
df = ROOT.RDataFrame(treeName, fileName)                                                                                                                                      
                                                                                                                                                                              
df.Histo1D("CaloTP.hcalTPieta").Draw()
input() // to keep the process alive while the canvas is displayed

```

 ![fig](https://root-forum.cern.ch/uploads/default/original/3X/f/f/ffda697085f3b3c145b500d9e1cfcab08cc1ad50.png)

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [July 29, 2020, 5:49pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/5 "2020-07-29T17:49:20Z")

</div>

Thank you for your reply Enrico!

> [@eguiraud](#):
>
> ```auto
> import ROOT
> fileName = "ZToMuMu_mc_off_MET_PU_mit_392SF.root"
> treeName = "l1CaloTowerTree/L1CaloTowerTree"
> print(ROOT.RDataFrame(treeName, fileName).GetColumnNames())
> 
> ```

This works. But when I try to run this

> [@eguiraud](#):
>
> ```auto
> import ROOT                                                                                                                                                                   
> fileName = "ZToMuMu_mc_off_MET_PU_mit_392SF.root"                                                                                                                             
> treeName = "l1CaloTowerTree/L1CaloTowerTree"                                                                                                                                  
> df = ROOT.RDataFrame(treeName, fileName)                                                                                                                                      
>                                                                                                                                                                               
> df.Histo1D("CaloTP.hcalTPieta").Draw()
> input() // to keep the process alive while the canvas is displayed
> 
> ```

I get

```auto
Traceback (most recent call last):
  File "L1Tntuple.py", line 6, in <module>
    df.Histo1D("CaloTP.hcalTPieta").Draw()
TypeError: can not resolve method template call for 'Histo1D'

```

I think this is related to the way hcalTPieta is accessed because I have no problems with Histo1D in other scripts.

> [@eguiraud](#):
>
> What ROOT version are you on?

ROOT 6.18/04

---

<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:** [July 30, 2020, 8:24am UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/6 "2020-07-30T08:24:58Z")

</div>

Hi,  
any chance you can upgrade to v6.22?  
This last problem you are encountering is an old bug that has been fixed in recent ROOT versions.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [July 31, 2020, 1:20pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/7 "2020-07-31T13:20:55Z")

</div>

Hi Enrico,  
Yes, upgrading ROOT to v6.22 via conda makes it work. Thanks!  
I was wondering how to access “CaloTP.nHCALTP”. In general, I get an error every time I try to access quantities like “.n…”.  
I see that [here](https://root.cern.ch/doc/master/df102__NanoAODDimuonAnalysis_8py.html) nMuon can be accessed like other RVec quantites.

---

<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:** [July 31, 2020, 1:37pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/8 "2020-07-31T13:37:48Z")

</div>

This one looks like a bug, sorry about that! `df.GetColumnType("CaloTP.nHCALTP")` returns `"L1Analysis::L1AnalysisCaloTPDataFormat"` but it should just be `Short_t`, right?

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [July 31, 2020, 2:53pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/9 "2020-07-31T14:53:26Z")

</div>

Thank you for your prompt reply Enrico.  
Yes, I get `L1Analysis::L1AnalysisCaloTPDataFormat`  
while it should be something like `Short_t` or `UInt_t`

---

<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:** [July 31, 2020, 4:13pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/10 "2020-07-31T16:13:06Z")

</div>

Yes, this is actually a consequence of a problem in TTree:

`tree->GetLeaf("CaloTP.nHCALTP")` and `tree->GetLeaf("CaloTP", "nHCALTP")` return a `nullptr` (while `tree->GetLeaf("nHCALTP")` returns the `TLeaf`).

The corresponding jira ticket is [https://sft.its.cern.ch/jira/browse/ROOT-10942](https://sft.its.cern.ch/jira/browse/ROOT-10942) and we consider it a critical bug, so it will be fixed in the next patch releases.

In ROOT master, the problem is partially fixed and, as a workaround, you can use `df.Histo1D("nHCALTP")` (just “nHCALTP”, without “CaloTP”) and it works.  
To get a ROOT nightly build via conda you can use (very fresh and still undocumented):

```auto
conda create -n root-nightly -c conda-forge -c https://root.cern/download/conda-nightly/latest root-nightly

```

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [August 3, 2020, 9:04pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/11 "2020-08-03T21:04:30Z")

</div>

Thank you very much @eguiraud  
The workaround works!

---

<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:** [August 5, 2020, 9:12am UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/12 "2020-08-05T09:12:12Z")

</div>

Hi,  
glad to hear that! FYI, yesterday I merged another patch in master that makes `df.Histo1D("CaloTP.nHCALTP")` also work.

If you re-install the conda ROOT nightly build you should get a ROOT version in which RDataFrame properly deals with all the cases discussed in this thread. If you have time to try this out, please let me know how it goes.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![Nathanael](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/n/48db29/32.png) [@Nathanael](https://root-forum.cern.ch/u/Nathanael)\
**Post date:** [August 5, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/13 "2020-08-05T12:12:57Z")

</div>

Hi Enrico,  
Just installed again ROOT nightly build via conda and yes,

```auto
df.Histo1D("CaloTP.nHCALTP")

```

works without any problem. Thanks!

---

<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:** [August 19, 2020, 12:12pm UTC](https://root-forum.cern.ch/t/how-to-access-the-information-contained-in-branches-in-rdataframe/40712/14 "2020-08-19T12:12:59Z")

</div>

Closed on August 19, 2020, 12:12pm UTC

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