Skip to content

Commit 15d4354

Browse files
committed
DPL Analysis: add decay chain cycle check to checkDataModelMC
1 parent aeec919 commit 15d4354

1 file changed

Lines changed: 64 additions & 15 deletions

File tree

Common/Tasks/checkDataModelMC.cxx

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ using namespace o2::framework::expressions;
3838
/// \param particle MC particle
3939
/// \param debugMode flag for debug mode
4040
/// \param debugHisto histo to fill for debug
41-
template <typename T, typename H>
41+
template <soa::is_table T>
4242
void checkDaughters(const T& particlesMC,
4343
const typename T::iterator& particle,
44-
long unsigned int offset,
45-
bool& debugMode,
46-
H debugHisto)
44+
uint64_t offset,
45+
bool debugMode,
46+
std::shared_ptr<TH1>& debugHisto)
4747
{
4848
auto firstDauIdx = particle.daughtersIds().front();
4949
auto lastDauIdx = particle.daughtersIds().back();
@@ -55,7 +55,7 @@ void checkDaughters(const T& particlesMC,
5555
}
5656
}
5757
for (const auto& idxDau : particle.daughtersIds()) {
58-
if (idxDau >= 0 && ((unsigned long int)idxDau > offset + particlesMC.size() || (unsigned long int)idxDau < offset)) {
58+
if (idxDau >= 0 && ((uint64_t)idxDau > offset + particlesMC.size() || (uint64_t)idxDau < offset)) {
5959
if (debugMode) {
6060
debugHisto->Fill(1);
6161
} else {
@@ -71,7 +71,49 @@ void checkDaughters(const T& particlesMC,
7171
}
7272
}
7373

74-
template <typename Table>
74+
/// Check for cycles in the decay chain
75+
/// \param particlesMC table with MC particles
76+
template <soa::is_table T>
77+
bool checkCycles(T const& particlesMC)
78+
{
79+
auto slow = particlesMC.begin();
80+
auto fast = particlesMC.begin();
81+
auto probe = particlesMC.begin();
82+
bool hasCycle = false;
83+
for (auto start = 0U; start < particlesMC.size(); ++start) {
84+
probe.setCursor(start);
85+
if (!probe.has_mothers() || probe.mothersIds()[0] == -1) {
86+
continue;
87+
}
88+
slow.setCursor(start);
89+
fast.setCursor(start);
90+
91+
while (true) {
92+
if (!slow.has_mothers() || slow.mothersIds()[0] < 0) {
93+
break;
94+
}
95+
slow.setCursor(slow.mothersIds()[0]);
96+
97+
if (!fast.has_mothers() || fast.mothersIds()[0] < 0) {
98+
break;
99+
}
100+
fast.setCursor(fast.mothersIds()[0]);
101+
if (!fast.has_mothers() || fast.mothersIds()[0] < 0) {
102+
break;
103+
}
104+
fast.setCursor(fast.mothersIds()[0]);
105+
106+
if (slow.globalIndex() == fast.globalIndex()) {
107+
LOGP(error, "Cycle found: particle: {} | cycle starts at: {}", start, slow.globalIndex());
108+
hasCycle = true;
109+
break;
110+
}
111+
}
112+
}
113+
return hasCycle;
114+
}
115+
116+
template <soa::is_table Table>
75117
struct LoadTable {
76118
OutputObj<TH1F> counter{TH1F("counter", "counter", 2, 0., 2)};
77119
void process(Table const& table)
@@ -82,6 +124,16 @@ struct LoadTable {
82124
}
83125
};
84126

127+
std::shared_ptr<TH1> defineHistogram(HistogramRegistry& registry)
128+
{
129+
std::shared_ptr<TH1> hDebug;
130+
hDebug = registry.add<TH1>("hDebug", "debug histo;;counts", HistType::kTH1F, {{3, -0.5, 2.5}});
131+
hDebug->GetXaxis()->SetBinLabel(1, "(-X, Y) or (X, -Y)");
132+
hDebug->GetXaxis()->SetBinLabel(2, "out of range");
133+
hDebug->GetXaxis()->SetBinLabel(3, "#minus max integer");
134+
return hDebug;
135+
}
136+
85137
struct CheckMcParticlesIndices {
86138

87139
Configurable<bool> debugMode{"debugMode", false, "flag to enable debug mode"};
@@ -91,15 +143,15 @@ struct CheckMcParticlesIndices {
91143

92144
void init(o2::framework::InitContext&)
93145
{
94-
hDebug = registry.add<TH1>("hDebug", "debug histo;;counts", HistType::kTH1F, {{3, -0.5, 2.5}});
95-
hDebug->GetXaxis()->SetBinLabel(1, "(-X, Y) or (X, -Y)");
96-
hDebug->GetXaxis()->SetBinLabel(2, "out of range");
97-
hDebug->GetXaxis()->SetBinLabel(3, "#minus max integer");
146+
hDebug = defineHistogram(registry);
98147
}
99148

100149
void process(aod::McParticles const& particlesMC)
101150
{
102-
long unsigned int offset = 0;
151+
if (checkCycles(particlesMC)) {
152+
LOG(fatal) << "Cycles found, aborting.";
153+
}
154+
uint64_t offset = 0;
103155
for (const auto& particle : particlesMC) {
104156
checkDaughters(particlesMC, particle, offset, debugMode.value, hDebug);
105157
}
@@ -115,10 +167,7 @@ struct CheckMcParticlesIndicesGrouped {
115167

116168
void init(o2::framework::InitContext&)
117169
{
118-
hDebug = registry.add<TH1>("hDebug", "debug histo;;counts", HistType::kTH1F, {{3, -0.5, 2.5}});
119-
hDebug->GetXaxis()->SetBinLabel(1, "(-X, Y) or (X, -Y)");
120-
hDebug->GetXaxis()->SetBinLabel(2, "out of range");
121-
hDebug->GetXaxis()->SetBinLabel(3, "#minus max integer");
170+
hDebug = defineHistogram(registry);
122171
}
123172

124173
void process(aod::McCollision const&,

0 commit comments

Comments
 (0)