diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/TRK/base/include/TRKBase/AlmiraParam.h b/Detectors/Upgrades/ALICE3/TRKFT3/TRK/base/include/TRKBase/AlmiraParam.h index c052f02fc3128..8e7db9fe29b37 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/TRK/base/include/TRKBase/AlmiraParam.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/TRK/base/include/TRKBase/AlmiraParam.h @@ -34,8 +34,6 @@ struct AlmiraParam : public o2::conf::ConfigurableParamHelper { static constexpr int kDefROFLengthInBC = 16; ///< default ROF length in BC (16 clock cycles of 1 BC for VD) int roFrameLengthInBCPerLayer[kNLayers] = {0}; ///< ROF length in BC per layer - float strobeDelayPerLayer[kNLayers] = {0}; ///< strobe delay in ns per layer - float strobeLengthContPerLayer[kNLayers] = {0}; ///< strobe length in ns per layer int roFrameBiasInBCPerLayer[kNLayers] = {0}; ///< ROF start bias in BC per layer int roFrameDelayInBCPerLayer[kNLayers] = {0}; ///< extra ROF delay in BC per layer float clockPeriodPerLayer[kNLayers] = {0}; ///< ALMIRA clock period in ns per layer (timestamp fine granularity), 0 = default (1 BC VD, 4 BC ML/OT/disks) @@ -84,8 +82,6 @@ struct AlmiraParam : public o2::conf::ConfigurableParamHelper { return kDefROFLengthInBC; } } - float getStrobeDelay(int layer) const { return strobeDelayPerLayer[layer]; } - float getStrobeLengthCont(int layer) const { return strobeLengthContPerLayer[layer]; } int getROFBiasInBC(int layer) const { return roFrameBiasInBCPerLayer[layer]; } int getROFDelayInBC(int layer) const { return roFrameDelayInBCPerLayer[layer]; } diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/AlmiraSignal.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/AlmiraSignal.h new file mode 100644 index 0000000000000..1871421794942 --- /dev/null +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/AlmiraSignal.h @@ -0,0 +1,62 @@ +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. +// All rights not expressly granted are reserved. +// +// This software is distributed under the terms of the GNU General Public +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +/// \file AlmiraSignal.h +/// \brief Charge dependence of the ALMIRA pixel front-end signal: time walk and time over threshold + +#ifndef ALICEO2_TRKFT3_ALMIRASIGNAL_H +#define ALICEO2_TRKFT3_ALMIRASIGNAL_H + +#include +#include + +namespace o2 +{ +namespace trkft3 +{ + +/// @class AlmiraSignal +/// @brief Time walk and time over threshold of the ALMIRA pixel front-end as a function of the collected charge. +/// Both are parametrised as f(Q) = a * (1 - exp(-Q / b)) + c, with Q the collected charge in electrons. +/// The fit parameters are kept in seconds (as in the original fit), the getters return ns. +class AlmiraSignal +{ + public: + /// time walk: delay of the leading edge wrt the charge arrival, in ns + float getTimeWalk(float nEle) const { return eval(nEle, TWa, TWb, TWc); } + /// time over threshold, in ns + float getToT(float nEle) const { return eval(nEle, ToTa, ToTb, ToTc); } + + void print() const + { + printf(" f(Q) = a * (1 - exp(-Q / b)) + c, Q in electrons\n"); + printf(" time walk : a = %.3e s, b = %.3e e, c = %.3e s\n", TWa, TWb, TWc); + printf(" time over threshold: a = %.3e s, b = %.3e e, c = %.3e s\n", ToTa, ToTb, ToTc); + } + + private: + static float eval(float nEle, float a, float b, float c) { return (a * (1.f - std::exp(-nEle / b)) + c) * kSecToNS; } + + static constexpr float kSecToNS = 1.e9f; + + // parametrization parameters + static constexpr float TWa = -8.51e-07f; ///< s (± 2.56e-08) + static constexpr float TWb = 7.35e+01f; ///< electrons (± 1.67e+00) + static constexpr float TWc = 8.94e-07f; ///< s (± 2.61e-08) + static constexpr float ToTa = 1.17e-05f; ///< s (± 2.68e-08) + static constexpr float ToTb = 9.44e+02f; ///< electrons (± 6.06e+00) + static constexpr float ToTc = -3.15e-08f; ///< s (± 1.57e-08) +}; + +} // namespace trkft3 +} // namespace o2 + +#endif diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/ChipDigitsContainer.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/ChipDigitsContainer.h index 6281a2f070fd7..979d21d9058b5 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/ChipDigitsContainer.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/ChipDigitsContainer.h @@ -20,10 +20,25 @@ #include "TRKFT3Simulation/DigiParams.h" #include #include +#include +#include namespace o2::trkft3 { +/// State of the analog signal of a pixel, surviving the flush of the ROFs (edge detection) +struct PixelState { + double tStart = 0.; ///< signal start (first contribution), absolute time in ns + double tEnd = 0.; ///< tStart + ToT(charge), absolute time in ns + int charge = 0; ///< total charge of the signal (N electrons) + std::vector labels; ///< labels of the contributions up to the threshold crossing + uint32_t lastDigitROF = ~0u; ///< ROF of the last digit written (one digit per pixel per ROF) + bool pending = false; ///< a digit is waiting to be written in a later ROF + uint32_t pendingROF = 0; ///< ROF of the pending digit (ROF of its leading edge) + float pendingToA = 0.f; ///< leading edge of the pending digit w.r.t. pendingROF start (ns) + int pendingCharge = 0; ///< charge of the pending digit +}; + /// @class ChipDigitsContainer /// @brief Container for simulated points connected to a given chip @@ -45,8 +60,9 @@ class ChipDigitsContainer o2::trkft3::PreDigit* findDigit(ULong64_t key); void addDigit(ULong64_t key, UInt_t roframe, UShort_t row, UShort_t col, int charge, o2::MCCompLabel lbl, float toa = 0.f); + void addContribution(ULong64_t key, UInt_t roframe, UShort_t row, UShort_t col, int charge, o2::MCCompLabel lbl, float toa, std::vector& extra); template - void addNoise(UInt_t rofMin, UInt_t rofMax, const o2::trkft3::DigiParams* params, int subDetID, int layer, float rofLengthNS); + void addNoise(UInt_t rof, const o2::trkft3::DigiParams* params, int subDetID, int layer, float rofLengthNS, std::vector& extra); /// Get global ordering key made of readout frame, column and row static ULong64_t getOrderingKey(UInt_t roframe, UShort_t row, UShort_t col) @@ -57,12 +73,17 @@ class ChipDigitsContainer bool isDisabled() const { return mDisabled; } void disable(bool v) { mDisabled = v; } + std::unordered_map& getPixelStates() { return mPixelStates; } + /// Key of the pixel in the pixel state map + static uint32_t getPixelKey(UShort_t row, UShort_t col) { return (static_cast(col) << 16) | row; } + protected: UShort_t mChipIndex = 0; ///< chip index bool mDisabled = false; const o2::itsmft::NoiseMap* mNoiseMap = nullptr; const o2::itsmft::NoiseMap* mDeadChanMap = nullptr; - std::map mDigits; ///< Map of fired pixels, possibly in multiple frames + std::map mDigits; ///< Map of fired pixels, possibly in multiple frames + std::unordered_map mPixelStates; //! pixel signal states, surviving the flush ClassDefNV(ChipDigitsContainer, 2); }; @@ -82,10 +103,28 @@ inline void ChipDigitsContainer::addDigit(ULong64_t key, UInt_t roframe, UShort_ mDigits.emplace(key, o2::trkft3::PreDigit(roframe, row, col, charge, lbl, toa)); } +//_______________________________________________________________________ +inline void ChipDigitsContainer::addContribution(ULong64_t key, UInt_t roframe, UShort_t row, UShort_t col, int charge, o2::MCCompLabel lbl, float toa, std::vector& extra) +{ + // add a contribution to the pixel in the given ROF: the first one is stored in the PreDigit, + // the following ones at the end of its chain in the extra buffer of that ROF + auto* pd = findDigit(key); + if (!pd) { + addDigit(key, roframe, row, col, charge, lbl, toa); + return; + } + int* nxt = &pd->contrib.next; // "next" index of the last element of the chain + while (*nxt >= 0) { + nxt = &extra[*nxt].next; + } + *nxt = extra.size(); // index set before emplace_back, which may reallocate the vector + extra.emplace_back(lbl, toa, charge); +} + } // namespace o2::trkft3 template -void o2::trkft3::ChipDigitsContainer::addNoise(UInt_t rofMin, UInt_t rofMax, const o2::trkft3::DigiParams* params, int subDetID, int layer, float rofLengthNS) +void o2::trkft3::ChipDigitsContainer::addNoise(UInt_t rof, const o2::trkft3::DigiParams* params, int subDetID, int layer, float rofLengthNS, std::vector& extra) { UInt_t row = 0; UInt_t col = 0; @@ -107,24 +146,22 @@ void o2::trkft3::ChipDigitsContainer::addNoise(UInt_t rofMin, UInt_t rofMax, con LOG(debug) << "Adding noise for chip " << mChipIndex << " with mean " << mean << " and charge " << nel; - for (UInt_t rof = rofMin; rof <= rofMax; rof++) { - nhits = gRandom->Poisson(mean); - for (Int_t i = 0; i < nhits; ++i) { - row = gRandom->Integer(maxRows); - col = gRandom->Integer(maxCols); - LOG(debug) << "Generated noise hit at ROF " << rof << ", row " << row << ", col " << col; - if (mNoiseMap && mNoiseMap->isNoisy(mChipIndex, row, col)) { - continue; - } - if (mDeadChanMap && mDeadChanMap->isNoisy(mChipIndex, row, col)) { - continue; - } - auto key = getOrderingKey(rof, row, col); - if (!findDigit(key)) { - float toa = gRandom->Uniform(0.f, rofLengthNS); // noise fires uniformly in the ROF - addDigit(key, rof, row, col, nel, o2::MCCompLabel(true), toa); - } + nhits = gRandom->Poisson(mean); + for (Int_t i = 0; i < nhits; ++i) { + row = gRandom->Integer(maxRows); + col = gRandom->Integer(maxCols); + LOG(debug) << "Generated noise hit at ROF " << rof << ", row " << row << ", col " << col; + if (mNoiseMap && mNoiseMap->isNoisy(mChipIndex, row, col)) { + continue; + } + if (mDeadChanMap && mDeadChanMap->isNoisy(mChipIndex, row, col)) { + continue; } + auto key = getOrderingKey(rof, row, col); + // provisional noise model (to be revised with the ALMIRA noise model): noise hits are ordinary + // contributions with charge 1.1 x threshold, going through the same TW/ToT/pixel-state logic as particles + float toa = gRandom->Uniform(0.f, rofLengthNS); // noise arrives uniformly in the ROF + addContribution(key, rof, row, col, nel, o2::MCCompLabel(true), toa, extra); } } diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/DPLDigitizerParam.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/DPLDigitizerParam.h index 3304b1c13a016..e4d8709dca1db 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/DPLDigitizerParam.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/DPLDigitizerParam.h @@ -32,13 +32,9 @@ struct DPLDigitizerParam : public o2::conf::ConfigurableParamHelper #include "DetectorsCommonDataFormats/DetID.h" -#include "ITSMFTSimulation/AlpideSignalTrapezoid.h" #include "ITSMFTSimulation/AlpideSimResponse.h" #include "TRKBase/AlmiraParam.h" +#include "TRKFT3Simulation/AlmiraSignal.h" //////////////////////////////////////////////////////////// // // @@ -63,7 +63,6 @@ class DigiParams { static_assert(DetIDV == o2::detectors::DetID::TRK || DetIDV == o2::detectors::DetID::FT3, "only TRK and FT3 digit parameters are supported"); - using SignalShape = o2::itsmft::AlpideSignalTrapezoid; static constexpr size_t MaxLayers = detail::DigiParamsLayerTraits::MaxLayers; public: @@ -82,12 +81,6 @@ class DigiParams float getROFrameLength(int layer) const { return mROFrameLayerLength[layer]; } float getROFrameLengthInv(int layer) const { return mROFrameLayerLengthInv[layer]; } - void setStrobeDelay(float ns, int layer) { mStrobeLayerDelay[layer] = ns; } - float getStrobeDelay(int layer) const { return mStrobeLayerDelay[layer]; } - - void setStrobeLength(float ns, int layer) { mStrobeLayerLength[layer] = ns; } - float getStrobeLength(int layer) const { return mStrobeLayerLength[layer]; } - void setTimeOffset(double sec) { mTimeOffset = sec; } double getTimeOffset() const { return mTimeOffset; } @@ -117,8 +110,7 @@ class DigiParams const o2::trkft3::ChipSimResponse* getResponse() const { return mResponse.get(); } void setResponse(const o2::itsmft::AlpideSimResponse*); - const SignalShape& getSignalShape() const { return mSignalShape; } - SignalShape& getSignalShape() { return (SignalShape&)mSignalShape; } + const AlmiraSignal& getAlmiraSignal() const { return mAlmiraSignal; } virtual void print() const; @@ -153,10 +145,8 @@ class DigiParams std::array mROFrameLayerLengthInBC; ///< staggering ROF length in BC for continuous mode per layer std::array mROFrameLayerBiasInBC; ///< staggering ROF bias in BC for continuous mode per layer std::array mROFrameLayerLength; ///< staggering ROF length in ns for continuous mode per layer - std::array mStrobeLayerLength; ///< staggering strobe length in ns per layer - std::array mStrobeLayerDelay; ///< staggering strobe delay in ns per layer - o2::itsmft::AlpideSignalTrapezoid mSignalShape; ///< signal timeshape parameterization + AlmiraSignal mAlmiraSignal; ///< ALMIRA front-end signal: time walk and time over threshold std::unique_ptr mResponse; //!< pointer on external response diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/Digitizer.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/Digitizer.h index 65629cf7de6fc..c5f1fd5d25b85 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/Digitizer.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/Digitizer.h @@ -44,7 +44,7 @@ class Digitizer { static_assert(DetID == o2::detectors::DetID::TRK || DetID == o2::detectors::DetID::FT3, "only TRK and FT3 digitizers are supported"); using GeometryTGeo = std::conditional_t; - using ExtraDig = std::vector; ///< container for extra contributions to PreDigits + using ExtraDig = std::vector; ///< container for extra contributions to PreDigits public: void setDigits(std::vector* dig) { mDigits = dig; } @@ -72,6 +72,9 @@ class Digitizer mNewROFrame = 0; mROFsWrtFirstRO = 0; mExtraBuff.clear(); + for (auto& chip : mChips) { + chip.getPixelStates().clear(); // signal times are relative to the TF start: states do not survive a new TF + } } const o2::trkft3::DigiParams& getDigitParams() const { return mParams; } @@ -94,8 +97,10 @@ class Digitizer private: void processHit(const o2::trkft3::Hit& hit, uint32_t& maxFr, int evID, int srcID, int rofLayer, std::vector& respMatrix); - void registerDigits(o2::trkft3::ChipDigitsContainer& chip, uint32_t roFrame, float toaInROF, int nROF, + void registerDigits(o2::trkft3::ChipDigitsContainer& chip, uint32_t roFrame, float toaInROF, uint16_t row, uint16_t col, int nEle, o2::MCCompLabel& lbl, int layer); + void writeDigit(o2::trkft3::ChipDigitsContainer& chip, uint16_t row, uint16_t col, int charge, float toaInROF, + o2::trkft3::PixelState& state, int layer); ExtraDig* getExtraDigBuffer(uint32_t roFrame) { @@ -157,6 +162,10 @@ class Digitizer static constexpr float sec2ns = 1e9; + // edge-detection statistics of the layer, printed at the final flush (LOG(debug)) + long mNDigits = 0; + long mNPreCharged = 0, mNLEAfterEnd = 0, mNSameROF = 0, mNMovedROF = 0, mNDeadTime = 0; + o2::trkft3::DigiParams mParams; ///< digitization parameters o2::InteractionTimeRecord mEventTime; ///< global event time and interaction record o2::InteractionRecord mIRFirstSampledTF; ///< IR of the 1st sampled IR, noise-only ROFs will be inserted till this IR only diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/PreDigit.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/PreDigit.h index 893496d41f9fe..d3681dadc5451 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/PreDigit.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/include/TRKFT3Simulation/PreDigit.h @@ -19,32 +19,32 @@ namespace o2::trkft3 { -// PreDigit is a contribution to the digit from a single hit which happen -// to be registered 1st in given chip row/column/roframe -// Eventual extra contributions will be registered as PreDigitExtra, pointed by -// the "next" index +// PreDigit: pixel row/column/roframe with at least one contribution (hit) in that ROF. +// Each contribution (label, arrival time, charge) is a PreDigitContribution; the first one +// is stored in the PreDigit, the others in a chain pointed by the "next" index. +// Contributions are kept separately (not summed) because, with edge detection, the digit is +// built at the flush by processing them in time order together with the pixel state. -// PreDigitExtra registers additional contributions to the same row/column/roframe -struct PreDigitLabelRef { +struct PreDigitContribution { o2::MCCompLabel label; ///< hit label + float toa = 0.f; ///< arrival time w.r.t. ROF start (ns) + int charge = 0; ///< N electrons of this contribution int next = -1; ///< eventual next contribution to the same pixel - PreDigitLabelRef(o2::MCCompLabel lbl = 0, int nxt = -1) : label(lbl), next(nxt) {} + PreDigitContribution(o2::MCCompLabel lbl = 0, float t = 0.f, int q = 0, int nxt = -1) : label(lbl), toa(t), charge(q), next(nxt) {} - ClassDefNV(PreDigitLabelRef, 1); + ClassDefNV(PreDigitContribution, 1); }; struct PreDigit { - UShort_t row = 0; ///< Pixel index in X - UShort_t col = 0; ///< Pixel index in Z - UInt_t roFrame = 0; ///< Readout frame - int charge = 0; ///< N electrons - float toa = 0.f; ///< Time of arrival in ns (earliest contribution) - PreDigitLabelRef labelRef; ///< label and reference to the next one + UShort_t row = 0; ///< Pixel index in X + UShort_t col = 0; ///< Pixel index in Z + UInt_t roFrame = 0; ///< Readout frame + PreDigitContribution contrib; ///< first contribution and reference to the next one PreDigit(UInt_t rf = 0, UShort_t rw = 0, UShort_t cl = 0, int nele = 0, o2::MCCompLabel lbl = 0, float t = 0.f) - : row(rw), col(cl), roFrame(rf), charge(nele), toa(t), labelRef(lbl) {} + : row(rw), col(cl), roFrame(rf), contrib(lbl, t, nele) {} - ClassDefNV(PreDigit, 1); + ClassDefNV(PreDigit, 2); }; } // namespace o2::trkft3 diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/DigiParams.cxx b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/DigiParams.cxx index 82e111b0601f6..c95bdfeba437e 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/DigiParams.cxx +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/DigiParams.cxx @@ -46,15 +46,15 @@ void DigiParams::setNSimSteps(int v) template void DigiParams::setChargeThreshold(int v, float frac2Account) { - // set charge threshold for digits creation and its fraction to account - // contribution from single hit + // set charge threshold for digits creation and the minimum charge of a single + // contribution to be accounted (smaller ones are neglected even when summed) mChargeThreshold = v; mMinChargeToAccount = v * frac2Account; if (mMinChargeToAccount < 0 || mMinChargeToAccount > mChargeThreshold) { mMinChargeToAccount = mChargeThreshold; } LOG(info) << "Set charge threshold to " << mChargeThreshold - << ", single hit will be accounted from " << mMinChargeToAccount + << ", single contribution will be accounted from " << mMinChargeToAccount << " electrons"; } @@ -77,8 +77,8 @@ void DigiParams::print() const } } - printf("Charge time-response:\n"); - mSignalShape.print(); + printf("ALMIRA front-end signal:\n"); + mAlmiraSignal.print(); } template diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/Digitizer.cxx b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/Digitizer.cxx index 5095bc91aeb3c..2111c6ff814c9 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/Digitizer.cxx +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/Digitizer.cxx @@ -24,6 +24,8 @@ #include #include #include // for LOG +#include +#include using o2::trkft3::Digit; using o2::trkft3::Hit; @@ -193,6 +195,8 @@ void Digitizer::setEventTime(const o2::InteractionTimeRecord& irt, int la template void Digitizer::fillOutputContainer(uint32_t frameLast, int layer) { + const bool finalFlush = (frameLast == 0xffffffff); + // // fill output with digits from min.cached up to requested frame, generating the noise beforehand if (frameLast > mROFrameMax) { frameLast = mROFrameMax; @@ -204,6 +208,7 @@ void Digitizer::fillOutputContainer(uint32_t frameLast, int layer) o2::trkft3::ROFRecord rcROF; /// using temporarly trkft3::ROFRecord + std::vector contribs; // contributions to a pixel in a ROF, reused // we have to write chips in RO increasing order, therefore have to loop over the frames here for (; mROFrameMin <= frameLast; mROFrameMin++) { rcROF.setROFrame(mROFrameMin); @@ -214,28 +219,96 @@ void Digitizer::fillOutputContainer(uint32_t frameLast, int layer) if (chip.isDisabled() || (layer >= 0 && getROFLayer(chip.getChipIndex()) != layer)) { continue; } - chip.addNoise(mROFrameMin, mROFrameMin, &mParams, mGeometry->getSubDetID(chip.getChipIndex()), mGeometry->getLayer(chip.getChipIndex()), mParams.getROFrameLength(getROFLayer(chip.getChipIndex()))); + chip.addNoise(mROFrameMin, &mParams, mGeometry->getSubDetID(chip.getChipIndex()), mGeometry->getLayer(chip.getChipIndex()), mParams.getROFrameLength(getROFLayer(chip.getChipIndex())), extra); + const double rofLength = mParams.getROFrameLength(getROFLayer(chip.getChipIndex())); // ns + const double rofStart = mROFrameMin * rofLength; // absolute start time of the ROF (ns) + auto& states = chip.getPixelStates(); + // write the digits waiting for this ROF and drop the states of the signals already over + for (auto it = states.begin(); it != states.end();) { + auto& st = it->second; + if (st.pending && st.pendingROF == mROFrameMin) { + const uint16_t row = it->first & 0xffff, col = it->first >> 16; // inverse of getPixelKey + writeDigit(chip, row, col, st.pendingCharge, st.pendingToA, st, layer); + st.pending = false; + } + if (!st.pending && st.tEnd <= rofStart) { + it = states.erase(it); + } else { + ++it; + } + } auto& buffer = chip.getPreDigits(); if (buffer.empty()) { continue; } + const int thr = mParams.getChargeThreshold(); + const auto& signal = mParams.getAlmiraSignal(); + auto itBeg = buffer.begin(); auto iter = itBeg; - ULong64_t maxKey = chip.getOrderingKey(mROFrameMin + 1, 0, 0) - 1; // fetch digits with key below that + ULong64_t maxKey = chip.getOrderingKey(mROFrameMin + 1, 0, 0) - 1; // fetch pre-digits with key below that for (; iter != buffer.end(); ++iter) { if (iter->first > maxKey) { break; // is the digit ROFrame from the key > the max requested frame } auto& preDig = iter->second; // preDigit - if (preDig.charge >= mParams.getChargeThreshold()) { - int digID = mDigits->size(); - mDigits->emplace_back(chip.getChipIndex(), preDig.row, preDig.col, preDig.charge, mParams.encodeTimestamp(preDig.toa, mROFrameMin, layer)); - LOG(debug) << "Adding digit ID: " << digID << " with chipID: " << chip.getChipIndex() << ", row: " << preDig.row << ", col: " << preDig.col << ", charge: " << preDig.charge << ", ToA: " << preDig.toa; - mMCLabels->addElement(digID, preDig.labelRef.label); - auto& nextRef = preDig.labelRef; // extra contributors are in extra array - while (nextRef.next >= 0) { - nextRef = extra[nextRef.next]; - mMCLabels->addElement(digID, nextRef.label); + + // collect the contributions to the pixel in this ROF and sort them in time + contribs.clear(); + contribs.push_back(preDig.contrib); + for (int nxt = preDig.contrib.next; nxt >= 0; nxt = extra[nxt].next) { + contribs.push_back(extra[nxt]); + } + std::sort(contribs.begin(), contribs.end(), [](const auto& a, const auto& b) { return a.toa < b.toa; }); + + // process them in time order together with the state of the pixel signal + auto& state = states[chip.getPixelKey(preDig.row, preDig.col)]; // created if not existing + for (const auto& c : contribs) { + const double t = rofStart + c.toa; // absolute arrival time + if (t >= state.tEnd) { // previous signal over (or no signal): a new signal starts + state.tStart = t; + state.charge = 0; + state.labels.clear(); + } + const int qPrev = state.charge; + state.charge += c.charge; + state.tEnd = state.tStart + signal.getToT(state.charge); + if (qPrev >= thr) { + ++mNDeadTime; // contribution in the dead time: adds charge (extends t_end), no edge, no label + continue; // pile-up on a signal already above threshold: no new edge, no label + } + state.labels.push_back(c.label); + if (state.charge < thr) { + continue; // still below threshold + } + // upward threshold crossing: leading edge delayed by the time walk of the total charge + if (qPrev > 0) { + ++mNPreCharged; + } + const double tLE = t + signal.getTimeWalk(state.charge); + if (tLE >= state.tEnd) { // the signal ends before the leading edge: no digit (approximation) + ++mNLEAfterEnd; + state.labels.clear(); + continue; + } + const uint32_t rofLE = uint32_t(tLE / rofLength); + float toaLE = float(tLE - rofLE * rofLength); // leading edge w.r.t. the start of its ROF + if (toaLE >= float(rofLength)) { // float rounding at the end of the ROF: keep the LE in its ROF + toaLE = std::nextafter(float(rofLength), 0.f); + } + if (rofLE == state.lastDigitROF) { // one digit per pixel per ROF: keep the first + ++mNSameROF; + state.labels.clear(); + continue; + } + if (rofLE == mROFrameMin) { + writeDigit(chip, preDig.row, preDig.col, state.charge, toaLE, state, layer); + } else { // the leading edge falls in a later ROF: the digit waits in the pixel state + state.pending = true; + state.pendingROF = rofLE; + state.pendingToA = toaLE; + state.pendingCharge = state.charge; + ++mNMovedROF; } } } @@ -252,6 +325,29 @@ void Digitizer::fillOutputContainer(uint32_t frameLast, int layer) mExtraBuff.emplace_back(mExtraBuff.front().release()); mExtraBuff.pop_front(); } + if (finalFlush) { // only for logging + // pending digits never written: leading edge in a ROF which was not flushed. Expected only for the + // noise of the last flushed ROF, since noise is generated only in the flushed ROFs + long nPendingParticle = 0, nPendingNoise = 0; + for (auto& chip : mChips) { + if (layer >= 0 && getROFLayer(chip.getChipIndex()) != layer) { + continue; + } + for (const auto& [key, st] : chip.getPixelStates()) { + if (!st.pending) { + continue; + } + const bool onlyNoise = std::all_of(st.labels.begin(), st.labels.end(), [](const auto& l) { return l.isNoise() && !l.isQED(); }); + onlyNoise ? ++nPendingNoise : ++nPendingParticle; + } + } + LOGP(debug, "{} layer {}: digits {}, moved to a later ROF by the time walk {}, threshold crossings on a pre-charged pixel {}, contributions in the dead time {} | edges discarded: leading edge after signal end {}, second edge in the same ROF {} | pending noise digits not written {}", + mGeometry->getName(), layer, mNDigits, mNMovedROF, mNPreCharged, mNDeadTime, mNLEAfterEnd, mNSameROF, nPendingNoise); + if (nPendingParticle > 0) { + LOGP(warning, "{} layer {}: {} pending digits from particles never written (leading edge in a ROF which was not flushed)", mGeometry->getName(), layer, nPendingParticle); + } + mNDigits = mNPreCharged = mNLEAfterEnd = mNSameROF = mNMovedROF = mNDeadTime = 0; + } } //_______________________________________________________________________ @@ -297,18 +393,17 @@ void Digitizer::processHit(const o2::trkft3::Hit& hit, uint32_t& maxFr, i if (timeInROF < 0) { timeInROF = 0.; } - float tTot = mParams.getSignalShape().getMaxDuration(); - // frame of the hit signal start wrt event ROFrame + // frame of the leading edge of the signal wrt event ROFrame (edge detection: the hit is read only in this ROF) int roFrameRel = int(timeInROF * mParams.getROFrameLengthInv(rofLayer)); // Time of Arrival w.r.t. the start of the ROF where the signal starts (= w.r.t. roFrameAbs) float toaInROF = timeInROF - roFrameRel * mParams.getROFrameLength(rofLayer); - - // frame of the hit signal end wrt event ROFrame: in the triggered mode we read just 1 frame - uint32_t roFrameRelMax = (timeInROF + tTot) * mParams.getROFrameLengthInv(rofLayer); - int nFrames = roFrameRelMax + 1 - roFrameRel; - uint32_t roFrameMax = mNewROFrame + roFrameRelMax; - if (roFrameMax > maxFr) { - maxFr = roFrameMax; // if signal extends beyond current maxFrame, increase the latter + uint32_t roFrameAbs = mNewROFrame + roFrameRel; + // the leading edge can be delayed by the time walk, at most TW(threshold) since the charge at the + // threshold crossing is >= threshold: make sure the ROFs it can reach are flushed + const float twMax = mParams.getAlmiraSignal().getTimeWalk(mParams.getChargeThreshold()); + uint32_t roFrameMaxLE = mNewROFrame + uint32_t((timeInROF + twMax) * mParams.getROFrameLengthInv(rofLayer)); + if (roFrameMaxLE > maxFr) { + maxFr = roFrameMaxLE; } // here we start stepping in the depth of the sensor to generate charge diffusion @@ -472,11 +567,10 @@ void Digitizer::processHit(const o2::trkft3::Hit& hit, uint32_t& maxFr, i LOG(debug) << "Response done; adding labels; making digits"; // fire the pixels assuming Poisson(n_response_electrons) o2::MCCompLabel lbl(hit.GetTrackID(), evID, srcID, false); - auto roFrameAbs = mNewROFrame + roFrameRel; LOG(debug) << "\nSpanning through rows and columns; rowspan = " << rowSpan << " colspan = " << colSpan << " = " << colE << " - " << colS << " +1 "; - for (int irow = rowSpan; irow--;) { // irow ranging from 4 to 0 - uint16_t rowIS = irow + rowS; // row distant irow from the row of the hit start - for (int icol = colSpan; icol--;) { // icol ranging from 4 to 0 + for (int irow = rowSpan; irow--;) { // irow ranging from 4 to 0 + uint16_t rowIS = irow + rowS; // row distant irow from the row of the hit start + for (int icol = colSpan; icol--;) { // icol ranging from 4 to 0 float nEleResp = respMatrix[irow * colSpan + icol]; // value of the probability of the response in this pixel if (nEleResp <= 1.e-36) { continue; @@ -484,8 +578,8 @@ void Digitizer::processHit(const o2::trkft3::Hit& hit, uint32_t& maxFr, i LOG(debug) << "nEleResp: value " << nEleResp << " for pixel " << irow << " " << icol; int nEle = gRandom->Poisson(nElectrons * nEleResp); // total charge in given pixel = number of electrons generated in the hit multiplied by the probability of being detected in their position LOG(debug) << "Charge detected in the pixel: " << nEle << " for pixel " << irow << " " << icol; - // ignore charge which have no chance to fire the pixel - if (nEle < mParams.getMinChargeToAccount()) { /// TODO: substitute with the threshold? + // ignore contributions too small to matter, even when summed with others on the same pixel + if (nEle < mParams.getMinChargeToAccount()) { LOG(debug) << "Ignoring pixel with nEle = " << nEle << " < min charge to account " << mParams.getMinChargeToAccount() << " for pixel " << irow << " " << icol; continue; @@ -498,68 +592,51 @@ void Digitizer::processHit(const o2::trkft3::Hit& hit, uint32_t& maxFr, i if (mDeadChanMap && mDeadChanMap->isNoisy(chipID, rowIS, colIS)) { continue; } - registerDigits(chip, roFrameAbs, toaInROF, nFrames, rowIS, colIS, nEle, lbl, rofLayer); + registerDigits(chip, roFrameAbs, toaInROF, rowIS, colIS, nEle, lbl, rofLayer); } } } //________________________________________________________________________________ template -void Digitizer::registerDigits(o2::trkft3::ChipDigitsContainer& chip, uint32_t roFrame, float toaInROF, int nROF, +void Digitizer::registerDigits(o2::trkft3::ChipDigitsContainer& chip, uint32_t roFrame, float toaInROF, uint16_t row, uint16_t col, int nEle, o2::MCCompLabel& lbl, int layer) { - // Register digits for given pixel, accounting for the possible signal contribution to - // multiple ROFrame. The signal starts at time toaInROF wrt the start of provided roFrame - // In every ROFrame we check the collected signal during strobe + // Register the contribution (label, arrival time, charge) of a hit to the given pixel in the ROF + // of arrival. No summing here: the digit is built at the flush (fillOutputContainer), processing + // the contributions in time order together with the pixel state (edge detection) LOG(debug) << "Registering digits for chip " << chip.getChipIndex() << " at ROFrame " << roFrame << " row " << row << " col " << col << " nEle " << nEle << " label " << lbl << " layer " << layer << " toa " << toaInROF; - float tStrobe = mParams.getStrobeDelay(layer) - toaInROF; // strobe start wrt signal start - for (int i = 0; i < nROF; i++) { // loop on all the ROFs occupied by the same signal to calculate the charge accumulated in that ROF - uint32_t roFr = roFrame + i; - float toaROF = toaInROF - i * mParams.getROFrameLength(layer); // ToA w.r.t. start of ROF - int nEleROF = mParams.getSignalShape().getCollectedCharge(nEle, tStrobe, tStrobe + mParams.getStrobeLength(layer)); - tStrobe += mParams.getROFrameLength(layer); // for the next ROF - - // discard too small contributions, they have no chance to produce a digit - if (nEleROF < mParams.getMinChargeToAccount()) { /// use threshold instead? - continue; - } - if (roFr > mEventROFrameMax) { - mEventROFrameMax = roFr; - } - if (roFr < mEventROFrameMin) { - mEventROFrameMin = roFr; - } - auto key = chip.getOrderingKey(roFr, row, col); - o2::trkft3::PreDigit* pd = chip.findDigit(key); - if (!pd) { - chip.addDigit(key, roFr, row, col, nEleROF, lbl, toaROF); - LOG(debug) << "Added digit with key: " << key << " ROF: " << roFr << " row: " << row << " col: " << col << " charge: " << nEleROF; - } else { // there is already a digit at this slot, account as PreDigitExtra contribution - LOG(debug) << "Added to pre-digit with key: " << key << " ROF: " << roFr << " row: " << row << " col: " << col << " charge: " << nEleROF; - pd->charge += nEleROF; - pd->toa = std::min(pd->toa, toaROF); /// in case of two hits on the same digit in the same ROF, get the first ToA - if (pd->labelRef.label == lbl) { // don't store the same label twice - continue; - } - ExtraDig* extra = getExtraDigBuffer(roFr); - int& nxt = pd->labelRef.next; - bool skip = false; - while (nxt >= 0) { - if ((*extra)[nxt].label == lbl) { // don't store the same label twice - skip = true; - break; - } - nxt = (*extra)[nxt].next; - } - if (skip) { - continue; - } - // new predigit will be added in the end of the chain - nxt = extra->size(); - extra->emplace_back(lbl); + if (roFrame > mEventROFrameMax) { + mEventROFrameMax = roFrame; + } + if (roFrame < mEventROFrameMin) { + mEventROFrameMin = roFrame; + } + auto key = chip.getOrderingKey(roFrame, row, col); + chip.addContribution(key, roFrame, row, col, nEle, lbl, toaInROF, *getExtraDigBuffer(roFrame)); + LOG(debug) << "Added contribution with key: " << key << " ROF: " << roFrame << " row: " << row << " col: " << col << " charge: " << nEle; +} + +//________________________________________________________________________________ +template +void Digitizer::writeDigit(o2::trkft3::ChipDigitsContainer& chip, uint16_t row, uint16_t col, int charge, float toaInROF, + o2::trkft3::PixelState& state, int layer) +{ + // Write the digit of the pixel in the ROF being flushed (mROFrameMin), with the labels of the + // contributions up to the threshold crossing, each label written once + int digID = mDigits->size(); + mDigits->emplace_back(chip.getChipIndex(), row, col, charge, mParams.encodeTimestamp(toaInROF, mROFrameMin, layer)); + ++mNDigits; + LOG(debug) << "Adding digit ID: " << digID << " with chipID: " << chip.getChipIndex() << ", row: " << row << ", col: " << col << ", charge: " << charge << ", ToA: " << toaInROF; + const auto& lbls = state.labels; + for (size_t i = 0; i < lbls.size(); ++i) { + if (std::find(lbls.begin(), lbls.begin() + i, lbls[i]) == lbls.begin() + i) { // not already written + mMCLabels->addElement(digID, lbls[i]); } } + state.labels.clear(); + state.lastDigitROF = mROFrameMin; } template class o2::trkft3::Digitizer; diff --git a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/TRKFT3SimulationLinkDef.h b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/TRKFT3SimulationLinkDef.h index bb877ae83028c..f1f993e552144 100644 --- a/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/TRKFT3SimulationLinkDef.h +++ b/Detectors/Upgrades/ALICE3/TRKFT3/common/simulation/src/TRKFT3SimulationLinkDef.h @@ -24,6 +24,6 @@ #pragma link C++ class o2::trkft3::DPLDigitizerParam < o2::detectors::DetID::FT3> + ; #pragma link C++ class o2::conf::ConfigurableParamHelper < o2::trkft3::DPLDigitizerParam < o2::detectors::DetID::FT3>> + ; #pragma link C++ class o2::trkft3::PreDigit + ; -#pragma link C++ class o2::trkft3::PreDigitLabelRef + ; +#pragma link C++ class o2::trkft3::PreDigitContribution + ; #endif diff --git a/Steer/DigitizerWorkflow/src/TRKDigitizerSpec.cxx b/Steer/DigitizerWorkflow/src/TRKDigitizerSpec.cxx index 8e9e6638691bb..02a8297dba0f2 100644 --- a/Steer/DigitizerWorkflow/src/TRKDigitizerSpec.cxx +++ b/Steer/DigitizerWorkflow/src/TRKDigitizerSpec.cxx @@ -301,9 +301,6 @@ class TRKFT3DPLDigitizerTask : BaseDPLDigitizer digipar.setROFrameLengthInBC(roFrameLengthInBC, iLayer); // ROF delay is treated as an additional bias from the digitizer point of view. digipar.setROFrameBiasInBC(aopt.getROFBiasInBC(parLayer) + aopt.getROFDelayInBC(parLayer), iLayer); - digipar.setStrobeDelay(aopt.getStrobeDelay(parLayer), iLayer); - const auto strobeLengthCont = aopt.getStrobeLengthCont(parLayer); - digipar.setStrobeLength(strobeLengthCont > 0 ? strobeLengthCont : frameNS - aopt.getStrobeDelay(parLayer), iLayer); digipar.setROFrameLength(frameNS, iLayer); digipar.setClockPeriod(aopt.getClockPeriod(parLayer), iLayer); // after setROFrameLengthInBC: computes and sets the clock cycles per ROF @@ -325,8 +322,6 @@ class TRKFT3DPLDigitizerTask : BaseDPLDigitizer LOGP(fatal, "{} layer {}: {} clock cycles per ROF do not fit in the {} fine bits of the timestamp. Possible values: 16 BC for VD; 16,32,64 BC for MLOT/FT3.", ID.getName(), iLayer, nCycles, aopt.nFineBits); } } - // parameters of signal time response: flat-top duration, max rise time and q @ which rise time is 0 - digipar.getSignalShape().setParameters(dopt.strobeFlatTop, dopt.strobeMaxRiseTime, dopt.strobeQRiseTime0); digipar.setChargeThreshold(dopt.chargeThreshold); // charge threshold in electrons digipar.setNoisePerPixel(dopt.noisePerPixel); // noise rate in hits/pixel/s digipar.setTimeOffset(dopt.timeOffset);