]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawStream.h
dd option to disable error logging for HLT use
[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 "AliTRDrawStreamBase.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 AliTRDrawStreamBase
32 {
33  public:
34   AliTRDrawStream(AliRawReader *rawReader = 0x0);
35   ~AliTRDrawStream();
36
37   Bool_t SetReader(AliRawReader *rawReader) { fRawReader = rawReader; return kTRUE; }
38   void SetDigitsManager(AliTRDdigitsManager *digMgr) { fDigitsManager = digMgr; }
39
40   AliTRDdigitsManager* GetDigitsManager() const { return fDigitsManager; }
41   TTree *GetTrackletTree() const { return fTrackletTree; }
42
43   Bool_t ReadEvent(TTree *trackletTree = 0x0);
44
45   Bool_t NextDDL();
46   Int_t NextChamber(AliTRDdigitsManager *digMgr, 
47                     UInt_t ** /* trackletContainer */, UShort_t ** /* errorContainer */);
48
49   Bool_t ConnectTracklets(TTree *trklTree);
50
51   void EnableErrorStorage()  { fStoreError = &AliTRDrawStream::StoreErrorTree; }
52   void DisableErrorStorage() { fStoreError = &AliTRDrawStream::ForgetError; }
53
54   // legacy code, to be removed
55   Bool_t SetRawVersion(Int_t) { return kTRUE; }
56   void SetSharedPadReadout(Bool_t) {}
57   void SetNoErrorWarning() {}
58
59   // error handling
60   enum ErrorCode_t { 
61     kUnknown = 0, 
62     kLinkMonitor, 
63     kPtrgCntMismatch, 
64     kNonTrdEq,
65     kStackHeaderInvalid,
66     kInvalidDetector,
67     kNoDigits,
68     kHCmismatch,
69     kHCcheckFailed,
70     kPosUnexp,
71     kTPmodeInvalid,
72     kTPmismatch,
73     kNtimebinsChanged,
74     kAdcMaskInconsistent,
75     kAdcCheckInvalid,
76     kAdcDataAbort,
77     kAdcChannelsMiss,
78     kMissMcmHeaders,
79     kLastErrorCode
80   }; 
81
82   enum ErrorBehav_t {
83     kTolerate = 0,
84     kAbort = 1, 
85     kDiscardMCM = 2,
86     kDiscardHC = 4
87   };
88
89   TTree* GetErrorTree() const { return fErrors; }
90   static const char* GetErrorMessage(ErrorCode_t errCode);
91
92   // event statistics
93   class AliTRDrawStats : public TObject {
94   public:
95     AliTRDrawStats() : TObject(), fBytesRead(0) {}
96     void ClearStats();
97
98     class AliTRDrawStatsSector : public TObject {
99     public:
100       AliTRDrawStatsSector() : TObject(), fBytes(0), fBytesRead(0), fNTracklets(0), fNMCMs(0), fNChannels(0) {}
101       void ClearStats();
102
103       class AliTRDrawStatsHC : public TObject {
104       public:
105         AliTRDrawStatsHC() : TObject(), fBytes(0), fBytesRead(0), fNTracklets(0), fNMCMs(0), fNChannels(0) {}
106         void ClearStats();
107
108         Int_t fBytes;             // number of bytes (not necessarily read)
109         Int_t fBytesRead;         // number of bytes read
110         Int_t fNTracklets;        // number of tracklets
111         Int_t fNMCMs;             // number of MCMs (from MCM headers)
112         Int_t fNChannels;         // number of channels
113         ClassDef(AliTRDrawStatsHC, 1);
114       };
115
116       Int_t fBytes;                  // number of bytes (not necessarily read)
117       Int_t fBytesRead;              // number of bytes read
118       Int_t fNTracklets;             // number of tracklets
119       Int_t fNMCMs;                  // number of MCMs (from MCM headers)
120       Int_t fNChannels;              // number of channels
121       AliTRDrawStatsHC fStatsHC[60]; //[60] HC-wise statistics
122       ClassDef(AliTRDrawStatsSector, 1);
123     };
124
125
126     AliTRDrawStatsSector fStatsSector[18]; //[18] sector-wise statistics
127     Int_t fBytesRead;                      // number of bytes read
128     ClassDef(AliTRDrawStats, 1);
129   };
130
131   AliTRDrawStats fStats;             // event statistics, clearing must be done by the user
132
133   AliTRDrawStats* GetStats() { return &fStats; }
134   Int_t GetEventSize(Int_t sector) { return fStats.fStatsSector[sector].fBytes; }
135   Int_t GetNTracklets(Int_t sector) { return fStats.fStatsSector[sector].fNTracklets; }
136   Int_t GetNMCMs(Int_t sector) { return fStats.fStatsSector[sector].fNMCMs; }
137   Int_t GetNChannels(Int_t sector) { return fStats.fStatsSector[sector].fNChannels; }
138
139   // raw data dumping
140   void SetDumpMCM(Int_t det, Int_t rob, Int_t mcm, Bool_t dump = kTRUE);
141
142   Bool_t IsDumping() { return (fNDumpMCMs > 0); }
143   Bool_t DumpingMCM(Int_t det, Int_t rob, Int_t mcm);
144
145   void DumpRaw(TString title, UInt_t *start, Int_t length); 
146
147  protected:
148   Int_t ReadSmHeader();
149   Int_t ReadStackIndexHeader(Int_t stack);
150
151   Int_t ReadLinkData();
152   Int_t ReadTracklets();
153   Int_t ReadHcHeader();
154   Int_t ReadTPData(Int_t mode = 1);
155   Int_t ReadZSData();
156   Int_t ReadNonZSData();
157
158   Int_t SeekNextLink();
159
160   // MCM header decoding
161   Int_t ROB(UInt_t mcmhdr) const { return 0x7 & mcmhdr >> 28; }
162   Int_t MCM(UInt_t mcmhdr) const { return 0xf & mcmhdr >> 24; }
163   Int_t Row(UInt_t mcmhdr) const { return (ROB(mcmhdr) / 2) * 4 + MCM(mcmhdr) / 4; }
164   Int_t AdcColOffset(UInt_t mcmhdr) const { return (MCM(mcmhdr) % 4 + 1) * 21 + (ROB(mcmhdr) % 2) * 84 - 1; }
165   Int_t PadColOffset(UInt_t mcmhdr) const { return (MCM(mcmhdr) % 4 + 1) * 18 + (ROB(mcmhdr) % 2) * 72 + 1; }
166   Int_t EvNo(UInt_t mcmhdr) const { return 0xfffff & mcmhdr >> 4; }
167   Int_t Check(UInt_t mcmhdr) const { return 0xf & mcmhdr; }
168   Int_t CouldBeMCMhdr(UInt_t mcmhdr) const { return ((0xf & mcmhdr) == 0xc); }
169
170   Int_t GetMCMReadoutPos(Int_t mcm) const { return (mcm > -1 && mcm < 16) ? fgkMcmOrder[mcm] : -1; }
171   Int_t GetROBReadoutPos(Int_t rob) const { return (rob > -1 && rob < 4) ? fgkRobOrder[rob] : -1; }
172
173   // ADC mask decoding
174   Int_t GetActiveChannels(UInt_t adcmask) const { return 0x1fffff & adcmask >> 4; }
175   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; }
176   Int_t GetNActiveChannels(UInt_t adcmask) const { return (0x1f & ~(adcmask >> 25)); }
177   Int_t CouldBeADCmask(UInt_t adcmask) const { return ((0xf & adcmask) == 0xc && (0x3 & adcmask >> 30) == 0x1); }
178       
179   // error message generation
180   void EquipmentError(ErrorCode_t err = kUnknown, const char *const msg = "", ...); 
181   void StackError    (ErrorCode_t err = kUnknown, const char *const msg = "", ...);     
182   void LinkError     (ErrorCode_t err = kUnknown, const char *const msg = "", ...); 
183   void ROBError      (ErrorCode_t err = kUnknown, const char *const msg = "", ...); 
184   void MCMError      (ErrorCode_t err = kUnknown, const char *const msg = "", ...); 
185   void StoreErrorTree() { fErrors->Fill(); }
186   void ForgetError() { return; }
187   void (AliTRDrawStream::*fStoreError)();       //! function pointer to method used for storing the error
188
189   static const char* fgErrorMessages[kLastErrorCode];     // error messages corresponding to the error codes
190   static const Int_t fgErrorDebugLevel[kLastErrorCode];   // error debug level
191   static       ErrorBehav_t fgErrorBehav[kLastErrorCode]; // bevhaviour in case of error of given type
192
193   // I/O
194   AliRawReader *fRawReader;                     // pointer to the raw reader to take the data from 
195   AliTRDdigitsManager *fDigitsManager;          // pointer to the digitsManager to fill the data
196   AliTRDdigitsParam   *fDigitsParam;            // pointer to the parameters belonging to the digits
197
198   TTree *fErrors;                               // tree containing the occured error codes
199   class AliTRDrawStreamError { 
200   public: 
201     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); 
202     Int_t fError;                               // error code
203     Int_t fSector;                              // sector
204     Int_t fStack;                               // stack
205     Int_t fLink;                                // link
206     Int_t fRob;                                 // ROB no
207     Int_t fMcm;                                 // MCM no
208     ClassDef(AliTRDrawStreamError, 1);
209   } fLastError;                                 // last error which occured
210   UInt_t fErrorFlags;                           // error flags used to steer subsequent reading
211   char   fErrorBuffer[100];                     // buffer for error message
212
213   UInt_t *fPayloadStart;                        // pointer to start of data payload
214   UInt_t *fPayloadCurr;                         // pointer to current reading position in the payload
215   Int_t   fPayloadSize;                         // size of the payload (in UInt_t words)
216
217   static const Int_t fgkNlinks;                 // number of links to read
218   static const Int_t fgkNstacks;                // number of stacks to read
219   static const UInt_t fgkDataEndmarker;         // data endmarker 
220   static const UInt_t fgkTrackletEndmarker;     // tracklet endmarker
221   static const Int_t fgkMcmOrder [];            // expected readout order of the MCMs
222   static const Int_t fgkRobOrder [];            // expected readout order of the ROBs
223
224   // persistent information
225   Int_t  fNtimebins;                            // number of timebins
226   Int_t  fLastEvId;                             // Event ID of last event 
227
228   // information valid at current reader position
229   // all the variables fCurr... refer to the value at the current
230   // reading position
231   Int_t fCurrSlot;                              // current slot 
232   Int_t fCurrLink;                              // current link
233   Int_t fCurrRobPos;                            // current ROB number
234   Int_t fCurrMcmPos;                            // current MCM number
235
236   // DDL header
237   UInt_t fCurrEquipmentId;                      // current Equipment ID
238
239   // SMU index header
240   UInt_t fCurrSmuIndexHeaderSize;               // current size of the SMU index header
241   UInt_t fCurrSmuIndexHeaderVersion;            // current version of the SMU index header
242   UInt_t fCurrTrackEnable;                      // current value of track enable
243   UInt_t fCurrTrackletEnable;                   // current value of tracklet enable
244   UInt_t fCurrStackMask;                        // current mask of active stacks
245
246   // Stack index header
247   UInt_t *fCurrStackIndexWord;                  // current stack index words
248   UInt_t *fCurrStackHeaderSize;                 // current stack index sizes
249   UInt_t *fCurrStackHeaderVersion;              // current stack header versions
250   UInt_t *fCurrLinkMask;                        // current link masks
251   UInt_t *fCurrCleanCheckout;                   // current clean checkout flags
252   UInt_t *fCurrBoardId;                         // current board IDs
253   UInt_t *fCurrHwRev;                           // current hardware revision
254   UInt_t *fCurrLinkMonitorFlags;                // current link monitor flags
255   UInt_t *fCurrLinkDataTypeFlags;               // current link data flags
256   UInt_t *fCurrLinkDebugFlags;                  // current link debug flags
257
258   // HC information
259   Int_t fCurrSpecial;                           // current value of the special flag
260   Int_t fCurrMajor;                             // current major version
261   Int_t fCurrMinor;                             // current minor version
262   Int_t fCurrAddHcWords;                        // current number of additional HC-header words
263   Int_t fCurrSm;                                // current sector
264   Int_t fCurrStack;                             // current stack
265   Int_t fCurrLayer;                             // current layer
266   Int_t fCurrSide;                              // current side
267   Int_t fCurrHC;                                // current HC
268   Int_t fCurrCheck;                             // current check bits
269   Int_t fCurrNtimebins;                         // current number of timebins
270   Int_t fCurrBC;                                // current BC
271   Int_t fCurrPtrgCnt;                           // current pretrigger count
272   Int_t fCurrPtrgPhase;                         // current pretrigger phase
273
274   // settings for dumping
275   Int_t fDumpMCM[100];                          // MCMs to dump
276   Int_t fNDumpMCMs;                             // number of MCMs to dump
277
278   // tracklet information
279   TClonesArray *fTrackletArray;                 // pointer to array for tracklet storage
280
281   // output data
282   AliTRDarrayADC *fAdcArray;                    // pointer to ADC array
283   AliTRDSignalIndex *fSignalIndex;              // pointer to the signal index
284   TTree *fTrackletTree;                         // pointer to the tree for tracklet storage
285
286   AliTRDrawStream(const AliTRDrawStream&);           // not implemented
287   AliTRDrawStream& operator=(const AliTRDrawStream&); // not implemented
288
289   ClassDef(AliTRDrawStream, 0);
290 };
291
292 Int_t AliTRDrawStream::GetNActiveChannelsFromMask(UInt_t adcmask) const
293 {
294   // return number of active bits in the ADC mask
295
296   adcmask = GetActiveChannels(adcmask);
297   adcmask = adcmask - ((adcmask >> 1) & 0x55555555);
298   adcmask = (adcmask & 0x33333333) + ((adcmask >> 2) & 0x33333333);
299   return (((adcmask + (adcmask >> 4)) & 0xF0F0F0F) * 0x1010101) >> 24;
300 }
301
302 #endif