| The Gaudi Framework v32r1 (f65d50dc)

PersSvc.cpp
Go to the documentation of this file.
1 // Include files
2 #include "GaudiKernel/DataObject.h"
3 #include "GaudiKernel/IRegistry.h"
4 #include "GaudiKernel/ISvcLocator.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/SmartDataPtr.h"
7 #include "RConverter.h"
8 #include "RootObjAddress.h"
9 #include "TFile.h"
10
11 namespace RootHistCnv {
12 static std::string stat_dir = "/stat";
13 static std::string undefFileName = "UndefinedROOTOutputFileName";
14 } // namespace RootHistCnv
15
16 #include "PersSvc.h"
17
18 //-----------------------------------------------------------------------------
19 //
20 // Implementation of class : RootHistCnv::PersSvc
21 //
22 // Author : Charles Leggett
23 //
24 //-----------------------------------------------------------------------------
25
26 // Instantiation of a static factory class used by clients to create
27 // instances of this service
28 using RootHistCnv::PersSvc;
29 DECLARE_COMPONENT( PersSvc )
30
31 //-----------------------------------------------------------------------------
32 StatusCode RootHistCnv::PersSvc::initialize()
33 //-----------------------------------------------------------------------------
34 {
35
36 StatusCode status = ConversionSvc::initialize();
37 if ( status.isFailure() ) return status;
38
39 // Get my properties from the JobOptionsSvc
40 if ( setProperties().isFailure() ) {
41 error() << "Could not set my properties" << endmsg;
42 return StatusCode::FAILURE;
43 }
44 if ( m_outputEnabled ) {
45 // Initialize ROOT if output file name is defined
46 if ( undefFileName != m_defFileName ) {
47 m_hfile.reset( TFile::Open( m_defFileName.value().c_str(), "RECREATE", "GAUDI Histograms" ) );
48 } else {
49 m_hfile.reset();
50 }
51 info() << "Writing ROOT histograms to: " << m_defFileName.value() << endmsg;
52 } else {
53 info() << "Writing ROOT histograms disabled." << endmsg;
54 }
55 return StatusCode( StatusCode::SUCCESS, true );
56 }
57
58 //-----------------------------------------------------------------------------
59 StatusCode RootHistCnv::PersSvc::finalize()
60 //-----------------------------------------------------------------------------
61 {
62 // Close ROOT only if the output file name is defined
63 debug() << "RootHistCnv::PersSvc::finalize()" << endmsg;
64 if ( m_outputEnabled ) {
65 if ( undefFileName != m_defFileName ) {
66 m_hfile->Write( "", TObject::kOverwrite );
67 m_hfile->Close();
68 }
69 }
70 return ConversionSvc::finalize();
71 }
72
73 //-----------------------------------------------------------------------------
74 StatusCode RootHistCnv::PersSvc::createRep( DataObject* pObject, IOpaqueAddress*& refpAddress )
75 //-----------------------------------------------------------------------------
76 {
77 // There are objects in the HDS to be stored
78 if ( m_outputEnabled && undefFileName != m_defFileName ) {
79 SmartDataPtr<DataObject> top( dataProvider(), stat_dir );
80 if ( top ) {
81 IRegistry* pReg = top->registry();
82 if ( pReg ) {
83 if ( top.ptr() == pObject ) {
84 TDirectory* pDir = m_hfile.get();
85 refpAddress = new RootObjAddress( repSvcType(), CLID_DataObject, stat_dir, m_defFileName.value(),
86 long( pDir ), long( 0 ) );
87 return StatusCode::SUCCESS;
88 } else {
89 StatusCode sc = ConversionSvc::createRep( pObject, refpAddress );
90 if ( sc.isFailure() ) {
91 error() << "Error while creating persistent Histogram:" << pReg->identifier() << endmsg;
92 }
93 return sc;
94 }
95 }
96 }
97 error() << "Internal error while creating Histogram persistent representations" << endmsg;
98 return StatusCode::FAILURE;
99 } else {
100 if ( m_outputEnabled && !m_prtWar ) {
101 m_prtWar = true;
102 warning() << "no ROOT output file name, "
103 << "Histograms cannot be persistified" << endmsg;
104 }
105 }
106 return StatusCode::SUCCESS;
107 }
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:72
RootHistCnv::PersSvc::finalize
StatusCode finalize() override
Finalise the service.
Definition: PersSvc.cpp:59
MsgStream.h
ConversionSvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: ConversionSvc.cpp:109
PersSvc.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:11
RConverter.h
RootHistCnv::PersSvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert a collection of transient data objects into another representation.
Definition: PersSvc.cpp:74
std::string
STL class.
SmartDataStorePtr< TYPE, SmartDataObjectPtr::ObjectLoader >::ptr
TYPE * ptr()
Automatic conversion to data type.
Definition: SmartDataStorePtr.h:84
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
SmartDataPtr.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
RootHistCnv::PersSvc::m_outputEnabled
Gaudi::Property< bool > m_outputEnabled
Definition: PersSvc.h:36
RootHistCnv
Definition: DirectoryCnv.h:17
IRegistry.h
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:107
RootObjAddress.h
SmartDataPtr
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:47
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
RootHistCnv::PersSvc
Persistency service - to store histograms in ROOT format.
Definition: PersSvc.h:18
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
RootHistCnv::PersSvc::m_hfile
std::unique_ptr< TFile > m_hfile
Pointer to the ROOT file.
Definition: PersSvc.h:38
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
ConversionSvc::finalize
StatusCode finalize() override
stop the service.
Definition: ConversionSvc.cpp:229
ISvcLocator.h
ConversionSvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: ConversionSvc.cpp:226
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
RootHistCnv::PersSvc::m_defFileName
Gaudi::Property< std::string > m_defFileName
Definition: PersSvc.h:34
DataObject.h