ATLAS Offline Software
AthHistogramming.h
Go to the documentation of this file.
1 
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:
42 public:
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:
66 protected:
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:
167 private:
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:
191 private:
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 
241  std::string m_histTitlePostfix;
242 
243 
245  std::string m_name;
246 
248  MsgStream m_msg;
249 
250 };
251 
252 
253 
254 
255 
257 // Inline methods:
259 
260 inline 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 
272 inline 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 
281 inline 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 
293 inline 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 
303 inline 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 
314 inline 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
324 inline 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 
335 inline 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 
346 inline 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
356 inline 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
369 inline 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
386 inline 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
403 inline 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
428 inline AthHistogramming::hash_t AthHistogramming::hash( const std::string& histName ) const
429 {
431  return (hash_t)(hash64 & 0xFFFFFFFF);
432 }
433 
434 
435 #endif //> !ATHENABASECOMPS_ATHHISTOGRAMMINGTOOL_H
AthHistogramming::m_rootDir
std::string m_rootDir
Name of the ROOT directory.
Definition: AthHistogramming.h:229
AthHistogramming::histSvc
const ServiceHandle< ITHistSvc > & histSvc() const
The standard THistSvc (for writing histograms and TTrees and more to a root file) Returns (kind of) a...
Definition: AthHistogramming.h:421
AthHistogramming::hist2d
TH2 * hist2d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 2-d histograms.
Definition: AthHistogramming.h:369
AthHistogramming::myReplace
void myReplace(std::string &str, const std::string &oldStr, const std::string &newStr)
Helper method to replace sub-string.
Definition: AthHistogramming.cxx:590
AthHistogramming::m_msg
MsgStream m_msg
Cached Message Stream.
Definition: AthHistogramming.h:248
AthHistogramming::m_histMap
HistMap_t m_histMap
The map of histogram names to their pointers.
Definition: AthHistogramming.h:201
AddEmptyComponent.histName
string histName
Definition: AddEmptyComponent.py:64
AthCheckMacros.h
AthHistogramming::TreeMap_t
std::map< const hash_t, TTree * > TreeMap_t
Typedef for convenience.
Definition: AthHistogramming.h:212
AthHistogramming::book
StatusCode book(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
Definition: AthHistogramming.h:303
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
AthHistogramming::GraphMap_t
std::map< const hash_t, TGraph * > GraphMap_t
Typedef for convenience.
Definition: AthHistogramming.h:219
AthHistogramming::efficiency
TEfficiency * efficiency(const std::string &effName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TEfficiency.
Definition: AthHistogramming.cxx:250
plotmaker.hist
hist
Definition: plotmaker.py:148
AthHistogramming
Definition: AthHistogramming.h:37
AthHistogramming::~AthHistogramming
virtual ~AthHistogramming()
Destructor:
Definition: AthHistogramming.cxx:54
AthHistogramming::graph
TGraph * graph(const std::string &graphName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TGraphs.
Definition: AthHistogramming.cxx:492
AthHistogramming::m_histNamePostfix
std::string m_histNamePostfix
The postfix for the histogram THx name.
Definition: AthHistogramming.h:235
AthenaPoolTestWrite.stream
string stream
Definition: AthenaPoolTestWrite.py:12
AthHistogramming::hist3d
TH3 * hist3d(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered 3-d histograms.
Definition: AthHistogramming.h:386
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
dumpFileToPlots.treeName
string treeName
Definition: dumpFileToPlots.py:20
xAOD::uint64_t
uint64_t
Definition: EventInfo_v1.cxx:123
AthHistogramming::m_graphMap
GraphMap_t m_graphMap
The map of TGraph names to their pointers.
Definition: AthHistogramming.h:222
AthHistogramming::m_streamName
std::string m_streamName
Name of the ROOT output stream (file)
Definition: AthHistogramming.h:226
AthHistogramming::configAthHistogramming
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.
Definition: AthHistogramming.cxx:66
AthHistogramming::hash_t
uint32_t hash_t
typedef for the internal hash
Definition: AthHistogramming.h:169
CxxUtils::crc64
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition: crc64.cxx:696
AthHistogramming::m_histTitlePostfix
std::string m_histTitlePostfix
The postfix for the histogram THx title.
Definition: AthHistogramming.h:241
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
AthHistogramming::hash
hash_t hash(const std::string &histName) const
Method to calculate a 32-bit hash from a string.
Definition: AthHistogramming.h:428
AthHistogramming::book
StatusCode book(const TGraph &graphRef, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of TGraphs.
xxh3::hash64
std::uint64_t hash64(const void *data, std::size_t size)
Passthrough to XXH3_64bits.
Definition: XXH.cxx:9
AthHistogramming::AthHistogramming
AthHistogramming(const std::string &name)
Constructor with parameters:
Definition: AthHistogramming.cxx:41
AthHistogramming::m_effMap
EffMap_t m_effMap
The map of histogram names to their pointers.
Definition: AthHistogramming.h:208
AthHistogramming::m_histTitlePrefix
std::string m_histTitlePrefix
The prefix for the histogram THx title.
Definition: AthHistogramming.h:238
AthHistogramming::m_treeMap
TreeMap_t m_treeMap
The map of TTree names to their pointers.
Definition: AthHistogramming.h:215
AthHistogramming::m_histNamePrefix
std::string m_histNamePrefix
The prefix for the histogram THx name.
Definition: AthHistogramming.h:232
AthHistogramming::hist
TH1 * hist(const std::string &histName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered histograms of any type.
Definition: AthHistogramming.cxx:198
AthHistogramming::m_histSvc
ServiceHandle< ITHistSvc > m_histSvc
Pointer to the THistSvc (event store by default)
Definition: AthHistogramming.h:194
dqt_zlumi_alleff_HIST.eff
int eff
Definition: dqt_zlumi_alleff_HIST.py:113
str
Definition: BTagTrackIpAccessor.cxx:11
crc64.h
A crc-64 implementation, using pclmul where possible.
AthHistogramming::m_name
std::string m_name
Instance name.
Definition: AthHistogramming.h:245
AthHistogramming::HistMap_t
std::map< const hash_t, TH1 * > HistMap_t
Typedef for convenience.
Definition: AthHistogramming.h:198
AthHistogramming::tree
TTree * tree(const std::string &treeName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TTrees.
Definition: AthHistogramming.cxx:378
AthHistogramming::EffMap_t
std::map< const hash_t, TEfficiency * > EffMap_t
Typedef for convenience.
Definition: AthHistogramming.h:205
AthHistogramming::buildBookingString
void buildBookingString(std::string &bookingString, std::string &histName, std::string &tDir, std::string &stream, bool usePrefixPostfix=false)
Method to build individual booking string.
Definition: AthHistogramming.cxx:560
ServiceHandle< ITHistSvc >
AthHistogramming::bookGetPointer
TH1 * bookGetPointer(const TH1 &hist, const std::string &tDir="", const std::string &stream="")
Simplify the booking and registering (into THistSvc) of histograms.
Definition: AthHistogramming.h:260