The Gaudi Framework  master (37c0b60a)
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"
14 #include <GaudiKernel/DataObject.h>
15 #include <GaudiKernel/IRegistry.h>
17 #include <GaudiKernel/MsgStream.h>
19 #include <TFile.h>
20 
21 namespace 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  }
62  return StatusCode::SUCCESS;
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  }
77  return ConversionSvc::finalize();
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 ) );
94  return StatusCode::SUCCESS;
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 }
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:81
std::string
STL class.
PersSvc.h
IOpaqueAddress
Definition: IOpaqueAddress.h:33
RootHistCnv::PersSvc::m_hfile
std::unique_ptr< TFile > m_hfile
Pointer to the ROOT file.
Definition: PersSvc.h:48
std::unique_ptr::get
T get(T... args)
IRegistry
Definition: IRegistry.h:32
RootHistCnv::RootObjAddress
Definition: RootObjAddress.h:21
RootHistCnv
Definition: DirectoryCnv.h:27
StatusCode
Definition: StatusCode.h:65
RootHistCnv::PersSvc::m_outputEnabled
Gaudi::Property< bool > m_outputEnabled
Definition: PersSvc.h:46
RootHistCnv::PersSvc::finalize
StatusCode finalize() override
Finalise the service.
Definition: PersSvc.cpp:66
SmartDataPtr.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
RootObjAddress.h
IRegistry.h
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConversionSvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: ConversionSvc.cpp:237
DataObject.h
SmartDataPtr
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
RootHistCnv::PersSvc
Definition: PersSvc.h:28
ConversionSvc::finalize
StatusCode finalize() override
stop the service.
Definition: ConversionSvc.cpp:240
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
IRegistry::identifier
virtual const id_type & identifier() const =0
Full identifier (or key)
DataObject
Definition: DataObject.h:36
ConversionSvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: ConversionSvc.cpp:120
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
AlgSequencer.top
top
Definition: AlgSequencer.py:37
RootHistCnv::PersSvc::m_defFileName
Gaudi::Property< std::string > m_defFileName
Definition: PersSvc.h:44
MsgStream.h