Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "DQM/SiStripMonitorClient/interface/SiStripHistoPlotter.h"
0002 #include "DQM/SiStripMonitorClient/interface/SiStripUtility.h"
0003 #include "DQMServices/Core/interface/DQMStore.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/ParameterSet/interface/FileInPath.h"
0006 
0007 #include "TText.h"
0008 #include "TROOT.h"
0009 #include "TPad.h"
0010 #include "TSystem.h"
0011 #include "TString.h"
0012 #include "TImage.h"
0013 #include "TPaveText.h"
0014 #include "TImageDump.h"
0015 #include "TAxis.h"
0016 #include "TStyle.h"
0017 #include "TPaveLabel.h"
0018 #include "TH1F.h"
0019 #include "TH2F.h"
0020 #include "TProfile.h"
0021 #include <iostream>
0022 //
0023 // -- Constructor
0024 //
0025 SiStripHistoPlotter::SiStripHistoPlotter() {
0026   edm::LogInfo("SiStripHistoPlotter") << " Creating SiStripHistoPlotter "
0027                                       << "\n";
0028 }
0029 //
0030 // --  Destructor
0031 //
0032 SiStripHistoPlotter::~SiStripHistoPlotter() {
0033   edm::LogInfo("SiStripHistoPlotter") << " Deleting SiStripHistoPlotter "
0034                                       << "\n";
0035 }
0036 //
0037 // -- Set New Plot
0038 //
0039 void SiStripHistoPlotter::setNewPlot(std::string const& path,
0040                                      std::string const& option,
0041                                      int const width,
0042                                      int const height) {
0043   std::string name = "Dummy";
0044   if (!hasNamedImage(name))
0045     createDummyImage(name);
0046   PlotParameter local_par{path, option, width, height};
0047   plotList_.push_back(std::move(local_par));
0048 }
0049 //
0050 // -- Create Plots
0051 //
0052 void SiStripHistoPlotter::createPlots(DQMStore* dqm_store) {
0053   if (plotList_.empty())
0054     return;
0055   std::string name = "Dummy";
0056   if (!hasNamedImage(name))
0057     createDummyImage(name);
0058   for (auto const& par : plotList_) {
0059     makePlot(dqm_store, par);
0060   }
0061   plotList_.clear();
0062 }
0063 //
0064 // -- Draw Histograms
0065 //
0066 void SiStripHistoPlotter::makePlot(DQMStore const* dqm_store, const PlotParameter& par) {
0067   TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
0068 
0069   MonitorElement* me = dqm_store->get(par.Path);
0070   if (me) {
0071     int istat = SiStripUtility::getMEStatus(me);
0072 
0073     std::string dopt = par.Option;
0074     std::string tag;
0075     int icol;
0076     SiStripUtility::getMEStatusColor(istat, icol, tag);
0077     if (me->kind() == MonitorElement::Kind::TH1F || me->kind() == MonitorElement::Kind::TH2F ||
0078         me->kind() == MonitorElement::Kind::TPROFILE || me->kind() == MonitorElement::Kind::TPROFILE2D) {
0079       TH1* histo = me->getTH1();
0080       TH1F* tproject = nullptr;
0081       if (dopt == "projection") {
0082         getProjection(me, tproject);
0083         if (tproject)
0084           tproject->Draw();
0085         else
0086           histo->Draw();
0087       } else {
0088         dopt = "";
0089         std::string name = histo->GetName();
0090         if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
0091           dopt = "colz";
0092           histo->SetStats(kFALSE);
0093         } else {
0094           if (name.find("Summary_Mean") != std::string::npos) {
0095             histo->SetStats(kFALSE);
0096           } else {
0097             histo->SetFillColor(1);
0098           }
0099         }
0100         histo->Draw(dopt.c_str());
0101       }
0102     }
0103     TText tTitle;
0104     tTitle.SetTextFont(64);
0105     tTitle.SetTextSizePixels(20);
0106     //    tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
0107 
0108     if (icol != 1) {
0109       TText tt;
0110       tt.SetTextSize(0.12);
0111       tt.SetTextColor(icol);
0112       tt.DrawTextNDC(0.5, 0.5, tag.c_str());
0113     }
0114     fillNamedImageBuffer(canvas, par.Path);
0115     canvas->Clear();
0116   }
0117   delete canvas;
0118 }
0119 //
0120 // -- Get Named Image buffer
0121 //
0122 void SiStripHistoPlotter::getNamedImageBuffer(const std::string& path, std::string& image) {
0123   std::map<std::string, std::string>::iterator cPos;
0124   if (path == "dummy_path") {
0125     std::cout << " Sending Dummy Image for : " << path << std::endl;
0126     cPos = namedPictureBuffer_.find("Dummy");
0127     image = cPos->second;
0128   } else {
0129     cPos = namedPictureBuffer_.find(path);
0130     if (cPos != namedPictureBuffer_.end()) {
0131       image = cPos->second;
0132       if (namedPictureBuffer_.size() > 99)
0133         namedPictureBuffer_.erase(cPos);
0134     } else {
0135       std::cout << " Sending Dummy Image for : " << path << std::endl;
0136       cPos = namedPictureBuffer_.find("Dummy");
0137       image = cPos->second;
0138     }
0139   }
0140 }
0141 /*! \brief (Documentation under construction).
0142  *
0143  *  This method
0144  */
0145 void SiStripHistoPlotter::fillNamedImageBuffer(TCanvas* c1, const std::string& name) {
0146   //  DQMScope enter;
0147   // Now extract the image
0148   // 114 - stands for "no write on Close"
0149   //   std::cout << ACYellow << ACBold
0150   //        << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
0151   //        << ACPlain
0152   //        << "A canvas: "
0153   //        << c1->GetName()
0154   //        << std::endl ;
0155   c1->Update();
0156   c1->Modified();
0157   TImageDump imgdump("tmp.png", 114);
0158   c1->Paint();
0159 
0160   // get an internal image which will be automatically deleted
0161   // in the imgdump destructor
0162   TImage* image = imgdump.GetImage();
0163 
0164   if (image == nullptr) {
0165     std::cout << "No TImage found for " << name << std::endl;
0166     return;
0167   }
0168   char* buf;
0169   int sz = 0;
0170   image->GetImageBuffer(&buf, &sz);
0171 
0172   std::ostringstream local_str;
0173   for (int i = 0; i < sz; i++)
0174     local_str << buf[i];
0175 
0176   //  delete [] buf;
0177   ::free(buf);  // buf is allocated via realloc() by a C language AfterStep library invoked by the
0178                 // default (and so far only) TImage implementation in root, TASImage.
0179 
0180   // clear the first element map if # of entries > 30
0181   if (hasNamedImage(name))
0182     namedPictureBuffer_.erase(name);
0183   namedPictureBuffer_[name] = local_str.str();
0184   //  if (namedPictureBuffer_[name].size() > 0) std::cout << "image created " << name << std::endl;
0185 }
0186 //
0187 // -- Check if the image exists
0188 //
0189 bool SiStripHistoPlotter::hasNamedImage(const std::string& name) {
0190   std::map<std::string, std::string>::const_iterator cPos = namedPictureBuffer_.find(name);
0191   if (cPos == namedPictureBuffer_.end()) {
0192     return false;
0193   } else
0194     return true;
0195 }
0196 //
0197 // -- Create Dummy Image
0198 //
0199 void SiStripHistoPlotter::createDummyImage(const std::string& name) {
0200   std::string image;
0201   getDummyImage(image);
0202   namedPictureBuffer_.insert(std::pair<std::string, std::string>(name, image));
0203 }
0204 //
0205 // -- Get Image reading a disk resident image
0206 //
0207 void SiStripHistoPlotter::getDummyImage(std::string& image) {
0208   std::string line;
0209   std::ostringstream local_str;
0210   // Read back the file line by line and temporarily store it in a stringstream
0211   std::string localPath = std::string("DQM/TrackerCommon/test/images/EmptyPlot.png");
0212   std::ifstream* imagefile = new std::ifstream((edm::FileInPath(localPath).fullPath()).c_str(), std::ios::in);
0213   if (imagefile->is_open()) {
0214     while (getline(*imagefile, line)) {
0215       local_str << line << std::endl;
0216     }
0217   }
0218   imagefile->close();
0219   image = local_str.str();
0220 }
0221 // -- Set Drawing Option
0222 //
0223 void SiStripHistoPlotter::setDrawingOption(TH1* hist) {
0224   if (!hist)
0225     return;
0226 
0227   TAxis* xa = hist->GetXaxis();
0228   TAxis* ya = hist->GetYaxis();
0229 
0230   xa->SetTitleOffset(0.7);
0231   xa->SetTitleSize(0.05);
0232   xa->SetLabelSize(0.04);
0233 
0234   ya->SetTitleOffset(0.7);
0235   ya->SetTitleSize(0.05);
0236   ya->SetLabelSize(0.04);
0237 }
0238 // -- Get Projection Histogram
0239 //
0240 void SiStripHistoPlotter::getProjection(MonitorElement* me, TH1F* tp) {
0241   std::string ptit = me->getTitle();
0242   ptit += "-Yprojection";
0243 
0244   if (me->kind() == MonitorElement::Kind::TH2F) {
0245     TH2F* hist2 = me->getTH2F();
0246     tp = new TH1F(
0247         ptit.c_str(), ptit.c_str(), hist2->GetNbinsY(), hist2->GetYaxis()->GetXmin(), hist2->GetYaxis()->GetXmax());
0248     tp->GetXaxis()->SetTitle(ptit.c_str());
0249     for (int j = 1; j < hist2->GetNbinsY() + 1; j++) {
0250       float tot_count = 0.0;
0251       for (int i = 1; i < hist2->GetNbinsX() + 1; i++) {
0252         tot_count += hist2->GetBinContent(i, j);
0253       }
0254       tp->SetBinContent(j, tot_count);
0255     }
0256   } else if (me->kind() == MonitorElement::Kind::TPROFILE) {
0257     TProfile* prof = me->getTProfile();
0258     tp = new TH1F(ptit.c_str(), ptit.c_str(), 100, 0.0, prof->GetMaximum() * 1.2);
0259     tp->GetXaxis()->SetTitle(ptit.c_str());
0260     for (int i = 1; i < prof->GetNbinsX() + 1; i++) {
0261       tp->Fill(prof->GetBinContent(i));
0262     }
0263   } else if (me->kind() == MonitorElement::Kind::TH1F) {
0264     TH1F* hist1 = me->getTH1F();
0265     tp = new TH1F(ptit.c_str(), ptit.c_str(), 100, 0.0, hist1->GetMaximum() * 1.2);
0266     tp->GetXaxis()->SetTitle(ptit.c_str());
0267     for (int i = 1; i < hist1->GetNbinsX() + 1; i++) {
0268       tp->Fill(hist1->GetBinContent(i));
0269     }
0270   }
0271 }
0272 //
0273 // -- Set New CondDB Plot
0274 //
0275 void SiStripHistoPlotter::setNewCondDBPlot(std::string const& path,
0276                                            std::string const& option,
0277                                            int const width,
0278                                            int const height) {
0279   PlotParameter local_par{path, option, width, height};
0280   condDBPlotList_.push_back(std::move(local_par));
0281 }
0282 //
0283 // -- Create CondDB Plots
0284 //
0285 void SiStripHistoPlotter::createCondDBPlots(DQMStore* dqm_store) {
0286   if (condDBPlotList_.empty())
0287     return;
0288   std::string name = "Dummy";
0289   if (!hasNamedImage(name))
0290     createDummyImage(name);
0291 
0292   for (std::vector<PlotParameter>::iterator it = condDBPlotList_.begin(); it != condDBPlotList_.end(); it++) {
0293     makeCondDBPlots(dqm_store, (*it));
0294   }
0295   condDBPlotList_.clear();
0296 }
0297 //
0298 // -- Draw CondDB Histograms
0299 //
0300 void SiStripHistoPlotter::makeCondDBPlots(DQMStore* dqm_store, const PlotParameter& par) {
0301   TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
0302 
0303   std::vector<std::string> htypes;
0304   std::string option = par.Option;
0305   SiStripUtility::split(option, htypes, ",");
0306 
0307   std::string tag;
0308   std::vector<MonitorElement*> all_mes = dqm_store->getContents(par.Path);
0309 
0310   for (std::vector<std::string>::const_iterator ih = htypes.begin(); ih != htypes.end(); ih++) {
0311     std::string type = (*ih);
0312     if (type.empty())
0313       continue;
0314     std::string tag = par.Path + "/";
0315     for (std::vector<MonitorElement*>::const_iterator it = all_mes.begin(); it != all_mes.end(); it++) {
0316       MonitorElement* me = (*it);
0317       if (!me)
0318         continue;
0319       std::string hname = me->getName();
0320       if (hname.find(type) != std::string::npos) {
0321         TH1* histo = me->getTH1();
0322         histo->Draw();
0323         tag += type;
0324         fillNamedImageBuffer(canvas, tag);
0325         canvas->Clear();
0326       }
0327     }
0328   }
0329   delete canvas;
0330 }