CMS 3D CMS Logo

SiPixelTemplateHelper.h
Go to the documentation of this file.
1 #ifndef CONDCORE_SIPIXELPLUGINS_SIPIXELTEMPLATEHELPER_H
2 #define CONDCORE_SIPIXELPLUGINS_SIPIXELTEMPLATEHELPER_H
3 
14 
15 #include <type_traits>
16 #include <memory>
17 #include <sstream>
18 #include <fmt/printf.h>
19 #include <boost/range/adaptor/indexed.hpp>
20 
21 // include ROOT
22 #include "TH2F.h"
23 #include "TH1F.h"
24 #include "TLegend.h"
25 #include "TCanvas.h"
26 #include "TLine.h"
27 #include "TStyle.h"
28 #include "TLatex.h"
29 #include "TPave.h"
30 #include "TPaveStats.h"
31 #include "TGaxis.h"
32 
33 namespace templateHelper {
34 
35  //************************************************
36  // Display of Template/GenError Titles
37  // *************************************************/
38  template <class PayloadType, class StoreType, class TransientType>
40  : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
41  public:
43  : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
44  "Table of SiPixelTemplate/GenError titles") {
45  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
46  isTemplate_ = false;
47  label_ = "SiPixelGenErrorDBObject_PayloadInspector";
48  } else {
49  isTemplate_ = true;
50  label_ = "SiPixelTemplateDBObject_PayloadInspector";
51  }
52  }
53 
54  bool fill() override {
55  auto tag = cond::payloadInspector::PlotBase::getTag<0>();
56  auto iov = tag.iovs.front();
57  auto tagname = tag.name;
58  std::vector<StoreType> thePixelTemp_;
59  std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
60 
61  std::string IOVsince = std::to_string(std::get<0>(iov));
62 
63  if (payload.get()) {
64  if (!TransientType::pushfile(*payload, thePixelTemp_)) {
65  throw cms::Exception(label_) << "\nERROR:" << (isTemplate_ ? "Templates" : "GenErrors")
66  << " not filled correctly."
67  << " Check the conditions. Using "
68  << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
69  << " version " << payload->version() << "\n\n";
70  }
71 
72  unsigned int mapsize = thePixelTemp_.size();
73  float pitch = 1. / (mapsize * 1.1);
74 
75  float y, x1, x2;
76  std::vector<float> y_x1, y_x2, y_line;
77  std::vector<std::string> s_x1, s_x2, s_x3;
78 
79  // starting table at y=1.0 (top of the canvas)
80  // first column is at 0.02, second column at 0.32 NDC
81  y = 1.0;
82  x1 = 0.02;
83  x2 = x1 + 0.30;
84 
85  y -= pitch;
86  y_x1.push_back(y);
87  s_x1.push_back(Form("#scale[1.2]{%s}", (isTemplate_ ? "Template ID" : "GenError ID")));
88  y_x2.push_back(y);
89  s_x2.push_back(Form("#scale[1.2]{#color[4]{%s} in IOV: #color[4]{%s}}", tagname.c_str(), IOVsince.c_str()));
90 
91  y -= pitch / 2.;
92  y_line.push_back(y);
93 
94  for (const auto& element : thePixelTemp_) {
95  y -= pitch;
96  y_x1.push_back(y);
97  s_x1.push_back(std::to_string(element.head.ID));
98 
99  y_x2.push_back(y);
100  s_x2.push_back(Form("#color[2]{%s}", element.head.title));
101 
102  y_line.push_back(y - (pitch / 2.));
103  }
104 
105  const auto& c_title = fmt::sprintf("%s titles", (isTemplate_ ? "Template" : "GenError"));
106  TCanvas canvas(c_title.c_str(), c_title.c_str(), 2000, std::max(y_x1.size(), y_x2.size()) * 40);
107  TLatex l;
108  // Draw the columns titles
109  l.SetTextAlign(12);
110 
111  float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.1);
112  float factor = newpitch / pitch;
113  l.SetTextSize(newpitch - 0.002);
114  canvas.cd();
115  for (unsigned int i = 0; i < y_x1.size(); i++) {
116  l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
117  }
118 
119  for (unsigned int i = 0; i < y_x2.size(); i++) {
120  l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
121  }
122 
123  canvas.cd();
124  canvas.Update();
125 
126  TLine lines[y_line.size()];
127  unsigned int iL = 0;
128  for (const auto& line : y_line) {
129  lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
130  lines[iL].SetLineWidth(1);
131  lines[iL].SetLineStyle(9);
132  lines[iL].SetLineColor(2);
133  lines[iL].Draw("same");
134  iL++;
135  }
136 
138  canvas.SaveAs(fileName.c_str());
139 
140  } // if payload.get()
141  return true;
142  }
143 
144  protected:
147  };
148 
149  /************************************************
150  // header plotting
151  *************************************************/
152  template <class PayloadType, class StoreType, class TransientType>
153  class SiPixelHeaderTable : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
154  public:
156  : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
157  "SiPixel CPE Conditions Header summary") {
158  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
159  isTemplate_ = false;
160  label_ = "SiPixelGenErrorDBObject_PayloadInspector";
161  } else {
162  isTemplate_ = true;
163  label_ = "SiPixelTemplateDBObject_PayloadInspector";
164  }
165  }
166 
167  bool fill() override {
168  gStyle->SetHistMinimumZero(); // will display zero as zero in the text map
169  gStyle->SetPalette(kMint); // for the ghost plot (colored BPix and FPix bins)
170 
171  auto tag = cond::payloadInspector::PlotBase::getTag<0>();
172  auto iov = tag.iovs.front();
173  auto tagname = tag.name;
174  std::vector<StoreType> thePixelTemp_;
175  std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
176 
177  if (payload.get()) {
178  if (!TransientType::pushfile(*payload, thePixelTemp_)) {
179  throw cms::Exception(label_) << "\nERROR:" << (isTemplate_ ? "Templates" : "GenErrors")
180  << " not filled correctly."
181  << " Check the conditions. Using "
182  << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
183  << payload->version() << "\n\n";
184  }
185 
186  // store the map of ID / interesting quantities
187  TransientType templ(thePixelTemp_);
188  TCanvas canvas("Header Summary", "Header summary", 1400, 1000);
189  canvas.cd();
190 
191  unsigned int tempSize = thePixelTemp_.size();
192 
193  canvas.SetTopMargin(0.07);
194  canvas.SetBottomMargin(0.06);
195  canvas.SetLeftMargin(0.17);
196  canvas.SetRightMargin(0.03);
197  canvas.Modified();
198  canvas.SetGrid();
199 
200  auto h2_Header = std::make_unique<TH2F>("Header", ";;", tempSize, 0, tempSize, 12, 0., 12.);
201  auto h2_ghost = std::make_unique<TH2F>("ghost", ";;", tempSize, 0, tempSize, 12, 0., 12.);
202  h2_Header->SetStats(false);
203  h2_ghost->SetStats(false);
204 
205  int tempVersion = -999;
206 
207  for (const auto& theTemp : thePixelTemp_ | boost::adaptors::indexed(1)) {
208  auto tempValue = theTemp.value();
209  auto idx = theTemp.index();
210  float uH = -99.;
211  if (tempValue.head.Bfield != 0.) {
212  uH = roundoff(tempValue.head.lorxwidth / tempValue.head.zsize / tempValue.head.Bfield, 4);
213  }
214 
215  // clang-format off
216  h2_Header->SetBinContent(idx, 12, tempValue.head.ID);
217  h2_Header->SetBinContent(idx, 11, tempValue.head.Bfield);
218  h2_Header->SetBinContent(idx, 10, uH);
219  h2_Header->SetBinContent(idx, 9, tempValue.head.xsize);
220  h2_Header->SetBinContent(idx, 8, tempValue.head.ysize);
221  h2_Header->SetBinContent(idx, 7, tempValue.head.zsize);
222  h2_Header->SetBinContent(idx, 6, tempValue.head.NTy);
223  h2_Header->SetBinContent(idx, 5, tempValue.head.NTyx);
224  h2_Header->SetBinContent(idx, 4, tempValue.head.NTxx);
225  h2_Header->SetBinContent(idx, 3, tempValue.head.Dtype);
226  h2_Header->SetBinContent(idx, 2, tempValue.head.qscale);
227  h2_Header->SetBinContent(idx, 1, tempValue.head.Vbias);
228  // clang-format on
229 
230  h2_Header->GetYaxis()->SetBinLabel(12, (isTemplate_ ? "TemplateID" : "GenErrorID"));
231  h2_Header->GetYaxis()->SetBinLabel(11, "B-field [T]");
232  h2_Header->GetYaxis()->SetBinLabel(10, "#mu_{H} [1/T]");
233  h2_Header->GetYaxis()->SetBinLabel(9, "x-size [#mum]");
234  h2_Header->GetYaxis()->SetBinLabel(8, "y-size [#mum]");
235  h2_Header->GetYaxis()->SetBinLabel(7, "z-size [#mum]");
236  h2_Header->GetYaxis()->SetBinLabel(6, "NTy");
237  h2_Header->GetYaxis()->SetBinLabel(5, "NTyx");
238  h2_Header->GetYaxis()->SetBinLabel(4, "NTxx");
239  h2_Header->GetYaxis()->SetBinLabel(3, "DetectorType");
240  h2_Header->GetYaxis()->SetBinLabel(2, "qScale");
241  h2_Header->GetYaxis()->SetBinLabel(1, "VBias [V]");
242  h2_Header->GetXaxis()->SetBinLabel(idx, "");
243 
244  for (unsigned int iy = 1; iy <= 12; iy++) {
245  // Some of the Phase-2 templates have DType = 0 for all partitions (TBPX, TEPX, TFPX)
246  // so they are distinguished by the uH strength value (<0).
247  // To avoid changing the behaviour of 0T payload (uH=-99) that case is treated separately
248  if (tempValue.head.Dtype != 0 || (uH < 0 && uH > -99)) {
249  h2_ghost->SetBinContent(idx, iy, 1);
250  } else {
251  h2_ghost->SetBinContent(idx, iy, -1);
252  }
253  h2_ghost->GetYaxis()->SetBinLabel(iy, h2_Header->GetYaxis()->GetBinLabel(iy));
254  h2_ghost->GetXaxis()->SetBinLabel(idx, "");
255  }
256 
257  if (tempValue.head.templ_version != tempVersion) {
258  tempVersion = tempValue.head.templ_version;
259  }
260  }
261 
262  h2_Header->GetXaxis()->LabelsOption("h");
263  h2_Header->GetXaxis()->SetNdivisions(500 + tempSize, false);
264  h2_Header->GetYaxis()->SetLabelSize(0.05);
265  h2_Header->SetMarkerSize(1.5);
266 
267  h2_ghost->GetXaxis()->LabelsOption("h");
268  h2_ghost->GetXaxis()->SetNdivisions(500 + tempSize, false);
269  h2_ghost->GetYaxis()->SetLabelSize(0.05);
270 
271  canvas.cd();
272  h2_ghost->Draw("col");
273  h2_Header->Draw("TEXTsame");
274 
275  TPaveText ksPt(0, 0, 0.88, 0.04, "NDC");
276  ksPt.SetBorderSize(0);
277  ksPt.SetFillColor(0);
278  const char* textToAdd = Form("%s Version: #color[2]{%i}. Payload hash: #color[2]{%s}",
279  (isTemplate_ ? "Template" : "GenError"),
280  tempVersion,
281  (std::get<1>(iov)).c_str());
282  ksPt.AddText(textToAdd);
283  ksPt.Draw();
284 
285  auto ltx = TLatex();
286  ltx.SetTextFont(62);
287  ltx.SetTextSize(0.040);
288  ltx.SetTextAlign(11);
289  ltx.DrawLatexNDC(
290  gPad->GetLeftMargin(),
291  1 - gPad->GetTopMargin() + 0.01,
292  ("#color[4]{" + tagname + "}, IOV: #color[4]{" + std::to_string(std::get<0>(iov)) + "}").c_str());
293 
295  canvas.SaveAs(fileName.c_str());
296  }
297  return true;
298  }
299 
300  float roundoff(float value, unsigned char prec) {
301  float pow_10 = pow(10.0f, (float)prec);
302  return round(value * pow_10) / pow_10;
303  }
304 
305  protected:
308  };
309 
310  //***********************************************
311  // TH2Poly Map of IDs
312  //***********************************************/
313  template <class PayloadType, SiPixelPI::DetType myType>
314  class SiPixelIDs : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
315  public:
317  : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
318  "SiPixelMap of Template / GenError ID Values") {
319  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
320  isTemplate_ = false;
321  label_ = "SiPixelGenErrorDBObject_PayloadInspector";
322  } else {
323  isTemplate_ = true;
324  label_ = "SiPixelTemplateDBObject_PayloadInspector";
325  }
326  }
327 
328  bool fill() override {
329  gStyle->SetPalette(kRainBow);
330 
331  auto tag = cond::payloadInspector::PlotBase::getTag<0>();
332  auto iov = tag.iovs.front();
333  std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
334 
335  std::string barrelName_ = fmt::sprintf("%sIDsBarrel", (isTemplate_ ? "template" : "genError"));
336  std::string endcapName_ = fmt::sprintf("%sIDsForward", (isTemplate_ ? "template" : "genError"));
337  std::string title_ = fmt::sprintf("%s IDs", (isTemplate_ ? "template" : "genError"));
338 
339  if (payload.get()) {
340  // Book the TH2Poly
341  Phase1PixelMaps theMaps("text");
342  if (myType == SiPixelPI::t_barrel) {
343  // book the barrel bins of the TH2Poly
344  theMaps.bookBarrelHistograms(barrelName_, title_.c_str(), title_.c_str());
345  } else if (myType == SiPixelPI::t_forward) {
346  // book the forward bins of the TH2Poly
347  theMaps.bookForwardHistograms(endcapName_, title_.c_str(), title_.c_str());
348  }
349 
350  std::map<unsigned int, short> templMap;
351  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
352  templMap = payload->getGenErrorIDs();
353  } else {
354  templMap = payload->getTemplateIDs();
355  }
356 
357  if (templMap.size() == SiPixelPI::phase0size || templMap.size() > SiPixelPI::phase1size) {
359  << "There are " << templMap.size()
360  << " DetIds in this payload. SiPixelIDs maps are not supported for non-Phase1 Pixel geometries !";
361  TCanvas canvas("Canv", "Canv", 1200, 1000);
362  SiPixelPI::displayNotSupported(canvas, templMap.size());
364  canvas.SaveAs(fileName.c_str());
365  return false;
366  } else {
367  if (templMap.size() < SiPixelPI::phase1size) {
368  edm::LogWarning(label_) << "\n ********* WARNING! ********* \n There are " << templMap.size()
369  << " DetIds in this payload !"
370  << "\n **************************** \n";
371  }
372  }
373 
374  /*
375  std::vector<unsigned int> detids;
376  std::transform(templMap.begin(),
377  templMap.end(),
378  std::back_inserter(detids),
379  [](const std::map<unsigned int, short>::value_type& pair) { return pair.first; });
380  */
381 
382  for (auto const& entry : templMap) {
383  COUT << "DetID: " << entry.first << fmt::sprintf("%s ID ", (isTemplate_ ? "Template" : "GenError"))
384  << entry.second << std::endl;
385  auto detid = DetId(entry.first);
386  if ((detid.subdetId() == PixelSubdetector::PixelBarrel) && (myType == SiPixelPI::t_barrel)) {
387  theMaps.fillBarrelBin(barrelName_, entry.first, entry.second);
388  } else if ((detid.subdetId() == PixelSubdetector::PixelEndcap) && (myType == SiPixelPI::t_forward)) {
389  theMaps.fillForwardBin(endcapName_, entry.first, entry.second);
390  }
391  }
392 
393  theMaps.beautifyAllHistograms();
394 
395  TCanvas canvas("Canv", "Canv", (myType == SiPixelPI::t_barrel) ? 1200 : 1500, 1000);
396  if (myType == SiPixelPI::t_barrel) {
397  theMaps.drawBarrelMaps(barrelName_, canvas);
398  } else if (myType == SiPixelPI::t_forward) {
399  theMaps.drawForwardMaps(endcapName_, canvas);
400  }
401 
402  canvas.cd();
403 
405  canvas.SaveAs(fileName.c_str());
406  }
407  return true;
408  }
409 
410  protected:
413  };
414 
415  /************************************************
416  Full Pixel Tracker Map class
417  *************************************************/
418  template <class PayloadType, class StoreType, class TransientType>
420  : public cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV> {
421  public:
423  : cond::payloadInspector::PlotImage<PayloadType, cond::payloadInspector::SINGLE_IOV>(
424  "SiPixel CPE conditions Map of IDs") {
425  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
426  isTemplate_ = false;
427  label_ = "SiPixelGenErrorDBObject_PayloadInspector";
428  } else {
429  isTemplate_ = true;
430  label_ = "SiPixelTemplateDBObject_PayloadInspector";
431  }
432  }
433 
434  bool fill() override {
435  gStyle->SetPalette(1);
436  auto tag = cond::payloadInspector::PlotBase::getTag<0>();
437  auto iov = tag.iovs.front();
438  std::vector<StoreType> thePixelTemp_;
439  std::shared_ptr<PayloadType> payload = this->fetchPayload(std::get<1>(iov));
440 
441  std::string payloadString = (isTemplate_ ? "Templates" : "GenErrors");
442 
443  if (payload.get()) {
444  if (!TransientType::pushfile(*payload, thePixelTemp_)) {
445  throw cms::Exception(label_) << "\nERROR: " << payloadString
446  << " not filled correctly. Check the conditions. Using "
447  << (isTemplate_ ? "SiPixelTemplateDBObject" : "SiPixelGenErrorDBObject")
448  << payload->version() << "\n\n";
449  }
450 
451  Phase1PixelSummaryMap fullMap("", fmt::sprintf("%s IDs", payloadString), fmt::sprintf("%s ID", payloadString));
452  fullMap.createTrackerBaseMap();
453 
454  std::map<unsigned int, short> templMap;
455  if constexpr (std::is_same_v<PayloadType, SiPixelGenErrorDBObject>) {
456  templMap = payload->getGenErrorIDs();
457  } else {
458  templMap = payload->getTemplateIDs();
459  }
460 
461  for (const auto& entry : templMap) {
462  fullMap.fillTrackerMap(entry.first, entry.second);
463  }
464 
465  if (templMap.size() == SiPixelPI::phase0size || templMap.size() > SiPixelPI::phase1size) {
467  << "There are " << templMap.size()
468  << " DetIds in this payload. SiPixelIDs maps are not supported for non-Phase1 Pixel geometries !";
469  TCanvas canvas("Canv", "Canv", 1200, 1000);
470  SiPixelPI::displayNotSupported(canvas, templMap.size());
472  canvas.SaveAs(fileName.c_str());
473  return false;
474  } else {
475  if (templMap.size() < SiPixelPI::phase1size) {
476  edm::LogWarning(label_) << "\n ********* WARNING! ********* \n There are " << templMap.size()
477  << " DetIds in this payload !"
478  << "\n **************************** \n";
479  }
480  }
481 
482  TCanvas canvas("Canv", "Canv", 3000, 2000);
483  fullMap.printTrackerMap(canvas);
484 
485  //fmt::sprintf("#color[2]{%s, IOV %i}",tag.name,std::get<0>(iov));
486 
487  auto ltx = TLatex();
488  ltx.SetTextFont(62);
489  ltx.SetTextSize(0.025);
490  ltx.SetTextAlign(11);
491  ltx.DrawLatexNDC(
492  gPad->GetLeftMargin() + 0.01,
493  gPad->GetBottomMargin() + 0.01,
494  ("#color[4]{" + tag.name + "}, IOV: #color[4]{" + std::to_string(std::get<0>(iov)) + "}").c_str());
495 
497  canvas.SaveAs(fileName.c_str());
498  }
499  return true;
500  }
501 
502  protected:
505  };
506 
507 } // namespace templateHelper
508 
509 #endif
void drawForwardMaps(const std::string &currentHistoName, TCanvas &canvas, const char *drawOption=nullptr)
static const unsigned int phase0size
std::string to_string(const V &value)
Definition: OMSAccess.h:71
void printTrackerMap(TCanvas &canvas, const float topMargin=0.02)
Log< level::Error, false > LogError
void fillForwardBin(const std::string &currentHistoName, unsigned int id, double value)
void drawBarrelMaps(const std::string &currentHistoName, TCanvas &canvas, const char *drawOption=nullptr)
static const unsigned int phase1size
double f[11][100]
Definition: value.py:1
#define COUT
void beautifyAllHistograms()
Definition: DetId.h:17
void bookBarrelHistograms(const std::string &currentHistoName, const char *what, const char *zaxis)
void displayNotSupported(TCanvas &canv, const unsigned int size)
void bookForwardHistograms(const std::string &currentHistoName, const char *what, const char *zaxis)
void fillBarrelBin(const std::string &currentHistoName, unsigned int id, double value)
Definition: plugin.cc:23
bool fillTrackerMap(unsigned int id, double value)
def canvas(sub, attr)
Definition: svgfig.py:482
Log< level::Warning, false > LogWarning
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
float roundoff(float value, unsigned char prec)
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)