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