ATLAS Offline Software
RpcPrepDataContainerCnv_p3.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
12 
13 // Gaudi
14 #include "GaudiKernel/ISvcLocator.h"
15 #include "GaudiKernel/Bootstrap.h"
16 
17 // Athena
18 #include "StoreGate/StoreGateSvc.h"
19 
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_RpcId);
46  if (sc.isFailure()) {
47  log << MSG::FATAL << "Could not get ID helper !" << endmsg;
48  return StatusCode::FAILURE;
49  } else {
50  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found the ID helper." << 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 << "RpcPrepDataContainerCnv_p3 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::RpcReadoutElement*>(cnv_tool->getDetectorElement(id));
65 }
67 {
68 
69  // The transient model has a container holding collections and the
70  // collections hold channels.
71  //
72  // The persistent model flattens this so that the persistent
73  // container has two vectors:
74  // 1) all collections, and
75  // 2) all PRDs
76  //
77  // The persistent collections, then only maintain indexes into the
78  // container's vector of all channels.
79  //
80  // So here we loop over all collection and add their channels
81  // to the container's vector, saving the indexes in the
82  // collection.
83 
84  if(log.level() <= MSG::DEBUG && !m_isInitialized) {
85  if (initialize(log) != StatusCode::SUCCESS) {
86  log << MSG::FATAL << "Could not initialize RpcPrepDataContainerCnv_p3 " << endmsg;
87  }
88  }
89 
90  typedef Muon::RpcPrepDataContainer TRANS;
91 
92  RpcPrepDataCnv_p3 chanCnv;
93  TRANS::const_iterator it_Coll = transCont->begin();
94  TRANS::const_iterator it_CollEnd = transCont->end();
95  unsigned int pcollIndex = 0; // index to the persistent collection we're filling
96  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
97  unsigned int pcollEnd = 0; // index to end
98  int numColl = transCont->numberOfCollections();
99  persCont->m_collections.resize(numColl);
100 
101  if (log.level() <= MSG::DEBUG)
102  log << MSG::DEBUG<< "Preparing " << persCont->m_collections.size() << " Collections" <<endmsg;
103  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
104  // Add in new collection
105  if (log.level() <= MSG::DEBUG)
106  log << MSG::DEBUG<<"New collection"<<endmsg;
107  const Muon::RpcPrepDataCollection& collection = (**it_Coll);
108  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
109 
110  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
111  pcollEnd += collection.size();
112 
113  pcollection.m_hashId = collection.identifyHash();
114  pcollection.m_id = collection.identify().get_identifier32().get_compact();
115  pcollection.m_size = collection.size();
116 
117  // Add in channels
118  persCont->m_prds.resize(pcollEnd); // FIXME! isn't this potentially a bit slow? Do a resize and a copy for each loop? EJWM.
119  persCont->m_prdDeltaId.resize(pcollEnd);
120 
121  unsigned int lastPRDIdHash = 0;
122  for (unsigned int i = 0; i < collection.size(); ++i) {
123  unsigned int pchanIndex=i+pcollBegin;
124  const RpcPrepData* chan = collection[i]; // channel being converted
125  RpcPrepData_p3* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
126  chanCnv.transToPers(chan, pchan, log); // convert from RpcPrepData to RpcPrepData_p3
127 
128  unsigned int clusIdCompact = chan->identify().get_identifier32().get_compact();
129  unsigned int collIdCompact = collection.identify().get_identifier32().get_compact();
130 
131  persCont->m_prdDeltaId[pchanIndex]=clusIdCompact - collIdCompact; //store delta identifiers, rather than full identifiers
132  // sanity checks - to be removed at some point
133  if(log.level() <= MSG::DEBUG){
134  log << MSG::DEBUG<<i<<":\t clusId: "<<clusIdCompact<<", \t collectionId="<<collIdCompact<<"\t delta="<<persCont->m_prdDeltaId[pchanIndex]<<endmsg;
135  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
136  if (temp!=chan->identify() )
137  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
138  else
139  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
140  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
141  lastPRDIdHash = chan->collectionHash();
142  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
143  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
144  if (chan->detectorElement() !=getReadOutElement(chan->identify()))
145  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
146  else
147  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
148  if (chan->detectorElement() !=getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
149  log << MSG::DEBUG<<"Finished loop"<<endmsg;
150  }
151  }
152  }
153  if (log.level() <= MSG::DEBUG)
154  log << MSG::DEBUG<< " *** Finished Writing RpcPrepDataContainer ***" <<endmsg;
155 }
156 
158 {
159 
160  // The transient model has a container holding collections and the
161  // collections hold channels.
162  //
163  // The persistent model flattens this so that the persistent
164  // container has two vectors:
165  // 1) all collections, and
166  // 2) all channels
167  //
168  // The persistent collections, then only maintain indexes into the
169  // container's vector of all channels.
170  //
171  // So here we loop over all collection and extract their channels
172  // from the vector.
173 
174  Muon::RpcPrepDataCollection* coll = nullptr;
175 
176  RpcPrepDataCnv_p3 chanCnv;
177  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
178  unsigned int pCollEnd = persCont->m_collections.size();
179  if (log.level() <= MSG::DEBUG)
180  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
181  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
182  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
183  IdentifierHash collIDHash(pcoll.m_hashId);
184  coll = new Muon::RpcPrepDataCollection(collIDHash);
185  coll->setIdentifier(Identifier(pcoll.m_id));
186 
187 
188  // FIXME - really would like to remove Identifier from collection, but cannot as there is :
189  // a) no way (apparently - find it hard to believe) to go from collection IdHash to collection Identifer.
190 
191  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
192  unsigned int chanIndex = 0; // transient index
193 
194  coll->reserve(pcoll.m_size);
195  // Fill with channels
196  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
197  const RpcPrepData_p3* pchan = &(persCont->m_prds[pchanIndex]);
198 
199  Identifier clusId(pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
200 
201  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
202  IdentifierHash deIDHash;
203  int result = m_RpcId->get_detectorElement_hash(clusId, deIDHash);
204  if (result&&log.level() <= MSG::WARNING)
205  log << MSG::WARNING<< " Muon::RpcPrepDataContainerCnv_p3::persToTrans: problem converting Identifier to DE hash "<<endmsg;
206  const MuonGM::RpcReadoutElement* detEl = getReadOutElement(clusId);
207 
208  auto chan = std::make_unique<RpcPrepData>
209  (chanCnv.createRpcPrepData (pchan,
210  clusId,
211  detEl,
212  log));
213 
214 
215  chan->setHashAndIndex(collIDHash, chanIndex);
216  coll->push_back(std::move(chan));
217  }
218 
219  // register the rdo collection in IDC with hash - faster addCollection
220  StatusCode sc = transCont->addCollection(coll, collIDHash);
221  if (sc.isFailure()) {
222  throw std::runtime_error("Failed to add collection to Identifiable Container");
223  }
224  if (log.level() <= MSG::DEBUG) {
225  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
226  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
227  }
228  }
229 
230  if (log.level() <= MSG::DEBUG)
231  log << MSG::DEBUG<< " *** Reading RpcPrepDataContainer ***" << endmsg;
232 }
233 
234 
235 
236 //================================================================
238 {
239  if (log.level() <= MSG::DEBUG)
240  log << MSG::DEBUG<< " Muon::RpcPrepDataContainerCnv_p3::createTransient" << endmsg;
241  if(!m_isInitialized) {
242  if (initialize(log) != StatusCode::SUCCESS) {
243  log << MSG::FATAL << "Could not initialize RpcPrepDataContainerCnv_p3 " << endmsg;
244  return nullptr;
245  }
246  }
247  std::unique_ptr<Muon::RpcPrepDataContainer> trans(new Muon::RpcPrepDataContainer(m_RpcId->module_hash_max()));
248  persToTrans(persObj, trans.get(), log);
249  return(trans.release());
250 }
251 
252 
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
Muon::RpcPrepDataContainerCnv_p3::m_isInitialized
bool m_isInitialized
Definition: RpcPrepDataContainerCnv_p3.h:43
Muon::RpcPrepDataContainerCnv_p3::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: RpcPrepDataContainerCnv_p3.h:42
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
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
RpcPrepDataCnv_p3.h
Muon::MuonPRD_Container_p2::m_prds
std::vector< PRD > m_prds
Definition: MuonPRD_Container_p2.h:31
Muon::RpcPrepDataCollection
MuonPrepDataCollection< RpcPrepData > RpcPrepDataCollection
Definition: MuonPrepDataCollection.h:107
initialize
void initialize()
Definition: run_EoverP.cxx:894
Muon::RpcPrepDataContainerCnv_p3::m_storeGate
StoreGateSvc * m_storeGate
Definition: RpcPrepDataContainerCnv_p3.h:41
MuonGM::RpcReadoutElement
An RpcReadoutElement corresponds to a single RPC module; therefore typicaly a barrel muon station con...
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/RpcReadoutElement.h:54
RpcPrepDataContainer.h
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
RpcPrepDataCnv_p3::transToPers
void transToPers(const Muon::RpcPrepData *transObj, Muon::RpcPrepData_p3 *persObj, MsgStream &log)
Definition: RpcPrepDataCnv_p3.cxx:53
RpcIdHelper.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
RpcPrepDataContainerCnv_p3.h
Muon::RpcPrepData_p3
We don't write out (from Trk::PrepRawData) m_indexAndHash (can be recomputed), m_clusId (can be recom...
Definition: RpcPrepData_p3.h:24
ITrkEventCnvTool.h
RpcPrepDataCnv_p3
Definition: RpcPrepDataCnv_p3.h:22
Muon::RpcPrepDataContainerCnv_p3::getReadOutElement
const MuonGM::RpcReadoutElement * getReadOutElement(const Identifier &id) const
Definition: RpcPrepDataContainerCnv_p3.cxx:61
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
MuonPRD_Container_p2.h
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
Muon::RpcPrepData
Class to represent RPC measurements.
Definition: RpcPrepData.h:35
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
Muon::RpcPrepDataContainerCnv_p3::createTransient
virtual Muon::RpcPrepDataContainer * createTransient(const Muon::RpcPrepDataContainer_p3 *persObj, MsgStream &log)
Definition: RpcPrepDataContainerCnv_p3.cxx:237
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::MuonPrepDataCollection::identify
virtual Identifier identify() const override final
Muon::RpcPrepDataContainerCnv_p3::initialize
StatusCode initialize(MsgStream &log)
Definition: RpcPrepDataContainerCnv_p3.cxx:21
Trk::ITrkEventCnvTool
Definition: ITrkEventCnvTool.h:31
IdentifiableContainerMT::end
const_iterator end() const
return const_iterator for end of container
Definition: IdentifiableContainerMT.h:242
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::RpcPrepDataContainerCnv_p3::m_RpcId
const RpcIdHelper * m_RpcId
Definition: RpcPrepDataContainerCnv_p3.h:40
Muon::MuonPRD_Collection_p2::m_hashId
unsigned int m_hashId
Hash Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:46
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
RpcPrepDataCnv_p3::createRpcPrepData
static Muon::RpcPrepData createRpcPrepData(const Muon::RpcPrepData_p3 *persObj, const Identifier &id, const MuonGM::RpcReadoutElement *detEl, MsgStream &log)
Definition: RpcPrepDataCnv_p3.cxx:16
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
RpcPrepData.h
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::RpcPrepDataContainerCnv_p3::persToTrans
virtual void persToTrans(const Muon::RpcPrepDataContainer_p3 *persCont, Muon::RpcPrepDataContainer *transCont, MsgStream &log)
Definition: RpcPrepDataContainerCnv_p3.cxx:157
Muon::RpcPrepDataContainerCnv_p3::transToPers
virtual void transToPers(const Muon::RpcPrepDataContainer *transCont, Muon::RpcPrepDataContainer_p3 *persCont, MsgStream &log)
Definition: RpcPrepDataContainerCnv_p3.cxx:66
Muon::MuonPRD_Collection_p2::m_id
unsigned int m_id
Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:43