Gaudi Framework, version v21r9

Home   Generated: 3 May 2010

HistogramPersistencySvc.cpp

Go to the documentation of this file.
00001 // $Id: HistogramPersistencySvc.cpp,v 1.9 2008/10/09 13:40:18 marcocle Exp $
00002 // ============================================================================
00003 //      HistogramPersistencySvc.cpp
00004 //--------------------------------------------------------------------
00005 //
00006 //      Package    : System ( The LHCb Offline System)
00007 //
00008 //  Description: implementation of the Event data persistency service
00009 //               This specialized service only deals with event related
00010 //               data
00011 //
00012 //      Author     : M.Frank
00013 //  History    :
00014 // +---------+----------------------------------------------+---------
00015 // |    Date |                 Comment                      | Who
00016 // +---------+----------------------------------------------+---------
00017 // | 29/10/98| Initial version                              | MF
00018 // +---------+----------------------------------------------+---------
00019 //
00020 //====================================================================
00021 #define  PERSISTENCYSVC_HISTOGRAMPERSISTENCYSVC_CPP
00022 // ============================================================================
00023 // Include files
00024 // ============================================================================
00025 // GaudiKernel
00026 // ============================================================================
00027 #include "GaudiKernel/SmartIF.h"
00028 #include "GaudiKernel/MsgStream.h"
00029 #include "GaudiKernel/SvcFactory.h"
00030 #include "GaudiKernel/ISvcLocator.h"
00031 #include "GaudiKernel/IJobOptionsSvc.h"
00032 #include "GaudiKernel/DataObject.h"
00033 #include "GaudiKernel/IRegistry.h"
00034 #include "GaudiKernel/IDataProviderSvc.h"
00035 // ============================================================================
00036 // local
00037 // ============================================================================
00038 #include "HistogramPersistencySvc.h"
00039 // ============================================================================
00040 // AIDA
00041 // ============================================================================
00043 #define class class GAUDI_API
00044 #include "AIDA/IBaseHistogram.h"
00045 #undef class
00046 // ============================================================================
00047 // Instantiation of a static factory class used by clients to create
00048 // instances of this service
00049 DECLARE_SERVICE_FACTORY(HistogramPersistencySvc)
00050 
00051 // ============================================================================
00052 // Finalize the service.
00053 StatusCode HistogramPersistencySvc::finalize()
00054 {
00055   //
00056   MsgStream log ( msgSvc() , name() );
00057   if ( !(m_convert.empty() && m_exclude.empty()) )
00058   { // print message if any of the two properties is used
00059     log << MSG::INFO  << "Histograms Converted/Excluded: "
00060         << m_converted.size() << "/" << m_excluded.size() << endmsg ;
00061   }
00062   //
00063   if ( !m_excluded.empty() )
00064   {
00065     log << MSG::DEBUG << "Excluded  Histos : #" << m_excluded.size() ;
00066     for ( Set::const_iterator item = m_excluded.begin() ;
00067           m_excluded.end() != item ; ++item )
00068     { log << std::endl << "  '" << (*item) << "'" ; }
00069     log << endmsg ;
00070   }
00071   //
00072   if ( !m_converted.empty() )
00073   {
00074     log << MSG::DEBUG << "Converted Histos : #" << m_converted.size() ;
00075     for ( Set::const_iterator item = m_converted.begin() ;
00076           m_converted.end() != item ; ++item )
00077     { log << std::endl << "  '" << (*item) << "'" ; }
00078     log << endmsg ;
00079   }
00080   //
00081   StatusCode status = PersistencySvc::finalize();
00082   return status;
00083 }
00084 // ============================================================================
00085 // Initialize the service.
00086 // ============================================================================
00087 StatusCode HistogramPersistencySvc::initialize()     {
00088   StatusCode status = PersistencySvc::initialize();
00089   if ( status.isSuccess() )   {
00090     status = reinitialize();
00091   }
00092   return status;
00093 }
00094 // ============================================================================
00095 // Reinitialize the service.
00096 // ============================================================================
00097 StatusCode HistogramPersistencySvc::reinitialize()
00098 {
00099   MsgStream log(msgSvc(), name());
00100   // Obtain the IProperty of the ApplicationMgr
00101   SmartIF<IProperty> prpMgr(serviceLocator());
00102   if ( !prpMgr.isValid() )   {
00103     log << MSG::FATAL << "IProperty interface not found in ApplicationMgr." << endmsg;
00104     return StatusCode::FAILURE;
00105   }
00106   else {
00107     setProperty(prpMgr->getProperty("HistogramPersistency")).ignore();
00108   }
00109 
00110   // To keep backward compatibility, we set the property of conversion service
00111   // into JobOptions catalogue
00112   if( m_outputFile != "" ) {
00113     SmartIF<IJobOptionsSvc> joptsvc(serviceLocator()->service("JobOptionsSvc"));
00114     if( joptsvc.isValid() ) {
00115       StringProperty p("OutputFile", m_outputFile);
00116       if ( m_histPersName == "ROOT" ) {
00117         joptsvc->addPropertyToCatalogue("RootHistSvc", p).ignore();
00118       } else if (m_histPersName == "HBOOK" ) {
00119         joptsvc->addPropertyToCatalogue("HbookHistSvc", p).ignore();
00120       }
00121     }
00122   }
00123 
00124   // Load the Histogram persistency service that's required as default
00125   setConversionSvc(0).ignore();
00126   if ( m_histPersName == "ROOT" ) {
00127     setConversionSvc(service("RootHistSvc")).ignore();
00128     if ( !conversionSvc() ) {
00129       return StatusCode::FAILURE;
00130     }
00131     enable(true);
00132   }
00133   else if ( m_histPersName == "HBOOK" ) {
00134     setConversionSvc(service("HbookHistSvc")).ignore();
00135     if ( !conversionSvc() ) {
00136       return StatusCode::FAILURE;
00137     }
00138     enable(true);
00139   }
00140   else if ( m_histPersName == "NONE" ) {
00141     enable(false);
00142     if ( m_warnings ) {
00143       log << MSG::WARNING << "Histograms saving not required." << endmsg;
00144     }
00145   }
00146   else {
00147     setConversionSvc(service(m_histPersName)).ignore();
00148     if ( !conversionSvc() ) {
00149       return StatusCode::FAILURE;
00150     }
00151     enable(true);
00152     if ( m_warnings ) {
00153       log << MSG::WARNING << "Unknown Histogram Persistency Mechanism " << m_histPersName << endmsg;
00154     }
00155   }
00156   return StatusCode::SUCCESS;
00157 }
00158 // ============================================================================
00159 namespace
00160 {
00161   // ==========================================================================
00163   const std::string s_NULL = "<NULL>" ;
00164   // ==========================================================================
00170   // ==========================================================================
00171   inline bool match
00172   ( const std::string& name ,
00173     const std::string& pat  )
00174   {
00175     // the most primitive match
00176     return std::string::npos != name.find ( pat );
00177   }
00178   // ==========================================================================
00183   inline const std::string& oname ( const DataObject* obj )
00184   {
00185     if ( 0 == obj ) { return s_NULL ; }
00186     const IRegistry* reg = obj->registry() ;
00187     return ( 0 == reg ) ? obj -> name () : reg -> identifier () ;
00188   }
00189   // ==========================================================================
00195   inline bool match ( const DataObject*  obj ,
00196                       const std::string& pat )
00197   {
00198     if ( 0 == obj ) { return false ; }
00199     return match ( oname ( obj ) , pat ) ;
00200   }
00201   // ==========================================================================
00202 }
00203 // ============================================================================
00204 // Convert the transient object to the requested representation.
00205 // ============================================================================
00206 StatusCode HistogramPersistencySvc::createRep
00207 ( DataObject*      pObj     ,
00208   IOpaqueAddress*& refpAddr )
00209 {
00210   // enable the conversion
00211   enable ( true ) ;
00212   // conversion is possible ?
00213   if ( "NONE" == m_histPersName )
00214   {
00215     enable ( false ) ;
00216     return PersistencySvc::createRep ( pObj , refpAddr ) ;   // RETURN
00217   }
00218   // histogram ?
00219   if ( 0 != dynamic_cast<AIDA::IBaseHistogram*> ( pObj ) )
00220   {
00221     bool select = false ;
00222     // Empty ConvertHistos property means convert all
00223     if ( m_convert.empty() ) { select = true ; }
00224     else
00225     {
00226       for ( Strings::const_iterator item = m_convert.begin() ;
00227             m_convert.end() != item ; ++item )
00228       { if ( match ( pObj , *item ) ) { select = true ; break ; } }
00229     }
00230     // exclude ?
00231     for ( Strings::const_iterator item = m_exclude.begin() ;
00232           m_exclude.end() != item && select ; ++item )
00233     { if ( match ( pObj , *item ) ) { select = false ; break ; } }
00234     //
00235     enable ( select ) ;
00236     //
00237     const std::string& path = oname ( pObj ) ;
00238     //
00239     if ( !select ) { m_excluded.insert  ( path ) ; }
00240     else           { m_converted.insert ( path ) ; }
00241   }
00242   //
00243   return PersistencySvc::createRep ( pObj , refpAddr ) ;     // RETURN
00244 }
00245 // ============================================================================
00246 // Standard Constructor
00247 // ============================================================================
00248 HistogramPersistencySvc::HistogramPersistencySvc
00249 ( const std::string& name ,
00250   ISvcLocator*       svc  )
00251   :  PersistencySvc(name, svc)
00252   //
00253   , m_convert   ()
00254   , m_exclude   ()
00255   , m_converted ()
00256   , m_excluded  ()
00257   //
00258 {
00259   std::vector<std::string> defServices;
00260   defServices.push_back("HbookHistSvc");
00261   defServices.push_back("RootHistSvc");
00262   m_svcNames.set(defServices);
00263   declareProperty ("HistogramPersistency", m_histPersName = "");
00264   declareProperty ("OutputFile", m_outputFile = "");
00265   //
00266   declareProperty
00267     ("ConvertHistos" , m_convert ,
00268      "The list of patterns to be accepted for conversion" ) ;
00269   //
00270   declareProperty
00271     ("ExcludeHistos" , m_exclude ,
00272      "The list of patterns to be excluded for conversion" ) ;
00273   declareProperty("Warnings",m_warnings=true,
00274                   "Set this property to false to suppress warning messages");
00275 }
00276 // ============================================================================
00277 // Standard Destructor
00278 // ============================================================================
00279 HistogramPersistencySvc::~HistogramPersistencySvc()   {}
00280 // ============================================================================
00281 
00282 
00283 // ============================================================================
00284 // The END
00285 // ============================================================================

Generated at Mon May 3 12:14:52 2010 for Gaudi Framework, version v21r9 by Doxygen version 1.5.6 written by Dimitri van Heesch, © 1997-2004