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 #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  MsgStream log ( msgSvc() , name() );
62  if ( !(m_convert.empty() && m_exclude.empty()) )
63  { // print message if any of the two properties is used
64  log << MSG::INFO << "Histograms Converted/Excluded: "
65  << m_converted.size() << "/" << m_excluded.size() << endmsg ;
66  }
67  if (msgLevel(MSG::DEBUG)) {
68  if ( !m_excluded.empty() )
69  {
70  log << MSG::DEBUG << "Excluded Histos : #" << m_excluded.size() ;
71  for ( const auto& item : m_excluded )
72  { log << std::endl << " '" << item << "'" ; }
73  log << endmsg ;
74  }
75  //
76  if ( !m_converted.empty() )
77  {
78  log << MSG::DEBUG << "Converted Histos : #" << m_converted.size() ;
79  for ( const auto& item : m_converted )
80  { log << std::endl << " '" << item << "'" ; }
81  log << endmsg ;
82  }
83  }
84  return PersistencySvc::finalize();
85 }
86 // ============================================================================
87 // Initialize the service.
88 // ============================================================================
91  if ( status.isSuccess() ) {
92  status = reinitialize();
93  }
94  return status;
95 }
96 // ============================================================================
97 // Reinitialize the service.
98 // ============================================================================
100 {
101  MsgStream log(msgSvc(), name());
102  // Obtain the IProperty of the ApplicationMgr
103  auto prpMgr = serviceLocator()->as<IProperty>();
104  if ( !prpMgr ) {
105  log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
106  return StatusCode::FAILURE;
107  }
108  else {
109  setProperty(prpMgr->getProperty("HistogramPersistency")).ignore();
110  }
111 
112  // To keep backward compatibility, we set the property of conversion service
113  // into JobOptions catalogue
114  if( !m_outputFile.empty() ) {
115  auto joptsvc = serviceLocator()->service<IJobOptionsSvc> ("JobOptionsSvc");
116  if( joptsvc ) {
117  StringProperty p("OutputFile", m_outputFile);
118  if ( m_histPersName == "ROOT" ) {
119  joptsvc->addPropertyToCatalogue("RootHistSvc", p).ignore();
120  } else if (m_histPersName == "HBOOK" ) {
121  joptsvc->addPropertyToCatalogue("HbookHistSvc", p).ignore();
122  }
123  }
124  }
125 
126  // Load the Histogram persistency service that's required as default
127  setConversionSvc(nullptr).ignore();
128  if ( m_histPersName == "ROOT" ) {
129  setConversionSvc(service("RootHistSvc")).ignore();
130  if ( !conversionSvc() ) {
131  return StatusCode::FAILURE;
132  }
133  enable(true);
134  }
135  else if ( m_histPersName == "HBOOK" ) {
136  setConversionSvc(service("HbookHistSvc")).ignore();
137  if ( !conversionSvc() ) {
138  return StatusCode::FAILURE;
139  }
140  enable(true);
141  }
142  else if ( m_histPersName == "NONE" ) {
143  enable(false);
144  if ( m_warnings ) {
145  log << MSG::WARNING << "Histograms saving not required." << endmsg;
146  }
147  }
148  else {
150  if ( !conversionSvc() ) {
151  return StatusCode::FAILURE;
152  }
153  enable(true);
154  if ( m_warnings ) {
155  log << MSG::WARNING << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
156  }
157  }
158  return StatusCode::SUCCESS;
159 }
160 // ============================================================================
161 namespace
162 {
163  // ==========================================================================
165  const std::string s_NULL = "<NULL>" ;
166  // ==========================================================================
172  // ==========================================================================
173  inline bool match
174  ( const std::string& name ,
175  const std::string& pat )
176  {
177  // the most primitive match
178  return std::string::npos != name.find ( pat );
179  }
180  // ==========================================================================
185  inline const std::string& oname ( const DataObject* obj )
186  {
187  if ( !obj ) { return s_NULL ; }
188  auto reg = obj->registry() ;
189  return reg ? reg -> identifier() : obj -> name () ;
190  }
191  // ==========================================================================
197  inline bool match ( const DataObject* obj ,
198  const std::string& pat )
199  {
200  return obj && match ( oname ( obj ) , pat ) ;
201  }
202  // ==========================================================================
203 }
204 // ============================================================================
205 // Convert the transient object to the requested representation.
206 // ============================================================================
208 ( DataObject* pObj ,
209  IOpaqueAddress*& refpAddr )
210 {
211  // enable the conversion
212  enable ( true ) ;
213  // conversion is possible ?
214  if ( "NONE" == m_histPersName )
215  {
216  enable ( false ) ;
217  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
218  }
219  // histogram ?
220  if ( dynamic_cast<AIDA::IBaseHistogram*> ( pObj ) )
221  {
222 
223  auto match_pObj = [&](const std::string& s) { return match( pObj , s ); };
224  // Empty ConvertHistos property means convert all
225  bool select = ( m_convert.empty()
226  || std::any_of( m_convert.begin(), m_convert.end(), match_pObj )
227  ) && std::none_of( m_exclude.begin(), m_exclude.end(), match_pObj );
228  //
229  enable ( select ) ;
230  //
231  const auto& path = oname ( pObj ) ;
232  //
233  if ( !select ) { m_excluded.insert ( path ) ; }
234  else { m_converted.insert ( path ) ; }
235  }
236  //
237  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
238 }
239 // ============================================================================
240 // Standard Constructor
241 // ============================================================================
243 ( const std::string& name ,
244  ISvcLocator* svc )
245  : PersistencySvc(name, svc)
246 {
247  std::vector<std::string> defServices;
248  defServices.push_back("RootHistSvc");
249  m_svcNames.set(defServices);
250  declareProperty ("HistogramPersistency", m_histPersName = "");
251  declareProperty ("OutputFile", m_outputFile = "");
252  //
253  declareProperty
254  ("ConvertHistos" , m_convert ,
255  "The list of patterns to be accepted for conversion" ) ;
256  //
257  declareProperty
258  ("ExcludeHistos" , m_exclude ,
259  "The list of patterns to be excluded for conversion" ) ;
260  declareProperty("Warnings",m_warnings=true,
261  "Set this property to false to suppress warning messages");
262 }
263 
264 // ============================================================================
265 // The END
266 // ============================================================================
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
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode initialize() override
Initialize the service.
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.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
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:254
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:245
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.