From 659a333f698423fcc0b74d937d616eafd37abd03 Mon Sep 17 00:00:00 2001
From: Archil Durglishvili <archil.durglishvili@cern.ch>
Date: Wed, 11 Dec 2024 16:37:26 +0100
Subject: [PATCH 1/4] update ALLEGRO_o1_v03/run_digi_reco.py for new HCal
 segmentation

---
 .../ALLEGRO_o1_v03/ctest_sim_digi_reco.sh     |   2 +-
 .../ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py   | 112 +++---------------
 2 files changed, 18 insertions(+), 96 deletions(-)

diff --git a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/ctest_sim_digi_reco.sh b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/ctest_sim_digi_reco.sh
index 56446f8..8d0d789 100755
--- a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/ctest_sim_digi_reco.sh
+++ b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/ctest_sim_digi_reco.sh
@@ -19,7 +19,7 @@ if ! test -f ./neighbours_map_ecalB_thetamodulemerged_hcalB_thetaphi.root; then
   wget https://fccsw.web.cern.ch/fccsw/filesForSimDigiReco/ALLEGRO/ALLEGRO_o1_v03/lgbm_calibration-CaloClusters.onnx
   wget https://fccsw.web.cern.ch/fccsw/filesForSimDigiReco/ALLEGRO/ALLEGRO_o1_v03/lgbm_calibration-CaloTopoClusters.onnx
   wget https://fccsw.web.cern.ch/fccsw/filesForSimDigiReco/ALLEGRO/ALLEGRO_o1_v03/neighbours_map_ecalB_thetamodulemerged.root
-  wget https://fccsw.web.cern.ch/fccsw/filesForSimDigiReco/ALLEGRO/ALLEGRO_o1_v03/neighbours_map_ecalB_thetamodulemerged_hcalB_thetaphi.root
+  wget https://fccsw.web.cern.ch/fccsw/filesForSimDigiReco/ALLEGRO/ALLEGRO_o1_v03/neighbours_map_ecalB_thetamodulemerged_hcalB_hcalEndcap_phitheta.root
 fi
 
 # run the RECO step
diff --git a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
index 8285e93..b8d0eb6 100644
--- a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
+++ b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
@@ -113,11 +113,11 @@
 
 
 # Event counter
-from Configurables import EventCounter
-eventCounter = EventCounter("EventCounter",
-                            OutputLevel=INFO,
-                            Frequency=10)
-TopAlg += [eventCounter]
+#from Configurables import EventCounter
+#eventCounter = EventCounter("EventCounter",
+#                            OutputLevel=INFO,
+#                            Frequency=10)
+#TopAlg += [eventCounter]
 # add a message sink service if you want a summary table at the end (not needed..)
 # ExtSvc += ["Gaudi::Monitoring::MessageSvcSink"]
 
@@ -176,15 +176,11 @@
 ecalEndcapReadoutName = "ECalEndcapTurbine"                # endcap, turbine-like (baseline)
 # - HCAL readouts
 if runHCal:
-    hcalBarrelReadoutName = "HCalBarrelReadout"            # barrel, original segmentation (row-phi)
-    hcalBarrelReadoutName2 = "BarHCal_Readout_phitheta"    # barrel, groups together cells of different row within same theta slice
-    hcalEndcapReadoutName = "HCalEndcapReadout"            # endcap, original segmentation
-    hcalEndcapReadoutName2 = "HCalEndcapReadout_phitheta"  # endcap, groups together cells of different row within same theta slice
+    hcalBarrelReadoutName = "HCalBarrelReadout"            # barrel, original segmentation (HCalPhiTheta)
+    hcalEndcapReadoutName = "HCalEndcapReadout"            # endcap, original segmentation (HCalPhiTheta)
 else:
     hcalBarrelReadoutName = ""
-    hcalBarrelReadoutName2 = ""
     hcalEndcapReadoutName = ""
-    hcalEndcapReadoutName2 = ""
 
 # - EM scale calibration (sampling fraction)
 from Configurables import CalibrateInLayersTool
@@ -243,12 +239,6 @@
         detectorName="HCalBarrel",
         OutputLevel=INFO
     )
