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 // Interface to the Altro format
19 // to read and write digits
20 // To be used in Alice Data Challenges
21 // and in the compression of the RAW data
23 #include "AliAltroBuffer.h"
24 #include "AliAltroMapping.h"
25 #include "AliRawDataHeaderSim.h"
27 #include "AliFstream.h"
31 ClassImp(AliAltroBuffer)
33 //_____________________________________________________________________________
34 AliAltroBuffer::AliAltroBuffer(const char* fileName, const AliAltroMapping *mapping):
43 //the buffer is cleaned
44 for (Int_t i = 0; i < 5; i++) fBuffer[i] = 0;
48 //open the output file
49 fFile = new AliFstream(fileName);
53 //_____________________________________________________________________________
54 AliAltroBuffer::~AliAltroBuffer()
58 //Flush out the Buffer content at the end only if Buffer wasn't completely filled
60 if (fVerbose) Info("~AliAltroBuffer", "File Created");
66 //_____________________________________________________________________________
67 AliAltroBuffer::AliAltroBuffer(const AliAltroBuffer& source):
69 fShift(source.fShift),
70 fCurrentCell(source.fCurrentCell),
71 fFreeCellBuffer(source.fFreeCellBuffer),
72 fVerbose(source.fVerbose),
74 fDataHeaderPos(source.fDataHeaderPos),
75 fMapping(source.fMapping)
79 Fatal("AliAltroBuffer", "copy constructor not implemented");
82 //_____________________________________________________________________________
83 AliAltroBuffer& AliAltroBuffer::operator = (const AliAltroBuffer& /*source*/)
87 Fatal("operator =", "assignment operator not implemented");
91 //_____________________________________________________________________________
92 void AliAltroBuffer::Flush()
94 // Flushes the Buffer content
95 if (fFreeCellBuffer != 16) {
96 Int_t temp = fFreeCellBuffer;
97 for (Int_t i = 0; i < temp; i++){
103 //_____________________________________________________________________________
104 void AliAltroBuffer::FillBuffer(Int_t val)
106 //Fills the Buffer with 16 ten bits words and write into a file
108 if ((val > 0x3FF) || (val < 0)) {
109 Error("FillBuffer", "Value out of range (10 bits): %d", val);
115 val = val >> (10-fShift);
116 fBuffer[fCurrentCell] |= val;
123 fBuffer[fCurrentCell] |= val;
125 //Buffer is written into a file
126 fFile->WriteBuffer((char*)fBuffer, sizeof(UInt_t)*5);
128 for (Int_t j = 0; j < 5; j++) fBuffer[j] = 0;
131 fFreeCellBuffer = 16;
136 //_____________________________________________________________________________
137 void AliAltroBuffer::WriteDummyTrailer(Int_t wordsNumber, Int_t padNumber,
138 Int_t rowNumber, Int_t secNumber)
140 //Writes a trailer of 40 bits
142 Int_t num = fFreeCellBuffer % 4;
143 for(Int_t i = 0; i < num; i++) {
146 FillBuffer(wordsNumber);
147 FillBuffer(padNumber);
148 FillBuffer(rowNumber);
149 FillBuffer(secNumber);
152 //_____________________________________________________________________________
153 void AliAltroBuffer::WriteTrailer(Int_t wordsNumber, Int_t padNumber,
154 Int_t rowNumber, Int_t secNumber)
156 //Writes a trailer of 40 bits
159 AliError("No ALTRO mapping information is loaded! Filling a dummy trailer!");
160 return WriteDummyTrailer(wordsNumber,padNumber,
161 rowNumber,secNumber);
164 Short_t hwAddress = fMapping->GetHWAddress(rowNumber,padNumber,secNumber);
166 AliFatal(Form("No hardware (ALTRO) adress found for these pad-row (%d) and pad (%d) indeces !",rowNumber,padNumber));
167 WriteTrailer(wordsNumber,hwAddress);
170 //_____________________________________________________________________________
171 void AliAltroBuffer::WriteTrailer(Int_t wordsNumber, Short_t hwAddress)
173 //Writes a trailer of 40 bits using
174 //a given hardware adress
175 Int_t num = fFreeCellBuffer % 4;
176 for(Int_t i = 0; i < num; i++) {
180 temp = hwAddress & 0x3FF;
183 temp = (wordsNumber << 6) & 0x3FF;
185 temp |= ((hwAddress >> 10) & 0x3);
189 temp |= ((wordsNumber & 0x3FF) >> 4);
196 //_____________________________________________________________________________
197 void AliAltroBuffer::WriteChannel(Int_t padNumber, Int_t rowNumber,
199 Int_t nTimeBins, const Int_t* adcValues,
202 //Write all ADC values and the trailer of a channel
203 Int_t nWords = WriteBunch(nTimeBins,adcValues,threshold);
205 WriteTrailer(nWords, padNumber, rowNumber, secNumber);
208 //_____________________________________________________________________________
209 void AliAltroBuffer::WriteChannel(Short_t hwAddress,
210 Int_t nTimeBins, const Int_t* adcValues,
213 //Write all ADC values and the trailer of a channel
214 Int_t nWords = WriteBunch(nTimeBins,adcValues,threshold);
216 WriteTrailer(nWords, hwAddress);
219 //_____________________________________________________________________________
220 Int_t AliAltroBuffer::WriteBunch(Int_t nTimeBins, const Int_t* adcValues,
223 //Write all ADC values
224 //Return number of words written
228 Int_t bunchLength = 0;
230 // loop over time bins
231 for (Int_t iTime = 0; iTime < nTimeBins; iTime++) {
232 if (adcValues[iTime] >= threshold) { // ADC value above threshold
233 FillBuffer(adcValues[iTime]);
238 } else if (timeBin >= 0) { // end of bunch
240 FillBuffer(bunchLength + 2);
247 if (timeBin >= 0) { // end of bunch
249 FillBuffer(bunchLength + 2);
256 //_____________________________________________________________________________
257 void AliAltroBuffer::WriteDataHeader(Bool_t dummy, Bool_t compressed)
259 //Write a (dummy or real) DDL data header,
260 //set the compression bit if compressed
262 AliRawDataHeaderSim header;
264 //if size=0 it means that this data header is a dummy data header
265 fDataHeaderPos = fFile->Tellp();
266 fFile->WriteBuffer((char*)(&header), sizeof(header));
268 UInt_t currentFilePos = fFile->Tellp();
269 fFile->Seekp(fDataHeaderPos);
270 header.fSize = currentFilePos-fDataHeaderPos;
271 header.SetAttribute(0); // valid data
272 if (compressed) header.SetAttribute(1);
273 fFile->WriteBuffer((char*)(&header), sizeof(header));
274 fFile->Seekp(currentFilePos);