ATLAS Offline Software
SiHitCollectionCnv_p4.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"
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
21 #include "StoreGate/StoreGateSvc.h"
22 
23 // * * * stolen from eflowRec * * * //
24 inline 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 * * * //
41 inline 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 
59 const double SiHitCollectionCnv_p4::m_persEneUnit = 1.0e-5;
60 const double SiHitCollectionCnv_p4::m_persLenUnit = 1.0e-5;
61 const double SiHitCollectionCnv_p4::m_persAngUnit = 1.0e-5;
62 const double SiHitCollectionCnv_p4::m_2bHalfMaximum = pow(2.0, 15.0);
63 const int SiHitCollectionCnv_p4::m_2bMaximum = (unsigned short)(-1);
64 
65 
66 void 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  int index{0};
107  proxy).at(0) != 0) {
108  index = currentLink->eventIndex();
109  }
110 
111  if ( lastTruthId != truthId || lastIndex != index || (idx-endTruthID)==USHRT_MAX ) {
112  lastTruthId = truthId;
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 truthId and eventIndex once for set of consecutive hits
119  // with the same truthId and eventIndex.
120  persCont->m_truthID.push_back(static_cast<unsigned long>(truthId));
121  persCont->m_mcEvtIndex.push_back(persIndex);
122 
123  if (idx > 0) {
124  persCont->m_nTruthID.push_back(idx - endTruthID);
125  endTruthID = idx;
126  }
127  }
128 
129  if ( ( (int)siHit->identify() != lastId ) || (idx-endId)==USHRT_MAX) {
130 
131  // store SiHitIdentifier once for set of consecutive hits with
132  // the same SiHitIdentifier
133 
134  lastId = siHit->identify();
135  persCont->m_id.push_back(lastId);
136 
137  if (idx > 0) {
138  persCont->m_nId.push_back(idx - endId);
139  endId = idx;
140  }
141  }
142 
143  HepGeom::Point3D<double> st = siHit->localStartPosition();
144  HepGeom::Point3D<double> en = siHit->localEndPosition();
145 
146  const double dx = st.x() - lastTransEnd.x();
147  const double dy = st.y() - lastTransEnd.y();
148  const double dz = st.z() - lastTransEnd.z();
149  const double t = siHit->meanTime();
150 
151  const double dRLast = sqrt(dx * dx + dy * dy + dz * dz); // dR between end of previous hit and start of current one
152  const double dTLast = fabs(t - lastT);
153 
154  CLHEP::Hep3Vector direction(0.0, 0.0, 0.0);
155  double theta = 0.0;
156  double phi = 0.0;
157  bool startNewString = (dRLast >= dRcut || dTLast >= dTcut || (idx - endHit) == USHRT_MAX);
158 
159  if (!startNewString) {
160 
161  // hit is part of existing string
162 
163  direction = CLHEP::Hep3Vector( en.x() - lastPersEnd.x(), en.y() - lastPersEnd.y(), en.z() - lastPersEnd.z() );
164 
165  theta = direction.theta();
166  phi = phicorr( direction.phi() );
167 
168  const int dTheta_2b = (int)( (theta - stringFirstTheta) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
169  const int dPhi_2b = (int)( (cycle(phi, stringFirstPhi) - stringFirstPhi) / m_persAngUnit + m_2bHalfMaximum + 0.5 );
170 
171  if ( dTheta_2b < m_2bMaximum && dTheta_2b >= 0 && dPhi_2b < m_2bMaximum && dPhi_2b >= 0) {
172  persCont->m_dTheta.push_back(dTheta_2b);
173  persCont->m_dPhi.push_back(dPhi_2b);
174  theta = stringFirstTheta + ( (double)dTheta_2b - m_2bHalfMaximum ) * m_persAngUnit;
175  phi = stringFirstPhi + ( (double)dPhi_2b - m_2bHalfMaximum ) * m_persAngUnit;
176  phi = phicorr(phi);
177  }
178  else {
179  startNewString = true;
180  }
181  }
182 
183  if (startNewString) {
184 
185  // begin new hit string
186 
187  direction = CLHEP::Hep3Vector( en.x() - st.x(), en.y() - st.y(), en.z() - st.z() );
188 
189  theta = direction.theta();
190  phi = phicorr( direction.phi() );
191 
192  persCont->m_hit1_meanTime.push_back(t);
193  persCont->m_hit1_x0.push_back(st.x());
194  persCont->m_hit1_y0.push_back(st.y());
195  persCont->m_hit1_z0.push_back(st.z());
196  persCont->m_hit1_theta.push_back(theta);
197  persCont->m_hit1_phi.push_back(phi);
198 
199  lastPersEnd = HepGeom::Point3D<double>(st.x(), st.y(), st.z());
200 
201  stringFirstTheta = theta;
202  stringFirstPhi = phi;
203 
204  if (idx > 0) {
205  persCont->m_nHits.push_back(idx - endHit);
206  endHit = idx;
207  }
208  }
209 
210  lastTransEnd = HepGeom::Point3D<double>(en.x(), en.y(), en.z());
211  transSumE += siHit->energyLoss();
212 
213  const int eneLoss_2b = (int)((transSumE - persSumE) / m_persEneUnit + 0.5); // calculated to allow recovery sum over
214  // whole hit string to chosen precision
215 
216  const int hitLength_2b = (int)(direction.mag() / m_persLenUnit + 0.5); // calculated to give the correct position to
217  // the chosen precision, NOT the length of the
218  // hit (small difference in practice).
219  double eneLoss = 0.0;
220 
221  if (eneLoss_2b >= m_2bMaximum) {
222  eneLoss = siHit->energyLoss();
223  persCont->m_hitEne_2b.push_back(m_2bMaximum);
224  persCont->m_hitEne_4b.push_back(eneLoss);
225  }
226  else {
227  eneLoss = eneLoss_2b * m_persEneUnit;
228  persCont->m_hitEne_2b.push_back(eneLoss_2b);
229  }
230 
231  double length = 0.0;
232 
233  if (hitLength_2b >= m_2bMaximum) {
234  length = direction.mag();
235  persCont->m_hitLength_2b.push_back(m_2bMaximum);
236  persCont->m_hitLength_4b.push_back(direction.mag());
237  }
238  else {
239  length = hitLength_2b * m_persLenUnit;
240  persCont->m_hitLength_2b.push_back(hitLength_2b);
241  }
242 
243  CLHEP::Hep3Vector persDir(length, 0.0, 0.0);
244  persDir.setTheta(theta);
245  persDir.setPhi(phi);
246 
247  lastPersEnd = (CLHEP::Hep3Vector)lastPersEnd + persDir;
248  persSumE += eneLoss;
249  lastT = t;
250 
251  ++idx;
252  }
253 
254  persCont->m_nTruthID.push_back(idx - endTruthID);
255  persCont->m_nId.push_back(idx - endId);
256  persCont->m_nHits.push_back(idx - endHit);
257 #ifdef ENABLE_SANITY_CHECKS
258  // Sanity check
259  const unsigned int init(0);
260  const unsigned int transContSize = transCont->size();
261  if (std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init)!=transContSize) {
262  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;
263  }
264  if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
265  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;
266  }
267  if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
268  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;
269  }
270 #endif
271 }
272 
273 
275  std::unique_ptr<SiHitCollection> trans(std::make_unique<SiHitCollection>("DefaultCollectionName",persObj->m_nHits.size()));
276  persToTrans(persObj, trans.get(), log);
277  return(trans.release());
278 }
279 
280 
281 #ifdef ENABLE_SANITY_CHECKS
282 void SiHitCollectionCnv_p4::persToTrans(const SiHitCollection_p4* persCont, SiHitCollection* transCont, MsgStream &log)
283 #else
284 void SiHitCollectionCnv_p4::persToTrans(const SiHitCollection_p4* persCont, SiHitCollection* transCont, MsgStream &/*log*/)
285 #endif
286 {
287  const EventContext& ctx = Gaudi::Hive::currentContext();
288 #ifdef ENABLE_SANITY_CHECKS
289  // Sanity check
290  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
291  const unsigned int init(0);
292  if (std::accumulate(persCont->m_nTruthID.begin(), persCont->m_nTruthID.end(), init)!=transContSize) {
293  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;
294  }
295  if (std::accumulate(persCont->m_nId.begin(), persCont->m_nId.end(), init)!=transContSize) {
296  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;
297  }
298  if (std::accumulate(persCont->m_nHits.begin(), persCont->m_nHits.end(), init)!=transContSize) {
299  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;
300  }
301 #endif
302 
303  unsigned int hitCount = 0;
304  unsigned int angleCount = 0;
305  unsigned int idxTruthID = 0;
306  unsigned int idxId = 0;
307  unsigned int idxEne4b = 0;
308  unsigned int idxLen4b = 0;
309  unsigned int endHit = 0;
310  unsigned int endTruthID = 0;
311  unsigned int endId = 0;
312 
313  for (unsigned int i = 0; i < persCont->m_nHits.size(); i++) {
314 
315  if (persCont->m_nHits[i]) {
316 
317  const unsigned int start = endHit;
318  endHit += persCont->m_nHits[i];
319 
320  const double t0 = persCont->m_hit1_meanTime[i];
321  const double theta0 = persCont->m_hit1_theta[i];
322  const double phi0 = persCont->m_hit1_phi[i];
323  HepGeom::Point3D<double> endLast(persCont->m_hit1_x0[i], persCont->m_hit1_y0[i], persCont->m_hit1_z0[i]);
324 
325  for (unsigned int j = start; j < endHit; j++) {
326 
327  if (j >= endTruthID + persCont->m_nTruthID[idxTruthID])
328  endTruthID += persCont->m_nTruthID[idxTruthID++];
329 
330  if (j >= endId + persCont->m_nId[idxId])
331  endId += persCont->m_nId[idxId++];
332 
333  const double eneLoss_2b = persCont->m_hitEne_2b[hitCount];
334  const double hitLength_2b = persCont->m_hitLength_2b[hitCount];
335 
336  const double eneLoss = (eneLoss_2b < m_2bMaximum) ? eneLoss_2b * m_persEneUnit : persCont->m_hitEne_4b[idxEne4b++];
337  const double length = (hitLength_2b < m_2bMaximum) ? hitLength_2b * m_persLenUnit : persCont->m_hitLength_4b[idxLen4b++];
338 
339  const double dTheta = (j > start) ? ((double)persCont->m_dTheta[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
340  const double dPhi = (j > start) ? ((double)persCont->m_dPhi[angleCount] - m_2bHalfMaximum) * m_persAngUnit : 0.0;
341 
342  const double meanTime = t0;
343  const double theta = theta0 + dTheta;
344  const double phi = phicorr(phi0 + dPhi);
345 
346  CLHEP::Hep3Vector r(length, 0.0, 0.0);
347  r.setTheta(theta);
348  r.setPhi(phi);
349 
350  HepGeom::Point3D<double> endThis( endLast + r );
351 
353  if (persCont->m_mcEvtIndex[idxTruthID] == 0) {
355  }
356  HepMcParticleLink partLink( persCont->m_truthID[idxTruthID], persCont->m_mcEvtIndex[idxTruthID], flag, HepMcParticleLink::IS_ID, ctx );
357  transCont->Emplace( endLast, endThis, eneLoss, meanTime, partLink, persCont->m_id[idxId] );
358 
359  endLast = endThis;
360 
361  ++hitCount;
362  if (j > start) ++angleCount;
363  }
364  }
365  }
366 }
SiHitCollection_p4::m_hit1_x0
std::vector< float > m_hit1_x0
Definition: SiHitCollection_p4.h:19
beamspotman.r
def r
Definition: beamspotman.py:676
SiHitCollection_p4::m_hitEne_2b
std::vector< unsigned short > m_hitEne_2b
Definition: SiHitCollection_p4.h:26
SiHitCollection_p4::m_hitLength_2b
std::vector< unsigned short > m_hitLength_2b
Definition: SiHitCollection_p4.h:27
xAOD::short
short
Definition: Vertex_v1.cxx:165
StateLessPT_NewConfig.proxy
proxy
Definition: StateLessPT_NewConfig.py:392
phi
Scalar phi() const
phi method
Definition: AmgMatrixBasePlugin.h:64
SiHitCollection_p4::m_truthID
std::vector< unsigned long > m_truthID
Definition: SiHitCollection_p4.h:36
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
SiHitCollection_p4::m_nTruthID
std::vector< unsigned short > m_nTruthID
Definition: SiHitCollection_p4.h:38
SiHit.h
index
Definition: index.py:1
ExtendedEventContext.h
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
theta
Scalar theta() const
theta method
Definition: AmgMatrixBasePlugin.h:71
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
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:423
M_PI
#define M_PI
Definition: ActiveFraction.h:11
SiHitCollectionCnv_p4::m_persAngUnit
static const double m_persAngUnit
Definition: SiHitCollectionCnv_p4.h:32
SiHitCollection_p4::m_mcEvtIndex
std::vector< unsigned short > m_mcEvtIndex
Definition: SiHitCollection_p4.h:37
SiHitCollectionCnv_p4.h
SiHitCollectionCnv_p4::m_2bHalfMaximum
static const double m_2bHalfMaximum
Definition: SiHitCollectionCnv_p4.h:33
SiHitCollection_p4::m_hit1_z0
std::vector< float > m_hit1_z0
Definition: SiHitCollection_p4.h:21
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
AtlasHitsVector::begin
const_iterator begin() const
Definition: AtlasHitsVector.h:131
SiHitCollection_p4::m_nHits
std::vector< unsigned short > m_nHits
Definition: SiHitCollection_p4.h:24
IProxyDict
A proxy dictionary.
Definition: AthenaKernel/AthenaKernel/IProxyDict.h:51
AtlasHitsVector::const_iterator
CONT::const_iterator const_iterator
Definition: AtlasHitsVector.h:43
Atlas::getExtendedEventContext
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
Definition: ExtendedEventContext.cxx:32
AtlasHitsVector::Emplace
void Emplace(Args &&... args)
Definition: AtlasHitsVector.h:81
SiHitCollection_p4::m_hitLength_4b
std::vector< float > m_hitLength_4b
Definition: SiHitCollection_p4.h:34
SiHitCollection_p4::m_dTheta
std::vector< unsigned short > m_dTheta
Definition: SiHitCollection_p4.h:29
SiHitCollectionCnv_p4::transToPers
virtual void transToPers(const SiHitCollection *transCont, SiHitCollection_p4 *persCont, MsgStream &log)
Definition: SiHitCollectionCnv_p4.cxx:66
cycle
double cycle(double a, double b)
Definition: SiHitCollectionCnv_p4.cxx:41
lumiFormat.i
int i
Definition: lumiFormat.py:92
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:530
SiHitCollection_p4::m_hit1_meanTime
std::vector< float > m_hit1_meanTime
Definition: SiHitCollection_p4.h:18
master.flag
bool flag
Definition: master.py:29
SiHitCollection_p4::m_hit1_phi
std::vector< float > m_hit1_phi
Definition: SiHitCollection_p4.h:23
SiHitCollection_p4::m_dPhi
std::vector< unsigned short > m_dPhi
Definition: SiHitCollection_p4.h:30
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
SiHitCollection_p4.h
SiHitCollection_p4::m_hit1_y0
std::vector< float > m_hit1_y0
Definition: SiHitCollection_p4.h:20
SiHitCollection_p4::m_hit1_theta
std::vector< float > m_hit1_theta
Definition: SiHitCollection_p4.h:22
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
PlotCalibFromCool.en
en
Definition: PlotCalibFromCool.py:399
python.PyKernel.init
def init(v_theApp, v_rootStream=None)
Definition: PyKernel.py:45
SiHitCollectionCnv_p4::persToTrans
virtual void persToTrans(const SiHitCollection_p4 *persCont, SiHitCollection *transCont, MsgStream &log)
Definition: SiHitCollectionCnv_p4.cxx:284
makeTRTBarrelCans.dy
tuple dy
Definition: makeTRTBarrelCans.py:21
DeMoScan.index
string index
Definition: DeMoScan.py:362
a
TList * a
Definition: liststreamerinfos.cxx:10
SiHitCollectionCnv_p4::createTransient
virtual SiHitCollection * createTransient(const SiHitCollection_p4 *persObj, MsgStream &log)
Definition: SiHitCollectionCnv_p4.cxx:274
AtlasHitsVector::end
const_iterator end() const
Definition: AtlasHitsVector.h:134
makeTRTBarrelCans.dx
tuple dx
Definition: makeTRTBarrelCans.py:20
phicorr
double phicorr(double a)
Definition: SiHitCollectionCnv_p4.cxx:24
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
SiHitCollection_p4::m_hitEne_4b
std::vector< float > m_hitEne_4b
Definition: SiHitCollection_p4.h:32
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
SiHitCollectionCnv_p4::m_2bMaximum
static const int m_2bMaximum
Definition: SiHitCollectionCnv_p4.h:34
AtlasHitsVector::size
size_type size() const
Definition: AtlasHitsVector.h:143
SiHitCollection_p4::m_nId
std::vector< unsigned short > m_nId
Definition: SiHitCollection_p4.h:41
SiHitCollectionCnv_p4::m_persEneUnit
static const double m_persEneUnit
Definition: SiHitCollectionCnv_p4.h:30
SiHitCollectionCnv_p4::m_persLenUnit
static const double m_persLenUnit
Definition: SiHitCollectionCnv_p4.h:31
StoreGateSvc.h
length
double length(const pvec &v)
Definition: FPGATrackSimLLPDoubletHoughTransformTool.cxx:26
SiHitCollection_p4::m_id
std::vector< unsigned long > m_id
Definition: SiHitCollection_p4.h:40
SiHitCollection_p4
Definition: SiHitCollection_p4.h:12
SiHitCollection.h