The Gaudi Framework  v29r0 (ff2e7097)
HistogramPersistencySvc.cpp
Go to the documentation of this file.
1 // ============================================================================
2 // HistogramPersistencySvc.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : System ( The LHCb Offline System)
6 //
7 // Description: implementation of the Event data persistency service
8 // This specialized service only deals with event related
9 // data
10 //
11 // Author : M.Frank
12 // History :
13 // +---------+----------------------------------------------+---------
14 // | Date | Comment | Who
15 // +---------+----------------------------------------------+---------
16 // | 29/10/98| Initial version | MF
17 // +---------+----------------------------------------------+---------
18 //
19 //====================================================================
20 #define PERSISTENCYSVC_HISTOGRAMPERSISTENCYSVC_CPP
21 // ============================================================================
22 // Include files
23 // ============================================================================
24 // GaudiKernel
25 // ============================================================================
26 #include "GaudiKernel/DataObject.h"
29 #include "GaudiKernel/IRegistry.h"
31 #include "GaudiKernel/MsgStream.h"
32 #include "GaudiKernel/SmartIF.h"
33 // ============================================================================
34 // local
35 // ============================================================================
37 // ============================================================================
38 // AIDA
39 // ============================================================================
41 #ifdef __clang__
42 #pragma clang diagnostic push
43 #pragma clang diagnostic ignored "-Wkeyword-macro"
44 #endif
45 #define class class GAUDI_API
46 #ifdef __clang__
47 #pragma clang diagnostic pop
48 #endif
49 #include "AIDA/IBaseHistogram.h"
50 #undef class
51 // ============================================================================
52 // Instantiation of a static factory class used by clients to create
53 // instances of this service
55 
56 // ============================================================================
57 // Finalize the service.
59 {
60  //
61  if ( !( m_convert.empty() && m_exclude.empty() ) ) { // print message if any of the two properties is used
62  info() << "Histograms Converted/Excluded: " << m_converted.size() << "/" << m_excluded.size() << endmsg;
63  }
64  if ( msgLevel( MSG::DEBUG ) ) {
65  if ( !m_excluded.empty() ) {
66  auto& log = debug();
67  log << "Excluded Histos : #" << m_excluded.size();
68  for ( const auto& item : m_excluded ) {
69  log << std::endl << " '" << item << "'";
70  }
71  log << endmsg;
72  }
73  //
74  if ( !m_converted.empty() ) {
75  auto& log = debug();
76  log << "Converted Histos : #" << m_converted.size();
77  for ( const auto& item : m_converted ) {
78  log << std::endl << " '" << item << "'";
79  }
80  log << endmsg;
81  }
82  }
83  return PersistencySvc::finalize();
84 }
85 // ============================================================================
86 // Initialize the service.
87 // ============================================================================
89 {
91  return status.isSuccess() ? reinitialize() : status;
92 }
93 // ============================================================================
94 // Reinitialize the service.
95 // ============================================================================
97 {
98  // Obtain the IProperty of the ApplicationMgr
99  auto prpMgr = serviceLocator()->as<IProperty>();
100  if ( !prpMgr ) {
101  fatal() << "IProperty interface not found in ApplicationMgr." << endmsg;
102  return StatusCode::FAILURE;
103  } else {
104  setProperty( prpMgr->getProperty( "HistogramPersistency" ) ).ignore();
105  }
106 
107  // To keep backward compatibility, we set the property of conversion service
108  // into JobOptions catalogue
109  if ( !m_outputFile.empty() ) {
110  auto joptsvc = serviceLocator()->service<IJobOptionsSvc>( "JobOptionsSvc" );
111  if ( joptsvc ) {
112  Gaudi::Property<std::string> p( "OutputFile", m_outputFile );
113  if ( m_histPersName == "ROOT" ) {
114  joptsvc->addPropertyToCatalogue( "RootHistSvc", p ).ignore();
115  } else if ( m_histPersName == "HBOOK" ) {
116  joptsvc->addPropertyToCatalogue( "HbookHistSvc", p ).ignore();
117  }
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() ) {
126  return StatusCode::FAILURE;
127  }
128  enable( true );
129  } else if ( m_histPersName == "HBOOK" ) {
130  setConversionSvc( service( "HbookHistSvc" ) ).ignore();
131  if ( !conversionSvc() ) {
132  return StatusCode::FAILURE;
133  }
134  enable( true );
135  } else if ( m_histPersName == "NONE" ) {
136  enable( false );
137  if ( m_warnings ) {
138  warning() << "Histograms saving not required." << endmsg;
139  }
140  } else {
142  if ( !conversionSvc() ) {
143  return StatusCode::FAILURE;
144  }
145  enable( true );
146  if ( m_warnings ) {
147  warning() << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
148  }
149  }
150  return StatusCode::SUCCESS;
151 }
152 // ============================================================================
153 namespace
154 {
155  // ==========================================================================
157  const std::string s_NULL = "<NULL>";
158  // ==========================================================================
164  // ==========================================================================
165  inline bool match( const std::string& name, const std::string& pat )
166  {
167  // the most primitive match
168  return std::string::npos != name.find( pat );
169  }
170  // ==========================================================================
175  inline const std::string& oname( const DataObject* obj )
176  {
177  if ( !obj ) {
178  return s_NULL;
179  }
180  auto reg = obj->registry();
181  return reg ? reg->identifier() : obj->name();
182  }
183  // ==========================================================================
189  inline bool match( const DataObject* obj, const std::string& pat ) { return obj && match( oname( obj ), pat ); }
190  // ==========================================================================
191 }
192 // ============================================================================
193 // Convert the transient object to the requested representation.
194 // ============================================================================
196 {
197  // enable the conversion
198  enable( true );
199  // conversion is possible ?
200  if ( "NONE" == m_histPersName ) {
201  enable( false );
202  return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
203  }
204  // histogram ?
205  if ( dynamic_cast<AIDA::IBaseHistogram*>( pObj ) ) {
206 
207  auto match_pObj = [&]( const std::string& s ) { return match( pObj, s ); };
208  // Empty ConvertHistos property means convert all
209  bool select = ( m_convert.empty() || std::any_of( m_convert.begin(), m_convert.end(), match_pObj ) ) &&
210  std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
211  //
212  enable( select );
213  //
214  const auto& path = oname( pObj );
215  //
216  if ( !select ) {
218  } else {
220  }
221  }
222  //
223  return PersistencySvc::createRep( pObj, refpAddr ); // RETURN
224 }
225 // ============================================================================
226 // Standard Constructor
227 // ============================================================================
229  : PersistencySvc( name, svc )
230 {
231  m_svcNames = std::vector<std::string>{{"RootHistSvc"}};
232 }
233 
234 // ============================================================================
235 // The END
236 // ============================================================================
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:289
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:319
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
Gaudi::Property< std::vector< std::string > > m_svcNames
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
Gaudi::Property< std::string > m_histPersName
T endl(T...args)
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
SmartIF< IFace > as()
Definition: ISvcLocator.h:109
StatusCode reinitialize() override
Reinitialize the service.
Set m_converted
for the final report: the list of converted histograms
Gaudi::Property< std::string > m_outputFile
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:33
STL class.
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:79
Main interface for the JobOptions service.
StatusCode initialize() override
Initialize the service.
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Gaudi::Property< bool > m_warnings
virtual const id_type & identifier() const =0
Full identifier (or key)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:28
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.
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
T insert(T...args)
T find(T...args)
T any_of(T...args)
string s
Definition: gaudirun.py:253
const std::string & name() const
Retreive DataObject name. It is the name when registered in the store.
Definition: DataObject.cpp:68
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:109
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:20
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:29
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209