ATLAS Offline Software
Loading...
Searching...
No Matches
AthHistogramming.h
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2019 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// STL includes
15#include <string>
16#include <map>
17
18// Framework includes
19#include "GaudiKernel/ServiceHandle.h"
20#include "GaudiKernel/ITHistSvc.h"
22#include "CxxUtils/crc64.h"
23#include "GaudiKernel/MsgStream.h"
24
25// ROOT includes
26#include "TH1.h"
27#include "TH2.h"
28#include "TH3.h"
29#include "TEfficiency.h"
30#include "TTree.h"
31#include "TGraph.h"
32
33
34
35
37{
38
40 // Public methods:
42public:
43
45 AthHistogramming( const std::string& name );
46
47
49 virtual ~AthHistogramming();
50
52 // Const methods:
54
59 const ServiceHandle<ITHistSvc>& histSvc() const;
60
61
62
64 // Public methods:
66protected:
67
70 const std::string& prefix, const std::string& rootDir,
71 const std::string& histNamePrefix, const std::string& histNamePostfix,
72 const std::string& histTitlePrefix, const std::string& histTitlePostfix );
73
74 // -----------------------
75 // For histogramming
76 // -----------------------
77
79 inline TH1* bookGetPointer( const TH1& hist, const std::string& tDir="", const std::string& stream="" );
80
82 inline TH1* bookGetPointer( TH1* hist, const std::string& tDir="", const std::string& stream="" );
83
85 TH1* bookGetPointer( TH1& histRef, std::string tDir="", std::string stream="" );
86
87
89 inline StatusCode book( const TH1& hist, const std::string& tDir="", const std::string& stream="" );
90
92 inline StatusCode book( TH1* hist, const std::string& tDir="", const std::string& stream="" );
93
95 inline StatusCode book( TH1& histRef, const std::string& tDir="", const std::string& stream="" );
96
97
99 TH1* hist( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
100
102 inline TH2* hist2d( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
103
105 inline TH3* hist3d( const std::string& histName, const std::string& tDir="", const std::string& stream="" );
106
107
108 // -----------------------
109 // For TTrees
110 // -----------------------
111
113 TTree* bookGetPointer( const TTree& treeRef, std::string tDir="", std::string stream="" );
114
116 inline StatusCode book( const TTree& treeRef, const std::string& tDir="", const std::string& stream="" );
117
119 TTree* tree( const std::string& treeName, const std::string& tDir="", const std::string& stream="" );
120
121
122 // -----------------------
123 // For TGraphs
124 // -----------------------
125
127 TGraph* bookGetPointer( const TGraph& graphRef, std::string tDir="", std::string stream="" );
128
130 inline StatusCode book( const TGraph& graphRef, const std::string& tDir="", const std::string& stream="" );
131
133 TGraph* graph( const std::string& graphName, const std::string& tDir="", const std::string& stream="" );
134
135
136 // -----------------------
137 // For TEfficiency
138 // -----------------------
139
141 inline TEfficiency* bookGetPointer( const TEfficiency& eff, const std::string& tDir="", const std::string& stream="" );
142
144 inline TEfficiency* bookGetPointer( TEfficiency* eff, const std::string& tDir="", const std::string& stream="" );
145
147 TEfficiency* bookGetPointer( TEfficiency& effRef, std::string tDir="", std::string stream="" );
148
149
151 inline StatusCode book( const TEfficiency& eff, const std::string& tDir="", const std::string& stream="" );
152
154 inline StatusCode book( TEfficiency* eff, const std::string& tDir="", const std::string& stream="" );
155
157 inline StatusCode book( TEfficiency& effRef, const std::string& tDir="", const std::string& stream="" );
158
159
161 TEfficiency* efficiency( const std::string& effName, const std::string& tDir="", const std::string& stream="" );
162
163
165 // Private methods:
167private:
169 typedef uint32_t hash_t;
170
172 void buildBookingString( std::string& bookingString,
173 std::string& histName,
174 std::string& tDir,
175 std::string& stream,
176 bool usePrefixPostfix = false);
177
179 void myReplace( std::string& str,
180 const std::string& oldStr,
181 const std::string& newStr);
182
184 hash_t hash( const std::string& histName ) const;
185
186
187
189 // Private data:
191private:
192
195
196
198 typedef std::map< const hash_t, TH1* > HistMap_t;
199
202
203
205 typedef std::map< const hash_t, TEfficiency* > EffMap_t;
206
209
210
212 typedef std::map< const hash_t, TTree* > TreeMap_t;
213
216
217
219 typedef std::map< const hash_t, TGraph* > GraphMap_t;
220
223
224
226 std::string m_streamName;
227
229 std::string m_rootDir;
230
232 std::string m_histNamePrefix;
233
235 std::string m_histNamePostfix;
236
238 std::string m_histTitlePrefix;
239
242
243
245 std::string m_name;
246
248 MsgStream m_msg;
249
250};
251
252
253
254
255
257// Inline methods:
259
260inline TH1* AthHistogramming::bookGetPointer( const TH1& hist, const std::string& tDir, const std::string& stream )
261{
262 // We need to create a non-const clone
263 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
264 if ( !histClone ) {
265 m_msg << MSG::ERROR << "Couldn't create a TH1 clone in bookGetPointer" << endmsg;
266 return 0;
267 }
268 return this->bookGetPointer( *histClone, tDir, stream );
269
270}
271
272inline TH1* AthHistogramming::bookGetPointer( TH1* hist, const std::string& tDir, const std::string& stream )
273{
274 if ( !hist ) {
275 m_msg << MSG::ERROR << "Got a zero pointer to a TH1 in bookGetPointer" << endmsg;
276 return 0;
277 }
278 return this->bookGetPointer( *hist, tDir, stream );
279}
280
281inline TEfficiency* AthHistogramming::bookGetPointer( const TEfficiency& hist, const std::string& tDir, const std::string& stream )
282{
283 // We need to create a non-const clone
284 TEfficiency* histClone = dynamic_cast< TEfficiency* >( hist.Clone() );
285 if ( !histClone ) {
286 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone in bookGetPointer" << endmsg;
287 return 0;
288 }
289 return this->bookGetPointer( *histClone, tDir, stream );
290
291}
292
293inline TEfficiency* AthHistogramming::bookGetPointer( TEfficiency* hist, const std::string& tDir, const std::string& stream )
294{
295 if ( !hist ) {
296 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency in bookGetPointer" << endmsg;
297 return 0;
298 }
299 return this->bookGetPointer( *hist, tDir, stream );
300}
301
302
303inline StatusCode AthHistogramming::book( const TH1& hist, const std::string& tDir, const std::string& stream )
304{
305 // We need to create a non-const clone
306 TH1* histClone = dynamic_cast< TH1* >( hist.Clone() );
307 if ( !histClone ) {
308 m_msg << MSG::ERROR << "Couldn't create a TH1 clone" << endmsg;
309 return StatusCode::FAILURE;
310 }
311 return this->book( *histClone, tDir, stream );
312}
313
314inline StatusCode AthHistogramming::book( TH1* hist, const std::string& tDir, const std::string& stream )
315{
316 if ( !hist ) {
317 m_msg << MSG::ERROR << "Got a zero pointer to a TH1" << endmsg;
318 return StatusCode::FAILURE;
319 }
320 return this->book( *hist, tDir, stream );
321}
322
323// Simplify the booking and registering (into THistSvc) of histograms
324inline StatusCode AthHistogramming::book( TH1& histRef, const std::string& tDir, const std::string& stream )
325{
326 // Call the other Book method and see if it returns a valid pointer
327 TH1* histPointer = this->bookGetPointer( histRef, tDir, stream );
328 if ( !histPointer ) {
329 m_msg << MSG::ERROR << "Couldn't book a TH1" << endmsg;
330 return StatusCode::FAILURE;
331 }
332 return StatusCode::SUCCESS;
333}
334
335inline StatusCode AthHistogramming::book( const TEfficiency& eff, const std::string& tDir, const std::string& stream )
336{
337 // We need to create a non-const clone
338 TEfficiency* effClone = dynamic_cast< TEfficiency* >( eff.Clone() );
339 if ( !effClone ) {
340 m_msg << MSG::ERROR << "Couldn't create a TEfficiency clone" << endmsg;
341 return StatusCode::FAILURE;
342 }
343 return this->book( *effClone, tDir, stream );
344}
345
346inline StatusCode AthHistogramming::book( TEfficiency* eff, const std::string& tDir, const std::string& stream )
347{
348 if ( !eff ) {
349 m_msg << MSG::ERROR << "Got a zero pointer to a TEfficiency" << endmsg;
350 return StatusCode::FAILURE;
351 }
352 return this->book( *eff, tDir, stream );
353}
354
355// Simplify the booking and registering (into THistSvc) of TEfficiency
356inline StatusCode AthHistogramming::book( TEfficiency& effRef, const std::string& tDir, const std::string& stream )
357{
358 // Call the other Book method and see if it returns a valid pointer
359 TEfficiency* effPointer = this->bookGetPointer( effRef, tDir, stream );
360 if ( !effPointer ) {
361 m_msg << MSG::ERROR << "Couldn't book a TEfficiency" << endmsg;
362 return StatusCode::FAILURE;
363 }
364 return StatusCode::SUCCESS;
365}
366
367
368// Simplify the retrieval of registered 2-d histograms
369inline TH2* AthHistogramming::hist2d( const std::string& histName, const std::string& tDir, const std::string& stream )
370{
371 // Get the TH1 pointer
372 TH1* th1Pointer = this->hist(histName, tDir, stream);
373 if ( !th1Pointer )
374 {
375 m_msg << MSG::ERROR
376 << "Cannot get a 2-d histogram with name " << histName
377 << "... will probably seg-fault!" << endmsg;
378 return NULL;
379 }
380 // If the TH1 pointer is valid, simply return the dynamic_cast
381 return dynamic_cast<TH2*>( th1Pointer );
382}
383
384
385// Simplify the retrieval of registered 3-d histograms
386inline TH3* AthHistogramming::hist3d( const std::string& histName, const std::string& tDir, const std::string& stream )
387{
388 // Get the TH1 pointer
389 TH1* th1Pointer = this->hist(histName, tDir, stream);
390 if ( !th1Pointer )
391 {
392 m_msg << MSG::ERROR
393 << "Cannot get a 3-d histogram with name " << histName
394 << "... will probably seg-fault!" << endmsg;
395 return NULL;
396 }
397 // If the TH1 pointer is valid, simply return the dynamic_cast
398 return dynamic_cast<TH3*>( th1Pointer );
399}
400
401
402// Simplify the booking and registering (into THistSvc) of TTrees
403inline StatusCode AthHistogramming::book( const TTree& treeRef, const std::string& tDir, const std::string& stream )
404{
405 // Call the other Book method and see if it returns a valid pointer
406 TTree* treePointer = this->bookGetPointer( treeRef, tDir, stream );
407 if ( treePointer )
408 {
409 return StatusCode::SUCCESS;
410 }
411 else
412 {
413 return StatusCode::FAILURE;
414 }
415}
416
417
418
419
420// For the THistSvc
422{
423 return m_histSvc;
424}
425
426
427// Create a 32-bit hash out of the histogram name
428inline AthHistogramming::hash_t AthHistogramming::hash( const std::string& histName ) const
429{
430 const uint64_t hash64 = CxxUtils::crc64( histName );
431 return (hash_t)(hash64 & 0xFFFFFFFF);
432}
433
434
435#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