Skip to content

Instantly share code, notes, and snippets.

@oschulz
Last active January 13, 2016 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oschulz/5917a6e4858390502472 to your computer and use it in GitHub Desktop.
Save oschulz/5917a6e4858390502472 to your computer and use it in GitHub Desktop.
Writing a CERN ROOT TTree from Julia using Cxx.jl
# Copyright (C) 2016 Oliver Schulz <oschulz@mpp.mpg.de>
# Licensed under the MIT License: http://opensource.org/licenses/MIT
# Note: Currently only works with ROOT-5, not ROOT-6, due to LLVM clashes
# between Cling and Julia.
using Cxx
using Distributions
incdir = strip(readall(`root-config --incdir`))
libdir = strip(readall(`root-config --libdir`))
addHeaderDir(incdir, kind=C_System)
Libdl.dlopen(joinpath(libdir, "libTree.so"), Libdl.RTLD_GLOBAL)
cxxinclude("TFile.h")
TFile(name, mode) = icxx""" new TFile($(pointer(name)), $(pointer(mode))); """
close(tfile) = icxx"""
$tfile->Write();
$tfile->Close();
delete $tfile;
"""
cxxinclude("TTree.h")
TTree(name, title) = icxx""" new TTree($(pointer(name)), $(pointer(title))); """
branch{T<:Number}(ttree, value::Ref{T}, name) = icxx"""
$ttree->Branch($(pointer(name)), &$value);
"""
fill(ttree) = icxx""" $ttree->Fill(); """
tfile = TFile("out.root", "recreate")
ttree = TTree("data", "Data")
idx = Ref{Int32}(0)
v = Ref(0.0)
branch(ttree, idx, "idx")
branch(ttree, v, "v")
dist = Normal(0.0, 5.0)
@time for i in 1:1000000
idx.x = i
v.x = rand(dist)
fill(ttree)
end
close(tfile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment