]> git.uio.no Git - u/mrichter/AliRoot.git/blame - FMD/AliFMDRawWriter.cxx
Coding conventions (Christian)
[u/mrichter/AliRoot.git] / FMD / AliFMDRawWriter.cxx
CommitLineData
e802be3e 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
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 **************************************************************************/
e802be3e 15/* $Id$ */
c2fc1258 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
20*/
e802be3e 21//____________________________________________________________________
22//
23// Class to write ADC values to a raw data file
24//
7684b53c 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.
28//
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.
34//
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
37// that format.
38//
56b1929b 39#include <AliLog.h> // ALILOG_H
40#include <AliLoader.h> // ALILOADER_H
41#include <AliAltroBuffer.h> // ALIALTROBUFFER_H
e802be3e 42#include "AliFMD.h" // ALIFMD_H
1a1fdef7 43#include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
e802be3e 44#include "AliFMDDigit.h" // ALIFMDDIGIT_H
45#include "AliFMDRawWriter.h" // ALIFMDRAWREADER_H
1e8f773e 46#include "AliFMDAltroMapping.h" // ALIFMDALTROMAPPING_H
c2fc1258 47// #include "AliFMDAltroIO.h" // ALIFMDALTROWRITER_H
56b1929b 48#include <TArrayI.h> // ROOT_TArrayI
e802be3e 49#include <TClonesArray.h> // ROOT_TClonesArray
02a27b50 50// #include <fstream>
e802be3e 51
52//____________________________________________________________________
925e6570 53ClassImp(AliFMDRawWriter)
1a1fdef7 54#if 0
55 ; // This is here to keep Emacs for indenting the next line
56#endif
e802be3e 57
58//____________________________________________________________________
59AliFMDRawWriter::AliFMDRawWriter(AliFMD* fmd)
60 : TTask("FMDRawWriter", "Writer of Raw ADC values from the FMD"),
61 fFMD(fmd)
02a27b50 62{
63 // CTOR
64}
65
e802be3e 66
67
68//____________________________________________________________________
69void
70AliFMDRawWriter::Exec(Option_t*)
71{
72 // Turn digits into raw data.
73 //
74 // Digits are read from the Digit branch, and processed to make
75 // three DDL files, one for each of the sub-detectors FMD1, FMD2,
76 // and FMD3.
77 //
78 // The raw data files consists of a header, followed by ALTRO
79 // formatted blocks.
80 //
81 // +-------------+
82 // | Header |
83 // +-------------+
84 // | ALTRO Block |
85 // | ... |
86 // +-------------+
87 // DDL file
88 //
89 // An ALTRO formatted block, in the FMD context, consists of a
90 // number of counts followed by a trailer.
91 //
92 // +------------------+
93 // | Count |
94 // | ... |
95 // | possible fillers |
96 // +------------------+
97 // | Trailer |
98 // +------------------+
99 // ALTRO block
100 //
101 // The counts are listed backwards, that is, starting with the
102 // latest count, and ending in the first.
103 //
104 // Each count consist of 1 or more ADC samples of the VA1_ALICE
105 // pre-amp. signal. Just how many samples are used depends on
106 // whether the ALTRO over samples the pre-amp. Each sample is a
107 // 10-bit word, and the samples are grouped into 40-bit blocks
108 //
109 // +------------------------------------+
c2fc1258 110 // | S(1) | S(2) | S(3) | S(4) |
e802be3e 111 // | ... | ... | ... | ... |
c2fc1258 112 // | S(n) | T(n) | n+2 | 2AA |
e802be3e 113 // +------------------------------------+
114 // Counts + possible filler
115 //
116 // The trailer of the number of words of signales, the starting
117 // strip number, the sector number, and the ring ID; each 10-bit
118 // words, packed into 40-bits.
119 //
120 // +------------------------------------+
c2fc1258 121 // | 2AAA | Len | A | Address |
e802be3e 122 // +------------------------------------+
123 // Trailer
124 //
125 // Note, that this method assumes that the digits are ordered.
126 //
127 AliLoader* loader = fFMD->GetLoader();
128 loader->LoadDigits();
129 TTree* digitTree = loader->TreeD();
130 if (!digitTree) {
131 Error("Digits2Raw", "no digit tree");
132 return;
133 }
134
135 TClonesArray* digits = new TClonesArray("AliFMDDigit", 1000);
136 fFMD->SetTreeAddress();
137 TBranch* digitBranch = digitTree->GetBranch(fFMD->GetName());
138 if (!digitBranch) {
139 Error("Digits2Raw", "no branch for %s", fFMD->GetName());
140 return;
141 }
142 digitBranch->SetAddress(&digits);
143
144 Int_t nEvents = Int_t(digitTree->GetEntries());
145 for (Int_t event = 0; event < nEvents; event++) {
146 fFMD->ResetDigits();
147 digitTree->GetEvent(event);
148
1e8f773e 149 // Write out the digits
150 WriteDigits(digits);
151 }
152 loader->UnloadDigits();
153}
e802be3e 154
c2fc1258 155#if 1
156//____________________________________________________________________
157void
158AliFMDRawWriter::WriteDigits(TClonesArray* digits)
159{
02a27b50 160 // WRite an array of digits to disk file
c2fc1258 161 Int_t nDigits = digits->GetEntries();
162 if (nDigits < 1) return;
163
164 AliFMDParameters* pars = AliFMDParameters::Instance();
165 UShort_t threshold = 0;
166 UInt_t prevddl = 0;
167 UInt_t prevaddr = 0xFFF;
168 // UShort_t prevStrip = 0;
169
170 // Which channel number in the ALTRO channel we're at
171 UShort_t nWords = 0;
172 UShort_t preSamples = 0;
173
174 // How many times the ALTRO Samples one VA1_ALICE channel
175 Int_t sampleRate = 1;
176
177 // A buffer to hold 1 ALTRO channel - Normally, one ALTRO channel
178 // holds 128 VA1_ALICE channels, sampled at a rate of `sampleRate'
179 TArrayI data(pars->GetChannelsPerAltro() * 8);
180
181 // The Altro buffer
182 AliAltroBuffer* altro = 0;
183
184 // Loop over the digits in the event. Note, that we assume the
185 // the digits are in order in the branch. If they were not, we'd
186 // have to cache all channels before we could write the data to
187 // the ALTRO buffer, or we'd have to set up a map of the digits.
188 for (Int_t i = 0; i < nDigits; i++) {
189 // Get the digit
190 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
191
192 UShort_t det = digit->Detector();
193 Char_t ring = digit->Ring();
194 UShort_t sector = digit->Sector();
195 UShort_t strip = digit->Strip();
196 UInt_t ddl;
197 UInt_t addr;
198 threshold = pars->GetZeroSuppression(det, ring, sector, strip);
199 if (!pars->Detector2Hardware(det, ring, sector, strip, ddl, addr)) {
200 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
201 det, ring, sector, strip));
202 continue;
203 }
204 if (addr != prevaddr) {
205 // Flush a channel to output
206 AliDebug(15, Form("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
207 "(board 0x%x, chip 0x%x, channel 0x%x), flushing old "
208 "channel at 0x%x with %d words",
209 addr, det, ring, sector, strip,
210 (addr >> 7), (addr >> 4) & 0x7, addr & 0xf,
211 prevaddr, nWords));
212 if (altro) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
213 nWords = preSamples;
214 prevaddr = addr;
215 for (size_t i = 0; i < nWords; i++) data[i] = digit->Count(0);
216 }
217 if (ddl != prevddl) {
218 AliDebug(15, Form("FMD: New DDL, was %d, now %d", prevddl, ddl));
219 // If an altro exists, delete the object, flushing the data to
220 // disk, and closing the file.
221 if (altro) {
222 // When the first argument is false, we write the real
223 // header.
224 AliDebug(15, Form("Closing output"));
225 altro->Flush();
226 altro->WriteDataHeader(kFALSE, kFALSE);
227 delete altro;
228 altro = 0;
229 }
230 prevddl = ddl;
231 // Need to open a new DDL!
232 TString filename(Form("%s_%d.ddl", fFMD->GetName(), ddl));
233 AliDebug(15, Form("New altro buffer with DDL file %s", filename.Data()));
234 // Create a new altro buffer - a `1' as the second argument
235 // means `write mode'
236 altro = new AliAltroBuffer(filename.Data(), 1);
237 altro->SetMapping(pars->GetAltroMap());
238 // Write a dummy (first argument is true) header to the DDL
239 // file - later on, when we close the file, we write the real
240 // header
241 altro->WriteDataHeader(kTRUE, kFALSE);
242 }
243
244 // Store the counts of the ADC in the channel buffer
245 sampleRate = pars->GetSampleRate(det, ring, sector, strip);
246 for (int s = 0; s < sampleRate; s++) {
247 data[nWords] = digit->Count(s);
248 nWords++;
249 }
250 }
251 // Finally, we need to close the final ALTRO buffer if it wasn't
252 // already
253 if (altro) {
254 if (nWords > 0) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
255 altro->Flush();
256 altro->WriteDataHeader(kFALSE, kFALSE);
257 delete altro;
258 }
259}
260#else
1e8f773e 261//____________________________________________________________________
262void
263AliFMDRawWriter::WriteDigits(TClonesArray* digits)
264{
265 Int_t nDigits = digits->GetEntries();
266 if (nDigits < 1) return;
e802be3e 267
1e8f773e 268 AliFMDParameters* pars = AliFMDParameters::Instance();
269 AliFMDAltroWriter* writer = 0;
270 Int_t sampleRate = -1;
271 UShort_t hwaddr = 0;
272 UShort_t ddl = 0;
273 std::ofstream* file = 0;
274 Int_t ret = 0;
275 for (Int_t i = 0; i < nDigits; i++) {
276 // Get the digit
277 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
278 UInt_t thisDDL, thisHwaddr;
279 UShort_t det = digit->Detector();
280 Char_t ring = digit->Ring();
281 UShort_t sector = digit->Sector();
282 UShort_t strip = digit->Strip();
283 if (!pars->Detector2Hardware(det,ring,sector,strip,thisDDL,thisHwaddr)) {
284 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
285 det, ring, sector, strip));
286 continue;
287 }
288 AliDebug(40, Form("Got DDL=%d and address=%d from FMD%d%c[%2d,%3d]",
289 thisDDL, thisHwaddr, det, ring, sector, strip));
290 // Check if we're still in the same channel
291 if (thisHwaddr != hwaddr) {
292 AliDebug(30, Form("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
293 "(board 0x%x, chip 0x%x, channel 0x%x)",
294 thisHwaddr, det, ring, sector, strip,
295 (thisHwaddr >> 7), (thisHwaddr >> 4) & 0x7,
296 thisHwaddr & 0xf));
297 if (writer) writer->AddChannelTrailer(hwaddr);
298 hwaddr = thisHwaddr;
299 }
300 // Check if we're still in the same detector (DDL)
301 if (ddl != thisDDL) {
302 if (writer) {
303 AliDebug(1, Form("Closing altro writer %p", writer));
304 if ((ret = writer->Close()) < 0) {
305 AliError(Form("Error: %s", writer->ErrorString(ret)));
306 return;
307 }
308 delete writer;
309 writer = 0;
310 file->close();
311 delete file;
312 file = 0;
313 }
314 ddl = thisDDL;
315 }
316 // If we haven't got a writer (either because none were made so
317 // far, or because we've switch DDL), make one.
318 if (!writer) {
319 AliDebug(1, Form("Opening new ALTRO writer w/file FMD_%d.ddl", ddl));
320 file = new std::ofstream(Form("FMD_%d.ddl", ddl));
321 if (!file || !*file) {
322 AliFatal(Form("Failed to open file FMD_%d.ddl", ddl));
323 return;
324 }
325 writer = new AliFMDAltroWriter(*file);
326 writer->SetThreshold(pars->GetZeroSuppression(det, ring, sector, strip));
1e8f773e 327 }
328 // Write out our signal
c2fc1258 329 sampleRate = pars->GetSampleRate(det,ring,sector,strip);
1e8f773e 330 writer->AddSignal(digit->Count1());
331 if (sampleRate >= 2) writer->AddSignal(digit->Count2());
332 if (sampleRate >= 3) writer->AddSignal(digit->Count3());
333 }
334 if (writer) {
335 writer->AddChannelTrailer(hwaddr);
336 writer->Close();
337 delete writer;
338 file->close();
339 delete file;
340 }
341}
9f662337 342#endif
7684b53c 343
c2fc1258 344
7684b53c 345
346
e802be3e 347//____________________________________________________________________
348//
349// EOF
350//