Project
Loading...
Searching...
No Matches
TrapSimulator.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#ifndef O2_TRD_TRAPSIMULATOR_H
13#define O2_TRD_TRAPSIMULATOR_H
14
16// //
17// TRAP Chip Simulation Class //
18// //
20
21#include <iosfwd>
22#include <iostream>
23#include <ostream>
24#include <fstream>
25#include <gsl/span>
26
27#include "TRDBase/FeeParam.h"
29
34
35namespace o2
36{
37namespace trd
38{
39
41{
42 public:
43 enum { PRINTRAW = 1,
47 enum { PLOTRAW = 1,
50
51 // Register for the ADC filters
52 struct FilterReg {
53 uint32_t mPedAcc; // Accumulator for pedestal filter
54 uint32_t mGainCounterA; // Counter for values above FGTA in the gain filter
55 uint32_t mGainCounterB; // Counter for values above FGTB in the gain filter
56 uint16_t mTailAmplLong; // Amplitude of the long component in the tail filter
57 uint16_t mTailAmplShort; // Amplitude of the short component in the tail filter
58 void ClearReg()
59 {
60 mPedAcc = 0;
61 mGainCounterA = 0;
62 mGainCounterB = 0;
63 mTailAmplLong = 0;
65 };
66 };
67
68 // Register for the tracklet fits
69 struct FitReg {
70 uint16_t nHits; // number of hits
71 uint16_t q0; // charge accumulated in first window
72 uint16_t q1; // charge accumulated in second window
73 uint16_t sumX; // sum x
74 int16_t sumY; // sum y (signed)
75 uint16_t sumX2; // sum x**2
76 uint16_t sumY2; // sum y**2
77 int32_t sumXY; // sum x*y (signed)
78 void ClearReg()
79 {
80 nHits = 0;
81 q0 = 0;
82 q1 = 0;
83 sumX = 0;
84 sumY = 0;
85 sumX2 = 0;
86 sumY2 = 0;
87 sumXY = 0;
88 }
89 void dumpHex(int channel) const
90 {
91 LOGF(info, "Channel = %i", channel);
92 LOGF(info, "Nhits = %i", nHits);
93 LOGF(info, "Q1 & Q0 = 0x%08x Q1 = %i, Q0 = %i", (q1 << 16) | q0, q1, q0);
94 LOGF(info, "SumX = 0x%08x = %i", sumX, sumX);
95 LOGF(info, "SumX2 = 0x%08x = %i", sumX2, sumX2);
96 LOGF(info, "SumY = 0x%08x = %i", sumY, sumY);
97 LOGF(info, "SumXY = 0x%08x = %i", sumXY, sumXY);
98 LOGF(info, "SumY2 = 0x%08x = %i", sumY2, sumY2);
99 }
100 void Print() const { LOGF(info, "FitReg: nHits(%u), q0(%u), q1(%u), sumX(%u), sumX2(%u), sumY(%i), sumY2(%u), sumXY(%i)", nHits, q0, q1, sumX, sumX2, sumY, sumY2, sumXY); }
101 };
102
103 // we don't allow copies of TrapSimulator
104 TrapSimulator() = default;
105 TrapSimulator(const TrapSimulator&) = delete;
107 ~TrapSimulator() = default;
108
109 // Initialize MCM by the position parameters
110 void init(TrapConfig* trapconfig, int det, int rob, int mcm);
111
112 bool checkInitialized() const { return mInitialized; }
113
114 // clears filter registers and internal data
115 void reset();
116
117 void noiseTest(int nsamples, int mean, int sigma, int inputGain = 1, int inputTail = 2);
118
119 // get unfiltered ADC data
120 int getDataRaw(int iadc, int timebin) const { return mADCR[iadc * mNTimeBin + timebin]; }
121 // get filtered ADC data
122 int getDataFiltered(int iadc, int timebin) const { return mADCF[iadc * mNTimeBin + timebin]; }
123 int getZeroSupressionMap(int iadc) const { return (mZSMap[iadc]); }
124 bool isDataSet() { return mDataIsSet; };
125 // set ADC data with array
126 void setData(int iadc, const ArrayADC& adc, unsigned int digitIdx);
127 // set the baselines to all channels
128 void setBaselines();
129 // set the pedestal value to all channels
130 void setDataPedestal(int iadc);
131 // set an additional ADC baseline value
132 void setAdditionalBaseline(int adc) { mAdditionalBaseline = adc; }
133 int getAdditionalBaseline() const { return mAdditionalBaseline; }
134
135 void setUseFloatingPointForQ() { mUseFloatingPointForQ = true; }
136 void setChargeScalingFactor(int scale) { mScaleQ = (1UL << 32UL) / scale; }
137
138 int getDetector() const { return mDetector; }; // Returns Chamber ID (0-539)
139 int getRobPos() const { return mRobPos; }; // Returns ROB position (0-7)
140 int getMcmPos() const { return mMcmPos; }; // Returns MCM position (0-17) (16,17 are mergers)
141 int getNumberOfTimeBins() const { return mNTimeBin; }; // Set via TrapConfig, but should be the same as o2::trd::constants::TIMEBINS
142
143 // transform Tracklet64 data into raw data format (up to 4 32-bit words)
144 // FIXME offset is not used, should it be removed?
145 int packData(std::vector<uint32_t>& rawdata, uint32_t offset) const;
146
147 // different stages of processing in the TRAP
148 void filter(); // Apply digital filters for existing data (according to configuration)
149 void zeroSupressionMapping(); // Do ZS mapping for existing data
150 void tracklet(); // Run tracklet preprocessor and perform tracklet fit
151
152 // apply individual filters to all channels and timebins
153 void filterPedestal(); // Apply pedestal filter
154 void filterGain(); // Apply gain filter
155 void filterTail(); // Apply tail filter
156
157 // filter initialization (resets internal registers)
158 void filterPedestalInit(int baseline = 10);
159 void filterGainInit();
160 void filterTailInit(int baseline = -1);
161
162 // feed single sample to individual filter
163 // this changes the internal registers
164 // all filters operate on (10+2)-bit values!
165 unsigned short filterPedestalNextSample(int adc, int timebin, unsigned short value);
166 unsigned short filterGainNextSample(int adc, unsigned short value);
167 unsigned short filterTailNextSample(int adc, unsigned short value);
168
169 // tracklet calculation
170 void addHitToFitreg(int adc, unsigned short timebin, unsigned short qtot, short ypos);
171 void calcFitreg();
172 void trackletSelection();
173 void fitTracklet();
174
175 // getters for calculated tracklets + labels
176 std::vector<Tracklet64>& getTrackletArray64() { return mTrackletArray64; }
177 std::vector<unsigned short>& getTrackletDigitCount() { return mTrackletDigitCount; }
178 std::vector<unsigned int>& getTrackletDigitIndices() { return mTrackletDigitIndices; }
179
180 // data display
181 void print(int choice) const; // print stored data to stdout
182 void draw(int choice, int index); // draw data (ADC data, hits and tracklets)
183
184 friend std::ostream& operator<<(std::ostream& os, const TrapSimulator& mcm); // data output using ostream (e.g. cout << mcm;)
185 static std::ostream& cfdat(std::ostream& os); // manipulator to activate cfdat output
186 static std::ostream& raw(std::ostream& os); // manipulator to activate raw output
187 static std::ostream& text(std::ostream& os); // manipulator to activate text output
188
189 // I/O
190 void printFitRegXml(std::ostream& os) const;
191 void printTrackletsXml(std::ostream& os) const;
192 void printAdcDatTxt(std::ostream& os) const;
193 void printAdcDatHuman(std::ostream& os) const;
194 void printAdcDatXml(std::ostream& os) const;
195 void printAdcDatDatx(std::ostream& os, bool broadcast = kFALSE, int timeBinOffset = -1) const;
196
197 static bool readPackedConfig(TrapConfig* cfg, int hc, unsigned int* data, int size);
198
199 // DMEM addresses
200 static constexpr int mgkDmemAddrLUTcor0 = 0xC02A;
201 static constexpr int mgkDmemAddrLUTcor1 = 0xC028;
202 static constexpr int mgkDmemAddrLUTnbins = 0xC029;
203
204 static constexpr int mgkDmemAddrLUTStart = 0xC100; // LUT start address
205 static constexpr int mgkDmemAddrLUTEnd = 0xC3FF; // maximum possible end address for the LUT table
206 static constexpr int mgkDmemAddrLUTLength = 0xC02B; // address where real size of the LUT table is stored
207
208 static constexpr int mgkDmemAddrTrackletStart = 0xC0E0; // Storage area for tracklets, start address
209 static constexpr int mgkDmemAddrTrackletEnd = 0xC0E3; // Storage area for tracklets, end address
210
211 static constexpr int mgkDmemAddrDeflCorr = 0xc022; // DMEM address of deflection correction
212 static constexpr int mgkDmemAddrNdrift = 0xc025; // DMEM address of Ndrift
213 static constexpr int mgkDmemAddrDeflCutStart = 0xc030; // DMEM start address of deflection cut
214 static constexpr int mgkDmemAddrDeflCutEnd = 0xc055; // DMEM end address of deflection cut
215 static constexpr int mgkDmemAddrTimeOffset = 0xc3fe; // DMEM address of time offset t0
216 static constexpr int mgkDmemAddrYcorr = 0xc3ff; // DMEM address of y correction (mis-alignment)
217 static constexpr int mQ2Startbin = 3; // Start range of Q2, for now here. TODO pull from a revised TrapConfig?
218 static constexpr int mQ2Endbin = 5; // End range of Q2, also pull from a revised trapconfig at some point.
219
220 static const int mgkFormatIndex; // index for format settings in stream
221 //TODO should this change to 3 for the new format ????? I cant remember now, ask someone.
222 static const int mgkAddDigits = 2; // additional digits used for internal representation of ADC data
223 // all internal data as after data control block (i.e. 12 bit), s. TRAP manual
224
225 static const std::array<unsigned short, 4> mgkFPshifts; // shifts for pedestal filter
226
227 private:
228 bool mInitialized{false}; // memory is allocated if initialized
229 bool mDataIsSet{false}; // flag whether input data has already been provided
230 int mDetector{-1}; // Chamber ID
231 int mRobPos{-1}; // ROB Position on chamber
232 int mMcmPos{-1}; // MCM Position on chamber
233 int mNTimeBin{-1}; // Number of timebins currently allocated
234 uint32_t mMcmHeaderEmpty; // the MCM header part without the charges (this depends only on the MCM row/column)
235 uint64_t mTrkltWordEmpty; // the part of the Tracklet64 which depends only on MCM row/column and detector
236 bool mDontSendEmptyHeaderTrklt{false}; // flag, whether empty headers should be discarded
237 int mADCFilled = 0; // bitpattern with ADC channels with data
238 int mAdditionalBaseline = 0; // additional baseline to be added to the ADC input values
239
240 // PID related settings
241 bool mUseFloatingPointForQ{false}; // whether to use floating point or fixed multiplier calculation of the charges
242 int mScaleQ{0x10000000}; // scale the charge by 1/16
243 int mSizeQ0Q1{7}; // size of Q0 and Q1 charge windows in bit
244 int mMaskQ0Q1{(1 << mSizeQ0Q1) - 1}; // bit mask 0x7f
245 int mSizeQ2{6}; // size of Q2 charge window in bit
246 int mMaxQ2{(1 << mSizeQ2) - 2}; // bit mask 0x3e
247 int mQ2LeftMargin{7};
248 int mQ2WindowWidth{7};
249 // Q = Q >> N, where N takes one of the 4 values mDynShiftx, defined below
250 int mDynSize{6}; // bit for charge in the dynamical mode
251 int mDynMask{(1 << mDynSize) - 1};
252 int mDynShift0{2};
253 int mDynShift1{4};
254 int mDynShift2{6};
255 int mDynShift3{8};
256 int mSizeLPID{12};
257 int mMaskLPID{(1 << mSizeLPID) - 1};
258 int mSizeHPID{8};
259 int mEmptyHPID8{(1 << mSizeHPID) - 1};
260 int mEmptyHPID24{mEmptyHPID8 | (mEmptyHPID8 << 8) | (mEmptyHPID8 << 16)};
261
262 //TODO adcr adcf labels zerosupressionmap can all go into their own class. Refactor when stable.
263 std::vector<int> mADCR; // Array with MCM ADC values (Raw, 12 bit) 2d with dimension mNTimeBin
264 std::vector<int> mADCF; // Array with MCM ADC values (Filtered, 12 bit) 2d with dimension mNTimeBin
265 std::array<int, constants::NADCMCM> mADCDigitIndices{}; // indices of the incoming digits, used to relate the tracklets to labels in TRDTrapSimulatorSpec
266 std::array<uint32_t, 4> mMCMT; // tracklet words for one mcm/trap-chip (one word for each cpu)
267 std::vector<Tracklet64> mTrackletArray64; // Array of 64 bit tracklets
268 std::vector<unsigned short> mTrackletDigitCount; // Keep track of the number of digits contributing to the tracklet (for MC labels)
269 std::vector<unsigned int> mTrackletDigitIndices; // For each tracklet the up to two global indices of the digits which contributed (global digit indices are managed in the TRDDPLTrapSimulatorTask class)
270 std::vector<int> mZSMap; // Zero suppression map (1 dimensional projection)
271
272 std::array<int, constants::NCPU> mFitPtr{}; // pointer to the tracklet to be calculated by CPU i
273 std::array<FitReg, constants::NADCMCM> mFitReg{}; // Fit register for each ADC channel
274 std::array<FilterReg, constants::NADCMCM> mInternalFilterRegisters;
275
276 // Parameter classes
277 FeeParam* mFeeParam{FeeParam::instance()}; // FEE parameters, a singleton
278 TrapConfig* mTrapConfig{nullptr}; // TRAP config
279
280 // Sort functions as in TRAP
281 void sort2(uint16_t idx1i, uint16_t idx2i, uint16_t val1i, uint16_t val2i,
282 uint16_t* idx1o, uint16_t* idx2o, uint16_t* val1o, uint16_t* val2o) const;
283 void sort3(uint16_t idx1i, uint16_t idx2i, uint16_t idx3i,
284 uint16_t val1i, uint16_t val2i, uint16_t val3i,
285 uint16_t* idx1o, uint16_t* idx2o, uint16_t* idx3o,
286 uint16_t* val1o, uint16_t* val2o, uint16_t* val3o) const;
287 void sort6To4(uint16_t idx1i, uint16_t idx2i, uint16_t idx3i, uint16_t idx4i, uint16_t idx5i, uint16_t idx6i,
288 uint16_t val1i, uint16_t val2i, uint16_t val3i, uint16_t val4i, uint16_t val5i, uint16_t val6i,
289 uint16_t* idx1o, uint16_t* idx2o, uint16_t* idx3o, uint16_t* idx4o,
290 uint16_t* val1o, uint16_t* val2o, uint16_t* val3o, uint16_t* val4o) const;
291 void sort6To2Worst(uint16_t idx1i, uint16_t idx2i, uint16_t idx3i, uint16_t idx4i, uint16_t idx5i, uint16_t idx6i,
292 uint16_t val1i, uint16_t val2i, uint16_t val3i, uint16_t val4i, uint16_t val5i, uint16_t val6i,
293 uint16_t* idx5o, uint16_t* idx6o) const;
294
295 // Add a and b (unsigned) with clipping to the maximum value representable by nbits
296 unsigned int addUintClipping(unsigned int a, unsigned int b, unsigned int nbits) const;
297
298 // The same LUT Venelin uses for his C++ test model of the TRAP (to check agreement with his results)
299 const uint16_t LUT_POS[128] = {
300 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15,
301 16, 16, 16, 17, 17, 18, 18, 19, 19, 19, 20, 20, 20, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 24, 25, 25, 25, 26, 26, 26, 26,
302 27, 27, 27, 27, 27, 27, 27, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 27, 27, 27, 27, 26,
303 26, 26, 26, 25, 25, 25, 24, 24, 23, 23, 22, 22, 21, 21, 20, 20, 19, 18, 18, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 7};
304};
305
306std::ostream& operator<<(std::ostream& os, const TrapSimulator& mcm);
307std::ostream& operator<<(std::ostream& os, TrapSimulator::FitReg& fitreg);
308
309} //namespace trd
310} //namespace o2
311#endif
uint16_t mcm
uint16_t rob
Global TRD definitions and constants.
void print() const
std::vector< uint16_t > nsamples
Definition testDigit.cxx:38
static FeeParam * instance()
Definition FeeParam.cxx:58
static constexpr int mgkDmemAddrDeflCorr
int getAdditionalBaseline() const
static constexpr int mgkDmemAddrLUTcor0
void filterPedestalInit(int baseline=10)
static const int mgkFormatIndex
std::vector< unsigned int > & getTrackletDigitIndices()
unsigned short filterTailNextSample(int adc, unsigned short value)
void setAdditionalBaseline(int adc)
static std::ostream & raw(std::ostream &os)
void printFitRegXml(std::ostream &os) const
static constexpr int mgkDmemAddrDeflCutStart
friend std::ostream & operator<<(std::ostream &os, const TrapSimulator &mcm)
static constexpr int mgkDmemAddrTimeOffset
static bool readPackedConfig(TrapConfig *cfg, int hc, unsigned int *data, int size)
int getZeroSupressionMap(int iadc) const
void printAdcDatDatx(std::ostream &os, bool broadcast=kFALSE, int timeBinOffset=-1) const
static std::ostream & cfdat(std::ostream &os)
void noiseTest(int nsamples, int mean, int sigma, int inputGain=1, int inputTail=2)
TrapSimulator & operator=(const TrapSimulator &)=delete
int getDataFiltered(int iadc, int timebin) const
static constexpr int mgkDmemAddrLUTnbins
int getNumberOfTimeBins() const
void printAdcDatTxt(std::ostream &os) const
std::vector< unsigned short > & getTrackletDigitCount()
void setChargeScalingFactor(int scale)
void printAdcDatXml(std::ostream &os) const
bool checkInitialized() const
static std::ostream & text(std::ostream &os)
TrapSimulator(const TrapSimulator &)=delete
std::vector< Tracklet64 > & getTrackletArray64()
static constexpr int mgkDmemAddrNdrift
static constexpr int mgkDmemAddrTrackletStart
int getDataRaw(int iadc, int timebin) const
static const std::array< unsigned short, 4 > mgkFPshifts
static const int mgkAddDigits
void setData(int iadc, const ArrayADC &adc, unsigned int digitIdx)
static constexpr int mgkDmemAddrLUTcor1
static constexpr int mgkDmemAddrLUTLength
static constexpr int mgkDmemAddrDeflCutEnd
unsigned short filterGainNextSample(int adc, unsigned short value)
static constexpr int mgkDmemAddrLUTStart
unsigned short filterPedestalNextSample(int adc, int timebin, unsigned short value)
void printTrackletsXml(std::ostream &os) const
void draw(int choice, int index)
static constexpr int mgkDmemAddrYcorr
static constexpr int mQ2Endbin
void filterTailInit(int baseline=-1)
static constexpr int mQ2Startbin
void init(TrapConfig *trapconfig, int det, int rob, int mcm)
static constexpr int mgkDmemAddrTrackletEnd
void printAdcDatHuman(std::ostream &os) const
static constexpr int mgkDmemAddrLUTEnd
void addHitToFitreg(int adc, unsigned short timebin, unsigned short qtot, short ypos)
int packData(std::vector< uint32_t > &rawdata, uint32_t offset) const
void setDataPedestal(int iadc)
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLboolean * data
Definition glcorearb.h:298
GLintptr offset
Definition glcorearb.h:660
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
std::array< ADC_t, constants::TIMEBINS > ArrayADC
Definition Digit.h:32
std::ostream & operator<<(std::ostream &stream, const Digit &d)
Definition Digit.cxx:78
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
void dumpHex(int channel) const
ArrayADC adc