ATLAS Offline Software
ITrkEventCnvTool.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "Identifier/IdentifierHash.h"
6 #include "TrkPrepRawData/PrepRawData.h"
7 #include "TrkRIO_OnTrack/RIO_OnTrack.h"
8 #include "StoreGate/ReadHandle.h"
9 
10 #include <typeinfo>
11 
12 
13 template <class CONT, class ROT>
14 void Trk::ITrkEventCnvTool::prepareRIO_OnTrackElementLink(const ROT* rot,
15  ELKey_t& key,
16  ELIndex_t& index) const
17 {
18  // verify pointer is ok
19  // assume EL is correct
20  // get data handles to convertors
21 
22  key = rot->m_rio.dataID();
23  index = rot->m_rio.index();
24 
25  bool isPersistifiable = (rot->m_rio.key() != 0
26  and IdentContIndex(index).isValid());
27 
28  if (isPersistifiable) {
29  return; // Already set - bail out.
30  }
31 
32  // When doing AOD to AOD copy we expect the PRD links to be zero.
33  if (rot->prepRawData()==nullptr) {
34  MsgStream log(&(*m_msgSvc), name());
35  log<<MSG::DEBUG<<"No PRD for this ROT: "<<(*rot)<<endmsg;
36  return;
37  }
38 
39  SG::ConstIterator<CONT> dh, dhEnd;
40  StatusCode sc = m_storeGate->retrieve(dh, dhEnd);
41  if (sc.isFailure()) {
42  MsgStream log(&(*m_msgSvc), name());
43  log << MSG::WARNING <<"No containers found!"<< endmsg;
44  return;
45  }
46 
47  // loop over dhs
48  for ( ; dh!=dhEnd; ++dh) {
49  const Trk::PrepRawData* prd = rot->prepRawData();
50  unsigned int oindex = prd->getHashAndIndex().objIndex(); // prd index within collection
51  IdentifierHash idHash = prd->getHashAndIndex().collHash(); // idHash of collection
52 
53  const CONT& cont = *dh; // container
54  auto coll = cont.indexFindPtr(idHash); //matching collection
55 
56  // does coll exist?
57  // check prd exists in collection
58  // check pointer value the same.
59  if ( (coll!=nullptr)&& (coll->size()>oindex) && (prd==(*coll)[oindex]) ) {
60  // okay, so we found the correct PRD in the container.
61  // Return EL components.
62  // dh.key() is the name of the container. oindex is the index within the collection. IdHash????
63  key = dh.key();
64  index = prd->getHashAndIndex().hashAndIndex();
65  return; //exit loop and function. We're done.
66  }
67  }
68  // so, we obviously didn't have the correct container (or something else went wrong)
69  MsgStream log(&(*m_msgSvc), name());
70  log << MSG::ERROR<<"Could not find matching PRD container for this ROT. ROT will be written out in incomplete state. "
71  << "Dumping ROT: "<<*rot<<endmsg;
72 
73  return;
74 }
75 
76 template <class CONT, class ROT>
77 void Trk::ITrkEventCnvTool::prepareRIO_OnTrackElementLink(ROT* rot) const
78 {
79  ELKey_t key;
80  ELIndex_t index;
81  prepareRIO_OnTrackElementLink<CONT> (rot, key, index);
82  rot->m_rio.resetWithKeyAndIndex (key, index);
83 }
84 
85 inline void Trk::ITrkEventCnvTool::setRoT_Values(std::pair<const Trk::TrkDetElementBase *, const Trk::PrepRawData *>& pair, Trk::RIO_OnTrack *RoT) const {
86  RoT->setValues(pair.first, pair.second);
87 }
88 
89 template <class CONT, class ROT>
90 bool Trk::ITrkEventCnvTool::getHashAndIndex(const ROT* rot,
91  const SG::ReadHandleKey<CONT>& contName,
92  typename ElementLink<CONT>::index_type& hashAndIndex) const {
93  if (rot==nullptr) return false;
94 
95  const Trk::PrepRawData* prd{rot->prepRawData()};
96  if (prd==nullptr) return false;
97 
98  SG::ReadHandle<CONT> cont (contName);
99  if (not cont.isValid()) return false;
100 
101  const IdentContIndex& idContIndex{prd->getHashAndIndex()};
102  const IdentifierHash& idHash{idContIndex.collHash()}; // idHash of collection
103  auto contItr = cont->indexFindPtr(idHash); //matching collection
104  if (contItr==nullptr) return false;
105 
106  unsigned int index{idContIndex.objIndex()}; // prd index within collection
107  if ((contItr->size()>index) and (prd==(*contItr)[index])) {
108  hashAndIndex = idContIndex.hashAndIndex();
109  return true;
110  }
111 
112  return false;
113 }