ATLAS Offline Software
Loading...
Searching...
No Matches
AthHistogramming.cxx
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.cxx
8// Implementation file for class AthHistogramming
9// Author: Karsten Koeneke
11
12// This class' header includes
14
15// Framework includes
16#include "GaudiKernel/MsgStream.h"
17#include "GaudiKernel/ITHistSvc.h"
19
20// STL includes
21#include <string>
22#include <map>
23
24// ROOT includes
25#include "TH1.h"
26#include "TH2.h"
27#include "TH3.h"
28#include "TEfficiency.h"
29#include "TTree.h"
30#include "TGraph.h"
31
32
33
34
36// Public methods:
38
39// Constructors
41AthHistogramming::AthHistogramming( const std::string& name ) :
42 m_histSvc ( "THistSvc/THistSvc", name ),
43 m_streamName(""),
44 m_name ( name ),
45 m_msg( Athena::getMessageSvc(), name )
46{
47}
48
49
50
51
52// Destructor
55{
56 //ATH_MSG_DEBUG ("Calling destructor of AthHistogramming");
57 if ( m_msg.level() <= MSG::VERBOSE ) m_msg << MSG::DEBUG << "Calling destructor of AthHistogramming" << endmsg;
58}
59
60
62// Non-const methods:
64
65// To be called by the derived classes to fill the internal configuration
67 const std::string& prefix, const std::string& rootDir,
68 const std::string& histNamePrefix, const std::string& histNamePostfix,
69 const std::string& histTitlePrefix, const std::string& histTitlePostfix )
70{
72 m_streamName = prefix;
73 m_rootDir = rootDir;
74 m_histNamePrefix = histNamePrefix;
75 m_histNamePostfix = histNamePostfix;
76 m_histTitlePrefix = histTitlePrefix;
77 m_histTitlePostfix = histTitlePostfix;
78
79 return StatusCode::SUCCESS;
80}
81
82
83
84
86// Protected methods:
88
89
90// -----------------------
91// For histogramming
92// -----------------------
93
94
95// =============================================================================
96// Simplify the booking and registering (into THistSvc) of histograms
97// =============================================================================
98TH1* AthHistogramming::bookGetPointer( TH1& histRef, std::string tDir, std::string stream )
99{
100 // Modify the name and title according to the prefixes of this classes instance
101 std::string histName(histRef.GetName());
102 const std::string histTitle(histRef.GetTitle());
103 std::string bookingString("");
104
105 this->buildBookingString( bookingString, histName, tDir, stream );
106 histRef.SetTitle((m_histTitlePrefix+histTitle+m_histTitlePostfix).c_str() );
107 histRef.SetName(histName.c_str());
108
109 // Check if the hash for this histName already exists, i.e., if we have a hash collision
110 const hash_t histHash = this->hash(histName);
111 HistMap_t::const_iterator it = m_histMap.find( histHash );
112 if ( it != m_histMap.end() ) // It does exist!
113 {
114 m_msg << MSG::WARNING
115 << "Detected a hash collision. The hash for the histogram with name=" << histName
116 << " already exists and points to a histogram with name=" << it->second->GetName()
117 << " NOT going to book the new histogram and returning a NULL pointer!" << endmsg;
118 return NULL;
119 }
120
121 // Set the new name and title for the histogram, based on the prefixes that the user set for this class instance
122 // Create a clone that has the new name
123
124 // Massage the final string to book things
125
126 // Register the histogram into the THistSvc
127 if ( !((histSvc()->regHist(bookingString, &histRef)).isSuccess()) )
128 {
129 m_msg << MSG::WARNING
130 << "Problem registering histogram with name " << histName
131 << ", name prefix " << m_histNamePrefix
132 << ", title " << histTitle
133 << ", tile prefix " << m_histTitlePrefix
134 << ", and tile postfix " << m_histTitlePostfix
135 << " in " << m_name << "!" << endmsg;
136 return NULL;
137 }
138
139 // Also register it in the local map of string to pointer
140 m_histMap.insert( m_histMap.end(), std::pair< const hash_t, TH1* >( histHash, &histRef ) );
141
142 return &histRef;
143}
144
145
146TEfficiency* AthHistogramming::bookGetPointer( TEfficiency& effRef, std::string tDir, std::string stream )
147{
148 // Modify the name and title according to the prefixes of this classes instance
149 std::string effName(effRef.GetName());
150 const std::string effTitle(effRef.GetTitle());
151 std::string bookingString("");
152
153 this->buildBookingString( bookingString, effName, tDir, stream );
154 effRef.SetTitle((m_histTitlePrefix+effTitle+m_histTitlePostfix).c_str() );
155 effRef.SetName(effName.c_str());
156
157 // Check if the hash for this effName already exists, i.e., if we have a hash collision
158 const hash_t effHash = this->hash(effName);
159 EffMap_t::const_iterator it = m_effMap.find( effHash );
160 if ( it != m_effMap.end() ) // It does exist!
161 {
162 m_msg << MSG::WARNING
163 << "Detected a hash collision. The hash for the TEfficiency with name=" << effName
164 << " already exists and points to a TEfficiency with name=" << it->second->GetName()
165 << " NOT going to book the new TEfficiency and returning a NULL pointer!" << endmsg;
166 return NULL;
167 }
168
169 // Set the new name and title for the TEfficiency, based on the prefixes that the user set for this class instance
170 // Create a clone that has the new name
171
172 // Massage the final string to book things
173
174 // Register the TEfficiency into the THistSvc
175 if ( !((histSvc()->regEfficiency(bookingString, &effRef)).isSuccess()) )
176 {
177 m_msg << MSG::WARNING
178 << "Problem registering TEfficiency with name " << effName
179 << ", name prefix " << m_histNamePrefix
180 << ", title " << effTitle
181 << ", tile prefix " << m_histTitlePrefix
182 << ", and tile postfix " << m_histTitlePostfix
183 << " in " << m_name << "!" << endmsg;
184 return NULL;
185 }
186
187 // Also register it in the local map of string to pointer
188 m_effMap.insert( m_effMap.end(), std::pair< const hash_t, TEfficiency* >( effHash, &effRef ) );
189
190 return &effRef;
191}
192
193
194
195// =============================================================================
196// Simplify the retrieval of registered histograms of any type
197// =============================================================================
198TH1* AthHistogramming::hist( const std::string& histName, const std::string& tDir, const std::string& stream )
199{
200 // Build a 32 bit hash out of the name
201 const hash_t histHash = this->hash(histName);
202
203 // See if this entry exists in the map
204 HistMap_t::const_iterator it = m_histMap.find( histHash );
205 if ( it == m_histMap.end() ) // It doesn't exist!
206 { // Let's see into the THistSvc if somebody else has registered the histogram...
207
208 // Need to copy the strings as we will massage them from here on
209 std::string histNameCopy = histName;
210 std::string tDirCopy = tDir;
211 std::string streamCopy = stream;
212
213 // Massage the final string to book things
214 std::string bookingString("");
215 this->buildBookingString( bookingString, histNameCopy, tDirCopy, streamCopy ,false);
216
217 TH1* histPointer(NULL);
218 if ( !((histSvc()->getHist(bookingString, histPointer)).isSuccess()) )
219 {
220 // Massage the final string to book things
221 std::string bookingString("");
222 this->buildBookingString( bookingString, histNameCopy, tDirCopy, streamCopy, true );
223
224 if ( !((histSvc()->getHist(bookingString, histPointer)).isSuccess()) )
225 {
226 m_msg << MSG::WARNING
227 << "Problem retrieving the histogram with name (including pre- and post-fixes) "
228 << m_histNamePrefix + histNameCopy + m_histNamePostfix
229 << " or with name " << histNameCopy
230 << " in " << m_name << "... it doesn't exist, neither in the cached map nor in the THistSvc!"
231 << " Will return an NULL pointer... you have to handle it correctly!" << endmsg;
232 return NULL;
233 }
234 // If we get to here, we actually found the histogram in the THistSvc.
235 // So let's add it to the local cache map and return its pointer
236 m_histMap.insert( m_histMap.end(), std::pair< const hash_t, TH1* >( histHash, histPointer ) );
237 return histPointer;
238 }
239 // If we get to here, we actually found the histogram in the THistSvc.
240 // So let's add it to the local cache map and return its pointer
241 m_histMap.insert( m_histMap.end(), std::pair< const hash_t, TH1* >( histHash, histPointer ) );
242 return histPointer;
243 }
244
245
246 // Return the pointer to the histogram that we got from the local cache map
247 return it->second;
248}
249
250TEfficiency* AthHistogramming::efficiency( const std::string& effName, const std::string& tDir, const std::string& stream )
251{
252 // Build a 32 bit hash out of the name
253 const hash_t effHash = this->hash(effName);
254
255 // See if this entry exists in the map
256 EffMap_t::const_iterator it = m_effMap.find( effHash );
257 if ( it == m_effMap.end() ) // It doesn't exist!
258 { // Let's see into the THistSvc if somebody else has registered the TEfficiency...
259
260 // Need to copy the strings as we will massage them from here on
261 std::string effNameCopy = effName;
262 std::string tDirCopy = tDir;
263 std::string streamCopy = stream;
264
265 // Massage the final string to book things
266 std::string bookingString("");
267 this->buildBookingString( bookingString, effNameCopy, tDirCopy, streamCopy ,false);
268
269 TEfficiency* effPointer(NULL);
270 if ( !((histSvc()->getEfficiency(bookingString, effPointer)).isSuccess()) )
271 {
272 // Massage the final string to book things
273 std::string bookingString("");
274 this->buildBookingString( bookingString, effNameCopy, tDirCopy, streamCopy, true );
275
276 if ( !((histSvc()->getEfficiency(bookingString, effPointer)).isSuccess()) )
277 {
278 m_msg << MSG::WARNING
279 << "Problem retrieving the TEfficiency with name (including pre- and post-fixes) "
280 << m_histNamePrefix + effNameCopy + m_histNamePostfix
281 << " or with name " << effNameCopy
282 << " in " << m_name << "... it doesn't exist, neither in the cached map nor in the THistSvc!"
283 << " Will return an NULL pointer... you have to handle it correctly!" << endmsg;
284 return NULL;
285 }
286 // If we get to here, we actually found the TEfficiency in the THistSvc.
287 // So let's add it to the local cache map and return its pointer
288 m_effMap.insert( m_effMap.end(), std::pair< const hash_t, TEfficiency* >( effHash, effPointer ) );
289 return effPointer;
290 }
291 // If we get to here, we actually found the TEfficiency in the THistSvc.
292 // So let's add it to the local cache map and return its pointer
293 m_effMap.insert( m_effMap.end(), std::pair< const hash_t, TEfficiency* >( effHash, effPointer ) );
294 return effPointer;
295 }
296
297 // Return the pointer to the TEfficiency that we got from the local cache map
298 return it->second;
299}
300
301
302
303
304
305// -----------------------
306// For TTrees
307// -----------------------
308
309// =============================================================================
310// Simplify the booking and registering (into THistSvc) of TTrees
311// =============================================================================
312TTree* AthHistogramming::bookGetPointer( const TTree& treeRef, std::string tDir, std::string stream )
313{
314 // Get a pointer
315 const TTree* treePointer = &treeRef;
316
317 // Check that we got a valid pointer
318 if ( !treePointer )
319 {
320 m_msg << MSG::WARNING
321 << "We got an invalid TTree pointer in the BookGetPointer(TTree*) method of the class" << m_name
322 << "!" << endmsg;
323 return NULL;
324 }
325
326 // Modify the name and title according to the prefixes of this classes instance
327 std::string treeName = treePointer->GetName();
328 const std::string treeTitle = treePointer->GetTitle();
329
330 // Check if the hash for this treeName already exists, i.e., if we have a hash collision
331 const hash_t treeHash = this->hash(treeName);
332 TreeMap_t::const_iterator it = m_treeMap.find( treeHash );
333 if ( it != m_treeMap.end() ) // It does exist!
334 {
335 m_msg << MSG::WARNING
336 << "Detected a hash collision. The hash for the TTree with name=" << treeName
337 << " already exists and points to a TTree with name=" << it->second->GetName()
338 << " NOT going to book the new histogram and returning a NULL pointer!" << endmsg;
339 return NULL;
340 }
341
342 // Create a clone that has the new name
343 TTree* treeClone = dynamic_cast< TTree* >( treePointer->Clone(treeName.c_str()) );
344 if( !treeClone )
345 {
346 m_msg << MSG::WARNING
347 << "We couldn't clone the TTree in the BookGetPointer(TTree&) method of the class" << m_name
348 << "!" << endmsg;
349 return NULL;
350 }
351 treeClone->SetTitle (treeTitle.c_str());
352
353 // Massage the final string to book things
354 std::string bookingString("");
355 this->buildBookingString( bookingString, treeName, tDir, stream );
356
357 // Register the TTree into the THistSvc
358 if ( !((histSvc()->regTree(bookingString, treeClone)).isSuccess()) )
359 {
360 m_msg << MSG::WARNING
361 << "Problem registering TTree with name " << treeName
362 << ", title " << treeTitle
363 << " in " << m_name << "!" << endmsg;
364 return NULL;
365 }
366
367 // Also register it in the local map of string to pointer
368 m_treeMap.insert( m_treeMap.end(), std::pair< const hash_t, TTree* >( treeHash, treeClone ) );
369
370 return treeClone;
371}
372
373
374
375// =============================================================================
376// Simplify the retrieval of registered TTrees
377// =============================================================================
378TTree* AthHistogramming::tree( const std::string& treeName, const std::string& tDir, const std::string& stream )
379{
380 // Build a 32 bit hash out of the name
381 const hash_t treeHash = this->hash(treeName);
382
383 // See if this entry exists in the map
384 TreeMap_t::const_iterator it = m_treeMap.find( treeHash );
385 if ( it == m_treeMap.end() ) // It doesn't exist!
386 { // Let's see into the THistSvc if somebody else has registered the TTree...
387
388 // Need to copy the strings as we will massage them from here on
389 std::string treeNameCopy = treeName;
390 std::string tDirCopy = tDir;
391 std::string streamCopy = stream;
392
393 // Massage the final string to book things
394 std::string bookingString("");
395 this->buildBookingString( bookingString, treeNameCopy, tDirCopy, streamCopy );
396
397 TTree* treePointer(NULL);
398 if ( !((histSvc()->getTree(bookingString, treePointer)).isSuccess()) )
399 {
400 m_msg << MSG::WARNING
401 << "Problem retrieving the TTree with name " << treeNameCopy
402 << " in " << m_name << "... it doesn't exist, neither in the cached map nor in the THistSvc!"
403 << " Will return an NULL pointer... you have to handle it correctly!" << endmsg;
404 return NULL;
405 }
406 // If we get to here, we actually found the TTree in the THistSvc.
407 // So let's add it to the local cache map and return its pointer
408 m_treeMap.insert( m_treeMap.end(), std::pair< const hash_t, TTree* >( treeHash, treePointer ) );
409 return treePointer;
410 }
411
412 // Return the pointer to the TTree that we got from the local cache map
413 return it->second;
414}
415
416
417
418
419
420// -----------------------
421// For TGraphs
422// -----------------------
423
424// =============================================================================
425// Simplify the booking and registering (into THistSvc) of TGraphs
426// =============================================================================
427TGraph* AthHistogramming::bookGetPointer( const TGraph& graphRef, std::string tDir, std::string stream )
428{
429 // Get a pointer
430 const TGraph* graphPointer = &graphRef;
431
432 // Check that we got a valid pointer
433 if ( !graphPointer )
434 {
435 m_msg << MSG::WARNING
436 << "We got an invalid TGraph pointer in the BookGetPointer(TGraph*) method of the class" << m_name
437 << "!" << endmsg;
438 return NULL;
439 }
440
441 // Modify the name and title according to the prefixes of this classes instance
442 std::string graphName = graphPointer->GetName();
443 const std::string graphTitle = graphPointer->GetTitle();
444
445 // Check if the hash for this graphName already exists, i.e., if we have a hash collision
446 const hash_t graphHash = this->hash(graphName);
447 GraphMap_t::const_iterator it = m_graphMap.find( graphHash );
448 if ( it != m_graphMap.end() ) // It does exist!
449 {
450 m_msg << MSG::WARNING
451 << "Detected a hash collision. The hash for the TGraph with name=" << graphName
452 << " already exists and points to a TGraph with name=" << it->second->GetName()
453 << " NOT going to book the new histogram and returning a NULL pointer!" << endmsg;
454 return NULL;
455 }
456
457 // Create a clone that has the new name
458 TGraph* graphClone = dynamic_cast< TGraph* >( graphPointer->Clone((m_histNamePrefix+graphName+m_histNamePostfix).c_str()) );
459 if( !graphClone )
460 {
461 m_msg << MSG::WARNING
462 << "We couldn't clone the TGraph in the BookGetPointer(TGraph&) method of the class" << m_name
463 << "!" << endmsg;
464 return NULL;
465 }
466 graphClone->SetTitle ((m_histTitlePrefix+graphTitle+m_histTitlePostfix).c_str());
467
468 // Massage the final string to book things
469 std::string bookingString("");
470 this->buildBookingString( bookingString, graphName, tDir, stream );
471
472 // Register the TGraph into the THistSvc
473 if ( !((histSvc()->regGraph(bookingString, graphClone)).isSuccess()) )
474 {
475 m_msg << MSG::WARNING
476 << "Problem registering TGraph with name " << graphName
477 << ", title " << graphTitle
478 << " in " << m_name << "!" << endmsg;
479 return NULL;
480 }
481
482 // Also register it in the local map of string to pointer
483 m_graphMap.insert( m_graphMap.end(), std::pair< const hash_t, TGraph* >( graphHash, graphClone ) );
484
485 return graphClone;
486}
487
488
489// =============================================================================
490// Simplify the retrieval of registered TGraphs
491// =============================================================================
492TGraph* AthHistogramming::graph( const std::string& graphName, const std::string& tDir, const std::string& stream )
493{
494 // Build a 32 bit hash out of the name
495 const hash_t graphHash = this->hash(graphName);
496
497 // See if this entry exists in the map
498 GraphMap_t::const_iterator it = m_graphMap.find( graphHash );
499 if ( it == m_graphMap.end() ) // It doesn't exist!
500 { // Let's see into the THistSvc if somebody else has registered the TGraph...
501
502 // Need to copy the strings as we will massage them from here on
503 std::string graphNameCopy = graphName;
504 std::string tDirCopy = tDir;
505 std::string streamCopy = stream;
506
507 // Massage the final string to book things
508 std::string bookingString("");
509 this->buildBookingString( bookingString, graphNameCopy, tDirCopy, streamCopy, true);
510
511 TGraph* graphPointer(NULL);
512 if ( !((histSvc()->getGraph(bookingString, graphPointer)).isSuccess()) )
513 {
514 // Massage the final string to book things
515 std::string bookingString("");
516 this->buildBookingString( bookingString, graphNameCopy, tDirCopy, streamCopy, false );
517
518 if ( !((histSvc()->getGraph(bookingString, graphPointer)).isSuccess()) )
519 {
520 m_msg << MSG::WARNING
521 << "Problem retrieving the TGraph with name (including pre- and post-fixes) "
522 << m_histNamePrefix + graphNameCopy + m_histNamePostfix
523 << " or with name " << graphNameCopy
524 << " in " << m_name << "... it doesn't exist, neither in the cached map nor in the THistSvc!"
525 << " Will return an NULL pointer... you have to handle it correctly!" << endmsg;
526 return NULL;
527 }
528 // If we get to here, we actually found the TGraph in the THistSvc.
529 // So let's add it to the local cache map and return its pointer
530 m_graphMap.insert( m_graphMap.end(), std::pair< const hash_t, TGraph* >( graphHash, graphPointer ) );
531 return graphPointer;
532 }
533 // If we get to here, we actually found the TGraph in the THistSvc.
534 // So let's add it to the local cache map and return its pointer
535 m_graphMap.insert( m_graphMap.end(), std::pair< const hash_t, TGraph* >( graphHash, graphPointer ) );
536 return graphPointer;
537 }
538
539
540 // Return the pointer to the TGraph that we got from the local cache map
541 return it->second;
542}
543
544
545
546
547
548
549
550
551
552
554// Private methods:
556
557// =============================================================================
558// Helper method to build the final string to be passed to the THistSvc
559// =============================================================================
560void AthHistogramming::buildBookingString( std::string& bookingString,
561 std::string& histName,
562 std::string& tDir,
563 std::string& stream,
564 bool usePrefixPostfix)
565{
566 // Massage the final string to book things
567 if(tDir.empty()) tDir = m_rootDir;
568 size_t pos = histName.rfind('/');
569 if(pos != std::string::npos){
570 tDir+='/';
571 tDir.append(histName, 0,pos);
572 histName.erase(0,pos+1);
573 };
574 if(stream.empty()) stream = m_streamName;
575
576 if(usePrefixPostfix){
577 bookingString = "/"+stream+"/"+tDir+"/"+m_histNamePrefix+histName+m_histNamePostfix;
578 } else {
579 bookingString = "/"+stream+"/"+tDir+"/"+histName;
580 }
581 while(bookingString.find("//") != std::string::npos){
582 this->myReplace(bookingString,"//","/");
583 }
584
585 return;
586}
587
588
589// Helper method to replace all sub-strings
591 const std::string& oldStr,
592 const std::string& newStr)
593{
594 size_t pos = 0;
595 while((pos = str.find(oldStr, pos)) != std::string::npos)
596 {
597 str.replace(pos, oldStr.length(), newStr);
598 pos += newStr.length();
599 }
600}
#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.
TTree * tree(const std::string &treeName, const std::string &tDir="", const std::string &stream="")
Simplify the retrieval of registered TTrees.
ServiceHandle< ITHistSvc > m_histSvc
Pointer to the THistSvc (event store by default)
std::string m_histNamePostfix
The postfix for the histogram THx name.
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.
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.
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)
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.
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.
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.
singleton-like access to IMessageSvc via open function and helper
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....