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