00001 #define RootHistCnv_PERSSVC_CPP
00002
00003
00004
00005 #include <iostream>
00006 #include <string>
00007 #include "GaudiKernel/DataObject.h"
00008 #include "GaudiKernel/SvcFactory.h"
00009 #include "GaudiKernel/CnvFactory.h"
00010 #include "GaudiKernel/MsgStream.h"
00011 #include "GaudiKernel/IDataSelector.h"
00012 #include "GaudiKernel/IConverter.h"
00013 #include "GaudiKernel/IHistogramSvc.h"
00014 #include "GaudiKernel/ISvcLocator.h"
00015 #include "PersSvc.h"
00016
00017 #include "TROOT.h"
00018 #include "TFile.h"
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 static SvcFactory<RootHistCnv::PersSvc> s_factory;
00033 const ISvcFactory& RootHistPersSvcFactory = s_factory;
00034
00035
00036 namespace RootHistCnv {
00037
00038 static std::string stat_dir = "/stat";
00039 static std::string undefFileName = "UndefinedROOTOutputFileName";
00040
00041
00043 StatusCode PersSvc::initialize() {
00044 StatusCode status = ConversionSvc::initialize();
00045
00046 MsgStream log( messageService(), name() );
00047
00048
00049 if ( status.isSuccess() ) {
00050
00051 IHistogramSvc *pIHS = 0;
00052 status = serviceLocator()->getService("HistogramDataSvc", IID_IHistogramSvc, (IInterface*&)pIHS);
00053 if ( status.isSuccess() ) {
00054 status = setStore ( pIHS );
00055 }
00056 else {
00057 return status;
00058 }
00059
00060 status = setProperties();
00061
00062
00063 if ( 0 == gROOT ) {
00064 static TROOT root("root","ROOT I/O");
00065
00066
00067 } else {
00068 log << MSG::INFO << "ROOT already initialized" << endreq;
00069 }
00070
00071
00072 if( undefFileName != m_defFileName ) {
00073 hfile = new TFile(m_defFileName.c_str(),"RECREATE","GAUDI Histograms");
00074 }
00075 log << MSG::INFO << "Writing ROOT histograms to: " << m_defFileName
00076 << endreq;
00077 }
00078 return status;
00079 }
00080
00081
00083 StatusCode PersSvc::finalize() {
00084
00085 MsgStream log( messageService(), name() );
00086 log << MSG::DEBUG << "RootHistCnv::PersSvc::finalize()" << endreq;
00087
00088 if( undefFileName != m_defFileName ) {
00089
00090 hfile->cd();
00091 hfile->Write();
00092 hfile->Close();
00093 }
00094 return ConversionSvc::finalize();
00095 }
00096
00097
00099 StatusCode PersSvc::createReps(IDataSelector* pSelector) {
00100
00101
00102
00103
00104 MsgStream log( messageService(), name() );
00105
00106
00107 if( 1 != pSelector->size() ) {
00108
00109
00110 if( undefFileName != m_defFileName ) {
00111
00112 TDirectory *gDir;
00113 gDir = gDirectory;
00114 hfile->cd("/");
00115
00116
00117 StatusCode sc = ConversionSvc::createReps(pSelector);
00118
00119
00120 if( sc.isSuccess() ) {
00121 log << MSG::INFO << "ALL Histograms converted successfully" << endreq;
00122
00123 } else {
00124 log << MSG::ERROR << "Error while creating Histogram persistent representations" << endreq;
00125 }
00126 gDirectory = gDir;
00127 return sc;
00128 }
00129 else {
00130 log << MSG::ERROR << "ROOT output file name missing, Histograms cannot be converted" << endreq;
00131 }
00132 }
00133
00134 return StatusCode::SUCCESS;
00135 }
00136
00137
00139 PersSvc::PersSvc(const std::string& name, ISvcLocator* svc)
00140 : ConversionSvc(name, svc, ROOT_StorageType) {
00141 declareProperty("OutputFile", m_defFileName = undefFileName);
00142 }
00143
00144
00146 PersSvc::~PersSvc() {
00147 if ( hfile != 0 ) {
00148 delete hfile;
00149 hfile = 0;
00150 }
00151 }
00152
00153 }