diff --git a/src/cpp/CMakeLists.txt b/src/cpp/CMakeLists.txt index a5d7f3cf2..38e1d7cda 100644 --- a/src/cpp/CMakeLists.txt +++ b/src/cpp/CMakeLists.txt @@ -121,6 +121,7 @@ SET( LCIO_UTIL_SRCS ./src/UTIL/ILDConf.cc ./src/UTIL/ProcessFlag.cc ./src/UTIL/TrackTools.cc + ./src/UTIL/LCCollectionTools.cc ) SET( LCIO_MT_SRCS diff --git a/src/cpp/include/UTIL/LCCollectionTools.h b/src/cpp/include/UTIL/LCCollectionTools.h new file mode 100644 index 000000000..ab54a7984 --- /dev/null +++ b/src/cpp/include/UTIL/LCCollectionTools.h @@ -0,0 +1,15 @@ +#ifndef UTIL_LCCollectionTools_H +#define UTIL_LCCollectionTools_H 1 + +#include "EVENT/LCObject.h" +#include "EVENT/LCCollection.h" + +namespace UTIL{ + /** Extract object index inside a given LCCollection. If object is not found, return -1. + * @author Bohdan Dudar + * @version August 2022 + */ + int getElementIndex(const EVENT::LCObject* item, EVENT::LCCollection* collection); +} + +#endif diff --git a/src/cpp/src/UTIL/LCCollectionTools.cc b/src/cpp/src/UTIL/LCCollectionTools.cc new file mode 100644 index 000000000..9294e7859 --- /dev/null +++ b/src/cpp/src/UTIL/LCCollectionTools.cc @@ -0,0 +1,10 @@ +#include "UTIL/LCCollectionTools.h" + +namespace UTIL{ + int getElementIndex(const EVENT::LCObject* item, EVENT::LCCollection* collection){ + for(int i=0; i < collection->getNumberOfElements(); ++i){ + if ( item == collection->getElementAt(i) ) return i; + } + return -1; + } +}