The Gaudi Framework  master (82fdf313)
Loading...
Searching...
No Matches
PersSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11// Include files
12#include "RConverter.h"
13#include "RootObjAddress.h"
19#include <TFile.h>
20
21namespace RootHistCnv {
22 static std::string stat_dir = "/stat";
23 static std::string undefFileName = "UndefinedROOTOutputFileName";
24} // namespace RootHistCnv
25
26#include "PersSvc.h"
27
28//-----------------------------------------------------------------------------
29//
30// Implementation of class : RootHistCnv::PersSvc
31//
32// Author : Charles Leggett
33//
34//-----------------------------------------------------------------------------
35
36// Instantiation of a static factory class used by clients to create
37// instances of this service
40
41//-----------------------------------------------------------------------------
43//-----------------------------------------------------------------------------
44{
45
47 if ( status.isFailure() ) return status;
48
49 if ( m_outputEnabled ) {
50 // Initialize ROOT if output file name is defined
51 if ( undefFileName != m_defFileName ) {
52 m_hfile.reset( TFile::Open( m_defFileName.value().c_str(), "RECREATE", "GAUDI Histograms" ) );
53 info() << "Writing ROOT histograms to: " << m_defFileName.value() << endmsg;
54 } else {
55 m_hfile.reset();
56 // assume using another output system (Gaudi::Histograming::Sink::Base) to write hists
57 // if default file name still set
58 }
59 } else {
60 info() << "Writing ROOT histograms disabled." << endmsg;
61 }
63}
64
65//-----------------------------------------------------------------------------
67//-----------------------------------------------------------------------------
68{
69 // Close ROOT only if the output file name is defined
70 debug() << "RootHistCnv::PersSvc::finalize()" << endmsg;
71 if ( m_outputEnabled ) {
72 if ( m_hfile.get() && undefFileName != m_defFileName ) {
73 m_hfile->Write( "", TObject::kOverwrite );
74 m_hfile->Close();
75 }
76 }
78}
79
80//-----------------------------------------------------------------------------
82//-----------------------------------------------------------------------------
83{
84 // There are objects in the HDS to be stored
85 if ( m_outputEnabled && undefFileName != m_defFileName ) {
86 SmartDataPtr<DataObject> top( dataProvider(), stat_dir );
87 if ( top ) {
88 IRegistry* pReg = top->registry();
89 if ( pReg ) {
90 if ( top.ptr() == pObject ) {
91 TDirectory* pDir = m_hfile.get();
92 refpAddress = new RootObjAddress( repSvcType(), CLID_DataObject, stat_dir, m_defFileName.value(),
93 long( pDir ), long( 0 ) );
95 } else {
96 StatusCode sc = ConversionSvc::createRep( pObject, refpAddress );
97 if ( sc.isFailure() ) {
98 error() << "Error while creating persistent Histogram:" << pReg->identifier() << endmsg;
99 }
100 return sc;
101 }
102 }
103 }
104 error() << "Internal error while creating Histogram persistent representations" << endmsg;
105 return StatusCode::FAILURE;
106 } else {
107 if ( m_outputEnabled && !m_prtWar ) {
108 m_prtWar = true;
109 // if undefined this may be due to using RootHistSink and not a warning
110 if ( undefFileName != m_defFileName ) {
111 warning() << "no ROOT output file name, "
112 << "Histograms cannot be persistified" << endmsg;
113 }
114 }
115 }
116 return StatusCode::SUCCESS;
117}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
long repSvcType() const override
Retrieve the class type of the data store the converter uses.
StatusCode finalize() override
stop the service.
StatusCode initialize() override
Initialize the service.
SmartIF< IDataProviderSvc > & dataProvider() const override
Implementation of IConverter: Get Data provider service.
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
A DataObject is the base class of any identifiable object on any data store.
Definition DataObject.h:37
Opaque address interface definition.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition IRegistry.h:29
virtual const id_type & identifier() const =0
Full identifier (or key)
Persistency service - to store histograms in ROOT format.
PersSvc(const std::string &name, ISvcLocator *svc)
Standard constructor.
Definition PersSvc.h:39
Gaudi::Property< bool > m_outputEnabled
Definition PersSvc.h:45
bool m_prtWar
Already printed a Warning.
Definition PersSvc.h:48
StatusCode finalize() override
Finalise the service.
Definition PersSvc.cpp:66
StatusCode initialize() override
Initialise the service.
Definition PersSvc.cpp:42
Gaudi::Property< std::string > m_defFileName
Definition PersSvc.h:43
std::unique_ptr< TFile > m_hfile
Pointer to the ROOT file.
Definition PersSvc.h:47
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert a collection of transient data objects into another representation.
Definition PersSvc.cpp:81
A small class used to access easily (and efficiently) data items residing in data stores.
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
bool isFailure() const
Definition StatusCode.h:129
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100