Skip to content

Commit 053c732

Browse files
authored
[PWGCF/FemtoUniverse] Move efficiency calculation and upload to a script (AliceO2Group#9601)
1 parent 7671dd3 commit 053c732

File tree

4 files changed

+254
-202
lines changed

4 files changed

+254
-202
lines changed

PWGCF/FemtoUniverse/Core/FemtoUniverseEfficiencyCalculator.h

Lines changed: 40 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -10,125 +10,87 @@
1010
// or submit itself to any jurisdiction.
1111

1212
/// \file FemtoUniverseEfficiencyCalculator.h
13-
/// \brief Abstraction for calculating efficiency and applying corrections with the help of CCDB
13+
/// \brief Abstraction for applying corrections based on efficiency from CCDB
1414
/// \author Dawid Karpiński, WUT Warsaw, [email protected]
1515

1616
#ifndef PWGCF_FEMTOUNIVERSE_CORE_FEMTOUNIVERSEEFFICIENCYCALCULATOR_H_
1717
#define PWGCF_FEMTOUNIVERSE_CORE_FEMTOUNIVERSEEFFICIENCYCALCULATOR_H_
1818

1919
#include <vector>
20-
#include <memory>
2120
#include <map>
2221
#include <string>
2322

2423
#include "Framework/Configurable.h"
25-
#include "Framework/CallbackService.h"
26-
#include "Framework/InitContext.h"
2724
#include "CCDB/BasicCCDBManager.h"
2825
#include "PWGCF/FemtoUniverse/DataModel/FemtoDerived.h"
2926
#include "FemtoUniverseParticleHisto.h"
3027

3128
namespace o2::analysis::femto_universe::efficiency
3229
{
33-
template <uint8_t T>
34-
concept isOneOrTwo = T == 1 || T == 2;
30+
enum ParticleNo : size_t {
31+
ONE = 1,
32+
TWO
33+
};
34+
35+
template <size_t T>
36+
concept isOneOrTwo = T == ParticleNo::ONE || T == ParticleNo::TWO;
3537

3638
struct EfficiencyConfigurableGroup : ConfigurableGroup {
37-
Configurable<bool> confEfficiencyCalculate{"confEfficiencyCalculate", false, "Should calculate efficiency"};
39+
Configurable<bool> confEfficiencyDoMCTruth{"confEfficiencyDoMCTruth", false, "Should fill MC Truth histogram"};
3840
Configurable<bool> confEfficiencyApplyCorrections{"confEfficiencyApplyCorrections", false, "Should apply corrections from efficiency"};
39-
Configurable<std::vector<std::string>> confEfficiencyCCDBLabels{"confEfficiencyCCDBLabels", {}, "Labels for efficiency objects in CCDB"};
40-
ConfigurableAxis confEfficiencyCCDBTimestamps{"confEfficiencyCCDBTimestamps", {-1, -1}, "Timestamps from which to query CCDB objects (default: -1 for both)"};
41+
Configurable<std::vector<std::string>> confEfficiencyCCDBLabels{"confEfficiencyCCDBLabels", {}, "Custom labels for efficiency objects in CCDB"};
42+
Configurable<int> confCCDBTrainNumber{"confCCDBTrainNumber", -1, "Train number for which to query CCDB objects (set to -1 to ignore)"};
43+
ConfigurableAxis confEfficiencyCCDBTimestamps{"confEfficiencyCCDBTimestamps", {-1, -1}, "Timestamps in CCDB, to query for specific objects (default: -1 for both)"};
4144

4245
// NOTE: in the future we might move the below configurables to a separate struct, eg. CCDBConfigurableGroup
4346
Configurable<std::string> confCCDBUrl{"confCCDBUrl", "http://alice-ccdb.cern.ch", "CCDB URL to be used"};
4447
Configurable<std::string> confCCDBPath{"confCCDBPath", "", "CCDB base path to where to upload objects"};
45-
Configurable<int64_t> confCCDBLifetime{"confCCDBLifetime", 365LL * 24 * 60 * 60 * 1000, "Lifetime of uploaded objects (default: 1 year)"};
4648
};
4749

4850
class EfficiencyCalculator
4951
{
5052
public:
51-
o2::ccdb::BasicCCDBManager& ccdb{o2::ccdb::BasicCCDBManager::instance()};
52-
5353
explicit EfficiencyCalculator(EfficiencyConfigurableGroup* config) : config(config) // o2-linter: disable=name/function-variable
5454
{
5555
}
5656

5757
auto init() -> void
5858
{
59-
ccdbApi.init(config->confCCDBUrl);
6059
ccdb.setURL(config->confCCDBUrl);
6160
ccdb.setLocalObjectValidityChecking();
6261
ccdb.setFatalWhenNull(false);
6362

6463
int64_t now = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
6564
ccdb.setCreatedNotAfter(now);
6665

67-
shouldCalculate = config->confEfficiencyCalculate;
66+
shouldDoTruth = config->confEfficiencyDoMCTruth;
6867
shouldApplyCorrections = config->confEfficiencyApplyCorrections;
6968

70-
ccdbFullPath = fmt::format("{}/{}", config->confCCDBPath.value, folderName);
71-
7269
if (config->confEfficiencyApplyCorrections) {
73-
if (config->confEfficiencyCCDBTimestamps->size() != 2) {
74-
LOGF(fatal, notify("CCDB timestamps configurable should be exactly of size 2"));
75-
}
7670
hLoaded = {
77-
loadEfficiencyFromCCDB<1>(config->confEfficiencyCCDBTimestamps.value[0]),
78-
loadEfficiencyFromCCDB<2>(config->confEfficiencyCCDBTimestamps.value[1]) //
71+
loadEfficiencyFromCCDB(ParticleNo::ONE),
72+
loadEfficiencyFromCCDB(ParticleNo::TWO), //
7973
};
8074
}
8175
}
8276

83-
template <uint8_t N>
77+
template <size_t N>
8478
requires isOneOrTwo<N>
85-
auto doMCTruth(FemtoUniverseParticleHisto<aod::femtouniverseparticle::ParticleType::kMCTruthTrack, N>& hMCTruth, const auto& particles) const -> void
86-
{
87-
for (const auto& particle : particles) {
88-
hMCTruth.template fillQA<false, false>(particle);
89-
}
90-
}
91-
92-
auto uploadOnStop(InitContext& ic) -> void
79+
auto doMCTruth(
80+
FemtoUniverseParticleHisto<aod::femtouniverseparticle::ParticleType::kMCTruthTrack, N>& hMCTruth,
81+
const auto& particles) const -> void
9382
{
94-
if (!shouldCalculate) {
95-
return;
96-
}
97-
98-
if (!shouldUploadOnStop) {
99-
shouldUploadOnStop = true;
100-
101-
auto& callbacks = ic.services().get<CallbackService>();
102-
callbacks.set<o2::framework::CallbackService::Id::Stop>([this]() {
103-
for (auto i = 0UL; i < hOutput.size(); i++) {
104-
const auto& output = hOutput[i];
105-
106-
if (isHistogramEmpty(output.get())) {
107-
LOGF(error, notify("Histogram %d is empty - save aborted"), i + 1);
108-
return;
109-
}
110-
LOGF(debug, notify("Found histogram %d: %s"), i + 1, output->GetTitle());
111-
112-
int64_t now = duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
113-
114-
if (ccdbApi.storeAsTFileAny(output.get(), ccdbFullPath, createMetadata(i), now, now + config->confCCDBLifetime) == 0) {
115-
LOGF(info, notify("Histogram %d saved successfully"), i + 1);
116-
} else {
117-
LOGF(fatal, notify("Histogram %d save failed"), i + 1);
118-
}
119-
}
120-
});
121-
} else {
122-
LOGF(warn, notify("Uploading on stop callback is already set up"));
83+
if (shouldDoTruth) {
84+
for (const auto& particle : particles) {
85+
hMCTruth.template fillQA<false, false>(particle);
86+
}
12387
}
12488
}
12589

126-
template <uint8_t N>
127-
requires isOneOrTwo<N>
128-
auto getWeight(const auto& particle) const -> float
90+
auto getWeight(const size_t partNo, const auto& particle) const -> float
12991
{
13092
auto weight = 1.0f;
131-
auto hEff = hLoaded[N - 1];
93+
auto hEff = hLoaded[partNo - 1];
13294

13395
if (shouldApplyCorrections && hEff) {
13496
auto bin = hEff->FindBin(particle.pt());
@@ -139,92 +101,44 @@ class EfficiencyCalculator
139101
return weight;
140102
}
141103

142-
template <uint8_t N>
143-
requires isOneOrTwo<N>
144-
auto calculate(std::shared_ptr<TH1> hEff, std::shared_ptr<TH1> hTruth, std::shared_ptr<TH1> hReco)
145-
{
146-
if (!shouldCalculate) {
147-
return;
148-
}
149-
150-
if (!hTruth || !hReco) {
151-
LOGF(error, notify("MC Truth & MC Reco histograms cannot be null"));
152-
return;
153-
}
154-
155-
if (!hEff) {
156-
LOGF(error, notify("No target histogram to fill specified for particle %d"), N);
157-
return;
158-
}
159-
160-
for (auto bin = 0; bin < hEff->GetNbinsX(); bin++) {
161-
auto denom = hTruth->GetBinContent(bin);
162-
hEff->SetBinContent(bin, denom == 0 ? 0 : hReco->GetBinContent(bin) / denom);
163-
}
164-
165-
hOutput[N - 1] = hEff;
166-
}
167-
168104
private:
169105
static inline auto notify(const std::string& msg) -> const std::string
170106
{
171107
return fmt::format("[EFFICIENCY] {}", msg);
172108
}
173109

174-
static auto isHistogramEmpty(TH1* hist) -> bool
110+
auto loadEfficiencyFromCCDB(const size_t partNo) const -> TH1*
175111
{
176-
if (!hist) {
177-
return true;
178-
}
112+
std::map<std::string, std::string> metadata{};
179113

180-
// check overflow bins as well
181-
for (auto idx = 0; idx <= hist->GetNbinsX() + 1; idx++) {
182-
if (hist->GetBinContent(idx) != 0) {
183-
return false;
184-
}
114+
if (partNo - 1 < config->confEfficiencyCCDBLabels->size()) {
115+
metadata["label"] = config->confEfficiencyCCDBLabels.value[partNo - 1];
185116
}
186-
187-
return true;
188-
}
189-
190-
auto createMetadata(const uint8_t partNo) const -> std::map<std::string, std::string>
191-
{
192-
if (config->confEfficiencyCCDBLabels->size() != 2) {
193-
LOGF(fatal, notify("CCDB labels configurable should be exactly of size 2"));
117+
if (config->confCCDBTrainNumber > 0) {
118+
metadata["trainNumber"] = std::to_string(config->confCCDBTrainNumber);
194119
}
195-
return std::map<std::string, std::string>{
196-
{"label", config->confEfficiencyCCDBLabels.value[partNo]} //
197-
};
198-
}
199120

200-
template <uint8_t N>
201-
requires isOneOrTwo<N>
202-
auto loadEfficiencyFromCCDB(const int64_t timestamp) const -> TH1*
203-
{
204-
auto hEff = ccdb.getSpecific<TH1>(ccdbFullPath, timestamp, createMetadata(N - 1));
121+
auto timestamp = partNo - 1 < config->confEfficiencyCCDBTimestamps->size()
122+
? static_cast<int64_t>(config->confEfficiencyCCDBTimestamps.value[partNo - 1])
123+
: -1;
124+
125+
auto hEff = ccdb.getSpecific<TH1>(config->confCCDBPath, timestamp, metadata);
205126
if (!hEff || hEff->IsZombie()) {
206-
LOGF(error, notify("Could not load histogram from %s"), config->confCCDBPath.value);
127+
LOGF(error, notify("Could not load histogram from %s for particle %d"), config->confCCDBPath.value, partNo);
207128
return nullptr;
208129
}
209130

210-
LOGF(info, notify("Histogram \"%s\" loaded from \"%s\""), hEff->GetTitle(), config->confCCDBPath.value);
131+
LOGF(info, notify("Histogram for particle %d loaded from \"%s\""), partNo, config->confCCDBPath.value);
211132
return hEff;
212133
}
213134

214135
EfficiencyConfigurableGroup* config{};
215136

216-
bool shouldUploadOnStop = false;
217-
bool shouldCalculate = false;
137+
bool shouldDoTruth = false;
218138
bool shouldApplyCorrections = false;
219139

220-
std::array<std::shared_ptr<TH1>, 2> hOutput{};
140+
o2::ccdb::BasicCCDBManager& ccdb{o2::ccdb::BasicCCDBManager::instance()};
221141
std::array<TH1*, 2> hLoaded{};
222-
223-
o2::ccdb::CcdbApi ccdbApi{};
224-
std::string ccdbFullPath{};
225-
226-
static constexpr std::string_view folderName{"Efficiency"};
227-
static constexpr std::array<std::string_view, 3> histSuffix{"", "_one", "_two"};
228142
};
229143

230144
} // namespace o2::analysis::femto_universe::efficiency

0 commit comments

Comments
 (0)