| The Gaudi Framework v27r1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

THistSvc.icc
Go to the documentation of this file.
1 #ifndef GAUDISVC_THISTSVC_ICC
2 #define GAUDISVC_THISTSVC_ICC
3
4 #ifndef GAUDIKERNEL_MSGSTREAM_H
5 #include "GaudiKernel/MsgStream.h"
6 #endif
7
8 #include "GaudiKernel/System.h"
9
10 #include <string>
11 #include <map>
12
13 #include "TObject.h"
14 #include "TFile.h"
15
16 template <typename T>
17 StatusCode THistSvc::regHist_i(T* hist, const std::string& id) {
18
19 GlobalDirectoryRestore restore;
20
21 std::string idr(id);
22 removeDoubleSlash( idr );
23
24 if (idr.find("/") == idr.length()) {
25 m_log << MSG::ERROR << "Badly formed identifier \"" << idr << "\": "
26 << "Must not end with a /" << endmsg;
27 return StatusCode::FAILURE;
28 }
29
30
31 TFile *f = nullptr;
32 std::string stream,rem;
33 if (!findStream(idr, stream, rem, f)) {
34 m_log << MSG::ERROR << "Could not register id: \"" << idr << "\""
35 << endmsg;
36 return StatusCode::FAILURE;
37 }
38
39 std::string uid = "/" + stream + "/" + rem;
40 auto itr = m_uids.find(uid);
41 if (itr != m_uids.end()) {
42 m_log << MSG::ERROR << "already registered an object with identifier \""
43 << idr << "\"" << endmsg;
44 return StatusCode::FAILURE;
45 }
46
47
48 bool temp = false;
49 if ( !f ) {
50 temp = true;
51 if (m_log.level() <= MSG::DEBUG)
52 m_log << MSG::DEBUG << "Historgram with id \"" << idr << "\" is temporary"
53 << endmsg;
54 }
55
56
57 TObject *to = nullptr;
58 THistID hid;
59
60 // check to see if this hist is to be read in;
61 if (!temp && m_files.find(stream)->second.second == READ) {
62
63 if (hist != 0) {
64 m_log << MSG::WARNING << "Registering id: \"" << idr
65 << "\" with non zero pointer!" << endmsg;
66 }
67
68 if (readHist_i(idr,hist).isFailure()) {
69 m_log << MSG::ERROR << "Unable to read in hist" << endmsg;
70 return StatusCode::FAILURE;
71 }
72 to = dynamic_cast<TObject*>(hist);
73 hid = THistID(uid,temp,to,f,m_files.find(stream)->second.second);
74
75 } else if (hist == 0) {
76 m_log << MSG::ERROR << "Unable to read in hist with id: \""
77 << idr << "\"" << endmsg;
78 return StatusCode::FAILURE;
79
80 } else {
81
82 to = dynamic_cast<TObject*>(hist);
83 if (to == 0) {
84 m_log << MSG::ERROR << "Could not dcast to TObject. id: \"" << idr
85 << "\"" << endmsg;
86 return StatusCode::FAILURE;
87 }
88
89 auto oitr = m_tobjs.find(to);
90 if (oitr != m_tobjs.end()) {
91 m_log << MSG::ERROR << "already registered id: \"" << idr
92 << "\" with identifier \"" << oitr->second.id << "\"" << endmsg;
93 return StatusCode::FAILURE;
94 }
95
96 hid = THistID(uid,temp,to,f,m_files.find(stream)->second.second);
97 TDirectory* dir = changeDir(hid);
98
99 if ( dynamic_cast<TTree*>(hist) ) {
100 dynamic_cast<TTree*>(hist)->SetDirectory(dir);
101 } else if ( dynamic_cast<TH1*>(hist) ) {
102 dynamic_cast<TH1*>(hist)->SetDirectory(dir);
103 } else if ( dynamic_cast<TGraph*>(hist) ) {
104 dir->Append(hist);
105 } else {
106 m_log << MSG::ERROR << "id: \"" << idr
107 << "\" is not a TH, TTree, or TGraph. Attaching it to current dir."
108 << endmsg;
109 dir->Append(hist);
110 }
111
112 }
113
114 std::string fname;
115 if ( !f ) {
116 fname = "none";
117 } else {
118 fname = f->GetName();
119 }
120
121 if (m_log.level() <= MSG::DEBUG)
122 m_log << MSG::DEBUG << "Registering " << System::typeinfoName(typeid(*hist))
123 << " title: \"" << hist->GetTitle()
124 << "\" id: \"" << uid << "\" dir: "
125 // << hist->GetDirectory()->GetPath() << " "
126 << changeDir(hid)->GetPath()
127 << " file: " << fname
128 << endmsg;
129
130 m_ids.emplace(rem, hid);
131 m_uids[uid] = hid;
132 m_tobjs[to] = hid;
133
134 return StatusCode::SUCCESS;
135
136 }
137
138 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
139
140 template <typename T>
141 StatusCode
142 THistSvc::getHist_i(const std::string& id, T*& hist, bool quiet) const {
143 // id starts with "/": unique
144
145 GlobalDirectoryRestore restore;
146
147 std::string idr(id);
148 removeDoubleSlash( idr );
149
150 if (idr.find("/") == 0) {
151 auto itr = m_uids.find(id);
152 if (itr == m_uids.end()) {
153 if (!quiet) {
154 m_log << MSG::ERROR << "Could not locate Hist with id \"" << idr << "\""
155 << endmsg;
156 }
157 hist = nullptr;
158 return StatusCode::FAILURE;
159 }
160
161 THistID hid = itr->second;
162 if (!quiet) {
163 hist = dynamic_cast<T*>(hid.obj);
164 if ( !hist ) {
165 m_log << MSG::ERROR << "dcast failed, Hist id: \"" << idr << "\""
166 << endmsg;
167 return StatusCode::FAILURE;
168 }
169 if (m_log.level() <= MSG::VERBOSE) {
170 m_log << MSG::VERBOSE << "found unique Hist title: \""
171 << hist->GetTitle()
172 << "\" id: \"" << idr << "\"" << endmsg;
173 }
174 } else {
175 if (m_log.level() <= MSG::VERBOSE) {
176 m_log << MSG::VERBOSE << "found unique Hist id: \"" << idr
177 << "\" type: \"" << hid.obj->IsA()->GetName() << "\""
178 << endmsg;
179 }
180 }
181
182 return StatusCode::SUCCESS;
183
184
185 // not necessarily unique
186 } else {
187
188 auto mitr = m_ids.equal_range(idr);
189
190
191 if (mitr.first == mitr.second) {
192 m_log << MSG::ERROR << "Could not locate Hist with id \"" << idr << "\""
193 << endmsg;
194 hist = nullptr;
195 return StatusCode::FAILURE;
196 } else {
197
198 if (distance(mitr.first,mitr.second) == 1) {
199 THistID hid = mitr.first->second;
200 if (!quiet) {
201 hist = dynamic_cast<T*>(hid.obj);
202 if (hist == 0) {
203 m_log << MSG::ERROR << "dcast failed" << endmsg;
204 return StatusCode::FAILURE;
205 }
206 if (m_log.level() <= MSG::VERBOSE) {
207 m_log << MSG::VERBOSE << "found Hist title: \"" << hist->GetTitle()
208 << "\" id: \"" << idr << "\"" << endmsg;
209 }
210 } else {
211 if (m_log.level() <= MSG::VERBOSE) {
212 m_log << MSG::VERBOSE << "found Hist id: \"" << idr << "\" type: \""
213 << hid.obj->IsA()->GetName() << "\""
214 << endmsg;
215 }
216 }
217 return StatusCode::SUCCESS;
218 } else {
219 if (!quiet) {
220 // return failure if trying to GET a single hist
221 m_log << MSG::ERROR << "Multiple matches with id \"" << idr << "\"."
222 << " Further specifications required."
223 << endmsg;
224 hist = nullptr;
225 return StatusCode::FAILURE;
226 } else {
227 // return a SUCCESS if just INQUIRING
228 m_log << MSG::INFO << "Found multiple matches with id \"" << idr
229 << "\"" << endmsg;
230 hist = nullptr;
231 return StatusCode::SUCCESS;
232 }
233 }
234 }
235 }
236 }
237
238 //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//
239
240 template <typename T>
241 StatusCode
242 THistSvc::readHist_i(const std::string& id, T*& hist) const {
243
244 GlobalDirectoryRestore restore;
245
246 std::string idr(id);
247 removeDoubleSlash( idr );
248
249 std::string stream, rem, dir, fdir, bdir, fdir2;
250 TFile *file;
251
252 if (!findStream(idr, stream, rem, file) ) {
253 return StatusCode::FAILURE;
254 }
255
256 if ( !file ) {
257 m_log << MSG::ERROR << "no associated file found" << endmsg;
258 return StatusCode::FAILURE;
259 }
260
261 file->cd("/");
262
263 fdir = idr;
264 bdir = dirname(fdir);
265 fdir2 = fdir;
266 while ( (dir=dirname(fdir)) != "" ) {
267 if (! gDirectory->GetKey(dir.c_str())) {
268 m_log << MSG::ERROR << "Directory \"" << fdir2 << "\" doesnt exist in "
269 << file->GetName() << endmsg;
270 return StatusCode::FAILURE;
271 }
272 gDirectory->cd(dir.c_str());
273 }
274
275 TObject *to=nullptr;
276 gDirectory->GetObject(fdir.c_str(), to);
277
278 if ( !to ) {
279 m_log << MSG::ERROR << "Could not get obj \"" << fdir << "\" in "
280 << gDirectory->GetPath() << endmsg;
281 return StatusCode::FAILURE;
282 }
283
284
285
286 hist = dynamic_cast<T*>(to);
287 if ( !hist ) {
288 m_log << MSG::ERROR << "Could not convert \"" << idr << "\" to a "
289 << System::typeinfoName(typeid(*hist)) << " as is a "
290 << to->IsA()->GetName()
291 << endmsg;
292 return StatusCode::FAILURE;
293 }
294
295
296 if (m_log.level() <= MSG::DEBUG) {
297 m_log << MSG::DEBUG << "Read in " << hist->IsA()->GetName() << " \""
298 << hist->GetName() << "\" from file "
299 << file->GetName() << endmsg;
300 hist->Print();
301 }
302
303 return StatusCode::SUCCESS;
304
305 }
306
307 #endif
System.h
THistSvc::THistID
Definition: THistSvc.h:116
MsgStream.h
MsgStream::level
MSG::Level level()
Retrieve output level.
Definition: MsgStream.h:112
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:297
THistSvc::findStream
bool findStream(const std::string &name, std::string &root, std::string &rem, TFile *&file) const
Definition: THistSvc.cpp:1161
MSG::DEBUG
Definition: IMessageSvc.h:18
std::map::end
T end(T...args)
THistSvc::changeDir
TDirectory * changeDir(const THistSvc::THistID &hid) const
Definition: THistSvc.cpp:1655
MSG::VERBOSE
Definition: IMessageSvc.h:17
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:86
THistSvc::m_tobjs
objMap m_tobjs
Definition: THistSvc.h:203
std::string
STL class.
StatusCode::FAILURE
Definition: StatusCode.h:29
THistSvc::dirname
std::string dirname(std::string &dir) const
Definition: THistSvc.cpp:1684
StatusCode::SUCCESS
Definition: StatusCode.h:30
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
THistSvc::m_files
std::map< std::string, std::pair< TFile *, Mode > > m_files
Definition: THistSvc.h:205
ana.file
list file
Definition: ana.py:160
THistSvc::getHist_i
StatusCode getHist_i(const std::string &name, T *&hist, bool quiet=false) const
Definition: THistSvc.icc:142
std::string::find
T find(T...args)
std::string::length
T length(T...args)
THistSvc::THistID::obj
TObject * obj
Definition: THistSvc.h:119
THistSvc::regHist_i
StatusCode regHist_i(T *hist, const std::string &name)
Definition: THistSvc.icc:17
THistSvc::m_uids
uidMap m_uids
Definition: THistSvc.h:201
THistSvc::readHist_i
StatusCode readHist_i(const std::string &name, T *&hist) const
Definition: THistSvc.icc:242
std::string::c_str
T c_str(T...args)
std::multimap::emplace
T emplace(T...args)
THistSvc::READ
Definition: THistSvc.h:108
THistSvc::m_ids
idMap m_ids
Definition: THistSvc.h:202
MSG::ERROR
Definition: IMessageSvc.h:21
MSG::WARNING
Definition: IMessageSvc.h:20
THistSvc::m_log
MsgStream m_log
Definition: THistSvc.h:180
MSG::INFO
Definition: IMessageSvc.h:19
std::multimap::equal_range
T equal_range(T...args)
THistSvc::GlobalDirectoryRestore
Definition: THistSvc.h:97
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
THistSvc::removeDoubleSlash
void removeDoubleSlash(std::string &) const
Definition: THistSvc.cpp:1720