From 9ef9d663029eff3ddb91a23d0c9e5ab9441a9a6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Karpi=C5=84ski?= Date: Mon, 26 May 2025 18:19:18 +0200 Subject: [PATCH] fix: dangling pointer issue with loaded histograms --- .../Core/FemtoUniverseEfficiencyCorrection.h | 36 ++++++++++++------- .../Macros/calculateEfficiencyCorrection.cxx | 8 +++++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/PWGCF/FemtoUniverse/Core/FemtoUniverseEfficiencyCorrection.h b/PWGCF/FemtoUniverse/Core/FemtoUniverseEfficiencyCorrection.h index a26b6cbc966..9936a5430d6 100644 --- a/PWGCF/FemtoUniverse/Core/FemtoUniverseEfficiencyCorrection.h +++ b/PWGCF/FemtoUniverse/Core/FemtoUniverseEfficiencyCorrection.h @@ -110,8 +110,6 @@ class EfficiencyCorrection LOGF(fatal, notify("Unknown configuration for efficiency variables")); break; } - } else { - hLoaded[idx] = nullptr; } } } @@ -201,17 +199,18 @@ class EfficiencyCorrection auto bin = -1; if (config->confEffCorVariables.value == "pt") { - bin = hLoaded[partNo - 1]->FindBin(particle.pt()); + bin = hWeights->FindBin(particle.pt()); } else if (config->confEffCorVariables.value == "pt,eta") { - bin = hLoaded[partNo - 1]->FindBin(particle.pt(), particle.eta()); + bin = hWeights->FindBin(particle.pt(), particle.eta()); } else if (config->confEffCorVariables.value == "pt,mult") { - bin = hLoaded[partNo - 1]->FindBin(particle.pt(), particle.fdCollision().multV0M()); + bin = hWeights->FindBin(particle.pt(), particle.fdCollision().multV0M()); } else if (config->confEffCorVariables.value == "pt,eta,mult") { - bin = hLoaded[partNo - 1]->FindBin(particle.pt(), particle.eta(), particle.fdCollision().multV0M()); + bin = hWeights->FindBin(particle.pt(), particle.eta(), particle.fdCollision().multV0M()); } else { LOGF(fatal, notify("Unknown configuration for efficiency variables")); return weight; } + weight = hWeights->GetBinContent(bin); } @@ -229,11 +228,21 @@ class EfficiencyCorrection if (!hist) { return true; } - for (auto idx = 0; idx <= hist->GetNbinsX() + 1; idx++) { - if (hist->GetBinContent(idx) > 0) { - return false; + + const int nBinsX = hist->GetNbinsX() + 2; + const int nBinsY = hist->GetNbinsY() + 2; + const int nBinsZ = hist->GetNbinsZ() + 2; + + for (int x = 0; x < nBinsX; ++x) { + for (int y = 0; y < nBinsY; ++y) { + for (int z = 0; z < nBinsZ; ++z) { + if (hist->GetBinContent(x, y, z) != 0) { + return false; + } + } } } + return true; } @@ -247,11 +256,14 @@ class EfficiencyCorrection } if (isHistEmpty(hWeights)) { - LOGF(warn, notify("Histogram \"%s/%ld\" has been loaded, but it is empty"), config->confEffCorCCDBUrl.value, timestamp); + LOGF(warn, notify("Histogram \"%s/%ld\" has been loaded, but it is empty"), config->confEffCorCCDBPath.value, timestamp); } + auto clonedHist = static_cast(hWeights->Clone()); + clonedHist->SetDirectory(nullptr); + LOGF(info, notify("Successfully loaded %ld"), timestamp); - return hWeights; + return clonedHist; } auto getDimensionFromVariables() -> size_t @@ -266,7 +278,7 @@ class EfficiencyCorrection bool shouldFillHistograms{false}; o2::ccdb::BasicCCDBManager& ccdb{o2::ccdb::BasicCCDBManager::instance()}; - std::array hLoaded{}; + std::array hLoaded{nullptr, nullptr}; framework::HistogramRegistry* histRegistry{}; static constexpr std::string_view histDirectory{"EfficiencyCorrection"}; diff --git a/PWGCF/FemtoUniverse/Macros/calculateEfficiencyCorrection.cxx b/PWGCF/FemtoUniverse/Macros/calculateEfficiencyCorrection.cxx index e99331231e2..3880661d9f7 100644 --- a/PWGCF/FemtoUniverse/Macros/calculateEfficiencyCorrection.cxx +++ b/PWGCF/FemtoUniverse/Macros/calculateEfficiencyCorrection.cxx @@ -148,6 +148,10 @@ auto calculateEfficiencyCorrection(const fs::path& resultsPath, const fs::path& histWeights->Reset(); setAxisTitles(histWeights, projection); + auto* histCont{cloneHistogram(histPrimary, "hCont")}; + histCont->Reset(); + setAxisTitles(histCont, projection); + forEachBin(histPrimary, [&](int x, int y, int z) { auto primVal{histPrimary->GetBinContent(x, y, z)}; auto primErr{histPrimary->GetBinError(x, y, z)}; @@ -178,6 +182,9 @@ auto calculateEfficiencyCorrection(const fs::path& resultsPath, const fs::path& contErr = std::sqrt(std::pow(secErr / totalVal, 2) + std::pow((secVal * totalErr / std::pow(totalVal, 2)), 2)); } + histCont->SetBinContent(x, y, z, contVal); + histCont->SetBinError(x, y, z, contErr); + auto weightVal{0.}; auto weightErr{0.}; if (effVal > 0) { @@ -190,6 +197,7 @@ auto calculateEfficiencyCorrection(const fs::path& resultsPath, const fs::path& }); outputFile->WriteTObject(histEfficiency); + outputFile->WriteTObject(histCont); outputFile->WriteTObject(histWeights); outputFile->Close();