From d111afee7ce7954fc89638fa53c9f288165dd21e Mon Sep 17 00:00:00 2001
From: Chao Peng <cpeng@anl.gov>
Date: Sat, 17 Jul 2021 23:19:13 -0500
Subject: [PATCH] add support for negative ids

---
 .../imaging_ecal/scripts/get_layerids.py      | 24 +++++++++++--------
 1 file changed, 14 insertions(+), 10 deletions(-)

diff --git a/benchmarks/imaging_ecal/scripts/get_layerids.py b/benchmarks/imaging_ecal/scripts/get_layerids.py
index 1264f532..3c4e0df4 100644
--- a/benchmarks/imaging_ecal/scripts/get_layerids.py
+++ b/benchmarks/imaging_ecal/scripts/get_layerids.py
@@ -21,7 +21,13 @@ class AthenaDecoder:
 
     def get(self, idvals, field):
         start, width = self.fieldsmap[field]
-        return np.bitwise_and(np.right_shift(idvals, start), (1 << width) - 1)
+        if width >= 0:
+            return np.bitwise_and(np.right_shift(idvals, start), (1 << width) - 1)
+        # first bit is sign bit
+        else:
+            width = abs(width) - 1
+            vals = np.bitwise_and(np.right_shift(idvals, start), (1 << width) - 1)
+            return np.where(np.bitwise_and(np.right_shift(idvals, start + width), 1), vals - (1 << width), vals)
 
     @staticmethod
     def getReadouts(path):
@@ -53,11 +59,11 @@ class AthenaDecoder:
         for field_bits in id_str.split(','):
             elements = field_bits.split(':')
             field_name = elements[0]
-            bit_width = abs(int(elements[-1]))
+            bit_width = int(elements[-1])
             if len(elements) == 3:
                 curr_bit = int(elements[1])
             res[field_name] = (curr_bit, bit_width)
-            curr_bit += bit_width
+            curr_bit += abs(bit_width)
         return res
 
 
@@ -113,17 +119,15 @@ if __name__ == '__main__':
     # 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))
-
+    # dd4hep_decoder = description.readout(args.readout).idSpec().decoder()
+    # lindex = dd4hep_decoder.index('x')
+    # get_layer_id = np.vectorize(lambda cid: dd4hep_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']])
-
     # always terminate dd4hep kernel
     # 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']])
+    df.loc[:, 'xID'] = decoder.get(df['cellID'].values, 'x')
+    print(df[['cellID', 'layerID', 'xID', 'position.x', 'position.y', 'position.z', 'energy']])
 
-- 
GitLab