CMS 3D CMS Logo

SiStripHistoPlotter.cc
Go to the documentation of this file.
6 
7 #include "TText.h"
8 #include "TROOT.h"
9 #include "TPad.h"
10 #include "TSystem.h"
11 #include "TString.h"
12 #include "TImage.h"
13 #include "TPaveText.h"
14 #include "TImageDump.h"
15 #include "TAxis.h"
16 #include "TStyle.h"
17 #include "TPaveLabel.h"
18 #include "TH1F.h"
19 #include "TH2F.h"
20 #include "TProfile.h"
21 #include <iostream>
22 //
23 // -- Constructor
24 //
26  edm::LogInfo("SiStripHistoPlotter") << " Creating SiStripHistoPlotter "
27  << "\n";
28 }
29 //
30 // -- Destructor
31 //
33  edm::LogInfo("SiStripHistoPlotter") << " Deleting SiStripHistoPlotter "
34  << "\n";
35 }
36 //
37 // -- Set New Plot
38 //
40  std::string const& option,
41  int const width,
42  int const height) {
43  std::string name = "Dummy";
44  if (!hasNamedImage(name))
46  PlotParameter local_par{path, option, width, height};
47  plotList_.push_back(std::move(local_par));
48 }
49 //
50 // -- Create Plots
51 //
53  if (plotList_.empty())
54  return;
55  std::string name = "Dummy";
56  if (!hasNamedImage(name))
58  for (auto const& par : plotList_) {
59  makePlot(dqm_store, par);
60  }
61  plotList_.clear();
62 }
63 //
64 // -- Draw Histograms
65 //
66 void SiStripHistoPlotter::makePlot(DQMStore const* dqm_store, const PlotParameter& par) {
67  TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
68 
69  MonitorElement* me = dqm_store->get(par.Path);
70  if (me) {
71  int istat = SiStripUtility::getMEStatus(me);
72 
73  std::string dopt = par.Option;
75  int icol;
77  if (me->kind() == MonitorElement::Kind::TH1F || me->kind() == MonitorElement::Kind::TH2F ||
79  TH1* histo = me->getTH1();
80  TH1F* tproject = nullptr;
81  if (dopt == "projection") {
82  getProjection(me, tproject);
83  if (tproject)
84  tproject->Draw();
85  else
86  histo->Draw();
87  } else {
88  dopt = "";
89  std::string name = histo->GetName();
90  if (me->kind() == MonitorElement::Kind::TPROFILE2D) {
91  dopt = "colz";
92  histo->SetStats(kFALSE);
93  } else {
94  if (name.find("Summary_Mean") != std::string::npos) {
95  histo->SetStats(kFALSE);
96  } else {
97  histo->SetFillColor(1);
98  }
99  }
100  histo->Draw(dopt.c_str());
101  }
102  }
103  TText tTitle;
104  tTitle.SetTextFont(64);
105  tTitle.SetTextSizePixels(20);
106  // tTitle.DrawTextNDC(0.1, 0.92, histo->GetName());
107 
108  if (icol != 1) {
109  TText tt;
110  tt.SetTextSize(0.12);
111  tt.SetTextColor(icol);
112  tt.DrawTextNDC(0.5, 0.5, tag.c_str());
113  }
115  canvas->Clear();
116  }
117  delete canvas;
118 }
119 //
120 // -- Get Named Image buffer
121 //
123  std::map<std::string, std::string>::iterator cPos;
124  if (path == "dummy_path") {
125  std::cout << " Sending Dummy Image for : " << path << std::endl;
126  cPos = namedPictureBuffer_.find("Dummy");
127  image = cPos->second;
128  } else {
129  cPos = namedPictureBuffer_.find(path);
130  if (cPos != namedPictureBuffer_.end()) {
131  image = cPos->second;
132  if (namedPictureBuffer_.size() > 99)
133  namedPictureBuffer_.erase(cPos);
134  } else {
135  std::cout << " Sending Dummy Image for : " << path << std::endl;
136  cPos = namedPictureBuffer_.find("Dummy");
137  image = cPos->second;
138  }
139  }
140 }
146  // DQMScope enter;
147  // Now extract the image
148  // 114 - stands for "no write on Close"
149  // std::cout << ACYellow << ACBold
150  // << "[SiPixelInformationExtractor::fillNamedImageBuffer()] "
151  // << ACPlain
152  // << "A canvas: "
153  // << c1->GetName()
154  // << std::endl ;
155  c1->Update();
156  c1->Modified();
157  TImageDump imgdump("tmp.png", 114);
158  c1->Paint();
159 
160  // get an internal image which will be automatically deleted
161  // in the imgdump destructor
162  TImage* image = imgdump.GetImage();
163 
164  if (image == nullptr) {
165  std::cout << "No TImage found for " << name << std::endl;
166  return;
167  }
168  char* buf;
169  int sz = 0;
170  image->GetImageBuffer(&buf, &sz);
171 
172  std::ostringstream local_str;
173  for (int i = 0; i < sz; i++)
174  local_str << buf[i];
175 
176  // delete [] buf;
177  ::free(buf); // buf is allocated via realloc() by a C language AfterStep library invoked by the
178  // default (and so far only) TImage implementation in root, TASImage.
179 
180  // clear the first element map if # of entries > 30
181  if (hasNamedImage(name))
182  namedPictureBuffer_.erase(name);
183  namedPictureBuffer_[name] = local_str.str();
184  // if (namedPictureBuffer_[name].size() > 0) std::cout << "image created " << name << std::endl;
185 }
186 //
187 // -- Check if the image exists
188 //
190  std::map<std::string, std::string>::const_iterator cPos = namedPictureBuffer_.find(name);
191  if (cPos == namedPictureBuffer_.end()) {
192  return false;
193  } else
194  return true;
195 }
196 //
197 // -- Create Dummy Image
198 //
200  std::string image;
201  getDummyImage(image);
202  namedPictureBuffer_.insert(std::pair<std::string, std::string>(name, image));
203 }
204 //
205 // -- Get Image reading a disk resident image
206 //
209  std::ostringstream local_str;
210  // Read back the file line by line and temporarily store it in a stringstream
211  std::string localPath = std::string("DQM/TrackerCommon/test/images/EmptyPlot.png");
212  std::ifstream* imagefile = new std::ifstream((edm::FileInPath(localPath).fullPath()).c_str(), std::ios::in);
213  if (imagefile->is_open()) {
214  while (getline(*imagefile, line)) {
215  local_str << line << std::endl;
216  }
217  }
218  imagefile->close();
219  image = local_str.str();
220 }
221 // -- Set Drawing Option
222 //
224  if (!hist)
225  return;
226 
227  TAxis* xa = hist->GetXaxis();
228  TAxis* ya = hist->GetYaxis();
229 
230  xa->SetTitleOffset(0.7);
231  xa->SetTitleSize(0.05);
232  xa->SetLabelSize(0.04);
233 
234  ya->SetTitleOffset(0.7);
235  ya->SetTitleSize(0.05);
236  ya->SetLabelSize(0.04);
237 }
238 // -- Get Projection Histogram
239 //
241  std::string ptit = me->getTitle();
242  ptit += "-Yprojection";
243 
244  if (me->kind() == MonitorElement::Kind::TH2F) {
245  TH2F* hist2 = me->getTH2F();
246  tp = new TH1F(
247  ptit.c_str(), ptit.c_str(), hist2->GetNbinsY(), hist2->GetYaxis()->GetXmin(), hist2->GetYaxis()->GetXmax());
248  tp->GetXaxis()->SetTitle(ptit.c_str());
249  for (int j = 1; j < hist2->GetNbinsY() + 1; j++) {
250  float tot_count = 0.0;
251  for (int i = 1; i < hist2->GetNbinsX() + 1; i++) {
252  tot_count += hist2->GetBinContent(i, j);
253  }
254  tp->SetBinContent(j, tot_count);
255  }
256  } else if (me->kind() == MonitorElement::Kind::TPROFILE) {
257  TProfile* prof = me->getTProfile();
258  tp = new TH1F(ptit.c_str(), ptit.c_str(), 100, 0.0, prof->GetMaximum() * 1.2);
259  tp->GetXaxis()->SetTitle(ptit.c_str());
260  for (int i = 1; i < prof->GetNbinsX() + 1; i++) {
261  tp->Fill(prof->GetBinContent(i));
262  }
263  } else if (me->kind() == MonitorElement::Kind::TH1F) {
264  TH1F* hist1 = me->getTH1F();
265  tp = new TH1F(ptit.c_str(), ptit.c_str(), 100, 0.0, hist1->GetMaximum() * 1.2);
266  tp->GetXaxis()->SetTitle(ptit.c_str());
267  for (int i = 1; i < hist1->GetNbinsX() + 1; i++) {
268  tp->Fill(hist1->GetBinContent(i));
269  }
270  }
271 }
272 //
273 // -- Set New CondDB Plot
274 //
276  std::string const& option,
277  int const width,
278  int const height) {
279  PlotParameter local_par{path, option, width, height};
280  condDBPlotList_.push_back(std::move(local_par));
281 }
282 //
283 // -- Create CondDB Plots
284 //
286  if (condDBPlotList_.empty())
287  return;
288  std::string name = "Dummy";
289  if (!hasNamedImage(name))
291 
292  for (std::vector<PlotParameter>::iterator it = condDBPlotList_.begin(); it != condDBPlotList_.end(); it++) {
293  makeCondDBPlots(dqm_store, (*it));
294  }
295  condDBPlotList_.clear();
296 }
297 //
298 // -- Draw CondDB Histograms
299 //
301  TCanvas* canvas = new TCanvas("TKCanvas", "TKCanvas", par.CWidth, par.CHeight);
302 
303  std::vector<std::string> htypes;
304  std::string option = par.Option;
305  SiStripUtility::split(option, htypes, ",");
306 
308  std::vector<MonitorElement*> all_mes = dqm_store->getContents(par.Path);
309 
310  for (std::vector<std::string>::const_iterator ih = htypes.begin(); ih != htypes.end(); ih++) {
311  std::string type = (*ih);
312  if (type.empty())
313  continue;
314  std::string tag = par.Path + "/";
315  for (std::vector<MonitorElement*>::const_iterator it = all_mes.begin(); it != all_mes.end(); it++) {
316  MonitorElement* me = (*it);
317  if (!me)
318  continue;
319  std::string hname = me->getName();
320  if (hname.find(type) != std::string::npos) {
321  TH1* histo = me->getTH1();
322  histo->Draw();
323  tag += type;
325  canvas->Clear();
326  }
327  }
328  }
329  delete canvas;
330 }
std::map< std::string, std::string > namedPictureBuffer_
bool hasNamedImage(const std::string &name)
void createPlots(DQMStore *dqm_store)
void setDrawingOption(TH1 *hist)
void setNewPlot(std::string const &path, std::string const &option, int width, int height)
std::vector< PlotParameter > condDBPlotList_
void getNamedImageBuffer(const std::string &path, std::string &image)
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)
void makeCondDBPlots(DQMStore *dqm_store, const PlotParameter &par)
__shared__ Hist hist
Log< level::Info, false > LogInfo
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=" ")
virtual MonitorElement * get(std::string const &fullpath) const
Definition: DQMStore.cc:673
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)
def move(src, dest)
Definition: eostools.py:511
virtual std::vector< dqm::harvesting::MonitorElement * > getContents(std::string const &path) const
Definition: DQMStore.cc:593