CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_10_patch1/src/DQM/SiStripCommissioningSummary/src/SummaryGenerator.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripCommissioningSummary/interface/SummaryGenerator.h"
00002 #include "DQM/SiStripCommissioningSummary/interface/SummaryGeneratorControlView.h"
00003 #include "DQM/SiStripCommissioningSummary/interface/SummaryGeneratorReadoutView.h"
00004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include <iostream>
00007 #include <sstream>
00008 #include <math.h>
00009 #include "TH2F.h"
00010 #include "TProfile.h"
00011 
00012 using namespace sistrip;
00013 
00014 // -----------------------------------------------------------------------------
00015 // 
00016 SummaryGenerator::SummaryGenerator( std::string name ) : 
00017   map_(),
00018   entries_(-1.),
00019   max_(-1.*sistrip::invalid_),
00020   min_(1.*sistrip::invalid_),
00021   label_(""),
00022   myName_(name)
00023 {
00024   // TH1::SetDefaultSumw2(true); // use square of weights to calc error   
00025 }
00026 
00027 // -----------------------------------------------------------------------------
00028 // 
00029 SummaryGenerator* SummaryGenerator::instance( const sistrip::View& view ) {
00030 
00031   SummaryGenerator* generator = 0;
00032   if ( view == sistrip::CONTROL_VIEW ) {
00033     generator = new SummaryGeneratorControlView();
00034   } else if ( view == sistrip::READOUT_VIEW ) {
00035     generator = new SummaryGeneratorReadoutView();
00036   } else { generator = 0; }
00037   
00038   if ( generator ) {
00039     LogTrace(mlSummaryPlots_) 
00040       << "[SummaryGenerator::" << __func__ << "]"
00041       << " Built \"" << generator->myName() << "\" object!";
00042   } else {
00043     edm::LogWarning(mlSummaryPlots_) 
00044       << "[SummaryGenerator::" << __func__ << "]"
00045       << " Unexpected view: \"" 
00046       << SiStripEnumsAndStrings::view( view )
00047       << "\" Unable to build Generator!"
00048       << " Returning NULL pointer!";
00049   }
00050   
00051   return generator;
00052 
00053 }
00054 
00055 // -----------------------------------------------------------------------------
00056 // 
00057 std::string SummaryGenerator::name( const sistrip::RunType& run_type, 
00058                                     const sistrip::Monitorable& mon, 
00059                                     const sistrip::Presentation& pres,
00060                                     const sistrip::View& view, 
00061                                     const std::string& directory ) {
00062   std::stringstream ss;
00063   ss << sistrip::summaryHisto_ << sistrip::sep_; 
00064   ss << SiStripEnumsAndStrings::presentation( pres ) << sistrip::sep_; 
00065   ss << SiStripEnumsAndStrings::runType( run_type ) << sistrip::sep_;
00066   ss << SiStripEnumsAndStrings::view( view ) << sistrip::sep_;
00067   ss << SiStripEnumsAndStrings::monitorable( mon );
00068   
00069   //LogTrace(mlSummaryPlots_) 
00070   //<< "[SummaryGenerator::" << __func__ << "]"
00071   //<< " Histogram name: \"" << ss.str() << "\"";
00072   
00073   return ss.str();
00074 }
00075 
00076 // -----------------------------------------------------------------------------
00077 // 
00078 /*
00079   fix nbins for 1D distribution? to 1024? then change within summary
00080   methods with SetBins() methods? but must limit nbins to < 1024!!!
00081 
00082 */
00083 TH1* SummaryGenerator::histogram( const sistrip::Presentation& pres,
00084                                   const uint32_t& xbins ) {
00085   if ( !xbins ) { return 0; }
00086 
00087   TH1* summary = 0;
00088   if ( pres == sistrip::HISTO_1D ) { 
00089     summary = new TH1F( "", "", 1024, 0., static_cast<float>(1024) ); 
00090   } else if ( pres == sistrip::HISTO_2D_SUM ) { 
00091     summary = new TH1F( "", "", xbins, 0., static_cast<float>(xbins) ); 
00092   } else if ( pres == sistrip::HISTO_2D_SCATTER ) { 
00093     summary = new TH2F( "", "", 100*xbins, 0., static_cast<float>(100*xbins), 1025, 0., 1025. ); 
00094   } else if ( pres == sistrip::PROFILE_1D ) { 
00095     summary = new TProfile( "", "", xbins, 0., static_cast<float>(xbins), 0., 1025. ); 
00096   } else { summary = 0; }
00097   
00098   if ( summary ) {
00099     LogTrace(mlSummaryPlots_) 
00100       << "[SummaryGenerator::" << __func__ << "]"
00101       << " Histogram name: \"" << summary->GetName() << "\"";
00102   } else { 
00103     edm::LogVerbatim(mlSummaryPlots_) 
00104       << "[SummaryGenerator::" << __func__ << "]"
00105       << " Unexpected presentation: \"" 
00106       << SiStripEnumsAndStrings::presentation( pres )
00107       << "\" Unable to build summary plot!"
00108       << " Returning NULL pointer!";
00109   }
00110   
00111   return summary;
00112   
00113 }
00114 
00115 // -----------------------------------------------------------------------------
00116 // 
00117 void SummaryGenerator::format( const sistrip::RunType& run_type, 
00118                                const sistrip::Monitorable& mon, 
00119                                const sistrip::Presentation& pres,
00120                                const sistrip::View& view, 
00121                                const std::string& directory,
00122                                const sistrip::Granularity& gran,
00123                                TH1& summary_histo ) {
00124   
00125   // Set name, title and entries
00126   //std::stringstream ss;
00127   //std::string name = SummaryGenerator::name( run_type, mon, pres, view, directory );
00128   //summary_histo.SetName( name.c_str() );
00129   //summary_histo.SetTitle( name.c_str() );
00130   if ( entries_ >= 0. ) { summary_histo.SetEntries( entries_ ); }
00131   
00132   // X axis
00133   summary_histo.GetXaxis()->SetLabelSize(0.03);
00134   summary_histo.GetXaxis()->SetTitleSize(0.03);
00135   summary_histo.GetXaxis()->SetTitleOffset(3.5); 
00136   //gPad->SetBottomMargin(0.2);
00137   
00138   // Y axis
00139   summary_histo.GetYaxis()->SetLabelSize(0.03);
00140   summary_histo.GetYaxis()->SetTitleSize(0.03);
00141   summary_histo.GetYaxis()->SetTitleOffset(1.5);
00142   //gPad->SetLeftMargin(0.2);
00143   
00144   // Axis label
00145   if ( pres == sistrip::HISTO_1D ) {
00146     std::string xtitle = label_ + " (for " + directory + ")";
00147     summary_histo.GetXaxis()->SetTitle( xtitle.c_str() );
00148     summary_histo.GetYaxis()->SetTitle( "Frequency" );
00149     summary_histo.GetXaxis()->SetTitleOffset(1.5); //@@ override value set above
00150   } else {
00151     std::string xtitle = SiStripEnumsAndStrings::granularity( gran ) + " within " + directory;
00152     summary_histo.GetXaxis()->SetTitle( xtitle.c_str() );
00153     summary_histo.GetYaxis()->SetTitle( label_.c_str() );
00154     //summary_histo.GetXaxis()->SetTitleOffset(1.5); //@@ override value set above (3.5?)
00155   }    
00156   
00157   // Formatting for 2D plots
00158   if ( pres == sistrip::HISTO_2D_SCATTER ) { 
00159     // Markers (open circles)
00160     summary_histo.SetMarkerStyle(2); 
00161     summary_histo.SetMarkerSize(0.6);
00162   }
00163 
00164   // Semi-generic formatting
00165   if ( pres == sistrip::HISTO_2D_SUM || 
00166        pres == sistrip::HISTO_2D_SCATTER ||
00167        pres == sistrip::PROFILE_1D ) {
00168     /*
00169     //put solid and dotted lines on summary to separate top- and
00170     //2nd-from-top- level bin groups.
00171 
00172     uint16_t topLevel = 0, topLevelOld = 0, secondLevel = 0, secondLevelOld = 0;
00173     std::string::size_type pos = 0;
00174     for ( HistoData::iterator ibin = map_.begin(); ibin != map_.end(); ibin++) {
00175 
00176       //draw line if top and second level numbers change.
00177       pos = ibin->first.find(sistrip::dot_,0);
00178       if (pos != std::string::npos) {
00179         if ((topLevel=atoi(std::string(ibin->first,0,pos).c_str())) != topLevelOld) {
00180           topLevel = topLevelOld;
00181           //
00182         }
00183         else if (ibin->first.find(sistrip::dot_,pos+1) != std::string::npos) {
00184             if ((secondLevelOld=atoi(std::string(ibin->first,pos+1,(ibin->first.find(sistrip::dot_,pos+1)- (pos+1))).c_str())) != secondLevel) {
00185               secondLevel = secondLevelOld;
00186               //
00187             }}}
00188     }
00189     */
00190   }
00191 }
00192 
00193 // -----------------------------------------------------------------------------
00194 // 
00195 void SummaryGenerator::clearMap() {
00196   HistoData::iterator iter = map_.begin(); 
00197   for ( ; iter != map_.end(); iter++ ) { iter->second.clear(); }
00198   map_.clear();
00199   entries_ = -1.;
00200   max_ = -1.*sistrip::invalid_;
00201   min_ =  1.*sistrip::invalid_;
00202 }
00203 
00204 // -----------------------------------------------------------------------------
00205 // 
00206 void SummaryGenerator::printMap() {
00207 
00208   std::stringstream ss;
00209   ss << "[SummaryGenerator::" << __func__ << "]"
00210      << " Printing contents of map: " << std::endl;
00211   
00212   HistoData::iterator iter = map_.begin(); 
00213   for ( ; iter != map_.end(); iter++ ) { 
00214     ss << " bin/entries: " << iter->first << "/" << iter->second.size() << " ";
00215     if ( !iter->second.empty() ) {
00216       ss << " value/error: ";
00217       std::vector<Data>::const_iterator jter = iter->second.begin();
00218       for ( ; jter != iter->second.end(); jter++ ) { 
00219         ss << jter->first << "/" << jter->second << " ";
00220       }
00221     }
00222     ss << std::endl;
00223   }
00224   
00225   ss << " Max value: " << max_ << std::endl
00226      << " Min value: " << min_ << std::endl;
00227 
00228   LogTrace(mlSummaryPlots_) << ss.str();
00229   
00230 }
00231 
00232 // -----------------------------------------------------------------------------
00233 // 
00234 void SummaryGenerator::fillMap( const std::string& top_level_dir,
00235                                 const sistrip::Granularity& gran,
00236                                 const uint32_t& device_key, 
00237                                 const float& value,
00238                                 const float& error ) {
00239   
00240   // Check if value is valid
00241   if ( value > 1. * sistrip::valid_ ) { return; }
00242   
00243   // Calculate maximum and minimum values in std::map
00244   if ( value > max_ ) { max_ = value; }
00245   if ( value < min_ ) { min_ = value; }
00246   
00247   // Check if error is valid
00248   if ( error < 1. * sistrip::valid_ ) { 
00249     fill( top_level_dir, gran, device_key, value, error );
00250   } else { 
00251     fill( top_level_dir, gran, device_key, value, 0. ); 
00252   } 
00253   
00254 }
00255 
00256 // -----------------------------------------------------------------------------
00257 // 
00258 void SummaryGenerator::fill( const std::string& top_level_dir,
00259                              const sistrip::Granularity& gran,
00260                              const uint32_t& device_key, 
00261                              const float& value,
00262                              const float& error ) {
00263   
00264   LogTrace(mlSummaryPlots_)
00265     << "[SummaryGenerator::" << __func__ << "]"
00266     << " Derived implementation does not exist!...";
00267 }
00268 
00269 //------------------------------------------------------------------------------
00270 //
00271 void SummaryGenerator::histo1D( TH1& his ) {
00272   
00273   // Check number of entries in map
00274   if ( map_.empty() ) { 
00275     edm::LogWarning(mlSummaryPlots_) 
00276       << "[SummaryGenerator::" << __func__ << "]" 
00277       << " No contents in std::map to histogram!";
00278     return; 
00279   }
00280   
00281   // Retrieve histogram  
00282   TH1F* histo = dynamic_cast<TH1F*>(&his);
00283   if ( !histo ) { 
00284     edm::LogWarning(mlSummaryPlots_)
00285       << "[SummaryGenerator::" << __func__ << "]"
00286       << " NULL pointer to TH1F histogram!";
00287     return;
00288   }
00289   
00290   // Calculate bin range
00291   int32_t high  = static_cast<int32_t>( fabs(max_) > 20. ? max_ + 0.05 * fabs(max_) : max_ + 1. );
00292   int32_t low   = static_cast<int32_t>( fabs(min_) > 20. ? min_ - 0.05 * fabs(min_) : min_ - 1. );
00293   int32_t range = high - low;
00294 
00295   // increase number of bins for floats
00296 //   if ( max_ - static_cast<int32_t>(max_) > 1.e-6 && 
00297 //        min_ - static_cast<int32_t>(min_) > 1.e-6 ) {
00298 //     range = 100 * range; 
00299 //   }
00300   
00301   // Set histogram binning
00302   histo->SetBins( range, static_cast<float>(low), static_cast<float>(high) );
00303   
00304   // Iterate through std::map, set bin labels and fill histogram
00305   entries_ = 0.;
00306   HistoData::const_iterator ibin = map_.begin();
00307   for ( ; ibin != map_.end(); ibin++ ) {
00308     if ( ibin->second.empty() ) { continue; }
00309     BinData::const_iterator ii = ibin->second.begin();
00310     for ( ; ii != ibin->second.end(); ii++ ) { 
00311       // bin (value) and weight (error)
00312       histo->Fill( ii->first ); //, ii->second ); 
00313       entries_++;
00314     }
00315   }
00316   
00317 }
00318 
00319 //------------------------------------------------------------------------------
00320 //
00321 void SummaryGenerator::histo2DSum( TH1& his ) {
00322   
00323   // Check number of entries in map
00324   if ( map_.empty() ) { 
00325     edm::LogWarning(mlSummaryPlots_)
00326       << "[SummaryGenerator::" << __func__ << "]" 
00327       << " No contents in std::map to histogram!";
00328     return; 
00329   }
00330   
00331   // Retrieve histogram  
00332   TH1F* histo = dynamic_cast<TH1F*>(&his);
00333   if ( !histo ) { 
00334     edm::LogWarning(mlSummaryPlots_)
00335       << "[SummaryGenerator::" << __func__ << "]"
00336       << " NULL pointer to TH1F histogram!";
00337     return;
00338   }
00339   
00340   // Iterate through map, set bin labels and fill histogram
00341   entries_ = 0.;
00342   uint16_t bin = 0;
00343   HistoData::const_iterator ibin = map_.begin();
00344   for ( ; ibin != map_.end(); ibin++ ) {
00345     bin++;
00346     histo->GetXaxis()->SetBinLabel( static_cast<Int_t>(bin), ibin->first.c_str() );
00347     if ( ibin->second.empty() ) { continue; }
00348     BinData::const_iterator ii = ibin->second.begin();
00349     for ( ; ii != ibin->second.end(); ii++ ) { 
00350       // x (bin), y (value) and weight (error)
00351       histo->Fill( static_cast<Double_t>(bin-0.5), 
00352                    static_cast<Double_t>(ii->first) ); //, ii->second ); 
00353       entries_ += 1. * ii->first;
00354     }
00355   }
00356   
00357 }
00358 
00359 //------------------------------------------------------------------------------
00360 //
00361 void SummaryGenerator::histo2DScatter( TH1& his ) {
00362 
00363   // Check number of entries in map
00364   if ( map_.empty() ) { 
00365     edm::LogWarning(mlSummaryPlots_)
00366       << "[SummaryGenerator::" << __func__ << "]" 
00367       << " No contents in std::map to histogram!";
00368     return; 
00369   }
00370   
00371   // Retrieve histogram  
00372   TH2F* histo = dynamic_cast<TH2F*>(&his);
00373   if ( !histo ) { 
00374     edm::LogWarning(mlSummaryPlots_)
00375       << "[SummaryGenerator::" << __func__ << "]"
00376       << " NULL pointer to TH2F histogram!";
00377     return;
00378   }
00379   
00380   // Iterate through std::map, set bin labels and fill histogram
00381   entries_ = 0.;
00382   uint16_t bin = 0;
00383   HistoData::const_iterator ibin = map_.begin();
00384   for ( ; ibin != map_.end(); ibin++ ) {
00385     bin++;
00386     histo->GetXaxis()->SetBinLabel( static_cast<Int_t>(bin), ibin->first.c_str() );
00387     if ( ibin->second.empty() ) { continue; }
00388     BinData::const_iterator ii = ibin->second.begin();
00389     for ( ; ii != ibin->second.end(); ii++ ) { 
00390       // x (bin), y (value) and weight (error)
00391       histo->Fill( static_cast<Double_t>(bin-0.5), 
00392                    static_cast<Double_t>(ii->first) ); // , ii->second ); 
00393       entries_++;
00394     }
00395   }
00396   
00397 }
00398 
00399 //------------------------------------------------------------------------------
00400 //
00401 void SummaryGenerator::profile1D( TH1& his ) {
00402   
00403   // Check number of entries in map
00404   if ( map_.empty() ) { 
00405     edm::LogWarning(mlSummaryPlots_)
00406       << "[SummaryGenerator::" << __func__ << "]" 
00407       << " No contents in std::map to histogram!";
00408     return; 
00409   }
00410   
00411   // Retrieve histogram  
00412   TProfile* histo = dynamic_cast<TProfile*>(&his);
00413   if ( !histo ) { 
00414     edm::LogWarning(mlSummaryPlots_) 
00415       << "[SummaryGenerator::" << __func__ << "]"
00416       << " NULL pointer to TProfile histogram!";
00417     return;
00418   }
00419   
00420   // Iterate through std::map, set bin labels and fill histogram
00421   entries_ = 0.;
00422   uint16_t bin = 0;
00423   HistoData::const_iterator ibin = map_.begin();
00424   for ( ; ibin != map_.end(); ibin++ ) {
00425     bin++;
00426     histo->GetXaxis()->SetBinLabel( static_cast<Int_t>(bin), ibin->first.c_str() );
00427     if ( ibin->second.empty() ) { continue; }
00428     BinData::const_iterator ii = ibin->second.begin();
00429     for ( ; ii != ibin->second.end(); ii++ ) { 
00430       // x (bin), y (value) and weight (error)
00431       histo->Fill( static_cast<Double_t>(bin-.5), 
00432                    static_cast<Double_t>(ii->first) ); //, ii->second ); 
00433       entries_++;
00434     }
00435   }
00436   
00437 }
00438 
00439 
00440