-    cellPositionHCalBarrelTool2 = CellPositionsHCalPhiThetaSegTool(
-        "CellPositionsHCalBarrel2",
-        readoutName=hcalBarrelReadoutName2,
-        detectorName="HCalBarrel",
-        OutputLevel=INFO
-    )
     cellPositionHCalEndcapTool = CellPositionsHCalPhiThetaSegTool(
         "CellPositionsHCalEndcap",
         readoutName=hcalEndcapReadoutName,
@@ -256,13 +246,6 @@
         numLayersHCalThreeParts=[6, 9, 22],
         OutputLevel=INFO
     )
-    cellPositionHCalEndcapTool2 = CellPositionsHCalPhiThetaSegTool(
-        "CellPositionsHCalEndcap2",
-        readoutName=hcalEndcapReadoutName2,
-        detectorName="HCalThreePartsEndcap",
-        numLayersHCalThreeParts=[6, 9, 22],
-        OutputLevel=INFO
-    )
 
 # - crosstalk tool
 if addCrosstalk:
@@ -431,39 +414,6 @@
                                                       OutputLevel=INFO)
     TopAlg += [createHCalBarrelCells]
 
-    # Compute new cellID of cells based on new readout - removing row information
-    # We use a RedoSegmentation. Using a RewriteBitField with removeIds=["row"],
-    # won't work because there are tiles with same layer/theta/phi but different row
-    # as a consequence there will be multiple cells with same cellID in the output collection
-    # and this will screw up the SW clustering
-
-    # first we create new hits with the readout without the row information
-    # and then merge them into new cells, wihotut applying the calibration again
-    from Configurables import RedoSegmentation
-    rewriteHCalBarrel = RedoSegmentation("ReSegmentationHCalBarrel",
-                                         # old bitfield (readout)
-                                         oldReadoutName=hcalBarrelReadoutName,
-                                         # specify which fields are going to be altered (deleted/rewritten)
-                                         oldSegmentationIds=["row", "theta", "phi"],
-                                         # new bitfield (readout), with new segmentation (theta-phi grid)
-                                         newReadoutName=hcalBarrelReadoutName2,
-                                         OutputLevel=INFO,
-                                         debugPrint=200,
-                                         inhits=hcalBarrelPositionedCellsName,
-                                         outhits="HCalBarrelCellsWithoutRow")
-    TopAlg += [rewriteHCalBarrel]
-
-    hcalBarrelPositionedCellsName2 = hcalBarrelReadoutName2 + "Positioned"
-    createHCalBarrelCells2 = CreatePositionedCaloCells("CreatePositionedHCalBarrelCells2",
-                                                       doCellCalibration=False,
-                                                       positionsTool=cellPositionHCalBarrelTool2,
-                                                       addCellNoise=False,
-                                                       filterCellNoise=False,
-                                                       OutputLevel=INFO,
-                                                       hits=rewriteHCalBarrel.outhits.Path,
-                                                       cells=hcalBarrelPositionedCellsName2)
-    TopAlg += [createHCalBarrelCells2]
-
     # Create cells in HCal endcap
     hcalEndcapPositionedCellsName = hcalEndcapReadoutName + "Positioned"
     createHCalEndcapCells = CreatePositionedCaloCells("CreatePositionedHCalEndcapCells",
@@ -477,39 +427,11 @@
                                                       cells=hcalEndcapPositionedCellsName)
     TopAlg += [createHCalEndcapCells]
 
-    rewriteHCalEndcap = RedoSegmentation("ReSegmentationHCalEndcap",
-                                         # old bitfield (readout)
-                                         oldReadoutName=hcalEndcapReadoutName,
-                                         # specify which fields are going to be altered (deleted/rewritten)
-                                         oldSegmentationIds=["row", "theta", "phi"],
-                                         # new bitfield (readout), with new segmentation (theta-phi grid)
-                                         newReadoutName=hcalEndcapReadoutName2,
-                                         OutputLevel=INFO,
-                                         debugPrint=200,
-                                         inhits=hcalEndcapPositionedCellsName,
-                                         outhits="HCalEndcapCellsWithoutRow")
-    TopAlg += [rewriteHCalEndcap]
-
-    hcalEndcapPositionedCellsName2 = hcalEndcapReadoutName2 + "Positioned"
-    createHCalEndcapCells2 = CreatePositionedCaloCells("CreatePositionedHCalEndcapCells2",
-                                                       doCellCalibration=False,
-                                                       positionsTool=cellPositionHCalEndcapTool2,
-                                                       addCellNoise=False,
-                                                       filterCellNoise=False,
-                                                       OutputLevel=INFO,
-                                                       hits=rewriteHCalEndcap.outhits.Path,
-                                                       cells=hcalEndcapPositionedCellsName2)
-    TopAlg += [createHCalEndcapCells2]
-
 else:
     hcalBarrelPositionedCellsName = "emptyCaloCells"
