The Gaudi Framework  v33r1 (b1225454)
HistogramPersistencySvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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"
39 #include "GaudiKernel/IRegistry.h"
41 #include "GaudiKernel/MsgStream.h"
42 #include "GaudiKernel/SmartIF.h"
43 // ============================================================================
44 // local
45 // ============================================================================
47 // ============================================================================
48 // AIDA
49 // ============================================================================
51 #ifdef __clang__
52 # pragma clang diagnostic push
53 # pragma clang diagnostic ignored "-Wkeyword-macro"
54 #endif
55 #define class class GAUDI_API
56 #ifdef __clang__
57 # pragma clang diagnostic pop
58 #endif
59 #include "AIDA/IBaseHistogram.h"
60 #undef class
61 // ============================================================================
62 // Instantiation of a static factory class used by clients to create
63 // instances of this service
65 
66 // ============================================================================
67 // Finalize the service.
69  //
70  if ( !( m_convert.empty() && m_exclude.empty() ) ) { // print message if any of the two properties is used
71  info() << "Histograms Converted/Excluded: " << m_converted.size() << "/" << m_excluded.size() << endmsg;
72  }
73  if ( msgLevel( MSG::DEBUG ) ) {
74  if ( !m_excluded.empty() ) {
75  auto& log = debug();
76  log << "Excluded Histos : #" << m_excluded.size();
77  for ( const auto& item : m_excluded ) { log << std::endl << " '" << item << "'"; }
78  log << endmsg;
79  }
80  //
81  if ( !m_converted.empty() ) {
82  auto& log = debug();
83  log << "Converted Histos : #" << m_converted.size();
84  for ( const auto& item : m_converted ) { log << std::endl << " '" << item << "'"; }
85  log << endmsg;
86  }
87  }
88  return PersistencySvc::finalize();
89 }
90 // ============================================================================
91 // Initialize the service.
92 // ============================================================================
95  return status.isSuccess() ? reinitialize() : status;
96 }
97 // ============================================================================
98 // Reinitialize the service.
99 // ============================================================================
101  // Obtain the IProperty of the ApplicationMgr
102  auto prpMgr = serviceLocator()->as<IProperty>();
103  if ( !prpMgr ) {
104  fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
105  return StatusCode::FAILURE;
106  } else {
107  setProperty( prpMgr->getProperty( "HistogramPersistency" ) ).ignore();
108  }
109 
110  // To keep backward compatibility, we set the property of conversion service
111  // into JobOptions catalogue
112  if ( !m_outputFile.empty() ) {
113  auto joptsvc = serviceLocator()->service<IJobOptionsSvc>( "JobOptionsSvc" );
114  if ( joptsvc ) {
115  Gaudi::Property<std::string> p( "OutputFile", m_outputFile );
116  if ( m_histPersName == "ROOT" ) {
117  joptsvc->addPropertyToCatalogue( "RootHistSvc", p ).ignore();
118  } else if ( m_histPersName == "HBOOK" ) {
119  joptsvc->addPropertyToCatalogue( "HbookHistSvc", p ).ignore();
120  }
121  }
122  }
123 
124  // Load the Histogram persistency service that's required as default
125  setConversionSvc( nullptr ).ignore();
126  if ( m_histPersName == "ROOT" ) {
127  setConversionSvc( service( "RootHistSvc" ) ).ignore();
128  if ( !conversionSvc() ) { return StatusCode::FAILURE; }
129  enable( true );
130  } else if ( m_histPersName == "HBOOK" ) {
131  setConversionSvc( service( "HbookHistSvc" ) ).ignore();
132  if ( !conversionSvc() ) { return StatusCode::FAILURE; }
133  enable( true );
134  } else if ( m_histPersName == "NONE" ) {
135  enable( false );
136  if ( msgLevel( MSG::DEBUG ) ) debug() << "Histograms saving not required." << endmsg;
137  } else {
139  if ( !conversionSvc() ) { return StatusCode::FAILURE; }
140  enable( true );
141  if ( m_warnings ) { warning() << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg; }
142  }
143  return StatusCode::SUCCESS;
144 }
145 // ============================================================================
146 namespace {
147  // ==========================================================================
149  const std::string s_NULL = "<NULL>";
150  // ==========================================================================
156  // ==========================================================================
157  inline bool match( const std::string& name, const std::string& pat ) {
158  // the most primitive match
159  return std::string::npos != name.find( pat );
160  }
161  // ==========================================================================
166  inline const std::string& oname( const DataObject* obj ) {
167  if ( !obj ) { return s_NULL; }
168  auto reg = obj->registry();
169  return reg ? reg->identifier() : obj->name();
170  }
171  // ==========================================================================
177  inline bool match( const DataObject* obj, const std::string& pat ) { return obj && match( oname( obj ), pat ); }
178  // ==========================================================================
179 } // namespace
180 // ============================================================================
181 // Convert the transient object to the requested representation.
182 // ============================================================================
184  // enable the conversion
185  enable( true );
186  // conversion is possible ?
187  if ( "NONE" == m_histPersName ) {
188  enable( false );
189  return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
190  }
191  // histogram ?
192  if ( dynamic_cast<AIDA::IBaseHistogram*>( pObj ) ) {
193 
194  auto match_pObj = [&]( const std::string& s ) { return match( pObj, s ); };
195  // Empty ConvertHistos property means convert all
196  bool select = ( m_convert.empty() || std::any_of( m_convert.begin(), m_convert.end(), match_pObj ) ) &&
197  std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
198  //
199  enable( select );
200  //
201  const auto& path = oname( pObj );
202  //
203  if ( !select ) {
205  } else {
207  }
208  }
209  //
210  return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
211 }
212 // ============================================================================
213 // Standard Constructor
214 // ============================================================================
216  : PersistencySvc( name, svc ) {
217  // bypass update handler
218  m_svcNames.value() = std::vector<std::string>{{"RootHistSvc"}};
219 }
220 
221 // ============================================================================
222 // The END
223 // ============================================================================
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:287
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:35
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:82
StatusCode initialize() override
Initialize the service.
Gaudi::Property< std::vector< std::string > > m_convert
Implementation of property with value of concrete type.
Definition: Property.h:370
StatusCode setProperty(const Gaudi::Details::PropertyBase &p) override
set the property form another property
Set m_excluded
for the final report: the list of excluded histograms
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Gaudi::Property< std::vector< std::string > > m_svcNames
Gaudi::Property< std::string > m_histPersName
T endl(T... args)
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
SmartIF< IFace > as()
Definition: ISvcLocator.h:113
StatusCode reinitialize() override
Reinitialize the service.
Set m_converted
for the final report: the list of converted histograms
Gaudi::Property< std::string > m_outputFile
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
#define DECLARE_COMPONENT(type)
Gaudi::Property< std::vector< std::string > > m_exclude
bool enable(bool value)
Set enabled flag.
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:86
Main interface for the JobOptions service.
StatusCode initialize() override
Initialize the service.
Gaudi::Property< bool > m_warnings
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
StatusCode finalize() override
stop the service.
PersistencySvc class implementation definition.
HistogramPersistencySvc class implementation definition.
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
bool isSuccess() const
Definition: StatusCode.h:365
const std::string & name() const
Retreive DataObject name. It is the name when registered in the store.
Definition: DataObject.cpp:72
T insert(T... args)
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
T any_of(T... args)
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:101
virtual const id_type & identifier() const =0
Full identifier (or key)
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:30
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202