Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
reconstruction_benchmarks
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
EIC
benchmarks
reconstruction_benchmarks
Commits
26f750fc
Commit
26f750fc
authored
3 years ago
by
Chao Peng
Browse files
Options
Downloads
Patches
Plain Diff
add field id decoder
parent
62751498
No related branches found
No related tags found
1 merge request
!128
add benchmark for pion0
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
benchmarks/imaging_ecal/scripts/get_layerids.py
+68
-11
68 additions, 11 deletions
benchmarks/imaging_ecal/scripts/get_layerids.py
with
68 additions
and
11 deletions
benchmarks/imaging_ecal/scripts/get_layerids.py
+
68
−
11
View file @
26f750fc
...
...
@@ -2,13 +2,63 @@
A simple analysis script to extract some basic info of Monte-Carlo hits
'''
import
os
import
DDG4
import
ROOT
import
pandas
as
pd
import
numpy
as
np
import
argparse
from
matplotlib
import
pyplot
as
plt
import
matplotlib.ticker
as
ticker
from
lxml
import
etree
as
ET
class
AthenaDecoder
:
def
__init__
(
self
,
compact
,
readout
):
self
.
readouts
=
self
.
getReadouts
(
compact
)
self
.
changeReadout
(
readout
)
def
changeReadout
(
self
,
readout
):
self
.
fieldsmap
=
self
.
decomposeIDs
(
self
.
readouts
[
readout
])
def
get
(
self
,
idvals
,
field
):
start
,
width
=
self
.
fieldsmap
[
field
]
return
np
.
bitwise_and
(
np
.
right_shift
(
idvals
,
start
),
(
1
<<
width
)
-
1
)
@staticmethod
def
getReadouts
(
path
):
res
=
dict
()
AthenaDecoder
.
__getReadoutsRecur
(
path
,
res
)
return
res
@staticmethod
def
__getReadoutsRecur
(
path
,
res
):
if
not
os
.
path
.
exists
(
path
):
print
(
'
Xml file {} not exist! Ignored it.
'
.
format
(
path
))
return
lccdd
=
ET
.
parse
(
path
).
getroot
()
readouts
=
lccdd
.
find
(
'
readouts
'
)
if
readouts
is
not
None
:
for
readout
in
readouts
.
getchildren
():
ids
=
readout
.
find
(
'
id
'
)
if
ids
is
not
None
:
res
[
readout
.
attrib
[
'
name
'
]]
=
ids
.
text
for
child
in
lccdd
.
getchildren
():
if
child
.
tag
==
'
include
'
:
root_dir
=
os
.
path
.
dirname
(
os
.
path
.
realpath
(
path
))
AthenaDecoder
.
__getReadoutsRecur
(
os
.
path
.
join
(
root_dir
,
child
.
attrib
[
'
ref
'
]),
res
)
@staticmethod
def
decomposeIDs
(
id_str
):
res
=
dict
()
curr_bit
=
0
for
field_bits
in
id_str
.
split
(
'
,
'
):
elements
=
field_bits
.
split
(
'
:
'
)
field_name
=
elements
[
0
]
bit_width
=
abs
(
int
(
elements
[
-
1
]))
if
len
(
elements
)
==
3
:
curr_bit
=
int
(
elements
[
1
])
res
[
field_name
]
=
(
curr_bit
,
bit_width
)
curr_bit
+=
bit_width
return
res
# read from RDataFrame and flatten a given collection, return pandas dataframe
...
...
@@ -50,23 +100,30 @@ if __name__ == '__main__':
help
=
'
Readout name for the hits collection
'
)
args
=
parser
.
parse_args
()
# decoder
decoder
=
AthenaDecoder
(
args
.
compact
,
args
.
readout
)
# get hits
rdf_rec
=
ROOT
.
RDataFrame
(
'
events
'
,
args
.
rec_file
)
df
=
flatten_collection
(
rdf_rec
,
args
.
coll
)
df
.
rename
(
columns
=
{
c
:
c
.
replace
(
args
.
coll
+
'
.
'
,
''
)
for
c
in
df
.
columns
},
inplace
=
True
)
# initialize dd4hep detector
kernel
=
DDG4
.
Kernel
()
description
=
kernel
.
detectorDescription
()
kernel
.
loadGeometry
(
"
file:{}
"
.
format
(
args
.
compact
)
)
decoder
=
description
.
readout
(
args
.
readout
).
idSpec
().
decoder
()
lindex
=
decoder
.
index
(
'
layer
'
)
get_layer_id
=
np
.
vectorize
(
lambda
cid
:
decoder
.
get
(
cid
,
lindex
))
# import DDG4
# kernel = DDG4.Kernel
()
# description = kernel.detectorDescription(
)
# kernel.loadGeometry("file:{}".format(args.compact))
#
decoder = description.readout(args.readout).idSpec().decoder()
#
lindex = decoder.index('layer')
#
get_layer_id = np.vectorize(lambda cid: decoder.get(cid, lindex))
df
.
loc
[:,
'
layerID
'
]
=
get_layer_id
(
df
[
'
cellID
'
].
astype
(
int
).
values
)
print
(
df
[[
'
cellID
'
,
'
layerID
'
,
'
position.x
'
,
'
position.y
'
,
'
position.z
'
,
'
energy
'
]])
#
df.loc[:, 'layerID'] = get_layer_id(df['cellID'].astype(int).values)
#
print(df[['cellID', 'layerID', 'position.x', 'position.y', 'position.z', 'energy']])
# always terminate dd4hep kernel
kernel
.
terminate
()
# kernel.terminate()
# faster way to get layerids
df
.
loc
[:,
'
layerID
'
]
=
decoder
.
get
(
df
[
'
cellID
'
].
values
,
'
layer
'
)
print
(
df
[[
'
cellID
'
,
'
layerID
'
,
'
position.x
'
,
'
position.y
'
,
'
position.z
'
,
'
energy
'
]])
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment