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