-    hcalBarrelPositionedCellsName2 = "emptyCaloCells"
     hcalEndcapPositionedCellsName = "emptyCaloCells"
-    hcalEndcapPositionedCellsName2 = "emptyCaloCells"
     cellPositionHCalBarrelTool = None
-    cellPositionHCalBarrelTool2 = None
     cellPositionHCalEndcapTool = None
-    cellPositionHCalEndcapTool2 = None
 
 # Empty cells for parts of calorimeter not implemented yet
 if doSWClustering or doTopoClustering:
@@ -876,14 +798,14 @@ def setupTopoClusters(inputCells,
         CaloClusterInputs = {
             "ecalBarrel": ecalBarrelPositionedCellsName,
             "ecalEndcap": ecalEndcapPositionedCellsName,
-            "hcalBarrel": hcalBarrelPositionedCellsName2,
-            "hcalEndcap": hcalEndcapPositionedCellsName2,
+            "hcalBarrel": hcalBarrelPositionedCellsName,
+            "hcalEndcap": hcalEndcapPositionedCellsName,
         }
         CaloClusterReadouts = {
             "ecalBarrel": ecalBarrelReadoutName,
             "ecalEndcap": ecalEndcapReadoutName,
-            "hcalBarrel": hcalBarrelReadoutName2,
-            "hcalEndcap": hcalEndcapReadoutName2,
+            "hcalBarrel": hcalBarrelReadoutName,
+            "hcalEndcap": hcalEndcapReadoutName,
         }
         setupSWClusters(CaloClusterInputs,
                         CaloClusterReadouts,
@@ -932,22 +854,22 @@ def setupTopoClusters(inputCells,
     if runHCal:
         CaloTopoClusterInputs = {
             "ecalBarrel": ecalBarrelPositionedCellsName,
-            "hcalBarrel": hcalBarrelPositionedCellsName2
+            "hcalBarrel": hcalBarrelPositionedCellsName
         }
         CaloTopoClusterReadouts = {
             "ecalBarrel": ecalBarrelReadoutName,
-            "hcalBarrel": hcalBarrelReadoutName2
+            "hcalBarrel": hcalBarrelReadoutName
         }
         CaloTopoClusterPositioningTools = {
             "ecalBarrel": cellPositionEcalBarrelTool,
-            "hcalBarrel": cellPositionHCalBarrelTool2,
+            "hcalBarrel": cellPositionHCalBarrelTool,
         }
         setupTopoClusters(CaloTopoClusterInputs,
                           CaloTopoClusterReadouts,
                           CaloTopoClusterPositioningTools,
                           "CaloTopoClusters",
                           0.0,
-                          dataFolder + "neighbours_map_ecalB_thetamodulemerged_hcalB_thetaphi.root",
+                          dataFolder + "neighbours_map_ecalB_thetamodulemerged_hcalB_hcalEndcap_phitheta.root",
                           dataFolder + "cellNoise_map_electronicsNoiseLevel_ecalB_thetamodulemerged_hcalB_thetaphi.root",
                           False,
                           False,
@@ -1008,8 +930,8 @@ def setupTopoClusters(inputCells,
     if resegmentECalBarrel:
         io_svc.outputCommands.append("drop %s" % ecalBarrelPositionedCellsName2)
     if runHCal:
-        io_svc.outputCommands.append("drop %s" % hcalBarrelPositionedCellsName2)
-        io_svc.outputCommands.append("drop %s" % hcalEndcapPositionedCellsName2)
+        io_svc.outputCommands.append("drop %s" % hcalBarrelPositionedCellsName)
+        io_svc.outputCommands.append("drop %s" % hcalEndcapPositionedCellsName)
 if not saveClusterCells:
     io_svc.outputCommands.append("drop *Calo*Cluster*Cells*")
 

From 595c87504e2882f44745e046b3615e56346e122e Mon Sep 17 00:00:00 2001
From: Archil Durglishvili <archil.durglishvili@cern.ch>
Date: Wed, 11 Dec 2024 17:52:16 +0100
Subject: [PATCH 2/4] returning back the EventCounter

---
 FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
index b8d0eb6..77b83c3 100644
--- a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
+++ b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
@@ -113,11 +113,11 @@
 
 
 # Event counter
-#from Configurables import EventCounter
-#eventCounter = EventCounter("EventCounter",
-#                            OutputLevel=INFO,
-#                            Frequency=10)
-#TopAlg += [eventCounter]
+from Configurables import EventCounter
+eventCounter = EventCounter("EventCounter",
+                            OutputLevel=INFO,
+                            Frequency=10)
+TopAlg += [eventCounter]
 # add a message sink service if you want a summary table at the end (not needed..)
 # ExtSvc += ["Gaudi::Monitoring::MessageSvcSink"]
 

From 11f7ec6340df779c83dbfa2dadc861d5d26bf05e Mon Sep 17 00:00:00 2001
From: Archil Durglishvili <archil.durglishvili@cern.ch>
Date: Wed, 11 Dec 2024 17:59:17 +0100
Subject: [PATCH 3/4] fixing HCal endcap PositioningTool name for
 topo-clustering

---
 FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
index 77b83c3..334d137 100644
--- a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
+++ b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
@@ -660,7 +660,7 @@ def setupTopoClusters(inputCells,
                                       # positionsEMFwdTool=inputPositioningTools.get('ecalFwd', None),
                                       positionsHCalBarrelTool=inputPositioningTools.get('hcalBarrel', None),
                                       positionsHCalBarrelNoSegTool=None,
-                                      positionsHCalExtBarrelTool=inputPositioningTools.get('hcalExtBarrel', None),
+                                      positionsHCalExtBarrelTool=inputPositioningTools.get('hcalEndcap', None),
                                       # positionsHECTool=inputPositioningTools.get('hcalEndcap', None),
                                       # positionsHFwdTool=inputPositioningTools.get('hcalFwd', None),
                                       noSegmentationHCal=False,

From ddf412fe8d2303b01eb7a1992487aa03682abcbf Mon Sep 17 00:00:00 2001
From: Archil Durglishvili <archil.durglishvili@cern.ch>
Date: Wed, 11 Dec 2024 18:22:40 +0100
Subject: [PATCH 4/4] add topo-clustering for HCal endcap in
 ALLEGRO_o1_v03/run_digi_reco.py

---
 FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
index 334d137..35d6d8a 100644
--- a/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
+++ b/FCCee/FullSim/ALLEGRO/ALLEGRO_o1_v03/run_digi_reco.py
@@ -854,15 +854,18 @@ def setupTopoClusters(inputCells,
     if runHCal:
         CaloTopoClusterInputs = {
             "ecalBarrel": ecalBarrelPositionedCellsName,
-            "hcalBarrel": hcalBarrelPositionedCellsName
+            "hcalBarrel": hcalBarrelPositionedCellsName,
+            "hcalEndcap": hcalEndcapPositionedCellsName,
         }
         CaloTopoClusterReadouts = {
             "ecalBarrel": ecalBarrelReadoutName,
-            "hcalBarrel": hcalBarrelReadoutName
+            "hcalBarrel": hcalBarrelReadoutName,
+            "hcalEndcap": hcalEndcapReadoutName,
         }
         CaloTopoClusterPositioningTools = {
             "ecalBarrel": cellPositionEcalBarrelTool,
             "hcalBarrel": cellPositionHCalBarrelTool,
+            "hcalEndcap": cellPositionHCalEndcapTool,
         }
         setupTopoClusters(CaloTopoClusterInputs,
                           CaloTopoClusterReadouts,