39 LOG(info) <<
"Initializing digitizer";
40 mNumberOfChips = mGeometry->getNumberOfChips();
41 mChips.resize(mNumberOfChips);
42 for (
int i = mNumberOfChips;
i--;) {
43 mChips[
i].setChipIndex(
i);
45 mChips[
i].setNoiseMap(mNoiseMap);
48 mChips[
i].disable(mDeadChanMap->isFullChipMasked(
i));
49 mChips[
i].setDeadChanMap(mDeadChanMap);
54 mChipSimResp = mParams.getResponse();
55 mChipSimRespVD = mChipSimResp;
56 mChipSimRespMLOT = mChipSimResp;
60 LOG(info) <<
" Depth max VD: " << mChipSimRespVD->getDepthMax();
61 LOG(info) <<
" Depth min VD: " << mChipSimRespVD->getDepthMin();
63 LOG(info) <<
" Depth max MLOT: " << mChipSimRespMLOT->getDepthMax();
64 LOG(info) <<
" Depth min MLOT: " << mChipSimRespMLOT->getDepthMin();
66 float thicknessVD = 0.0095;
69 LOG(info) <<
"Using response name: " << mRespName;
70 mSimRespOrientation =
false;
72 if (mRespName ==
"APTS") {
75 mSimRespVDShift = mChipSimRespVD->getDepthMax();
78 mSimRespOrientation =
true;
79 }
else if (mRespName ==
"ALICE3") {
82 mSimRespVDShift = mChipSimRespVD->getDepthMax();
86 LOG(fatal) <<
"Unknown response name: " << mRespName;
89 mSimRespMLOTShift = mChipSimRespMLOT->getDepthMax() - thicknessMLOT / 2.f;
93 LOGP(info,
"VD shift = {} ; ML/OT shift = {} = {} - {}", mSimRespVDShift, mSimRespMLOTShift, mChipSimRespMLOT->getDepthMax(), thicknessMLOT / 2.f);
94 LOGP(info,
"VD pixel scale on x = {} ; z = {}", mSimRespVDScaleX, mSimRespVDScaleZ);
95 LOGP(info,
"ML/OT pixel scale on x = {} ; z = {}", mSimRespMLOTScaleX, mSimRespMLOTScaleZ);
96 LOGP(info,
"Response orientation: {}", mSimRespOrientation ?
"flipped" :
"normal");
104 if (mGeometry->getSubDetID(chipID) == 0) {
105 return mChipSimRespVD;
108 else if (mGeometry->getSubDetID(chipID) == 1 || mGeometry->getSubDetID(chipID) == 2) {
109 return mChipSimRespMLOT;
120 LOG(info) <<
" Digitizing " << mGeometry->getName() <<
" (ID: " << mGeometry->getDetID()
121 <<
") hits of event " << evID <<
" from source " << srcID
122 <<
" at time " << mEventTime.getTimeNS() <<
" ROFrame = " << mNewROFrame
123 <<
" Min/Max ROFrames " << mROFrameMin <<
"/" << mROFrameMax <<
" layer " <<
layer;
129 if (mNewROFrame > mROFrameMin) {
130 fillOutputContainer(mNewROFrame - 1,
layer);
133 int nHits = hits->size();
134 std::vector<int> hitIdx(nHits);
135 std::iota(std::begin(hitIdx), std::end(hitIdx), 0);
137 std::sort(hitIdx.begin(), hitIdx.end(),
138 [hits](
auto lhs,
auto rhs) {
139 return (*hits)[lhs].GetDetectorID() < (*hits)[rhs].GetDetectorID();
141 LOG(info) <<
"Processing " << nHits <<
" hits";
142 std::vector<float> respMatrix;
143 for (
int i : hitIdx |
std::views::
filter([&](int idx) {
147 return getROFLayer((*hits)[idx].GetDetectorID()) ==
layer;
149 processHit((*hits)[
i], mROFrameMax, evID, srcID,
layer, respMatrix);
157 LOG(
debug) <<
"Setting event time to " << irt.
getTimeNS() <<
" ns after orbit 0 bc 0";
162 auto nbc = mEventTime.differenceInBC(mIRFirstSampledTF);
164 if (mCollisionTimeWrtROF < 0 && nbc > 0) {
167 mCollisionTimeWrtROF += o2::constants::lhc::LHCBunchSpacingNS;
170 mROFsWrtFirstRO = std::floor(
float(nbc) / mParams.getROFrameLengthInBC(
layer));
174 mNewROFrame = nbc / mParams.getROFrameLengthInBC(
layer);
177 LOG(
debug) <<
" NewROFrame " << mNewROFrame <<
" nbc " << nbc <<
" ROFsWrtFirstRO " << mROFsWrtFirstRO;
180 mCollisionTimeWrtROF += (nbc % mParams.getROFrameLengthInBC(
layer)) * o2::constants::lhc::LHCBunchSpacingNS;
182 if (mNewROFrame < mROFrameMin) {
183 LOG(
error) <<
"New ROFrame " << mNewROFrame <<
" (" << irt <<
") precedes currently cashed " << mROFrameMin;
184 throw std::runtime_error(
"deduced ROFrame precedes already processed one");
187 if (mROFrameMax < mNewROFrame) {
188 mROFrameMax = mNewROFrame - 1;
197 if (frameLast > mROFrameMax) {
198 frameLast = mROFrameMax;
201 getExtraDigBuffer(mROFrameMax);
202 LOG(info) <<
"Filling " << mGeometry->getName() <<
" digits output for RO frames " << mROFrameMin <<
":"
208 for (; mROFrameMin <= frameLast; mROFrameMin++) {
212 auto& extra = *(mExtraBuff.front().get());
213 for (
auto& chip : mChips) {
214 if (chip.isDisabled() || (
layer >= 0 && getROFLayer(chip.getChipIndex()) !=
layer)) {
217 chip.addNoise(mROFrameMin, mROFrameMin, &mParams, mGeometry->getSubDetID(chip.getChipIndex()), mGeometry->getLayer(chip.getChipIndex()), mParams.getROFrameLength(getROFLayer(chip.getChipIndex())));
218 auto&
buffer = chip.getPreDigits();
222 auto itBeg =
buffer.begin();
224 ULong64_t maxKey = chip.getOrderingKey(mROFrameMin + 1, 0, 0) - 1;
225 for (; iter !=
buffer.end(); ++iter) {
226 if (iter->first > maxKey) {
229 auto& preDig = iter->second;
230 if (preDig.charge >= mParams.getChargeThreshold()) {
231 int digID = mDigits->size();
232 mDigits->emplace_back(chip.getChipIndex(), preDig.row, preDig.col, preDig.charge, preDig.toa);
233 LOG(
debug) <<
"Adding digit ID: " << digID <<
" with chipID: " << chip.getChipIndex() <<
", row: " << preDig.row <<
", col: " << preDig.col <<
", charge: " << preDig.charge <<
", ToA: " << preDig.toa;
234 mMCLabels->addElement(digID, preDig.labelRef.label);
235 auto& nextRef = preDig.labelRef;
236 while (nextRef.next >= 0) {
237 nextRef = extra[nextRef.next];
238 mMCLabels->addElement(digID, nextRef.label);
242 buffer.erase(itBeg, iter);
248 mROFRecords->push_back(rcROF);
252 mExtraBuff.emplace_back(mExtraBuff.front().release());
253 mExtraBuff.pop_front();
262 int subDetID = mGeometry->getSubDetID(chipID);
264 int layer = mGeometry->getLayer(chipID);
265 int disk = getDisk(chipID);
268 LOG(
debug) <<
"Skipping VD disk " << disk;
272 LOG(
debug) <<
"Processing hit for chip " << chipID;
273 auto& chip = mChips[chipID];
274 if (chip.isDisabled()) {
275 LOG(
debug) <<
"Skipping disabled chip " << chipID;
278 float timeInROF = hit.
GetTime() * sec2ns;
279 LOG(
debug) <<
"Hit time: " << timeInROF <<
" ns";
280 if (timeInROF > 20e3) {
281 const int maxWarn = 10;
282 static int warnNo = 0;
283 if (warnNo < maxWarn) {
284 LOG(warning) <<
"Ignoring hit with time_in_event = " << timeInROF <<
" ns"
285 << ((++warnNo < maxWarn) ?
"" :
" (suppressing further warnings)");
289 timeInROF += mCollisionTimeWrtROF;
290 if (mROFsWrtFirstRO < -1 || (mROFsWrtFirstRO == -1 && timeInROF < 0)) {
292 LOG(
debug) <<
"Ignoring hit with timeInROF = " << timeInROF;
300 float tTot = mParams.getSignalShape().getMaxDuration();
302 int roFrameRel =
int(timeInROF * mParams.getROFrameLengthInv(rofLayer));
304 float toaInROF = timeInROF - roFrameRel * mParams.getROFrameLength(rofLayer);
307 uint32_t roFrameRelMax = (timeInROF + tTot) * mParams.getROFrameLengthInv(rofLayer);
308 int nFrames = roFrameRelMax + 1 - roFrameRel;
309 uint32_t roFrameMax = mNewROFrame + roFrameRelMax;
310 if (roFrameMax > maxFr) {
315 float nStepsInv = mParams.getNSimStepsInv();
316 int nSteps = mParams.getNSimSteps();
318 const auto& matrix = mGeometry->getMatrixL2G(hit.
GetDetectorID());
322 math_utils::Vector3D<float> xyzLocS(matrix ^ (hit.
GetPosStart()));
323 math_utils::Vector3D<float> xyzLocE(matrix ^ (hit.
GetPos()));
329 LOG(
debug) <<
"Called curved to flat: " << xyzLocS.x() <<
" -> " << xyFlatS.x() <<
", " << xyzLocS.y() <<
" -> " << xyFlatS.y();
331 xyzLocS.SetXYZ(xyFlatS.x(), xyFlatS.y(), xyzLocS.Z());
332 xyzLocE.SetXYZ(xyFlatE.x(), xyFlatE.y(), xyzLocE.Z());
341 math_utils::Vector3D<float>
step(xyzLocE);
346 math_utils::Vector3D<float> stepH(step * 0.5);
350 LOG(
debug) <<
"Step into the sensitive volume: " <<
step <<
". Number of steps: " << nSteps;
351 int rowS = -1, colS = -1, rowE = -1, colE = -1, nSkip = 0;
356 if (++nSkip >= nSteps) {
357 LOG(
debug) <<
"Did not enter to sensitive matrix, " << nSkip <<
" >= " << nSteps;
365 if (++nSkip >= nSteps) {
366 LOG(
debug) <<
"Did not enter to sensitive matrix, " << nSkip <<
" >= " << nSteps;
377 std::swap(rowS, rowE);
380 std::swap(colS, colE);
398 int rowSpan = rowE - rowS + 1, colSpan = colE - colS + 1;
400 respMatrix.assign(
static_cast<size_t>(rowSpan) * colSpan, 0.f);
402 float nElectrons = hit.
GetEnergyLoss() * mParams.getEnergyToNElectrons();
403 nElectrons *= nStepsInv;
408 int rowPrev = -1, colPrev = -1,
row,
col;
409 float cRowPix = 0.f, cColPix = 0.f;
420 xyzLocS.SetY(xyzLocS.Y() + ((subDetID == 0) ? mSimRespVDShift : mSimRespMLOTShift));
424 for (
int iStep = nSteps; iStep--;) {
427 if (
row != rowPrev ||
col != colPrev) {
434 bool flipCol =
false, flipRow =
false;
436 float rowMax{}, colMax{};
441 rspmat = resp->
getResponse(mSimRespVDScaleX * (xyzLocS.X() - cRowPix), mSimRespVDScaleZ * (xyzLocS.Z() - cColPix), xyzLocS.Y(), flipRow, flipCol, rowMax, colMax);
445 rspmat = resp->
getResponse(mSimRespMLOTScaleX * (xyzLocS.X() - cRowPix), mSimRespMLOTScaleZ * (xyzLocS.Z() - cColPix), xyzLocS.Y(), flipRow, flipCol, rowMax, colMax);
450 if (rspmat ==
nullptr) {
451 LOG(
debug) <<
"Error in rspmat for step " << iStep <<
" / " << nSteps;
460 if (rowDest < 0 || rowDest >= rowSpan) {
465 if (colDest < 0 || colDest >= colSpan) {
468 respMatrix[rowDest * colSpan + colDest] += rspmat->getValue(irow, icol, mSimRespOrientation ? !flipRow : flipRow, flipCol);
472 LOG(
debug) <<
"Response done; adding labels; making digits";
475 auto roFrameAbs = mNewROFrame + roFrameRel;
476 LOG(
debug) <<
"\nSpanning through rows and columns; rowspan = " << rowSpan <<
" colspan = " << colSpan <<
" = " << colE <<
" - " << colS <<
" +1 ";
477 for (
int irow = rowSpan; irow--;) {
479 for (
int icol = colSpan; icol--;) {
480 float nEleResp = respMatrix[irow * colSpan + icol];
481 if (nEleResp <= 1.e-36) {
484 LOG(
debug) <<
"nEleResp: value " << nEleResp <<
" for pixel " << irow <<
" " << icol;
485 int nEle = gRandom->Poisson(nElectrons * nEleResp);
486 LOG(
debug) <<
"Charge detected in the pixel: " << nEle <<
" for pixel " << irow <<
" " << icol;
488 if (nEle < mParams.getMinChargeToAccount()) {
489 LOG(
debug) <<
"Ignoring pixel with nEle = " << nEle <<
" < min charge to account "
490 << mParams.getMinChargeToAccount() <<
" for pixel " << irow <<
" " << icol;
495 if (mNoiseMap && mNoiseMap->isNoisy(chipID, rowIS, colIS)) {
498 if (mDeadChanMap && mDeadChanMap->isNoisy(chipID, rowIS, colIS)) {
501 registerDigits(chip, roFrameAbs, toaInROF, nFrames, rowIS, colIS, nEle, lbl, rofLayer);
514 LOG(
debug) <<
"Registering digits for chip " << chip.
getChipIndex() <<
" at ROFrame " << roFrame
515 <<
" row " <<
row <<
" col " <<
col <<
" nEle " << nEle <<
" label " << lbl <<
" layer " <<
layer <<
" toa " << toaInROF;
516 float tStrobe = mParams.getStrobeDelay(
layer) - toaInROF;
517 for (
int i = 0;
i < nROF;
i++) {
519 float toaROF = toaInROF -
i * mParams.getROFrameLength(
layer);
520 int nEleROF = mParams.getSignalShape().getCollectedCharge(nEle, tStrobe, tStrobe + mParams.getStrobeLength(
layer));
521 tStrobe += mParams.getROFrameLength(
layer);
524 if (nEleROF < mParams.getMinChargeToAccount()) {
527 if (roFr > mEventROFrameMax) {
528 mEventROFrameMax = roFr;
530 if (roFr < mEventROFrameMin) {
531 mEventROFrameMin = roFr;
537 LOG(
debug) <<
"Added digit with key: " <<
key <<
" ROF: " << roFr <<
" row: " <<
row <<
" col: " <<
col <<
" charge: " << nEleROF;
539 LOG(
debug) <<
"Added to pre-digit with key: " <<
key <<
" ROF: " << roFr <<
" row: " <<
row <<
" col: " <<
col <<
" charge: " << nEleROF;
541 pd->
toa = std::min(pd->
toa, toaROF);
545 ExtraDig* extra = getExtraDigBuffer(roFr);
549 if ((*extra)[nxt].
label == lbl) {
553 nxt = (*extra)[nxt].next;
560 extra->emplace_back(lbl);