ATLAS Offline Software
MdtPrepDataContainerCnv_p2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 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
19 #include "StoreGate/StoreGateSvc.h"
20 
21 
23  // Do not initialize again:
24  m_isInitialized=true;
25 
26  // Get Storegate, ID helpers, and so on
27  ISvcLocator* svcLocator = Gaudi::svcLocator();
28  // get StoreGate service
29  StatusCode sc = svcLocator->service("StoreGateSvc", m_storeGate);
30  if (sc.isFailure()) {
31  log << MSG::FATAL << "StoreGate service not found !" << endmsg;
32  return StatusCode::FAILURE;
33  }
34 
35  // get DetectorStore service
37  sc = svcLocator->service("DetectorStore", detStore);
38  if (sc.isFailure()) {
39  log << MSG::FATAL << "DetectorStore service not found !" << endmsg;
40  return StatusCode::FAILURE;
41  } else {
42  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found DetectorStore." << endmsg;
43  }
44 
45  // Get the helper from the detector store
46  sc = detStore->retrieve(m_MdtId);
47  if (sc.isFailure()) {
48  log << MSG::FATAL << "Could not get ID helper !" << endmsg;
49  return StatusCode::FAILURE;
50  } else {
51  if (log.level() <= MSG::DEBUG)log << MSG::DEBUG << "Found the ID helper." << endmsg;
52  }
53 
54  if (m_eventCnvTool.retrieve().isFailure()) {
55  log << MSG::FATAL << "Could not get DetectorDescription manager" << endmsg;
56  return StatusCode::FAILURE;
57  }
58 
59  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized." << endmsg;
60  return StatusCode::SUCCESS;
61 }
62 
64  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
65  if (!cnv_tool) return nullptr;
66  return dynamic_cast<const MuonGM::MdtReadoutElement*>(cnv_tool->getDetectorElement(id));
67 }
68 
70 {
71 
72  // The transient model has a container holding collections and the
73  // collections hold channels.
74  //
75  // The persistent model flattens this so that the persistent
76  // container has two vectors:
77  // 1) all collections, and
78  // 2) all PRDs
79  //
80  // The persistent collections, then only maintain indexes into the
81  // container's vector of all channels.
82  //
83  // So here we loop over all collection and add their channels
84  // to the container's vector, saving the indexes in the
85  // collection.
86 
87  typedef Muon::MdtPrepDataContainer TRANS;
88 
89  MdtPrepDataCnv_p2 chanCnv;
90  TRANS::const_iterator it_Coll = transCont->begin();
91  TRANS::const_iterator it_CollEnd = transCont->end();
92  unsigned int pcollIndex =0; // index to the persistent collection we're filling
93  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
94  unsigned int pcollEnd = 0; // index to end
95  int numColl = transCont->numberOfCollections();
96  persCont->m_collections.resize(numColl);
97 
98  if (log.level() <= MSG::DEBUG)
99  log << MSG::DEBUG<< " Preparing " << persCont->m_collections.size() << "Collections" <<endmsg;
100  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
101  // Add in new collection
102  if (log.level() <= MSG::DEBUG)
103  log << MSG::DEBUG<<"New collection"<<endmsg;
104  const Muon::MdtPrepDataCollection& collection = (**it_Coll);
105  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
106 
107  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
108  pcollEnd += collection.size();
109 
110  pcollection.m_hashId = collection.identifyHash();
111  pcollection.m_id = collection.identify().get_identifier32().get_compact();
112  pcollection.m_size = collection.size();
113 
114  // Add in channels
115  persCont->m_prds.resize(pcollEnd); // FIXME! isn't this potentially a bit slow? Do a resize and a copy for each loop? EJWM.
116  persCont->m_prdDeltaId.resize(pcollEnd);
117 
118  for (unsigned int i = 0; i < collection.size(); ++i) {
119  unsigned int pchanIndex=i+pcollBegin;
120  const MdtPrepData* chan = collection[i]; // channel being converted
121  MdtPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
122  chanCnv.transToPers(chan, pchan, log); // convert from MdtPrepData to MdtPrepData_p2
123 
124  persCont->m_prdDeltaId[pchanIndex]=chan->identify().get_identifier32().get_compact() - collection.identify().get_identifier32().get_compact(); //store delta identifiers, rather than full identifiers
125  }
126  }
127  if (log.level() <= MSG::DEBUG)
128  log << MSG::DEBUG<< " *** Writing MdtPrepDataContainer ***" <<endmsg;
129 }
130 
132 {
133 
134  // The transient model has a container holding collections and the
135  // collections hold channels.
136  //
137  // The persistent model flattens this so that the persistent
138  // container has two vectors:
139  // 1) all collections, and
140  // 2) all channels
141  //
142  // The persistent collections, then only maintain indexes into the
143  // container's vector of all channels.
144  //
145  // So here we loop over all collection and extract their channels
146  // from the vector.
147 
148  Muon::MdtPrepDataCollection* coll = nullptr;
149 
150  MdtPrepDataCnv_p2 chanCnv;
151  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
152  unsigned int pCollEnd = persCont->m_collections.size();
153  if (log.level() <= MSG::DEBUG)
154  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
155  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
156  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
157  IdentifierHash collIDHash(pcoll.m_hashId);
158  coll = new Muon::MdtPrepDataCollection(collIDHash);
159  coll->setIdentifier(Identifier(pcoll.m_id));
160 
161 
162  // FIXME - really would like to remove Identifier from collection, but cannot as there is :
163  // a) no way (apparently - find it hard to believe) to go from collection IdHash to collection Identifer.
164 
165  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
166  unsigned int chanIndex = 0; // transient index
167 
168  coll->reserve(pcoll.m_size);
169  // Fill with channels
170  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
171  const MdtPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]);
172 
173  Identifier clusId (pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
174 
175  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
176  IdentifierHash deIDHash;
177  int result = m_MdtId->get_detectorElement_hash(clusId, deIDHash);
178  if (result&&log.level() <= MSG::WARNING)
179  log << MSG::WARNING<< " Muon::MdtPrepDataContainerCnv_p2::persToTrans: problem converting Identifier to DE hash "<<endmsg;
180  const MuonGM::MdtReadoutElement* detEl = getReadOutElement(clusId);
181 
182  auto chan = std::make_unique<MdtPrepData>
183  (chanCnv.createMdtPrepData (pchan,
184  clusId,
185  detEl,
186  log));
187 
188  chan->setHashAndIndex(collIDHash, chanIndex);
189  coll->push_back(std::move(chan));
190  }
191 
192  // register the rdo collection in IDC with hash - faster addCollection
193  StatusCode sc = transCont->addCollection(coll, collIDHash);
194  if (sc.isFailure()) {
195  throw std::runtime_error("Failed to add collection to Identifiable Container");
196  }
197  if (log.level() <= MSG::DEBUG) {
198  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
199  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
200  }
201  }
202 
203  if (log.level() <= MSG::DEBUG)
204  log << MSG::DEBUG<< " *** Reading MdtPrepDataContainer ***" << endmsg;
205 }
206 
207 
208 
209 //================================================================
211 {
212  if(!m_isInitialized) {
213  if (this->initialize(log) != StatusCode::SUCCESS) {
214  log << MSG::FATAL << "Could not initialize MdtPrepDataContainerCnv_p2 " << endmsg;
215  return nullptr;
216  }
217  }
218  std::unique_ptr<Muon::MdtPrepDataContainer> trans(new Muon::MdtPrepDataContainer(m_MdtId->module_hash_max()));
219  persToTrans(persObj, trans.get(), log);
220  return(trans.release());
221 }
222 
223 
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:22
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
MdtPrepData.h
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Muon::MdtPrepDataContainerCnv_p2::m_MdtId
const MdtIdHelper * m_MdtId
Definition: MdtPrepDataContainerCnv_p2.h:41
MdtPrepDataContainerCnv_p2.h
ITrkEventCnvTool.h
MdtPrepDataContainer.h
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:128
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
Muon::MdtPrepDataContainerCnv_p2::m_storeGate
StoreGateSvc * m_storeGate
Definition: MdtPrepDataContainerCnv_p2.h:42
MuonPRD_Container_p2.h
Identifier32::get_compact
value_type get_compact(void) const
Get the compact id.
Definition: Identifier32.h:171
MuonGM::MdtReadoutElement
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/MdtReadoutElement.h:50
Muon::MdtPrepDataContainerCnv_p2::m_isInitialized
bool m_isInitialized
Definition: MdtPrepDataContainerCnv_p2.h:44
lumiFormat.i
int i
Definition: lumiFormat.py:92
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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
Muon::MdtPrepDataContainerCnv_p2::getReadOutElement
const MuonGM::MdtReadoutElement * getReadOutElement(const Identifier &id) const
Definition: MdtPrepDataContainerCnv_p2.cxx:63
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
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:37
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:210
MdtPrepDataCnv_p2::transToPers
void transToPers(const Muon::MdtPrepData *transObj, Muon::MdtPrepData_p2 *persObj, MsgStream &log)
Definition: MdtPrepDataCnv_p2.cxx:54
Muon::MdtPrepDataContainerCnv_p2::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: MdtPrepDataContainerCnv_p2.h:43
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
Identifier::get_compact
value_type get_compact(void) const
Get the compact id.
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:69
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
IdentifierHash
Definition: IdentifierHash.h:38
Identifier::get_identifier32
Identifier32 get_identifier32(void) const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
Muon::MdtPrepDataContainerCnv_p2::persToTrans
virtual void persToTrans(const Muon::MdtPrepDataContainer_p2 *persCont, Muon::MdtPrepDataContainer *transCont, MsgStream &log)
Definition: MdtPrepDataContainerCnv_p2.cxx:131
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