Skip to content

Commit 5aac19d

Browse files
authored
Return ptr when adding histogram to registry (#6783)
Especially useful when setting labels for the axis
1 parent 2564cfb commit 5aac19d

4 files changed

Lines changed: 30 additions & 24 deletions

File tree

Analysis/EventFiltering/cefpTask.cxx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,8 @@ struct centralEventFilterTask {
118118
}
119119
LOG(INFO) << "Middle init, total number of columns " << nCols;
120120

121-
scalers.add("mScalers", "", HistType::kTH1F, {{nCols + 1, -0.5, 0.5 + nCols, ";;Number of events"}});
122-
scalers.add("mFiltered", "", HistType::kTH1F, {{nCols + 1, -0.5, 0.5 + nCols, ";;Number of filtered events"}});
123-
auto mScalers = scalers.get<TH1>(HIST("mScalers"));
124-
auto mFiltered = scalers.get<TH1>(HIST("mFiltered"));
121+
auto mScalers = std::get<std::shared_ptr<TH1>>(scalers.add("mScalers", ";;Number of events", HistType::kTH1F, {{nCols + 1, -0.5, 0.5 + nCols}}));
122+
auto mFiltered = std::get<std::shared_ptr<TH1>>(scalers.add("mFiltered", ";;Number of filtered events", HistType::kTH1F, {{nCols + 1, -0.5, 0.5 + nCols}}));
125123

126124
mScalers->GetXaxis()->SetBinLabel(1, "Total number of events");
127125
mFiltered->GetXaxis()->SetBinLabel(1, "Total number of events");
@@ -143,13 +141,15 @@ struct centralEventFilterTask {
143141

144142
void run(ProcessingContext& pc)
145143
{
144+
auto mScalers{scalers.get<TH1>(HIST("mScalers"))};
145+
auto mFiltered{scalers.get<TH1>(HIST("mFiltered"))};
146146

147-
auto mScalers = scalers.get<TH1>(HIST("mScalers"));
148-
auto mFiltered = scalers.get<TH1>(HIST("mFiltered"));
149147
int64_t nEvents{-1};
150148
for (auto& tableName : mDownscaling) {
149+
if (!pc.inputs().isValid(tableName.first)) {
150+
LOG(FATAL) << tableName.first << " table is not valid.";
151+
}
151152
auto tableConsumer = pc.inputs().get<TableConsumer>(tableName.first);
152-
153153
auto tablePtr{tableConsumer->asArrowTable()};
154154
int64_t nRows{tablePtr->num_rows()};
155155
nEvents = nEvents < 0 ? nRows : nEvents;

Analysis/EventFiltering/nucleiFilter.cxx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ struct nucleiFilter {
8080
spectra.add("fCollZpos", "collision z position", HistType::kTH1F, {{600, -20., +20., "z position (cm)"}});
8181
spectra.add("fTPCsignal", "Specific energy loss", HistType::kTH2F, {{600, 0., 3, "#it{p} (GeV/#it{c})"}, {1400, 0, 1400, "d#it{E} / d#it{X} (a. u.)"}});
8282
spectra.add("fTPCcounts", "n-sigma TPC", HistType::kTH2F, {ptAxis, {200, -100., +100., "n#sigma_{He} (a. u.)"}});
83-
spectra.add("fProcessedEvents", "Nuclei - event filtered", HistType::kTH1F, {{4, -0.5, 3.5, "Event counter"}});
83+
84+
auto scalers{std::get<std::shared_ptr<TH1>>(spectra.add("fProcessedEvents", ";;Number of filtered events", HistType::kTH1F, {{4, -0.5, 3.5}}))};
85+
for (uint32_t iS{1}; iS <= nucleiNames.size(); ++iS) {
86+
scalers->GetXaxis()->SetBinLabel(iS, nucleiNames[iS - 1].data());
87+
}
8488
}
8589

8690
Filter collisionFilter = nabs(aod::collision::posZ) < cfgCutVertex;
@@ -95,7 +99,7 @@ struct nucleiFilter {
9599
spectra.fill(HIST("fCollZpos"), collision.posZ());
96100
//
97101

98-
for (auto track : tracks) { // start loop over tracks
102+
for (auto& track : tracks) { // start loop over tracks
99103

100104
const float nSigmaTPC[nNuclei]{
101105
track.tpcNSigmaDe(), track.tpcNSigmaTr(), track.tpcNSigmaHe(), track.tpcNSigmaAl()};

Framework/Core/include/Framework/HistogramRegistry.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ class HistogramRegistry
9090
HistogramRegistry(char const* const name, std::vector<HistogramSpec> histSpecs = {}, OutputObjHandlingPolicy policy = OutputObjHandlingPolicy::AnalysisObject, bool sortHistos = true, bool createRegistryDir = false);
9191

9292
// functions to add histograms to the registry
93-
void add(const HistogramSpec& histSpec);
94-
void add(char const* const name, char const* const title, const HistogramConfigSpec& histConfigSpec, bool callSumw2 = false);
95-
void add(char const* const name, char const* const title, HistType histType, std::vector<AxisSpec> axes, bool callSumw2 = false);
93+
HistPtr add(const HistogramSpec& histSpec);
94+
HistPtr add(char const* const name, char const* const title, const HistogramConfigSpec& histConfigSpec, bool callSumw2 = false);
95+
HistPtr add(char const* const name, char const* const title, HistType histType, std::vector<AxisSpec> axes, bool callSumw2 = false);
9696
void addClone(const std::string& source, const std::string& target);
9797

9898
// function to query if name is already in use
@@ -137,11 +137,11 @@ class HistogramRegistry
137137

138138
private:
139139
// create histogram from specification and insert it into the registry
140-
void insert(const HistogramSpec& histSpec);
140+
HistPtr insert(const HistogramSpec& histSpec);
141141

142142
// clone an existing histogram and insert it into the registry
143143
template <typename T>
144-
void insertClone(const HistName& histName, const std::shared_ptr<T>& originalHist);
144+
HistPtr insertClone(const HistName& histName, const std::shared_ptr<T>& originalHist);
145145

146146
// helper function that checks if histogram name can be used in registry
147147
void validateHistName(const char* name, const uint32_t hash);
@@ -345,7 +345,7 @@ auto& HistogramRegistry::operator()(const HistName& histName)
345345
}
346346

347347
template <typename T>
348-
void HistogramRegistry::insertClone(const HistName& histName, const std::shared_ptr<T>& originalHist)
348+
HistPtr HistogramRegistry::insertClone(const HistName& histName, const std::shared_ptr<T>& originalHist)
349349
{
350350
validateHistName(histName.str, histName.hash);
351351
for (auto i = 0u; i < MAX_REGISTRY_SIZE; ++i) {
@@ -356,10 +356,11 @@ void HistogramRegistry::insertClone(const HistName& histName, const std::shared_
356356
mRegistryKey[imask(histName.idx + i)] = histName.hash;
357357
mRegistryValue[imask(histName.idx + i)] = std::shared_ptr<T>(static_cast<T*>(originalHist->Clone(histName.str)));
358358
lookup += i;
359-
return;
359+
return mRegistryValue[imask(histName.idx + i)];
360360
}
361361
}
362362
LOGF(FATAL, R"(Internal array of HistogramRegistry "%s" is full.)", mName);
363+
return HistPtr();
363364
}
364365

365366
template <typename T>

Framework/Core/src/HistogramRegistry.cxx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void HistogramRegistry::setHash(uint32_t hash)
5757
}
5858

5959
// create histogram from specification and insert it into the registry
60-
void HistogramRegistry::insert(const HistogramSpec& histSpec)
60+
HistPtr HistogramRegistry::insert(const HistogramSpec& histSpec)
6161
{
6262
validateHistName(histSpec.name.data(), histSpec.hash);
6363
const uint32_t idx = imask(histSpec.hash);
@@ -69,10 +69,11 @@ void HistogramRegistry::insert(const HistogramSpec& histSpec)
6969
mRegistryKey[imask(idx + i)] = histSpec.hash;
7070
mRegistryValue[imask(idx + i)] = HistFactory::createHistVariant(histSpec);
7171
lookup += i;
72-
return;
72+
return mRegistryValue[imask(idx + i)];
7373
}
7474
}
7575
LOGF(FATAL, R"(Internal array of HistogramRegistry "%s" is full.)", mName);
76+
return HistPtr();
7677
}
7778

7879
// helper function that checks if histogram name can be used in registry
@@ -95,19 +96,19 @@ void HistogramRegistry::validateHistName(const char* name, const uint32_t hash)
9596
*/
9697
}
9798

98-
void HistogramRegistry::add(const HistogramSpec& histSpec)
99+
HistPtr HistogramRegistry::add(const HistogramSpec& histSpec)
99100
{
100-
insert(histSpec);
101+
return insert(histSpec);
101102
}
102103

103-
void HistogramRegistry::add(char const* const name, char const* const title, const HistogramConfigSpec& histConfigSpec, bool callSumw2)
104+
HistPtr HistogramRegistry::add(char const* const name, char const* const title, const HistogramConfigSpec& histConfigSpec, bool callSumw2)
104105
{
105-
insert({name, title, histConfigSpec, callSumw2});
106+
return insert({name, title, histConfigSpec, callSumw2});
106107
}
107108

108-
void HistogramRegistry::add(char const* const name, char const* const title, HistType histType, std::vector<AxisSpec> axes, bool callSumw2)
109+
HistPtr HistogramRegistry::add(char const* const name, char const* const title, HistType histType, std::vector<AxisSpec> axes, bool callSumw2)
109110
{
110-
insert({name, title, {histType, axes}, callSumw2});
111+
return insert({name, title, {histType, axes}, callSumw2});
111112
}
112113

113114
// store a copy of an existing histogram (or group of histograms) under a different name

0 commit comments

Comments
 (0)