diff --git a/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/Tracker.h b/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/Tracker.h index f711b707419ad..72d9258554ea1 100644 --- a/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/Tracker.h +++ b/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/Tracker.h @@ -47,7 +47,7 @@ class Tracker : public TrackerConfig public: Tracker(bool useMC); - ~Tracker() = default; + ~Tracker(); Tracker(const Tracker&) = delete; Tracker& operator=(const Tracker&) = delete; diff --git a/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/TrackerConfig.h b/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/TrackerConfig.h index beecde239eec7..fe5fa2bbe13db 100644 --- a/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/TrackerConfig.h +++ b/Detectors/ITSMFT/MFT/tracking/include/MFTTracking/TrackerConfig.h @@ -25,6 +25,10 @@ namespace mft { using namespace constants::mft; +using BinContainer = std::array, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)>; +using RArray = std::array; +using PhiArray = std::array; +using InverseRArray = std::array; class TrackerConfig { @@ -39,9 +43,7 @@ class TrackerConfig const Int_t getPhiBinIndex(const Float_t phi) const; const Int_t getBinIndex(const Int_t rIndex, const Int_t phiIndex) const; - // tracking configuration parameters - const auto& getBinsS() { return mBinsS; } - const auto& getBins() { return mBins; } + static void initBinContainers(); const std::pair& getClusterBinIndexRange(Int_t layerId, Int_t bin) const { return mClusterBinIndexRange[layerId][bin]; } @@ -67,13 +69,16 @@ class TrackerConfig bool mFullClusterScan = false; Float_t mTrueTrackMCThreshold; // Minimum fraction of correct MC labels to tag True tracks + static std::mutex sTCMutex; + static Float_t mPhiBinSize; static Float_t mInversePhiBinSize; - static std::array mPhiBinWin; - static std::array mRBinSize; - static std::array mInverseRBinSize; - static std::array, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> mBins; - static std::array, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> mBinsS; + + static std::unique_ptr mInverseRBinSize; + static std::unique_ptr mPhiBinWin; + static std::unique_ptr mRBinSize; + static std::unique_ptr mBins; + static std::unique_ptr mBinsS; std::array, constants::index_table::MaxRPhiBins>, constants::mft::LayersNumber> mClusterBinIndexRange; ClassDefNV(TrackerConfig, 3); @@ -81,15 +86,16 @@ class TrackerConfig inline Float_t TrackerConfig::mPhiBinSize; inline Float_t TrackerConfig::mInversePhiBinSize; -inline std::array TrackerConfig::mPhiBinWin; -inline std::array TrackerConfig::mRBinSize; -inline std::array TrackerConfig::mInverseRBinSize; -inline std::array, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> TrackerConfig::mBins; -inline std::array, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> TrackerConfig::mBinsS; + +inline std::unique_ptr TrackerConfig::mInverseRBinSize; +inline std::unique_ptr TrackerConfig::mPhiBinWin; +inline std::unique_ptr TrackerConfig::mRBinSize; +inline std::unique_ptr TrackerConfig::mBins; +inline std::unique_ptr TrackerConfig::mBinsS; inline const Int_t TrackerConfig::getRBinIndex(const Float_t r, const Int_t layer) const { - return (Int_t)((r - constants::index_table::RMin[layer]) * mInverseRBinSize[layer]); + return (Int_t)((r - constants::index_table::RMin[layer]) * (*mInverseRBinSize)[layer]); } inline const Int_t TrackerConfig::getPhiBinIndex(const Float_t phi) const diff --git a/Detectors/ITSMFT/MFT/tracking/src/Tracker.cxx b/Detectors/ITSMFT/MFT/tracking/src/Tracker.cxx index 5bb227e301301..85fe43a2b2758 100644 --- a/Detectors/ITSMFT/MFT/tracking/src/Tracker.cxx +++ b/Detectors/ITSMFT/MFT/tracking/src/Tracker.cxx @@ -25,6 +25,8 @@ namespace o2 namespace mft { +std::mutex TrackerConfig::sTCMutex; + //_________________________________________________________________________________________________ template Tracker::Tracker(bool useMC) : mUseMC{useMC} @@ -113,6 +115,13 @@ void Tracker::initializeFinder() return; } + // The lock will prevent executing the code below at the same time for different tracker copes (one will wait for other) + std::lock_guard guard(TrackerConfig::sTCMutex); + if (mBins) { + return; + } + TrackerConfig::initBinContainers(); + /// calculate Look-Up-Table of the R-Phi bins projection from one layer to another /// layer1 + global R-Phi bin index ---> layer2 + R bin index + Phi bin index /// To be executed by the first tracker in case of multiple threads @@ -122,14 +131,14 @@ void Tracker::initializeFinder() // Needs to be executed only once since it is filling static data members used by all tracker threads mPhiBinSize = (constants::index_table::PhiMax - constants::index_table::PhiMin) / mPhiBins; mInversePhiBinSize = 1.0 / mPhiBinSize; - mRBinSize[layer] = (constants::index_table::RMax[layer] - constants::index_table::RMin[layer]) / mRBins; - mInverseRBinSize[layer] = 1.0 / mRBinSize[layer]; + (*mRBinSize)[layer] = (constants::index_table::RMax[layer] - constants::index_table::RMin[layer]) / mRBins; + (*mInverseRBinSize)[layer] = 1.0 / (*mRBinSize)[layer]; auto ZL0 = LayerZCoordinate()[0]; auto deltaZ = (abs(LayerZCoordinate()[layer]) - abs(ZL0)); auto binArcLenght = constants::index_table::RMin[layer] * o2::constants::math::TwoPI / mPhiBins; Float_t NconicalBins = 2.0 * deltaZ * mRCutAtZmin / (abs(ZL0) + mZVtxMin) / binArcLenght; - mPhiBinWin[layer] = std::max(3, int(ceil(NconicalBins))); - LOG(debug) << "mPhiBinWin[" << layer << "] = " << mPhiBinWin[layer] << std::endl; + (*mPhiBinWin)[layer] = std::max(3, int(ceil(NconicalBins))); + LOG(debug) << "mPhiBinWin[" << layer << "] = " << (*mPhiBinWin)[layer] << std::endl; } Float_t dz, x, y, r, phi, x_proj, y_proj, r_proj, phi_proj, zLayer1, zLayer2; @@ -141,7 +150,7 @@ void Tracker::initializeFinder() for (Int_t iRBin = 0; iRBin < mRBins; ++iRBin) { bool isFirstPhiBin = true; - r = (iRBin + 0.5) * mRBinSize[layer1] + constants::index_table::RMin[layer1]; + r = (iRBin + 0.5) * (*mRBinSize)[layer1] + constants::index_table::RMin[layer1]; for (Int_t iPhiBin = 0; iPhiBin < mPhiBins; ++iPhiBin) { isFirstPhiBin = !iPhiBin; @@ -167,7 +176,7 @@ void Tracker::initializeFinder() binR_proj = getRBinIndex(r_proj, layer2); binPhi_proj = getPhiBinIndex(phi_proj); - int binwPhiS = mPhiBinWin[layer2]; + int binwPhiS = (*mPhiBinWin)[layer2]; int binhwPhiS = binwPhiS / 2; float rMin = r * (mZVtxMax + abs(zLayer2)) / (mZVtxMax + abs(zLayer1)); @@ -195,11 +204,11 @@ void Tracker::initializeFinder() } binIndex2S = getBinIndex(binR, binPhiS); - mBinsS[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2S); + (*mBinsS.get())[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2S); } } - int binwPhi = mPhiBinWin[layer2]; + int binwPhi = (*mPhiBinWin)[layer2]; int binhwPhi = binwPhi / 2; for (Int_t binR = rBinMin; binR <= rBinMax; ++binR) { @@ -211,7 +220,7 @@ void Tracker::initializeFinder() } binIndex2 = getBinIndex(binR, binPhi); - mBins[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2); + (*mBins.get())[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2); } } @@ -288,7 +297,7 @@ void Tracker::findTracksLTF(ROframe& event) clsInLayer1 = it1 - event.getClustersInLayer(layer1).begin(); // loop over the bins in the search window - for (const auto& binS : getBinsS()[layer1][layer2 - 1][cluster1.indexTableBin]) { + for (const auto& binS : (*mBinsS.get())[layer1][layer2 - 1][cluster1.indexTableBin]) { getBinClusterRange(event, layer2, binS, clsMinIndexS, clsMaxIndexS); @@ -318,7 +327,7 @@ void Tracker::findTracksLTF(ROframe& event) // loop over the bins in the search window dR2min = mLTFConeRadius ? dR2cut * dRCone * dRCone : dR2cut; - for (const auto& bin : getBins()[layer1][layer - 1][cluster1.indexTableBin]) { + for (const auto& bin : (*mBins.get())[layer1][layer - 1][cluster1.indexTableBin]) { getBinClusterRange(event, layer, bin, clsMinIndex, clsMaxIndex); @@ -567,7 +576,7 @@ void Tracker::findTracksCA(ROframe& event) clsInLayer1 = it1 - event.getClustersInLayer(layer1).begin(); // loop over the bins in the search window - for (const auto& binS : getBinsS()[layer1][layer2 - 1][cluster1.indexTableBin]) { + for (const auto& binS : (*mBinsS.get())[layer1][layer2 - 1][cluster1.indexTableBin]) { getBinClusterRange(event, layer2, binS, clsMinIndexS, clsMaxIndexS); @@ -593,7 +602,7 @@ void Tracker::findTracksCA(ROframe& event) dR2min = mLTFConeRadius ? dR2cut * dRCone * dRCone : dR2cut; // loop over the bins in the search window - for (const auto& bin : getBins()[layer1][layer - 1][cluster1.indexTableBin]) { + for (const auto& bin : (*mBins.get())[layer1][layer - 1][cluster1.indexTableBin]) { getBinClusterRange(event, layer, bin, clsMinIndex, clsMaxIndex); @@ -1080,6 +1089,21 @@ bool Tracker::fitTracks(ROframe& event) return true; } +//_________________________________________________________________________________________________ +template +Tracker::~Tracker() +{ + // Mutex used here to avoid race condition + std::lock_guard guard(TrackerConfig::sTCMutex); + // Deallocate the memory that was previously reserved for these arrays + TrackerConfig::mBins.reset(); + TrackerConfig::mBinsS.reset(); + + TrackerConfig::mRBinSize.reset(); + TrackerConfig::mPhiBinWin.reset(); + TrackerConfig::mInverseRBinSize.reset(); +} + template class Tracker; template class Tracker; diff --git a/Detectors/ITSMFT/MFT/tracking/src/TrackerConfig.cxx b/Detectors/ITSMFT/MFT/tracking/src/TrackerConfig.cxx index 053b899b0970c..62c0cb3bc1b90 100644 --- a/Detectors/ITSMFT/MFT/tracking/src/TrackerConfig.cxx +++ b/Detectors/ITSMFT/MFT/tracking/src/TrackerConfig.cxx @@ -42,4 +42,24 @@ void o2::mft::TrackerConfig::initialize(const MFTTrackingParam& trkParam) mTrueTrackMCThreshold = trkParam.TrueTrackMCThreshold; assert(mRPhiBins < constants::index_table::MaxRPhiBins && "Track finder binning overflow"); -} \ No newline at end of file +} + +//__________________________________________________________________________ +void o2::mft::TrackerConfig::initBinContainers() +{ + if (!mBins) { + mBins = std::make_unique(); + } + if (!mBinsS) { + mBinsS = std::make_unique(); + } + if (!mRBinSize) { + mRBinSize = std::make_unique(); + } + if (!mPhiBinWin) { + mPhiBinWin = std::make_unique(); + } + if (!mInverseRBinSize) { + mInverseRBinSize = std::make_unique(); + } +}