CbmRoot
Loading...
Searching...
No Matches
CbmQaReportTable.cxx
Go to the documentation of this file.
1/* Copyright (C) 2024 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergei Zharko [committer] */
4
9
10#include "CbmQaReportTable.h"
11
12#include "CbmQaTable.h"
13#include "Logger.h"
14
16
17#include <algorithm>
18#include <sstream>
19
20// ---------------------------------------------------------------------------------------------------------------------
21//
22Table::Table(std::string_view label, std::string_view title) : Table(label, 0, 0, title) {}
23
24// ---------------------------------------------------------------------------------------------------------------------
25//
26Table::Table(std::string_view label, int nRows, int nCols, std::string_view title)
27 : Element(std::string("tab:") + std::string(label), title)
28 , fNofRows(nRows)
29 , fNofCols(nCols)
30{
31 fvsTable.clear();
32 fvsTable.resize(fNofRows * fNofCols);
33 fvsTableHeader.clear();
35}
36
37// ---------------------------------------------------------------------------------------------------------------------
38//
39void Table::Set(const CbmQaTable* pQaTable, const std::string& cellFormat)
40{
41 if (!pQaTable) {
42 LOG(fatal) << "cbm::qa::report::Table::Set(): A nullptr QA-table is passed to the function. The table label is \""
43 << GetLabel() << '\"';
44 }
45
46 // Define table size
47 fvsTable.clear();
48 fvsTableHeader.clear();
49 int nRowsQa = pQaTable->GetNrows();
50 int nColsQa = pQaTable->GetNcols();
51 fsCaption = pQaTable->GetTitle();
52
53 bool bRowsHaveTitles = false;
54 for (int iRow = 0; iRow < nRowsQa; ++iRow) {
55 if (!std::string(pQaTable->GetXaxis()->GetBinLabel(nRowsQa - iRow)).empty()) {
56 bRowsHaveTitles = true;
57 }
58 }
59 fNofRows = nRowsQa;
60 fNofCols = nColsQa + static_cast<int>(bRowsHaveTitles); // Extra column comes from row names
61
62 // Parse row format
63 // NOTE: If the sequence of formats was passed, and it is shorter, than number of columns, then the last
64 // columns will be formated with default format, which is {:.3}.
65 bool bCommonRowFormat = cellFormat.find_first_of('|') == std::string::npos;
66 std::vector<std::string> vCellFormat(nColsQa, bCommonRowFormat ? cellFormat : std::string(kDefaultFormat));
67 if (!bCommonRowFormat) {
68 std::stringstream stream(cellFormat);
69 int iCol = 0;
70 while (iCol < nColsQa && std::getline(stream, vCellFormat[iCol], '|')) {
71 if (vCellFormat.empty()) {
72 vCellFormat[iCol] = "{}";
73 }
74 ++iCol;
75 }
76 }
77
78 // Test formatting:
79 for (auto& entry : vCellFormat) {
80 try {
81 std::string sTest = fmt::format(entry, 0.);
82 }
83 catch (const std::runtime_error& err) {
84 LOG(error) << "cbm::qa::report::Table(): Inacceptable format \"" << entry << "\" is passed, using default";
85 entry = kDefaultFormat;
86 }
87 }
88
89 // Filling the table
90 fvsTable.resize(fNofCols * fNofRows);
92 for (int iCol = static_cast<int>(bRowsHaveTitles); iCol < fNofCols; ++iCol) {
93 this->SetColumnTitle(iCol, std::string(pQaTable->GetXaxis()->GetBinLabel(iCol)));
94 }
95 for (int iRow = 0; iRow < fNofRows; ++iRow) {
96 // Print row title
97 if (bRowsHaveTitles) {
98 this->SetCell(iRow, 0, pQaTable->GetYaxis()->GetBinLabel(fNofRows - iRow));
99 }
100 for (int iColQa = 0; iColQa < nColsQa; ++iColQa) {
101 double entry = pQaTable->GetCell(iRow, iColQa);
102 int iCol = iColQa + static_cast<int>(bRowsHaveTitles);
103 this->SetCell(iRow, iCol, fmt::format(vCellFormat[iColQa], entry));
104 }
105 }
106}
Base class for the report table (header)
Definition of CbmQaTable class.
TODO: SZh, 30.01.2023: Override THistPainter::PaintText() to add zeroes in tables.
Definition CbmQaTable.h:24
Double_t GetCell(Int_t iRow, Int_t iCol) const
Gets cell content. Please mind, that the signature and result of this function is different to TH2D::...
Int_t GetNrows() const
Sets number of rows.
Definition CbmQaTable.h:51
Int_t GetNcols() const
Sets number of columns.
Definition CbmQaTable.h:54
Interface for the report element.
const std::string & GetLabel() const
Gets label.
Table element in the report.
Table(std::string_view label, std::string_view title="")
Constructor.
void SetCell(int iRow, int iCol, std::string_view cell)
Sets cell.
void Set(const CbmQaTable *pQaTable, const std::string &cellFormat=std::string(kDefaultFormat))
Sets a table from CbmQaTable.
std::vector< std::string > fvsTable
std::vector< std::string > fvsTableHeader
void SetColumnTitle(int iCol, std::string_view title)
Sets column title.
static constexpr std::string_view kDefaultFormat
Default format of double entries.
Hash for CbmL1LinkKey.