ATLAS Offline Software
sTgcPrepDataContainerCnv_p1.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 // Gaudi
14 #include "GaudiKernel/ISvcLocator.h"
15 #include "GaudiKernel/Bootstrap.h"
16 
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_sTgcId);
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 
59  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized. " << endmsg;
60  return StatusCode::SUCCESS;
61 }
63  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
64  if (!cnv_tool) return nullptr;
65  return dynamic_cast<const MuonGM::sTgcReadoutElement*>(cnv_tool->getDetectorElement(id));
66 }
67 
69 {
70 
71  if(!m_isInitialized) {
72  if (this->initialize(log) != StatusCode::SUCCESS) {
73  log << MSG::FATAL << "Could not initialize sTgcPrepDataContainerCnv_p1 " << 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  typedef Muon::sTgcPrepDataContainer TRANS;
93 
94  sTgcPrepDataCnv_p1 chanCnv;
95  TRANS::const_iterator it_Coll = transCont->begin();
96  TRANS::const_iterator it_CollEnd = transCont->end();
97  unsigned int pcollIndex = 0; // index to the persistent collection we're filling
98  unsigned int pcollBegin = 0; // index to start of persistent collection we're filling, in long list of persistent PRDs
99  unsigned int pcollEnd = 0; // index to end
100  int numColl = transCont->numberOfCollections();
101  persCont->m_collections.resize(numColl);
102 
103  if (log.level() <= MSG::DEBUG)
104  log << MSG::DEBUG<< " Preparing " << persCont->m_collections.size() << "Collections" <<endmsg;
105 
106  for (pcollIndex = 0; it_Coll != it_CollEnd; ++pcollIndex, ++it_Coll) {
107  // Add in new collection
108  if (log.level() <= MSG::DEBUG)
109  log << MSG::DEBUG<<"New collection"<<endmsg;
110  const Muon::sTgcPrepDataCollection& collection = (**it_Coll);
111  Muon::MuonPRD_Collection_p2& pcollection = persCont->m_collections[pcollIndex]; //get ref to collection we're going to fill
112 
113  pcollBegin = pcollEnd; // Next collection starts at end of previous one.
114  pcollEnd += collection.size();
115 
116  pcollection.m_hashId = collection.identifyHash();
117  pcollection.m_id = collection.identify().get_identifier32().get_compact();
118  pcollection.m_size = collection.size();
119 
120  // Add in channels
121  persCont->m_prds.resize(pcollEnd);
122  persCont->m_prdDeltaId.resize(pcollEnd);
123 
124  unsigned int lastPRDIdHash = 0;
125  for (unsigned int i = 0; i < collection.size(); ++i) {
126  unsigned int pchanIndex=i+pcollBegin;
127 
128  const sTgcPrepData* chan = collection[i]; // channel being converted
129  sTgcPrepData_p1* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
130 
131  chanCnv.transToPers(chan, pchan, log); // convert from sTgcPrepData to sTgcPrepData_p1
132 
133  // persCont->m_prdDeltaId is of data type unsigned short, thus we need to encode the channel (starting from the
134  // collection (module) is in contained) into 16 bits, we do it by storing multilayer, gasGap and channel
135  int multilayer = (m_sTgcId->multilayer(chan->identify())-1); // ranges between 1-2 (1bit)
136  int gasGap = (m_sTgcId->gasGap(chan->identify())-1); // ranges between 1-4 (2bits)
137  int channelType = (m_sTgcId->channelType(chan->identify())); // ranges between 0-2 (2bits)
138  int channel = (m_sTgcId->channel(chan->identify())-sTgcIdHelper::channelMin());
139 
140  // created an unsigned short and store multilayer, gasGap and channel by bit-shifts
141  unsigned short diff = ( channel << 5 | channelType << 3 | gasGap << 1 | multilayer) ;
142  log << MSG::DEBUG << "Translated id=" << chan->identify().get_compact() << " (multilayer=" << multilayer
143  << ", gasGap << " << gasGap << ", channel=" << channel << ") into diff=" << diff << endmsg;
144 
145 
146  persCont->m_prdDeltaId[pchanIndex]=diff; //store delta identifiers, rather than full identifiers
147 
148  if(log.level() <= MSG::DEBUG){
149  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
150  if (temp!=chan->identify() )
151  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
152  else
153  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
154  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
155  lastPRDIdHash = chan->collectionHash();
156  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
157 
158  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
159  if (chan->detectorElement() !=getReadOutElement(chan->identify()))
160  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
161  else
162  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
163  if (chan->detectorElement() !=getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
164  log << MSG::DEBUG<<"Finished loop"<<endmsg;
165  }
166  }
167  }
168  if (log.level() <= MSG::DEBUG)
169  log << MSG::DEBUG<< " *** Writing sTgcPrepDataContainer ***" <<endmsg;
170 }
171 
173 {
174 
175  // The transient model has a container holding collections and the
176  // collections hold channels.
177  //
178  // The persistent model flattens this so that the persistent
179  // container has two vectors:
180  // 1) all collections, and
181  // 2) all channels
182  //
183  // The persistent collections, then only maintain indexes into the
184  // container's vector of all channels.
185  //
186  // So here we loop over all collection and extract their channels
187  // from the vector.
188 
189  Muon::sTgcPrepDataCollection* coll = nullptr;
190 
191  sTgcPrepDataCnv_p1 chanCnv;
192  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
193  unsigned int pCollEnd = persCont->m_collections.size();
194  if (log.level() <= MSG::DEBUG)
195  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
196  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
197  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
198  IdentifierHash collIDHash(pcoll.m_hashId);
199  coll = new Muon::sTgcPrepDataCollection(collIDHash);
200  coll->setIdentifier(Identifier(pcoll.m_id));
201 
202  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
203  unsigned int chanIndex = 0; // transient index
204 
205  coll->reserve(pcoll.m_size);
206  // Fill with channels
207  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
208  const sTgcPrepData_p1* pchan = &(persCont->m_prds[pchanIndex]);
209 
211  unsigned short diff = persCont->m_prdDeltaId[pchanIndex];
212  // we need to redo the bit-shift to retrieve channel, gasGap and multilayer
213  int channel = (diff>>5);
214  int channelType = ( 3 & (diff>>3) );
215  int gasGap = ( 3 & (diff>>1));
216  int multilayer = ( 1 & diff );
217  Identifier clusId = m_sTgcId->channelID(Identifier(pcoll.m_id), multilayer+1, gasGap+1,
218  channelType, channel+sTgcIdHelper::channelMin());
219  log << MSG::DEBUG << "Diff of " << diff << " translated into multilayer="
220  << multilayer << ", gasGap=" << gasGap << ", channelType=" << channelType
221  << ", channel=" << channel << " -> id=" << clusId.get_compact() << endmsg;
222 
223  if ( !m_sTgcId->valid(clusId) ) {
224  // have invalid PRD
225  log << MSG::WARNING << "Tgc PRD has invalid Identifier of "<< m_sTgcId->show_to_string(clusId)<< " - are you sure you have the correct geometry loaded, and NSW enabled?"<<endmsg;
226  }
227 
228  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
229  IdentifierHash deIDHash;
230  int result = m_sTgcId->get_detectorElement_hash(clusId, deIDHash);
231  if (result&&log.level() <= MSG::WARNING)
232  log << MSG::WARNING<< " Muon::sTgcPrepDataContainerCnv_p1::persToTrans: problem converting Identifier to DE hash "<<endmsg;
233  const MuonGM::sTgcReadoutElement* detEl =
234  getReadOutElement(clusId);
235  if (!detEl) {
236  log << MSG::WARNING<< "Muon::sTgcPrepDataContainerCnv_p1::persToTrans: could not get valid det element for PRD with id="<<clusId<<". Skipping."<<endmsg;
237  continue;
238  }
239 
240  auto chan = std::make_unique<sTgcPrepData>
241  (chanCnv.createsTgcPrepData (pchan,
242  clusId,
243  detEl,
244  log));
245 
246  chan->setHashAndIndex(collIDHash, chanIndex);
247  coll->push_back(std::move(chan));
248  }
249 
250  // register the rdo collection in IDC with hash - faster addCollection
251  StatusCode sc = transCont->addCollection(coll, collIDHash);
252  if (sc.isFailure()) {
253  throw std::runtime_error("Failed to add collection to Identifiable Container");
254  }
255  if (log.level() <= MSG::DEBUG) {
256  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
257  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
258  }
259  }
260 
261  if (log.level() <= MSG::DEBUG)
262  log << MSG::DEBUG<< " *** Reading sTgcPrepDataContainer ***" << endmsg;
263 }
264 
265 
266 
267 //================================================================
269 {
270  if(!m_isInitialized) {
271  if (this->initialize(log) != StatusCode::SUCCESS) {
272  log << MSG::FATAL << "Could not initialize sTgcPrepDataContainerCnv_p1 " << endmsg;
273  return nullptr;
274  }
275  }
276  std::unique_ptr<Muon::sTgcPrepDataContainer> trans(new Muon::sTgcPrepDataContainer(m_sTgcId->module_hash_max()));
277  persToTrans(persObj, trans.get(), log);
278  return(trans.release());
279 }
280 
281 
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
dumpTgcDigiDeadChambers.gasGap
list gasGap
Definition: dumpTgcDigiDeadChambers.py:33
get_generator_info.result
result
Definition: get_generator_info.py:21
sTgcIdHelper.h
plotting.yearwise_efficiency.channel
channel
Definition: yearwise_efficiency.py:28
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
Muon::sTgcPrepDataContainerCnv_p1::m_isInitialized
bool m_isInitialized
Definition: sTgcPrepDataContainerCnv_p1.h:44
Muon::MuonPRD_Container_p2::m_prds
std::vector< PRD > m_prds
Definition: MuonPRD_Container_p2.h:31
Muon::sTgcPrepDataContainerCnv_p1::transToPers
virtual void transToPers(const Muon::sTgcPrepDataContainer *transCont, Muon::sTgcPrepDataContainer_p1 *persCont, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p1.cxx:68
sTgcPrepData_p1.h
initialize
void initialize()
Definition: run_EoverP.cxx:894
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
Muon::sTgcPrepDataContainerCnv_p1::m_sTgcId
const sTgcIdHelper * m_sTgcId
Definition: sTgcPrepDataContainerCnv_p1.h:41
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
sTgcPrepDataContainer.h
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Muon::sTgcPrepDataContainerCnv_p1::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: sTgcPrepDataContainerCnv_p1.h:43
Muon::sTgcPrepDataContainerCnv_p1::getReadOutElement
const MuonGM::sTgcReadoutElement * getReadOutElement(const Identifier &id) const
Definition: sTgcPrepDataContainerCnv_p1.cxx:62
ITrkEventCnvTool.h
sTgcPrepData.h
StoreGateSvc
The Athena Transient Store API.
Definition: StoreGateSvc.h:128
sTgcPrepDataCnv_p1::createsTgcPrepData
static Muon::sTgcPrepData createsTgcPrepData(const Muon::sTgcPrepData_p1 *persObj, const Identifier clusId, const MuonGM::sTgcReadoutElement *m_detEl, MsgStream &log)
Definition: sTgcPrepDataCnv_p1.cxx:17
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
sTgcPrepDataCnv_p1::transToPers
void transToPers(const Muon::sTgcPrepData *transObj, Muon::sTgcPrepData_p1 *persObj, MsgStream &log)
Definition: sTgcPrepDataCnv_p1.cxx:58
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
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Identifier
Definition: DetectorDescription/Identifier/Identifier/Identifier.h:32
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
sTgcPrepDataCnv_p1
Definition: sTgcPrepDataCnv_p1.h:22
Muon::MuonPRD_Collection_p2
Class to hold the persistent representation of MuonPRD_Collection.
Definition: MuonPRD_Collection_p2.h:22
MuonGM::sTgcReadoutElement
An sTgcReadoutElement corresponds to a single STGC module; therefore typicaly a barrel muon station c...
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/sTgcReadoutElement.h:28
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::sTgcPrepDataContainerCnv_p1::createTransient
virtual Muon::sTgcPrepDataContainer * createTransient(const Muon::sTgcPrepDataContainer_p1 *persObj, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p1.cxx:268
Muon::sTgcPrepDataContainerCnv_p1::initialize
StatusCode initialize(MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p1.cxx:22
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
sTgcIdHelper::channelMin
static int channelMin()
Definition: sTgcIdHelper.cxx:1050
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::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
sTgcPrepDataContainerCnv_p1.h
Muon::sTgcPrepDataContainerCnv_p1::m_storeGate
StoreGateSvc * m_storeGate
Definition: sTgcPrepDataContainerCnv_p1.h:42
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::sTgcPrepDataCollection
MuonPrepDataCollection< sTgcPrepData > sTgcPrepDataCollection
Definition: MuonPrepDataCollection.h:112
DEBUG
#define DEBUG
Definition: page_access.h:11
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
Muon::sTgcPrepData_p1
We don't write out (from Trk::PrepRawData) m_indexAndHash (can be recomputed),.
Definition: sTgcPrepData_p1.h:24
sTgcPrepDataCnv_p1.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.
Muon::sTgcPrepData
Class to represent sTgc measurements.
Definition: sTgcPrepData.h:20
Muon::sTgcPrepDataContainerCnv_p1::persToTrans
virtual void persToTrans(const Muon::sTgcPrepDataContainer_p1 *persCont, Muon::sTgcPrepDataContainer *transCont, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p1.cxx:172
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