ATLAS Offline Software
Loading...
Searching...
No Matches
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
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}
#define endmsg
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
static Double_t sc
void diff(const Jet &rJet1, const Jet &rJet2, std::map< std::string, double > varDiff)
Difference between jets - Non-Class function required by trigger.
Definition Jet.cxx:631
void reserve(size_type n)
value_type push_back(value_type pElem)
size_type size() const noexcept
const_iterator end() const
return const_iterator for end of container
virtual size_t numberOfCollections() const override final
return number of collections
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,...
const_iterator begin() const
return const_iterator for first entry
value_type get_compact() const
Get the compact id.
This is a "hash" representation of an Identifier.
value_type get_compact() const
Get the compact id.
Identifier32 get_identifier32() const
Get the 32-bit version Identifier, will be invalid if >32 bits needed.
An sTgcReadoutElement corresponds to a single STGC module; therefore typicaly a barrel muon station c...
Class to hold the persistent representation of MuonPRD_Collection.
unsigned short m_size
Collection size into master collection Note I use a short.
unsigned int m_hashId
Hash Identifier of this collection.
unsigned int m_id
Identifier of this collection.
std::vector< MuonPRD_Collection_p2 > m_collections
std::vector< unsigned short > m_prdDeltaId
The delta identifiers of the PRD i.e.
virtual Identifier identify() const override final
virtual void setIdentifier(Identifier id)
virtual IdentifierHash identifyHash() const override final
virtual Muon::sTgcPrepDataContainer * createTransient(const Muon::sTgcPrepDataContainer_p3 *persObj, MsgStream &log)
virtual void transToPers(const Muon::sTgcPrepDataContainer *transCont, Muon::sTgcPrepDataContainer_p3 *persCont, MsgStream &log)
const MuonGM::sTgcReadoutElement * getReadOutElement(const Identifier &id) const
virtual void persToTrans(const Muon::sTgcPrepDataContainer_p3 *persCont, Muon::sTgcPrepDataContainer *transCont, MsgStream &log)
ToolHandle< Trk::IEventCnvSuperTool > m_eventCnvTool
Class to represent sTgc measurements.
virtual const Trk::TrkDetElementBase * getDetectorElement(const Identifier &id, const IdentifierHash &idHash) const =0
Returns the detectorElement associated with this Identifier & Hash.
static int channelMin()
static Muon::sTgcPrepData createsTgcPrepData(const Muon::sTgcPrepData_p3 *persObj, const Identifier clusId, const MuonGM::sTgcReadoutElement *m_detEl, MsgStream &log)
void transToPers(const Muon::sTgcPrepData *transObj, Muon::sTgcPrepData_p3 *persObj, MsgStream &log)
MuonPRD_Container_p2< sTgcPrepData_p3 > sTgcPrepDataContainer_p3
MuonPrepDataContainerT< sTgcPrepData > sTgcPrepDataContainer
MuonPrepDataCollection< sTgcPrepData > sTgcPrepDataCollection
void initialize()