CMS 3D CMS Logo

PayloadInspector.h
Go to the documentation of this file.
1 #ifndef CondCore_Utilities_PayloadInspector_h
2 #define CondCore_Utilities_PayloadInspector_h
3 
7 #include <iostream>
8 
9 #include <string>
10 #include <tuple>
11 #include <vector>
12 #include <set>
13 #include <type_traits>
14 
17 
18 #include <pybind11/pybind11.h>
19 #include <pybind11/stl.h>
20 namespace py = pybind11;
21 
22 namespace PI {
23  __attribute__((visibility("default"))) inline py::list mk_input(const std::string& tagName,
24  cond::Time_t start,
25  cond::Time_t end) {
26  py::list ret;
27  ret.append(py::make_tuple(tagName, std::to_string(start), std::to_string(end)));
28  return ret;
29  }
30  __attribute__((visibility("default"))) inline py::list mk_input(const std::string& tagName0,
31  cond::Time_t start0,
32  cond::Time_t end0,
33  const std::string& tagName1,
34  cond::Time_t start1,
35  cond::Time_t end1) {
36  py::list ret;
37  ret.append(py::make_tuple(tagName0, std::to_string(start0), std::to_string(end0)));
38  ret.append(py::make_tuple(tagName1, std::to_string(start1), std::to_string(end1)));
39  return ret;
40  }
41 } // namespace PI
42 
43 namespace cond {
44 
45  namespace payloadInspector {
46 
47  // Metadata dictionary
48  struct PlotAnnotations {
49  static constexpr const char* const PLOT_TYPE_K = "type";
50  static constexpr const char* const TITLE_K = "title";
51  static constexpr const char* const PAYLOAD_TYPE_K = "payload_type";
52  static constexpr const char* const INFO_K = "info";
53  static constexpr const char* const XAXIS_K = "x_label";
54  static constexpr const char* const YAXIS_K = "y_label";
55  static constexpr const char* const ZAXIS_K = "z_label";
57  std::string get(const std::string& key) const;
58  std::map<std::string, std::string> m;
59  int ntags = 1;
60  bool twoTags = false;
61  bool singleIov = false;
62  };
63 
64  static const char* const JSON_FORMAT_VERSION = "1.0";
65 
66  // Serialize functions
67  template <typename V>
68  std::string serializeValue(const std::string& entryLabel, const V& value) {
69  std::stringstream ss;
70 
71  // N.B.:
72  // This hack is to output a line to stringstream only in case the
73  // return type of getFromPayload is a std::pair<bool, float>
74  // and the bool is true. This allows to control which points should
75  // enter the trend and which should not.
76 
77  if constexpr (std::is_same_v<V, std::pair<bool, float>>) {
78  if (value.first) {
79  ss << "\"" << entryLabel << "\":" << value.second;
80  }
81  } else if constexpr (std::is_same_v<V, double>) {
82  if ((value - int(value)) == 0) {
83  ss.precision(0);
84  }
85  ss << "\"" << entryLabel << "\":" << std::fixed << value;
86  } else {
87  ss << "\"" << entryLabel << "\":" << value;
88  }
89  return ss.str();
90  }
91 
92  template <>
93  inline std::string serializeValue(const std::string& entryLabel, const std::string& value) {
94  std::stringstream ss;
95  ss << "\"" << entryLabel << "\":\"" << value << "\"";
96  return ss.str();
97  }
98 
99  // Specialization for the multi-values coordinates ( to support the combined time+runlumi abscissa )
100  template <typename V>
101  std::string serializeValue(const std::string& entryLabel, const std::tuple<V, std::string>& value) {
102  std::stringstream ss;
103  ss << serializeValue(entryLabel, std::get<0>(value));
104  ss << ", ";
105  ss << serializeValue(entryLabel + "_label", std::get<1>(value));
106  return ss.str();
107  }
108 
109  // Specialization for the error bars
110  template <typename V>
111  std::string serializeValue(const std::string& entryLabel, const std::pair<V, V>& value) {
112  std::stringstream ss;
113  ss << serializeValue(entryLabel, value.first);
114  ss << ", ";
115  ss << serializeValue(entryLabel + "_err", value.second);
116  return ss.str();
117  }
118 
119  inline std::string serializeAnnotations(const PlotAnnotations& annotations) {
120  std::stringstream ss;
121  ss << "\"version\": \"" << JSON_FORMAT_VERSION << "\",";
122  ss << "\"annotations\": {";
123  bool first = true;
124  for (const auto& a : annotations.m) {
125  if (!first)
126  ss << ",";
127  ss << "\"" << a.first << "\":\"" << a.second << "\"";
128  first = false;
129  }
130  ss << "}";
131  return ss.str();
132  }
133 
134  template <typename X, typename Y>
135  std::string serialize(const PlotAnnotations& annotations, const std::vector<std::tuple<X, Y>>& data) {
136  // prototype implementation...
137  std::stringstream ss;
138  ss << "{";
139  ss << serializeAnnotations(annotations);
140  ss << ",";
141  ss << "\"data\": [";
142  bool first = true;
143  for (auto d : data) {
144  auto serializedX = serializeValue("x", std::get<0>(d));
145  auto serializedY = serializeValue("y", std::get<1>(d));
146 
147  // N.B.:
148  // we output to JSON only if the stringstream
149  // from serializeValue is not empty
150 
151  if (!serializedY.empty()) {
152  if (!first) {
153  ss << ",";
154  }
155  ss << " { " << serializedX << ", " << serializedY << " }";
156  first = false;
157  }
158  }
159  ss << "]";
160  ss << "}";
161  return ss.str();
162  }
163 
164  template <typename X, typename Y, typename Z>
165  std::string serialize(const PlotAnnotations& annotations, const std::vector<std::tuple<X, Y, Z>>& data) {
166  // prototype implementation...
167  std::stringstream ss;
168  ss << "{";
169  ss << serializeAnnotations(annotations);
170  ss << ",";
171  ss << "\"data\": [";
172  bool first = true;
173  for (auto d : data) {
174  if (!first)
175  ss << ",";
176  ss << " { " << serializeValue("x", std::get<0>(d)) << ", " << serializeValue("y", std::get<1>(d)) << ", "
177  << serializeValue("z", std::get<2>(d)) << " }";
178  first = false;
179  }
180  ss << "]";
181  ss << "}";
182  return ss.str();
183  }
184 
185  inline std::string serialize(const PlotAnnotations& annotations, const std::string& imageFileName) {
186  std::stringstream ss;
187  ss << "{";
188  ss << serializeAnnotations(annotations);
189  ss << ",";
190  ss << "\"file\": \"" << imageFileName << "\"";
191  ss << "}";
192  return ss.str();
193  }
194 
195  struct ModuleVersion {
196  static constexpr const char* const label = "2.0";
197  };
198 
199  struct TagReference {
201  const std::pair<cond::Time_t, cond::Time_t>& b,
202  const std::vector<std::tuple<cond::Time_t, cond::Hash>>& i)
203  : name(n), boundary(b), iovs(i) {}
204  TagReference(const TagReference& rhs) : name(rhs.name), boundary(rhs.boundary), iovs(rhs.iovs) {}
206  const std::pair<cond::Time_t, cond::Time_t>& boundary;
207  const std::vector<std::tuple<cond::Time_t, cond::Hash>>& iovs;
208  };
209 
210  // Base class, factorizing the functions exposed in the python interface
211  class PlotBase {
212  public:
213  PlotBase();
214  virtual ~PlotBase() = default;
215  // required in the browser to find corresponding tags
216  std::string payloadType() const;
217 
218  // required in the browser
219  std::string title() const;
220 
221  // required in the browser
222  std::string type() const;
223 
224  // required in the browser
225  unsigned int ntags() const;
226 
227  //TBRemoved
228  bool isTwoTags() const;
229 
230  // required in the browser
231  bool isSingleIov() const;
232 
233  // required in the browser
234  __attribute__((visibility("default"))) py::list inputParams() const;
235 
236  // required in the browser
237  __attribute__((visibility("default"))) void setInputParamValues(const py::dict& values);
238 
239  // returns the json file with the plot data
240  std::string data() const;
241 
242  // triggers the processing producing the plot
243  __attribute__((visibility("default"))) bool process(const std::string& connectionString,
244  const py::list& tagsWithTimeBoundaries);
245 
246  // called by the above method - to be used in C++ unit tests...
247  bool exec_process(const std::string& connectionString,
248  const std::vector<std::tuple<std::string, cond::Time_t, cond::Time_t>>& tagsWithTimeBoundaries);
249 
250  // not exposed in python:
251  // called internally in process()
252  virtual void init();
253 
254  // not exposed in python:
255  // called internally in process()
256  //virtual std::string processData(const std::vector<std::tuple<cond::Time_t, cond::Hash> >& iovs);
257  virtual std::string processData();
258 
259  //
260  void addInputParam(const std::string& paramName);
261 
262  // access to the fetch function of the configured reader, to be used in the processData implementations
263  template <typename PayloadType>
264  std::shared_ptr<PayloadType> fetchPayload(const cond::Hash& payloadHash) {
265  return m_dbSession.fetchPayload<PayloadType>(payloadHash);
266  }
267 
269 
270  template <int index>
272  size_t sz = m_tagNames.size();
273  if (sz == 0 || index >= sz) {
274  cond::throwException("Index out of range", "PlotBase::getTag()");
275  }
277  }
278 
279  const std::map<std::string, std::string>& inputParamValues() const;
280 
281  // access to the underlying db session
283 
284  protected:
285  // possibly shared with the derived classes
287  std::set<std::string> m_inputParams;
288  std::vector<std::string> m_tagNames;
289  std::vector<std::pair<cond::Time_t, cond::Time_t>> m_tagBoundaries;
290  std::vector<std::vector<std::tuple<cond::Time_t, cond::Hash>>> m_tagIovs;
291  std::map<std::string, std::string> m_inputParamValues;
292 
293  private:
294  // this stuff should not be modified...
297  };
298 
300 
301  inline void setAnnotations(
302  const std::string& type, const std::string& title, IOVMultiplicity IOV_M, int NTAGS, PlotAnnotations& target) {
305  target.ntags = NTAGS;
306  target.singleIov = (IOV_M == SINGLE_IOV);
307  }
308 
309  template <IOVMultiplicity IOV_M, int NTAGS>
310  class PlotImpl : public PlotBase {
311  public:
313  setAnnotations(type, title, IOV_M, NTAGS, m_plotAnnotations);
314  }
315  ~PlotImpl() override = default;
316 
317  virtual std::string serializeData() = 0;
318 
320  fill();
321  return serializeData();
322  }
323 
324  virtual bool fill() = 0;
325  };
326 
327  // specialisations
328  template <int NTAGS>
329  class PlotImpl<UNSPECIFIED_IOV, NTAGS> : public PlotBase {
330  public:
333  }
334  ~PlotImpl() override = default;
335 
336  virtual std::string serializeData() = 0;
337 
339  fill();
340  return serializeData();
341  }
342 
343  virtual bool fill() = 0;
344 
346  };
347 
348  template <>
349  class PlotImpl<MULTI_IOV, 0> : public PlotBase {
350  public:
353  }
354  ~PlotImpl() override = default;
355 
356  virtual std::string serializeData() = 0;
357 
359  fill();
360  return serializeData();
361  }
362 
363  virtual bool fill() = 0;
364 
365  void setTwoTags(bool flag) {
366  if (flag)
368  else
370  }
371  };
372 
373  template <>
374  class PlotImpl<SINGLE_IOV, 0> : public PlotBase {
375  public:
378  }
379  ~PlotImpl() override = default;
380 
381  virtual std::string serializeData() = 0;
382 
384  fill();
385  return serializeData();
386  }
387 
388  virtual bool fill() = 0;
389 
390  void setTwoTags(bool flag) {
391  if (flag)
393  else
395  }
396  };
397 
398  template <>
399  class PlotImpl<UNSPECIFIED_IOV, 0> : public PlotBase {
400  public:
403  }
404  ~PlotImpl() override = default;
405 
406  virtual std::string serializeData() = 0;
407 
409  fill();
410  return serializeData();
411  }
412 
413  virtual bool fill() {
414  std::vector<std::tuple<cond::Time_t, cond::Hash>> theIovs = PlotBase::getTag<0>().iovs;
415  if (m_plotAnnotations.ntags == 2) {
416  auto tag2iovs = PlotBase::getTag<1>().iovs;
417  size_t oldSize = theIovs.size();
418  size_t newSize = oldSize + tag2iovs.size();
419  theIovs.resize(newSize);
420  for (size_t i = 0; i < tag2iovs.size(); i++) {
421  theIovs[i + oldSize] = tag2iovs[i];
422  }
423  }
424  return fill(theIovs);
425  }
426 
427  virtual bool fill(const std::vector<std::tuple<cond::Time_t, cond::Hash>>& iovs) { return false; }
428 
429  void setSingleIov(bool flag) {
431  m_singleIovSet = true;
432  }
433 
434  void setTwoTags(bool flag) {
435  if (flag) {
437  if (!m_singleIovSet)
439  } else
441  }
442 
443  bool m_singleIovSet = false;
444  };
445 
446  template <>
447  class PlotImpl<UNSPECIFIED_IOV, 1> : public PlotBase {
448  public:
451  }
452  ~PlotImpl() override = default;
453 
454  virtual std::string serializeData() = 0;
455 
457  fill();
458  return serializeData();
459  }
460 
461  virtual bool fill() {
462  std::vector<std::tuple<cond::Time_t, cond::Hash>> theIovs = PlotBase::getTag<0>().iovs;
463  if (m_plotAnnotations.ntags == 2) {
464  auto tag2iovs = PlotBase::getTag<1>().iovs;
465  size_t oldSize = theIovs.size();
466  size_t newSize = oldSize + tag2iovs.size();
467  theIovs.resize(newSize);
468  for (size_t i = 0; i < tag2iovs.size(); i++) {
469  theIovs[i + oldSize] = tag2iovs[i];
470  }
471  }
472  return fill(theIovs);
473  }
474 
475  virtual bool fill(const std::vector<std::tuple<cond::Time_t, cond::Hash>>& iovs) { return false; }
476 
478  };
479 
480  // Concrete plot-types implementations
481  template <typename PayloadType, typename X, typename Y, IOVMultiplicity IOV_M = UNSPECIFIED_IOV, int NTAGS = 0>
482  class Plot2D : public PlotImpl<IOV_M, NTAGS> {
483  public:
485  Plot2D(const std::string& type, const std::string& title, const std::string xLabel, const std::string& yLabel)
486  : Base(type, title), m_plotData() {
489  Base::m_plotAnnotations.m[PlotAnnotations::PAYLOAD_TYPE_K] = cond::demangledName(typeid(PayloadType));
490  }
491  ~Plot2D() override = default;
493 
494  std::shared_ptr<PayloadType> fetchPayload(const cond::Hash& payloadHash) {
495  return PlotBase::fetchPayload<PayloadType>(payloadHash);
496  }
497 
498  protected:
499  std::vector<std::tuple<X, Y>> m_plotData;
500  };
501 
502  template <typename PayloadType,
503  typename X,
504  typename Y,
505  typename Z,
507  int NTAGS = 0>
508  class Plot3D : public PlotImpl<IOV_M, NTAGS> {
509  public:
512  const std::string& title,
513  const std::string xLabel,
514  const std::string& yLabel,
515  const std::string& zLabel)
516  : Base(type, title), m_plotData() {
520  Base::m_plotAnnotations.m[PlotAnnotations::PAYLOAD_TYPE_K] = cond::demangledName(typeid(PayloadType));
521  }
522  ~Plot3D() override = default;
524 
525  std::shared_ptr<PayloadType> fetchPayload(const cond::Hash& payloadHash) {
526  return PlotBase::fetchPayload<PayloadType>(payloadHash);
527  }
528 
529  protected:
530  std::vector<std::tuple<X, Y, Z>> m_plotData;
531  };
532 
533  template <typename PayloadType, typename Y>
534  class HistoryPlot : public Plot2D<PayloadType, unsigned long long, Y, MULTI_IOV, 1> {
535  public:
537 
538  HistoryPlot(const std::string& title, const std::string& yLabel) : Base("History", title, "iov_since", yLabel) {}
539 
540  ~HistoryPlot() override = default;
541 
542  bool fill() override {
543  auto tag = PlotBase::getTag<0>();
544  for (auto iov : tag.iovs) {
545  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
546  if (payload.get()) {
548  Base::m_plotData.push_back(std::make_tuple(std::get<0>(iov), value));
549  }
550  }
551  return true;
552  }
553  virtual Y getFromPayload(PayloadType& payload) = 0;
554  };
555 
556  template <typename PayloadType, typename Y>
557  class RunHistoryPlot : public Plot2D<PayloadType, std::tuple<float, std::string>, Y, MULTI_IOV, 1> {
558  public:
560 
562  : Base("RunHistory", title, "iov_since", yLabel) {}
563 
564  ~RunHistoryPlot() override = default;
565 
566  bool fill() override {
567  auto tag = PlotBase::getTag<0>();
568  // for the lumi iovs we need to count the number of lumisections in every runs
569  std::map<cond::Time_t, unsigned int> runs;
570 
572  if (tagInfo.timeType == cond::lumiid) {
573  for (auto iov : tag.iovs) {
574  unsigned int run = std::get<0>(iov) >> 32;
575  auto it = runs.find(run);
576  if (it == runs.end())
577  it = runs.insert(std::make_pair(run, 0)).first;
578  it->second++;
579  }
580  }
581  unsigned int currentRun = 0;
582  float lumiIndex = 0;
583  unsigned int lumiSize = 0;
584  unsigned int rind = 0;
585  float ind = 0;
586  std::string label("");
587  for (auto iov : tag.iovs) {
588  unsigned long long since = std::get<0>(iov);
589  // for the lumi iovs we squeeze the lumi section available in the constant run 'slot' of witdth=1
590  if (tagInfo.timeType == cond::lumiid) {
591  unsigned int run = since >> 32;
592  unsigned int lumi = since & 0xFFFFFFFF;
593  if (run != currentRun) {
594  rind++;
595  lumiIndex = 0;
596  auto it = runs.find(run);
597  if (it == runs.end()) {
598  // it should never happen
599  return false;
600  }
601  lumiSize = it->second;
602  } else {
603  lumiIndex++;
604  }
605  ind = rind + (lumiIndex / lumiSize);
607  currentRun = run;
608  } else {
609  ind++;
610  // for the timestamp based iovs, it does not really make much sense to use this plot...
611  if (tagInfo.timeType == cond::timestamp) {
612  boost::posix_time::ptime t = cond::time::to_boost(since);
613  label = boost::posix_time::to_simple_string(t);
614  } else {
616  }
617  }
618  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
619  if (payload.get()) {
621  Base::m_plotData.push_back(std::make_tuple(std::make_tuple(ind, label), value));
622  }
623  }
624  return true;
625  }
626 
627  virtual Y getFromPayload(PayloadType& payload) = 0;
628  };
629 
630  template <typename PayloadType, typename Y>
631  class TimeHistoryPlot : public Plot2D<PayloadType, std::tuple<unsigned long long, std::string>, Y, MULTI_IOV, 1> {
632  public:
634 
636  : Base("TimeHistory", title, "iov_since", yLabel) {}
637  ~TimeHistoryPlot() override = default;
638 
639  bool fill() override {
640  auto tag = PlotBase::getTag<0>();
642 
644  if (tagInfo.timeType == cond::lumiid || tagInfo.timeType == cond::runnumber) {
645  cond::Time_t min = std::get<0>(tag.iovs.front());
646  cond::Time_t max = std::get<0>(tag.iovs.back());
647  if (tagInfo.timeType == cond::lumiid) {
648  min = min >> 32;
649  max = max >> 32;
650  }
652  }
653  for (auto iov : tag.iovs) {
654  cond::Time_t since = std::get<0>(iov);
655  boost::posix_time::ptime time;
656  std::string label("");
657  if (tagInfo.timeType == cond::lumiid || tagInfo.timeType == cond::runnumber) {
658  unsigned int nlumi = since & 0xFFFFFFFF;
659  if (tagInfo.timeType == cond::lumiid)
660  since = since >> 32;
662  auto it = runInfo.find(since);
663  if (it == runInfo.end()) {
664  // this should never happen...
665  return false;
666  }
667  time = (*it).start;
668  // add the lumi sections...
669  if (tagInfo.timeType == cond::lumiid) {
671  label += (" : " + std::to_string(nlumi));
672  }
673  } else if (tagInfo.timeType == cond::timestamp) {
675  label = boost::posix_time::to_simple_string(time);
676  }
677  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
678  if (payload.get()) {
680  Base::m_plotData.push_back(std::make_tuple(std::make_tuple(cond::time::from_boost(time), label), value));
681  }
682  }
683  return true;
684  }
685 
686  virtual Y getFromPayload(PayloadType& payload) = 0;
687  };
688 
689  template <typename PayloadType, typename X, typename Y>
690  class ScatterPlot : public Plot2D<PayloadType, X, Y, MULTI_IOV, 1> {
691  public:
693  // the x axis label will be overwritten by the plot rendering application
694  ScatterPlot(const std::string& title, const std::string& xLabel, const std::string& yLabel)
695  : Base("Scatter", title, xLabel, yLabel) {}
696  ~ScatterPlot() override = default;
697 
698  bool fill() override {
699  auto tag = PlotBase::getTag<0>();
700  for (auto iov : tag.iovs) {
701  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
702  if (payload.get()) {
703  std::tuple<X, Y> value = getFromPayload(*payload);
704  Base::m_plotData.push_back(value);
705  }
706  }
707  return true;
708  }
709 
710  virtual std::tuple<X, Y> getFromPayload(PayloadType& payload) = 0;
711  };
712 
713  //
714  template <typename AxisType, typename PayloadType, IOVMultiplicity IOV_M = UNSPECIFIED_IOV>
715  class Histogram1 : public Plot2D<PayloadType, AxisType, AxisType, IOV_M, 1> {
716  public:
718  // naive implementation, essentially provided as an example...
720  const std::string& xLabel,
721  size_t nbins,
722  float min,
723  float max,
724  const std::string& yLabel = "entries")
725  : Base("Histo1D", title, xLabel, yLabel), m_nbins(nbins), m_min(min), m_max(max) {}
726 
727  //
728  void init() override {
729  if (m_nbins < 1) {
730  edm::LogError("payloadInspector::Histogram1D()")
731  << " trying to book an histogram with less then 1 bin!" << std::endl;
732  }
733 
734  if (m_min > m_max) {
735  edm::LogError("payloadInspector::Histogram1D()")
736  << " trying to book an histogram with minimum " << m_min << "> maximum" << m_max << " !" << std::endl;
737  }
738 
739  Base::m_plotData.clear();
740  float binSize = (m_max - m_min) / m_nbins;
741  if (binSize > 0) {
742  m_binSize = binSize;
743  Base::m_plotData.resize(m_nbins);
744  for (size_t i = 0; i < m_nbins; i++) {
745  Base::m_plotData[i] = std::make_tuple(m_min + i * m_binSize, 0);
746  }
747  }
748  }
749 
750  // to be used to fill the histogram!
752  // ignoring underflow/overflows ( they can be easily added - the total entries as well )
753  if (!Base::m_plotData.empty() && (value < m_max) && (value >= m_min)) {
754  size_t ibin = (value - m_min) / m_binSize;
755  std::get<1>(Base::m_plotData[ibin]) += weight;
756  }
757  }
758 
759  // to be used to fill the histogram!
761  if (bin < Base::m_plotData.size()) {
762  std::get<1>(Base::m_plotData[bin]) = weight;
763  }
764  }
765 
766  // this one can ( and in general should ) be overridden - the implementation should use fillWithValue
767  bool fill() override {
768  auto tag = PlotBase::getTag<0>();
769  for (auto iov : tag.iovs) {
770  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
771  if (payload.get()) {
774  }
775  }
776  return true;
777  }
778 
779  // implement this one if you use the default fill implementation, otherwise ignore it...
780  virtual AxisType getFromPayload(PayloadType& payload) { return 0; }
781 
782  private:
783  float m_binSize = 0;
784  size_t m_nbins;
785  float m_min;
786  float m_max;
787  };
788 
789  // clever way to reduce the number of templated arguments
790  // see https://stackoverflow.com/questions/3881633/reducing-number-of-template-arguments-for-class
791  // for reference
792 
793  template <typename PayloadType, IOVMultiplicity IOV_M = UNSPECIFIED_IOV>
795 
796  template <typename PayloadType, IOVMultiplicity IOV_M = UNSPECIFIED_IOV>
798 
799  //
800  template <typename PayloadType, IOVMultiplicity IOV_M = UNSPECIFIED_IOV>
801  class Histogram2D : public Plot3D<PayloadType, float, float, float, IOV_M, 1> {
802  public:
804  // naive implementation, essentially provided as an example...
806  const std::string& xLabel,
807  size_t nxbins,
808  float xmin,
809  float xmax,
810  const std::string& yLabel,
811  size_t nybins,
812  float ymin,
813  float ymax)
814  : Base("Histo2D", title, xLabel, yLabel, "entries"),
815  m_nxbins(nxbins),
816  m_xmin(xmin),
817  m_xmax(xmax),
818  m_nybins(nybins),
819  m_ymin(ymin),
820  m_ymax(ymax) {}
821 
822  //
823  void init() override {
824  // some protections
825  if ((m_nxbins < 1) || (m_nybins < 1)) {
826  edm::LogError("payloadInspector::Histogram2D()")
827  << " trying to book an histogram with less then 1 bin!" << std::endl;
828  }
829 
830  if (m_xmin > m_xmax) {
831  edm::LogError("payloadInspector::Histogram2D()") << " trying to book an histogram with x-minimum " << m_xmin
832  << "> x-maximum" << m_xmax << " !" << std::endl;
833  }
834 
835  if (m_ymin > m_ymax) {
836  edm::LogError("payloadInspector::Histogram2D()") << " trying to book an histogram with y-minimum " << m_ymin
837  << "> y-maximum" << m_ymax << " !" << std::endl;
838  }
839 
840  Base::m_plotData.clear();
841  float xbinSize = (m_xmax - m_xmin) / m_nxbins;
842  float ybinSize = (m_ymax - m_ymin) / m_nybins;
843  if (xbinSize > 0 && ybinSize > 0) {
844  m_xbinSize = xbinSize;
845  m_ybinSize = ybinSize;
847  for (size_t i = 0; i < m_nybins; i++) {
848  for (size_t j = 0; j < m_nxbins; j++) {
849  Base::m_plotData[i * m_nxbins + j] = std::make_tuple(m_xmin + j * m_xbinSize, m_ymin + i * m_ybinSize, 0);
850  }
851  }
852  }
853  }
854 
855  // to be used to fill the histogram!
856  void fillWithValue(float xvalue, float yvalue, float weight = 1) {
857  // ignoring underflow/overflows ( they can be easily added - the total entries as well )
858  if (!Base::m_plotData.empty() && xvalue < m_xmax && xvalue >= m_xmin && yvalue < m_ymax && yvalue >= m_ymin) {
859  size_t ixbin = (xvalue - m_xmin) / m_xbinSize;
860  size_t iybin = (yvalue - m_ymin) / m_ybinSize;
861  std::get<2>(Base::m_plotData[iybin * m_nxbins + ixbin]) += weight;
862  }
863  }
864 
865  // this one can ( and in general should ) be overridden - the implementation should use fillWithValue
866  bool fill() override {
867  auto tag = PlotBase::getTag<0>();
868  for (auto iov : tag.iovs) {
869  std::shared_ptr<PayloadType> payload = Base::fetchPayload(std::get<1>(iov));
870  if (payload.get()) {
871  std::tuple<float, float> value = getFromPayload(*payload);
872  fillWithValue(std::get<0>(value), std::get<1>(value));
873  }
874  }
875  return true;
876  }
877 
878  // implement this one if you use the default fill implementation, otherwise ignore it...
879  virtual std::tuple<float, float> getFromPayload(PayloadType& payload) {
880  float x = 0;
881  float y = 0;
882  return std::make_tuple(x, y);
883  }
884 
885  private:
886  size_t m_nxbins;
887  float m_xbinSize = 0;
888  float m_xmin;
889  float m_xmax;
890  float m_ybinSize = 0;
891  size_t m_nybins;
892  float m_ymin;
893  float m_ymax;
894  };
895 
896  //
897  template <typename PayloadType, IOVMultiplicity IOV_M = UNSPECIFIED_IOV, int NTAGS = 0>
898  class PlotImage : public PlotImpl<IOV_M, NTAGS> {
899  public:
901  explicit PlotImage(const std::string& title) : Base("Image", title) {
902  std::string payloadTypeName = cond::demangledName(typeid(PayloadType));
905  }
906 
908 
909  std::shared_ptr<PayloadType> fetchPayload(const cond::Hash& payloadHash) {
910  return PlotBase::fetchPayload<PayloadType>(payloadHash);
911  }
912 
913  protected:
915  };
916 
917  } // namespace payloadInspector
918 
919 } // namespace cond
920 
921 #endif
virtual bool fill(const std::vector< std::tuple< cond::Time_t, cond::Hash >> &iovs)
size
Write out results.
Definition: start.py:1
virtual AxisType getFromPayload(PayloadType &payload)
std::map< std::string, std::string > m_inputParamValues
PlotImage(const std::string &title)
cond::Tag_t getTagInfo(const std::string &tag)
double seconds()
std::string serializeData() override
void fillWithValue(float xvalue, float yvalue, float weight=1)
std::set< std::string > m_inputParams
ret
prodAgent to be discontinued
Plot2D(const std::string &type, const std::string &title, const std::string xLabel, const std::string &yLabel)
std::vector< std::string > m_tagNames
std::string serializeData() override
void setAnnotations(const std::string &type, const std::string &title, IOVMultiplicity IOV_M, int NTAGS, PlotAnnotations &target)
PlotImpl(const std::string &type, const std::string &title)
static constexpr const char *const PAYLOAD_TYPE_K
Plot2D< PayloadType, X, Y, MULTI_IOV, 1 > Base
#define X(str)
Definition: MuonsGrabber.cc:38
static constexpr const char *const ZAXIS_K
std::string to_string(const V &value)
Definition: OMSAccess.h:71
Definition: weight.py:1
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
static const char *const JSON_FORMAT_VERSION
PlotImpl(const std::string &type, const std::string &title)
Log< level::Error, false > LogError
Plot3D(const std::string &type, const std::string &title, const std::string xLabel, const std::string &yLabel, const std::string &zLabel)
cond::persistency::Session dbSession()
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:18
static constexpr const char *const PLOT_TYPE_K
Plot2D< PayloadType, unsigned long long, Y, MULTI_IOV, 1 > Base
Plot2D< PayloadType, std::tuple< float, std::string >, Y, MULTI_IOV, 1 > Base
ScatterPlot(const std::string &title, const std::string &xLabel, const std::string &yLabel)
std::string serialize(const PlotAnnotations &annotations, const std::vector< std::tuple< X, Y >> &data)
std::vector< std::tuple< X, Y > > m_plotData
char const * label
virtual std::tuple< float, float > getFromPayload(PayloadType &payload)
std::string processData() override
unsigned long long Time_t
Definition: Time.h:14
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t V
PlotImpl(const std::string &type, const std::string &title)
TagReference(const TagReference &rhs)
static constexpr const char *const YAXIS_K
Histogram1(const std::string &title, const std::string &xLabel, size_t nbins, float min, float max, const std::string &yLabel="entries")
virtual Y getFromPayload(PayloadType &payload)=0
std::string serializeValue(const std::string &entryLabel, const V &value)
void fillWithValue(AxisType value, AxisType weight=1)
PlotImpl(const std::string &type, const std::string &title)
TagReference(const std::string &n, const std::pair< cond::Time_t, cond::Time_t > &b, const std::vector< std::tuple< cond::Time_t, cond::Hash >> &i)
std::string Hash
Definition: Types.h:43
RunHistoryPlot(const std::string &title, const std::string &yLabel)
static constexpr const char *const XAXIS_K
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)
Definition: value.py:1
AxisType
Definition: SoftLepton.cc:133
Plot2D< PayloadType, std::tuple< unsigned long long, std::string >, Y, MULTI_IOV, 1 > Base
Plot3D< PayloadType, float, float, float, IOV_M, 1 > Base
PlotImpl< IOV_M, NTAGS > Base
const std::vector< std::tuple< cond::Time_t, cond::Hash > > & iovs
Time_t from_boost(boost::posix_time::ptime bt)
d
Definition: ztail.py:151
std::vector< std::tuple< X, Y, Z > > m_plotData
virtual std::tuple< X, Y > getFromPayload(PayloadType &payload)=0
const std::pair< cond::Time_t, cond::Time_t > & boundary
PlotImpl< IOV_M, NTAGS > Base
const unsigned int SECONDS_PER_LUMI(23)
virtual Y getFromPayload(PayloadType &payload)=0
const std::map< std::string, std::string > & inputParamValues() const
std::string createGlobalIdentifier(bool binary=false)
void addInputParam(const std::string &paramName)
__attribute__((visibility("default"))) inline py
static constexpr const char *const label
PlotImpl< IOV_M, NTAGS > Base
double b
Definition: hdecay.h:118
Histogram2D(const std::string &title, const std::string &xLabel, size_t nxbins, float xmin, float xmax, const std::string &yLabel, size_t nybins, float ymin, float ymax)
TimeHistoryPlot(const std::string &title, const std::string &yLabel)
virtual std::string serializeData()=0
HistoryPlot(const std::string &title, const std::string &yLabel)
std::string serializeAnnotations(const PlotAnnotations &annotations)
std::string serializeData() override
~Plot3D() override=default
cond::Time_t Time_t
Definition: Time.h:18
Definition: plugin.cc:23
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
cond::persistency::Session m_dbSession
std::map< std::string, std::string > m
double a
Definition: hdecay.h:119
virtual bool fill(const std::vector< std::tuple< cond::Time_t, cond::Hash >> &iovs)
static constexpr const char *const TITLE_K
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)
float x
virtual Y getFromPayload(PayloadType &payload)=0
std::vector< std::pair< cond::Time_t, cond::Time_t > > m_tagBoundaries
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)
~Plot2D() override=default
void fillWithBinAndValue(size_t bin, AxisType weight=1)
PlotImpl(const std::string &type, const std::string &title)
PlotImpl(const std::string &type, const std::string &title)
Plot2D< PayloadType, AxisType, AxisType, IOV_M, 1 > Base
static constexpr const char *const INFO_K
boost::posix_time::ptime to_boost(Time_t iValue)
std::vector< std::vector< std::tuple< cond::Time_t, cond::Hash > > > m_tagIovs
std::shared_ptr< PayloadType > fetchPayload(const cond::Hash &payloadHash)
RunInfoProxy getRunInfo(cond::Time_t start, cond::Time_t end)
Definition: Session.cc:193