Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:13:55

0001 #pragma once
0002 
0003 #include <utility>
0004 using namespace std;
0005 namespace InttQa
0006 {
0007   /*!
0008     @brief A common header file for INTT QA
0009    */
0010   // common variables
0011   const int kFelix_num = 8; //! the number of our FELIX server
0012   const int kFee_num   = 14;  //! the number of half-ladders in a single FELIX server
0013   const int kChip_num  = 26; //! the number of chip in a half-ladder
0014   const int kChan_num  = 128; //! the number of channel in a single chip
0015   const int kFirst_pid = 3001; //! the first pid (packet ID), which means intt0
0016   const int kBco_max   = 128;
0017 
0018   const int kColors[10] = {
0019     kBlack,    kRed,       kBlue, 
0020     kGreen+2,  kMagenta+1, kYellow+1, 
0021     kCyan+1,   kOrange+1,  kBlue+9, 
0022     kGray + 2
0023   }; //! A list of nice colors
0024 
0025   // functions depending on variables above
0026 
0027   //! 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.
0028   template < class aaa >
0029   int GetColor( aaa num )
0030   {
0031     assert( 0 <= num && num < 10 );
0032     return InttQa::kColors[ num ];
0033   }
0034 
0035   //! Some configuration is done for a better-looking histogram
0036   template < class TH >
0037   void HistConfig( TH* hist, int index=0 )
0038   {
0039     hist->SetLineColor( InttQa::GetColor(index) );
0040     hist->SetFillColorAlpha( hist->GetLineColor(), 0.1 );
0041   }
0042 
0043   // functions depending on variables/functions above
0044 
0045   /*!
0046     @brief A wrapper function to draw a palette axis as you like
0047     @param xmin A relative coordinate of minimum x, which can be from 0 to 1.
0048     @param xmin A relative coordinate of minimum y, which can be from 0 to 1.
0049     @param xmax A relative coordinate of maximum x, which can be from 0 to 1.
0050     @param xmax A relative coordinate of maximum y, which can be from 0 to 1.
0051    */
0052   template < typename TH >
0053   TPaletteAxis* DrawPaletteAxis( TH* hist,
0054                  double xmin, double ymin,
0055                  double xmax, double ymax,
0056                  double label_size = 0.04 )
0057     
0058   {
0059    
0060     gPad->Update();
0061     TPaletteAxis *pal = (TPaletteAxis*)hist->GetListOfFunctions()->FindObject("palette");
0062     pal->GetAxis()->SetLabelSize( label_size );
0063     pal->GetAxis()->CenterTitle();
0064     
0065     pal->SetX1NDC( xmin );
0066     pal->SetX2NDC( xmax );
0067     
0068     pal->SetY1NDC( ymin );
0069     pal->SetY2NDC( ymax );
0070     pal->Draw();
0071     
0072     return pal;
0073   }
0074   
0075   /*!
0076     @brief A wrapper function to draw a statistical box as you like
0077     @param hist A pointer of a histogram object, which can be any of TH1, TH2, and TH3.
0078     @param xmin A relative coordinate of minimum x, which can be from 0 to 1.
0079     @param xmin A relative coordinate of minimum y, which can be from 0 to 1.
0080     @param xmax A relative coordinate of maximum x, which can be from 0 to 1.
0081     @param xmax A relative coordinate of maximum y, which can be from 0 to 1.
0082    */
0083   template < typename TH >
0084   void DrawStats( TH* hist, double xmin, double ymin, double xmax, double ymax, int font = 4)
0085   {
0086     
0087     gPad->Update();
0088     TPaveStats *st = (TPaveStats*)hist->FindObject("stats");
0089     if( st == nullptr )
0090       return;
0091     
0092     st->SetTextColorAlpha( hist->GetLineColor(), 1.0 );
0093     st->SetLineColorAlpha( hist->GetLineColor(), 1.0 );
0094     st->SetFillStyle( 1001 );
0095     st->SetFillColor( 0 );
0096     
0097     st->SetX1NDC( xmin );
0098     st->SetX2NDC( xmax );
0099     st->SetY1NDC( ymin );
0100     st->SetY2NDC( ymax );
0101     
0102     st->Draw("same");
0103   }
0104 
0105   //! i don't remember what's this
0106   template < class TH >
0107   void HistsConfig( int hist_num, TH* hists )
0108   {
0109 
0110     std::vector < int > bin_x_with_entry;
0111     std::vector < int > bin_y_contents;
0112     for( int i=0; i<hist_num; i++ )
0113       {
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         //cout << hists[i]->GetBinContent( j ) << endl;
0122           }
0123       }
0124       }
0125 
0126     int min_non_zero_x = *std::min_element( bin_x_with_entry.begin(), bin_x_with_entry.end() );
0127     if( min_non_zero_x > 1 )
0128       min_non_zero_x--;
0129     
0130     int max_non_zero_x = *std::max_element( bin_x_with_entry.begin(), bin_x_with_entry.end() );
0131     // if no entry is found in the hostograms, set a dummy value
0132     if( max_non_zero_x < hists[0]->GetNbinsX() )
0133       max_non_zero_x++;
0134     // else if( max_non_zero_x >= hists[0]->GetNbinsX() ) // if the bin index is larger than the max bin index, force it to be the max bin index. It happen somehow...
0135     //   max_non_zero_x = hists[0]->GetNbinsX();
0136 
0137     // if( max_non_zero_x > 1e6 )
0138     //   max_non_zero_x = 8e5;
0139 
0140     // std::cout << "Comment: "
0141     //        << hists[0]->GetName() << "\t"
0142     //        << max_non_zero_x << "\t"
0143     //        << hists[0]->GetNbinsX()
0144     //        << std::endl;
0145     
0146     hists[0]->GetXaxis()->SetRange( min_non_zero_x, max_non_zero_x );
0147 
0148     int min_y = *std::min_element( bin_y_contents.begin(), bin_y_contents.end() );
0149     int max_y = *std::max_element( bin_y_contents.begin(), bin_y_contents.end() );
0150     if( gPad->GetLogy() == 0 ) // linear
0151       max_y *= 1.2;
0152     else
0153       max_y *= 2;
0154       
0155     hists[0]->GetYaxis()->SetRangeUser( min_y, max_y );
0156     for( int i=0; i<hist_num; i++ )
0157       {
0158     InttQa::HistConfig( hists[i], i );
0159       }
0160     
0161     std::cout << "X range: " << min_non_zero_x << "\t" << max_non_zero_x << std::endl;
0162     std::cout << "Y range: " << min_y << "\t" << max_y << std::endl;
0163   }
0164 
0165   template < class T >
0166   std::pair < int, int > OptimizeRange( T* hist, int axis_param=0 )
0167   {
0168     /*!
0169       @brief The range of histogram is optimized to show bins with non-zero entries
0170       @param hist A histogram to be modified
0171       @param axis_param Choice of axis to be modified. It can be x(0), y(1), or z(2).
0172       @retval pair < int, int > The minimum bin (.first) and maximum bin (.second)
0173      */
0174 
0175     TAxis* axis = hist->GetXaxis();
0176     if( axis_param == 1 )
0177       axis = hist->GetYaxis();
0178     else if( axis_param == 2 )
0179       axis = hist->GetZaxis();
0180     
0181     int bin_min = 0; // container for the minimum bin ID
0182     int bin_max = axis->GetNbins(); // container for the maximum bin ID
0183     int bin_max_sweep = bin_max; // for for loop
0184 
0185     // sweep from low to up to find the minimum bin with non-zero content
0186     for( int i=1; i<bin_max_sweep; i++ )
0187       {
0188 
0189     auto content = hist->GetBinContent( i );
0190     if( content > 0 )
0191       {
0192         bin_min = i;
0193         break;
0194       }
0195       }
0196 
0197     // sweep from up to low to find the maximum bin with non-zero content
0198     for( int i=bin_max_sweep-1; i>0; i-- )
0199       {
0200     auto content = hist->GetBinContent( i );
0201     if( content > 0 )
0202       {
0203         bin_max = i;
0204         std::cout << std::setw(10) << i << "\t" << content << std::endl;
0205         break;
0206       }
0207       }
0208 
0209     if( bin_max == axis->GetNbins() )
0210       bin_max = 1;
0211     
0212     std::cout << "detemination... " << bin_max << std::endl;
0213     axis->SetRange( bin_min, bin_max );
0214     std::pair < int, int >rtn( bin_min, bin_max );
0215     return rtn;
0216   }
0217 } // end of namespace InttQa