@@ -351,16 +351,21 @@ struct PidNSigma {
351351 // / @brief Encode 0.2 sigma interval to 0~10 range
352352 static uint8_t encodeNSigma (float nSigma)
353353 {
354- float encoded = std::abs ((nSigma - 1.5 ) / 0.2 ); // Convert to 0~10 range
355- encoded = std::min (std::max (encoded, 0 .f ), 10 .f ); // Clamp to 0~10 range
356- return (uint8_t )round (encoded);
354+ const float x = std::abs (nSigma);
355+ if (x <= 1.5 ) return 0 ; // Return 0 when absolute nSigma is smaller than 1.5
356+ float t = (x - 1.5 ) / 0.2 ;
357+ int encoded = static_cast <int >(std::ceil (t)); // (1.5,1.7]->1, ..., (3.3,3.5]->10
358+ if (encoded < 1 ) encoded = 1 ;
359+ if (encoded > 10 ) encoded = 10 ;
360+ return static_cast <uint8_t >(encoded);
357361 }
358362
359363 // / @brief Decode 0~10 value to original 1.5~3.5 sigma range
360364 static float decodeNSigma (uint8_t encoded)
361365 {
362- encoded = std::min (encoded, (uint8_t )10 ); // Safety check, should not be needed if encode is used properly
363- return (encoded * 0.2 ) + 1.5 ;
366+ if (encoded == 0 ) return 1.5 ;
367+ if (encoded > 10 ) encoded = 10 ;
368+ return 1.5 + static_cast <float >(encoded) * 0.2 ;
364369 }
365370
366371 // / @brief Check if TOF info is available
@@ -416,14 +421,15 @@ struct ResoMicroTrackSelFlag {
416421 flag = (DCAxyEncoded << 4 ) | DCAzEncoded; // Upper 4 bits = DCAxy, Lower 4 bits = DCAz
417422 }
418423
419- // / @brief Convert DCA to 1~15 steps (0 value is not used )
424+ // / @brief Convert DCA to 1~15 steps (|DCA|<0.1 is saved in 0 )
420425 static uint8_t encodeDCA (float DCA)
421426 {
422- for (uint8_t i = 1 ; i < 15 ; i++) {
423- if (DCA < i * 0 .1f )
424- return i;
425- }
426- return 15 ;
427+ float x = std::fabs (DCA);
428+ if (x < 0.1 ) return 0 ;
429+ int encoded = static_cast <int >(std::ceil ((x - 0.1 ) / 0.1 )); // (0.1, 0.2] -> 1, ..., (1.4, 1.5] -> 14
430+ if (encoded < 1 ) encoded = 1 ;
431+ if (encoded > 14 ) encoded = 15 ;
432+ return static_cast <uint8_t >(encoded);
427433 }
428434
429435 // / @brief Operator to convert to `uint8_t` (for SOA storage)
0 commit comments