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/SmartIF.h"
27 #include "GaudiKernel/MsgStream.h"
28 #include "GaudiKernel/ISvcLocator.h"
29 #include "GaudiKernel/IJobOptionsSvc.h"
30 #include "GaudiKernel/DataObject.h"
31 #include "GaudiKernel/IRegistry.h"
32 #include "GaudiKernel/IDataProviderSvc.h"
33 // ============================================================================
34 // local
35 // ============================================================================
37 // ============================================================================
38 // AIDA
39 // ============================================================================
41 #define class class GAUDI_API
42 #include "AIDA/IBaseHistogram.h"
43 #undef class
44 // ============================================================================
45 // Instantiation of a static factory class used by clients to create
46 // instances of this service
48 
49 // ============================================================================
50 // Finalize the service.
52 {
53  //
54  MsgStream log ( msgSvc() , name() );
55  if ( !(m_convert.empty() && m_exclude.empty()) )
56  { // print message if any of the two properties is used
57  log << MSG::INFO << "Histograms Converted/Excluded: "
58  << m_converted.size() << "/" << m_excluded.size() << endmsg ;
59  }
60  if (msgLevel(MSG::DEBUG)) {
61  if ( !m_excluded.empty() )
62  {
63  log << MSG::DEBUG << "Excluded Histos : #" << m_excluded.size() ;
64  for ( const auto& item : m_excluded )
65  { log << std::endl << " '" << item << "'" ; }
66  log << endmsg ;
67  }
68  //
69  if ( !m_converted.empty() )
70  {
71  log << MSG::DEBUG << "Converted Histos : #" << m_converted.size() ;
72  for ( const auto& item : m_converted )
73  { log << std::endl << " '" << item << "'" ; }
74  log << endmsg ;
75  }
76  }
77  return PersistencySvc::finalize();
78 }
79 // ============================================================================
80 // Initialize the service.
81 // ============================================================================
84  if ( status.isSuccess() ) {
85  status = reinitialize();
86  }
87  return status;
88 }
89 // ============================================================================
90 // Reinitialize the service.
91 // ============================================================================
93 {
94  MsgStream log(msgSvc(), name());
95  // Obtain the IProperty of the ApplicationMgr
96  auto prpMgr = serviceLocator()->as<IProperty>();
97  if ( !prpMgr ) {
98  log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
99  return StatusCode::FAILURE;
100  }
101  else {
102  setProperty(prpMgr->getProperty("HistogramPersistency")).ignore();
103  }
104 
105  // To keep backward compatibility, we set the property of conversion service
106  // into JobOptions catalogue
107  if( !m_outputFile.empty() ) {
108  auto joptsvc = serviceLocator()->service<IJobOptionsSvc> ("JobOptionsSvc");
109  if( joptsvc ) {
110  StringProperty p("OutputFile", m_outputFile);
111  if ( m_histPersName == "ROOT" ) {
112  joptsvc->addPropertyToCatalogue("RootHistSvc", p).ignore();
113  } else if (m_histPersName == "HBOOK" ) {
114  joptsvc->addPropertyToCatalogue("HbookHistSvc", p).ignore();
115  }
116  }
117  }
118 
119  // Load the Histogram persistency service that's required as default
120  setConversionSvc(nullptr).ignore();
121  if ( m_histPersName == "ROOT" ) {
122  setConversionSvc(service("RootHistSvc")).ignore();
123  if ( !conversionSvc() ) {
124  return StatusCode::FAILURE;
125  }
126  enable(true);
127  }
128  else if ( m_histPersName == "HBOOK" ) {
129  setConversionSvc(service("HbookHistSvc")).ignore();
130  if ( !conversionSvc() ) {
131  return StatusCode::FAILURE;
132  }
133  enable(true);
134  }
135  else if ( m_histPersName == "NONE" ) {
136  enable(false);
137  if ( m_warnings ) {
138  log << MSG::WARNING << "Histograms saving not required." << endmsg;
139  }
140  }
141  else {
143  if ( !conversionSvc() ) {
144  return StatusCode::FAILURE;
145  }
146  enable(true);
147  if ( m_warnings ) {
148  log << MSG::WARNING << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
149  }
150  }
151  return StatusCode::SUCCESS;
152 }
153 // ============================================================================
154 namespace
155 {
156  // ==========================================================================
158  const std::string s_NULL = "<NULL>" ;
159  // ==========================================================================
165  // ==========================================================================
166  inline bool match
167  ( const std::string& name ,
168  const std::string& pat )
169  {
170  // the most primitive match
171  return std::string::npos != name.find ( pat );
172  }
173  // ==========================================================================
178  inline const std::string& oname ( const DataObject* obj )
179  {
180  if ( !obj ) { return s_NULL ; }
181  auto reg = obj->registry() ;
182  return reg ? reg -> identifier() : obj -> name () ;
183  }
184  // ==========================================================================
190  inline bool match ( const DataObject* obj ,
191  const std::string& pat )
192  {
193  return obj && match ( oname ( obj ) , pat ) ;
194  }
195  // ==========================================================================
196 }
197 // ============================================================================
198 // Convert the transient object to the requested representation.
199 // ============================================================================
201 ( DataObject* pObj ,
202  IOpaqueAddress*& refpAddr )
203 {
204  // enable the conversion
205  enable ( true ) ;
206  // conversion is possible ?
207  if ( "NONE" == m_histPersName )
208  {
209  enable ( false ) ;
210  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
211  }
212  // histogram ?
213  if ( dynamic_cast<AIDA::IBaseHistogram*> ( pObj ) )
214  {
215 
216  auto match_pObj = [&](const std::string& s) { return match( pObj , s ); };
217  // Empty ConvertHistos property means convert all
218  bool select = ( m_convert.empty()
219  || std::any_of( m_convert.begin(), m_convert.end(), match_pObj )
220  ) && std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
221  //
222  enable ( select ) ;
223  //
224  const auto& path = oname ( pObj ) ;
225  //
226  if ( !select ) { m_excluded.insert ( path ) ; }
227  else { m_converted.insert ( path ) ; }
228  }
229  //
230  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
231 }
232 // ============================================================================
233 // Standard Constructor
234 // ============================================================================
236 ( const std::string& name ,
237  ISvcLocator* svc )
238  : PersistencySvc(name, svc)
239 {
240  std::vector<std::string> defServices;
241  defServices.push_back("RootHistSvc");
242  m_svcNames.set(defServices);
243  declareProperty ("HistogramPersistency", m_histPersName = "");
244  declareProperty ("OutputFile", m_outputFile = "");
245  //
246  declareProperty
247  ("ConvertHistos" , m_convert ,
248  "The list of patterns to be accepted for conversion" ) ;
249  //
250  declareProperty
251  ("ExcludeHistos" , m_exclude ,
252  "The list of patterns to be excluded for conversion" ) ;
253  declareProperty("Warnings",m_warnings=true,
254  "Set this property to false to suppress warning messages");
255 }
256 
257 // ============================================================================
258 // The END
259 // ============================================================================
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
StatusCode initialize() override
Initialize the service.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
list path
Definition: __init__.py:15
StatusCode setConversionSvc(IConversionSvc *svc) override
Set conversion service the converter is connected to.
StatusCode reinitialize() override
Reinitialize the service.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:74
bool m_warnings
Flag to disable warning messages when using external input.
bool enable(bool value)
Set enabled flag.
Main interface for the JobOptions service.
StatusCode initialize() override
Initialize the service.
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
StatusCode finalize() override
stop the service.
bool PyHelper() setProperty(IInterface *p, char *name, char *value)
Definition: Bootstrap.cpp:255
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.
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
SmartIF< IConversionSvc > & conversionSvc() const override
Get conversion service the converter is connected to.
string s
Definition: gaudirun.py:246
tuple item
print s1,s2
Definition: ana.py:146
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:108
The IProperty is the basic interface for all components which have properties that can be set or get...
Definition: IProperty.h:21
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
std::string m_outputFile
Name of the outputFile.
def select(list, sel)
Definition: AnalysisInit.py:29
std::string m_histPersName
Name of the Hist Pers type.