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#include <string_view>
32#include <format>
33
35
36public:
37 HGTD_AnalysisAlgBase(const std::string& name, ISvcLocator* svc_locator);
38 virtual ~HGTD_AnalysisAlgBase();
39
40 virtual StatusCode initialize();
41
42 Gaudi::Property<std::string> m_directory_name{
43 this, "DirectoryName", "/HGTD_ANA/", "The output directory name"};
44
53 template <typename T = TH1F, typename... Ts>
54 void bookSubdir(const std::string& trk_sel_name, const std::string& time_wp,
55 const std::string& hist_name, const std::string& title,
56 Ts... args) {
57 // check if hist exists already and warn for duplication
58 // doesn't do anything then...
59 const std::string name = trk_sel_name + "/" + time_wp + "/" + hist_name;
60 if (m_histos.contains(name)) {
61 ATH_MSG_WARNING("You are duplicating histogram: "
62 << name << ", this is not a good idea");
63 return;
64 }
65 auto * ptrT = new T(hist_name.c_str(), title.c_str(), args...);
66 m_histos.emplace(name, ptrT);
67 ptrT->Sumw2();
68 if (not m_hist_svc->regHist(m_directory_name + name, ptrT).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.contains(name)) {
78 ATH_MSG_WARNING("You are duplicating histogram: "
79 << name << ", this is not a good idea");
80 return;
81 }
82 auto * ptrT = new T(name.c_str(), title.c_str(), args...);
83 m_histos.emplace(name, ptrT);
84 ptrT->Sumw2();
85 if (not m_hist_svc->regHist(m_directory_name + name,ptrT).isSuccess()) {
86 ATH_MSG_WARNING("Failed to book " << name);
87 }
88 }
89
90 template <typename... Ts>
91 void bookEffSubdir(const std::string& trk_sel_name,
92 const std::string& time_wp, const std::string& hist_name,
93 const std::string& title, Ts... args) {
94 // check if hist exists already and warn for duplication
95 // doesn't do anything then...
96 const std::string name = trk_sel_name + "/" + time_wp + "/" + hist_name;
97 if (m_histos.contains(name)) {
98 ATH_MSG_WARNING("You are duplicating histogram: "
99 << name << ", this is not a good idea");
100 return;
101 }
102 auto * ptrT = new TEfficiency(hist_name.c_str(), title.c_str(), args...);
103 m_histos.emplace(name, ptrT);
104 if (not m_hist_svc->regEfficiency(m_directory_name + name, ptrT).isSuccess()) {
105 ATH_MSG_WARNING("Failed to book " << name);
106 }
107 }
108
109 template <typename... Ts>
110 void bookEff(const std::string& name, const std::string& title, Ts... args) {
111 // check if hist exists already and warn for duplication
112 // doesn't do anything then...
113 if (m_histos.contains(name)) {
114 ATH_MSG_WARNING("You are duplicating histogram: "
115 << name << ", this is not a good idea");
116 return;
117 }
118 auto * ptrT = new TEfficiency(name.c_str(), title.c_str(), args...);
119 m_histos.emplace(name,ptrT);
120 if (not m_hist_svc->regEfficiency(m_directory_name + name,ptrT).isSuccess()) {
121 ATH_MSG_WARNING("Failed to book " << name);
122 }
123 }
124
129 template <typename T, typename... Ts>
130 void fill(std::string_view name, Ts... args) {
131 auto it = m_histos.find(name);
132 if (it == m_histos.end() or it->second == nullptr) {
134 "[HistogramHandler::fill] ERROR: you are attempting to fill "
135 "a histogram with name "
136 << name << " which doesn't exist!");
137 return;
138 }
139 auto * ptrT = dynamic_cast<T*>(it->second);
140 if (!ptrT)[[unlikely]]{
141 ATH_MSG_WARNING("Cast failed for "<< name);
142 return;
143 }
144 ptrT->Fill(args...);
145 }
146
147 template <typename T, typename... Ts>
148 void fillSubdir(std::string_view trk_sel_name, std::string_view time_wp,
149 std::string_view hist_name, Ts... args) {
150 const std::string name = std::format("{}/{}/{}", trk_sel_name, time_wp, hist_name);
151 auto it = m_histos.find(name);
152 if (it == m_histos.end() or it->second == nullptr) {
154 "[HistogramHandler::fill] ERROR: you are attempting to fill "
155 "a histogram with name "
156 << name << " which doesn't exist!");
157 return;
158 }
159 auto * ptrT = dynamic_cast<T*>(it->second);
160 if (!ptrT)[[unlikely]]{
161 ATH_MSG_WARNING("Cast failed for "<< name);
162 return;
163 }
164 ptrT->Fill(args...);
165 }
166
167 template <typename... Ts>
168 void fillEff(std::string_view name, Ts... args) {
169 const auto it = m_histos.find(name);
170 if (it == m_histos.end() or it->second == nullptr)[[unlikely]] {
172 "[HistogramHandler::fillEff] ERROR: you are attempting to fill "
173 "a histogram with name "
174 << name << " which doesn't exist!");
175 return;
176 }
177 auto * ptrT = dynamic_cast<TEfficiency*>(it->second);
178 if (!ptrT)[[unlikely]]{
179 ATH_MSG_WARNING("Cast failed for "<< name);
180 return;
181 }
182 ptrT->Fill(args...);
183 }
184
185 template <typename... Ts>
186 void fillEffSubDir(std::string_view trk_sel_name,
187 std::string_view wp_name, std::string_view hist_name,
188 Ts... args) {
189 const std::string name = std::format("{}/{}/{}", trk_sel_name, wp_name, hist_name);
190 auto it = m_histos.find(name) ;
191 if (it == m_histos.end() or it->second == nullptr) {
193 "[HistogramHandler::fill] ERROR: you are attempting to fill "
194 "a histogram with name "
195 << name << " which doesn't exist!");
196 return;
197 }
198 auto * ptrT = dynamic_cast<TEfficiency*>(it->second);
199 if (!ptrT)[[unlikely]]{
200 ATH_MSG_WARNING("Cast failed for "<< name);
201 return;
202 }
203 ptrT->Fill(args...);
204 }
205
206protected:
207 ServiceHandle<ITHistSvc> m_hist_svc{this, "THistSvc", "THistSvc"};
208
209private:
210 std::map<std::string, TObject*, std::less<>> m_histos;
211};
212
213#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 bookEff(const std::string &name, const std::string &title, Ts... args)
void fill(std::string_view name, Ts... args)
Templated function for filling histograms The type of the histogram is passed as a template parameter...
std::map< std::string, TObject *, std::less<> > m_histos
virtual StatusCode initialize()
HGTD_AnalysisAlgBase(const std::string &name, ISvcLocator *svc_locator)
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 fillEff(std::string_view name, Ts... args)
void fillEffSubDir(std::string_view trk_sel_name, std::string_view wp_name, std::string_view hist_name, Ts... args)
void book(const std::string &name, const std::string &title, Ts... args)
void fillSubdir(std::string_view trk_sel_name, std::string_view time_wp, std::string_view hist_name, Ts... args)
#define unlikely(x)