1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////////////////
20 // TRD raw data conversion class //
22 ///////////////////////////////////////////////////////////////////////////////
24 #include <Riostream.h>
28 #include "AliRawDataHeader.h"
29 #include "AliRawReader.h"
32 #include "AliTRDrawData.h"
33 #include "AliTRDdigitsManager.h"
34 #include "AliTRDgeometry.h"
35 #include "AliTRDdataArrayI.h"
36 #include "AliTRDRawStream.h"
38 #include "AliTRDcalibDB.h"
39 #include "AliFstream.h"
41 #include "AliTRDSignalIndex.h"
43 #include "AliTRDfeeParam.h"
44 #include "AliTRDmcmSim.h"
46 ClassImp(AliTRDrawData)
48 //_____________________________________________________________________________
49 AliTRDrawData::AliTRDrawData()
56 // Default constructor
58 fFee = AliTRDfeeParam::Instance();
59 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
62 //_____________________________________________________________________________
63 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
72 fFee = AliTRDfeeParam::Instance();
73 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
76 //_____________________________________________________________________________
77 AliTRDrawData::~AliTRDrawData()
84 //_____________________________________________________________________________
85 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
88 // Initialize necessary parameters and call one
89 // of the raw data simulator selected by SetRawVersion.
91 // Currently tracklet output is not spported yet and it
92 // will be supported in higher version simulator.
95 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
97 if (!digitsManager->ReadDigits(digitsTree)) {
102 if (tracks != NULL) {
103 delete digitsManager;
104 AliError("Tracklet input is not supported yet.");
108 fGeo = new AliTRDgeometry();
110 if (!AliTRDcalibDB::Instance()) {
111 AliError("Could not get calibration object");
113 delete digitsManager;
117 Int_t retval = kTRUE;
118 Int_t rv = fFee->GetRAWversion();
120 // Call appropriate Raw Simulator
121 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
124 AliWarning(Form("Unsupported raw version (%d).", rv));
129 delete digitsManager;
135 //_____________________________________________________________________________
136 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
139 // Raw data simulator for all versions > 0. This is prepared for real data.
140 // This version simulate only raw data with ADC data and not with tracklet.
143 const Int_t kMaxHcWords = (fGeo->TBmax()/3)*fGeo->ADCmax()*fGeo->MCMmax()*fGeo->ROBmaxC1()/2 + 100 + 20;
145 // Buffer to temporary store half chamber data
146 UInt_t *hc_buffer = new UInt_t[kMaxHcWords];
148 // sect is same as iDDL, so I use only sect here.
149 for (Int_t sect = 0; sect < fGeo->Nsect(); sect++) {
152 sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
154 AliFstream* of = new AliFstream(name);
156 // Write a dummy data header
157 AliRawDataHeader header; // the event header
158 UInt_t hpos = of->Tellp();
159 of->WriteBuffer((char *) (& header), sizeof(header));
161 // Reset payload byte size (payload does not include header).
162 Int_t npayloadbyte = 0;
164 // GTU common data header (5x4 bytes per super module, shows link mask)
165 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++ ) {
166 UInt_t GtuCdh = (UInt_t)(0xe << 28);
167 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
168 Int_t iDet = fGeo->GetDetector(plan, cham, sect);
169 // If chamber status is ok, we assume that the optical link is also OK.
170 // This is shown in the GTU link mask.
171 if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
172 GtuCdh = GtuCdh | (3 << (2*plan));
174 of->WriteBuffer((char *) (& GtuCdh), sizeof(GtuCdh));
178 // Prepare chamber data
179 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++) {
180 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
182 Int_t iDet = fGeo->GetDetector(plan,cham,sect);
184 // Get the digits array
185 AliTRDdataArrayI *digits = digitsManager->GetDigits(iDet);
189 Int_t rv = fFee->GetRAWversion();
191 // Process A side of the chamber
192 if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,0,iDet,hc_buffer,kMaxHcWords);
193 if ( rv == 3 ) hcwords = ProduceHcDataV3 (digits,0,iDet,hc_buffer,kMaxHcWords);
195 of->WriteBuffer((char *) hc_buffer, hcwords*4);
196 npayloadbyte += hcwords*4;
198 // Process B side of the chamber
199 if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,1,iDet,hc_buffer,kMaxHcWords);
200 if ( rv >= 3 ) hcwords = ProduceHcDataV3 (digits,1,iDet,hc_buffer,kMaxHcWords);
202 of->WriteBuffer((char *) hc_buffer, hcwords*4);
203 npayloadbyte += hcwords*4;
208 header.fSize = UInt_t(of->Tellp()) - hpos;
209 header.SetAttribute(0); // Valid data
210 of->Seekp(hpos); // Rewind to header position
211 of->WriteBuffer((char *) (& header), sizeof(header));
220 //_____________________________________________________________________________
221 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
222 , Int_t det, UInt_t *buf, Int_t maxSize)
225 // This function simulates: 1) SM-I commissiong data Oct. 06 (Raw Version == 1).
226 // 2) Full Raw Production Version (Raw Version == 2)
228 // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
231 // side=0 means A side with ROB positions 0, 2, 4, 6.
232 // side=1 means B side with ROB positions 1, 3, 5, 7.
234 // Chamber type (C0 orC1) is determined by "det" automatically.
235 // Appropriate size of buffer (*buf) must be prepared prior to calling this function.
236 // Pointer to the buffer and its size must be given to "buf" and "maxSize".
237 // Return value is the number of valid data filled in the buffer in unit of 32 bits
239 // If buffer size if too small, the data is truncated with the buffer size however
240 // the function will finish without crash (this behaviour is similar to the MCM).
243 Int_t nw = 0; // Number of written words
244 Int_t of = 0; // Number of overflowed words
245 Int_t plan = fGeo->GetPlane( det ); // Plane
246 Int_t cham = fGeo->GetChamber( det ); // Chamber
247 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
248 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
249 Int_t nCol = fGeo->GetColMax( plan );
250 const Int_t nTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
251 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
252 Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
253 UInt_t x = 0; // General used number
254 Int_t rv = fFee->GetRAWversion();
256 // Check the nCol and nRow.
258 (nRow == 16 || nRow == 12)) {
259 kCtype = (nRow-12) / 4;
262 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
267 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
268 ,sect,plan,cham,side));
270 // Tracklet should be processed here but not implemented yet
272 // Write end of tracklet marker
274 buf[nw++] = kEndoftrackletmarker;
280 // Half Chamber header
282 // Now it is the same version as used in SM-I commissioning.
283 Int_t dcs = det+100; // DCS Serial (in simulation, it is meaningless
284 x = (dcs<<20) | (sect<<15) | (plan<<12) | (cham<<9) | (side<<8) | 1;
292 else if ( rv == 2 ) {
293 // h[0] (there are 3 HC header)
294 Int_t minorv = 0; // The minor version number
295 Int_t add = 1; // The number of additional header words to follow
296 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
304 Int_t bc_ctr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
305 Int_t pt_ctr = 15; // pretrigger counter. Here it is set to 15 always for no reason
306 Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
307 x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
315 Int_t ped_setup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
316 Int_t gain_setup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
317 Int_t tail_setup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
318 Int_t xt_setup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
319 Int_t nonlin_setup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
320 Int_t bypass_setup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
321 Int_t common_additive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
322 x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
323 | (bypass_setup<<26) | (common_additive<<20) | 1;
332 // Scan for ROB and MCM
333 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
334 Int_t iRob = iRobRow * 2 + side;
335 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
336 Int_t padrow = iRobRow * 4 + iMcm / 4;
339 x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
348 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
349 Int_t padcol = fGeo->GetPadColFromADC(iRob, iMcm, iAdc);
350 UInt_t aa = !(iAdc & 1) + 2;
351 UInt_t *a = new UInt_t[nTBin+2];
352 // 3 timebins are packed into one 32 bits word
353 for (Int_t iT = 0; iT < nTBin; iT+=3) {
354 if ((padcol >= 0) && (padcol < nCol)) {
355 a[iT ] = ((iT ) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT ) : 0;
356 a[iT+1] = ((iT + 1) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 1) : 0;
357 a[iT+2] = ((iT + 2) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 2) : 0;
360 a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
362 x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
373 for (Int_t iT = 0; iT < nTBin; iT++) {
374 avg += (Float_t) (a[iT]);
376 avg /= (Float_t) nTBin;
377 for (Int_t iT = 0; iT < nTBin; iT++) {
378 rms += ((Float_t) (a[iT]) - avg) * ((Float_t) (a[iT]) - avg);
380 rms = TMath::Sqrt(rms / (Float_t) nTBin);
382 AliDebug(2,Form("Large RMS (>1.7) (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
383 ,iRob,iMcm,iAdc,avg,rms));
390 // Write end of raw data marker
392 buf[nw++] = kEndofrawdatamarker;
398 AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
406 //_____________________________________________________________________________
407 Int_t AliTRDrawData::ProduceHcDataV3(AliTRDdataArrayI *digits, Int_t side
408 , Int_t det, UInt_t *buf, Int_t maxSize)
411 // This function simulates: Raw Version == 3 (Zero Suppression Prototype)
414 Int_t nw = 0; // Number of written words
415 Int_t of = 0; // Number of overflowed words
416 Int_t plan = fGeo->GetPlane( det ); // Plane
417 Int_t cham = fGeo->GetChamber( det ); // Chamber
418 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
419 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
420 Int_t nCol = fGeo->GetColMax( plan );
421 const Int_t nTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
422 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
423 Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
424 UInt_t x = 0; // General used number
425 Int_t rv = fFee->GetRAWversion();
427 // Check the nCol and nRow.
429 (nRow == 16 || nRow == 12)) {
430 kCtype = (nRow-12) / 4;
433 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
438 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
439 ,sect,plan,cham,side));
441 // Tracklet should be processed here but not implemented yet
443 // Write end of tracklet marker
445 buf[nw++] = kEndoftrackletmarker;
451 // Half Chamber header
452 // h[0] (there are 3 HC header)
453 Int_t minorv = 0; // The minor version number
454 Int_t add = 1; // The number of additional header words to follow
455 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
463 Int_t bc_ctr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
464 Int_t pt_ctr = 15; // pretrigger counter. Here it is set to 15 always for no reason
465 Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
466 x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
474 Int_t ped_setup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
475 Int_t gain_setup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
476 Int_t tail_setup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
477 Int_t xt_setup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
478 Int_t nonlin_setup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
479 Int_t bypass_setup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
480 Int_t common_additive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
481 x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
482 | (bypass_setup<<26) | (common_additive<<20) | 1;
490 AliTRDmcmSim *mcm = new AliTRDmcmSim();
492 // Scan for ROB and MCM
493 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
494 Int_t iRob = iRobRow * 2 + side;
495 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
497 mcm->Init( det, iRob, iMcm );
498 Int_t padrow = mcm->GetRow();
500 // Copy ADC data to MCM simulator
501 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
502 Int_t padcol = mcm->GetCol( iAdc );
503 if ((padcol >= 0) && (padcol < nCol)) {
504 for (Int_t iT = 0; iT < nTBin; iT++) {
505 mcm->SetData( iAdc, iT, digits->GetDataUnchecked( padrow, padcol, iT) );
507 } else { // this means it is out of chamber, and masked ADC
508 mcm->SetDataPedestal( iAdc );
511 // Simulate process in MCM
512 mcm->Filter(); // Apply filter
513 mcm->ZSMapping(); // Calculate zero suppression mapping
515 // Write MCM data to buffer
516 Int_t temp_nw = mcm->ProduceRawStream( &buf[nw], maxSize - nw );
520 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
527 // Write end of raw data marker
529 buf[nw++] = kEndofrawdatamarker;
535 AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
542 //_____________________________________________________________________________
543 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
546 // Vx of the raw data reading
549 AliTRDdataArrayI *digits = 0;
550 AliTRDdataArrayI *track0 = 0;
551 AliTRDdataArrayI *track1 = 0;
552 AliTRDdataArrayI *track2 = 0;
554 AliTRDSignalIndex *indexes = 0;
555 // Create the digits manager
556 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
557 digitsManager->CreateArrays();
559 AliTRDRawStream input(rawReader);
560 input.SetRawVersion( fFee->GetRAWversion() );
563 // Loop through the digits
570 det = input.GetDet();
577 if (digits) digits->Compress(1,0);
578 if (track0) track0->Compress(1,0);
579 if (track1) track1->Compress(1,0);
580 if (track2) track2->Compress(1,0);
582 // Add a container for the digits of this detector
583 digits = digitsManager->GetDigits(det);
584 track0 = digitsManager->GetDictionary(det,0);
585 track1 = digitsManager->GetDictionary(det,1);
586 track2 = digitsManager->GetDictionary(det,2);
588 // Allocate memory space for the digits buffer
589 if (digits->GetNtime() == 0)
591 digits->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
592 track0->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
593 track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
594 track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
597 indexes = digitsManager->GetIndexes(det);
598 indexes->SetSM(input.GetSM());
599 indexes->SetStack(input.GetStack());
600 indexes->SetLayer(input.GetLayer());
601 indexes->SetDetNumber(det);
602 if (indexes->IsAllocated() == kFALSE)
603 indexes->Allocate(input.GetMaxRow(), input.GetMaxCol(), input.GetNumberOfTimeBins());
606 for (it = 0; it < 3; it++)
608 if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
610 if (input.GetSignals()[it] > 0)
612 digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
613 input.GetTimeBin() + it, input.GetSignals()[it]);
615 indexes->AddIndexTBin(input.GetRow(), input.GetCol(),
616 input.GetTimeBin() + it);
617 track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
618 input.GetTimeBin() + it, 0);
619 track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
620 input.GetTimeBin() + it, 0);
621 track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
622 input.GetTimeBin() + it, 0);
628 if (digits) digits->Compress(1,0);
629 if (track0) track0->Compress(1,0);
630 if (track1) track1->Compress(1,0);
631 if (track2) track2->Compress(1,0);
633 return digitsManager;