13 #include "CLHEP/Geometry/Point3D.h"
14 #include "CLHEP/Units/SystemOfUnits.h"
17 #include "GaudiKernel/MsgStream.h"
18 #include "GaudiKernel/ThreadLocalContext.h"
51 static const double dRcut = 1.0e-7*
CLHEP::mm;
52 static const double dTcut = 1.0*
CLHEP::ns;
56 const EventContext& ctx = Gaudi::Hive::currentContext();
63 unsigned int endBC = 0;
64 unsigned int endId = 0;
65 unsigned int endHit = 0;
66 HepGeom::Point3D<double> lastEnd(0.0, 0.0, 0.0);
79 if ( lastBarcode !=
barcode || lastIndex !=
index || (
idx - endBC > 65500) ) {
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_barcode.push_back(
static_cast<unsigned int>(lastBarcode));
93 persCont->
m_nBC.push_back(
idx - endBC);
98 if ( (
int)trtHit->GetParticleEncoding() != lastId ||
idx - endId > 65500) {
100 lastId = trtHit->GetParticleEncoding();
101 persCont->
m_id.push_back(lastId);
103 persCont->
m_nId.push_back(
idx - endId);
108 const HepGeom::Point3D<double> hitStart(trtHit->GetPreStepX(), trtHit->GetPreStepY(), trtHit->GetPreStepZ());
110 const double meanTime = trtHit->GetGlobalTime();
111 const double dTLast = std::abs(meanTime - lastT);
112 const double dRLast = lastEnd.distance(hitStart);
117 if ( dRLast >= dRcut || dTLast >= dTcut ) {
129 const unsigned int strawId = trtHit->GetHitID();
130 persCont->
m_strawId1b.push_back( (
unsigned char)(strawId % 256) );
131 persCont->
m_strawId2b.push_back( (
unsigned short)(strawId / 256) );
132 if ( strawId>16777215 )
133 log << MSG::WARNING <<
"TRT_HitCollectionCnv: strawId > 2^24-1 cannot be persistified correctly! " <<
endmsg;
142 const double startR = sqrt( hitStart.x()*hitStart.x() + hitStart.y()*hitStart.y() );
143 unsigned short istartRflag;
156 const double startPhi = atan2( hitStart.y(), hitStart.x() );
157 persCont->
m_startPhi.push_back( (
unsigned char)( (startPhi+
M_PI)/(2.0*
M_PI)*256.0 ) );
173 istartZ = (istartZ << 1) | istartRflag;
174 persCont->
m_startZ.push_back( istartZ );
196 const HepGeom::Point3D<double> hitEnd(trtHit->GetPostStepX(), trtHit->GetPostStepY(), trtHit->GetPostStepZ());
197 const HepGeom::Point3D<double> hitLength = (hitEnd - hitStart);
216 double kinEne = trtHit->GetKineticEnergy() * 1.0e9;
217 double steplength = hitLength.distance() * 1.0e9;
218 if ( kinEne < 1.0 ) kinEne=1.0;
219 if ( steplength < 1.0 ) steplength=1.0;
220 if ( kinEne > 9.0e18 ) kinEne=9.0e18;
221 if ( steplength > 9.0e18 ) steplength=9.0e18;
222 const unsigned int kexponent = (
unsigned int)ceil(log10(kinEne)/0.30102999566398);
223 const unsigned int sexponent = (
unsigned int)ceil(log10(steplength)/0.30102999566398);
224 const unsigned int kmantissa = (
unsigned int)(kinEne/
pow(2.0,kexponent)*1024) - 512;
225 const unsigned int smantissa = (
unsigned int)(steplength/
pow(2.0,sexponent)*1024) - 512;
226 persCont->
m_kinEne.push_back( (kmantissa << 6) | kexponent );
227 persCont->
m_steplength.push_back( (smantissa << 6) | sexponent );
237 const double endR = sqrt( hitEnd.x()*hitEnd.x() + hitEnd.y()*hitEnd.y() );
238 unsigned short iendRflag;
244 persCont->
m_endR.push_back( (
unsigned char)(endR/(2.0*
CLHEP::mm)*256.0) );
252 const double endPhi = atan2( hitEnd.y(), hitEnd.x() );
253 persCont->
m_endPhi.push_back( (
unsigned char)( (endPhi+
M_PI)/(2.0*
M_PI)*256.0 ) );
261 unsigned short idZsign = (hitLength.z()>0.0) ? 1 : 0;
262 unsigned short imeanTime = ( meanTime < 75.0*
CLHEP::ns ) ? (
unsigned short)(meanTime/(75.0*
CLHEP::ns)*1024.0) : 1023;
263 if ( imeanTime == 1023 ) persCont->
m_meanTimeof.push_back( (
float)meanTime );
264 imeanTime = (imeanTime << 2) | (idZsign << 1) | iendRflag;
272 (
int)(abs(lastId)/100000) == 41 ||
273 (
int)(abs(lastId)/10000000) == 1
274 ) persCont->
m_hitEne.push_back( (
float)(trtHit->GetEnergyDeposit()) );
294 persCont->
m_nBC.push_back(
idx - endBC);
295 persCont->
m_nId.push_back(
idx - endId);
303 std::unique_ptr<TRTUncompressedHitCollection> trans(std::make_unique<TRTUncompressedHitCollection>(
"DefaultCollectionName",persObj->
m_nHits.size()));
305 return(trans.release());
312 const EventContext& ctx = Gaudi::Hive::currentContext();
317 unsigned int meanTimeofCount=0, startRCount=0, endRCount=0, hitEneCount=0;
318 unsigned int idxBC=0, idxId=0, endHit=0, endBC=0, endId=0;
324 for (
unsigned int i = 0;
i < persCont->
m_nHits.size();
i++ ) {
328 const unsigned int startHit = endHit;
336 const unsigned int strawId = i2*256+i1;
341 const unsigned int istartPhi = persCont->
m_startPhi[
i];
342 const double startPhi = -
M_PI + (istartPhi+0.5)*2.0*
M_PI/256.0;
347 const unsigned int istartZ = persCont->
m_startZ[
i] >> 1;
353 const unsigned int istartRflag = persCont->
m_startZ[
i] & 1;
359 if ( istartRflag == 1 ) {
363 const unsigned int istartR = persCont->
m_startR[startRCount++];
364 startR = (istartR+0.5)*2.0*
CLHEP::mm/256.0;
371 double startX = startR*
cos(startPhi);
372 double startY = startR*
sin(startPhi);
388 for (
unsigned int j = startHit; j < endHit; j++ ) {
390 if ( j >= endBC + persCont->
m_nBC[idxBC] ) endBC += persCont->
m_nBC[idxBC++];
391 if ( j >= endId + persCont->
m_nId[idxId] ) endId += persCont->
m_nId[idxId++];
396 const unsigned int imeanTime = persCont->
m_meanTime[j] >> 2;
397 double meanTime = (imeanTime+0.5)*75.0*
CLHEP::ns/1024.0;
398 if ( imeanTime == 1023 ) meanTime = (
double)persCont->
m_meanTimeof[meanTimeofCount++];
403 const unsigned int idZsign = (persCont->
m_meanTime[j] >> 1 ) & 1;
408 const unsigned int iendRflag = persCont->
m_meanTime[j] & 1;
413 const double hitEne = ( persCont->
m_id[idxId] == 22 ||
414 (
int)(abs(persCont->
m_id[idxId])/100000) == 41 ||
415 (
int)(abs(persCont->
m_id[idxId])/10000000) == 1
421 const unsigned int iendPhi = persCont->
m_endPhi[j];
422 double endPhi = -
M_PI + (iendPhi+0.5)*2.0*
M_PI/256.0;
428 if ( iendRflag==1 ) {
432 const unsigned int iendR = persCont->
m_endR[endRCount++];
440 double endX = endR*
cos(endPhi);
441 double endY = endR*
sin(endPhi);
453 const int kmantissa = persCont->
m_kinEne[j] >> 6;
455 const int kexponent = persCont->
m_kinEne[j] & 0x3F;
457 const double kinEne = (kmantissa+512.5)/1024 *
pow(2.0,kexponent) / 1.0e9;
458 double g4steplength = (smantissa+512.5)/1024 *
pow(2.0,sexponent) / 1.0e9;
459 if ( idZsign==0 ) g4steplength = -g4steplength;
464 double dX = endX-startX;
465 double dY = endY-startY;
467 double dXY2 = dX*dX+dY*dY;
468 double dL2 = g4steplength*g4steplength;
471 if (g4steplength<0.0) dZ=-dZ;
474 dX = dX * sqrt(dL2/dXY2);
475 dY = dY * sqrt(dL2/dXY2);
482 double endZ = startZ + dZ;
512 transCont->
Emplace( strawId, partLink, persCont->
m_id[idxId],
513 kinEne, hitEne, startX, startY, startZ,
514 endX, endY, endZ, meanTime );
520 startX = endXo; startY = endYo; startZ = endZ;