CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/DQM/SiStripCommon/src/UpdateTProfile.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommon/interface/UpdateTProfile.h"
00002 #include "TProfile.h"
00003 #include <iostream>
00004 #include <math.h>
00005 
00006 
00007 // -----------------------------------------------------------------------------
00009 UpdateTProfile::UpdateTProfile() {;}
00010 
00011 // -----------------------------------------------------------------------------
00013 UpdateTProfile::~UpdateTProfile() {;}
00014 
00015 // -----------------------------------------------------------------------------
00017 void UpdateTProfile::setBinContents( TProfile* const prof,
00018                                      const uint32_t& bin, 
00019                                      const double& num_of_entries, 
00020                                      const double& sum_of_contents,
00021                                      const double& sum_of_squares ) {
00022   //   std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
00023   
00024   double mean = 0.;
00025   double spread = 0.;
00026   if ( num_of_entries ) { 
00027     mean = sum_of_contents / num_of_entries;
00028     spread = sqrt( sum_of_squares/ num_of_entries - mean * mean ); 
00029   }
00030    
00031 //   LogTrace("TEST")
00032 //     << "[UpdateTProfile::setBinContents]"
00033 //     << " bin: " << bin
00034 //     << " entries: " << num_of_entries
00035 //     << " contents: " << sum_of_contents
00036 //     << " squared: " << sum_of_squares
00037 //     << " mean: " << mean
00038 //     << " spread: " << spread;
00039   
00040   UpdateTProfile::setBinContent( prof, bin, num_of_entries, mean, spread );
00041   
00042 }
00043 
00044 // -----------------------------------------------------------------------------
00082 void UpdateTProfile::setBinContent( TProfile* const prof,
00083                                     const uint32_t& bin, 
00084                                     const double& num_of_entries, 
00085                                     const double& mean,
00086                                     const double& spread ) {
00087   //   std::cout << "[UpdateTProfile::setBinContents]" << std::endl;
00088 
00089   // Check histo exists
00090   if ( !prof ) {
00091     std::cerr << "[UpdateTProfile::setBinContents]"
00092          << " NULL pointer to TProfile object!" << std::endl;
00093     return;
00094   }
00095 
00096   // Check bin number is valid
00097   if ( bin == 0 || bin > static_cast<uint32_t>(prof->GetNbinsX()) ) {
00098     std::cerr << "[UpdateTProfile::setBinContents]"
00099          << " Unexpected bin number!" << std::endl;
00100     return;
00101   }
00102 
00103   // Check entries are present
00104   if ( num_of_entries <= 0. ) { return; } //@@ what about negative weights???
00105   double entries = num_of_entries;
00106   
00107   // Check error option
00108   const char* spread_option = "s";
00109   const char* default_option = "";
00110   //   const char* option = prof->GetErrorOption();
00111   //   if ( option[0] != spread_option[0] ) {
00112   //     std::cout << "[UpdateTProfile::setBinContents]"
00113   //     << " Setting error option for TProfile to 's'!" << std::endl;
00114   //     prof->SetErrorOption( "s" );
00115   //   }
00116   const char* error_option = prof->GetErrorOption();
00117 
00118   // Calculate "weight" used for SetBinError() method
00119   double weight;
00120   if ( error_option[0] == spread_option[0] ) {
00121     weight = sqrt( mean*mean*entries + spread*spread*entries  );
00122   } else if (error_option[0] == default_option[0] ) {
00123     weight = sqrt( mean*mean*entries + spread*spread*entries*entries );
00124   } else { 
00125     std::cerr << "[UpdateTProfile::setBinContents]"
00126          << " Unexpected error option for TProfile!" << std::endl;
00127     weight = 0.; 
00128   }
00129   
00130   // Set bin entries, contents and error
00131   prof->SetBinEntries( bin, entries );
00132   prof->SetBinContent( bin, mean * entries );
00133   prof->SetBinError( bin, weight );
00134  
00135 //   LogTrace("TEST")
00136 //     << "[UpdateTProfile::" << __func__ << "]"
00137 //     << " bin/entries/mean/content/error: " 
00138 //     << bin << "/"
00139 //     << entries << "/"
00140 //     << mean << "/"
00141 //     << mean*entries << "/"
00142 //     << weight;
00143 
00144   // Set total number of entries 
00145   if ( bin == 1 ) { prof->SetEntries( entries ); }
00146   else { prof->SetEntries( prof->GetEntries() + entries ); }
00147   
00148 }
00149 
00150