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