Last active
January 13, 2016 23:19
-
-
Save oschulz/5917a6e4858390502472 to your computer and use it in GitHub Desktop.
Writing a CERN ROOT TTree from Julia using Cxx.jl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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