]> git.uio.no Git - u/mrichter/AliRoot.git/blob - TRD/AliTRDrawData.cxx
Faster version of raw data reading + raw-data error log
[u/mrichter/AliRoot.git] / TRD / AliTRDrawData.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //  TRD raw data conversion class                                            //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <Riostream.h>
25 #include <TMath.h>
26
27 #include "AliDAQ.h"
28 #include "AliRawDataHeader.h"
29 #include "AliRawReader.h"
30 #include "AliLog.h"
31
32 #include "AliTRDrawData.h"
33 #include "AliTRDdigitsManager.h"
34 #include "AliTRDgeometry.h"
35 #include "AliTRDdataArrayI.h"
36 #include "AliTRDRawStream.h"
37
38 #include "AliTRDCommonParam.h"
39 #include "AliTRDcalibDB.h"
40
41 ClassImp(AliTRDrawData)
42
43 //_____________________________________________________________________________
44 AliTRDrawData::AliTRDrawData()
45   :TObject()
46   ,fRawVersion(2)    // Default Raw Data version set here
47   ,fCommonParam(0)
48   ,fCalibration(0)
49   ,fGeo(0)
50   ,fNumberOfDDLs(0)
51 {
52   //
53   // Default constructor
54   //
55
56 }
57
58 //_____________________________________________________________________________
59 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
60   :TObject(r)
61   ,fRawVersion(2)    // Default Raw Data version set here
62   ,fCommonParam(0)
63   ,fCalibration(0)
64   ,fGeo(0)
65   ,fNumberOfDDLs(0)
66 {
67   //
68   // Copy constructor
69   //
70
71 }
72
73 //_____________________________________________________________________________
74 AliTRDrawData::~AliTRDrawData()
75 {
76   //
77   // Destructor
78   //
79
80 }
81
82 //_____________________________________________________________________________
83 Bool_t AliTRDrawData::SetRawVersion(Int_t v)
84 {
85   //
86   // Set the raw data version (Currently only version 0, 1 and 2 are available)
87   //
88
89   if ( (v >= 0) && (v <= 2) ) {
90     fRawVersion = v;
91     return kTRUE;
92   }
93
94   return kFALSE;
95
96 }
97
98 //_____________________________________________________________________________
99 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
100 {
101   //
102   // Initialize necessary parameters and call one
103   // of the raw data simulator selected by SetRawVersion.
104   //
105   // Currently tracklet output is not spported yet and it
106   // will be supported in higher version simulator.
107   //
108
109   fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
110
111   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
112
113   if (!digitsManager->ReadDigits(digitsTree)) {
114     delete digitsManager;
115     return kFALSE;
116   }
117
118   if (tracks != NULL) {
119     delete digitsManager;
120     AliError("Tracklet input is not supported yet.");
121     return kFALSE;
122   }
123
124   fGeo = new AliTRDgeometry();
125
126   fCommonParam = AliTRDCommonParam::Instance();
127   if (!fCommonParam) {
128     AliError("Could not get common params");
129     delete fGeo;
130     delete digitsManager;
131     return kFALSE;
132   }
133
134   fCalibration = AliTRDcalibDB::Instance();
135   if (!fCalibration) {
136     AliError("Could not get calibration object");
137     delete fGeo;
138     delete digitsManager;
139     return kFALSE;
140   }
141
142   Int_t retval = kTRUE;
143
144   // Call appropriate Raw Simulator
145   if ( fRawVersion > 0 && fRawVersion <= 2 ) retval = Digits2Raw(digitsManager); 
146   else {
147     retval = kFALSE;
148     AliWarning(Form("Unsupported raw version (fRawVersion=%d).",fRawVersion));
149   }
150
151   // Cleanup
152   delete fGeo;
153   delete digitsManager;
154
155   return retval;
156
157 }
158
159 //_____________________________________________________________________________
160 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
161 {
162   //
163   // Raw data simulator for all versions > 0. This is prepared for real data.
164   // This version simulate only raw data with ADC data and not with tracklet.
165   //
166
167   const Int_t kMaxHcWords = (fGeo->TBmax()/3)*fGeo->ADCmax()*fGeo->MCMmax()*fGeo->ROBmaxC1()/2 + 100 + 20;
168
169   // Buffer to temporary store half chamber data
170   UInt_t     *hc_buffer   = new UInt_t[kMaxHcWords];
171
172   // sect is same as iDDL, so I use only sect here.
173   for (Int_t sect = 0; sect < fGeo->Nsect(); sect++) { 
174
175     char name[1024];
176     sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
177
178 #ifndef __DECCXX
179     ofstream *of = new ofstream(name, ios::binary);
180 #else
181     ofstream *of = new ofstream(name);
182 #endif
183
184     // Write a dummy data header
185     AliRawDataHeader  header;  // the event header
186     UInt_t hpos = of->tellp();
187     of->write((char *) (& header), sizeof(header));
188
189     // Reset payload byte size (payload does not include header).
190     Int_t npayloadbyte = 0;
191
192     // GTU common data header (5x4 bytes per super module, shows link mask)
193     for( Int_t cham = 0; cham < fGeo->Ncham(); cham++ ) {
194       UInt_t GtuCdh = (UInt_t)(0xe << 28);
195       for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
196         Int_t iDet = fGeo->GetDetector(plan, cham, sect);
197         // If chamber status is ok, we assume that the optical link is also OK.
198         // This is shown in the GTU link mask.
199         if ( fCalibration->GetChamberStatus(iDet) )
200           GtuCdh = GtuCdh | (3 << (2*plan));
201       }
202       of->write((char *) (& GtuCdh), sizeof(GtuCdh));
203       npayloadbyte += 4;
204     }
205
206     // Prepare chamber data
207     for( Int_t cham = 0; cham < fGeo->Ncham(); cham++) {
208       for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
209
210         Int_t iDet = fGeo->GetDetector(plan,cham,sect);
211
212         // Get the digits array
213         AliTRDdataArrayI *digits = digitsManager->GetDigits(iDet);
214         digits->Expand();
215
216         Int_t hcwords = 0;
217
218         // Process A side of the chamber
219         if ( fRawVersion >= 1 && fRawVersion <= 2 ) hcwords = ProduceHcDataV1andV2(digits,0,iDet,hc_buffer,kMaxHcWords);
220         of->write((char *) hc_buffer, hcwords*4);
221         npayloadbyte += hcwords*4;
222
223         // Process B side of the chamber
224         if ( fRawVersion >= 1 && fRawVersion <= 2 ) hcwords = ProduceHcDataV1andV2(digits,1,iDet,hc_buffer,kMaxHcWords);
225         of->write((char *) hc_buffer, hcwords*4);
226         npayloadbyte += hcwords*4;
227
228       }
229     }
230
231     // Complete header
232     header.fSize = UInt_t(of->tellp()) - hpos;
233     header.SetAttribute(0);  // Valid data
234     of->seekp(hpos);         // Rewind to header position
235     of->write((char *) (& header), sizeof(header));
236     of->close();
237     delete of;
238
239   }
240
241   delete hc_buffer;
242   return kTRUE;
243
244 }
245
246 //_____________________________________________________________________________
247 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
248                                         , Int_t det, UInt_t *buf, Int_t maxSize)
249 {
250   //
251   // This function simulates: 1) SM-I commissiong data Oct. 06 (fRawVersion == 1).
252   //                          2) Full Raw Production Version   (fRawVersion == 2)
253   //
254   // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
255   // where
256   //
257   //   side=0 means A side with ROB positions 0, 2, 4, 6.
258   //   side=1 means B side with ROB positions 1, 3, 5, 7.
259   //
260   // Chamber type (C0 orC1) is determined by "det" automatically.
261   // Appropriate size of buffer (*buf) must be prepared prior to calling this function.
262   // Pointer to the buffer and its size must be given to "buf" and "maxSize".
263   // Return value is the number of valid data filled in the buffer in unit of 32 bits
264   // UInt_t words.
265   // If buffer size if too small, the data is truncated with the buffer size however
266   // the function will finish without crash (this behaviour is similar to the MCM).
267   //
268
269   Int_t          nw = 0;                       // Number of written    words
270   Int_t          of = 0;                       // Number of overflowed words
271   Int_t        plan = fGeo->GetPlane( det );   // Plane
272   Int_t        cham = fGeo->GetChamber( det ); // Chamber
273   Int_t        sect = fGeo->GetSector( det );  // Sector (=iDDL)
274   Int_t        nRow = fCommonParam->GetRowMax( plan, cham, sect );
275   Int_t        nCol = fCommonParam->GetColMax( plan );
276   const Int_t nTBin = fCalibration->GetNumberOfTimeBins();
277   Int_t      kCtype = 0;                       // Chamber type (0:C0, 1:C1)
278   Int_t         iEv = 0xA;                     // Event ID. Now fixed to 10, how do I get event id?
279   UInt_t          x = 0;                       // General used number
280
281   // Check the nCol and nRow.
282   if ((nCol == 144) && 
283       (nRow == 16 || nRow == 12)) {
284     kCtype = (nRow-12) / 4;
285   } 
286   else {
287     AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
288                  ,nRow,nCol));
289     return 0;
290   }
291
292   AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
293                  ,sect,plan,cham,side));
294
295   // Tracklet should be processed here but not implemented yet
296
297   // Write end of tracklet marker
298   if (nw < maxSize) {
299     buf[nw++] = kEndoftrackletmarker;
300   } 
301   else {
302     of++;
303   }
304
305   // Half Chamber header
306   if      ( fRawVersion == 1 ) {
307     // Now it is the same version as used in SM-I commissioning.
308     Int_t  dcs = det+100;      // DCS Serial (in simulation, it is meaningless
309     x = (dcs<<20) | (sect<<15) | (plan<<12) | (cham<<9) | (side<<8) | 1;
310     if (nw < maxSize) {
311       buf[nw++] = x; 
312     }
313     else {
314       of++;
315     }
316   } 
317   else if ( fRawVersion == 2 ) {
318     // h[0] (there are 3 HC header)
319     Int_t minorv = 0;      // The minor version number
320     Int_t add    = 1;      // The number of additional header words to follow
321     x = (1<<31) | (fRawVersion<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
322     if (nw < maxSize) {
323       buf[nw++] = x; 
324     }
325     else {
326       of++;
327     }
328     // h[1]
329     Int_t bc_ctr   = 99; // bunch crossing counter. Here it is set to 99 always for no reason
330     Int_t pt_ctr   = 15; // pretrigger counter. Here it is set to 15 always for no reason
331     Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
332     x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
333     if (nw < maxSize) {
334       buf[nw++] = x; 
335     }
336     else {
337       of++;
338     }
339     // h[2]
340     Int_t ped_setup       = 1;    // Pedestal filter setup (0:1). Here it is always 1 for no reason
341     Int_t gain_setup      = 1;    // Gain filter setup (0:1). Here it is always 1 for no reason
342     Int_t tail_setup      = 1;    // Tail filter setup (0:1). Here it is always 1 for no reason
343     Int_t xt_setup        = 0;    // Cross talk filter setup (0:1). Here it is always 0 for no reason
344     Int_t nonlin_setup    = 0;    // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
345     Int_t bypass_setup    = 0;    // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
346     Int_t common_additive = 10;   // Digital filter common additive (0:63). Here it is always 10 for no reason
347     x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
348       | (bypass_setup<<26) | (common_additive<<20) | 1;
349     if (nw < maxSize) {
350       buf[nw++] = x; 
351     }
352     else {
353       of++;
354     }
355   }
356
357   // Scan for ROB and MCM
358   for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
359     Int_t iRob = iRobRow * 2 + side;
360     for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
361       Int_t padrow = iRobRow * 4 + iMcm / 4;
362
363       // MCM header
364       x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
365       if (nw < maxSize) {
366         buf[nw++] = x; 
367       }
368       else {
369         of++;
370       }
371
372       // ADC data
373       for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
374         Int_t padcol = fGeo->GetPadColFromADC(iRob, iMcm, iAdc);
375         UInt_t aa = !(iAdc & 1) + 2;
376         UInt_t *a = new UInt_t[nTBin+2];
377         // 3 timebins are packed into one 32 bits word
378         for (Int_t iT = 0; iT < nTBin; iT+=3) { 
379           if ((padcol >=    0) && (padcol <  nCol)) {
380             a[iT  ] = ((iT    ) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT    ) : 0;
381             a[iT+1] = ((iT + 1) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 1) : 0;
382             a[iT+2] = ((iT + 2) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 2) : 0; 
383           } 
384           else {
385             a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
386           }
387           x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
388           if (nw < maxSize) {
389             buf[nw++] = x; 
390           }
391           else {
392             of++;
393           }
394         }
395         // Diagnostics
396         Float_t avg = 0;
397         Float_t rms = 0;
398         for (Int_t iT = 0; iT < nTBin; iT++) {
399           avg += (Float_t) (a[iT]);
400         }
401         avg /= (Float_t) nTBin;
402         for (Int_t iT = 0; iT < nTBin; iT++) {
403           rms += ((Float_t) (a[iT]) - avg) * ((Float_t) (a[iT]) - avg);
404         }
405         rms = TMath::Sqrt(rms / (Float_t) nTBin);
406         if (rms > 1.7) {
407           AliDebug(2,Form("Large RMS (>1.7)  (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
408                           ,iRob,iMcm,iAdc,avg,rms));
409         }
410         delete a;
411       }
412     }
413   }
414
415   // Write end of raw data marker
416   if (nw < maxSize) {
417     buf[nw++] = kEndofrawdatamarker; 
418   }
419   else {
420     of++;
421   }
422   if (of != 0) {
423     AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
424   }
425
426   return nw;
427
428 }
429
430 //_____________________________________________________________________________
431 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
432 {
433   //
434   // Vx of the raw data reading
435   //
436
437   AliTRDdataArrayI *digits = 0;
438   AliTRDdataArrayI *track0 = 0;
439   AliTRDdataArrayI *track1 = 0;
440   AliTRDdataArrayI *track2 = 0; 
441
442   // Create the digits manager
443   AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
444   digitsManager->CreateArrays();
445
446   AliTRDRawStream input(rawReader);
447   input.SetRawVersion( fRawVersion );
448   input.Init();
449
450   // Loop through the digits
451   Int_t lastdet = -1;
452   Int_t det    = 0;
453   Int_t it = 0;
454   while (input.Next()) 
455     {
456
457       det    = input.GetDet();
458
459       if (det != lastdet) 
460         {       
461         
462           lastdet = det;
463
464           if (digits) digits->Compress(1,0);
465           if (track0) track0->Compress(1,0);
466           if (track1) track1->Compress(1,0);
467           if (track2) track2->Compress(1,0);
468         
469           // Add a container for the digits of this detector
470           digits = digitsManager->GetDigits(det);
471           track0 = digitsManager->GetDictionary(det,0);
472           track1 = digitsManager->GetDictionary(det,1);
473           track2 = digitsManager->GetDictionary(det,2);
474
475           // Allocate memory space for the digits buffer
476           if (digits->GetNtime() == 0) 
477             {
478               digits->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
479               track0->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
480               track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
481               track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
482             }
483         }
484     
485       for (it = 0; it < 3; it++)
486         {
487           if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
488             {
489               digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
490                                        input.GetTimeBin() + it, input.GetSignals()[it]);
491               track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
492                                        input.GetTimeBin() + it, 0);
493               track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
494                                        input.GetTimeBin() + it, 0);
495               track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
496                                        input.GetTimeBin() + it, 0);
497
498             }
499         }
500   }
501
502   if (digits) digits->Compress(1,0);
503   if (track0) track0->Compress(1,0);
504   if (track1) track1->Compress(1,0);
505   if (track2) track2->Compress(1,0);
506
507   return digitsManager;
508
509 }