ATLAS Offline Software
TgcPrepDataContainerCnv_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 // Gaudi
14 #include "GaudiKernel/ISvcLocator.h"
15 #include "GaudiKernel/Bootstrap.h"
16 
17 // Athena
19 #include "StoreGate/StoreGateSvc.h"
20 
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_TgcId) );
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::TgcReadoutElement*>(cnv_tool->getDetectorElement(id));
42 }
44 {
45 
46  if(log.level() <= MSG::DEBUG && !m_isInitialized) {
47  if (this->initialize(log) != StatusCode::SUCCESS) {
48  log << MSG::FATAL << "Could not initialize TgcPrepDataContainerCnv_p2 " << endmsg;
49  }
50  }
51 
52  // The transient model has a container holding collections and the
53  // collections hold channels.
54  //
55  // The persistent model flattens this so that the persistent
56  // container has two vectors:
57  // 1) all collections, and
58  // 2) all PRDs
59  //
60  // The persistent collections, then only maintain indexes into the
61  // container's vector of all channels.
62  //
63  // So here we loop over all collection and add their channels
64  // to the container's vector, saving the indexes in the
65  // collection.
66 
67 // std::cout<<"Starting transToPers"<<std::endl;
68  typedef Muon::TgcPrepDataContainer TRANS;
69  //typedef ITPConverterFor<Trk::PrepRawData> CONV;
70 
71  TgcPrepDataCnv_p2 chanCnv;
72  TRANS::const_iterator it_Coll = transCont->begin();
73  TRANS::const_iterator it_CollEnd = transCont->end();
74  unsigned int pcollIndex = 0; // index to the persistent collection we're filling
75  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
76  unsigned int pcollEnd = 0; // index to end
77  int numColl = transCont->numberOfCollections();
78  persCont->m_collections.resize(numColl);
79 
80  if (log.level() <= MSG::DEBUG)
81  log << MSG::DEBUG<< " Preparing " << persCont->m_collections.size() << "Collections" <<endmsg;
82  // std::cout<<"Preparing " << persCont->m_collections.size() << "Collections" << std::endl;
83  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
84  // Add in new collection
85  if (log.level() <= MSG::DEBUG)
86  log << MSG::DEBUG<<"New collection"<<endmsg;
87  const Muon::TgcPrepDataCollection& collection = (**it_Coll);
88  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
89 
90  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
91  pcollEnd += collection.size();
92 
93  pcollection.m_hashId = collection.identifyHash();
94  pcollection.m_id = collection.identify().get_identifier32().get_compact();
95  pcollection.m_size = collection.size();
96 
97  // Add in channels
98  persCont->m_prds.resize(pcollEnd); // FIXME! isn't this potentially a bit slow? Do a resize and a copy for each loop? EJWM.
99  persCont->m_prdDeltaId.resize(pcollEnd);
100 
101  unsigned int lastPRDIdHash = 0;
102  for (unsigned int i = 0; i < collection.size(); ++i) {
103  unsigned int pchanIndex=i+pcollBegin;
104  const TgcPrepData* chan = collection[i]; // channel being converted
105  TgcPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
106  chanCnv.transToPers(chan, pchan, log); // convert from TgcPrepData to TgcPrepData_p2
107 
108  unsigned int clusIdCompact = chan->identify().get_identifier32().get_compact();
109  unsigned int collIdCompact = collection.identify().get_identifier32().get_compact();
110 
111  persCont->m_prdDeltaId[pchanIndex]=clusIdCompact - collIdCompact; //store delta identifiers, rather than full identifiers
112  if(log.level() <= MSG::DEBUG){
113  log << MSG::DEBUG<<i<<":\t clusId: "<<clusIdCompact<<", \t collectionId="<<collIdCompact<<"\t delta="<<persCont->m_prdDeltaId[pchanIndex]<<endmsg;
114  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
115  if (temp!=chan->identify() )
116  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
117  else
118  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
119  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
120  lastPRDIdHash = chan->collectionHash();
121  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
122  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
123  if (chan->detectorElement() !=getReadOutElement(chan->identify()))
124  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
125  else
126  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
127  if (chan->detectorElement() !=getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
128  log << MSG::DEBUG<<"Finished loop"<<endmsg;
129  }
130  }
131  }
132  if (log.level() <= MSG::DEBUG)
133  log << MSG::DEBUG<< " *** Writing TgcPrepDataContainer ***" <<endmsg;
134 }
135 
137 {
138 
139  // The transient model has a container holding collections and the
140  // collections hold channels.
141  //
142  // The persistent model flattens this so that the persistent
143  // container has two vectors:
144  // 1) all collections, and
145  // 2) all channels
146  //
147  // The persistent collections, then only maintain indexes into the
148  // container's vector of all channels.
149  //
150  // So here we loop over all collection and extract their channels
151  // from the vector.
152 
153  Muon::TgcPrepDataCollection* coll = nullptr;
154 
155  TgcPrepDataCnv_p2 chanCnv;
156  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
157  unsigned int pCollEnd = persCont->m_collections.size();
158  if (log.level() <= MSG::DEBUG)
159  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
160  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
161  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
162  IdentifierHash collIDHash(pcoll.m_hashId);
163  coll = new Muon::TgcPrepDataCollection(collIDHash);
164  // Identifier firstChanId = persCont->m_prds[collBegin].m_clusId;
165  // Identifier collId = m_TgcId->parentID(firstChanId);
166  coll->setIdentifier(Identifier(pcoll.m_id));
167 
168  // FIXME - really would like to remove Identifier from collection, but cannot as there is :
169  // a) no way (apparently - find it hard to believe) to go from collection IdHash to collection Identifer.
170 
171  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
172  unsigned int chanIndex = 0; // transient index
173 
174  coll->reserve(pcoll.m_size);
175  // Fill with channels
176  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
177  const TgcPrepData_p2* pchan = &(persCont->m_prds[pchanIndex]);
178 
179  Identifier clusId(pcoll.m_id + persCont->m_prdDeltaId[pchanIndex]);
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_TgcId->get_detectorElement_hash(clusId, deIDHash);
184  if (result&&log.level() <= MSG::WARNING)
185  log << MSG::WARNING<< " Muon::TgcPrepDataContainerCnv_p2::persToTrans: problem converting Identifier to DE hash "<<endmsg;
186  const MuonGM::TgcReadoutElement* detEl =
187  getReadOutElement(clusId);
188 
189  auto chan = std::make_unique<TgcPrepData>
190  (chanCnv.createTgcPrepData (pchan,
191  clusId,
192  detEl,
193  log));
194 
195 
196  chan->setHashAndIndex(collIDHash, chanIndex);
197  coll->push_back(std::move(chan));
198  }
199 
200  // register the rdo collection in IDC with hash - faster addCollection
201  StatusCode sc = transCont->addCollection(coll, collIDHash);
202  if (sc.isFailure()) {
203  throw std::runtime_error("Failed to add collection to Identifiable Container");
204  }
205  if (log.level() <= MSG::DEBUG) {
206  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
207  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
208  }
209  }
210 
211  if (log.level() <= MSG::DEBUG)
212  log << MSG::DEBUG<< " *** Reading TgcPrepDataContainer ***" << endmsg;
213 }
214 
215 
216 
217 //================================================================
219 {
220  if(!m_isInitialized) {
221  if (this->initialize(log) != StatusCode::SUCCESS) {
222  log << MSG::FATAL << "Could not initialize TgcPrepDataContainerCnv_p2 " << endmsg;
223  return nullptr;
224  }
225  }
226  std::unique_ptr<Muon::TgcPrepDataContainer> trans(new Muon::TgcPrepDataContainer(m_TgcId->module_hash_max()));
227  persToTrans(persObj, trans.get(), log);
228  return(trans.release());
229 }
230 
231 
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
get_generator_info.result
result
Definition: get_generator_info.py:21
Muon::TgcPrepDataContainerCnv_p2::getReadOutElement
const MuonGM::TgcReadoutElement * getReadOutElement(const Identifier &id) const
Definition: TgcPrepDataContainerCnv_p2.cxx:38
TgcPrepDataCnv_p2::createTgcPrepData
static Muon::TgcPrepData createTgcPrepData(const Muon::TgcPrepData_p2 *persObj, const Identifier &id, const MuonGM::TgcReadoutElement *detEl, MsgStream &log)
Definition: TgcPrepDataCnv_p2.cxx:16
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:18
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
TgcPrepDataCnv_p2::transToPers
void transToPers(const Muon::TgcPrepData *transObj, Muon::TgcPrepData_p2 *persObj, MsgStream &log)
Definition: TgcPrepDataCnv_p2.cxx:50
initialize
void initialize()
Definition: run_EoverP.cxx:894
TgcPrepDataCnv_p2
Definition: TgcPrepDataCnv_p2.h:22
Identifier::get_identifier32
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
Identifier::get_compact
value_type get_compact() const
Get the compact id.
Muon::TgcPrepDataContainerCnv_p2::m_TgcId
const TgcIdHelper * m_TgcId
Definition: TgcPrepDataContainerCnv_p2.h:39
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
TgcPrepData.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
MuonPRD_Container_p2.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
Muon::TgcPrepDataContainerCnv_p2::m_isInitialized
bool m_isInitialized
Definition: TgcPrepDataContainerCnv_p2.h:41
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
TgcPrepDataContainer.h
Muon::MuonPRD_Collection_p2
Class to hold the persistent representation of MuonPRD_Collection.
Definition: MuonPRD_Collection_p2.h:22
Muon::TgcPrepDataContainerCnv_p2::initialize
StatusCode initialize(MsgStream &log)
Definition: TgcPrepDataContainerCnv_p2.cxx:22
Muon::MuonPrepDataCollection::identify
virtual Identifier identify() const override final
MuonGM::TgcReadoutElement
A TgcReadoutElement corresponds to a single TGC chamber; therefore typically a TGC station contains s...
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/TgcReadoutElement.h:42
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
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
TgcPrepDataCnv_p2.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.
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
Muon::TgcPrepDataContainerCnv_p2::transToPers
virtual void transToPers(const Muon::TgcPrepDataContainer *transCont, Muon::TgcPrepDataContainer_p2 *persCont, MsgStream &log)
Definition: TgcPrepDataContainerCnv_p2.cxx:43
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
Muon::TgcPrepDataContainerCnv_p2::persToTrans
virtual void persToTrans(const Muon::TgcPrepDataContainer_p2 *persCont, Muon::TgcPrepDataContainer *transCont, MsgStream &log)
Definition: TgcPrepDataContainerCnv_p2.cxx:136
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
Muon::TgcPrepData
Class to represent TGC measurements.
Definition: TgcPrepData.h:32
Muon::TgcPrepDataContainerCnv_p2::createTransient
virtual Muon::TgcPrepDataContainer * createTransient(const Muon::TgcPrepDataContainer_p2 *persObj, MsgStream &log)
Definition: TgcPrepDataContainerCnv_p2.cxx:218
TgcIdHelper.h
Muon::TgcPrepData_p2
We don't write out (from Trk::PrepRawData) m_indexAndHash (can be recomputed), m_clusId (can be recom...
Definition: TgcPrepData_p2.h:25
Muon::TgcPrepDataCollection
MuonPrepDataCollection< TgcPrepData > TgcPrepDataCollection
Definition: MuonPrepDataCollection.h:108
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
TgcPrepData_p2.h
StoreGateSvc.h
TgcPrepDataContainerCnv_p2.h
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
Muon::TgcPrepDataContainerCnv_p2::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: TgcPrepDataContainerCnv_p2.h:40
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