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 // Class to write ADC values to a raw data file
22 // This class writes FMD Raw data to a file. The sample rate (number
23 // of times the ALTRO ADC samples each pre-amp. channel - that is,
24 // data from a single strip), can be set via SetSampleRate.
26 // Zero-suppression can be enabled by calling SetThreshold with a
27 // non-zero argument. ADC values less than the value set will not be
28 // written to output. Note, that if you use zero-suppression, you
29 // need to explicitly set the sample rate when reading back the data
30 // with AliFMDRawReader.
32 // This class uses the AliAltroBuffer class to write the data in the
33 // ALTRO format. See the Exec member function for more information on
36 #include <AliLog.h> // ALILOG_H
37 #include <AliLoader.h> // ALILOADER_H
38 #include <AliAltroBuffer.h> // ALIALTROBUFFER_H
39 #include "AliFMD.h" // ALIFMD_H
40 #include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
41 #include "AliFMDDigit.h" // ALIFMDDIGIT_H
42 #include "AliFMDRawWriter.h" // ALIFMDRAWREADER_H
43 #include "AliFMDAltroMapping.h" // ALIFMDALTROMAPPING_H
44 #include "AliFMDAltroIO.h" // ALIFMDALTROWRITER_H
45 #include <TArrayI.h> // ROOT_TArrayI
46 #include <TClonesArray.h> // ROOT_TClonesArray
49 //____________________________________________________________________
50 ClassImp(AliFMDRawWriter)
52 ; // This is here to keep Emacs for indenting the next line
55 //____________________________________________________________________
56 AliFMDRawWriter::AliFMDRawWriter(AliFMD* fmd)
57 : TTask("FMDRawWriter", "Writer of Raw ADC values from the FMD"),
60 AliFMDParameters* pars = AliFMDParameters::Instance();
61 fSampleRate = pars->GetSampleRate(AliFMDParameters::kBaseDDL);
62 fChannelsPerAltro = pars->GetChannelsPerAltro();
66 //____________________________________________________________________
68 AliFMDRawWriter::Exec(Option_t*)
70 // Turn digits into raw data.
72 // Digits are read from the Digit branch, and processed to make
73 // three DDL files, one for each of the sub-detectors FMD1, FMD2,
76 // The raw data files consists of a header, followed by ALTRO
87 // An ALTRO formatted block, in the FMD context, consists of a
88 // number of counts followed by a trailer.
90 // +------------------+
93 // | possible fillers |
94 // +------------------+
96 // +------------------+
99 // The counts are listed backwards, that is, starting with the
100 // latest count, and ending in the first.
102 // Each count consist of 1 or more ADC samples of the VA1_ALICE
103 // pre-amp. signal. Just how many samples are used depends on
104 // whether the ALTRO over samples the pre-amp. Each sample is a
105 // 10-bit word, and the samples are grouped into 40-bit blocks
107 // +------------------------------------+
108 // | S(n) | S(n-1) | S(n-2) | S(n-3) |
109 // | ... | ... | ... | ... |
110 // | S(2) | S(1) | AA | AA |
111 // +------------------------------------+
112 // Counts + possible filler
114 // The trailer of the number of words of signales, the starting
115 // strip number, the sector number, and the ring ID; each 10-bit
116 // words, packed into 40-bits.
118 // +------------------------------------+
119 // | # words | start | sector | ring |
120 // +------------------------------------+
123 // Note, that this method assumes that the digits are ordered.
125 AliLoader* loader = fFMD->GetLoader();
126 loader->LoadDigits();
127 TTree* digitTree = loader->TreeD();
129 Error("Digits2Raw", "no digit tree");
133 TClonesArray* digits = new TClonesArray("AliFMDDigit", 1000);
134 fFMD->SetTreeAddress();
135 TBranch* digitBranch = digitTree->GetBranch(fFMD->GetName());
137 Error("Digits2Raw", "no branch for %s", fFMD->GetName());
140 digitBranch->SetAddress(&digits);
142 Int_t nEvents = Int_t(digitTree->GetEntries());
143 for (Int_t event = 0; event < nEvents; event++) {
145 digitTree->GetEvent(event);
147 // Write out the digits
150 loader->UnloadDigits();
153 //____________________________________________________________________
155 AliFMDRawWriter::WriteDigits(TClonesArray* digits)
157 Int_t nDigits = digits->GetEntries();
158 if (nDigits < 1) return;
160 AliFMDParameters* pars = AliFMDParameters::Instance();
161 AliFMDAltroWriter* writer = 0;
162 Int_t sampleRate = -1;
165 std::ofstream* file = 0;
167 for (Int_t i = 0; i < nDigits; i++) {
169 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
170 UInt_t thisDDL, thisHwaddr;
171 UShort_t det = digit->Detector();
172 Char_t ring = digit->Ring();
173 UShort_t sector = digit->Sector();
174 UShort_t strip = digit->Strip();
175 if (!pars->Detector2Hardware(det,ring,sector,strip,thisDDL,thisHwaddr)) {
176 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
177 det, ring, sector, strip));
180 AliDebug(40, Form("Got DDL=%d and address=%d from FMD%d%c[%2d,%3d]",
181 thisDDL, thisHwaddr, det, ring, sector, strip));
182 // Check if we're still in the same channel
183 if (thisHwaddr != hwaddr) {
184 AliDebug(30, Form("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
185 "(board 0x%x, chip 0x%x, channel 0x%x)",
186 thisHwaddr, det, ring, sector, strip,
187 (thisHwaddr >> 7), (thisHwaddr >> 4) & 0x7,
189 if (writer) writer->AddChannelTrailer(hwaddr);
192 // Check if we're still in the same detector (DDL)
193 if (ddl != thisDDL) {
195 AliDebug(1, Form("Closing altro writer %p", writer));
196 if ((ret = writer->Close()) < 0) {
197 AliError(Form("Error: %s", writer->ErrorString(ret)));
208 // If we haven't got a writer (either because none were made so
209 // far, or because we've switch DDL), make one.
211 AliDebug(1, Form("Opening new ALTRO writer w/file FMD_%d.ddl", ddl));
212 file = new std::ofstream(Form("FMD_%d.ddl", ddl));
213 if (!file || !*file) {
214 AliFatal(Form("Failed to open file FMD_%d.ddl", ddl));
217 writer = new AliFMDAltroWriter(*file);
218 writer->SetThreshold(pars->GetZeroSuppression(det, ring, sector, strip));
219 sampleRate = pars->GetSampleRate(ddl);
221 // Write out our signal
222 writer->AddSignal(digit->Count1());
223 if (sampleRate >= 2) writer->AddSignal(digit->Count2());
224 if (sampleRate >= 3) writer->AddSignal(digit->Count3());
227 writer->AddChannelTrailer(hwaddr);
238 //____________________________________________________________________
240 AliFMDRawWriter::WriteDigits(TClonesArray* digits)
242 Int_t nDigits = digits->GetEntries();
243 if (nDigits < 1) return;
245 AliFMDParameters* pars = AliFMDParameters::Instance();
246 UShort_t prevDetector = 0;
247 Char_t prevRing = '\0';
248 UShort_t prevSector = 0;
249 // UShort_t prevStrip = 0;
251 // The first seen strip number for a channel
252 UShort_t startStrip = 0;
254 // Which channel number in the ALTRO channel we're at
257 // How many times the ALTRO Samples one VA1_ALICE channel
258 Int_t sampleRate = 1;
260 // A buffer to hold 1 ALTRO channel - Normally, one ALTRO channel
261 // holds 128 VA1_ALICE channels, sampled at a rate of `sampleRate'
262 TArrayI channel(fChannelsPerAltro * sampleRate);
265 AliAltroBuffer* altro = 0;
267 // Loop over the digits in the event. Note, that we assume the
268 // the digits are in order in the branch. If they were not, we'd
269 // have to cache all channels before we could write the data to
270 // the ALTRO buffer, or we'd have to set up a map of the digits.
271 for (Int_t i = 0; i < nDigits; i++) {
273 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
275 UShort_t det = digit->Detector();
276 Char_t ring = digit->Ring();
277 UShort_t sector = digit->Sector();
278 UShort_t strip = digit->Strip();
279 fThreshold = pars->GetZeroSuppression(det, ring, sector, strip);
280 if (det != prevDetector) {
281 AliDebug(15, Form("FMD: New DDL, was %d, now %d",
282 AliFMDParameters::kBaseDDL + prevDetector - 1,
283 AliFMDParameters::kBaseDDL + det - 1));
284 // If an altro exists, delete the object, flushing the data to
285 // disk, and closing the file.
287 // When the first argument is false, we write the real
289 AliDebug(15, Form("New altro: Write channel at %d Strip: %d "
290 "Sector: %d Ring: %d",
291 i, startStrip, prevSector, prevRing));
292 // TPC to FMD translations
295 // ----------+-----------
300 WriteChannel(altro, startStrip, prevSector, prevRing, channel);
302 altro->WriteDataHeader(kFALSE, kFALSE);
307 // Need to open a new DDL!
308 Int_t ddlId = AliFMDParameters::kBaseDDL + det - 1;
309 TString filename(Form("%s_%d.ddl", fFMD->GetName(), ddlId));
311 AliDebug(15, Form("New altro buffer with DDL file %s",
313 AliDebug(15, Form("New altro at %d", i));
314 // Create a new altro buffer - a `1' as the second argument
315 // means `write mode'
316 altro = new AliAltroBuffer(filename.Data(), 1);
317 altro->SetMapping(pars->GetAltroMap());
319 // Write a dummy (first argument is true) header to the DDL
320 // file - later on, when we close the file, we write the real
322 altro->WriteDataHeader(kTRUE, kFALSE);
324 // Figure out the sample rate
325 if (fSampleRate > 0) sampleRate = fSampleRate;
327 if (digit->Count2() >= 0) sampleRate = 2;
328 if (digit->Count3() >= 0) sampleRate = 3;
331 channel.Set(fChannelsPerAltro * sampleRate);
337 else if (offset == fChannelsPerAltro
338 || digit->Ring() != prevRing
339 || digit->Sector() != prevSector) {
340 // Force a new Altro channel
341 AliDebug(15, Form("Flushing channel to disk because %s",
342 (offset == fChannelsPerAltro ? "channel is full" :
343 (ring != prevRing ? "new ring up" :
345 AliDebug(15, Form("New Channel: Write channel at %d Strip: %d "
346 "Sector: %d Ring: %d",
347 i, startStrip, prevSector, prevRing));
348 WriteChannel(altro, startStrip, prevSector, prevRing, channel);
349 // Reset and update channel variables
357 // Store the counts of the ADC in the channel buffer
358 channel[offset * sampleRate] = digit->Count1();
360 channel[offset * sampleRate + 1] = digit->Count2();
362 channel[offset * sampleRate + 2] = digit->Count3();
365 // Finally, we need to close the final ALTRO buffer if it wasn't
369 altro->WriteDataHeader(kFALSE, kFALSE);
375 //____________________________________________________________________
377 AliFMDRawWriter::WriteChannel(AliAltroBuffer* altro,
378 UShort_t strip, UShort_t sector, Char_t ring,
381 // Write out one ALTRO channel to the data file.
382 // Derived classes can overload this method to use a per-ALTRO
383 // threshold. This implementation uses the common threshold set by
385 altro->WriteChannel(Int_t(strip),
387 Int_t((ring == 'I' ? 0 : 1)),
388 data.fN, data.fArray, fThreshold);
393 //____________________________________________________________________