CbmRoot
Loading...
Searching...
No Matches
CbmQaTable.cxx
Go to the documentation of this file.
1/* Copyright (C) 2022 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
2 SPDX-License-Identifier: GPL-3.0-only
3 Authors: Sergey Gorbunov, Sergei Zharko [committer] */
4
12
13#include "CbmQaTable.h"
14
15#include "TAxis.h"
16
17#include <fstream>
18#include <iomanip>
19#include <iostream>
20#include <sstream>
21
22// TODO: Insert class info, try __PRETTY_FUNCTION__ and inline function for its modification (S.Zharko)
23
24//------------------------------------------------------------------------------------------------------------------------
25//
26CbmQaTable::CbmQaTable(const char* name, const char* title, Int_t nRows, Int_t nCols)
27 : TH2D(name, title, nCols, 0., static_cast<Double_t>(nCols), nRows, 0., static_cast<Double_t>(nRows))
28 , fNcols(nCols)
29 , fNrows(nRows)
30{
31 //
32 // Setup default style of the table
33 TH2D::SetStats(kFALSE);
34 TH2D::SetOption("textX+");
35 TH2D::GetXaxis()->SetTickLength(0.);
36 TH2D::GetYaxis()->SetTickLength(0.);
37 TH2D::GetXaxis()->SetLabelFont(kDefaultFontStyle);
38 TH2D::GetYaxis()->SetLabelFont(kDefaultFontStyle);
39
40 // Define basic names of rows and columns
41 for (Int_t iRow = 1; iRow <= fNrows; ++iRow) {
42 //TH2D::GetYaxis()->SetBinLabel(fNrows - iRow + 1, TString::Format("row %d", iRow).Data());
43 TH2D::GetYaxis()->SetBinLabel(fNrows - iRow + 1, "");
44 }
45 for (Int_t iCol = 1; iCol <= fNcols; ++iCol) {
46 TH2D::GetXaxis()->SetBinLabel(iCol, TString::Format("col %d", iCol).Data());
47 }
48}
49//
50//------------------------------------------------------------------------------------------------------------------------
51//
53//
54//------------------------------------------------------------------------------------------------------------------------
55//
56Double_t CbmQaTable::GetCell(Int_t iRow, Int_t iCol) const { return TH2D::GetBinContent(iCol + 1, fNrows - iRow); }
57//
58//------------------------------------------------------------------------------------------------------------------------
59//
60Double_t CbmQaTable::GetCellError(Int_t iRow, Int_t iCol) const { return TH2D::GetBinError(iCol + 1, fNrows - iRow); }
61//
62//------------------------------------------------------------------------------------------------------------------------
63//
64void CbmQaTable::SetCell(Int_t iRow, Int_t iCol, Double_t content, Double_t error)
65{
66 TH2D::SetBinContent(iCol + 1, fNrows - iRow, content);
67 TH2D::SetBinError(iCol + 1, fNrows - iRow, error);
68}
69//
70//------------------------------------------------------------------------------------------------------------
71//
72void CbmQaTable::SetNamesOfCols(const std::vector<std::string>& names)
73{
74 Int_t nEntries = (fNcols > static_cast<Int_t>(names.size())) ? static_cast<Int_t>(names.size()) : fNcols;
75 // TODO: Possibly, we need a restriction on the names size (S.Zharko)
76 for (Int_t iCol = 1; iCol <= nEntries; ++iCol) {
77 TH2D::GetXaxis()->SetBinLabel(iCol, names[iCol - 1].c_str());
78 }
79}
80//
81//------------------------------------------------------------------------------------------------------------------------
82//
83void CbmQaTable::SetNamesOfRows(const std::vector<std::string>& names)
84{
85 Int_t nEntries = (fNrows > static_cast<Int_t>(names.size())) ? static_cast<Int_t>(names.size()) : fNrows;
86 // TODO: Possibly, we need a restriction on the names size (S.Zharko)
87 for (Int_t iRow = 1; iRow <= nEntries; ++iRow) {
88 TH2D::GetYaxis()->SetBinLabel(fNrows - iRow + 1, names[iRow - 1].c_str());
89 }
90}
91//
92//------------------------------------------------------------------------------------------------------------------------
93//
94void CbmQaTable::SetRowName(Int_t iRow, const char* name) { TH2D::GetYaxis()->SetBinLabel(fNrows - iRow, name); }
95//
96//------------------------------------------------------------------------------------------------------------------------
97//
98std::string CbmQaTable::ToString(int prec, bool drawErr) const
99{
100 std::stringstream aStream;
101 aStream.setf(std::ios::fixed);
102 aStream.setf(std::ios::showpoint);
103 aStream.setf(std::ios::left);
104 aStream.precision(prec);
105
106 aStream << "Table: " << GetTitle() << '\n';
107 aStream << std::setw(fColWidth + 10) << std::setfill(' ') << ' ' << ' ';
108
109 for (Int_t iCol = 0; iCol < fNcols; ++iCol) {
110 std::string entry = std::string(this->GetXaxis()->GetBinLabel(iCol + 1));
111 if (Int_t(entry.size()) + 3 > fColWidth) {
112 entry = entry.substr(0, fColWidth - 3) + "...";
113 }
114 aStream << std::setw(fColWidth) << std::setfill(' ') << entry << ' ';
115 }
116 aStream << '\n';
117
118 for (Int_t iRow = 0; iRow < fNrows; ++iRow) {
119 // Print row title
120 std::string entry = std::string(GetYaxis()->GetBinLabel(fNrows - iRow));
121 if (static_cast<Int_t>(entry.size()) > fColWidth) {
122 entry = entry.substr(0, fColWidth + 7) + "...";
123 }
124 aStream << std::setw(fColWidth + 10) << std::setfill(' ') << entry << ' ';
125
126 for (Int_t iCol = 0; iCol < fNcols; ++iCol) {
127 std::stringstream cell;
128 cell.setf(std::ios::fixed);
129 cell.setf(std::ios::showpoint);
130 cell.setf(std::ios::left);
131 cell.precision(prec);
132 cell << GetCell(iRow, iCol);
133 if (drawErr) {
134 cell << "+/-" << GetCellError(iRow, iCol);
135 }
136 aStream << std::setw(fColWidth) << std::setfill(' ') << cell.str() << ' ';
137 }
138 aStream << '\n';
139 }
140 return aStream.str();
141}
142//
143//------------------------------------------------------------------------------------------------------------------------
144//
145void CbmQaTable::ToTextFile(const std::string& fileName, std::ios_base::openmode mode) const
146{
147 std::ofstream fileOut(fileName, mode);
148 fileOut << (*this);
149 fileOut.close();
150}
151//
152//------------------------------------------------------------------------------------------------------------------------
153//
155{
156 TH2D::SetMarkerSize(size / kDefaultTextSize);
157 TH2D::GetXaxis()->SetLabelSize(size);
158 TH2D::GetYaxis()->SetLabelSize(size);
159}
160
161//
162//------------------------------------------------------------------------------------------------------------------------
163//
164std::ostream& operator<<(std::ostream& out, const CbmQaTable& aTable)
165{
166 out << aTable.ToString(3);
167 return out;
168}
169//
170//------------------------------------------------------------------------------------------------------------------------
171//
std::ostream & operator<<(std::ostream &out, const CbmQaTable &aTable)
Definition of CbmQaTable class.
static constexpr size_t size()
Definition KfSimdPseudo.h:2
TODO: SZh, 30.01.2023: Override THistPainter::PaintText() to add zeroes in tables.
Definition CbmQaTable.h:24
void ToTextFile(const std::string &fileName, std::ios_base::openmode mode=std::ios_base::out) const
static constexpr Float_t kDefaultTextSize
default size of text
Definition CbmQaTable.h:95
Int_t fNcols
number of columns in a table
Definition CbmQaTable.h:89
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::...
void SetNamesOfCols(const std::vector< std::string > &names)
Sets the names of table columns.
Double_t GetCellError(Int_t iRow, Int_t iCol) const
Gets cell error. Please mind, that the signature and result of this function is different to TH2D::Ge...
void SetNamesOfRows(const std::vector< std::string > &names)
Sets the names of table rows.
virtual ~CbmQaTable()
Destructor.
static constexpr Style_t kDefaultFontStyle
default text style
Definition CbmQaTable.h:96
void SetCell(Int_t iRow, Int_t iCol, Double_t content, Double_t error=0.)
CbmQaTable()
Default constructor.
Definition CbmQaTable.h:27
Int_t fColWidth
Width of column in number of symbols.
Definition CbmQaTable.h:92
Int_t fNrows
number of rows in a table
Definition CbmQaTable.h:90
void SetRowName(Int_t iRow, const char *name)
Set name of a row.
void SetTextSize(Float_t size=0.03)
Sets size of the text.
std::string ToString(int prec, bool useErr=false) const