| # Gaudi Framework, version v22r2 |

Home |  | Generated: Tue May 10 2011

# PersSvc.cpp

Go to the documentation of this file.

    00001 // $Id: PersSvc.cpp,v 1.15 2007/03/15 15:53:15 hmd Exp $
    00002
    00003 // Include files
    00004 #include "GaudiKernel/DataObject.h"
    00005 #include "GaudiKernel/SvcFactory.h"
    00006 #include "GaudiKernel/MsgStream.h"
    00007 #include "GaudiKernel/ISvcLocator.h"
    00008 #include "GaudiKernel/IRegistry.h"
    00009 #include "GaudiKernel/SmartDataPtr.h"
    00010 #include "RootObjAddress.h"
    00011 #include "RConverter.h"
    00012 #include "PersSvc.h"
    00013 #include "TFile.h"
    00014
    00015 //-----------------------------------------------------------------------------
    00016 //
    00017 // Implementation of class :  RootHistCnv::PersSvc
    00018 //
    00019 // Author :                   Charles Leggett
    00020 //
    00021 //-----------------------------------------------------------------------------
    00022
    00023 // Instantiation of a static factory class used by clients to create
    00024 // instances of this service
    00025 DECLARE_NAMESPACE_SERVICE_FACTORY(RootHistCnv,PersSvc)
    00026
    00027 namespace RootHistCnv {
    00028   static std::string stat_dir = "/stat";
    00029   static std::string undefFileName = "UndefinedROOTOutputFileName";
    00030 }
    00031
    00032 //-----------------------------------------------------------------------------
    00033 StatusCode RootHistCnv::PersSvc::initialize()
    00034 //-----------------------------------------------------------------------------
    00035 {
    00036   MsgStream log( msgSvc(), name() );
    00037
    00038   StatusCode status = ConversionSvc::initialize();
    00039   if( status.isFailure() ) return status;
    00040
    00041   // Get my properties from the JobOptionsSvc
    00042   if (setProperties().isFailure()) {
    00043     log << MSG::ERROR << "Could not set my properties" << endmsg;
    00044     return StatusCode::FAILURE;
    00045   }
    00046   if (m_outputEnabled) {
    00047     // Initialize ROOT if output file name is defined
    00048     if( undefFileName != m_defFileName ) {
    00049       m_hfile = TFile::Open(m_defFileName.c_str(),"RECREATE","GAUDI Histograms");
    00050     } else {
    00051       m_hfile = 0;
    00052     }
    00053     log << MSG::INFO << "Writing ROOT histograms to: " << m_defFileName
    00054         << endmsg;
    00055   }
    00056   else {
    00057     log << MSG::INFO << "Writing ROOT histograms disabled." << endmsg;
    00058   }
    00059   return StatusCode(StatusCode::SUCCESS,true);
    00060 }
    00061
    00062
    00063 //-----------------------------------------------------------------------------
    00064 StatusCode RootHistCnv::PersSvc::finalize()
    00065 //-----------------------------------------------------------------------------
    00066 {
    00067   // Close ROOT only if the output file name is defined
    00068   MsgStream log( msgSvc(), name() );
    00069   log << MSG::DEBUG << "RootHistCnv::PersSvc::finalize()" << endmsg;
    00070   if( undefFileName != m_defFileName ) {
    00071     m_hfile->Write("",TObject::kOverwrite);
    00072     m_hfile->Close();
    00073   }
    00074   return ConversionSvc::finalize();
    00075 }
    00076
    00077 //-----------------------------------------------------------------------------
    00078 StatusCode RootHistCnv::PersSvc::createRep(DataObject* pObject,
    00079                                            IOpaqueAddress*& refpAddress)
    00080 //-----------------------------------------------------------------------------
    00081 {
    00082   // There are objects in the HDS to be stored
    00083   if( m_outputEnabled && undefFileName != m_defFileName )  {
    00084     SmartDataPtr<DataObject> top(dataProvider(), stat_dir);
    00085     if ( 0 != top )    {
    00086       IRegistry* pReg = top->registry();
    00087       if ( pReg )   {
    00088         if ( top.ptr() == pObject )   {
    00089           TDirectory* pDir = m_hfile;
    00090           refpAddress = new RootObjAddress( repSvcType(),
    00091                                             CLID_DataObject,
    00092                                             stat_dir,
    00093                                             m_defFileName,
    00094                                             long(pDir),
    00095                                             long(0));
    00096           return StatusCode::SUCCESS;
    00097         }
    00098         else    {
    00099           StatusCode sc = ConversionSvc::createRep(pObject, refpAddress);
    00100           if( sc.isFailure() )   {
    00101             MsgStream log( msgSvc(), name() );
    00102             log << MSG::ERROR
    00103                 << "Error while creating persistent Histogram:"
    00104                 << pReg->identifier()
    00105                 << endmsg;
    00106           }
    00107           return sc;
    00108         }
    00109       }
    00110     }
    00111     MsgStream err( msgSvc(), name() );
    00112     err << MSG::ERROR
    00113         << "Internal error while creating Histogram persistent representations"
    00114         << endmsg;
    00115     return StatusCode::FAILURE;
    00116   } else {
    00117     if (m_outputEnabled && !m_prtWar) {
    00118       m_prtWar = true;
    00119       MsgStream log( msgSvc(), name() );
    00120       log << MSG::WARNING
    00121           << "no ROOT output file name, "
    00122           << "Histograms cannot be persistified" << endmsg;
    00123     }
    00124   }
    00125   return StatusCode::SUCCESS;
    00126 }
    00127
    00128 //-----------------------------------------------------------------------------
    00129 RootHistCnv::PersSvc::PersSvc(const std::string& name, ISvcLocator* svc)
    00130 //-----------------------------------------------------------------------------
    00131 : ConversionSvc(name, svc, ROOT_StorageType), m_hfile(0), m_prtWar(false) {
    00132   declareProperty("OutputFile", m_defFileName = undefFileName);
    00133   declareProperty("ForceAlphaIds", m_alphaIds = false);
    00134   declareProperty("OutputEnabled", m_outputEnabled = true,
    00135                   "Flag to enable/disable the output to file.");
    00136 }
    00137
    00138 //-----------------------------------------------------------------------------
    00139 RootHistCnv::PersSvc::~PersSvc()
    00140 //-----------------------------------------------------------------------------
    00141 {
    00142   if ( m_hfile != 0 ) {
    00143     delete m_hfile;
    00144     m_hfile = 0;
    00145   }
    00146 }

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines