The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
HistogramPersistencySvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11// ============================================================================
12// HistogramPersistencySvc.cpp
13//--------------------------------------------------------------------
14//
15// Package : System ( The LHCb Offline System)
16//
17// Description: implementation of the Event data persistency service
18// This specialized service only deals with event related
19// data
20//
21// Author : M.Frank
22// History :
23// +---------+----------------------------------------------+---------
24// | Date | Comment | Who
25// +---------+----------------------------------------------+---------
26// | 29/10/98| Initial version | MF
27// +---------+----------------------------------------------+---------
28//
29//====================================================================
30#define PERSISTENCYSVC_HISTOGRAMPERSISTENCYSVC_CPP
31// ============================================================================
32// Include files
33// ============================================================================
34// GaudiKernel
35// ============================================================================
41#include <GaudiKernel/SmartIF.h>
42// ============================================================================
43// local
44// ============================================================================
46// ============================================================================
47// AIDA
48// ============================================================================
50#ifdef __clang__
51# pragma clang diagnostic push
52# pragma clang diagnostic ignored "-Wkeyword-macro"
53#endif
54#define class class GAUDI_API
55#ifdef __clang__
56# pragma clang diagnostic pop
57#endif
58#include <AIDA/IBaseHistogram.h>
59#undef class
60// ============================================================================
61// Instantiation of a static factory class used by clients to create
62// instances of this service
64
65// ============================================================================
66// Finalize the service.
68 //
69 if ( !( m_convert.empty() && m_exclude.empty() ) ) { // print message if any of the two properties is used
70 info() << "Histograms Converted/Excluded: " << m_converted.size() << "/" << m_excluded.size() << endmsg;
71 }
72 if ( msgLevel( MSG::DEBUG ) ) {
73 if ( !m_excluded.empty() ) {
74 auto& log = debug();
75 log << "Excluded Histos : #" << m_excluded.size();
76 for ( const auto& item : m_excluded ) { log << std::endl << " '" << item << "'"; }
77 log << endmsg;
78 }
79 //
80 if ( !m_converted.empty() ) {
81 auto& log = debug();
82 log << "Converted Histos : #" << m_converted.size();
83 for ( const auto& item : m_converted ) { log << std::endl << " '" << item << "'"; }
84 log << endmsg;
85 }
86 }
88}
89// ============================================================================
90// Initialize the service.
91// ============================================================================
96// ============================================================================
97// Reinitialize the service.
98// ============================================================================
100 // Obtain the IProperty of the ApplicationMgr
101 auto prpMgr = serviceLocator()->as<IProperty>();
102 if ( !prpMgr ) {
103 fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
104 return StatusCode::FAILURE;
105 } else {
106 setProperty( prpMgr->getProperty( "HistogramPersistency" ) ).ignore();
107 }
108
109 // To keep backward compatibility, we set the property of conversion service
110 // into JobOptions catalogue
111 if ( !m_outputFile.empty() ) {
112 auto& opts = serviceLocator()->getOptsSvc();
113 const std::string outputFile = '"' + m_outputFile + '"';
114 if ( m_histPersName == "ROOT" ) {
115 opts.set( "RootHistSvc.OutputFile", outputFile );
116 } else if ( m_histPersName == "HBOOK" ) {
117 opts.set( "HbookHistSvc.OutputFile", outputFile );
118 }
119 }
120
121 // Load the Histogram persistency service that's required as default
122 setConversionSvc( nullptr ).ignore();
123 if ( m_histPersName == "ROOT" ) {
124 setConversionSvc( service( "RootHistSvc" ) ).ignore();
125 if ( !conversionSvc() ) { return StatusCode::FAILURE; }
126 enable( true );
127 } else if ( m_histPersName == "HBOOK" ) {
128 setConversionSvc( service( "HbookHistSvc" ) ).ignore();
129 if ( !conversionSvc() ) { return StatusCode::FAILURE; }
130 enable( true );
131 } else if ( m_histPersName == "NONE" ) {
132 enable( false );
133 if ( msgLevel( MSG::DEBUG ) ) debug() << "Histograms saving not required." << endmsg;
134 } else {
136 if ( !conversionSvc() ) { return StatusCode::FAILURE; }
137 enable( true );
138 if ( m_warnings ) { warning() << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg; }
139 }
140 return StatusCode::SUCCESS;
141}
142// ============================================================================
143namespace {
144 // ==========================================================================
146 const std::string s_NULL = "<NULL>";
147 // ==========================================================================
153 // ==========================================================================
154 inline bool match( const std::string& name, const std::string& pat ) {
155 // the most primitive match
156 return std::string::npos != name.find( pat );
157 }
158 // ==========================================================================
163 inline const std::string& oname( const DataObject* obj ) {
164 if ( !obj ) { return s_NULL; }
165 auto reg = obj->registry();
166 return reg ? reg->identifier() : obj->name();
167 }
168 // ==========================================================================
174 inline bool match( const DataObject* obj, const std::string& pat ) { return obj && match( oname( obj ), pat ); }
175 // ==========================================================================
176} // namespace
177// ============================================================================
178// Convert the transient object to the requested representation.
179// ============================================================================
181 // enable the conversion
182 enable( true );
183 // conversion is possible ?
184 if ( m_histPersName == "NONE" ) {
185 enable( false );
186 return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
187 }
188 // histogram ?
189 if ( dynamic_cast<AIDA::IBaseHistogram*>( pObj ) ) {
190
191 auto match_pObj = [&]( const std::string& s ) { return match( pObj, s ); };
192 // Empty ConvertHistos property means convert all
193 bool select = ( m_convert.empty() || std::any_of( m_convert.begin(), m_convert.end(), match_pObj ) ) &&
194 std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
195 //
196 enable( select );
197 //
198 const auto& path = oname( pObj );
199 //
200 if ( !select ) {
201 m_excluded.insert( path );
202 } else {
203 m_converted.insert( path );
204 }
205 }
206 //
207 return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
208}
209// ============================================================================
210// Standard Constructor
211// ============================================================================
213 : PersistencySvc( name, svc ) {
214 // bypass update handler
215 m_svcNames.value() = std::vector<std::string>{ { "RootHistSvc" } };
216}
217
218// ============================================================================
219// The END
220// ============================================================================
bool PyHelper setProperty(IInterface *p, char *name, char *value)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
const std::string & name() const
Retreive DataObject name. It is the name when registered in the store.
IRegistry * registry() const
Get pointer to Registry.
Definition DataObject.h:79
HistogramPersistencySvc class implementation definition.
Set m_converted
for the final report: the list of converted histograms
Gaudi::Property< std::string > m_histPersName
StatusCode initialize() override
Initialize the service.
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
StatusCode finalize() override
Finalize the service.
Gaudi::Property< std::vector< std::string > > m_convert
Gaudi::Property< bool > m_warnings
StatusCode reinitialize() override
Reinitialize the service.
Set m_excluded
for the final report: the list of excluded histograms
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Gaudi::Property< std::string > m_outputFile
Gaudi::Property< std::vector< std::string > > m_exclude
Opaque address interface definition.
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition IProperty.h:32
virtual const id_type & identifier() const =0
Full identifier (or key)
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
SmartIF< IFace > as()
Definition ISvcLocator.h:64
Gaudi::Interfaces::IOptionsSvc & getOptsSvc()
Direct access to Gaudi::Interfaces::IOptionsSvc implementation.
PersistencySvc class implementation definition.
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
Gaudi::Property< std::vector< std::string > > m_svcNames
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
StatusCode initialize() override
Initialize the service.
bool enable(bool value)
Set enabled flag.
StatusCode finalize() override
stop the service.
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition StatusCode.h:139
bool isSuccess() const
Definition StatusCode.h:314
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
@ DEBUG
Definition IMessageSvc.h:22