CbmRoot
Loading...
Searching...
No Matches
CbmQaTask.h
Go to the documentation of this file.
1/* Copyright (C) 2023 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#pragma once
11
12#include "CbmEvent.h"
13#include "CbmQaCompare.h"
14#include "CbmQaIO.h"
15#include "CbmQaTable.h"
16#include "FairTask.h"
17#include "Logger.h"
18#include "TCanvas.h"
19#include "TEfficiency.h"
20#include "TFile.h"
21#include "TFolder.h"
22#include "TH1.h"
23#include "TH2.h"
24#include "TH3.h"
25#include "TParameter.h"
26#include "TProfile.h"
27#include "TProfile2D.h"
28#include "TProfile3D.h"
29#include "TROOT.h"
30#include "TString.h"
31
32#include <algorithm>
33#include <map>
34#include <regex>
35#include <string_view>
36#include <tuple>
37#include <type_traits>
38
39#include <yaml-cpp/yaml.h>
40
41class CbmEvent;
42class TClonesArray;
43
46class CbmQaTask : public FairTask, public CbmQaIO {
51 struct CheckFlags {
52 std::string fMsg = "";
53 bool fResult = false;
54 bool fStatus = false;
55 };
56
60 uint8_t fPoint = 0;
61 uint8_t fRatio = 0;
62 uint8_t fStat = 0;
63 double fRatioMin = 0;
64 double fRatioMax = std::numeric_limits<double>::max();
65 double fChi2NdfMax = std::numeric_limits<double>::max();
66 std::string fStatOpt = "UU";
67 std::string fCanvOpt = "";
68 };
69
70 public:
76 CbmQaTask(const char* name, int verbose, bool isMCUsed, ECbmRecoMode recoMode = ECbmRecoMode::Timeslice);
77
79 CbmQaTask() = delete; // TODO: Let's see, what can happen, if one deletes default constructor
80
82 virtual ~CbmQaTask() = default;
83
85 CbmQaTask(const CbmQaTask&) = delete;
86
88 CbmQaTask(CbmQaTask&&) = delete;
89
91 CbmQaTask& operator=(const CbmQaTask&) = delete;
92
95
99 virtual void Check() = 0;
100
104 bool CompareQaObjects();
105
110
111
113 const std::map<std::string, CheckFlags>& GetCheckList() const { return fmCheckList; }
114
116 const TString& GetDefaultTag() const { return fsDefaultTag; }
117
119 const std::string& GetSetupName() const { return fsSetupName; }
120
122 const TString& GetVersionTag() const { return fsVersionTag; }
123
124
126 bool IsMCUsed() const { return fbUseMC; }
127
130
131
134 void SetCheckFile(const std::shared_ptr<TFile>& pCheckFile) { fpBenchmarkInput = pCheckFile; }
135
138 void SetCompareOutput(const std::shared_ptr<TFile>& pCompareOutput) { fpBenchmarkOutput = pCompareOutput; }
139
141 void SetVersionTag(const TString& tag) { fsVersionTag = tag; }
142
144 void SetDefaultTag(const TString& tag) { fsDefaultTag = tag; }
145
152 void SetRecoMode(ECbmRecoMode recoMode) { fRecoMode = recoMode; }
153
155 void SetSetupName(const char* setup) { fsSetupName = setup; }
156
157 protected:
159 virtual void DeInit() {}
160
162 void Exec(Option_t* /*option*/) override final;
163
165 void Finish() override final;
166
168 InitStatus Init() override final;
169
171 InitStatus ReInit() override final;
172
174 virtual InitStatus InitQa() { return kSUCCESS; }
175
177 virtual void CreateSummary() {}
178
180 virtual void ExecQa() {}
181
183 int GetEventNumber() const { return fNofEvents.GetVal(); }
184
185 // ***********************
186 // ** Utility functions **
187 // ***********************
188
195 template<typename T>
196 bool CheckRange(std::string_view name, T var, T lo, T hi) const;
197
205 template<typename T>
206 bool CheckRange(std::string_view name, T var, T varErr, T lo, T hi) const;
207
210 bool CheckRange(TH1* h, double meanMax, double rmsMin, double rmsMax);
211
213 void PutSetupNameOnPad(double xMin, double yMin, double xMax, double yMax);
214
217
222 void StoreCheckResult(const std::string& tag, bool result, const std::string& msg = "");
223
224 private:
226 void DeInitBase();
227
236 template<class Obj>
237 bool CompareTwoObjects(const TObject* pObjL, const TObject* pObjR, const TString& objName,
238 const ObjectComparisonConfig& cfg);
239
240 bool fbUseMC = false;
241
243
251 std::map<std::string, CheckFlags> fmCheckList;
252
253 TClonesArray* fpBrEvents = nullptr;
255 std::string fsSetupName = "";
256 TParameter<int> fNofEvents{"nEvents", 0};
257
258 TString fsVersionTag = "";
259 TString fsDefaultTag = "";
260
261 std::shared_ptr<TFile> fpBenchmarkInput = nullptr;
262 std::shared_ptr<TFile> fpBenchmarkOutput = nullptr;
263
265};
266
267
268// *************************************************
269// ** Inline and template function implementation **
270// *************************************************
271
272// ---------------------------------------------------------------------------------------------------------------------
273//
274template<typename T>
275bool CbmQaTask::CheckRange(std::string_view name, T var, T lo, T hi) const
276{
277 return CheckRange<T>(name, var, 0., lo, hi);
278}
279
280// ---------------------------------------------------------------------------------------------------------------------
281//
282template<typename T>
283bool CbmQaTask::CheckRange(std::string_view name, T var, T varErr, T lo, T hi) const
284{
285 bool ret = true;
286 if (var + 3.5 * varErr < lo) {
287 LOG(error) << fName << ": " << name << " is found to be under the lower limit (" << var << " +- 3.5 x " << varErr
288 << " < " << lo << ')';
289 ret = false;
290 }
291 if (var - 3.5 * varErr > hi) {
292 LOG(error) << fName << ": " << name << " is found to be above the upper limit (" << var << " +-3.5 x " << varErr
293 << " > " << hi << ')';
294 ret = false;
295 }
296 if (ret) {
297 LOG(debug) << fName << ": " << name << " is found to be within limit (" << lo << " <= " << var << " +- 3.5 x "
298 << varErr << " <= " << hi << ')';
299 }
300 return ret;
301}
302
303// ---------------------------------------------------------------------------------------------------------------------
304//
305template<class Obj>
306bool CbmQaTask::CompareTwoObjects(const TObject* pObjL, const TObject* pObjR, const TString& objName,
307 const ObjectComparisonConfig& cfg)
308{
309 const auto* pHistL = static_cast<const Obj*>(pObjL);
310 const auto* pHistR = dynamic_cast<const Obj*>(pObjR);
311 if (!pHistR) {
312 LOG(error) << fName << "::CompareObjects(): the default " << pObjR->GetName()
313 << " has different type to the new object";
314 return false;
315 }
316 std::string opt = "";
317 if (cfg.fPoint > 0) {
318 opt += "p";
319 }
320 if (cfg.fRatio > 0) {
321 opt += "r";
322 }
323 if (cfg.fStat > 0) {
324 opt += "s";
325 }
326
327 auto comparator = CbmQaCompare<Obj>(pHistL, pHistR, 0);
328 auto out = comparator(opt, cfg.fStatOpt);
329
330 if (!out.fConsistent) {
331 LOG(info) << fName << ": This and default versions of the object " << pObjL->GetName() << " are incompatible";
332 return false;
333 }
334
335 // Collect status and print log
336 bool res = true;
337 {
338 std::stringstream msg;
339 using std::setw;
340 constexpr const char* kEqual = "\e[1;32mequal\e[0m";
341 constexpr const char* kDiff = "\e[1;32mdifferent\e[0m";
342 msg << "\tobject " << setw(12) << pObjL->ClassName() << ' ' << setw(50) << objName;
343 if (cfg.fPoint) {
344 msg << ": point-by-point -> " << (out.fPointByPoint ? kEqual : kDiff);
345 if (cfg.fPoint == 2) {
346 res &= out.fPointByPoint;
347 }
348 }
349 if (cfg.fRatio) {
350 bool bOk = (out.fRatioLo >= cfg.fRatioMin && out.fRatioUp <= cfg.fRatioMax);
351 msg << ", ratio -> " << (bOk ? kEqual : kDiff) << "(lo: " << out.fRatioLo << ", up: " << out.fRatioUp << ')';
352 if (cfg.fRatio == 2) {
353 res &= bOk;
354 }
355 }
356 if (cfg.fStat) {
357 bool bOk = out.fChi2NDF <= cfg.fChi2NdfMax;
358 msg << ", stat. test -> " << (bOk ? kEqual : kDiff) << "(chi2/NDF: " << out.fChi2NDF << ')';
359 if (cfg.fStat == 2) {
360 res &= bOk;
361 }
362 }
363 LOG(info) << msg.str();
364 }
365
366 // Write comparison result
367 if (fpBenchmarkOutput.get()) {
368 fpBenchmarkOutput->mkdir(objName);
369 auto* pDir = fpBenchmarkOutput->GetDirectory(objName);
370 pDir->cd();
371 pObjL->Write(Form("%s_%s", pObjL->GetName(), fsVersionTag.Data()));
372 pObjR->Write(Form("%s_%s", pObjL->GetName(), fsDefaultTag.Data()));
373 if (true || !res) { // Save canvas only if the histograms are different
374 auto* pCanv = comparator.GetComparisonCanvas(cfg.fCanvOpt);
375 if (pCanv) {
376 pCanv->Write(Form("%s_cmp_canvas", pObjL->GetName()));
377 delete pCanv;
378 }
379 }
380 }
381 return res;
382}
ECbmRecoMode
Reconstruct the full time slice or event-by-event.
Definition CbmDefs.h:162
A histogram comparison module for the QA task (declaration)
Module for ROOT objects IO interface (header)
Definition of CbmQaTable class.
Class characterising one event by a collection of links (indices) to data objects,...
Definition CbmEvent.h:34
Class to compare histograms of the QA task with default ones.
ROOT object IO interface for QA.
Definition CbmQaIO.h:44
void SetVersionTag(const TString &tag)
Sets version tag.
Definition CbmQaTask.h:141
void SetCheckFile(const std::shared_ptr< TFile > &pCheckFile)
Sets check-file.
Definition CbmQaTask.h:134
CbmEvent * GetCurrentEvent()
Gets pointer to current event.
Definition CbmQaTask.h:216
virtual void CreateSummary()
Initializes QA-task summary: canvases, tables etc.
Definition CbmQaTask.h:177
virtual ~CbmQaTask()=default
Destructor.
void SetDefaultTag(const TString &tag)
Sets default tag.
Definition CbmQaTask.h:144
const std::map< std::string, CheckFlags > & GetCheckList() const
Gets check-list.
Definition CbmQaTask.h:113
const TString & GetVersionTag() const
Gets version tag.
Definition CbmQaTask.h:122
std::string fsSetupName
Name of the setup (to draw on the canvases)
Definition CbmQaTask.h:255
bool CheckRange(std::string_view name, T var, T lo, T hi) const
Checks range of variable.
Definition CbmQaTask.h:275
CbmQaTask & operator=(const CbmQaTask &)=delete
Copy assignment operator.
void DisableEventMode()
Disables event-by-event execution.
const TString & GetDefaultTag() const
Gets default tag.
Definition CbmQaTask.h:116
virtual void DeInit()
De-initialize the task.
Definition CbmQaTask.h:159
void SetRecoMode(ECbmRecoMode recoMode)
Sets data processing (reconstruction) mode.
Definition CbmQaTask.h:152
std::map< std::string, CheckFlags > fmCheckList
A QA check-list map.
Definition CbmQaTask.h:251
InitStatus Init() override final
FairTask: Task initialization in the beginning of the run.
Definition CbmQaTask.cxx:88
void SetCompareOutput(const std::shared_ptr< TFile > &pCompareOutput)
Sets compare output file.
Definition CbmQaTask.h:138
std::shared_ptr< TFile > fpBenchmarkOutput
An output file for histograms cross-check.
Definition CbmQaTask.h:262
bool CompareQaObjects()
Process ROOT objects comparison.
void DeInitBase()
De-initializes this task.
CbmQaTask(const CbmQaTask &)=delete
Copy constructor.
std::shared_ptr< TFile > fpBenchmarkInput
A file with default ROOT objects used for the cross-check.
Definition CbmQaTask.h:261
virtual void ExecQa()
Method to fill histograms per event or time-slice.
Definition CbmQaTask.h:180
CbmQaTask & operator=(CbmQaTask &&)=delete
Move assignment operator.
virtual InitStatus InitQa()
Initializes the task.
Definition CbmQaTask.h:174
void Exec(Option_t *) override final
FairTask: Defines action of the task in the event/TS.
Definition CbmQaTask.cxx:44
bool CompareTwoObjects(const TObject *pObjL, const TObject *pObjR, const TString &objName, const ObjectComparisonConfig &cfg)
Process object comparison.
Definition CbmQaTask.h:306
const std::string & GetSetupName() const
Gets name of the setup.
Definition CbmQaTask.h:119
virtual void Check()=0
Function to check, if the QA results are acceptable.
void StoreCheckResult(const std::string &tag, bool result, const std::string &msg="")
Stores check flag to the check-list.
int GetEventNumber() const
Get current event number.
Definition CbmQaTask.h:183
CbmQaTask()=delete
Default constructor.
CbmQaTask(CbmQaTask &&)=delete
Move constructor.
TString fsVersionTag
Version tag (git SHA etc.)
Definition CbmQaTask.h:258
void ReadCheckListFromConfig()
Reads check-list from the configuration file.
void SetSetupName(const char *setup)
Sets name of the setup.
Definition CbmQaTask.h:155
TParameter< int > fNofEvents
Number of processed events.
Definition CbmQaTask.h:256
bool IsMCUsed() const
Returns flag, whether MC information is used or not in the task.
Definition CbmQaTask.h:126
bool fbUseMC
Flag, if MC is used.
Definition CbmQaTask.h:240
TString fsDefaultTag
Default tag (git SHA etc.)
Definition CbmQaTask.h:259
void PutSetupNameOnPad(double xMin, double yMin, double xMax, double yMax)
Puts setup title on the canvas.
ClassDefOverride(CbmQaTask, 0)
TClonesArray * fpBrEvents
Pointer to CbmEvent branch.
Definition CbmQaTask.h:253
CbmEvent * fpCurrentEvent
Pointer to the current event.
Definition CbmQaTask.h:254
ECbmRecoMode fRecoMode
Reconstruction mode.
Definition CbmQaTask.h:242
void Finish() override final
FairTask: Defines action of the task in the end of run.
Definition CbmQaTask.cxx:64
InitStatus ReInit() override final
FairTask: Task reinitialization.
Data class with information on a STS local track.
Contains a check result and its activeness status of the check-list entry.
Definition CbmQaTask.h:51
bool fResult
Check result storage.
Definition CbmQaTask.h:53
std::string fMsg
Supporting message for the check.
Definition CbmQaTask.h:52
bool fStatus
Status of the check.
Definition CbmQaTask.h:54
Contains configuration to compare two root objects (histograms)
Definition CbmQaTask.h:59