ATLAS Offline Software
Loading...
Searching...
No Matches
HGTD_AnalysisAlgBase.h
Go to the documentation of this file.
1
14
15#ifndef HGTD_ANALYSIS_ANALYSISALGBASE_H
16#define HGTD_ANALYSIS_ANALYSISALGBASE_H
17
19
20// Athena includes
21#include "GaudiKernel/ITHistSvc.h"
22#include "GaudiKernel/ServiceHandle.h"
23
24// ROOT includes
25#include "TEfficiency.h"
26#include "TH1.h"
27
28// stl includes
29#include <map>
30#include <string>
31
33
34public:
35 HGTD_AnalysisAlgBase(const std::string& name, ISvcLocator* svc_locator);
36 virtual ~HGTD_AnalysisAlgBase();
37
38 virtual StatusCode initialize();
39
40 Gaudi::Property<std::string> m_directory_name{
41 this, "DirectoryName", "/HGTD_ANA/", "The output directory name"};
42
51 template <typename T = TH1F, typename... Ts>
52 void bookSubdir(const std::string& trk_sel_name, const std::string& time_wp,
53 const std::string& hist_name, const std::string& title,
54 Ts... args) {
55 // check if hist exists already and warn for duplication
56 // doesn't do anything then...
57 std::string name = trk_sel_name + "/" + time_wp + "/" + hist_name;
58 if (m_histos.find(name) != m_histos.end()) {
59 ATH_MSG_WARNING("You are duplicating histogram: "
60 << name << ", this is not a good idea\n");
61 return;
62 }
63 m_histos[name] = new T(hist_name.c_str(), title.c_str(), args...);
64 dynamic_cast<T*>(m_histos[name])->Sumw2();
65 if (not m_hist_svc
66 ->regHist(m_directory_name + name,
67 dynamic_cast<T*>(m_histos[name]))
68 .isSuccess()) {
69 ATH_MSG_WARNING("Failed to book " << name);
70 }
71 }
72
73 template <typename T, typename... Ts>
74 void book(const std::string& name, const std::string& title, Ts... args) {
75 // check if hist exists already and warn for duplication
76 // doesn't do anything then...
77 if (m_histos.find(name) != m_histos.end()) {
78 ATH_MSG_WARNING("You are duplicating histogram: "
79 << name << ", this is not a good idea\n");
80 return;
81 }
82 m_histos[name] = new T(name.c_str(), title.c_str(), args...);
83 dynamic_cast<T*>(m_histos[name])->Sumw2();
84 if (not m_hist_svc
85 ->regHist(m_directory_name + name,
86 dynamic_cast<T*>(m_histos[name]))
87 .isSuccess()) {
88 ATH_MSG_WARNING("Failed to book " << name);
89 }
90 }
91
92 template <typename... Ts>
93 void bookEffSubdir(const std::string& trk_sel_name,
94 const std::string& time_wp, const std::string& hist_name,
95 const std::string& title, Ts... args) {
96 // check if hist exists already and warn for duplication
97 // doesn't do anything then...
98 std::string name = trk_sel_name + "/" + time_wp + "/" + hist_name;
99 if (m_histos.find(name) != m_histos.end()) {
100 ATH_MSG_WARNING("You are duplicating histogram: "
101 << name << ", this is not a good idea\n");
102 return;
103 }
104 m_histos[name] = new TEfficiency(hist_name.c_str(), title.c_str(), args...);
105 if (not m_hist_svc
106 ->regGraph(m_directory_name + name,
107 reinterpret_cast<TGraph*>(m_histos[name]))
108 .isSuccess()) {
109 ATH_MSG_WARNING("Failed to book " << name);
110 }
111 }
112
113 template <typename... Ts>
114 void bookEff(const std::string& name, const std::string& title, Ts... args) {
115 // check if hist exists already and warn for duplication
116 // doesn't do anything then...
117 if (m_histos.find(name) != m_histos.end()) {
118 ATH_MSG_WARNING("You are duplicating histogram: "
119 << name << ", this is not a good idea\n");
120 return;
121 }
122 m_histos[name] = new TEfficiency(name.c_str(), title.c_str(), args...);
123 if (not m_hist_svc
124 ->regGraph(m_directory_name + name,
125 reinterpret_cast<TGraph*>(m_histos[name]))
126 .isSuccess()) {
127 ATH_MSG_WARNING("Failed to book " << name);
128 }
129 }
130
135 template <typename T, typename... Ts>
136 void fill(const std::string& name, Ts... args) {
137 if (m_histos[name] == nullptr or m_histos.find(name) == m_histos.end()) {
139 "[HistogramHandler::fill] ERROR: you are attempting to fill "
140 "a histogram with name "
141 << name << " which doesn't exist!\n");
142 return;
143 }
144 dynamic_cast<T*>(m_histos[name])->Fill(args...);
145 }
146
147 template <typename T, typename... Ts>
148 void fillSubdir(const std::string& trk_sel_name, const std::string& time_wp,
149 const std::string& hist_name, Ts... args) {
150 std::string name = trk_sel_name + "/" + time_wp + "/" + hist_name;
151 if (m_histos[name] == nullptr or m_histos.find(name) == m_histos.end()) {
153 "[HistogramHandler::fill] ERROR: you are attempting to fill "
154 "a histogram with name "
155 << name << " which doesn't exist!\n");
156 return;
157 }
158 dynamic_cast<T*>(m_histos[name])->Fill(args...);
159 }
160
161 template <typename... Ts> void fillEff(const std::string& name, Ts... args) {
162 if (m_histos[name] == nullptr or m_histos.find(name) == m_histos.end()) {
164 "[HistogramHandler::fill] ERROR: you are attempting to fill "
165 "a histogram with name "
166 << name << " which doesn't exist!\n");
167 return;
168 }
169 dynamic_cast<TEfficiency*>(m_histos[name])->Fill(args...);
170 }
171
172 template <typename... Ts>
173 void fillEffSubDir(const std::string& trk_sel_name,
174 const std::string& wp_name, const std::string& hist_name,
175 Ts... args) {
176 std::string name = trk_sel_name + "/" + wp_name + "/" + hist_name;
177 if (m_histos[name] == nullptr or m_histos.find(name) == m_histos.end()) {
179 "[HistogramHandler::fill] ERROR: you are attempting to fill "
180 "a histogram with name "
181 << name << " which doesn't exist!\n");
182 return;
183 }
184 dynamic_cast<TEfficiency*>(m_histos[name])->Fill(args...);
185 }
186
187protected:
188 ServiceHandle<ITHistSvc> m_hist_svc{this, "THistSvc", "THistSvc"};
189
190private:
191 std::map<std::string, TObject*> m_histos;
192};
193
194#endif // HGTD_ANALYSIS_ANALYSISALGBASE_H
#define ATH_MSG_WARNING(x,...)
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor.
Gaudi::Property< std::string > m_directory_name
void bookSubdir(const std::string &trk_sel_name, const std::string &time_wp, const std::string &hist_name, const std::string &title, Ts... args)
Templated function for booking histograms The type of the histogram is passed as a template parameter...
void fillEff(const std::string &name, Ts... args)
void bookEff(const std::string &name, const std::string &title, Ts... args)
std::map< std::string, TObject * > m_histos
void fill(const std::string &name, Ts... args)
Templated function for filling histograms The type of the histogram is passed as a template parameter...
virtual StatusCode initialize()
HGTD_AnalysisAlgBase(const std::string &name, ISvcLocator *svc_locator)
void fillEffSubDir(const std::string &trk_sel_name, const std::string &wp_name, const std::string &hist_name, Ts... args)
void bookEffSubdir(const std::string &trk_sel_name, const std::string &time_wp, const std::string &hist_name, const std::string &title, Ts... args)
ServiceHandle< ITHistSvc > m_hist_svc
void fillSubdir(const std::string &trk_sel_name, const std::string &time_wp, const std::string &hist_name, Ts... args)
void book(const std::string &name, const std::string &title, Ts... args)