Convert from CINT to PyROOT

Hi,

I am trying to get access to data from a awkwardly formatted ROOT file. I got this to work in CINT:

[code]{
gROOT->Reset();
TFile *f = (TFile *)TFile::Open("/path/to/file");

// get tree
TTree *sn_all = (TTree *)gDirectory->Get("all");

// maximal number of channels
Int_t max_channel = 6000;

//Declaration of leaf types
ULong_t base_time;
ULong_t gps_time_high;
ULong_t gps_time_low;
ULong_t buf_pos;
ULong_t buf_loop;
Int_t no_channels;
UShort_t data[max_channel]; //no_channels

// List of branches
TBranch *b_all_data_base_time;
TBranch *b_all_data_gps_time_high;
TBranch *b_all_data_gps_time_low;
TBranch *b_all_data_buf_pos;
TBranch *b_all_data_buf_loop;
TBranch *b_all_data_no_channels;
TBranch *b_data;


// Set branch addresses
sn_all->SetMakeClass(1); //important!!
sn_all->SetBranchAddress("base_time", &base_time, &b_all_data_base_time);
sn_all->SetBranchAddress("gps_time_high", &gps_time_high, &b_all_data_gps_time_high);
sn_all->SetBranchAddress("gps_time_low", &gps_time_low, &b_all_data_gps_time_low);
sn_all->SetBranchAddress("buf_pos", &buf_pos, &b_all_data_buf_pos);
sn_all->SetBranchAddress("buf_loop", &buf_loop, &b_all_data_buf_loop);
sn_all->SetBranchAddress("no_channels", &no_channels, &b_all_data_no_channels);
sn_all->SetBranchAddress("data", data, &b_data);

// now get set up for the loop
Long64_t nentries = all->GetEntries();
cout << " number of entries= " << nentries << endl;
Long64_t nbytes = 0;

// and the event loop begins ....
for (Long64_t i = 0; i < nentries; i++)
{
    nbytes += all->GetEntry(i);
    for (Long64_t k = 0; k < no_channels; k++)
    {

        ULong64_t timestamp_ns;
        timestamp_ns = gps_time_high;
        timestamp_ns = timestamp_ns << 32;
        timestamp_ns += gps_time_low;
        cout  << "nbytes= " << nbytes << endl;
        cout  << "base_time= " << base_time << endl;
        cout  << "no of channels= " << no_channels << endl;
        cout  << "gps_time_low= " << gps_time_low  <<  endl;
        cout  << "gps_time_high= " << gps_time_high  <<  endl;
        cout  << "Timestamp = " << timestamp_ns << endl;
        cout  << "buf_loop= " << buf_loop  <<  endl;
        cout  << "buf_pos= " << buf_pos  <<  endl;
        cout  << "data[" << k << "] " << data[k] << endl;
    }

}

}[/code]

Is there a way to convert this to PyROOT script?

When I try to just poke at the branch in pyroot, the entries return the same value for all entries:

In [1]: import ROOT
f = ROOT.TFile("/path/to/file")
In [4]: t = f.Get("all")
In [6]: data = t.GetBranch("data")
In [7]: data.GetEntries()
Out[7]: 57605L

In [8]: data.GetEntry(1)
Out[8]: 10311

In [9]: data.GetEntry(2)
Out[9]: 10311

In [12]: data.Print()
*Br    0 :data      : UShort_t data[no_channels]                             *
*Entries :    57605 : Total  Size=  595180899 bytes  File Size  =  345332405 *
*Baskets :    11521 : Basket Size=      60000 bytes  Compression=   1.72     *
*............................................................................*

I have tried using the gROOT.ProcessLine() to set the address:

[code]#!/usr/bin/env python
import ROOT

ROOT.gROOT.Reset()

s = “struct variables_t{
Int_t max_channel = 5153;
ULong_t base_time;
ULong_t gps_time_high;
ULong_t gps_time_low;
ULong_t buf_pos;
ULong_t buf_loop;
Int_t no_channels;
UShort_t data[max_channel];
TBranch *b_all_data_base_time;
TBranch *b_all_data_gps_time_high;
TBranch *b_all_data_gps_time_low;
TBranch *b_all_data_buf_pos;
TBranch *b_all_data_buf_loop;
TBranch *b_all_data_no_channels;
TBranch *b_data;
}”

ROOT.gROOT.ProcessLine(s)

f = ROOT.TFile("/path/to/file")
t = f.Get(“all”)

variables = variables_t()

t.SetBranchAddress(“data”,ROOT.AddressOf(variables,‘data’) )[/code]

It claims that variables_t is unknown

Thanks a lot in advance.

Hi,

[quote=“madtowneast”]When I try to just poke at the branch in pyroot, the entries return the same value for all entries:[/quote]Calls to GetEntry() return the number of bytes read, so that should be the same value.

[quote=“madtowneast”]It claims that variables_t is unknown[/quote]Yes, since you did “import ROOT”, so all ROOT-stuff is namespaced with “ROOT.”. Iow., you’ll find the class as ROOT.variables_t. If you did “from ROOT import *”, then “variables_t” would be known globally even if it is defined after that import (this is b/c names are looked up lazily).

Cheers,
Wim

1 Like