ATLAS Offline Software
Loading...
Searching...
No Matches
StandaloneL1TopoHistSvc.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
5#include "TFile.h"
6#include "TH1.h"
7#include "TH2.h"
8
9#include <ostream>
10#include <map>
11
13
14
16{
17public:
18
20 TrigConfMessaging("StandaloneL1TopoHistSvc")
21 {}
22
24 for( const auto & h : m_hists1D ) {
25 delete h.second;
26 }
27 for( const auto & h : m_hists2D ) {
28 delete h.second;
29 }
30 }
31
32 void registerHist(TH1 * h) {
33 if(h != nullptr) {
34 TRG_MSG_DEBUG("Registering histogram " << h->GetName());
35 const std::string key = h->GetName();
36 if( m_hists1D.find(key) == end(m_hists1D) ) {
37 m_hists1D[key] = h;
38 } else {
39 TRG_MSG_WARNING("StandaloneL1TopoHistSvc::registerHist: a histogram " << key << " exists already. Will keep the first one and delete the newly requested.");
40 delete h;
41 }
42 }
43 }
44
45 void registerHist(TH2 * h) {
46 if(h != nullptr) {
47 TRG_MSG_DEBUG("Registering histogram " << h->GetName());
48 const std::string key = h->GetName();
49 if( m_hists2D.find(key) == end(m_hists2D) ) {
50 m_hists2D[key] = h;
51 } else {
52 TRG_MSG_WARNING("StandaloneL1TopoHistSvc::registerHist: a histogram " << key << " exists already. Will keep the first one and delete the newly requested.");
53 delete h;
54 }
55 }
56 }
57
58 TH1 * findHist(const std::string & histName) {
59 auto colPos = histName.find_first_of('/');
60 std::string realhistName = histName.substr(colPos+1);
61 auto h = m_hists1D.find(realhistName);
62 if( h == end(m_hists1D) ) {
63 return nullptr;
64 } else {
65 return h->second;
66 }
67 }
68
69 void fillHist1D(const std::string & histName,double x) {
70 auto h = m_hists1D.find(histName);
71 if( h == end(m_hists1D) ) {
72 TRG_MSG_WARNING("No histogram found for " << histName);
73 } else
74 h->second->Fill(x);
75 }
76
77 void fillHist2D(const std::string & histName,double x,double y) {
78 auto h = m_hists2D.find(histName);
79 if( h == end(m_hists2D) ) {
80 TRG_MSG_WARNING("No histogram found for " << histName);
81 } else
82 h->second->Fill(x,y);
83 }
84
85 void setBaseDir(const std::string & baseDir) {
86 m_baseDir = baseDir;
87 }
88
89 void save() {
90
91 std::string filename = "L1Topo.root";
92 std::string basepath = "";
93
94 std::string opt = "RECREATE";
95
96 auto colPos = m_baseDir.find_last_of(':');
97 if( colPos != std::string::npos ) {
98 filename = m_baseDir.substr(0, colPos);
99 basepath = m_baseDir.substr(colPos+1);
100 } else {
101 basepath = m_baseDir;
102 }
103
104
105 TFile * f = TFile::Open(filename.c_str(),opt.c_str());
106 for( const auto & h : m_hists1D ) {
107
108 std::string fullName(h.second->GetName());
109 std::string path(basepath);
110
111 auto slashPos = fullName.find_last_of('/');
112 if(slashPos != std::string::npos) {
113 if(path!="")
114 path += "/";
115 path += fullName.substr(0,slashPos);
116 // set the name
117 h.second->SetName( fullName.substr(slashPos+1).c_str() );
118 }
119
120 const char* dir = path.c_str();
121 if( ! f->GetDirectory(dir)) {
122 f->mkdir(dir);
123 }
124 f->cd(dir);
125 h.second->Write();
126 }
127 for( const auto & h : m_hists2D ) {
128
129 std::string fullName(h.second->GetName());
130 std::string path(basepath);
131
132 auto slashPos = fullName.find_last_of('/');
133 if(slashPos != std::string::npos) {
134 if(path!="")
135 path += "/";
136 path += fullName.substr(0,slashPos);
137 // set the name
138 h.second->SetName( fullName.substr(slashPos+1).c_str() );
139 }
140
141 const char* dir = path.c_str();
142 if( ! f->GetDirectory(dir)) {
143 f->mkdir(dir);
144 }
145 f->cd(dir);
146 h.second->Write();
147 }
148 f->Write();
149 f->Close();
150 TRG_MSG_INFO("Wrote " << m_hists1D.size()+m_hists2D.size() << " l1topo algorithm histograms to file " << filename);
151 }
152
153private:
154
155 std::map<std::string,TH1*> m_hists1D;
156 std::map<std::string,TH2*> m_hists2D;
157 std::string m_baseDir {""};
158
159};
160
161
162
163
164// the interface class
165
169
172
173// forwarding the calls from the interface class
174
175void
177 //cout << "Called StandaloneL1TopoHistSvc::registerHist(" << h->GetName() << ")" << endl;
178 m_impl->registerHist(h);
179}
180
181void
183 //cout << "Called StandaloneL1TopoHistSvc::registerHist(" << h->GetName() << ")" << endl;
184 m_impl->registerHist(h);
185}
186
187TH1 *
188StandaloneL1TopoHistSvc::findHist(const std::string & histName) {
189 //cout << "Called StandaloneL1TopoHistSvc::findHist(" << histName << ")" << endl;
190 return m_impl->findHist(histName);
191}
192
193void StandaloneL1TopoHistSvc::fillHist1D(const std::string & histName, double x) {
194 m_impl->fillHist1D(histName,x);
195}
196
197void StandaloneL1TopoHistSvc::fillHist2D(const std::string & histName, double x, double y) {
198 m_impl->fillHist2D(histName,x,y);
199}
200
201void
202StandaloneL1TopoHistSvc::setBaseDir(const std::string & baseDir) {
203 m_impl->setBaseDir(baseDir);
204}
205
206
207void
211
212
213
Messaging base class for TrigConf code shared with Lvl1 ( AthMessaging)
#define y
#define x
Header file for AthHistogramAlgorithm.
void fillHist1D(const std::string &histName, double x)
void fillHist2D(const std::string &histName, double x, double y)
std::unique_ptr< StandaloneL1TopoHistSvcImpl > m_impl
virtual TH1 * findHist(const std::string &histName) override
virtual void setBaseDir(const std::string &baseDir) override
virtual void fillHist1D(const std::string &histName, double x) override
virtual void registerHist(TH1 *h) override
virtual void fillHist2D(const std::string &histName, double x, double y) override
Class to provide easy access to TrigConf::MsgStream for TrigConf classes.
TrigConfMessaging(const std::string &name)
Constructor with parameters.