CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
SiPixelHistoPlotter.cc
Go to the documentation of this file.
7 
8 
9 
10 #include "TText.h"
11 #include "TROOT.h"
12 #include "TPad.h"
13 #include "TSystem.h"
14 #include "TString.h"
15 #include "TImage.h"
16 #include "TPaveText.h"
17 #include "TImageDump.h"
18 #include "TAxis.h"
19 #include "TStyle.h"
20 #include "TPaveLabel.h"
21 #include "TH1F.h"
22 #include "TH2F.h"
23 #include "TProfile.h"
24 #include <iostream>
25 using namespace std;
26 //
27 // -- Constructor
28 //
30  edm::LogInfo("SiPixelHistoPlotter") <<
31  " Creating SiPixelHistoPlotter " << "\n" ;
32 }
33 //
34 // -- Destructor
35 //
37  edm::LogInfo("SiPixelHistoPlotter") <<
38  " Deleting SiPixelHistoPlotter " << "\n" ;
39  plotList_.clear();
40 
41 }
42 
43 //
44 // -- Set New
45 //
47  std::string& option,
48  int width,
49  int height) {
50 //cout<<"Entering SiPixelHistoPlotter::setNewPlot for "<<path<<endl;
51  PlotParameter local_par;
52  local_par.Path = path;
53  local_par.Option = option;
54  local_par.CWidth = width;
55  local_par.CHeight = height;
56  plotList_.push_back(local_par);
57 //cout<<"... leaving SiPixelHistoPlotter::setNewPlot!"<<endl;
58 }
59 
60 //
61 // -- Create Plots
62 //
64  string name = "Dummy";
65  if (!hasNamedImage(name)) createDummyImage(name);
66  //cout<<"HP::createPlots:PlotList size="<<plotList_.size()<<endl;
67  for (vector<PlotParameter>::iterator it = plotList_.begin();
68  it != plotList_.end(); it++) {
69  makePlot(bei, (*it));
70  }
71  plotList_.clear();
72 }
73 
74 //
75 // -- Draw Histograms
76 //
78 //cout<<"Entering SiPixelHistoPlotter::makePlot: "<<endl;
79  TCanvas * canvas = new TCanvas("PXCanvas", "PXCanvas", par.CWidth, par.CHeight);
80  gROOT->Reset();
81  gStyle->SetPalette(1,0);
82  gStyle->SetOptStat(111110);
83  canvas->SetBorderMode(0);
84  canvas->SetFillColor(0);
85 
86  TPaveText * paveOnCanvas = new TPaveText(0.57,0.79,0.77,0.99,"NDCtr");
87  paveOnCanvas->SetFillColor(0);
88 
89  //std::cout<<"::makePlot:Trying to plot "<<par.Path<<std::endl;
90  MonitorElement * me = bei->get(par.Path);
91  if (me) {
92 
93  string opt = "";
94  if (me->kind() == MonitorElement::DQM_KIND_TH2F)
95  { opt = "COLZ"; gStyle->SetOptStat(10); }
96  me->getRootObject()->Draw(opt.c_str());
97  int istat = SiPixelUtility::getStatus(me);
98 
99  string dopt = par.Option;
100  string tag;
101  int icol;
102  SiPixelUtility::getStatusColor(istat, icol, tag);
103 
104  TH1F* histo = dynamic_cast<TH1F*>(me->getRootObject());
105  string var = (me->getName()).substr((me->getName()).find_last_of("/")+1);
106  if (histo) {
107  //setDrawingOption(histo);
108  //histo->Draw(opt.c_str());
109  string name = histo->GetName();
110  if (me->getRefRootObject()) {
111  TH1* histo_ref = me->getRefTH1();
112  if (histo_ref) {
113  histo_ref->SetLineColor(4);
114  histo_ref->SetMarkerColor(4);
115  if (name.find("SUM") != string::npos) histo_ref->Draw("same");
116  else histo_ref->DrawNormalized("same", histo->GetEntries());
117  }
118  }
119  }
120  canvas->Update();
121 
122  //TH1* histo = me->getTH1();
123  //TH1F* tproject = 0;
124  //if (dopt == "projection") {
125  //getProjection(me, tproject);
126  //if (tproject) tproject->Draw();
127  //else histo->Draw();
128  //} else {
129  // histo->Draw();
130  //}
131  //TText tTitle;
132  //tTitle.SetTextFont(64);
133  //tTitle.SetTextSizePixels(20);
134  //tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
135 
136  //if (icol != 1) {
137  // TText tt;
138  // tt.SetTextSize(0.12);
139  // tt.SetTextColor(icol);
140  // tt.DrawTextNDC(0.5, 0.5, tag.c_str());
141  //}
142 
143  TLine* l_min = new TLine();
144  TLine* l_max = new TLine();
145  l_min->SetLineWidth(4);
146  l_max->SetLineWidth(4);
147 
148 
149  if(istat!=0){
150  TText* statusOnCanvas = paveOnCanvas->AddText(tag.c_str());
151  statusOnCanvas->SetTextSize(0.08);
152  statusOnCanvas->SetTextFont(112);
153  statusOnCanvas->SetNDC(kTRUE);
154  statusOnCanvas->SetTextColor(icol);
155 
156  double ymax = -1.;
157  double ymin = -1.;
158  double xmax = -1.;
159  double xmin = -1.;
160  double warning = -1.;
161  double error = -1.;
162  double channelFraction = -1.;
163  //if(var.find("SUM") != string::npos){
164  //cout << "ME name: " << var << endl;
165  setLines(me,var,ymin,ymax,warning,error,channelFraction);
166  //cout << "ymin: " << ymin << " ymax: " << ymax << " warning: " << warning << " error: " << error << " channelFraction: " << channelFraction << endl;
167 
168  if(istat!=dqm::qstatus::STATUS_OK){
169  string textMessage = "fraction of channels failing:";
170  TText* messageOnCanvas = paveOnCanvas->AddText(textMessage.c_str());
171  messageOnCanvas->SetTextSize(0.03);
172  messageOnCanvas->SetNDC(kTRUE);
173  char text[10];
174  sprintf(text,"%.2f %%",channelFraction);
175  messageOnCanvas = paveOnCanvas->AddText(text);
176  messageOnCanvas->SetTextSize(0.035);
177  messageOnCanvas->SetNDC(kTRUE);
178  }
179  if(ymin!= -1. && ymax!=-1.){
181  l_min->SetLineColor(icol);
182  l_max->SetLineColor(icol);
183  if(var.find("SUM") != string::npos){
184  xmin = histo->GetXaxis()->GetXmin();
185  xmax = histo->GetXaxis()->GetXmax();
186  //cout<<"xmin="<<xmin<<" , xmax="<<xmax<<" , ymin="<<ymin<<" , ymax="<<ymax<<endl;
187  l_min->SetX1(xmin);
188  l_min->SetX2(xmax);
189  l_min->SetY1(ymin);
190  l_min->SetY2(ymin);
191  l_min->Draw("same");
192  l_max->SetX1(xmin);
193  l_max->SetX2(xmax);
194  l_max->SetY1(ymax);
195  l_max->SetY2(ymax);
196  l_max->Draw("same");
197  }else{
198  xmin = ymin;
199  xmax = ymax;
200  ymin = histo->GetYaxis()->GetBinLowEdge(1);
201  ymax = histo->GetMaximum();
202  //cout<<"xmin="<<xmin<<" , xmax="<<xmax<<" , ymin="<<ymin<<" , ymax="<<ymax<<endl;
203  l_min->SetX1(xmin);
204  l_min->SetX2(xmin);
205  l_min->SetY1(ymin);
206  l_min->SetY2(ymax);
207  l_min->Draw("same");
208  l_max->SetX1(xmax);
209  l_max->SetX2(xmax);
210  l_max->SetY1(ymin);
211  l_max->SetY2(ymax);
212  l_max->Draw("same");
213  }
214  }
215  //setSubDetAxisDrawing((me->getName()),histo);
216  //}
217  paveOnCanvas->Draw("same");
218  }
219  if(((var.find("Barrel") != string::npos) && (var.find("SUM") != string::npos)) ||
220  ((var.find("Endcap") != string::npos) && (var.find("SUM") != string::npos)))
221  setSubDetAxisDrawing((me->getName()),histo);
222 
223  canvas->Update();
224 
225 
226  //cout<<"Calling fillNamedImageBuffer now!"<<endl;
227  fillNamedImageBuffer(canvas, par.Path);
228  canvas->Clear();
229  } else {
230  createDummyImage(par.Path);
231  }
232  delete canvas;
233  //cout<<"... leaving SiPixelHistoPlotter::makePlot!"<<endl;
234 }
235 
236 //
237 // -- Get Named Image buffer
238 //
239 void SiPixelHistoPlotter::getNamedImageBuffer(const string& path, string& image) {
240  map<string, string>::iterator cPos = namedPictureBuffer_.find(path);
241  if (cPos != namedPictureBuffer_.end()) {
242  image = cPos->second;
243  if (namedPictureBuffer_.size() > 99 ) namedPictureBuffer_.erase(cPos);
244  } else {
245  cPos = namedPictureBuffer_.find("Dummy");
246  image = cPos->second;
247  }
248 }
249 
250 
256 {
257 //cout<<"Entering SiPixelHistoPlotter::fillNamedImageBuffer: "<<endl;
258  // DQMScope enter;
259  // Now extract the image
260  // 114 - stands for "no write on Close"
261 // cout << ACYellow << ACBold
262 // << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
263 // << ACPlain
264 // << "A canvas: "
265 // << c1->GetName()
266 // << endl ;
267  c1->Update();
268  c1->Modified();
269  TImageDump imgdump("tmp.png", 114);
270  c1->Paint();
271 
272 // get an internal image which will be automatically deleted
273 // in the imgdump destructor
274  TImage *image = imgdump.GetImage();
275 
276  if( image == NULL )
277  {
278  //cout << "No TImage found for "
279 // << name
280  // << endl ;
281  return ;
282  }
283  //cout<<"found an image!"<<endl;
284  char *buf;
285  int sz = 0;
286  image->GetImageBuffer(&buf, &sz);
287 
288  ostringstream local_str;
289  for (int i = 0; i < sz; i++) local_str << buf[i];
290 
291 // delete [] buf;
292  ::free(buf); // buf is allocated via realloc() by a C language AfterStep library invoked by the
293  // default (and so far only) TImage implementation in root, TASImage.
294 
295  // clear the first element map if # of entries > 30
296  if (hasNamedImage(name)) namedPictureBuffer_.erase(name);
297  //cout<<"filling namedPictureBuffer_["<<name<<"] now"<<endl;
298  namedPictureBuffer_[name] = local_str.str();
299  //if (namedPictureBuffer_[name].size() > 0) cout << "image created " << name << endl;
300 //cout<<"... leaving SiPixelHistoPlotter::fillNamedImageBuffer!"<<endl;
301 }
302 
303 //
304 // -- Check if the image exists
305 //
307  map<string, string>::const_iterator cPos = namedPictureBuffer_.find(name);
308  if (cPos == namedPictureBuffer_.end()) {
309  return false;
310  } else return true;
311 }
312 
313 //
314 // -- Create Dummy Image
315 //
317  string line;
318  ostringstream local_str;
319  // Read back the file line by line and temporarily store it in a stringstream
320  ifstream * imagefile = new ifstream("images/EmptyPlot.png",ios::in);
321  if(imagefile->is_open()) {
322  while (getline( *imagefile, line )) {
323  local_str << line << endl ;
324  }
325  }
326  namedPictureBuffer_.insert(pair<string, string>(name, local_str.str()));
327 
328  imagefile->close() ;
329 }
330 
331 // -- Set Drawing Option
332 //
334  if (!hist) return;
335 
336  TAxis* xa = hist->GetXaxis();
337  TAxis* ya = hist->GetYaxis();
338 
339  xa->SetTitleOffset(0.7);
340  xa->SetTitleSize(0.05);
341  xa->SetLabelSize(0.04);
342 
343  ya->SetTitleOffset(0.7);
344  ya->SetTitleSize(0.05);
345  ya->SetLabelSize(0.04);
346 
347 }
348 
349 //
350 // -- create static plots
351 //
353  TH1* hist1 = me->getTH1();
354  TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", 600, 400);
355  if (hist1) {
356  TText tTitle;
357  tTitle.SetTextFont(64);
358  tTitle.SetTextSizePixels(20);
359 
360  setDrawingOption(hist1);
361  hist1->Draw();
362  string name = hist1->GetName();
363  if (me->getRefRootObject()) {
364  TH1* hist1_ref = me->getRefTH1();
365  if (hist1_ref) {
366  hist1_ref->SetLineColor(4);
367  hist1_ref->SetMarkerColor(4);
368  if (name.find("SUM") != string::npos) hist1_ref->Draw("same");
369  else hist1_ref->DrawNormalized("same", hist1->GetEntries());
370  }
371  }
372  }
373  canvas->Update();
374  string command = "rm -f " + file_name;
375  gSystem->Exec(command.c_str());
376  canvas->Print(file_name.c_str(),"png");
377  canvas->Clear();
378  delete canvas;
379 }
380 
381 
383  string & meName,
384  double & ymin,
385  double & ymax,
386  double & warning,
387  double & error,
388  double & channelFraction) {
389 //cout<<"Entering SiPixelHistoPlotter::setLines for "<<meName<<endl;
390  std::vector<QReport *> report;
391  std::string colour;
392 
393  if (me->hasError()){
394  colour="red";
395  report= me->getQErrors();
396  } else if( me->hasWarning()){
397  colour="orange";
398  report= me->getQWarnings();
399  } else if(me->hasOtherReport()){
400  colour="black";
401  report= me->getQOthers();
402  } else {
403  colour="green";
404  }
405  for(std::vector<QReport *>::iterator itr=report.begin(); itr!=report.end();++itr ){
406  std::string text= (*itr)->getMessage();
407  //std::cout<<"ME: "<<meName<<" QTmessage:"<<text<<std::endl;
408  int num1 = text.find_first_of(":")+1;
409  int num2 = text.find_first_of("-",num1)+1;
410  int num3 = text.find_first_of(" ",num2);
411  std::stringstream dummy(text.substr(num1,num2-num1-1));
412  dummy >> ymin;
413  std::stringstream dummy1(text.substr(num2,num3-num2));
414  dummy1 >> ymax;
415  //std::cout<<",ymin="<<ymin<<",ymax="<<ymax<<std::endl;
416  int num4 = text.find_last_of("=")+2;
417  std::stringstream dummy2(text.substr(num4));
418  dummy2 >> channelFraction;
419  channelFraction = (1.- channelFraction)*100.;
420  error = 25.;
421  warning = 10.;
422  //std::cout<<",warning="<<warning<<",error="<<error<<std::endl;
423  }
424 
425 }
426 
427 //
428 // -- Set Axis Drawing Option for slide show plots
429 //
430 void SiPixelHistoPlotter::setSubDetAxisDrawing(string detector, TH1F * histo) {
431 
432  histo->GetXaxis()->SetLabelColor(0);
433 
434  string ownName = "";
435  if(detector.find("Barrel") != string::npos || detector.find("Endcap") != string::npos){
436  if(detector.find("Barrel") != string::npos){
437  ownName = "Shell";
438  }
439  if(detector.find("Endcap") != string::npos){
440  ownName = "HalfCylinder";
441  }
442 
443  TText tt;
444  tt.SetTextSize(0.04);
445  string mI = ownName + "_mI"; tt.DrawTextNDC(0.12, 0.04, mI.c_str());
446  string mO = ownName + "_mO"; tt.DrawTextNDC(0.32, 0.04, mO.c_str());
447  string pI = ownName + "_pI"; tt.DrawTextNDC(0.52, 0.04, pI.c_str());
448  string pO = ownName + "_pO"; tt.DrawTextNDC(0.72, 0.04, pO.c_str());
449  }
450  if(detector.find("Shell") != string::npos){
451  ownName = "Layer";
452  TText tt;
453  tt.SetTextSize(0.04);
454  string l_one = ownName + "_1"; tt.DrawTextNDC(0.12, 0.03, l_one.c_str());
455  string l_two = ownName + "_2"; tt.DrawTextNDC(0.42, 0.03, l_two.c_str());
456  string l_three = ownName + "_3"; tt.DrawTextNDC(0.72, 0.03, l_three.c_str());
457  }
458  if(detector.find("HalfCylinder") != string::npos){
459  ownName = "Disk";
460  TText tt;
461  tt.SetTextSize(0.04);
462  string d_one = ownName + "_1"; tt.DrawTextNDC(0.12, 0.03, d_one.c_str());
463  string d_two = ownName + "_2"; tt.DrawTextNDC(0.62, 0.03, d_two.c_str());
464  }
465 }
bool hasNamedImage(const std::string &name)
const std::string & getName(void) const
get name of ME
int i
Definition: DBlmapReader.cc:9
TH1 * getRefTH1(void) const
void makePlot(DQMStore *bei, const PlotParameter &par)
void setSubDetAxisDrawing(std::string detector, TH1F *histo)
bool hasError(void) const
true if at least of one of the quality tests returned an error
#define NULL
Definition: scimark2.h:8
void setLines(MonitorElement *me, std::string &meName, double &ymin, double &ymax, double &warning, double &error, double &channelFraction)
void setDrawingOption(TH1 *hist)
void getNamedImageBuffer(const std::string &path, std::string &image)
void createDummyImage(const std::string &name)
def canvas
Definition: svgfig.py:481
std::vector< QReport * > getQErrors(void) const
get errors from last set of quality tests
tuple report
Definition: zeeHLT_cff.py:9
static void setDrawingOption(TH1 *hist, float xlow=-1., float xhigh=-1.)
static int getStatus(MonitorElement *me)
bool hasWarning(void) const
true if at least of one of the quality tests returned a warning
static void getStatusColor(int status, int &rval, int &gval, int &bval)
TH1 * getTH1(void) const
tuple text
Definition: runonSM.py:42
Kind kind(void) const
Get the type of the monitor element.
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. &quot;my/long/dir/my_histo&quot;)
Definition: DQMStore.cc:1473
void fillNamedImageBuffer(TCanvas *c1, const std::string &name)
(Documentation under construction).
void createPlots(DQMStore *bei)
TObject * getRootObject(void) const
std::vector< QReport * > getQOthers(void) const
void createStaticPlot(MonitorElement *me, const std::string &file_name)
bool hasOtherReport(void) const
true if at least of one of the tests returned some other (non-ok) status
std::vector< QReport * > getQWarnings(void) const
get warnings from last set of quality tests
static const int STATUS_OK
void setNewPlot(std::string &path, std::string &option, int width, int height)
TObject * getRefRootObject(void) const