The Gaudi Framework  v33r1 (b1225454)
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  // Get my properties from the JobOptionsSvc
50  if ( setProperties().isFailure() ) {
51  error() << "Could not set my properties" << endmsg;
52  return StatusCode::FAILURE;
53  }
54  if ( m_outputEnabled ) {
55  // Initialize ROOT if output file name is defined
56  if ( undefFileName != m_defFileName ) {
57  m_hfile.reset( TFile::Open( m_defFileName.value().c_str(), "RECREATE", "GAUDI Histograms" ) );
58  } else {
59  m_hfile.reset();
60  }
61  info() << "Writing ROOT histograms to: " << m_defFileName.value() << endmsg;
62  } else {
63  info() << "Writing ROOT histograms disabled." << endmsg;
64  }
65  return StatusCode( StatusCode::SUCCESS, true );
66 }
67 
68 //-----------------------------------------------------------------------------
70 //-----------------------------------------------------------------------------
71 {
72  // Close ROOT only if the output file name is defined
73  debug() << "RootHistCnv::PersSvc::finalize()" << endmsg;
74  if ( m_outputEnabled ) {
75  if ( m_hfile.get() && undefFileName != m_defFileName ) {
76  m_hfile->Write( "", TObject::kOverwrite );
77  m_hfile->Close();
78  }
79  }
80  return ConversionSvc::finalize();
81 }
82 
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 {
87  // There are objects in the HDS to be stored
88  if ( m_outputEnabled && undefFileName != m_defFileName ) {
89  SmartDataPtr<DataObject> top( dataProvider(), stat_dir );
90  if ( top ) {
91  IRegistry* pReg = top->registry();
92  if ( pReg ) {
93  if ( top.ptr() == pObject ) {
94  TDirectory* pDir = m_hfile.get();
95  refpAddress = new RootObjAddress( repSvcType(), CLID_DataObject, stat_dir, m_defFileName.value(),
96  long( pDir ), long( 0 ) );
97  return StatusCode::SUCCESS;
98  } else {
99  StatusCode sc = ConversionSvc::createRep( pObject, refpAddress );
100  if ( sc.isFailure() ) {
101  error() << "Error while creating persistent Histogram:" << pReg->identifier() << endmsg;
102  }
103  return sc;
104  }
105  }
106  }
107  error() << "Internal error while creating Histogram persistent representations" << endmsg;
108  return StatusCode::FAILURE;
109  } else {
110  if ( m_outputEnabled && !m_prtWar ) {
111  m_prtWar = true;
112  warning() << "no ROOT output file name, "
113  << "Histograms cannot be persistified" << endmsg;
114  }
115  }
116  return StatusCode::SUCCESS;
117 }
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:82
StatusCode finalize() override
Finalise the service.
Definition: PersSvc.cpp:69
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Implementation of IConverter: Convert the transient object to the requested representation.
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress) override
Convert a collection of transient data objects into another representation.
Definition: PersSvc.cpp:84
STL class.
TYPE * ptr()
Automatic conversion to data type.
#define DECLARE_COMPONENT(type)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Gaudi::Property< bool > m_outputEnabled
Definition: PersSvc.h:46
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:32
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
T get(T... args)
A small class used to access easily (and efficiently) data items residing in data stores.
Definition: SmartDataPtr.h:57
constexpr static const auto FAILURE
Definition: StatusCode.h:101
Persistency service - to store histograms in ROOT format.
Definition: PersSvc.h:28
virtual const id_type & identifier() const =0
Full identifier (or key)
bool isFailure() const
Definition: StatusCode.h:145
Opaque address interface definition.
std::unique_ptr< TFile > m_hfile
Pointer to the ROOT file.
Definition: PersSvc.h:48
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:40
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:202
Gaudi::Property< std::string > m_defFileName
Definition: PersSvc.h:44