ROOT Version: master
Platform: Linux 5.4 (Manjaro)
Compiler: GCC 9.3
I can create a ROOT file with a variable sized array of type Long64_t
like this -
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
void jagged3() {
TFile *f = new TFile("jagged.root", "RECREATE");
f->SetCompressionLevel(0);
TTree *t = new TTree("t", "");
Int_t n = 1;
t->Branch("n", &n, "n/I");
Long64_t branch[100];
Long64_t count = 0;
t->Branch("branch", branch, "branch[n]/L");
for (int i=0; i<3; i++) {
for (int j=0; j<n; j++) {
branch[j] = count;
count = count + 1;
}
t->Fill();
n = n + 1;
}
t->Write();
f->Close();
}
How do I read this out in ROOT?
The code to read it out would probably look something like this (I assume) -
#include "TFile.h"
#include "TTree.h"
#include "TBranch.h"
#include <stdio.h>
void readint() {
TFile *f = new TFile("jagged.root");
Long64_t x;
auto tree = f->Get<TTree>("t");
auto branch = tree->GetBranch("branch");
branch->SetAddress(&x);
for (int i=0; i<tree->GetEntries(); i++) {
tree->GetEvent(i);
for (int j=0; j<#number of entries in current event#; j++) {
printf("%lld\n", x);
}
}
}
But for this how do I get the number of entries in a particular event?
My main intention is to read this out in Python(into lists or numpy arrays), so once I have a working C/C++ code where I can read it successfully, I would read it out in Python using ROOT.gInterpreter.Declare
. I know this can probably be achieved in an easier way like -
f = ROOT.TFile.Open("jagged.root")
tree = f.Get("t")
for _, event in enumerate(tree):
print([x for x in event.branch])
but I am trying to avoid doing that.