Project
Loading...
Searching...
No Matches
ValueMonitor.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef ALICEO2_MATHUTILS_VALUEMONITOR_H_
13#define ALICEO2_MATHUTILS_VALUEMONITOR_H_
14
15#include "TH1.h"
16#include <unordered_map>
17#include <string>
18
19namespace o2
20{
21namespace utils
22{
23
24/*
25 ValueMonitor: Facility to record values in a hist
26
27 Mainly meant as a service class that makes it easy
28 to dump variable values (within an algorithm) to a histogram
29 for later visual inspection.
30 The class is similar in spirit with the TreeStreamer facility
31 but complementary since directly using histograms in memory.
32
33 Different histograms are saved to the same file.
34
35 ```C++
36 ValueMonitor mon(filename);
37
38 float x;
39 mon.Collect<float>("x", x); --> collects x in histogram named "x"
40
41 double y;
42 mon.Collect<double>("y", y); --> collects y in histogram named "y"
43 ```
44*/
46{
47 public:
48 ValueMonitor(std::string filename);
50
52 template <typename T>
53 void Collect(const char* key, T value);
54
55 private:
56 std::string mFileName; // name of file where histograms are dumped to
57
58 std::unordered_map<const char*, TH1*> mHistos; // container of histograms (identified by name)
59};
60
61namespace
62{
63template <typename T>
64inline TH1* makeHist(const char* key)
65{
66 return nullptr;
67}
68
69template <>
70inline TH1* makeHist<int>(const char* key)
71{
72 return new TH1I(key, key, 200, 0, 1);
73}
74
75template <>
76inline TH1* makeHist<double>(const char* key)
77{
78 return new TH1D(key, key, 200, 0, 1);
79}
80
81template <>
82inline TH1* makeHist<float>(const char* key)
83{
84 return new TH1F(key, key, 200, 0, 1);
85}
86} // namespace
87
88template <typename T>
89inline void ValueMonitor::Collect(const char* key, T value)
90{
91 // see if we have this histogram already
92 TH1* h = nullptr;
93 auto iter = mHistos.find(key);
94 if (iter == mHistos.end()) {
95 auto newHist = makeHist<T>(key);
96 newHist->SetCanExtend(TH1::kAllAxes);
97 mHistos[key] = newHist;
98 h = newHist;
99 } else {
100 h = (*iter).second;
101 }
102 h->Fill(value);
103}
104
105} // namespace utils
106} // namespace o2
107
108#endif
StringRef key
Class for time synchronization of RawReader instances.
void Collect(const char *key, T value)
main interface to add a value to a histogram called "key"
GLsizei const GLfloat * value
Definition glcorearb.h:819
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
Common utility functions.
std::string filename()