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