The Gaudi Framework  master (37c0b60a)
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 // ============================================================================
36 #include <GaudiKernel/DataObject.h>
38 #include <GaudiKernel/IRegistry.h>
40 #include <GaudiKernel/MsgStream.h>
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  }
87  return PersistencySvc::finalize();
88 }
89 // ============================================================================
90 // Initialize the service.
91 // ============================================================================
94  return status.isSuccess() ? reinitialize() : status;
95 }
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 // ============================================================================
143 namespace {
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 ) {
202  } else {
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 // ============================================================================
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
PersistencySvc
PersistencySvc class implementation definition.
Definition: PersistencySvc.h:57
PersistencySvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: PersistencySvc.cpp:111
PersistencySvc::finalize
StatusCode finalize() override
stop the service.
Definition: PersistencySvc.cpp:496
HistogramPersistencySvc::HistogramPersistencySvc
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
Definition: HistogramPersistencySvc.cpp:212
DataObject::name
const std::string & name() const
Retreive DataObject name. It is the name when registered in the store.
Definition: DataObject.cpp:72
std::string
STL class.
Gaudi.Configuration.log
log
Definition: Configuration.py:28
AtlasMCRecoFullPrecedenceDump.path
path
Definition: AtlasMCRecoFullPrecedenceDump.py:49
PersistencySvc::conversionSvc
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
Definition: PersistencySvc.cpp:166
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:314
HistogramPersistencySvc::m_convert
Gaudi::Property< std::vector< std::string > > m_convert
Definition: HistogramPersistencySvc.h:85
gaudirun.s
string s
Definition: gaudirun.py:346
HistogramPersistencySvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: HistogramPersistencySvc.cpp:180
IOpaqueAddress
Definition: IOpaqueAddress.h:33
std::vector< std::string >
PropertyHolder< CommonMessaging< implements< IService, IProperty, IStateful > > >::setProperty
StatusCode setProperty(const Gaudi::Details::PropertyBase &p)
Set the property from a property.
Definition: IProperty.h:39
ISvcLocator
Definition: ISvcLocator.h:46
std::any_of
T any_of(T... args)
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
IDataProviderSvc.h
HistogramPersistencySvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: HistogramPersistencySvc.cpp:92
HistogramPersistencySvc::m_excluded
Set m_excluded
for the final report: the list of excluded histograms
Definition: HistogramPersistencySvc.h:94
IProperty
Definition: IProperty.h:33
SmartIF.h
StatusCode
Definition: StatusCode.h:65
gaudirun.opts
opts
Definition: gaudirun.py:336
HistogramPersistencySvc.h
PersistencySvc::service
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
Definition: PersistencySvc.cpp:194
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
HistogramPersistencySvc::m_converted
Set m_converted
for the final report: the list of converted histograms
Definition: HistogramPersistencySvc.h:92
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
HistogramPersistencySvc::m_histPersName
Gaudi::Property< std::string > m_histPersName
Definition: HistogramPersistencySvc.h:83
PersistencySvc::m_svcNames
Gaudi::Property< std::vector< std::string > > m_svcNames
Definition: PersistencySvc.h:236
IRegistry.h
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
HistogramPersistencySvc::m_warnings
Gaudi::Property< bool > m_warnings
Definition: HistogramPersistencySvc.h:89
PersistencySvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: PersistencySvc.cpp:487
PersistencySvc::setConversionSvc
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
Definition: PersistencySvc.cpp:160
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
std::endl
T endl(T... args)
DataObject.h
HistogramPersistencySvc::reinitialize
StatusCode reinitialize() override
Reinitialize the service.
Definition: HistogramPersistencySvc.cpp:99
HistogramPersistencySvc::m_outputFile
Gaudi::Property< std::string > m_outputFile
Definition: HistogramPersistencySvc.h:84
HistogramPersistencySvc
HistogramPersistencySvc class implementation definition.
Definition: HistogramPersistencySvc.h:57
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::set::insert
T insert(T... args)
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:36
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
PersistencySvc::enable
bool enable(bool value)
Set enabled flag.
Definition: PersistencySvc.cpp:506
ISvcLocator.h
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:78
HistogramPersistencySvc::m_exclude
Gaudi::Property< std::vector< std::string > > m_exclude
Definition: HistogramPersistencySvc.h:87
MsgStream.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335