ATLAS Offline Software
MdtPrepDataContainerCnv_p2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
13 
14 // Gaudi
15 #include "GaudiKernel/ISvcLocator.h"
16 #include "GaudiKernel/Bootstrap.h"
17 
18 // Athena
20 #include "StoreGate/StoreGateSvc.h"
21 
22 
24  // Do not initialize again:
25  m_isInitialized=true;
26 
27  // get DetectorStore service
28  SmartIF<StoreGateSvc> detStore{Gaudi::svcLocator()->service("DetectorStore")};
29  CHECK( detStore.isValid() );
30 
31  // Get the helper from the detector store
32  CHECK( detStore->retrieve(m_MdtId) );
33 
34  CHECK( m_eventCnvTool.retrieve() );
35 
36  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized." << endmsg;
37  return StatusCode::SUCCESS;
38 }
39 
41  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
42  if (!cnv_tool) return nullptr;
43  return dynamic_cast<const MuonGM::MdtReadoutElement*>(cnv_tool->getDetectorElement(id));
44 }
45 
47 {
48 
49  // The transient model has a container holding collections and the
50  // collections hold channels.
51  //
52  // The persistent model flattens this so that the persistent
53  // container has two vectors:
54  // 1) all collections, and
55  // 2) all PRDs
56  //
57  // The persistent collections, then only maintain indexes into the
58  // container's vector of all channels.
59  //
60  // So here we loop over all collection and add their channels
61  // to the container's vector, saving the indexes in the
62  // collection.
63 
64  typedef Muon::MdtPrepDataContainer TRANS;
65 
66  MdtPrepDataCnv_p2 chanCnv;
67  TRANS::const_iterator it_Coll = transCont->begin();
68  TRANS::const_iterator it_CollEnd = transCont->end();
69  unsigned int pcollIndex =0; // index to the persistent collection we're filling
70  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
71  unsigned int pcollEnd = 0; // index to end
72  int numColl = transCont->numberOfCollections();
73  persCont->m_collections.resize(numColl);
74 
75  if (log.level() <= MSG::DEBUG)
76  log << MSG::DEBUG<< " Preparing " << persCont->m_collections.size() << "Collections" <<endmsg;
77  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
78  // Add in new collection
79  if (log.level() <= MSG::DEBUG)
80  log << MSG::DEBUG<<"New collection"<<endmsg;
81  const Muon::MdtPrepDataCollection& collection = (**it_Coll);
82  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
83 
84  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
85  pcollEnd += collection.size();
86 
87  pcollection.m_hashId = collection.identifyHash();
88  pcollection.m_id = collection.identify().get_identifier32().get_compact();
89  pcollection.m_size = collection.size();
90 
91  // Add in channels
92  persCont->m_prds.resize(pcollEnd); // FIXME! isn't this potentially a bit slow? Do a resize and a copy for each loop? EJWM.
93  persCont->m_prdDeltaId.resize(pcollEnd);
94 
95  for (unsigned int i = 0; i < collection.size(); ++i) {
96  unsigned int pchanIndex=i+pcollBegin;
97  const MdtPrepData* chan = collection[i]; // channel being converted
98  MdtPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
99  chanCnv.transToPers(chan, pchan, log); // convert from MdtPrepData to MdtPrepData_p2
100 
101  persCont->m_prdDeltaId[pchanIndex]=chan->identify().get_identifier32().get_compact() - collection.identify().get_identifier32().get_compact(); //store delta identifiers, rather than full identifiers
102  }
103  }
104  if (log.level() <= MSG::DEBUG)
105  log << MSG::DEBUG<< " *** Writing MdtPrepDataContainer ***" <<endmsg;
106 }
107 
109 {
110 
111  // The transient model has a container holding collections and the
112  // collections hold channels.
113  //
114  // The persistent model flattens this so that the persistent
115  // container has two vectors:
116  // 1) all collections, and
117  // 2) all channels
118  //
119  // The persistent collections, then only maintain indexes into the
120  // container's vector of all channels.
121  //
122  // So here we loop over all collection and extract their channels
123  // from the vector.
124 
125  Muon::MdtPrepDataCollection* coll = nullptr;
126 
127  MdtPrepDataCnv_p2 chanCnv;
128  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
129  unsigned int pCollEnd = persCont->m_collections.size();
130  if (log.level() <= MSG::DEBUG)
131  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
132  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
133  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
134  IdentifierHash collIDHash(pcoll.m_hashId);
135  coll = new Muon::MdtPrepDataCollection(collIDHash);
136  coll->setIdentifier(Identifier(pcoll.m_id));
137 
138 
139  // FIXME - really would like to remove Identifier from collection, but cannot as there is :
140  // a) no way (apparently - find it hard to believe) to go from collection IdHash to collection Identifer.
141 
142  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
143  unsigned int chanIndex = 0; // transient index
144 
145  coll->reserve(pcoll.m_size);
146  // Fill with channels
147  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
148  const MdtPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]);
149 
150  Identifier clusId (pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
151 
152  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
153  IdentifierHash deIDHash;
154  int result = m_MdtId->get_detectorElement_hash(clusId, deIDHash);
155  if (result&&log.level() <= MSG::WARNING)
156  log << MSG::WARNING<< " Muon::MdtPrepDataContainerCnv_p2::persToTrans: problem converting Identifier to DE hash "<<endmsg;
157  const MuonGM::MdtReadoutElement* detEl = getReadOutElement(clusId);
158 
159  auto chan = std::make_unique<MdtPrepData>
160  (chanCnv.createMdtPrepData (pchan,
161  clusId,
162  detEl,
163  log));
164 
165  chan->setHashAndIndex(collIDHash, chanIndex);
166  coll->push_back(std::move(chan));
167  }
168 
169  // register the rdo collection in IDC with hash - faster addCollection
170  StatusCode sc = transCont->addCollection(coll, collIDHash);
171  if (sc.isFailure()) {
172  throw std::runtime_error("Failed to add collection to Identifiable Container");
173  }
174  if (log.level() <= MSG::DEBUG) {
175  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
176  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
177  }
178  }
179 
180  if (log.level() <= MSG::DEBUG)
181  log << MSG::DEBUG<< " *** Reading MdtPrepDataContainer ***" << endmsg;
182 }
183 
184 
185 
186 //================================================================
188 {
189  if(!m_isInitialized) {
190  if (this->initialize(log) != StatusCode::SUCCESS) {
191  log << MSG::FATAL << "Could not initialize MdtPrepDataContainerCnv_p2 " << endmsg;
192  return nullptr;
193  }
194  }
195  std::unique_ptr<Muon::MdtPrepDataContainer> trans(new Muon::MdtPrepDataContainer(m_MdtId->module_hash_max()));
196  persToTrans(persObj, trans.get(), log);
197  return(trans.release());
198 }
199 
200 
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
Muon::MuonPrepDataContainer
Template for Muon PRD containers (which are basically collections of MuonPrepDataCollections).
Definition: MuonPrepDataContainer.h:42
get_generator_info.result
result
Definition: get_generator_info.py:21
MdtPrepData_p2.h
MdtPrepDataCnv_p2.h
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
IdentifiableContainerMT::addCollection
virtual StatusCode addCollection(const T *coll, IdentifierHash hashId) override final
insert collection into container with id hash if IDC should not take ownership of collection,...
Definition: IdentifiableContainerMT.h:300
Muon::MdtPrepDataContainerCnv_p2::initialize
StatusCode initialize(MsgStream &log)
Definition: MdtPrepDataContainerCnv_p2.cxx:23
Muon::MuonPRD_Container_p2::m_prds
std::vector< PRD > m_prds
Definition: MuonPRD_Container_p2.h:31
initialize
void initialize()
Definition: run_EoverP.cxx:894
Muon::MdtPrepDataCollection
MuonPrepDataCollection< MdtPrepData > MdtPrepDataCollection
Definition: MuonPrepDataCollection.h:106
Identifier::get_identifier32
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
Identifier::get_compact
value_type get_compact() const
Get the compact id.
MdtPrepData.h
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
Identifier32::get_compact
value_type get_compact() const
Get the compact id.
Definition: Identifier32.h:44
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Muon::MdtPrepDataContainerCnv_p2::m_MdtId
const MdtIdHelper * m_MdtId
Definition: MdtPrepDataContainerCnv_p2.h:40
MdtPrepDataContainerCnv_p2.h
ITrkEventCnvTool.h
MdtPrepDataContainer.h
Muon::MuonPrepDataCollection::identifyHash
virtual IdentifierHash identifyHash() const override final
IdentifiableContainerMT::numberOfCollections
virtual size_t numberOfCollections() const override final
return number of collections
Definition: IdentifiableContainerMT.h:216
Muon::MuonPRD_Collection_p2::m_size
unsigned short m_size
Collection size into master collection Note I use a short.
Definition: MuonPRD_Collection_p2.h:51
MuonPRD_Container_p2.h
MuonGM::MdtReadoutElement
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/MdtReadoutElement.h:51
Muon::MdtPrepDataContainerCnv_p2::m_isInitialized
bool m_isInitialized
Definition: MdtPrepDataContainerCnv_p2.h:42
lumiFormat.i
int i
Definition: lumiFormat.py:85
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
Muon::MuonPRD_Collection_p2
Class to hold the persistent representation of MuonPRD_Collection.
Definition: MuonPRD_Collection_p2.h:22
Muon::MdtPrepData_p2
Persistent representation of the transient Muon::MdtPrepData class.
Definition: MdtPrepData_p2.h:24
MdtPrepDataCnv_p2::createMdtPrepData
static Muon::MdtPrepData createMdtPrepData(const Muon::MdtPrepData_p2 *persObj, const Identifier &id, const MuonGM::MdtReadoutElement *detEl, MsgStream &log)
Definition: MdtPrepDataCnv_p2.cxx:16
Muon::MuonPrepDataCollection::identify
virtual Identifier identify() const override final
Trk::ITrkEventCnvTool
Definition: ITrkEventCnvTool.h:31
MdtIdHelper.h
IdentifiableContainerMT::end
const_iterator end() const
return const_iterator for end of container
Definition: IdentifiableContainerMT.h:242
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
Muon::MdtPrepDataContainerCnv_p2::getReadOutElement
const MuonGM::MdtReadoutElement * getReadOutElement(const Identifier &id) const
Definition: MdtPrepDataContainerCnv_p2.cxx:40
MdtPrepDataCnv_p2
Definition: MdtPrepDataCnv_p2.h:22
IdentifiableContainerMT::begin
const_iterator begin() const
return const_iterator for first entry
Definition: IdentifiableContainerMT.h:236
Muon::MuonPRD_Container_p2::m_collections
std::vector< MuonPRD_Collection_p2 > m_collections
Definition: MuonPRD_Container_p2.h:29
Muon::MuonPrepDataCollection
Template to hold collections of MuonPrepRawData objects.
Definition: MuonPrepDataCollection.h:46
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
Muon::MuonPRD_Container_p2
Class to contain the Muon Prep Raw Data.
Definition: MuonPRD_Container_p2.h:26
errorcheck.h
Helpers for checking error return status codes and reporting errors.
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Muon::MdtPrepData
Class to represent measurements from the Monitored Drift Tubes.
Definition: MdtPrepData.h:33
Muon::MuonPRD_Collection_p2::m_hashId
unsigned int m_hashId
Hash Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:46
Muon::MdtPrepDataContainerCnv_p2::createTransient
virtual Muon::MdtPrepDataContainer * createTransient(const Muon::MdtPrepDataContainer_p2 *persObj, MsgStream &log)
Definition: MdtPrepDataContainerCnv_p2.cxx:187
MdtPrepDataCnv_p2::transToPers
void transToPers(const Muon::MdtPrepData *transObj, Muon::MdtPrepData_p2 *persObj, MsgStream &log)
Definition: MdtPrepDataCnv_p2.cxx:50
Muon::MdtPrepDataContainerCnv_p2::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: MdtPrepDataContainerCnv_p2.h:41
Muon::MuonPRD_Container_p2::m_prdDeltaId
std::vector< unsigned short > m_prdDeltaId
The delta identifiers of the PRD i.e.
Definition: MuonPRD_Container_p2.h:33
Trk::ITrkEventCnvTool::getDetectorElement
virtual const Trk::TrkDetElementBase * getDetectorElement(const Identifier &id, const IdentifierHash &idHash) const =0
Returns the detectorElement associated with this Identifier & Hash.
DEBUG
#define DEBUG
Definition: page_access.h:11
Muon::MdtPrepDataContainerCnv_p2::transToPers
virtual void transToPers(const Muon::MdtPrepDataContainer *transCont, Muon::MdtPrepDataContainer_p2 *persCont, MsgStream &log)
Definition: MdtPrepDataContainerCnv_p2.cxx:46
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
IdentifierHash
This is a "hash" representation of an Identifier. This encodes a 32 bit index which can be used to lo...
Definition: IdentifierHash.h:25
Muon::MdtPrepDataContainerCnv_p2::persToTrans
virtual void persToTrans(const Muon::MdtPrepDataContainer_p2 *persCont, Muon::MdtPrepDataContainer *transCont, MsgStream &log)
Definition: MdtPrepDataContainerCnv_p2.cxx:108
StoreGateSvc.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Muon::MuonPRD_Collection_p2::m_id
unsigned int m_id
Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:43
Identifier
Definition: IdentifierFieldParser.cxx:14