All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HistogramPersistencySvc.cpp
Go to the documentation of this file.
1 // $Id: HistogramPersistencySvc.cpp,v 1.9 2008/10/09 13:40:18 marcocle Exp $
2 // ============================================================================
3 // HistogramPersistencySvc.cpp
4 //--------------------------------------------------------------------
5 //
6 // Package : System ( The LHCb Offline System)
7 //
8 // Description: implementation of the Event data persistency service
9 // This specialized service only deals with event related
10 // data
11 //
12 // Author : M.Frank
13 // History :
14 // +---------+----------------------------------------------+---------
15 // | Date | Comment | Who
16 // +---------+----------------------------------------------+---------
17 // | 29/10/98| Initial version | MF
18 // +---------+----------------------------------------------+---------
19 //
20 //====================================================================
21 #define PERSISTENCYSVC_HISTOGRAMPERSISTENCYSVC_CPP
22 // ============================================================================
23 // Include files
24 // ============================================================================
25 // GaudiKernel
26 // ============================================================================
27 #include "GaudiKernel/SmartIF.h"
28 #include "GaudiKernel/MsgStream.h"
31 #include "GaudiKernel/DataObject.h"
32 #include "GaudiKernel/IRegistry.h"
34 // ============================================================================
35 // local
36 // ============================================================================
38 // ============================================================================
39 // AIDA
40 // ============================================================================
42 #define class class GAUDI_API
43 #include "AIDA/IBaseHistogram.h"
44 #undef class
45 // ============================================================================
46 // Instantiation of a static factory class used by clients to create
47 // instances of this service
49 
50 // ============================================================================
51 // Finalize the service.
53 {
54  //
55  MsgStream log ( msgSvc() , name() );
56  if ( !(m_convert.empty() && m_exclude.empty()) )
57  { // print message if any of the two properties is used
58  log << MSG::INFO << "Histograms Converted/Excluded: "
59  << m_converted.size() << "/" << m_excluded.size() << endmsg ;
60  }
61  if (msgLevel(MSG::DEBUG)) {
62  if ( !m_excluded.empty() )
63  {
64  log << MSG::DEBUG << "Excluded Histos : #" << m_excluded.size() ;
65  for ( Set::const_iterator item = m_excluded.begin() ;
66  m_excluded.end() != item ; ++item )
67  { log << std::endl << " '" << (*item) << "'" ; }
68  log << endmsg ;
69  }
70  //
71  if ( !m_converted.empty() )
72  {
73  log << MSG::DEBUG << "Converted Histos : #" << m_converted.size() ;
74  for ( Set::const_iterator item = m_converted.begin() ;
75  m_converted.end() != item ; ++item )
76  { log << std::endl << " '" << (*item) << "'" ; }
77  log << endmsg ;
78  }
79  }
80  return PersistencySvc::finalize();
81 }
82 // ============================================================================
83 // Initialize the service.
84 // ============================================================================
87  if ( status.isSuccess() ) {
88  status = reinitialize();
89  }
90  return status;
91 }
92 // ============================================================================
93 // Reinitialize the service.
94 // ============================================================================
96 {
97  MsgStream log(msgSvc(), name());
98  // Obtain the IProperty of the ApplicationMgr
100  if ( !prpMgr.isValid() ) {
101  log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
102  return StatusCode::FAILURE;
103  }
104  else {
105  setProperty(prpMgr->getProperty("HistogramPersistency")).ignore();
106  }
107 
108  // To keep backward compatibility, we set the property of conversion service
109  // into JobOptions catalogue
110  if( m_outputFile != "" ) {
111  SmartIF<IJobOptionsSvc> joptsvc(serviceLocator()->service("JobOptionsSvc"));
112  if( joptsvc.isValid() ) {
113  StringProperty p("OutputFile", m_outputFile);
114  if ( m_histPersName == "ROOT" ) {
115  joptsvc->addPropertyToCatalogue("RootHistSvc", p).ignore();
116  } else if (m_histPersName == "HBOOK" ) {
117  joptsvc->addPropertyToCatalogue("HbookHistSvc", p).ignore();
118  }
119  }
120  }
121 
122  // Load the Histogram persistency service that's required as default
124  if ( m_histPersName == "ROOT" ) {
125  setConversionSvc(service("RootHistSvc")).ignore();
126  if ( !conversionSvc() ) {
127  return StatusCode::FAILURE;
128  }
129  enable(true);
130  }
131  else if ( m_histPersName == "HBOOK" ) {
132  setConversionSvc(service("HbookHistSvc")).ignore();
133  if ( !conversionSvc() ) {
134  return StatusCode::FAILURE;
135  }
136  enable(true);
137  }
138  else if ( m_histPersName == "NONE" ) {
139  enable(false);
140  if ( m_warnings ) {
141  log << MSG::WARNING << "Histograms saving not required." << endmsg;
142  }
143  }
144  else {
146  if ( !conversionSvc() ) {
147  return StatusCode::FAILURE;
148  }
149  enable(true);
150  if ( m_warnings ) {
151  log << MSG::WARNING << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
152  }
153  }
154  return StatusCode::SUCCESS;
155 }
156 // ============================================================================
157 namespace
158 {
159  // ==========================================================================
161  const std::string s_NULL = "<NULL>" ;
162  // ==========================================================================
168  // ==========================================================================
169  inline bool match
170  ( const std::string& name ,
171  const std::string& pat )
172  {
173  // the most primitive match
174  return std::string::npos != name.find ( pat );
175  }
176  // ==========================================================================
181  inline const std::string& oname ( const DataObject* obj )
182  {
183  if ( 0 == obj ) { return s_NULL ; }
184  const IRegistry* reg = obj->registry() ;
185  return ( 0 == reg ) ? obj -> name () : reg -> identifier () ;
186  }
187  // ==========================================================================
193  inline bool match ( const DataObject* obj ,
194  const std::string& pat )
195  {
196  if ( 0 == obj ) { return false ; }
197  return match ( oname ( obj ) , pat ) ;
198  }
199  // ==========================================================================
200 }
201 // ============================================================================
202 // Convert the transient object to the requested representation.
203 // ============================================================================
205 ( DataObject* pObj ,
206  IOpaqueAddress*& refpAddr )
207 {
208  // enable the conversion
209  enable ( true ) ;
210  // conversion is possible ?
211  if ( "NONE" == m_histPersName )
212  {
213  enable ( false ) ;
214  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
215  }
216  // histogram ?
217  if ( 0 != dynamic_cast<AIDA::IBaseHistogram*> ( pObj ) )
218  {
219  bool select = false ;
220  // Empty ConvertHistos property means convert all
221  if ( m_convert.empty() ) { select = true ; }
222  else
223  {
224  for ( Strings::const_iterator item = m_convert.begin() ;
225  m_convert.end() != item ; ++item )
226  { if ( match ( pObj , *item ) ) { select = true ; break ; } }
227  }
228  // exclude ?
229  for ( Strings::const_iterator item = m_exclude.begin() ;
230  m_exclude.end() != item && select ; ++item )
231  { if ( match ( pObj , *item ) ) { select = false ; break ; } }
232  //
233  enable ( select ) ;
234  //
235  const std::string& path = oname ( pObj ) ;
236  //
237  if ( !select ) { m_excluded.insert ( path ) ; }
238  else { m_converted.insert ( path ) ; }
239  }
240  //
241  return PersistencySvc::createRep ( pObj , refpAddr ) ; // RETURN
242 }
243 // ============================================================================
244 // Standard Constructor
245 // ============================================================================
247 ( const std::string& name ,
248  ISvcLocator* svc )
249  : PersistencySvc(name, svc)
250  //
251  , m_convert ()
252  , m_exclude ()
253  , m_converted ()
254  , m_excluded ()
255  //
256 {
257  std::vector<std::string> defServices;
258  defServices.push_back("RootHistSvc");
259  m_svcNames.set(defServices);
260  declareProperty ("HistogramPersistency", m_histPersName = "");
261  declareProperty ("OutputFile", m_outputFile = "");
262  //
263  declareProperty
264  ("ConvertHistos" , m_convert ,
265  "The list of patterns to be accepted for conversion" ) ;
266  //
267  declareProperty
268  ("ExcludeHistos" , m_exclude ,
269  "The list of patterns to be excluded for conversion" ) ;
270  declareProperty("Warnings",m_warnings=true,
271  "Set this property to false to suppress warning messages");
272 }
273 // ============================================================================
274 // Standard Destructor
275 // ============================================================================
277 // ============================================================================
278 
279 
280 // ============================================================================
281 // The END
282 // ============================================================================
SmartIF< IConversionSvc > & service(const std::string &nam)
Retrieve conversion service by name.
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Implementation of IConverter: Convert the transient object to the requested representation.
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:26
virtual StatusCode setProperty(const Property &p)
Set the property by property.
Definition: Service.cpp:342
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:75
virtual StatusCode initialize()
Initialize the service.
virtual StatusCode setConversionSvc(IConversionSvc *svc)
Set conversion service the converter is connected to.
virtual StatusCode reinitialize()
Reinitialize the service.
virtual StatusCode initialize()
Initialize the service.
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
bool m_warnings
Flag to disable warning messages when using external input.
virtual StatusCode finalize()
stop the service.
bool enable(bool value)
Set enabled flag.
virtual SmartIF< IConversionSvc > & conversionSvc() const
Get conversion service the converter is connected to.
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Implementation of IConverter: Convert the transient object to the requested representation.
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:51
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
PersistencySvc class implementation definition.
HistogramPersistencySvc class implementation definition.
HistogramPersistencySvc(const std::string &name, ISvcLocator *svc)
Standard Constructor.
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
GAUDI_API std::string path(const AIDA::IBaseHistogram *aida)
get the path in THS for AIDA histogram
virtual ~HistogramPersistencySvc()
Standard Destructor.
tuple item
print s1,s2
Definition: ana.py:146
Opaque address interface definition.
void ignore() const
Definition: StatusCode.h:107
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
std::string m_outputFile
Name of the outputFile.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
SmartIF< ISvcLocator > & serviceLocator() const
Retrieve pointer to service locator.
Definition: Service.cpp:336
std::string m_histPersName
Name of the Hist Pers type.