13 #include "CLHEP/Geometry/Point3D.h"
14 #include "CLHEP/Units/SystemOfUnits.h"
17 #include "GaudiKernel/MsgStream.h"
18 #include "GaudiKernel/ThreadLocalContext.h"
50 static const double dRcut = 1.0e-7*
CLHEP::mm;
51 static const double dTcut = 1.0*
CLHEP::ns;
55 const EventContext& ctx = Gaudi::Hive::currentContext();
62 unsigned int endTruthID = 0;
63 unsigned int endId = 0;
64 unsigned int endHit = 0;
65 HepGeom::Point3D<double> lastEnd(0.0, 0.0, 0.0);
71 const int truthId = currentLink->
id();
79 if ( lastTruthId != truthId || lastIndex !=
index || (
idx - endTruthID > 65500) ) {
80 lastTruthId = truthId;
82 const unsigned short persIndex =
static_cast<unsigned short>(
index);
84 log << MSG::WARNING <<
"Attempting to persistify an eventIndex larger than max unsigned short!" <<
endmsg;
88 persCont->
m_truthID.push_back(
static_cast<unsigned int>(lastTruthId));
97 if ( (
int)trtHit->GetParticleEncoding() != lastId ||
idx - endId > 65500) {
99 lastId = trtHit->GetParticleEncoding();
100 persCont->
m_id.push_back(lastId);
102 persCont->
m_nId.push_back(
idx - endId);
107 const HepGeom::Point3D<double> hitStart(trtHit->GetPreStepX(), trtHit->GetPreStepY(), trtHit->GetPreStepZ());
109 const double meanTime = trtHit->GetGlobalTime();
110 const double dTLast = std::abs(meanTime - lastT);
111 const double dRLast = lastEnd.distance(hitStart);
116 if ( dRLast >= dRcut || dTLast >= dTcut ) {
128 const unsigned int strawId = trtHit->GetHitID();
129 persCont->
m_strawId1b.push_back( (
unsigned char)(strawId % 256) );
130 persCont->
m_strawId2b.push_back( (
unsigned short)(strawId / 256) );
131 if ( strawId>16777215 )
132 log << MSG::WARNING <<
"TRT_HitCollectionCnv: strawId > 2^24-1 cannot be persistified correctly! " <<
endmsg;
141 const double startR = sqrt( hitStart.x()*hitStart.x() + hitStart.y()*hitStart.y() );
142 unsigned short istartRflag;
155 const double startPhi = atan2( hitStart.y(), hitStart.x() );
156 persCont->
m_startPhi.push_back( (
unsigned char)( (startPhi+
M_PI)/(2.0*
M_PI)*256.0 ) );
172 istartZ = (istartZ << 1) | istartRflag;
173 persCont->
m_startZ.push_back( istartZ );
195 const HepGeom::Point3D<double> hitEnd(trtHit->GetPostStepX(), trtHit->GetPostStepY(), trtHit->GetPostStepZ());
196 const HepGeom::Point3D<double> hitLength = (hitEnd - hitStart);
215 double kinEne = trtHit->GetKineticEnergy() * 1.0e9;
216 double steplength = hitLength.distance() * 1.0e9;
217 if ( kinEne < 1.0 ) kinEne=1.0;
218 if ( steplength < 1.0 ) steplength=1.0;
219 if ( kinEne > 9.0e18 ) kinEne=9.0e18;
220 if ( steplength > 9.0e18 ) steplength=9.0e18;
221 const unsigned int kexponent = (
unsigned int)ceil(log10(kinEne)/0.30102999566398);
222 const unsigned int sexponent = (
unsigned int)ceil(log10(steplength)/0.30102999566398);
223 const unsigned int kmantissa = (
unsigned int)(kinEne/
pow(2.0,kexponent)*1024) - 512;
224 const unsigned int smantissa = (
unsigned int)(steplength/
pow(2.0,sexponent)*1024) - 512;
225 persCont->
m_kinEne.push_back( (kmantissa << 6) | kexponent );
226 persCont->
m_steplength.push_back( (smantissa << 6) | sexponent );
236 const double endR = sqrt( hitEnd.x()*hitEnd.x() + hitEnd.y()*hitEnd.y() );
237 unsigned short iendRflag;
243 persCont->
m_endR.push_back( (
unsigned char)(endR/(2.0*
CLHEP::mm)*256.0) );
251 const double endPhi = atan2( hitEnd.y(), hitEnd.x() );
252 persCont->
m_endPhi.push_back( (
unsigned char)( (endPhi+
M_PI)/(2.0*
M_PI)*256.0 ) );
260 unsigned short idZsign = (hitLength.z()>0.0) ? 1 : 0;
261 unsigned short imeanTime = ( meanTime < 75.0*
CLHEP::ns ) ? (
unsigned short)(meanTime/(75.0*
CLHEP::ns)*1024.0) : 1023;
262 if ( imeanTime == 1023 ) persCont->
m_meanTimeof.push_back( (
float)meanTime );
263 imeanTime = (imeanTime << 2) | (idZsign << 1) | iendRflag;
271 (
int)(abs(lastId)/100000) == 41 ||
272 (
int)(abs(lastId)/10000000) == 1
273 ) persCont->
m_hitEne.push_back( (
float)(trtHit->GetEnergyDeposit()) );
294 persCont->
m_nId.push_back(
idx - endId);
302 std::unique_ptr<TRTUncompressedHitCollection> trans(std::make_unique<TRTUncompressedHitCollection>(
"DefaultCollectionName",persObj->
m_nHits.size()));
304 return(trans.release());
311 const EventContext& ctx = Gaudi::Hive::currentContext();
316 unsigned int meanTimeofCount=0, startRCount=0, endRCount=0, hitEneCount=0;
317 unsigned int idxTruthID=0, idxId=0, endHit=0, endTruthID=0, endId=0;
323 for (
unsigned int i = 0;
i < persCont->
m_nHits.size();
i++ ) {
327 const unsigned int startHit = endHit;
335 const unsigned int strawId = i2*256+i1;
340 const unsigned int istartPhi = persCont->
m_startPhi[
i];
341 const double startPhi = -
M_PI + (istartPhi+0.5)*2.0*
M_PI/256.0;
346 const unsigned int istartZ = persCont->
m_startZ[
i] >> 1;
352 const unsigned int istartRflag = persCont->
m_startZ[
i] & 1;
358 if ( istartRflag == 1 ) {
362 const unsigned int istartR = persCont->
m_startR[startRCount++];
363 startR = (istartR+0.5)*2.0*
CLHEP::mm/256.0;
370 double startX = startR*
cos(startPhi);
371 double startY = startR*
sin(startPhi);
387 for (
unsigned int j = startHit; j < endHit; j++ ) {
389 if ( j >= endTruthID + persCont->
m_nTruthID[idxTruthID] ) endTruthID += persCont->
m_nTruthID[idxTruthID++];
390 if ( j >= endId + persCont->
m_nId[idxId] ) endId += persCont->
m_nId[idxId++];
395 const unsigned int imeanTime = persCont->
m_meanTime[j] >> 2;
396 double meanTime = (imeanTime+0.5)*75.0*
CLHEP::ns/1024.0;
397 if ( imeanTime == 1023 ) meanTime = (
double)persCont->
m_meanTimeof[meanTimeofCount++];
402 const unsigned int idZsign = (persCont->
m_meanTime[j] >> 1 ) & 1;
407 const unsigned int iendRflag = persCont->
m_meanTime[j] & 1;
412 const double hitEne = ( persCont->
m_id[idxId] == 22 ||
413 (
int)(abs(persCont->
m_id[idxId])/100000) == 41 ||
414 (
int)(abs(persCont->
m_id[idxId])/10000000) == 1
420 const unsigned int iendPhi = persCont->
m_endPhi[j];
421 double endPhi = -
M_PI + (iendPhi+0.5)*2.0*
M_PI/256.0;
427 if ( iendRflag==1 ) {
431 const unsigned int iendR = persCont->
m_endR[endRCount++];
439 double endX = endR*
cos(endPhi);
440 double endY = endR*
sin(endPhi);
452 const int kmantissa = persCont->
m_kinEne[j] >> 6;
454 const int kexponent = persCont->
m_kinEne[j] & 0x3F;
456 const double kinEne = (kmantissa+512.5)/1024 *
pow(2.0,kexponent) / 1.0e9;
457 double g4steplength = (smantissa+512.5)/1024 *
pow(2.0,sexponent) / 1.0e9;
458 if ( idZsign==0 ) g4steplength = -g4steplength;
463 double dX = endX-startX;
464 double dY = endY-startY;
466 double dXY2 = dX*dX+dY*dY;
467 double dL2 = g4steplength*g4steplength;
470 if (g4steplength<0.0) dZ=-dZ;
473 dX = dX * sqrt(dL2/dXY2);
474 dY = dY * sqrt(dL2/dXY2);
481 double endZ = startZ + dZ;
509 transCont->
Emplace( strawId, partLink, persCont->
m_id[idxId],
510 kinEne, hitEne, startX, startY, startZ,
511 endX, endY, endZ, meanTime );
517 startX = endXo; startY = endYo; startZ = endZ;