Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MFT] Move static arrays to be initialized before the processing starts #10665

Merged
merged 5 commits into from
Feb 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Tracker : public TrackerConfig

public:
Tracker(bool useMC);
~Tracker() = default;
~Tracker();

Tracker(const Tracker&) = delete;
Tracker& operator=(const Tracker&) = delete;
Expand Down
34 changes: 20 additions & 14 deletions Detectors/ITSMFT/MFT/tracking/include/MFTTracking/TrackerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ namespace mft
{

using namespace constants::mft;
using BinContainer = std::array<std::array<std::array<std::vector<Int_t>, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)>;
using RArray = std::array<Float_t, constants::mft::LayersNumber>;
using PhiArray = std::array<Int_t, constants::mft::LayersNumber>;
using InverseRArray = std::array<Float_t, constants::mft::LayersNumber>;

class TrackerConfig
{
Expand All @@ -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<Int_t, Int_t>& getClusterBinIndexRange(Int_t layerId, Int_t bin) const { return mClusterBinIndexRange[layerId][bin]; }

Expand All @@ -67,29 +69,33 @@ 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<Int_t, constants::mft::LayersNumber> mPhiBinWin;
static std::array<Float_t, constants::mft::LayersNumber> mRBinSize;
static std::array<Float_t, constants::mft::LayersNumber> mInverseRBinSize;
static std::array<std::array<std::array<std::vector<Int_t>, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> mBins;
static std::array<std::array<std::array<std::vector<Int_t>, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> mBinsS;

static std::unique_ptr<InverseRArray> mInverseRBinSize;
static std::unique_ptr<PhiArray> mPhiBinWin;
static std::unique_ptr<RArray> mRBinSize;
static std::unique_ptr<BinContainer> mBins;
static std::unique_ptr<BinContainer> mBinsS;
std::array<std::array<std::pair<Int_t, Int_t>, constants::index_table::MaxRPhiBins>, constants::mft::LayersNumber> mClusterBinIndexRange;

ClassDefNV(TrackerConfig, 3);
};

inline Float_t TrackerConfig::mPhiBinSize;
inline Float_t TrackerConfig::mInversePhiBinSize;
inline std::array<Int_t, constants::mft::LayersNumber> TrackerConfig::mPhiBinWin;
inline std::array<Float_t, constants::mft::LayersNumber> TrackerConfig::mRBinSize;
inline std::array<Float_t, constants::mft::LayersNumber> TrackerConfig::mInverseRBinSize;
inline std::array<std::array<std::array<std::vector<Int_t>, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> TrackerConfig::mBins;
inline std::array<std::array<std::array<std::vector<Int_t>, constants::index_table::MaxRPhiBins>, (constants::mft::LayersNumber - 1)>, (constants::mft::LayersNumber - 1)> TrackerConfig::mBinsS;

inline std::unique_ptr<InverseRArray> TrackerConfig::mInverseRBinSize;
inline std::unique_ptr<PhiArray> TrackerConfig::mPhiBinWin;
inline std::unique_ptr<RArray> TrackerConfig::mRBinSize;
inline std::unique_ptr<BinContainer> TrackerConfig::mBins;
inline std::unique_ptr<BinContainer> 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
Expand Down
50 changes: 37 additions & 13 deletions Detectors/ITSMFT/MFT/tracking/src/Tracker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace o2
namespace mft
{

std::mutex TrackerConfig::sTCMutex;

//_________________________________________________________________________________________________
template <typename T>
Tracker<T>::Tracker(bool useMC) : mUseMC{useMC}
Expand Down Expand Up @@ -113,6 +115,13 @@ void Tracker<T>::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<std::mutex> 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
Expand All @@ -122,14 +131,14 @@ void Tracker<T>::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;
Expand All @@ -141,7 +150,7 @@ void Tracker<T>::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;
Expand All @@ -167,7 +176,7 @@ void Tracker<T>::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));
Expand Down Expand Up @@ -195,11 +204,11 @@ void Tracker<T>::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) {
Expand All @@ -211,7 +220,7 @@ void Tracker<T>::initializeFinder()
}

binIndex2 = getBinIndex(binR, binPhi);
mBins[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2);
(*mBins.get())[layer1][layer2 - 1][binIndex1].emplace_back(binIndex2);
}
}

Expand Down Expand Up @@ -288,7 +297,7 @@ void Tracker<T>::findTracksLTF(ROframe<T>& 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);

Expand Down Expand Up @@ -318,7 +327,7 @@ void Tracker<T>::findTracksLTF(ROframe<T>& 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);

Expand Down Expand Up @@ -567,7 +576,7 @@ void Tracker<T>::findTracksCA(ROframe<T>& 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);

Expand All @@ -593,7 +602,7 @@ void Tracker<T>::findTracksCA(ROframe<T>& 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);

Expand Down Expand Up @@ -1080,6 +1089,21 @@ bool Tracker<T>::fitTracks(ROframe<T>& event)
return true;
}

//_________________________________________________________________________________________________
template <typename T>
Tracker<T>::~Tracker()
{
// Mutex used here to avoid race condition
std::lock_guard<std::mutex> 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<o2::mft::TrackLTF>;
template class Tracker<o2::mft::TrackLTFL>;

Expand Down
22 changes: 21 additions & 1 deletion Detectors/ITSMFT/MFT/tracking/src/TrackerConfig.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

//__________________________________________________________________________
void o2::mft::TrackerConfig::initBinContainers()
{
if (!mBins) {
mBins = std::make_unique<BinContainer>();
}
if (!mBinsS) {
mBinsS = std::make_unique<BinContainer>();
}
if (!mRBinSize) {
mRBinSize = std::make_unique<RArray>();
}
if (!mPhiBinWin) {
mPhiBinWin = std::make_unique<PhiArray>();
}
if (!mInverseRBinSize) {
mInverseRBinSize = std::make_unique<InverseRArray>();
}
}