From c37e4b9b70177147398e1f172ff8722570f9dcb6 Mon Sep 17 00:00:00 2001
From: christos <christos@cern.ch>
Date: Tue, 5 Jan 2021 18:02:48 +0100
Subject: [PATCH] Refactor and clean up the method for adding cells from the
 satellites using constaints/boxing. This might be relevant depending on the
 outcome of the discussion on the PC on if we keep this or not.

---
 .../src/egammaSuperClusterBuilder.cxx         | 597 +++++++++---------
 .../src/egammaSuperClusterBuilder.h           |  24 +-
 2 files changed, 320 insertions(+), 301 deletions(-)

diff --git a/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.cxx b/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.cxx
index 78fc6765949..b88d88c2500 100644
--- a/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.cxx
+++ b/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.cxx
@@ -61,13 +61,15 @@ etaphi_range(const CaloDetDescrManager& dd_man,
   constexpr double eps = 0.001;
 
   // Now look in the negative eta direction.
-  const CaloDetDescrElement* elt_l = dd_man.get_element_raw(sampling, eta - elt->deta() - eps, phi);
+  const CaloDetDescrElement* elt_l =
+    dd_man.get_element_raw(sampling, eta - elt->deta() - eps, phi);
   double deta_l = 0; // Eta difference on the low (left) side.
   if (elt_l) {
     deta_l = std::abs(eta - elt_l->eta_raw()) + eps;
   }
   // Now look in the positive eta direction.
-  const CaloDetDescrElement* elt_r = dd_man.get_element_raw(sampling, eta + elt->deta() + eps, phi);
+  const CaloDetDescrElement* elt_r =
+    dd_man.get_element_raw(sampling, eta + elt->deta() + eps, phi);
   double deta_r = 0; // Eta difference on the high (right) side.
   if (elt_r) {
     deta_r = std::abs(eta - elt_r->eta_raw()) + eps;
@@ -80,16 +82,18 @@ etaphi_range(const CaloDetDescrManager& dd_man,
   // Thus we have to look again at the adjacent eta cells, and
   // take the largest variation.
   // Now look in the negative eta direction.
-  elt_l = dd_man.get_element_raw(
-    sampling, eta - elt->deta() - eps, CaloPhiRange::fix(phi - elt->dphi() - eps));
+  elt_l = dd_man.get_element_raw(sampling,
+                                 eta - elt->deta() - eps,
+                                 CaloPhiRange::fix(phi - elt->dphi() - eps));
 
   double dphi_l = 0; // Phi difference on the low-eta () side.
   if (elt_l) {
     dphi_l = std::abs(CaloPhiRange::fix(phi - elt_l->phi_raw())) + eps;
   }
   // Now look in the positive eta direction.
-  elt_r = dd_man.get_element_raw(
-    sampling, eta + elt->deta() + eps, CaloPhiRange::fix(phi - elt->dphi() - eps));
+  elt_r = dd_man.get_element_raw(sampling,
+                                 eta + elt->deta() + eps,
+                                 CaloPhiRange::fix(phi - elt->dphi() - eps));
   double dphi_r = 0; // Phi difference on the positive (down) side.
   if (elt_r) {
     dphi_r = std::abs(CaloPhiRange::fix(phi - elt_r->phi_raw())) + eps;
@@ -114,10 +118,14 @@ egammaSuperClusterBuilder::egammaSuperClusterBuilder(const std::string& name,
   m_searchWindowPhiEndcap = m_searchWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
   m_searchWindowEtaEndcap = m_searchWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
 
-  m_addCellsWindowPhiBarrel = m_addCellsWindowPhiCellsBarrel * s_cellPhiSize * 0.5;
-  m_addCellsWindowEtaBarrel = m_addCellsWindowEtaCellsBarrel * s_cellEtaSize * 0.5;
-  m_addCellsWindowPhiEndcap = m_addCellsWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
-  m_addCellsWindowEtaEndcap = m_addCellsWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
+  m_addCellsWindowPhiBarrel =
+    m_addCellsWindowPhiCellsBarrel * s_cellPhiSize * 0.5;
+  m_addCellsWindowEtaBarrel =
+    m_addCellsWindowEtaCellsBarrel * s_cellEtaSize * 0.5;
+  m_addCellsWindowPhiEndcap =
+    m_addCellsWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
+  m_addCellsWindowEtaEndcap =
+    m_addCellsWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
 
   // the +1 is to account for the offset in the centers of the clusters
   m_extraL0L1PhiSize = m_extraL0L1PhiSizeCells * s_cellPhiSize;
@@ -132,17 +140,24 @@ egammaSuperClusterBuilder::initialize()
   m_searchWindowPhiEndcap = m_searchWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
   m_searchWindowEtaEndcap = m_searchWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
 
-  m_addCellsWindowPhiBarrel = m_addCellsWindowPhiCellsBarrel * s_cellPhiSize * 0.5;
-  m_addCellsWindowEtaBarrel = m_addCellsWindowEtaCellsBarrel * s_cellEtaSize * 0.5;
-  m_addCellsWindowPhiEndcap = m_addCellsWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
-  m_addCellsWindowEtaEndcap = m_addCellsWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
+  m_addCellsWindowPhiBarrel =
+    m_addCellsWindowPhiCellsBarrel * s_cellPhiSize * 0.5;
+  m_addCellsWindowEtaBarrel =
+    m_addCellsWindowEtaCellsBarrel * s_cellEtaSize * 0.5;
+  m_addCellsWindowPhiEndcap =
+    m_addCellsWindowPhiCellsEndcap * s_cellPhiSize * 0.5;
+  m_addCellsWindowEtaEndcap =
+    m_addCellsWindowEtaCellsEndcap * s_cellEtaSize * 0.5;
 
   // the +1 is to account for the offset in the centers of the clusters
   m_extraL0L1PhiSize = m_extraL0L1PhiSizeCells * s_cellPhiSize;
 
-  if (m_addCellsWindowPhiCellsBarrel % 2 == 0 || m_addCellsWindowPhiCellsEndcap % 2 == 0 ||
-      m_addCellsWindowEtaCellsBarrel % 2 == 0 || m_addCellsWindowEtaCellsEndcap % 2 == 0) {
-    ATH_MSG_FATAL("For adding cells relative to the hottest cell to be symmetric, the AddCells "
+  if (m_addCellsWindowPhiCellsBarrel % 2 == 0 ||
+      m_addCellsWindowPhiCellsEndcap % 2 == 0 ||
+      m_addCellsWindowEtaCellsBarrel % 2 == 0 ||
+      m_addCellsWindowEtaCellsEndcap % 2 == 0) {
+    ATH_MSG_FATAL("For adding cells relative to the hottest cell to be "
+                  "symmetric, the AddCells "
                   "window size needs to be odd");
     return StatusCode::FAILURE;
   }
@@ -165,36 +180,43 @@ egammaSuperClusterBuilder::matchesInWindow(const xAOD::CaloCluster* ref,
 {
   // First the case where we are both endcap and barrel, i.e in the crack
   // Check around both measurements of the seed
-  if (ref->hasSampling(CaloSampling::EMB2) && ref->hasSampling(CaloSampling::EME2)) {
+  if (ref->hasSampling(CaloSampling::EMB2) &&
+      ref->hasSampling(CaloSampling::EME2)) {
     float dEta(fabs(ref->eta() - clus->eta()));
     float dPhi(fabs(P4Helpers::deltaPhi(ref->phi(), clus->phi())));
     //
     float dEtaBarrel(fabs(ref->etaSample(CaloSampling::EMB2) - clus->eta()));
-    float dPhiBarrel(fabs(P4Helpers::deltaPhi(ref->phiSample(CaloSampling::EMB2), clus->phi())));
+    float dPhiBarrel(fabs(
+      P4Helpers::deltaPhi(ref->phiSample(CaloSampling::EMB2), clus->phi())));
     //
     float dEtaEndcap(fabs(ref->etaSample(CaloSampling::EME2) - clus->eta()));
-    float dPhiEndcap(fabs(P4Helpers::deltaPhi(ref->phiSample(CaloSampling::EME2), clus->phi())));
+    float dPhiEndcap(fabs(
+      P4Helpers::deltaPhi(ref->phiSample(CaloSampling::EME2), clus->phi())));
     // Matches any in case of split
-    return ((dEta < m_searchWindowEtaBarrel && dPhi < m_searchWindowPhiBarrel) ||
-            (dEta < m_searchWindowEtaEndcap && dPhi < m_searchWindowPhiEndcap) ||
-            (dEtaBarrel < m_searchWindowEtaBarrel && dPhiBarrel < m_searchWindowPhiBarrel) ||
-            (dEtaEndcap < m_searchWindowEtaEndcap && dPhiEndcap < m_searchWindowPhiEndcap));
-  } if (xAOD::EgammaHelpers::isBarrel(clus)) {
+    return (
+      (dEta < m_searchWindowEtaBarrel && dPhi < m_searchWindowPhiBarrel) ||
+      (dEta < m_searchWindowEtaEndcap && dPhi < m_searchWindowPhiEndcap) ||
+      (dEtaBarrel < m_searchWindowEtaBarrel &&
+       dPhiBarrel < m_searchWindowPhiBarrel) ||
+      (dEtaEndcap < m_searchWindowEtaEndcap &&
+       dPhiEndcap < m_searchWindowPhiEndcap));
+  }
+  if (xAOD::EgammaHelpers::isBarrel(clus)) {
     float dEta(fabs(ref->eta() - clus->eta()));
     float dPhi(fabs(P4Helpers::deltaPhi(ref->phi(), clus->phi())));
     return (dEta < m_searchWindowEtaBarrel && dPhi < m_searchWindowPhiBarrel);
   }
-    float dEta(fabs(ref->eta() - clus->eta()));
-    float dPhi(fabs(P4Helpers::deltaPhi(ref->phi(), clus->phi())));
-    return (dEta < m_searchWindowEtaEndcap && dPhi < m_searchWindowPhiEndcap);
-
+  float dEta(fabs(ref->eta() - clus->eta()));
+  float dPhi(fabs(P4Helpers::deltaPhi(ref->phi(), clus->phi())));
+  return (dEta < m_searchWindowEtaEndcap && dPhi < m_searchWindowPhiEndcap);
 }
 
 std::unique_ptr<xAOD::CaloCluster>
-egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
-                                            const std::vector<const xAOD::CaloCluster*>& clusters,
-                                            const CaloDetDescrManager& mgr,
-                                            xAOD::EgammaParameters::EgammaType egType) const
+egammaSuperClusterBuilder::createNewCluster(
+  const EventContext& ctx,
+  const std::vector<const xAOD::CaloCluster*>& clusters,
+  const CaloDetDescrManager& mgr,
+  xAOD::EgammaParameters::EgammaType egType) const
 {
 
   const auto acSize = clusters.size();
@@ -204,7 +226,9 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
   }
 
   std::unique_ptr<xAOD::CaloCluster> newCluster(
-    CaloClusterStoreHelper::makeCluster(clusters.at(0)->getCellLinks()->getCellContainer()));
+    CaloClusterStoreHelper::makeCluster(
+      clusters.at(0)->getCellLinks()->getCellContainer()));
+
   if (!newCluster) {
     ATH_MSG_ERROR("CaloClusterStoreHelper::makeCluster failed.");
     return nullptr;
@@ -214,13 +238,14 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
   // This will be used as the center for restricting the cluster size.
   // In the future can refine (or add sanity checks) to the selection
   CentralPosition cpRef = findCentralPosition(clusters);
-  // these are the same as the reference but in calo frame (after the processing below)
+  // these are the same as the reference but in calo frame (after the processing
+  // below)
   CentralPosition cp0 = cpRef;
-
   // Get the hotest in raw co-ordinates
   if (cp0.emaxB > 0) {
     CaloCell_ID::CaloSample xsample = CaloCell_ID::EMB2;
-    const CaloDetDescrElement* dde = mgr.get_element(xsample, cpRef.etaB, cpRef.phiB);
+    const CaloDetDescrElement* dde =
+      mgr.get_element(xsample, cpRef.etaB, cpRef.phiB);
     if (dde) {
       cp0.etaB = dde->eta_raw();
       cp0.phiB = dde->phi_raw();
@@ -231,7 +256,8 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
   }
   if (cp0.emaxEC > 0) {
     CaloCell_ID::CaloSample xsample = CaloCell_ID::EME2;
-    const CaloDetDescrElement* dde = mgr.get_element(xsample, cpRef.etaEC, cpRef.phiEC);
+    const CaloDetDescrElement* dde =
+      mgr.get_element(xsample, cpRef.etaEC, cpRef.phiEC);
     if (dde) {
       cp0.etaEC = dde->eta_raw();
       cp0.phiEC = dde->phi_raw();
@@ -240,7 +266,6 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
                       << cpRef.etaEC << ", phi = " << cpRef.phiEC);
     }
   }
-
   // Set the eta0/phi0 based on the references, but in raw coordinates
   if (cp0.emaxB >= cp0.emaxEC) {
     newCluster->setEta0(cp0.etaB);
@@ -250,51 +275,19 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
     newCluster->setPhi0(cp0.phiEC);
   }
 
-  // Need a vector of element Links to the constituent Cluster
-  std::vector<ElementLink<xAOD::CaloClusterContainer>> constituentLinks;
-  static const SG::AuxElement::Accessor<ElementLink<xAOD::CaloClusterContainer>> sisterCluster(
-    "SisterCluster");
-
-  // Now add the cluster cells (other than L1 and PS)
-  for (size_t i = 0; i < acSize; i++) {
-    // Add te EM cells of the accumulated to the cluster
-    if (addEMCellsToCluster(newCluster.get(), clusters[i], cp0).isFailure()) {
-      ATH_MSG_DEBUG("There was problem adding the topocluster cells to the the cluster:  "
-                    "potentially no L2 or L3 cells in cluster");
-      return nullptr;
-    }
-    // Set the element Link to the constitents
-    if (sisterCluster.isAvailable(*clusters[i])) {
-      constituentLinks.push_back(sisterCluster(*clusters[i]));
-    } else {
-      ATH_MSG_WARNING("No sister Link available");
-    }
-  }
-
-  // Now calculate the cluster size; use that for restricting the L1 cells
-  PhiSize phiSize = findPhiSize(cp0, newCluster.get());
-
-  // now add L1 cells
-  for (size_t i = 0; i < acSize; i++) {
-    // Add te EM cells of the accumulated to the cluster
-    if (addL0L1EMCellsToCluster(newCluster.get(), clusters[i], cp0, phiSize).isFailure()) {
-      ATH_MSG_WARNING(
-        "There was problem adding the topocluster PS and L1 cells to the the cluster");
-      return nullptr;
-    }
+  // Actually fill the cluster here
+  if (fillClusterConstrained(*newCluster, clusters, cp0).isFailure()) {
+    ATH_MSG_DEBUG(
+      "There was problem adding the topocluster cells to the the cluster:  "
+      "potentially no L2 or L3 cells in cluster");
+    return nullptr;
   }
-
   // Apply SW-style summation of TileGap3 cells (if necessary).
-  if (addTileGap3CellsinWindow(newCluster.get(), mgr).isFailure()) {
-    ATH_MSG_ERROR("Problem with the input cluster when running AddTileGap3CellsinWindow?");
+  if (addTileGap3CellsinWindow(*newCluster, mgr).isFailure()) {
+    ATH_MSG_ERROR(
+      "Problem with the input cluster when running AddTileGap3CellsinWindow?");
     return nullptr;
   }
-
-  // Set the link from the super cluster to the constituents (accumulated) clusters used.
-  static const SG::AuxElement::Accessor<std::vector<ElementLink<xAOD::CaloClusterContainer>>>
-    caloClusterLinks("constituentClusterLinks");
-  caloClusterLinks(*newCluster) = constituentLinks;
-
   /// Calculate the kinematics of the new cluster, after all cells are added
   CaloClusterKineHelper::calculateKine(newCluster.get(), true, true);
 
@@ -305,7 +298,8 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
 
   // Check to see if cluster pases basic requirements. If not, kill it.
   if (!m_egammaCheckEnergyDepositTool.empty() &&
-      !m_egammaCheckEnergyDepositTool->checkFractioninSamplingCluster(newCluster.get())) {
+      !m_egammaCheckEnergyDepositTool->checkFractioninSamplingCluster(
+        newCluster.get())) {
     ATH_MSG_DEBUG("Cluster failed sample check: dont make ROI");
     return nullptr;
   }
@@ -315,167 +309,227 @@ egammaSuperClusterBuilder::createNewCluster(const EventContext& ctx,
     ATH_MSG_WARNING("There was problem calibrating the object");
     return nullptr;
   }
+
+  // EDM vector to constituent clusters
+  std::vector<ElementLink<xAOD::CaloClusterContainer>> constituentLinks;
+  static const SG::AuxElement::Accessor<ElementLink<xAOD::CaloClusterContainer>>
+    sisterCluster("SisterCluster");
+  for (size_t i = 0; i < acSize; i++) {
+    // Set the element Link to the constitents
+    if (sisterCluster.isAvailable(*clusters[i])) {
+      constituentLinks.push_back(sisterCluster(*clusters[i]));
+    } else {
+      ATH_MSG_WARNING("No sister Link available");
+    }
+  }
+  // Set the link from the super cluster to the constituents (accumulated)
+  // clusters used.
+  static const SG::AuxElement::Accessor<
+    std::vector<ElementLink<xAOD::CaloClusterContainer>>>
+    caloClusterLinks("constituentClusterLinks");
+  caloClusterLinks(*newCluster) = constituentLinks;
+
   // return the new cluster
   return newCluster;
 }
 
 StatusCode
-egammaSuperClusterBuilder::addEMCellsToCluster(
-  xAOD::CaloCluster* newCluster,
-  const xAOD::CaloCluster* ref,
+egammaSuperClusterBuilder::fillClusterConstrained(
+  xAOD::CaloCluster& tofill,
+  const std::vector<const xAOD::CaloCluster*>& clusters,
   const egammaSuperClusterBuilder::CentralPosition& cp0) const
 {
-  if (!newCluster || !ref) {
-    ATH_MSG_ERROR("Invalid input in AddEMCellsToCluster");
-    return StatusCode::FAILURE;
-  }
-  //
-  xAOD::CaloCluster::const_cell_iterator cell_itr = ref->begin();
-  xAOD::CaloCluster::const_cell_iterator cell_end = ref->end();
-  // Loop over cells
-  for (; cell_itr != cell_end; ++cell_itr) {
-
-    // sanity check on the cell
-    const CaloCell* cell = *cell_itr;
-    if (!cell) {
-      continue;
-    }
+  /*
+   * Fill super cluster constraining its size
+   * in eta,phi around the overall hottest cell
+   * and the L2 size
+   */
+
+  // Loop for L2/L3
+  for (const xAOD::CaloCluster* tocheck : clusters) {
+    xAOD::CaloCluster::const_cell_iterator cell_itr = tocheck->begin();
+    xAOD::CaloCluster::const_cell_iterator cell_end = tocheck->end();
+    // Loop over cells
+    for (; cell_itr != cell_end; ++cell_itr) {
+      // sanity check on the cell
+      const CaloCell* cell = *cell_itr;
+      if (!cell) {
+        continue;
+      }
+      const CaloDetDescrElement* dde = cell->caloDDE();
+      if (!dde) {
+        continue;
+      }
 
-    bool addCell = false;
+      bool addCell = false;
 
-    // lets remove from consideration if outside the range
-    if (cp0.emaxB > 0) { // has cells in the barrel
-      if (fabs(cp0.etaB - cell->caloDDE()->eta_raw()) < m_addCellsWindowEtaBarrel &&
-          fabs(P4Helpers::deltaPhi(cp0.phiB, cell->caloDDE()->phi_raw())) <
-            m_addCellsWindowPhiBarrel) {
-        addCell = true;
+      // lets remove from consideration if outside the range
+      if (cp0.emaxB > 0) { // has cells in the barrel
+        if (fabs(cp0.etaB - dde->eta_raw()) < m_addCellsWindowEtaBarrel &&
+            fabs(P4Helpers::deltaPhi(cp0.phiB, dde->phi_raw())) <
+              m_addCellsWindowPhiBarrel) {
+          addCell = true;
+        }
       }
-    }
-    if (cp0.emaxEC > 0) { // has cells in the endcap
-      if (fabs(cp0.etaEC - cell->caloDDE()->eta_raw()) < m_addCellsWindowEtaEndcap &&
-          fabs(P4Helpers::deltaPhi(cp0.phiEC, cell->caloDDE()->phi_raw())) <
-            m_addCellsWindowPhiEndcap) {
-        addCell = true;
+      if (cp0.emaxEC > 0) { // has cells in the endcap
+        if (fabs(cp0.etaEC - dde->eta_raw()) < m_addCellsWindowEtaEndcap &&
+            fabs(P4Helpers::deltaPhi(cp0.phiEC, dde->phi_raw())) <
+              m_addCellsWindowPhiEndcap) {
+          addCell = true;
+        }
       }
-    }
-
-    if (!addCell)
-      continue;
 
-    const CaloDetDescrElement* dde = cell->caloDDE();
-    if (!dde) {
-      continue;
-    }
+      if (!addCell) {
+        continue;
+      }
 
-    auto sampling = dde->getSampling();
+      auto sampling = dde->getSampling();
 
-    // skip if sampling is L1 or presample, since that is a separate step
-    if (CaloCell_ID::EMB1 == sampling || CaloCell_ID::EME1 == sampling ||
-        CaloCell_ID::PreSamplerB == sampling || CaloCell_ID::PreSamplerE == sampling) {
-      continue;
-    }
+      // skip if sampling is L1 or presample, since that is a separate step
+      if (CaloCell_ID::EMB1 == sampling || CaloCell_ID::EME1 == sampling ||
+          CaloCell_ID::PreSamplerB == sampling ||
+          CaloCell_ID::PreSamplerE == sampling) {
+        continue;
+      }
 
-    // For the cells we have not skipped either because TileGap, bounds, or L1
-    if (dde->getSubCalo() == CaloCell_ID::LAREM) {
-      // Avoid summing inner wheel Endcap cells
-      if (!(dde->is_lar_em_endcap_inner())) {
-        newCluster->addCell(cell_itr.index(), cell_itr.weight());
+      // For the cells we have not skipped either because TileGap, bounds, or L1
+      if (dde->getSubCalo() == CaloCell_ID::LAREM) {
+        // Avoid summing inner wheel Endcap cells
+        if (!(dde->is_lar_em_endcap_inner())) {
+          tofill.addCell(cell_itr.index(), cell_itr.weight());
+        }
       }
-    }
-  } // Loop over cells
-  if (newCluster->size() == 0) {
-    return StatusCode::FAILURE;
-  }
-  return StatusCode::SUCCESS;
-}
+    } // Loop over cells for L2/L3
+  }   // Loop over clusters for L2/L3
 
-StatusCode
-egammaSuperClusterBuilder::addL0L1EMCellsToCluster(
-  xAOD::CaloCluster* newCluster,
-  const xAOD::CaloCluster* ref,
-  const egammaSuperClusterBuilder::CentralPosition& cp0,
-  const egammaSuperClusterBuilder::PhiSize& phiSize) const
-{
-  if (!newCluster || !ref) {
-    ATH_MSG_ERROR("Invalid input in addL0L1EMCellsToCluster");
+  // We should have a size here
+  if (tofill.size() == 0) {
     return StatusCode::FAILURE;
   }
-
+  // Now calculate the cluster size; use that for restricting the L1 cells
+  PhiSize phiSize = findPhiSize(cp0, tofill);
   float phiPlusB = cp0.phiB + phiSize.plusB + m_extraL0L1PhiSize;
   float phiMinusB = cp0.phiB - phiSize.minusB - m_extraL0L1PhiSize;
-
   ATH_MSG_DEBUG("barrel phi range = " << phiMinusB << " to " << phiPlusB);
-
   float phiPlusEC = cp0.phiEC + phiSize.plusEC + m_extraL0L1PhiSize;
   float phiMinusEC = cp0.phiEC - phiSize.minusEC - m_extraL0L1PhiSize;
-
   ATH_MSG_DEBUG("endcap phi range = " << phiMinusEC << " to " << phiPlusEC);
 
-  //
-  xAOD::CaloCluster::const_cell_iterator cell_itr = ref->begin();
-  xAOD::CaloCluster::const_cell_iterator cell_end = ref->end();
-  // Loop over cells
-  for (; cell_itr != cell_end; ++cell_itr) {
+  // Loop for L0/L1
+  for (const xAOD::CaloCluster* tocheck : clusters) {
+    xAOD::CaloCluster::const_cell_iterator cell_itr = tocheck->begin();
+    xAOD::CaloCluster::const_cell_iterator cell_end = tocheck->end();
+    // Loop over cells
+    for (; cell_itr != cell_end; ++cell_itr) {
+      // sanity check on the cell
+      const CaloCell* cell = *cell_itr;
+      if (!cell) {
+        continue;
+      }
+      const CaloDetDescrElement* dde = cell->caloDDE();
+      if (!dde) {
+        continue;
+      }
+      bool addCell = false;
+      // lets remove from consideration if outside the range
+      if (cp0.emaxB > 0) { // has cells in the barrel
+        if (fabs(cp0.etaB - dde->eta_raw()) < m_addCellsWindowEtaBarrel &&
+            fabs(P4Helpers::deltaPhi(cp0.phiB, cell->caloDDE()->phi_raw())) <
+              m_addCellsWindowPhiBarrel) {
+          addCell = true;
+        }
+      }
+      if (cp0.emaxEC > 0) { // has cells in the endcap
+        if (fabs(cp0.etaEC - dde->eta_raw()) < m_addCellsWindowEtaEndcap &&
+            fabs(P4Helpers::deltaPhi(cp0.phiEC, cell->caloDDE()->phi_raw())) <
+              m_addCellsWindowPhiEndcap) {
+          addCell = true;
+        }
+      }
+      if (!addCell) {
+        continue;
+      }
+      auto sampling = dde->getSampling();
+
+      // only deal with L1 or PS
+      if (CaloCell_ID::EMB1 == sampling ||
+          CaloCell_ID::PreSamplerB == sampling) {
+        double cell_phi = proxim(dde->phi_raw(), cp0.phiB);
+        bool inRange = cell_phi > phiMinusB && cell_phi < phiPlusB;
+        ATH_MSG_DEBUG("Found PS or L1 cell with phi = "
+                      << cell_phi << "; inRange = " << inRange);
+        if (inRange) {
+          tofill.addCell(cell_itr.index(), cell_itr.weight());
+        }
+      }
+      if (CaloCell_ID::EME1 == sampling ||
+          CaloCell_ID::PreSamplerE == sampling) {
+        double cell_phi = proxim(dde->phi_raw(), cp0.phiEC);
+        bool inRange = cell_phi > phiMinusEC && cell_phi < phiPlusEC;
+        ATH_MSG_DEBUG("Found PS or L1 cell with phi = "
+                      << cell_phi << "; inRange = " << inRange);
+        if (inRange) {
+          tofill.addCell(cell_itr.index(), cell_itr.weight());
+        }
+      }
+    } // Cell Loop for L0/L1
+  }   // Cluster loop for L0/L1
+  return StatusCode::SUCCESS;
+}
 
-    // sanity check on the cell
-    const CaloCell* cell = *cell_itr;
-    if (!cell) {
-      continue;
-    }
+StatusCode
+egammaSuperClusterBuilder::addTileGap3CellsinWindow(
+  xAOD::CaloCluster& tofill,
+  const CaloDetDescrManager& mgr) const
+{
 
-    bool addCell = false;
+  constexpr double searchWindowEta = 0.2;
+  constexpr double searchWindowPhi = 2 * M_PI / 64.0 + M_PI / 64; // ~ 0.15 rad
+  std::vector<const CaloCell*> cells;
+  cells.reserve(16);
+  const CaloCellContainer* inputcells =
+    tofill.getCellLinks()->getCellContainer();
 
-    // lets remove from consideration if outside the range
-    if (cp0.emaxB > 0) { // has cells in the barrel
-      if (fabs(cp0.etaB - cell->caloDDE()->eta_raw()) < m_addCellsWindowEtaBarrel &&
-          fabs(P4Helpers::deltaPhi(cp0.phiB, cell->caloDDE()->phi_raw())) <
-            m_addCellsWindowPhiBarrel) {
-        addCell = true;
-      }
-    }
-    if (cp0.emaxEC > 0) { // has cells in the endcap
-      if (fabs(cp0.etaEC - cell->caloDDE()->eta_raw()) < m_addCellsWindowEtaEndcap &&
-          fabs(P4Helpers::deltaPhi(cp0.phiEC, cell->caloDDE()->phi_raw())) <
-            m_addCellsWindowPhiEndcap) {
-        addCell = true;
-      }
-    }
+  if (!inputcells) {
+    ATH_MSG_ERROR("No cell container in addRemainingCellsToCluster?");
+    return StatusCode::FAILURE;
+  }
 
-    if (!addCell)
-      continue;
-    const CaloDetDescrElement* dde = cell->caloDDE();
-    if (!dde) {
-      continue;
-    }
+  CaloCellList myList(inputcells);
 
-    auto sampling = dde->getSampling();
+  const std::vector<CaloSampling::CaloSample> samples = {
+    CaloSampling::TileGap3
+  };
+  for (auto samp : samples) {
+    myList.select(mgr,
+                  tofill.eta0(),
+                  tofill.phi0(),
+                  searchWindowEta,
+                  searchWindowPhi,
+                  samp);
+    cells.insert(cells.end(), myList.begin(), myList.end());
+  }
 
-    // only deal with L1 or PS
-    if (CaloCell_ID::EMB1 == sampling || CaloCell_ID::PreSamplerB == sampling) {
-      double cell_phi = proxim(cell->caloDDE()->phi_raw(), cp0.phiB);
-      bool inRange = cell_phi > phiMinusB && cell_phi < phiPlusB;
-      ATH_MSG_DEBUG("Found PS or L1 cell with phi = " << cell_phi << "; inRange = " << inRange);
-      if (inRange) {
-        newCluster->addCell(cell_itr.index(), cell_itr.weight());
-      }
+  for (const auto* cell : cells) {
+    if (!cell || !cell->caloDDE()) {
+      continue;
     }
-    if (CaloCell_ID::EME1 == sampling || CaloCell_ID::PreSamplerE == sampling) {
-      double cell_phi = proxim(cell->caloDDE()->phi_raw(), cp0.phiEC);
-      bool inRange = cell_phi > phiMinusEC && cell_phi < phiPlusEC;
-      ATH_MSG_DEBUG("Found PS or L1 cell with phi = " << cell_phi << "; inRange = " << inRange);
-      if (inRange) {
-        newCluster->addCell(cell_itr.index(), cell_itr.weight());
-      }
+    if ((CaloCell_ID::TileGap3 == cell->caloDDE()->getSampling()) &&
+        (std::abs(cell->caloDDE()->eta_raw()) > 1.4 &&
+         std::abs(cell->caloDDE()->eta_raw()) < 1.6)) {
+      int index = inputcells->findIndex(cell->caloDDE()->calo_hash());
+      tofill.addCell(index, 1.);
     }
   }
   return StatusCode::SUCCESS;
 }
 
 StatusCode
-egammaSuperClusterBuilder::calibrateCluster(const EventContext& ctx,
-                                            xAOD::CaloCluster* newCluster,
-                                            const CaloDetDescrManager& mgr,
-                                            const xAOD::EgammaParameters::EgammaType egType) const
+egammaSuperClusterBuilder::calibrateCluster(
+  const EventContext& ctx,
+  xAOD::CaloCluster* newCluster,
+  const CaloDetDescrManager& mgr,
+  const xAOD::EgammaParameters::EgammaType egType) const
 {
 
   ATH_CHECK(refineEta1Position(newCluster, mgr));
@@ -491,33 +545,35 @@ egammaSuperClusterBuilder::calibrateCluster(const EventContext& ctx,
   newCluster->setRawPhi(newCluster->phi());
   //
   ATH_CHECK(fillPositionsInCalo(newCluster, mgr));
-  // At this point we do not have the final tracks vertices attached on the cluster/ new egamma Rec
-  // Object. So We will need at the end to do the final update in the EMClusterTool For now apply
-  // just cluster info only calibration.
   ATH_CHECK(m_MVACalibSvc->execute(*newCluster, egType));
 
   return StatusCode::SUCCESS;
 }
-// ==========================================================================
+
 StatusCode
-egammaSuperClusterBuilder::fillPositionsInCalo(xAOD::CaloCluster* cluster,
-                                               const CaloDetDescrManager& mgr) const
+egammaSuperClusterBuilder::fillPositionsInCalo(
+  xAOD::CaloCluster* cluster,
+  const CaloDetDescrManager& mgr) const
 {
   const bool isBarrel = xAOD::EgammaHelpers::isBarrel(cluster);
-  CaloCell_ID::CaloSample sample = isBarrel ? CaloCell_ID::EMB2 : CaloCell_ID::EME2;
+  CaloCell_ID::CaloSample sample =
+    isBarrel ? CaloCell_ID::EMB2 : CaloCell_ID::EME2;
   // eta and phi of the cluster in the calorimeter frame
   double eta;
   double phi;
-  m_caloCellDetPos.getDetPosition(mgr, sample, cluster->eta(), cluster->phi(), eta, phi);
+  m_caloCellDetPos.getDetPosition(
+    mgr, sample, cluster->eta(), cluster->phi(), eta, phi);
   cluster->insertMoment(xAOD::CaloCluster::ETACALOFRAME, eta);
   cluster->insertMoment(xAOD::CaloCluster::PHICALOFRAME, phi);
   //  eta in the second sampling
-  m_caloCellDetPos.getDetPosition(mgr, sample, cluster->etaBE(2), cluster->phiBE(2), eta, phi);
+  m_caloCellDetPos.getDetPosition(
+    mgr, sample, cluster->etaBE(2), cluster->phiBE(2), eta, phi);
   cluster->insertMoment(xAOD::CaloCluster::ETA2CALOFRAME, eta);
   cluster->insertMoment(xAOD::CaloCluster::PHI2CALOFRAME, phi);
   //  eta in the first sampling
   sample = isBarrel ? CaloCell_ID::EMB1 : CaloCell_ID::EME1;
-  m_caloCellDetPos.getDetPosition(mgr, sample, cluster->etaBE(1), cluster->phiBE(1), eta, phi);
+  m_caloCellDetPos.getDetPosition(
+    mgr, sample, cluster->etaBE(1), cluster->phiBE(1), eta, phi);
   cluster->insertMoment(xAOD::CaloCluster::ETA1CALOFRAME, eta);
   cluster->insertMoment(xAOD::CaloCluster::PHI1CALOFRAME, phi);
 
@@ -525,12 +581,14 @@ egammaSuperClusterBuilder::fillPositionsInCalo(xAOD::CaloCluster* cluster,
 }
 
 StatusCode
-egammaSuperClusterBuilder::refineEta1Position(xAOD::CaloCluster* cluster,
-                                              const CaloDetDescrManager& mgr) const
+egammaSuperClusterBuilder::refineEta1Position(
+  xAOD::CaloCluster* cluster,
+  const CaloDetDescrManager& mgr) const
 {
 
   // This only makes sense if we have cells there
-  if (!cluster->hasSampling(CaloSampling::EMB1) && !cluster->hasSampling(CaloSampling::EME1)) {
+  if (!cluster->hasSampling(CaloSampling::EMB1) &&
+      !cluster->hasSampling(CaloSampling::EME1)) {
     ATH_MSG_DEBUG("No  layer sampling - skipping refine eta ");
     return StatusCode::SUCCESS;
   }
@@ -546,18 +604,21 @@ egammaSuperClusterBuilder::refineEta1Position(xAOD::CaloCluster* cluster,
 }
 
 StatusCode
-egammaSuperClusterBuilder::makeCorrection1(xAOD::CaloCluster* cluster,
-                                           const CaloDetDescrManager& mgr,
-                                           const CaloSampling::CaloSample sample) const
+egammaSuperClusterBuilder::makeCorrection1(
+  xAOD::CaloCluster* cluster,
+  const CaloDetDescrManager& mgr,
+  const CaloSampling::CaloSample sample) const
 {
 
   // Protections.
   ATH_MSG_DEBUG("Hottest cell in layer 1 ATLAS co-ordinates (eta,phi): ("
-                << cluster->etamax(sample) << " , " << cluster->phimax(sample) << ")");
+                << cluster->etamax(sample) << " , " << cluster->phimax(sample)
+                << ")");
   if (cluster->etamax(sample) == -999. || cluster->phimax(sample) == -999.) {
     return StatusCode::SUCCESS;
   }
-  if (fabs(cluster->etamax(sample)) < 1E-6 && fabs(cluster->phimax(sample)) < 1E-6) {
+  if (fabs(cluster->etamax(sample)) < 1E-6 &&
+      fabs(cluster->phimax(sample)) < 1E-6) {
     return StatusCode::SUCCESS;
   }
   // Get the hotest in raw co-ordinates
@@ -569,16 +630,17 @@ egammaSuperClusterBuilder::makeCorrection1(xAOD::CaloCluster* cluster,
   const CaloDetDescrElement* dde =
     mgr.get_element(xsample, cluster->etamax(sample), cluster->phimax(sample));
   if (!dde) {
-    ATH_MSG_WARNING("Couldn't get CaloDetDescrElement from mgr for: cluster->etamax(sample): "
-                    << cluster->etamax(sample) << " cluster->phimax(sample): "
-                    << cluster->phimax(sample) << " will not refine the position in layer1");
+    ATH_MSG_WARNING(
+      "Couldn't get CaloDetDescrElement from mgr for: cluster->etamax(sample): "
+      << cluster->etamax(sample) << " cluster->phimax(sample): "
+      << cluster->phimax(sample) << " will not refine the position in layer1");
     return StatusCode::SUCCESS;
   }
   //
   double etamax = dde->eta_raw();
   double phimax = dde->phi_raw();
-  ATH_MSG_DEBUG("Hottest cell in layer 1 Calo co-ordinates (eta,phi): (" << etamax << " , "
-                                                                         << phimax << ")");
+  ATH_MSG_DEBUG("Hottest cell in layer 1 Calo co-ordinates (eta,phi): ("
+                << etamax << " , " << phimax << ")");
   //
   // now Locate the +-1 range
   double detastr(-999);
@@ -589,8 +651,14 @@ egammaSuperClusterBuilder::makeCorrection1(xAOD::CaloCluster* cluster,
   // Given the range refine the position employing the smaller window
   if (detastr > 0 && dphistr > 0) {
     CaloLayerCalculator helper;
-    const auto *const cellLink = cluster->getCellLinks();
-    helper.fill(cellLink->begin(), cellLink->end(), etamax, phimax, detastr, dphistr, sample);
+    const auto* const cellLink = cluster->getCellLinks();
+    helper.fill(cellLink->begin(),
+                cellLink->end(),
+                etamax,
+                phimax,
+                detastr,
+                dphistr,
+                sample);
 
     // Here is where we (re-)fill the eta in the 1st sampling
     if (helper.etam() != -999.) {
@@ -602,57 +670,13 @@ egammaSuperClusterBuilder::makeCorrection1(xAOD::CaloCluster* cluster,
   return StatusCode::SUCCESS;
 }
 
-StatusCode
-egammaSuperClusterBuilder::addTileGap3CellsinWindow(xAOD::CaloCluster* myCluster,
-                                                    const CaloDetDescrManager& mgr) const
-{
-
-  if (!myCluster) {
-    ATH_MSG_ERROR("Invalid input in addRemainingCellsToCluster");
-    return StatusCode::FAILURE;
-  }
-
-  static const double searchWindowEta = 0.2;
-  static const double searchWindowPhi = 2 * M_PI / 64.0 + M_PI / 64; // ~ 0.15 rad
-  std::vector<const CaloCell*> cells;
-  cells.reserve(16);
-  const CaloCellContainer* inputcells = myCluster->getCellLinks()->getCellContainer();
-
-  if (!inputcells) {
-    ATH_MSG_ERROR("No cell container in addRemainingCellsToCluster?");
-    return StatusCode::FAILURE;
-  }
-
-  CaloCellList myList(inputcells);
-
-  const std::vector<CaloSampling::CaloSample> samples = { CaloSampling::TileGap3 };
-  for (auto samp : samples) {
-    myList.select(
-      mgr, myCluster->eta0(), myCluster->phi0(), searchWindowEta, searchWindowPhi, samp);
-    cells.insert(cells.end(), myList.begin(), myList.end());
-  }
-
-  for (const auto *cell : cells) {
-    if (!cell || !cell->caloDDE()) {
-      continue;
-    }
-    if ((CaloCell_ID::TileGap3 == cell->caloDDE()->getSampling()) &&
-        (std::abs(cell->caloDDE()->eta_raw()) > 1.4 &&
-         std::abs(cell->caloDDE()->eta_raw()) < 1.6)) {
-      int index = inputcells->findIndex(cell->caloDDE()->calo_hash());
-      myCluster->addCell(index, 1.);
-    }
-  }
-  return StatusCode::SUCCESS;
-}
-
 egammaSuperClusterBuilder::CentralPosition
 egammaSuperClusterBuilder::findCentralPosition(
   const std::vector<const xAOD::CaloCluster*>& clusters) const
 {
 
   CentralPosition cp;
-  for (const auto *cluster : clusters) {
+  for (const auto* cluster : clusters) {
     if (cluster->hasSampling(CaloSampling::EMB2)) {
       float thisEmax = cluster->energy_max(CaloSampling::EMB2);
       if (thisEmax > cp.emaxB) {
@@ -674,15 +698,14 @@ egammaSuperClusterBuilder::findCentralPosition(
 }
 
 egammaSuperClusterBuilder::PhiSize
-egammaSuperClusterBuilder::findPhiSize(const egammaSuperClusterBuilder::CentralPosition& cp0,
-                                       const xAOD::CaloCluster* cluster) const
+egammaSuperClusterBuilder::findPhiSize(
+  const egammaSuperClusterBuilder::CentralPosition& cp0,
+  const xAOD::CaloCluster& cluster) const
 {
 
   PhiSize phiSize;
-
-  auto cell_itr = cluster->cell_cbegin();
-  auto cell_end = cluster->cell_cend();
-
+  auto cell_itr = cluster.cell_cbegin();
+  auto cell_end = cluster.cell_cend();
   for (; cell_itr != cell_end; ++cell_itr) {
     const CaloCell* cell = *cell_itr;
     if (!cell) {
@@ -696,7 +719,7 @@ egammaSuperClusterBuilder::findPhiSize(const egammaSuperClusterBuilder::CentralP
 
     if (CaloCell_ID::EMB2 == dde->getSampling()) {
       const float phi0 = cp0.phiB;
-      double cell_phi = proxim(cell->caloDDE()->phi_raw(), phi0);
+      double cell_phi = proxim(dde->phi_raw(), phi0);
       if (cell_phi > phi0) {
         auto diff = cell_phi - phi0;
         if (diff > phiSize.plusB) {
@@ -710,7 +733,7 @@ egammaSuperClusterBuilder::findPhiSize(const egammaSuperClusterBuilder::CentralP
       }
     } else if (CaloCell_ID::EME2 == dde->getSampling()) {
       const float phi0 = cp0.phiEC;
-      double cell_phi = proxim(cell->caloDDE()->phi_raw(), phi0);
+      double cell_phi = proxim(dde->phi_raw(), phi0);
       if (cell_phi > phi0) {
         auto diff = cell_phi - phi0;
         if (diff > phiSize.plusEC) {
@@ -726,19 +749,23 @@ egammaSuperClusterBuilder::findPhiSize(const egammaSuperClusterBuilder::CentralP
   }
   // some safety checks
   if (phiSize.plusB > 1.0) {
-    ATH_MSG_WARNING("phiSizePlusB is large: " << phiSize.plusB << ", capping at 1.0");
+    ATH_MSG_WARNING("phiSizePlusB is large: " << phiSize.plusB
+                                              << ", capping at 1.0");
     phiSize.plusB = 1.0;
   }
   if (phiSize.plusEC > 1.0) {
-    ATH_MSG_WARNING("phiSizePlusEC is large: " << phiSize.plusEC << ", capping at 1.0");
+    ATH_MSG_WARNING("phiSizePlusEC is large: " << phiSize.plusEC
+                                               << ", capping at 1.0");
     phiSize.plusEC = 1.0;
   }
   if (phiSize.minusB > 1.0) {
-    ATH_MSG_WARNING("phiSizeMinusB is large: " << phiSize.minusB << ", capping at 1.0");
+    ATH_MSG_WARNING("phiSizeMinusB is large: " << phiSize.minusB
+                                               << ", capping at 1.0");
     phiSize.minusB = 1.0;
   }
   if (phiSize.minusEC > 1.0) {
-    ATH_MSG_WARNING("phiSizeMinusEC is large: " << phiSize.minusEC << ", capping at 1.0");
+    ATH_MSG_WARNING("phiSizeMinusEC is large: " << phiSize.minusEC
+                                                << ", capping at 1.0");
     phiSize.minusEC = 1.0;
   }
   return phiSize;
diff --git a/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.h b/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.h
index f925bbd605f..820faf2c26d 100644
--- a/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.h
+++ b/Reconstruction/egamma/egammaAlgs/src/egammaSuperClusterBuilder.h
@@ -103,24 +103,16 @@ private:
 
   /** Find the size of the cluster */
   PhiSize findPhiSize(const CentralPosition& cp0,
-                      const xAOD::CaloCluster* cluster) const;
-
-  /** Add the EM cells from reference cluster to self; eta and phi are the ones
-     to use for limiting size. This excludes L1 (which is done as a separate
-     step). note, use raw eta and phi! */
-  StatusCode addEMCellsToCluster(xAOD::CaloCluster* newCluster,
-                                 const xAOD::CaloCluster* ref,
-                                 const CentralPosition& cp0) const;
-
-  /** Add the preshower and L1 EM cells from reference cluster to self; note,
-   * use raw eta and phi! */
-  StatusCode addL0L1EMCellsToCluster(xAOD::CaloCluster* newCluster,
-                                     const xAOD::CaloCluster* ref,
-                                     const CentralPosition& cp0,
-                                     const PhiSize& phiSize) const;
+                      const xAOD::CaloCluster& cluster) const;
+
+  /** fill Super Clusterlimiting its size.*/
+  StatusCode fillClusterConstrained(
+    xAOD::CaloCluster& tofill,
+    const std::vector<const xAOD::CaloCluster*>& clusters,
+    const CentralPosition& cp0) const;
 
   /** functions to add all tile Gap 3 cells in a window*/
-  StatusCode addTileGap3CellsinWindow(xAOD::CaloCluster* myCluster,
+  StatusCode addTileGap3CellsinWindow(xAOD::CaloCluster& tofill,
                                       const CaloDetDescrManager& mgr) const;
 
   /** function to calibrate the new clusters energy */
-- 
GitLab