Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

PersSvc.cpp

Go to the documentation of this file.
00001 #define RootHistCnv_PERSSVC_CPP
00002 
00003 
00004 // Include files
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 // Implementation of class :  RootHistCnv::PersSvc
00024 //
00025 // Author :                   Charles Leggett
00026 //
00027 //------------------------------------------------------------------------------
00028 
00029 
00030 // Instantiation of a static factory class used by clients to create
00031 // instances of this service
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 //      log << MSG::INFO << "initialize()" << endreq;
00048 
00049     if ( status.isSuccess() ) {
00050       // Set histogram data store
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       // Get my properties from the JobOptionsSvc
00060       status = setProperties();
00061 
00062       // Protect against multiple instances of TROOT
00063       if ( 0 == gROOT )   {
00064         static TROOT root("root","ROOT I/O");
00065 //      log << MSG::INFO << "Initializing ROOT" << endreq;
00066         //      gDebug = 99;
00067       } else {
00068         log << MSG::INFO << "ROOT already initialized" << endreq;
00069       }
00070 
00071       // Initialize ROOT if output file name is defined
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     // Close ROOT only if the output file name is defined
00085     MsgStream log( messageService(), name() );
00086     log << MSG::DEBUG << "RootHistCnv::PersSvc::finalize()" << endreq;
00087 
00088     if( undefFileName != m_defFileName ) {
00089       //      gDirectory->cd( hfile->GetPath() );
00090       hfile->cd();
00091       hfile->Write();
00092       hfile->Close();
00093     }
00094     return ConversionSvc::finalize();
00095   }
00096 
00097 
00099   StatusCode PersSvc::createReps(IDataSelector* pSelector)            {
00100     // Convert histograms only if HBOOK output file name is defined
00101     //   and number of entries in the histogram data store is not equal 1
00102     //     (1 means, that the HDS is "empty", there is only
00103     //     the root directory "/stat", and no histograms)
00104     MsgStream log( messageService(), name() );
00105 //    log << MSG::DEBUG << "PersSvc::createReps()  nObj:" << pSelector->size()
00106 //      << endreq;
00107     if( 1 != pSelector->size() ) {
00108       // There are objects in the HDS to be stored
00109 
00110       if( undefFileName != m_defFileName ) {
00111 
00112         TDirectory *gDir;
00113         gDir = gDirectory;
00114         hfile->cd("/");
00115 
00116         // Valid output file name
00117         StatusCode sc = ConversionSvc::createReps(pSelector);
00118 //      log << MSG::DEBUG << "called createReps on conversion svc: " 
00119 //          << sc << endreq;
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     // Nothing to be converted, there are no histograms in the HDS
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 }    // namespace RootHistCnv

Generated at Wed Nov 21 12:20:49 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000