BOSS 7.0.4
BESIII Offline Software System
Loading...
Searching...
No Matches
RootEvtSelector.cxx
Go to the documentation of this file.
1// File and Version Information:
2// $Header: /bes/bes/BossCvs/Event/RootCnvSvc/src/RootEvtSelector.cxx,v 1.20 2015/06/17 03:12:16 dengzy Exp $
3//
4// Description:
5#include "GaudiKernel/MsgStream.h"
6#include "GaudiKernel/StatusCode.h"
7#include "GaudiKernel/SvcFactory.h"
8#include "GaudiKernel/IDataManagerSvc.h"
9#include "GaudiKernel/IAddressCreator.h"
10#include "GaudiKernel/GenericAddress.h"
11#include "RootCnvSvc/RootEvtSelector.h"
12#include "RootCnvSvc/RootInterface.h"
13#include "RootCnvSvc/RootEventContext.h"
14#include "EventModel/EventModel.h"
15#include "TagFilterSvc/ITagFilterSvc.h"
16#include "TagFilterSvc/TagFilterSvc.h"
17
18#include "GaudiKernel/SmartIF.h"
19#include "GaudiKernel/Bootstrap.h"
20#include "GaudiKernel/IAppMgrUI.h"
21#include "GaudiKernel/IProperty.h"
22#include "GaudiKernel/ISvcLocator.h"
23
24#include "TFile.h"
25#include "TTree.h"
26#include "TFormula.h"
27#include <fstream>
28#include <vector>
29
30extern const CLID& CLID_Event;
31extern const long int ROOT_StorageType;
32
33//static const SvcFactory<RootEvtSelector> s_factory;
34//const ISvcFactory& RootEvtSelectorFactory = s_factory;
35
36// Constructor
37RootEvtSelector::RootEvtSelector( const std::string& name, ISvcLocator* svcloc )
38 : Service( name, svcloc),
39 p_beginContext(0),
40 p_endContext(0) {
41 m_recId = new int;
42 (*m_recId) = -1;
43 declareProperty( "Input", m_jobInput="ROOT");
44 getMaxEvent();
45}
47 delete m_recId;
48}
49// Instantiation of a static factory class used by clients to create
50// instances of this service
52 MsgStream log(msgSvc(), name());
53 StatusCode sc = Service::initialize();
54 log << MSG::DEBUG << "RootEvtSelector::initialize" << endreq; if( sc.isSuccess() ) {
55 setProperties();
56 } else {
57 log << MSG::ERROR << "Unable to initialize service " << endreq;
58 }
59
60 if(m_jobInput.value() != " "){
61 sc = setCriteria(m_jobInput);
62 if(sc.isFailure()){
63 log << MSG::ERROR << "Unable to get input value" << endreq;
64 return sc;
65 }
66 }
67
68 // Retrieve conversion service handling event iteration
69 sc = serviceLocator()->service("EventCnvSvc", m_addrCreator);
70 if( !sc.isSuccess() ) {
71 log << MSG::ERROR <<
72 "Unable to localize interface IID_IAddressCreator from service:"
73 << "RootEventSelector"
74 << endreq;
75 return sc;
76 }
77
78 // Get DataSvc
79 IDataManagerSvc* eds = 0;
80 sc = serviceLocator()->service("EventDataSvc", eds, true);
81 if( !sc.isSuccess() ) {
82 log << MSG::ERROR
83 << "Unable to localize interface IID_IDataManagerSvc "
84 << "from RootEventSelector"
85 << endreq;
86 return sc;
87 }
88
89 m_rootCLID = eds->rootCLID();
90
91 // provide access to the Root Interface
92 m_rootInterface=RootInterface::Instance(log);
93 log << MSG::INFO << "currentFile in RootEvtSelector::initialize "<<m_rootInterface->getCurrentFileName()<<endreq;
94 if (!m_rootInterface) log << MSG::ERROR << "Unable to start Root service within RootCnvSvc" << endreq;
95
96 // create root event context
97 p_beginContext = new RootEventContext(this);
98
99
100 ITagFilterSvc *tmpSvc;
101
102 ISvcLocator* svcLocator = Gaudi::svcLocator();
103 StatusCode status = svcLocator->service("TagFilterSvc",tmpSvc);
104 if (status.isSuccess()) {
105 log << MSG::INFO << "get the TagFilterSvc" << endreq;
106 m_tagFilterSvc=dynamic_cast<TagFilterSvc *>(tmpSvc);
107 }else {
108 log << MSG::WARNING << "could not get the TagFilterSvc. Ignore it." << endreq;
109 }
110
111 m_rootInterface->setTagInputFile(m_tagFilterSvc->getTagFiles());
112
113 vector<string> dstFiles = m_tagFilterSvc->getDstFiles();
114 int size = dstFiles.size();
115 std::string treeName="Event";
116
117 for(int i=0;i<size;i++)
118 {
119 std::cout<<"RootEvtSelector, dstFile: "<<dstFiles[i]<<std::endl;
120 m_rootInterface->addInput(treeName, dstFiles[i].c_str());
121 }
122 return sc;
123}
124
125StatusCode RootEvtSelector::setCriteria( const std::string& criteria ) {
126 // Purpose and Method: set the input criteria
127 MsgStream log(msgSvc(), name());
128 m_criteria = criteria;
129
130 if( criteria == "ROOT" ) {
131 log << MSG::INFO << "RootEvtSelector input has been set to ROOT" << endreq;
132 m_criteriaType = ROOT;
133 }else if( criteria == "NONE"){
134 m_criteriaType = NONE;
135 log << MSG::INFO << "RootEvtSelector input has been set to NONE" << endreq;
136 }
137 else {
138 log << MSG::ERROR << "Invalid Event Selection Criteria: " << criteria << endreq;
139 return StatusCode::FAILURE;
140 }
141 return StatusCode::SUCCESS;
142}
143
144StatusCode
145RootEvtSelector::createContext(IEvtSelector::Context*& it) const
146{
147 it = p_beginContext;
148 return(StatusCode::SUCCESS);
149}
150
151// Implementation of IEvtSelector::next().
152StatusCode RootEvtSelector::next(IEvtSelector::Context& it) const
153{
154 return this->next();
155}
156
157//__________________________________________________________________________
158StatusCode RootEvtSelector::next(IEvtSelector::Context& ctxt, int jump) const {
159 if ( jump > 0 ) {
160 for ( int i = 0; i < jump; ++i ) {
161 StatusCode status = next(ctxt);
162 if ( !status.isSuccess() ) {
163 return status;
164 }
165 }
166 return StatusCode::SUCCESS;
167 }
168 return StatusCode::FAILURE;
169}
170
171StatusCode RootEvtSelector::previous(IEvtSelector::Context& /*it*/) const {
172 MsgStream log(messageService(), name());
173 log << MSG::ERROR << "RootEvtSelector::previous() not implemented" << endreq;
174 return(StatusCode::FAILURE);
175}
176
177StatusCode RootEvtSelector::previous(IEvtSelector::Context& it,int /*jump*/) const {
178 return(previous(it));
179}
180
181StatusCode RootEvtSelector::last(IEvtSelector::Context& it)const {
182 if (it.identifier() == p_endContext->identifier()) {
183 MsgStream log(messageService(), name());
184 log << MSG::DEBUG << "last(): Last event in InputStream." <<endreq;
185 return(StatusCode::SUCCESS);
186 }
187 return (StatusCode::FAILURE);
188}
189
190StatusCode RootEvtSelector::resetCriteria(const std::string& /*criteria*/, IEvtSelector::Context& /*ctxt*/) const {
191 return(StatusCode::SUCCESS);
192}
193
194
195StatusCode RootEvtSelector::rewind(IEvtSelector::Context& /*it*/) const {
196 MsgStream log(messageService(), name());
197 log << MSG::ERROR << "RootEvtSelector::rewind() not implemented" << endreq;
198 return(StatusCode::FAILURE);
199}
200
201StatusCode
202RootEvtSelector::releaseContext(IEvtSelector::Context*& /*it*/)const {
203 return(StatusCode::SUCCESS);
204}
205
206StatusCode RootEvtSelector::createAddress(const IEvtSelector::Context& /*it*/,
207 IOpaqueAddress*& iop) const {
208 iop = new GenericAddress(ROOT_StorageType, CLID_Event, " ", " ", 0);
209 return(StatusCode::SUCCESS);
210}
211
213 bool flag=true;;
214 MsgStream log(messageService(), name());
215 static std::string tempFile = "";
216 log << MSG::DEBUG <<"current file: "<<m_rootInterface->getCurrentFileName()<<endreq;
217 std::ifstream fin;
218 static std::string tagFile;
219 static int i=-1;
220 static std::vector<int> ventry;
221 int num1 = m_rootInterface->getCurrentFileNum();
222 int num2 = m_rootInterface->getTotalFileNum();
223
224
225 if(tempFile != m_rootInterface->getCurrentFileName())
226 {
227 tempFile = m_rootInterface->getCurrentFileName();
228 log << MSG::DEBUG<<"temp file: "<<tempFile<<endreq;
229 i=-1;
230 ventry.clear();
231 log << MSG::DEBUG<<"num1: "<<num1<<" num2: "<<num2<<endreq;
232 tagFile = (m_rootInterface->getTagInputFile())[num1];
233 log << MSG::DEBUG <<"tag file: "<<tagFile<<endreq;
234 m_tagFilterSvc->getVEntry(tagFile, ventry);
235 } //end of read a tag file
236
237 i++;
238 log << MSG::DEBUG<<"ventry i: "<<i<<endreq;
239 log << MSG::DEBUG<<"ventry size: "<<ventry.size()<<endreq;
240 if(i<ventry.size())
241 {
242 (*m_recId)=ventry[i];
243 log << MSG::DEBUG<<"ventry[i]: "<<ventry[i]<<endreq;
244 }
245 else
246 (*m_recId)= -1;
247
248 log << MSG::DEBUG<<"(*m_recId): "<<(*m_recId)<<endreq;
249
250 int vs = ventry.size();
251
252 if( (i>(vs-1)) && (num1<(num2-1)))
253 {
254 bool endAll = m_rootInterface->checkEndOfTree();
256 }
257 if( (i>(vs-1)) && (num1==(num2-1)))
258 (*m_recId)= -1;
259}
260
261
262StatusCode RootEvtSelector::next() const {
263 MsgStream log(msgSvc(), name());
264 if(m_criteriaType == ROOT)
265 {
266 log<<MSG::INFO<<"m_criteriaType == ROOT"<<endreq;
267 if(m_rootInterface->getSelectFromTag()!=0)
268 {
270 if((*m_recId)<0)
271 return StatusCode::FAILURE;
272 }
273
274 else
275 {
276 (*m_recId)++;
277
278 //if((*m_recId)>m_evtMax){ // if get the max event number?
279 // return StatusCode::FAILURE;
280 //}else{ //didn't get the max event number
281
282 static int entry = 0;
283
284 bool endOfFile = m_rootInterface->getEOF();
285 entry++;
286 bool maxEntry = (entry >m_rootInterface->getEntries() && m_rootInterface->getEntries()>=0);
287 if(maxEntry) entry =1;
288 log<<MSG::INFO<<"entry = "<<entry<<" "<<maxEntry<<" "<<endOfFile<<endreq;
289 if(endOfFile || maxEntry){ //the using file is get max event or get the end of file
290 bool endAll = m_rootInterface->checkEndOfTree();
291
292 if(endAll){ //if all is finished ?
293 return StatusCode::FAILURE;
294 }
295 }
296 }
297 //} // to make it run if not set event number from jobOption
298 }// end if(m_criteriaType == ROOT)
299 else if( m_criteriaType == NONE)
300 {
301 log << MSG::DEBUG << "Reading Event " << (*m_recId) << endreq;
302 //If we go over the count set equal to the end
303 if((*m_recId) > m_evtMax) {
304 return StatusCode::FAILURE;
305 }
306 }else {
307 //We are going to blow up if this situation happens
308 log << MSG::FATAL << "Cannot iterate a dead iterator" << endreq;
309 return StatusCode::FAILURE;
310 }
311
312 log << MSG::DEBUG << "Reading Event " << (*m_recId) << endreq;
313 log << MSG::DEBUG << "Max Event " << m_evtMax << endreq;
314 return StatusCode::SUCCESS;
315}
316
317StatusCode RootEvtSelector::queryInterface(const InterfaceID& riid, void** ppvInterface) {
318 if ( riid == IID_IRootEvtSelector ) {
319 *ppvInterface = (RootEvtSelector*)this;
320 }
321 else if ( riid == IEvtSelector::interfaceID() ) {
322 *ppvInterface = (IEvtSelector*)this;
323 }
324 else if ( riid == IProperty::interfaceID() ) {
325 *ppvInterface = (IProperty*)this;
326 }
327 else {
328 return Service::queryInterface( riid, ppvInterface );
329 }
330 addRef();
331 return SUCCESS;
332}
333
334StatusCode RootEvtSelector::getMaxEvent() {
335 IProperty* appPropMgr=0;
336 StatusCode status =
337 serviceLocator()->getService("ApplicationMgr", IProperty::interfaceID(),
338 reinterpret_cast<IInterface*&>( appPropMgr ));
339 if( status.isFailure() ) return status;
340
341 IntegerProperty evtMax("EvtMax",0);
342 status = appPropMgr->getProperty( &evtMax );
343 if (status.isFailure()) return status;
344
345 m_evtMax = evtMax.value();
346 return status;
347}
348
349
350
351
352
353
354
const long int ROOT_StorageType
const CLID & CLID_Event
Definition: EventModel.cxx:193
const CLID & CLID_Event
Definition: EventModel.cxx:193
const long int ROOT_StorageType
This class provides the Context for RootEventSelector.
virtual void * identifier() const
Inequality operator.
RootEvtSelector performs the function of controlling the ApplicationMgr loop.
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvInterface)
virtual StatusCode setCriteria(const std::string &criteria)
virtual StatusCode rewind(Context &it) const
virtual StatusCode resetCriteria(const std::string &criteria, Context &context) const
StatusCode next() const
virtual StatusCode initialize()
virtual StatusCode last(Context &it) const
virtual StatusCode releaseContext(Context *&it) const
void getEntryFromTag() const
virtual StatusCode previous(Context &it) const
virtual StatusCode createContext(Context *&it) const
virtual StatusCode createAddress(const Context &it, IOpaqueAddress *&iop) const
RootEvtSelector(const std::string &name, ISvcLocator *svcloc)
virtual bool checkEndOfTree()
check if all the files is over 2005-11-28
virtual void setTagInputFile(std::vector< std::string > input)
virtual StatusCode addInput(const std::string &treename, const std::string &file)
add input tree to the list
static RootInterface * Instance(MsgStream log)
singleton behaviour
virtual std::vector< std::string > getTagInputFile()
vector< string > getDstFiles()
StatusCode getVEntry(string, vector< int > &)