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