-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyRootMegalibReader.py
More file actions
369 lines (252 loc) · 10.5 KB
/
PyRootMegalibReader.py
File metadata and controls
369 lines (252 loc) · 10.5 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
"""
------------------------------------------------------------------------
A script to read in simulated MEGALib events & store infomation as pandas dataframes.
Author: Henrike Fleischhack (fleischhack@cua.edu)
Date: August 13th, 2020
------------------------------------------------------------------------
"""
import os
import time
import sys
import fileinput
import numpy
from itertools import product, combinations, repeat
from collections import OrderedDict
import glob
import pandas
import math
import pickle
import gzip
from astropy.coordinates import cartesian_to_spherical, spherical_to_cartesian
from matplotlib import pyplot
import ROOT
ROOT.gSystem.Load("$(MEGALIB)/lib/libMEGAlib.so")
# Initialize MEGAlib
G = ROOT.MGlobal()
G.Initialize()
#We can use these to make printing of MEGAlib (and other root classes) easier.
def MegaPrint(self):
try:
return self.Data()
except:
return ":("
def MegaToStringPrint(self):
try:
return self.ToString().Data()
except:
return ":("
def MegaNamePrint(self):
try:
return self.GetName().Data()
except:
return ":("
setattr(ROOT.MString, '__str__', MegaPrint)
setattr(ROOT.MVector, '__str__', MegaToStringPrint)
setattr(ROOT.MSimHT, '__str__', MegaToStringPrint)
setattr(ROOT.MSimIA, '__str__', MegaToStringPrint)
setattr(ROOT.MComptonEvent, '__str__', MegaToStringPrint)
setattr(ROOT.MPairEvent, '__str__', MegaToStringPrint)
setattr(ROOT.MPhysicalEvent, '__str__', MegaToStringPrint)
setattr(ROOT.MDDetector, '__str__', MegaNamePrint)
setattr(ROOT.MDVoxel3D, '__str__', MegaNamePrint)
setattr(ROOT.MDStrip2D, '__str__', MegaNamePrint)
def MegaSimEventToDict(Event, Geometry):
theDict = {}
theHits = []
theDict["ID"] = Event.GetID()
pos1 = Event.GetIAAt(1).GetPosition()
vol = Geometry.GetVolume( pos1 )
theDict["TrueFirstIAVolume"] = vol.GetName().Data()
if vol.IsSensitive():
theDict["TrueFirstIADetector"] = vol.GetDetector().GetName().Data()
else:
theDict["TrueFirstIADetector"] = "passive"
theDict["TrueFirstIAX"] = pos1.GetX()
theDict["TrueFirstIAY"] = pos1.GetY()
theDict["TrueFirstIAZ"] = pos1.GetZ()
#First interaction type
theDict["TrueType"] = Event.GetIAAt(1).GetProcess().Data()
#The ED keyword (Total energy deposit in active material)
ED = 0.0;
for iH in range(0, Event.GetNHTs()):
theHit = Event.GetHTAt(iH)
Ehit = theHit.GetEnergy()
ED += Ehit
detectorType = theHit.GetDetector()
theKey = "NHit_{}".format( detectorType )
theDict[ theKey ] = theDict.get(theKey, 0) + 1
theKey = "EHit_{}".format( detectorType )
theDict[ theKey ] = theDict.get(theKey, 0) + Ehit/1e3
hitDict = {}
hitPos = theHit.GetPosition()
hitDict["ID"] = theDict["ID"]
hitDict["HitX"] = hitPos.GetX()
hitDict["HitY"] = hitPos.GetY()
hitDict["HitZ"] = hitPos.GetZ()
hitDict["HitEnergy"] = Ehit/1e3
hitDict["HitDetector"] = Geometry.GetDetector( theHit.GetPosition() ).GetName().Data()
theHits.append( hitDict )
for iH in range(0, Event.GetNGRs()):
ED += Event.GetGRAt(iH).GetEnergy()
theDict["ED"] = ED
#The EC keyword (Escapes)
EC = 0.0;
for iA in range(0, Event.GetNIAs()):
if (Event.GetIAAt(iA).GetProcess().Data() == "ESCP"):
EC += Event.GetIAAt(iA).GetMotherEnergy()
theDict["EC"] = EC
#Deposits in non-sensitive material
theDict["NS"] = Event.GetEnergyDepositNotSensitiveMaterial()
#incoming gamma-ray energy
theDict["TrueEnergy"] = Event.GetICEnergy()/1e3
#First interaction type
theDict["TrueType"] = Event.GetIAAt(1).GetProcess().Data()
initialPhotonDirection = -1*Event.GetICOrigin()
theDict["TrueTheta"] = initialPhotonDirection.Theta()
theDict["TruePhi"] = initialPhotonDirection.Phi()
if theDict["TrueType"] == "COMP":
theDict["TrueThetaC"] = Event.GetICScatterAngle() #True Compton angle
theDict["TrueThetaE"] = Event.GetICElectronD().Angle(Event.GetICOrigin()) #True electron angle
if theDict["TrueType"] == "PAIR":
theDict["TruePairAngle"] = Event.GetIPPositronDir().Angle(Event.GetIPElectronDir() )
return theDict, theHits
def readSimFile( FileName, GeometryName ):
fileinfo = {}
fileinfo["filename"] = FileName
theEvents = []
theHits = []
#parse some info by hand
if FileName[-2:] == "gz":
import gzip
op = gzip.open
else:
op = open
with op(FileName, mode="rt") as fp:
for line in fp:
fields = line.replace(";", " ").split()
if len(fields) < 1:
continue
keyword = fields[0]
if keyword == "SimulationStartAreaFarField":
fileinfo["Area"] = float(fields[1])
elif keyword == "BeamType" and fields[1] == "FarFieldPointSource":
fileinfo["SourceTheta"] = numpy.deg2rad(float(fields[2]))
fileinfo["SourcePhi"] = numpy.deg2rad(float(fields[3]))
elif keyword == "SpectralType" and fields[1] == "Mono":
fileinfo["SourceEnergy"] = float(fields[2])/1000 #keV to MeV
#first event! We'll use MEGAlib for the rest.
if keyword == "SE":
break
# Use MEGAlib reader for the rest
Geometry = ROOT.MDGeometryQuest()
if Geometry.ScanSetupFile(ROOT.MString(GeometryName)) == True:
print("Geometry " + GeometryName + " loaded!")
else:
print("Unable to load geometry " + GeometryName + " - Aborting!")
quit()
Reader = ROOT.MFileEventsSim(Geometry)
if Reader.Open(ROOT.MString(FileName)) == False:
print("Unable to open file " + FileName + ". Aborting!")
quit()
fileinfo["Area"] = Reader.GetSimulationStartAreaFarField()
while True:
Event = Reader.GetNextEvent()
if not Event:
break
ROOT.SetOwnership(Event, True)
eventDict, hitList = MegaSimEventToDict(Event, Geometry)
theEvents.append( eventDict )
theHits.extend( hitList )
fileinfo["thrownEvents"] = Reader.GetSimulatedEvents()
df = pandas.DataFrame(theEvents)
#cleanup
df.fillna(0, inplace=True)
for b in ["NHit_1", "NHit_2", "NHit_4", "NHit_8"]:
if b in df.columns:
df = df.astype( {b:'int32' } )
df.set_index("ID", inplace = True)
df2 = pandas.DataFrame(theHits)
df2.fillna(0, inplace = True)
try:
df2.set_index("ID", inplace = True)
except:
pass
return df, df2, fileinfo
def MegaTraEventToDict( Event):
theDict = {}
theDict["ID"] = Event.GetId()
theType = Event.GetTypeString().Data()
theDict["RecoType"] = theType
if theType == "Unidentifiable":
theDict["RecoType"] = "bad"
theDict["Subtype"] = " ({})".format(Event.GetBadString().Data() )
if theType == "Compton":
comptonPhotonEnergy = Event.Eg()/1e3
comptonElectronEnergy = Event.Ee()/1e3
totalEnergy = comptonPhotonEnergy+comptonElectronEnergy
theDict["RecoEnergy"] = totalEnergy
theDict["RecoThetaC_Cal"] = Event.Phi()
theDict["RecoThetaE_Cal"] = Event.Epsilon()
theDict["RecoDeltaTheta"] = Event.DeltaTheta()
if Event.HasTrack():
theDict["Subtype"] = " (tracked)"
else:
theDict["Subtype"] = " (untracked)"
originalGammaDirection = -Event.GetOIDirection()
scatteredGammaDirection = Event.C2() - Event.C1()
theDict["RecoThetaC_Track"] = originalGammaDirection.Angle( scatteredGammaDirection )
elif theType == "Pair":
pairEnergy = Event.GetEnergy()/1e3
theDict["RecoEnergy"] = pairEnergy
theDict["RecoTheta"] = Event.GetOrigin().Theta()
theDict["RecoPhi"] = Event.GetOrigin().Phi()
theDict["RecoPairAngle"] = Event.GetOpeningAngle()
theDict["RecoDelAngle"] = Event.GetOrigin().Angle(-Event.GetOIDirection())
return theDict
def readTraFile( FileName ):
theEvents = []
Reader = ROOT.MFileEventsTra()
if Reader.Open(ROOT.MString(FileName)) == False:
print("Unable to open file " + FileName + ". Aborting!")
quit()
while True:
Event = Reader.GetNextEvent()
if not Event:
break
ROOT.SetOwnership(Event, True)
eventDict = MegaTraEventToDict(Event)
theEvents.append( eventDict )
if len(theEvents) > 0:
df = pandas.DataFrame(theEvents)
#cleanup
df["Subtype"] = df["Subtype"].fillna( "" )
df.set_index("ID", inplace = True)
return df
else:
return None
def readOneSetOfSims( geometry, simFileName, traFileName = None ):
print (geometry, simFileName, traFileName)
if traFileName is None:
traFileName = simFileName.replace(".sim", ".tra")
simDfName = simFileName + ".csv"
hitDfName = simFileName.replace(".sim", ".hits.csv" ).replace(".gz", "")
simInfoName = simFileName + ".pkl"
dfName = traFileName + ".csv"
simData, hitData, simInfo = readSimFile(simFileName, geometry)
simData.to_csv( simDfName )
hitData.to_csv( hitDfName )
with open(simInfoName,"wb") as f:
pickle.dump(simInfo,f)
recData = readTraFile(traFileName)
#0 events in file
if recData is None:
return simData, hitData, simInfo, None, None, None
df = simData.join(recData)
df["RecoType"] = df["RecoType"].fillna( "no trigger" )
df["Subtype"] = df["Subtype"].fillna( "" )
df["LongType"] = df.RecoType + df.Subtype
df.to_csv(dfName)
return simData, hitData, simInfo, df
#Usage: Import this file and then call e.g.
#_, _, _, df1 = readOneSetOfSims( "/Users/hfleisc1/amego_software/ComPair/Geometry/AMEGO_Midex/TradeStudies/Tracker/BasePixelTracker/AmegoBase.geo.setup", "/Users/hfleisc1/amego_software/ComPair/simfiles/test_pixel/sim/FarFieldPointSource_100.000MeV_Cos0.8_Phi0.0.inc1.id1.sim", "/Users/hfleisc1/amego_software/ComPair/simfiles/test_pixel/revan/FarFieldPointSource_100.000MeV_Cos0.8_Phi0.0.inc1.id1.tra", )