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