Root -> pyROOT SetBranchAddress Error


I am new in pyROOT. I am converting a ROOT code to pyRoot to have better integration with my python codes, but I am having some errors.
I am using Root 5.34.34 without possibility to upgrade and the correspont pyROOT version with python 2.7

My root code is like that for now:

#include <fstream>

#include "TROOT.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TH2D.h"
#include "TFile.h"
#include "TTree.h"

using namespace std;

void root(int dataset, int t, bool isCalib=false)


    const char* dataType;
    if( isCalib )
        dataType = "calibration";
        dataType = "background";

    TFile* dataFile = new TFile(Form("data/%s_%d_%03d.root",dataType,dataset,t));

    TTree* chi2tree = (TTree*) dataFile->Get("chi2tree");

    Int_t chan    = 0;
    Float_t energy  = 0;

    chi2tree->SetBranchAddress("chan", &chan);
    chi2tree->SetBranchAddress("energy", &energy);

When I do the same in pyROOT, everything goes fine until the SetBranchAddress where I get the following error:

TypeError: none of the 3 overloaded methods succeeded. Full details:
Int_t TTree::SetBranchAddress(const char* bname, void** add, TBranch** ptr = 0) =>
could not convert argument 2
Int_t TTree::SetBranchAddress(const char* bname, void* add, TClass* realClass, EDataType datatype, Bool_t isptr) =>
takes at least 5 arguments (2 given)
Int_t TTree::SetBranchAddress(const char* bname, void* add, TBranch** ptr, TClass* realClass, EDataType datatype, Bool_t isptr) =>
takes at least 6 arguments (2 given)

My pyROOT code is the following:

import ROOT
import sys

print(" This is my script ")

def main(args):
    tamanho = len(args)
    if tamanho==4:

def root(dataset,t,isCalib=False):
    if isCalib:
        print("\nLoading calibration data\n")
        dataType = "calibration"
        print("\nLoading background data\n")
        dataType = "background"

    dataFile = ROOT.TFile("data/{0}_{1}_{2:03}.root".format(dataType,dataset,int(t)))
    chi2tree = dataFile.Get("chi2tree")

    chan = 0
    energy = 0.

    chi2tree.SetBranchAddress("chan", chan)
   chi2tree.SetBranchAddress("energy", energy)

Thanks in advance,

Welcome to the ROOT Forum! Maybe @etejedor can help


In order to use SetBranchAddress with an integer and a float in Python, you need to create an array.array of one position and pass that array to SetBranchAddress. You have an example here:

(look for the PyROOT box)

This works with ROOT 6 but I am not sure if it was the case already with 5. Anyway, it is worth a try.

import array
chan = array.array('l', [0]); # one "Int_t"
energy = array.array('f', [0]); # one "Float_t"
chi2tree.SetBranchAddress("chan", chan)
chi2tree.SetBranchAddress("energy", energy)
chi2tree.GetEntry(0) # test the very first entry
print 'chan = ', chan[0]
print 'energy = ', energy[0]

@etejedor @Wile_E_Coyote
It worked perfectly.
Thank you very much :slight_smile:

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