TagCollectionStream.cpp
Go to the documentation of this file.00001
00002 #define GAUDISVC_PERSISTENCYSVC_OUTPUTSTREAM_CPP
00003
00004
00005 #include "GaudiKernel/SmartIF.h"
00006 #include "GaudiKernel/MsgStream.h"
00007 #include "GaudiKernel/IRegistry.h"
00008 #include "GaudiKernel/AlgFactory.h"
00009 #include "GaudiKernel/Tokenizer.h"
00010 #include "GaudiKernel/SmartDataPtr.h"
00011 #include "GaudiKernel/IDataSourceMgr.h"
00012 #include "GaudiKernel/IDataManagerSvc.h"
00013 #include "GaudiKernel/GenericAddress.h"
00014 #include "TagCollectionStream.h"
00015
00016
00017 DECLARE_ALGORITHM_FACTORY(TagCollectionStream)
00018
00019 static const char SEPARATOR = IDataProviderSvc::SEPARATOR;
00020
00021
00022 TagCollectionStream::TagCollectionStream(const std::string& nam, ISvcLocator* pSvc)
00023 : OutputStream(nam, pSvc), m_addrColumn(0),
00024 m_isTopLeaf(false), m_collectionSvc(0)
00025 {
00026 declareProperty("AddressLeaf", m_addrLeaf = "/Event" );
00027 declareProperty("AddressColumn", m_addrColName = "Address");
00028 declareProperty("TagCollectionSvc", m_collSvcName = "NTupleSvc");
00029 declareProperty("ObjectsFirst", m_objectsFirst = true);
00030 declareProperty("Collection", m_tagName );
00031 m_addr = new GenericAddress();
00032 }
00033
00034
00035 TagCollectionStream::~TagCollectionStream() {
00036 delete m_addr;
00037 }
00038
00039
00040 StatusCode TagCollectionStream::connectAddress() {
00041 MsgStream log(msgSvc(), name());
00042 NTuplePtr nt(m_collectionSvc, m_tagName);
00043 if ( nt ) {
00044 m_addrColumn = nt->find(m_addrColName);
00045 if ( 0 == m_addrColumn ) {
00046 StatusCode sc = nt->addItem (m_addrColName,m_item);
00047 if ( sc.isSuccess() ) {
00048 m_addrColumn = m_item.operator->();
00049 return sc;
00050 }
00051 log << MSG::ERROR << "Failed to add the address column:"
00052 << m_addrColName << " to the tag collection " << m_tagName
00053 << endmsg;
00054 return sc;
00055 }
00056 return StatusCode::SUCCESS;
00057 }
00058 log << MSG::ERROR << "Failed to connect to the tag collection "
00059 << m_tagName << endmsg;
00060 return StatusCode::FAILURE;
00061 }
00062
00063
00064 StatusCode TagCollectionStream::initialize() {
00065 std::string log_node, log_file, logical_name;
00066 StatusCode sc = service(m_collSvcName, m_collectionSvc, true);
00067 if ( sc.isSuccess() ) {
00068 Tokenizer tok(true);
00069 tok.analyse(m_output, " ", "", "", "=", "'", "'");
00070 m_output = "";
00071 for(Tokenizer::Items::iterator i = tok.items().begin(); i != tok.items().end(); ++i) {
00072 const std::string& tag = (*i).tag();
00073 const std::string& val = (*i).value();
00074 switch( ::toupper(tag[0]) ) {
00075 case 'C':
00076 m_tagName = val;
00077 break;
00078 case 'A':
00079 m_addrLeaf = val;
00080 break;
00081 default:
00082 m_output += tag + "='" + val + "' ";
00083 break;
00084 }
00085 }
00086 std::string::size_type idx = m_tagName[0]==SEPARATOR ? m_tagName.find(SEPARATOR,1) : 0;
00087 log_node = m_tagName.substr(idx,m_tagName.find(SEPARATOR,idx+1));
00088 log_file = log_node + " " + m_output + " SHARED='YES'";
00089 }
00090 m_addrColumn = 0;
00091 sc = OutputStream::initialize();
00092 if ( sc.isSuccess() ) {
00093 SmartIF<IDataSourceMgr> src_mgr(m_collectionSvc);
00094 if ( src_mgr.isValid() ) {
00095 SmartIF<IDataManagerSvc> data_mgr(m_collectionSvc);
00096 if ( data_mgr.isValid() ) {
00097 logical_name = data_mgr->rootName();
00098 logical_name += SEPARATOR;
00099 logical_name += log_node;
00100 m_topLeafName = m_addrLeaf.substr(0,m_addrLeaf.find(SEPARATOR,m_addrLeaf[0]=='/' ? 1 : 0));
00101 m_isTopLeaf = m_topLeafName == m_addrLeaf;
00102 if ( src_mgr->isConnected(logical_name) ) {
00103 return sc;
00104 }
00105 sc = src_mgr->connect(log_file);
00106 if ( sc.isSuccess() ) {
00107 return sc;
00108 }
00109 }
00110 }
00111 }
00112 MsgStream log(msgSvc(), name());
00113 log << MSG::ERROR << "Failed to initialize TagCollection Stream." << endmsg;
00114 return StatusCode::FAILURE;
00115 }
00116
00117
00118 StatusCode TagCollectionStream::finalize() {
00119 MsgStream log(msgSvc(), name());
00120 StatusCode status = OutputStream::finalize();
00121 if ( m_collectionSvc ) m_collectionSvc->release();
00122 m_collectionSvc = 0;
00123 m_addrColumn = 0;
00124 return status;
00125 }
00126
00127
00128 StatusCode TagCollectionStream::writeTuple() {
00129 StatusCode sc = m_collectionSvc->writeRecord(m_tagName);
00130 if ( !sc.isSuccess() ) {
00131 MsgStream log(msgSvc(), name());
00132 log << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00133 << "[Tuple write error]" << endmsg;
00134 }
00135 return sc;
00136 }
00137
00138
00139 StatusCode TagCollectionStream::writeData() {
00140 StatusCode sc = OutputStream::writeObjects();
00141 if ( !sc.isSuccess() ) {
00142 MsgStream log(msgSvc(), name());
00143 log << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00144 << "[Object write error]" << endmsg;
00145 }
00146 return sc;
00147 }
00148
00149
00150 StatusCode TagCollectionStream::writeRecord() {
00151 if ( writeTuple().isSuccess() ) {
00152 return m_objectsFirst ? StatusCode::SUCCESS : writeData();
00153 }
00154 return StatusCode::FAILURE;
00155 }
00156
00157
00158 StatusCode TagCollectionStream::writeObjects() {
00159 StatusCode status = m_addrColumn == 0 ? connectAddress() : StatusCode::SUCCESS;
00160 if ( status.isSuccess() ) {
00161 status = m_objectsFirst ? writeData() : StatusCode::SUCCESS;
00162 if ( status.isSuccess() ) {
00163 if ( !m_addrColName.empty() ) {
00164 SmartDataPtr<DataObject> top(eventSvc(), m_topLeafName);
00165 if ( top != 0 ) {
00166 IOpaqueAddress* pA = top->registry()->address();
00167 if ( pA != 0 ) {
00168 std::string* par = (std::string*)m_addr->par();
00169 unsigned long* ipar = (unsigned long*)m_addr->ipar();
00170 m_addr->setClID(pA->clID());
00171 m_addr->setSvcType(pA->svcType());
00172 par[0] = pA->par()[0];
00173 par[1] = pA->par()[1];
00174 ipar[0] = pA->ipar()[0];
00175 ipar[1] = pA->ipar()[1];
00176 *(IOpaqueAddress**)(m_addrColumn->buffer()) = m_addr;
00177 if ( m_isTopLeaf ) {
00178 return writeRecord();
00179 }
00180
00181
00182 SmartDataPtr<DataObject> leaf(eventSvc(), m_addrLeaf);
00183 if ( 0 != leaf ) {
00184 IOpaqueAddress* redir = leaf->registry()->address();
00185 if ( redir ) {
00186 par[0] = redir->par()[0];
00187 ipar[0] = ~0x0;
00188 ipar[1] = redir->ipar()[1];
00189 return writeRecord();
00190 }
00191 MsgStream log3(msgSvc(), name());
00192 log3 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00193 << m_addrLeaf << "'s address not found." << endmsg;
00194 return StatusCode::FAILURE;
00195 }
00196 MsgStream log2(msgSvc(), name());
00197 log2 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00198 << m_addrLeaf << " not found." << endmsg;
00199 return StatusCode::FAILURE;
00200 }
00201 MsgStream log1(msgSvc(), name());
00202 log1 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00203 << m_topLeafName << "'s address not found." << endmsg;
00204 return StatusCode::FAILURE;
00205 }
00206 MsgStream log0(msgSvc(), name());
00207 log0 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
00208 << m_topLeafName << " not found." << endmsg;
00209 return StatusCode::FAILURE;
00210 }
00211 }
00212 }
00213 return status;
00214 }