All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PersSvc.cpp
Go to the documentation of this file.
1 // Include files
7 #include "RootObjAddress.h"
8 #include "RConverter.h"
9 #include "PersSvc.h"
10 #include "TFile.h"
11 
12 //-----------------------------------------------------------------------------
13 //
14 // Implementation of class : RootHistCnv::PersSvc
15 //
16 // Author : Charles Leggett
17 //
18 //-----------------------------------------------------------------------------
19 
20 // Instantiation of a static factory class used by clients to create
21 // instances of this service
24 
25 namespace RootHistCnv {
26  static std::string stat_dir = "/stat";
27  static std::string undefFileName = "UndefinedROOTOutputFileName";
28 }
29 
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
33 {
34  MsgStream log( msgSvc(), name() );
35 
37  if( status.isFailure() ) return status;
38 
39  // Get my properties from the JobOptionsSvc
40  if (setProperties().isFailure()) {
41  log << MSG::ERROR << "Could not set my properties" << endmsg;
42  return StatusCode::FAILURE;
43  }
44  if (m_outputEnabled) {
45  // Initialize ROOT if output file name is defined
46  if( undefFileName != m_defFileName ) {
47  m_hfile = TFile::Open(m_defFileName.c_str(),"RECREATE","GAUDI Histograms");
48  } else {
49  m_hfile = 0;
50  }
51  log << MSG::INFO << "Writing ROOT histograms to: " << m_defFileName
52  << endmsg;
53  }
54  else {
55  log << MSG::INFO << "Writing ROOT histograms disabled." << endmsg;
56  }
57  return StatusCode(StatusCode::SUCCESS,true);
58 }
59 
60 
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 {
65  // Close ROOT only if the output file name is defined
66  MsgStream log( msgSvc(), name() );
67  log << MSG::DEBUG << "RootHistCnv::PersSvc::finalize()" << endmsg;
68  if( undefFileName != m_defFileName ) {
69  m_hfile->Write("",TObject::kOverwrite);
70  m_hfile->Close();
71  }
72  return ConversionSvc::finalize();
73 }
74 
75 //-----------------------------------------------------------------------------
77  IOpaqueAddress*& refpAddress)
78 //-----------------------------------------------------------------------------
79 {
80  // There are objects in the HDS to be stored
81  if( m_outputEnabled && undefFileName != m_defFileName ) {
82  SmartDataPtr<DataObject> top(dataProvider(), stat_dir);
83  if ( 0 != top ) {
84  IRegistry* pReg = top->registry();
85  if ( pReg ) {
86  if ( top.ptr() == pObject ) {
87  TDirectory* pDir = m_hfile;
88  refpAddress = new RootObjAddress( repSvcType(),
89  CLID_DataObject,
90  stat_dir,
91  m_defFileName,
92  long(pDir),
93  long(0));
94  return StatusCode::SUCCESS;
95  }
96  else {
97  StatusCode sc = ConversionSvc::createRep(pObject, refpAddress);
98  if( sc.isFailure() ) {
99  MsgStream log( msgSvc(), name() );
100  log << MSG::ERROR
101  << "Error while creating persistent Histogram:"
102  << pReg->identifier()
103  << endmsg;
104  }
105  return sc;
106  }
107  }
108  }
109  MsgStream err( msgSvc(), name() );
110  err << MSG::ERROR
111  << "Internal error while creating Histogram persistent representations"
112  << endmsg;
113  return StatusCode::FAILURE;
114  } else {
115  if (m_outputEnabled && !m_prtWar) {
116  m_prtWar = true;
117  MsgStream log( msgSvc(), name() );
118  log << MSG::WARNING
119  << "no ROOT output file name, "
120  << "Histograms cannot be persistified" << endmsg;
121  }
122  }
123  return StatusCode::SUCCESS;
124 }
125 
126 //-----------------------------------------------------------------------------
127 RootHistCnv::PersSvc::PersSvc(const std::string& name, ISvcLocator* svc)
128 //-----------------------------------------------------------------------------
129 : ConversionSvc(name, svc, ROOT_StorageType), m_hfile(0), m_prtWar(false) {
130  declareProperty("OutputFile", m_defFileName = undefFileName);
131  declareProperty("ForceAlphaIds", m_alphaIds = false);
132  declareProperty("OutputEnabled", m_outputEnabled = true,
133  "Flag to enable/disable the output to file.");
134 }
135 
136 //-----------------------------------------------------------------------------
138 //-----------------------------------------------------------------------------
139 {
140  if ( m_hfile != 0 ) {
141  delete m_hfile;
142  m_hfile = 0;
143  }
144 }
bool m_outputEnabled
Flag to enable/disable the output to file.
Definition: PersSvc.h:42
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Implementation of IConverter: Convert the transient object to the requested representation.
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
virtual StatusCode initialize()
Initialise the service.
Definition: PersSvc.cpp:31
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:26
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Convert a collection of transient data objects into another representation.
Definition: PersSvc.cpp:76
virtual ~PersSvc()
Standard destructor.
Definition: PersSvc.cpp:137
PersSvc(const std::string &name, ISvcLocator *svc)
Standard constructor.
Definition: PersSvc.cpp:127
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:85
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:69
std::string m_defFileName
Default file name.
Definition: PersSvc.h:38
#define DECLARE_COMPONENT(type)
Definition: PluginService.h:36
TYPE * ptr()
Automatic conversion to data type.
virtual StatusCode finalize()
stop the service.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
const long ROOT_StorageType
Definition: ClassID.h:53
bool m_alphaIds
Force alphabetic histograms/ntuple IDs.
Definition: PersSvc.h:41
StatusCode setProperties()
Method for setting declared properties to the values specified for the job.
Definition: Service.cpp:371
virtual StatusCode finalize()
Finalise the service.
Definition: PersSvc.cpp:62
virtual const std::string & name() const
Retrieve name of the service.
Definition: Service.cpp:331
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:46
Persistency service - to store histograms in ROOT format.
Definition: PersSvc.h:20
virtual StatusCode initialize()
Initialize the service.
virtual const id_type & identifier() const =0
Full identifier (or key)
Opaque address interface definition.
Base class for all conversion services.
Definition: ConversionSvc.h:47
Property * declareProperty(const std::string &name, T &property, const std::string &doc="none") const
Declare the named property.
Definition: Service.h:211
TFile * m_hfile
Pointer to the ROOT file.
Definition: PersSvc.h:39
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:31
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244