CMS 3D CMS Logo

SiStripDetVOff_PayloadInspector.cc
Go to the documentation of this file.
1 // system includes
2 #include <iostream>
3 #include <memory>
4 #include <sstream>
5 #include <fmt/printf.h>
6 
7 // user includes
16 
17 // include ROOT
18 #include "TH2F.h"
19 #include "TLegend.h"
20 #include "TCanvas.h"
21 #include "TLine.h"
22 #include "TStyle.h"
23 #include "TLatex.h"
24 #include "TPave.h"
25 #include "TPaveStats.h"
26 #include "TGaxis.h"
27 
28 namespace {
29 
30  using namespace cond::payloadInspector;
31 
32  class SiStripDetVOff_LV : public TimeHistoryPlot<SiStripDetVOff, int> {
33  public:
34  SiStripDetVOff_LV() : TimeHistoryPlot<SiStripDetVOff, int>("Nr of mod with LV OFF vs time", "nLVOff") {}
35 
36  int getFromPayload(SiStripDetVOff& payload) override { return payload.getLVoffCounts(); }
37  };
38 
39  class SiStripDetVOff_HV : public TimeHistoryPlot<SiStripDetVOff, int> {
40  public:
41  SiStripDetVOff_HV() : TimeHistoryPlot<SiStripDetVOff, int>("Nr of mod with HV OFF vs time", "nHVOff") {}
42 
43  int getFromPayload(SiStripDetVOff& payload) override { return payload.getHVoffCounts(); }
44  };
45 
46  namespace SiStripDetVOffPI {
47  enum type { t_LV = 0, t_HV = 1, t_V };
48  }
49 
50  /************************************************
51  Templated TrackerMap of Module L- H- L||V Voff
52  *************************************************/
53  template <SiStripDetVOffPI::type my_type>
54  class SiStripDetVOff_TrackerMapBase : public PlotImage<SiStripDetVOff, SINGLE_IOV> {
55  public:
56  SiStripDetVOff_TrackerMapBase() : PlotImage<SiStripDetVOff, SINGLE_IOV>("Tracker Map: Is Module VOff") {}
57 
58  bool fill() override {
59  auto tag = PlotBase::getTag<0>();
60  auto iov = tag.iovs.front();
61  auto tagname = tag.name;
62  unsigned long IOVsince = std::get<0>(iov);
63  std::shared_ptr<SiStripDetVOff> payload = fetchPayload(std::get<1>(iov));
64 
65  std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripIsModuleVOff");
66  tmap->setPalette(1);
67  std::string titleMap{};
68 
69  switch (my_type) {
70  case SiStripDetVOffPI::t_LV: {
71  titleMap = fmt::sprintf("TrackerMap of LV VOff modules | Tag: %s | IOV: %s", tagname, getIOVsince(IOVsince));
72  break;
73  }
74  case SiStripDetVOffPI::t_HV: {
75  titleMap = fmt::sprintf("TrackerMap of HV VOff modules | Tag: %s | IOV: %s", tagname, getIOVsince(IOVsince));
76  break;
77  }
78  case SiStripDetVOffPI::t_V: {
79  titleMap =
80  fmt::sprintf("TrackerMap of VOff modules (HV or LV) | Tag: %s | IOV: %s", tagname, getIOVsince(IOVsince));
81  break;
82  }
83  default:
84  edm::LogError("SiStripDetVOff_IsModuleVOff_TrackerMap") << "Unrecognized type: " << my_type << std::endl;
85  break;
86  }
87 
88  tmap->setTitle(titleMap);
89 
90  std::vector<uint32_t> detid;
91  payload->getDetIds(detid);
92 
93  for (const auto& d : detid) {
94  if ((payload->IsModuleLVOff(d) && (my_type == SiStripDetVOffPI::t_LV)) ||
95  (payload->IsModuleHVOff(d) && (my_type == SiStripDetVOffPI::t_HV)) ||
96  (payload->IsModuleVOff(d) && (my_type == SiStripDetVOffPI::t_V))) {
97  tmap->fill(d, 1.);
98  }
99  } // loop over detIds
100 
101  std::string fileName(m_imageFileName);
102  //tmap->save_as_HVtrackermap(true, 0., 1.01, fileName); // not working ?
103  tmap->save(true, 0., 1.01, fileName);
104 
105  return true;
106  }
107 
108  private:
109  const char* getIOVsince(const unsigned long IOV) {
110  int run = 0;
111  static char buf[256];
112 
113  if (IOV < 4294967296) { // run type IOV
114  run = IOV;
115  std::sprintf(buf, "%d", run);
116  } else { // time type IOV
117  run = IOV >> 32;
118  time_t t = run;
119  struct tm lt;
120  localtime_r(&t, &lt);
121  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
122  buf[sizeof(buf) - 1] = 0;
123  }
124  return buf;
125  }
126  };
127 
128  using SiStripDetVOff_IsModuleVOff_TrackerMap = SiStripDetVOff_TrackerMapBase<SiStripDetVOffPI::t_V>;
129  using SiStripDetVOff_IsModuleLVOff_TrackerMap = SiStripDetVOff_TrackerMapBase<SiStripDetVOffPI::t_LV>;
130  using SiStripDetVOff_IsModuleHVOff_TrackerMap = SiStripDetVOff_TrackerMapBase<SiStripDetVOffPI::t_HV>;
131 
132  /************************************************
133  List of unpowered modules
134  *************************************************/
135  template <SiStripDetVOffPI::type my_type>
136  class SiStripDetVOffListOfModules : public Histogram1DD<SiStripDetVOff, SINGLE_IOV> {
137  public:
138  SiStripDetVOffListOfModules()
140  "SiStrip Off modules", "SiStrip Off modules", 15148, 0., 15148., "DetId of VOff module") {}
141 
142  bool fill() override {
143  auto tag = PlotBase::getTag<0>();
144  for (auto const& iov : tag.iovs) {
145  std::shared_ptr<SiStripDetVOff> payload = Base::fetchPayload(std::get<1>(iov));
146  if (payload.get()) {
147  std::vector<uint32_t> detid;
148  payload->getDetIds(detid);
149  int i = 0; // count modules
150 
151  //std::cout.precision(1);
152 
153  for (const auto& d : detid) {
154  switch (my_type) {
155  case SiStripDetVOffPI::t_LV: {
156  if (payload->IsModuleLVOff(d)) {
157  //std::cout << "is LV: " << i << " " << std::fixed << double(d) << std::endl;
158  fillWithBinAndValue(i, double(d));
159  }
160  break;
161  }
162  case SiStripDetVOffPI::t_HV: {
163  if (payload->IsModuleHVOff(d)) {
164  //std::cout << "is HV: " << i << " " << std::fixed << double(d) << std::endl;
165  fillWithBinAndValue(i, double(d));
166  }
167  break;
168  }
169  case SiStripDetVOffPI::t_V: {
170  if (payload->IsModuleVOff(d)) {
171  //std::cout << "is V: " << i << " " << std::fixed << double(d) << std::endl;
172  fillWithBinAndValue(i, double(d));
173  }
174  break;
175  }
176  default:
177  edm::LogError("SiStripDetVOffListOfModules") << "Unrecognized type: " << my_type << std::endl;
178  break;
179  } // switch
180  i++; // increase counting of modules
181  } // loop on detids
182  } // if gets the payload
183  } // loop on iovs
184  return true;
185  } // fill()
186  };
187 
188  using SiStripVOffListOfModules = SiStripDetVOffListOfModules<SiStripDetVOffPI::t_V>;
189  using SiStripLVOffListOfModules = SiStripDetVOffListOfModules<SiStripDetVOffPI::t_LV>;
190  using SiStripHVOffListOfModules = SiStripDetVOffListOfModules<SiStripDetVOffPI::t_HV>;
191 
192  /************************************************
193  test class
194  *************************************************/
195 
196  class SiStripDetVOffTest : public Histogram1D<SiStripDetVOff, SINGLE_IOV> {
197  public:
198  SiStripDetVOffTest()
199  : Histogram1D<SiStripDetVOff, SINGLE_IOV>("SiStrip DetVOff test", "SiStrip DetVOff test", 10, 0.0, 10.0),
201  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
202 
203  bool fill() override {
204  auto tag = PlotBase::getTag<0>();
205  for (auto const& iov : tag.iovs) {
206  std::shared_ptr<SiStripDetVOff> payload = Base::fetchPayload(std::get<1>(iov));
207  if (payload.get()) {
208  std::vector<uint32_t> detid;
209  payload->getDetIds(detid);
210 
211  SiStripDetSummary summaryHV{&m_trackerTopo};
212  SiStripDetSummary summaryLV{&m_trackerTopo};
213 
214  for (const auto& d : detid) {
215  if (payload->IsModuleLVOff(d))
216  summaryLV.add(d);
217  if (payload->IsModuleHVOff(d))
218  summaryHV.add(d);
219  }
220  std::map<unsigned int, SiStripDetSummary::Values> mapHV = summaryHV.getCounts();
221  std::map<unsigned int, SiStripDetSummary::Values> mapLV = summaryLV.getCounts();
222 
223  // SiStripPI::printSummary(mapHV);
224  // SiStripPI::printSummary(mapLV);
225 
226  std::stringstream ss;
227  ss << "Summary of HV off detectors:" << std::endl;
228  summaryHV.print(ss, true);
229 
230  ss << "Summary of LV off detectors:" << std::endl;
231  summaryLV.print(ss, true);
232 
233  std::cout << ss.str() << std::endl;
234 
235  } // payload
236  } // iovs
237  return true;
238  } // fill
239  private:
240  TrackerTopology m_trackerTopo;
241  };
242 
243  /************************************************
244  Plot DetVOff by region
245  *************************************************/
246 
247  class SiStripDetVOffByRegion : public PlotImage<SiStripDetVOff, SINGLE_IOV> {
248  public:
249  SiStripDetVOffByRegion()
250  : PlotImage<SiStripDetVOff, SINGLE_IOV>("SiStrip DetVOff By Region"),
252  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath())} {}
253 
254  bool fill() override {
255  auto tag = PlotBase::getTag<0>();
256  auto iov = tag.iovs.front();
257  std::shared_ptr<SiStripDetVOff> payload = fetchPayload(std::get<1>(iov));
258 
259  unsigned long IOV = std::get<0>(iov);
260  int run = 0;
261  if (IOV < 4294967296) {
262  run = std::get<0>(iov);
263  } else { // time type IOV
264  run = IOV >> 32;
265  }
266 
267  std::vector<uint32_t> detid;
268  payload->getDetIds(detid);
269 
270  SiStripDetSummary summaryHV{&m_trackerTopo};
271  SiStripDetSummary summaryLV{&m_trackerTopo};
272 
273  for (const auto& d : detid) {
274  if (payload->IsModuleLVOff(d))
275  summaryLV.add(d);
276  if (payload->IsModuleHVOff(d))
277  summaryHV.add(d);
278  }
279  std::map<unsigned int, SiStripDetSummary::Values> mapHV = summaryHV.getCounts();
280  std::map<unsigned int, SiStripDetSummary::Values> mapLV = summaryLV.getCounts();
281  std::vector<unsigned int> keys;
283  mapHV.begin(),
284  mapHV.end(),
285  std::back_inserter(keys),
286  [](const std::map<unsigned int, SiStripDetSummary::Values>::value_type& pair) { return pair.first; });
287 
288  //=========================
289 
290  TCanvas canvas("DetVOff Partion summary", "SiStripDetVOff region summary", 1200, 1000);
291  canvas.cd();
292  auto h_HV = std::make_unique<TH1F>(
293  "HVbyRegion", "SiStrip HV/LV summary by region;; modules with HV off", mapHV.size(), 0., mapHV.size());
294  auto h_LV = std::make_unique<TH1F>(
295  "LVbyRegion", "SiStrip HV/LV summary by region;; modules with LV off", mapLV.size(), 0., mapLV.size());
296 
297  h_HV->SetStats(false);
298  h_LV->SetStats(false);
299 
300  h_HV->SetTitle(nullptr);
301  h_LV->SetTitle(nullptr);
302 
303  canvas.SetBottomMargin(0.18);
304  canvas.SetLeftMargin(0.10);
305  canvas.SetRightMargin(0.10);
306  canvas.Modified();
307 
308  std::vector<int> boundaries;
309  unsigned int iBin = 0;
310 
312  std::string currentDetector;
313 
314  for (const auto& index : keys) {
315  iBin++;
316  int countHV = mapHV[index].count;
317  int countLV = mapLV[index].count;
318 
319  if (currentDetector.empty())
320  currentDetector = "TIB";
321 
322  switch ((index) / 1000) {
323  case 1:
324  detector = "TIB";
325  break;
326  case 2:
327  detector = "TOB";
328  break;
329  case 3:
330  detector = "TEC";
331  break;
332  case 4:
333  detector = "TID";
334  break;
335  }
336 
337  h_HV->SetBinContent(iBin, countHV);
338  h_HV->GetXaxis()->SetBinLabel(iBin, SiStripPI::regionType(index).second);
339  h_HV->GetXaxis()->LabelsOption("v");
340 
341  h_LV->SetBinContent(iBin, countLV);
342  h_LV->GetXaxis()->SetBinLabel(iBin, SiStripPI::regionType(index).second);
343  h_LV->GetXaxis()->LabelsOption("v");
344 
345  if (detector != currentDetector) {
346  boundaries.push_back(iBin);
347  currentDetector = detector;
348  }
349  }
350 
351  auto extrema = SiStripPI::getExtrema(h_LV.get(), h_HV.get());
352  h_HV->GetYaxis()->SetRangeUser(extrema.first, extrema.second);
353  h_LV->GetYaxis()->SetRangeUser(extrema.first, extrema.second);
354 
355  h_HV->SetMarkerStyle(20);
356  h_HV->SetMarkerSize(1);
357  h_HV->SetLineColor(kRed);
358  h_HV->SetMarkerColor(kRed);
359  h_HV->Draw("HIST");
360  h_HV->Draw("TEXT45same");
361 
362  h_LV->SetMarkerStyle(21);
363  h_LV->SetMarkerSize(1);
364  h_LV->SetLineColor(kBlue);
365  h_LV->SetLineStyle(9);
366  h_LV->SetMarkerColor(kBlue);
367  h_LV->Draw("HISTsame");
368  h_LV->Draw("TEXT45same");
369 
370  canvas.Update();
371  canvas.cd();
372 
373  TLine l[boundaries.size()];
374  unsigned int i = 0;
375  for (const auto& line : boundaries) {
376  l[i] = TLine(
377  h_HV->GetBinLowEdge(line), canvas.cd()->GetUymin(), h_HV->GetBinLowEdge(line), canvas.cd()->GetUymax());
378  l[i].SetLineWidth(1);
379  l[i].SetLineStyle(9);
380  l[i].SetLineColor(2);
381  l[i].Draw("same");
382  i++;
383  }
384 
385  TLegend legend = TLegend(0.45, 0.80, 0.90, 0.9);
386  legend.SetHeader((std::get<1>(iov)).c_str(), "C"); // option "C" allows to center the header
387  legend.AddEntry(h_HV.get(), ("HV channels: " + std::to_string(payload->getHVoffCounts())).c_str(), "PL");
388  legend.AddEntry(h_LV.get(), ("LV channels: " + std::to_string(payload->getLVoffCounts())).c_str(), "PL");
389  legend.SetTextSize(0.025);
390  legend.Draw("same");
391 
392  TLatex t1;
393  t1.SetNDC();
394  t1.SetTextAlign(26);
395  t1.SetTextSize(0.05);
396  if (IOV < 4294967296)
397  t1.DrawLatex(0.5, 0.96, Form("SiStrip DetVOff, IOV %i", run));
398  else { // time type IOV
399  time_t t = run;
400  char buf[256];
401  struct tm lt;
402  localtime_r(&t, &lt);
403  strftime(buf, sizeof(buf), "%F %R:%S", &lt);
404  buf[sizeof(buf) - 1] = 0;
405  t1.DrawLatex(0.5, 0.96, Form("SiStrip DetVOff, IOV %s", buf));
406  }
407 
408  // Remove the current axis
409  h_HV.get()->GetYaxis()->SetLabelOffset(999);
410  h_HV.get()->GetYaxis()->SetTickLength(0);
411  h_HV.get()->GetYaxis()->SetTitleOffset(999);
412 
413  h_LV.get()->GetYaxis()->SetLabelOffset(999);
414  h_LV.get()->GetYaxis()->SetTickLength(0);
415  h_LV.get()->GetYaxis()->SetTitleOffset(999);
416 
417  //draw an axis on the left side
418  auto l_axis = std::make_unique<TGaxis>(
419  gPad->GetUxmin(), gPad->GetUymin(), gPad->GetUxmin(), gPad->GetUymax(), 0, extrema.second, 510);
420  l_axis->SetLineColor(kRed);
421  l_axis->SetTextColor(kRed);
422  l_axis->SetLabelColor(kRed);
423  l_axis->SetTitleOffset(1.2);
424  l_axis->SetTitleColor(kRed);
425  l_axis->SetTitle(h_HV.get()->GetYaxis()->GetTitle());
426  l_axis->Draw();
427 
428  //draw an axis on the right side
429  auto r_axis = std::make_unique<TGaxis>(
430  gPad->GetUxmax(), gPad->GetUymin(), gPad->GetUxmax(), gPad->GetUymax(), 0, extrema.second, 510, "+L");
431  r_axis->SetLineColor(kBlue);
432  r_axis->SetTextColor(kBlue);
433  r_axis->SetLabelColor(kBlue);
434  r_axis->SetTitleColor(kBlue);
435  r_axis->SetTitleOffset(1.2);
436  r_axis->SetTitle(h_LV.get()->GetYaxis()->GetTitle());
437  r_axis->Draw();
438 
439  std::string fileName(m_imageFileName);
440  canvas.SaveAs(fileName.c_str());
441 
442  return true;
443  }
444 
445  private:
446  TrackerTopology m_trackerTopo;
447  };
448 
449 } // namespace
450 
452  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOff_LV);
453  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOff_HV);
454  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOff_IsModuleVOff_TrackerMap);
455  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOff_IsModuleLVOff_TrackerMap);
456  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOff_IsModuleHVOff_TrackerMap);
457  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOffTest);
458  PAYLOAD_INSPECTOR_CLASS(SiStripVOffListOfModules);
459  PAYLOAD_INSPECTOR_CLASS(SiStripLVOffListOfModules);
460  PAYLOAD_INSPECTOR_CLASS(SiStripHVOffListOfModules);
461  PAYLOAD_INSPECTOR_CLASS(SiStripDetVOffByRegion);
462 }
std::pair< int, const char * > regionType(int index)
Log< level::Error, false > LogError
static std::string to_string(const XMLCh *ch)
U second(std::pair< T, U > const &p)
#define PAYLOAD_INSPECTOR_CLASS(CLASS_NAME)
std::pair< float, float > getExtrema(TH1 *h1, TH1 *h2)
void add(DetId detid, float value)
Used to compute the mean value of the value variable divided by subdetector, layer and mono/stereo...
#define PAYLOAD_INSPECTOR_MODULE(PAYLOAD_TYPENAME)
d
Definition: ztail.py:151
TrackerTopology fromTrackerParametersXMLFile(const std::string &xmlFileName)
def canvas(sub, attr)
Definition: svgfig.py:482
unsigned transform(const HcalDetId &id, unsigned transformCode)