-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot2struct.m
43 lines (38 loc) · 1.18 KB
/
root2struct.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
%% Import ROOT Ntuples into MATLAB as Structure of Tables
% Copyright 2024 The Mathworks, Inc.
%
% Path management
orig = pwd;
parserPath = orig+"\NtupleParserCode.py";
rootfilePath = orig+"\py-hsimple2.root";
rootfilePath = "'"+replace(rootfilePath,"\","/")+"'";
%%
% Navigate into root bin folder, run thisroot.bat, then run PyROOT code.
cd('C:\root_v6.30.04\bin\')
try
S = struct;
!thisroot.bat
%%
% Collect Keys from ROOT file and list only the Ntuples
[Keys,isfolder]=pyrunfile(orig+"\CollectNtuples.py "+rootfilePath,["KeyNameList","IsFolderList"]);
Keys = replace(string(char(Keys)),"'",'"');
eval("Keys="+Keys)
isfolder = logical(isfolder);
Keys = Keys(isfolder);
%%
% Loop over Ntuples to grab leaf data and names. Save Ntuple content to a MATLAB
% Table. Save Tables to Structure.
for j=1:length(Keys)
[Values,leafNames]=pyrunfile(parserPath+" "+rootfilePath+" "+Keys(j),["NtupleVars","leafNames"]);
Values = double(Values).';
leaves = replace(string(char(leafNames)),"'",'"');
eval("leaves="+leaves)
T = array2table(Values);
T.Properties.VariableNames = leaves;
S.(Keys(j)) = T;
end
catch Merr
disp(Merr.message)
end
cd(orig)
save("structFromROOT","S")