ATLAS Offline Software
Loading...
Searching...
No Matches
AthHistogramming.h
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// AthHistogramming.h
8// Header file for class AthHistogramming
9// Author: Karsten Koeneke
11#ifndef ATHENABASECOMPS_ATHHISTOGRAMMING_H
12#define ATHENABASECOMPS_ATHHISTOGRAMMING_H 1
13
14
15
16// Framework includes
17#include "GaudiKernel/ServiceHandle.h"
18#include "GaudiKernel/ITHistSvc.h"
20#include "CxxUtils/crc64.h"
21#include "GaudiKernel/MsgStream.h"
22
23// ROOT includes
24#include "TH1.h"
25#include "TH2.h"
26#include "TH3.h"
27#include "TEfficiency.h"
28#include "TTree.h"
29#include "TGraph.h"
30
31// STL includes
32#include <string>
33#include <map>
34
35
36
37
39{
40
42 // Public methods:
44public:
45
47 AthHistogramming( const std::string& name );
48
49
51 virtual ~AthHistogramming();
52
54 // Const methods:
56
61 const ServiceHandle<ITHistSvc>& histSvc() const;
62
63
64
66 // Public methods:
68protected:
69
72 const std::string& prefix, const std::string& rootDir,
73 const std::string& histNamePrefix, const std::string& histNamePostfix,
74 const std::string& histTitlePrefix, const std::string& histTitlePostfix );
75
76 // -----------------------
77 // For histogramming
78 // -----------------------
79
81 inline TH1* bookGetPointer( const TH1& hist, const std::string& tDir="", const std::string& stream="" );
82
84 inline TH1* bookGetPointer( TH1* hist, const std::string& tDir="", const std::string& stream="" );
85
87 TH1* bookGetPointer( TH1& histRef, std::string tDir="", std::string stream="" );
88
89
91 inline StatusCode book( const TH1& hist, const std::string& tDir="", const std::string& stream="" );
92
94 inline StatusCode book( TH1* hist, const std::string& tDir="", const std::string& stream="" );
95
97 inline StatusCode book( TH1& histRef, const std::string& tDir="", const std::string& stream="" );
98
99
101 TH1* hist( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
102
104 inline TH2* hist2d( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
105
107 inline TH3* hist3d( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
108
109
110 // -----------------------
111 // For TTrees
112 // -----------------------
113
115 TTree* bookGetPointer( const TTree& treeRef, std::string tDir="", std::string stream="" );
116
118 inline StatusCode book( const TTree& treeRef, const std::string& tDir="", const std::string& stream="" );
119
121 TTree* tree( const std::string& treeName, const std::string& tDir="", const std::string& stream="" );
122
123
124 // -----------------------
125 // For TGraphs
126 // -----------------------
127
129 TGraph* bookGetPointer( const TGraph& graphRef, std::string tDir="", std::string stream="" );
130
132 inline StatusCode book( const TGraph& graphRef, const std::string& tDir="", const std::string& stream="" );
133
135 TGraph* graph( const std::string& graphName, const std::string& tDir="", const std::string& stream="" );
136
137
138 // -----------------------
139 // For TEfficiency
140 // -----------------------
141
143 inline TEfficiency* bookGetPointer( const TEfficiency& eff, const std::string& tDir="", const std::string& stream="" );
144
146 inline TEfficiency* bookGetPointer( TEfficiency* eff, const std::string& tDir="", const std::string& stream="" );
147
149 TEfficiency* bookGetPointer( TEfficiency& effRef, std::string tDir="", std::string stream="" );
150
151
153 inline StatusCode book( const TEfficiency& eff, const std::string& tDir="", const std::string& stream="" );
154
156 inline StatusCode book( TEfficiency* eff, const std::string& tDir="", const std::string& stream="" );
157
159 inline StatusCode book( TEfficiency& effRef, const std::string& tDir="", const std::string& stream="" );
160
161
163 TEfficiency* efficiency( const std::string& effName, const std::string& tDir="", const std::string& stream="" );
164
165
167 // Private methods:
169private:
171 typedef uint32_t hash_t;
172
174 void buildBookingString( std::string& bookingString,
175 std::string& histName,
176 std::string& tDir,
177 std::string& stream,
178 bool usePrefixPostfix = false);
179
181 void myReplace( std::string& str,
182 const std::string& oldStr,
183 const std::string& newStr);
184
186 hash_t hash( const std::string& histName ) const;
187
188
189
191 // Private data:
193private:
194
197
198
200 typedef std::map< const hash_t, TH1* > HistMap_t;
201
204
205
207 typedef std::map< const hash_t, TEfficiency* > EffMap_t;
208
211
212
214 typedef std::map< const hash_t, TTree* > TreeMap_t;
215
218
219
221 typedef std::map< const hash_t, TGraph* > GraphMap_t;
222
225
226
228 std::string m_streamName;
229
231 std::string m_rootDir;
232
234 std::string m_histNamePrefix;
235
237 std::string m_histNamePostfix;
238
240 std::string m_histTitlePrefix;
241
244
245
247 std::string m_name;
248
250 MsgStream m_msg;
251
252};
253
254
255
256
257
259// Inline methods:
261
262inline TH1* AthHistogramming::bookGetPointer( const TH1& hist, const std::string& tDir, const std::string& stream )
263{
264 // We need to create a non-const clone
265 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
266 if ( !histClone ) {
267 m_msg << MSG::ERROR << "Couldn't create a TH1 clone in bookGetPointer" << endmsg;
268 return 0;
269 }
270 return this->bookGetPointer( *histClone, tDir, stream );
271
272}
273
274inline TH1* AthHistogramming::bookGetPointer( TH1* hist, const std::string& tDir, const std::string& stream )
275{
276 if ( !hist ) {
277 m_msg << MSG::ERROR << "Got a zero pointer to a TH1 in bookGetPointer" << endmsg;
278 return 0;
279 }
280 return this->bookGetPointer( *hist, tDir, stream );
281}
282
283inline TEfficiency* AthHistogramming::bookGetPointer( const TEfficiency& hist, const std::string& tDir, const std::string& stream )
284{
285 // We need to create a non-const clone
286 TEfficiency* histClone = dynamic_cast< TEfficiency* >( hist.Clone() );
287 if ( !histClone ) {
288 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone in bookGetPointer" << endmsg;
289 return 0;
290 }
291 return this->bookGetPointer( *histClone, tDir, stream );
292
293}
294
295inline TEfficiency* AthHistogramming::bookGetPointer( TEfficiency* hist, const std::string& tDir, const std::string& stream )
296{
297 if ( !hist ) {
298 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency in bookGetPointer" << endmsg;
299 return 0;
300 }
301 return this->bookGetPointer( *hist, tDir, stream );
302}
303
304
305inline StatusCode AthHistogramming::book( const TH1& hist, const std::string& tDir, const std::string& stream )
306{
307 // We need to create a non-const clone
308 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
309 if ( !histClone ) {
310 m_msg << MSG::ERROR << "Couldn't create a TH1 clone" << endmsg;
311 return StatusCode::FAILURE;
312 }
313 return this->book( *histClone, tDir, stream );
314}
315
316inline StatusCode AthHistogramming::book( TH1* hist, const std::string& tDir, const std::string& stream )
317{
318 if ( !hist ) {
319 m_msg << MSG::ERROR << "Got a zero pointer to a TH1" << endmsg;
320 return StatusCode::FAILURE;
321 }
322 return this->book( *hist, tDir, stream );
323}
324
325// Simplify the booking and registering (into THistSvc) of histograms
326inline StatusCode AthHistogramming::book( TH1& histRef, const std::string& tDir, const std::string& stream )
327{
328 // Call the other Book method and see if it returns a valid pointer
329 TH1* histPointer = this->bookGetPointer( histRef, tDir, stream );
330 if ( !histPointer ) {
331 m_msg << MSG::ERROR << "Couldn't book a TH1" << endmsg;
332 return StatusCode::FAILURE;
333 }
334 return StatusCode::SUCCESS;
335}
336
337inline StatusCode AthHistogramming::book( const TEfficiency& eff, const std::string& tDir, const std::string& stream )
338{
339 // We need to create a non-const clone
340 TEfficiency* effClone = dynamic_cast< TEfficiency* >( eff.Clone() );
341 if ( !effClone ) {
342 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone" << endmsg;
343 return StatusCode::FAILURE;
344 }
345 return this->book( *effClone, tDir, stream );
346}
347
348inline StatusCode AthHistogramming::book( TEfficiency* eff, const std::string& tDir, const std::string& stream )
349{
350 if ( !eff ) {
351 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency" << endmsg;
352 return StatusCode::FAILURE;
353 }
354 return this->book( *eff, tDir, stream );
355}
356
357// Simplify the booking and registering (into THistSvc) of TEfficiency
358inline StatusCode AthHistogramming::book( TEfficiency& effRef, const std::string& tDir, const std::string& stream )
359{
360 // Call the other Book method and see if it returns a valid pointer
361 TEfficiency* effPointer = this->bookGetPointer( effRef, tDir, stream );
362 if ( !effPointer ) {
363 m_msg << MSG::ERROR << "Couldn't book a TEfficiency" << endmsg;
364 return StatusCode::FAILURE;
365 }
366 return StatusCode::SUCCESS;
367}
368
369
370// Simplify the retrieval of registered 2-d histograms
371inline TH2* AthHistogramming::hist2d( const std::string& histName, const std::string& tDir, const std::string& stream )
372{
373 // Get the TH1 pointer
374 TH1* th1Pointer = this->hist(histName, tDir, stream);
375 if ( !th1Pointer )
376 {
377 m_msg << MSG::ERROR
378 << "Cannot get a 2-d histogram with name " << histName
379 << "... will probably seg-fault!" << endmsg;
380 return NULL;
381 }
382 // If the TH1 pointer is valid, simply return the dynamic_cast
383 return dynamic_cast<TH2*>( th1Pointer );
384}
385
386
387// Simplify the retrieval of registered 3-d histograms
388inline TH3* AthHistogramming::hist3d( const std::string& histName, const std::string& tDir, const std::string& stream )
389{
390 // Get the TH1 pointer
391 TH1* th1Pointer = this->hist(histName, tDir, stream);
392 if ( !th1Pointer )
393 {
394 m_msg << MSG::ERROR
395 << "Cannot get a 3-d histogram with name " << histName
396 << "... will probably seg-fault!" << endmsg;
397 return NULL;
398 }
399 // If the TH1 pointer is valid, simply return the dynamic_cast
400 return dynamic_cast<TH3*>( th1Pointer );
401}
402
403
404// Simplify the booking and registering (into THistSvc) of TTrees
405inline StatusCode AthHistogramming::book( const TTree& treeRef, const std::string& tDir, const std::string& stream )
406{
407 // Call the other Book method and see if it returns a valid pointer
408 TTree* treePointer = this->bookGetPointer( treeRef, tDir, stream );
409 if ( treePointer )
410 {
411 return StatusCode::SUCCESS;
412 }
413 else
414 {
415 return StatusCode::FAILURE;
416 }
417}
418
419
420
421
422// For the THistSvc
424{
425 return m_histSvc;
426}
427
428
429// Create a 32-bit hash out of the histogram name
430inline AthHistogramming::hash_t AthHistogramming::hash( const std::string& histName ) const
431{
432 const uint64_t hash64 = CxxUtils::crc64( histName );
433 return (hash_t)(hash64 & 0xFFFFFFFF);
434}
435
436
437#endif //> !ATHENABASECOMPS_ATHHISTOGRAMMINGTOOL_H
#define endmsg
const ServiceHandle< ITHistSvc > & histSvc() const
The standard THistSvc (for writing histograms and TTrees and more to a root file) Returns (kind of) a...
HistMap_t m_histMap
The map of histogram names to their pointers.
ServiceHandle< ITHistSvc > m_histSvc
Pointer to the THistSvc (event store by default).
std::string m_histNamePostfix
The postfix for the histogram THx name.
std::map< const hash_t, TH1 * > HistMap_t
Typedef for convenience.
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
hash_t hash(const std::string &histName) const
Method to calculate a 32-bit hash from a string.
std::string m_rootDir
Name of the ROOT directory.
StatusCode book(const TGraph &graphRef, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of TGraphs.
uint32_t hash_t
typedef for the internal hash
TH1 * bookGetPointer(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
std::string m_histTitlePostfix
The postfix for the histogram THx title.
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
std::map< const hash_t, TEfficiency * > EffMap_t
Typedef for convenience.
virtual ~AthHistogramming()
Destructor:
AthHistogramming(const std::string &name)
Constructor with parameters:
TreeMap_t m_treeMap
The map of TTree names to their pointers.
std::string m_histTitlePrefix
The prefix for the histogram THx title.
TH3 * hist3d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 3-d histograms.
std::string m_streamName
Name of the ROOT output stream (file).
StatusCode configAthHistogramming(const ServiceHandle< ITHistSvc > &histSvc, const std::string &prefix, const std::string &rootDir, const std::string &histNamePrefix, const std::string &histNamePostfix, const std::string &histTitlePrefix, const std::string &histTitlePostfix)
To be called by the derived classes to fill the internal configuration.
std::string m_name
Instance name.
std::map< const hash_t, TTree * > TreeMap_t
Typedef for convenience.
std::map< const hash_t, TGraph * > GraphMap_t
Typedef for convenience.
MsgStream m_msg
Cached Message Stream.
void buildBookingString(std::string &bookingString, std::string &histName, std::string &tDir, std::string &stream, bool usePrefixPostfix=false)
Method to build individual booking string.
EffMap_t m_effMap
The map of histogram names to their pointers.
TEfficiency * efficiency(const std::string &effName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TEfficiency.
TH2 * hist2d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 2-d histograms.
void myReplace(std::string &str, const std::string &oldStr, const std::string &newStr)
Helper method to replace sub-string.
TGraph * graph(const std::string &graphName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TGraphs.
std::string m_histNamePrefix
The prefix for the histogram THx name.
GraphMap_t m_graphMap
The map of TGraph names to their pointers.
A crc-64 implementation, using pclmul where possible.
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition crc64.cxx:696
TChain * tree