Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:43:23

0001 #include "DQM/SiStripCommon/interface/UpdateTProfile.h"
0002 #include "TProfile.h"
0003 #include <iostream>
0004 #include <cmath>
0005 
0006 // -----------------------------------------------------------------------------
0007 /** */
0008 UpdateTProfile::UpdateTProfile() { ; }
0009 
0010 // -----------------------------------------------------------------------------
0011 /** */
0012 UpdateTProfile::~UpdateTProfile() { ; }
0013 
0014 // -----------------------------------------------------------------------------
0015 /** */
0016 void UpdateTProfile::setBinContents(TProfile* const prof,
0017                                     const uint32_t& bin,
0018                                     const double& num_of_entries,
0019                                     const double& sum_of_contents,
0020                                     const double& sum_of_squares) {
0021   //   std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
0022 
0023   double mean = 0.;
0024   double spread = 0.;
0025   if (num_of_entries) {
0026     mean = sum_of_contents / num_of_entries;
0027     spread = sqrt(sum_of_squares / num_of_entries - mean * mean);
0028   }
0029 
0030   //   LogTrace("TEST")
0031   //     << "[UpdateTProfile::setBinContents]"
0032   //     << " bin: " << bin
0033   //     << " entries: " << num_of_entries
0034   //     << " contents: " << sum_of_contents
0035   //     << " squared: " << sum_of_squares
0036   //     << " mean: " << mean
0037   //     << " spread: " << spread;
0038 
0039   UpdateTProfile::setBinContent(prof, bin, num_of_entries, mean, spread);
0040 }
0041 
0042 // -----------------------------------------------------------------------------
0043 /** 
0044     Error option "s" means that the error returned using the
0045     GetBinError() method is the spread for that bin. (If the default
0046     error option is used, the error returned is spread / sqrt(N),
0047     where N is the number of entries for that bin). The commissioning
0048     procedures use option "s" throughout.
0049 
0050     In order that the profile histo correctly displays the "bin_mean"
0051     and "bin_spread" (an example being: peds +/- strip noise), we
0052     have to manipulate the "contents" and "error" data as follows:
0053     
0054     TProfile::SetBinEntries( bin_number, bin_entries ) 
0055     TProfile::SetBinContents( bin_number, bin_mean * bin_entries ) 
0056     TProfile::SetBinError( bin_number, weight ) 
0057 
0058     "weight" is calculated based on the GetBinError() method, which
0059     returns an error ("err" below) based on the following equation:
0060     
0061     1) if error option is set to "s":
0062     err = sqrt( wei^2 / num - ( sum / num )^2 ) 
0063     => err = sqrt( wei^2 / num - sum^2 / num^2 ) 
0064     => wei = sqrt( err^2 * num + sum^2 / num )
0065     => wei = sqrt( err^2 * num + mean^2 * num )
0066     
0067     2) else if error option is set to "" (ie, default):
0068     err = ( 1 / sqrt( num ) ) * sqrt( wei^2 / num - ( sum / num )^2 ) 
0069     => err = sqrt( wei^2 / num^2 - sum^2 / num^3 ) 
0070     => wei = sqrt( err^2 * num^2 + cont^2 / num )
0071     => wei = sqrt( err^2 * num^2 + mean^2 * num )
0072 
0073     where:
0074     "num" is the bin entries, as set by the method SetBinEntries()
0075     "sum" is the bin content, as set by the method SetBinContent()
0076     "wei" is the bin error, as set by the method SetBinError()
0077     and "mean" = sum / num
0078 
0079 */
0080 void UpdateTProfile::setBinContent(
0081     TProfile* const prof, const uint32_t& bin, const double& num_of_entries, const double& mean, const double& spread) {
0082   //   std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
0083 
0084   // Check histo exists
0085   if (!prof) {
0086     std::cerr << "[UpdateTProfile::setBinContents]"
0087               << " NULL pointer to TProfile object!" << std::endl;
0088     return;
0089   }
0090 
0091   // Check bin number is valid
0092   if (bin == 0 || bin > static_cast<uint32_t>(prof->GetNbinsX())) {
0093     std::cerr << "[UpdateTProfile::setBinContents]"
0094               << " Unexpected bin number!" << std::endl;
0095     return;
0096   }
0097 
0098   // Check entries are present
0099   if (num_of_entries <= 0.) {
0100     return;
0101   }  //@@ what about negative weights???
0102   double entries = num_of_entries;
0103 
0104   // Check error option
0105   const char* spread_option = "s";
0106   const char* default_option = "";
0107   //   const char* option = prof->GetErrorOption();
0108   //   if ( option[0] != spread_option[0] ) {
0109   //     std::cout << "[UpdateTProfile::setBinContents]"
0110   //     << " Setting error option for TProfile to 's'!" << std::endl;
0111   //     prof->SetErrorOption( "s" );
0112   //   }
0113   const char* error_option = prof->GetErrorOption();
0114 
0115   // Calculate "weight" used for SetBinError() method
0116   double weight;
0117   if (error_option[0] == spread_option[0]) {
0118     weight = sqrt(mean * mean * entries + spread * spread * entries);
0119   } else if (error_option[0] == default_option[0]) {
0120     weight = sqrt(mean * mean * entries + spread * spread * entries * entries);
0121   } else {
0122     std::cerr << "[UpdateTProfile::setBinContents]"
0123               << " Unexpected error option for TProfile!" << std::endl;
0124     weight = 0.;
0125   }
0126 
0127   // Set bin entries, contents and error
0128   prof->SetBinEntries(bin, entries);
0129   prof->SetBinContent(bin, mean * entries);
0130   prof->SetBinError(bin, weight);
0131 
0132   //   LogTrace("TEST")
0133   //     << "[UpdateTProfile::" << __func__ << "]"
0134   //     << " bin/entries/mean/content/error: "
0135   //     << bin << "/"
0136   //     << entries << "/"
0137   //     << mean << "/"
0138   //     << mean*entries << "/"
0139   //     << weight;
0140 
0141   // Set total number of entries
0142   if (bin == 1) {
0143     prof->SetEntries(entries);
0144   } else {
0145     prof->SetEntries(prof->GetEntries() + entries);
0146   }
0147 }