ATLAS Offline Software
Loading...
Searching...
No Matches
SiHitCollectionCnv_p4.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
10
11#include <cmath>
12
13//CLHEP
14#include "CLHEP/Geometry/Point3D.h"
15// Gaudi
16#include "GaudiKernel/MsgStream.h"
17#include "GaudiKernel/ThreadLocalContext.h"
18
19// Athena
22
23// * * * stolen from eflowRec * * * //
24inline double phicorr(double a)
25{
26 if (a <= -M_PI)
27 {
28 return a+(2*M_PI*floor(-(a-M_PI)/(2*M_PI)));
29 }
30 else if (a > M_PI)
31 {
32 return a-(2*M_PI*floor((a+M_PI)/(2*M_PI)));
33 }
34 else
35 {
36 return a;
37 }
38}
39
40// * * * stolen from eflowRec * * * //
41inline double cycle(double a, double b)
42{
43 double del = b-a;
44 if (del > M_PI)
45 {
46 return a+2.0*M_PI;
47 }
48 else if (del < -M_PI)
49 {
50 return a-2.0*M_PI;
51 }
52 else
53 {
54 return a;
55 }
56}
57
58
59const double SiHitCollectionCnv_p4::m_persEneUnit = 1.0e-5;
60const double SiHitCollectionCnv_p4::m_persLenUnit = 1.0e-5;
61const double SiHitCollectionCnv_p4::m_persAngUnit = 1.0e-5;
62const double SiHitCollectionCnv_p4::m_2bHalfMaximum = pow(2.0, 15.0);
63const int SiHitCollectionCnv_p4::m_2bMaximum = (unsigned short)(-1);
64
65
66void SiHitCollectionCnv_p4::transToPers(const SiHitCollection* transCont, SiHitCollection_p4* persCont, MsgStream &log)
67{
68 // Finds hits belonging to a "string" (in which the end point of one hit is the same as the start point of the next) and
69 // persistifies the end point of each hit plus the start point of the first hit in each string.
70 //
71 // Further compression is achieved by optimising the storage of the position vectors:- start (x,y,z) and (theta,phi) of
72 // first hit are stored as floats, (delta_theta,delta_phi) relative to the fisrst hit are stored as 2 byte numbers and
73 // used to specify the hit direction. All hit lengths are stored as 2 byte numbers.
74 //
75 // Additional savings are achieved by storing the energy loss for each hit as a 2 byte number and only storing the mean
76 // time of the first hit per string.
77 //
78 // See http://indico.cern.ch/getFile.py/access?contribId=11&resId=2&materialId=slides&confId=30893 for more info.
79
80 static const double dRcut = 1.0e-7;
81 static const double dTcut = 1.0;
82
83 const EventContext& ctx = Gaudi::Hive::currentContext();
84 const IProxyDict* proxy = Atlas::getExtendedEventContext(ctx).proxy();
85 int lastIndex{-1};
86 int lastTruthId{-1};
87 int lastId = -1;
88 double stringFirstTheta = 0.0;
89 double stringFirstPhi = 0.0;
90 double lastT = 0.0;
91 double persSumE = 0.0;
92 double transSumE = 0.0;
93 unsigned int idx = 0;
94 unsigned int endTruthID = 0;
95 unsigned int endId = 0;
96 unsigned int endHit = 0;
97 HepGeom::Point3D<double> lastTransEnd(0.0, 0.0, 0.0);
98 HepGeom::Point3D<double> lastPersEnd(0.0, 0.0, 0.0);
99
100 for (SiHitCollection::const_iterator it = transCont->begin(); it != transCont->end(); ++it) {
101
103 const HepMcParticleLink * currentLink = &(siHit->particleLink());
104 const int truthId = currentLink->id();
105 const char truthSupp= currentLink->getTruthSuppressionTypeAsChar();
106 int index{0};
108 proxy).at(0) != 0) {
109 index = currentLink->eventIndex();
110 }
111
112 if ( lastTruthId != truthId || lastIndex != index || (idx-endTruthID)==USHRT_MAX ) {
113 lastTruthId = truthId;
114 lastIndex = index;
115 const unsigned short persIndex = static_cast<unsigned short>(index);
116 if (static_cast<HepMcParticleLink::index_type>(lastIndex) != static_cast<HepMcParticleLink::index_type>(persIndex)) {
117 log << MSG::WARNING << "Attempting to persistify an eventIndex larger than max unsigned short!" << endmsg;
118 }
119 // store truthId and eventIndex once for set of consecutive hits
120 // with the same truthId and eventIndex.
121 persCont->m_truthID.push_back(static_cast<unsigned long>(truthId));
122 persCont->m_mcEvtIndex.push_back(persIndex);
123 persCont->m_truthSupp.push_back(truthSupp); // TODO include this in the equality check above?
124 if (idx > 0) {
125 persCont->m_nTruthID.push_back(idx - endTruthID);
126 endTruthID = idx;
127 }
128 }
129
130 if ( ( (int)siHit->identify() != lastId ) || (idx-endId)==USHRT_MAX) {
131
132 // store SiHitIdentifier once for set of consecutive hits with
133 // the same SiHitIdentifier
134
135 lastId = siHit->identify();
136 persCont->m_id.push_back(lastId);
137
138 if (idx > 0) {
139 persCont->m_nId.push_back(idx - endId);
140 endId = idx;
141 }
142 }
143
144 HepGeom::Point3D<double> st = siHit->localStartPosition();
145 HepGeom::Point3D<double> en = siHit->localEndPosition();
146
147 const double dx = st.x() - lastTransEnd.x();
148 const double dy = st.y() - lastTransEnd.y();
149 const double dz = st.z() - lastTransEnd.z();
150 const double t = siHit->meanTime();
151
152 const double dRLast = sqrt(dx * dx + dy * dy + dz * dz); // dR between end of previous hit and start of current one
153 const double dTLast = fabs(t - lastT);
154
155 CLHEP::Hep3Vector direction(0.0, 0.0, 0.0);
156 double theta = 0.0;
157 double phi = 0.0;
158 bool startNewString = (dRLast >= dRcut || dTLast >= dTcut || (idx - endHit) == USHRT_MAX);
159
160 if (!startNewString) {
161
162 // hit is part of existing string
163
164 direction = CLHEP::Hep3Vector( en.x() - lastPersEnd.x(), en.y() - lastPersEnd.y(), en.z() - lastPersEnd.z() );
165
166 theta = direction.theta();
167 phi = phicorr( direction.phi() );
168
169 const int dTheta_2b = (int)( (theta - stringFirstTheta) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
170 const int dPhi_2b = (int)( (cycle(phi, stringFirstPhi) - stringFirstPhi) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
171
172 if ( dTheta_2b < m_2bMaximum && dTheta_2b >= 0 && dPhi_2b < m_2bMaximum && dPhi_2b >= 0) {
173 persCont->m_dTheta.push_back(dTheta_2b);
174 persCont->m_dPhi.push_back(dPhi_2b);
175 theta = stringFirstTheta + ( (double)dTheta_2b - m_2bHalfMaximum ) * m_persAngUnit;
176 phi = stringFirstPhi + ( (double)dPhi_2b - m_2bHalfMaximum ) * m_persAngUnit;
177 phi = phicorr(phi);
178 }
179 else {
180 startNewString = true;
181 }
182 }
183
184 if (startNewString) {
185
186 // begin new hit string
187
188 direction = CLHEP::Hep3Vector( en.x() - st.x(), en.y() - st.y(), en.z() - st.z() );
189
190 theta = direction.theta();
191 phi = phicorr( direction.phi() );
192
193 persCont->m_hit1_meanTime.push_back(t);
194 persCont->m_hit1_x0.push_back(st.x());
195 persCont->m_hit1_y0.push_back(st.y());
196 persCont->m_hit1_z0.push_back(st.z());
197 persCont->m_hit1_theta.push_back(theta);
198 persCont->m_hit1_phi.push_back(phi);
199
200 lastPersEnd = HepGeom::Point3D<double>(st.x(), st.y(), st.z());
201
202 stringFirstTheta = theta;
203 stringFirstPhi = phi;
204
205 if (idx > 0) {
206 persCont->m_nHits.push_back(idx - endHit);
207 endHit = idx;
208 }
209 }
210
211 lastTransEnd = HepGeom::Point3D<double>(en.x(), en.y(), en.z());
212 transSumE += siHit->energyLoss();
213
214 const int eneLoss_2b = (int)((transSumE - persSumE) / m_persEneUnit + 0.5); // calculated to allow recovery sum over
215 // whole hit string to chosen precision
216
217 const int hitLength_2b = (int)(direction.mag() / m_persLenUnit + 0.5); // calculated to give the correct position to
218 // the chosen precision, NOT the length of the
219 // hit (small difference in practice).
220 double eneLoss = 0.0;
221
222 if (eneLoss_2b >= m_2bMaximum) {
223 eneLoss = siHit->energyLoss();
224 persCont->m_hitEne_2b.push_back(m_2bMaximum);
225 persCont->m_hitEne_4b.push_back(eneLoss);
226 }
227 else {
228 eneLoss = eneLoss_2b * m_persEneUnit;
229 persCont->m_hitEne_2b.push_back(eneLoss_2b);
230 }
231
232 double length = 0.0;
233
234 if (hitLength_2b >= m_2bMaximum) {
235 length = direction.mag();
236 persCont->m_hitLength_2b.push_back(m_2bMaximum);
237 persCont->m_hitLength_4b.push_back(direction.mag());
238 }
239 else {
240 length = hitLength_2b * m_persLenUnit;
241 persCont->m_hitLength_2b.push_back(hitLength_2b);
242 }
243
244 CLHEP::Hep3Vector persDir(length, 0.0, 0.0);
245 persDir.setTheta(theta);
246 persDir.setPhi(phi);
247
248 lastPersEnd = (CLHEP::Hep3Vector)lastPersEnd + persDir;
249 persSumE += eneLoss;
250 lastT = t;
251
252 ++idx;
253 }
254
255 persCont->m_nTruthID.push_back(idx - endTruthID);
256 persCont->m_nId.push_back(idx - endId);
257 persCont->m_nHits.push_back(idx - endHit);
258#ifdef ENABLE_SANITY_CHECKS
259 // Sanity check
260 const unsigned int init(0);
261 const unsigned int transContSize = transCont->size();
262 if (std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init)!=transContSize) {
263 log << MSG::ERROR << "transToPers: sum of entries of persCont->m_nTruthID (" << std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
264 }
265 if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
266 log << MSG::ERROR << "transToPers: sum of entries of persCont->m_nId (" << std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
267 }
268 if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
269 log << MSG::ERROR << "transToPers: sum of entries of persCont->m_nHits (" << std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
270 }
271#endif
272}
273
274
276 std::unique_ptr<SiHitCollection> trans(std::make_unique<SiHitCollection>("DefaultCollectionName",persObj->m_nHits.size()));
277 persToTrans(persObj, trans.get(), log);
278 return(trans.release());
279}
280
281
282#ifdef ENABLE_SANITY_CHECKS
283void SiHitCollectionCnv_p4::persToTrans(const SiHitCollection_p4* persCont, SiHitCollection* transCont, MsgStream &log)
284#else
285void SiHitCollectionCnv_p4::persToTrans(const SiHitCollection_p4* persCont, SiHitCollection* transCont, MsgStream &/*log*/)
286#endif
287{
288 const EventContext& ctx = Gaudi::Hive::currentContext();
289#ifdef ENABLE_SANITY_CHECKS
290 // Sanity check
291 const unsigned int transContSize = persCont->m_hitEne_2b.size(); // this vector has one entry per transient SiHit, so its size can be used as a proxy for the transient Container size
292 const unsigned int init(0);
293 if (std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init)!=transContSize) {
294 log << MSG::ERROR << "persToTrans: sum of entries of persCont->m_nTruthID (" << std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
295 }
296 if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
297 log << MSG::ERROR << "persToTrans: sum of entries of persCont->m_nId (" << std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
298 }
299 if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
300 log << MSG::ERROR << "persToTrans: sum of entries of persCont->m_nHits (" << std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init) << ") does not match transient container size = " << transContSize << endmsg;
301 }
302#endif
303
304 unsigned int hitCount = 0;
305 unsigned int angleCount = 0;
306 unsigned int idxTruthID = 0;
307 unsigned int idxId = 0;
308 unsigned int idxEne4b = 0;
309 unsigned int idxLen4b = 0;
310 unsigned int endHit = 0;
311 unsigned int endTruthID = 0;
312 unsigned int endId = 0;
313
314 for (unsigned int i = 0; i < persCont->m_nHits.size(); i++) {
315
316 if (persCont->m_nHits[i]) {
317
318 const unsigned int start = endHit;
319 endHit += persCont->m_nHits[i];
320
321 const double t0 = persCont->m_hit1_meanTime[i];
322 const double theta0 = persCont->m_hit1_theta[i];
323 const double phi0 = persCont->m_hit1_phi[i];
324 HepGeom::Point3D<double> endLast(persCont->m_hit1_x0[i], persCont->m_hit1_y0[i], persCont->m_hit1_z0[i]);
325
326 for (unsigned int j = start; j < endHit; j++) {
327
328 if (j >= endTruthID + persCont->m_nTruthID[idxTruthID])
329 endTruthID += persCont->m_nTruthID[idxTruthID++];
330
331 if (j >= endId + persCont->m_nId[idxId])
332 endId += persCont->m_nId[idxId++];
333
334 const double eneLoss_2b = persCont->m_hitEne_2b[hitCount];
335 const double hitLength_2b = persCont->m_hitLength_2b[hitCount];
336
337 const double eneLoss = (eneLoss_2b < m_2bMaximum) ? eneLoss_2b * m_persEneUnit : persCont->m_hitEne_4b[idxEne4b++];
338 const double length = (hitLength_2b < m_2bMaximum) ? hitLength_2b * m_persLenUnit : persCont->m_hitLength_4b[idxLen4b++];
339
340 const double dTheta = (j > start) ? ((double)persCont->m_dTheta[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
341 const double dPhi = (j > start) ? ((double)persCont->m_dPhi[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
342
343 const double meanTime = t0;
344 const double theta = theta0 + dTheta;
345 const double phi = phicorr(phi0 + dPhi);
346
347 CLHEP::Hep3Vector r(length, 0.0, 0.0);
348 r.setTheta(theta);
349 r.setPhi(phi);
350
351 HepGeom::Point3D<double> endThis( endLast + r );
352
354 if (persCont->m_mcEvtIndex[idxTruthID] == 0) {
356 }
357 HepMcParticleLink partLink( persCont->m_truthID[idxTruthID], persCont->m_mcEvtIndex[idxTruthID], flag, HepMcParticleLink::IS_ID, ctx );
359 transCont->Emplace( endLast, endThis, eneLoss, meanTime, partLink, persCont->m_id[idxId] );
360
361 endLast = std::move(endThis);
362
363 ++hitCount;
364 if (j > start) ++angleCount;
365 }
366 }
367 }
368}
#define M_PI
Scalar phi() const
phi method
Scalar theta() const
theta method
#define endmsg
double length(const pvec &v)
static Double_t a
static Double_t t0
double phicorr(double a)
double cycle(double a, double b)
double phicorr(double a)
double cycle(double a, double b)
AtlasHitsVector< SiHit > SiHitCollection
constexpr int pow(int base, int exp) noexcept
CONT::const_iterator const_iterator
const_iterator begin() const
void Emplace(Args &&... args)
size_type size() const
const_iterator end() const
static const double m_persLenUnit
virtual void transToPers(const SiHitCollection *transCont, SiHitCollection_p4 *persCont, MsgStream &log)
static const double m_2bHalfMaximum
virtual void persToTrans(const SiHitCollection_p4 *persCont, SiHitCollection *transCont, MsgStream &log)
static const double m_persEneUnit
virtual SiHitCollection * createTransient(const SiHitCollection_p4 *persObj, MsgStream &log)
static const double m_persAngUnit
std::vector< unsigned short > m_hitEne_2b
std::vector< char > m_truthSupp
std::vector< float > m_hit1_y0
std::vector< float > m_hitEne_4b
std::vector< unsigned short > m_dTheta
std::vector< float > m_hit1_z0
std::vector< float > m_hit1_phi
std::vector< float > m_hitLength_4b
std::vector< unsigned short > m_dPhi
std::vector< unsigned short > m_nHits
std::vector< unsigned short > m_hitLength_2b
std::vector< float > m_hit1_meanTime
std::vector< float > m_hit1_x0
std::vector< float > m_hit1_theta
std::vector< unsigned long > m_truthID
std::vector< unsigned short > m_nTruthID
std::vector< unsigned long > m_id
std::vector< unsigned short > m_nId
std::vector< unsigned short > m_mcEvtIndex
int r
Definition globals.cxx:22
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
Definition index.py:1