CMS 3D CMS Logo

AlCaRecoTriggerBits_PayloadInspector.cc
Go to the documentation of this file.
5 
6 #include <fmt/printf.h>
7 #include <memory>
8 #include <sstream>
9 #include <iostream>
10 #include "TCanvas.h"
11 #include "TLatex.h"
12 #include "TLine.h"
13 
14 namespace {
15 
16  using namespace cond::payloadInspector;
17 
18  /************************************************
19  Display AlCaRecoTriggerBits mapping
20  *************************************************/
21  class AlCaRecoTriggerBits_Display : public PlotImage<AlCaRecoTriggerBits, SINGLE_IOV> {
22  public:
23  AlCaRecoTriggerBits_Display() : PlotImage<AlCaRecoTriggerBits, SINGLE_IOV>("Table of AlCaRecoTriggerBits") {}
24 
25  bool fill() override {
26  auto tag = PlotBase::getTag<0>();
27  auto iov = tag.iovs.front();
28  std::shared_ptr<AlCaRecoTriggerBits> payload = fetchPayload(std::get<1>(iov));
29 
30  std::string IOVsince = std::to_string(std::get<0>(iov));
31 
32  // Get map of strings to concatenated list of names of HLT paths:
33  typedef std::map<std::string, std::string> TriggerMap;
34  const TriggerMap &triggerMap = payload->m_alcarecoToTrig;
35 
36  unsigned int mapsize = triggerMap.size();
37  float pitch = 1. / (mapsize * 1.1);
38 
39  float y, x1, x2;
40  std::vector<float> y_x1, y_x2, y_line;
41  std::vector<std::string> s_x1, s_x2, s_x3;
42 
43  // starting table at y=1.0 (top of the canvas)
44  // first column is at 0.02, second column at 0.32 NDC
45  y = 1.0;
46  x1 = 0.02;
47  x2 = x1 + 0.30;
48 
49  y -= pitch;
50  y_x1.push_back(y);
51  s_x1.push_back("#scale[1.2]{Key}");
52  y_x2.push_back(y);
53  s_x2.push_back("#scale[1.2]{tag: " + tag.name + " in IOV: " + IOVsince + "}");
54 
55  y -= pitch / 2.;
56  y_line.push_back(y);
57 
58  for (const auto &element : triggerMap) {
59  y -= pitch;
60  y_x1.push_back(y);
61  s_x1.push_back(element.first);
62 
63  std::vector<std::string> output;
64  std::string toAppend = "";
65  const std::vector<std::string> paths = payload->decompose(element.second);
66  for (unsigned int iPath = 0; iPath < paths.size(); ++iPath) {
67  // if the line to be added has less than 60 chars append to current
68  if ((toAppend + paths[iPath]).length() < 60) {
69  toAppend += paths[iPath] + ";";
70  } else {
71  // else if the line exceeds 60 chars, dump in the vector and resume from scratch
72  output.push_back(toAppend);
73  toAppend.clear();
74  toAppend += paths[iPath] + ";";
75  }
76  // if it's the last, dump it
77  if (iPath == paths.size() - 1)
78  output.push_back(toAppend);
79  }
80 
81  for (unsigned int br = 0; br < output.size(); br++) {
82  y_x2.push_back(y);
83  s_x2.push_back("#color[2]{" + output[br] + "}");
84  if (br != output.size() - 1)
85  y -= pitch;
86  }
87 
88  y_line.push_back(y - (pitch / 2.));
89  }
90 
91  TCanvas canvas("AlCaRecoTriggerBits", "AlCaRecoTriggerBits", 2000, std::max(y_x1.size(), y_x2.size()) * 40);
92  TLatex l;
93  // Draw the columns titles
94  l.SetTextAlign(12);
95 
96  float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.1);
97  float factor = newpitch / pitch;
98  l.SetTextSize(newpitch - 0.002);
99  canvas.cd();
100  for (unsigned int i = 0; i < y_x1.size(); i++) {
101  l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
102  }
103 
104  for (unsigned int i = 0; i < y_x2.size(); i++) {
105  l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
106  }
107 
108  canvas.cd();
109  canvas.Update();
110 
111  TLine lines[y_line.size()];
112  unsigned int iL = 0;
113  for (const auto &line : y_line) {
114  lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
115  lines[iL].SetLineWidth(1);
116  lines[iL].SetLineStyle(9);
117  lines[iL].SetLineColor(2);
118  lines[iL].Draw("same");
119  iL++;
120  }
121 
122  std::string fileName(m_imageFileName);
123  canvas.SaveAs(fileName.c_str());
124  return true;
125  }
126  };
127 
128  /************************************************
129  Compare AlCaRecoTriggerBits mapping
130  *************************************************/
131  template <IOVMultiplicity nIOVs, int ntags>
132  class AlCaRecoTriggerBits_CompareBase : public PlotImage<AlCaRecoTriggerBits, nIOVs, ntags> {
133  public:
134  AlCaRecoTriggerBits_CompareBase()
135  : PlotImage<AlCaRecoTriggerBits, nIOVs, ntags>("Table of AlCaRecoTriggerBits comparison") {}
136 
137  bool fill() override {
138  // trick to deal with the multi-ioved tag and two tag case at the same time
139  auto theIOVs = PlotBase::getTag<0>().iovs;
140  auto f_tagname = PlotBase::getTag<0>().name;
141  std::string l_tagname = "";
142  auto firstiov = theIOVs.front();
143  std::tuple<cond::Time_t, cond::Hash> lastiov;
144 
145  // we don't support (yet) comparison with more than 2 tags
146  assert(this->m_plotAnnotations.ntags < 3);
147 
148  if (this->m_plotAnnotations.ntags == 2) {
149  auto tag2iovs = PlotBase::getTag<1>().iovs;
150  l_tagname = PlotBase::getTag<1>().name;
151  lastiov = tag2iovs.front();
152  } else {
153  lastiov = theIOVs.back();
154  }
155 
156  std::shared_ptr<AlCaRecoTriggerBits> last_payload = this->fetchPayload(std::get<1>(lastiov));
157  std::shared_ptr<AlCaRecoTriggerBits> first_payload = this->fetchPayload(std::get<1>(firstiov));
158 
159  std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
160  std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
161 
162  // Get map of strings to concatenated list of names of HLT paths:
163  typedef std::map<std::string, std::string> TriggerMap;
164  const TriggerMap &first_triggerMap = first_payload->m_alcarecoToTrig;
165  const TriggerMap &last_triggerMap = last_payload->m_alcarecoToTrig;
166 
167  std::vector<std::string> first_keys, not_in_first_keys;
168  std::vector<std::string> last_keys, not_in_last_keys;
169 
170  // fill the vector of first keys
171  for (const auto &element : first_triggerMap) {
172  first_keys.push_back(element.first);
173  }
174 
175  // fill the vector of last keys
176  for (const auto &element : last_triggerMap) {
177  last_keys.push_back(element.first);
178  }
179 
180  // find the elements not in common
181  std::set_difference(first_keys.begin(),
182  first_keys.end(),
183  last_keys.begin(),
184  last_keys.end(),
185  std::inserter(not_in_last_keys, not_in_last_keys.begin()));
186 
187  std::set_difference(last_keys.begin(),
188  last_keys.end(),
189  first_keys.begin(),
190  first_keys.end(),
191  std::inserter(not_in_first_keys, not_in_first_keys.begin()));
192 
193  float pitch = 0.013;
194  float y, x1, x2, x3;
195 
196  std::vector<float> y_x1, y_x2, y_x3, y_line;
197  std::vector<std::string> s_x1, s_x2, s_x3;
198 
199  y = 1.0;
200  x1 = 0.02;
201  x2 = x1 + 0.20;
202  x3 = x2 + 0.30;
203 
204  y -= pitch;
205  y_x1.push_back(y);
206  s_x1.push_back("#scale[1.2]{Key}");
207  y_x2.push_back(y);
208  s_x2.push_back(fmt::sprintf("#scale[1.2]{%s in IOV: %s}", f_tagname, firstIOVsince));
209  y_x3.push_back(y);
210  s_x3.push_back(fmt::sprintf("#scale[1.2]{%s in IOV: %s}", l_tagname, lastIOVsince));
211  y -= pitch / 3;
212 
213  // print the ones missing in the last key
214  for (const auto &key : not_in_last_keys) {
215  y -= pitch;
216  y_x1.push_back(y);
217  s_x1.push_back(key);
218 
219  const std::vector<std::string> missing_in_last_paths = first_payload->decompose(first_triggerMap.at(key));
220 
221  std::vector<std::string> output;
222  std::string toAppend = "";
223  for (unsigned int iPath = 0; iPath < missing_in_last_paths.size(); ++iPath) {
224  // if the line to be added has less than 60 chars append to current
225  if ((toAppend + missing_in_last_paths[iPath]).length() < 60) {
226  toAppend += missing_in_last_paths[iPath] + ";";
227  } else {
228  // else if the line exceeds 60 chars, dump in the vector and resume from scratch
229  output.push_back(toAppend);
230  toAppend.clear();
231  toAppend += missing_in_last_paths[iPath] + ";";
232  }
233  // if it's the last, dump it
234  if (iPath == missing_in_last_paths.size() - 1)
235  output.push_back(toAppend);
236  }
237 
238  for (unsigned int br = 0; br < output.size(); br++) {
239  y_x2.push_back(y);
240  s_x2.push_back("#color[2]{" + output[br] + "}");
241  if (br != output.size() - 1)
242  y -= pitch;
243  }
244  y_line.push_back(y - 0.008);
245  }
246 
247  // print the ones missing in the first key
248  for (const auto &key : not_in_first_keys) {
249  y -= pitch;
250  y_x1.push_back(y);
251  s_x1.push_back(key);
252  const std::vector<std::string> missing_in_first_paths = last_payload->decompose(last_triggerMap.at(key));
253 
254  std::vector<std::string> output;
255  std::string toAppend = "";
256  for (unsigned int iPath = 0; iPath < missing_in_first_paths.size(); ++iPath) {
257  // if the line to be added has less than 60 chars append to current
258  if ((toAppend + missing_in_first_paths[iPath]).length() < 60) {
259  toAppend += missing_in_first_paths[iPath] + ";";
260  } else {
261  // else if the line exceeds 60 chars, dump in the vector and resume from scratch
262  output.push_back(toAppend);
263  toAppend.clear();
264  toAppend += missing_in_first_paths[iPath] + ";";
265  }
266  // if it's the last, dump it
267  if (iPath == missing_in_first_paths.size() - 1)
268  output.push_back(toAppend);
269  }
270 
271  for (unsigned int br = 0; br < output.size(); br++) {
272  y_x3.push_back(y);
273  s_x3.push_back("#color[4]{" + output[br] + "}");
274  if (br != output.size() - 1)
275  y -= pitch;
276  }
277  y_line.push_back(y - 0.008);
278  }
279 
280  for (const auto &element : first_triggerMap) {
281  if (last_triggerMap.find(element.first) != last_triggerMap.end()) {
282  auto lastElement = last_triggerMap.find(element.first);
283 
285  const std::vector<std::string> first_paths = first_payload->decompose(element.second);
286  const std::vector<std::string> last_paths = last_payload->decompose(lastElement->second);
287 
288  std::vector<std::string> not_in_first;
289  std::vector<std::string> not_in_last;
290 
291  std::set_difference(first_paths.begin(),
292  first_paths.end(),
293  last_paths.begin(),
294  last_paths.end(),
295  std::inserter(not_in_last, not_in_last.begin()));
296 
297  std::set_difference(last_paths.begin(),
298  last_paths.end(),
299  first_paths.begin(),
300  first_paths.end(),
301  std::inserter(not_in_first, not_in_first.begin()));
302 
303  if (!not_in_last.empty() || !not_in_first.empty()) {
304  y -= pitch;
305  y_x1.push_back(y);
306  s_x1.push_back(element.first);
307 
308  std::vector<std::string> output;
309  std::string toAppend = "";
310  for (unsigned int iPath = 0; iPath < not_in_last.size(); ++iPath) {
311  // if the line to be added has less than 60 chars append to current
312  if ((toAppend + not_in_last[iPath]).length() < 60) {
313  toAppend += not_in_last[iPath] + ";";
314  } else {
315  // else if the line exceeds 60 chars, dump in the vector and resume from scratch
316  output.push_back(toAppend);
317  toAppend.clear();
318  toAppend += not_in_last[iPath] + ";";
319  }
320  // if it's the last and not empty, dump it
321  if (toAppend.length() > 0 && iPath == not_in_last.size() - 1)
322  output.push_back(toAppend);
323  }
324 
325  unsigned int count = output.size();
326 
327  for (unsigned int br = 0; br < count; br++) {
328  y_x2.push_back(y - (br * pitch));
329  s_x2.push_back("#color[6]{" + output[br] + "}");
330  }
331 
332  // clear vector and string
333  toAppend.clear();
334  output.clear();
335  for (unsigned int jPath = 0; jPath < not_in_first.size(); ++jPath) {
336  // if the line to be added has less than 60 chars append to current
337  if ((toAppend + not_in_first[jPath]).length() < 60) {
338  toAppend += not_in_first[jPath] + ";";
339  } else {
340  // else if the line exceeds 60 chars, dump in the vector and resume from scratch
341  output.push_back(toAppend);
342  toAppend.clear();
343  toAppend += not_in_first[jPath] + ";";
344  }
345  // if it's the last and not empty, dump it
346  if (toAppend.length() > 0 && jPath == not_in_first.size() - 1)
347  output.push_back(toAppend);
348  }
349 
350  unsigned int count1 = output.size();
351 
352  for (unsigned int br = 0; br < count1; br++) {
353  y_x3.push_back(y - (br * pitch));
354  s_x3.push_back("#color[8]{" + output[br] + "}");
355  }
356 
357  // decrease the y position to the maximum of the two lists
358  y -= (std::max(count, count1) - 1) * pitch;
359  //y-=count*pitch;
360  y_line.push_back(y - 0.008);
361 
362  } // close if there is at least a difference
363  } // if there is a common key
364  } //loop on the keys
365 
366  TCanvas canvas("AlCaRecoTriggerBits", "AlCaRecoTriggerBits", 2500., std::max(y_x1.size(), y_x2.size()) * 40);
367 
368  TLatex l;
369  // Draw the columns titles
370  l.SetTextAlign(12);
371 
372  // rescale the width of the table row to fit into the canvas
373  float newpitch = 1 / (std::max(y_x1.size(), y_x2.size()) * 1.65);
374  float factor = newpitch / pitch;
375  l.SetTextSize(newpitch - 0.002);
376  canvas.cd();
377  for (unsigned int i = 0; i < y_x1.size(); i++) {
378  l.DrawLatexNDC(x1, 1 - (1 - y_x1[i]) * factor, s_x1[i].c_str());
379  }
380 
381  for (unsigned int i = 0; i < y_x2.size(); i++) {
382  l.DrawLatexNDC(x2, 1 - (1 - y_x2[i]) * factor, s_x2[i].c_str());
383  }
384 
385  for (unsigned int i = 0; i < y_x3.size(); i++) {
386  l.DrawLatexNDC(x3, 1 - (1 - y_x3[i]) * factor, s_x3[i].c_str());
387  }
388 
389  canvas.cd();
390  canvas.Update();
391 
392  TLine lines[y_line.size()];
393  unsigned int iL = 0;
394  for (const auto &line : y_line) {
395  lines[iL] = TLine(gPad->GetUxmin(), 1 - (1 - line) * factor, gPad->GetUxmax(), 1 - (1 - line) * factor);
396  lines[iL].SetLineWidth(1);
397  lines[iL].SetLineStyle(9);
398  lines[iL].SetLineColor(2);
399  lines[iL].Draw("same");
400  iL++;
401  }
402 
403  //canvas.SetCanvasSize(2000,(1-y)*1000);
404  std::string fileName(this->m_imageFileName);
405  canvas.SaveAs(fileName.c_str());
406  return true;
407  }
408  };
409 
410  using AlCaRecoTriggerBits_Compare = AlCaRecoTriggerBits_CompareBase<MULTI_IOV, 1>;
411  using AlCaRecoTriggerBits_CompareTwoTags = AlCaRecoTriggerBits_CompareBase<SINGLE_IOV, 2>;
412 
413 } // namespace
414 
416  PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_Display);
417  PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_Compare);
418  PAYLOAD_INSPECTOR_CLASS(AlCaRecoTriggerBits_CompareTwoTags);
419 }
std::string to_string(const V &value)
Definition: OMSAccess.h:71
assert(be >=bs)
#define PAYLOAD_INSPECTOR_CLASS(CLASS_NAME)
#define PAYLOAD_INSPECTOR_MODULE(PAYLOAD_TYPENAME)
def canvas(sub, attr)
Definition: svgfig.py:482
std::vector< std::string > set_difference(std::vector< std::string > const &v1, std::vector< std::string > const &v2)