CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MonitorElement.h
Go to the documentation of this file.
1 #ifndef DQMSERVICES_CORE_MONITOR_ELEMENT_H
2 #define DQMSERVICES_CORE_MONITOR_ELEMENT_H
3 
4 #if __GNUC__ && !defined DQM_DEPRECATED
5 //#define DQM_DEPRECATED __attribute__((deprecated))
6 #define DQM_DEPRECATED
7 #endif
8 
10 
12 
14 
15 #include "TF1.h"
16 #include "TH1F.h"
17 #include "TH1S.h"
18 #include "TH1D.h"
19 #include "TH2F.h"
20 #include "TH2S.h"
21 #include "TH2D.h"
22 #include "TH3F.h"
23 #include "TProfile.h"
24 #include "TProfile2D.h"
25 #include "TObjString.h"
26 #include "TAxis.h"
27 
28 #include <mutex>
29 #include <memory>
30 #include <string>
31 #include <atomic>
32 #include <sstream>
33 #include <iomanip>
34 #include <cassert>
35 #include <cstdint>
36 #include <sys/time.h>
37 #include <oneapi/tbb/spin_mutex.h>
38 
39 // TODO: cleaup the usages and remove.
42 
43 // TODO: move to a better location (changing all usages)
44 namespace dqm {
47  namespace qstatus {
48  static const int OTHER = 30; //< Anything but 'ok','warning' or 'error'.
49  static const int DISABLED = 50; //< Test has been disabled.
50  static const int INVALID = 60; //< Problem preventing test from running.
51  static const int INSUF_STAT = 70; //< Insufficient statistics.
52  static const int DID_NOT_RUN = 90; //< Algorithm did not run.
53  static const int STATUS_OK = 100; //< Test was succesful.
54  static const int WARNING = 200; //< Test had some problems.
55  static const int ERROR = 300; //< Test has failed.
56  } // namespace qstatus
57 
58  namespace me_util {
60  }
61 } // namespace dqm
62 
63 // forward declarations for all our friends
64 namespace dqm::implementation {
65  class DQMStore;
66  class IBooker;
67 } // namespace dqm::implementation
68 struct DQMTTreeIO;
69 namespace dqm {
70  class DQMFileSaverPB;
71 }
72 class DQMService;
73 class QualityTester;
74 
75 namespace dqm::impl {
76 
77  using dqmmutex = tbb::spin_mutex;
78 
79  struct Access {
80  std::unique_lock<dqmmutex> guard_;
83  };
84  // TODO: can this be the same type, just const?
85  struct AccessMut {
86  std::unique_lock<dqmmutex> guard_;
89  };
90 
94  Access access() { return Access{std::unique_lock<dqmmutex>(lock_), data_.key_, data_.value_}; }
95  AccessMut accessMut() { return AccessMut{std::unique_lock<dqmmutex>(lock_), data_.key_, data_.value_}; }
96  };
97 
100  // these need to create and destroy MEs.
103  // these need to access some of the IO related methods.
104  friend ::DQMTTreeIO; // declared in DQMRootSource
105  friend ::dqm::DQMFileSaverPB;
106  friend ::DQMService;
107  // this one only needs syncCoreObject.
108  friend ::QualityTester;
109 
110  public:
113 
114  // Comparison helper used in DQMStore to insert into sets. This needs deep
115  // private access to the MEData, that is why it lives here.
116  struct MEComparison {
117  using is_transparent = int; // magic marker to allow C++14 heterogeneous set lookup.
118 
119  auto make_tuple(MonitorElement *me) const {
120  return std::make_tuple(std::reference_wrapper(me->getPathname()), std::reference_wrapper(me->getName()));
121  }
123  return std::make_tuple(std::reference_wrapper(path.getDirname()), std::reference_wrapper(path.getObjectname()));
124  }
125  bool operator()(MonitorElement *left, MonitorElement *right) const {
126  return make_tuple(left) < make_tuple(right);
127  }
128  bool operator()(MonitorElement *left, MonitorElementData::Path const &right) const {
129  return make_tuple(left) < make_tuple(right);
130  }
131  bool operator()(MonitorElementData::Path const &left, MonitorElement *right) const {
132  return make_tuple(left) < make_tuple(right);
133  }
134  bool operator()(MonitorElementData::Path const &left, MonitorElementData::Path const &right) const {
135  return make_tuple(left) < make_tuple(right);
136  }
137  };
138 
139  private:
140  std::shared_ptr<MutableMonitorElementData> mutable_; // only set if this is a mutable copy of this ME
141  // there are no immutable MEs at this time, but we might need them in the future.
151  const Access access() const {
152  // First, check if there is a mutable object
153  if (mutable_) {
154  // if there is a mutable object, that is the truth, and we take a lock.
155  return mutable_->access();
156  } // else
157  throw cms::Exception("LogicError") << "MonitorElement " << getName() << " not backed by any data!";
158  }
159 
161  // For completeness, set the legacy `updated` marker.
162  this->update();
163 
164  // First, check if there is a mutable object
165  if (mutable_) {
166  // if there is a mutable object, that is the truth, and we take a lock.
167  return mutable_->accessMut();
168  } // else
169  throw cms::Exception("LogicError") << "MonitorElement " << getName() << " not backed by any data!";
170  }
171 
172  private:
173  // but internal -- only for DQMStore etc.
174 
175  // Create ME using this data. A ROOT object pointer may be moved into the
176  // new ME. The new ME will own this data.
178  // Create new ME and take ownership of this data.
179  MonitorElement(std::shared_ptr<MutableMonitorElementData> data);
180  // Create a new ME sharing data with this existing ME.
182 
183  // return a new clone of the data of this ME. Calls ->Clone(), new object
184  // is owned by the returned value.
186 
187  // Remove access to the data.
188  std::shared_ptr<MutableMonitorElementData> release();
189 
190  // re-initialize this ME as a shared copy of the other.
191  void switchData(MonitorElement *other);
192  // re-initialize taking ownership of this data.
193  void switchData(std::shared_ptr<MutableMonitorElementData> data);
194 
195  // Replace the ROOT object in this ME's data with the new object, taking
196  // ownership. The old object is deleted.
197  void switchObject(std::unique_ptr<TH1> &&newobject);
198 
199  // copy applicable fileds into the DQMNet core object for compatibility.
200  // In a few places these flags are also still used by the ME.
201  void syncCoreObject();
203 
204  // check if the ME is currently backed by MEData; if false (almost) any
205  // access will throw.
206  bool isValid() const { return mutable_ != nullptr; }
207 
208  // used to implement getQErrors et. al.
209  template <typename FILTER>
210  std::vector<MonitorElementData::QReport *> filterQReports(FILTER filter) const;
211 
212  // legacy interfaces, there are no alternatives but they should not be used
213 
215  bool operator<(const MonitorElement &x) const { return DQMNet::setOrder(data_, x.data_); }
217  static bool CheckBinLabels(const TAxis *a1, const TAxis *a2);
219  uint32_t flags() const { return data_.flags; }
222 
223  // mostly used for IO, should be private.
224  std::string valueString() const;
225  std::string tagString() const;
226  std::string tagLabelString() const;
227  std::string effLabelString() const;
229 
230  // kept for DQMService. data_ is also used for MEComparison, without it
231  // we'd need to keep a copy od the name somewhere else.
233  bool wasUpdated() const { return data_.flags & DQMNet::DQM_PROP_NEW; }
234  void packScalarData(std::string &into, const char *prefix) const;
235  void packQualityData(std::string &into) const;
236  DQMNet::CoreObject data_; //< Core object information.
237 
238  public:
239  MonitorElement &operator=(const MonitorElement &) = delete;
241  virtual ~MonitorElement();
242 
243  public:
244  // good to be used in subsystem code
245 
248 
250  const std::string &getName() const { return this->data_.objname; }
251 
253  const std::string &getPathname() const { return this->data_.dirname; }
254 
257 
259 
261 
263  bool getLumiFlag() const { return access().key.scope_ == MonitorElementData::Scope::LUMI; }
264 
268  auto access = this->accessMut();
269  if (access.value.object_)
270  access.value.object_->SetBit(TH1::kIsAverage);
271  }
273  auto access = this->access();
274  return access.value.object_ && access.value.object_->TestBit(TH1::kIsAverage);
275  }
276 
277  private:
278  // A static assert to check that T actually fits in
279  // int64_t.
280  template <typename T>
282  int checkArray[sizeof(int64_t) - sizeof(T) + 1];
283  };
284 
285  void doFill(int64_t x);
286 
287  public:
288  // filling API.
289 
290  void Fill(long long x) {
292  doFill(static_cast<int64_t>(x));
293  }
294  void Fill(unsigned long long x) {
296  doFill(static_cast<int64_t>(x));
297  }
298  void Fill(unsigned long x) {
300  doFill(static_cast<int64_t>(x));
301  }
302  void Fill(long x) {
304  doFill(static_cast<int64_t>(x));
305  }
306  void Fill(unsigned int x) {
308  doFill(static_cast<int64_t>(x));
309  }
310  void Fill(int x) {
312  doFill(static_cast<int64_t>(x));
313  }
314  void Fill(short x) {
316  doFill(static_cast<int64_t>(x));
317  }
318  void Fill(unsigned short x) {
320  doFill(static_cast<int64_t>(x));
321  }
322  void Fill(char x) {
324  doFill(static_cast<int64_t>(x));
325  }
326  void Fill(unsigned char x) {
328  doFill(static_cast<int64_t>(x));
329  }
330 
331  void Fill(float x) { Fill(static_cast<double>(x)); }
332  void Fill(double x);
333  void Fill(std::string &value);
334 
335  void Fill(double x, double yw);
336  void Fill(double x, double y, double zw);
337  void Fill(double x, double y, double z, double w);
339  void ShiftFillLast(double y, double ye = 0., int32_t xscale = 1);
340 
341  public:
342  // additional APIs, mainly for harvesting.
343 
345  virtual void Reset();
346 
349 
352 
355 
357  const MonitorElementData::QReport *getQReport(const std::string &qtname) const;
359  std::vector<MonitorElementData::QReport *> getQReports() const;
361  void getQReport(bool create, const std::string &qtname, MonitorElementData::QReport *&qr, DQMNet::QValue *&qv);
362 
364  std::vector<MonitorElementData::QReport *> getQWarnings() const;
366  std::vector<MonitorElementData::QReport *> getQErrors() const;
368  std::vector<MonitorElementData::QReport *> getQOthers() const;
369 
370  // const and data-independent -- safe
371  virtual int getNbinsX() const;
372  virtual int getNbinsY() const;
373  virtual int getNbinsZ() const;
374  virtual int getBin(int binx, int biny) const;
375  virtual std::string getAxisTitle(int axis = 1) const;
376  virtual std::string getTitle() const;
377 
378  // const but data-dependent -- semantically unsafe in RECO
379  virtual double getMean(int axis = 1) const;
380  virtual double getMeanError(int axis = 1) const;
381  virtual double getRMS(int axis = 1) const;
382  virtual double getRMSError(int axis = 1) const;
383  virtual double getBinContent(int binx) const;
384  virtual double getBinContent(int binx, int biny) const;
385  virtual double getBinContent(int binx, int biny, int binz) const;
386  virtual double getBinError(int binx) const;
387  virtual double getBinError(int binx, int biny) const;
388  virtual double getBinError(int binx, int biny, int binz) const;
389  virtual double getEntries() const;
390  virtual double getBinEntries(int bin) const;
391  virtual double getBinEntries(int binx, int biny) const;
392  virtual double integral() const;
393 
394  virtual int64_t getIntValue() const;
395  virtual double getFloatValue() const;
396  virtual const std::string &getStringValue() const;
397 
398  // non-const -- thread safety and semantical issues
399  virtual void setBinContent(int binx, double content);
400  virtual void setBinContent(int binx, int biny, double content);
401  virtual void setBinContent(int binx, int biny, int binz, double content);
402  virtual void setBinError(int binx, double error);
403  virtual void setBinError(int binx, int biny, double error);
404  virtual void setBinError(int binx, int biny, int binz, double error);
405  virtual void setBinEntries(int bin, double nentries);
406  virtual void setEntries(double nentries);
407  virtual void divide(const MonitorElement *, const MonitorElement *, double, double, const char *);
408  virtual void setBinLabel(int bin, const std::string &label, int axis = 1);
409  virtual void setAxisRange(double xmin, double xmax, int axis = 1);
410  virtual void setAxisTitle(const std::string &title, int axis = 1);
411  virtual void setAxisTimeDisplay(int value, int axis = 1);
412  virtual void setAxisTimeFormat(const char *format = "", int axis = 1);
413  virtual void setTitle(const std::string &title);
414 
415  // additional operations mainly for booking
416  virtual void setXTitle(std::string const &title);
417  virtual void setYTitle(std::string const &title);
418  virtual void enableSumw2();
419  virtual void disableAlphanumeric();
420  virtual void setOption(const char *option);
421  virtual double getAxisMin(int axis = 1) const;
422  virtual double getAxisMax(int axis = 1) const;
423  // We should avoid extending histograms in general, and if the behaviour
424  // is actually needed, provide a more specific interface rather than
425  // relying on the ROOT behaviour.
427  virtual void setCanExtend(unsigned int value);
428  // We should decide if we support this (or make it default)
430  virtual void setStatOverflows(bool value);
431  virtual bool getStatOverflows();
432 
433  // these should be non-const, since they are potentially not thread-safe
434  virtual TObject const *getRootObject() const;
435  virtual TH1 *getTH1();
436  virtual TH1F *getTH1F();
437  virtual TH1S *getTH1S();
438  virtual TH1D *getTH1D();
439  virtual TH2F *getTH2F();
440  virtual TH2S *getTH2S();
441  virtual TH2D *getTH2D();
442  virtual TH3F *getTH3F();
443  virtual TProfile *getTProfile();
444  virtual TProfile2D *getTProfile2D();
445 
446  private:
447  void incompatible(const char *func) const;
448  TH1 const *accessRootObject(Access const &access, const char *func, int reqdim) const;
449  TH1 *accessRootObject(AccessMut const &, const char *func, int reqdim) const;
450 
451  TAxis const *getAxis(Access const &access, const char *func, int axis) const;
452  TAxis *getAxis(AccessMut const &access, const char *func, int axis) const;
453  };
454 
455 } // namespace dqm::impl
456 
457 // These may become distinct classes in the future.
458 namespace dqm::reco {
460 }
461 namespace dqm::legacy {
463  public:
464  // import constructors
466 
467  // Add ROOT object accessors without cost here so that harvesting code can
468  // still freely use getTH1() and friends.
470  TObject *getRootObject() const override {
471  return const_cast<TObject *>(
473  };
475  virtual TH1 *getTH1() const {
477  };
479  virtual TH1F *getTH1F() const {
481  };
483  virtual TH1S *getTH1S() const {
485  };
487  virtual TH1D *getTH1D() const {
489  };
491  virtual TH2F *getTH2F() const {
493  };
495  virtual TH2S *getTH2S() const {
497  };
499  virtual TH2D *getTH2D() const {
501  };
503  virtual TH3F *getTH3F() const {
505  };
507  virtual TProfile *getTProfile() const {
509  };
511  virtual TProfile2D *getTProfile2D() const {
513  };
514  };
515 } // namespace dqm::legacy
516 namespace dqm::harvesting {
518 }
519 
520 #endif // DQMSERVICES_CORE_MONITOR_ELEMENT_H
virtual std::string getTitle() const
get MonitorElement title
virtual TH2D * getTH2D() const
static const uint32_t DQM_PROP_REPORT_WARN
Definition: DQMNet.h:50
virtual void setTitle(const std::string &title)
set (ie. change) histogram/profile title
std::shared_ptr< MutableMonitorElementData > mutable_
MonitorElement & operator=(const MonitorElement &)=delete
bool operator()(MonitorElementData::Path const &left, MonitorElement *right) const
virtual TH2F * getTH2F() const
virtual DQM_DEPRECATED void setCanExtend(unsigned int value)
virtual double integral() const
get integral of bins
virtual void disableAlphanumeric()
static const int OTHER
std::shared_ptr< MutableMonitorElementData > release()
TAxis const * getAxis(Access const &access, const char *func, int axis) const
virtual int getNbinsY() const
get # of bins in Y-axis
DQMNet::CoreObject data_
double Scalar
Definition: Definitions.h:25
virtual double getAxisMin(int axis=1) const
virtual void setEntries(double nentries)
set # of entries
virtual void setAxisTimeDisplay(int value, int axis=1)
set x-, y-, or z-axis to display time values
MonitorElementData::Key const & key
virtual void setOption(const char *option)
virtual TH1F * getTH1F() const
const std::string & getPathname() const
get pathname of parent folder
virtual void setAxisTimeFormat(const char *format="", int axis=1)
set the format of the time values that are displayed on an axis
uint32_t flags
Definition: DQMNet.h:89
edm::propagate_const< std::unique_ptr< TH1 > > object_
static bool CheckBinLabels(const TAxis *a1, const TAxis *a2)
Check the consistency of the axis labels.
virtual double getBinError(int binx) const
get uncertainty on content of bin (1-D) - See TH1::GetBinError for details
virtual int64_t getIntValue() const
virtual int getNbinsZ() const
get # of bins in Z-axis
bool operator()(MonitorElement *left, MonitorElement *right) const
virtual std::string getAxisTitle(int axis=1) const
get x-, y- or z-axis title (axis=1, 2, 3 respectively)
static const int WARNING
Kind kind() const
Get the type of the monitor element.
const std::string & getName() const
get name of ME
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
bool operator()(MonitorElementData::Path const &left, MonitorElementData::Path const &right) const
virtual bool getStatOverflows()
bool hasOtherReport() const
true if at least of one of the tests returned some other (non-ok) status
tbb::spin_mutex dqmmutex
const MonitorElementData::QReport * getQReport(const std::string &qtname) const
get QReport corresponding to &lt;qtname&gt; (null pointer if QReport does not exist)
MonitorElement(MonitorElementData &&data)
void packQualityData(std::string &into) const
serialise quality report information into a string.
std::vector< MonitorElementData::QReport * > getQOthers() const
from last set of quality tests
void incompatible(const char *func) const
void switchData(MonitorElement *other)
virtual TH1S * getTH1S() const
dqm::reco::DQMStore DQMStore
void Fill(long long x)
virtual void Reset()
Remove all data from the ME, keept the empty histogram with all its settings.
DQM_DEPRECATED void ShiftFillLast(double y, double ye=0., int32_t xscale=1)
std::vector< MonitorElementData::QReport * > getQErrors() const
get errors from last set of quality tests
char const * label
MonitorElementData::QReport QReport
edm::LuminosityBlockID getRunLumi()
bool hasWarning() const
true if at least of one of the quality tests returned a warning
std::vector< MonitorElementData::QReport * > getQReports() const
get map of QReports
static const int INVALID
std::string const & getDirname() const
virtual void setXTitle(std::string const &title)
virtual TProfile2D * getTProfile2D()
bool operator<(const MonitorElement &x) const
Compare monitor elements, for ordering in sets.
MonitorElementData::Kind Kind
virtual int getNbinsX() const
get # of bins in X-axis
virtual void setAxisRange(double xmin, double xmax, int axis=1)
set x-, y- or z-axis range (axis=1, 2, 3 respectively)
virtual double getEntries() const
get # of entries
std::string tagString() const
std::unique_lock< dqmmutex > guard_
static const int INSUF_STAT
void switchObject(std::unique_ptr< TH1 > &&newobject)
bool operator()(MonitorElement *left, MonitorElementData::Path const &right) const
void Fill(unsigned int x)
void doFill(int64_t x)
&quot;Fill&quot; ME method for int64_t
MonitorElementData cloneMEData()
static const int DID_NOT_RUN
static const uint32_t DQM_PROP_REPORT_ERROR
Definition: DQMNet.h:49
static const int DISABLED
std::string dirname
Definition: DQMNet.h:96
std::unique_lock< dqmmutex > guard_
static const uint32_t DQM_PROP_REPORT_OTHER
Definition: DQMNet.h:51
void Fill(unsigned short x)
void Fill(unsigned long x)
virtual void setBinLabel(int bin, const std::string &label, int axis=1)
set bin label for x, y or z axis (axis=1, 2, 3 respectively)
virtual double getBinContent(int binx) const
get content of bin (1-D)
virtual TProfile2D * getTProfile2D() const
virtual TProfile * getTProfile()
std::string objname
Definition: DQMNet.h:97
MonitorElementData::Value & value
MonitorElementData::Scope getScope()
virtual TH2S * getTH2S() const
void Fill(unsigned char x)
void packScalarData(std::string &into, const char *prefix) const
convert scalar data into a string.
std::vector< MonitorElementData::QReport * > filterQReports(FILTER filter) const
MonitorElementData::Value const & value
void update()
Mark the object updated.
virtual double getMean(int axis=1) const
get mean value of histogram along x, y or z axis (axis=1, 2, 3 respectively)
virtual void setYTitle(std::string const &title)
virtual void setBinEntries(int bin, double nentries)
set # of bin entries (to be used for profiles)
virtual int getBin(int binx, int biny) const
get global bin number (for 2-D profiles)
virtual void setBinContent(int binx, double content)
set content of bin (1-D)
dqm::legacy::MonitorElement MonitorElement
bool hasError() const
true if at least of one of the quality tests returned an error
virtual TProfile * getTProfile() const
virtual TH1D * getTH1D() const
virtual double getMeanError(int axis=1) const
MonitorElementData::Key const & key
virtual double getRMS(int axis=1) const
get RMS of histogram along x, y or z axis (axis=1, 2, 3 respectively)
virtual DQM_DEPRECATED void setStatOverflows(bool value)
uint32_t flags() const
Get the object flags.
auto make_tuple(MonitorElement *me) const
bool getLumiFlag() const
true if ME is meant to be stored for each luminosity section
std::string valueString() const
virtual double getBinEntries(int bin) const
get # of bin entries (for profiles)
std::string const & getObjectname() const
bool wasUpdated() const
true if ME was updated in last monitoring cycle
std::vector< MonitorElementData::QReport * > getQWarnings() const
get warnings from last set of quality tests
MonitorElementData::QReport::DQMChannel DQMChannel
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
virtual double getFloatValue() const
void Fill(unsigned long long x)
static bool setOrder(const CoreObject &a, const CoreObject &b)
Definition: DQMNet.h:165
const Access access() const
virtual const std::string & getStringValue() const
std::string tagLabelString() const
return label string for the monitor element tag (eg. &lt;name&gt;t=12345&lt;/name&gt;)
int checkArray[sizeof(int64_t)-sizeof(T)+1]
virtual double getRMSError(int axis=1) const
get RMS uncertainty of histogram along x, y or z axis(axis=1,2,3 respectively)
std::string getFullname() const
get full name of ME including Pathname
static const int STATUS_OK
dqm::reco::DQMStore::IBooker IBooker
auto zw(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:75
TObject * getRootObject() const override
virtual void setBinError(int binx, double error)
set uncertainty on content of bin (1-D)
T w() const
static const uint32_t DQM_PROP_NEW
Definition: DQMNet.h:59
static const uint32_t DQM_PROP_TYPE_MASK
Definition: DQMNet.h:28
TH1 const * accessRootObject(Access const &access, const char *func, int reqdim) const
long double T
virtual TObject const * getRootObject() const
std::string effLabelString() const
return label string for the monitor element tag (eg. &lt;name&gt;t=12345&lt;/name&gt;)
virtual double getAxisMax(int axis=1) const
virtual TH1 * getTH1() const
virtual TH3F * getTH3F() const
virtual void divide(const MonitorElement *, const MonitorElement *, double, double, const char *)
Replace entries with results of dividing num by denom.
#define DQM_DEPRECATED
Definition: DQMStore.cc:2
std::string qualityTagString(const DQMNet::QValue &qv) const
static const int ERROR
auto make_tuple(MonitorElementData::Path const &path) const
virtual void setAxisTitle(const std::string &title, int axis=1)
set x-, y- or z-axis title (axis=1, 2, 3 respectively)