File indexing completed on 2025-08-06 08:18:42
0001 #include <Rtypes.h>
0002 #include <TAxis.h>
0003 #include <TPad.h>
0004 #include <TPaletteAxis.h>
0005 #include <TPaveStats.h>
0006
0007 #include <iostream>
0008 #include <utility>
0009
0010 namespace InttQa
0011 {
0012
0013
0014
0015
0016 const int kFelix_num = 8;
0017 const int kFee_num = 14;
0018 const int kChip_num = 26;
0019 const int kChan_num = 128;
0020 const int kFirst_pid = 3001;
0021 const int kBco_max = 128;
0022
0023 const int kColors[10] = {
0024 kBlack, kRed, kBlue,
0025 kGreen + 2, kMagenta + 1, kYellow + 1,
0026 kCyan + 1, kOrange + 1, kBlue + 9,
0027 kGray + 2};
0028
0029
0030
0031
0032 template <class aaa>
0033 int GetColor(aaa num)
0034 {
0035 assert(0 <= num && num < 10);
0036 return InttQa::kColors[num];
0037 }
0038
0039
0040 template <class TH>
0041 void HistConfig(TH* hist, int index = 0)
0042 {
0043 hist->SetLineColor(InttQa::GetColor(index));
0044 hist->SetFillColorAlpha(hist->GetLineColor(), 0.1);
0045 }
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 template <typename TH>
0057 TPaletteAxis* DrawPaletteAxis(TH* hist,
0058 double xmin, double ymin,
0059 double xmax, double ymax,
0060 double label_size = 0.04)
0061
0062 {
0063 gPad->Update();
0064 TPaletteAxis* pal = (TPaletteAxis*) hist->GetListOfFunctions()->FindObject("palette");
0065 pal->GetAxis()->SetLabelSize(label_size);
0066 pal->GetAxis()->CenterTitle();
0067
0068 pal->SetX1NDC(xmin);
0069 pal->SetX2NDC(xmax);
0070
0071 pal->SetY1NDC(ymin);
0072 pal->SetY2NDC(ymax);
0073 pal->Draw();
0074
0075 return pal;
0076 }
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 template <typename TH>
0087 void DrawStats(TH* hist, double xmin, double ymin, double xmax, double ymax, int )
0088 {
0089 gPad->Update();
0090 TPaveStats* st = (TPaveStats*) hist->FindObject("stats");
0091 if (st == nullptr)
0092 return;
0093
0094 st->SetTextColorAlpha(hist->GetLineColor(), 1.0);
0095 st->SetLineColorAlpha(hist->GetLineColor(), 1.0);
0096 st->SetFillStyle(1001);
0097 st->SetFillColor(0);
0098
0099 st->SetX1NDC(xmin);
0100 st->SetX2NDC(xmax);
0101 st->SetY1NDC(ymin);
0102 st->SetY2NDC(ymax);
0103
0104 st->Draw("same");
0105 }
0106
0107
0108 template <class TH>
0109 void HistsConfig(int hist_num, TH* hists)
0110 {
0111 std::vector<int> bin_x_with_entry;
0112 std::vector<int> bin_y_contents;
0113 for (int i = 0; i < hist_num; i++)
0114 {
0115 for (int j = 1; j < hists[i]->GetNbinsX() + 1; j++)
0116 {
0117 if (hists[i]->GetBinContent(j) != 0)
0118 {
0119 bin_x_with_entry.push_back(j);
0120 bin_y_contents.push_back(hists[i]->GetBinContent(j));
0121 }
0122 }
0123 }
0124
0125 int min_non_zero_x = *std::min_element(bin_x_with_entry.begin(), bin_x_with_entry.end());
0126 if (min_non_zero_x > 1)
0127 min_non_zero_x--;
0128
0129 int max_non_zero_x = *std::max_element(bin_x_with_entry.begin(), bin_x_with_entry.end());
0130 if (max_non_zero_x < hists[0]->GetNbinsX())
0131 max_non_zero_x++;
0132
0133 hists[0]->GetXaxis()->SetRange(min_non_zero_x, max_non_zero_x);
0134
0135 int min_y = *std::min_element(bin_y_contents.begin(), bin_y_contents.end());
0136 int max_y = *std::max_element(bin_y_contents.begin(), bin_y_contents.end());
0137 if (gPad->GetLogy() == 0)
0138 max_y *= 1.2;
0139 else
0140 max_y *= 2;
0141
0142 hists[0]->GetYaxis()->SetRangeUser(min_y, max_y);
0143 for (int i = 0; i < hist_num; i++)
0144 {
0145 InttQa::HistConfig(hists[i], i);
0146 }
0147
0148 std::cout << "X range: " << min_non_zero_x << "\t" << max_non_zero_x << std::endl;
0149 std::cout << "Y range: " << min_y << "\t" << max_y << std::endl;
0150 }
0151
0152 template <class T>
0153 std::pair<int, int> OptimizeRange(T* hist, int axis_param = 0)
0154 {
0155
0156
0157
0158
0159
0160
0161
0162 TAxis* axis = hist->GetXaxis();
0163 if (axis_param == 1)
0164 axis = hist->GetYaxis();
0165 else if (axis_param == 2)
0166 axis = hist->GetZaxis();
0167
0168 int bin_min = 0;
0169 int bin_max = axis->GetNbins();
0170 int bin_max_sweep = bin_max;
0171
0172
0173 for (int i = 1; i < bin_max_sweep; i++)
0174 {
0175 auto content = hist->GetBinContent(i);
0176 if (content > 0)
0177 {
0178 bin_min = i;
0179 break;
0180 }
0181 }
0182
0183
0184 for (int i = bin_max_sweep - 1; i > 0; i--)
0185 {
0186 auto content = hist->GetBinContent(i);
0187 if (content > 0)
0188 {
0189 bin_max = i;
0190 break;
0191 }
0192 }
0193
0194 axis->SetRange(bin_min, bin_max);
0195 std::pair<int, int> rtn(bin_min, bin_max);
0196 return rtn;
0197 }
0198 }