From a13713549f71cb41862a4e4a71e6d07117945754 Mon Sep 17 00:00:00 2001
From: Erika De Lucia <erika.de.lucia@cern.ch>
Date: Wed, 12 Mar 2025 11:25:21 +0100
Subject: [PATCH 1/8] To fix crashing Muon code in Boole dd4hep

---
 Muon/MuonBackground/src/MuonBackground.cpp | 43 ++++++++++++++++------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/Muon/MuonBackground/src/MuonBackground.cpp b/Muon/MuonBackground/src/MuonBackground.cpp
index b2eea4b3a..c4c10a809 100755
--- a/Muon/MuonBackground/src/MuonBackground.cpp
+++ b/Muon/MuonBackground/src/MuonBackground.cpp
@@ -50,6 +50,9 @@
 
 #ifdef USE_DD4HEP
 #  include <DD4hep/GrammarUnparsed.h>
+#  include "Detector/Muon/DeMuon.h"
+#  include "Detector/Muon/Namespace.h"
+#  include "LbDD4hep/ConditionAccessorHolder.h"
 #endif
 
 /**
@@ -276,7 +279,7 @@ MuonBackground::Cache MuonBackground::makeCache( const DeMuonDetector& det ) con
 StatusCode MuonBackground::execute() {
   const Cache& c = m_cache.get( getConditionContext( Gaudi::Hive::currentContext() ) );
 
-  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute" << m_readSpilloverEvents << endmsg;
+  if ( msgLevel( MSG::DEBUG ) ) debug() << "==> Execute m_readSpilloverEvents = " << m_readSpilloverEvents << endmsg;
   StatusCode sc;
 
   for ( int ispill = 0; ispill <= m_readSpilloverEvents; ispill++ ) {
@@ -547,20 +550,33 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
     preGap     = -1;
     preIndex   = -1;
     preChamber = -1;
+    int MyDetID;
+    
     if ( hitPointer != 0 ) {
       for ( auto& hit : *hitPointer ) {
-        int det = hit->sensDetID();
-        if ( det < 0 ) continue;
+	//        int det = hit->sensDetID();
+	//	MyDetID = hit->sensDetID();
+	 MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
+	 if ( MyDetID < 0 ) continue;
 
-        station = c.det->stationID( det );
-        region  = c.det->regionID( det );
-        gap     = c.det->gapID( det );
+	 station = c.det->stationID(MyDetID);
+	region  = c.det->regionID(MyDetID);
+	gap     = c.det->gapID(MyDetID);
+	chamber = c.det->chamberID(MyDetID) + c.chamberOffset( station, region );
 
-        chamber = c.det->chamberID( det ) + c.chamberOffset( station, region );
+	//        station = c.det->stationID( det );
+	//        region  = c.det->regionID( det );
+	//        gap     = c.det->gapID( det );
+
+	//        chamber = c.det->chamberID( det ) + c.chamberOffset( station, region );
+
+	
         index   = hit->mcParticle()->key();
-        if ( msgLevel( MSG::VERBOSE ) ) {
-          verbose() << " index, chamber, gap " << index << " " << chamber << " " << gap << endmsg;
-          verbose() << " index " << index << " in position " << hit->entry().x() << " " << hit->entry().y() << " "
+        if ( msgLevel( MSG::DEBUG ) ) {
+	  debug() << " MyDetID " << MyDetID << endmsg;
+	  debug() << " station, region " << station << " " << region << endmsg;
+          debug() << " index, chamber, gap " << index << " " << chamber << " " << gap << endmsg;
+          debug() << " index " << index << " in position " << hit->entry().x() << " " << hit->entry().y() << " "
                     << hit->entry().z() << " "
                     << " out position  " << hit->exit().x() << " " << hit->exit().y() << " " << hit->exit().z()
                     << endmsg;
@@ -602,7 +618,7 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
     }
   }
 
-  if ( msgLevel( MSG::DEBUG ) ) debug() << " --- end of routine " << endmsg;
+  if ( msgLevel( MSG::DEBUG ) ) debug() << " <== calculateStartingNumberOfHit --- end of routine  " << endmsg;
   return StatusCode::SUCCESS;
 }
 
@@ -681,6 +697,11 @@ MuonBackground::getRadialPosition( const Cache& c, int index, int station ) cons
 
       try {
         DetElementRef<DeMuonChamber> chamber = c.det->pos2StChamber( xpos, ypos, station );
+	if ( msgLevel( MSG::DEBUG ) )
+	  debug() << " MuonBackground::getRadialPosition ==> Chamber station number " << chamber->stationNumber()
+		  << " station " << station << " position " << xpos << " " << ypos << " "
+		  << endmsg;
+	
         // check n ot only that hit is inside chamber but also gap...
         if ( chamber->checkHitAndGapInChamber( xpos, ypos ) ) {
           // remember this is the chamber number inside a region....
-- 
GitLab


From 6e49575dfe459b1708fa94d36084ba4b0ca56e54 Mon Sep 17 00:00:00 2001
From: Gitlab CI <noreply@cern.ch>
Date: Wed, 12 Mar 2025 10:27:47 +0000
Subject: [PATCH 2/8] Fixed formatting

patch generated by https://gitlab.cern.ch/lhcb/Boole/-/jobs/52621468
---
 Muon/MuonBackground/src/MuonBackground.cpp | 49 ++++++++++------------
 1 file changed, 23 insertions(+), 26 deletions(-)

diff --git a/Muon/MuonBackground/src/MuonBackground.cpp b/Muon/MuonBackground/src/MuonBackground.cpp
index c4c10a809..4d1ccef29 100755
--- a/Muon/MuonBackground/src/MuonBackground.cpp
+++ b/Muon/MuonBackground/src/MuonBackground.cpp
@@ -49,10 +49,10 @@
 #endif
 
 #ifdef USE_DD4HEP
-#  include <DD4hep/GrammarUnparsed.h>
 #  include "Detector/Muon/DeMuon.h"
 #  include "Detector/Muon/Namespace.h"
 #  include "LbDD4hep/ConditionAccessorHolder.h"
+#  include <DD4hep/GrammarUnparsed.h>
 #endif
 
 /**
@@ -551,35 +551,33 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
     preIndex   = -1;
     preChamber = -1;
     int MyDetID;
-    
+
     if ( hitPointer != 0 ) {
       for ( auto& hit : *hitPointer ) {
-	//        int det = hit->sensDetID();
-	//	MyDetID = hit->sensDetID();
-	 MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
-	 if ( MyDetID < 0 ) continue;
+        //        int det = hit->sensDetID();
+        //	MyDetID = hit->sensDetID();
+        MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
+        if ( MyDetID < 0 ) continue;
 
-	 station = c.det->stationID(MyDetID);
-	region  = c.det->regionID(MyDetID);
-	gap     = c.det->gapID(MyDetID);
-	chamber = c.det->chamberID(MyDetID) + c.chamberOffset( station, region );
+        station = c.det->stationID( MyDetID );
+        region  = c.det->regionID( MyDetID );
+        gap     = c.det->gapID( MyDetID );
+        chamber = c.det->chamberID( MyDetID ) + c.chamberOffset( station, region );
 
-	//        station = c.det->stationID( det );
-	//        region  = c.det->regionID( det );
-	//        gap     = c.det->gapID( det );
+        //        station = c.det->stationID( det );
+        //        region  = c.det->regionID( det );
+        //        gap     = c.det->gapID( det );
 
-	//        chamber = c.det->chamberID( det ) + c.chamberOffset( station, region );
+        //        chamber = c.det->chamberID( det ) + c.chamberOffset( station, region );
 
-	
-        index   = hit->mcParticle()->key();
+        index = hit->mcParticle()->key();
         if ( msgLevel( MSG::DEBUG ) ) {
-	  debug() << " MyDetID " << MyDetID << endmsg;
-	  debug() << " station, region " << station << " " << region << endmsg;
+          debug() << " MyDetID " << MyDetID << endmsg;
+          debug() << " station, region " << station << " " << region << endmsg;
           debug() << " index, chamber, gap " << index << " " << chamber << " " << gap << endmsg;
           debug() << " index " << index << " in position " << hit->entry().x() << " " << hit->entry().y() << " "
-                    << hit->entry().z() << " "
-                    << " out position  " << hit->exit().x() << " " << hit->exit().y() << " " << hit->exit().z()
-                    << endmsg;
+                  << hit->entry().z() << " "
+                  << " out position  " << hit->exit().x() << " " << hit->exit().y() << " " << hit->exit().z() << endmsg;
         }
         const LHCb::MCVertex* pointVertex = hit->mcParticle()->primaryVertex();
         unsigned int          collNumber  = numberOfCollision( pointVertex, vtxList );
@@ -697,11 +695,10 @@ MuonBackground::getRadialPosition( const Cache& c, int index, int station ) cons
 
       try {
         DetElementRef<DeMuonChamber> chamber = c.det->pos2StChamber( xpos, ypos, station );
-	if ( msgLevel( MSG::DEBUG ) )
-	  debug() << " MuonBackground::getRadialPosition ==> Chamber station number " << chamber->stationNumber()
-		  << " station " << station << " position " << xpos << " " << ypos << " "
-		  << endmsg;
-	
+        if ( msgLevel( MSG::DEBUG ) )
+          debug() << " MuonBackground::getRadialPosition ==> Chamber station number " << chamber->stationNumber()
+                  << " station " << station << " position " << xpos << " " << ypos << " " << endmsg;
+
         // check n ot only that hit is inside chamber but also gap...
         if ( chamber->checkHitAndGapInChamber( xpos, ypos ) ) {
           // remember this is the chamber number inside a region....
-- 
GitLab


From 818be0f5546c1e80f28f34cad20af8c306353f10 Mon Sep 17 00:00:00 2001
From: Erika De Lucia <erika.de.lucia@cern.ch>
Date: Wed, 12 Mar 2025 15:28:07 +0100
Subject: [PATCH 3/8] Enable boole-Muon test

---
 Digi/Boole/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/Digi/Boole/CMakeLists.txt b/Digi/Boole/CMakeLists.txt
index 7dc169beb..f2ab20078 100644
--- a/Digi/Boole/CMakeLists.txt
+++ b/Digi/Boole/CMakeLists.txt
@@ -34,7 +34,6 @@ if(BUILD_TESTING AND USE_DD4HEP)
     set_property(
         TEST
            # these test are using condition Conditions/Calibration/VP/VPDigitisationParam/Thresholds
-           Boole.boole-Muon
            Boole.boole-spillover
            Boole.boole-upgrade-baseline
            Boole.boole-run3
-- 
GitLab


From 2b0761d96f3149fd59486fc21a7b363f64b73b27 Mon Sep 17 00:00:00 2001
From: Erika De Lucia <erika.de.lucia@cern.ch>
Date: Wed, 12 Mar 2025 16:50:33 +0100
Subject: [PATCH 4/8] MuonBackground.cpp cleaning

---
 Muon/MuonBackground/src/MuonBackground.cpp | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/Muon/MuonBackground/src/MuonBackground.cpp b/Muon/MuonBackground/src/MuonBackground.cpp
index 4d1ccef29..f56c2dc5d 100755
--- a/Muon/MuonBackground/src/MuonBackground.cpp
+++ b/Muon/MuonBackground/src/MuonBackground.cpp
@@ -554,8 +554,6 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
 
     if ( hitPointer != 0 ) {
       for ( auto& hit : *hitPointer ) {
-        //        int det = hit->sensDetID();
-        //	MyDetID = hit->sensDetID();
         MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
         if ( MyDetID < 0 ) continue;
 
@@ -564,12 +562,6 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
         gap     = c.det->gapID( MyDetID );
         chamber = c.det->chamberID( MyDetID ) + c.chamberOffset( station, region );
 
-        //        station = c.det->stationID( det );
-        //        region  = c.det->regionID( det );
-        //        gap     = c.det->gapID( det );
-
-        //        chamber = c.det->chamberID( det ) + c.chamberOffset( station, region );
-
         index = hit->mcParticle()->key();
         if ( msgLevel( MSG::DEBUG ) ) {
           debug() << " MyDetID " << MyDetID << endmsg;
-- 
GitLab


From 63c40d5b95ec8100358a9b5b735696687d8c547a Mon Sep 17 00:00:00 2001
From: Erika De Lucia <erika.de.lucia@cern.ch>
Date: Wed, 2 Apr 2025 13:53:14 +0200
Subject: [PATCH 5/8] fixing debug messages

---
 Muon/MuonBackground/src/MuonBackground.cpp | 44 ++++++++++++++++------
 1 file changed, 32 insertions(+), 12 deletions(-)

diff --git a/Muon/MuonBackground/src/MuonBackground.cpp b/Muon/MuonBackground/src/MuonBackground.cpp
index f56c2dc5d..3863b5b9b 100755
--- a/Muon/MuonBackground/src/MuonBackground.cpp
+++ b/Muon/MuonBackground/src/MuonBackground.cpp
@@ -49,10 +49,13 @@
 #endif
 
 #ifdef USE_DD4HEP
-#  include "Detector/Muon/DeMuon.h"
-#  include "Detector/Muon/Namespace.h"
-#  include "LbDD4hep/ConditionAccessorHolder.h"
-#  include <DD4hep/GrammarUnparsed.h>
+#include "Detector/Muon/DeMuon.h"
+#include "Detector/Muon/DeMuonChamber.h"
+#include "Detector/Muon/MuonConstants.h"
+#include "Detector/Muon/MuonVolumeID.h"
+#include "Detector/Muon/Namespace.h"
+#include "LbDD4hep/ConditionAccessorHolder.h"
+#include <DD4hep/GrammarUnparsed.h>
 #endif
 
 /**
@@ -285,7 +288,7 @@ StatusCode MuonBackground::execute() {
   for ( int ispill = 0; ispill <= m_readSpilloverEvents; ispill++ ) {
     auto collisions = calculateNumberOfCollision( ispill );
     if ( !collisions ) continue;
-    if ( msgLevel( MSG::VERBOSE ) ) verbose() << "==> collision " << collisions << endmsg;
+    if ( msgLevel( MSG::VERBOSE ) ) verbose() << "================= ispill = " << ispill<< " and ==> collision " << collisions << endmsg;
     LHCb::MCHits*                 hitsContainer = new LHCb::MCHits();
     std::vector<std::vector<int>> results{collisions};
 
@@ -297,7 +300,8 @@ StatusCode MuonBackground::execute() {
           for ( unsigned int multi = 0; multi < c.gaps; multi++ ) {
             int index        = station * c.gaps + multi;
             int startingHits = results[coll][index];
-            if ( msgLevel( MSG::VERBOSE ) ) verbose() << "station safe start end hits " << startingHits << endmsg;
+	    if ( msgLevel( MSG::VERBOSE ) ) verbose() << "> collision n. " << coll << " station n. " << station << " multi n. " << multi << endmsg;
+            if ( msgLevel( MSG::VERBOSE ) ) verbose() << "> station safe start end hits " << startingHits << endmsg;
 
             // extract number of hits to be added
             int   hitToAdd = 0;
@@ -313,7 +317,7 @@ StatusCode MuonBackground::execute() {
             //  yy<< " "<< hitToAdd<<" "<<startingHits<<endmsg;
 
             if ( msgLevel( MSG::VERBOSE ) )
-              verbose() << "adding " << hitToAdd << " to orginal " << startingHits << " in station " << station
+              verbose() << "  m_type == LowEnergy >> adding " << hitToAdd << " to orginal " << startingHits << " in station " << station
                         << " for multiplicity " << multi << " and collisions/spill " << coll << " " << ispill << endmsg;
 
             for ( int hitID = 0; hitID < hitToAdd; hitID++ ) {
@@ -362,7 +366,7 @@ StatusCode MuonBackground::execute() {
             int hitToAdd = 0;
             hitToAdd     = howManyHit( floatHit );
             if ( msgLevel( MSG::VERBOSE ) )
-              verbose() << "adding " << hitToAdd << " in station " << station << " for multiplicity " << multi
+              verbose() << "  m_type == FlatSpill >> adding " << hitToAdd << " in station " << station << " for multiplicity " << multi
                         << " and spill" << fspill << endmsg;
             for ( int hitID = 0; hitID < hitToAdd; hitID++ ) {
               StatusCode asc = createHit( c, hitsContainer, station, multi, fspill );
@@ -555,6 +559,7 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
     if ( hitPointer != 0 ) {
       for ( auto& hit : *hitPointer ) {
         MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
+	if ( msgLevel( MSG::DEBUG ) ) debug() << " calculateStartingNumberOfHit >>> MyDetID " << MyDetID << endmsg;
         if ( MyDetID < 0 ) continue;
 
         station = c.det->stationID( MyDetID );
@@ -686,12 +691,14 @@ MuonBackground::getRadialPosition( const Cache& c, int index, int station ) cons
       float ypos = float( r * sin( globalPhi ) * Gaudi::Units::mm * m_unitLength );
 
       try {
+
         DetElementRef<DeMuonChamber> chamber = c.det->pos2StChamber( xpos, ypos, station );
         if ( msgLevel( MSG::DEBUG ) )
-          debug() << " MuonBackground::getRadialPosition ==> Chamber station number " << chamber->stationNumber()
+          debug() << " MuonBackground::getRadialPosition ==> Chamber stationNumber " << chamber->stationNumber()
+		  << " Chamber regionNumber " << chamber->regionNumber()
                   << " station " << station << " position " << xpos << " " << ypos << " " << endmsg;
 
-        // check n ot only that hit is inside chamber but also gap...
+        // check not only that hit is inside chamber but also gap...
         if ( chamber->checkHitAndGapInChamber( xpos, ypos ) ) {
           // remember this is the chamber number inside a region....
           if ( msgLevel( MSG::DEBUG ) )
@@ -714,6 +721,7 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
                                       int ispill ) const {
   int index = station * c.gaps + multi;
 
+  if ( msgLevel( MSG::DEBUG ) ) debug() << "MuonBackground::createHit >> station = " << station << " multi =  " << multi<< endmsg;
   // 1) extract the radial position
   auto pos = getRadialPosition( c, index, station );
 
@@ -780,7 +788,12 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
           pChamber->calculateHitPosInGap( gapNumber, xpos, ypos, xSlope, ySlope, averageZ, entryGlobal, exitGlobal );
       if ( success ) {
         if ( msgLevel( MSG::DEBUG ) ) debug() << "found hit in chamber  " << endmsg;
+        if ( msgLevel( MSG::DEBUG ) ) debug() << " gapNumber " <<gapNumber<< "  xpos "
+					      << xpos << " ypos "<< ypos << " xSlope " << xSlope
+					      << " ySlope " << ySlope << " averageZ "<< averageZ
+					      << " entryGlobal " << entryGlobal << " exitGlobal " << exitGlobal << endmsg;
       } else {
+	if ( msgLevel( MSG::DEBUG ) ) debug() << "NOT found hit in chamber  " << endmsg;
         allHitsInsideCha = false;
       }
     }
@@ -825,11 +838,18 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
       }
       if ( msgLevel( MSG::DEBUG ) ) debug() << " mid point " << x << " " << y << " " << z << endmsg;
 
-      int sen = c.det->sensitiveVolumeID( Gaudi::XYZPoint( x, y, z ) );
+      ROOT::Math::XYZPoint midpoint( x, y, z );
+
+      if ( msgLevel( MSG::DEBUG ) ) debug() << " midpoint x = " <<  midpoint.x() << " y = " <<  midpoint.y() << " z = " <<  midpoint.z() << endmsg;
+      int sen = c.det->sensitiveVolumeID( midpoint );
+
       if ( msgLevel( MSG::DEBUG ) ) debug() << " the volume ID is " << sen << endmsg;
+      if ( msgLevel( MSG::DEBUG ) ) debug() << " Station " <<  c.det->stationID(sen) << " Region " <<  c.det->regionID( sen ) << " Gap " << c.det->gapID( sen ) << endmsg;
+      
       pHit->setSensDetID( sen );
+      
       if ( msgLevel( MSG::DEBUG ) )
-        debug() << "gap, time, position " << gapNumber << " " << pChamber->chamberNumber() << " "
+        debug() << "gap, pChamber->chamberNumber(), time, position " << gapNumber << " " << pChamber->chamberNumber() << " "
                 << timeBest + tofOfLight << " " << x << " " << y << " " << endmsg;
       if ( correct ) {
         ( hitsContainer )->push_back( pHit );
-- 
GitLab


From eb84c4db47e59bed4f4456b34174f9fbe530adf5 Mon Sep 17 00:00:00 2001
From: Gitlab CI <noreply@cern.ch>
Date: Wed, 2 Apr 2025 11:53:59 +0000
Subject: [PATCH 6/8] Fixed formatting

patch generated by https://gitlab.cern.ch/lhcb/Boole/-/jobs/54053641
---
 Muon/MuonBackground/src/MuonBackground.cpp | 63 ++++++++++++----------
 1 file changed, 35 insertions(+), 28 deletions(-)

diff --git a/Muon/MuonBackground/src/MuonBackground.cpp b/Muon/MuonBackground/src/MuonBackground.cpp
index 3863b5b9b..521cccb8c 100755
--- a/Muon/MuonBackground/src/MuonBackground.cpp
+++ b/Muon/MuonBackground/src/MuonBackground.cpp
@@ -49,13 +49,13 @@
 #endif
 
 #ifdef USE_DD4HEP
-#include "Detector/Muon/DeMuon.h"
-#include "Detector/Muon/DeMuonChamber.h"
-#include "Detector/Muon/MuonConstants.h"
-#include "Detector/Muon/MuonVolumeID.h"
-#include "Detector/Muon/Namespace.h"
-#include "LbDD4hep/ConditionAccessorHolder.h"
-#include <DD4hep/GrammarUnparsed.h>
+#  include "Detector/Muon/DeMuon.h"
+#  include "Detector/Muon/DeMuonChamber.h"
+#  include "Detector/Muon/MuonConstants.h"
+#  include "Detector/Muon/MuonVolumeID.h"
+#  include "Detector/Muon/Namespace.h"
+#  include "LbDD4hep/ConditionAccessorHolder.h"
+#  include <DD4hep/GrammarUnparsed.h>
 #endif
 
 /**
@@ -288,7 +288,8 @@ StatusCode MuonBackground::execute() {
   for ( int ispill = 0; ispill <= m_readSpilloverEvents; ispill++ ) {
     auto collisions = calculateNumberOfCollision( ispill );
     if ( !collisions ) continue;
-    if ( msgLevel( MSG::VERBOSE ) ) verbose() << "================= ispill = " << ispill<< " and ==> collision " << collisions << endmsg;
+    if ( msgLevel( MSG::VERBOSE ) )
+      verbose() << "================= ispill = " << ispill << " and ==> collision " << collisions << endmsg;
     LHCb::MCHits*                 hitsContainer = new LHCb::MCHits();
     std::vector<std::vector<int>> results{collisions};
 
@@ -300,7 +301,8 @@ StatusCode MuonBackground::execute() {
           for ( unsigned int multi = 0; multi < c.gaps; multi++ ) {
             int index        = station * c.gaps + multi;
             int startingHits = results[coll][index];
-	    if ( msgLevel( MSG::VERBOSE ) ) verbose() << "> collision n. " << coll << " station n. " << station << " multi n. " << multi << endmsg;
+            if ( msgLevel( MSG::VERBOSE ) )
+              verbose() << "> collision n. " << coll << " station n. " << station << " multi n. " << multi << endmsg;
             if ( msgLevel( MSG::VERBOSE ) ) verbose() << "> station safe start end hits " << startingHits << endmsg;
 
             // extract number of hits to be added
@@ -317,8 +319,9 @@ StatusCode MuonBackground::execute() {
             //  yy<< " "<< hitToAdd<<" "<<startingHits<<endmsg;
 
             if ( msgLevel( MSG::VERBOSE ) )
-              verbose() << "  m_type == LowEnergy >> adding " << hitToAdd << " to orginal " << startingHits << " in station " << station
-                        << " for multiplicity " << multi << " and collisions/spill " << coll << " " << ispill << endmsg;
+              verbose() << "  m_type == LowEnergy >> adding " << hitToAdd << " to orginal " << startingHits
+                        << " in station " << station << " for multiplicity " << multi << " and collisions/spill "
+                        << coll << " " << ispill << endmsg;
 
             for ( int hitID = 0; hitID < hitToAdd; hitID++ ) {
               StatusCode asc = createHit( c, hitsContainer, station, multi, ispill );
@@ -366,8 +369,8 @@ StatusCode MuonBackground::execute() {
             int hitToAdd = 0;
             hitToAdd     = howManyHit( floatHit );
             if ( msgLevel( MSG::VERBOSE ) )
-              verbose() << "  m_type == FlatSpill >> adding " << hitToAdd << " in station " << station << " for multiplicity " << multi
-                        << " and spill" << fspill << endmsg;
+              verbose() << "  m_type == FlatSpill >> adding " << hitToAdd << " in station " << station
+                        << " for multiplicity " << multi << " and spill" << fspill << endmsg;
             for ( int hitID = 0; hitID < hitToAdd; hitID++ ) {
               StatusCode asc = createHit( c, hitsContainer, station, multi, fspill );
               if ( asc.isFailure() && msgLevel( MSG::DEBUG ) ) debug() << "failing hit creation " << endmsg;
@@ -559,7 +562,7 @@ StatusCode MuonBackground::calculateStartingNumberOfHits( const Cache& c, int is
     if ( hitPointer != 0 ) {
       for ( auto& hit : *hitPointer ) {
         MyDetID = c.det->sensitiveVolumeID( hit->midPoint() );
-	if ( msgLevel( MSG::DEBUG ) ) debug() << " calculateStartingNumberOfHit >>> MyDetID " << MyDetID << endmsg;
+        if ( msgLevel( MSG::DEBUG ) ) debug() << " calculateStartingNumberOfHit >>> MyDetID " << MyDetID << endmsg;
         if ( MyDetID < 0 ) continue;
 
         station = c.det->stationID( MyDetID );
@@ -695,8 +698,8 @@ MuonBackground::getRadialPosition( const Cache& c, int index, int station ) cons
         DetElementRef<DeMuonChamber> chamber = c.det->pos2StChamber( xpos, ypos, station );
         if ( msgLevel( MSG::DEBUG ) )
           debug() << " MuonBackground::getRadialPosition ==> Chamber stationNumber " << chamber->stationNumber()
-		  << " Chamber regionNumber " << chamber->regionNumber()
-                  << " station " << station << " position " << xpos << " " << ypos << " " << endmsg;
+                  << " Chamber regionNumber " << chamber->regionNumber() << " station " << station << " position "
+                  << xpos << " " << ypos << " " << endmsg;
 
         // check not only that hit is inside chamber but also gap...
         if ( chamber->checkHitAndGapInChamber( xpos, ypos ) ) {
@@ -721,7 +724,8 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
                                       int ispill ) const {
   int index = station * c.gaps + multi;
 
-  if ( msgLevel( MSG::DEBUG ) ) debug() << "MuonBackground::createHit >> station = " << station << " multi =  " << multi<< endmsg;
+  if ( msgLevel( MSG::DEBUG ) )
+    debug() << "MuonBackground::createHit >> station = " << station << " multi =  " << multi << endmsg;
   // 1) extract the radial position
   auto pos = getRadialPosition( c, index, station );
 
@@ -788,12 +792,12 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
           pChamber->calculateHitPosInGap( gapNumber, xpos, ypos, xSlope, ySlope, averageZ, entryGlobal, exitGlobal );
       if ( success ) {
         if ( msgLevel( MSG::DEBUG ) ) debug() << "found hit in chamber  " << endmsg;
-        if ( msgLevel( MSG::DEBUG ) ) debug() << " gapNumber " <<gapNumber<< "  xpos "
-					      << xpos << " ypos "<< ypos << " xSlope " << xSlope
-					      << " ySlope " << ySlope << " averageZ "<< averageZ
-					      << " entryGlobal " << entryGlobal << " exitGlobal " << exitGlobal << endmsg;
+        if ( msgLevel( MSG::DEBUG ) )
+          debug() << " gapNumber " << gapNumber << "  xpos " << xpos << " ypos " << ypos << " xSlope " << xSlope
+                  << " ySlope " << ySlope << " averageZ " << averageZ << " entryGlobal " << entryGlobal
+                  << " exitGlobal " << exitGlobal << endmsg;
       } else {
-	if ( msgLevel( MSG::DEBUG ) ) debug() << "NOT found hit in chamber  " << endmsg;
+        if ( msgLevel( MSG::DEBUG ) ) debug() << "NOT found hit in chamber  " << endmsg;
         allHitsInsideCha = false;
       }
     }
@@ -840,17 +844,20 @@ StatusCode MuonBackground::createHit( const Cache& c, LHCb::MCHits* hitsContaine
 
       ROOT::Math::XYZPoint midpoint( x, y, z );
 
-      if ( msgLevel( MSG::DEBUG ) ) debug() << " midpoint x = " <<  midpoint.x() << " y = " <<  midpoint.y() << " z = " <<  midpoint.z() << endmsg;
+      if ( msgLevel( MSG::DEBUG ) )
+        debug() << " midpoint x = " << midpoint.x() << " y = " << midpoint.y() << " z = " << midpoint.z() << endmsg;
       int sen = c.det->sensitiveVolumeID( midpoint );
 
       if ( msgLevel( MSG::DEBUG ) ) debug() << " the volume ID is " << sen << endmsg;
-      if ( msgLevel( MSG::DEBUG ) ) debug() << " Station " <<  c.det->stationID(sen) << " Region " <<  c.det->regionID( sen ) << " Gap " << c.det->gapID( sen ) << endmsg;
-      
+      if ( msgLevel( MSG::DEBUG ) )
+        debug() << " Station " << c.det->stationID( sen ) << " Region " << c.det->regionID( sen ) << " Gap "
+                << c.det->gapID( sen ) << endmsg;
+
       pHit->setSensDetID( sen );
-      
+
       if ( msgLevel( MSG::DEBUG ) )
-        debug() << "gap, pChamber->chamberNumber(), time, position " << gapNumber << " " << pChamber->chamberNumber() << " "
-                << timeBest + tofOfLight << " " << x << " " << y << " " << endmsg;
+        debug() << "gap, pChamber->chamberNumber(), time, position " << gapNumber << " " << pChamber->chamberNumber()
+                << " " << timeBest + tofOfLight << " " << x << " " << y << " " << endmsg;
       if ( correct ) {
         ( hitsContainer )->push_back( pHit );
         if ( msgLevel( MSG::DEBUG ) ) {
-- 
GitLab


From 38d7fcbde5f6a30d999268fe9855c593caf11c13 Mon Sep 17 00:00:00 2001
From: Erika De Lucia <erika.de.lucia@cern.ch>
Date: Wed, 2 Apr 2025 14:48:59 +0200
Subject: [PATCH 7/8] use sensitiveVolumeID instead of sensDetID

---
 Muon/MuonAlgs/src/MuonDigitization.cpp     | 46 +++++++++++++++++-----
 Muon/MuonMoniDigi/src/MuonDigitChecker.cpp | 12 +++---
 2 files changed, 43 insertions(+), 15 deletions(-)

diff --git a/Muon/MuonAlgs/src/MuonDigitization.cpp b/Muon/MuonAlgs/src/MuonDigitization.cpp
index a3971091c..f6192cb26 100644
--- a/Muon/MuonAlgs/src/MuonDigitization.cpp
+++ b/Muon/MuonAlgs/src/MuonDigitization.cpp
@@ -34,6 +34,12 @@
 #include <vector>
 #ifdef USE_DD4HEP
 #  include <DD4hep/GrammarUnparsed.h>
+#include "Detector/Muon/DeMuon.h"
+#include "Detector/Muon/DeMuonChamber.h"
+#include "Detector/Muon/MuonConstants.h"
+#include "Detector/Muon/MuonVolumeID.h"
+#include "Detector/Muon/Namespace.h"
+#include "LbDD4hep/ConditionAccessorHolder.h"
 #endif
 
 using namespace std::string_literals;
@@ -55,6 +61,16 @@ private:
         : det{muonDet}, detectorResponse{std::make_unique<MuonDetectorResponse>( muonDet, randSvc, msgSvc )} {}
     Cache& operator=( Cache&& ) = default;
 
+    // ---- EDL ----
+    // To do as it is defined in MuonDigitChecker.cpp cache
+    // -------
+    // int maxDimension() const { return gaps * stationsNumber; }
+    // int chamberOffset( int station, int region ) const { return chamberInRegion[station * 4 + region]; }
+    // int                           stationsNumber = 0;
+    // std::vector<std::string>      stationsNames;
+    // unsigned int                  gaps = 0;
+    // std::array<int, 20>           chamberInRegion;
+    // ----
     DetElementRef<DeMuonDetector>         det;
     std::unique_ptr<MuonDetectorResponse> detectorResponse;
   };
@@ -176,7 +192,8 @@ StatusCode MuonDigitization::execute() const {
           debug() << "muon x , y, z entry ,  " << i->entry().x() << " " << i->entry().y() << "  " << i->entry().z()
                   << endmsg;
           debug() << "time of flight ,  " << i->time() << endmsg;
-          int det = i->sensDetID();
+
+	  int det =  cache.det->sensitiveVolumeID( i->midPoint() );
           debug() << " chamber and gap ID	" << cache.det->chamberID( det ) << " " << cache.det->gapID( det )
                   << endmsg;
         }
@@ -315,15 +332,24 @@ void MuonDigitization::createInput( MuonDigitizationData<MuonPhyChannelInput>& P
       SmartDataPtr<LHCb::MCHits> hitPointer( eventSvc(), path );
       if ( !hitPointer ) continue;
 
+      int MyDetID;
+      
       for ( auto& iter : *hitPointer ) { // loop over hits
-        const int det = iter->sensDetID();
+	if ( m_verboseDebug ) { info() << "createInput: midpoint " << iter->midPoint().x() << " "  << iter->midPoint().y() << " " << iter->midPoint().z() << endmsg; }
+	MyDetID =  cache.det->sensitiveVolumeID( iter->midPoint() );
+	int det = MyDetID;
+	if ( m_verboseDebug ) { info() << "createInput: MyDetID " << cache.det->sensitiveVolumeID( iter->midPoint() ) << endmsg; }
         if ( det < 0 ) continue;
 
-        const unsigned int hitStation = cache.det->stationID( det );
-        const unsigned int hitRegion  = cache.det->regionID( det );
-        const unsigned int hitChamber = cache.det->chamberID( det );
-        const unsigned int hitGap     = cache.det->gapID( det );
-        const unsigned int hitQuarter = cache.det->quadrantID( det );
+        const unsigned int hitStation = cache.det->stationID( MyDetID );
+        const unsigned int hitRegion  = cache.det->regionID( MyDetID );
+	// ---- EDL ----
+	// To do as it is defined in MuonDigitChecker.cpp cache
+	// -------	
+	//  const unsigned int hitChamber = cache.det->chamberID( MyDetID ) + cache.chamberOffset( hitStation, hitRegion );
+	const unsigned int hitChamber = cache.det->chamberID( MyDetID );
+        const unsigned int hitGap     = cache.det->gapID( MyDetID  );
+        const unsigned int hitQuarter = cache.det->quadrantID( MyDetID );
 
         for ( const auto& [fe, distanceFromBoundary] :
               cache.det->listOfPhysChannels( iter->entry(), iter->exit(), hitRegion, hitChamber ) ) {
@@ -359,9 +385,9 @@ void MuonDigitization::createInput( MuonDigitizationData<MuonPhyChannelInput>& P
 
           if ( m_verboseDebug ) {
             const int code = ( iter->mcParticle() ? iter->mcParticle()->particleID().abspid() : 0 );
-            info() << "hit processing " << hitStation << " " << hitRegion << " " << hitQuarter << " " << hitChamber
-                   << " " << hitGap << " " << fe.getLayer() << " " << fe.getReadout() << " " << tofOfLight << " "
-                   << OriginOfHitsContainer[container] << " " << ispill << " " << code << endmsg;
+            info() << "hit processing Station = " << hitStation << "  Region = " << hitRegion << " Quarter = " << hitQuarter << " Chamber = " << hitChamber
+                   << " Gap = " << hitGap << " fe.getLayer() = " << fe.getLayer() << " fe.getReadout() = " << fe.getReadout() << " tofOfLight =  " << tofOfLight << " OriginOfHitsContainer[container] = "
+                   << OriginOfHitsContainer[container] << " ispill = " << ispill << " code =  " << code << endmsg;
             info() << " ph ch ID " << inputPointer.phChID() << " id " << inputPointer.phChID().getID() << endmsg;
           }
 
diff --git a/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp b/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
index 4cb7f12e9..da42a2b44 100644
--- a/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
+++ b/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
@@ -143,8 +143,7 @@ void MuonDigitChecker::operator()( const LHCb::MCHeader& evt, const LHCb::MCHits
     int MyDetID;
     // Loop over Muon Hits of given type
     for ( const auto* hit : hits ) {
-
-      MyDetID = hit->sensDetID();
+      MyDetID = muon.sensitiveVolumeID( hit->midPoint() );
       if ( MyDetID < 0 ) continue;
       // Needs to extract info from sens ID
       int station = muon.stationID( MyDetID );
@@ -303,16 +302,19 @@ void MuonDigitChecker::operator()( const LHCb::MCHeader& evt, const LHCb::MCHits
     digit_firing.push_back( Dfir );
     digit_origin.push_back( Dcon );
     digit_bx.push_back( Deve );
-
+    if ( msgLevel( MSG::DEBUG )) debug() << " Hit origin = "<< Dcon << "  = " << endmsg;
     // Hits mupltiplicity
     digit_multi.push_back( MCmd->mcHits().size() );
     // if(Deve)
     ++tnDhit[( ( Dsta + m_stationIdOffset ) * 4 + Dreg ) * 6 + Dcon];
-    // if(globalTimeOffset()>0)info()<<" qui "<<Dcon<<" "<<Dfir<<" "<<Deve<<" "<<Dsta<<" "<<Dreg<<endmsg;
+    if ( msgLevel( MSG::DEBUG )) debug() << " Firing Time" << Dfir <<" Belongs to event ? "<<Deve<<" Station "<<Dsta<<" Region  "<<Dreg<<endmsg;
   }
 
   // the weird start of loop is to skip M1 if not present
-  for ( std::size_t i = m_stationIdOffset * 4 * 6; i < tnDhit.size(); ++i ) { m_avgDHits[i] += tnDhit[i]; }
+  for ( std::size_t i = m_stationIdOffset * 4 * 6; i < tnDhit.size(); ++i ) {
+    m_avgDHits[i] += tnDhit[i];
+    if ( msgLevel( MSG::DEBUG )) debug() << " i = "<< i << "  m_avgDHits[i] = " <<  m_avgDHits[i] << endmsg;
+  }
 
   // --  Fill some histos with digits infos
   plot( occu[0], 3002, "Digits Multiplicity Station M2 ", 0., 350., 50 );
-- 
GitLab


From d9cabfb3ebd9d78414e1a154cb13c2ed8aea6f8b Mon Sep 17 00:00:00 2001
From: Gitlab CI <noreply@cern.ch>
Date: Wed, 2 Apr 2025 12:49:23 +0000
Subject: [PATCH 8/8] Fixed formatting

patch generated by https://gitlab.cern.ch/lhcb/Boole/-/jobs/54057304
---
 Muon/MuonAlgs/src/MuonDigitization.cpp     | 51 +++++++++++++---------
 Muon/MuonMoniDigi/src/MuonDigitChecker.cpp |  8 ++--
 2 files changed, 35 insertions(+), 24 deletions(-)

diff --git a/Muon/MuonAlgs/src/MuonDigitization.cpp b/Muon/MuonAlgs/src/MuonDigitization.cpp
index f6192cb26..8f68205c1 100644
--- a/Muon/MuonAlgs/src/MuonDigitization.cpp
+++ b/Muon/MuonAlgs/src/MuonDigitization.cpp
@@ -33,13 +33,13 @@
 #include <string>
 #include <vector>
 #ifdef USE_DD4HEP
+#  include "Detector/Muon/DeMuon.h"
+#  include "Detector/Muon/DeMuonChamber.h"
+#  include "Detector/Muon/MuonConstants.h"
+#  include "Detector/Muon/MuonVolumeID.h"
+#  include "Detector/Muon/Namespace.h"
+#  include "LbDD4hep/ConditionAccessorHolder.h"
 #  include <DD4hep/GrammarUnparsed.h>
-#include "Detector/Muon/DeMuon.h"
-#include "Detector/Muon/DeMuonChamber.h"
-#include "Detector/Muon/MuonConstants.h"
-#include "Detector/Muon/MuonVolumeID.h"
-#include "Detector/Muon/Namespace.h"
-#include "LbDD4hep/ConditionAccessorHolder.h"
 #endif
 
 using namespace std::string_literals;
@@ -193,7 +193,7 @@ StatusCode MuonDigitization::execute() const {
                   << endmsg;
           debug() << "time of flight ,  " << i->time() << endmsg;
 
-	  int det =  cache.det->sensitiveVolumeID( i->midPoint() );
+          int det = cache.det->sensitiveVolumeID( i->midPoint() );
           debug() << " chamber and gap ID	" << cache.det->chamberID( det ) << " " << cache.det->gapID( det )
                   << endmsg;
         }
@@ -333,22 +333,28 @@ void MuonDigitization::createInput( MuonDigitizationData<MuonPhyChannelInput>& P
       if ( !hitPointer ) continue;
 
       int MyDetID;
-      
+
       for ( auto& iter : *hitPointer ) { // loop over hits
-	if ( m_verboseDebug ) { info() << "createInput: midpoint " << iter->midPoint().x() << " "  << iter->midPoint().y() << " " << iter->midPoint().z() << endmsg; }
-	MyDetID =  cache.det->sensitiveVolumeID( iter->midPoint() );
-	int det = MyDetID;
-	if ( m_verboseDebug ) { info() << "createInput: MyDetID " << cache.det->sensitiveVolumeID( iter->midPoint() ) << endmsg; }
+        if ( m_verboseDebug ) {
+          info() << "createInput: midpoint " << iter->midPoint().x() << " " << iter->midPoint().y() << " "
+                 << iter->midPoint().z() << endmsg;
+        }
+        MyDetID = cache.det->sensitiveVolumeID( iter->midPoint() );
+        int det = MyDetID;
+        if ( m_verboseDebug ) {
+          info() << "createInput: MyDetID " << cache.det->sensitiveVolumeID( iter->midPoint() ) << endmsg;
+        }
         if ( det < 0 ) continue;
 
         const unsigned int hitStation = cache.det->stationID( MyDetID );
         const unsigned int hitRegion  = cache.det->regionID( MyDetID );
-	// ---- EDL ----
-	// To do as it is defined in MuonDigitChecker.cpp cache
-	// -------	
-	//  const unsigned int hitChamber = cache.det->chamberID( MyDetID ) + cache.chamberOffset( hitStation, hitRegion );
-	const unsigned int hitChamber = cache.det->chamberID( MyDetID );
-        const unsigned int hitGap     = cache.det->gapID( MyDetID  );
+        // ---- EDL ----
+        // To do as it is defined in MuonDigitChecker.cpp cache
+        // -------
+        //  const unsigned int hitChamber = cache.det->chamberID( MyDetID ) + cache.chamberOffset( hitStation, hitRegion
+        //  );
+        const unsigned int hitChamber = cache.det->chamberID( MyDetID );
+        const unsigned int hitGap     = cache.det->gapID( MyDetID );
         const unsigned int hitQuarter = cache.det->quadrantID( MyDetID );
 
         for ( const auto& [fe, distanceFromBoundary] :
@@ -385,9 +391,12 @@ void MuonDigitization::createInput( MuonDigitizationData<MuonPhyChannelInput>& P
 
           if ( m_verboseDebug ) {
             const int code = ( iter->mcParticle() ? iter->mcParticle()->particleID().abspid() : 0 );
-            info() << "hit processing Station = " << hitStation << "  Region = " << hitRegion << " Quarter = " << hitQuarter << " Chamber = " << hitChamber
-                   << " Gap = " << hitGap << " fe.getLayer() = " << fe.getLayer() << " fe.getReadout() = " << fe.getReadout() << " tofOfLight =  " << tofOfLight << " OriginOfHitsContainer[container] = "
-                   << OriginOfHitsContainer[container] << " ispill = " << ispill << " code =  " << code << endmsg;
+            info() << "hit processing Station = " << hitStation << "  Region = " << hitRegion
+                   << " Quarter = " << hitQuarter << " Chamber = " << hitChamber << " Gap = " << hitGap
+                   << " fe.getLayer() = " << fe.getLayer() << " fe.getReadout() = " << fe.getReadout()
+                   << " tofOfLight =  " << tofOfLight
+                   << " OriginOfHitsContainer[container] = " << OriginOfHitsContainer[container]
+                   << " ispill = " << ispill << " code =  " << code << endmsg;
             info() << " ph ch ID " << inputPointer.phChID() << " id " << inputPointer.phChID().getID() << endmsg;
           }
 
diff --git a/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp b/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
index da42a2b44..e4bd736f9 100644
--- a/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
+++ b/Muon/MuonMoniDigi/src/MuonDigitChecker.cpp
@@ -302,18 +302,20 @@ void MuonDigitChecker::operator()( const LHCb::MCHeader& evt, const LHCb::MCHits
     digit_firing.push_back( Dfir );
     digit_origin.push_back( Dcon );
     digit_bx.push_back( Deve );
-    if ( msgLevel( MSG::DEBUG )) debug() << " Hit origin = "<< Dcon << "  = " << endmsg;
+    if ( msgLevel( MSG::DEBUG ) ) debug() << " Hit origin = " << Dcon << "  = " << endmsg;
     // Hits mupltiplicity
     digit_multi.push_back( MCmd->mcHits().size() );
     // if(Deve)
     ++tnDhit[( ( Dsta + m_stationIdOffset ) * 4 + Dreg ) * 6 + Dcon];
-    if ( msgLevel( MSG::DEBUG )) debug() << " Firing Time" << Dfir <<" Belongs to event ? "<<Deve<<" Station "<<Dsta<<" Region  "<<Dreg<<endmsg;
+    if ( msgLevel( MSG::DEBUG ) )
+      debug() << " Firing Time" << Dfir << " Belongs to event ? " << Deve << " Station " << Dsta << " Region  " << Dreg
+              << endmsg;
   }
 
   // the weird start of loop is to skip M1 if not present
   for ( std::size_t i = m_stationIdOffset * 4 * 6; i < tnDhit.size(); ++i ) {
     m_avgDHits[i] += tnDhit[i];
-    if ( msgLevel( MSG::DEBUG )) debug() << " i = "<< i << "  m_avgDHits[i] = " <<  m_avgDHits[i] << endmsg;
+    if ( msgLevel( MSG::DEBUG ) ) debug() << " i = " << i << "  m_avgDHits[i] = " << m_avgDHits[i] << endmsg;
   }
 
   // --  Fill some histos with digits infos
-- 
GitLab