CMS 3D CMS Logo

SiStripHistoPlotter.cc
Go to the documentation of this file.
7 
8 #include "TText.h"
9 #include "TROOT.h"
10 #include "TPad.h"
11 #include "TSystem.h"
12 #include "TString.h"
13 #include "TImage.h"
14 #include "TPaveText.h"
15 #include "TImageDump.h"
16 #include "TAxis.h"
17 #include "TStyle.h"
18 #include "TPaveLabel.h"
19 #include "TH1F.h"
20 #include "TH2F.h"
21 #include "TProfile.h"
22 #include <iostream>
23 //
24 // -- Constructor
25 //
27  edm::LogInfo("SiStripHistoPlotter") <<
28  " Creating SiStripHistoPlotter " << "\n" ;
29 }
30 //
31 // -- Destructor
32 //
34  edm::LogInfo("SiStripHistoPlotter") <<
35  " Deleting SiStripHistoPlotter " << "\n" ;
36  plotList_.clear();
37  condDBPlotList_.clear();
38 
39 }
40 //
41 // -- Set New Plot
42 //
44  std::string name = "Dummy";
45  if (!hasNamedImage(name)) createDummyImage(name);
46  PlotParameter local_par;
47  local_par.Path = path;
48  local_par.Option = option;
49  local_par.CWidth = width;
50  local_par.CHeight = height;
51  plotList_.push_back(local_par);
52 }
53 //
54 // -- Create Plots
55 //
57  if (plotList_.empty()) return;
58  std::string name = "Dummy";
59  if (!hasNamedImage(name)) createDummyImage(name);
60  for (std::vector<PlotParameter>::iterator it = plotList_.begin();
61  it != plotList_.end(); it++) {
62  makePlot(dqm_store, (*it));
63  }
64  plotList_.clear();
65 }
66 //
67 // -- Draw Histograms
68 //
70  TCanvas * canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
71 
72  MonitorElement * me = dqm_store->get(par.Path);
73  if (me) {
74 
75  int istat = SiStripUtility::getMEStatus(me);
76 
77  std::string dopt = par.Option;
79  int icol;
80  SiStripUtility::getMEStatusColor(istat, icol, tag);
81  if (me->kind() == MonitorElement::DQM_KIND_TH1F ||
85  TH1* histo = me->getTH1();
86  TH1F* tproject = nullptr;
87  if (dopt == "projection") {
88  getProjection(me, tproject);
89  if (tproject) tproject->Draw();
90  else histo->Draw();
91  } else {
92  dopt = "";
93  std::string name = histo->GetName();
95  dopt = "colz";
96  histo->SetStats( kFALSE );
97  } else {
98  if (name.find("Summary_Mean") != std::string::npos) {
99  histo->SetStats( kFALSE );
100  } else {
101  histo->SetFillColor(1);
102  }
103  }
104  histo->Draw(dopt.c_str());
105  }
106  }
107  TText tTitle;
108  tTitle.SetTextFont(64);
109  tTitle.SetTextSizePixels(20);
110  // tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
111 
112  if (icol != 1) {
113  TText tt;
114  tt.SetTextSize(0.12);
115  tt.SetTextColor(icol);
116  tt.DrawTextNDC(0.5, 0.5, tag.c_str());
117  }
118  fillNamedImageBuffer(canvas, par.Path);
119  canvas->Clear();
120  }
121  delete canvas;
122 }
123 //
124 // -- Get Named Image buffer
125 //
127  std::map<std::string, std::string>::iterator cPos;
128  if (path == "dummy_path") {
129  std::cout << " Sending Dummy Image for : "
130  << path << std::endl;
131  cPos = namedPictureBuffer_.find("Dummy");
132  image = cPos->second;
133  } else {
134  cPos = namedPictureBuffer_.find(path);
135  if (cPos != namedPictureBuffer_.end()) {
136  image = cPos->second;
137  if (namedPictureBuffer_.size() > 99 ) namedPictureBuffer_.erase(cPos);
138  } else {
139  std::cout << " Sending Dummy Image for : "
140  << path << std::endl;
141  cPos = namedPictureBuffer_.find("Dummy");
142  image = cPos->second;
143  }
144  }
145 }
151 {
152  // DQMScope enter;
153  // Now extract the image
154  // 114 - stands for "no write on Close"
155 // std::cout << ACYellow << ACBold
156 // << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
157 // << ACPlain
158 // << "A canvas: "
159 // << c1->GetName()
160 // << std::endl ;
161  c1->Update();
162  c1->Modified();
163  TImageDump imgdump("tmp.png", 114);
164  c1->Paint();
165 
166 // get an internal image which will be automatically deleted
167 // in the imgdump destructor
168  TImage *image = imgdump.GetImage();
169 
170  if( image == nullptr )
171  {
172  std::cout << "No TImage found for "
173  << name
174  << std::endl ;
175  return ;
176  }
177  char *buf;
178  int sz = 0;
179  image->GetImageBuffer(&buf, &sz);
180 
181  std::ostringstream local_str;
182  for (int i = 0; i < sz; i++) local_str << buf[i];
183 
184 // delete [] buf;
185  ::free(buf); // buf is allocated via realloc() by a C language AfterStep library invoked by the
186  // default (and so far only) TImage implementation in root, TASImage.
187 
188  // clear the first element map if # of entries > 30
189  if (hasNamedImage(name)) namedPictureBuffer_.erase(name);
190  namedPictureBuffer_[name] = local_str.str();
191  // if (namedPictureBuffer_[name].size() > 0) std::cout << "image created " << name << std::endl;
192 }
193 //
194 // -- Check if the image exists
195 //
197  std::map<std::string, std::string>::const_iterator cPos = namedPictureBuffer_.find(name);
198  if (cPos == namedPictureBuffer_.end()) {
199  return false;
200  } else return true;
201 }
202 //
203 // -- Create Dummy Image
204 //
206  std::string image;
207  getDummyImage(image);
208  namedPictureBuffer_.insert(std::pair<std::string, std::string>(name, image));
209 }
210 //
211 // -- Get Image reading a disk resident image
212 //
215  std::ostringstream local_str;
216  // Read back the file line by line and temporarily store it in a stringstream
217  std::string localPath = std::string("DQM/TrackerCommon/test/images/EmptyPlot.png");
218  std::ifstream * imagefile = new std::ifstream((edm::FileInPath(localPath).fullPath()).c_str(),std::ios::in);
219  if(imagefile->is_open()) {
220  while (getline( *imagefile, line )) {
221  local_str << line << std::endl ;
222  }
223  }
224  imagefile->close();
225  image = local_str.str();
226 }
227 // -- Set Drawing Option
228 //
230  if (!hist) return;
231 
232  TAxis* xa = hist->GetXaxis();
233  TAxis* ya = hist->GetYaxis();
234 
235  xa->SetTitleOffset(0.7);
236  xa->SetTitleSize(0.05);
237  xa->SetLabelSize(0.04);
238 
239  ya->SetTitleOffset(0.7);
240  ya->SetTitleSize(0.05);
241  ya->SetLabelSize(0.04);
242 
243 }
244 // -- Get Projection Histogram
245 //
247 
248  std::string ptit = me->getTitle();
249  ptit += "-Yprojection";
250 
251  if (me->kind() == MonitorElement::DQM_KIND_TH2F) {
252  TH2F* hist2 = me->getTH2F();
253  tp = new TH1F(ptit.c_str(),ptit.c_str(),hist2->GetNbinsY(),
254  hist2->GetYaxis()->GetXmin(),hist2->GetYaxis()->GetXmax());
255  tp->GetXaxis()->SetTitle(ptit.c_str());
256  for (int j = 1; j < hist2->GetNbinsY()+1; j++) {
257  float tot_count = 0.0;
258  for (int i = 1; i < hist2->GetNbinsX()+1; i++) {
259  tot_count += hist2->GetBinContent(i,j);
260  }
261  tp->SetBinContent(j, tot_count);
262  }
263  } else if (me->kind() == MonitorElement::DQM_KIND_TPROFILE) {
264  TProfile* prof = me->getTProfile();
265  tp = new TH1F(ptit.c_str(),ptit.c_str(),100,
266  0.0,prof->GetMaximum()*1.2);
267  tp->GetXaxis()->SetTitle(ptit.c_str());
268  for (int i = 1; i < prof->GetNbinsX()+1; i++) {
269  tp->Fill(prof->GetBinContent(i));
270  }
271  } else if (me->kind() == MonitorElement::DQM_KIND_TH1F) {
272  TH1F* hist1 = me->getTH1F();
273  tp = new TH1F(ptit.c_str(),ptit.c_str(),100,
274  0.0,hist1->GetMaximum()*1.2);
275  tp->GetXaxis()->SetTitle(ptit.c_str());
276  for (int i = 1; i < hist1->GetNbinsX()+1; i++) {
277  tp->Fill(hist1->GetBinContent(i));
278  }
279  }
280 }
281 //
282 // -- create static plots
283 //
285  TH1* hist1 = me->getTH1();
286  TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", 600, 400);
287  if (hist1) {
288  TText tTitle;
289  tTitle.SetTextFont(64);
290  tTitle.SetTextSizePixels(20);
291 
292  setDrawingOption(hist1);
293  hist1->Draw();
294  std::string name = hist1->GetName();
295  if (me->getRefRootObject()) {
296  TH1* hist1_ref = me->getRefTH1();
297  if (hist1_ref) {
298  hist1_ref->SetLineColor(3);
299  hist1_ref->SetMarkerColor(3);
300  if (name.find("Summary") != std::string::npos) hist1_ref->Draw("same");
301  else hist1_ref->DrawNormalized("same", hist1->GetEntries());
302  }
303  }
304  }
305  canvas->Update();
306  std::string command = "rm -f " + file_name;
307  gSystem->Exec(command.c_str());
308  canvas->Print(file_name.c_str(),"png");
309  canvas->Clear();
310  delete canvas;
311 }
312 //
313 // -- Set New CondDB Plot
314 //
316  PlotParameter local_par;
317  local_par.Path = path;
318  local_par.Option = option;
319  local_par.CWidth = width;
320  local_par.CHeight = height;
321  condDBPlotList_.push_back(local_par);
322 }
323 //
324 // -- Create CondDB Plots
325 //
327  if (condDBPlotList_.empty()) return;
328  std::string name = "Dummy";
329  if (!hasNamedImage(name)) createDummyImage(name);
330 
331  for (std::vector<PlotParameter>::iterator it = condDBPlotList_.begin();
332  it != condDBPlotList_.end(); it++) {
333  makeCondDBPlots(dqm_store, (*it));
334  }
335  condDBPlotList_.clear();
336 }
337 //
338 // -- Draw CondDB Histograms
339 //
341  TCanvas * canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
342 
343  std::vector<std::string> htypes;
344  std::string option = par.Option;
345  SiStripUtility::split(option, htypes, ",");
346 
348  std::vector<MonitorElement*> all_mes = dqm_store->getContents(par.Path);
349 
350  for (std::vector<std::string>::const_iterator ih = htypes.begin();
351  ih!= htypes.end(); ih++) {
352  std::string type = (*ih);
353  if (type.empty()) continue;
354  std::string tag = par.Path + "/";
355  for (std::vector<MonitorElement *>::const_iterator it = all_mes.begin();
356  it!= all_mes.end(); it++) {
357  MonitorElement * me = (*it);
358  if (!me) continue;
359  std::string hname = me->getName();
360  if (hname.find(type) != std::string::npos) {
361  TH1* histo = me->getTH1();
362  histo->Draw();
363  tag += type;
364  fillNamedImageBuffer(canvas, tag);
365  canvas->Clear();
366  }
367  }
368  }
369  delete canvas;
370 }
std::map< std::string, std::string > namedPictureBuffer_
TProfile * getTProfile() const
type
Definition: HCALResponse.h:21
bool hasNamedImage(const std::string &name)
void createPlots(DQMStore *dqm_store)
TH1F * getTH1F() const
TH1 * getTH1() const
void setDrawingOption(TH1 *hist)
const std::string & getName() const
get name of ME
void setNewCondDBPlot(std::string &path, std::string &option, int width, int height)
TH1 * getRefTH1() const
TObject * getRefRootObject() const
static void split(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
return((rh^lh)&mask)
void createStaticPlot(MonitorElement *me, const std::string &file_name)
std::vector< PlotParameter > condDBPlotList_
std::string getTitle() const
get MonitorElement title
void getNamedImageBuffer(const std::string &path, std::string &image)
void createCondDBPlots(DQMStore *dqm_store)
MonitorElement * get(const std::string &path) const
get ME from full pathname (e.g. "my/long/dir/my_histo")
Definition: DQMStore.cc:1610
std::vector< MonitorElement * > getContents(const std::string &path) const
Definition: DQMStore.cc:1638
TH2F * getTH2F() const
void makeCondDBPlots(DQMStore *dqm_store, const PlotParameter &par)
static void getMEStatusColor(int status, int &rval, int &gval, int &bval)
static int getMEStatus(MonitorElement *me)
list command
Definition: mps_check.py:23
def canvas(sub, attr)
Definition: svgfig.py:481
void getProjection(MonitorElement *me, TH1F *tp)
void setNewPlot(std::string &path, std::string &option, int width, int height)
void fillNamedImageBuffer(TCanvas *c1, const std::string &name)
(Documentation under construction).
void createDummyImage(const std::string &name)
std::vector< PlotParameter > plotList_
void getDummyImage(std::string &image)
Kind kind() const
Get the type of the monitor element.
void makePlot(DQMStore *dqm_store, const PlotParameter &par)