ATLAS Offline Software
SiHitCollectionCnv_p2.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "InDetSimEvent/SiHit.h"
9 
10 #include <cmath>
11 
12 //CLHEP
13 #include "CLHEP/Geometry/Point3D.h"
14 // Gaudi
15 #include "GaudiKernel/MsgStream.h"
16 // Athena
18 #include "StoreGate/StoreGateSvc.h"
19 
20 // * * * stolen from eflowRec * * * //
21 inline double phicorr(double a)
22 {
23  if (a <= -M_PI)
24  {
25  return a+(2*M_PI*floor(-(a-M_PI)/(2*M_PI)));
26  }
27  else if (a > M_PI)
28  {
29  return a-(2*M_PI*floor((a+M_PI)/(2*M_PI)));
30  }
31  else
32  {
33  return a;
34  }
35 }
36 
37 // * * * stolen from eflowRec * * * //
38 inline double cycle(double a, double b)
39 {
40  double del = b-a;
41  if (del > M_PI)
42  {
43  return a+2.0*M_PI;
44  }
45  else if (del < -M_PI)
46  {
47  return a-2.0*M_PI;
48  }
49  else
50  {
51  return a;
52  }
53 }
54 
55 
56 const double SiHitCollectionCnv_p2::m_persEneUnit = 1.0e-5;
57 const double SiHitCollectionCnv_p2::m_persLenUnit = 1.0e-5;
58 const double SiHitCollectionCnv_p2::m_persAngUnit = 1.0e-5;
59 const double SiHitCollectionCnv_p2::m_2bHalfMaximum = pow(2.0, 15.0);
60 const int SiHitCollectionCnv_p2::m_2bMaximum = (unsigned short)(-1);
61 
62 
63 void SiHitCollectionCnv_p2::transToPers(const SiHitCollection* transCont, SiHitCollection_p2* persCont, MsgStream &/*log*/)
64 {
65  // 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
66  // persistifies the end point of each hit plus the start point of the first hit in each string.
67  //
68  // Further compression is achieved by optimising the storage of the position vectors:- start (x,y,z) and (theta,phi) of
69  // first hit are stored as floats, (delta_theta,delta_phi) relative to the fisrst hit are stored as 2 byte numbers and
70  // used to specify the hit direction. All hit lengths are stored as 2 byte numbers.
71  //
72  // Additional savings are achieved by storing the energy loss for each hit as a 2 byte number and only storing the mean
73  // time of the first hit per string.
74  //
75  // See http://indico.cern.ch/getFile.py/access?contribId=11&resId=2&materialId=slides&confId=30893 for more info.
76 
77  static const double dRcut = 1.0e-7;
78  static const double dTcut = 1.0;
79 
80  int lastBarcode = -1;
81  int lastId = -1;
82  double stringFirstTheta = 0.0;
83  double stringFirstPhi = 0.0;
84  double lastT = 0.0;
85  double persSumE = 0.0;
86  double transSumE = 0.0;
87  unsigned int idx = 0;
88  unsigned int endBC = 0;
89  unsigned int endId = 0;
90  unsigned int endHit = 0;
91  HepGeom::Point3D<double> lastTransEnd(0.0, 0.0, 0.0);
92  HepGeom::Point3D<double> lastPersEnd(0.0, 0.0, 0.0);
93 
94  for (SiHitCollection::const_iterator it = transCont->begin(); it != transCont->end(); ++it) {
95 
96  // const SiHit* siHit = *it;
97 
99 
100 
101  if ( siHit->particleLink().barcode() != lastBarcode || (idx-endBC)==USHRT_MAX ) {
102 
103  // store barcode once for set of consecutive hits with same barcode
104 
105  lastBarcode = siHit->particleLink().barcode();
106  persCont->m_barcode.push_back(lastBarcode);
107 
108  if (idx > 0) {
109  persCont->m_nBC.push_back(idx - endBC);
110  endBC = idx;
111  }
112  }
113 
114  if ( ( (int)siHit->identify() != lastId ) || (idx-endId)==USHRT_MAX) {
115 
116  // store id once for set of consecutive hits with same barcode
117 
118  lastId = siHit->identify();
119  persCont->m_id.push_back(lastId);
120 
121  if (idx > 0) {
122  persCont->m_nId.push_back(idx - endId);
123  endId = idx;
124  }
125  }
126 
127  HepGeom::Point3D<double> st = siHit->localStartPosition();
128  HepGeom::Point3D<double> en = siHit->localEndPosition();
129 
130  const double dx = st.x() - lastTransEnd.x();
131  const double dy = st.y() - lastTransEnd.y();
132  const double dz = st.z() - lastTransEnd.z();
133  const double t = siHit->meanTime();
134 
135  const double dRLast = sqrt(dx * dx + dy * dy + dz * dz); // dR between end of previous hit and start of current one
136  const double dTLast = fabs(t - lastT);
137 
138  CLHEP::Hep3Vector direction(0.0, 0.0, 0.0);
139  double theta = 0.0;
140  double phi = 0.0;
141  bool startNewString = (dRLast >= dRcut || dTLast >= dTcut || (idx - endHit) == USHRT_MAX);
142 
143  if (!startNewString) {
144 
145  // hit is part of existing string
146 
147  direction = CLHEP::Hep3Vector( en.x() - lastPersEnd.x(), en.y() - lastPersEnd.y(), en.z() - lastPersEnd.z() );
148 
149  theta = direction.theta();
150  phi = phicorr( direction.phi() );
151 
152  const int dTheta_2b = (int)( (theta - stringFirstTheta) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
153  const int dPhi_2b = (int)( (cycle(phi, stringFirstPhi) - stringFirstPhi) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
154 
155  if ( dTheta_2b < m_2bMaximum && dTheta_2b >= 0 && dPhi_2b < m_2bMaximum && dPhi_2b >= 0) {
156  persCont->m_dTheta.push_back(dTheta_2b);
157  persCont->m_dPhi.push_back(dPhi_2b);
158  theta = stringFirstTheta + ( (double)dTheta_2b - m_2bHalfMaximum ) * m_persAngUnit;
159  phi = stringFirstPhi + ( (double)dPhi_2b - m_2bHalfMaximum ) * m_persAngUnit;
160  phi = phicorr(phi);
161  }
162  else {
163  startNewString = true;
164  }
165  }
166 
167  if (startNewString) {
168 
169  // begin new hit string
170 
171  direction = CLHEP::Hep3Vector( en.x() - st.x(), en.y() - st.y(), en.z() - st.z() );
172 
173  theta = direction.theta();
174  phi = phicorr( direction.phi() );
175 
176  persCont->m_hit1_meanTime.push_back(t);
177  persCont->m_hit1_x0.push_back(st.x());
178  persCont->m_hit1_y0.push_back(st.y());
179  persCont->m_hit1_z0.push_back(st.z());
180  persCont->m_hit1_theta.push_back(theta);
181  persCont->m_hit1_phi.push_back(phi);
182 
183  lastPersEnd = st;
184 
185  stringFirstTheta = theta;
186  stringFirstPhi = phi;
187 
188  if (idx > 0) {
189  persCont->m_nHits.push_back(idx - endHit);
190  endHit = idx;
191  }
192  }
193 
194  lastTransEnd = en;
195  transSumE += siHit->energyLoss();
196 
197  const int eneLoss_2b = (int)((transSumE - persSumE) / m_persEneUnit + 0.5); // calculated to allow recovery sum over
198  // whole hit string to chosen precision
199 
200  const int hitLength_2b = (int)(direction.mag() / m_persLenUnit + 0.5); // calculated to give the correct position to
201  // the chosen precision, NOT the length of the
202  // hit (small difference in practice).
203  double eneLoss = 0.0;
204 
205  if (eneLoss_2b >= m_2bMaximum) {
206  eneLoss = siHit->energyLoss();
207  persCont->m_hitEne_2b.push_back(m_2bMaximum);
208  persCont->m_hitEne_4b.push_back(eneLoss);
209  }
210  else {
211  eneLoss = eneLoss_2b * m_persEneUnit;
212  persCont->m_hitEne_2b.push_back(eneLoss_2b);
213  }
214 
215  double length = 0.0;
216 
217  if (hitLength_2b >= m_2bMaximum) {
218  length = direction.mag();
219  persCont->m_hitLength_2b.push_back(m_2bMaximum);
220  persCont->m_hitLength_4b.push_back(direction.mag());
221  }
222  else {
223  length = hitLength_2b * m_persLenUnit;
224  persCont->m_hitLength_2b.push_back(hitLength_2b);
225  }
226 
227  CLHEP::Hep3Vector persDir(length, 0.0, 0.0);
228  persDir.setTheta(theta);
229  persDir.setPhi(phi);
230 
231  lastPersEnd = (CLHEP::Hep3Vector)lastPersEnd + persDir;
232  persSumE += eneLoss;
233  lastT = t;
234 
235  ++idx;
236  }
237 
238  persCont->m_nBC.push_back(idx - endBC);
239  persCont->m_nId.push_back(idx - endId);
240  persCont->m_nHits.push_back(idx - endHit);
241 #ifdef ENABLE_SANITY_CHECKS
242  // Sanity check
243  const unsigned int init(0);
244  const unsigned int transContSize = transCont->size();
245  if (std::accumulate(persCont->m_nBC.begin(), persCont->m_nBC.end(), init)!=transContSize) {
246  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;
247  }
248  if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
249  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;
250  }
251  if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
252  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;
253  }
254 #endif
255 }
256 
257 
259  std::unique_ptr<SiHitCollection> trans(std::make_unique<SiHitCollection>("DefaultCollectionName",persObj->m_nHits.size()));
260  persToTrans(persObj, trans.get(), log);
261  return(trans.release());
262 }
263 
264 
265 #ifdef ENABLE_SANITY_CHECKS
266 void SiHitCollectionCnv_p2::persToTrans(const SiHitCollection_p2* persCont, SiHitCollection* transCont, MsgStream &log)
267 #else
268 void SiHitCollectionCnv_p2::persToTrans(const SiHitCollection_p2* persCont, SiHitCollection* transCont, MsgStream &/*log*/)
269 #endif
270 {
271 #ifdef ENABLE_SANITY_CHECKS
272  // Sanity check
273  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
274  const unsigned int init(0);
275  if (std::accumulate(persCont->m_nBC.begin(), persCont->m_nBC.end(), init)!=transContSize) {
276  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;
277  }
278  if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
279  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;
280  }
281  if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
282  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;
283  }
284 #endif
285 
286  unsigned int hitCount = 0;
287  unsigned int angleCount = 0;
288  unsigned int idxBC = 0;
289  unsigned int idxId = 0;
290  unsigned int idxEne4b = 0;
291  unsigned int idxLen4b = 0;
292  unsigned int endHit = 0;
293  unsigned int endBC = 0;
294  unsigned int endId = 0;
295  // Assume that all Hits should be linked to the hard-scatter GenEvent
297  const int event_number = HepMcParticleLink::getEventNumberAtPosition (0, sg);
298  for (unsigned int i = 0; i < persCont->m_nHits.size(); i++) {
299 
300  if (persCont->m_nHits[i]) {
301 
302  const unsigned int start = endHit;
303  endHit += persCont->m_nHits[i];
304 
305  const double t0 = persCont->m_hit1_meanTime[i];
306  const double theta0 = persCont->m_hit1_theta[i];
307  const double phi0 = persCont->m_hit1_phi[i];
308  HepGeom::Point3D<double> endLast(persCont->m_hit1_x0[i], persCont->m_hit1_y0[i], persCont->m_hit1_z0[i]);
309 
310  for (unsigned int j = start; j < endHit; j++) {
311 
312  if (j >= endBC + persCont->m_nBC[idxBC])
313  endBC += persCont->m_nBC[idxBC++];
314 
315  if (j >= endId + persCont->m_nId[idxId])
316  endId += persCont->m_nId[idxId++];
317 
318  const double eneLoss_2b = persCont->m_hitEne_2b[hitCount];
319  const double hitLength_2b = persCont->m_hitLength_2b[hitCount];
320 
321  const double eneLoss = (eneLoss_2b < m_2bMaximum) ? eneLoss_2b * m_persEneUnit : persCont->m_hitEne_4b[idxEne4b++];
322  const double length = (hitLength_2b < m_2bMaximum) ? hitLength_2b * m_persLenUnit : persCont->m_hitLength_4b[idxLen4b++];
323 
324  const double dTheta = (j > start) ? ((double)persCont->m_dTheta[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
325  const double dPhi = (j > start) ? ((double)persCont->m_dPhi[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
326 
327  const double meanTime = t0;
328  const double theta = theta0 + dTheta;
329  const double phi = phicorr(phi0 + dPhi);
330 
331  CLHEP::Hep3Vector r(length, 0.0, 0.0);
332  r.setTheta(theta);
333  r.setPhi(phi);
334 
335  HepGeom::Point3D<double> endThis( endLast + r );
336 
337  HepMcParticleLink partLink( persCont->m_barcode[idxBC], event_number, HepMcParticleLink::IS_EVENTNUM, HepMcParticleLink::IS_BARCODE, sg);
338  if ( HepMC::BarcodeBased::is_truth_suppressed_pileup(static_cast<int>(persCont->m_barcode[idxBC])) ) {
340  }
341  transCont->Emplace( endLast, endThis, eneLoss, meanTime, partLink, persCont->m_id[idxId]);
342 
343  endLast = endThis;
344 
345  ++hitCount;
346  if (j > start) ++angleCount;
347  }
348  }
349  }
350 }
beamspotman.r
def r
Definition: beamspotman.py:676
xAOD::short
short
Definition: Vertex_v1.cxx:165
SiHitCollection_p2::m_hit1_phi
std::vector< float > m_hit1_phi
Definition: SiHitCollection_p2.h:29
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:67
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
SiHitCollectionCnv_p2::m_2bMaximum
static const int m_2bMaximum
Definition: SiHitCollectionCnv_p2.h:37
SiHitCollection_p2::m_hitLength_4b
std::vector< float > m_hitLength_4b
Definition: SiHitCollection_p2.h:40
SiHit.h
SiHitCollection_p2::m_hitLength_2b
std::vector< unsigned short > m_hitLength_2b
Definition: SiHitCollection_p2.h:33
SiHitCollection_p2
Definition: SiHitCollection_p2.h:18
SiHitCollection_p2::m_hit1_theta
std::vector< float > m_hit1_theta
Definition: SiHitCollection_p2.h:28
InDetAccessor::phi0
@ phi0
Definition: InDetAccessor.h:33
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
mergePhysValFiles.start
start
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:14
cycle
double cycle(double a, double b)
Definition: SiHitCollectionCnv_p2.cxx:38
theta
Scalar theta() const
theta method
Definition: AmgMatrixBasePlugin.h:75
ALFA_EventTPCnv_Dict::t0
std::vector< ALFA_RawData_p1 > t0
Definition: ALFA_EventTPCnvDict.h:42
AtlasHitsVector
Definition: AtlasHitsVector.h:33
skel.it
it
Definition: skel.GENtoEVGEN.py:396
M_PI
#define M_PI
Definition: ActiveFraction.h:11
SiHitCollection_p2::m_hit1_y0
std::vector< float > m_hit1_y0
Definition: SiHitCollection_p2.h:26
SG::CurrentEventStore::store
static IProxyDict * store()
Fetch the current store.
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
AtlasHitsVector::begin
const_iterator begin() const
Definition: AtlasHitsVector.h:131
IProxyDict
A proxy dictionary.
Definition: AthenaKernel/AthenaKernel/IProxyDict.h:47
AtlasHitsVector::const_iterator
CONT::const_iterator const_iterator
Definition: AtlasHitsVector.h:43
SiHitCollection_p2::m_nBC
std::vector< unsigned short > m_nBC
Definition: SiHitCollection_p2.h:43
SiHitCollection_p2::m_id
std::vector< unsigned long > m_id
Definition: SiHitCollection_p2.h:45
AtlasHitsVector::Emplace
void Emplace(Args &&... args)
Definition: AtlasHitsVector.h:81
SiHitCollectionCnv_p2::m_persAngUnit
static const double m_persAngUnit
Definition: SiHitCollectionCnv_p2.h:35
SiHitCollection_p2::m_hitEne_4b
std::vector< float > m_hitEne_4b
Definition: SiHitCollection_p2.h:38
lumiFormat.i
int i
Definition: lumiFormat.py:85
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
SiHitCollection_p2::m_nHits
std::vector< unsigned short > m_nHits
Definition: SiHitCollection_p2.h:30
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:538
SiHitCollection_p2::m_hit1_x0
std::vector< float > m_hit1_x0
Definition: SiHitCollection_p2.h:25
SiHitCollectionCnv_p2::persToTrans
virtual void persToTrans(const SiHitCollection_p2 *persCont, SiHitCollection *transCont, MsgStream &log)
Definition: SiHitCollectionCnv_p2.cxx:268
SiHitCollection_p2::m_barcode
std::vector< unsigned long > m_barcode
Definition: SiHitCollection_p2.h:42
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
SiHitCollectionCnv_p2.h
SiHitCollection_p2::m_hit1_meanTime
std::vector< float > m_hit1_meanTime
Definition: SiHitCollection_p2.h:24
MagicNumbers.h
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
PlotCalibFromCool.en
en
Definition: PlotCalibFromCool.py:399
HepMC::BarcodeBased::is_truth_suppressed_pileup
bool is_truth_suppressed_pileup(const T &p)
Method to establish if a particle (or barcode) corresponds to truth-suppressed pile-up.
Definition: MagicNumbers.h:197
SiHitCollectionCnv_p2::m_persEneUnit
static const double m_persEneUnit
Definition: SiHitCollectionCnv_p2.h:33
python.PyKernel.init
def init(v_theApp, v_rootStream=None)
Definition: PyKernel.py:45
SiHitCollection_p2::m_dTheta
std::vector< unsigned short > m_dTheta
Definition: SiHitCollection_p2.h:35
makeTRTBarrelCans.dy
tuple dy
Definition: makeTRTBarrelCans.py:21
SiHitCollection_p2.h
SiHitCollectionCnv_p2::transToPers
virtual void transToPers(const SiHitCollection *transCont, SiHitCollection_p2 *persCont, MsgStream &log)
Definition: SiHitCollectionCnv_p2.cxx:63
phicorr
double phicorr(double a)
Definition: SiHitCollectionCnv_p2.cxx:21
a
TList * a
Definition: liststreamerinfos.cxx:10
AtlasHitsVector::end
const_iterator end() const
Definition: AtlasHitsVector.h:134
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
AtlasHitsVector::size
size_type size() const
Definition: AtlasHitsVector.h:143
EBC_PU_SUPPRESSED
@ EBC_PU_SUPPRESSED
Definition: MagicNumbers.h:30
SiHitCollection_p2::m_hit1_z0
std::vector< float > m_hit1_z0
Definition: SiHitCollection_p2.h:27
SiHitCollectionCnv_p2::createTransient
virtual SiHitCollection * createTransient(const SiHitCollection_p2 *persObj, MsgStream &log)
Definition: SiHitCollectionCnv_p2.cxx:258
SiHitCollection_p2::m_dPhi
std::vector< unsigned short > m_dPhi
Definition: SiHitCollection_p2.h:36
SiHitCollectionCnv_p2::m_persLenUnit
static const double m_persLenUnit
Definition: SiHitCollectionCnv_p2.h:34
SiHitCollection_p2::m_nId
std::vector< unsigned short > m_nId
Definition: SiHitCollection_p2.h:46
pow
constexpr int pow(int base, int exp) noexcept
Definition: ap_fixedTest.cxx:15
StoreGateSvc.h
SiHitCollectionCnv_p2::m_2bHalfMaximum
static const double m_2bHalfMaximum
Definition: SiHitCollectionCnv_p2.h:36
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
SiHitCollection_p2::m_hitEne_2b
std::vector< unsigned short > m_hitEne_2b
Definition: SiHitCollection_p2.h:32
SiHitCollection.h