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