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