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 "AliTRDCommonParam.h"
39 #include "AliTRDcalibDB.h"
41 ClassImp(AliTRDrawData)
43 //_____________________________________________________________________________
44 AliTRDrawData::AliTRDrawData()
46 ,fRawVersion(2) // Default Raw Data version set here
53 // Default constructor
58 //_____________________________________________________________________________
59 AliTRDrawData::AliTRDrawData(const AliTRDrawData &r)
61 ,fRawVersion(2) // Default Raw Data version set here
73 //_____________________________________________________________________________
74 AliTRDrawData::~AliTRDrawData()
82 //_____________________________________________________________________________
83 Bool_t AliTRDrawData::SetRawVersion(Int_t v)
86 // Set the raw data version (Currently only version 0, 1 and 2 are available)
89 if ( (v >= 0) && (v <= 2) ) {
98 //_____________________________________________________________________________
99 Bool_t AliTRDrawData::Digits2Raw(TTree *digitsTree, TTree *tracks )
102 // Initialize necessary parameters and call one
103 // of the raw data simulator selected by SetRawVersion.
105 // Currently tracklet output is not spported yet and it
106 // will be supported in higher version simulator.
109 fNumberOfDDLs = AliDAQ::NumberOfDdls("TRD");
111 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
113 if (!digitsManager->ReadDigits(digitsTree)) {
114 delete digitsManager;
118 if (tracks != NULL) {
119 delete digitsManager;
120 AliError("Tracklet input is not supported yet.");
124 fGeo = new AliTRDgeometry();
126 fCommonParam = AliTRDCommonParam::Instance();
128 AliError("Could not get common params");
130 delete digitsManager;
134 fCalibration = AliTRDcalibDB::Instance();
136 AliError("Could not get calibration object");
138 delete digitsManager;
142 Int_t retval = kTRUE;
144 // Call appropriate Raw Simulator
145 if ( fRawVersion > 0 && fRawVersion <= 2 ) retval = Digits2Raw(digitsManager);
148 AliWarning(Form("Unsupported raw version (fRawVersion=%d).",fRawVersion));
153 delete digitsManager;
159 //_____________________________________________________________________________
160 Bool_t AliTRDrawData::Digits2Raw(AliTRDdigitsManager *digitsManager)
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.
167 const Int_t kMaxHcWords = (fGeo->TBmax()/3)*fGeo->ADCmax()*fGeo->MCMmax()*fGeo->ROBmaxC1()/2 + 100 + 20;
169 // Buffer to temporary store half chamber data
170 UInt_t *hc_buffer = new UInt_t[kMaxHcWords];
172 // sect is same as iDDL, so I use only sect here.
173 for (Int_t sect = 0; sect < fGeo->Nsect(); sect++) {
176 sprintf(name,"TRD_%d.ddl",sect + AliTRDRawStream::kDDLOffset);
179 ofstream *of = new ofstream(name, ios::binary);
181 ofstream *of = new ofstream(name);
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));
189 // Reset payload byte size (payload does not include header).
190 Int_t npayloadbyte = 0;
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));
202 of->write((char *) (& GtuCdh), sizeof(GtuCdh));
206 // Prepare chamber data
207 for( Int_t cham = 0; cham < fGeo->Ncham(); cham++) {
208 for( Int_t plan = 0; plan < fGeo->Nplan(); plan++) {
210 Int_t iDet = fGeo->GetDetector(plan,cham,sect);
212 // Get the digits array
213 AliTRDdataArrayI *digits = digitsManager->GetDigits(iDet);
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;
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;
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));
246 //_____________________________________________________________________________
247 Int_t AliTRDrawData::ProduceHcDataV1andV2(AliTRDdataArrayI *digits, Int_t side
248 , Int_t det, UInt_t *buf, Int_t maxSize)
251 // This function simulates: 1) SM-I commissiong data Oct. 06 (fRawVersion == 1).
252 // 2) Full Raw Production Version (fRawVersion == 2)
254 // Produce half chamber data (= an ORI data) for the given chamber (det) and side (side)
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.
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
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).
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
281 // Check the nCol and nRow.
283 (nRow == 16 || nRow == 12)) {
284 kCtype = (nRow-12) / 4;
287 AliError(Form("This type of chamber is not supported (nRow=%d, nCol=%d)."
292 AliDebug(1,Form("Producing raw data for sect=%d plan=%d cham=%d side=%d"
293 ,sect,plan,cham,side));
295 // Tracklet should be processed here but not implemented yet
297 // Write end of tracklet marker
299 buf[nw++] = kEndoftrackletmarker;
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;
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;
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;
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;
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;
364 x = ((iRob * fGeo->MCMmax() + iMcm) << 24) | ((iEv % 0x100000) << 4) | 0xC;
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;
385 a[iT] = a[iT+1] = a[iT+2] = 0; // This happenes at the edge of chamber (should be pedestal! How?)
387 x = (a[iT+2] << 22) | (a[iT+1] << 12) | (a[iT] << 2) | aa;
398 for (Int_t iT = 0; iT < nTBin; iT++) {
399 avg += (Float_t) (a[iT]);
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);
405 rms = TMath::Sqrt(rms / (Float_t) nTBin);
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));
415 // Write end of raw data marker
417 buf[nw++] = kEndofrawdatamarker;
423 AliWarning("Buffer overflow. Data is truncated. Please increase buffer size and recompile.");
430 //_____________________________________________________________________________
431 AliTRDdigitsManager *AliTRDrawData::Raw2Digits(AliRawReader *rawReader)
434 // Vx of the raw data reading
437 AliTRDdataArrayI *digits = 0;
438 AliTRDdataArrayI *track0 = 0;
439 AliTRDdataArrayI *track1 = 0;
440 AliTRDdataArrayI *track2 = 0;
442 // Create the digits manager
443 AliTRDdigitsManager* digitsManager = new AliTRDdigitsManager();
444 digitsManager->CreateArrays();
446 AliTRDRawStream input(rawReader);
447 input.SetRawVersion( fRawVersion );
450 // Loop through the digits
457 det = input.GetDet();
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);
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);
475 // Allocate memory space for the digits buffer
476 if (digits->GetNtime() == 0)
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());
485 for (it = 0; it < 3; it++)
487 if ( input.GetTimeBin() + it < input.GetNumberOfTimeBins() )
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);
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);
507 return digitsManager;