Skip to content

Commit 473bb30

Browse files
authored
Common: allow access to the reference comparator plots in derived classes (#2578)
* [Common] allow access to the reference comparator plots in derived classes The changes allow to use the ReferenceComparatorTask as a base class, providing access to the reference comparison plots for the derived classes. A typical use case would be a post-processing task that produces some derived plots, which also need to compared with some reference. With the current production code, this involves adding a dedicated ReferenceComparatorTask in the workflow, that fetches the plots from the post-processing task from the QCDB. This commit allows to solve the above problem by deriving the post-processing task from the ReferenceComparatorTask, such that the reference comparison plots can be filled directly by the derived task, without going through the QCDB. This is achieved with the following two changes: * addition of a function that exposes the reference comparison plots in the ReferenceComparatorTask interface * storage of the pointer to the reference histogram in the ReferenceComparatorPlot object, such that it does not need to be passed to the `update()` method * [Common] add reference comparator plots accessor functions Add to the ReferenceUtils.h header file the functions that allow to access the current, reference and ratio plots from the TCanvas created by the ReferenceComparartorPlot class. This will allow for example custom checkers to access the plots.
1 parent 7e7a1f4 commit 473bb30

File tree

6 files changed

+102
-82
lines changed

6 files changed

+102
-82
lines changed

Framework/include/QualityControl/ReferenceUtils.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
#include "QualityControl/QcInfoLogger.h"
2626
#include "QualityControl/RepoPathUtils.h"
2727

28+
#include <TH1.h>
29+
#include <TCanvas.h>
30+
2831
namespace o2::quality_control::checker
2932
{
3033

@@ -42,6 +45,70 @@ static std::shared_ptr<quality_control::core::MonitorObject> getReferencePlot(qu
4245
return qcdb->retrieveMO(path, name, repository::DatabaseInterface::Timestamp::Latest, referenceActivity);
4346
}
4447

48+
//_________________________________________________________________________________________
49+
//
50+
// Get the current and reference histograms from the container canvas.
51+
// The two histograms are returned as a std::pair
52+
53+
static std::pair<TH1*, TH1*> getPlotsFromCanvas(TCanvas* canvas, std::string& message)
54+
{
55+
// Get the pad containing the current histogram, as well as the reference one in the case of 1-D plots
56+
TPad* padHist = dynamic_cast<TPad*>(canvas->GetPrimitive(TString::Format("%s_PadHist", canvas->GetName())));
57+
if (!padHist) {
58+
message = "missing PadHist";
59+
return { nullptr, nullptr };
60+
}
61+
// Get the pad containing the reference histogram.
62+
// This pad is only present for 2-D histograms.
63+
// 1-D histograms are drawn superimposed in the same pad
64+
TPad* padHistRef = (TPad*)canvas->GetPrimitive(TString::Format("%s_PadHistRef", canvas->GetName()));
65+
66+
// Get the current histogram
67+
TH1* hist = dynamic_cast<TH1*>(padHist->GetPrimitive(TString::Format("%s_hist", canvas->GetName())));
68+
if (!hist) {
69+
message = "missing histogram";
70+
return { nullptr, nullptr };
71+
}
72+
73+
// Get the reference histogram, trying both pads
74+
TH1* histRef = nullptr;
75+
if (padHistRef) {
76+
histRef = dynamic_cast<TH1*>(padHistRef->GetPrimitive(TString::Format("%s_hist_ref", canvas->GetName())));
77+
} else {
78+
histRef = dynamic_cast<TH1*>(padHist->GetPrimitive(TString::Format("%s_hist_ref", canvas->GetName())));
79+
}
80+
81+
if (!histRef) {
82+
message = "missing reference histogram";
83+
return { nullptr, nullptr };
84+
}
85+
86+
// return a pair with the two histograms
87+
return { hist, histRef };
88+
}
89+
90+
//_________________________________________________________________________________________
91+
//
92+
// Get the ratio histogram from the container canvas
93+
94+
static TH1* getRatioPlotFromCanvas(TCanvas* canvas)
95+
{
96+
// Get the pad containing the current histogram, as well as the reference one in the case of 1-D plots
97+
TPad* padHistRatio = dynamic_cast<TPad*>(canvas->GetPrimitive(TString::Format("%s_PadHistRatio", canvas->GetName())));
98+
if (!padHistRatio) {
99+
return nullptr;
100+
}
101+
102+
// Get the current histogram
103+
TH1* histRatio = dynamic_cast<TH1*>(padHistRatio->GetPrimitive(TString::Format("%s_hist_ratio", canvas->GetName())));
104+
if (!histRatio) {
105+
return nullptr;
106+
}
107+
108+
// return a pair with the two histograms
109+
return histRatio;
110+
}
111+
45112
} // namespace o2::quality_control::checker
46113

47114
#endif // QUALITYCONTROL_ReferenceUtils_H

Modules/Common/include/Common/ReferenceComparatorPlot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ReferenceComparatorPlot
4848
virtual ~ReferenceComparatorPlot() = default;
4949

5050
TObject* getMainCanvas();
51-
void update(TH1* histogram, TH1* referenceHistogram);
51+
void update(TH1* histogram);
5252

5353
private:
5454
std::shared_ptr<ReferenceComparatorPlotImpl> mImplementation;

Modules/Common/include/Common/ReferenceComparatorTask.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ class ReferenceComparatorTask : public quality_control::postprocessing::PostProc
5050
void update(quality_control::postprocessing::Trigger, framework::ServiceRegistryRef) override;
5151
void finalize(quality_control::postprocessing::Trigger, framework::ServiceRegistryRef) override;
5252

53+
std::map<std::string, std::shared_ptr<ReferenceComparatorPlot>>& getComparatorPlots() { return mHistograms; }
54+
std::shared_ptr<ReferenceComparatorPlot> getComparatorPlot(std::string plotName);
55+
5356
struct HistoWithRef {
5457
std::shared_ptr<TH1> mPlot;
5558
std::shared_ptr<TH1> mRefPlot;

Modules/Common/src/ReferenceComparatorCheck.cxx

Lines changed: 3 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -75,72 +75,10 @@ void ReferenceComparatorCheck::endOfActivity(const Activity& activity)
7575
{
7676
}
7777

78-
//_________________________________________________________________________________________
79-
//
80-
// Get the current and reference histograms from the canvas.
81-
// The two histograms are returned as a std::pair
82-
static std::pair<TH1*, TH1*> getPlotsFromCanvas(TCanvas* canvas, std::string& message)
83-
{
84-
// Get the pad containing the current histogram, as well as the reference one in the case of 1-D plots
85-
TPad* padHist = (TPad*)canvas->GetPrimitive(TString::Format("%s_PadHist", canvas->GetName()));
86-
if (!padHist) {
87-
message = "missing PadHist";
88-
return { nullptr, nullptr };
89-
}
90-
// Get the pad containing the reference histogram.
91-
// This pad is only present for 2-D histograms.
92-
// 1-D histograms are drawn superimposed in the same pad
93-
TPad* padHistRef = (TPad*)canvas->GetPrimitive(TString::Format("%s_PadHistRef", canvas->GetName()));
94-
95-
// Get the current histogram
96-
TH1* hist = dynamic_cast<TH1*>(padHist->GetPrimitive(TString::Format("%s_hist", canvas->GetName())));
97-
if (!hist) {
98-
message = "missing histogram";
99-
return { nullptr, nullptr };
100-
}
101-
102-
// Get the reference histogram, trying both pads
103-
TH1* histRef = nullptr;
104-
if (padHistRef) {
105-
histRef = dynamic_cast<TH1*>(padHistRef->GetPrimitive(TString::Format("%s_hist_ref", canvas->GetName())));
106-
} else {
107-
histRef = dynamic_cast<TH1*>(padHist->GetPrimitive(TString::Format("%s_hist_ref", canvas->GetName())));
108-
}
109-
110-
if (!histRef) {
111-
message = "missing reference histogram";
112-
return { nullptr, nullptr };
113-
}
114-
115-
// return a pair with the two histograms
116-
return { hist, histRef };
117-
}
118-
11978
static std::pair<TH1*, TH1*> getPlotsFromCanvas(TCanvas* canvas)
12079
{
12180
std::string dummyMessage;
122-
return getPlotsFromCanvas(canvas, dummyMessage);
123-
}
124-
125-
//_________________________________________________________________________________________
126-
//
127-
// Get the ratio histograms from the canvas
128-
static TH1* getRatioPlotFromCanvas(TCanvas* canvas)
129-
{
130-
// Get the pad containing the current histogram, as well as the reference one in the case of 1-D plots
131-
TPad* padHistRatio = (TPad*)canvas->GetPrimitive(TString::Format("%s_PadHistRatio", canvas->GetName()));
132-
if (!padHistRatio) {
133-
return nullptr;
134-
}
135-
136-
// Get the current histogram
137-
TH1* histRatio = dynamic_cast<TH1*>(padHistRatio->GetPrimitive(TString::Format("%s_hist_ratio", canvas->GetName())));
138-
if (!histRatio) {
139-
return nullptr;
140-
}
141-
142-
// return a pair with the two histograms
143-
return histRatio;
81+
return o2::quality_control::checker::getPlotsFromCanvas(canvas, dummyMessage);
14482
}
14583

14684
// Get the current and reference histograms from the canvas, and compare them using the comparator object passed as parameter
@@ -156,7 +94,7 @@ static Quality compare(TCanvas* canvas, ObjectComparatorInterface* comparator, s
15694
}
15795

15896
// extract the histograms from the canvas
159-
auto plots = getPlotsFromCanvas(canvas, message);
97+
auto plots = o2::quality_control::checker::getPlotsFromCanvas(canvas, message);
16098
if (!plots.first || !plots.second) {
16199
return Quality::Null;
162100
}
@@ -389,7 +327,7 @@ void ReferenceComparatorCheck::beautify(std::shared_ptr<MonitorObject> mo, Quali
389327
setQualityLabel(canvas, quality);
390328

391329
// draw a double-arrow indicating the horizontal range for the check, if it is set
392-
beautifyRatioPlot(moName, getRatioPlotFromCanvas(canvas), quality);
330+
beautifyRatioPlot(moName, o2::quality_control::checker::getRatioPlotFromCanvas(canvas), quality);
393331
} else {
394332
// draw the quality label directly on the plot if the MO is an histogram
395333
auto* th1 = dynamic_cast<TH1*>(mo->getObject());

Modules/Common/src/ReferenceComparatorPlot.cxx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,21 @@ static std::shared_ptr<HIST> createHisto2D(const char* name, const char* title,
159159
class ReferenceComparatorPlotImpl
160160
{
161161
public:
162-
ReferenceComparatorPlotImpl(bool scaleReference)
163-
: mScaleReference(scaleReference)
162+
ReferenceComparatorPlotImpl(TH1* referenceHistogram, bool scaleReference)
163+
: mReferenceHistogram(referenceHistogram), mScaleReference(scaleReference)
164164
{
165165
}
166166

167167
virtual ~ReferenceComparatorPlotImpl() = default;
168168

169-
virtual TObject* init(TH1* referenceHistogram, std::string outputPath, bool scaleReference, bool drawRatioOnly, std::string drawOption)
169+
virtual TObject* getMainCanvas()
170170
{
171171
return nullptr;
172172
}
173173

174-
virtual TObject* getMainCanvas()
174+
TH1* getReferenceHistogram()
175175
{
176-
return nullptr;
176+
return mReferenceHistogram;
177177
}
178178

179179
void setScaleRef(bool scaleReference)
@@ -183,9 +183,10 @@ class ReferenceComparatorPlotImpl
183183

184184
bool getScaleReference() { return mScaleReference; }
185185

186-
virtual void update(TH1* histogram, TH1* referenceHistogram) = 0;
186+
virtual void update(TH1* histogram) = 0;
187187

188188
private:
189+
TH1* mReferenceHistogram{ nullptr };
189190
bool mScaleReference{ true };
190191
};
191192

@@ -200,7 +201,7 @@ class ReferenceComparatorPlotImpl1D : public ReferenceComparatorPlotImpl
200201
bool drawRatioOnly,
201202
double legendHeight,
202203
const std::string& drawOption)
203-
: ReferenceComparatorPlotImpl(scaleReference), mLegendHeight(legendHeight)
204+
: ReferenceComparatorPlotImpl(referenceHistogram, scaleReference), mLegendHeight(legendHeight)
204205
{
205206
float labelSize = 0.04;
206207

@@ -343,8 +344,9 @@ class ReferenceComparatorPlotImpl1D : public ReferenceComparatorPlotImpl
343344
return mCanvas.get();
344345
}
345346

346-
void update(TH1* hist, TH1* referenceHistogram)
347+
void update(TH1* hist)
347348
{
349+
TH1* referenceHistogram = getReferenceHistogram();
348350
if (!hist || !referenceHistogram) {
349351
return;
350352
}
@@ -389,7 +391,7 @@ class ReferenceComparatorPlotImpl2D : public ReferenceComparatorPlotImpl
389391
bool scaleReference,
390392
bool drawRatioOnly,
391393
const std::string& drawOption)
392-
: ReferenceComparatorPlotImpl(scaleReference)
394+
: ReferenceComparatorPlotImpl(referenceHistogram, scaleReference)
393395
{
394396
if (!referenceHistogram) {
395397
return;
@@ -498,8 +500,9 @@ class ReferenceComparatorPlotImpl2D : public ReferenceComparatorPlotImpl
498500
return mCanvas.get();
499501
}
500502

501-
void update(TH1* histogram, TH1* referenceHistogram)
503+
void update(TH1* histogram)
502504
{
505+
TH1* referenceHistogram = getReferenceHistogram();
503506
if (!histogram || !referenceHistogram) {
504507
return;
505508
}
@@ -559,10 +562,10 @@ TObject* ReferenceComparatorPlot::getMainCanvas()
559562
return (mImplementation.get() ? mImplementation->getMainCanvas() : nullptr);
560563
}
561564

562-
void ReferenceComparatorPlot::update(TH1* histogram, TH1* referenceHistogram)
565+
void ReferenceComparatorPlot::update(TH1* histogram)
563566
{
564567
if (mImplementation) {
565-
mImplementation->update(histogram, referenceHistogram);
568+
mImplementation->update(histogram);
566569
}
567570
}
568571

Modules/Common/src/ReferenceComparatorTask.cxx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ void ReferenceComparatorTask::initialize(quality_control::postprocessing::Trigge
167167

168168
//_________________________________________________________________________________________
169169

170+
std::shared_ptr<ReferenceComparatorPlot> ReferenceComparatorTask::getComparatorPlot(std::string plotName)
171+
{
172+
// check if a corresponding output plot was initialized
173+
auto iter = mHistograms.find(plotName);
174+
if (iter == mHistograms.end()) {
175+
return {};
176+
}
177+
return iter->second;
178+
}
179+
180+
//_________________________________________________________________________________________
181+
170182
void ReferenceComparatorTask::update(quality_control::postprocessing::Trigger trigger, framework::ServiceRegistryRef services)
171183
{
172184
auto& qcdb = services.get<repository::DatabaseInterface>();
@@ -194,10 +206,7 @@ void ReferenceComparatorTask::update(quality_control::postprocessing::Trigger tr
194206
}
195207

196208
// update the plot ratios and the histograms with superimposed reference
197-
auto referenceMO = mReferencePlots[plotName];
198-
TH1* referenceHistogram = dynamic_cast<TH1*>(referenceMO->getObject());
199-
200-
iter->second->update(histogram, referenceHistogram);
209+
iter->second->update(histogram);
201210
}
202211
}
203212
}

0 commit comments

Comments
 (0)