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 **************************************************************************/
16 /** @file AliFMDRawWriter.cxx
17 @author Christian Holm Christensen <cholm@nbi.dk>
18 @date Mon Mar 27 12:45:56 2006
19 @brief Class to write raw data
21 //____________________________________________________________________
23 // Class to write ADC values to a raw data file
25 // This class writes FMD Raw data to a file. The sample rate (number
26 // of times the ALTRO ADC samples each pre-amp. channel - that is,
27 // data from a single strip), can be set via SetSampleRate.
29 // Zero-suppression can be enabled by calling SetThreshold with a
30 // non-zero argument. ADC values less than the value set will not be
31 // written to output. Note, that if you use zero-suppression, you
32 // need to explicitly set the sample rate when reading back the data
33 // with AliFMDRawReader.
35 // This class uses the AliAltroBuffer class to write the data in the
36 // ALTRO format. See the Exec member function for more information on
39 // #include <AliLog.h> // ALILOG_H
40 #include "AliFMDDebug.h" // Better debug macros
41 #include <AliLoader.h> // ALILOADER_H
42 #include <AliAltroBuffer.h> // ALIALTROBUFFER_H
43 #include "AliFMD.h" // ALIFMD_H
44 #include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
45 #include "AliFMDDigit.h" // ALIFMDDIGIT_H
46 #include "AliFMDRawWriter.h" // ALIFMDRAWREADER_H
47 #include "AliFMDAltroMapping.h" // ALIFMDALTROMAPPING_H
48 // #include "AliFMDAltroIO.h" // ALIFMDALTROWRITER_H
49 #include <TArrayI.h> // ROOT_TArrayI
50 #include <TClonesArray.h> // ROOT_TClonesArray
54 //____________________________________________________________________
55 ClassImp(AliFMDRawWriter)
57 ; // This is here to keep Emacs for indenting the next line
60 //____________________________________________________________________
61 AliFMDRawWriter::AliFMDRawWriter(AliFMD* fmd)
62 : TTask("FMDRawWriter", "Writer of Raw ADC values from the FMD"),
73 //____________________________________________________________________
75 AliFMDRawWriter::Exec(Option_t*)
77 // Turn digits into raw data.
79 // Digits are read from the Digit branch, and processed to make
80 // three DDL files, one for each of the sub-detectors FMD1, FMD2,
83 // The raw data files consists of a header, followed by ALTRO
94 // An ALTRO formatted block, in the FMD context, consists of a
95 // number of counts followed by a trailer.
97 // +------------------+
100 // | possible fillers |
101 // +------------------+
103 // +------------------+
106 // The counts are listed backwards, that is, starting with the
107 // latest count, and ending in the first.
109 // Each count consist of 1 or more ADC samples of the VA1_ALICE
110 // pre-amp. signal. Just how many samples are used depends on
111 // whether the ALTRO over samples the pre-amp. Each sample is a
112 // 10-bit word, and the samples are grouped into 40-bit blocks
114 // +------------------------------------+
115 // | S(1) | S(2) | S(3) | S(4) |
116 // | ... | ... | ... | ... |
117 // | S(n) | T(n) | n+2 | 2AA |
118 // +------------------------------------+
119 // Counts + possible filler
121 // The trailer of the number of words of signales, the starting
122 // strip number, the sector number, and the ring ID; each 10-bit
123 // words, packed into 40-bits.
125 // +------------------------------------+
126 // | 2AAA | Len | A | Address |
127 // +------------------------------------+
130 // Note, that this method assumes that the digits are ordered.
132 AliLoader* loader = fFMD->GetLoader();
133 loader->LoadDigits();
134 TTree* digitTree = loader->TreeD();
136 Error("Digits2Raw", "no digit tree");
140 TClonesArray* digits = new TClonesArray("AliFMDDigit", 1000);
141 fFMD->SetTreeAddress();
142 TBranch* digitBranch = digitTree->GetBranch(fFMD->GetName());
144 Error("Digits2Raw", "no branch for %s", fFMD->GetName());
147 digitBranch->SetAddress(&digits);
149 Int_t nEvents = Int_t(digitTree->GetEntries());
150 AliFMDDebug(5, ("Got a total of %5d events from tree", nEvents));
151 for (Int_t event = 0; event < nEvents; event++) {
153 digitTree->GetEvent(event);
155 // Write out the digits
158 loader->UnloadDigits();
162 //____________________________________________________________________
164 AliFMDRawWriter::WriteDigits(TClonesArray* digits)
166 // WRite an array of digits to disk file
167 Int_t nDigits = digits->GetEntries();
168 if (nDigits < 1) return;
169 AliFMDDebug(5, ("Got a total of %5d digits from tree", nDigits));
171 AliFMDParameters* pars = AliFMDParameters::Instance();
172 UShort_t threshold = 0;
173 UInt_t prevddl = 0xFFFF;
174 UInt_t prevaddr = 0xFFF;
175 // UShort_t prevStrip = 0;
177 // Which channel number in the ALTRO channel we're at
179 UShort_t preSamples = 0;
181 // How many times the ALTRO Samples one VA1_ALICE channel
182 Int_t sampleRate = 1;
184 // A buffer to hold 1 ALTRO channel - Normally, one ALTRO channel
185 // holds 128 VA1_ALICE channels, sampled at a rate of `sampleRate'
186 TArrayI data(pars->GetChannelsPerAltro() * 8);
189 AliAltroBuffer* altro = 0;
191 // Loop over the digits in the event. Note, that we assume the
192 // the digits are in order in the branch. If they were not, we'd
193 // have to cache all channels before we could write the data to
194 // the ALTRO buffer, or we'd have to set up a map of the digits.
195 UShort_t oldDet = 1000;
196 for (Int_t i = 0; i < nDigits; i++) {
198 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
199 UShort_t det = digit->Detector();
200 Char_t ring = digit->Ring();
201 UShort_t sector = digit->Sector();
202 UShort_t strip = digit->Strip();
206 AliFMDDebug(5, ("Got new detector: %d (was %d)", det, oldDet));
209 AliFMDDebug(10, ("Processing digit # %5d FMD%d%c[%2d,%3d]",
210 i, det, ring, sector, strip));
211 threshold = pars->GetZeroSuppression(det, ring, sector, strip);
212 if (!pars->Detector2Hardware(det, ring, sector, strip, ddl, addr)) {
213 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
214 det, ring, sector, strip));
217 AliFMDDebug(10, ("FMD%d%c[%2d,%3d]-> ddl: 0x%x addr: 0x%x",
218 det, ring, sector, strip, ddl, addr));
219 if (addr != prevaddr) {
220 // Flush a channel to output
221 AliFMDDebug(15, ("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
222 "(board 0x%x, chip 0x%x, channel 0x%x), flushing old "
223 "channel at 0x%x with %d words",
224 addr, det, ring, sector, strip,
225 (addr >> 7), (addr >> 4) & 0x7, addr & 0xf,
227 if (altro) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
230 for (size_t i = 0; i < nWords; i++) data[i] = digit->Count(0);
232 if (ddl != prevddl) {
233 AliFMDDebug(5, ("FMD: New DDL, was %d, now %d", prevddl, ddl));
234 // If an altro exists, delete the object, flushing the data to
235 // disk, and closing the file.
237 // When the first argument is false, we write the real
239 AliFMDDebug(15, ("Closing output"));
241 altro->WriteDataHeader(kFALSE, kFALSE);
246 // Need to open a new DDL!
247 TString filename(AliDAQ::DdlFileName(fFMD->GetName(), ddl));
248 AliFMDDebug(5, ("New altro buffer with DDL file %s", filename.Data()));
249 // Create a new altro buffer - a `1' as the second argument
250 // means `write mode'
251 altro = new AliAltroBuffer(filename.Data());
252 altro->SetMapping(pars->GetAltroMap());
253 // Write a dummy (first argument is true) header to the DDL
254 // file - later on, when we close the file, we write the real
256 altro->WriteDataHeader(kTRUE, kFALSE);
259 // Store the counts of the ADC in the channel buffer
260 sampleRate = pars->GetSampleRate(det, ring, sector, strip);
261 for (int s = 0; s < sampleRate; s++) {
262 data[nWords] = digit->Count(s);
266 // Finally, we need to close the final ALTRO buffer if it wasn't
269 if (nWords > 0) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
271 altro->WriteDataHeader(kFALSE, kFALSE);
276 //____________________________________________________________________
278 AliFMDRawWriter::WriteDigits(TClonesArray* digits)
280 Int_t nDigits = digits->GetEntries();
281 if (nDigits < 1) return;
283 AliFMDParameters* pars = AliFMDParameters::Instance();
284 AliFMDAltroWriter* writer = 0;
285 Int_t sampleRate = -1;
288 std::ofstream* file = 0;
290 for (Int_t i = 0; i < nDigits; i++) {
292 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
293 UInt_t thisDDL, thisHwaddr;
294 UShort_t det = digit->Detector();
295 Char_t ring = digit->Ring();
296 UShort_t sector = digit->Sector();
297 UShort_t strip = digit->Strip();
298 if (!pars->Detector2Hardware(det,ring,sector,strip,thisDDL,thisHwaddr)) {
299 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
300 det, ring, sector, strip));
303 AliFMDDebug(40, ("Got DDL=%d and address=%d from FMD%d%c[%2d,%3d]",
304 thisDDL, thisHwaddr, det, ring, sector, strip));
305 // Check if we're still in the same channel
306 if (thisHwaddr != hwaddr) {
307 AliFMDDebug(30, ("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
308 "(board 0x%x, chip 0x%x, channel 0x%x)",
309 thisHwaddr, det, ring, sector, strip,
310 (thisHwaddr >> 7), (thisHwaddr >> 4) & 0x7,
312 if (writer) writer->AddChannelTrailer(hwaddr);
315 // Check if we're still in the same detector (DDL)
316 if (ddl != thisDDL) {
318 AliFMDDebug(1, ("Closing altro writer %p", writer));
319 if ((ret = writer->Close()) < 0) {
320 AliError(Form("Error: %s", writer->ErrorString(ret)));
331 // If we haven't got a writer (either because none were made so
332 // far, or because we've switch DDL), make one.
334 AliFMDDebug(1, ("Opening new ALTRO writer w/file %s",
335 AliDAQ::DdlFileName("FMD",ddl)));
336 file = new std::ofstream(AliDAQ::DdlFileName("FMD",ddl));
337 if (!file || !*file) {
338 AliFatal(Form("Failed to open file %s",
339 AliDAQ::DdlFileName("FMD",ddl)));
342 writer = new AliFMDAltroWriter(*file);
343 writer->SetThreshold(pars->GetZeroSuppression(det, ring, sector, strip));
345 // Write out our signal
346 sampleRate = pars->GetSampleRate(det,ring,sector,strip);
347 writer->AddSignal(digit->Count1());
348 if (sampleRate >= 2) writer->AddSignal(digit->Count2());
349 if (sampleRate >= 3) writer->AddSignal(digit->Count3());
352 writer->AddChannelTrailer(hwaddr);
364 //____________________________________________________________________