-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergertreeHDF5.py
252 lines (231 loc) · 11.6 KB
/
mergertreeHDF5.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# Python HDF5 merger tree reader
# (requires util/hdf5lib.py)
#
# see example_X.py for usage
#
#
# Mark Vogelsberger ([email protected])
import numpy as np
import os
import sys
import hdf5lib
import pdb
mergertree_datablocks = {"Descendant": ["int32", 1, True],
"FirstProgenitor": ["int32", 1, True],
"NextProgenitor": ["int32", 1, True],
"FirstHaloInFOFGroup": ["int32", 1, True],
"NextHaloInFOFGroup": ["int32", 1, True],
"SubhaloLen": ["int32", 1, True],
"Group_M_Mean200": ["float32", 1, True],
"Group_M_Crit200": ["float32", 1, True],
"Group_M_TopHat200": ["float32", 1, True],
"SubhaloPos": ["float32", 3, True],
"SubhaloVel": ["float32", 3, True],
"SubhaloVelDisp": ["float32", 1, True],
"SubhaloVMax": ["float32", 1, True],
"SubhaloSpin": ["float32", 3, True],
"SubhaloIDMostBound": ["int64", 1, True],
"SnapNum": ["int32", 1, True],
"FileNr": ["int32", 1, True],
"SubhaloGrNr": ["int32", 1, True],
"SubhaloNumber": ["int32", 1, True],
"SubhaloSFR": ["float32", 1, True],
"SubhaloGasMetallicity": ["float32", 1, True],
"SubhaloGasMetallicitySfr": ["float32", 1, True],
"SubhaloStarMetallicity": ["float32", 1, True],
"SubhaloOffsetType": ["int64", 6, True],
"SubhaloLenType": ["int32", 6, True],
"SubhaloMassType": ["float32", 6, True],
"SubhaloMassInRadType": ["float32", 6, True],
"SubhaloHalfmassRadType": ["float32", 6, True],
"SubhaloBHMass": ["float32", 1, True],
"SubhaloBHMdot": ["float32", 1, True],
"SubhaloSFRinRad": ["float32", 1, True],
"SubhaloStellarPhotometrics": ["float32", 8, True]}
class merger_tree:
def __init__(self, basedir, skipfac, snapnum, filenum = 0, tree_start = -1, tree_num = -1, keysel = None):
self.filebase = basedir + "trees_sf"+str(skipfac)+"_"+str(snapnum).zfill(3)
self.basedir = basedir
self.filenum = filenum
filename = self.filebase + "." + str(filenum) + ".hdf5"
f=hdf5lib.OpenFile(filename)
self.NtreesPerFile = hdf5lib.GetAttr(f, "Header", "NtreesPerFile")
self.NumberOfOutputFiles = hdf5lib.GetAttr(f, "Header", "NumberOfOutputFiles")
self.ParticleMass = hdf5lib.GetAttr(f, "Header", "ParticleMass")
if (self.ParticleMass == 0):
print("WARNING: ParticleMass = 0, needed for merger rate calculation")
self.TreeNHalos = hdf5lib.GetData(f, "Header/TreeNHalos")[:]
self.TotNsubhalos = hdf5lib.GetData(f, "Header/TotNsubhalos")[:]
self.Redshifts = hdf5lib.GetData(f, "Header/Redshifts")[:]
if (tree_start == -1 ) | (tree_num == -1):
tree_start = 0
tree_num = self.NtreesPerFile
self.trees = np.empty(tree_num - tree_start, dtype='object')
self.tree_start = tree_start
self.tree_num = tree_num
for ntree in range(tree_start, tree_start + tree_num):
list = []
if (keysel==None):
for datablock in list(mergertree_datablocks.keys()):
data = hdf5lib.GetData(f, "Tree"+str(ntree)+"/"+datablock)[:]
list.append((datablock,data))
else:
for datablock in keysel:
if hdf5lib.Contains(f, "Tree"+str(ntree), datablock):
data = hdf5lib.GetData(f, "Tree"+str(ntree)+"/"+datablock)[:]
list.append((datablock,data))
self.trees[ntree - tree_start] = dict(list)
f.close()
def __count_unique(self, keys):
uniq_keys = np.unique(keys)
bins = uniq_keys.searchsorted(keys)
return uniq_keys, np.bincount(bins)
def getNumberOfMergers(self, snapnum, bins_halo = 10, bins_ratio = 10, halo_min = 8, halo_max = 13, ratio_min = 0, ratio_max = 1):
htot = np.zeros([bins_halo, bins_ratio])
xtot = 0
ytot = 0
for ntree in range(0,self.tree_num):
idx = (self.trees[ntree]["SnapNum"][:] == snapnum) & (self.trees[ntree]["Descendant"] >= 0)
if (idx.any()):
halos = np.arange(0, self.TreeNHalos[ntree])[idx]
descs = self.trees[ntree]["Descendant"][idx]
d_tmp, n_tmp = self.__count_unique(descs)
merger_descs = d_tmp[n_tmp > 1] #indices of halos where more than 1 object 'descends-into'
if (len(descs) > 0):
for md in merger_descs:
len_desc = self.trees[ntree]["SubhaloLenType"][:,1][md] #len of the central descendant, snp=snapnum+1
len_halos = self.trees[ntree]["SubhaloLenType"][:,1][md == descs] #len of the objects that descend into the one above, snp=snapnum
ratio = 1.0 * len_halos / len_desc
x = np.log10(len_halos * self.ParticleMass * 1e10)
y = ratio
h, xtot, ytot = np.histogram2d(x, y, bins=(bins_halo, bins_ratio), range = [[halo_min,halo_max], [ratio_min, ratio_max]])
htot += h
return [xtot, ytot, htot]
def getNumberOfMergersMainBranch(self, snapnum, id_descendant, ntree, bins_halo = 10, bins_ratio = 10, halo_min = 8, halo_max = 13, ratio_min = 0, ratio_max = 1):
htot = np.zeros([bins_halo, bins_ratio])
xtot = 0
ytot = 0
for ntree in range(ntree,ntree+1):
#####idx = (self.trees[ntree]["SnapNum"][:] == snapnum) & (self.trees[ntree]["Descendant"] >= 0)
idx = (self.trees[ntree]["SnapNum"][:] == snapnum) & (self.trees[ntree]["Descendant"] == id_descendant)
if (idx.any()):
print('LVS TreeNHalos',self.TreeNHalos[ntree])
halos = np.arange(0, self.TreeNHalos[ntree])[idx]
iix = self.trees[ntree]["FirstProgenitor"][id_descendant]
descs = self.trees[ntree]["Descendant"][idx]
aux = halos != iix
halos = halos[aux]
descs = descs[aux]
merger_descs = descs
if merger_descs.any(): print('LVS and descs=',descs)
if (len(descs) > 0):
for md in merger_descs:
len_desc = self.trees[ntree]["SubhaloLenType"][:,1][md] #len of the central descendant, snp=snapnum+1
len_halos = self.trees[ntree]["SubhaloLenType"][:,1][halos] #len of the objects that descend into the one above, snp=snapnum
ratio = 1.0 * len_halos / len_desc
print('LVS ratio',(ratio,len_halos,len_desc))
pdb.set_trace()
x = np.log10(len_halos * self.ParticleMass * 1e10)
y = ratio
h, xtot, ytot = np.histogram2d(x, y, bins=(bins_halo, bins_ratio), range = [[halo_min,halo_max], [ratio_min, ratio_max]])
htot += h
return [xtot, ytot, htot]
def getNumberJoinFOFMainBranch(self, snapnum, id_descendant, ntree, ratio_min = 0, ratio_max = 1):
ratio = []
#for ntree in range(0,self.tree_num): ## LVS: ask Mark, if loop needed here, modify ratio to append new mergers
for ntree in range(ntree,ntree+1):
idx = (self.trees[ntree]["SnapNum"][:] == snapnum) & (self.trees[ntree]["FirstHaloInFOFGroup"][self.trees[ntree]["Descendant"][:]] == id_descendant)
if (idx.any()):
halos = np.arange(0, self.TreeNHalos[ntree])[idx]
centrals = self.trees[ntree]["FirstHaloInFOFGroup"][idx]
iix = self.trees[ntree]["FirstProgenitor"][id_descendant]
descs = self.trees[ntree]["Descendant"][idx]
aux = ((halos == centrals) & (halos != iix))
halos = halos[aux]
descs = descs[aux]
merger_descs = descs
if (len(descs) > 0):
#len_desc = self.trees[ntree]["SubhaloLenType"][:,1][id_descendant] #len of the central descendant, snp=snapnum+1
len_desc = self.trees[ntree]["SubhaloLen"][id_descendant] #len of the central descendant, snp=snapnum+1
len_halos = self.trees[ntree]["SubhaloLen"][halos] #len of the objects that descend into the one above, snp=snapnum
ratio = 1.0 * len_halos / len_desc
#print 'LVS: ratio=',ratio
ikeep = ((ratio > ratio_min) & (ratio < ratio_max))
ratio = ratio[ikeep]
return ratio
def getAllProgenitors(self, ntree, nhalo):
list_next = []
list_first = []
list_first.append(self.trees[ntree]["FirstProgenitor"][nhalo])
while (len(list_first) > 0):
next = list_first.pop()
while (next >= 0):
list_next.append(next)
new_next = self.trees[ntree]["NextProgenitor"][next]
new_first = self.trees[ntree]["FirstProgenitor"][next]
list_first.append(new_first)
next = new_next
return list_next
def getProgenitors(self, ntree, nhalo):
list = []
next = self.trees[ntree]["FirstProgenitor"][nhalo]
while (next >= 0):
list.append(next)
next = self.trees[ntree]["NextProgenitor"][next]
return list
def getFirstProgenitors(self, ntree, nhalo):
list = []
next = nhalo
while (next >= 0):
list.append(next)
next = self.trees[ntree]["FirstProgenitor"][next]
return list
def getHalosInFOFGroup(self, ntree, nhalo):
list = []
next = self.trees[ntree]["FirstHaloInFOFGroup"][nhalo]
while (next >= 0):
list.append(next)
next =self.trees[ntree]["NextHaloInFOFGroup"][next]
return list
def getDescendants(self, ntree, nhalo):
list = []
next = self.trees[ntree]["Descendant"][nhalo]
while (next >=0 ):
list.append(next)
next = self.trees[ntree]["Descendant"][next]
return list
def constructSubhaloLookup(self, snapnum):
self.SubhaloLookupTable = np.zeros([self.TotNsubhalos[snapnum],3], dtype='int32') - 1
for ntree in range(0,self.tree_num):
idx = (self.trees[ntree]["SnapNum"][:] == snapnum)
halos = np.arange(0, self.TreeNHalos[ntree], dtype='int32')[idx]
subnums = self.trees[ntree]["SubhaloNumber"][idx]
self.SubhaloLookupTable[subnums,0] = self.filenum
self.SubhaloLookupTable[subnums,1] = ntree
self.SubhaloLookupTable[subnums,2] = halos
f=open(self.basedir+"/SubhaloLookup_"+str(snapnum).zfill(3)+"."+str(self.filenum)+".dat","wb")
self.SubhaloLookupTable.astype("int32").tofile(f)
f.close()
def combineSubhaloLookup(self, snapnum):
self.SubhaloLookupTable = np.zeros([self.TotNsubhalos[snapnum],3], dtype='int32') - 1
for filenum in range(0, self.NumberOfOutputFiles):
f=open(self.basedir+"/SubhaloLookup_"+str(snapnum).zfill(3)+"."+str(filenum)+".dat","rb")
tmp = np.fromfile(f, dtype="int32", count=3 * self.TotNsubhalos[snapnum]).reshape([self.TotNsubhalos[snapnum],3])
f.close()
idx = tmp != -1
self.SubhaloLookupTable[idx] = tmp[idx]
def saveSubhaloLookup(self, base, snapnum):
f=open(base+"/SubhaloLookup_"+str(snapnum).zfill(3)+".dat","wb")
self.SubhaloLookupTable.astype("int32").tofile(f)
f.close()
def loadSubhaloLookup(self, base, snapnum):
f=open(base+"/SubhaloLookup_"+str(snapnum).zfill(3)+".dat","rb")
self.SubhaloLookupTable = np.fromfile(f, dtype="int32", count=3 * self.TotNsubhalos[snapnum]).reshape([self.TotNsubhalos[snapnum],3])
f.close()
def getSubhaloLookupTable(self):
return self.SubhaloLookupTable
def lookupSubhalo(self, subhalo_num):
filenum = self.SubhaloLookupTable[subhalo_num,0]
ntree = self.SubhaloLookupTable[subhalo_num,1]
nhalo = self.SubhaloLookupTable[subhalo_num,2]
return [filenum, ntree, nhalo]