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