Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

groot: add write support for Branch+buffer of leaves #879

Open
sbinet opened this issue Nov 24, 2021 · 0 comments
Open

groot: add write support for Branch+buffer of leaves #879

sbinet opened this issue Nov 24, 2021 · 0 comments

Comments

@sbinet
Copy link
Member

sbinet commented Nov 24, 2021

use case "discovered" in:

the following PyROOT script:

import numpy as np

dt = np.dtype([
    ('x', "f4"), ('y', "f4"), ("z", "f4"), 
    ("px", "f4"), ("py", "f4"), 
    ("Ekin", "f4"), ("wepl", "f4"), 
    ("pdgID", "<i4"), 
    ("flag", "<u2"), 
    ("tof", "f4"),
    ("runID","<i4"),
    ("evtID","<i4"),
    ("trkID","<i4"),
    ("parID","<i4")])

evts = np.fromfile("./PhaseSpace.phsp", dtype=dt)
evts = np.sort(evts, order="evtID")

import ROOT
f = ROOT.TFile.Open("out.root", "RECREATE")
t = ROOT.TTree("PhaseSpaceTree", "PhaseSpaceTree")

dt = np.dtype([
    ('x', "f4"), ('y', "f4"), ("z", "f4"), 
    ("px", "f4"), ("py", "f4"), ("pz", "f4"),
    ("Ekin", "f4"), ("wepl", "f4"), 
    ("pdgID", "<i4"), 
    ("event", "<i4"),
    ("trackID","<i4"),
    ("PB_ID","<i4")])

buf = np.array([
    0,0,0,
    0,0,0,
    0,0,
    0,0,0
    ], dtype=dt)

t.Branch("PhaseSpaceBranch", buf, "x/F:y/F:z/F:px/F:py/F:pz/F:Ekin/F:wepl/F:pdgID/I:event/I:trackID/I:PB_ID/I")
for v in evts:
    buf["x"] = v["x"]
    buf["y"] = v["y"]
    buf["z"] = v["z"]
    buf["px"] = v["px"]
    buf["py"] = v["py"]
    buf["pz"] = 1 - buf["px"]**2 - buf["py"]**2
    buf["Ekin"] = v["Ekin"]
    buf["wepl"] = v["wepl"]
    buf["pdgID"] = v["pdgID"]
    buf["event"] = v["evtID"]
    buf["trackID"] = v["trkID"]
    buf["PB_ID"] = v["parID"]
    t.Fill()
t.Write()
f.Close()

will create a ROOT file+tree with the following structure:

$> root-ls -t ./out.root 
=== [./out.root] ===
version: 62406
  TTree              PhaseSpaceTree                                       PhaseSpaceTree (entries=4122)
    PhaseSpaceBranch "x/F:y/F:z/F:px/F:py/F:pz/F:Ekin/F:wepl/F:pdgI[...]" TBranch

$> root-dump ./out.root | head -n 20
>>> file[./out.root]
key[000]: PhaseSpaceTree;1 "PhaseSpaceTree" (TTree)
[000][PhaseSpaceBranch.x]: -1.95
[000][PhaseSpaceBranch.y]: 0.18836617
[000][PhaseSpaceBranch.z]: -0.23779297
[000][PhaseSpaceBranch.px]: 0.99966717
[000][PhaseSpaceBranch.py]: -0.019618336
[000][PhaseSpaceBranch.pz]: 0.00028066634
[000][PhaseSpaceBranch.Ekin]: 86.00329
[000][PhaseSpaceBranch.wepl]: 1
[000][PhaseSpaceBranch.pdgID]: 2212
[000][PhaseSpaceBranch.event]: 0
[000][PhaseSpaceBranch.trackID]: 1
[000][PhaseSpaceBranch.PB_ID]: 999
[001][PhaseSpaceBranch.x]: -1.95
[001][PhaseSpaceBranch.y]: -0.44054914
[001][PhaseSpaceBranch.z]: 0.03474723
[001][PhaseSpaceBranch.px]: 0.9999974
[001][PhaseSpaceBranch.py]: 0.0012126022
[001][PhaseSpaceBranch.pz]: 3.7748046e-06

right now (v0.29), groot/rtree doesn't provide the ability to fill a Tree from a contiguous buffer of bytes, with some metadata to instruct on how to interpret that buffer.

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

No branches or pull requests

1 participant