1717#include < limits>
1818#include < string>
1919#include < vector>
20+ #include < ranges>
21+
2022#ifndef GPUCA_GPUCODE
2123#include < format>
22-
2324#include " Framework/Logger.h"
2425#endif
26+
2527#include " CommonConstants/LHCConstants.h"
2628#include " CommonDataFormat/RangeReference.h"
2729#include " DataFormatsITS/TimeEstBC.h"
@@ -159,15 +161,19 @@ struct ROFOverlapTableView {
159161 return mLayers [layer];
160162 }
161163
162- GPUh () int getClock () const noexcept
164+ GPUh () int32_t getClock () const noexcept
163165 {
164166 // we take the fastest layer as clock
165- int fastest = 0 ;
166- uint32_t shortestROF{std::numeric_limits< uint32_t >:: max () };
167- for (int iL{0 }; iL < NLayers; ++iL) {
167+ int32_t fastest = 0 ;
168+ uint32_t maxNROFs{ 0 };
169+ for (int32_t iL{0 }; iL < NLayers; ++iL) {
168170 const auto & layer = getLayer (iL);
169- if (layer.mROFLength < shortestROF) {
171+ // by definition the fastest layer has the most ROFs
172+ // this also solves the problem of a delay large than ROFLength
173+ // if mNROFsTF is correct
174+ if (layer.mNROFsTF > maxNROFs) {
170175 fastest = iL;
176+ maxNROFs = layer.mNROFsTF ;
171177 }
172178 }
173179 return fastest;
@@ -524,7 +530,6 @@ class ROFVertexLookupTable : public LayerTimingBase<NLayers>
524530 using BCType = LayerTiming::BCType;
525531 using TableEntry = dataformats::RangeReference<T, T>;
526532 using TableIndex = dataformats::RangeReference<T, T>;
527-
528533 using View = ROFVertexLookupTableView<NLayers, TableEntry, TableIndex>;
529534
530535 ROFVertexLookupTable () = default ;
@@ -684,15 +689,15 @@ class ROFVertexLookupTable : public LayerTimingBase<NLayers>
684689};
685690
686691// GPU-friendly view of the ROF mask table
687- template <int32_t NLayers>
692+ template <int32_t NLayers, typename TableEntry, typename TableIndex >
688693struct ROFMaskTableView {
689- const uint8_t * mFlatMask {nullptr };
690- const int32_t * mLayerROFOffsets {nullptr }; // size NLayers+1
694+ const TableEntry * mFlatMask {nullptr };
695+ const TableIndex * mLayerROFOffsets {nullptr }; // size NLayers+1
691696
692697 GPUhdi () bool isROFEnabled (int32_t layer, int32_t rofId) const noexcept
693698 {
694699 assert (layer >= 0 && layer < NLayers);
695- return mFlatMask [mLayerROFOffsets [layer] + rofId] != 0 ;
700+ return mFlatMask [mLayerROFOffsets [layer] + rofId] != 0u ;
696701 }
697702
698703#ifndef GPUCA_GPUCODE
@@ -715,6 +720,23 @@ struct ROFMaskTableView {
715720 LOGF (info, " %*d | %*d" , w_rof, i, w_active, (int )isROFEnabled (layer, i));
716721 }
717722 }
723+
724+ GPUh () std::string asString(int32_t layer) const
725+ {
726+ int32_t nROFs = mLayerROFOffsets [layer + 1 ] - mLayerROFOffsets [layer];
727+ int32_t enabledROFs = 0 ;
728+ for (int32_t j = 0 ; j < nROFs; ++j) {
729+ if (isROFEnabled (layer, j)) {
730+ ++enabledROFs;
731+ }
732+ }
733+ return std::format (" ROFMask on Layer {} ROFs enabled: {}/{}" , layer, enabledROFs, nROFs);
734+ }
735+
736+ GPUh () void print(int32_t layer) const
737+ {
738+ LOG (info) << asString (layer);
739+ }
718740#endif
719741};
720742
@@ -723,10 +745,13 @@ template <int32_t NLayers>
723745class ROFMaskTable : public LayerTimingBase <NLayers>
724746{
725747 public:
726- using BCRange = dataformats::RangeReference<LayerTiming::BCType, LayerTiming::BCType>;
727- using View = ROFMaskTableView<NLayers>;
748+ using T = LayerTimingBase<NLayers>::T;
749+ using BCRange = dataformats::RangeReference<T, T>;
750+ using TableIndex = uint32_t ;
751+ using TableEntry = uint8_t ;
752+ using View = ROFMaskTableView<NLayers, TableEntry, TableIndex>;
728753
729- GPUdDefault () ROFMaskTable() = default ;
754+ ROFMaskTable () = default ;
730755 GPUh () explicit ROFMaskTable (const LayerTimingBase<NLayers>& timingBase) : LayerTimingBase<NLayers>(timingBase) { init (); }
731756
732757 GPUh () void init ()
@@ -737,13 +762,11 @@ class ROFMaskTable : public LayerTimingBase<NLayers>
737762 totalROFs += this ->getLayer (layer).mNROFsTF ;
738763 }
739764 mLayerROFOffsets [NLayers] = totalROFs; // sentinel
740- mFlatMask .resize (totalROFs, 1 );
765+ mFlatMask .resize (totalROFs, 0u );
741766 }
742767
743768 GPUh () size_t getFlatMaskSize () const noexcept { return mFlatMask .size (); }
744769
745- GPUh () bool isROFEnabled (int32_t layer, int32_t rofId) const noexcept { return mFlatMask [mLayerROFOffsets [layer] + rofId] != 0 ; }
746-
747770 GPUh () void setROFEnabled (int32_t layer, int32_t rofId, uint8_t state = 1 ) noexcept
748771 {
749772 assert (layer >= 0 && layer < NLayers);
@@ -770,7 +793,7 @@ class ROFMaskTable : public LayerTimingBase<NLayers>
770793 for (int32_t rofId{0 }; rofId < lay.mNROFsTF ; ++rofId) {
771794 if (static_cast <int32_t >(lay.getROFStartInBC (rofId)) < bcEnd &&
772795 static_cast <int32_t >(lay.getROFEndInBC (rofId)) > bcStart) {
773- mFlatMask [offset + rofId] = 1 ;
796+ mFlatMask [offset + rofId] = 1u ;
774797 }
775798 }
776799 }
@@ -785,7 +808,7 @@ class ROFMaskTable : public LayerTimingBase<NLayers>
785808 }
786809 }
787810
788- GPUh () void resetMask (uint8_t s = 0 )
811+ GPUh () void resetMask (uint8_t s = 0u )
789812 {
790813 std::memset (mFlatMask .data (), s, mFlatMask .size ());
791814 }
@@ -809,39 +832,17 @@ class ROFMaskTable : public LayerTimingBase<NLayers>
809832 return view;
810833 }
811834
812- GPUh () View getDeviceView (const uint8_t * deviceFlatMaskPtr, const int32_t * deviceOffsetPtr) const
835+ GPUh () View getDeviceView (const TableEntry * deviceFlatMaskPtr, const TableIndex * deviceOffsetPtr) const
813836 {
814837 View view;
815838 view.mFlatMask = deviceFlatMaskPtr;
816839 view.mLayerROFOffsets = deviceOffsetPtr;
817840 return view;
818841 }
819- #ifndef GPUCA_GPUCODE
820- GPUh () std::string asString() const
821- {
822- std::string mask_str;
823- for (int32_t i = 0 ; i < NLayers; ++i) {
824- int32_t nROFs = mLayerROFOffsets [i + 1 ] - mLayerROFOffsets [i];
825- int32_t enabledROFs = 0 ;
826- for (int32_t j = 0 ; j < nROFs; ++j) {
827- if (isROFEnabled (i, j)) {
828- ++enabledROFs;
829- }
830- }
831- mask_str += std::format (" Layer {} ROFs enabled: {}/{} | " , i, enabledROFs, nROFs);
832- }
833- return mask_str;
834- }
835-
836- GPUh () void print() const
837- {
838- LOG (info) << asString ();
839- }
840- #endif
841842
842843 private:
843- int32_t mLayerROFOffsets [NLayers + 1 ]{}; // NLayers entries + 1 sentinel
844- std::vector<uint8_t > mFlatMask ;
844+ TableIndex mLayerROFOffsets [NLayers + 1 ] = { 0 };
845+ std::vector<TableEntry > mFlatMask ;
845846};
846847
847848} // namespace o2::its
0 commit comments