Small patch for file copying for special outputs in proof mode (M.Vala)
[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
69}
70
e802be3e 71
72
73//____________________________________________________________________
74void
75AliFMDRawWriter::Exec(Option_t*)
76{
77 // Turn digits into raw data.
78 //
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,
81 // and FMD3.
82 //
83 // The raw data files consists of a header, followed by ALTRO
84 // formatted blocks.
85 //
86 // +-------------+
87 // | Header |
88 // +-------------+
89 // | ALTRO Block |
90 // | ... |
91 // +-------------+
92 // DDL file
93 //
94 // An ALTRO formatted block, in the FMD context, consists of a
95 // number of counts followed by a trailer.
96 //
97 // +------------------+
98 // | Count |
99 // | ... |
100 // | possible fillers |
101 // +------------------+
102 // | Trailer |
103 // +------------------+
104 // ALTRO block
105 //
106 // The counts are listed backwards, that is, starting with the
107 // latest count, and ending in the first.
108 //
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
113 //
114 // +------------------------------------+
c2fc1258 115 // | S(1) | S(2) | S(3) | S(4) |
e802be3e 116 // | ... | ... | ... | ... |
c2fc1258 117 // | S(n) | T(n) | n+2 | 2AA |
e802be3e 118 // +------------------------------------+
119 // Counts + possible filler
120 //
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.
124 //
125 // +------------------------------------+
c2fc1258 126 // | 2AAA | Len | A | Address |
e802be3e 127 // +------------------------------------+
128 // Trailer
129 //
130 // Note, that this method assumes that the digits are ordered.
131 //
132 AliLoader* loader = fFMD->GetLoader();
133 loader->LoadDigits();
134 TTree* digitTree = loader->TreeD();
135 if (!digitTree) {
136 Error("Digits2Raw", "no digit tree");
137 return;
138 }
139
140 TClonesArray* digits = new TClonesArray("AliFMDDigit", 1000);
141 fFMD->SetTreeAddress();
142 TBranch* digitBranch = digitTree->GetBranch(fFMD->GetName());
143 if (!digitBranch) {
144 Error("Digits2Raw", "no branch for %s", fFMD->GetName());
145 return;
146 }
147 digitBranch->SetAddress(&digits);
148
149 Int_t nEvents = Int_t(digitTree->GetEntries());
f95a63c4 150 AliFMDDebug(5, ("Got a total of %5d events from tree", nEvents));
e802be3e 151 for (Int_t event = 0; event < nEvents; event++) {
152 fFMD->ResetDigits();
153 digitTree->GetEvent(event);
154
1e8f773e 155 // Write out the digits
156 WriteDigits(digits);
157 }
158 loader->UnloadDigits();
159}
e802be3e 160
c2fc1258 161#if 1
162//____________________________________________________________________
163void
164AliFMDRawWriter::WriteDigits(TClonesArray* digits)
165{
02a27b50 166 // WRite an array of digits to disk file
c2fc1258 167 Int_t nDigits = digits->GetEntries();
168 if (nDigits < 1) return;
f95a63c4 169 AliFMDDebug(5, ("Got a total of %5d digits from tree", nDigits));
c2fc1258 170
171 AliFMDParameters* pars = AliFMDParameters::Instance();
172 UShort_t threshold = 0;
f95a63c4 173 UInt_t prevddl = 0xFFFF;
c2fc1258 174 UInt_t prevaddr = 0xFFF;
175 // UShort_t prevStrip = 0;
176
177 // Which channel number in the ALTRO channel we're at
178 UShort_t nWords = 0;
179 UShort_t preSamples = 0;
180
181 // How many times the ALTRO Samples one VA1_ALICE channel
182 Int_t sampleRate = 1;
183
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);
187
188 // The Altro buffer
189 AliAltroBuffer* altro = 0;
190
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.
f95a63c4 195 UShort_t oldDet = 1000;
c2fc1258 196 for (Int_t i = 0; i < nDigits; i++) {
197 // Get the digit
198 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
c2fc1258 199 UShort_t det = digit->Detector();
200 Char_t ring = digit->Ring();
201 UShort_t sector = digit->Sector();
202 UShort_t strip = digit->Strip();
203 UInt_t ddl;
204 UInt_t addr;
f95a63c4 205 if (det != oldDet) {
206 AliFMDDebug(5, ("Got new detector: %d (was %d)", det, oldDet));
207 oldDet = det;
208 }
209 AliFMDDebug(10, ("Processing digit # %5d FMD%d%c[%2d,%3d]",
210 i, det, ring, sector, strip));
c2fc1258 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));
215 continue;
216 }
625d7886 217 preSamples = pars->GetPreSamples(det, ring, sector, strip);
218
f95a63c4 219 AliFMDDebug(10, ("FMD%d%c[%2d,%3d]-> ddl: 0x%x addr: 0x%x",
220 det, ring, sector, strip, ddl, addr));
c2fc1258 221 if (addr != prevaddr) {
222 // Flush a channel to output
f95a63c4 223 AliFMDDebug(15, ("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
224 "(board 0x%x, chip 0x%x, channel 0x%x), flushing old "
225 "channel at 0x%x with %d words",
226 addr, det, ring, sector, strip,
227 (addr >> 7), (addr >> 4) & 0x7, addr & 0xf,
228 prevaddr, nWords));
c2fc1258 229 if (altro) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
230 nWords = preSamples;
231 prevaddr = addr;
458e52e8 232 for (size_t j = 0; j < nWords; j++) data[j] = digit->Count(0);
c2fc1258 233 }
234 if (ddl != prevddl) {
f95a63c4 235 AliFMDDebug(5, ("FMD: New DDL, was %d, now %d", prevddl, ddl));
c2fc1258 236 // If an altro exists, delete the object, flushing the data to
237 // disk, and closing the file.
238 if (altro) {
239 // When the first argument is false, we write the real
240 // header.
f95a63c4 241 AliFMDDebug(15, ("Closing output"));
c2fc1258 242 altro->Flush();
243 altro->WriteDataHeader(kFALSE, kFALSE);
244 delete altro;
245 altro = 0;
246 }
247 prevddl = ddl;
248 // Need to open a new DDL!
362c9d61 249 TString filename(AliDAQ::DdlFileName(fFMD->GetName(), ddl));
f95a63c4 250 AliFMDDebug(5, ("New altro buffer with DDL file %s", filename.Data()));
c2fc1258 251 // Create a new altro buffer - a `1' as the second argument
252 // means `write mode'
4c846604 253 altro = new AliAltroBuffer(filename.Data());
c2fc1258 254 altro->SetMapping(pars->GetAltroMap());
255 // Write a dummy (first argument is true) header to the DDL
256 // file - later on, when we close the file, we write the real
257 // header
258 altro->WriteDataHeader(kTRUE, kFALSE);
259 }
260
261 // Store the counts of the ADC in the channel buffer
262 sampleRate = pars->GetSampleRate(det, ring, sector, strip);
263 for (int s = 0; s < sampleRate; s++) {
264 data[nWords] = digit->Count(s);
265 nWords++;
266 }
267 }
268 // Finally, we need to close the final ALTRO buffer if it wasn't
269 // already
270 if (altro) {
271 if (nWords > 0) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
272 altro->Flush();
273 altro->WriteDataHeader(kFALSE, kFALSE);
274 delete altro;
275 }
276}
277#else
1e8f773e 278//____________________________________________________________________
279void
280AliFMDRawWriter::WriteDigits(TClonesArray* digits)
281{
282 Int_t nDigits = digits->GetEntries();
283 if (nDigits < 1) return;
e802be3e 284
1e8f773e 285 AliFMDParameters* pars = AliFMDParameters::Instance();
286 AliFMDAltroWriter* writer = 0;
287 Int_t sampleRate = -1;
288 UShort_t hwaddr = 0;
289 UShort_t ddl = 0;
290 std::ofstream* file = 0;
291 Int_t ret = 0;
292 for (Int_t i = 0; i < nDigits; i++) {
293 // Get the digit
294 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
295 UInt_t thisDDL, thisHwaddr;
296 UShort_t det = digit->Detector();
297 Char_t ring = digit->Ring();
298 UShort_t sector = digit->Sector();
299 UShort_t strip = digit->Strip();
300 if (!pars->Detector2Hardware(det,ring,sector,strip,thisDDL,thisHwaddr)) {
301 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
302 det, ring, sector, strip));
303 continue;
304 }
f95a63c4 305 AliFMDDebug(40, ("Got DDL=%d and address=%d from FMD%d%c[%2d,%3d]",
1e8f773e 306 thisDDL, thisHwaddr, det, ring, sector, strip));
307 // Check if we're still in the same channel
308 if (thisHwaddr != hwaddr) {
f95a63c4 309 AliFMDDebug(30, ("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
1e8f773e 310 "(board 0x%x, chip 0x%x, channel 0x%x)",
311 thisHwaddr, det, ring, sector, strip,
312 (thisHwaddr >> 7), (thisHwaddr >> 4) & 0x7,
313 thisHwaddr & 0xf));
314 if (writer) writer->AddChannelTrailer(hwaddr);
315 hwaddr = thisHwaddr;
316 }
317 // Check if we're still in the same detector (DDL)
318 if (ddl != thisDDL) {
319 if (writer) {
f95a63c4 320 AliFMDDebug(1, ("Closing altro writer %p", writer));
1e8f773e 321 if ((ret = writer->Close()) < 0) {
322 AliError(Form("Error: %s", writer->ErrorString(ret)));
323 return;
324 }
325 delete writer;
326 writer = 0;
327 file->close();
328 delete file;
329 file = 0;
330 }
331 ddl = thisDDL;
332 }
333 // If we haven't got a writer (either because none were made so
334 // far, or because we've switch DDL), make one.
335 if (!writer) {
f95a63c4 336 AliFMDDebug(1, ("Opening new ALTRO writer w/file %s",
337 AliDAQ::DdlFileName("FMD",ddl)));
362c9d61 338 file = new std::ofstream(AliDAQ::DdlFileName("FMD",ddl));
1e8f773e 339 if (!file || !*file) {
f95a63c4 340 AliFatal(Form("Failed to open file %s",
341 AliDAQ::DdlFileName("FMD",ddl)));
1e8f773e 342 return;
343 }
344 writer = new AliFMDAltroWriter(*file);
345 writer->SetThreshold(pars->GetZeroSuppression(det, ring, sector, strip));
1e8f773e 346 }
347 // Write out our signal
c2fc1258 348 sampleRate = pars->GetSampleRate(det,ring,sector,strip);
1e8f773e 349 writer->AddSignal(digit->Count1());
350 if (sampleRate >= 2) writer->AddSignal(digit->Count2());
351 if (sampleRate >= 3) writer->AddSignal(digit->Count3());
352 }
353 if (writer) {
354 writer->AddChannelTrailer(hwaddr);
355 writer->Close();
356 delete writer;
357 file->close();
358 delete file;
359 }
360}
9f662337 361#endif
7684b53c 362
c2fc1258 363
7684b53c 364
365
366//____________________________________________________________________
e802be3e 367//
368// EOF
369//