]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawStream.h
make the script working
[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
52   Bool_t ReadEvent();
53
54   Bool_t NextDDL();
55   Int_t NextChamber(AliTRDdigitsManager *digMgr);
56   Int_t NextChamber(AliTRDdigitsManager *digMgr,
57                       UInt_t ** /* trackletContainer */, UShort_t ** /* errorContainer */) { AliError("Deprecated, use NextChamber(AliTRDdigitsManger*) instead!"); return NextChamber(digMgr); }
58
59   void StoreErrorsInTree()   { fStoreError = &AliTRDrawStream::StoreErrorTree; }
60   void StoreErrorsInArray()  { fStoreError = &AliTRDrawStream::StoreErrorArray; }
61   void EnableErrorStorage()  { fStoreError = &AliTRDrawStream::StoreErrorTree; }
62   void DisableErrorStorage() { fStoreError = &AliTRDrawStream::ForgetError; }
63
64   // error handling
65   enum ErrorCode_t {
66     kUnknown = 0,
67     kLinkMonitor,
68     kEvCntMismatch,
69     kNonTrdEq,
70     kStackHeaderInvalid,
71     kInvalidDetector,
72     kInvalidPadRow,
73     kNoDigits,
74     kHCmismatch,
75     kHCcheckFailed,
76     kPosUnexp,
77     kTPmodeInvalid,
78     kTPmismatch,
79     kNtimebinsChanged,
80     kAdcMaskInconsistent,
81     kAdcCheckInvalid,
82     kAdcDataAbort,
83     kAdcChannelsMiss,
84     kMissMcmHeaders,
85     kMissTpData,
86     kCRCmismatch,
87     kLastErrorCode
88   };
89
90   enum ErrorBehav_t {
91     kTolerate = 0,
92     kDiscardMCM = 1,
93     kDiscardHC = 2,
94     kDiscardDDL = 4,
95     kAbort = 8
96   };
97
98   enum MarkerCode_t {
99     kHCactive = 1,
100     kSecactive = 2
101   };
102
103   TTree* GetErrorTree() const { return fErrors; }
104   static const char* GetErrorMessage(ErrorCode_t errCode);
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; }
107
108   class AliTRDrawStreamError : public TObject {
109   public:
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);
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
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
154     AliTRDrawStatsSector fStatsSector[18]; //[18] sector-wise statistics
155     Int_t fBytesRead;                      // number of bytes read
156     ClassDef(AliTRDrawStats, 1);
157   };
158
159   AliTRDrawStats* GetStats() { return &fStats; }
160   Int_t GetEventSize(Int_t sector)  const { return fStats.fStatsSector[sector].fBytes; }
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   }
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   }
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; }
174
175   ULong64_t GetTrkFlags(Int_t sector, Int_t stack) const { return (fCurrTrgFlags[sector] & (1 << (27 + stack))) ? fCurrTrkFlags[sector*fgkNstacks + stack] : 0; }
176   UInt_t GetTriggerFlags(Int_t sector) const { return fCurrTrgFlags[sector]; }
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
198
199   // raw data dumping
200   void SetDumpMCM(Int_t det, Int_t rob, Int_t mcm, Bool_t dump = kTRUE);
201
202   Bool_t IsDumping() const { return (fNDumpMCMs > 0); }
203   Bool_t DumpingMCM(Int_t det, Int_t rob, Int_t mcm) const;
204
205   TString DumpRaw(TString title, const UInt_t *start, Int_t length, UInt_t endmarker = 0xffffffff);
206   TString DumpMcmHeader(TString title, UInt_t word);
207   TString DumpAdcMask(TString title, UInt_t word);
208
209   static void SortTracklets(TClonesArray *trklArray, TList &sortedTracklets, Int_t *indices);
210   static void AssignTracklets(AliESDTrdTrack *trdTrack, Int_t *trackletIndex, Int_t refIndex[6]);
211
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; }
215
216  protected:
217   Int_t ReadGTUHeaders(UInt_t *buffer);
218   Int_t ReadSmHeader();
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();
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
232   UShort_t CalcLinkChecksum(UInt_t *data, Int_t size);
233
234   Int_t SeekNextStack();
235   Int_t SeekNextLink();
236
237   // MCM header decoding
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
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; }
249
250   // ADC mask decoding
251   Int_t GetActiveChannels(UInt_t adcmask) const { return 0x1fffff & adcmask >> 4; }
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; }
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); }
255
256   // error message generation
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 = " ", ...);
262   void StoreErrorTree() { fErrors->Fill(); }
263   void StoreErrorArray() { new ((*fMarkers)[fMarkers->GetEntriesFast()]) AliTRDrawStreamError(fLastError); }
264   void ForgetError() { return; }
265   void (AliTRDrawStream::*fStoreError)();       //! function pointer to method used for storing the error
266
267   static const char* fgkErrorMessages[kLastErrorCode];     // error messages corresponding to the error codes
268   static       Int_t fgErrorDebugLevel[kLastErrorCode];   // error debug level
269   static       ErrorBehav_t fgErrorBehav[kLastErrorCode]; // bevhaviour in case of error of given type
270
271   // I/O
272   AliRawReader *fRawReader;                     // pointer to the raw reader to take the data from
273   AliTRDdigitsManager *fDigitsManager;          // pointer to the digitsManager to fill the data
274   AliTRDdigitsParam   *fDigitsParam;            // pointer to the parameters belonging to the digits
275
276   TTree *fErrors;                               // tree containing the occured error codes
277   AliTRDrawStreamError fLastError;              // last error which occured
278   UInt_t fErrorFlags;                           // error flags used to steer subsequent reading
279   char   fErrorBuffer[100];                     // buffer for error message
280
281   AliTRDrawStats fStats;             // event statistics, clearing must be done by the user
282
283   UInt_t *fPayloadStart;                        // pointer to start of data payload
284   UInt_t *fPayloadCurr;                         // pointer to current reading position in the payload
285   Int_t   fPayloadSize;                         // size of the payload (in UInt_t words)
286
287   static const Int_t fgkNlinks;                 // number of links to read
288   static const Int_t fgkNsectors;               // number of sectors
289   static const Int_t fgkNstacks;                // number of stacks to read
290   static const Int_t fgkNtriggers;              // number of triggers in data stream
291   static const UInt_t fgkDataEndmarker;         // data endmarker
292   static const UInt_t fgkTrackletEndmarker;     // tracklet endmarker
293   static const UInt_t fgkStackEndmarker[];      // stack endmarker (used from version 0xd on)
294   static       Int_t fgMcmOrder [];             // expected readout order of the MCMs
295   static       Int_t fgRobOrder [];             // expected readout order of the ROBs
296
297   // persistent information
298   Int_t  fNtimebins;                            // number of timebins
299   Int_t  fLastEvId;                             // Event ID of last event
300
301   // information valid at current reader position
302   // all the variables fCurr... refer to the value at the current
303   // reading position
304   Int_t fCurrSlot;                              // current slot
305   Int_t fCurrLink;                              // current link
306   Int_t fCurrRobPos;                            // current ROB number
307   Int_t fCurrMcmPos;                            // current MCM number
308
309   // DDL header
310   UInt_t fCurrEquipmentId;                      // current Equipment ID
311
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
319   UInt_t fCurrStackEndmarkerAvail;              // current stack endmarker availability
320   UInt_t fCurrEvType;                           // current event type
321   UInt_t fCurrTriggerEnable;                    // current trigger enable
322   UInt_t fCurrTriggerFired;                     // current trigger fired
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
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
335
336   // Tracking header
337   UInt_t *fCurrTrkHeaderIndexWord;              // current tracking header index word
338   UInt_t *fCurrTrkHeaderSize;                   // current tracking header index word
339   ULong64_t *fCurrTrkFlags;                     // current tracking done flags
340
341   // Trigger header
342   UInt_t *fCurrTrgHeaderIndexWord;              // current tracking header index word
343   UInt_t *fCurrTrgHeaderSize;                   // current tracking header index word
344   UInt_t *fCurrTrgFlags;                        // current trigger flags of all sectors
345
346   // Stack header
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
353   UInt_t  fCurrHwRev;                           // current hardware revision
354   UInt_t *fCurrHwRevTMU;                        // current hardware revision
355   UInt_t *fCurrLinkMonitorFlags;                // current link monitor flags
356   UInt_t *fCurrLinkDataTypeFlags;               // current link data flags
357   UInt_t *fCurrLinkDebugFlags;                  // current link debug flags
358
359   // CRC checks from trailer
360   Char_t fCurrMatchFlagsSRAM[18];
361   Char_t fCurrMatchFlagsPostBP[18];
362   UInt_t fCurrChecksumStack[18][5];
363   UInt_t fCurrChecksumSIU;
364
365   // HC information
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
377   Int_t fCurrPtrgCnt;                           // current pretrigger count
378   Int_t fCurrPtrgPhase;                         // current pretrigger phase
379 #ifdef TRD_RAW_DEBUG
380   Int_t fCurrBC[1080];                          // current BC
381 #endif
382
383   // settings for dumping
384   Int_t fDumpMCM[100];                          // MCMs to dump
385   Int_t fNDumpMCMs;                             // number of MCMs to dump
386
387   // output data
388   AliTRDarrayADC *fAdcArray;                    // pointer to ADC array
389   AliTRDSignalIndex *fSignalIndex;              // pointer to the signal index
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, ...)
393
394   AliTRDrawStream(const AliTRDrawStream&);           // not implemented
395   AliTRDrawStream& operator=(const AliTRDrawStream&); // not implemented
396
397   ClassDef(AliTRDrawStream, 0);
398 };
399
400 Int_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
410 #endif