1515
1616#include < oneapi/tbb/parallel_for.h>
1717#include < oneapi/tbb/combinable.h>
18+ #include < oneapi/tbb/blocked_range.h>
19+ #include < oneapi/tbb/parallel_sort.h>
20+
1821#include < algorithm>
19- #include < iostream>
2022#include < iterator>
2123#include < limits>
2224#include < ranges>
23- #include < type_traits>
24-
25- #ifdef OPTIMISATION_OUTPUT
26- #include < format>
27- #include < fstream>
28- #endif
29-
30- #include < oneapi/tbb/blocked_range.h>
31- #include < oneapi/tbb/parallel_sort.h>
3225
3326#include " DetectorsRaw/HBFUtils.h"
3427#include " Steer/MCKinematicsReader.h"
3528#include " SimulationDataFormat/O2DatabasePDG.h"
3629#include " CommonConstants/MathConstants.h"
30+ #include " CommonUtils/TreeStreamRedirector.h"
3731#include " DetectorsBase/Propagator.h"
3832#include " ITSMFTBase/DPLAlpideParam.h"
3933#include " GPUCommonMath.h"
4640#include " ITStracking/IndexTableUtils.h"
4741#include " ITStracking/Tracklet.h"
4842#include " ReconstructionDataFormats/Track.h"
49- #include " CommonUtils/TreeStreamRedirector.h"
5043
51- using o2::base::PropagatorF;
44+ // optimization output
45+ #define OPTIMISATION_TRACKLETS (1 << 0 )
46+ #define OPTIMISATION (OPTIMISATION_TRACKLETS)
47+ // #define OPTIMISATION 0
48+ #define OPTIMISATION_ANY (OPTIMISATION != 0 )
49+ #define OPTIMISATION_SET (flag ) ((OPTIMISATION & (flag)) != 0 )
5250
5351namespace o2 ::its
5452{
53+ namespace
54+ {
55+ utils::TreeStreamRedirector* sDBGOut {nullptr };
56+ }
5557
5658struct PassMode {
5759 using OnePass = std::integral_constant<int , 0 >;
@@ -60,13 +62,26 @@ struct PassMode {
6062};
6163
6264template <int NLayers>
63- void TrackerTraits<NLayers>::computeLayerTracklets( const int iteration, const int iSlice, const int iVertex )
65+ TrackerTraits<NLayers>::TrackerTraits( )
6466{
65- #ifdef OPTIMISATION_OUTPUT
66- static int iter{0 };
67- std::ofstream off (std::format (" tracklets{}.txt" , iter++));
67+ #if OPTIMISATION_ANY
68+ sDBGOut = new utils::TreeStreamRedirector (" its_debug.root" );
6869#endif
70+ }
6971
72+ template <int NLayers>
73+ TrackerTraits<NLayers>::~TrackerTraits ()
74+ {
75+ #if OPTIMISATION_ANY
76+ sDBGOut ->Close ();
77+ delete sDBGOut ;
78+ sDBGOut = nullptr ;
79+ #endif
80+ }
81+
82+ template <int NLayers>
83+ void TrackerTraits<NLayers>::computeLayerTracklets(const int iteration, const int iSlice, const int iVertex)
84+ {
7085 for (int iLayer = 0 ; iLayer < mRecoParams [iteration].params .TrackletsPerRoad (); ++iLayer) {
7186 mTimeFrame ->getTracklets ()[iLayer].clear ();
7287 mTimeFrame ->getTrackletsLabel (iLayer).clear ();
@@ -133,8 +148,7 @@ void TrackerTraits<NLayers>::computeLayerTracklets(const int iteration, const in
133148 const float tanLambda = deltaZ * inverseR0;
134149 const float zAtRmin = tanLambda * (mTimeFrame ->getMinR (iLayer + 1 ) - currentCluster.radius ) + currentCluster.zCoordinate ;
135150 const float zAtRmax = tanLambda * (mTimeFrame ->getMaxR (iLayer + 1 ) - currentCluster.radius ) + currentCluster.zCoordinate ;
136- const float sqInvDeltaZ0 = (deltaZ2 < res2) ? res2 : 1 .f / (deltaZ2 + constants::Tolerance);
137- const float sigmaZ = o2::gpu::CAMath::Sqrt ((res2 + pvZ2) * math_utils::Sq (tanLambda) * ((math_utils::Sq (inverseR0) + sqInvDeltaZ0) * math_utils::Sq (meanDeltaR) + 1 .f ) + math_utils::Sq (meanDeltaR * mTimeFrame ->getMSangle (iLayer)));
151+ const float sigmaZ = o2::gpu::CAMath::Sqrt ((res2 + pvZ2) * math_utils::Sq (tanLambda) * math_utils::Sq ((inverseR0 * math_utils::Sq (meanDeltaR)) + 1 .f ) + math_utils::Sq (meanDeltaR * mTimeFrame ->getMSangle (iLayer)));
138152 const float nSigmaZ = sigmaZ * mRecoParams [iteration].params .NSigmaCutZ ;
139153 // phi-angle resolution
140154 const float sigmaPhi = o2::gpu::CAMath::Sqrt (pvR2 + math_utils::Sq (mTimeFrame ->getPhiCut (iLayer)));
@@ -174,28 +188,14 @@ void TrackerTraits<NLayers>::computeLayerTracklets(const int iteration, const in
174188 if (iteration && mTimeFrame ->isClusterUsed (iLayer + 1 , nextCluster.clusterId )) {
175189 continue ;
176190 }
177- float deltaPhi = o2::gpu::GPUCommonMath::Abs (currentCluster.phi - nextCluster.phi );
178- float deltaZ = o2::gpu::GPUCommonMath::Abs ((tanLambda * (nextCluster.radius - currentCluster.radius )) + currentCluster.zCoordinate - nextCluster.zCoordinate );
179191
180- #ifdef OPTIMISATION_OUTPUT
181- MCCompLabel label;
182- int currentId{currentCluster.clusterId };
183- int nextId{nextCluster.clusterId };
184- for (auto & lab1 : mTimeFrame ->getClusterLabels (iLayer, currentId)) {
185- for (auto & lab2 : mTimeFrame ->getClusterLabels (iLayer + 1 , nextId)) {
186- if (lab1 == lab2 && lab1.isValid ()) {
187- label = lab1;
188- break ;
189- }
190- }
191- if (label.isValid ()) {
192- break ;
193- }
194- }
195- off << std::format (" {}\t {:d}\t {}\t {}\t {}\t {}" , iLayer, label.isValid (), (tanLambda * (nextCluster.radius - currentCluster.radius ) + currentCluster.zCoordinate - nextCluster.zCoordinate ) / sigmaZ, tanLambda, resolution, sigmaZ) << std::endl;
196- #endif
192+ const float deltaPhi = o2::gpu::CAMath::Abs (o2::math_utils::toPMPi (currentCluster.phi - nextCluster.phi ));
193+ const float deltaZ = o2::gpu::CAMath::Abs ((tanLambda * (nextCluster.radius - currentCluster.radius )) + currentCluster.zCoordinate - nextCluster.zCoordinate );
197194
198- if (deltaZ < nSigmaZ && ((deltaPhi < nSigmaPhi || o2::gpu::GPUCommonMath::Abs (deltaPhi - o2::constants::math::TwoPI) < nSigmaPhi))) {
195+ const bool accept = deltaZ < nSigmaZ && o2::gpu::GPUCommonMath::Abs (deltaPhi) < nSigmaPhi;
196+ debugComputeLayerTracklets (iteration, iLayer, currentCluster, nextCluster, pv, sigmaZ, sigmaPhi, accept);
197+
198+ if (accept) {
199199 const float phi{o2::gpu::CAMath::ATan2 (currentCluster.yCoordinate - nextCluster.yCoordinate , currentCluster.xCoordinate - nextCluster.xCoordinate )};
200200 const float tanL = (currentCluster.zCoordinate - nextCluster.zCoordinate ) / (currentCluster.radius - nextCluster.radius );
201201
@@ -346,7 +346,7 @@ void TrackerTraits<NLayers>::computeLayerCells(const int iteration)
346346
347347 const float deltaTanLambda{std::abs (currentTracklet.tanLambda - nextTracklet.tanLambda )};
348348
349- #ifdef OPTIMISATION_OUTPUT
349+ #ifdef OPTIMISATION_OUTPUT_
350350 float resolution{o2::gpu::CAMath::Sqrt (0 .5f * (mRecoParams [iteration].params .SystErrorZ2 [iLayer] + mRecoParams [iteration].params .SystErrorZ2 [iLayer + 1 ] + mRecoParams [iteration].params .SystErrorZ2 [iLayer + 2 ] + mRecoParams [iteration].params .SystErrorY2 [iLayer] + mRecoParams [iteration].params .SystErrorY2 [iLayer + 1 ] + mRecoParams [iteration].params .SystErrorY2 [iLayer + 2 ])) / mRecoParams [iteration].params .LayerResolution [iLayer]};
351351 resolution = resolution > 1 .e -12 ? resolution : 1 .f ;
352352 bool good{mTimeFrame ->getTrackletsLabel (iLayer)[iTracklet] == mTimeFrame ->getTrackletsLabel (iLayer + 1 )[iNextTracklet]};
@@ -786,7 +786,7 @@ void TrackerTraits<NLayers>::findCellSeeds(const int iteration)
786786template <int NLayers>
787787void TrackerTraits<NLayers>::findCellsNeighbours(const int iteration)
788788{
789- #ifdef OPTIMISATION_OUTPUT
789+ #ifdef OPTIMISATION_OUTPUT_
790790 std::ofstream off (std::format (" cellneighs{}.txt" , iteration));
791791#endif
792792
@@ -833,7 +833,7 @@ void TrackerTraits<NLayers>::findCellsNeighbours(const int iteration)
833833 }
834834 float chi2 = currentCellSeed.getPredictedChi2 (nextCellSeed); // / TODO: switch to the chi2 wrt cluster to avoid correlation
835835
836- #ifdef OPTIMISATION_OUTPUT
836+ #ifdef OPTIMISATION_OUTPUT_
837837 bool good{mTimeFrame ->getCellsLabel (iLayer)[iCell] == mTimeFrame ->getCellsLabel (iLayer + 1 )[iNextCell]};
838838 off << std::format (" {}\t {:d}\t {}" , iLayer, good, chi2) << std::endl;
839839#endif
@@ -1437,12 +1437,12 @@ bool TrackerTraits<NLayers>::trackFollowing(TrackITSExt* track, int rof, bool ou
14371437 }
14381438 // estimate hypo's trk parameters at that x
14391439 auto & hypoParam{outward ? hypo.getParamOut () : hypo.getParamIn ()};
1440- if (!propInstance->propagateToX (hypoParam, x, mTimeFrame ->getBz (), PropagatorF::MAX_SIN_PHI,
1441- PropagatorF::MAX_STEP, mRecoParams [iteration].params .CorrType )) {
1440+ if (!propInstance->propagateToX (hypoParam, x, mTimeFrame ->getBz (), base:: PropagatorF::MAX_SIN_PHI,
1441+ base:: PropagatorF::MAX_STEP, mRecoParams [iteration].params .CorrType )) {
14421442 continue ;
14431443 }
14441444
1445- if (mRecoParams [iteration].params .CorrType == PropagatorF::MatCorrType::USEMatCorrNONE) { // account for material affects if propagator does not
1445+ if (mRecoParams [iteration].params .CorrType == base:: PropagatorF::MatCorrType::USEMatCorrNONE) { // account for material affects if propagator does not
14461446 if (!hypoParam.correctForMaterial (mRecoParams [iteration].params .LayerxX0 [iLayer], mRecoParams [iteration].params .LayerxX0 [iLayer] * constants::Radl * constants::Rho, true )) {
14471447 continue ;
14481448 }
@@ -1496,7 +1496,7 @@ bool TrackerTraits<NLayers>::trackFollowing(TrackITSExt* track, int rof, bool ou
14961496 }
14971497
14981498 if (!propInstance->propagateToX (tbuParams, trackingHit.xTrackingFrame , mTimeFrame ->getBz (),
1499- PropagatorF::MAX_SIN_PHI, PropagatorF::MAX_STEP, PropagatorF::MatCorrType::USEMatCorrNONE)) {
1499+ base:: PropagatorF::MAX_SIN_PHI, base:: PropagatorF::MAX_STEP, base:: PropagatorF::MatCorrType::USEMatCorrNONE)) {
15001500 continue ;
15011501 }
15021502
@@ -1641,7 +1641,8 @@ void TrackerTraits<NLayers>::updateTrackingParameters(const std::vector<RecoIter
16411641template <int NLayers>
16421642void TrackerTraits<NLayers>::setNThreads(int n, std::shared_ptr<tbb::task_arena>& arena)
16431643{
1644- #if defined(OPTIMISATION_OUTPUT) || defined(CA_DEBUG)
1644+ #if OPTIMISATION_ANY
1645+ LOGP (info, " Debug output enabled; enforcing single-threaded" );
16451646 mTaskArena = std::make_shared<tbb::task_arena>(1 );
16461647#else
16471648 if (arena == nullptr ) {
@@ -1654,6 +1655,62 @@ void TrackerTraits<NLayers>::setNThreads(int n, std::shared_ptr<tbb::task_arena>
16541655#endif
16551656}
16561657
1658+ // / Debug dumps
1659+ template <int NLayers>
1660+ inline void TrackerTraits<NLayers>::debugComputeLayerTracklets(int iteration, int layer, const Cluster& currentCls, const Cluster& nextCls, const Vertex& pv, float sigmaZ, float sigmaPhi, bool accepted)
1661+ {
1662+ #if !(OPTIMISATION_SET(OPTIMISATION_TRACKLETS))
1663+ return ; // no-op
1664+ #endif
1665+
1666+ static LogLogThrottler logger;
1667+ LOG_IF (info, logger.needToLog (iteration, layer)) << " debugTree: LayerTracklets:" << iteration << " :" << layer << " dumped entries " << logger.evCount ;
1668+
1669+ static uint32_t cnt{0 };
1670+ if ((++cnt % 50 ) != 0 ) {
1671+ return ;
1672+ }
1673+
1674+ MCCompLabel label;
1675+ if (mTimeFrame ->hasMCinformation ()) {
1676+ int currentId{currentCls.clusterId };
1677+ int nextId{nextCls.clusterId };
1678+ for (auto & lab1 : mTimeFrame ->getClusterLabels (layer, currentId)) {
1679+ for (auto & lab2 : mTimeFrame ->getClusterLabels (layer + 1 , nextId)) {
1680+ if (lab1 == lab2 && lab1.isValid ()) {
1681+ label = lab1;
1682+ break ;
1683+ }
1684+ }
1685+ if (label.isValid ()) {
1686+ break ;
1687+ }
1688+ }
1689+ }
1690+
1691+ const float deltaPvZ = currentCls.zCoordinate - pv.getZ ();
1692+ const float tanLambda = deltaPvZ / currentCls.radius ;
1693+ const float deltaZ = o2::gpu::GPUCommonMath::Abs ((tanLambda * (nextCls.radius - currentCls.radius )) + currentCls.zCoordinate - nextCls.zCoordinate );
1694+ const float tglDiff = ((nextCls.zCoordinate - currentCls.zCoordinate ) / (nextCls.radius - currentCls.radius )) - tanLambda;
1695+ const float deltaPhi = o2::gpu::CAMath::Abs (o2::math_utils::toPMPi (currentCls.phi - nextCls.phi ));
1696+ (*sDBGOut ) << " tracklets"
1697+ << " iter=" << iteration
1698+ << " lay=" << layer
1699+ << " lbl=" << label
1700+ << " pv=" << pv
1701+ << " curCls=" << currentCls
1702+ << " nextCls=" << nextCls
1703+ << " sigZ=" << sigmaZ
1704+ << " delZ=" << deltaZ
1705+ << " sigPhi=" << sigmaPhi
1706+ << " delPhi=" << deltaPhi
1707+ << " tgl=" << tanLambda
1708+ << " tglDiff=" << tglDiff
1709+ << " acc=" << accepted
1710+ << " \n " ;
1711+ }
1712+
1713+ // explicitly instaniate the ITS2/ITS3 tracker functions
16571714template class TrackerTraits <7 >;
16581715
16591716} // namespace o2::its
0 commit comments