2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5#include "Identifier/IdentifierHash.h"
6#include "TrkPrepRawData/PrepRawData.h"
7#include "TrkRIO_OnTrack/RIO_OnTrack.h"
8#include "StoreGate/ReadHandle.h"
13template <class CONT, class ROT>
14void Trk::ITrkEventCnvTool::prepareRIO_OnTrackElementLink(const ROT* rot,
16 ELIndex_t& index) const
18 // verify pointer is ok
19 // assume EL is correct
21 key = rot->m_rio.dataID();
22 index = rot->m_rio.index();
24 bool isPersistifiable = (rot->m_rio.key() != 0
25 and IdentContIndex(index).isValid());
27 if (isPersistifiable) {
28 return; // Already set - bail out.
31 // When doing AOD to AOD copy we expect the PRD links to be zero.
32 if (rot->prepRawData()==nullptr) {
33 MsgStream log(&(*m_msgSvc), name());
34 log<<MSG::DEBUG<<"No PRD for this ROT: "<<(*rot)<<endmsg;
38 SG::ConstIterator<CONT> obj, objEnd;
39 StatusCode sc = m_storeGate->retrieve(obj, objEnd);
41 MsgStream log(&(*m_msgSvc), name());
42 log << MSG::WARNING <<"No containers found!"<< endmsg;
47 for ( ; obj!=objEnd; ++obj) {
48 const Trk::PrepRawData* prd = rot->prepRawData();
49 unsigned int oindex = prd->getHashAndIndex().objIndex(); // prd index within collection
50 IdentifierHash idHash = prd->getHashAndIndex().collHash(); // idHash of collection
52 const CONT& cont = *obj; // container
53 auto coll = cont.indexFindPtr(idHash); //matching collection
56 // check prd exists in collection
57 // check pointer value the same.
58 if ( (coll!=nullptr)&& (coll->size()>oindex) && (prd==(*coll)[oindex]) ) {
59 // okay, so we found the correct PRD in the container.
60 // Return EL components.
61 // objh.key() is the name of the container. oindex is the index within the collection. IdHash????
63 index = prd->getHashAndIndex().hashAndIndex();
64 return; //exit loop and function. We're done.
66 if (coll != nullptr) {
67 MsgStream log(m_msgSvc.get(), name());
68 for (const auto* prd : *coll) {
69 log<<MSG::ERROR<<"Available prd in collection: "<<(*prd)<<endmsg;
73 // so, we obviously didn't have the correct container (or something else went wrong)
74 MsgStream log(&(*m_msgSvc), name());
75 log << MSG::ERROR<<"Could not find matching PRD container for this ROT. ROT will be written out in incomplete state. "
76 << "Dumping ROT: "<<*rot<<endmsg;
77 throw std::runtime_error("Failed to persitify");
81template <class CONT, class ROT>
82void Trk::ITrkEventCnvTool::prepareRIO_OnTrackElementLink(ROT* rot) const
86 prepareRIO_OnTrackElementLink<CONT> (rot, key, index);
87 rot->m_rio.resetWithKeyAndIndex (key, index);
90inline void Trk::ITrkEventCnvTool::setRoT_Values(std::pair<const Trk::TrkDetElementBase *, const Trk::PrepRawData *>& pair, Trk::RIO_OnTrack *RoT) const {
91 RoT->setValues(pair.first, pair.second);
94template <class CONT, class ROT>
95bool Trk::ITrkEventCnvTool::getHashAndIndex(const ROT* rot,
96 const SG::ReadHandleKey<CONT>& contName,
97 typename ElementLink<CONT>::index_type& hashAndIndex) const {
98 if (rot==nullptr) return false;
100 const Trk::PrepRawData* prd{rot->prepRawData()};
101 if (prd==nullptr) return false;
103 SG::ReadHandle<CONT> cont (contName);
104 if (not cont.isValid()) return false;
106 const IdentContIndex& idContIndex{prd->getHashAndIndex()};
107 const IdentifierHash& idHash{idContIndex.collHash()}; // idHash of collection
108 auto contItr = cont->indexFindPtr(idHash); //matching collection
109 if (contItr==nullptr) return false;
111 unsigned int index{idContIndex.objIndex()}; // prd index within collection
112 if ((contItr->size()>index) and (prd==(*contItr)[index])) {
113 hashAndIndex = idContIndex.hashAndIndex();