ATLAS Offline Software
sTgcPrepDataContainerCnv_p3.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 
14 // Gaudi
15 #include "GaudiKernel/ISvcLocator.h"
16 #include "GaudiKernel/Bootstrap.h"
17 
18 // Athena
20 #include "StoreGate/StoreGateSvc.h"
21 
22 
24  // Do not initialize again:
25  m_isInitialized=true;
26 
27  // get DetectorStore service
28  SmartIF<StoreGateSvc> detStore{Gaudi::svcLocator()->service("DetectorStore")};
29  CHECK( detStore.isValid() );
30 
31  // Get the helper from the detector store
32  CHECK( detStore->retrieve(m_sTgcId) );
33 
34  CHECK( m_eventCnvTool.retrieve() );
35 
36  if (log.level() <= MSG::DEBUG) log << MSG::DEBUG << "Converter initialized. " << endmsg;
37  return StatusCode::SUCCESS;
38 }
40  const Trk::ITrkEventCnvTool* cnv_tool = m_eventCnvTool->getCnvTool(id);
41  if (!cnv_tool) return nullptr;
42  return dynamic_cast<const MuonGM::sTgcReadoutElement*>(cnv_tool->getDetectorElement(id));
43 }
44 
46 {
47 
48  if(!m_isInitialized) {
49  if (this->initialize(log) != StatusCode::SUCCESS) {
50  log << MSG::FATAL << "Could not initialize sTgcPrepDataContainerCnv_p3 " << endmsg;
51  }
52  }
53 
54  // The transient model has a container holding collections and the
55  // collections hold channels.
56  //
57  // The persistent model flattens this so that the persistent
58  // container has two vectors:
59  // 1) all collections, and
60  // 2) all PRDs
61  //
62  // The persistent collections, then only maintain indexes into the
63  // container's vector of all channels.
64  //
65  // So here we loop over all collection and add their channels
66  // to the container's vector, saving the indexes in the
67  // collection.
68 
69  typedef Muon::sTgcPrepDataContainer TRANS;
70 
71  sTgcPrepDataCnv_p3 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 
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::sTgcPrepDataCollection& 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);
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 
105  const sTgcPrepData* chan = collection[i]; // channel being converted
106  sTgcPrepData_p3* pchan = &(persCont->m_prds[pchanIndex]); // persistent version to fill
107 
108  chanCnv.transToPers(chan, pchan, log); // convert from sTgcPrepData to sTgcPrepData_p3
109 
110  // persCont->m_prdDeltaId is of data type unsigned short, thus we need to encode the channel (starting from the
111  // collection (module) is in contained) into 16 bits, we do it by storing multilayer, gasGap and channel
112  int multilayer = (m_sTgcId->multilayer(chan->identify())-1); // ranges between 1-2 (1bit)
113  int gasGap = (m_sTgcId->gasGap(chan->identify())-1); // ranges between 1-4 (2bits)
114  int channelType = (m_sTgcId->channelType(chan->identify())); // ranges between 0-2 (2bits)
115  int channel = (m_sTgcId->channel(chan->identify())-sTgcIdHelper::channelMin());
116 
117  // created an unsigned short and store multilayer, gasGap and channel by bit-shifts
118  unsigned short diff = ( channel << 5 | channelType << 3 | gasGap << 1 | multilayer) ;
119  log << MSG::DEBUG << "Translated id=" << chan->identify().get_compact() << " (multilayer=" << multilayer
120  << ", gasGap << " << gasGap << ", channel=" << channel << ") into diff=" << diff << endmsg;
121 
122 
123  persCont->m_prdDeltaId[pchanIndex]=diff; //store delta identifiers, rather than full identifiers
124 
125  if(log.level() <= MSG::DEBUG){
126  Identifier temp(pcollection.m_id + persCont->m_prdDeltaId[pchanIndex]);
127  if (temp!=chan->identify() )
128  log << MSG::WARNING << "PRD ids differ! Transient:"<<chan->identify()<<", From persistent:"<<temp<<" diff = "<<chan->identify().get_compact()-temp.get_compact()<<endmsg;
129  else
130  log << MSG::DEBUG <<" PRD ids match."<<endmsg;
131  if (lastPRDIdHash && lastPRDIdHash != chan->collectionHash() ) log << MSG::WARNING << "Collection Identifier hashes differ!"<<endmsg;
132  lastPRDIdHash = chan->collectionHash();
133  log << MSG::DEBUG<<"Collection hash = "<<lastPRDIdHash<<endmsg;
134 
135  if (chan->collectionHash()!= collection.identifyHash() ) log << MSG::WARNING << "Collection's idHash does not match PRD collection hash!"<<endmsg;
136  if (chan->detectorElement() !=getReadOutElement(chan->identify()))
137  log << MSG::WARNING << "Getting de from identity didn't work!"<<endmsg;
138  else
139  log << MSG::DEBUG<<"Getting de from identity did work "<<endmsg;
140  if (chan->detectorElement() !=getReadOutElement(temp)) log << MSG::WARNING << "Getting de from reconstructed identity didn't work!"<<endmsg;
141  log << MSG::DEBUG<<"Finished loop"<<endmsg;
142  }
143  }
144  }
145  if (log.level() <= MSG::DEBUG)
146  log << MSG::DEBUG<< " *** Writing sTgcPrepDataContainer ***" <<endmsg;
147 }
148 
150 {
151 
152  // The transient model has a container holding collections and the
153  // collections hold channels.
154  //
155  // The persistent model flattens this so that the persistent
156  // container has two vectors:
157  // 1) all collections, and
158  // 2) all channels
159  //
160  // The persistent collections, then only maintain indexes into the
161  // container's vector of all channels.
162  //
163  // So here we loop over all collection and extract their channels
164  // from the vector.
165 
166  Muon::sTgcPrepDataCollection* coll = nullptr;
167 
168  sTgcPrepDataCnv_p3 chanCnv;
169  unsigned int pchanIndex(0); // position within persCont->m_prds. Incremented inside innermost loop
170  unsigned int pCollEnd = persCont->m_collections.size();
171  if (log.level() <= MSG::DEBUG)
172  log << MSG::DEBUG<< " Reading " << pCollEnd << "Collections" <<endmsg;
173  for (unsigned int pcollIndex = 0; pcollIndex < pCollEnd; ++pcollIndex) {
174  const Muon::MuonPRD_Collection_p2& pcoll = persCont->m_collections[pcollIndex];
175  IdentifierHash collIDHash(pcoll.m_hashId);
176  coll = new Muon::sTgcPrepDataCollection(collIDHash);
177  coll->setIdentifier(Identifier(pcoll.m_id));
178 
179  unsigned int pchanEnd = pchanIndex+pcoll.m_size;
180  unsigned int chanIndex = 0; // transient index
181 
182  coll->reserve(pcoll.m_size);
183  // Fill with channels
184  for (; pchanIndex < pchanEnd; ++ pchanIndex, ++chanIndex) {
185  const sTgcPrepData_p3* pchan = &(persCont->m_prds[pchanIndex]);
186 
188  unsigned short diff = persCont->m_prdDeltaId[pchanIndex];
189  // we need to redo the bit-shift to retrieve channel, gasGap and multilayer
190  int channel = (diff>>5);
191  int channelType = ( 3 & (diff>>3) );
192  int gasGap = ( 3 & (diff>>1));
193  int multilayer = ( 1 & diff );
194  Identifier clusId = m_sTgcId->channelID(Identifier(pcoll.m_id), multilayer+1, gasGap+1,
195  channelType, channel+sTgcIdHelper::channelMin());
196  log << MSG::DEBUG << "Diff of " << diff << " translated into multilayer="
197  << multilayer << ", gasGap=" << gasGap << ", channelType=" << channelType
198  << ", channel=" << channel << " -> id=" << clusId.get_compact() << endmsg;
199 
200  if ( !m_sTgcId->valid(clusId) ) {
201  // have invalid PRD
202  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;
203  }
204 
205  // The reason I need to do the following is that one collection can have several detector elements in, the collection hashes!=detector element hashes
206  IdentifierHash deIDHash;
207  int result = m_sTgcId->get_detectorElement_hash(clusId, deIDHash);
208  if (result&&log.level() <= MSG::WARNING)
209  log << MSG::WARNING<< " Muon::sTgcPrepDataContainerCnv_p3::persToTrans: problem converting Identifier to DE hash "<<endmsg;
210  const MuonGM::sTgcReadoutElement* detEl =
211  getReadOutElement(clusId);
212  if (!detEl) {
213  log << MSG::WARNING<< "Muon::sTgcPrepDataContainerCnv_p3::persToTrans: could not get valid det element for PRD with id="<<clusId<<". Skipping."<<endmsg;
214  continue;
215  }
216 
217  auto chan = std::make_unique<sTgcPrepData>
218  (chanCnv.createsTgcPrepData (pchan,
219  clusId,
220  detEl,
221  log));
222 
223  chan->setHashAndIndex(collIDHash, chanIndex);
224  coll->push_back(std::move(chan));
225  }
226 
227  // register the rdo collection in IDC with hash - faster addCollection
228  StatusCode sc = transCont->addCollection(coll, collIDHash);
229  if (sc.isFailure()) {
230  throw std::runtime_error("Failed to add collection to Identifiable Container");
231  }
232  if (log.level() <= MSG::DEBUG) {
233  log << MSG::DEBUG << "AthenaPoolTPCnvIDCont::persToTrans, collection, hash_id/coll id = " << (int) collIDHash << " / " <<
234  coll->identify().get_compact() << ", added to Identifiable container." << endmsg;
235  }
236  }
237 
238  if (log.level() <= MSG::DEBUG)
239  log << MSG::DEBUG<< " *** Reading sTgcPrepDataContainer ***" << endmsg;
240 }
241 
242 
243 
244 //================================================================
246 {
247  if(!m_isInitialized) {
248  if (this->initialize(log) != StatusCode::SUCCESS) {
249  log << MSG::FATAL << "Could not initialize sTgcPrepDataContainerCnv_p3 " << endmsg;
250  return nullptr;
251  }
252  }
253  std::unique_ptr<Muon::sTgcPrepDataContainer> trans(new Muon::sTgcPrepDataContainer(m_sTgcId->module_hash_max()));
254  persToTrans(persObj, trans.get(), log);
255  return(trans.release());
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
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:24
python.Constants.FATAL
int FATAL
Definition: Control/AthenaCommon/python/Constants.py:19
sTgcPrepDataCnv_p3::transToPers
void transToPers(const Muon::sTgcPrepData *transObj, Muon::sTgcPrepData_p3 *persObj, MsgStream &log)
Definition: sTgcPrepDataCnv_p3.cxx:62
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
sTgcPrepDataCnv_p3.h
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::sTgcPrepDataContainerCnv_p3::m_isInitialized
bool m_isInitialized
Definition: sTgcPrepDataContainerCnv_p3.h:42
Muon::sTgcPrepDataContainerCnv_p3::transToPers
virtual void transToPers(const Muon::sTgcPrepDataContainer *transCont, Muon::sTgcPrepDataContainer_p3 *persCont, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p3.cxx:45
Muon::MuonPrepDataCollection::setIdentifier
virtual void setIdentifier(Identifier id)
sTgcPrepDataContainer.h
Muon::sTgcPrepDataContainerCnv_p3::m_eventCnvTool
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Definition: sTgcPrepDataContainerCnv_p3.h:41
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
sTgcPrepData.h
Muon::MuonPrepDataCollection::identifyHash
virtual IdentifierHash identifyHash() const override final
sTgcPrepDataCnv_p3::createsTgcPrepData
static Muon::sTgcPrepData createsTgcPrepData(const Muon::sTgcPrepData_p3 *persObj, const Identifier clusId, const MuonGM::sTgcReadoutElement *m_detEl, MsgStream &log)
Definition: sTgcPrepDataCnv_p3.cxx:16
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
lumiFormat.i
int i
Definition: lumiFormat.py:85
ReadCellNoiseFromCool.chan
chan
Definition: ReadCellNoiseFromCool.py:52
Muon::sTgcPrepDataContainerCnv_p3::createTransient
virtual Muon::sTgcPrepDataContainer * createTransient(const Muon::sTgcPrepDataContainer_p3 *persObj, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p3.cxx:245
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
MuonGM::sTgcReadoutElement
An sTgcReadoutElement corresponds to a single STGC module; therefore typicaly a barrel muon station c...
Definition: MuonDetDescr/MuonReadoutGeometry/MuonReadoutGeometry/sTgcReadoutElement.h:30
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
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
Muon::sTgcPrepDataContainerCnv_p3::persToTrans
virtual void persToTrans(const Muon::sTgcPrepDataContainer_p3 *persCont, Muon::sTgcPrepDataContainer *transCont, MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p3.cxx:149
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
python.PyKernel.detStore
detStore
Definition: PyKernel.py:41
sTgcPrepDataCnv_p3
Definition: sTgcPrepDataCnv_p3.h:22
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
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::sTgcPrepData_p3
Definition: sTgcPrepData_p3.h:19
Muon::MuonPRD_Collection_p2::m_hashId
unsigned int m_hashId
Hash Identifier of this collection.
Definition: MuonPRD_Collection_p2.h:46
sTgcPrepDataContainerCnv_p3.h
Muon::sTgcPrepDataContainerCnv_p3::m_sTgcId
const sTgcIdHelper * m_sTgcId
Definition: sTgcPrepDataContainerCnv_p3.h:40
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
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::sTgcPrepDataContainerCnv_p3::getReadOutElement
const MuonGM::sTgcReadoutElement * getReadOutElement(const Identifier &id) const
Definition: sTgcPrepDataContainerCnv_p3.cxx:39
sTgcPrepData_p3.h
Muon::sTgcPrepDataContainerCnv_p3::initialize
StatusCode initialize(MsgStream &log)
Definition: sTgcPrepDataContainerCnv_p3.cxx:23
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
Muon::sTgcPrepData
Class to represent sTgc measurements.
Definition: sTgcPrepData.h:20
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