added slewing correction by data
[u/mrichter/AliRoot.git] / TRD / AliTRDrawStream.h
CommitLineData
0508ca31 1/* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
2 * See cxx source for full Copyright notice */
3
4//-----------------------------------
5//
5f006bd7 6// decoding of TRD raw data stream
0508ca31 7// and translation into digits
8//
9//----------------------------------
10
d60fe037 11#ifndef ALITRDRAWSTREAM_H
12#define ALITRDRAWSTREAM_H
13
f4b3235e 14#include "TTree.h"
9cb9c409 15#include "AliLog.h"
6419bebb 16#include "TClonesArray.h"
9cb9c409 17
6419bebb 18class TObject;
d60fe037 19class TObjArray;
20class TString;
cc26f39c 21class TBranch;
d60fe037 22
23class AliRawReader;
24class AliTRDdigitsManager;
25class AliTRDdigitsParam;
26class AliTRDarrayADC;
27class AliTRDSignalIndex;
cc26f39c 28class AliTRDtrackletContainer;
03e2f460 29class AliESDTrdTrack;
d60fe037 30
8df1f8f5 31class AliTRDrawStream : public TObject
d60fe037 32{
33 public:
34 AliTRDrawStream(AliRawReader *rawReader = 0x0);
35 ~AliTRDrawStream();
36
9cb9c409 37 enum { kDDLOffset = 0x400, // Offset for DDL numbers
38 kDDLMax = 0x411 }; // Max DDL number for TRD SM
84f471a7 39
d60fe037 40 Bool_t SetReader(AliRawReader *rawReader) { fRawReader = rawReader; return kTRUE; }
41 void SetDigitsManager(AliTRDdigitsManager *digMgr) { fDigitsManager = digMgr; }
5fdfc9e4 42 void SetTrackletArray(TClonesArray *ar) { fTracklets = ar; }
43 void SetTrackArray(TClonesArray *ar) { fTracks = ar; }
44 void SetMarkerArray(TClonesArray *ar) { fMarkers = ar; }
45
46 TClonesArray* GetTrackletArray() const { return fTracklets; }
47 TClonesArray* GetTrackArray() const { return fTracks; }
48 TClonesArray* GetMarkerArray() const { return fMarkers; }
d60fe037 49
f4b3235e 50 AliTRDdigitsManager* GetDigitsManager() const { return fDigitsManager; }
f4b3235e 51
f77d6f54 52 Bool_t ReadEvent();
d60fe037 53
54 Bool_t NextDDL();
5f006bd7 55 Int_t NextChamber(AliTRDdigitsManager *digMgr);
9cb9c409 56 Int_t NextChamber(AliTRDdigitsManager *digMgr,
2519cca4 57 UInt_t ** /* trackletContainer */, UShort_t ** /* errorContainer */) { AliError("Deprecated, use NextChamber(AliTRDdigitsManger*) instead!"); return NextChamber(digMgr); }
d60fe037 58
5fdfc9e4 59 void StoreErrorsInTree() { fStoreError = &AliTRDrawStream::StoreErrorTree; }
60 void StoreErrorsInArray() { fStoreError = &AliTRDrawStream::StoreErrorArray; }
f4b3235e 61 void EnableErrorStorage() { fStoreError = &AliTRDrawStream::StoreErrorTree; }
62 void DisableErrorStorage() { fStoreError = &AliTRDrawStream::ForgetError; }
63
d60fe037 64 // error handling
5f006bd7 65 enum ErrorCode_t {
66 kUnknown = 0,
67 kLinkMonitor,
52a2b6c0 68 kEvCntMismatch,
d60fe037 69 kNonTrdEq,
70 kStackHeaderInvalid,
71 kInvalidDetector,
789a8b20 72 kInvalidPadRow,
d60fe037 73 kNoDigits,
74 kHCmismatch,
75 kHCcheckFailed,
76 kPosUnexp,
77 kTPmodeInvalid,
78 kTPmismatch,
79 kNtimebinsChanged,
80 kAdcMaskInconsistent,
81 kAdcCheckInvalid,
82 kAdcDataAbort,
83 kAdcChannelsMiss,
84 kMissMcmHeaders,
2519cca4 85 kMissTpData,
52a2b6c0 86 kCRCmismatch,
d60fe037 87 kLastErrorCode
5f006bd7 88 };
d60fe037 89
cc26f39c 90 enum ErrorBehav_t {
92223bf6 91 kTolerate = 0,
5fedeee9 92 kDiscardMCM = 1,
93 kDiscardHC = 2,
94 kDiscardDDL = 4,
95 kAbort = 8
cc26f39c 96 };
97
5fdfc9e4 98 enum MarkerCode_t {
8df9496c 99 kHCactive = 1,
100 kSecactive = 2
5fdfc9e4 101 };
102
0508ca31 103 TTree* GetErrorTree() const { return fErrors; }
d60fe037 104 static const char* GetErrorMessage(ErrorCode_t errCode);
9cb9c409 105 static void SetErrorDebugLevel(ErrorCode_t error, Int_t level) { fgErrorDebugLevel[error] = level; }
106 static void SetErrorBehaviour(ErrorCode_t error, ErrorBehav_t behav) { fgErrorBehav[error] = behav; }
d60fe037 107
8df9496c 108 class AliTRDrawStreamError : public TObject {
109 public:
5f006bd7 110 AliTRDrawStreamError(Int_t error = 0, Int_t sector = -1, Int_t stack = -1, Int_t link = -1, Int_t rob = -1, Int_t mcm = -1);
8df9496c 111 virtual ~AliTRDrawStreamError() {}
112 Int_t fError; // error code
113 Int_t fSector; // sector
114 Int_t fStack; // stack
115 Int_t fLink; // link
116 Int_t fRob; // ROB no
117 Int_t fMcm; // MCM no
118 ClassDef(AliTRDrawStreamError, 1);
119 };
120
cc26f39c 121 // event statistics
122 class AliTRDrawStats : public TObject {
123 public:
124 AliTRDrawStats() : TObject(), fBytesRead(0) {}
125 void ClearStats();
126
127 class AliTRDrawStatsSector : public TObject {
128 public:
129 AliTRDrawStatsSector() : TObject(), fBytes(0), fBytesRead(0), fNTracklets(0), fNMCMs(0), fNChannels(0) {}
130 void ClearStats();
131
132 class AliTRDrawStatsHC : public TObject {
133 public:
134 AliTRDrawStatsHC() : TObject(), fBytes(0), fBytesRead(0), fNTracklets(0), fNMCMs(0), fNChannels(0) {}
135 void ClearStats();
136
137 Int_t fBytes; // number of bytes (not necessarily read)
138 Int_t fBytesRead; // number of bytes read
139 Int_t fNTracklets; // number of tracklets
140 Int_t fNMCMs; // number of MCMs (from MCM headers)
141 Int_t fNChannels; // number of channels
142 ClassDef(AliTRDrawStatsHC, 1);
143 };
144
145 Int_t fBytes; // number of bytes (not necessarily read)
146 Int_t fBytesRead; // number of bytes read
147 Int_t fNTracklets; // number of tracklets
148 Int_t fNMCMs; // number of MCMs (from MCM headers)
149 Int_t fNChannels; // number of channels
150 AliTRDrawStatsHC fStatsHC[60]; //[60] HC-wise statistics
151 ClassDef(AliTRDrawStatsSector, 1);
152 };
153
cc26f39c 154 AliTRDrawStatsSector fStatsSector[18]; //[18] sector-wise statistics
155 Int_t fBytesRead; // number of bytes read
156 ClassDef(AliTRDrawStats, 1);
157 };
158
cc26f39c 159 AliTRDrawStats* GetStats() { return &fStats; }
92305359 160 Int_t GetEventSize(Int_t sector) const { return fStats.fStatsSector[sector].fBytes; }
5fedeee9 161 Int_t GetEventSize(Int_t sector, Int_t stack) const {
162 Int_t size = 0;
163 for (Int_t iHC = 0; iHC < 12; iHC++) {
164 size += fStats.fStatsSector[sector].fStatsHC[12*stack + iHC].fBytes;
165 }
166 return size;
167 }
f77d6f54 168 Int_t GetEventSize(Int_t sector, Int_t stack, Int_t hc) const {
169 return fStats.fStatsSector[sector].fStatsHC[12*stack + hc].fBytes;
170 }
92305359 171 Int_t GetNTracklets(Int_t sector) const { return fStats.fStatsSector[sector].fNTracklets; }
172 Int_t GetNMCMs(Int_t sector) const { return fStats.fStatsSector[sector].fNMCMs; }
173 Int_t GetNChannels(Int_t sector) const { return fStats.fStatsSector[sector].fNChannels; }
cc26f39c 174
03e2f460 175 ULong64_t GetTrkFlags(Int_t sector, Int_t stack) const { return (fCurrTrgFlags[sector] & (1 << (27 + stack))) ? fCurrTrkFlags[sector*fgkNstacks + stack] : 0; }
2519cca4 176 UInt_t GetTriggerFlags(Int_t sector) const { return fCurrTrgFlags[sector]; }
f77d6f54 177 UInt_t GetLinkMonitorFlags(Int_t sector, Int_t stack) const {
178 UInt_t temp = 0;
179 for (Int_t iLink = 0; iLink < 12; iLink++)
180 temp |= (fCurrLinkMonitorFlags[(sector * fgkNstacks + stack) * fgkNlinks + iLink] & 0x3) << (iLink * 2);
181 temp |= ((GetMatchFlagsBP(sector) >> stack) & 0x1) << 24;
182 temp |= ((GetMatchFlagsSRAM(sector) >> stack) & 0x1) << 25;
183 return temp;
184 }
185 UInt_t GetMatchFlagsSRAM(Int_t sector) const { return fCurrMatchFlagsSRAM[sector]; }
186 UInt_t GetMatchFlagsBP(Int_t sector) const { return fCurrMatchFlagsPostBP[sector]; }
187
188#ifdef TRD_RAW_DEBUG
189 UInt_t GetBC(Int_t hc) const { return fCurrBC[hc]; }
190 UInt_t GetEvCount(Int_t det) const { return fCurrEvCount[det]; }
191 UInt_t GetL0Count(Int_t sector) const { return fCurrL0Count[sector]; }
192 UInt_t GetL1aCount(Int_t sector) const { return fCurrL1aCount[sector]; }
193 UInt_t GetL1rCount(Int_t sector) const { return fCurrL1rCount[sector]; }
194 UInt_t GetL2aCount(Int_t sector) const { return fCurrL2aCount[sector]; }
195 UInt_t GetL2rCount(Int_t sector) const { return fCurrL2rCount[sector]; }
196 UInt_t GetChecksumStack(Int_t sector, Int_t stack) const { return fCurrChecksumStack[sector][stack]; }
197#endif
2519cca4 198
cc26f39c 199 // raw data dumping
200 void SetDumpMCM(Int_t det, Int_t rob, Int_t mcm, Bool_t dump = kTRUE);
201
92305359 202 Bool_t IsDumping() const { return (fNDumpMCMs > 0); }
203 Bool_t DumpingMCM(Int_t det, Int_t rob, Int_t mcm) const;
cc26f39c 204
6419bebb 205 TString DumpRaw(TString title, const UInt_t *start, Int_t length, UInt_t endmarker = 0xffffffff);
9cb9c409 206 TString DumpMcmHeader(TString title, UInt_t word);
207 TString DumpAdcMask(TString title, UInt_t word);
208
52a2b6c0 209 static void SortTracklets(TClonesArray *trklArray, TList &sortedTracklets, Int_t *indices);
03e2f460 210 static void AssignTracklets(AliESDTrdTrack *trdTrack, Int_t *trackletIndex, Int_t refIndex[6]);
52a2b6c0 211
9cb9c409 212 // temporary: allow to change expected readout order
213 static void SetMCMReadoutPos(Int_t mcm, Int_t pos) { if (mcm > -1 && mcm < 16) fgMcmOrder[mcm] = pos; }
214 static void SetROBReadoutPos(Int_t robpair, Int_t pos) { if (robpair > -1 && robpair < 4) fgMcmOrder[robpair] = pos; }
cc26f39c 215
d60fe037 216 protected:
9cb9c409 217 Int_t ReadGTUHeaders(UInt_t *buffer);
d60fe037 218 Int_t ReadSmHeader();
9cb9c409 219 Int_t ReadTrackingHeader(Int_t stack);
220 Int_t ReadTriggerHeaders();
221 Int_t ReadStackHeader(Int_t stack);
222 Int_t DecodeGTUtracks();
223 Int_t ReadGTUTrailer();
d60fe037 224
225 Int_t ReadLinkData();
226 Int_t ReadTracklets();
227 Int_t ReadHcHeader();
228 Int_t ReadTPData(Int_t mode = 1);
229 Int_t ReadZSData();
230 Int_t ReadNonZSData();
231
f77d6f54 232 UShort_t CalcLinkChecksum(UInt_t *data, Int_t size);
233
52a2b6c0 234 Int_t SeekNextStack();
92223bf6 235 Int_t SeekNextLink();
236
d60fe037 237 // MCM header decoding
0508ca31 238 Int_t ROB(UInt_t mcmhdr) const { return 0x7 & mcmhdr >> 28; }
239 Int_t MCM(UInt_t mcmhdr) const { return 0xf & mcmhdr >> 24; }
240 Int_t Row(UInt_t mcmhdr) const { return (ROB(mcmhdr) / 2) * 4 + MCM(mcmhdr) / 4; }
241 Int_t AdcColOffset(UInt_t mcmhdr) const { return (MCM(mcmhdr) % 4 + 1) * 21 + (ROB(mcmhdr) % 2) * 84 - 1; }
242 Int_t PadColOffset(UInt_t mcmhdr) const { return (MCM(mcmhdr) % 4 + 1) * 18 + (ROB(mcmhdr) % 2) * 72 + 1; }
243 Int_t EvNo(UInt_t mcmhdr) const { return 0xfffff & mcmhdr >> 4; }
244 Int_t Check(UInt_t mcmhdr) const { return 0xf & mcmhdr; }
245 Int_t CouldBeMCMhdr(UInt_t mcmhdr) const { return ((0xf & mcmhdr) == 0xc); }
246
9cb9c409 247 Int_t GetMCMReadoutPos(Int_t mcm) const { return (mcm > -1 && mcm < 16) ? fgMcmOrder[mcm] : -1; }
248 Int_t GetROBReadoutPos(Int_t rob) const { return (rob > -1 && rob < 4) ? fgRobOrder[rob] : -1; }
d60fe037 249
250 // ADC mask decoding
0508ca31 251 Int_t GetActiveChannels(UInt_t adcmask) const { return 0x1fffff & adcmask >> 4; }
82029d44 252 inline Int_t GetNActiveChannelsFromMask(UInt_t adcmask) const; // { Int_t nch = 0; for (Int_t i = 0; i < 21; i++) if ((GetActiveChannels(adcmask) & 1 << i)) nch++; return nch; }
0508ca31 253 Int_t GetNActiveChannels(UInt_t adcmask) const { return (0x1f & ~(adcmask >> 25)); }
254 Int_t CouldBeADCmask(UInt_t adcmask) const { return ((0xf & adcmask) == 0xc && (0x3 & adcmask >> 30) == 0x1); }
5f006bd7 255
d60fe037 256 // error message generation
1dd71b58 257 void EquipmentError(ErrorCode_t err = kUnknown, const char *const msg = " ", ...);
258 void StackError (ErrorCode_t err = kUnknown, const char *const msg = " ", ...);
259 void LinkError (ErrorCode_t err = kUnknown, const char *const msg = " ", ...);
260 void ROBError (ErrorCode_t err = kUnknown, const char *const msg = " ", ...);
261 void MCMError (ErrorCode_t err = kUnknown, const char *const msg = " ", ...);
f4b3235e 262 void StoreErrorTree() { fErrors->Fill(); }
5fdfc9e4 263 void StoreErrorArray() { new ((*fMarkers)[fMarkers->GetEntriesFast()]) AliTRDrawStreamError(fLastError); }
f4b3235e 264 void ForgetError() { return; }
265 void (AliTRDrawStream::*fStoreError)(); //! function pointer to method used for storing the error
d60fe037 266
92305359 267 static const char* fgkErrorMessages[kLastErrorCode]; // error messages corresponding to the error codes
268 static Int_t fgErrorDebugLevel[kLastErrorCode]; // error debug level
92223bf6 269 static ErrorBehav_t fgErrorBehav[kLastErrorCode]; // bevhaviour in case of error of given type
d60fe037 270
271 // I/O
5f006bd7 272 AliRawReader *fRawReader; // pointer to the raw reader to take the data from
0508ca31 273 AliTRDdigitsManager *fDigitsManager; // pointer to the digitsManager to fill the data
274 AliTRDdigitsParam *fDigitsParam; // pointer to the parameters belonging to the digits
d60fe037 275
0508ca31 276 TTree *fErrors; // tree containing the occured error codes
8df9496c 277 AliTRDrawStreamError fLastError; // last error which occured
92223bf6 278 UInt_t fErrorFlags; // error flags used to steer subsequent reading
279 char fErrorBuffer[100]; // buffer for error message
d60fe037 280
2519cca4 281 AliTRDrawStats fStats; // event statistics, clearing must be done by the user
282
0508ca31 283 UInt_t *fPayloadStart; // pointer to start of data payload
284 UInt_t *fPayloadCurr; // pointer to current reading position in the payload
cc26f39c 285 Int_t fPayloadSize; // size of the payload (in UInt_t words)
d60fe037 286
0508ca31 287 static const Int_t fgkNlinks; // number of links to read
9cb9c409 288 static const Int_t fgkNsectors; // number of sectors
0508ca31 289 static const Int_t fgkNstacks; // number of stacks to read
9cb9c409 290 static const Int_t fgkNtriggers; // number of triggers in data stream
5f006bd7 291 static const UInt_t fgkDataEndmarker; // data endmarker
0508ca31 292 static const UInt_t fgkTrackletEndmarker; // tracklet endmarker
52a2b6c0 293 static const UInt_t fgkStackEndmarker[]; // stack endmarker (used from version 0xd on)
9cb9c409 294 static Int_t fgMcmOrder []; // expected readout order of the MCMs
295 static Int_t fgRobOrder []; // expected readout order of the ROBs
d60fe037 296
297 // persistent information
0508ca31 298 Int_t fNtimebins; // number of timebins
5f006bd7 299 Int_t fLastEvId; // Event ID of last event
d60fe037 300
301 // information valid at current reader position
0508ca31 302 // all the variables fCurr... refer to the value at the current
303 // reading position
5f006bd7 304 Int_t fCurrSlot; // current slot
0508ca31 305 Int_t fCurrLink; // current link
306 Int_t fCurrRobPos; // current ROB number
307 Int_t fCurrMcmPos; // current MCM number
d60fe037 308
309 // DDL header
0508ca31 310 UInt_t fCurrEquipmentId; // current Equipment ID
d60fe037 311
9cb9c409 312 // SM header
313 UInt_t fCurrSmHeaderSize; // current size of the SM header
314 UInt_t fCurrSmHeaderVersion; // current version of the SM header
315 UInt_t fCurrTrailerReadout; // current presence of trailer (after the payload)
316 UInt_t fCurrTrgHeaderAvail; // current trigger information availability
317 UInt_t fCurrTrgHeaderReadout; // current readout mode for the trigger headers
318 UInt_t fCurrTrkHeaderAvail; // current tracking information availability
52a2b6c0 319 UInt_t fCurrStackEndmarkerAvail; // current stack endmarker availability
9cb9c409 320 UInt_t fCurrEvType; // current event type
321 UInt_t fCurrTriggerEnable; // current trigger enable
322 UInt_t fCurrTriggerFired; // current trigger fired
0508ca31 323 UInt_t fCurrTrackEnable; // current value of track enable
324 UInt_t fCurrTrackletEnable; // current value of tracklet enable
325 UInt_t fCurrStackMask; // current mask of active stacks
f77d6f54 326#ifdef TRD_RAW_DEBUG
327 UInt_t *fCurrL0Count; // number of received L0 triggers
328 UInt_t *fCurrL1aCount; // number of received L1a triggers
329 UInt_t *fCurrL1rCount; // number of received L1r triggers
330 UInt_t *fCurrL2aCount; // number of received L2a triggers
331 UInt_t *fCurrL2rCount; // number of received L2r triggers
332 Int_t fCurrL0offset[540]; // current offset for L0 accepts from GTU and chambers
333 UInt_t fCurrEvCount[540]; // current event count from the MCMs
334#endif
d60fe037 335
9cb9c409 336 // Tracking header
337 UInt_t *fCurrTrkHeaderIndexWord; // current tracking header index word
338 UInt_t *fCurrTrkHeaderSize; // current tracking header index word
2519cca4 339 ULong64_t *fCurrTrkFlags; // current tracking done flags
9cb9c409 340
341 // Trigger header
342 UInt_t *fCurrTrgHeaderIndexWord; // current tracking header index word
343 UInt_t *fCurrTrgHeaderSize; // current tracking header index word
2519cca4 344 UInt_t *fCurrTrgFlags; // current trigger flags of all sectors
9cb9c409 345
346 // Stack header
0508ca31 347 UInt_t *fCurrStackIndexWord; // current stack index words
348 UInt_t *fCurrStackHeaderSize; // current stack index sizes
349 UInt_t *fCurrStackHeaderVersion; // current stack header versions
350 UInt_t *fCurrLinkMask; // current link masks
351 UInt_t *fCurrCleanCheckout; // current clean checkout flags
352 UInt_t *fCurrBoardId; // current board IDs
9cb9c409 353 UInt_t fCurrHwRev; // current hardware revision
354 UInt_t *fCurrHwRevTMU; // current hardware revision
0508ca31 355 UInt_t *fCurrLinkMonitorFlags; // current link monitor flags
356 UInt_t *fCurrLinkDataTypeFlags; // current link data flags
357 UInt_t *fCurrLinkDebugFlags; // current link debug flags
d60fe037 358
52a2b6c0 359 // CRC checks from trailer
f77d6f54 360 Char_t fCurrMatchFlagsSRAM[18];
361 Char_t fCurrMatchFlagsPostBP[18];
362 UInt_t fCurrChecksumStack[18][5];
52a2b6c0 363 UInt_t fCurrChecksumSIU;
364
d60fe037 365 // HC information
0508ca31 366 Int_t fCurrSpecial; // current value of the special flag
367 Int_t fCurrMajor; // current major version
368 Int_t fCurrMinor; // current minor version
369 Int_t fCurrAddHcWords; // current number of additional HC-header words
370 Int_t fCurrSm; // current sector
371 Int_t fCurrStack; // current stack
372 Int_t fCurrLayer; // current layer
373 Int_t fCurrSide; // current side
374 Int_t fCurrHC; // current HC
375 Int_t fCurrCheck; // current check bits
376 Int_t fCurrNtimebins; // current number of timebins
0508ca31 377 Int_t fCurrPtrgCnt; // current pretrigger count
378 Int_t fCurrPtrgPhase; // current pretrigger phase
f77d6f54 379#ifdef TRD_RAW_DEBUG
380 Int_t fCurrBC[1080]; // current BC
381#endif
d60fe037 382
cc26f39c 383 // settings for dumping
384 Int_t fDumpMCM[100]; // MCMs to dump
385 Int_t fNDumpMCMs; // number of MCMs to dump
386
d60fe037 387 // output data
0508ca31 388 AliTRDarrayADC *fAdcArray; // pointer to ADC array
389 AliTRDSignalIndex *fSignalIndex; // pointer to the signal index
5fdfc9e4 390 TClonesArray *fTracklets; // pointer to array of tracklets
391 TClonesArray *fTracks; // pointer to array of GTU tracks
392 TClonesArray *fMarkers; // pointer to array of markers (data present, errors, ...)
d60fe037 393
394 AliTRDrawStream(const AliTRDrawStream&); // not implemented
0508ca31 395 AliTRDrawStream& operator=(const AliTRDrawStream&); // not implemented
d60fe037 396
397 ClassDef(AliTRDrawStream, 0);
398};
399
82029d44 400Int_t AliTRDrawStream::GetNActiveChannelsFromMask(UInt_t adcmask) const
401{
402 // return number of active bits in the ADC mask
403
404 adcmask = GetActiveChannels(adcmask);
405 adcmask = adcmask - ((adcmask >> 1) & 0x55555555);
406 adcmask = (adcmask & 0x33333333) + ((adcmask >> 2) & 0x33333333);
407 return (((adcmask + (adcmask >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
408}
409
d60fe037 410#endif