00001 #define _RootEventBaseCnv_CXX
00002
00003 #include "TROOT.h"
00004 #include "TObject.h"
00005 #include "TClass.h"
00006
00007 #include "GaudiKernel/IDataManagerSvc.h"
00008 #include "GaudiKernel/IService.h"
00009 #include "GaudiKernel/IEvtSelector.h"
00010 #include "GaudiKernel/ISvcLocator.h"
00011 #include "GaudiKernel/RegistryEntry.h"
00012 #include "GaudiKernel/MsgStream.h"
00013 #include "GaudiKernel/DataObject.h"
00014 #include "GaudiKernel/IConverter.h"
00015 #include "GaudiKernel/IAddressCreator.h"
00016 #include "GaudiKernel/SmartIF.h"
00017
00018
00019 #include "RootEventData/TDigiEvent.h"
00020 #include "RootCnvSvc/RootEvtSelector.h"
00021 #include "RootCnvSvc/RootEventBaseCnv.h"
00022 #include "RootCnvSvc/RootCnvSvc.h"
00023 #include "RootCnvSvc/RootAddress.h"
00024
00025 extern const InterfaceID IID_IRootCnvSvc;
00026
00027 using namespace DataSvcHelpers;
00028
00029 RootEventBaseCnv::RootEventBaseCnv(const CLID& clid, ISvcLocator* svc)
00030 : Converter(ROOT_StorageType, clid, svc) ,m_cnvSvc(0),m_rootBranchname(""),m_rootTreename("Event"), m_evtsel(0)
00031 {
00032
00033 StatusCode sc;
00034 MsgStream log(msgSvc(), "RootEventBaseCnv");
00035
00036
00037 IService* isvc = 0;
00038 sc = serviceLocator()->getService("EventDataSvc", isvc, true);
00039 if(sc.isSuccess() ) {
00040 sc = isvc->queryInterface(IDataProviderSvc::interfaceID(), (void**)&m_eds);
00041 }
00042 if(sc.isFailure()){
00043 log << MSG::ERROR << "Unable start EventData service within RootEventBaseCnv" << endreq;
00044 }
00045
00046 m_rootInterface=RootInterface::Instance(log);
00047 if (!m_rootInterface) log << MSG::ERROR << "Unable to start Root interface within RootCnvSvc" << endreq;
00048
00053
00054 sc = serviceLocator()->getService ("RootEvtSelector", isvc, false);
00055 if (!sc.isSuccess()) sc = serviceLocator()->getService ("EventSelector", isvc, false);
00056 if (sc.isSuccess()) {
00057 sc = isvc->queryInterface(IID_IRootEvtSelector, (void**)&m_evtsel);
00058 }
00059 if(sc.isFailure()) {
00060 log << MSG::WARNING << "Unable to start event selector service within RootCnvSvc" << endreq;
00061 }
00063
00064
00065 m_branchNr=-1;
00066 m_branchNrDst=-1;
00067 m_branchNrMc=-1;
00068 m_branchNrRecon=-1;
00069 m_branchNrEvtRec=-1;
00070 m_branchNrEvtHeader=-1;
00071 m_branchNrEvtNavigator=-1;
00072 m_objRead=0;
00073 CLID_top=0;
00074 m_branchNumbers= new TArrayS(0);
00075 }
00076
00077
00078 StatusCode RootEventBaseCnv::createRep(DataObject* obj,
00079 IOpaqueAddress*& addr) {
00080
00081
00082 MsgStream log(msgSvc(), "RootEventBaseCnv");
00083
00084 StatusCode sc= StatusCode::SUCCESS;
00085
00086 RootAddress *rootaddr;
00087 sc=m_cnvSvc->createAddress(obj,addr);
00088
00089 rootaddr = dynamic_cast<RootAddress *>(addr);
00090
00091 if (sc.isFailure() || !rootaddr ) {
00092 log << MSG::ERROR << "Could not create address for clid " <<obj->clID()<<", objname "<<obj->name()<<endreq;
00093 return StatusCode::FAILURE;
00094 }
00095
00096
00097 sc = DataObjectToTObject(obj,rootaddr);
00098
00099 delete addr;
00100 addr = NULL;
00101
00102 if (sc.isFailure()) {
00103 log << MSG::ERROR << "Could not transform object" << endreq;
00104 return sc;
00105 }
00106
00107 return StatusCode::SUCCESS;
00108 }
00109 StatusCode RootEventBaseCnv::fillRepRefs(IOpaqueAddress* ,
00110 DataObject* ) {
00111
00112
00113 MsgStream log(msgSvc(), "RootEventBaseCnv");
00114 return StatusCode::SUCCESS;
00115 }
00116
00117 StatusCode RootEventBaseCnv::fillObjRefs(IOpaqueAddress* ,
00118 DataObject* ) {
00119
00120
00121 MsgStream log(msgSvc(), "RootEventBaseCnv");
00122 return StatusCode::SUCCESS;
00123 }
00124
00125
00126 StatusCode RootEventBaseCnv::initialize() {
00127
00128 StatusCode status = Converter::initialize();
00129
00130 if ( status.isSuccess() ) {
00131 IService* isvc = 0;
00132 status = serviceLocator()->service("RootCnvSvc", isvc, false);
00133 if ( !status.isSuccess() ) status = serviceLocator()->service("EventCnvSvc", isvc, true);
00134 if ( status.isSuccess() ) {
00135 status = isvc->queryInterface(IID_IRootCnvSvc, (void**)&m_cnvSvc);
00136 }
00137 }
00138
00139 return status;
00140 }
00141
00142 StatusCode RootEventBaseCnv::finalize() {
00143 if ( m_cnvSvc ) {
00144 m_cnvSvc->release();
00145 m_cnvSvc=0;
00146 }
00147 return Converter::finalize();
00148 }
00149
00150 void RootEventBaseCnv::declareObject(const std::string& path, const CLID& cl,
00151 const std::string& treename, const std::string& branchname) {
00152
00153
00154 m_leaves.push_back(RootCnvSvc::Leaf(path, cl,treename,branchname));
00155 }
00156
00157 StatusCode RootEventBaseCnv::createObj(IOpaqueAddress* addr,
00158 DataObject*& refpObject) {
00159
00160 MsgStream log(msgSvc(), "RootEventBaseCnv");
00161 log << MSG::DEBUG << "RootEventBaseCnv::createObj with clid " <<addr->clID()<< endreq;
00162 StatusCode sc;
00163
00164
00165
00166
00167 RootAddress *raddr=dynamic_cast<RootAddress *>(addr);
00168 if (!raddr) {
00169 log << MSG::ERROR << "Could not downcast to Root address" << endreq;
00170 return StatusCode::FAILURE;
00171 }
00172
00173 static int temp =0;
00174 static int entryN =0;
00175 static int brN =0;
00176 int lastBrn = brN;
00177
00178 static int branchN=0;
00179 static bool isSet=true;
00180
00181 static int entryBefore = 0;
00182 static bool addEntryEachFile = true;
00183
00184
00185 if(m_rootInterface->getENDFILE() || (temp >0 && temp < branchN)){
00186
00187 if(m_rootInterface->getENDFILE() ) {
00188 entryN = 0;
00189 }
00190
00191 temp++;
00192
00193 delete m_branchNumbers;
00194 m_branchNumbers = new TArrayS(0);
00195
00196 if(temp == branchN) {
00197 temp =0;
00198 }
00199 }
00200
00201 if(m_rootInterface->getENDFILE()) addEntryEachFile = true;
00202
00203 if(m_evtsel->getRecId() - entryBefore == 0) {
00204 delete m_branchNumbers;
00205 m_branchNumbers = new TArrayS(0);
00206 }
00207
00208
00209 if(m_currentFileName=="") m_currentFileName = m_rootInterface->getCurrentFileName();
00210 if(!(m_currentFileName == m_rootInterface->getCurrentFileName())){
00211 m_currentFileName = m_rootInterface->getCurrentFileName();
00212 delete m_branchNumbers;
00213 m_branchNumbers = new TArrayS(0);
00214 }
00215
00216
00217 if (m_branchNumbers->GetSize()<=0) {
00218 if(isSet) brN++;
00219 int branchNumber;
00220 for (int nb=0;nb<raddr->getNrBranches();nb++) {
00221 sc=m_rootInterface->setBranchAddress(raddr->getTreename().c_str(),raddr->getBranchname(nb).c_str(),m_adresses[nb],branchNumber);
00222 if (!sc.isSuccess())
00223 {
00224 if(isSet) brN--;
00225
00226 if(temp>0) temp--;
00227 return sc;
00228 }
00229 m_branchNumbers->Set(nb+1);
00230 m_branchNumbers->AddAt(branchNumber,nb);
00231
00232 }
00233 }
00234
00236 if(addEntryEachFile&&(m_evtsel->getRecId()>entryBefore)){
00237 entryBefore += m_rootInterface->getEntries();
00238 addEntryEachFile = false;
00239 }
00240
00241 if(lastBrn == brN && isSet ){
00242 branchN = brN;
00243 isSet=false;
00244 }
00245
00246 if(isSet==false) log << MSG::INFO <<" 1st method set event as : "<<int(entryN/branchN)<<endreq;
00247 if(isSet==false) raddr->setEntryNr(int(entryN/branchN));
00248 if(m_evtsel) log << MSG::INFO <<" event id = "<<m_evtsel->getRecId()<<endreq;
00249
00250
00251 int eventID = 0;
00252 if(entryBefore > m_evtsel->getRecId())
00253 eventID = m_evtsel->getRecId() + m_rootInterface->getEntries() - entryBefore;
00254 else if(entryBefore == m_evtsel->getRecId()) eventID = 0;
00255 else log << MSG::ERROR <<"eventId error!!!"<<endreq;
00256
00257 log << MSG::INFO <<" 2nd method set event as : "<<eventID<<endreq;
00258
00259 if(m_evtsel) raddr->setEntryNr(eventID);
00260
00261
00262 if(m_rootInterface->getSelectFromTag()!=0)
00263 {
00264 if(m_evtsel)
00265 raddr->setEntryNr( m_evtsel->getRecId() );
00266 }
00267
00268
00269
00270 if (m_branchNumbers->GetSize()>0) {
00271 int nbtot=0,nb;
00272 for (int ib=0;ib<m_branchNumbers->GetSize();ib++) {
00273
00274
00275 sc=m_rootInterface->getBranchEntry(m_branchNumbers->At(ib),raddr->getEntryNr(),m_adresses[ib],nb);
00276 if (sc.isFailure()) {
00277 log << MSG::ERROR << "Could not read branch " << raddr->getBranchname(nb) << endreq;
00278 return sc;
00279 }
00280 nbtot+=nb;
00281 }
00282 }
00283
00284 else {
00285 if (CLID_top) {
00286 IConverter *p=conversionSvc()->converter(CLID_top);
00287 RootEventBaseCnv *cnv=dynamic_cast<RootEventBaseCnv *>(p);
00288 if (!cnv) {
00289 log << MSG::ERROR << "Could not downcast to RootEventBaseCnv " << endreq;
00290 return StatusCode::FAILURE;
00291 }
00292 m_objRead=cnv->getReadObject();
00293 }
00294 }
00295
00296
00297 sc = TObjectToDataObject(refpObject);
00298 if (sc.isFailure()) {
00299 log << MSG::ERROR << "Could not transform object" << endreq;
00300 return sc;
00301 }
00302
00303
00304 IRegistry* ent = addr->registry();
00305 if ( ent == 0) {
00306 sc=m_eds->registerObject(raddr->getPath(),refpObject);
00307 if (sc.isFailure()) {
00308 log << MSG::ERROR << "Could not register object " << raddr->getPath()<<" status "<<sc.getCode()<<endreq;
00309 }
00310
00311 }
00312
00313 entryN++;
00314 return StatusCode::SUCCESS;
00315 }