27#include "Gaudi/Property.h"
30#include "GaudiKernel/FileIncident.h"
31#include "GaudiKernel/IIoComponentMgr.h"
32#include "GaudiKernel/ISvcLocator.h"
33#include "GaudiKernel/ITHistSvc.h"
34#include "GaudiKernel/MsgStream.h"
35#include "GaudiKernel/StatusCode.h"
36#include "GaudiKernel/System.h"
37#include "GaudiKernel/IClassIDSvc.h"
55#include "EventInfo/EventInfo.h"
68#include "GaudiKernel/ServiceHandle.h"
95 const std::vector<std::string>&
files()
const {
return m_evtsel->m_inputCollectionsName.value(); }
113 ISvcLocator* svcLoc ) :
114 base_class ( name, svcLoc ),
116 m_imetaStore(
"StoreGateSvc/InputMetaDataStore", name ),
122 m_ppSvc (
"ProxyProviderSvc" , name ),
135 declareProperty(
"SkipEvents",
m_skipEvts = 0,
"Number of events to skip at the beginning" );
136 declareProperty(
"AccessMode",
m_accessMode = -1,
"-1 = use TEvent Default; 0 = BranchAccess; 1 = ClassAccess; 2 = AthenaAccess" );
138 declareProperty(
"FillEventInfo",
m_fillEventInfo=
false,
"If True, will fill old EDM EventInfo with xAOD::EventInfo content, necessary for database reading (IOVDbSvc)");
140 declareProperty(
"PrintPerfStats",
m_printPerfStats=
false,
"If True, at end of job will print the xAOD perf stats");
143 declareProperty(
"EvtStore",
m_dataStore,
"Store where to publish data");
144 declareProperty(
"ProxyProviderSvc" ,
m_ppSvc ,
"The ProxyProviderSvc that we should register ourself in and connect the EvtStore to");
145 declareProperty(
"InputMetaStore",
m_imetaStore,
"Store where to publish (input) metadata");
146 declareProperty(
"MetaStore",
m_ometaStore,
"Store where to publish (output) metadata");
147 declareProperty(
"TreeName",
m_tupleName =
"CollectionTree",
"Name of the TTree to load/read from input file(s)" );
148 declareProperty(
"MetaDataTreeName",
m_metadataName =
"MetaData",
"Name of the TTree to load/read metadata from input file(s)" );
150 declareProperty(
"ReadMetaDataWithPool",
m_readMetadataWithPool=
false,
"If true, using POOL to read metadata, will ensure input file is registered with catalog");
154 declareProperty(
"CollectionType",
m_collectionType=
"",
"Compability flag for RecExCommon");
172 ATH_MSG_ERROR(
"You have to give a TTree name to read from the ROOT files !");
173 return StatusCode::FAILURE;
177 if ( nbrInputFiles < 1 ) {
179 <<
"(Got [" << nbrInputFiles <<
"] file instead !)");
180 return StatusCode::FAILURE;
182 ATH_MSG_INFO(
"Selector configured to read [" << nbrInputFiles <<
"] file(s)...");
186 int old_level = gErrorIgnoreLevel;
187 gErrorIgnoreLevel = kWarning;
189 gErrorIgnoreLevel = old_level;
191 if(serviceLocator()->existsService(
"AthROOTErrorHandlerSvc")) {
193 CHECK( ehSvc.retrieve() );
194 CHECK( ehSvc->initialize() );
195 CHECK( ehSvc.release() );
198 case -1:
ATH_MSG_INFO(
"Using DEFAULT xAOD access mode (usually same as CLASS mode)");
break;
199 case 0:
ATH_MSG_INFO(
"Using BRANCH xAOD access mode");
break;
200 case 1:
ATH_MSG_INFO(
"Using CLASS xAOD access mode");
break;
201 case 2:
ATH_MSG_INFO(
"Using ATHENA xAOD access mode");
break;
215 return StatusCode::FAILURE;
217 if(!f->Get(
"##Shapes")) {
218 ATH_MSG_INFO(
"First file is not POOL file (e.g. is CxAOD), so reading metadata with xAOD::TEvent instead");
228 if (!iomgr.retrieve().isSuccess()) {
230 return StatusCode::FAILURE;
232 if (!iomgr->io_register(
this).isSuccess()) {
233 ATH_MSG_FATAL(
"Could not register myself with the IoComponentMgr !");
234 return StatusCode::FAILURE;
239 for (std::size_t icol = 0,
imax = incol.size(); icol <
imax; icol++) {
240 if (!iomgr->io_register(
this,
241 IIoComponentMgr::IoMode::READ,
242 incol[icol]).isSuccess()) {
243 ATH_MSG_FATAL(
"could not register [" << incol[icol] <<
"] for output !");
246 ATH_MSG_VERBOSE(
"io_register[" << this->name() <<
"](" << incol[icol] <<
") [ok]");
250 return StatusCode::FAILURE;
255 return StatusCode::FAILURE;
287 std::vector<std::string> propVal;
289 bool foundSvc(
false);
bool foundPoolSvc(
false);
290 for(
const std::string& s : propVal) {
291 if(s==
"Athena::xAODCnvSvc") { foundSvc=
true; }
292 if(s==
"AthenaPoolCnvSvc") { foundPoolSvc=
true; }
294 if(!foundSvc) propVal.push_back(
"Athena::xAODCnvSvc");
298 CHECK( epSvc->setProperty(
"CnvServices", Gaudi::Utils::toString( propVal ) ));
307 std::vector<std::string> propVal;
308 IProperty* prop =
dynamic_cast<IProperty*
>(&*
m_ppSvc);
310 return StatusCode::FAILURE;
313 bool foundSvc(
false);
314 for(
const std::string& s : propVal) {
315 if(s==
"MetaDataSvc") { foundSvc=
true;
break; }
318 propVal.push_back(
"MetaDataSvc");
319 IProperty* prop =
dynamic_cast<IProperty*
>(&*
m_ppSvc);
321 return StatusCode::FAILURE;
323 CHECK( prop->setProperty(
"ProviderNames", Gaudi::Utils::toString( propVal ) ));
351 return StatusCode::SUCCESS;
359 return StatusCode::SUCCESS;
375 return StatusCode::SUCCESS;
393 throw GaudiException(
"xAODEventSelector::next() - Unable to get xAODEventContext",
"xAODEventSelector",StatusCode::FAILURE);
406 auto& fnames = rctx->
files();
408 if( rctx->setFile(
"").isFailure() ) {
409 throw GaudiException(
"xAODEventSelector::next() - Fatal error when trying to setFile('')",
"xAODEventSelector",StatusCode::FAILURE);
415 if( rctx->setFile( fname ).isFailure() ) {
416 throw GaudiException(
"xAODEventSelector::next() - Fatal error when trying to setFile('" + fname +
"')",
"xAODEventSelector",StatusCode::FAILURE);
421 rctx->
file()->GetName(), {},
422 rctx->
file()->GetName());
425 return StatusCode::FAILURE;
439 int64_t global_entry = rctx->
entry();
440 int64_t
entry = global_entry;
454 if(
m_tevent->getEntry( entry ) < 0 ) {
455 ATH_MSG_ERROR(
"Failed to load entry " <<
static_cast< int >( entry ) );
456 throw GaudiException(
"xAODEventSelector::next() - xAOD::TEvent::getEntry returned less than 0 bytes",
"xAODEventSelector",StatusCode::FAILURE);
468 if(
m_tevent->retrieve( xaodEventInfo ,
"EventInfo").isFailure()) {
470 throw GaudiException(
"xAODEventSelector::next() - Could not find xAOD::EventInfo",
"xAODEventSelector",StatusCode::FAILURE);
473 EventType* evtType =
new EventType;
477 if ( !
m_dataStore->record( evtInfo,
"EventInfo" ).isSuccess() ) {
479 delete evtInfo; evtInfo = 0;
480 throw GaudiException(
"xAODEventSelector::next() - Could not record EventInfo",
"xAODEventSelector",StatusCode::FAILURE);
483 return StatusCode::SUCCESS;
512 iaddr.second =
false;
513 SG::TransientAddress* taddr = iaddr.first;
516 const bool forceRemove =
false;
521 CHECK( rctx->setFile(
"") );
536 return StatusCode::FAILURE;
544 return next( ctx, -1 );
550 return next( ctx, -jump );
556 ATH_MSG_ERROR (
"............. Last Event Not Implemented .............");
557 return StatusCode::FAILURE;
572 return StatusCode::SUCCESS;
577 IOpaqueAddress*& )
const
580 return StatusCode::SUCCESS;
586 if(refCtxt==0)
return StatusCode::SUCCESS;
590 return StatusCode::SUCCESS;
593 return StatusCode::FAILURE;
599 ATH_MSG_ERROR (
"............. resetCriteria Not Implemented .............");
600 return StatusCode::FAILURE;
624 if (coll_idx == -1 && evtnum <
m_curEvt) {
628 if (coll_idx == -1) {
630 return StatusCode::RECOVERABLE;
637 return StatusCode::FAILURE;
645 return StatusCode::SUCCESS;
666 if (!iomgr.retrieve().isSuccess()) {
668 return StatusCode::FAILURE;
670 if (!iomgr->io_hasitem(
this)) {
671 ATH_MSG_FATAL(
"IoComponentMgr does not know about myself !");
672 return StatusCode::FAILURE;
681 std::string &fname = inputCollections[i];
683 if (!iomgr->io_contains(
this, fname)) {
684 ATH_MSG_ERROR(
"IoComponentMgr does not know about [" << fname <<
"] !");
685 return StatusCode::FAILURE;
687 if (!iomgr->io_retrieve(
this, fname).isSuccess()) {
688 ATH_MSG_FATAL(
"Could not retrieve new value for [" << fname <<
"] !");
689 return StatusCode::FAILURE;
709 return StatusCode::FAILURE;
713 return StatusCode::SUCCESS;
727 return StatusCode::SUCCESS;
739 return StatusCode::SUCCESS;
745 const EventContext& )
751 return StatusCode::SUCCESS;
754 << tad->
name() <<
") NOT known to us.");
755 return StatusCode::FAILURE;
759 return StatusCode::SUCCESS;
776 return(StatusCode::SUCCESS);
781 return StatusCode::FAILURE;
795 std::set<std::string> missingAux;
797 for(
auto itr =
m_tevent->inputEventFormat()->begin(); itr!=
m_tevent->inputEventFormat()->end();++itr) {
798 if(inputTree->GetBranch(itr->second.branchName().c_str())==0)
continue;
799 ATH_MSG_VERBOSE(
"EFE:" << itr->first <<
" branchName = " << itr->second.branchName() <<
" className=" << itr->second.className());
801 if(
m_clidsvc->getIDOfTypeInfoName(itr->second.className(),
id).isFailure() &&
802 m_clidsvc->getIDOfTypeName(itr->second.className(),
id).isFailure()) {
804 if(itr->second.branchName().compare(itr->second.branchName().length()-4,4,
"Aux.")==0) {
805 missingAux.insert( itr->second.className() );
continue;
808 TString className = itr->second.className();
812 TClass *cls = TClass::GetClass(className);
814 const std::type_info *ti = cls->GetTypeInfo();
815 if(ti) className = System::typeinfoName(*ti);
818 if(
m_clidsvc->getIDOfTypeInfoName(className.Data(),
id).isFailure() &&
819 m_clidsvc->getIDOfTypeName(className.Data(),
id).isFailure()) {
820 ATH_MSG_WARNING(
"No CLID for class " << itr->second.className() <<
" , cannot read " << itr->second.branchName());
826 const std::string br_name = itr->second.branchName();
832 (
unsigned long)(value_ptr),
845 if (old->clID() ==
id &&
846 old->name() == br_name) {
849 old->setAddress(addr);
851 iaddr->second =
true;
861 tads.push_back(taddr);
871 if(missingAux.size()) {
872 std::string allAux;
for(
auto& s : missingAux) allAux += s +
", ";
873 ATH_MSG_WARNING(
"The following AuxStore types are not directly accessible (missing CLID, possibly from schema evolution): " << allAux);
882 ATH_MSG_DEBUG(
"In xAODEventSelector::createRootBranchAddresses end ...");
884 return StatusCode::SUCCESS;
891 ATH_MSG_DEBUG(
"In xAODEventSelector::createMetaDataRootBranchAddresses start ...");
895 throw "xAODEventSelector: Unable to get m_tfile";
903 if (!
tree) std::abort();
904 TObjArray *leaves =
tree->GetListOfLeaves();
907 return StatusCode::SUCCESS;
911 for (Int_t i = 0; i < leaves->GetEntries(); ++i) {
912 TLeaf *leaf = (TLeaf *)leaves->At(i);
913 TBranch *branch = leaf->GetBranch();
918 const std::string type_name = leaf->GetTypeName();
919 const std::string br_name = branch->GetName();
921 const std::string toCheck =
"xAOD::";
922 if (type_name.find(toCheck) == std::string::npos) {
923 ATH_MSG_DEBUG(
"** Skip type-name = " << type_name <<
", br_name = " << br_name );
926 const std::string sg_key = br_name;
927 TClass *cls = TClass::GetClass(type_name.c_str());
928 const std::type_info *ti = 0;
932 if( br_name ==
"EventFormat" )
continue;
935 ti = cls->GetTypeInfo();
951 << type_name <<
"]");
957 ATH_MSG_DEBUG(
"id = " <<
id <<
", m_metadataName.value() = " <<
m_metadataName.value() <<
", br_name = " << br_name <<
", value_ptr = " << value_ptr);
963 (
unsigned long)(value_ptr),
964 (
unsigned long)(1)));
965 if (!
m_imetaStore->recordAddress(br_name, std::move(addr),
true).isSuccess()) {
966 ATH_MSG_ERROR(
"could not record address at [" << br_name <<
"] in store ["
978 ATH_MSG_DEBUG(
"In xAODEventSelector::createMetaDataRootBranchAddresses end ...");
980 return StatusCode::SUCCESS;
988 if(fname.empty())
return file;
991 file = (TFile*)gROOT->GetListOfFiles()->FindObject(fname.c_str());
994 file = TFile::Open(fname.c_str(),
"READ");
995 if (
file)
file->SetName(fname.c_str());
999 ATH_MSG_ERROR(
"could not open next file in input collection ["
1013 if(!newFile && !fname.empty()) {
1014 ATH_MSG_FATAL(
"xAODEventSelector: Unable to fetch Ntuple: " << fname);
1015 return StatusCode::FAILURE;
1019 const std::string currFile =
m_tfile->GetName();
1021 m_poolSvc->disconnectDb(
"PFN:"+currFile).ignore();
1029 if(!
m_tfile)
return StatusCode::SUCCESS;
1032 ATH_MSG_FATAL(
"xAODEventSelector: TEvent cannot read " << fname);
1033 return StatusCode::FAILURE;
1041 return StatusCode::FAILURE;
1054 }
catch (std::exception &e) {
1057 if (
sc.isRecoverable() || collPtr ==
nullptr) {
1069 return StatusCode::SUCCESS;
1082 zero.min_entries = -1;
1083 zero.max_entries = -1;
1103 return StatusCode::SUCCESS;
1131 throw "xAODEventSelector: Unable to fetch ntuple";
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_WARNING(x,...)
#define ATH_MSG_VERBOSE(x,...)
#define ATH_MSG_INFO(x,...)
#define ATH_MSG_FATAL(x,...)
a static registry of CLID->typeName entries.
macros to associate a CLID to a type
#define CHECK(...)
Evaluate an expression and check for errors.
This class provides a unique identification for each event, in terms of run/event number and/or a tim...
This class provides general information about an event.
uint32_t CLID
The Class ID type.
Include TBranchElement.h, suppressing clang warnings.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
state of a few global resources from ROOT and restores their initial value upon d-tor call.
A simple class to do the retrieve from TEvent.
event selector context ... just holds reference back to the selector
int64_t entry() const
access to the current event entry number
StatusCode setFile ATLAS_NOT_THREAD_SAFE(const std::string &fname)
call to setFile on evtSel
std::size_t fileIndex() const
access to the file iterator
virtual void * identifier() const override
identifier method required by IEvtSelector::Context
const std::vector< std::string > & files() const
access to the container of files
xAODEventContext(const xAODEventSelector *sel)
standard c-tor with initialization
const xAODEventSelector * m_evtsel
reference to the hosting event selector instance
const TFile * file() const
access to the current file
virtual ~xAODEventContext()
standard d-tor
Class implementing the GAUDI IEvtSelector interface using ROOT TTree as a backend.
ServiceHandle< StoreGateSvc > m_ometaStore
Pointer to the StoreGateSvc output metadata store.
std::optional< InputFileIncidentGuard > m_inputFileGuard
RAII guard: guarantees a matching EndInputFile for every BeginInputFile.
virtual StatusCode updateAddress(StoreID::type storeID, SG::TransientAddress *tad, const EventContext &ctx) override
update a transient Address
virtual int size(Context &refCtxt) const override
ICollectionSize interface
StringProperty m_tupleName
Name of TTree to load from collection of input files.
virtual StatusCode next(Context &refCtxt) const override
xAODEventSelector(const std::string &name, ISvcLocator *svcLoc)
Constructor with parameters:
long m_nbrEvts
Number of Events read so far.
ServiceHandle< IDictLoaderSvc > m_dictsvc
Pointer to the IDictLoaderSvc.
TFile * m_tfile
current TFile being read
virtual StatusCode resetCriteria(const std::string &cr, Context &ctx) const override
StringArrayProperty m_inputCollectionsName
List of input files containing TTree.
StatusCode createRootBranchAddresses(StoreID::type storeID, tadList &tads)
helper method to create proxies
int find_coll_idx(int evtidx) const
helper method to get the collection index (into m_inputCollectionsName) for a given event index evtid...
StatusCode createMetaDataRootBranchAddresses() const
helper method to create proxies for the metadata store
bool m_readMetadataWithPool
StringProperty m_collectionType
virtual StatusCode releaseContext(Context *&refCtxt) const override
friend class Athena::xAODEventContext
StringProperty m_metadataName
Name of TTree to load from metadata of input files.
xAOD::xAODTEvent * m_tevent
current TEvent being read
xAODEventSelector * self() const
non-const access to self (b/c next() is const)
std::vector< CollMetaData > m_collEvts
cache of the number of entries for each collection
StatusCode do_init_io()
helper method to init the i/o components
virtual StatusCode initialize() override
virtual StatusCode io_reinit() override
Callback method to reinitialize the internal state of the component for I/O purposes (e....
ServiceHandle< IProxyProviderSvc > m_ppSvc
Handle to the proxy provider svc.
virtual StatusCode loadAddresses(StoreID::type storeID, tadList &list) override
get all new addresses from Provider for this Event.
ServiceHandle< StoreGateSvc > m_dataStore
Pointer to the StoreGateSvc event store.
bool m_printEventProxyWarnings
StatusCode setFile(const std::string &fname)
switch to given file, loading metadata and triggering a beginInputFile incident
virtual StatusCode previous(Context &refCtxt) const override
std::unordered_map< SG::TransientAddress *, bool > m_rootAddresses
virtual int curEvent(const Context &refCtxt) const override
return the current event number.
ServiceHandle< IIncidentSvc > m_incsvc
Handle to the incident service.
virtual StatusCode createAddress(const Context &refCtxt, IOpaqueAddress *&) const override
virtual StatusCode finalize() override
ServiceHandle< IClassIDSvc > m_clidsvc
Pointer to the IClassIDSvc.
virtual StatusCode last(Context &refContext) const override
virtual StatusCode seek(Context &refCtxt, int evtnum) const override
Seek to a given event number.
TFile * fetchNtupleFile(const std::string &fname) const
helper method to retrieve the correct tuple
long m_curEvt
current event index
ServiceHandle< IPoolSvc > m_poolSvc
Handle to the PoolSvc (used in Hybrid mode when user is reading metadata with pool).
virtual StatusCode createContext(Context *&refpCtxt) const override
virtual ~xAODEventSelector()
Destructor:
long m_skipEvts
Number of events to skip at the beginning.
virtual StatusCode rewind(Context &refCtxt) const override
long m_collIdx
current collection index (into m_inputCollectionsName)
virtual void handle(const Incident &incident) override
virtual StatusCode stop() override
virtual StatusCode preLoadAddresses(StoreID::type storeID, tadList &list) override
ServiceHandle< StoreGateSvc > m_imetaStore
Pointer to the StoreGateSvc input metadata store.
static CLID typeinfoToCLID(const std::type_info &ti)
Return the CLID corresponding to a type_info.
Simple smart pointer for Gaudi-style refcounted objects.
CLID clID() const
Retrieve string key:
void clearAddress(const bool &flag)
Return the clearAddress flag.
const std::string & name() const
Get the primary (hashed) SG key.
void setAddress(CxxUtils::RefCountedPtr< IOpaqueAddress > pAddress)
Retrieve primary clid.
void setProvider(IAddressProvider *provider, StoreID::type storeID)
A service for creating, accessing and managing an ensemble of collections of event references.
An interface to a storage technology specific collection of event references and attributes.
uint32_t lumiBlock() const
The current event's luminosity block number.
uint32_t bcid() const
The bunch crossing ID of the event.
uint32_t timeStamp() const
POSIX time in seconds from 1970. January 1st.
uint32_t runNumber() const
The current event's run number.
uint32_t timeStampNSOffset() const
Nanosecond time offset wrt. the time stamp.
uint64_t eventNumber() const
The current event's event number.
ReadStats & stats()
Access the object belonging to the current thread.
static IOStats & instance()
Singleton object accessor.
void stop()
Stop the statistics collection.
void start(bool clear=true)
Start the statistics collection.
static PerfStats & instance()
Function accessing the singleton instance.
void Print(::Option_t *option="") const
Print information about the collected statistics.
EAuxMode
Auxiliary store "mode".
void zero(TH2 *h)
zero the contents of a 2d histogram
Some weak symbol referencing magic... These are declared in AthenaKernel/getMessageSvc....
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
static const DbType ROOT_StorageType
static const DbType POOL_StorageType
StatusCode Init(const char *appname)
Function initialising ROOT/PyROOT for using the ATLAS EDM.
EventInfo_v1 EventInfo
Definition of the latest event info version.