ATLAS Offline Software
MMPrepDataContainerCnv_p1.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
13 
14 // Gaudi
15 #include "GaudiKernel/Bootstrap.h"
16 #include "GaudiKernel/ISvcLocator.h"
17 
18 // Athena
19 #include "StoreGate/StoreGateSvc.h"
20 
22  // Do not initialize again:
23  m_isInitialized = true;
24 
25  // Get Storegate, ID helpers, and so on
26  ISvcLocator* svcLocator = Gaudi::svcLocator();
27  // get StoreGate service
28  StatusCode sc = svcLocator->service("StoreGateSvc", m_storeGate);
29  if (sc.isFailure()) {
30  log << MSG::FATAL << "StoreGate service not found !" << endmsg;
31  return StatusCode::FAILURE;
32  }
33 
34  // get DetectorStore service
36  sc = svcLocator->service("DetectorStore", detStore);
37  if (sc.isFailure()) {
38  log << MSG::FATAL << "DetectorStore service not found !" << endmsg;
39  return StatusCode::FAILURE;
40  } else {
41  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found DetectorStore." << endmsg;
42  }
43 
44  // Get the helper from the detector store
45  sc = detStore->retrieve(m_MMId);
46  if (sc.isFailure()) {
47  log << MSG::FATAL << "Could not get MM IdHelper !" << endmsg;
48  return StatusCode::FAILURE;
49  } else {
50  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found the MM IdHelper." << endmsg;
51  }
52 
53  if (m_eventCnvTool.retrieve().isFailure()) {
54  log << MSG::FATAL << "Could not get DetectorDescription manager" << endmsg;
55  return StatusCode::FAILURE;
56  }
57 
58  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized." << endmsg;
59  return StatusCode::SUCCESS;
60 }
62  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
63  if (!cnv_tool) return nullptr;
64  return dynamic_cast<const MuonGM::MMReadoutElement*>(cnv_tool->getDetectorElement(id));
65 }
67  MsgStream& log) {
68  if (!m_isInitialized) {
69  if (this->initialize(log) != StatusCode::SUCCESS) {
70  log << MSG::FATAL << "Could not initialize MMPrepDataContainerCnv_p1 " << endmsg;
71  }
72  }
73  // The transient model has a container holding collections (= 64 modules in MmIdHelper
74  // each consisting of two MicroMegas quadruplets in reality) and the collections hold channels.
75  //
76  // The persistent model flattens this so that the persistent
77  // container has two vectors:
78  // 1) all collections, and
79  // 2) all PRDs
80  //
81  // The persistent collections, then only maintain indexes into the
82  // container's vector of all channels.
83  //
84  // So here we loop over all collection and add their channels
85  // to the container's vector, saving the indexes in the collection.
86 
87  MMPrepDataCnv_p1 chanCnv;
88  Muon::MMPrepDataContainer::const_iterator it_Coll = transCont->begin();
89  Muon::MMPrepDataContainer::const_iterator it_CollEnd = transCont->end();
90  unsigned int pcollEnd = 0; // index to end
91  int numColl = transCont->numberOfCollections();
92  persCont->m_collections.resize(numColl);
93  if (log.level() <= MSG::DEBUG) { log << MSG::DEBUG << " Preparing " << persCont->m_collections.size() << "collections" << endmsg; }
94 
95  for (unsigned int pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
96  const Muon::MMPrepDataCollection& collection = (**it_Coll);
97  if (log.level() <= MSG::DEBUG) {
98  log << MSG::DEBUG << "Coll hash for " << pcollIndex << ": " << collection.identifyHash() << endmsg;
99  }
100  // Add in new collection
101  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; // get ref to collection we're going to fill
102 
103  pcollEnd += collection.size();
104 
105  pcollection.m_hashId = collection.identifyHash(); // the collection hash is the module hash in MmIdHelper
106  pcollection.m_id = collection.identify().get_identifier32().get_compact();
107  pcollection.m_size = collection.size();
108  // Add in channels
109  persCont->m_prds.reserve(pcollEnd);
110  persCont->m_prdDeltaId.reserve(pcollEnd);
111 
112  for (unsigned int i = 0; i < collection.size(); ++i) {
113  const MMPrepData* chan = collection[i]; // channel being converted
114  MMPrepData_p1 pchan{}; // persistent version to fill
115  const Identifier chan_id = chan->identify();
116  chanCnv.transToPers(chan, &pchan, log); // convert from MMPrepData to MMPrepData_p1
117 
118  // persCont->m_prdDeltaId is of data type unsigned short, thus we need to encode the channel (starting from the
119  // collection (module) is in contained) into 16 bits, we do it by storing multilayer, gasGap and channel
120  int multilayer = (m_MMId->multilayer(chan_id) - 1); // ranges between 1-2 (1bit)
121  int gasGap = (m_MMId->gasGap(chan_id) - 1); // ranges between 1-4 (2bits)
122  int channel = (m_MMId->channel(chan_id) - MmIdHelper::channelMin()); // ranges between 1-5012 (13bits)
123 
124  // created an unsigned short and store multilayer, gasGap and channel by bit-shifts
125  unsigned short diff = (channel << 3 | gasGap << 1 | multilayer);
126  if (log.level() <= MSG::DEBUG) {
127  log << MSG::DEBUG << "Translated id=" << chan_id.get_compact() << " (multilayer=" << multilayer << ", gasGap=" << gasGap
128  << ", channel=" << channel << ") into diff=" << diff << endmsg;
129  }
130 
131  persCont->m_prdDeltaId.emplace_back(diff); // store delta identifiers, rather than full identifiers
132 
133  const MuonGM::MMReadoutElement* ele_from_mgr = getReadOutElement(chan_id);
134  if (log.level() <= MSG::WARNING && chan->detectorElement()->identify() != ele_from_mgr->identify()) {
135  log << MSG::WARNING << "DE from det manager (" << m_MMId->print_to_string(ele_from_mgr->identify())
136  << ") does not match that from PRD (" << m_MMId->print_to_string(chan->detectorElement()->identify()) << ") for PRD "
137  << m_MMId->print_to_string(chan_id) << endmsg;
138  }
139  persCont->m_prds.emplace_back(std::move(pchan));
140  }
141  }
142  log << MSG::DEBUG << " *** Writing MMPrepDataContainer ***" << endmsg;
143 }
144 
146  MsgStream& log) {
147  // The transient model has a container holding collections (= 64 modules in MmIdHelper
148  // each consisting of two MicroMegas quadruplets in reality) and the collections hold channels.
149  //
150  // The persistent model flattens this so that the persistent
151  // container has two vectors:
152  // 1) all collections, and
153  // 2) all channels
154  //
155  // The persistent collections, then only maintain indexes into the
156  // container's vector of all channels.
157  //
158  // So here we loop over all collection and extract their channels
159  // from the vector.
160 
161  Muon::MMPrepDataCollection* coll = nullptr;
162 
163  MMPrepDataCnv_p1 chanCnv;
164  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
165  unsigned int pCollEnd = persCont->m_collections.size();
166  if (log.level() <= MSG::DEBUG) { log << MSG::DEBUG << " Reading " << pCollEnd << "Collections" << endmsg; }
167  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
168  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
169  IdentifierHash collIDHash(pcoll.m_hashId);
170  if (log.level() <= MSG::DEBUG) { log << MSG::DEBUG << "Coll hash for " << pcollIndex << ": " << collIDHash << endmsg; }
171 
172  coll = new Muon::MMPrepDataCollection(collIDHash);
173  coll->setIdentifier(Identifier(pcoll.m_id));
174 
175  unsigned int pchanEnd = pchanIndex + pcoll.m_size;
176  unsigned int chanIndex = 0; // transient index
177 
178  coll->reserve(pcoll.m_size);
179  // Fill with channels
180  for (; pchanIndex < pchanEnd; ++pchanIndex, ++chanIndex) {
181  const MMPrepData_p1* pchan = &(persCont->m_prds[pchanIndex]);
182 
184  unsigned short diff = persCont->m_prdDeltaId[pchanIndex];
185  // we need to redo the bit-shift to retrieve channel, gasGap and multilayer
186  int channel = (diff >> 3);
187  int gasGap = (3 & (diff >> 1));
188  int multilayer = (1 & diff);
189  Identifier clusId = m_MMId->channelID(Identifier(pcoll.m_id), multilayer + 1, gasGap + 1, channel + MmIdHelper::channelMin());
190  if (log.level() <= MSG::DEBUG) {
191  log << MSG::DEBUG << "Diff of " << diff << " translated into multilayer=" << multilayer << ", gasGap=" << gasGap
192  << ", channel=" << channel << " -> id=" << clusId.get_compact() << endmsg;
193  }
194 
195  const MuonGM::MMReadoutElement* detEl = getReadOutElement(clusId);
196  if (!detEl) {
197  if (log.level() <= MSG::WARNING) {
198  log << MSG::WARNING << "Muon::MMPrepDataContainerCnv_p1::persToTrans: could not get valid det element for PRD with id="
199  << m_MMId->show_to_string(clusId) << ". Skipping." << endmsg;
200  }
201  continue;
202  }
203 
204  auto chan = std::make_unique<MMPrepData>(chanCnv.createMMPrepData(pchan, clusId, detEl, log));
205 
206  if (!m_MMId->valid(chan->identify())) {
207  log << MSG::WARNING << "MM PRD has invalid Identifier of " << chan->identify().get_compact()
208  << " - are you sure you have the correct geometry loaded, and NSW enabled?" << endmsg;
209  }
210 
211  // chanCnv.persToTrans(pchan, chan, log); // Fill chan with data from pchan FIXME! Put this back once diff is sane.
212  if (log.level() <= MSG::DEBUG) { log << MSG::DEBUG << "chan identify(): " << chan->identify().get_compact() << endmsg; }
213 
214  chan->setHashAndIndex(collIDHash, chanIndex);
215  coll->push_back(std::move(chan));
216  }
217 
218  // register the rdo collection in IDC with hash - faster addCollection
219  StatusCode sc = transCont->addCollection(coll, collIDHash);
220  if (sc.isFailure()) { throw std::runtime_error("Failed to add collection to Identifiable Container"); }
221  if (log.level() <= MSG::DEBUG) {
222  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int)collIDHash << " / "
223  << coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
224  }
225  }
226  if (log.level() <= MSG::DEBUG) { log << MSG::DEBUG << " *** Reading MMPrepDataContainer ***" << endmsg; }
227 }
228 
230  if (!m_isInitialized) {
231  if (this->initialize(log) != StatusCode::SUCCESS) {
232  log << MSG::FATAL << "Could not initialize MMPrepDataContainerCnv_p1 " << endmsg;
233  return nullptr;
234  }
235  }
236  std::unique_ptr<Muon::MMPrepDataContainer> trans = std::make_unique<Muon::MMPrepDataContainer>(m_MMId->module_hash_max());
237  persToTrans(persObj, trans.get(), log);
238  return (trans.release());
239 }
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
dumpTgcDigiDeadChambers.gasGap
list gasGap
Definition: dumpTgcDigiDeadChambers.py:33
Muon::MMPrepData
Class to represent MM measurements.
Definition: MMPrepData.h:22
Muon::MMPrepDataContainerCnv_p1::m_MMId
const MmIdHelper * m_MMId
Definition: MMPrepDataContainerCnv_p1.h:41
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:28
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::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::MMPrepDataContainerCnv_p1::m_storeGate
StoreGateSvc * m_storeGate
Definition: MMPrepDataContainerCnv_p1.h:42
MMPrepDataCnv_p1.h
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
MMPrepDataCnv_p1
Definition: MMPrepDataCnv_p1.h:22
MMPrepDataContainer.h
Muon::MMPrepDataContainerCnv_p1::createTransient
virtual Muon::MMPrepDataContainer * createTransient(const Muon::MMPrepDataContainer_p1 *persObj, MsgStream &log)
Definition: MMPrepDataContainerCnv_p1.cxx:229
MMPrepDataCnv_p1::createMMPrepData
static Muon::MMPrepData createMMPrepData(const Muon::MMPrepData_p1 *persObj, Identifier clusId, const MuonGM::MMReadoutElement *detEl, MsgStream &log)
function used to create an MMPrepDataObject when converting Pers to trans
Definition: MMPrepDataCnv_p1.cxx:15
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
MMPrepData_p1.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
ITrkEventCnvTool.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
Identifier32::get_compact
value_type get_compact(void) const
Get the compact id.
Definition: Identifier32.h:171
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::MMPrepDataContainerCnv_p1::initialize
StatusCode initialize(MsgStream &log)
Definition: MMPrepDataContainerCnv_p1.cxx:21
MMPrepData.h
Muon::MuonPrepDataCollection::identify
virtual Identifier identify() const override final
MmIdHelper.h
Trk::ITrkEventCnvTool
Definition: ITrkEventCnvTool.h:31
IdentifiableContainerMT::end
const_iterator end() const
return const_iterator for end of container
Definition: IdentifiableContainerMT.h:242
IdentifiableContainerMT::const_iterator
Definition: IdentifiableContainerMT.h:82
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::MMPrepDataContainerCnv_p1::m_isInitialized
bool m_isInitialized
Definition: MMPrepDataContainerCnv_p1.h:44
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::MMPrepData_p1
We don't write out (from Trk::PrepRawData) m_indexAndHash (can be recomputed), m_clusId (can be recom...
Definition: MMPrepData_p1.h:20
Muon::MuonPRD_Collection_p2::m_hashId
unsigned int m_hashId
Hash Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:46
Muon::MMPrepDataCollection
MuonPrepDataCollection< MMPrepData > MMPrepDataCollection
Definition: MuonPrepDataCollection.h:113
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.
Muon::MMPrepDataContainerCnv_p1::transToPers
virtual void transToPers(const Muon::MMPrepDataContainer *transCont, Muon::MMPrepDataContainer_p1 *persCont, MsgStream &log)
Definition: MMPrepDataContainerCnv_p1.cxx:66
Trk::ITrkEventCnvTool::getDetectorElement
virtual const Trk::TrkDetElementBase * getDetectorElement(const Identifier &id, const IdentifierHash &idHash) const =0
Returns the detectorElement associated with this Identifier & Hash.
MmIdHelper::channelMin
static int channelMin()
Definition: MmIdHelper.cxx:829
DEBUG
#define DEBUG
Definition: page_access.h:11
MuonGM::MuonReadoutElement::identify
Identifier identify() const override final
Returns the ATLAS Identifier of the MuonReadOutElement.
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/MuonReadoutElement.h:184
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
MuonGM::MMReadoutElement
An MMReadoutElement corresponds to a single STGC module; therefore typicaly a barrel muon station con...
Definition: MMReadoutElement.h:23
MMPrepDataContainerCnv_p1.h
Muon::MMPrepDataContainerCnv_p1::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: MMPrepDataContainerCnv_p1.h:43
Muon::MMPrepDataContainerCnv_p1::persToTrans
virtual void persToTrans(const Muon::MMPrepDataContainer_p1 *persCont, Muon::MMPrepDataContainer *transCont, MsgStream &log)
Definition: MMPrepDataContainerCnv_p1.cxx:145
MMPrepDataCnv_p1::transToPers
void transToPers(const Muon::MMPrepData *transObj, Muon::MMPrepData_p1 *persObj, MsgStream &log)
Definition: MMPrepDataCnv_p1.cxx:65
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.
StoreGateSvc.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Muon::MMPrepDataContainerCnv_p1::getReadOutElement
const MuonGM::MMReadoutElement * getReadOutElement(const Identifier &id) const
Definition: MMPrepDataContainerCnv_p1.cxx:61
Muon::MuonPRD_Collection_p2::m_id
unsigned int m_id
Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:43