CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/DQM/SiPixelMonitorClient/src/SiPixelHistoPlotter.cc

Go to the documentation of this file.
00001 #include "DQM/SiPixelMonitorClient/interface/SiPixelHistoPlotter.h"
00002 #include "DQM/SiPixelMonitorClient/interface/SiPixelUtility.h"
00003 #include "DQMServices/Core/interface/DQMStore.h"
00004 #include "DQMServices/Core/interface/MonitorElement.h"
00005 #include "DQM/SiPixelMonitorClient/interface/ANSIColors.h"
00006 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00007 
00008 
00009 
00010 #include "TText.h"
00011 #include "TROOT.h"
00012 #include "TPad.h"
00013 #include "TSystem.h"
00014 #include "TString.h"
00015 #include "TImage.h"
00016 #include "TPaveText.h"
00017 #include "TImageDump.h"
00018 #include "TAxis.h"
00019 #include "TStyle.h"
00020 #include "TPaveLabel.h"
00021 #include "TH1F.h"
00022 #include "TH2F.h"
00023 #include "TProfile.h"
00024 #include <iostream>
00025 using namespace std;
00026 //
00027 // -- Constructor
00028 // 
00029 SiPixelHistoPlotter::SiPixelHistoPlotter() {
00030   edm::LogInfo("SiPixelHistoPlotter") << 
00031     " Creating SiPixelHistoPlotter " << "\n" ;
00032 }
00033 //
00034 // --  Destructor
00035 // 
00036 SiPixelHistoPlotter::~SiPixelHistoPlotter() {
00037   edm::LogInfo("SiPixelHistoPlotter") << 
00038     " Deleting SiPixelHistoPlotter " << "\n" ;
00039   plotList_.clear();
00040 
00041 }
00042 
00043 //
00044 // -- Set New
00045 //
00046 void SiPixelHistoPlotter::setNewPlot(std::string& path, 
00047                                      std::string& option, 
00048                                      int width,
00049                                      int height) {
00050 //cout<<"Entering SiPixelHistoPlotter::setNewPlot for "<<path<<endl;
00051   PlotParameter local_par;
00052   local_par.Path    = path;
00053   local_par.Option  = option;
00054   local_par.CWidth  = width;
00055   local_par.CHeight = height;
00056   plotList_.push_back(local_par);  
00057 //cout<<"... leaving SiPixelHistoPlotter::setNewPlot!"<<endl;
00058 }
00059 
00060 //
00061 // -- Create Plots 
00062 //
00063 void SiPixelHistoPlotter::createPlots(DQMStore* bei) {
00064   string name = "Dummy";
00065   if (!hasNamedImage(name)) createDummyImage(name);
00066   //cout<<"HP::createPlots:PlotList size="<<plotList_.size()<<endl;
00067   for (vector<PlotParameter>::iterator it = plotList_.begin(); 
00068        it != plotList_.end(); it++) {
00069     makePlot(bei, (*it));
00070   }
00071   plotList_.clear();
00072 }
00073 
00074 //
00075 // -- Draw Histograms 
00076 //
00077 void SiPixelHistoPlotter::makePlot(DQMStore* bei, const PlotParameter& par) {
00078 //cout<<"Entering SiPixelHistoPlotter::makePlot: "<<endl;
00079   TCanvas * canvas = new TCanvas("PXCanvas", "PXCanvas", par.CWidth, par.CHeight);
00080   gROOT->Reset(); 
00081   gStyle->SetPalette(1,0);
00082   gStyle->SetOptStat(111110);
00083   canvas->SetBorderMode(0);
00084   canvas->SetFillColor(0);
00085 
00086   TPaveText * paveOnCanvas = new TPaveText(0.57,0.79,0.77,0.99,"NDCtr");   
00087   paveOnCanvas->SetFillColor(0);
00088   
00089   //std::cout<<"::makePlot:Trying to plot "<<par.Path<<std::endl;
00090   MonitorElement * me = bei->get(par.Path);
00091   if (me) { 
00092         
00093     string opt = "";
00094     if (me->kind() == MonitorElement::DQM_KIND_TH2F) 
00095       { opt = "COLZ"; gStyle->SetOptStat(10); }
00096     me->getRootObject()->Draw(opt.c_str());
00097     int istat =  SiPixelUtility::getStatus(me);
00098     
00099     string dopt = par.Option;
00100     string tag;
00101     int icol;
00102     SiPixelUtility::getStatusColor(istat, icol, tag);
00103 
00104     TH1F* histo = dynamic_cast<TH1F*>(me->getRootObject());
00105     string var = (me->getName()).substr((me->getName()).find_last_of("/")+1);
00106     if (histo) {
00107       //setDrawingOption(histo);
00108       //histo->Draw(opt.c_str());
00109       string name = histo->GetName();
00110       if (me->getRefRootObject()) {
00111         TH1* histo_ref = me->getRefTH1();
00112         if (histo_ref) {
00113           histo_ref->SetLineColor(4);
00114           histo_ref->SetMarkerColor(4);
00115           if (name.find("SUM") != string::npos) histo_ref->Draw("same");
00116           else histo_ref->DrawNormalized("same", histo->GetEntries());
00117         }
00118       }
00119     }
00120     canvas->Update();
00121     
00122     //TH1* histo = me->getTH1();    
00123     //TH1F* tproject = 0;
00124     //if (dopt == "projection") {
00125       //getProjection(me, tproject);
00126       //if (tproject) tproject->Draw();
00127       //else histo->Draw();
00128     //} else {
00129      // histo->Draw();
00130     //}
00131     //TText tTitle;
00132     //tTitle.SetTextFont(64);
00133     //tTitle.SetTextSizePixels(20);
00134     //tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
00135 
00136     //if (icol != 1) {
00137     //  TText tt;
00138     //  tt.SetTextSize(0.12);
00139     //  tt.SetTextColor(icol);
00140     //  tt.DrawTextNDC(0.5, 0.5, tag.c_str());
00141     //}
00142     
00143     TLine* l_min = new TLine();
00144     TLine* l_max = new TLine();
00145     l_min->SetLineWidth(4);
00146     l_max->SetLineWidth(4);
00147   
00148     
00149     if(istat!=0){
00150       TText* statusOnCanvas = paveOnCanvas->AddText(tag.c_str());
00151       statusOnCanvas->SetTextSize(0.08);
00152       statusOnCanvas->SetTextFont(112);
00153       statusOnCanvas->SetNDC(kTRUE);
00154       statusOnCanvas->SetTextColor(icol);
00155       
00156       double ymax = -1.;  
00157       double ymin = -1.;
00158       double xmax = -1.;  
00159       double xmin = -1.;
00160       double warning = -1.;
00161       double error = -1.;
00162       double channelFraction = -1.;
00163       //if(var.find("SUM") != string::npos){
00164       //cout << "ME name: " << var << endl;
00165       setLines(me,var,ymin,ymax,warning,error,channelFraction);
00166       //cout << "ymin: " << ymin << " ymax: " << ymax << " warning: " << warning << " error: " << error << " channelFraction: " << channelFraction << endl;
00167         
00168       if(istat!=dqm::qstatus::STATUS_OK){
00169         string textMessage = "fraction of channels failing:";
00170         TText* messageOnCanvas = paveOnCanvas->AddText(textMessage.c_str());
00171         messageOnCanvas->SetTextSize(0.03);
00172         messageOnCanvas->SetNDC(kTRUE);
00173         char text[10];
00174         sprintf(text,"%.2f %%",channelFraction);
00175         messageOnCanvas = paveOnCanvas->AddText(text);
00176         messageOnCanvas->SetTextSize(0.035);
00177         messageOnCanvas->SetNDC(kTRUE);
00178       }
00179       if(ymin!= -1. && ymax!=-1.){
00180         SiPixelUtility::setDrawingOption(histo);
00181         l_min->SetLineColor(icol);
00182         l_max->SetLineColor(icol);
00183         if(var.find("SUM") != string::npos){      
00184           xmin = histo->GetXaxis()->GetXmin();
00185           xmax = histo->GetXaxis()->GetXmax(); 
00186           //cout<<"xmin="<<xmin<<" , xmax="<<xmax<<" , ymin="<<ymin<<" , ymax="<<ymax<<endl;
00187           l_min->SetX1(xmin);
00188           l_min->SetX2(xmax);
00189           l_min->SetY1(ymin);
00190           l_min->SetY2(ymin);
00191           l_min->Draw("same");
00192           l_max->SetX1(xmin);
00193           l_max->SetX2(xmax);
00194           l_max->SetY1(ymax);
00195           l_max->SetY2(ymax);
00196           l_max->Draw("same");
00197         }else{
00198           xmin = ymin;
00199           xmax = ymax;
00200           ymin = histo->GetYaxis()->GetBinLowEdge(1);
00201           ymax = histo->GetMaximum();
00202           //cout<<"xmin="<<xmin<<" , xmax="<<xmax<<" , ymin="<<ymin<<" , ymax="<<ymax<<endl;
00203           l_min->SetX1(xmin);
00204           l_min->SetX2(xmin);
00205           l_min->SetY1(ymin);
00206           l_min->SetY2(ymax);
00207           l_min->Draw("same");
00208           l_max->SetX1(xmax);
00209           l_max->SetX2(xmax);
00210           l_max->SetY1(ymin);
00211           l_max->SetY2(ymax);
00212           l_max->Draw("same");
00213         }
00214       }
00215       //setSubDetAxisDrawing((me->getName()),histo);
00216       //}
00217       paveOnCanvas->Draw("same");
00218     }
00219     if(((var.find("Barrel") != string::npos) && (var.find("SUM") != string::npos)) ||
00220        ((var.find("Endcap") != string::npos) && (var.find("SUM") != string::npos))) 
00221       setSubDetAxisDrawing((me->getName()),histo);
00222     
00223     canvas->Update();
00224     
00225     
00226     //cout<<"Calling fillNamedImageBuffer now!"<<endl;
00227     fillNamedImageBuffer(canvas, par.Path);
00228     canvas->Clear();
00229   } else {
00230     createDummyImage(par.Path);
00231   }
00232   delete canvas;
00233   //cout<<"... leaving SiPixelHistoPlotter::makePlot!"<<endl;
00234 }
00235 
00236 //
00237 // -- Get Named Image buffer
00238 //
00239 void SiPixelHistoPlotter::getNamedImageBuffer(const string& path, string& image) {
00240   map<string, string>::iterator cPos = namedPictureBuffer_.find(path);
00241   if (cPos != namedPictureBuffer_.end()) {
00242     image = cPos->second;
00243     if (namedPictureBuffer_.size() > 99 ) namedPictureBuffer_.erase(cPos);
00244   } else {
00245      cPos = namedPictureBuffer_.find("Dummy");
00246      image = cPos->second;
00247   }
00248 }
00249 
00250 
00255 void SiPixelHistoPlotter::fillNamedImageBuffer(TCanvas * c1, const std::string& name) 
00256 {
00257 //cout<<"Entering SiPixelHistoPlotter::fillNamedImageBuffer: "<<endl;
00258   //  DQMScope enter;
00259  // Now extract the image
00260   // 114 - stands for "no write on Close"
00261 //   cout << ACYellow << ACBold
00262 //        << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
00263 //        << ACPlain
00264 //        << "A canvas: "
00265 //        << c1->GetName() 
00266 //        << endl ;
00267   c1->Update();
00268   c1->Modified();  
00269   TImageDump imgdump("tmp.png", 114);
00270   c1->Paint();
00271 
00272 // get an internal image which will be automatically deleted
00273 // in the imgdump destructor
00274   TImage *image = imgdump.GetImage();
00275 
00276   if( image == NULL )
00277   {
00278    //cout << "No TImage found for "
00279 //      << name
00280    //   << endl ;
00281     return ;
00282   }
00283   //cout<<"found an image!"<<endl;
00284   char *buf;
00285   int sz = 0;
00286   image->GetImageBuffer(&buf, &sz);
00287 
00288   ostringstream local_str;
00289   for (int i = 0; i < sz; i++) local_str << buf[i];
00290   
00291 //  delete [] buf;
00292   ::free(buf); // buf is allocated via realloc() by a C language AfterStep library invoked by the
00293                // default (and so far only) TImage implementation in root, TASImage.
00294   
00295   // clear the first element map if # of entries > 30
00296   if (hasNamedImage(name)) namedPictureBuffer_.erase(name);
00297   //cout<<"filling namedPictureBuffer_["<<name<<"] now"<<endl;
00298   namedPictureBuffer_[name] = local_str.str();
00299   //if (namedPictureBuffer_[name].size() > 0) cout << "image created " << name << endl;
00300 //cout<<"... leaving SiPixelHistoPlotter::fillNamedImageBuffer!"<<endl;
00301 }
00302 
00303 //
00304 // -- Check if the image exists
00305 //
00306 bool SiPixelHistoPlotter::hasNamedImage(const string& name) {
00307   map<string, string>::const_iterator cPos = namedPictureBuffer_.find(name);
00308   if (cPos == namedPictureBuffer_.end()) { 
00309     return false;
00310   } else return true;
00311 }
00312 
00313 //
00314 // -- Create Dummy Image 
00315 //
00316 void SiPixelHistoPlotter::createDummyImage(const string & name) {
00317   string          line;
00318   ostringstream   local_str;
00319   // Read back the file line by line and temporarily store it in a stringstream
00320   ifstream * imagefile = new ifstream("images/EmptyPlot.png",ios::in);
00321   if(imagefile->is_open()) {
00322     while (getline( *imagefile, line )) {
00323       local_str << line << endl ;
00324     }
00325   }  
00326   namedPictureBuffer_.insert(pair<string, string>(name, local_str.str()));
00327 
00328   imagefile->close() ;
00329 }
00330 
00331 // -- Set Drawing Option
00332 //
00333 void SiPixelHistoPlotter::setDrawingOption(TH1* hist) {
00334   if (!hist) return;
00335 
00336   TAxis* xa = hist->GetXaxis();
00337   TAxis* ya = hist->GetYaxis();
00338 
00339   xa->SetTitleOffset(0.7);
00340   xa->SetTitleSize(0.05);
00341   xa->SetLabelSize(0.04);
00342 
00343   ya->SetTitleOffset(0.7);
00344   ya->SetTitleSize(0.05);
00345   ya->SetLabelSize(0.04);
00346 
00347 }
00348 
00349 //
00350 // -- create static plots
00351 // 
00352 void SiPixelHistoPlotter::createStaticPlot(MonitorElement* me, const string& file_name) {
00353   TH1* hist1 = me->getTH1();
00354   TCanvas* canvas  = new TCanvas("TKCanvas", "TKCanvas", 600, 400);
00355   if (hist1) {
00356     TText tTitle;
00357     tTitle.SetTextFont(64);
00358     tTitle.SetTextSizePixels(20);
00359     
00360     setDrawingOption(hist1);
00361     hist1->Draw();
00362     string name = hist1->GetName();
00363     if (me->getRefRootObject()) {
00364       TH1* hist1_ref = me->getRefTH1();
00365       if (hist1_ref) {
00366         hist1_ref->SetLineColor(4);
00367         hist1_ref->SetMarkerColor(4);
00368         if (name.find("SUM") != string::npos) hist1_ref->Draw("same");
00369         else hist1_ref->DrawNormalized("same", hist1->GetEntries());
00370       }
00371     }
00372   }
00373   canvas->Update();
00374   string command = "rm -f " + file_name;
00375   gSystem->Exec(command.c_str());
00376   canvas->Print(file_name.c_str(),"png");
00377   canvas->Clear();
00378   delete canvas;        
00379 }
00380 
00381 
00382 void SiPixelHistoPlotter::setLines(MonitorElement * me,
00383                                            string & meName, 
00384                                            double & ymin,
00385                                            double & ymax, 
00386                                            double & warning, 
00387                                            double & error, 
00388                                            double & channelFraction) {
00389 //cout<<"Entering SiPixelHistoPlotter::setLines for "<<meName<<endl;
00390    std::vector<QReport *> report;
00391    std::string colour;
00392 
00393    if (me->hasError()){
00394      colour="red";
00395      report= me->getQErrors();
00396    } else if( me->hasWarning()){ 
00397      colour="orange";
00398      report= me->getQWarnings();
00399    } else if(me->hasOtherReport()){
00400      colour="black";
00401      report= me->getQOthers();
00402    } else {
00403      colour="green";
00404    }
00405    for(std::vector<QReport *>::iterator itr=report.begin(); itr!=report.end();++itr ){
00406      std::string text= (*itr)->getMessage();
00407      //std::cout<<"ME: "<<meName<<" QTmessage:"<<text<<std::endl;
00408      int num1 = text.find_first_of(":")+1;
00409      int num2 = text.find_first_of("-",num1)+1;
00410      int num3 = text.find_first_of(" ",num2);
00411      std::stringstream dummy(text.substr(num1,num2-num1-1));
00412      dummy >> ymin; 
00413      std::stringstream dummy1(text.substr(num2,num3-num2));
00414      dummy1 >> ymax; 
00415      //std::cout<<",ymin="<<ymin<<",ymax="<<ymax<<std::endl;
00416      int num4 = text.find_last_of("=")+2;
00417      std::stringstream dummy2(text.substr(num4));
00418      dummy2 >> channelFraction;
00419      channelFraction = (1.- channelFraction)*100.;
00420      error = 25.;
00421      warning = 10.;
00422      //std::cout<<",warning="<<warning<<",error="<<error<<std::endl;
00423    }
00424           
00425 }
00426 
00427 //
00428 // -- Set Axis Drawing Option for slide show plots
00429 //
00430 void SiPixelHistoPlotter::setSubDetAxisDrawing(string detector, TH1F * histo) {
00431 
00432   histo->GetXaxis()->SetLabelColor(0);
00433 
00434   string ownName = "";
00435   if(detector.find("Barrel") != string::npos || detector.find("Endcap") != string::npos){ 
00436     if(detector.find("Barrel") != string::npos){
00437       ownName      = "Shell";
00438     }
00439     if(detector.find("Endcap") != string::npos){
00440       ownName = "HalfCylinder";
00441     }
00442     
00443     TText tt;
00444     tt.SetTextSize(0.04);
00445     string mI = ownName + "_mI"; tt.DrawTextNDC(0.12, 0.04, mI.c_str());
00446     string mO = ownName + "_mO"; tt.DrawTextNDC(0.32, 0.04, mO.c_str());
00447     string pI = ownName + "_pI"; tt.DrawTextNDC(0.52, 0.04, pI.c_str());
00448     string pO = ownName + "_pO"; tt.DrawTextNDC(0.72, 0.04, pO.c_str());
00449   }
00450   if(detector.find("Shell") != string::npos){
00451     ownName = "Layer";
00452     TText tt;
00453     tt.SetTextSize(0.04);
00454     string l_one   = ownName + "_1"; tt.DrawTextNDC(0.12, 0.03, l_one.c_str());
00455     string l_two   = ownName + "_2"; tt.DrawTextNDC(0.42, 0.03, l_two.c_str());
00456     string l_three = ownName + "_3"; tt.DrawTextNDC(0.72, 0.03, l_three.c_str());
00457   }
00458   if(detector.find("HalfCylinder") != string::npos){
00459     ownName = "Disk";
00460     TText tt;
00461     tt.SetTextSize(0.04);
00462     string d_one   = ownName + "_1"; tt.DrawTextNDC(0.12, 0.03, d_one.c_str());
00463     string d_two   = ownName + "_2"; tt.DrawTextNDC(0.62, 0.03, d_two.c_str());
00464   }
00465 }