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 ///////////////////////////////////////////////////////////////////////////////
28 #include "AliRawDataHeaderSim.h"
29 #include "AliRawReader.h"
31 #include "AliFstream.h"
33 #include "AliTRDrawData.h"
34 #include "AliTRDdigitsManager.h"
35 #include "AliTRDgeometry.h"
36 #include "AliTRDdataArrayI.h"
37 #include "AliTRDdataArrayS.h"
38 #include "AliTRDrawStreamBase.h"
39 #include "AliTRDRawStream.h"
40 #include "AliTRDRawStreamV2.h"
41 #include "AliTRDcalibDB.h"
42 #include "AliTRDSignalIndex.h"
43 #include "AliTRDfeeParam.h"
44 #include "AliTRDmcmSim.h"
46 ClassImp(AliTRDrawData)
48 //_____________________________________________________________________________
49 AliTRDrawData::AliTRDrawData()
56 // Default constructor
59 fFee = AliTRDfeeParam::Instance();
60 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
64 //_____________________________________________________________________________
65 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
75 fFee = AliTRDfeeParam::Instance();
76 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
80 //_____________________________________________________________________________
81 AliTRDrawData::~AliTRDrawData()
89 //_____________________________________________________________________________
90 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
93 // Initialize necessary parameters and call one
94 // of the raw data simulator selected by SetRawVersion.
96 // Currently tracklet output is not spported yet and it
97 // will be supported in higher version simulator.
100 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
102 if (!digitsManager->ReadDigits(digitsTree)) {
103 delete digitsManager;
107 if (tracks != NULL) {
108 delete digitsManager;
109 AliError("Tracklet input is not supported yet.");
113 fGeo = new AliTRDgeometry();
115 if (!AliTRDcalibDB::Instance()) {
116 AliError("Could not get calibration object");
118 delete digitsManager;
122 Int_t retval = kTRUE;
123 Int_t rv = fFee->GetRAWversion();
125 // Call appropriate Raw Simulator
126 if ( rv > 0 && rv <= 3 ) retval = Digits2Raw(digitsManager);
129 AliWarning(Form("Unsupported raw version (%d).", rv));
134 delete digitsManager;
140 //_____________________________________________________________________________
141 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
144 // Raw data simulator for all versions > 0. This is prepared for real data.
145 // This version simulate only raw data with ADC data and not with tracklet.
148 const Int_t kMaxHcWords = (fGeo->TBmax()/3)
154 // Buffer to temporary store half chamber data
155 UInt_t *hcBuffer = new UInt_t[kMaxHcWords];
157 // sect is same as iDDL, so I use only sect here.
158 for (Int_t sect = 0; sect < fGeo->Nsect(); sect++) {
161 sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
163 AliFstream* of = new AliFstream(name);
165 // Write a dummy data header
166 AliRawDataHeaderSim header; // the event header
167 UInt_t hpos = of->Tellp();
168 of->WriteBuffer((char *) (& header), sizeof(header));
170 // Reset payload byte size (payload does not include header).
171 Int_t npayloadbyte = 0;
173 // GTU common data header (5x4 bytes per super module, shows link mask)
174 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++ ) {
175 UInt_t gtuCdh = (UInt_t)(0xe << 28);
176 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
177 Int_t iDet = fGeo->GetDetector(plan, cham, sect);
178 // If chamber status is ok, we assume that the optical link is also OK.
179 // This is shown in the GTU link mask.
180 if ( AliTRDcalibDB::Instance()->GetChamberStatus(iDet) )
181 gtuCdh = gtuCdh | (3 << (2*plan));
183 of->WriteBuffer((char *) (& gtuCdh), sizeof(gtuCdh));
187 // Prepare chamber data
188 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++) {
189 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
191 Int_t iDet = fGeo->GetDetector(plan,cham,sect);
193 // Get the digits array
194 AliTRDdataArrayS *digits = (AliTRDdataArrayS *) digitsManager->GetDigits(iDet);
195 if (digits->HasData()) {
200 Int_t rv = fFee->GetRAWversion();
202 // Process A side of the chamber
203 if ( rv >= 1 && rv <= 2 ) {
204 hcwords = ProduceHcDataV1andV2(digits,0,iDet,hcBuffer,kMaxHcWords);
207 hcwords = ProduceHcDataV3 (digits,0,iDet,hcBuffer,kMaxHcWords);
210 of->WriteBuffer((char *) hcBuffer, hcwords*4);
211 npayloadbyte += hcwords*4;
213 // Process B side of the chamber
214 if ( rv >= 1 && rv <= 2 ) {
215 hcwords = ProduceHcDataV1andV2(digits,1,iDet,hcBuffer,kMaxHcWords);
218 hcwords = ProduceHcDataV3 (digits,1,iDet,hcBuffer,kMaxHcWords);
221 of->WriteBuffer((char *) hcBuffer, hcwords*4);
222 npayloadbyte += hcwords*4;
230 header.fSize = UInt_t(of->Tellp()) - hpos;
231 header.SetAttribute(0); // Valid data
232 of->Seekp(hpos); // Rewind to header position
233 of->WriteBuffer((char *) (& header), sizeof(header));
243 //_____________________________________________________________________________
244 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayS *digits, Int_t side
245 , Int_t det, UInt_t *buf, Int_t maxSize)
248 // This function simulates: 1) SM-I commissiong data Oct. 06 (Raw Version == 1).
249 // 2) Full Raw Production Version (Raw Version == 2)
251 // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
254 // side=0 means A side with ROB positions 0, 2, 4, 6.
255 // side=1 means B side with ROB positions 1, 3, 5, 7.
257 // Chamber type (C0 orC1) is determined by "det" automatically.
258 // Appropriate size of buffer (*buf) must be prepared prior to calling this function.
259 // Pointer to the buffer and its size must be given to "buf" and "maxSize".
260 // Return value is the number of valid data filled in the buffer in unit of 32 bits
262 // If buffer size if too small, the data is truncated with the buffer size however
263 // the function will finish without crash (this behaviour is similar to the MCM).
266 Int_t nw = 0; // Number of written words
267 Int_t of = 0; // Number of overflowed words
268 Int_t plan = fGeo->GetPlane( det ); // Plane
269 Int_t cham = fGeo->GetChamber( det ); // Chamber
270 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
271 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
272 Int_t nCol = fGeo->GetColMax( plan );
273 const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
274 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
275 Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
276 UInt_t x = 0; // General used number
277 Int_t rv = fFee->GetRAWversion();
279 // Check the nCol and nRow.
281 (nRow == 16 || nRow == 12)) {
282 kCtype = (nRow-12) / 4;
285 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
290 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
291 ,sect,plan,cham,side));
293 // Tracklet should be processed here but not implemented yet
295 // Write end of tracklet marker
297 buf[nw++] = kEndoftrackletmarker;
303 // Half Chamber header
305 // Now it is the same version as used in SM-I commissioning.
306 Int_t dcs = det+100; // DCS Serial (in simulation, it is meaningless
307 x = (dcs<<20) | (sect<<15) | (plan<<12) | (cham<<9) | (side<<8) | 1;
315 else if ( rv == 2 ) {
316 // h[0] (there are 3 HC header)
317 Int_t minorv = 0; // The minor version number
318 Int_t add = 2; // The number of additional header words to follow
319 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
327 Int_t bcCtr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
328 Int_t ptCtr = 15; // pretrigger counter. Here it is set to 15 always for no reason
329 Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
330 x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1;
338 Int_t pedSetup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
339 Int_t gainSetup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
340 Int_t tailSetup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
341 Int_t xtSetup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
342 Int_t nonlinSetup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
343 Int_t bypassSetup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
344 Int_t commonAdditive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
345 x = (pedSetup<<31) | (gainSetup<<30) | (tailSetup<<29) | (xtSetup<<28) | (nonlinSetup<<27)
346 | (bypassSetup<<26) | (commonAdditive<<20) | 1;
355 // Scan for ROB and MCM
356 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
357 Int_t iRob = iRobRow * 2 + side;
358 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
359 Int_t padrow = iRobRow * 4 + iMcm / 4;
362 x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
371 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
372 Int_t padcol = fFee->GetPadColFromADC(iRob, iMcm, iAdc);
373 UInt_t aa = !(iAdc & 1) + 2;
374 UInt_t *a = new UInt_t[kNTBin+2];
375 // 3 timebins are packed into one 32 bits word
376 for (Int_t iT = 0; iT < kNTBin; iT+=3) {
377 if ((padcol >= 0) && (padcol < nCol)) {
378 a[iT ] = ((iT ) < kNTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT ) : 0;
379 a[iT+1] = ((iT + 1) < kNTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 1) : 0;
380 a[iT+2] = ((iT + 2) < kNTBin ) ? digits->GetDataUnchecked(padrow,padcol,iT + 2) : 0;
383 a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
385 x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
396 for (Int_t iT = 0; iT < kNTBin; iT++) {
397 avg += (Float_t) (a[iT]);
399 avg /= (Float_t) kNTBin;
400 for (Int_t iT = 0; iT < kNTBin; iT++) {
401 rms += ((Float_t) (a[iT]) - avg) * ((Float_t) (a[iT]) - avg);
403 rms = TMath::Sqrt(rms / (Float_t) kNTBin);
405 AliDebug(2,Form("Large RMS (>1.7) (ROB,MCM,ADC)=(%02d,%02d,%02d), avg=%03.1f, rms=%03.1f"
406 ,iRob,iMcm,iAdc,avg,rms));
413 // Write end of raw data marker
415 buf[nw++] = kEndofrawdatamarker;
421 AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
428 //_____________________________________________________________________________
429 Int_t AliTRDrawData::ProduceHcDataV3(AliTRDdataArrayS *digits, Int_t side
430 , Int_t det, UInt_t *buf, Int_t maxSize)
433 // This function simulates: Raw Version == 3 (Zero Suppression Prototype)
436 Int_t nw = 0; // Number of written words
437 Int_t of = 0; // Number of overflowed words
438 Int_t plan = fGeo->GetPlane( det ); // Plane
439 Int_t cham = fGeo->GetChamber( det ); // Chamber
440 Int_t sect = fGeo->GetSector( det ); // Sector (=iDDL)
441 Int_t nRow = fGeo->GetRowMax( plan, cham, sect );
442 Int_t nCol = fGeo->GetColMax( plan );
443 const Int_t kNTBin = AliTRDcalibDB::Instance()->GetNumberOfTimeBins();
444 Int_t kCtype = 0; // Chamber type (0:C0, 1:C1)
445 //Int_t iEv = 0xA; // Event ID. Now fixed to 10, how do I get event id?
446 UInt_t x = 0; // General used number
447 Int_t rv = fFee->GetRAWversion();
449 // Check the nCol and nRow.
451 (nRow == 16 || nRow == 12)) {
452 kCtype = (nRow-12) / 4;
455 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
460 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
461 ,sect,plan,cham,side));
463 // Tracklet should be processed here but not implemented yet
465 // Write end of tracklet marker
467 buf[nw++] = kEndoftrackletmarker;
473 // Half Chamber header
474 // h[0] (there are 3 HC header)
475 Int_t minorv = 0; // The minor version number
476 Int_t add = 2; // The number of additional header words to follow
477 x = (1<<31) | (rv<<24) | (minorv<<17) | (add<<14) | (sect<<9) | (plan<<6) | (cham<<3) | (side<<2) | 1;
485 Int_t bcCtr = 99; // bunch crossing counter. Here it is set to 99 always for no reason
486 Int_t ptCtr = 15; // pretrigger counter. Here it is set to 15 always for no reason
487 Int_t ptPhase = 11; // pretrigger phase. Here it is set to 11 always for no reason
488 x = (bcCtr<<16) | (ptCtr<<12) | (ptPhase<<8) | ((kNTBin-1)<<2) | 1;
496 Int_t pedSetup = 1; // Pedestal filter setup (0:1). Here it is always 1 for no reason
497 Int_t gainSetup = 1; // Gain filter setup (0:1). Here it is always 1 for no reason
498 Int_t tailSetup = 1; // Tail filter setup (0:1). Here it is always 1 for no reason
499 Int_t xtSetup = 0; // Cross talk filter setup (0:1). Here it is always 0 for no reason
500 Int_t nonlinSetup = 0; // Nonlinearity filter setup (0:1). Here it is always 0 for no reason
501 Int_t bypassSetup = 0; // Filter bypass (for raw data) setup (0:1). Here it is always 0 for no reason
502 Int_t commonAdditive = 10; // Digital filter common additive (0:63). Here it is always 10 for no reason
503 x = (pedSetup<<31) | (gainSetup<<30) | (tailSetup<<29) | (xtSetup<<28) | (nonlinSetup<<27)
504 | (bypassSetup<<26) | (commonAdditive<<20) | 1;
512 // Scan for ROB and MCM
513 for (Int_t iRobRow = 0; iRobRow < (kCtype + 3); iRobRow++ ) {
514 Int_t iRob = iRobRow * 2 + side;
515 for (Int_t iMcm = 0; iMcm < fGeo->MCMmax(); iMcm++ ) {
517 AliTRDmcmSim *mcm = new AliTRDmcmSim();
518 mcm->Init( det, iRob, iMcm );
519 Int_t padrow = mcm->GetRow();
521 // Copy ADC data to MCM simulator
522 for (Int_t iAdc = 0; iAdc < 21; iAdc++ ) {
523 Int_t padcol = mcm->GetCol( iAdc );
524 if ((padcol >= 0) && (padcol < nCol)) {
525 for (Int_t iT = 0; iT < kNTBin; iT++) {
526 mcm->SetData( iAdc, iT, digits->GetDataUnchecked( padrow, padcol, iT) );
528 } else { // this means it is out of chamber, and masked ADC
529 mcm->SetDataPedestal( iAdc );
532 // Simulate process in MCM
533 mcm->Filter(); // Apply filter
534 mcm->ZSMapping(); // Calculate zero suppression mapping
535 //mcm->DumpData( "trdmcmdata.txt", "RFZS" ); // debugging purpose
537 // Write MCM data to buffer
538 Int_t tempNw = mcm->ProduceRawStream( &buf[nw], maxSize - nw );
542 AliError(Form("Buffer overflow detected. Please increase the buffer size and recompile."));
552 // Write end of raw data marker
554 buf[nw++] = kEndofrawdatamarker;
560 AliError("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
567 //_____________________________________________________________________________
568 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
571 // Vx of the raw data reading
574 AliTRDdataArrayS *digits = 0;
575 AliTRDdataArrayI *track0 = 0;
576 AliTRDdataArrayI *track1 = 0;
577 AliTRDdataArrayI *track2 = 0;
579 //AliTRDSignalIndex *indexes = 0;
580 // Create the digits manager
581 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
582 digitsManager->CreateArrays();
584 //AliTRDRawStream input(rawReader);
585 // AliTRDRawStreamV2 input(rawReader);
586 // input.SetRawVersion( fFee->GetRAWversion() );
589 AliTRDrawStreamBase *pinput = AliTRDrawStreamBase::GetRawStream(rawReader);
590 AliTRDrawStreamBase &input = *pinput;
592 AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
594 // Loop through the digits
599 det = input.NextChamber(digitsManager);
603 digits = (AliTRDdataArrayS *) digitsManager->GetDigits(det);
604 track0 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,0);
605 track1 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,1);
606 track2 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,2);
608 if (digits) digits->Compress(1,0);
609 if (track0) track0->Compress(1,0);
610 if (track1) track1->Compress(1,0);
611 if (track2) track2->Compress(1,0);
618 return digitsManager;
621 //_____________________________________________________________________________
622 AliTRDdigitsManager *AliTRDrawData::Raw2DigitsOLD(AliRawReader *rawReader)
625 // Vx of the raw data reading
628 AliTRDdataArrayS *digits = 0;
629 AliTRDdataArrayI *track0 = 0;
630 AliTRDdataArrayI *track1 = 0;
631 AliTRDdataArrayI *track2 = 0;
633 AliTRDSignalIndex *indexes = 0;
634 // Create the digits manager
635 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
636 digitsManager->CreateArrays();
638 //AliTRDRawStream input(rawReader);
639 AliTRDRawStreamV2 input(rawReader);
640 input.SetRawVersion( fFee->GetRAWversion() );
643 AliInfo(Form("Stream version: %s", input.IsA()->GetName()));
645 // Loop through the digits
649 while (input.Next()) {
651 det = input.GetDet();
653 if (det != lastdet) { // If new detector found
657 if (digits) digits->Compress(1,0);
658 if (track0) track0->Compress(1,0);
659 if (track1) track1->Compress(1,0);
660 if (track2) track2->Compress(1,0);
662 // Add a container for the digits of this detector
663 digits = (AliTRDdataArrayS *) digitsManager->GetDigits(det);
664 track0 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,0);
665 track1 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,1);
666 track2 = (AliTRDdataArrayI *) digitsManager->GetDictionary(det,2);
668 // Allocate memory space for the digits buffer
669 if (digits->GetNtime() == 0)
671 digits->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
672 track0->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
673 track1->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
674 track2->Allocate(input.GetMaxRow(),input.GetMaxCol(), input.GetNumberOfTimeBins());
677 indexes = digitsManager->GetIndexes(det);
678 indexes->SetSM(input.GetSM());
679 indexes->SetStack(input.GetStack());
680 indexes->SetLayer(input.GetLayer());
681 indexes->SetDetNumber(det);
682 if (indexes->IsAllocated() == kFALSE)
683 indexes->Allocate(input.GetMaxRow(), input.GetMaxCol(), input.GetNumberOfTimeBins());
686 // 3 timebin data are stored per word
687 for (it = 0; it < 3; it++)
689 if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
691 if (input.GetSignals()[it] > 0)
693 digits->SetDataUnchecked(input.GetRow(), input.GetCol(),
694 input.GetTimeBin() + it, input.GetSignals()[it]);
696 indexes->AddIndexTBin(input.GetRow(), input.GetCol(),
697 input.GetTimeBin() + it);
698 track0->SetDataUnchecked(input.GetRow(), input.GetCol(),
699 input.GetTimeBin() + it, 0);
700 track1->SetDataUnchecked(input.GetRow(), input.GetCol(),
701 input.GetTimeBin() + it, 0);
702 track2->SetDataUnchecked(input.GetRow(), input.GetCol(),
703 input.GetTimeBin() + it, 0);
709 if (digits) digits->Compress(1,0);
710 if (track0) track0->Compress(1,0);
711 if (track1) track1->Compress(1,0);
712 if (track2) track2->Compress(1,0);
714 return digitsManager;