CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/DQM/SiStripMonitorClient/src/SiStripHistoPlotter.cc

Go to the documentation of this file.
00001 #include "DQM/SiStripMonitorClient/interface/SiStripHistoPlotter.h"
00002 #include "DQM/SiStripMonitorClient/interface/SiStripUtility.h"
00003 #include "DQMServices/Core/interface/DQMStore.h"
00004 #include "DQMServices/Core/interface/MonitorElement.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 #include "FWCore/ParameterSet/interface/FileInPath.h"
00007 
00008 #include "TText.h"
00009 #include "TROOT.h"
00010 #include "TPad.h"
00011 #include "TSystem.h"
00012 #include "TString.h"
00013 #include "TImage.h"
00014 #include "TPaveText.h"
00015 #include "TImageDump.h"
00016 #include "TAxis.h"
00017 #include "TStyle.h"
00018 #include "TPaveLabel.h"
00019 #include "TH1F.h"
00020 #include "TH2F.h"
00021 #include "TProfile.h"
00022 #include <iostream>
00023 //
00024 // -- Constructor
00025 // 
00026 SiStripHistoPlotter::SiStripHistoPlotter() {
00027   edm::LogInfo("SiStripHistoPlotter") << 
00028     " Creating SiStripHistoPlotter " << "\n" ;
00029 }
00030 //
00031 // --  Destructor
00032 // 
00033 SiStripHistoPlotter::~SiStripHistoPlotter() {
00034   edm::LogInfo("SiStripHistoPlotter") << 
00035     " Deleting SiStripHistoPlotter " << "\n" ;
00036   plotList_.clear();
00037   condDBPlotList_.clear();
00038 
00039 }
00040 //
00041 // -- Set New Plot
00042 //
00043 void SiStripHistoPlotter::setNewPlot(std::string& path, std::string& option, int width, int height) {
00044   std::string name = "Dummy";
00045   if (!hasNamedImage(name)) createDummyImage(name);
00046   PlotParameter local_par;
00047   local_par.Path    = path;
00048   local_par.Option  = option;
00049   local_par.CWidth  = width;
00050   local_par.CHeight = height;
00051   plotList_.push_back(local_par);  
00052 }
00053 //
00054 // -- Create Plots 
00055 //
00056 void SiStripHistoPlotter::createPlots(DQMStore* dqm_store) {
00057   if (plotList_.size() == 0) return;
00058   std::string name = "Dummy";
00059   if (!hasNamedImage(name)) createDummyImage(name);
00060   for (std::vector<PlotParameter>::iterator it = plotList_.begin(); 
00061        it != plotList_.end(); it++) {
00062     makePlot(dqm_store, (*it));
00063   }
00064   plotList_.clear();
00065 }
00066 //
00067 // -- Draw Histograms 
00068 //
00069 void SiStripHistoPlotter::makePlot(DQMStore* dqm_store, const PlotParameter& par) {
00070   TCanvas * canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
00071 
00072   MonitorElement * me = dqm_store->get(par.Path);
00073   if (me) { 
00074         
00075     int istat =  SiStripUtility::getMEStatus(me);
00076     
00077     std::string dopt = par.Option;
00078     std::string tag;
00079     int icol;
00080     SiStripUtility::getMEStatusColor(istat, icol, tag);
00081     if (me->kind() == MonitorElement::DQM_KIND_TH1F ||
00082         me->kind() == MonitorElement::DQM_KIND_TH2F ||
00083         me->kind() == MonitorElement::DQM_KIND_TPROFILE ||
00084         me->kind() == MonitorElement::DQM_KIND_TPROFILE2D ) {
00085       TH1* histo = me->getTH1();
00086       TH1F* tproject = 0;
00087       if (dopt == "projection") {
00088         getProjection(me, tproject);
00089         if (tproject) tproject->Draw();
00090         else histo->Draw();
00091       } else {
00092         dopt = ""; 
00093         std::string name = histo->GetName();
00094         if (me->kind() == MonitorElement::DQM_KIND_TPROFILE2D ) {
00095           dopt = "colz";
00096           histo->SetStats( kFALSE );
00097         } else {    
00098           if (name.find("Summary_Mean") != std::string::npos) {
00099             histo->SetStats( kFALSE );
00100           } else {
00101             histo->SetFillColor(1);
00102           }
00103         }
00104         histo->Draw(dopt.c_str());
00105       }
00106     }
00107     TText tTitle;
00108     tTitle.SetTextFont(64);
00109     tTitle.SetTextSizePixels(20);
00110     //    tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
00111     
00112     if (icol != 1) {
00113       TText tt;
00114       tt.SetTextSize(0.12);
00115       tt.SetTextColor(icol);
00116       tt.DrawTextNDC(0.5, 0.5, tag.c_str());
00117     }
00118     fillNamedImageBuffer(canvas, par.Path);
00119     canvas->Clear();
00120   }
00121   delete canvas;
00122 }
00123 //
00124 // -- Get Named Image buffer
00125 //
00126 void SiStripHistoPlotter::getNamedImageBuffer(const std::string& path, std::string& image) {
00127   std::map<std::string, std::string>::iterator cPos;
00128   if (path == "dummy_path") {
00129     std::cout << " Sending Dummy Image for : "
00130          <<  path << std::endl;
00131     cPos = namedPictureBuffer_.find("Dummy");
00132     image = cPos->second;
00133   } else {
00134     cPos = namedPictureBuffer_.find(path);
00135     if (cPos != namedPictureBuffer_.end()) {
00136       image = cPos->second;
00137       if (namedPictureBuffer_.size() > 99 ) namedPictureBuffer_.erase(cPos);
00138     } else {
00139       std::cout << " Sending Dummy Image for : "
00140            <<  path << std::endl;
00141       cPos = namedPictureBuffer_.find("Dummy");
00142       image = cPos->second;
00143     }
00144   }
00145 }
00150 void SiStripHistoPlotter::fillNamedImageBuffer(TCanvas * c1, const std::string& name) 
00151 {
00152   //  DQMScope enter;
00153  // Now extract the image
00154   // 114 - stands for "no write on Close"
00155 //   std::cout << ACYellow << ACBold
00156 //        << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
00157 //        << ACPlain
00158 //        << "A canvas: "
00159 //        << c1->GetName() 
00160 //        << std::endl ;
00161   c1->Update();
00162   c1->Modified(); 
00163   TImageDump imgdump("tmp.png", 114);
00164   c1->Paint();
00165 
00166 // get an internal image which will be automatically deleted
00167 // in the imgdump destructor
00168   TImage *image = imgdump.GetImage();
00169 
00170   if( image == NULL )
00171   {
00172    std::cout << "No TImage found for "
00173         << name
00174         << std::endl ;
00175     return ;
00176   }
00177   char *buf;
00178   int sz = 0;
00179   image->GetImageBuffer(&buf, &sz);
00180 
00181   std::ostringstream local_str;
00182   for (int i = 0; i < sz; i++) local_str << buf[i];
00183   
00184 //  delete [] buf;
00185   ::free(buf); // buf is allocated via realloc() by a C language AfterStep library invoked by the
00186                // default (and so far only) TImage implementation in root, TASImage.
00187   
00188   // clear the first element map if # of entries > 30
00189   if (hasNamedImage(name)) namedPictureBuffer_.erase(name);
00190   namedPictureBuffer_[name] = local_str.str();
00191   //  if (namedPictureBuffer_[name].size() > 0) std::cout << "image created " << name << std::endl;
00192 }
00193 //
00194 // -- Check if the image exists
00195 //
00196 bool SiStripHistoPlotter::hasNamedImage(const std::string& name) {
00197   std::map<std::string, std::string>::const_iterator cPos = namedPictureBuffer_.find(name);
00198   if (cPos == namedPictureBuffer_.end()) { 
00199     return false;
00200   } else return true;
00201 }
00202 //
00203 // -- Create Dummy Image 
00204 //
00205 void SiStripHistoPlotter::createDummyImage(const std::string & name) {
00206   std::string image;
00207   getDummyImage(image);
00208   namedPictureBuffer_.insert(std::pair<std::string, std::string>(name, image));
00209 }
00210 //
00211 // -- Get Image reading a disk resident image
00212 //
00213 void SiStripHistoPlotter::getDummyImage(std::string & image) {
00214   std::string          line;
00215   std::ostringstream   local_str;
00216   // Read back the file line by line and temporarily store it in a stringstream
00217   std::string localPath = std::string("DQM/TrackerCommon/test/images/EmptyPlot.png");
00218   ifstream * imagefile = new ifstream((edm::FileInPath(localPath).fullPath()).c_str(),std::ios::in);
00219   if(imagefile->is_open()) {
00220     while (getline( *imagefile, line )) {
00221       local_str << line << std::endl ;
00222     }
00223   }  
00224   imagefile->close();
00225   image = local_str.str();
00226 }
00227 // -- Set Drawing Option
00228 //
00229 void SiStripHistoPlotter::setDrawingOption(TH1* hist) {
00230   if (!hist) return;
00231 
00232   TAxis* xa = hist->GetXaxis();
00233   TAxis* ya = hist->GetYaxis();
00234 
00235   xa->SetTitleOffset(0.7);
00236   xa->SetTitleSize(0.05);
00237   xa->SetLabelSize(0.04);
00238 
00239   ya->SetTitleOffset(0.7);
00240   ya->SetTitleSize(0.05);
00241   ya->SetLabelSize(0.04);
00242 
00243 }
00244 // -- Get Projection Histogram 
00245 //
00246 void SiStripHistoPlotter::getProjection(MonitorElement* me, TH1F* tp) {
00247   
00248   std::string ptit = me->getTitle();
00249   ptit += "-Yprojection";
00250 
00251   if (me->kind() == MonitorElement::DQM_KIND_TH2F) {
00252     TH2F* hist2 = me->getTH2F();
00253     tp = new TH1F(ptit.c_str(),ptit.c_str(),hist2->GetNbinsY(), 
00254                   hist2->GetYaxis()->GetXmin(),hist2->GetYaxis()->GetXmax());
00255     tp->GetXaxis()->SetTitle(ptit.c_str());
00256     for (int j = 1; j < hist2->GetNbinsY()+1; j++) {
00257       float tot_count = 0.0;
00258       for (int i = 1; i < hist2->GetNbinsX()+1; i++) {
00259         tot_count += hist2->GetBinContent(i,j);
00260       }
00261       tp->SetBinContent(j, tot_count);
00262     }
00263   } else if (me->kind() == MonitorElement::DQM_KIND_TPROFILE) {
00264      TProfile* prof = me->getTProfile();
00265      tp = new TH1F(ptit.c_str(),ptit.c_str(),100, 
00266                     0.0,prof->GetMaximum()*1.2);
00267      tp->GetXaxis()->SetTitle(ptit.c_str());
00268      for (int i = 1; i < prof->GetNbinsX()+1; i++) {
00269        tp->Fill(prof->GetBinContent(i));
00270      }
00271   } else if (me->kind() == MonitorElement::DQM_KIND_TH1F) {
00272     TH1F* hist1 = me->getTH1F();
00273     tp = new TH1F(ptit.c_str(),ptit.c_str(),100, 
00274               0.0,hist1->GetMaximum()*1.2);
00275     tp->GetXaxis()->SetTitle(ptit.c_str());
00276     for (int i = 1; i < hist1->GetNbinsX()+1; i++) {
00277       tp->Fill(hist1->GetBinContent(i));
00278     }
00279   }
00280 }
00281 //
00282 // -- create static plots
00283 // 
00284 void SiStripHistoPlotter::createStaticPlot(MonitorElement* me, const std::string& file_name) {
00285   TH1* hist1 = me->getTH1();
00286   TCanvas* canvas  = new TCanvas("TKCanvas", "TKCanvas", 600, 400);
00287   if (hist1) {
00288     TText tTitle;
00289     tTitle.SetTextFont(64);
00290     tTitle.SetTextSizePixels(20);
00291     
00292     setDrawingOption(hist1);
00293     hist1->Draw();
00294     std::string name = hist1->GetName();
00295     if (me->getRefRootObject()) {
00296       TH1* hist1_ref = me->getRefTH1();
00297       if (hist1_ref) {
00298         hist1_ref->SetLineColor(3);
00299         hist1_ref->SetMarkerColor(3);
00300         if (name.find("Summary") != std::string::npos) hist1_ref->Draw("same");
00301         else hist1_ref->DrawNormalized("same", hist1->GetEntries());
00302       }
00303     }
00304   }
00305   canvas->Update();
00306   std::string command = "rm -f " + file_name;
00307   gSystem->Exec(command.c_str());
00308   canvas->Print(file_name.c_str(),"png");
00309   canvas->Clear();
00310   delete canvas;        
00311 }
00312 //
00313 // -- Set New CondDB Plot
00314 //
00315 void SiStripHistoPlotter::setNewCondDBPlot(std::string& path, std::string& option, int width, int height) {
00316   PlotParameter local_par;
00317   local_par.Path    = path;
00318   local_par.Option  = option;
00319   local_par.CWidth  = width;
00320   local_par.CHeight = height;
00321   condDBPlotList_.push_back(local_par);  
00322 }
00323 //
00324 // -- Create CondDB Plots 
00325 //
00326 void SiStripHistoPlotter::createCondDBPlots(DQMStore* dqm_store) {
00327   if (condDBPlotList_.size() == 0) return;
00328   std::string name = "Dummy";
00329   if (!hasNamedImage(name)) createDummyImage(name);
00330 
00331   for (std::vector<PlotParameter>::iterator it = condDBPlotList_.begin(); 
00332        it != condDBPlotList_.end(); it++) {
00333     makeCondDBPlots(dqm_store, (*it));
00334   }
00335   condDBPlotList_.clear();
00336 }
00337 //
00338 // -- Draw CondDB Histograms 
00339 //
00340 void SiStripHistoPlotter::makeCondDBPlots(DQMStore* dqm_store, const PlotParameter& par) {
00341   TCanvas * canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
00342 
00343   std::vector<std::string> htypes;
00344   std::string option = par.Option;
00345   SiStripUtility::split(option, htypes, ",");
00346 
00347   std::string tag;
00348   std::vector<MonitorElement*> all_mes = dqm_store->getContents(par.Path);
00349 
00350   for (std::vector<std::string>::const_iterator ih = htypes.begin();
00351        ih!= htypes.end(); ih++) {
00352     std::string type = (*ih);
00353     if (type.size() == 0) continue;
00354     std::string tag = par.Path + "/";
00355     for (std::vector<MonitorElement *>::const_iterator it = all_mes.begin();
00356          it!= all_mes.end(); it++) {  
00357       MonitorElement * me = (*it);
00358       if (!me) continue;
00359       std::string hname = me->getName();
00360       if (hname.find(type) != std::string::npos) {
00361         TH1* histo = me->getTH1();
00362         histo->Draw();
00363         tag += type;
00364         fillNamedImageBuffer(canvas, tag);
00365         canvas->Clear();        
00366       } 
00367     }
00368   }
00369   delete canvas;
00370 }