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