Skip to content

Commit 87d1e6a

Browse files
committed
ITS: debug dump
Signed-off-by: Felix Schlepper <felix.schlepper@cern.ch>
1 parent 13e3780 commit 87d1e6a

8 files changed

Lines changed: 226 additions & 20 deletions

File tree

Detectors/ITSMFT/ITS/tracking/include/ITStracking/Definitions.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <type_traits>
1919
#include <cstdint>
2020

21+
#define ITS_ALLOW_DEBUG_TREES
22+
2123
namespace o2::its
2224
{
2325

@@ -26,10 +28,14 @@ enum class TrackletMode {
2628
Layer1Layer2 = 2
2729
};
2830

29-
template <bool IsConst, typename T>
30-
using maybe_const = typename std::conditional<IsConst, const T, T>::type;
31+
#ifdef ITS_ALLOW_DEBUG_TREES
32+
enum DebugTreeFlags : uint32_t {
33+
DebugTrackletCandidates = 1u << 0,
34+
DebugTracklets = 1u << 1,
35+
DebugCells = 1u << 2,
36+
};
3137

32-
// simple implemnetion of logging with exp. backoff
38+
// Simple implementation of logging with exponential backoff.
3339
struct LogLogThrottler {
3440
uint64_t evCount{0};
3541
uint64_t nextLog{1};
@@ -50,6 +56,7 @@ struct LogLogThrottler {
5056
return false;
5157
}
5258
};
59+
#endif
5360

5461
struct TimingStats {
5562
std::uint64_t calls = 0;
@@ -63,6 +70,9 @@ struct TimingStats {
6370
double averageTimeMs() const { return calls ? totalTimeMs / static_cast<double>(calls) : 0.; }
6471
};
6572

73+
template <bool IsConst, typename T>
74+
using maybe_const = typename std::conditional<IsConst, const T, T>::type;
75+
6676
} // namespace o2::its
6777

6878
#endif

Detectors/ITSMFT/ITS/tracking/include/ITStracking/TimeFrame.h

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#define TRACKINGITSU_INCLUDE_TIMEFRAME_H_
1515

1616
#include <array>
17+
#include <cstdint>
18+
#include <memory>
19+
#include <string>
1720
#include <vector>
1821
#include <utility>
1922
#include <algorithm>
@@ -26,6 +29,7 @@
2629
#include "ITStracking/Cell.h"
2730
#include "ITStracking/Cluster.h"
2831
#include "ITStracking/Configuration.h"
32+
#include "ITStracking/Definitions.h"
2933
#include "ITStracking/ClusterLines.h"
3034
#include "ITStracking/Tracklet.h"
3135
#include "ITStracking/IndexTableUtils.h"
@@ -35,11 +39,17 @@
3539
#include "ITStracking/TrackingTopology.h"
3640
#include "SimulationDataFormat/MCCompLabel.h"
3741
#include "SimulationDataFormat/MCTruthContainer.h"
38-
3942
#include "DetectorsBase/Propagator.h"
4043

4144
namespace o2
4245
{
46+
#ifdef ITS_ALLOW_DEBUG_TREES
47+
namespace utils
48+
{
49+
class TreeStreamRedirector;
50+
}
51+
#endif
52+
4353
namespace gpu
4454
{
4555
class GPUChainITS;
@@ -292,6 +302,20 @@ struct TimeFrame {
292302
virtual bool isGPU() const noexcept { return false; }
293303
virtual const char* getName() const noexcept { return "CPU"; }
294304

305+
#ifdef ITS_ALLOW_DEBUG_TREES
306+
std::string mDBGTreeFileName{"dbg_its.root"};
307+
void setDebugTreeFileName(std::string n) { mDBGTreeFileName = std::move(n); }
308+
static void destroyDebugTree(o2::utils::TreeStreamRedirector*);
309+
using DebugTreePtr = std::unique_ptr<o2::utils::TreeStreamRedirector, void (*)(o2::utils::TreeStreamRedirector*)>;
310+
DebugTreePtr mDBGOut{nullptr, &TimeFrame::destroyDebugTree};
311+
void closeDebugTree();
312+
auto getDBGOut() { return mDBGOut.get(); }
313+
uint32_t mDBGFlags{0};
314+
bool isDebugFlag(DebugTreeFlags flag) const noexcept { return (mDBGFlags & static_cast<uint32_t>(flag)) != 0; }
315+
void setDebugFlags(uint32_t flags) noexcept { mDBGFlags = flags; }
316+
auto getDebugFlags() const noexcept { return mDBGFlags; }
317+
#endif
318+
295319
protected:
296320
void prepareClusters(const TrackingParameters& trkParam, const int maxLayers = NLayers);
297321
float mBz = 5.;

Detectors/ITSMFT/ITS/tracking/include/ITStracking/TrackerTraits.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ class TrackerTraits
8181
virtual int getTFNumberOfCells() const { return mTimeFrame->getNumberOfCells(); }
8282

8383
private:
84+
#ifdef ITS_ALLOW_DEBUG_TREES
85+
void dumpTracklet(int iteration, int transitionId, int fromLayer, int toLayer, const Tracklet& tracklet);
86+
void dumpTrackletCandidate(int iteration, int transitionId, int fromLayer, int toLayer, int pivotROF, int targetROF,
87+
const Cluster& currentCluster, const Cluster& nextCluster, const Vertex& pv,
88+
float sigmaZ, float phiCut, float deltaZ, bool accepted);
89+
#endif
90+
8491
std::shared_ptr<BoundedMemoryResource> mMemoryPool;
8592
std::shared_ptr<tbb::task_arena> mTaskArena;
8693

Detectors/ITSMFT/ITS/tracking/src/IOUtils.cxx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@
2323
#include "ITStracking/TrackingConfigParam.h"
2424
#include "ITSMFTReconstruction/ChipMappingITS.h"
2525

26-
namespace
27-
{
28-
constexpr int PrimaryVertexLayerId{-1};
29-
constexpr int EventLabelsSeparator{-1};
30-
} // namespace
31-
3226
using namespace o2::its;
3327

3428
/// convert compact clusters to 3D spacepoints

Detectors/ITSMFT/ITS/tracking/src/TimeFrame.cxx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include <numeric>
1717

18+
#include "ITStracking/Definitions.h"
19+
#ifdef ITS_ALLOW_DEBUG_TREES
20+
#include "CommonUtils/TreeStreamRedirector.h"
21+
#endif
1822
#include "Framework/Logger.h"
1923
#include "ITStracking/TimeFrame.h"
2024
#include "ITStracking/MathUtils.h"
@@ -37,12 +41,28 @@ struct ClusterHelper {
3741

3842
namespace o2::its
3943
{
40-
4144
constexpr float DefClusErrorRow = o2::itsmft::SegmentationAlpide::PitchRow * 0.5;
4245
constexpr float DefClusErrorCol = o2::itsmft::SegmentationAlpide::PitchCol * 0.5;
4346
constexpr float DefClusError2Row = DefClusErrorRow * DefClusErrorRow;
4447
constexpr float DefClusError2Col = DefClusErrorCol * DefClusErrorCol;
4548

49+
#ifdef ITS_ALLOW_DEBUG_TREES
50+
template <int NLayers>
51+
void TimeFrame<NLayers>::destroyDebugTree(o2::utils::TreeStreamRedirector* stream)
52+
{
53+
delete stream;
54+
}
55+
56+
template <int NLayers>
57+
void TimeFrame<NLayers>::closeDebugTree()
58+
{
59+
if (mDBGOut) {
60+
mDBGOut->Close();
61+
mDBGOut.reset();
62+
}
63+
}
64+
#endif
65+
4666
template <int NLayers>
4767
void TimeFrame<NLayers>::addPrimaryVertex(const Vertex& vert)
4868
{
@@ -265,6 +285,12 @@ void TimeFrame<NLayers>::initTrackerTopologies(gsl::span<const TrackingParameter
265285
template <int NLayers>
266286
void TimeFrame<NLayers>::initialise(const TrackingParameters& trkParam, const int maxLayers, const int iteration)
267287
{
288+
#ifdef ITS_ALLOW_DEBUG_TREES
289+
if (mDBGFlags != 0 && !mDBGOut) {
290+
mDBGOut.reset(new o2::utils::TreeStreamRedirector(mDBGTreeFileName.c_str(), "recreate"));
291+
}
292+
#endif
293+
268294
mTrackingTopologyView = iteration != constants::UnusedIndex ? mTrackerTopologies[iteration].getView() : (maxLayers == 3 ? mVertexingTopology.getView() : mDefaultTrackingTopology.getView());
269295

270296
if (trkParam.PassFlags[IterationStep::FirstPass]) {

Detectors/ITSMFT/ITS/tracking/src/TrackerTraits.cxx

Lines changed: 139 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include <oneapi/tbb/blocked_range.h>
2222
#include <oneapi/tbb/enumerable_thread_specific.h>
2323

24+
#include "ITStracking/Definitions.h"
25+
#ifdef ITS_ALLOW_DEBUG_TREES
26+
#include "CommonUtils/TreeStreamRedirector.h"
27+
#endif
2428
#include "DetectorsBase/Propagator.h"
2529
#include "GPUCommonMath.h"
2630
#include "ITStracking/BoundedAllocator.h"
@@ -149,9 +153,19 @@ void TrackerTraits<NLayers>::computeLayerTracklets(const int iteration, int iVer
149153
continue;
150154
}
151155
const float deltaZ = o2::gpu::CAMath::Abs((tanLambda * (nextCluster.radius - currentCluster.radius)) + currentCluster.zCoordinate - nextCluster.zCoordinate);
156+
const bool accepted = deltaZ / sigmaZ < mTrkParams[iteration].NSigmaCut &&
157+
math_utils::isPhiDifferenceBelow(currentCluster.phi, nextCluster.phi, phiCut);
158+
159+
#ifdef ITS_ALLOW_DEBUG_TREES
160+
if constexpr (decltype(Tag)::value == PassMode::OnePass::value) {
161+
if (mTimeFrame->isDebugFlag(DebugTrackletCandidates)) {
162+
dumpTrackletCandidate(iteration, transitionId, transition.fromLayer, transition.toLayer, pivotROF, targetROF,
163+
currentCluster, nextCluster, pv, sigmaZ, phiCut, deltaZ, accepted);
164+
}
165+
}
166+
#endif
152167

153-
if (deltaZ / sigmaZ < mTrkParams[iteration].NSigmaCut &&
154-
math_utils::isPhiDifferenceBelow(currentCluster.phi, nextCluster.phi, phiCut)) {
168+
if (accepted) {
155169
const float phi{o2::gpu::CAMath::ATan2(currentCluster.yCoordinate - nextCluster.yCoordinate, currentCluster.xCoordinate - nextCluster.xCoordinate)};
156170
const float tanL = (currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius);
157171
if constexpr (decltype(Tag)::value == PassMode::OnePass::value) {
@@ -212,6 +226,14 @@ void TrackerTraits<NLayers>::computeLayerTracklets(const int iteration, int iVer
212226
std::sort(trkl.begin(), trkl.end());
213227
trkl.erase(std::unique(trkl.begin(), trkl.end()), trkl.end());
214228
trkl.shrink_to_fit();
229+
#ifdef ITS_ALLOW_DEBUG_TREES
230+
if (mTimeFrame->isDebugFlag(DebugTracklets)) {
231+
const auto& transition = topology.getTransition(transitionId);
232+
for (const auto& tkl : trkl) {
233+
dumpTracklet(iteration, transitionId, transition.fromLayer, transition.toLayer, tkl);
234+
}
235+
}
236+
#endif
215237
auto& lut{mTimeFrame->getTrackletsLookupTable()[transitionId]};
216238
if (!trkl.empty()) {
217239
for (const auto& tkl : trkl) {
@@ -903,18 +925,129 @@ void TrackerTraits<NLayers>::setBz(float bz)
903925
template <int NLayers>
904926
void TrackerTraits<NLayers>::setNThreads(int n, std::shared_ptr<tbb::task_arena>& arena)
905927
{
906-
#if defined(OPTIMISATION_OUTPUT)
907-
mTaskArena = std::make_shared<tbb::task_arena>(1);
908-
#else
928+
#ifdef ITS_ALLOW_DEBUG_TREES
929+
if (mTimeFrame->getDebugFlags() != 0) {
930+
mTaskArena = std::make_shared<tbb::task_arena>(1);
931+
LOGP(info, "Forcing ITS tracker to 1 thread for debug tree output.");
932+
return;
933+
}
934+
#endif
909935
if (arena == nullptr) {
910936
mTaskArena = std::make_shared<tbb::task_arena>(std::abs(n));
911937
LOGP(info, "Setting tracker with {} threads.", n);
912938
} else {
913939
mTaskArena = arena;
914940
}
915-
#endif
916941
}
917942

943+
#ifdef ITS_ALLOW_DEBUG_TREES
944+
template <int NLayers>
945+
void TrackerTraits<NLayers>::dumpTracklet(int iteration, int transitionId, int fromLayer, int toLayer, const Tracklet& tracklet)
946+
{
947+
auto* dbgOut = mTimeFrame->getDBGOut();
948+
if (dbgOut == nullptr || !mTimeFrame->isDebugFlag(DebugTracklets)) {
949+
return;
950+
}
951+
952+
static LogLogThrottler logger;
953+
if (logger.needToLog(iteration, transitionId)) {
954+
LOGP(info, "debugTree: LayerTracklets:{}:{} dumped entries {}", iteration, transitionId, logger.evCount);
955+
}
956+
957+
MCCompLabel label;
958+
if (mTimeFrame->hasMCinformation()) {
959+
const int currentId = mTimeFrame->getClusters()[fromLayer][tracklet.firstClusterIndex].clusterId;
960+
const int nextId = mTimeFrame->getClusters()[toLayer][tracklet.secondClusterIndex].clusterId;
961+
for (const auto& lab1 : mTimeFrame->getClusterLabels(fromLayer, currentId)) {
962+
for (const auto& lab2 : mTimeFrame->getClusterLabels(toLayer, nextId)) {
963+
if (lab1 == lab2 && lab1.isValid()) {
964+
label = lab1;
965+
break;
966+
}
967+
}
968+
if (label.isValid()) {
969+
break;
970+
}
971+
}
972+
}
973+
974+
const float tglTracklet = tracklet.tanLambda;
975+
const float etaTracklet = std::asinh(tglTracklet);
976+
const bool accepted = true;
977+
978+
(*dbgOut) << "tracklets"
979+
<< "iter=" << iteration
980+
<< "transition=" << transitionId
981+
<< "fromLayer=" << fromLayer
982+
<< "toLayer=" << toLayer
983+
<< "lbl=" << label
984+
<< "tglTracklet=" << tglTracklet
985+
<< "etaTracklet=" << etaTracklet
986+
<< "acc=" << accepted
987+
<< "\n";
988+
}
989+
990+
template <int NLayers>
991+
void TrackerTraits<NLayers>::dumpTrackletCandidate(int iteration, int transitionId, int fromLayer, int toLayer, int pivotROF, int targetROF,
992+
const Cluster& currentCluster, const Cluster& nextCluster, const Vertex& pv,
993+
float sigmaZ, float phiCut, float deltaZ, bool accepted)
994+
{
995+
auto* dbgOut = mTimeFrame->getDBGOut();
996+
if (dbgOut == nullptr || !mTimeFrame->isDebugFlag(DebugTrackletCandidates)) {
997+
return;
998+
}
999+
1000+
static LogLogThrottler logger;
1001+
if (logger.needToLog(iteration, transitionId)) {
1002+
LOGP(info, "debugTree: LayerTrackletCandidates:{}:{} dumped entries {}", iteration, transitionId, logger.evCount);
1003+
}
1004+
1005+
MCCompLabel label;
1006+
if (mTimeFrame->hasMCinformation()) {
1007+
const int currentId = currentCluster.clusterId;
1008+
const int nextId = nextCluster.clusterId;
1009+
for (const auto& lab1 : mTimeFrame->getClusterLabels(fromLayer, currentId)) {
1010+
for (const auto& lab2 : mTimeFrame->getClusterLabels(toLayer, nextId)) {
1011+
if (lab1 == lab2 && lab1.isValid()) {
1012+
label = lab1;
1013+
break;
1014+
}
1015+
}
1016+
if (label.isValid()) {
1017+
break;
1018+
}
1019+
}
1020+
}
1021+
1022+
const float tglTracklet = (currentCluster.zCoordinate - nextCluster.zCoordinate) / (currentCluster.radius - nextCluster.radius);
1023+
const float etaTracklet = std::asinh(tglTracklet);
1024+
const float deltaPhiRaw = o2::gpu::CAMath::Abs(currentCluster.phi - nextCluster.phi);
1025+
const float deltaPhi = o2::gpu::CAMath::Min(deltaPhiRaw, o2::gpu::CAMath::Abs(deltaPhiRaw - o2::constants::math::TwoPI));
1026+
const float zTrackletAtPvR = currentCluster.zCoordinate + tglTracklet * (pv.getR() - currentCluster.radius);
1027+
const float deltaZTrackletPv = zTrackletAtPvR - pv.getZ();
1028+
1029+
(*dbgOut) << "trackletCandidates"
1030+
<< "iter=" << iteration
1031+
<< "transition=" << transitionId
1032+
<< "fromLayer=" << fromLayer
1033+
<< "toLayer=" << toLayer
1034+
<< "pivotROF=" << pivotROF
1035+
<< "targetROF=" << targetROF
1036+
<< "lbl=" << label
1037+
<< "pvZ=" << pv.getZ()
1038+
<< "sigZ=" << sigmaZ
1039+
<< "delZ=" << deltaZ
1040+
<< "sigPhi=" << phiCut
1041+
<< "delPhi=" << deltaPhi
1042+
<< "tglTracklet=" << tglTracklet
1043+
<< "etaTracklet=" << etaTracklet
1044+
<< "zTrackletAtPvR=" << zTrackletAtPvR
1045+
<< "deltaZTrackletPv=" << deltaZTrackletPv
1046+
<< "acc=" << accepted
1047+
<< "\n";
1048+
}
1049+
#endif
1050+
9181051
template class TrackerTraits<7>;
9191052
template void TrackerTraits<7>::processNeighbours<CellSeed>(int, int, int, const bounded_vector<CellSeed>&, const bounded_vector<int>&, const bounded_vector<int>&, bounded_vector<TrackSeed<7>>&, bounded_vector<int>&, bounded_vector<int>&);
9201053
template void TrackerTraits<7>::processNeighbours<TrackSeed<7>>(int, int, int, const bounded_vector<TrackSeed<7>>&, const bounded_vector<int>&, const bounded_vector<int>&, bounded_vector<TrackSeed<7>>&, bounded_vector<int>&, bounded_vector<int>&);

Detectors/ITSMFT/ITS/tracking/src/TrackingInterface.cxx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,9 @@ void ITSTrackingInterface::finaliseCCDB(ConcreteDataMatcher& matcher, void* obj)
467467

468468
void ITSTrackingInterface::printSummary() const
469469
{
470+
#ifdef ITS_ALLOW_DEBUG_TREES
471+
mTimeFrame->closeDebugTree();
472+
#endif
470473
mVertexer->printSummary();
471474
mTracker->printSummary();
472475
}

0 commit comments

Comments
 (0)