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