| # Gaudi Framework, version v20r4
Generated: 8 Jan 2009

# 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( messageService(), 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" << endreq;
    00044     return StatusCode::FAILURE;
    00045   }
    00046   // Initialize ROOT if output file name is defined
    00047   if( undefFileName != m_defFileName ) {
    00048     m_hfile = TFile::Open(m_defFileName.c_str(),"RECREATE","GAUDI Histograms");
    00049   } else {
    00050     m_hfile = 0;
    00051   }
    00052   log << MSG::INFO << "Writing ROOT histograms to: " << m_defFileName
    00053       << endreq;
    00054
    00055   return StatusCode(StatusCode::SUCCESS,true);
    00056 }
    00057
    00058
    00059 //-----------------------------------------------------------------------------
    00060 StatusCode RootHistCnv::PersSvc::finalize()
    00061 //-----------------------------------------------------------------------------
    00062 {
    00063   // Close ROOT only if the output file name is defined
    00064   MsgStream log( messageService(), name() );
    00065   log << MSG::DEBUG << "RootHistCnv::PersSvc::finalize()" << endreq;
    00066   if( undefFileName != m_defFileName ) {
    00067     m_hfile->Write("",TObject::kOverwrite);
    00068     m_hfile->Close();
    00069   }
    00070   return ConversionSvc::finalize();
    00071 }
    00072
    00073 //-----------------------------------------------------------------------------
    00074 StatusCode RootHistCnv::PersSvc::createRep(DataObject* pObject,
    00075                                            IOpaqueAddress*& refpAddress)
    00076 //-----------------------------------------------------------------------------
    00077 {
    00078   // There are objects in the HDS to be stored
    00079   if( undefFileName != m_defFileName )  {
    00080     SmartDataPtr<DataObject> top(dataProvider(), stat_dir);
    00081     if ( 0 != top )    {
    00082       IRegistry* pReg = top->registry();
    00083       if ( pReg )   {
    00084         if ( top.ptr() == pObject )   {
    00085           TDirectory* pDir = m_hfile;
    00086           refpAddress = new RootObjAddress( repSvcType(),
    00087                                             CLID_DataObject,
    00088                                             stat_dir,
    00089                                             m_defFileName,
    00090                                             long(pDir),
    00091                                             long(0));
    00092           return StatusCode::SUCCESS;
    00093         }
    00094         else    {
    00095           StatusCode sc = ConversionSvc::createRep(pObject, refpAddress);
    00096           if( sc.isFailure() )   {
    00097             MsgStream log( messageService(), name() );
    00098             log << MSG::ERROR
    00099                 << "Error while creating persistent Histogram:"
    00100                 << pReg->identifier()
    00101                 << endreq;
    00102           }
    00103           return sc;
    00104         }
    00105       }
    00106     }
    00107     MsgStream err( messageService(), name() );
    00108     err << MSG::ERROR
    00109         << "Internal error while creating Histogram persistent representations"
    00110         << endreq;
    00111     return StatusCode::FAILURE;
    00112   } else {
    00113     if (!m_prtWar) {
    00114       m_prtWar = true;
    00115       MsgStream log( messageService(), name() );
    00116       log << MSG::WARNING
    00117           << "no ROOT output file name, "
    00118           << "Histograms cannot be persistified" << endreq;
    00119     }
    00120   }
    00121   return StatusCode::SUCCESS;
    00122 }
    00123
    00124 //-----------------------------------------------------------------------------
    00125 RootHistCnv::PersSvc::PersSvc(const std::string& name, ISvcLocator* svc)
    00126 //-----------------------------------------------------------------------------
    00127 : ConversionSvc(name, svc, ROOT_StorageType), m_prtWar(false) {
    00128   declareProperty("OutputFile", m_defFileName = undefFileName);
    00129   declareProperty("ForceAlphaIds", m_alphaIds = false);
    00130 }
    00131
    00132 //-----------------------------------------------------------------------------
    00133 RootHistCnv::PersSvc::~PersSvc()
    00134 //-----------------------------------------------------------------------------
    00135 {
    00136   if ( m_hfile != 0 ) {
    00137     delete m_hfile;
    00138     m_hfile = 0;
    00139   }
    00140 }