ATLAS Offline Software
CscPrepDataContainerCnv_p2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
13 #include "GaudiKernel/ISvcLocator.h"
14 #include "GaudiKernel/Bootstrap.h"
15 
16 // Athena
18 #include "StoreGate/StoreGateSvc.h"
19 
20 
22  // Do not initialize again:
23  m_isInitialized=true;
24 
25  // get DetectorStore service
26  SmartIF<StoreGateSvc> detStore{Gaudi::svcLocator()->service("DetectorStore")};
27  CHECK( detStore.isValid() );
28 
29  // Get the CSC helper from the detector store
30  CHECK( detStore->retrieve(m_CscId) );
31 
32  CHECK( m_eventCnvTool.retrieve() );
33 
34  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized." << endmsg;
35  return StatusCode::SUCCESS;
36 }
38  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
39  if (!cnv_tool) return nullptr;
40  return dynamic_cast<const MuonGM::CscReadoutElement*>(cnv_tool->getDetectorElement(id));
41 }
43 {
44  if(log.level() <= MSG::DEBUG && !m_isInitialized) {
45  if (this->initialize(log) != StatusCode::SUCCESS) {
46  log << MSG::FATAL << "Could not initialize CscPrepDataContainerCnv_p2 " << endmsg;
47  }
48  }
49  // The transient model has a container holding collections and the
50  // collections hold channels.
51  //
52  // The persistent model flattens this so that the persistent
53  // container has two vectors:
54  // 1) all collections, and
55  // 2) all PRDs
56  //
57  // The persistent collections, then only maintain indexes into the
58  // container's vector of all channels.
59  //
60  // So here we loop over all collection and add their channels
61  // to the container's vector, saving the indexes in the
62  // collection.
63 
64  typedef Muon::CscPrepDataContainer TRANS;
65 
66  CscPrepDataCnv_p2 chanCnv;
67  TRANS::const_iterator it_Coll = transCont->begin();
68  TRANS::const_iterator it_CollEnd = transCont->end();
69  unsigned int pcollIndex = 0; // index to the persistent collection we're filling
70  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
71  unsigned int pcollEnd = 0; // index to end
72  int numColl = transCont->numberOfCollections();
73  persCont->m_collections.resize(numColl);
74 
75  if (log.level() <= MSG::DEBUG)
76  log << MSG::DEBUG<< " Preparing " << persCont->m_collections.size() << "Collections" <<endmsg;
77  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
78  // Add in new collection
79  if (log.level() <= MSG::DEBUG)
80  log << MSG::DEBUG<<"New collection"<<endmsg;
81  const Muon::CscPrepDataCollection& collection = (**it_Coll);
82  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
83 
84  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
85  pcollEnd += collection.size();
86 
87  pcollection.m_hashId = collection.identifyHash();
88  pcollection.m_id = collection.identify().get_identifier32().get_compact();
89  pcollection.m_size = collection.size();
90 
91  // Add in channels
92  persCont->m_prds.resize(pcollEnd); // FIXME! isn't this potentially a bit slow? Do a resize and a copy for each loop? EJWM.
93  persCont->m_prdDeltaId.resize(pcollEnd);
94 
95  unsigned int lastPRDIdHash = 0;
96  for (unsigned int i = 0; i < collection.size(); ++i) {
97  unsigned int pchanIndex=i+pcollBegin;
98  const CscPrepData* chan = collection[i]; // channel being converted
99  CscPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
100  chanCnv.transToPers(chan, pchan, log); // convert from CscPrepData to CscPrepData_p2
101  unsigned int clusIdCompact = chan->identify().get_identifier32().get_compact();
102  unsigned int collIdCompact = collection.identify().get_identifier32().get_compact();
103  int diff = clusIdCompact - collIdCompact;
104  persCont->m_prdDeltaId[pchanIndex]=clusIdCompact - collIdCompact; //store delta identifiers, rather than full identifiers
105  // sanity checks - to be removed at some point
106  if(log.level() <= MSG::DEBUG){
107  log << MSG::DEBUG<<i<<":\t clusId: "<<clusIdCompact<<", \t collectionId="<<collIdCompact<<"\t delta="<<persCont->m_prdDeltaId[pchanIndex]<<"\t diff="<<diff<<endmsg;
108  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
109  if (temp!=chan->identify() )
110  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
111  else
112  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
113  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
114  lastPRDIdHash = chan->collectionHash();
115  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
116  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
117  if (chan->detectorElement() != getReadOutElement(chan->identify()))
118  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
119  else
120  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
121  if (chan->detectorElement() != getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
122  log << MSG::DEBUG<<"Finished loop"<<endmsg;
123  }
124  }
125  }
126  if (log.level() <= MSG::DEBUG)
127  log << MSG::DEBUG<< " *** Writing CscPrepDataContainer ***" <<endmsg;
128 }
129 
131 {
132 
133  // The transient model has a container holding collections and the
134  // collections hold channels.
135  //
136  // The persistent model flattens this so that the persistent
137  // container has two vectors:
138  // 1) all collections, and
139  // 2) all channels
140  //
141  // The persistent collections, then only maintain indexes into the
142  // container's vector of all channels.
143  //
144  // So here we loop over all collection and extract their channels
145  // from the vector.
146 
147  Muon::CscPrepDataCollection* coll = nullptr;
148 
149  CscPrepDataCnv_p2 chanCnv;
150  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
151  unsigned int pCollEnd = persCont->m_collections.size();
152  if (log.level() <= MSG::DEBUG)
153  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
154  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
155  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
156  IdentifierHash collIDHash(pcoll.m_hashId);
157  coll = new Muon::CscPrepDataCollection(collIDHash);
158  // Identifier firstChanId = persCont->m_prds[collBegin].m_clusId;
159  // Identifier collId = m_CscId->parentID(firstChanId);
160  coll->setIdentifier(Identifier(pcoll.m_id));
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 CscPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]);
172 
173  Identifier clusId (pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
174  if ( !m_CscId->valid(clusId) ) {
175  // have invalid PRD
176  log << MSG::WARNING << "Csc PRD has invalid Identifier of "<< m_CscId->show_to_string(clusId)
177  <<" and will be skipped!" << endmsg;
178  continue;
179  }
180 
181  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
182  IdentifierHash deIDHash;
183  int result = m_CscId->get_detectorElement_hash(clusId, deIDHash);
184 
185  if (result&&log.level() <= MSG::WARNING)
186  log << MSG::WARNING<< " Muon::CscPrepDataContainerCnv_p2::persToTrans: problem converting Identifier to DE hash "<<endmsg;
187 
188  const MuonGM::CscReadoutElement* detEl = getReadOutElement(clusId);
189  if (!detEl) {
190  if (log.level() <= MSG::WARNING)
191  log << MSG::WARNING<< "Muon::CscPrepDataContainerCnv_p2::persToTrans: could not get valid det element for PRD with id="<<clusId<<". Skipping."<<endmsg;
192  continue;
193  }
194 
195  auto chan = std::make_unique<CscPrepData>
196  (chanCnv.createCscPrepData (pchan,
197  clusId,
198  detEl,
199  log));
200 
201 
202  chan->setHashAndIndex(collIDHash, chanIndex);
203  coll->push_back(std::move(chan));
204  }
205 
206  // register the rdo collection in IDC with hash - faster addCollection
207  StatusCode sc = transCont->addCollection(coll, collIDHash);
208  if (sc.isFailure()) {
209  throw std::runtime_error("Failed to add collection to Identifiable Container");
210  }
211  if (log.level() <= MSG::DEBUG) {
212  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
213  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
214  }
215  }
216 
217  if (log.level() <= MSG::DEBUG)
218  log << MSG::DEBUG<< " *** Reading CscPrepDataContainer ***" << endmsg;
219 }
220 
222 {
223  if(!m_isInitialized) {
224  if (this->initialize(log) != StatusCode::SUCCESS) {
225  log << MSG::FATAL << "Could not initialize CscPrepDataContainerCnv_p2 " << endmsg;
226  return nullptr;
227  }
228  }
229  std::unique_ptr<Muon::CscPrepDataContainer> trans(new Muon::CscPrepDataContainer(m_CscId->module_hash_max()));
230  persToTrans(persObj, trans.get(), log);
231  return(trans.release());
232 }
233 
234 
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:18
Muon::CscPrepDataContainerCnv_p2::createTransient
virtual Muon::CscPrepDataContainer * createTransient(const Muon::CscPrepDataContainer_p2 *persObj, MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:221
CscPrepDataCnv_p2::transToPers
void transToPers(const Muon::CscPrepData *transObj, Muon::CscPrepData_p2 *persObj, MsgStream &log)
Definition: CscPrepDataCnv_p2.cxx:58
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
Muon::CscPrepDataContainerCnv_p2::persToTrans
virtual void persToTrans(const Muon::CscPrepDataContainer_p2 *persCont, Muon::CscPrepDataContainer *transCont, MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:130
initialize
void initialize()
Definition: run_EoverP.cxx:894
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
Identifier::get_compact
value_type get_compact() const
Get the compact id.
Muon::CscPrepDataContainerCnv_p2::m_CscId
const CscIdHelper * m_CscId
Definition: CscPrepDataContainerCnv_p2.h:40
MuonGM::CscReadoutElement
Definition: CscReadoutElement.h:56
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
Identifier32::get_compact
value_type get_compact() const
Get the compact id.
Definition: Identifier32.h:44
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
CscPrepData_p2.h
ITrkEventCnvTool.h
Muon::CscPrepDataContainerCnv_p2::getReadOutElement
const MuonGM::CscReadoutElement * getReadOutElement(const Identifier &id) const
Definition: CscPrepDataContainerCnv_p2.cxx:37
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
MuonPRD_Container_p2.h
Muon::CscPrepDataContainerCnv_p2::m_isInitialized
bool m_isInitialized
Definition: CscPrepDataContainerCnv_p2.h:42
CscPrepData.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Muon::CscPrepData
Class representing clusters from the CSC.
Definition: CscPrepData.h:39
Muon::CscPrepDataContainerCnv_p2::initialize
StatusCode initialize(MsgStream &log)
Definition: CscPrepDataContainerCnv_p2.cxx:21
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:239
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
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
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
errorcheck.h
Helpers for checking error return status codes and reporting errors.
Muon::CscPrepDataContainerCnv_p2::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: CscPrepDataContainerCnv_p2.h:41
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
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
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:42
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::MuonPRD_Collection_p2::m_id
unsigned int m_id
Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:43
Identifier
Definition: IdentifierFieldParser.cxx:14