Skip to content

Commit 04f42d1

Browse files
authored
Improve code clarity and fix potential issues
Refactor lambdaProtonBalanceFunction.cxx for clarity and correctness.
1 parent 006b399 commit 04f42d1

1 file changed

Lines changed: 31 additions & 14 deletions

File tree

‎PWGCF/TwoParticleCorrelations/Tasks/lambdaProtonBalanceFunction.cxx‎

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include <cstddef>
5959
#include <cstdint>
6060
#include <cstdlib>
61+
#include <ios>
6162
#include <locale>
6263
#include <memory>
6364
#include <sstream>
@@ -631,7 +632,9 @@ struct LambdaProtonBalanceFunction {
631632
const double minPt = species < LambdaSpeciesCount ? lambdaV0MinPt.value : pProtonMinPt.value;
632633
const double maxPt = species < LambdaSpeciesCount ? lambdaV0MaxPt.value : pProtonMaxPt.value;
633634
const double maxY = species < LambdaSpeciesCount ? lambdaV0MaxY.value : pProtonMaxY.value;
634-
if (!(minPt < maxPt && maxY > 0.) || h->GetYaxis()->GetXmin() > minPt ||
635+
// Keep rejecting NaN bounds when simplifying the ordered comparisons.
636+
if (std::isnan(minPt) || std::isnan(maxPt) || std::isnan(maxY) ||
637+
minPt >= maxPt || maxY <= 0. || h->GetYaxis()->GetXmin() > minPt ||
635638
h->GetYaxis()->GetXmax() < maxPt || h->GetZaxis()->GetXmin() > -maxY ||
636639
h->GetZaxis()->GetXmax() < maxY ||
637640
(species >= LambdaSpeciesCount && (h->GetYaxis()->GetXmax() <= maxPt || h->GetZaxis()->GetXmax() <= maxY))) {
@@ -645,7 +648,10 @@ struct LambdaProtonBalanceFunction {
645648
const auto contract = correctionContract();
646649
for (const auto& species : state.correctionInputs) {
647650
for (const auto& h : species) {
648-
h->SetTitle((std::string(h->GetName()) + "|" + contract).c_str());
651+
std::string title = h->GetName();
652+
title += '|';
653+
title += contract;
654+
h->SetTitle(title.c_str());
649655
}
650656
}
651657
// A count plus the individual bits of the exact contract, not a hash or an
@@ -719,7 +725,7 @@ struct LambdaProtonBalanceFunction {
719725
}
720726
for (size_t character = 0; character < contract.size(); ++character) {
721727
for (int bit = 0; bit < MetadataBitsPerByte; ++bit) {
722-
const bool set = (static_cast<unsigned char>(contract[character]) >> bit) & 1U;
728+
const bool set = ((static_cast<unsigned char>(contract[character]) >> bit) & 1U) != 0U;
723729
if (metadata->GetBinContent(2 + MetadataBitsPerByte * character + bit) != (set ? workers : 0.)) {
724730
LOGF(fatal, "Combined-correction inputs mix incompatible worker selections/axes or metadata was modified");
725731
return;
@@ -753,7 +759,10 @@ struct LambdaProtonBalanceFunction {
753759
const std::string name = std::string(kCorrectionInputs[stage]) + kCorrectionSpecies[species];
754760
raw[stage] = dynamic_cast<TH3D*>(list.FindObject(name.c_str()));
755761
auto* h = raw[stage];
756-
if (!h || std::string(h->GetTitle()) != name + "|" + contract ||
762+
std::string expectedTitle = name;
763+
expectedTitle += '|';
764+
expectedTitle += contract;
765+
if (h == nullptr || std::string(h->GetTitle()) != expectedTitle ||
757766
!correctionAxisMatches(*h->GetXaxis(), *expected->GetXaxis()) ||
758767
!correctionAxisMatches(*h->GetYaxis(), *expected->GetYaxis()) ||
759768
!correctionAxisMatches(*h->GetZaxis(), *expected->GetZaxis()) || h->GetSumw2N() != h->GetNcells()) {
@@ -773,7 +782,14 @@ struct LambdaProtonBalanceFunction {
773782
const std::array<const char*, 5> mapNames = {"hPurity_", "hPrimaryFraction_", "hEfficiency_", "hCombinedWeight_", "hCombinedValid_"};
774783
for (size_t kind = 0; kind < mapNames.size(); ++kind) {
775784
const std::string name = std::string(mapNames[kind]) + kCorrectionSpecies[species];
776-
state.correctionMaps[species][kind].reset(static_cast<TH3D*>(raw[0]->Clone(name.c_str())));
785+
auto* clonedObject = raw[0]->Clone(name.c_str());
786+
auto* map = dynamic_cast<TH3D*>(clonedObject);
787+
if (map == nullptr) {
788+
delete clonedObject;
789+
LOGF(fatal, "Cannot clone combined correction map %s as TH3D", name.c_str());
790+
return;
791+
}
792+
state.correctionMaps[species][kind].reset(map);
777793
state.correctionMaps[species][kind]->SetDirectory(nullptr);
778794
state.correctionMaps[species][kind]->Reset();
779795
}
@@ -821,7 +837,9 @@ struct LambdaProtonBalanceFunction {
821837
maps[3]->SetBinContent(bin, weight);
822838
maps[4]->SetBinContent(bin, 1.);
823839
++validBins;
824-
efficiencyAboveOne += efficiency > 1.;
840+
if (efficiency > 1.) {
841+
++efficiencyAboveOne;
842+
}
825843
}
826844
if (validBins == 0) {
827845
LOGF(fatal, "No valid combined-correction bins for %s", kCorrectionSpecies[species]);
@@ -3511,7 +3529,7 @@ struct LambdaProtonBalanceFunction {
35113529
state.correctionMetadata->Fill(1.);
35123530
for (size_t character = 0; character < contract.size(); ++character) {
35133531
for (int bit = 0; bit < MetadataBitsPerByte; ++bit) {
3514-
if ((static_cast<unsigned char>(contract[character]) >> bit) & 1U) {
3532+
if (((static_cast<unsigned char>(contract[character]) >> bit) & 1U) != 0U) {
35153533
state.correctionMetadata->Fill(2 + MetadataBitsPerByte * character + bit);
35163534
}
35173535
}
@@ -3668,9 +3686,8 @@ struct LambdaProtonBalanceFunction {
36683686
a.negDaughter == b.posDaughter || a.negDaughter == b.negDaughter) {
36693687
continue;
36703688
}
3671-
} else if (a.isV0 && (a.posDaughter == b.info.gid || a.negDaughter == b.info.gid)) {
3672-
continue;
3673-
} else if (b.isV0 && (b.posDaughter == a.info.gid || b.negDaughter == a.info.gid)) {
3689+
} else if ((a.isV0 && (a.posDaughter == b.info.gid || a.negDaughter == b.info.gid)) ||
3690+
(b.isV0 && (b.posDaughter == a.info.gid || b.negDaughter == a.info.gid))) {
36743691
continue;
36753692
}
36763693
const auto& p = a.info;
@@ -3699,9 +3716,9 @@ struct LambdaProtonBalanceFunction {
36993716
{
37003717
const auto selected = selectCorrectionCandidates(collision, v0s, tracks);
37013718
std::array<std::vector<CombinedParticle>, 4> particles;
3702-
const auto append = [&](CombinedParticle particle, size_t species) {
3719+
const auto append = [&](CombinedParticle const& particle, size_t species) {
37033720
if (particle.weight > 0.) {
3704-
particles[species].push_back(std::move(particle));
3721+
particles[species].push_back(particle);
37053722
}
37063723
};
37073724
for (const auto& v0 : selected.lambdas) {
@@ -5216,7 +5233,7 @@ struct LambdaProtonBalanceFunction {
52165233
// ─────────────────────────────────────────────────────────────────────
52175234
};
52185235

5219-
WorkflowSpec defineDataProcessing(ConfigContext const& cfgc)
5236+
WorkflowSpec defineDataProcessing(ConfigContext const& context)
52205237
{
5221-
return WorkflowSpec{adaptAnalysisTask<LambdaProtonBalanceFunction>(cfgc)};
5238+
return WorkflowSpec{adaptAnalysisTask<LambdaProtonBalanceFunction>(context)};
52225239
}

0 commit comments

Comments
 (0)