ATLAS Offline Software
CscPrepDataContainerCnv_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 #include "GaudiKernel/ISvcLocator.h"
14 #include "GaudiKernel/Bootstrap.h"
15 
16 // Athena
17 #include "StoreGate/StoreGateSvc.h"
18 
19 
21  // Do not initialize again:
22  m_isInitialized=true;
23 
24  // Get Storegate, ID helpers, and so on
25  ISvcLocator* svcLocator = Gaudi::svcLocator();
26  // get StoreGate service
27  StatusCode sc = svcLocator->service("StoreGateSvc", m_storeGate);
28  if (sc.isFailure()) {
29  log << MSG::FATAL << "StoreGate service not found !" << endmsg;
30  return StatusCode::FAILURE;
31  }
32 
33  // get DetectorStore service
35  sc = svcLocator->service("DetectorStore", detStore);
36  if (sc.isFailure()) {
37  log << MSG::FATAL << "DetectorStore service not found !" << endmsg;
38  return StatusCode::FAILURE;
39  } else {
40  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found DetectorStore." << endmsg;
41  }
42 
43  // Get the helper from the detector store
44  sc = detStore->retrieve(m_CscId);
45  if (sc.isFailure()) {
46  log << MSG::FATAL << "Could not get ID helper !" << endmsg;
47  return StatusCode::FAILURE;
48  } else {
49  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Found the ID helper." << endmsg;
50  }
51 
52  if (m_eventCnvTool.retrieve().isFailure()) {
53  log << MSG::FATAL << "Could not get DetectorDescription manager" << endmsg;
54  return StatusCode::FAILURE;
55  }
56 
57  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized." << endmsg;
58  return StatusCode::SUCCESS;
59 }
61  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
62  if (!cnv_tool) return nullptr;
63  return dynamic_cast<const MuonGM::CscReadoutElement*>(cnv_tool->getDetectorElement(id));
64 }
66 {
67  if(log.level() <= MSG::DEBUG && !m_isInitialized) {
68  if (this->initialize(log) != StatusCode::SUCCESS) {
69  log << MSG::FATAL << "Could not initialize CscPrepDataContainerCnv_p2 " << endmsg;
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::CscPrepDataContainer TRANS;
88 
89  CscPrepDataCnv_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::CscPrepDataCollection& 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  unsigned int lastPRDIdHash = 0;
119  for (unsigned int i = 0; i < collection.size(); ++i) {
120  unsigned int pchanIndex=i+pcollBegin;
121  const CscPrepData* chan = collection[i]; // channel being converted
122  CscPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
123  chanCnv.transToPers(chan, pchan, log); // convert from CscPrepData to CscPrepData_p2
124  unsigned int clusIdCompact = chan->identify().get_identifier32().get_compact();
125  unsigned int collIdCompact = collection.identify().get_identifier32().get_compact();
126  int diff = clusIdCompact - collIdCompact;
127  persCont->m_prdDeltaId[pchanIndex]=clusIdCompact - collIdCompact; //store delta identifiers, rather than full identifiers
128  // sanity checks - to be removed at some point
129  if(log.level() <= MSG::DEBUG){
130  log << MSG::DEBUG<<i<<":\t clusId: "<<clusIdCompact<<", \t collectionId="<<collIdCompact<<"\t delta="<<persCont->m_prdDeltaId[pchanIndex]<<"\t diff="<<diff<<endmsg;
131  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
132  if (temp!=chan->identify() )
133  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
134  else
135  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
136  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
137  lastPRDIdHash = chan->collectionHash();
138  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
139  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
140  if (chan->detectorElement() != getReadOutElement(chan->identify()))
141  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
142  else
143  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
144  if (chan->detectorElement() != getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
145  log << MSG::DEBUG<<"Finished loop"<<endmsg;
146  }
147  }
148  }
149  if (log.level() <= MSG::DEBUG)
150  log << MSG::DEBUG<< " *** Writing CscPrepDataContainer ***" <<endmsg;
151 }
152 
154 {
155 
156  // The transient model has a container holding collections and the
157  // collections hold channels.
158  //
159  // The persistent model flattens this so that the persistent
160  // container has two vectors:
161  // 1) all collections, and
162  // 2) all channels
163  //
164  // The persistent collections, then only maintain indexes into the
165  // container's vector of all channels.
166  //
167  // So here we loop over all collection and extract their channels
168  // from the vector.
169 
170  Muon::CscPrepDataCollection* coll = nullptr;
171 
172  CscPrepDataCnv_p2 chanCnv;
173  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
174  unsigned int pCollEnd = persCont->m_collections.size();
175  if (log.level() <= MSG::DEBUG)
176  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
177  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
178  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
179  IdentifierHash collIDHash(pcoll.m_hashId);
180  coll = new Muon::CscPrepDataCollection(collIDHash);
181  // Identifier firstChanId = persCont->m_prds[collBegin].m_clusId;
182  // Identifier collId = m_CscId->parentID(firstChanId);
183  coll->setIdentifier(Identifier(pcoll.m_id));
184 
185  // FIXME - really would like to remove Identifier from collection, but cannot as there is :
186  // a) no way (apparently - find it hard to believe) to go from collection IdHash to collection Identifer.
187 
188  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
189  unsigned int chanIndex = 0; // transient index
190 
191  coll->reserve(pcoll.m_size);
192  // Fill with channels
193  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
194  const CscPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]);
195 
196  Identifier clusId (pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
197  if ( !m_CscId->valid(clusId) ) {
198  // have invalid PRD
199  log << MSG::WARNING << "Csc PRD has invalid Identifier of "<< m_CscId->show_to_string(clusId)
200  <<" and will be skipped!" << endmsg;
201  continue;
202  }
203 
204  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
205  IdentifierHash deIDHash;
206  int result = m_CscId->get_detectorElement_hash(clusId, deIDHash);
207 
208  if (result&&log.level() <= MSG::WARNING)
209  log << MSG::WARNING<< " Muon::CscPrepDataContainerCnv_p2::persToTrans: problem converting Identifier to DE hash "<<endmsg;
210 
211  const MuonGM::CscReadoutElement* detEl = getReadOutElement(clusId);
212  if (!detEl) {
213  if (log.level() <= MSG::WARNING)
214  log << MSG::WARNING<< "Muon::CscPrepDataContainerCnv_p2::persToTrans: could not get valid det element for PRD with id="<<clusId<<". Skipping."<<endmsg;
215  continue;
216  }
217 
218  auto chan = std::make_unique<CscPrepData>
219  (chanCnv.createCscPrepData (pchan,
220  clusId,
221  detEl,
222  log));
223 
224 
225  chan->setHashAndIndex(collIDHash, chanIndex);
226  coll->push_back(std::move(chan));
227  }
228 
229  // register the rdo collection in IDC with hash - faster addCollection
230  StatusCode sc = transCont->addCollection(coll, collIDHash);
231  if (sc.isFailure()) {
232  throw std::runtime_error("Failed to add collection to Identifiable Container");
233  }
234  if (log.level() <= MSG::DEBUG) {
235  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
236  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
237  }
238  }
239 
240  if (log.level() <= MSG::DEBUG)
241  log << MSG::DEBUG<< " *** Reading CscPrepDataContainer ***" << endmsg;
242 }
243 
245 {
246  if(!m_isInitialized) {
247  if (this->initialize(log) != StatusCode::SUCCESS) {
248  log << MSG::FATAL << "Could not initialize CscPrepDataContainerCnv_p2 " << endmsg;
249  return nullptr;
250  }
251  }
252  std::unique_ptr<Muon::CscPrepDataContainer> trans(new Muon::CscPrepDataContainer(m_CscId->module_hash_max()));
253  persToTrans(persObj, trans.get(), log);
254  return(trans.release());
255 }
256 
257 
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
CscIdHelper.h
Muon::MuonPrepDataContainer
Template for Muon PRD containers (which are basically collections of MuonPrepDataCollections).
Definition: MuonPrepDataContainer.h:42
CscPrepDataContainerCnv_p2.h
get_generator_info.result
result
Definition: get_generator_info.py:21
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
Muon::CscPrepDataContainerCnv_p2::createTransient
virtual Muon::CscPrepDataContainer * createTransient(const Muon::CscPrepDataContainer_p2 *persObj, MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:244
CscPrepDataCnv_p2::transToPers
void transToPers(const Muon::CscPrepData *transObj, Muon::CscPrepData_p2 *persObj, MsgStream &log)
Definition: CscPrepDataCnv_p2.cxx:58
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
Muon::CscPrepDataContainerCnv_p2::persToTrans
virtual void persToTrans(const Muon::CscPrepDataContainer_p2 *persCont, Muon::CscPrepDataContainer *transCont, MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:153
initialize
void initialize()
Definition: run_EoverP.cxx:894
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
Muon::CscPrepDataContainerCnv_p2::m_CscId
const CscIdHelper * m_CscId
Definition: CscPrepDataContainerCnv_p2.h:41
MuonGM::CscReadoutElement
Definition: CscReadoutElement.h:56
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
CscPrepData_p2.h
ITrkEventCnvTool.h
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:128
Muon::CscPrepDataContainerCnv_p2::getReadOutElement
const MuonGM::CscReadoutElement * getReadOutElement(const Identifier &id) const
Definition: CscPrepDataContainerCnv_p2.cxx:60
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
Muon::CscPrepDataContainerCnv_p2::m_isInitialized
bool m_isInitialized
Definition: CscPrepDataContainerCnv_p2.h:44
CscPrepData.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
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
Muon::CscPrepData
Class representing clusters from the CSC.
Definition: CscPrepData.h:39
Muon::CscPrepDataContainerCnv_p2::initialize
StatusCode initialize(MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:20
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
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
Muon::CscPrepDataCollection
MuonPrepDataCollection< CscPrepData > CscPrepDataCollection
Definition: MuonPrepDataCollection.h:109
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
CscPrepDataContainer.h
Muon::MuonPRD_Container_p2
Class to contain the Muon Prep Raw Data.
Definition: MuonPRD_Container_p2.h:26
Muon::CscPrepDataContainerCnv_p2::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: CscPrepDataContainerCnv_p2.h:43
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
CscPrepDataCnv_p2::createCscPrepData
static Muon::CscPrepData createCscPrepData(const Muon::CscPrepData_p2 *persObj, const Identifier &clusId, const MuonGM::CscReadoutElement *detEl, MsgStream &log)
Definition: CscPrepDataCnv_p2.cxx:15
Muon::MuonPRD_Collection_p2::m_hashId
unsigned int m_hashId
Hash Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:46
Muon::CscPrepData_p2
We don't write out (from Trk::PrepRawData) m_indexAndHash (can be recomputed), m_clusId (can be recom...
Definition: CscPrepData_p2.h:25
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
CscPrepDataCnv_p2
Definition: CscPrepDataCnv_p2.h:22
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
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
CscPrepDataCnv_p2.h
Muon::CscPrepDataContainerCnv_p2::transToPers
virtual void transToPers(const Muon::CscPrepDataContainer *transCont, Muon::CscPrepDataContainer_p2 *persCont, MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.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::CscPrepDataContainerCnv_p2::m_storeGate
StoreGateSvc * m_storeGate
Definition: CscPrepDataContainerCnv_p2.h:42
Muon::MuonPRD_Collection_p2::m_id
unsigned int m_id
Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:43