Back to home page

sPhenix code displayed by LXR

 
 

    


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     @brief A common header file for INTT QA
0014    */
0015   // common variables
0016   const int kFelix_num = 8;     //! the number of our FELIX server
0017   const int kFee_num = 14;      //! the number of half-ladders in a single FELIX server
0018   const int kChip_num = 26;     //! the number of chip in a half-ladder
0019   const int kChan_num = 128;    //! the number of channel in a single chip
0020   const int kFirst_pid = 3001;  //! the first pid (packet ID), which means intt0
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};  //! A list of nice colors
0028 
0029   // functions depending on variables above
0030 
0031   //! It returns int, which can be used as color in ROOT, for num-th graph/histograms. It checks if given parameter is in the range or not.
0032   template <class aaa>
0033   int GetColor(aaa num)
0034   {
0035     assert(0 <= num && num < 10);
0036     return InttQa::kColors[num];
0037   }
0038 
0039   //! Some configuration is done for a better-looking histogram
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   // functions depending on variables/functions above
0048 
0049   /*!
0050     @brief A wrapper function to draw a palette axis as you like
0051     @param xmin A relative coordinate of minimum x, which can be from 0 to 1.
0052     @param xmin A relative coordinate of minimum y, which can be from 0 to 1.
0053     @param xmax A relative coordinate of maximum x, which can be from 0 to 1.
0054     @param xmax A relative coordinate of maximum y, which can be from 0 to 1.
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     @brief A wrapper function to draw a statistical box as you like
0080     @param hist A pointer of a histogram object, which can be any of TH1, TH2, and TH3.
0081     @param xmin A relative coordinate of minimum x, which can be from 0 to 1.
0082     @param xmin A relative coordinate of minimum y, which can be from 0 to 1.
0083     @param xmax A relative coordinate of maximum x, which can be from 0 to 1.
0084     @param xmax A relative coordinate of maximum y, which can be from 0 to 1.
0085    */
0086   template <typename TH>
0087   void DrawStats(TH* hist, double xmin, double ymin, double xmax, double ymax, int /*font = 4*/)
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   //! i don't remember what's this
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)  // linear
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       @brief The range of histogram is optimized to show bins with non-zero entries
0157       @param hist A histogram to be modified
0158       @param axis_param Choice of axis to be modified. It can be x(0), y(1), or z(2).
0159       @retval pair < int, int > The minimum bin (.first) and maximum bin (.second)
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;                 // container for the minimum bin ID
0169     int bin_max = axis->GetNbins();  // container for the maximum bin ID
0170     int bin_max_sweep = bin_max;     // for for loop
0171 
0172     // sweep from low to up to find the minimum bin with non-zero content
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     // sweep from up to low to find the maximum bin with non-zero content
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 }  // end of namespace InttQa