]> git.uio.no Git - u/mrichter/AliRoot.git/blame - FMD/AliFMDRawReader.cxx
Check-in to fix a few problems:
[u/mrichter/AliRoot.git] / FMD / AliFMDRawReader.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 AliFMDRawReader.cxx
17 @author Christian Holm Christensen <cholm@nbi.dk>
18 @date Mon Mar 27 12:45:23 2006
19 @brief Class to read raw data
02a27b50 20 @ingroup FMD_rec
c2fc1258 21*/
e802be3e 22//____________________________________________________________________
23//
24// Class to read ADC values from a AliRawReader object.
25//
7684b53c 26// This class uses the AliFMDRawStreamer class to read the ALTRO
27// formatted data.
28//
29// +-------+
30// | TTask |
31// +-------+
32// ^
33// |
34// +-----------------+ <<references>> +--------------+
35// | AliFMDRawReader |<>----------------| AliRawReader |
36// +-----------------+ +--------------+
37// | ^
38// | <<uses>> |
39// V |
40// +-----------------+ <<uses>> |
41// | AliFMDRawStream |------------------------+
42// +-----------------+
43// |
44// V
45// +----------------+
46// | AliAltroStream |
47// +----------------+
48//
f95a63c4 49// #include <AliLog.h> // ALILOG_H
50#include "AliFMDDebug.h" // Better debug macros
1a1fdef7 51#include "AliFMDParameters.h" // ALIFMDPARAMETERS_H
e802be3e 52#include "AliFMDDigit.h" // ALIFMDDIGIT_H
53#include "AliFMDRawStream.h" // ALIFMDRAWSTREAM_H
6169f936 54// #include "AliRawReader.h" // ALIRAWREADER_H
e802be3e 55#include "AliFMDRawReader.h" // ALIFMDRAWREADER_H
c2fc1258 56// #include "AliFMDAltroIO.h" // ALIFMDALTROIO_H
02a27b50 57// #include <TArrayI.h> // ROOT_TArrayI
1a1fdef7 58#include <TTree.h> // ROOT_TTree
59#include <TClonesArray.h> // ROOT_TClonesArray
02a27b50 60// #include <iostream>
61// #include <iomanip>
e802be3e 62
63//____________________________________________________________________
925e6570 64ClassImp(AliFMDRawReader)
1a1fdef7 65#if 0
66 ; // This is here to keep Emacs for indenting the next line
67#endif
e802be3e 68
69//____________________________________________________________________
1a1fdef7 70AliFMDRawReader::AliFMDRawReader(AliRawReader* reader, TTree* tree)
e802be3e 71 : TTask("FMDRawReader", "Reader of Raw ADC values from the FMD"),
1a1fdef7 72 fTree(tree),
73 fReader(reader),
74 fSampleRate(1)
e802be3e 75{
56b1929b 76 // Default CTOR
e802be3e 77}
78
c2fc1258 79//____________________________________________________________________
80void
81AliFMDRawReader::Exec(Option_t*)
82{
02a27b50 83 // Read the data
c2fc1258 84 TClonesArray* array = new TClonesArray("AliFMDDigit");
85 if (!fTree) {
86 AliError("No tree");
87 return;
88 }
89 fTree->Branch("FMD", &array);
90 ReadAdcs(array);
91 Int_t nWrite = fTree->Fill();
f95a63c4 92 AliFMDDebug(1, ("Got a grand total of %d digits, wrote %d bytes to tree",
c2fc1258 93 array->GetEntries(), nWrite));
94}
95
96
97#if 1
98//____________________________________________________________________
99Bool_t
100AliFMDRawReader::ReadAdcs(TClonesArray* array)
101{
102 // Read raw data into the digits array, using AliFMDAltroReader.
103 if (!array) {
104 AliError("No TClonesArray passed");
105 return kFALSE;
106 }
d56f2b53 107 // if (!fReader->ReadHeader()) {
108 // AliError("Couldn't read header");
109 // return kFALSE;
110 // }
c2fc1258 111 // Get sample rate
112 AliFMDParameters* pars = AliFMDParameters::Instance();
113 AliFMDRawStream input(fReader);
c2fc1258 114
115 UShort_t stripMin = 0;
116 UShort_t stripMax = 127;
117 UShort_t preSamp = 0;
118
119 UInt_t ddl = 0;
120 UInt_t rate = 0;
121 UInt_t last = 0;
122 UInt_t hwaddr = 0;
123 // Data array is approx twice the size needed.
124 UShort_t data[2048];
125 while (input.ReadChannel(ddl, hwaddr, last, data)) {
f95a63c4 126 AliFMDDebug(5, ("Read channel 0x%x of size %d", hwaddr, last));
c2fc1258 127 UShort_t det, sec, str;
128 Char_t ring;
129 if (!pars->Hardware2Detector(ddl, hwaddr, det, ring, sec, str)) {
130 AliError(Form("Failed to get detector id from DDL %d "
131 "and hardware address 0x%x", ddl, hwaddr));
132 continue;
133 }
134 rate = pars->GetSampleRate(det, ring, sec, str);
135 stripMin = pars->GetMinStrip(det, ring, sec, str);
136 stripMax = pars->GetMaxStrip(det, ring, sec, str);
f95a63c4 137 AliFMDDebug(5, ("DDL 0x%04x, address 0x%03x maps to FMD%d%c[%2d,%3d]",
c2fc1258 138 ddl, hwaddr, det, ring, sec, str));
139
140 // Loop over the `timebins', and make the digits
141 for (size_t i = 0; i < last; i++) {
142 if (i < preSamp) continue;
b29d3b0b 143 Int_t n = array->GetEntriesFast();
c2fc1258 144 UShort_t curStr = str + stripMin + i / rate;
145 if ((curStr-str) > stripMax) {
146 AliError(Form("Current strip is %d but DB says max is %d",
147 curStr, stripMax));
148 }
f95a63c4 149 AliFMDDebug(5, ("making digit for FMD%d%c[%2d,%3d] from sample %4d",
c2fc1258 150 det, ring, sec, curStr, i));
151 new ((*array)[n]) AliFMDDigit(det, ring, sec, curStr, data[i],
152 (rate >= 2 ? data[i+1] : 0),
153 (rate >= 3 ? data[i+2] : 0));
154 if (rate >= 2) i++;
155 if (rate >= 3) i++;
156 }
157 }
158 return kTRUE;
159}
160#else
1e8f773e 161//____________________________________________________________________
d760ea03 162Bool_t
163AliFMDRawReader::ReadAdcs(TClonesArray* array)
1e8f773e 164{
165 // Read raw data into the digits array, using AliFMDAltroReader.
d760ea03 166 if (!array) {
167 AliError("No TClonesArray passed");
168 return kFALSE;
169 }
d56f2b53 170 // if (!fReader->ReadHeader()) {
171 // AliError("Couldn't read header");
172 // return kFALSE;
173 // }
1e8f773e 174 // Get sample rate
175 AliFMDParameters* pars = AliFMDParameters::Instance();
176
177 // Select FMD DDL's
362c9d61 178 fReader->Select("FMD");
1e8f773e 179
180 UShort_t stripMin = 0;
181 UShort_t stripMax = 127;
182 UShort_t preSamp = 0;
183
184 do {
185 UChar_t* cdata;
186 if (!fReader->ReadNextData(cdata)) break;
187 size_t nchar = fReader->GetDataSize();
362c9d61 188 UShort_t ddl = fReader->GetDDLID();
c2fc1258 189 UShort_t rate = 0;
f95a63c4 190 AliFMDDebug(1, ("Reading %d bytes (%d 10bit words) from %d",
1e8f773e 191 nchar, nchar * 8 / 10, ddl));
192 // Make a stream to read from
193 std::string str((char*)(cdata), nchar);
194 std::istringstream s(str);
195 // Prep the reader class.
196 AliFMDAltroReader r(s);
197 // Data array is approx twice the size needed.
198 UShort_t data[2048], hwaddr, last;
199 while (r.ReadChannel(hwaddr, last, data) > 0) {
f95a63c4 200 AliFMDDebug(5, ("Read channel 0x%x of size %d", hwaddr, last));
1e8f773e 201 UShort_t det, sec, str;
202 Char_t ring;
203 if (!pars->Hardware2Detector(ddl, hwaddr, det, ring, sec, str)) {
204 AliError(Form("Failed to detector id from DDL %d "
205 "and hardware address 0x%x", ddl, hwaddr));
206 continue;
207 }
c2fc1258 208 rate = pars->GetSampleRate(det, ring, sec, str);
209 stripMin = pars->GetMinStrip(det, ring, sec, str);
210 stripMax = pars->GetMaxStrip(det, ring, sec, str);
f95a63c4 211 AliFMDDebug(5, ("DDL 0x%04x, address 0x%03x maps to FMD%d%c[%2d,%3d]",
1e8f773e 212 ddl, hwaddr, det, ring, sec, str));
213
214 // Loop over the `timebins', and make the digits
215 for (size_t i = 0; i < last; i++) {
216 if (i < preSamp) continue;
d760ea03 217 Int_t n = array->GetEntries();
1e8f773e 218 UShort_t curStr = str + stripMin + i / rate;
219 if ((curStr-str) > stripMax) {
220 AliError(Form("Current strip is %d but DB says max is %d",
221 curStr, stripMax));
222 }
f95a63c4 223 AliFMDDebug(5, ("making digit for FMD%d%c[%2d,%3d] from sample %4d",
1e8f773e 224 det, ring, sec, curStr, i));
225 new ((*array)[n]) AliFMDDigit(det, ring, sec, curStr, data[i],
226 (rate >= 2 ? data[i+1] : 0),
227 (rate >= 3 ? data[i+2] : 0));
228 if (rate >= 2) i++;
229 if (rate >= 3) i++;
230 }
231 if (r.IsBof()) break;
232 }
233 } while (true);
d760ea03 234 return kTRUE;
235}
236
237
238
1e8f773e 239// This is the old method, for comparison. It's really ugly, and far
240// too convoluted.
e802be3e 241//____________________________________________________________________
242void
243AliFMDRawReader::Exec(Option_t*)
244{
56b1929b 245 // Read raw data into the digits array
d56f2b53 246 // if (!fReader->ReadHeader()) {
247 // Error("ReadAdcs", "Couldn't read header");
248 // return;
249 // }
e802be3e 250
1a1fdef7 251 Int_t n = 0;
252 TClonesArray* array = new TClonesArray("AliFMDDigit");
253 fTree->Branch("FMD", &array);
254
8f6ee336 255 // Get sample rate
256 AliFMDParameters* pars = AliFMDParameters::Instance();
362c9d61 257 fSampleRate = pars->GetSampleRate(0);
8f6ee336 258
e802be3e 259 // Use AliAltroRawStream to read the ALTRO format. No need to
260 // reinvent the wheel :-)
261 AliFMDRawStream input(fReader, fSampleRate);
262 // Select FMD DDL's
362c9d61 263 fReader->Select("FMD");
e802be3e 264
265 Int_t oldDDL = -1;
266 Int_t count = 0;
267 UShort_t detector = 1; // Must be one here
268 UShort_t oldDetector = 0;
269 Bool_t next = kTRUE;
270
271 // local Cache
272 TArrayI counts(10);
273 counts.Reset(-1);
274
275 // Loop over data in file
276 while (next) {
277 next = input.Next();
278
279 count++;
280 Int_t ddl = fReader->GetDDLID();
f95a63c4 281 AliFMDDebug(10, ("Current DDL is %d", ddl));
e802be3e 282 if (ddl != oldDDL || input.IsNewStrip() || !next) {
283 // Make a new digit, if we have some data (oldDetector == 0,
284 // means that we haven't really read anything yet - that is,
285 // it's the first time we get here).
286 if (oldDetector > 0) {
287 // Got a new strip.
f95a63c4 288 AliFMDDebug(10, ("Add a new strip: FMD%d%c[%2d,%3d] "
e802be3e 289 "(current: FMD%d%c[%2d,%3d])",
290 oldDetector, input.PrevRing(),
291 input.PrevSector() , input.PrevStrip(),
292 detector , input.Ring(), input.Sector(),
293 input.Strip()));
1a1fdef7 294 new ((*array)[n]) AliFMDDigit(oldDetector,
295 input.PrevRing(),
296 input.PrevSector(),
297 input.PrevStrip(),
298 counts[0], counts[1], counts[2]);
299 n++;
e802be3e 300#if 0
301 AliFMDDigit* digit =
302 static_cast<AliFMDDigit*>(fFMD->Digits()->
303 UncheckedAt(fFMD->GetNdigits()-1));
304#endif
305 }
306
307 if (!next) {
f95a63c4 308 AliFMDDebug(10, ("Read %d channels for FMD%d",
e802be3e 309 count + 1, detector));
310 break;
311 }
312
313
314 // If we got a new DDL, it means we have a new detector.
315 if (ddl != oldDDL) {
316 if (detector != 0)
f95a63c4 317 AliFMDDebug(10, ("Read %d channels for FMD%d", count + 1, detector));
e802be3e 318 // Reset counts, and update the DDL cache
319 count = 0;
320 oldDDL = ddl;
321 // Check that we're processing a FMD detector
322 Int_t detId = fReader->GetDetectorID();
362c9d61 323 if (detId != (AliDAQ::DetectorID("FMD"))) {
1e8f773e 324 AliError(Form("Detector ID %d != %d",
362c9d61 325 detId, (AliDAQ::DetectorID("FMD"))));
e802be3e 326 break;
327 }
328 // Figure out what detector we're deling with
329 oldDetector = detector;
330 switch (ddl) {
331 case 0: detector = 1; break;
332 case 1: detector = 2; break;
333 case 2: detector = 3; break;
334 default:
1e8f773e 335 AliError(Form("Unknown DDL 0x%x for FMD", ddl));
e802be3e 336 return;
337 }
f95a63c4 338 AliFMDDebug(10, ("Reading ADCs for 0x%x - That is FMD%d",
e802be3e 339 fReader->GetEquipmentId(), detector));
340 }
341 counts.Reset(-1);
342 }
343
344 counts[input.Sample()] = input.Count();
345
f95a63c4 346 AliFMDDebug(10, ("ADC of FMD%d%c[%2d,%3d] += %d",
e802be3e 347 detector, input.Ring(), input.Sector(),
348 input.Strip(), input.Count()));
349 oldDetector = detector;
350 }
1a1fdef7 351 fTree->Fill();
e802be3e 352 return;
353
354}
1e8f773e 355#endif
e802be3e 356
357//____________________________________________________________________
358//
359// EOF
360//