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>
29 #include "AliRawDataHeader.h"
30 #include "AliRawReader.h"
33 #include "AliTRDrawData.h"
34 #include "AliTRDdigitsManager.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDdataArrayI.h"
37 #include "AliTRDRawStream.h"
38 #include "AliTRDRawStreamV2.h"
40 #include "AliTRDcalibDB.h"
41 #include "AliFstream.h"
43 #include "AliTRDSignalIndex.h"
45 #include "AliTRDfeeParam.h"
46 #include "AliTRDmcmSim.h"
48 ClassImp(AliTRDrawData)
50 //_____________________________________________________________________________
51 AliTRDrawData::AliTRDrawData()
58 // Default constructor
60 fFee = AliTRDfeeParam::Instance();
61 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
64 //_____________________________________________________________________________
65 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
74 fFee = AliTRDfeeParam::Instance();
75 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
78 //_____________________________________________________________________________
79 AliTRDrawData::~AliTRDrawData()
86 //_____________________________________________________________________________
87 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
90 // Initialize necessary parameters and call one
91 // of the raw data simulator selected by SetRawVersion.
93 // Currently tracklet output is not spported yet and it
94 // will be supported in higher version simulator.
97 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
99 if (!digitsManager->ReadDigits(digitsTree)) {
100 delete digitsManager;
104 if (tracks != NULL) {
105 delete digitsManager;
106 AliError("Tracklet input is not supported yet.");
110 fGeo = new AliTRDgeometry();
112 if (!AliTRDcalibDB::Instance()) {
113 AliError("Could not get calibration object");
115 delete digitsManager;
119 Int_t retval = kTRUE;
120 Int_t rv = fFee->GetRAWversion();
122 // Call appropriate Raw Simulator
123 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
126 AliWarning(Form("Unsupported raw version (%d).", rv));
131 delete digitsManager;
137 //_____________________________________________________________________________
138 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
141 // Raw data simulator for all versions > 0. This is prepared for real data.
142 // This version simulate only raw data with ADC data and not with tracklet.
145 const Int_t kMaxHcWords = (fGeo->TBmax()/3)*fGeo->ADCmax()*fGeo->MCMmax()*fGeo->ROBmaxC1()/2 + 100 + 20;
147 // Buffer to temporary store half chamber data
148 UInt_t *hc_buffer = new UInt_t[kMaxHcWords];
150 // sect is same as iDDL, so I use only sect here.
151 for (Int_t sect = 0; sect < fGeo->Nsect(); sect++) {
154 sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
156 AliFstream* of = new AliFstream(name);
158 // Write a dummy data header
159 AliRawDataHeader header; // the event header
160 UInt_t hpos = of->Tellp();
161 of->WriteBuffer((char *) (& header), sizeof(header));
163 // Reset payload byte size (payload does not include header).
164 Int_t npayloadbyte = 0;
166 // GTU common data header (5x4 bytes per super module, shows link mask)
167 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++ ) {
168 UInt_t GtuCdh = (UInt_t)(0xe << 28);
169 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
170 Int_t iDet = fGeo->GetDetector(plan, cham, sect);
171 // If chamber status is ok, we assume that the optical link is also OK.
172 // This is shown in the GTU link mask.
173 if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
174 GtuCdh = GtuCdh | (3 << (2*plan));
176 of->WriteBuffer((char *) (& GtuCdh), sizeof(GtuCdh));
180 // Prepare chamber data
181 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++) {
182 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
184 Int_t iDet = fGeo->GetDetector(plan,cham,sect);
186 // Get the digits array
187 AliTRDdataArrayI *digits = digitsManager->GetDigits(iDet);
191 Int_t rv = fFee->GetRAWversion();
193 // Process A side of the chamber
194 if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,0,iDet,hc_buffer,kMaxHcWords);
195 if ( rv == 3 ) hcwords = ProduceHcDataV3 (digits,0,iDet,hc_buffer,kMaxHcWords);
197 of->WriteBuffer((char *) hc_buffer, hcwords*4);
198 npayloadbyte += hcwords*4;
200 // Process B side of the chamber
201 if ( rv >= 1 && rv <= 2 ) hcwords = ProduceHcDataV1andV2(digits,1,iDet,hc_buffer,kMaxHcWords);
202 if ( rv >= 3 ) hcwords = ProduceHcDataV3 (digits,1,iDet,hc_buffer,kMaxHcWords);
204 of->WriteBuffer((char *) hc_buffer, hcwords*4);
205 npayloadbyte += hcwords*4;
210 header.fSize = UInt_t(of->Tellp()) - hpos;
211 header.SetAttribute(0); // Valid data
212 of->Seekp(hpos); // Rewind to header position
213 of->WriteBuffer((char *) (& header), sizeof(header));
222 //_____________________________________________________________________________
223 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
224 , Int_t det, UInt_t *buf, Int_t maxSize)
227 // This function simulates: 1) SM-I commissiong data Oct. 06 (Raw Version == 1).
228 // 2) Full Raw Production Version (Raw Version == 2)
230 // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
233 // side=0 means A side with ROB positions 0, 2, 4, 6.
234 // side=1 means B side with ROB positions 1, 3, 5, 7.
236 // Chamber type (C0 orC1) is determined by "det" automatically.
237 // Appropriate size of buffer (*buf) must be prepared prior to calling this function.
238 // Pointer to the buffer and its size must be given to "buf" and "maxSize".
239 // Return value is the number of valid data filled in the buffer in unit of 32 bits
241 // If buffer size if too small, the data is truncated with the buffer size however
242 // the function will finish without crash (this behaviour is similar to the MCM).
245 Int_t nw = 0; // Number of written words
246 Int_t of = 0; // Number of overflowed words
247 Int_t plan = fGeo->GetPlane( det ); // Plane
248 Int_t cham = fGeo->GetChamber( det ); // Chamber
249 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
250 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
251 Int_t nCol = fGeo->GetColMax( plan );
252 const Int_t nTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
253 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
254 Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
255 UInt_t x = 0; // General used number
256 Int_t rv = fFee->GetRAWversion();
258 // Check the nCol and nRow.
260 (nRow == 16 || nRow == 12)) {
261 kCtype = (nRow-12) / 4;
264 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
269 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
270 ,sect,plan,cham,side));
272 // Tracklet should be processed here but not implemented yet
274 // Write end of tracklet marker
276 buf[nw++] = kEndoftrackletmarker;
282 // Half Chamber header
284 // Now it is the same version as used in SM-I commissioning.
285 Int_t dcs = det+100; // DCS Serial (in simulation, it is meaningless
286 x = (dcs<<20) | (sect<<15) | (plan<<12) | (cham<<9) | (side<<8) | 1;
294 else if ( rv == 2 ) {
295 // h[0] (there are 3 HC header)
296 Int_t minorv = 0; // The minor version number
297 Int_t add = 2; // The number of additional header words to follow
298 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
306 Int_t bc_ctr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
307 Int_t pt_ctr = 15; // pretrigger counter. Here it is set to 15 always for no reason
308 Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
309 x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
317 Int_t ped_setup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
318 Int_t gain_setup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
319 Int_t tail_setup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
320 Int_t xt_setup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
321 Int_t nonlin_setup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
322 Int_t bypass_setup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
323 Int_t common_additive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
324 x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
325 | (bypass_setup<<26) | (common_additive<<20) | 1;
334 // Scan for ROB and MCM
335 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
336 Int_t iRob = iRobRow * 2 + side;
337 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
338 Int_t padrow = iRobRow * 4 + iMcm / 4;
341 x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
350 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
351 Int_t padcol = fFee->GetPadColFromADC(iRob, iMcm, iAdc);
352 UInt_t aa = !(iAdc & 1) + 2;
353 UInt_t *a = new UInt_t[nTBin+2];
354 // 3 timebins are packed into one 32 bits word
355 for (Int_t iT = 0; iT < nTBin; iT+=3) {
356 if ((padcol >= 0) && (padcol < nCol)) {
357 a[iT ] = ((iT ) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT ) : 0;
358 a[iT+1] = ((iT + 1) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 1) : 0;
359 a[iT+2] = ((iT + 2) < nTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 2) : 0;
362 a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
364 x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
375 for (Int_t iT = 0; iT < nTBin; iT++) {
376 avg += (Float_t) (a[iT]);
378 avg /= (Float_t) nTBin;
379 for (Int_t iT = 0; iT < nTBin; iT++) {
380 rms += ((Float_t) (a[iT]) - avg) * ((Float_t) (a[iT]) - avg);
382 rms = TMath::Sqrt(rms / (Float_t) nTBin);
384 AliDebug(2,Form("Large RMS (>1.7) (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
385 ,iRob,iMcm,iAdc,avg,rms));
392 // Write end of raw data marker
394 buf[nw++] = kEndofrawdatamarker;
400 AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
408 //_____________________________________________________________________________
409 Int_t AliTRDrawData::ProduceHcDataV3(AliTRDdataArrayI *digits, Int_t side
410 , Int_t det, UInt_t *buf, Int_t maxSize)
413 // This function simulates: Raw Version == 3 (Zero Suppression Prototype)
416 Int_t nw = 0; // Number of written words
417 Int_t of = 0; // Number of overflowed words
418 Int_t plan = fGeo->GetPlane( det ); // Plane
419 Int_t cham = fGeo->GetChamber( det ); // Chamber
420 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
421 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
422 Int_t nCol = fGeo->GetColMax( plan );
423 const Int_t nTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
424 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
425 Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
426 UInt_t x = 0; // General used number
427 Int_t rv = fFee->GetRAWversion();
429 // Check the nCol and nRow.
431 (nRow == 16 || nRow == 12)) {
432 kCtype = (nRow-12) / 4;
435 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
440 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
441 ,sect,plan,cham,side));
443 // Tracklet should be processed here but not implemented yet
445 // Write end of tracklet marker
447 buf[nw++] = kEndoftrackletmarker;
453 // Half Chamber header
454 // h[0] (there are 3 HC header)
455 Int_t minorv = 0; // The minor version number
456 Int_t add = 2; // The number of additional header words to follow
457 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
465 Int_t bc_ctr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
466 Int_t pt_ctr = 15; // pretrigger counter. Here it is set to 15 always for no reason
467 Int_t pt_phase = 11; // pretrigger phase. Here it is set to 11 always for no reason
468 x = (bc_ctr<<16) | (pt_ctr<<12) | (pt_phase<<8) | ((nTBin-1)<<2) | 1;
476 Int_t ped_setup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
477 Int_t gain_setup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
478 Int_t tail_setup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
479 Int_t xt_setup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
480 Int_t nonlin_setup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
481 Int_t bypass_setup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
482 Int_t common_additive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
483 x = (ped_setup<<31) | (gain_setup<<30) | (tail_setup<<29) | (xt_setup<<28) | (nonlin_setup<<27)
484 | (bypass_setup<<26) | (common_additive<<20) | 1;
493 // Scan for ROB and MCM
494 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
495 Int_t iRob = iRobRow * 2 + side;
496 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
498 AliTRDmcmSim *mcm = new AliTRDmcmSim();
499 mcm->Init( det, iRob, iMcm );
500 Int_t padrow = mcm->GetRow();
502 // Copy ADC data to MCM simulator
503 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
504 Int_t padcol = mcm->GetCol( iAdc );
505 if ((padcol >= 0) && (padcol < nCol)) {
506 for (Int_t iT = 0; iT < nTBin; iT++) {
507 mcm->SetData( iAdc, iT, digits->GetDataUnchecked( padrow, padcol, iT) );
509 } else { // this means it is out of chamber, and masked ADC
510 mcm->SetDataPedestal( iAdc );
513 // Simulate process in MCM
514 mcm->Filter(); // Apply filter
515 mcm->ZSMapping(); // Calculate zero suppression mapping
516 //mcm->DumpData( "trdmcmdata.txt", "RFZS" ); // debugging purpose
518 // Write MCM data to buffer
519 Int_t temp_nw = mcm->ProduceRawStream( &buf[nw], maxSize - nw );
523 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
533 // Write end of raw data marker
535 buf[nw++] = kEndofrawdatamarker;
541 AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
548 //_____________________________________________________________________________
549 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
552 // Vx of the raw data reading
555 AliTRDdataArrayI *digits = 0;
556 AliTRDdataArrayI *track0 = 0;
557 AliTRDdataArrayI *track1 = 0;
558 AliTRDdataArrayI *track2 = 0;
560 AliTRDSignalIndex *indexes = 0;
561 // Create the digits manager
562 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
563 digitsManager->CreateArrays();
565 //AliTRDRawStream input(rawReader);
566 AliTRDRawStreamV2 input(rawReader);
567 input.SetRawVersion( fFee->GetRAWversion() );
570 AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
572 // Loop through the digits
576 while (input.Next()) {
578 det = input.GetDet();
580 if (det != lastdet) { // If new detector found
584 if (digits) digits->Compress(1,0);
585 if (track0) track0->Compress(1,0);
586 if (track1) track1->Compress(1,0);
587 if (track2) track2->Compress(1,0);
589 // Add a container for the digits of this detector
590 digits = digitsManager->GetDigits(det);
591 track0 = digitsManager->GetDictionary(det,0);
592 track1 = digitsManager->GetDictionary(det,1);
593 track2 = digitsManager->GetDictionary(det,2);
595 // Allocate memory space for the digits buffer
596 if (digits->GetNtime() == 0)
598 digits->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
599 track0->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
600 track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
601 track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
604 indexes = digitsManager->GetIndexes(det);
605 indexes->SetSM(input.GetSM());
606 indexes->SetStack(input.GetStack());
607 indexes->SetLayer(input.GetLayer());
608 indexes->SetDetNumber(det);
609 if (indexes->IsAllocated() == kFALSE)
610 indexes->Allocate(input.GetMaxRow(), input.GetMaxCol(), input.GetNumberOfTimeBins());
613 // 3 timebin data are stored per word
614 for (it = 0; it < 3; it++)
616 if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
618 if (input.GetSignals()[it] > 0)
620 digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
621 input.GetTimeBin() + it, input.GetSignals()[it]);
623 indexes->AddIndexTBin(input.GetRow(), input.GetCol(),
624 input.GetTimeBin() + it);
625 track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
626 input.GetTimeBin() + it, 0);
627 track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
628 input.GetTimeBin() + it, 0);
629 track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
630 input.GetTimeBin() + it, 0);
636 if (digits) digits->Compress(1,0);
637 if (track0) track0->Compress(1,0);
638 if (track1) track1->Compress(1,0);
639 if (track2) track2->Compress(1,0);
641 return digitsManager;