|
| 1 | +// Copyright 2019-2022 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// \file FemtoUniverseEfficiencyCorrection.h |
| 13 | +/// \brief Abstraction for applying efficiency corrections based on weights from CCDB |
| 14 | +/// \author Dawid Karpiński, WUT Warsaw, dawid.karpinski@cern.ch |
| 15 | + |
| 16 | +#ifndef PWGCF_FEMTOUNIVERSE_CORE_FEMTOUNIVERSEEFFICIENCYCORRECTION_H_ |
| 17 | +#define PWGCF_FEMTOUNIVERSE_CORE_FEMTOUNIVERSEEFFICIENCYCORRECTION_H_ |
| 18 | + |
| 19 | +#include <vector> |
| 20 | +#include <string> |
| 21 | +#include <algorithm> |
| 22 | + |
| 23 | +#include "Framework/Configurable.h" |
| 24 | +#include "CCDB/BasicCCDBManager.h" |
| 25 | +#include "TH1.h" |
| 26 | +#include "TH2.h" |
| 27 | +#include "TH3.h" |
| 28 | + |
| 29 | +namespace o2::analysis::femto_universe::efficiency_correction |
| 30 | +{ |
| 31 | +enum ParticleNo : size_t { |
| 32 | + ONE = 1, |
| 33 | + TWO, |
| 34 | +}; |
| 35 | + |
| 36 | +template <size_t T> |
| 37 | +concept isOneOrTwo = T == ParticleNo::ONE || T == ParticleNo::TWO; |
| 38 | + |
| 39 | +template <typename T> |
| 40 | +consteval auto getHistDim() -> int |
| 41 | +{ |
| 42 | + if (std::is_same_v<T, TH1>) |
| 43 | + return 1; |
| 44 | + else if (std::is_same_v<T, TH2>) |
| 45 | + return 2; |
| 46 | + else if (std::is_same_v<T, TH3>) |
| 47 | + return 3; |
| 48 | + else |
| 49 | + return -1; |
| 50 | +} |
| 51 | + |
| 52 | +struct EffCorConfigurableGroup : framework::ConfigurableGroup { |
| 53 | + framework::Configurable<bool> confEffCorApply{"confEffCorApply", false, "[Efficiency Correction] Should apply efficiency corrections"}; |
| 54 | + framework::Configurable<std::string> confEffCorCCDBUrl{"confEffCorCCDBUrl", "http://alice-ccdb.cern.ch", "[Efficiency Correction] CCDB URL to use"}; |
| 55 | + framework::Configurable<std::string> confEffCorCCDBPath{"confEffCorCCDBPath", "", "[Efficiency Correction] CCDB path to histograms"}; |
| 56 | + framework::Configurable<std::vector<std::string>> confEffCorCCDBTimestamps{"confEffCorCCDBTimestamps", {}, "[Efficiency Correction] Timestamps of histograms in CCDB (0 can be used as a placeholder, e.g. when running subwagons)"}; |
| 57 | +}; |
| 58 | + |
| 59 | +template <typename HistType> |
| 60 | + requires std::is_base_of_v<TH1, HistType> |
| 61 | +class EfficiencyCorrection |
| 62 | +{ |
| 63 | + public: |
| 64 | + explicit EfficiencyCorrection(EffCorConfigurableGroup* config) : config(config) // o2-linter: disable=name/function-variable |
| 65 | + { |
| 66 | + } |
| 67 | + |
| 68 | + auto init() -> void |
| 69 | + { |
| 70 | + ccdb.setURL(config->confEffCorCCDBUrl); |
| 71 | + ccdb.setLocalObjectValidityChecking(); |
| 72 | + ccdb.setFatalWhenNull(false); |
| 73 | + |
| 74 | + auto now = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| 75 | + ccdb.setCreatedNotAfter(now); |
| 76 | + |
| 77 | + shouldApplyCorrection = config->confEffCorApply; |
| 78 | + |
| 79 | + if (shouldApplyCorrection && !config->confEffCorCCDBTimestamps.value.empty()) { |
| 80 | + for (auto idx = 0UL; idx < config->confEffCorCCDBTimestamps.value.size(); idx++) { |
| 81 | + auto timestamp = 0L; |
| 82 | + try { |
| 83 | + timestamp = std::max(0L, std::stol(config->confEffCorCCDBTimestamps.value[idx])); |
| 84 | + } catch (const std::exception&) { |
| 85 | + LOGF(error, notify("Could not parse CCDB timestamp \"%s\""), config->confEffCorCCDBTimestamps.value[idx]); |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + hLoaded[idx] = timestamp > 0 ? loadHistFromCCDB(timestamp) : nullptr; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + template <typename... BinVars> |
| 95 | + requires(sizeof...(BinVars) == getHistDim<HistType>()) |
| 96 | + auto getWeight(ParticleNo partNo, const BinVars&... binVars) const -> float |
| 97 | + { |
| 98 | + auto weight = 1.0f; |
| 99 | + auto hWeights = hLoaded[partNo - 1]; |
| 100 | + |
| 101 | + if (shouldApplyCorrection && hWeights) { |
| 102 | + auto bin = hWeights->FindBin(binVars...); |
| 103 | + weight = hWeights->GetBinContent(bin); |
| 104 | + } |
| 105 | + |
| 106 | + return weight; |
| 107 | + } |
| 108 | + |
| 109 | + private: |
| 110 | + static inline auto notify(const std::string& msg) -> const std::string |
| 111 | + { |
| 112 | + return fmt::format("[EFFICIENCY CORRECTION] {}", msg); |
| 113 | + } |
| 114 | + |
| 115 | + static auto isHistEmpty(HistType* hist) -> bool |
| 116 | + { |
| 117 | + if (!hist) { |
| 118 | + return true; |
| 119 | + } |
| 120 | + for (auto idx = 0; idx <= hist->GetNbinsX() + 1; idx++) { |
| 121 | + if (hist->GetBinContent(idx) > 0) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + } |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + auto loadHistFromCCDB(const int64_t timestamp) const -> HistType* |
| 129 | + { |
| 130 | + auto hWeights = ccdb.getForTimeStamp<HistType>(config->confEffCorCCDBPath, timestamp); |
| 131 | + if (!hWeights || hWeights->IsZombie()) { |
| 132 | + LOGF(error, notify("Could not load histogram \"%s/%ld\""), config->confEffCorCCDBPath.value, timestamp); |
| 133 | + return nullptr; |
| 134 | + } |
| 135 | + |
| 136 | + if (isHistEmpty(hWeights)) { |
| 137 | + LOGF(warn, notify("Histogram \"%s/%ld\" has been loaded, but it is empty"), config->confEffCorCCDBUrl.value, timestamp); |
| 138 | + } |
| 139 | + |
| 140 | + LOGF(info, notify("Successfully loaded %ld"), timestamp); |
| 141 | + return hWeights; |
| 142 | + } |
| 143 | + |
| 144 | + EffCorConfigurableGroup* config{}; |
| 145 | + |
| 146 | + bool shouldApplyCorrection = false; |
| 147 | + |
| 148 | + o2::ccdb::BasicCCDBManager& ccdb{o2::ccdb::BasicCCDBManager::instance()}; |
| 149 | + std::array<HistType*, 2> hLoaded{}; |
| 150 | +}; |
| 151 | + |
| 152 | +} // namespace o2::analysis::femto_universe::efficiency_correction |
| 153 | + |
| 154 | +#endif // PWGCF_FEMTOUNIVERSE_CORE_FEMTOUNIVERSEEFFICIENCYCORRECTION_H_ |
0 commit comments