ATLAS Offline Software
Loading...
Searching...
No Matches
Athena::RootNtupleEventSelector Class Reference

Class implementing the GAUDI IEvtSelector interface using ROOT TTree as a backend. More...

#include <RootNtupleEventSelector.h>

Inheritance diagram for Athena::RootNtupleEventSelector:
Collaboration diagram for Athena::RootNtupleEventSelector:

Public Member Functions

 RootNtupleEventSelector (const std::string &name, ISvcLocator *svcLoc)
 Constructor with parameters:
virtual ~RootNtupleEventSelector ()
 Destructor:
virtual StatusCode initialize () override
virtual StatusCode stop () override
virtual StatusCode finalize () override
virtual void handle (const Incident &incident) override
virtual StatusCode io_reinit () override
 Callback method to reinitialize the internal state of the component for I/O purposes (e.g.
virtual int size (Context &refCtxt) const override
 ICollectionSize interface
virtual StatusCode createContext (Context *&refpCtxt) const override
virtual StatusCode last (Context &refContext) const override
virtual StatusCode next (Context &refCtxt) const override
virtual StatusCode next (Context &refCtxt, int jump) const override
virtual StatusCode previous (Context &refCtxt) const override
virtual StatusCode previous (Context &refCtxt, int jump) const override
virtual StatusCode rewind (Context &refCtxt) const override
virtual StatusCode createAddress (const Context &refCtxt, IOpaqueAddress *&) const override
virtual StatusCode releaseContext (Context *&refCtxt) const override
virtual StatusCode resetCriteria (const std::string &cr, Context &ctx) const override
virtual StatusCode seek (Context &refCtxt, int evtnum) const override
 Seek to a given event number.
virtual int curEvent (const Context &refCtxt) const override
 return the current event number.
virtual StatusCode preLoadAddresses (StoreID::type storeID, tadList &list) override
virtual StatusCode loadAddresses (StoreID::type storeID, tadList &list) override
 get all new addresses from Provider for this Event.
virtual StatusCode updateAddress (StoreID::type storeID, SG::TransientAddress *tad, const EventContext &ctx) override
 update a transient Address

Private Member Functions

StatusCode endInputFile (RootNtupleEventContext *rctx) const
void setupInputCollection (Gaudi::Details::PropertyBase &inputCollectionsName)
 callback to synchronize the list of input files
StatusCode createRootBranchAddresses (StoreID::type storeID, tadList &tads)
 helper method to create proxies
TTree * fetchNtuple (const std::string &fname, const std::string &tupleName) const
 helper method to retrieve the correct tuple
void addMetadataFromDirectoryName (const std::string &metadirname, TFile *fileObj, const std::string &prefix="") const
void addMetadataFromDirectory (TDirectoryFile *metadir, const std::string &prefix="") const
void addMetadata (TTree *metatree, const std::string &path="") const
void addMetadata (TObjString *metastring, const std::string &path="") const
StatusCode createMetaDataRootBranchAddresses (StoreGateSvc *store, TTree *tree, const std::string &prefix) const
 helper method to create proxies for the metadata store
StatusCode do_init_io ()
 helper method to init the i/o components

Private Attributes

std::vector< std::vector< CollMetaData > > m_collEvts
 helper method to get the collection index (into m_inputCollectionsName) and tuple index (into m_tupleNames') for a given event index evtidx`.
TTree * m_tuple
 current tree being read
bool m_needReload
 The (python) selection function to apply on the TChain we are reading.
std::optional< InputFileIncidentGuardm_inputFileGuard
 RAII guard: guarantees a matching EndInputFile for every BeginInputFile.
bool m_fireBIF {true}
 Flag to fire BeginInputFile on the next BeginEvent incident.
std::vector< TFile * > m_files

Friends

class Athena::RootNtupleEventContext

Detailed Description

Class implementing the GAUDI IEvtSelector interface using ROOT TTree as a backend.

Definition at line 48 of file RootNtupleEventSelector.h.

Constructor & Destructor Documentation

◆ RootNtupleEventSelector()

Athena::RootNtupleEventSelector::RootNtupleEventSelector ( const std::string & name,
ISvcLocator * svcLoc )

Constructor with parameters:

Definition at line 222 of file RootNtupleEventSelector.cxx.

223 :
224 extends ( name, svcLoc ),
225 m_dataStore( "StoreGateSvc/StoreGateSvc", name ),
226 m_imetaStore( "StoreGateSvc/InputMetaDataStore", name ),
227 m_ometaStore( "StoreGateSvc/MetaDataStore", name ),
228 m_clidsvc ( "ClassIDSvc", name ),
229 m_dictsvc ( "AthDictLoaderSvc", name ),
230 m_incsvc ( "IncidentSvc", name ),
231 m_nbrEvts ( 0 ),
232 m_curEvt ( 0 ),
233 m_collEvts ( ),
234 m_tuple (NULL),
235 m_needReload (true)
236{
237 declareProperty( "DataStore",
238 m_dataStore,
239 "Store where to publish data");
240
241 declareProperty( "InputMetaStore",
242 m_imetaStore,
243 "Store where to publish (input) metadata");
244
245 declareProperty( "MetaStore",
246 m_ometaStore,
247 "Store where to publish (output) metadata");
248
249 declareProperty( "InputCollections",
250 m_inputCollectionsName,
251 "List of input (ROOT) file names" );
252 m_inputCollectionsName.declareUpdateHandler
254
255 declareProperty( "TupleName",
256 m_tupleName = "CollectionTree",
257 "Name of the TTree to load/read from input file(s). "
258 "May be a semicolon-separated string to read multiple TTrees.");
259
260 declareProperty( "SkipEvents",
261 m_skipEvts = 0,
262 "Number of events to skip at the beginning" );
263
264 declareProperty( "ActiveBranches",
265 m_activeBranchNames,
266 "List of branch names to activate" );
267}
void setupInputCollection(Gaudi::Details::PropertyBase &inputCollectionsName)
callback to synchronize the list of input files
TTree * m_tuple
current tree being read
std::vector< std::vector< CollMetaData > > m_collEvts
helper method to get the collection index (into m_inputCollectionsName) and tuple index (into m_tuple...
bool m_needReload
The (python) selection function to apply on the TChain we are reading.

◆ ~RootNtupleEventSelector()

Athena::RootNtupleEventSelector::~RootNtupleEventSelector ( )
virtual

Destructor:

Definition at line 271 of file RootNtupleEventSelector.cxx.

272{}

Member Function Documentation

◆ addMetadata() [1/2]

void Athena::RootNtupleEventSelector::addMetadata ( TObjString * metastring,
const std::string & path = "" ) const
private

Definition at line 1171 of file RootNtupleEventSelector.cxx.

1172{
1173 std::string *converted = new std::string(metastring->String());
1174 if (!m_imetaStore->record(converted, path).isSuccess()) {
1175 ATH_MSG_INFO("Could not create metadata for string [" << path << "]");
1176 }
1177}
#define ATH_MSG_INFO(x)

◆ addMetadata() [2/2]

void Athena::RootNtupleEventSelector::addMetadata ( TTree * metatree,
const std::string & path = "" ) const
private

Definition at line 1159 of file RootNtupleEventSelector.cxx.

1160{
1161 if (metatree->GetEntry(0) < 0) {
1162 ATH_MSG_INFO("Problem retrieving data from metadata-tree [" << path << "] !!");
1163 return;
1164 }
1165
1166 if (!createMetaDataRootBranchAddresses(m_imetaStore.get(), metatree, path).isSuccess()) {
1167 ATH_MSG_INFO("Could not create metadata for tree [" << path << "]");
1168 }
1169}
StatusCode createMetaDataRootBranchAddresses(StoreGateSvc *store, TTree *tree, const std::string &prefix) const
helper method to create proxies for the metadata store

◆ addMetadataFromDirectory()

void Athena::RootNtupleEventSelector::addMetadataFromDirectory ( TDirectoryFile * metadir,
const std::string & prefix = "" ) const
private

Definition at line 1117 of file RootNtupleEventSelector.cxx.

1118{
1119 std::unordered_set<std::string> meta_keys;
1120 const TList *keys = metadir->GetListOfKeys();
1121 for (Int_t i=0; i < keys->GetSize(); ++i) {
1122 TKey* key = dynamic_cast<TKey*>(keys->At(i));
1123 if (!key) {
1124 continue;
1125 }
1126
1127 const std::string meta_key = key->GetName();
1128 if (!meta_keys.emplace(key->GetName()).second) {
1129 // key was already in the set:
1130 // meta_key is another cycle from a previous key entry.
1131 // *ASSUME* the highest cycle is the one we are interested in
1132 // *AND* that it was the previous one...
1133 continue;
1134 }
1135
1136 std::string fullPrefix(prefix);
1137 if (prefix != "") fullPrefix += "/";
1138 const std::string path = fullPrefix + key->GetName();
1139
1140 TString fullKeyName(TString::Format("%s;%hi", key->GetName(), key->GetCycle()));
1141 TObject *objRef = metadir->Get(fullKeyName);
1142
1143 TTree *metatree = dynamic_cast<TTree*>(objRef);
1144 if (metatree) {
1145 addMetadata(metatree, path);
1146 continue;
1147 }
1148
1149 TObjString *metaObjString = dynamic_cast<TObjString*>(objRef);
1150 if (metaObjString) {
1151 addMetadata(metaObjString, path);
1152 continue;
1153 }
1154
1155 ATH_MSG_WARNING("Unsupported metadata type: " << objRef->ClassName());
1156 }
1157}
#define ATH_MSG_WARNING(x)
void addMetadata(TTree *metatree, const std::string &path="") const
path
python interpreter configuration --------------------------------------—
Definition athena.py:130

◆ addMetadataFromDirectoryName()

void Athena::RootNtupleEventSelector::addMetadataFromDirectoryName ( const std::string & metadirname,
TFile * fileObj,
const std::string & prefix = "" ) const
private

Definition at line 1110 of file RootNtupleEventSelector.cxx.

1111{
1112 TDirectoryFile *metadir = (TDirectoryFile*)fileObj->Get(metadirname.c_str());
1113 if (!metadir) return;
1114 addMetadataFromDirectory(metadir, prefix);
1115}
void addMetadataFromDirectory(TDirectoryFile *metadir, const std::string &prefix="") const

◆ createAddress()

StatusCode Athena::RootNtupleEventSelector::createAddress ( const Context & refCtxt,
IOpaqueAddress *&  ) const
overridevirtual

Definition at line 637 of file RootNtupleEventSelector.cxx.

639{
640 //std::cerr << "::TTES::createAddress()...\n";
641 return StatusCode::SUCCESS;
642}

◆ createContext()

StatusCode Athena::RootNtupleEventSelector::createContext ( Context *& refpCtxt) const
overridevirtual

IEvtSelector interface

Definition at line 629 of file RootNtupleEventSelector.cxx.

630{
632 refCtx = ctx;
633 return StatusCode::SUCCESS;
634}

◆ createMetaDataRootBranchAddresses()

StatusCode Athena::RootNtupleEventSelector::createMetaDataRootBranchAddresses ( StoreGateSvc * store,
TTree * tree,
const std::string & prefix ) const
private

helper method to create proxies for the metadata store

Definition at line 944 of file RootNtupleEventSelector.cxx.

947{
948 if (0 == store) {
949 ATH_MSG_ERROR("null pointer to store !");
950 return StatusCode::FAILURE;
951 }
952
953 if (0 == tree) {
954 ATH_MSG_ERROR("null pointer to n-tuple !");
955 return StatusCode::FAILURE;
956 }
957
958 // Record tree in Storegate for later writing
959 TransferTree* temp = new TransferTree(tree);
960 if (store->record(temp,prefix).isFailure()) ATH_MSG_ERROR("Unable to record metadata tree " << tree->GetName());
961
962 const std::string tree_name = tree->GetName();
963 TObjArray *branches = tree->GetListOfBranches();
964 if (!branches) {
965 ATH_MSG_INFO("no branches!!");
966 return StatusCode::SUCCESS;
967 }
968
969 // loop over branches
970 for (Int_t i = 0; i < branches->GetEntries(); ++i) {
971 TBranch *branch = (TBranch *)branches->At(i);
972 if (branch) {
973
974 CLID id = 0;
975 const void* value_ptr = tree;
976 const std::string type_name = branch->GetClassName();
977 const std::string br_name = branch->GetName();
978 const std::string sg_key = prefix.empty()
979 ? br_name
980 : prefix + "/" + br_name;
981 TClass *cls = NULL;
982 if (!type_name.empty()) {
983 cls = TClass::GetClass(type_name.c_str());
984 }
985 const std::type_info *ti = 0;
986
987 if (cls) {
988 ti = cls->GetTypeInfo();
989 // first, try to load a dict for that class...
990 if (ti) {
991 m_dictsvc->load_type(*ti);
992 }
993 if (!ti) {
994 ATH_MSG_WARNING("could not find a type-info for [" <<
995 type_name << "]");
996 continue;
997 }
998 std::string ti_typename = System::typeinfoName(*ti);
999 if (!m_clidsvc->getIDOfTypeInfoName(ti_typename, id)
1000 .isSuccess()) {
1001 // try another one...
1002 {
1003 // Protect against data race inside TClassEdit.
1004 // https://github.com/root-project/root/issues/10353
1005 // Should be fixed in root 6.26.02.
1006 R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
1007 ti_typename = TClassEdit::ShortType(ti_typename.c_str(),
1008 TClassEdit::kDropAllDefault);
1009 }
1010 if (!m_clidsvc->getIDOfTypeInfoName(ti_typename, id)
1011 .isSuccess()) {
1012 ATH_MSG_INFO("** could not find a CLID from type-info ["
1013 << System::typeinfoName(*ti) << "]");
1014 ATH_MSG_INFO("** could not find a CLID from type-info-alias ["
1015 << ti_typename << "]");
1016 continue;
1017 }
1018 }
1019 } else {
1020 // probably a built-in type...
1021 TObjArray *leaves = branch->GetListOfLeaves();
1022 if (leaves &&
1023 leaves->GetEntries() == 1) {
1024 const std::string type_name = ((TLeaf*)leaves->At(0))->GetTypeName();
1025 if (!m_clidsvc->getIDOfTypeName(::root_typename(type_name), id)
1026 .isSuccess()) {
1027 ATH_MSG_INFO("** could not find a CLID for type-name ["
1028 << type_name << "]");
1029 continue;
1030 }
1031 }
1032 }
1033 if (id == 0) {
1034 ATH_MSG_INFO("** could not find a CLID for type-name ["
1035 << type_name << "]");
1036 continue;
1037 }
1038 CxxUtils::RefCountedPtr<Athena::RootBranchAddress> addr
1039 (new Athena::RootBranchAddress
1040 (ROOT_StorageType, id,
1041 tree_name,
1042 br_name,
1043 (unsigned long)(value_ptr),
1044 (unsigned long)(0)));
1045 if (!store->recordAddress(sg_key, std::move(addr), true).isSuccess()) {
1046 ATH_MSG_ERROR("could not record address at [" << sg_key << "] in store ["
1047 << store->name() << "]");
1048 }
1049 // SG::TransientAddress* taddr = new SG::TransientAddress
1050 // (id, sg_key, addr);
1051 // taddr->setProvider(this);
1052 // taddr->clearAddress(true);
1053 // tads.push_back(taddr);
1054 }
1055 }
1056 return StatusCode::SUCCESS;
1057}
#define ATH_MSG_ERROR(x)
uint32_t CLID
The Class ID type.
void record(const T *p, const std::string &key)
Definition TestStore.h:81
virtual const std::string & name() const override
Definition TestStore.cxx:97
TestStore store
Definition TestStore.cxx:23
TChain * tree

◆ createRootBranchAddresses()

StatusCode Athena::RootNtupleEventSelector::createRootBranchAddresses ( StoreID::type storeID,
tadList & tads )
private

helper method to create proxies

Definition at line 837 of file RootNtupleEventSelector.cxx.

839{
840 if (storeID != StoreID::EVENT_STORE) {
841 ATH_MSG_INFO("-- not the event-store --");
842 return(StatusCode::SUCCESS);
843 }
844
845 if (0 == m_tuple) {
846 ATH_MSG_ERROR("null pointer to n-tuple !");
847 return StatusCode::FAILURE;
848 }
849
850 TObjArray *leaves = m_tuple->GetListOfLeaves();
851 if (!leaves) {
852 ATH_MSG_INFO("no leaves!!");
853 return StatusCode::SUCCESS;
854 }
855
856 // loop over leaves
857 for (Int_t i = 0; i < leaves->GetEntries(); ++i) {
858 TLeaf *leaf = (TLeaf *)leaves->At(i);
859 TBranch *branch = leaf->GetBranch();
860 if (branch) {
861
862 CLID id = 0;
863 const void* value_ptr = m_tuple;
864 const std::string type_name = leaf->GetTypeName();
865 const std::string br_name = branch->GetName();
866 const std::string sg_key = br_name;//m_tupleName.value()+"/"+br_name;
867 TClass *cls = TClass::GetClass(type_name.c_str());
868 const std::type_info *ti = 0;
869
870 if (cls) {
871 ti = cls->GetTypeInfo();
872 // first, try to load a dict for that class...
873 if (ti) {
874 m_dictsvc->load_type(*ti);
875 }
876 if (!ti) {
877 ATH_MSG_WARNING("could not find a type-info for [" <<
878 type_name << "]");
879 continue;
880 }
881 std::string ti_typename = System::typeinfoName(*ti);
882 if (!m_clidsvc->getIDOfTypeInfoName(ti_typename, id)
883 .isSuccess()) {
884 // try another one...
885 {
886 // Protect against data race inside TClassEdit.
887 // https://github.com/root-project/root/issues/10353
888 // Should be fixed in root 6.26.02.
889 R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
890 ti_typename = TClassEdit::ShortType(ti_typename.c_str(),
891 TClassEdit::kDropAllDefault);
892 }
893 if (!m_clidsvc->getIDOfTypeInfoName(ti_typename, id)
894 .isSuccess()) {
895 ATH_MSG_DEBUG("** could not find a CLID from type-info ["
896 << System::typeinfoName(*ti) << "]");
897 ATH_MSG_DEBUG("** could not find a CLID from type-info-alias ["
898 << ti_typename << "]");
899 continue;
900 }
901 }
902 } else {
903 // probably a built-in type...
904 if (!m_clidsvc->getIDOfTypeName(::root_typename(type_name), id)
905 .isSuccess()) {
906 ATH_MSG_DEBUG("** could not find a CLID for type-name ["
907 << type_name << "]");
908 continue;
909 }
910 }
911 if (id == 0) {
912 ATH_MSG_DEBUG("** could not find a CLID for type-name ["
913 << type_name << "]");
914 continue;
915 }
916 Athena::RootBranchAddress* addr = new Athena::RootBranchAddress
917 (ROOT_StorageType, id,
918 m_tuple->GetName(),
919 br_name,
920 (unsigned long)(value_ptr),
921 (unsigned long)(m_curEvt-1));
922
923 // recycle old rootaddress, if any.
924 SG::DataProxy* proxy = m_dataStore->proxy (id, sg_key);
925 if (proxy) {
926 proxy->setAddress (addr);
927 }
928 else {
929 auto taddr = new SG::TransientAddress(id, sg_key, addr, false);
930 taddr->setProvider(this, storeID);
931 // only add the *new* TransientAddress to the input list as the *old* ones
932 // are already tracked by the datastore (via the sticky proxies)
933 tads.push_back(taddr);
934 }
935 }
936 }
937 m_needReload = false;
938 // BeginInputFile is deferred to handle() on BeginEvent — see comment there.
939
940 return StatusCode::SUCCESS;
941}
#define ATH_MSG_DEBUG(x)
@ EVENT_STORE
Definition StoreID.h:26

◆ curEvent()

int Athena::RootNtupleEventSelector::curEvent ( const Context & refCtxt) const
overridevirtual

return the current event number.

Returns
The current event number.

Definition at line 720 of file RootNtupleEventSelector.cxx.

721{
722 return m_curEvt;
723}

◆ do_init_io()

StatusCode Athena::RootNtupleEventSelector::do_init_io ( )
private

helper method to init the i/o components

Definition at line 1180 of file RootNtupleEventSelector.cxx.

1181{
1182 // std::cout << "::fetchNtuple..." << std::endl;
1183
1184 // initialize some helper structures and data
1185 {
1186 CollMetaData zero;
1187 zero.min_entries = -1;
1188 zero.max_entries = -1;
1189 zero.entries = -1;
1190 m_collEvts.resize (m_tupleNames.size());
1191 for (size_t i = 0; i < m_collEvts.size(); i++) {
1192 m_collEvts[i].resize(m_inputCollectionsName.value().size(), zero);
1193 }
1194 }
1195
1196 m_tuple = fetchNtuple(m_inputCollectionsName.value()[0],
1197 m_tupleNames[0]);
1198 if (!m_tuple) {
1199 throw "RootNtupleEventSelector: Unable to fetch Ntuple";
1200 }
1201
1202 // std::cout << "::clear-root-addresses..." << std::endl;
1203 // reset the list of branches
1204 m_needReload = true;
1205
1206 // skip events we are asked to skip
1207 m_curEvt = m_skipEvts;
1208 m_nbrEvts = 0;
1209
1210 // std::cout << "::fetchNtuple...[done]" << std::endl;
1211 return StatusCode::SUCCESS;
1212}
TTree * fetchNtuple(const std::string &fname, const std::string &tupleName) const
helper method to retrieve the correct tuple
void zero(TH2 *h)
zero the contents of a 2d histogram

◆ endInputFile()

StatusCode Athena::RootNtupleEventSelector::endInputFile ( RootNtupleEventContext * rctx) const
private

Definition at line 433 of file RootNtupleEventSelector.cxx.

434{
435 // Fire EndInputFile via guard reset
436 m_inputFileGuard.reset();
437
438 // prepare for next file, if any...
439 // std::cout << "=========================================================="
440 // << std::endl;
441 // std::cerr << "::switch to next file...\n";
442
443 // iterate over proxies and
444 // mark as garbage and drop the RootBranchAddress (as a side effect of
445 // ::setAddress(NULL).
446 // this way, the next time we hit ::createRootBranchAddress or ::updateAddress
447 // all internal states are kosher.
448 for (const SG::DataProxy* cdp : m_dataStore->proxies()) {
449 if (dynamic_cast<Athena::RootBranchAddress*> (cdp->address()) != nullptr) {
450 if (SG::DataProxy* dp = m_dataStore->proxy_exact (cdp->sgkey())) {
451 dp->setAddress (nullptr);
452 }
453 }
454 }
455
456 const bool forceRemove = false;
457 CHECK( m_dataStore->clearStore(forceRemove) ); //must clear the storegate so that any tampering user did in EndInputFile incident is cleared
458 m_needReload = true;
459 // Defer BeginInputFile for the next file to the next BeginEvent.
460 // The actual file name is resolved in handle() from m_tuple.
461 m_fireBIF = true;
462
463 return StatusCode::SUCCESS;
464}
#define CHECK(...)
Evaluate an expression and check for errors.
bool m_fireBIF
Flag to fire BeginInputFile on the next BeginEvent incident.
std::optional< InputFileIncidentGuard > m_inputFileGuard
RAII guard: guarantees a matching EndInputFile for every BeginInputFile.

◆ fetchNtuple()

TTree * Athena::RootNtupleEventSelector::fetchNtuple ( const std::string & fname,
const std::string & tupleName ) const
private

helper method to retrieve the correct tuple

Definition at line 1060 of file RootNtupleEventSelector.cxx.

1062{
1063 // std::cout << "::fetchNtuple(" << fname << ")..." << std::endl;
1064 TTree* tree = NULL;
1065 RootGlobalsRestore rgr;
1066 // std::cout << "::TFile::Open()..." << std::endl;
1067 TFile *f = (TFile*)gROOT->GetListOfFiles()->FindObject(fname.c_str());
1068 TFile* fnew = nullptr;
1069 if (!f) {
1070 f = TFile::Open(fname.c_str(), "READ");
1071 fnew = f;
1072 if (f) {
1073 f->SetName(fname.c_str());
1074 }
1075 }
1076 if (!f || f->IsZombie()) {
1077 ATH_MSG_ERROR("could not open next file in input collection ["
1078 << fname << "]");
1079 if (f) {
1080 f->Close();
1081 }
1082 return tree;
1083 }
1084 // std::cout << "::TFile::GetTree(" << m_tupleName << ")..." << std::endl;
1085 tree = (TTree*)f->Get(tupleName.c_str());
1086 if (!tree) {
1087 ATH_MSG_ERROR("could not retrieve tree [" << tupleName << "]"
1088 << " from file [" << fname << "]");
1089 f->Close();
1090 return tree;
1091 }
1092
1093 if (fnew)
1094 m_files.push_back(fnew);
1095
1096 // std::cout << "::TTree::SetBranchStatus()..." << std::endl;
1097 // disable all branches
1098 tree->SetBranchStatus("*", 0);
1099
1100 if (!m_imetaStore->clearStore().isSuccess()) {
1101 ATH_MSG_INFO("could not clear store [" << m_imetaStore.typeAndName() << "]");
1102 return tree;
1103 }
1104
1105 addMetadataFromDirectoryName(tupleName+"Meta", f);
1106 addMetadataFromDirectoryName("Lumi", f, "Lumi");
1107 return tree;
1108}
void addMetadataFromDirectoryName(const std::string &metadirname, TFile *fileObj, const std::string &prefix="") const

◆ finalize()

StatusCode Athena::RootNtupleEventSelector::finalize ( )
overridevirtual

Definition at line 412 of file RootNtupleEventSelector.cxx.

413{
414 ATH_MSG_INFO ("Finalize...");
415 // FIXME: this should be tweaked/updated if/when a selection function
416 // or filtering predicate is applied (one day?)
417 ATH_MSG_INFO ("Total events read: " << (m_nbrEvts - m_skipEvts));
418
419 // Explicitly delete all the files we created.
420 // If we leave it up to root, then xrootd can get cleaned up before
421 // the root destructors run, leading to a crash.
422 for (TFile* f : m_files)
423 delete f;
424 m_files.clear();
425
426 return StatusCode::SUCCESS;
427}

◆ handle()

void Athena::RootNtupleEventSelector::handle ( const Incident & incident)
overridevirtual

Definition at line 1270 of file RootNtupleEventSelector.cxx.

1270 {
1271 if (m_fireBIF && incident.type() == IncidentType::BeginEvent) {
1272 std::string fname = m_tuple->GetCurrentFile()->GetName();
1274 fname, {},
1275 /*endFileName=*/fname);
1276 m_fireBIF = false;
1277 }
1278}
static InputFileIncidentGuard begin(IIncidentSvc &incSvc, std::string_view source, std::string_view beginFileName, std::string_view guid, std::string_view endFileName={}, std::string_view beginType=IncidentType::BeginInputFile, std::string_view endType=IncidentType::EndInputFile)
Factory: fire the begin incident and return a guard whose destructor fires the matching end incident.

◆ initialize()

StatusCode Athena::RootNtupleEventSelector::initialize ( )
overridevirtual

Definition at line 274 of file RootNtupleEventSelector.cxx.

275{
276 ATH_MSG_INFO ("Enter RootNtupleEventSelector initialization...");
277
278 // retrieve clidsvc
279 if ( !m_clidsvc.retrieve().isSuccess() ) {
281 ("Could not retrieve [" << m_clidsvc.typeAndName() << "]");
282 return StatusCode::FAILURE;
283 }
284
285 // retrieve dictsvc
286 if ( !m_dictsvc.retrieve().isSuccess() ) {
288 ("Could not retrieve [" << m_dictsvc.typeAndName() << "]");
289 return StatusCode::FAILURE;
290 }
291
292 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
293 boost::char_separator<char> sep (" ;");
294 tokenizer tokens (m_tupleName.value(), sep);
295 m_tupleNames.assign (tokens.begin(), tokens.end());
296
297 if ( m_tupleNames.empty() ) {
299 ("You have to give a TTree name to read from the ROOT files !");
300 return StatusCode::FAILURE;
301 }
302
303 CHECK( m_incsvc.retrieve() );
304 m_incsvc->addListener(this,IncidentType::BeginEvent,99); //used to trigger BeginInputFile on start of first event of file - 99 priority so AFTER storegatesvc done
305
306
307 setupInputCollection( m_inputCollectionsName );
308 const std::size_t nbrInputFiles = m_inputCollectionsName.value().size();
309 if ( nbrInputFiles < 1 ) {
311 ("You need to give at least 1 input file !!" << endmsg
312 << "(Got [" << nbrInputFiles << "] file instead !)");
313 return StatusCode::FAILURE;
314 } else {
316 ("Selector configured to read [" << nbrInputFiles << "] file(s)..."
317 << endmsg
318 << " TTree [" << m_tupleName.value() << "]");
319 }
320
321 {
322 // register this service for 'I/O' events
323 ServiceHandle<IIoComponentMgr> iomgr("IoComponentMgr", name());
324 if (!iomgr.retrieve().isSuccess()) {
325 ATH_MSG_FATAL("Could not retrieve IoComponentMgr !");
326 return StatusCode::FAILURE;
327 }
328 if (!iomgr->io_register(this).isSuccess()) {
329 ATH_MSG_FATAL("Could not register myself with the IoComponentMgr !");
330 return StatusCode::FAILURE;
331 }
332 // register input file's names with the I/O manager
333 const std::vector<std::string>& incol = m_inputCollectionsName.value();
334 bool allGood = true;
335 for (std::size_t icol = 0, imax = incol.size(); icol < imax; icol++) {
336 if (!iomgr->io_register(this,
337 IIoComponentMgr::IoMode::READ,
338 incol[icol]).isSuccess()) {
339 ATH_MSG_FATAL("could not register [" << incol[icol] << "] for output !");
340 allGood = false;
341 } else {
342 ATH_MSG_VERBOSE("io_register[" << this->name() << "](" << incol[icol] << ") [ok]");
343 }
344 }
345 if (!allGood) {
346 return StatusCode::FAILURE;
347 }
348 }
349
350 if (!do_init_io().isSuccess()) {
351 return StatusCode::FAILURE;
352 }
353 // retrieve event store
354 // this needs to happen *after* having initialized the i/o
355 // as our branches (which need a valid m_ntuple pointer)
356 // may be asked to be registered as we are a ProxyProvider.
357 // retrieving the event store will poke the ProxyProviderSvc...
358 /*
359 if ( !m_dataStore.retrieve().isSuccess() ) {
360 ATH_MSG_ERROR
361 ("Could not retrieve [" << m_dataStore.typeAndName() << "] !!");
362 return StatusCode::FAILURE;
363 }
364
365 // ditto for (input) meta data store
366 if (!m_imetaStore.retrieve().isSuccess()) {
367 ATH_MSG_ERROR
368 ("Could not retrieve [" << m_imetaStore.typeAndName() << "] !!");
369 return StatusCode::FAILURE;
370 }
371
372 // ditto for (output) meta data store
373 if (!m_ometaStore.retrieve().isSuccess()) {
374 ATH_MSG_ERROR
375 ("Could not retrieve [" << m_ometaStore.typeAndName() << "] !!");
376 return StatusCode::FAILURE;
377 }
378 */
379
380 //ensure the Athena::NtupleCnvSvc is in the EventPersistencySvc
381 ServiceHandle<IProperty> epSvc("EventPersistencySvc",name());
382 std::vector<std::string> propVal;
383 CHECK( Gaudi::Parsers::parse( propVal , epSvc->getProperty("CnvServices").toString() ) );
384 bool foundSvc(false);
385 for(const std::string& s : propVal) {
386 if(s=="Athena::xAODCnvSvc") { foundSvc=true; break; }
387 }
388 if(!foundSvc) {
389 propVal.push_back("Athena::NtupleCnvSvc");
390 CHECK( epSvc->setProperty("CnvServices", Gaudi::Utils::toString( propVal ) ));
391 }
392
393 //we should also add ourself as a proxy provider
394 ServiceHandle<IProxyProviderSvc> ppSvc("ProxyProviderSvc",name());
395 CHECK( ppSvc.retrieve() );
396 ppSvc->addProvider( this );
397
398
399
400 return StatusCode::SUCCESS;
401}
#define endmsg
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
int imax(int i, int j)
StatusCode do_init_io()
helper method to init the i/o components
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
bool allGood
Loop over the SDOs & Digits.

◆ io_reinit()

StatusCode Athena::RootNtupleEventSelector::io_reinit ( )
overridevirtual

Callback method to reinitialize the internal state of the component for I/O purposes (e.g.

upon fork(2))

Definition at line 728 of file RootNtupleEventSelector.cxx.

729{
730 ATH_MSG_INFO("I/O reinitialization...");
731
732 ServiceHandle<IIoComponentMgr> iomgr("IoComponentMgr", name());
733 if (!iomgr.retrieve().isSuccess()) {
734 ATH_MSG_FATAL("Could not retrieve IoComponentMgr !");
735 return StatusCode::FAILURE;
736 }
737 if (!iomgr->io_hasitem(this)) {
738 ATH_MSG_FATAL("IoComponentMgr does not know about myself !");
739 return StatusCode::FAILURE;
740 }
741 std::vector<std::string> inputCollections = m_inputCollectionsName.value();
742
743 for (std::size_t
744 i = 0,
745 imax = m_inputCollectionsName.value().size();
746 i < imax;
747 ++i) {
748 std::string &fname = inputCollections[i];
749 // std::cout << "--retrieve new name for [" << fname << "]...\n";
750 if (!iomgr->io_contains(this, fname)) {
751 ATH_MSG_ERROR("IoComponentMgr does not know about [" << fname << "] !");
752 return StatusCode::FAILURE;
753 }
754 if (!iomgr->io_retrieve(this, fname).isSuccess()) {
755 ATH_MSG_FATAL("Could not retrieve new value for [" << fname << "] !");
756 return StatusCode::FAILURE;
757 }
758 // std::cout << "--> [" << fname << "]\n" << std::flush;
759 }
760 // all good... copy over.
761 m_inputCollectionsName = inputCollections;
762
763 // remove our EventInfo if any...
764 // {
765 // const bool force_remove = true;
766 // if (!m_dataStore->clearStore(force_remove).isSuccess()) {
767 // ATH_MSG_ERROR("could not clear event store!");
768 // return StatusCode::FAILURE;
769 // } else {
770 // ATH_MSG_INFO("sgdump: \n" << m_dataStore->dump());
771 // }
772 // }
773
774 // std::cout << "--> do_init_io...\n" << std::flush;
775 if (!do_init_io().isSuccess()) {
776 return StatusCode::FAILURE;
777 }
778
779 ATH_MSG_INFO("I/O reinitialization... [done]");
780 return StatusCode::SUCCESS;
781}

◆ last()

StatusCode Athena::RootNtupleEventSelector::last ( Context & refContext) const
overridevirtual

Definition at line 615 of file RootNtupleEventSelector.cxx.

616{
617 ATH_MSG_ERROR ("............. Last Event Not Implemented .............");
618 return StatusCode::FAILURE;
619}

◆ loadAddresses()

StatusCode Athena::RootNtupleEventSelector::loadAddresses ( StoreID::type storeID,
tadList & list )
overridevirtual

get all new addresses from Provider for this Event.

Definition at line 799 of file RootNtupleEventSelector.cxx.

800{
801 if (m_needReload) {
802 return createRootBranchAddresses(storeID, tads);
803 }
804
805 return StatusCode::SUCCESS;
806}
StatusCode createRootBranchAddresses(StoreID::type storeID, tadList &tads)
helper method to create proxies

◆ next() [1/2]

virtual StatusCode Athena::RootNtupleEventSelector::next ( Context & refCtxt) const
overridevirtual

◆ next() [2/2]

StatusCode Athena::RootNtupleEventSelector::next ( Context & refCtxt,
int jump ) const
overridevirtual

Definition at line 592 of file RootNtupleEventSelector.cxx.

593{
594 ATH_MSG_DEBUG ("next(" << jump << ") : iEvt " << m_curEvt);
595
596 if (self()->seek(ctx, m_curEvt + jump).isSuccess()) {
597 return StatusCode::FAILURE;
598 }
599 return next(ctx);
600}
virtual StatusCode seek(Context &refCtxt, int evtnum) const override
Seek to a given event number.
virtual StatusCode next(Context &refCtxt) const override

◆ preLoadAddresses()

StatusCode Athena::RootNtupleEventSelector::preLoadAddresses ( StoreID::type storeID,
tadList & list )
overridevirtual

IAddressProvider interface get all addresses from Provider : Called before Begin Event

Definition at line 788 of file RootNtupleEventSelector.cxx.

790{
791 // std::cerr << "TTES::preLoadAddresses(" << int(storeID)
792 // << "," << tads.size()
793 // << ")...\n";
794 return StatusCode::SUCCESS;
795}

◆ previous() [1/2]

virtual StatusCode Athena::RootNtupleEventSelector::previous ( Context & refCtxt) const
overridevirtual

◆ previous() [2/2]

StatusCode Athena::RootNtupleEventSelector::previous ( Context & refCtxt,
int jump ) const
overridevirtual

Definition at line 609 of file RootNtupleEventSelector.cxx.

610{
611 return next( ctx, -jump );
612}

◆ releaseContext()

StatusCode Athena::RootNtupleEventSelector::releaseContext ( Context *& refCtxt) const
overridevirtual

Definition at line 645 of file RootNtupleEventSelector.cxx.

646{
647 RootNtupleEventContext *ctx = dynamic_cast<RootNtupleEventContext*>(refCtxt);
648 if ( ctx ) {
649 delete ctx; ctx = 0;
650 return StatusCode::SUCCESS;
651 }
652
653 return StatusCode::FAILURE;
654}

◆ resetCriteria()

StatusCode Athena::RootNtupleEventSelector::resetCriteria ( const std::string & cr,
Context & ctx ) const
overridevirtual

Definition at line 657 of file RootNtupleEventSelector.cxx.

658{
659 ATH_MSG_ERROR ("............. resetCriteria Not Implemented .............");
660 return StatusCode::FAILURE;
661}

◆ rewind()

StatusCode Athena::RootNtupleEventSelector::rewind ( Context & refCtxt) const
overridevirtual

Definition at line 623 of file RootNtupleEventSelector.cxx.

624{
625 return self()->seek(ctxt, 0);
626}

◆ seek()

StatusCode Athena::RootNtupleEventSelector::seek ( Context & ctx,
int evtnum ) const
overridevirtual

Seek to a given event number.

IEvtSelectorSeek interface

Parameters
evtnumThe event number to which to seek.
evtnumThe event number to which to seek.

Definition at line 672 of file RootNtupleEventSelector.cxx.

673{
674 RootNtupleEventContext* rctx = dynamic_cast<RootNtupleEventContext*>(&ctx);
675 if (!rctx) {
676 return StatusCode::FAILURE;
677 }
678
679 // std::cout << "::seek - evtnum=" << evtnum
680 // << " curevt=" << m_curEvt
681 // << " curcol=" << rctx->fileIndex()
682 // << std::endl;
683 long coll_idx, tuple_idx;
684 find_coll_idx(evtnum, coll_idx, tuple_idx);
685 // std::cout << "::seek - evtnum=" << evtnum
686 // << " curevt=" << m_curEvt
687 // << " curcol=" << rctx->fileIndex()
688 // << " colidx=" << coll_idx
689 // << std::endl;
690 if ((coll_idx == -1 || tuple_idx == -1) && evtnum < m_curEvt) {
691 coll_idx = rctx->fileIndex();
692 tuple_idx = rctx->tupleIndex();
693 }
694
695 if (coll_idx == -1 || tuple_idx == -1) {
696 ATH_MSG_INFO("seek: reached end of input.");
697 return StatusCode::RECOVERABLE;
698 }
699
700 if (coll_idx != static_cast<int>(rctx->fileIndex()) ||
701 tuple_idx != static_cast<int>(rctx->tupleIndex()))
702 {
703 // tell everyone we switched files...
704 m_tuple = NULL;
705 CHECK( endInputFile (rctx) );
706 }
707
708 rctx->setFileIndex (coll_idx);
709 rctx->setTupleIndex (tuple_idx);
710 m_curEvt = evtnum;
711
712 return StatusCode::SUCCESS;
713}
StatusCode endInputFile(RootNtupleEventContext *rctx) const
find_coll_idx(-1, coll_idx, tuple_idx)

◆ setupInputCollection()

void Athena::RootNtupleEventSelector::setupInputCollection ( Gaudi::Details::PropertyBase & inputCollectionsName)
private

callback to synchronize the list of input files

Definition at line 830 of file RootNtupleEventSelector.cxx.

831{
832 // nothing ?
833 return;
834}

◆ size()

virtual int Athena::RootNtupleEventSelector::size ( Context & refCtxt) const
overridevirtual

ICollectionSize interface

◆ stop()

StatusCode Athena::RootNtupleEventSelector::stop ( )
overridevirtual

Definition at line 403 of file RootNtupleEventSelector.cxx.

404{
405 // Fire EndInputFile for any file still open (the event loop may end
406 // before the file is fully read).
407 m_inputFileGuard.reset();
408 m_fireBIF = false;
409 return StatusCode::SUCCESS;
410}

◆ updateAddress()

StatusCode Athena::RootNtupleEventSelector::updateAddress ( StoreID::type storeID,
SG::TransientAddress * tad,
const EventContext & ctx )
overridevirtual

update a transient Address

Definition at line 810 of file RootNtupleEventSelector.cxx.

812{
813 if (tad) {
814 if (m_dataStore->proxy_exact (tad->sgkey())) {
815 return StatusCode::SUCCESS;
816 }
817 return StatusCode::FAILURE;
818 }
819 // do nothing.
820 return StatusCode::SUCCESS;
821}
sgkey_t sgkey() const
Set the primary (hashed) SG key.

◆ Athena::RootNtupleEventContext

friend class Athena::RootNtupleEventContext
friend

Definition at line 53 of file RootNtupleEventSelector.h.

Member Data Documentation

◆ m_collEvts

std::vector<std::vector<CollMetaData> > Athena::RootNtupleEventSelector::m_collEvts
mutableprivate

helper method to get the collection index (into m_inputCollectionsName) and tuple index (into m_tupleNames') for a given event index evtidx`.

returns -1 if not found. */ void find_coll_idx(int evtidx, long& coll_idx, long& tuple_idx) const;

/ non-const access to self (b/c ::next() is const) RootNtupleEventSelector self() const { return const_cast<RootNtupleEventSelector>(this); }

///////////////////////////////////////////////////////////////// Private data: ///////////////////////////////////////////////////////////////// private:

typedef ServiceHandle<StoreGateSvc> StoreGateSvc_t; / Pointer to the StoreGateSvc event store StoreGateSvc_t m_dataStore;

/ Pointer to the StoreGateSvc input metadata store StoreGateSvc_t m_imetaStore;

/ Pointer to the StoreGateSvc output metadata store StoreGateSvc_t m_ometaStore;

typedef ServiceHandle<IClassIDSvc> ICLIDSvc_t; / Pointer to the IClassIDSvc ICLIDSvc_t m_clidsvc;

typedef ServiceHandle<IDictLoaderSvc> IDictSvc_t; / Pointer to the IDictLoaderSvc IDictSvc_t m_dictsvc;

typedef ServiceHandle<IIncidentSvc> IIncSvc_t; / Handle to the incident service IIncSvc_t m_incsvc;

/ List of input files containing TTree StringArrayProperty m_inputCollectionsName;

/ Name of TTree to load from collection of input files StringProperty m_tupleName;

/ List of branches to activate in the TTree StringArrayProperty m_activeBranchNames;

/ Number of events to skip at the beginning long m_skipEvts;

/ Names of all trees over which to iterate. std::vector<std::string> m_tupleNames;

/ Number of Events read so far. mutable long m_nbrEvts;

/ current event index mutable long m_curEvt;

struct CollMetaData { / number of entries up to this collection long min_entries; / number of entries after this collection long max_entries; / number of entries in this collection. long entries; }; /** cache of the number of entries for each collection Indexed like [tuple][collection]

Definition at line 242 of file RootNtupleEventSelector.h.

◆ m_files

std::vector<TFile*> Athena::RootNtupleEventSelector::m_files
mutableprivate

Definition at line 268 of file RootNtupleEventSelector.h.

◆ m_fireBIF

bool Athena::RootNtupleEventSelector::m_fireBIF {true}
mutableprivate

Flag to fire BeginInputFile on the next BeginEvent incident.

Definition at line 259 of file RootNtupleEventSelector.h.

259{true};

◆ m_inputFileGuard

std::optional<InputFileIncidentGuard> Athena::RootNtupleEventSelector::m_inputFileGuard
mutableprivate

RAII guard: guarantees a matching EndInputFile for every BeginInputFile.

BeginInputFile is deferred to the next BeginEvent (data must be loaded in the store first), so the guard is created in handle(), not immediately.

Definition at line 257 of file RootNtupleEventSelector.h.

◆ m_needReload

bool Athena::RootNtupleEventSelector::m_needReload
mutableprivate

The (python) selection function to apply on the TChain we are reading.

Definition at line 252 of file RootNtupleEventSelector.h.

◆ m_tuple

TTree* Athena::RootNtupleEventSelector::m_tuple
mutableprivate

current tree being read

Definition at line 245 of file RootNtupleEventSelector.h.


The documentation for this class was generated from the following files: