00001
00002
00003
00004
00005 #include "GaudiKernel/MsgStream.h"
00006 #include "GaudiKernel/StatusCode.h"
00007 #include "GaudiKernel/SvcFactory.h"
00008 #include "GaudiKernel/IDataManagerSvc.h"
00009 #include "GaudiKernel/IAddressCreator.h"
00010 #include "GaudiKernel/GenericAddress.h"
00011 #include "RootCnvSvc/RootEvtSelector.h"
00012 #include "RootCnvSvc/RootInterface.h"
00013 #include "RootCnvSvc/RootEventContext.h"
00014 #include "EventModel/EventModel.h"
00015 #include "TagFilterSvc/ITagFilterSvc.h"
00016 #include "TagFilterSvc/TagFilterSvc.h"
00017
00018 #include "GaudiKernel/SmartIF.h"
00019 #include "GaudiKernel/Bootstrap.h"
00020 #include "GaudiKernel/IAppMgrUI.h"
00021 #include "GaudiKernel/IProperty.h"
00022 #include "GaudiKernel/ISvcLocator.h"
00023
00024 #include "TFile.h"
00025 #include "TTree.h"
00026 #include "TFormula.h"
00027 #include <fstream>
00028 #include <vector>
00029
00030 extern const CLID& CLID_Event;
00031 extern const long int ROOT_StorageType;
00032
00033
00034
00035
00036
00037 RootEvtSelector::RootEvtSelector( const std::string& name, ISvcLocator* svcloc )
00038 : Service( name, svcloc),
00039 p_beginContext(0),
00040 p_endContext(0) {
00041 m_recId = new int;
00042 (*m_recId) = -1;
00043 declareProperty( "Input", m_jobInput="ROOT");
00044 getMaxEvent();
00045 }
00046 RootEvtSelector::~RootEvtSelector() {
00047 delete m_recId;
00048 }
00049
00050
00051 StatusCode RootEvtSelector::initialize() {
00052 MsgStream log(msgSvc(), name());
00053 StatusCode sc = Service::initialize();
00054 log << MSG::DEBUG << "RootEvtSelector::initialize" << endreq; if( sc.isSuccess() ) {
00055 setProperties();
00056 } else {
00057 log << MSG::ERROR << "Unable to initialize service " << endreq;
00058 }
00059
00060 if(m_jobInput.value() != " "){
00061 sc = setCriteria(m_jobInput);
00062 if(sc.isFailure()){
00063 log << MSG::ERROR << "Unable to get input value" << endreq;
00064 return sc;
00065 }
00066 }
00067
00068
00069 sc = serviceLocator()->service("EventCnvSvc", m_addrCreator);
00070 if( !sc.isSuccess() ) {
00071 log << MSG::ERROR <<
00072 "Unable to localize interface IID_IAddressCreator from service:"
00073 << "RootEventSelector"
00074 << endreq;
00075 return sc;
00076 }
00077
00078
00079 IDataManagerSvc* eds = 0;
00080 sc = serviceLocator()->service("EventDataSvc", eds, true);
00081 if( !sc.isSuccess() ) {
00082 log << MSG::ERROR
00083 << "Unable to localize interface IID_IDataManagerSvc "
00084 << "from RootEventSelector"
00085 << endreq;
00086 return sc;
00087 }
00088
00089 m_rootCLID = eds->rootCLID();
00090
00091
00092 m_rootInterface=RootInterface::Instance(log);
00093 log << MSG::INFO << "currentFile in RootEvtSelector::initialize "<<m_rootInterface->getCurrentFileName()<<endreq;
00094 if (!m_rootInterface) log << MSG::ERROR << "Unable to start Root service within RootCnvSvc" << endreq;
00095
00096
00097 p_beginContext = new RootEventContext(this);
00098
00099
00100 ITagFilterSvc *tmpSvc;
00101
00102 ISvcLocator* svcLocator = Gaudi::svcLocator();
00103 StatusCode status = svcLocator->service("TagFilterSvc",tmpSvc);
00104 if (status.isSuccess()) {
00105 log << MSG::INFO << "get the TagFilterSvc" << endreq;
00106 m_tagFilterSvc=dynamic_cast<TagFilterSvc *>(tmpSvc);
00107 }else {
00108 log << MSG::WARNING << "could not get the TagFilterSvc. Ignore it." << endreq;
00109 }
00110
00111 m_rootInterface->setTagInputFile(m_tagFilterSvc->getTagFiles());
00112
00113 vector<string> dstFiles = m_tagFilterSvc->getDstFiles();
00114 int size = dstFiles.size();
00115 std::string treeName="Event";
00116
00117 for(int i=0;i<size;i++)
00118 {
00119 std::cout<<"RootEvtSelector, dstFile: "<<dstFiles[i]<<std::endl;
00120 m_rootInterface->addInput(treeName, dstFiles[i].c_str());
00121 }
00122 return sc;
00123 }
00124
00125 StatusCode RootEvtSelector::setCriteria( const std::string& criteria ) {
00126
00127 MsgStream log(msgSvc(), name());
00128 m_criteria = criteria;
00129
00130 if( criteria == "ROOT" ) {
00131 log << MSG::INFO << "RootEvtSelector input has been set to ROOT" << endreq;
00132 m_criteriaType = ROOT;
00133 }else if( criteria == "NONE"){
00134 m_criteriaType = NONE;
00135 log << MSG::INFO << "RootEvtSelector input has been set to NONE" << endreq;
00136 }
00137 else {
00138 log << MSG::ERROR << "Invalid Event Selection Criteria: " << criteria << endreq;
00139 return StatusCode::FAILURE;
00140 }
00141 return StatusCode::SUCCESS;
00142 }
00143
00144 StatusCode
00145 RootEvtSelector::createContext(IEvtSelector::Context*& it) const
00146 {
00147 it = p_beginContext;
00148 return(StatusCode::SUCCESS);
00149 }
00150
00151
00152 StatusCode RootEvtSelector::next(IEvtSelector::Context& it) const
00153 {
00154 return this->next();
00155 }
00156
00157
00158 StatusCode RootEvtSelector::next(IEvtSelector::Context& ctxt, int jump) const {
00159 if ( jump > 0 ) {
00160 for ( int i = 0; i < jump; ++i ) {
00161 StatusCode status = next(ctxt);
00162 if ( !status.isSuccess() ) {
00163 return status;
00164 }
00165 }
00166 return StatusCode::SUCCESS;
00167 }
00168 return StatusCode::FAILURE;
00169 }
00170
00171 StatusCode RootEvtSelector::previous(IEvtSelector::Context& ) const {
00172 MsgStream log(messageService(), name());
00173 log << MSG::ERROR << "RootEvtSelector::previous() not implemented" << endreq;
00174 return(StatusCode::FAILURE);
00175 }
00176
00177 StatusCode RootEvtSelector::previous(IEvtSelector::Context& it,int ) const {
00178 return(previous(it));
00179 }
00180
00181 StatusCode RootEvtSelector::last(IEvtSelector::Context& it)const {
00182 if (it.identifier() == p_endContext->identifier()) {
00183 MsgStream log(messageService(), name());
00184 log << MSG::DEBUG << "last(): Last event in InputStream." <<endreq;
00185 return(StatusCode::SUCCESS);
00186 }
00187 return (StatusCode::FAILURE);
00188 }
00189
00190 StatusCode RootEvtSelector::resetCriteria(const std::string& , IEvtSelector::Context& ) const {
00191 return(StatusCode::SUCCESS);
00192 }
00193
00194
00195 StatusCode RootEvtSelector::rewind(IEvtSelector::Context& ) const {
00196 MsgStream log(messageService(), name());
00197 log << MSG::ERROR << "RootEvtSelector::rewind() not implemented" << endreq;
00198 return(StatusCode::FAILURE);
00199 }
00200
00201 StatusCode
00202 RootEvtSelector::releaseContext(IEvtSelector::Context*& )const {
00203 return(StatusCode::SUCCESS);
00204 }
00205
00206 StatusCode RootEvtSelector::createAddress(const IEvtSelector::Context& ,
00207 IOpaqueAddress*& iop) const {
00208 iop = new GenericAddress(ROOT_StorageType, CLID_Event, " ", " ", 0);
00209 return(StatusCode::SUCCESS);
00210 }
00211
00212 void RootEvtSelector::getEntryFromTag() const {
00213 bool flag=true;;
00214 MsgStream log(messageService(), name());
00215 static std::string tempFile = "";
00216 log << MSG::DEBUG <<"current file: "<<m_rootInterface->getCurrentFileName()<<endreq;
00217 std::ifstream fin;
00218 static std::string tagFile;
00219 static int i=-1;
00220 static std::vector<int> ventry;
00221 int num1 = m_rootInterface->getCurrentFileNum();
00222 int num2 = m_rootInterface->getTotalFileNum();
00223
00224
00225 if(tempFile != m_rootInterface->getCurrentFileName())
00226 {
00227 tempFile = m_rootInterface->getCurrentFileName();
00228 log << MSG::DEBUG<<"temp file: "<<tempFile<<endreq;
00229 i=-1;
00230 ventry.clear();
00231 log << MSG::DEBUG<<"num1: "<<num1<<" num2: "<<num2<<endreq;
00232 tagFile = (m_rootInterface->getTagInputFile())[num1];
00233 log << MSG::DEBUG <<"tag file: "<<tagFile<<endreq;
00234 m_tagFilterSvc->getVEntry(tagFile, ventry);
00235 }
00236
00237 i++;
00238 log << MSG::DEBUG<<"ventry i: "<<i<<endreq;
00239 log << MSG::DEBUG<<"ventry size: "<<ventry.size()<<endreq;
00240 if(i<ventry.size())
00241 {
00242 (*m_recId)=ventry[i];
00243 log << MSG::DEBUG<<"ventry[i]: "<<ventry[i]<<endreq;
00244 }
00245 else
00246 (*m_recId)= -1;
00247
00248 log << MSG::DEBUG<<"(*m_recId): "<<(*m_recId)<<endreq;
00249
00250 int vs = ventry.size();
00251
00252 if( (i>(vs-1)) && (num1<(num2-1)))
00253 {
00254 bool endAll = m_rootInterface->checkEndOfTree();
00255 getEntryFromTag();
00256 }
00257 if( (i>(vs-1)) && (num1==(num2-1)))
00258 (*m_recId)= -1;
00259 }
00260
00261
00262 StatusCode RootEvtSelector::next() const {
00263 MsgStream log(msgSvc(), name());
00264 if(m_criteriaType == ROOT)
00265 {
00266 log<<MSG::INFO<<"m_criteriaType == ROOT"<<endreq;
00267 if(m_rootInterface->getSelectFromTag()!=0)
00268 {
00269 getEntryFromTag();
00270 if((*m_recId)<0)
00271 return StatusCode::FAILURE;
00272 }
00273
00274 else
00275 {
00276 (*m_recId)++;
00277
00278
00279
00280
00281
00282 static int entry = 0;
00283
00284 bool endOfFile = m_rootInterface->getEOF();
00285 entry++;
00286 bool maxEntry = (entry >m_rootInterface->getEntries() && m_rootInterface->getEntries()>=0);
00287 if(maxEntry) entry =1;
00288 log<<MSG::INFO<<"entry = "<<entry<<" "<<maxEntry<<" "<<endOfFile<<endreq;
00289 if(endOfFile || maxEntry){
00290 bool endAll = m_rootInterface->checkEndOfTree();
00291
00292 if(endAll){
00293 return StatusCode::FAILURE;
00294 }
00295 }
00296 }
00297
00298 }
00299 else if( m_criteriaType == NONE)
00300 {
00301 log << MSG::DEBUG << "Reading Event " << (*m_recId) << endreq;
00302
00303 if((*m_recId) > m_evtMax) {
00304 return StatusCode::FAILURE;
00305 }
00306 }else {
00307
00308 log << MSG::FATAL << "Cannot iterate a dead iterator" << endreq;
00309 return StatusCode::FAILURE;
00310 }
00311
00312 log << MSG::DEBUG << "Reading Event " << (*m_recId) << endreq;
00313 log << MSG::DEBUG << "Max Event " << m_evtMax << endreq;
00314 return StatusCode::SUCCESS;
00315 }
00316
00317 StatusCode RootEvtSelector::queryInterface(const InterfaceID& riid, void** ppvInterface) {
00318 if ( riid == IID_IRootEvtSelector ) {
00319 *ppvInterface = (RootEvtSelector*)this;
00320 }
00321 else if ( riid == IEvtSelector::interfaceID() ) {
00322 *ppvInterface = (IEvtSelector*)this;
00323 }
00324 else if ( riid == IProperty::interfaceID() ) {
00325 *ppvInterface = (IProperty*)this;
00326 }
00327 else {
00328 return Service::queryInterface( riid, ppvInterface );
00329 }
00330 addRef();
00331 return SUCCESS;
00332 }
00333
00334 StatusCode RootEvtSelector::getMaxEvent() {
00335 IProperty* appPropMgr=0;
00336 StatusCode status =
00337 serviceLocator()->getService("ApplicationMgr", IProperty::interfaceID(),
00338 reinterpret_cast<IInterface*&>( appPropMgr ));
00339 if( status.isFailure() ) return status;
00340
00341 IntegerProperty evtMax("EvtMax",0);
00342 status = appPropMgr->getProperty( &evtMax );
00343 if (status.isFailure()) return status;
00344
00345 m_evtMax = evtMax.value();
00346 return status;
00347 }
00348
00349
00350
00351
00352
00353
00354