Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:41

0001 #include "DQM/SiStripCommissioningSummary/interface/SummaryGenerator.h"
0002 #include "DQM/SiStripCommissioningSummary/interface/SummaryGeneratorControlView.h"
0003 #include "DQM/SiStripCommissioningSummary/interface/SummaryGeneratorReadoutView.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0006 #include <iostream>
0007 #include <sstream>
0008 #include <cmath>
0009 #include "TH2F.h"
0010 #include "TProfile.h"
0011 
0012 using namespace sistrip;
0013 
0014 // -----------------------------------------------------------------------------
0015 //
0016 SummaryGenerator::SummaryGenerator(std::string name)
0017     : map_(), entries_(-1.), max_(-1. * sistrip::invalid_), min_(1. * sistrip::invalid_), label_(""), myName_(name) {
0018   // TH1::SetDefaultSumw2(true); // use square of weights to calc error
0019 }
0020 
0021 // -----------------------------------------------------------------------------
0022 //
0023 SummaryGenerator* SummaryGenerator::instance(const sistrip::View& view) {
0024   SummaryGenerator* generator = nullptr;
0025   if (view == sistrip::CONTROL_VIEW) {
0026     generator = new SummaryGeneratorControlView();
0027   } else if (view == sistrip::READOUT_VIEW) {
0028     generator = new SummaryGeneratorReadoutView();
0029   } else {
0030     generator = nullptr;
0031   }
0032 
0033   if (generator) {
0034     LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0035                               << " Built \"" << generator->myName() << "\" object!";
0036   } else {
0037     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0038                                      << " Unexpected view: \"" << SiStripEnumsAndStrings::view(view)
0039                                      << "\" Unable to build Generator!"
0040                                      << " Returning NULL pointer!";
0041   }
0042 
0043   return generator;
0044 }
0045 
0046 // -----------------------------------------------------------------------------
0047 //
0048 std::string SummaryGenerator::name(const sistrip::RunType& run_type,
0049                                    const sistrip::Monitorable& mon,
0050                                    const sistrip::Presentation& pres,
0051                                    const sistrip::View& view,
0052                                    const std::string& directory) {
0053   std::stringstream ss;
0054   ss << sistrip::summaryHisto_ << sistrip::sep_;
0055   ss << SiStripEnumsAndStrings::presentation(pres) << sistrip::sep_;
0056   ss << SiStripEnumsAndStrings::runType(run_type) << sistrip::sep_;
0057   ss << SiStripEnumsAndStrings::view(view) << sistrip::sep_;
0058   ss << SiStripEnumsAndStrings::monitorable(mon);
0059 
0060   //LogTrace(mlSummaryPlots_)
0061   //<< "[SummaryGenerator::" << __func__ << "]"
0062   //<< " Histogram name: \"" << ss.str() << "\"";
0063 
0064   return ss.str();
0065 }
0066 
0067 // -----------------------------------------------------------------------------
0068 //
0069 /*
0070   fix nbins for 1D distribution? to 1024? then change within summary
0071   methods with SetBins() methods? but must limit nbins to < 1024!!!
0072 
0073 */
0074 TH1* SummaryGenerator::histogram(const sistrip::Presentation& pres, const uint32_t& xbins) {
0075   if (!xbins) {
0076     return nullptr;
0077   }
0078 
0079   TH1* summary = nullptr;
0080   if (pres == sistrip::HISTO_1D) {
0081     summary = new TH1F("", "", 1024, 0., static_cast<float>(1024));
0082   } else if (pres == sistrip::HISTO_2D_SUM) {
0083     summary = new TH1F("", "", xbins, 0., static_cast<float>(xbins));
0084   } else if (pres == sistrip::HISTO_2D_SCATTER) {
0085     summary = new TH2F("", "", 100 * xbins, 0., static_cast<float>(100 * xbins), 1025, 0., 1025.);
0086   } else if (pres == sistrip::PROFILE_1D) {
0087     summary = new TProfile("", "", xbins, 0., static_cast<float>(xbins), 0., 1025.);
0088   } else {
0089     summary = nullptr;
0090   }
0091 
0092   if (summary) {
0093     LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0094                               << " Histogram name: \"" << summary->GetName() << "\"";
0095   } else {
0096     edm::LogVerbatim(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0097                                       << " Unexpected presentation: \"" << SiStripEnumsAndStrings::presentation(pres)
0098                                       << "\" Unable to build summary plot!"
0099                                       << " Returning NULL pointer!";
0100   }
0101 
0102   return summary;
0103 }
0104 
0105 // -----------------------------------------------------------------------------
0106 //
0107 void SummaryGenerator::format(const sistrip::RunType& run_type,
0108                               const sistrip::Monitorable& mon,
0109                               const sistrip::Presentation& pres,
0110                               const sistrip::View& view,
0111                               const std::string& directory,
0112                               const sistrip::Granularity& gran,
0113                               TH1& summary_histo) {
0114   // Set name, title and entries
0115   //std::stringstream ss;
0116   //std::string name = SummaryGenerator::name( run_type, mon, pres, view, directory );
0117   //summary_histo.SetName( name.c_str() );
0118   //summary_histo.SetTitle( name.c_str() );
0119   if (entries_ >= 0.) {
0120     summary_histo.SetEntries(entries_);
0121   }
0122 
0123   // X axis
0124   summary_histo.GetXaxis()->SetLabelSize(0.03);
0125   summary_histo.GetXaxis()->SetTitleSize(0.03);
0126   summary_histo.GetXaxis()->SetTitleOffset(3.5);
0127   //gPad->SetBottomMargin(0.2);
0128 
0129   // Y axis
0130   summary_histo.GetYaxis()->SetLabelSize(0.03);
0131   summary_histo.GetYaxis()->SetTitleSize(0.03);
0132   summary_histo.GetYaxis()->SetTitleOffset(1.5);
0133   //gPad->SetLeftMargin(0.2);
0134 
0135   // Axis label
0136   if (pres == sistrip::HISTO_1D) {
0137     std::string xtitle = label_ + " (for " + directory + ")";
0138     summary_histo.GetXaxis()->SetTitle(xtitle.c_str());
0139     summary_histo.GetYaxis()->SetTitle("Frequency");
0140     summary_histo.GetXaxis()->SetTitleOffset(1.5);  //@@ override value set above
0141   } else {
0142     std::string xtitle = SiStripEnumsAndStrings::granularity(gran) + " within " + directory;
0143     summary_histo.GetXaxis()->SetTitle(xtitle.c_str());
0144     summary_histo.GetYaxis()->SetTitle(label_.c_str());
0145     //summary_histo.GetXaxis()->SetTitleOffset(1.5); //@@ override value set above (3.5?)
0146   }
0147 
0148   // Formatting for 2D plots
0149   if (pres == sistrip::HISTO_2D_SCATTER) {
0150     // Markers (open circles)
0151     summary_histo.SetMarkerStyle(2);
0152     summary_histo.SetMarkerSize(0.6);
0153   }
0154 
0155   // Semi-generic formatting
0156   if (pres == sistrip::HISTO_2D_SUM || pres == sistrip::HISTO_2D_SCATTER || pres == sistrip::PROFILE_1D) {
0157     /*
0158     //put solid and dotted lines on summary to separate top- and
0159     //2nd-from-top- level bin groups.
0160 
0161     uint16_t topLevel = 0, topLevelOld = 0, secondLevel = 0, secondLevelOld = 0;
0162     std::string::size_type pos = 0;
0163     for ( HistoData::iterator ibin = map_.begin(); ibin != map_.end(); ibin++) {
0164 
0165       //draw line if top and second level numbers change.
0166       pos = ibin->first.find(sistrip::dot_,0);
0167       if (pos != std::string::npos) {
0168     if ((topLevel=atoi(std::string(ibin->first,0,pos).c_str())) != topLevelOld) {
0169       topLevel = topLevelOld;
0170       //
0171     }
0172     else if (ibin->first.find(sistrip::dot_,pos+1) != std::string::npos) {
0173         if ((secondLevelOld=atoi(std::string(ibin->first,pos+1,(ibin->first.find(sistrip::dot_,pos+1)- (pos+1))).c_str())) != secondLevel) {
0174           secondLevel = secondLevelOld;
0175           //
0176         }}}
0177     }
0178     */
0179   }
0180 }
0181 
0182 // -----------------------------------------------------------------------------
0183 //
0184 void SummaryGenerator::clearMap() {
0185   HistoData::iterator iter = map_.begin();
0186   for (; iter != map_.end(); iter++) {
0187     iter->second.clear();
0188   }
0189   map_.clear();
0190   entries_ = -1.;
0191   max_ = -1. * sistrip::invalid_;
0192   min_ = 1. * sistrip::invalid_;
0193 }
0194 
0195 // -----------------------------------------------------------------------------
0196 //
0197 void SummaryGenerator::printMap() {
0198   std::stringstream ss;
0199   ss << "[SummaryGenerator::" << __func__ << "]"
0200      << " Printing contents of map: " << std::endl;
0201 
0202   HistoData::iterator iter = map_.begin();
0203   for (; iter != map_.end(); iter++) {
0204     ss << " bin/entries: " << iter->first << "/" << iter->second.size() << " ";
0205     if (!iter->second.empty()) {
0206       ss << " value/error: ";
0207       std::vector<Data>::const_iterator jter = iter->second.begin();
0208       for (; jter != iter->second.end(); jter++) {
0209         ss << jter->first << "/" << jter->second << " ";
0210       }
0211     }
0212     ss << std::endl;
0213   }
0214 
0215   ss << " Max value: " << max_ << std::endl << " Min value: " << min_ << std::endl;
0216 
0217   LogTrace(mlSummaryPlots_) << ss.str();
0218 }
0219 
0220 // -----------------------------------------------------------------------------
0221 //
0222 void SummaryGenerator::fillMap(const std::string& top_level_dir,
0223                                const sistrip::Granularity& gran,
0224                                const uint32_t& device_key,
0225                                const float& value,
0226                                const float& error) {
0227   // Check if value is valid
0228   if (value > 1. * sistrip::valid_) {
0229     return;
0230   }
0231 
0232   // Calculate maximum and minimum values in std::map
0233   if (value > max_) {
0234     max_ = value;
0235   }
0236   if (value < min_) {
0237     min_ = value;
0238   }
0239 
0240   // Check if error is valid
0241   if (error < 1. * sistrip::valid_) {
0242     fill(top_level_dir, gran, device_key, value, error);
0243   } else {
0244     fill(top_level_dir, gran, device_key, value, 0.);
0245   }
0246 }
0247 
0248 // -----------------------------------------------------------------------------
0249 //
0250 void SummaryGenerator::fill(const std::string& top_level_dir,
0251                             const sistrip::Granularity& gran,
0252                             const uint32_t& device_key,
0253                             const float& value,
0254                             const float& error) {
0255   LogTrace(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0256                             << " Derived implementation does not exist!...";
0257 }
0258 
0259 //------------------------------------------------------------------------------
0260 //
0261 void SummaryGenerator::histo1D(TH1& his) {
0262   // Check number of entries in map
0263   if (map_.empty()) {
0264     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0265                                      << " No contents in std::map to histogram!";
0266     return;
0267   }
0268 
0269   // Retrieve histogram
0270   TH1F* histo = dynamic_cast<TH1F*>(&his);
0271   if (!histo) {
0272     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0273                                      << " NULL pointer to TH1F histogram!";
0274     return;
0275   }
0276 
0277   // Calculate bin range
0278   int32_t high = static_cast<int32_t>(fabs(max_) > 20. ? max_ + 0.05 * fabs(max_) : max_ + 1.);
0279   int32_t low = static_cast<int32_t>(fabs(min_) > 20. ? min_ - 0.05 * fabs(min_) : min_ - 1.);
0280   int32_t range = high - low;
0281 
0282   // increase number of bins for floats
0283   //   if ( max_ - static_cast<int32_t>(max_) > 1.e-6 &&
0284   //        min_ - static_cast<int32_t>(min_) > 1.e-6 ) {
0285   //     range = 100 * range;
0286   //   }
0287 
0288   // Set histogram binning
0289   histo->SetBins(range, static_cast<float>(low), static_cast<float>(high));
0290 
0291   // Iterate through std::map, set bin labels and fill histogram
0292   entries_ = 0.;
0293   HistoData::const_iterator ibin = map_.begin();
0294   for (; ibin != map_.end(); ibin++) {
0295     if (ibin->second.empty()) {
0296       continue;
0297     }
0298     BinData::const_iterator ii = ibin->second.begin();
0299     for (; ii != ibin->second.end(); ii++) {
0300       // bin (value) and weight (error)
0301       histo->Fill(ii->first);  //, ii->second );
0302       entries_++;
0303     }
0304   }
0305 }
0306 
0307 //------------------------------------------------------------------------------
0308 //
0309 void SummaryGenerator::histo2DSum(TH1& his) {
0310   // Check number of entries in map
0311   if (map_.empty()) {
0312     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0313                                      << " No contents in std::map to histogram!";
0314     return;
0315   }
0316 
0317   // Retrieve histogram
0318   TH1F* histo = dynamic_cast<TH1F*>(&his);
0319   if (!histo) {
0320     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0321                                      << " NULL pointer to TH1F histogram!";
0322     return;
0323   }
0324 
0325   // Iterate through map, set bin labels and fill histogram
0326   entries_ = 0.;
0327   uint16_t bin = 0;
0328   HistoData::const_iterator ibin = map_.begin();
0329   for (; ibin != map_.end(); ibin++) {
0330     bin++;
0331     histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
0332     if (ibin->second.empty()) {
0333       continue;
0334     }
0335     BinData::const_iterator ii = ibin->second.begin();
0336     for (; ii != ibin->second.end(); ii++) {
0337       // x (bin), y (value) and weight (error)
0338       histo->Fill(static_cast<Double_t>(bin - 0.5),
0339                   static_cast<Double_t>(ii->first));  //, ii->second );
0340       entries_ += 1. * ii->first;
0341     }
0342   }
0343 }
0344 
0345 //------------------------------------------------------------------------------
0346 //
0347 void SummaryGenerator::histo2DScatter(TH1& his) {
0348   // Check number of entries in map
0349   if (map_.empty()) {
0350     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0351                                      << " No contents in std::map to histogram!";
0352     return;
0353   }
0354 
0355   // Retrieve histogram
0356   TH2F* histo = dynamic_cast<TH2F*>(&his);
0357   if (!histo) {
0358     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0359                                      << " NULL pointer to TH2F histogram!";
0360     return;
0361   }
0362 
0363   // Iterate through std::map, set bin labels and fill histogram
0364   entries_ = 0.;
0365   uint16_t bin = 0;
0366   HistoData::const_iterator ibin = map_.begin();
0367   for (; ibin != map_.end(); ibin++) {
0368     bin++;
0369     histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
0370     if (ibin->second.empty()) {
0371       continue;
0372     }
0373     BinData::const_iterator ii = ibin->second.begin();
0374     for (; ii != ibin->second.end(); ii++) {
0375       // x (bin), y (value) and weight (error)
0376       histo->Fill(static_cast<Double_t>(bin - 0.5),
0377                   static_cast<Double_t>(ii->first));  // , ii->second );
0378       entries_++;
0379     }
0380   }
0381 }
0382 
0383 //------------------------------------------------------------------------------
0384 //
0385 void SummaryGenerator::profile1D(TH1& his) {
0386   // Check number of entries in map
0387   if (map_.empty()) {
0388     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0389                                      << " No contents in std::map to histogram!";
0390     return;
0391   }
0392 
0393   // Retrieve histogram
0394   TProfile* histo = dynamic_cast<TProfile*>(&his);
0395   if (!histo) {
0396     edm::LogWarning(mlSummaryPlots_) << "[SummaryGenerator::" << __func__ << "]"
0397                                      << " NULL pointer to TProfile histogram!";
0398     return;
0399   }
0400 
0401   // Iterate through std::map, set bin labels and fill histogram
0402   entries_ = 0.;
0403   uint16_t bin = 0;
0404   HistoData::const_iterator ibin = map_.begin();
0405   for (; ibin != map_.end(); ibin++) {
0406     bin++;
0407     histo->GetXaxis()->SetBinLabel(static_cast<Int_t>(bin), ibin->first.c_str());
0408     if (ibin->second.empty()) {
0409       continue;
0410     }
0411     BinData::const_iterator ii = ibin->second.begin();
0412     for (; ii != ibin->second.end(); ii++) {
0413       // x (bin), y (value) and weight (error)
0414       histo->Fill(static_cast<Double_t>(bin - .5),
0415                   static_cast<Double_t>(ii->first));  //, ii->second );
0416       entries_++;
0417     }
0418   }
0419 }