# Reading boolean data from TTree using PyROOT

**URL:** <https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178>\
**Category:** Newbie\
**Tags:** python\
**Created:** [April 30, 2020, 5:43am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178 "2020-04-30T05:43:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reik](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/reik/32/17302_2.png) [@Reik](https://root-forum.cern.ch/u/Reik)\
**Post date:** [April 30, 2020, 5:43am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/1 "2020-04-30T05:43:11Z")

</div>

* * *

_ROOT Version:_ master  
_Platform:_ Linux 5.4 (Manjaro)  
_Compiler:_ GCC 9.3

* * *

I can create a TTree with a branch holding boolean values like this -

```auto
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"

void tleafo() {
    TFile *f;
    f = new TFile("tleafo.root", "RECREATE");
    f->SetCompressionLevel(0);

    TTree *t = new TTree("sample", "");
    Bool_t b;
    t->Branch("b", &b, "b/O");
    for (int i = 0; i<5; i++) {
        b = 1;
        t->Fill();
    }
    t->Write();
    f->Close();
}

```

I want to read the data out using PyROOT.  
This is how I am trying to do that -

```auto
import ROOT
import numpy

ROOT.gInterpreter.Declare("""
void readnewbasket(bool* arr, char* filename) {
    Bool_t x;
    TFile f(filename);
    auto tree = f.Get<TTree>("t");
    auto branch = tree->GetBranch("booleanBranch");
    branch->SetAddress(&x);
    for (int i=0; i<tree->GetEntries(); i++) {
        tree->GetEvent(i);
        arr[i] = x;
    }
}""")

initializer = numpy.array([0, 0, 0, 0, 0], dtype=">?")
ROOT.readnewbasket(initializer, "tleafo.root")
for i in range(5):
    print(initializer[i])

```

But when I try to execute the above Python code, I get an error (on both pyroot\_experimental ON and OFF) -

```auto
TypeError: void ::readnewbasket(bool* arr, char* filename) =>
    TypeError: could not convert argument 1 (could not convert argument to buffer or nullptr)

```

I would really appreciate any help with this. Thanks!

Not directly related but might be useful information - I think this used to work in the past when I had originally written the above Python script (maybe for ROOT 6.16… I am not sure). But this also works on the [conda-forge build of ROOT](https://github.com/conda-forge/root-feedstock).

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [April 30, 2020, 7:32am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/2 "2020-04-30T07:32:36Z")

</div>

Hi!

Indeed, I think the automatic conversion from a boolean buffer to a `bool*` is broken. We will investigate! However, to fix your application, you can do the following:

```python
import ROOT
import numpy

ROOT.gInterpreter.Declare("""
void tleafo() {
    TFile *f;
    f = new TFile("tleafo.root", "RECREATE");
    f->SetCompressionLevel(0);

    TTree *t = new TTree("sample", "");
    Bool_t b;
    t->Branch("b", &b, "b/O");
    for (int i = 0; i<5; i++) {
        b = 1;
        t->Fill();
    }
    t->Write();
    f->Close();
}

void readnewbasket(void* arr, char* filename) {
    Bool_t x;
    auto arr2 = (bool*) arr;
    TFile f(filename);
    auto tree = f.Get<TTree>("sample");
    auto branch = tree->GetBranch("b");
    branch->SetAddress(&x);
    for (int i=0; i<tree->GetEntries(); i++) {
        tree->GetEvent(i);
        arr2[i] = x;
    }
    f.Close();
}""")

ROOT.tleafo()
initializer = numpy.array([0, 0, 0, 0, 0], dtype=numpy.bool)
ROOT.readnewbasket(initializer, "tleafo.root")
for i in range(5):
    print(initializer[i])

```

So in short: Take a `void*` in the signature in C++ and then simply cast it to a `bool*` inside. This fixes your issue with the PyROOT layer as long we investigate further.

Side note, you can achieve your conversion to numpy also in a single line like this

```python
b = ROOT.RDataFrame('sample', 'tleafo.root').AsNumpy(['b'])['b']

```

Though, if the buffer is small, your implementation is more efficient.

Best  
Stefan

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [April 30, 2020, 7:39am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/3 "2020-04-30T07:39:34Z")

</div>

Hi again,

I’ve created a Jira ticket to keep track of the issue:  
[https://sft.its.cern.ch/jira/browse/ROOT-10731](https://sft.its.cern.ch/jira/browse/ROOT-10731)

Best  
Stefan

---

<div class="post-metadata">

**Author:** ![Reik](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/reik/32/17302_2.png) [@Reik](https://root-forum.cern.ch/u/Reik)\
**Post date:** [April 30, 2020, 8:12am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/4 "2020-04-30T08:12:40Z")

</div>

Thanks! 🙂

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [April 30, 2020, 8:36am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/5 "2020-04-30T08:36:54Z")

</div>

Small update, I think I found the issue. We don’t recognize correctly the type identifier `"?"`, just use `"b"`, and you should be good to go.

---

<div class="post-metadata">

**Author:** ![Reik](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/reik/32/17302_2.png) [@Reik](https://root-forum.cern.ch/u/Reik)\
**Post date:** [April 30, 2020, 9:14am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/6 "2020-04-30T09:14:10Z")

</div>

But “b” means `numpy.int8` or ROOT’s `TLeafB` and not the boolean data type right?

---

<div class="post-metadata">

**Author:** ![swunsch](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@swunsch](https://root-forum.cern.ch/u/swunsch)\
**Post date:** [April 30, 2020, 9:27am UTC](https://root-forum.cern.ch/t/reading-boolean-data-from-ttree-using-pyroot/39178/7 "2020-04-30T09:27:46Z")

</div>

Actually fully correct! If you don’t care on the python side about that detail (normally numpy abstracts this detail for you very nicely), your code should do the same thing. On the C++ side, both of them have 1 byte and we are fine. Also, the Python type code `b` is actually what we recognize as a `bool` in PyROOT for conversion.
