CMS 3D CMS Logo

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

Generated on Tue Jun 9 17:33:35 2009 for CMSSW by  doxygen 1.5.4