| # Gaudi Framework, version v25r0 |

Home |  | Generated: Mon Feb 17 2014

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TagCollectionStream.cpp
Go to the documentation of this file.
1 // Framework include files
2 #include "GaudiKernel/SmartIF.h"
3 #include "GaudiKernel/MsgStream.h"
4 #include "GaudiKernel/IRegistry.h"
5 #include "GaudiKernel/Tokenizer.h"
6 #include "GaudiKernel/SmartDataPtr.h"
7 #include "GaudiKernel/IDataSourceMgr.h"
8 #include "GaudiKernel/IDataManagerSvc.h"
9 #include "GaudiKernel/GenericAddress.h"
10 #include "TagCollectionStream.h"
11
12 // Define the algorithm factory for the standard output data writer
13 DECLARE_COMPONENT(TagCollectionStream)
14
15 static const char SEPARATOR = IDataProviderSvc::SEPARATOR;
16
17 // Standard Constructor
18 TagCollectionStream::TagCollectionStream(const std::string& nam, ISvcLocator* pSvc)
19 : OutputStream(nam, pSvc), m_addrColumn(0),
20 m_isTopLeaf(false), m_collectionSvc(0)
21 {
22 declareProperty("AddressLeaf", m_addrLeaf = "/Event" );
23 declareProperty("AddressColumn", m_addrColName = "Address");
24 declareProperty("TagCollectionSvc", m_collSvcName = "NTupleSvc");
25 declareProperty("ObjectsFirst", m_objectsFirst = true);
26 declareProperty("Collection", m_tagName );
27 m_addr = new GenericAddress();
28 }
29
30 // Standard Destructor
31 TagCollectionStream::~TagCollectionStream() {
32 delete m_addr;
33 }
34
35 // Connect address column, if not already connected
36 StatusCode TagCollectionStream::connectAddress() {
37 MsgStream log(msgSvc(), name());
38 NTuplePtr nt(m_collectionSvc, m_tagName);
39 if ( nt ) {
40 m_addrColumn = nt->find(m_addrColName);
41 if ( 0 == m_addrColumn ) {
42 StatusCode sc = nt->addItem (m_addrColName,m_item);
43 if ( sc.isSuccess() ) {
44 m_addrColumn = m_item.operator->();
45 return sc;
46 }
47 log << MSG::ERROR << "Failed to add the address column:"
48 << m_addrColName << " to the tag collection " << m_tagName
49 << endmsg;
50 return sc;
51 }
52 return StatusCode::SUCCESS;
53 }
54 log << MSG::ERROR << "Failed to connect to the tag collection "
55 << m_tagName << endmsg;
56 return StatusCode::FAILURE;
57 }
58
59 // initialize data writer
60 StatusCode TagCollectionStream::initialize() {
61 std::string log_node, log_file, logical_name;
62 StatusCode sc = service(m_collSvcName, m_collectionSvc, true);
63 if ( sc.isSuccess() ) {
64 Tokenizer tok(true);
65 tok.analyse(m_output, " ", "", "", "=", "'", "'");
66 m_output = "";
67 for(Tokenizer::Items::iterator i = tok.items().begin(); i != tok.items().end(); ++i) {
68 const std::string& tag = (*i).tag();
69 const std::string& val = (*i).value();
70 switch( ::toupper(tag[0]) ) {
71 case 'C':
72 m_tagName = val;
73 break;
74 case 'A':
75 m_addrLeaf = val;
76 break;
77 default:
78 m_output += tag + "='" + val + "' ";
79 break;
80 }
81 }
82 std::string::size_type idx = m_tagName[0]==SEPARATOR ? m_tagName.find(SEPARATOR,1) : 0;
83 log_node = m_tagName.substr(idx,m_tagName.find(SEPARATOR,idx+1));
84 log_file = log_node + " " + m_output + " SHARED='YES'";
85 }
86 m_addrColumn = 0; // reset pointer to item column
87 sc = OutputStream::initialize(); // Now initialize the base class
88 if ( sc.isSuccess() ) {
89 SmartIF<IDataSourceMgr> src_mgr(m_collectionSvc);
90 if ( src_mgr.isValid() ) {
91 SmartIF<IDataManagerSvc> data_mgr(m_collectionSvc);
92 if ( data_mgr.isValid() ) {
93 logical_name = data_mgr->rootName();
94 logical_name += SEPARATOR;
95 logical_name += log_node;
96 m_topLeafName = m_addrLeaf.substr(0,m_addrLeaf.find(SEPARATOR,m_addrLeaf[0]=='/' ? 1 : 0));
97 m_isTopLeaf = m_topLeafName == m_addrLeaf;
98 if ( src_mgr->isConnected(logical_name) ) {
99 return sc;
100 }
101 sc = src_mgr->connect(log_file);
102 if ( sc.isSuccess() ) {
103 return sc;
104 }
105 }
106 }
107 }
108 MsgStream log(msgSvc(), name());
109 log << MSG::ERROR << "Failed to initialize TagCollection Stream." << endmsg;
110 return StatusCode::FAILURE;
111 }
112
113 // terminate data writer
114 StatusCode TagCollectionStream::finalize() {
115 MsgStream log(msgSvc(), name());
116 StatusCode status = OutputStream::finalize();
117 if ( m_collectionSvc ) m_collectionSvc->release();
118 m_collectionSvc = 0;
119 m_addrColumn = 0;
120 return status;
121 }
122
123 // Write tuple data
124 StatusCode TagCollectionStream::writeTuple() {
125 StatusCode sc = m_collectionSvc->writeRecord(m_tagName);
126 if ( !sc.isSuccess() ) {
127 MsgStream log(msgSvc(), name());
128 log << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
129 << "[Tuple write error]" << endmsg;
130 }
131 return sc;
132 }
133
134 // Write data objects
135 StatusCode TagCollectionStream::writeData() {
136 StatusCode sc = OutputStream::writeObjects();
137 if ( !sc.isSuccess() ) {
138 MsgStream log(msgSvc(), name());
139 log << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
140 << "[Object write error]" << endmsg;
141 }
142 return sc;
143 }
144
145 // Write full event record
146 StatusCode TagCollectionStream::writeRecord() {
147 if ( writeTuple().isSuccess() ) {
148 return m_objectsFirst ? StatusCode::SUCCESS : writeData();
149 }
150 return StatusCode::FAILURE;
151 }
152
153 // Work entry point
154 StatusCode TagCollectionStream::writeObjects() {
155 StatusCode status = m_addrColumn == 0 ? connectAddress() : StatusCode::SUCCESS;
156 if ( status.isSuccess() ) {
157 status = m_objectsFirst ? writeData() : StatusCode::SUCCESS;
158 if ( status.isSuccess() ) {
159 if ( !m_addrColName.empty() ) {
160 SmartDataPtr<DataObject> top(eventSvc(), m_topLeafName);
161 if ( top != 0 ) {
162 IOpaqueAddress* pA = top->registry()->address();
163 if ( pA != 0 ) {
164 std::string* par = (std::string*)m_addr->par();
165 unsigned long* ipar = (unsigned long*)m_addr->ipar();
166 m_addr->setClID(pA->clID());
167 m_addr->setSvcType(pA->svcType());
168 par[0] = pA->par()[0];
169 par[1] = pA->par()[1];
170 ipar[0] = pA->ipar()[0];
171 ipar[1] = pA->ipar()[1];
172 *(IOpaqueAddress**)(m_addrColumn->buffer()) = m_addr;
173 if ( m_isTopLeaf ) {
174 return writeRecord();
175 }
176 // Handle redirection mode. Normal tag collection mode
177 // is the same like leaving this out....
178 SmartDataPtr<DataObject> leaf(eventSvc(), m_addrLeaf);
179 if ( 0 != leaf ) {
180 IOpaqueAddress* redir = leaf->registry()->address();
181 if ( redir ) {
182 par[0] = redir->par()[0];
183 ipar[0] = ~0x0;
184 ipar[1] = redir->ipar()[1];
185 return writeRecord();
186 }
187 MsgStream log3(msgSvc(), name());
188 log3 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
189 << m_addrLeaf << "'s address not found." << endmsg;
190 return StatusCode::FAILURE;
191 }
192 MsgStream log2(msgSvc(), name());
193 log2 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
194 << m_addrLeaf << " not found." << endmsg;
195 return StatusCode::FAILURE;
196 }
197 MsgStream log1(msgSvc(), name());
198 log1 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
199 << m_topLeafName << "'s address not found." << endmsg;
200 return StatusCode::FAILURE;
201 }
202 MsgStream log0(msgSvc(), name());
203 log0 << MSG::ERROR << "Failed to write tag collection " << m_tagName << ". "
204 << m_topLeafName << " not found." << endmsg;
205 return StatusCode::FAILURE;
206 }
207 }
208 }
209 return status;
210 }