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