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