1
|
|
|
import h5py |
|
|
|
|
2
|
|
|
import numpy as np |
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
try: |
6
|
|
|
import ROOT |
7
|
|
|
from threeML.io.cern_root_utils.io_utils import open_ROOT_file |
8
|
|
|
from threeML.io.cern_root_utils.tobject_to_numpy import tree_to_ndarray, th2_to_arrays |
9
|
|
|
|
10
|
|
|
has_root = True |
|
|
|
|
11
|
|
|
|
12
|
|
|
except(ImportError): |
|
|
|
|
13
|
|
|
|
14
|
|
|
has_root = False |
|
|
|
|
15
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
18
|
|
|
if has_root: |
19
|
|
|
|
20
|
|
|
def polar_polarization_to_hdf5(polarization_root_file, hdf5_out_file): |
|
|
|
|
21
|
|
|
""" |
22
|
|
|
|
23
|
|
|
:param polarization_root_file: The ROOT file from which to build the response |
24
|
|
|
:param hdf5_out_file: The output HDF5 file name |
25
|
|
|
""" |
26
|
|
|
energy = [] |
27
|
|
|
degree = [] |
28
|
|
|
angle = [] |
29
|
|
|
|
30
|
|
|
energy_str = [] |
31
|
|
|
degree_str = [] |
32
|
|
|
angle_str = [] |
33
|
|
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
with open_ROOT_file(polarization_root_file) as f: |
|
|
|
|
36
|
|
|
|
37
|
|
|
tmp = [key.GetName() for key in f.GetListOfKeys()] |
38
|
|
|
tmp = filter(lambda x: 'sim' in x, tmp) |
39
|
|
|
for tmp2 in tmp: |
40
|
|
|
_, x, y, z = tmp2.split('_') |
|
|
|
|
41
|
|
|
|
42
|
|
|
energy.append(float(x)) |
43
|
|
|
degree.append(float(y)) |
44
|
|
|
angle.append(float(z)) |
45
|
|
|
|
46
|
|
|
energy_str.append(x) |
47
|
|
|
degree_str.append(y) |
48
|
|
|
angle_str.append(z) |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
53
|
|
|
energy = np.array(np.unique(energy)) |
54
|
|
|
degree = np.array(np.unique(degree)) |
55
|
|
|
angle = np.array(np.unique(angle)) |
|
|
|
|
56
|
|
|
|
57
|
|
|
energy_str = np.array(np.unique(energy_str)) |
58
|
|
|
degree_str = np.array(np.unique(degree_str)) |
59
|
|
|
angle_str = np.array(np.unique(angle_str)) |
|
|
|
|
60
|
|
|
|
61
|
|
|
# just to get the bins |
62
|
|
|
# must change this from ints later |
63
|
|
|
|
64
|
|
|
file_string = 'sim_%s_%s_%s' % (energy_str[1], degree_str[1], angle_str[1]) |
65
|
|
|
|
66
|
|
|
bins, _, hist = th2_to_arrays(f.Get(file_string)) |
67
|
|
|
|
68
|
|
|
out_matrix = np.zeros((len(energy), len(angle), len(degree), len(hist))) |
69
|
|
|
|
70
|
|
|
with h5py.File(hdf5_out_file, 'w', libver='latest') as database: |
71
|
|
|
|
72
|
|
|
for i,x in enumerate(energy_str): |
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
for j, y in enumerate(angle_str): |
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
for k, z in enumerate(degree_str): |
|
|
|
|
81
|
|
|
|
82
|
|
|
file_string = 'sim_%s_%s_%s' % (x, z, y) |
83
|
|
|
|
84
|
|
|
_ , _, hist = th2_to_arrays(f.Get(file_string)) |
|
|
|
|
85
|
|
|
|
86
|
|
|
out_matrix[i,j,k,:] = hist |
|
|
|
|
87
|
|
|
|
88
|
|
|
|
89
|
|
|
|
90
|
|
|
database.create_dataset('matrix',data=out_matrix,compression='lzf') |
|
|
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
if np.min(bins) < 0: |
94
|
|
|
# we will try to automatically correct for the badly specified bins |
95
|
|
|
bins = np.array(bins) |
96
|
|
|
|
97
|
|
|
bins += -np.min(bins) |
98
|
|
|
|
99
|
|
|
assert np.min(bins) >=0, 'The scattering bins have egdes less than zero' |
|
|
|
|
100
|
|
|
assert np.max(bins) <=360, 'The scattering bins have egdes greater than 360' |
|
|
|
|
101
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
|
104
|
|
|
database.create_dataset('bins', data=bins, compression='lzf') |
105
|
|
|
database.create_dataset('pol_ang', data=angle, compression='lzf') |
106
|
|
|
database.create_dataset('pol_deg', data=degree, compression='lzf') |
107
|
|
|
database.create_dataset('energy',data=energy,compression='lzf') |
|
|
|
|
108
|
|
|
|
109
|
|
|
def polar_spectra_to_hdf5(polar_root_file, polar_rsp_root_file, hdf5_out_file): |
|
|
|
|
110
|
|
|
""" |
111
|
|
|
|
112
|
|
|
:param polar_root_file: The spectral ROOT file |
113
|
|
|
:param polar_rsp_root_file: The response ROOT file |
114
|
|
|
:param hdf5_out_file: the name of the output HDF5 file |
115
|
|
|
""" |
116
|
|
|
|
117
|
|
|
# extract the info from the crappy root file |
118
|
|
|
|
119
|
|
|
with h5py.File(hdf5_out_file, 'w') as outfile: |
120
|
|
|
|
121
|
|
|
# first we do the RSP |
122
|
|
|
|
123
|
|
|
rsp_grp = outfile.create_group('rsp') |
124
|
|
|
|
125
|
|
|
with open_ROOT_file(polar_rsp_root_file) as f: |
|
|
|
|
126
|
|
|
|
127
|
|
|
matrix = th2_to_arrays(f.Get('rsp'))[-1] |
128
|
|
|
|
129
|
|
|
rsp_grp.create_dataset('matrix', data=matrix, compression='lzf') |
130
|
|
|
|
131
|
|
|
ebounds = th2_to_arrays(f.Get('EM_bounds'))[-1] |
132
|
|
|
|
133
|
|
|
rsp_grp.create_dataset('ebounds', data=ebounds, compression='lzf') |
134
|
|
|
|
135
|
|
|
mc_low = th2_to_arrays(f.Get('ER_low'))[-1] |
136
|
|
|
|
137
|
|
|
rsp_grp.create_dataset('mc_low', data=mc_low, compression='lzf') |
138
|
|
|
|
139
|
|
|
mc_high = th2_to_arrays(f.Get('ER_high'))[-1] |
140
|
|
|
|
141
|
|
|
rsp_grp.create_dataset('mc_high', data=mc_high, compression='lzf') |
142
|
|
|
|
143
|
|
|
# now we get the spectral informations |
144
|
|
|
keys_to_use = ['polar_out'] |
145
|
|
|
|
146
|
|
|
f = ROOT.TFile(polar_root_file) |
|
|
|
|
147
|
|
|
|
148
|
|
|
extra_grp = outfile.create_group('extras') |
149
|
|
|
|
150
|
|
|
for key in f.GetListOfKeys(): |
151
|
|
|
|
152
|
|
|
name = key.GetName() |
153
|
|
|
|
154
|
|
|
if name not in keys_to_use: |
155
|
|
|
try: |
156
|
|
|
|
157
|
|
|
# first we see if it is a TTree and then |
158
|
|
|
# add a new group and attach its data |
159
|
|
|
|
160
|
|
|
tree = tree_to_ndarray(f.Get(name)) |
161
|
|
|
|
162
|
|
|
new_grp = extra_grp.create_group(name) |
163
|
|
|
|
164
|
|
|
for new_name in tree.dtype.names: |
165
|
|
|
new_grp.create_dataset(new_name, data=tree[new_name], compression='lzf') |
166
|
|
|
|
167
|
|
|
except: |
|
|
|
|
168
|
|
|
|
169
|
|
|
# in this case we just want the actual data |
170
|
|
|
|
171
|
|
|
data = th2_to_arrays(f.Get(name))[-1] |
172
|
|
|
|
173
|
|
|
extra_grp.create_dataset(name, data=data, compression='lzf') |
174
|
|
|
|
175
|
|
|
# now we will deal with the data that is important |
176
|
|
|
|
177
|
|
|
tmp = tree_to_ndarray(f.Get('polar_out')) |
178
|
|
|
|
179
|
|
|
outfile.create_dataset('energy', data=tmp['Energy'], compression='lzf') |
180
|
|
|
|
181
|
|
|
outfile.create_dataset('scatter_angle', data=tmp['scatter_angle'], compression='lzf') |
182
|
|
|
|
|
|
|
|
183
|
|
|
outfile.create_dataset('dead_ratio', data=tmp['dead_ratio'], compression='lzf') |
184
|
|
|
|
185
|
|
|
outfile.create_dataset('time', data=tmp['tunix'], compression='lzf') |
186
|
|
|
|
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
|
189
|
|
|
f.Close() |
190
|
|
|
|
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.