BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
RootEventBaseCnv.cxx
Go to the documentation of this file.
1#define _RootEventBaseCnv_CXX
2
3#include "TClass.h"
4#include "TObject.h"
5#include "TROOT.h"
6
7#include "GaudiKernel/CommonMessaging.h"
8#include "GaudiKernel/DataObject.h"
9#include "GaudiKernel/IAddressCreator.h"
10#include "GaudiKernel/IConverter.h"
11#include "GaudiKernel/IDataManagerSvc.h"
12#include "GaudiKernel/IEvtSelector.h"
13#include "GaudiKernel/IService.h"
14#include "GaudiKernel/ISvcLocator.h"
15#include "GaudiKernel/MsgStream.h"
16#include "GaudiKernel/RegistryEntry.h"
17#include "GaudiKernel/SmartIF.h"
18
19#include "RootCnvSvc/IRootCnvSvc.h"
20#include "RootCnvSvc/RootAddress.h"
21#include "RootCnvSvc/RootCnvSvc.h"
22#include "RootCnvSvc/RootEventBaseCnv.h"
23#include "RootCnvSvc/RootEvtSelector.h"
24#include "RootEventData/TDigiEvent.h"
25
26extern const InterfaceID IID_IRootCnvSvc;
27
28using namespace DataSvcHelpers;
29
30RootEventBaseCnv::RootEventBaseCnv( const CLID& clid, ISvcLocator* svc )
31 : Converter( ROOT_StorageType, clid, svc )
32 , m_cnvSvc( 0 )
33 , m_rootBranchname( "" )
34 , m_rootTreename( "Event" )
35 , m_evtsel( 0 ) {
36
37 m_branchNr = -1;
38 m_branchNrDst = -1;
39 m_branchNrMc = -1;
40 m_branchNrRecon = -1;
44 m_objRead = 0;
45 CLID_top = 0;
46 m_branchNumbers = new TArrayS( 0 );
47}
48
49StatusCode RootEventBaseCnv::createRep( DataObject* obj, IOpaqueAddress*& addr ) {
50 // Purpose and Method: Convert the transient object to ROOT
51
52 MsgStream log( msgSvc(), "RootEventBaseCnv" );
53
54 StatusCode sc = StatusCode::SUCCESS;
55 // get the corresponding address
56 RootAddress* rootaddr;
57 sc = m_cnvSvc->createAddress( obj, addr );
58
59 rootaddr = dynamic_cast<RootAddress*>( addr );
60
61 if ( sc.isFailure() || !rootaddr )
62 {
63 log << MSG::ERROR << "Could not create address for clid " << obj->clID() << ", objname "
64 << obj->name() << endmsg;
65 return StatusCode::FAILURE;
66 }
67
68 // do the real conversion in the derived converter
69 sc = DataObjectToTObject( obj, rootaddr );
70
71 delete addr;
72 addr = NULL;
73
74 if ( sc.isFailure() )
75 {
76 log << MSG::ERROR << "Could not transform object" << endmsg;
77 return sc;
78 }
79
80 return StatusCode::SUCCESS;
81}
82StatusCode RootEventBaseCnv::fillRepRefs( IOpaqueAddress*, DataObject* ) {
83 // Purpose and Method: Resolve the references of the converted object.
84 // It is expected that derived classes will override this method.
85 MsgStream log( msgSvc(), "RootEventBaseCnv" );
86 return StatusCode::SUCCESS;
87}
88
89StatusCode RootEventBaseCnv::fillObjRefs( IOpaqueAddress*, DataObject* ) {
90 // Purpose and Method: Resolve the references of the converted object.
91 // It is expected that derived classes will override this method.
92 MsgStream log( msgSvc(), "RootEventBaseCnv" );
93 return StatusCode::SUCCESS;
94}
95
97
98 MsgStream log( msgSvc(), "RootEventBaseCnv" );
99 log << MSG::INFO << "RootEventBaseCnv::initialize" << endmsg;
100
101 StatusCode status = Converter::initialize();
102
103 m_rootInterface = RootInterface::Instance( "RootEventBaseCnv" );
104 if ( !m_rootInterface )
105 log << MSG::ERROR << "Unable to start Root interface within RootCnvSvc" << endmsg;
106
107 StatusCode sc;
108 // MsgStream log( msgSvc(), "RootEventBaseCnv" );
109
110 // Get DataSvc
111 IService* isvc = 0;
112 sc = serviceLocator()->getService( "EventDataSvc", isvc, true );
113 if ( sc.isSuccess() )
114 { sc = isvc->queryInterface( IDataProviderSvc::interfaceID(), (void**)&m_eds ); }
115 if ( sc.isFailure() )
116 { log << MSG::ERROR << "Unable start EventData service within RootEventBaseCnv" << endmsg; }
117
118 ///////////////////// liangyt, try to get event id from evt selector
119 ////////////////////////////////// / the former method to retrieve the event number of one
120 /// branch is to calculate/////////////////// / division of branch number of one event (brN)
121 /// and total readed branch num(entryN)/////////////// / there are some disadvantages for
122 /// this method. NOW, we try to get event id from evt selector////
123
124 sc = serviceLocator()->getService( "RootEvtSelector", isvc, false );
125 if ( !sc.isSuccess() ) sc = serviceLocator()->getService( "EventSelector", isvc, false );
126 if ( sc.isSuccess() )
127 { sc = isvc->queryInterface( IID_IRootEvtSelector, (void**)&m_evtsel ); }
128 if ( sc.isFailure() )
129 {
130 log << MSG::WARNING << "Unable to start event selector service within RootCnvSvc"
131 << endmsg;
132 }
133 ////////////////////////
134
135 // if ( status.isSuccess() )
136 // {
137 // IService* isvc = 0;
138 // status = serviceLocator()->service( "RootCnvSvc", isvc, false );
139 // if ( !status.isSuccess() )
140 // {
141 // log << MSG::DEBUG << "retrieved RootCnvSvc" << endmsg;
142 // status = serviceLocator()->service( "EventCnvSvc", isvc, true );
143 // }
144 // else
145 // {
146 // log << MSG::DEBUG << "failed to retrieved RootCnvSvc" << endmsg;
147 // status = isvc->queryInterface( IID_IRootCnvSvc, (void**)&m_cnvSvc );
148 // }
149 // }
150
151 if ( !status.isSuccess() ) return status;
152
153 status = service( "RootCnvSvc", m_cnvSvc, false );
154 if ( !status.isSuccess() ) status = service( "EventCnvSvc", m_cnvSvc, true );
155
156 return status;
157}
158
160 if ( m_cnvSvc )
161 {
162 m_cnvSvc->release();
163 m_cnvSvc = 0;
164 }
165 return Converter::finalize();
166}
167
168void RootEventBaseCnv::declareObject( const std::string& path, const CLID& cl,
169 const std::string& treename,
170 const std::string& branchname ) {
171 // Purpose and Method: Save the path on the TDS, treename, pathname in the m_leaves vector,
172 // corresponding to the DataObject that the converter handles.
173 m_leaves.push_back( RootCnvSvc::Leaf( path, cl, treename, branchname ) );
174}
175
176StatusCode RootEventBaseCnv::createObj( IOpaqueAddress* addr, DataObject*& refpObject ) {
177 // transform ROOT object to TDS object
178 MsgStream log( msgSvc(), "RootEventBaseCnv" );
179 log << MSG::DEBUG << "RootEventBaseCnv::createObj with clid " << addr->clID() << endmsg;
180 StatusCode sc;
181
182 // add 2005-11-29
183 // log<<MSG::INFO<<"######### RootEventBaseCnv ::createObj begin of createObj:
184 // m_branchNumbers
185 // "<<m_branchNumbers->GetSize()<<"###############"<<endmsg;
186
187 RootAddress* raddr = dynamic_cast<RootAddress*>( addr );
188 if ( !raddr )
189 {
190 log << MSG::ERROR << "Could not downcast to Root address" << endmsg;
191 return StatusCode::FAILURE;
192 }
193
194 static int temp = 0; // control the begin of each files 2005-12-01
195 static int entryN = 0; // control the event number of each files 2005-21-01
196 static int brN = 0; // control munber of branch of the tree;
197 int lastBrn = brN;
198 // lastBrn = brN;
199 static int branchN = 0;
200 static bool isSet = true;
201
202 static int entryBefore = 0;
203 static bool addEntryEachFile = true;
204
205 if ( m_rootInterface->getENDFILE() || ( temp > 0 && temp < branchN ) )
206 { // if the file has get the end:y the go to next file to create a new tree
207
208 if ( m_rootInterface->getENDFILE() ) { entryN = 0; }
209
210 temp++;
211
212 delete m_branchNumbers;
213 m_branchNumbers = new TArrayS( 0 );
214
215 if ( temp == branchN ) { temp = 0; }
216 }
217
218 if ( m_rootInterface->getENDFILE() ) addEntryEachFile = true;
219 // the 2nd method
220 if ( m_evtsel->getRecId() - entryBefore == 0 )
221 { // first event in this file
222 delete m_branchNumbers;
223 m_branchNumbers = new TArrayS( 0 );
224 }
225
226 // new method to initialize the branchNumber
227 if ( m_currentFileName == "" ) m_currentFileName = m_rootInterface->getCurrentFileName();
228 if ( !( m_currentFileName == m_rootInterface->getCurrentFileName() ) )
229 {
230 m_currentFileName = m_rootInterface->getCurrentFileName();
231 delete m_branchNumbers;
232 m_branchNumbers = new TArrayS( 0 );
233 }
234 //----------------------------------------
235
236 if ( m_branchNumbers->GetSize() <= 0 )
237 {
238 if ( isSet ) brN++;
239 int branchNumber;
240 for ( int nb = 0; nb < raddr->getNrBranches(); nb++ )
241 {
242 sc = m_rootInterface->setBranchAddress( raddr->getTreename().c_str(),
243 raddr->getBranchname( nb ).c_str(),
244 m_adresses[nb], branchNumber );
245 if ( !sc.isSuccess() )
246 {
247 if ( isSet )
248 brN--; // liangyt: if fail to retrieve this branch, this will be not a effective
249 // branch.
250 // entryN++; //liangyt: this is the second method
251 if ( temp > 0 ) temp--; // temp > 0 means recording effective branch number.
252 return sc;
253 }
254 m_branchNumbers->Set( nb + 1 );
255 m_branchNumbers->AddAt( branchNumber, nb );
256 }
257 }
258
259 //// after a file initialized, m_rootInterface get entries
260 if ( addEntryEachFile && ( m_evtsel->getRecId() > entryBefore ) )
261 { // for a new file, add entry for ONLY one time.
262 entryBefore += m_rootInterface->getEntries();
263 addEntryEachFile = false;
264 }
265
266 if ( lastBrn == brN && isSet )
267 {
268 branchN = brN;
269 isSet = false;
270 }
271
272 if ( isSet == false )
273 log << MSG::INFO << " 1st method set event as : " << int( entryN / branchN ) << endmsg;
274 if ( isSet == false )
275 raddr->setEntryNr( int( entryN / branchN ) ); // former method, keep it to be backup.
276 if ( m_evtsel ) log << MSG::INFO << " event id = " << m_evtsel->getRecId() << endmsg;
277
278 int eventID = 0;
279 if ( entryBefore > m_evtsel->getRecId() )
280 eventID = m_evtsel->getRecId() + m_rootInterface->getEntries() - entryBefore;
281 else if ( entryBefore == m_evtsel->getRecId() ) eventID = 0;
282 else log << MSG::ERROR << "eventId error!!!" << endmsg;
283
284 log << MSG::INFO << " 2nd method set event as : " << eventID << endmsg;
285
286 if ( m_evtsel ) raddr->setEntryNr( eventID );
287
288 // add dengzy
289 if ( m_rootInterface->getSelectFromTag() != 0 )
290 {
291 if ( m_evtsel ) raddr->setEntryNr( m_evtsel->getRecId() );
292 } // end of add by dengzy
293
294 // read branch
295
296 if ( m_branchNumbers->GetSize() > 0 )
297 {
298 int nbtot = 0, nb;
299 for ( int ib = 0; ib < m_branchNumbers->GetSize(); ib++ )
300 {
301 // sc=m_rootInterface->getBranchEntry(m_branchNumbers->At(ib),raddr->getEntryNr(),nb);
302 // change to get branch entry with addr(set address for each entry) liangyt
303 sc = m_rootInterface->getBranchEntry( m_branchNumbers->At( ib ), raddr->getEntryNr(),
304 m_adresses[ib], nb );
305 if ( sc.isFailure() )
306 {
307 log << MSG::ERROR << "Could not read branch " << raddr->getBranchname( nb ) << endmsg;
308 return sc;
309 }
310 nbtot += nb;
311 }
312 }
313
314 else
315 { // get ROOT object
316 if ( CLID_top )
317 {
318 IConverter* p = conversionSvc()->converter( CLID_top );
319 RootEventBaseCnv* cnv = dynamic_cast<RootEventBaseCnv*>( p );
320 if ( !cnv )
321 {
322 log << MSG::ERROR << "Could not downcast to RootEventBaseCnv " << endmsg;
323 return StatusCode::FAILURE;
324 }
325 m_objRead = cnv->getReadObject();
326 }
327 }
328
329 // do concrete transformation in derived converter
330 sc = TObjectToDataObject( refpObject );
331 if ( sc.isFailure() )
332 {
333 log << MSG::ERROR << "Could not transform object" << endmsg;
334 return sc;
335 }
336
337 // verify if we have to register
338 IRegistry* ent = addr->registry();
339 if ( ent == 0 )
340 {
341 sc = m_eds->registerObject( raddr->getPath(), refpObject );
342 if ( sc.isFailure() )
343 {
344 log << MSG::ERROR << "Could not register object " << raddr->getPath() << " status "
345 << sc.getCode() << endmsg;
346 }
347 // }
348 }
349
350 entryN++;
351 return StatusCode::SUCCESS;
352}
const long int ROOT_StorageType
const InterfaceID IID_IRootCnvSvc
IMessageSvc * msgSvc()
Definition of a Root address, derived from IOpaqueAddress.
std::string getBranchname(int i) const
object regrouping CLID and pathname with treename/branchname
virtual StatusCode DataObjectToTObject(DataObject *dat, RootAddress *addr)=0
Do the concrete conversion from TDS to ROOT.
virtual StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)
Resolve the references of the converted object.
std::string m_rootTreename
each converter knows it's treename
virtual StatusCode createObj(IOpaqueAddress *addr, DataObject *&dat)
Convert the persistent object to transient.
IDataProviderSvc * m_eds
pointer to eventdataservice
virtual StatusCode initialize()
TObject * getReadObject() const
get the object to be read
virtual StatusCode finalize()
std::vector< void * > m_adresses
each converter knows the corresponding adresses
TArrayS * m_branchNumbers
array with number of branches for reading
virtual StatusCode TObjectToDataObject(DataObject *&dat)=0
Do the concrete conversion from ROOT to TDS.
void declareObject(const std::string &fullPath, const CLID &clid, const std::string &treename, const std::string &branchname)
Store TDS path to link a particular converter to an object on the TDS.
int m_branchNr
the branchNr of this converter for writing
RootInterface * m_rootInterface
pointer to the RootInterface
std::string m_rootBranchname
root branchname (may be concatenated of severals)
CLID CLID_top
the CLID of the upper converter if any
virtual StatusCode fillRepRefs(IOpaqueAddress *pAddress, DataObject *pObject)
Resolve the references of the converted object.
RootEventBaseCnv(const CLID &clid, ISvcLocator *svc)
virtual StatusCode createRep(DataObject *pObject, IOpaqueAddress *&refpAddress)
Convert the transient object to the requested representation.
static RootInterface * Instance(const std::string &name)
singleton behaviour