@@ -99,20 +99,19 @@ void SiPixelCompareRecHits<T>::analyzeSeparate(U tokenRef, V tokenTar, const edm
9999 const auto & rhsoaHandleRef = iEvent.getHandle (tokenRef);
100100 const auto & rhsoaHandleTar = iEvent.getHandle (tokenTar);
101101
102- if (not rhsoaHandleRef or not rhsoaHandleTar) {
102+ // Exit early if any handle is invalid
103+ if (!rhsoaHandleRef || !rhsoaHandleTar) {
103104 edm::LogWarning out (" SiPixelCompareRecHits" );
104- if (not rhsoaHandleRef) {
105+ if (! rhsoaHandleRef)
105106 out << " reference rechits not found; " ;
106- }
107- if (not rhsoaHandleTar) {
107+ if (!rhsoaHandleTar)
108108 out << " target rechits not found; " ;
109- }
110109 out << " the comparison will not run." ;
111110 return ;
112111 }
113112
114- auto const & rhsoaRef = *rhsoaHandleRef;
115- auto const & rhsoaTar = *rhsoaHandleTar;
113+ const auto & rhsoaRef = *rhsoaHandleRef;
114+ const auto & rhsoaTar = *rhsoaHandleTar;
116115
117116 auto const & soa2dRef = rhsoaRef.const_view ();
118117 auto const & soa2dTar = rhsoaTar.const_view ();
@@ -121,15 +120,28 @@ void SiPixelCompareRecHits<T>::analyzeSeparate(U tokenRef, V tokenTar, const edm
121120 uint32_t nHitsTar = soa2dTar.metadata ().size ();
122121
123122 hnHits_->Fill (nHitsRef, nHitsTar);
123+
124+ // Map detector indices to target hits for quick access
125+ std::unordered_map<uint16_t , std::vector<size_t >> detectorIndexMap;
126+ detectorIndexMap.reserve (nHitsTar);
127+ for (size_t j = 0 ; j < nHitsTar; ++j) {
128+ detectorIndexMap[soa2dTar[j].detectorIndex ()].push_back (j);
129+ }
130+
124131 auto detIds = tkGeom_->detUnitIds ();
132+
133+ // Loop through reference hits
125134 for (uint32_t i = 0 ; i < nHitsRef; i++) {
126135 float minD = mind2cut_;
127136 uint32_t matchedHit = invalidHit_;
128137 uint16_t indRef = soa2dRef[i].detectorIndex ();
129138 float xLocalRef = soa2dRef[i].xLocal ();
130139 float yLocalRef = soa2dRef[i].yLocal ();
131- for (uint32_t j = 0 ; j < nHitsTar; j++) {
132- if (soa2dTar.detectorIndex (j) == indRef) {
140+
141+ // Look up hits in target with matching detector index
142+ auto it = detectorIndexMap.find (indRef);
143+ if (it != detectorIndexMap.end ()) {
144+ for (auto j : it->second ) {
133145 float dx = xLocalRef - soa2dTar[j].xLocal ();
134146 float dy = yLocalRef - soa2dTar[j].yLocal ();
135147 float distance = dx * dx + dy * dy;
@@ -139,22 +151,29 @@ void SiPixelCompareRecHits<T>::analyzeSeparate(U tokenRef, V tokenTar, const edm
139151 }
140152 }
141153 }
154+
155+ // Gather reference hit properties
142156 DetId id = detIds[indRef];
143157 uint32_t chargeRef = soa2dRef[i].chargeAndStatus ().charge ;
144- int16_t sizeXRef = std::ceil (float (std::abs (soa2dRef[i].clusterSizeX ()) / 8 .));
145- int16_t sizeYRef = std::ceil (float (std::abs (soa2dRef[i].clusterSizeY ()) / 8 .));
158+ int16_t sizeXRef = (soa2dRef[i].clusterSizeX () + 7 ) / 8 ;
159+ int16_t sizeYRef = (soa2dRef[i].clusterSizeY () + 7 ) / 8 ;
160+
161+ // Initialize target hit properties
146162 uint32_t chargeTar = 0 ;
147163 int16_t sizeXTar = -99 ;
148164 int16_t sizeYTar = -99 ;
149165 float xLocalTar = -999 .;
150166 float yLocalTar = -999 .;
167+
151168 if (matchedHit != invalidHit_) {
152169 chargeTar = soa2dTar[matchedHit].chargeAndStatus ().charge ;
153- sizeXTar = std::ceil ( float ( std::abs ( soa2dTar[matchedHit].clusterSizeX ()) / 8 .)) ;
154- sizeYTar = std::ceil ( float ( std::abs ( soa2dTar[matchedHit].clusterSizeY ()) / 8 .)) ;
170+ sizeXTar = ( soa2dTar[matchedHit].clusterSizeX () + 7 ) / 8 ;
171+ sizeYTar = ( soa2dTar[matchedHit].clusterSizeY () + 7 ) / 8 ;
155172 xLocalTar = soa2dTar[matchedHit].xLocal ();
156173 yLocalTar = soa2dTar[matchedHit].yLocal ();
157174 }
175+
176+ // Populate histograms based on subdetector type
158177 switch (id.subdetId ()) {
159178 case PixelSubdetector::PixelBarrel:
160179 hBchargeL_[tTopo_->pxbLayer (id) - 1 ]->Fill (chargeRef, chargeTar);
0 commit comments