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, std::string_view tDir="", std::string_view stream="" );
82
84 inline TH1* bookGetPointer( TH1* hist, std::string_view tDir="", std::string_view stream="" );
85
87 TH1* bookGetPointer( TH1& histRef, std::string_view tDir="", std::string_view stream="" );
88
89
91 inline StatusCode book( const TH1& hist, std::string_view tDir="", std::string_view stream="" );
92
94 inline StatusCode book( TH1* hist, std::string_view tDir="", std::string_view stream="" );
95
97 inline StatusCode book( TH1& histRef, std::string_view tDir="", std::string_view stream="" );
98
99
101 TH1* hist( std::string_view histName, std::string_view tDir="", std::string_view stream="" );
102
104 inline TH2* hist2d( std::string_view histName, std::string_view tDir="", std::string_view stream="" );
105
107 inline TH3* hist3d( std::string_view histName, std::string_view tDir="", std::string_view stream="" );
108
109
110 // -----------------------
111 // For TTrees
112 // -----------------------
113
115 TTree* bookGetPointer( const TTree& treeRef, std::string_view tDir="", std::string_view stream="" );
116
118 inline StatusCode book( const TTree& treeRef, std::string_view tDir="", std::string_view stream="" );
119
121 TTree* tree( std::string_view treeName, std::string_view tDir="", std::string_view stream="" );
122
123
124 // -----------------------
125 // For TGraphs
126 // -----------------------
127
129 TGraph* bookGetPointer( const TGraph& graphRef, std::string_view tDir="", std::string_view stream="" );
130
132 inline StatusCode book( const TGraph& graphRef, std::string_view tDir="", std::string_view stream="" );
133
135 TGraph* graph( std::string_view graphName, std::string_view tDir="", std::string_view stream="" );
136
137
138 // -----------------------
139 // For TEfficiency
140 // -----------------------
141
143 inline TEfficiency* bookGetPointer( const TEfficiency& eff, std::string_view tDir="", std::string_view stream="" );
144
146 inline TEfficiency* bookGetPointer( TEfficiency* eff, std::string_view tDir="", std::string_view stream="" );
147
149 TEfficiency* bookGetPointer( TEfficiency& effRef, std::string_view tDir="", std::string_view stream="" );
150
151
153 inline StatusCode book( const TEfficiency& eff, std::string_view tDir="", std::string_view stream="" );
154
156 inline StatusCode book( TEfficiency* eff, std::string_view tDir="", std::string_view stream="" );
157
159 inline StatusCode book( TEfficiency& effRef, std::string_view tDir="", std::string_view stream="" );
160
161
163 TEfficiency* efficiency( std::string_view effName, std::string_view tDir="", std::string_view stream="" );
164
165
167 // Private methods:
169private:
171 typedef uint32_t hash_t;
172
174 std::string buildBookingString( std::string_view histName, std::string_view tDir,
175 std::string_view stream, bool usePrefixPostfix = false) const;
176
178 hash_t hash( std::string_view histName ) const;
179
180
181
183 // Private data:
185private:
186
189
190
192 typedef std::map< const hash_t, TH1* > HistMap_t;
193
196
197
199 typedef std::map< const hash_t, TEfficiency* > EffMap_t;
200
203
204
206 typedef std::map< const hash_t, TTree* > TreeMap_t;
207
210
211
213 typedef std::map< const hash_t, TGraph* > GraphMap_t;
214
217
218
220 std::string m_streamName;
221
223 std::string m_rootDir;
224
226 std::string m_histNamePrefix;
227
229 std::string m_histNamePostfix;
230
232 std::string m_histTitlePrefix;
233
236
237
239 std::string m_name;
240
242 MsgStream m_msg;
243
244};
245
246
247
248
249
251// Inline methods:
253
254inline TH1* AthHistogramming::bookGetPointer( const TH1& hist, std::string_view tDir, std::string_view stream )
255{
256 // We need to create a non-const clone
257 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
258 if ( !histClone ) {
259 m_msg << MSG::ERROR << "Couldn't create a TH1 clone in bookGetPointer" << endmsg;
260 return 0;
261 }
262 return this->bookGetPointer( *histClone, tDir, stream );
263
264}
265
266inline TH1* AthHistogramming::bookGetPointer( TH1* hist, std::string_view tDir, std::string_view stream )
267{
268 if ( !hist ) {
269 m_msg << MSG::ERROR << "Got a zero pointer to a TH1 in bookGetPointer" << endmsg;
270 return 0;
271 }
272 return this->bookGetPointer( *hist, tDir, stream );
273}
274
275inline TEfficiency* AthHistogramming::bookGetPointer( const TEfficiency& hist, std::string_view tDir, std::string_view stream )
276{
277 // We need to create a non-const clone
278 TEfficiency* histClone = dynamic_cast< TEfficiency* >( hist.Clone() );
279 if ( !histClone ) {
280 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone in bookGetPointer" << endmsg;
281 return 0;
282 }
283 return this->bookGetPointer( *histClone, tDir, stream );
284
285}
286
287inline TEfficiency* AthHistogramming::bookGetPointer( TEfficiency* hist, std::string_view tDir, std::string_view stream )
288{
289 if ( !hist ) {
290 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency in bookGetPointer" << endmsg;
291 return 0;
292 }
293 return this->bookGetPointer( *hist, tDir, stream );
294}
295
296
297inline StatusCode AthHistogramming::book( const TH1& hist, std::string_view tDir, std::string_view stream )
298{
299 // We need to create a non-const clone
300 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
301 if ( !histClone ) {
302 m_msg << MSG::ERROR << "Couldn't create a TH1 clone" << endmsg;
303 return StatusCode::FAILURE;
304 }
305 return this->book( *histClone, tDir, stream );
306}
307
308inline StatusCode AthHistogramming::book( TH1* hist, std::string_view tDir, std::string_view stream )
309{
310 if ( !hist ) {
311 m_msg << MSG::ERROR << "Got a zero pointer to a TH1" << endmsg;
312 return StatusCode::FAILURE;
313 }
314 return this->book( *hist, tDir, stream );
315}
316
317// Simplify the booking and registering (into THistSvc) of histograms
318inline StatusCode AthHistogramming::book( TH1& histRef, std::string_view tDir, std::string_view stream )
319{
320 // Call the other Book method and see if it returns a valid pointer
321 TH1* histPointer = this->bookGetPointer( histRef, tDir, stream );
322 if ( !histPointer ) {
323 m_msg << MSG::ERROR << "Couldn't book a TH1" << endmsg;
324 return StatusCode::FAILURE;
325 }
326 return StatusCode::SUCCESS;
327}
328
329inline StatusCode AthHistogramming::book( const TEfficiency& eff, std::string_view tDir, std::string_view stream )
330{
331 // We need to create a non-const clone
332 TEfficiency* effClone = dynamic_cast< TEfficiency* >( eff.Clone() );
333 if ( !effClone ) {
334 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone" << endmsg;
335 return StatusCode::FAILURE;
336 }
337 return this->book( *effClone, tDir, stream );
338}
339
340inline StatusCode AthHistogramming::book( TEfficiency* eff, std::string_view tDir, std::string_view stream )
341{
342 if ( !eff ) {
343 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency" << endmsg;
344 return StatusCode::FAILURE;
345 }
346 return this->book( *eff, tDir, stream );
347}
348
349// Simplify the booking and registering (into THistSvc) of TEfficiency
350inline StatusCode AthHistogramming::book( TEfficiency& effRef, std::string_view tDir, std::string_view stream )
351{
352 // Call the other Book method and see if it returns a valid pointer
353 TEfficiency* effPointer = this->bookGetPointer( effRef, tDir, stream );
354 if ( !effPointer ) {
355 m_msg << MSG::ERROR << "Couldn't book a TEfficiency" << endmsg;
356 return StatusCode::FAILURE;
357 }
358 return StatusCode::SUCCESS;
359}
360
361
362// Simplify the retrieval of registered 2-d histograms
363inline TH2* AthHistogramming::hist2d( std::string_view histName, std::string_view tDir, std::string_view stream )
364{
365 // Get the TH1 pointer
366 TH1* th1Pointer = this->hist(histName, tDir, stream);
367 if ( !th1Pointer )
368 {
369 m_msg << MSG::ERROR
370 << "Cannot get a 2-d histogram with name " << histName
371 << "... will probably seg-fault!" << endmsg;
372 return NULL;
373 }
374 // If the TH1 pointer is valid, simply return the dynamic_cast
375 return dynamic_cast<TH2*>( th1Pointer );
376}
377
378
379// Simplify the retrieval of registered 3-d histograms
380inline TH3* AthHistogramming::hist3d( std::string_view histName, std::string_view tDir, std::string_view stream )
381{
382 // Get the TH1 pointer
383 TH1* th1Pointer = this->hist(histName, tDir, stream);
384 if ( !th1Pointer )
385 {
386 m_msg << MSG::ERROR
387 << "Cannot get a 3-d histogram with name " << histName
388 << "... will probably seg-fault!" << endmsg;
389 return NULL;
390 }
391 // If the TH1 pointer is valid, simply return the dynamic_cast
392 return dynamic_cast<TH3*>( th1Pointer );
393}
394
395
396// Simplify the booking and registering (into THistSvc) of TTrees
397inline StatusCode AthHistogramming::book( const TTree& treeRef, std::string_view tDir, std::string_view stream )
398{
399 // Call the other Book method and see if it returns a valid pointer
400 TTree* treePointer = this->bookGetPointer( treeRef, tDir, stream );
401 if ( treePointer )
402 {
403 return StatusCode::SUCCESS;
404 }
405 else
406 {
407 return StatusCode::FAILURE;
408 }
409}
410
411
412
413
414// For the THistSvc
416{
417 return m_histSvc;
418}
419
420
421// Create a 32-bit hash out of the histogram name
422inline AthHistogramming::hash_t AthHistogramming::hash( std::string_view histName ) const
423{
424 const uint64_t hash64 = CxxUtils::crc64( std::string{histName} );
425 return (hash_t)(hash64 & 0xFFFFFFFF);
426}
427
428
429#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.
hash_t hash(std::string_view histName) const
Method to calculate a 32-bit hash from a string.
ServiceHandle< ITHistSvc > m_histSvc
Pointer to the THistSvc (event store by default).
std::string buildBookingString(std::string_view histName, std::string_view tDir, std::string_view stream, bool usePrefixPostfix=false) const
Method to build individual booking string.
std::string m_histNamePostfix
The postfix for the histogram THx name.
std::map< const hash_t, TH1 * > HistMap_t
Typedef for convenience.
TH2 * hist2d(std::string_view histName, std::string_view tDir="", std::string_view stream="")
Simplify the retrieval of registered 2-d histograms.
std::string m_rootDir
Name of the ROOT directory.
uint32_t hash_t
typedef for the internal hash
std::string m_histTitlePostfix
The postfix for the histogram THx title.
TH1 * hist(std::string_view histName, std::string_view tDir="", std::string_view stream="")
Simplify the retrieval of registered histograms of any type.
std::map< const hash_t, TEfficiency * > EffMap_t
Typedef for convenience.
TH3 * hist3d(std::string_view histName, std::string_view tDir="", std::string_view stream="")
Simplify the retrieval of registered 3-d histograms.
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.
std::string m_streamName
Name of the ROOT output stream (file).
TH1 * bookGetPointer(const TH1 &hist, std::string_view tDir="", std::string_view stream="")
Simplify the booking and registering (into THistSvc) of histograms.
StatusCode book(const TGraph &graphRef, std::string_view tDir="", std::string_view stream="")
Simplify the booking and registering (into THistSvc) of TGraphs.
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 to fill the internal configuration.
std::string m_name
Instance name.
std::map< const hash_t, TTree * > TreeMap_t
Typedef for convenience.
TGraph * graph(std::string_view graphName, std::string_view tDir="", std::string_view stream="")
Simplify the retrieval of registered TGraphs.
StatusCode book(const TH1 &hist, std::string_view tDir="", std::string_view stream="")
Simplify the booking and registering (into THistSvc) of histograms.
std::map< const hash_t, TGraph * > GraphMap_t
Typedef for convenience.
MsgStream m_msg
Cached Message Stream.
TEfficiency * efficiency(std::string_view effName, std::string_view tDir="", std::string_view stream="")
Simplify the retrieval of registered TEfficiency.
EffMap_t m_effMap
The map of histogram names to their pointers.
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:695
TChain * tree