ATLAS Offline Software
Loading...
Searching...
No Matches
SiHitCollectionCnv_p3.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 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
23
24// * * * stolen from eflowRec * * * //
25inline double phicorr(double a)
26{
27 if (a <= -M_PI)
28 {
29 return a+(2*M_PI*floor(-(a-M_PI)/(2*M_PI)));
30 }
31 else if (a > M_PI)
32 {
33 return a-(2*M_PI*floor((a+M_PI)/(2*M_PI)));
34 }
35 else
36 {
37 return a;
38 }
39}
40
41// * * * stolen from eflowRec * * * //
42inline double cycle(double a, double b)
43{
44 double del = b-a;
45 if (del > M_PI)
46 {
47 return a+2.0*M_PI;
48 }
49 else if (del < -M_PI)
50 {
51 return a-2.0*M_PI;
52 }
53 else
54 {
55 return a;
56 }
57}
58
59
60const double SiHitCollectionCnv_p3::m_persEneUnit = 1.0e-5;
61const double SiHitCollectionCnv_p3::m_persLenUnit = 1.0e-5;
62const double SiHitCollectionCnv_p3::m_persAngUnit = 1.0e-5;
63const double SiHitCollectionCnv_p3::m_2bHalfMaximum = pow(2.0, 15.0);
64const int SiHitCollectionCnv_p3::m_2bMaximum = (unsigned short)(-1);
65
66
67void SiHitCollectionCnv_p3::transToPers(const SiHitCollection* transCont, SiHitCollection_p3* persCont, MsgStream &log)
68{
69 // 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
70 // persistifies the end point of each hit plus the start point of the first hit in each string.
71 //
72 // Further compression is achieved by optimising the storage of the position vectors:- start (x,y,z) and (theta,phi) of
73 // first hit are stored as floats, (delta_theta,delta_phi) relative to the fisrst hit are stored as 2 byte numbers and
74 // used to specify the hit direction. All hit lengths are stored as 2 byte numbers.
75 //
76 // Additional savings are achieved by storing the energy loss for each hit as a 2 byte number and only storing the mean
77 // time of the first hit per string.
78 //
79 // See http://indico.cern.ch/getFile.py/access?contribId=11&resId=2&materialId=slides&confId=30893 for more info.
80
81 static const double dRcut = 1.0e-7;
82 static const double dTcut = 1.0;
83
85 int lastIndex{-1};
86 int lastBarcode{-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 endBC = 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 barcode = currentLink->barcode();
105 int index{0};
107 proxy).at(0) != 0) {
108 index = currentLink->eventIndex();
109 }
110
111 if ( lastBarcode != barcode || lastIndex != index || (idx-endBC)==USHRT_MAX ) {
112 lastBarcode = barcode;
113 lastIndex = index;
114 const unsigned short persIndex = static_cast<unsigned short>(index);
115 if (static_cast<HepMcParticleLink::index_type>(lastIndex) != static_cast<HepMcParticleLink::index_type>(persIndex)) {
116 log << MSG::WARNING << "Attempting to persistify an eventIndex larger than max unsigned short!" << endmsg;
117 }
118 // store barcode, eventIndex and McEventCollection once for set
119 // of consecutive hits with the same barcode and eventIndex
120 persCont->m_barcode.push_back(static_cast<unsigned long>(lastBarcode));
121 persCont->m_mcEvtIndex.push_back(persIndex);
122 persCont->m_evtColl.push_back('a'); // Hard-coding as this only ever had a single value in production
123
124 if (idx > 0) {
125 persCont->m_nBC.push_back(idx - endBC);
126 endBC = 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_nBC.push_back(idx - endBC);
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_nBC.begin(), persCont->m_nBC.end(), init)!=transContSize) {
263 log << MSG::ERROR << "transToPers: sum of entries of persCont->m_nBC (" << std::accumulate(persCont->m_nBC.begin(), persCont->m_nBC.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_p3::persToTrans(const SiHitCollection_p3* persCont, SiHitCollection* transCont, MsgStream &log)
284#else
285void SiHitCollectionCnv_p3::persToTrans(const SiHitCollection_p3* 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_nBC.begin(), persCont->m_nBC.end(), init)!=transContSize) {
294 log << MSG::ERROR << "persToTrans: sum of entries of persCont->m_nBC (" << std::accumulate(persCont->m_nBC.begin(), persCont->m_nBC.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 idxBC = 0;
307 unsigned int idxId = 0;
308 unsigned int idxEne4b = 0;
309 unsigned int idxLen4b = 0;
310 unsigned int endHit = 0;
311 unsigned int endBC = 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 >= endBC + persCont->m_nBC[idxBC])
329 endBC += persCont->m_nBC[idxBC++];
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[idxBC] == 0) {
356 }
357 HepMcParticleLink partLink( persCont->m_barcode[idxBC], persCont->m_mcEvtIndex[idxBC], flag, HepMcParticleLink::IS_BARCODE, ctx );
358 if ( HepMC::BarcodeBased::is_truth_suppressed_pileup(static_cast<int>(persCont->m_barcode[idxBC])) ) {
360 }
361 transCont->Emplace( endLast, endThis, eneLoss, meanTime, partLink, persCont->m_id[idxId]);
362
363 endLast = std::move(endThis);
364
365 ++hitCount;
366 if (j > start) ++angleCount;
367 }
368 }
369 }
370}
#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
@ EBC_PU_SUPPRESSED
double phicorr(double a)
double cycle(double a, double b)
double phicorr(double a)
double cycle(double a, double b)
AtlasHitsVector< SiHit > SiHitCollection
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_2bHalfMaximum
static const double m_persEneUnit
static const double m_persLenUnit
static const double m_persAngUnit
virtual void transToPers(const SiHitCollection *transCont, SiHitCollection_p3 *persCont, MsgStream &log)
virtual SiHitCollection * createTransient(const SiHitCollection_p3 *persObj, MsgStream &log)
virtual void persToTrans(const SiHitCollection_p3 *persCont, SiHitCollection *transCont, MsgStream &log)
std::vector< unsigned short > m_nBC
std::vector< float > m_hit1_theta
std::vector< float > m_hit1_meanTime
std::vector< float > m_hit1_y0
std::vector< float > m_hit1_x0
std::vector< char > m_evtColl
std::vector< unsigned long > m_barcode
std::vector< float > m_hitEne_4b
std::vector< unsigned short > m_mcEvtIndex
std::vector< unsigned short > m_nId
std::vector< unsigned short > m_dPhi
std::vector< float > m_hitLength_4b
std::vector< unsigned short > m_nHits
std::vector< unsigned short > m_dTheta
std::vector< unsigned short > m_hitLength_2b
std::vector< float > m_hit1_z0
std::vector< unsigned short > m_hitEne_2b
std::vector< float > m_hit1_phi
std::vector< unsigned long > m_id
TH1F * trans(TH1F *h, bool t=false)
int r
Definition globals.cxx:22
IProxyDict * proxyDictFromEventContext()
Return the IProxyDict for this thread's current context.
bool is_truth_suppressed_pileup(const T &p)
Method to establish if a particle (or barcode) corresponds to truth-suppressed pile-up.
constexpr int pow(int x)
Definition conifer.h:27
Definition index.py:1
Some out-of-line helpers for ExtendedEventContext.