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 <AliLoader.h> // ALILOADER_H
41 #include <AliAltroBuffer.h> // ALIALTROBUFFER_H
42 #include "AliFMD.h" // ALIFMD_H
43 #include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
44 #include "AliFMDDigit.h" // ALIFMDDIGIT_H
45 #include "AliFMDRawWriter.h" // ALIFMDRAWREADER_H
46 #include "AliFMDAltroMapping.h" // ALIFMDALTROMAPPING_H
47 // #include "AliFMDAltroIO.h" // ALIFMDALTROWRITER_H
48 #include <TArrayI.h> // ROOT_TArrayI
49 #include <TClonesArray.h> // ROOT_TClonesArray
52 //____________________________________________________________________
53 ClassImp(AliFMDRawWriter)
55 ; // This is here to keep Emacs for indenting the next line
58 //____________________________________________________________________
59 AliFMDRawWriter::AliFMDRawWriter(AliFMD* fmd)
60 : TTask("FMDRawWriter", "Writer of Raw ADC values from the FMD"),
65 //____________________________________________________________________
67 AliFMDRawWriter::Exec(Option_t*)
69 // Turn digits into raw data.
71 // Digits are read from the Digit branch, and processed to make
72 // three DDL files, one for each of the sub-detectors FMD1, FMD2,
75 // The raw data files consists of a header, followed by ALTRO
86 // An ALTRO formatted block, in the FMD context, consists of a
87 // number of counts followed by a trailer.
89 // +------------------+
92 // | possible fillers |
93 // +------------------+
95 // +------------------+
98 // The counts are listed backwards, that is, starting with the
99 // latest count, and ending in the first.
101 // Each count consist of 1 or more ADC samples of the VA1_ALICE
102 // pre-amp. signal. Just how many samples are used depends on
103 // whether the ALTRO over samples the pre-amp. Each sample is a
104 // 10-bit word, and the samples are grouped into 40-bit blocks
106 // +------------------------------------+
107 // | S(1) | S(2) | S(3) | S(4) |
108 // | ... | ... | ... | ... |
109 // | S(n) | T(n) | n+2 | 2AA |
110 // +------------------------------------+
111 // Counts + possible filler
113 // The trailer of the number of words of signales, the starting
114 // strip number, the sector number, and the ring ID; each 10-bit
115 // words, packed into 40-bits.
117 // +------------------------------------+
118 // | 2AAA | Len | A | Address |
119 // +------------------------------------+
122 // Note, that this method assumes that the digits are ordered.
124 AliLoader* loader = fFMD->GetLoader();
125 loader->LoadDigits();
126 TTree* digitTree = loader->TreeD();
128 Error("Digits2Raw", "no digit tree");
132 TClonesArray* digits = new TClonesArray("AliFMDDigit", 1000);
133 fFMD->SetTreeAddress();
134 TBranch* digitBranch = digitTree->GetBranch(fFMD->GetName());
136 Error("Digits2Raw", "no branch for %s", fFMD->GetName());
139 digitBranch->SetAddress(&digits);
141 Int_t nEvents = Int_t(digitTree->GetEntries());
142 for (Int_t event = 0; event < nEvents; event++) {
144 digitTree->GetEvent(event);
146 // Write out the digits
149 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 UShort_t threshold = 0;
163 UInt_t prevaddr = 0xFFF;
164 // UShort_t prevStrip = 0;
166 // Which channel number in the ALTRO channel we're at
168 UShort_t preSamples = 0;
170 // How many times the ALTRO Samples one VA1_ALICE channel
171 Int_t sampleRate = 1;
173 // A buffer to hold 1 ALTRO channel - Normally, one ALTRO channel
174 // holds 128 VA1_ALICE channels, sampled at a rate of `sampleRate'
175 TArrayI data(pars->GetChannelsPerAltro() * 8);
178 AliAltroBuffer* altro = 0;
180 // Loop over the digits in the event. Note, that we assume the
181 // the digits are in order in the branch. If they were not, we'd
182 // have to cache all channels before we could write the data to
183 // the ALTRO buffer, or we'd have to set up a map of the digits.
184 for (Int_t i = 0; i < nDigits; i++) {
186 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
188 UShort_t det = digit->Detector();
189 Char_t ring = digit->Ring();
190 UShort_t sector = digit->Sector();
191 UShort_t strip = digit->Strip();
194 threshold = pars->GetZeroSuppression(det, ring, sector, strip);
195 if (!pars->Detector2Hardware(det, ring, sector, strip, ddl, addr)) {
196 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
197 det, ring, sector, strip));
200 if (addr != prevaddr) {
201 // Flush a channel to output
202 AliDebug(15, Form("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
203 "(board 0x%x, chip 0x%x, channel 0x%x), flushing old "
204 "channel at 0x%x with %d words",
205 addr, det, ring, sector, strip,
206 (addr >> 7), (addr >> 4) & 0x7, addr & 0xf,
208 if (altro) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
211 for (size_t i = 0; i < nWords; i++) data[i] = digit->Count(0);
213 if (ddl != prevddl) {
214 AliDebug(15, Form("FMD: New DDL, was %d, now %d", prevddl, ddl));
215 // If an altro exists, delete the object, flushing the data to
216 // disk, and closing the file.
218 // When the first argument is false, we write the real
220 AliDebug(15, Form("Closing output"));
222 altro->WriteDataHeader(kFALSE, kFALSE);
227 // Need to open a new DDL!
228 TString filename(Form("%s_%d.ddl", fFMD->GetName(), ddl));
229 AliDebug(15, Form("New altro buffer with DDL file %s", filename.Data()));
230 // Create a new altro buffer - a `1' as the second argument
231 // means `write mode'
232 altro = new AliAltroBuffer(filename.Data(), 1);
233 altro->SetMapping(pars->GetAltroMap());
234 // Write a dummy (first argument is true) header to the DDL
235 // file - later on, when we close the file, we write the real
237 altro->WriteDataHeader(kTRUE, kFALSE);
240 // Store the counts of the ADC in the channel buffer
241 sampleRate = pars->GetSampleRate(det, ring, sector, strip);
242 for (int s = 0; s < sampleRate; s++) {
243 data[nWords] = digit->Count(s);
247 // Finally, we need to close the final ALTRO buffer if it wasn't
250 if (nWords > 0) altro->WriteChannel(prevaddr,nWords,data.fArray,threshold);
252 altro->WriteDataHeader(kFALSE, kFALSE);
257 //____________________________________________________________________
259 AliFMDRawWriter::WriteDigits(TClonesArray* digits)
261 Int_t nDigits = digits->GetEntries();
262 if (nDigits < 1) return;
264 AliFMDParameters* pars = AliFMDParameters::Instance();
265 AliFMDAltroWriter* writer = 0;
266 Int_t sampleRate = -1;
269 std::ofstream* file = 0;
271 for (Int_t i = 0; i < nDigits; i++) {
273 AliFMDDigit* digit = static_cast<AliFMDDigit*>(digits->At(i));
274 UInt_t thisDDL, thisHwaddr;
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 if (!pars->Detector2Hardware(det,ring,sector,strip,thisDDL,thisHwaddr)) {
280 AliError(Form("Failed to get hardware address for FMD%d%c[%2d,%3d]",
281 det, ring, sector, strip));
284 AliDebug(40, Form("Got DDL=%d and address=%d from FMD%d%c[%2d,%3d]",
285 thisDDL, thisHwaddr, det, ring, sector, strip));
286 // Check if we're still in the same channel
287 if (thisHwaddr != hwaddr) {
288 AliDebug(30, Form("Now hardware address 0x%x from FMD%d%c[%2d,%3d] "
289 "(board 0x%x, chip 0x%x, channel 0x%x)",
290 thisHwaddr, det, ring, sector, strip,
291 (thisHwaddr >> 7), (thisHwaddr >> 4) & 0x7,
293 if (writer) writer->AddChannelTrailer(hwaddr);
296 // Check if we're still in the same detector (DDL)
297 if (ddl != thisDDL) {
299 AliDebug(1, Form("Closing altro writer %p", writer));
300 if ((ret = writer->Close()) < 0) {
301 AliError(Form("Error: %s", writer->ErrorString(ret)));
312 // If we haven't got a writer (either because none were made so
313 // far, or because we've switch DDL), make one.
315 AliDebug(1, Form("Opening new ALTRO writer w/file FMD_%d.ddl", ddl));
316 file = new std::ofstream(Form("FMD_%d.ddl", ddl));
317 if (!file || !*file) {
318 AliFatal(Form("Failed to open file FMD_%d.ddl", ddl));
321 writer = new AliFMDAltroWriter(*file);
322 writer->SetThreshold(pars->GetZeroSuppression(det, ring, sector, strip));
324 // Write out our signal
325 sampleRate = pars->GetSampleRate(det,ring,sector,strip);
326 writer->AddSignal(digit->Count1());
327 if (sampleRate >= 2) writer->AddSignal(digit->Count2());
328 if (sampleRate >= 3) writer->AddSignal(digit->Count3());
331 writer->AddChannelTrailer(hwaddr);
343 //____________________________________________________________________