The Gaudi Framework  v36r1 (3e2fb5a8)
PersSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 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 "GaudiKernel/DataObject.h"
13 #include "GaudiKernel/IRegistry.h"
15 #include "GaudiKernel/MsgStream.h"
17 #include "RConverter.h"
18 #include "RootObjAddress.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  } else {
54  m_hfile.reset();
55  }
56  info() << "Writing ROOT histograms to: " << m_defFileName.value() << endmsg;
57  } else {
58  info() << "Writing ROOT histograms disabled." << endmsg;
59  }
60  return StatusCode::SUCCESS;
61 }
62 
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 {
67  // Close ROOT only if the output file name is defined
68  debug() << "RootHistCnv::PersSvc::finalize()" << endmsg;
69  if ( m_outputEnabled ) {
70  if ( m_hfile.get() && undefFileName != m_defFileName ) {
71  m_hfile->Write( "", TObject::kOverwrite );
72  m_hfile->Close();
73  }
74  }
75  return ConversionSvc::finalize();
76 }
77 
78 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 {
82  // There are objects in the HDS to be stored
83  if ( m_outputEnabled && undefFileName != m_defFileName ) {
84  SmartDataPtr<DataObject> top( dataProvider(), stat_dir );
85  if ( top ) {
86  IRegistry* pReg = top->registry();
87  if ( pReg ) {
88  if ( top.ptr() == pObject ) {
89  TDirectory* pDir = m_hfile.get();
90  refpAddress = new RootObjAddress( repSvcType(), CLID_DataObject, stat_dir, m_defFileName.value(),
91  long( pDir ), long( 0 ) );
92  return StatusCode::SUCCESS;
93  } else {
94  StatusCode sc = ConversionSvc::createRep( pObject, refpAddress );
95  if ( sc.isFailure() ) {
96  error() << "Error while creating persistent Histogram:" << pReg->identifier() << endmsg;
97  }
98  return sc;
99  }
100  }
101  }
102  error() << "Internal error while creating Histogram persistent representations" << endmsg;
103  return StatusCode::FAILURE;
104  } else {
105  if ( m_outputEnabled && !m_prtWar ) {
106  m_prtWar = true;
107  warning() << "no ROOT output file name, "
108  << "Histograms cannot be persistified" << endmsg;
109  }
110  }
111  return StatusCode::SUCCESS;
112 }
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:79
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:64
SmartDataPtr.h
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
RootObjAddress.h
IRegistry.h
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConversionSvc::initialize
StatusCode initialize() override
Initialize the service.
Definition: ConversionSvc.cpp:236
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:239
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:40
ConversionSvc::createRep
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
Definition: ConversionSvc.cpp:119
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
ISvcLocator.h
AlgSequencer.top
top
Definition: AlgSequencer.py:31
RootHistCnv::PersSvc::m_defFileName
Gaudi::Property< std::string > m_defFileName
Definition: PersSvc.h:44
MsgStream.h