|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#ifndef ALICEO2_ITS_CA_PUBLICATIONADAPTER_H_ |
| 13 | +#define ALICEO2_ITS_CA_PUBLICATIONADAPTER_H_ |
| 14 | + |
| 15 | +#ifndef GPUCA_GPUCODE |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <cstdint> |
| 19 | +#include <cmath> |
| 20 | +#include <limits> |
| 21 | +#include <optional> |
| 22 | +#include <vector> |
| 23 | + |
| 24 | +#include "DetectorsCommonDataFormats/DetID.h" |
| 25 | +#include "GPUCommonMath.h" |
| 26 | +#include "ITSMFTTracking/detail/ITSSharedClusterCompatibility.h" |
| 27 | +#include "ITSMFTTracking/GenericTrack.h" |
| 28 | +#include "ITSMFTTracking/TimeFrame.h" |
| 29 | +#include "ITSMFTTracking/MathUtils.h" |
| 30 | + |
| 31 | +namespace o2::its::ca |
| 32 | +{ |
| 33 | + |
| 34 | +// Workflow-owned ITS compatibility for generic tracking results. |
| 35 | +class PublicationAdapter |
| 36 | +{ |
| 37 | + public: |
| 38 | + void adoptITSSharedClusterCompatibility(o2::itsmft::tracking::ITSSharedClusterCompatibility* sidecar) noexcept { mSidecar = sidecar; } |
| 39 | + o2::itsmft::tracking::ITSSharedClusterCompatibility* getITSSharedClusterCompatibility() const noexcept { return mSidecar; } |
| 40 | + |
| 41 | + bool completeAccepted(gsl::span<const uint32_t> trackIndices, |
| 42 | + const o2::itsmft::IterationParameters& params, |
| 43 | + const o2::itsmft::tracking::TimeFrame& frame, |
| 44 | + bool final) |
| 45 | + { |
| 46 | + if (mSidecar == nullptr) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + if (!stageSharedClusterFlags(trackIndices, params, frame)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + return !final || mSidecar->replaceFromAcceptedTrackIndices(mAcceptedTrackIndices, mSharedClusterFlags); |
| 53 | + } |
| 54 | + |
| 55 | + void reset() noexcept |
| 56 | + { |
| 57 | + mSharedClusterFlags.clear(); |
| 58 | + mAcceptedTrackIndices.clear(); |
| 59 | + if (mSidecar != nullptr) { |
| 60 | + mSidecar->clear(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + class Cleanup |
| 65 | + { |
| 66 | + public: |
| 67 | + explicit Cleanup(PublicationAdapter& adapter) : mAdapter(adapter) { mAdapter.reset(); } |
| 68 | + Cleanup(const Cleanup&) = delete; |
| 69 | + Cleanup& operator=(const Cleanup&) = delete; |
| 70 | + ~Cleanup() noexcept { mAdapter.reset(); } |
| 71 | + |
| 72 | + private: |
| 73 | + PublicationAdapter& mAdapter; |
| 74 | + }; |
| 75 | + Cleanup cleanupOnExit() { return Cleanup{*this}; } |
| 76 | + |
| 77 | + private: |
| 78 | + struct SharedClusterTrackInfo { |
| 79 | + int layer{-1}; |
| 80 | + uint32_t clusterId{std::numeric_limits<uint32_t>::max()}; |
| 81 | + int rof{-1}; |
| 82 | + float phi{0.f}; |
| 83 | + float eta{0.f}; |
| 84 | + int charge{0}; |
| 85 | + }; |
| 86 | + |
| 87 | + static std::optional<SharedClusterTrackInfo> makeSharedClusterTrackInfo(const o2::itsmft::tracking::GenericTrack& track, |
| 88 | + const o2::itsmft::tracking::TimeFrame& frame) |
| 89 | + { |
| 90 | + const int layer = track.hitLayers.first(); |
| 91 | + const auto& references = frame.getTrackClusterIndices(); |
| 92 | + if (layer < 0 || !isValidTrackRange(track, static_cast<uint32_t>(references.size())) || |
| 93 | + track.firstClusterRef == track.clusterRefEnd || |
| 94 | + static_cast<std::size_t>(layer) >= frame.getLayout().size()) { |
| 95 | + return std::nullopt; |
| 96 | + } |
| 97 | + const auto& reference = references[track.firstClusterRef]; |
| 98 | + if (reference.layer != o2::itsmft::tracking::LayerId{static_cast<uint16_t>(layer)} || !reference.isValid()) { |
| 99 | + return std::nullopt; |
| 100 | + } |
| 101 | + const auto& state = track.innerState; |
| 102 | + if (!state.hasRecognizedKind() || !o2::gpu::GPUCommonMath::Finite(state.parameters[3]) || |
| 103 | + !o2::gpu::GPUCommonMath::Finite(state.parameters[4])) { |
| 104 | + return std::nullopt; |
| 105 | + } |
| 106 | + const float phi = state.kind == o2::itsmft::tracking::SurfaceKind::Cylinder ? std::asin(state.parameters[2]) + state.alpha : state.parameters[2]; |
| 107 | + const float eta = std::asinh(state.parameters[3]); |
| 108 | + if (!o2::gpu::GPUCommonMath::Finite(phi) || !o2::gpu::GPUCommonMath::Finite(eta)) { |
| 109 | + return std::nullopt; |
| 110 | + } |
| 111 | + return SharedClusterTrackInfo{layer, reference.clusterId, frame.getClusterROF(layer, static_cast<int>(reference.clusterId)), |
| 112 | + phi, eta, state.parameters[4] < 0.f ? -1 : 1}; |
| 113 | + } |
| 114 | + |
| 115 | + bool stageSharedClusterFlags(gsl::span<const uint32_t> trackIndices, |
| 116 | + const o2::itsmft::IterationParameters& params, |
| 117 | + const o2::itsmft::tracking::TimeFrame& frame) |
| 118 | + { |
| 119 | + mAcceptedTrackIndices.reserve(mAcceptedTrackIndices.size() + trackIndices.size()); |
| 120 | + for (const auto index : trackIndices) { |
| 121 | + if (index >= frame.getGenericTracks().size() || |
| 122 | + (!mAcceptedTrackIndices.empty() && mAcceptedTrackIndices.back() >= index)) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + mAcceptedTrackIndices.push_back(index); |
| 126 | + } |
| 127 | + if (!trackIndices.empty() && mSharedClusterFlags.size() <= trackIndices.back()) { |
| 128 | + mSharedClusterFlags.resize(static_cast<std::size_t>(trackIndices.back()) + 1, 0); |
| 129 | + } |
| 130 | + if (!params.AllowSharingFirstCluster) { |
| 131 | + return true; |
| 132 | + } |
| 133 | + std::vector<SharedClusterTrackInfo> trackInfo; |
| 134 | + trackInfo.reserve(trackIndices.size()); |
| 135 | + for (const auto index : trackIndices) { |
| 136 | + const auto info = makeSharedClusterTrackInfo(frame.getGenericTracks()[index], frame); |
| 137 | + if (!info) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + trackInfo.push_back(*info); |
| 141 | + } |
| 142 | + for (size_t first = 0; first < trackInfo.size(); ++first) { |
| 143 | + for (size_t second = first + 1; second < trackInfo.size(); ++second) { |
| 144 | + if (trackInfo[second].layer != trackInfo[first].layer || trackInfo[second].clusterId != trackInfo[first].clusterId) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + if (trackInfo[first].rof != trackInfo[second].rof) { |
| 148 | + continue; |
| 149 | + } |
| 150 | + if (!o2::its::math_utils::isPhiDifferenceBelow(trackInfo[first].phi, trackInfo[second].phi, params.SharedClusterMaxDeltaPhi)) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + if (std::abs(trackInfo[first].eta - trackInfo[second].eta) > params.SharedClusterMaxDeltaEta) { |
| 154 | + continue; |
| 155 | + } |
| 156 | + if (params.SharedClusterOppositeSign && trackInfo[first].charge == trackInfo[second].charge) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + mSharedClusterFlags[trackIndices[first]] = 1; |
| 160 | + mSharedClusterFlags[trackIndices[second]] = 1; |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + o2::itsmft::tracking::ITSSharedClusterCompatibility* mSidecar = nullptr; |
| 167 | + std::vector<uint8_t> mSharedClusterFlags; |
| 168 | + std::vector<uint32_t> mAcceptedTrackIndices; |
| 169 | +}; |
| 170 | + |
| 171 | +} // namespace o2::its::ca |
| 172 | + |
| 173 | +#endif // !GPUCA_GPUCODE |
| 174 | + |
| 175 | +#endif // ALICEO2_ITS_CA_PUBLICATIONADAPTER_H_ |
0 commit comments