Skip to content

Instantly share code, notes, and snippets.

@dpiparo
Last active March 3, 2019 04:02
Show Gist options
  • Select an option

  • Save dpiparo/6f48c08ccf49ed28a21e60ec684c2439 to your computer and use it in GitHub Desktop.

Select an option

Save dpiparo/6f48c08ccf49ed28a21e60ec684c2439 to your computer and use it in GitHub Desktop.
Try Bulk read using existing ROOT IO low level interface
void write()
{
int n_muon;
float muon_pt[100];
TFile f("f.root", "RECREATE");
TTree t("t","t");
t.Branch("n_muon", &n_muon, "n_muon/I");
t.Branch("muon_pt", muon_pt, "muon_pt[n_muon]/F");
auto count = 42;
for (auto i : ROOT::TSeqI(2)) {
n_muon = 3 + i;
for (auto j : ROOT::TSeqI(n_muon)) {
muon_pt[j] = count++;
}
t.Fill();
}
t.Write();
f.Close();
}
void bulkread()
{
write();
TFile f("f.root");
auto &t = *(TTree*) f.Get("t");
auto &tbranch = *t.GetBranch("muon_pt");
tbranch.GetEntry(0);
auto tbasket = tbranch.GetBasket(0);
// With a buffer
auto tbuffer = tbasket->GetBufferRef();
float farr[100];
tbuffer->ReadFastArray(farr,3);
cout << farr[1] << endl;
// With a leaf
auto tleaf = t.GetLeaf("muon_pt");
auto p = tleaf->GetValuePointer();
cout << ((float*)p)[1] << endl;
}
@pcanal
Copy link

pcanal commented Mar 3, 2019

Before reading from the buffer, we need to moving the TBuffer's cursor to the beginning of the first event (past some meta-data). See the call to SetOffset in TBranch::GetEntry (and the content/semantic of TBranch::fEntryOffset)

... humm but tbranch.GetEntry(0); is called so it should already have been set to the 'right' offset .... so I am not sure what is going on ....

TBranch::GetEntry should induce a call to TLeafF::ReadBasket which should find the TBuffer in the same state as line 40 of the above code (for things to work). The first debugging step is likely to be to understand the different (in particular for TBuffer::GetOffset)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment