minor bugfix: correctly close ESD files in destructor
[u/mrichter/AliRoot.git] / HLT / rec / test / testAliRawReaderFile.C
CommitLineData
c4532b95 1// $Id$
2
3/**************************************************************************
4 * This file is property of and copyright by the ALICE HLT Project *
5 * ALICE Experiment at CERN, All rights reserved. *
6 * *
7 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no> *
8 * for The ALICE HLT Project. *
9 * *
10 * Permission to use, copy, modify and distribute this software and its *
11 * documentation strictly for non-commercial purposes is hereby granted *
12 * without fee, provided that the above copyright notice appears in all *
13 * copies and that both the copyright notice and this permission notice *
14 * appear in the supporting documentation. The authors make no claims *
15 * about the suitability of this software for any purpose. It is *
16 * provided "as is" without express or implied warranty. *
17 **************************************************************************/
18
19/** @file testAliRawReaderMemory.C
20 @author Matthias Richter
21 @date
22 @brief Test macro/program for the AliRawReaderMemory
23 */
24
25#ifndef __CINT__
26#include "TFile.h"
27#include "TDatime.h"
28#include "TRandom.h"
29#include "TArrayI.h"
30#include "TArrayC.h"
31#include "TSystem.h"
32#include "AliRawDataHeader.h"
33#include "AliRawReaderFile.h"
34#include "AliDAQ.h"
35#include "AliHLTSystem.h"
36#include <ostream>
37#endif //__CINT__
38
39#ifndef __CINT__
40const int sizeofAliRawDataHeader=sizeof(AliRawDataHeader);
41#else
42// cint does not handle sizeof correctly
43const int sizeofAliRawDataHeader=32;
44#endif
45
46/////////////////////////////////////////////////////////////////
47/////////////////////////////////////////////////////////////////
48/////////////////////////////////////////////////////////////////
49//
50// configuration of the test program
51//
52const char* tmpdir="/tmp";
53const int nofDDLs=2;
54
55
56/////////////////////////////////////////////////////////////////
57/////////////////////////////////////////////////////////////////
58/////////////////////////////////////////////////////////////////
59int GetRandom(int min, int max);
60int FillRandomDDL(TArrayC& target);
61int WriteDDL(TArrayC& ddl, int ddlid);
62int CheckDDL_ReadNext(AliRawReader* pRawReader, TArrayC& ddl, int ddlid);
63int CheckDDL_ReadNextData(AliRawReader* pRawReader, TArrayC& ddl, int ddlid);
64int CheckDDL_ReadNextChar(AliRawReader* pRawReader, TArrayC& ddl, int ddlid);
65
66int testAliRawReaderMemory()
67{
68 int iResult=0;
69 TArrayC ddl[nofDDLs];
70 int ddlid[nofDDLs]={768, 769};
71
72 for (int i=0; i<nofDDLs; i++) {
73 FillRandomDDL(ddl[i]);
74 WriteDDL(ddl[i], ddlid[i]);
75 }
76
77 TString rawdir=tmpdir;
78 if (!rawdir.EndsWith("/")) rawdir+="/";
79 AliRawReader* pRawReader=AliRawReader::Create(rawdir);
80 if (!pRawReader) {
81 cerr << "can not create RawReader" << endl;
82 return -1;
83 }
84
85 if (!pRawReader->NextEvent()) {
86 cerr << "error: getting event failed" << endl;
87 return -1;
88 }
89
90 // 1st pass: check AliRawReader::ReadNext()
91 while (pRawReader->ReadHeader()) {
92 int id=pRawReader->GetEquipmentId();
93 int i=0;
94 for (i=0; i<nofDDLs; i++) {
95 if (ddlid[i]==id) break;
96 }
97 if (i==nofDDLs) {
98 cerr << "error: can not find ddl id " << id << endl;
99 return -1;
100 }
101
102 if ((iResult=CheckDDL_ReadNext(pRawReader, ddl[i], ddlid[i]))<0) return iResult;
103 }
104
105 // 2nd pass: check AliRawReader::ReadNextData()
106 pRawReader->Reset();
107 while (pRawReader->ReadHeader()) {
108 int id=pRawReader->GetEquipmentId();
109 int i=0;
110 for (i=0; i<nofDDLs; i++) {
111 if (ddlid[i]==id) break;
112 }
113 if (i==nofDDLs) {
114 cerr << "error: can not find ddl id " << id << endl;
115 return -1;
116 }
117
118 if ((iResult=CheckDDL_ReadNextData(pRawReader, ddl[i], ddlid[i]))<0) return iResult;
119 }
120
121 // 3rd pass: check AliRawReader::ReadNextData()
122 pRawReader->Reset();
123 while (pRawReader->ReadHeader()) {
124 int i=0;
125 do {
126 int id=pRawReader->GetEquipmentId();
127 for (i=0; i<nofDDLs; i++) {
128 if (ddlid[i]==id) break;
129 }
130 if (i==nofDDLs) {
131 cerr << "error: can not find ddl id " << id << endl;
132 return -1;
133 }
134
135 if ((iResult=CheckDDL_ReadNextChar(pRawReader, ddl[i], ddlid[i]))<0) return iResult;
136 }
137 // note: the ReadHeader is hidden in the ReadNextChar
138 while (iResult!=ddlid[i]);
139 }
140
141 delete pRawReader;
142 return 0;
143}
144
145Bool_t seedSet=kFALSE;
146
147/**
148 * Get a random number in the given range.
149 */
150int GetRandom(int min, int max)
151{
152 if (max-min<2) return min;
153 static TRandom rand;
154 if (!seedSet) {
155 TDatime dt;
156 rand.SetSeed(dt.Get());
157 seedSet=kTRUE;
158 }
159 return rand.Integer(max-min);
160}
161
162int FillRandomDDL(TArrayC& target)
163{
164 int size=0;
165 do size=GetRandom(100, 10000);
166 while (size<100);
167
168 target.Set(size);
169 *((Int_t*)target.GetArray())=size;
170 for (int i=sizeofAliRawDataHeader; i<size; i++) {
171 Int_t data=GetRandom(0,255);
172 target.AddAt((UChar_t)data, i);
173 }
174 return size;
175}
176
177int WriteDDL(TArrayC& ddl, int ddlid)
178{
179 Int_t dummy=0;
180 TString filename;
181 filename.Form("%s/raw0", tmpdir);
182 gSystem->mkdir(filename);
183 filename.Form("%s/%s_%d.ddl", filename.Data(), AliDAQ::DetectorNameFromDdlID(ddlid, dummy), ddlid);
184 FILE* fp=fopen(filename.Data(), "w");
185 if (!fp) return -1;
186
187 fwrite(ddl.GetArray(), 1, ddl.GetSize(), fp);
188 fclose(fp);
189
190 return 0;
191}
192
193int CheckDDL_ReadNext(AliRawReader* pRawReader, TArrayC& ddl, int ddlid)
194{
195 int dataSize=pRawReader->GetDataSize();
196 if (ddl.GetSize()!=dataSize+sizeofAliRawDataHeader) {
197 cerr << "error: size mismatch in ddl " << ddlid << ": " << dataSize+sizeofAliRawDataHeader << " required " << ddl.GetSize() << endl;
198 return -1;
199 }
200 TArrayC buffer(dataSize);
201 UChar_t* pTgt=(UChar_t*)buffer.GetArray();
202
203 if (!pRawReader->ReadNext(pTgt, buffer.GetSize())) {
204 cerr << "error: reading " << buffer.GetSize() << " byte(s) from ReadNext (ddl " << ddlid << ")" << endl;
205 return -1;
206 }
207
208 cout << "verify ReadNext: ddl " << ddlid << endl;
209 if (memcmp(ddl.GetArray()+sizeofAliRawDataHeader, buffer.GetArray(), buffer.GetSize())!=0) {
210 cerr << "error: verification of ddl " << ddlid << ")" << endl;
211 return -1;
212 }
213 return 0;
214}
215
216int CheckDDL_ReadNextData(AliRawReader* pRawReader, TArrayC& ddl, int ddlid)
217{
218 int dataSize=pRawReader->GetDataSize();
219 if (ddl.GetSize()!=dataSize+sizeofAliRawDataHeader) {
220 cerr << "error: size mismatch in ddl " << ddlid << ": " << dataSize+sizeofAliRawDataHeader << " required " << ddl.GetSize() << endl;
221 return -1;
222 }
223
224 UChar_t* pTgt=NULL;
225 if (!pRawReader->ReadNextData(pTgt) || pTgt==NULL) {
226 cerr << "error: reading " << dataSize << " byte(s) from ReadNext (ddl " << ddlid << ")" << endl;
227 return -1;
228 }
229
230 cout << "verify ReadNextData: ddl " << ddlid << endl;
231 if (memcmp(ddl.GetArray()+sizeofAliRawDataHeader, pTgt, dataSize)!=0) {
232 cerr << "error: verification of ddl " << ddlid << endl;
233 return -1;
234 }
235 return 0;
236}
237
238int CheckDDL_ReadNextChar(AliRawReader* pRawReader, TArrayC& ddl, int ddlid)
239{
240 int iResult=ddlid;
241 int dataSize=pRawReader->GetDataSize();
242 if (ddl.GetSize()!=dataSize+sizeofAliRawDataHeader) {
243 cerr << "error: size mismatch in ddl " << ddlid << ": " << dataSize+sizeofAliRawDataHeader << " required " << ddl.GetSize() << endl;
244 return -1;
245 }
246
247 cout << "verify ReadNextChar: ddl " << ddlid << endl;
248 static int offset=0;
249 int i=sizeofAliRawDataHeader+offset;
250 UChar_t data=0;
251 Bool_t haveData=kFALSE;
252 for (; (haveData=pRawReader->ReadNextChar(data)) && i<ddl.GetSize(); i++) {
253 if (data!=(UChar_t)ddl.At(i)) {
254 cerr << "error: at position " << i << " of ddl " << ddlid << ": read " << (UShort_t)data << " required " << (UShort_t)(UChar_t)ddl.At(i) << endl;
255 return -1;
256 }
257 }
258
259 if (i<ddl.GetSize()) {
260 cerr << "error: reading data of ddl " << ddlid << ": " << i << " of " << ddl.GetSize() << endl;
261 return -1;
262 }
263 if (pRawReader->GetEquipmentId()>=0 && ddlid!=pRawReader->GetEquipmentId()) {
264 //cerr << "error: ddl id missmatch, expecting " << ddlid << " got " << pRawReader->GetEquipmentId() << endl;
265 // thats not an error condition, RawReader just changes silently to the next DDL
266 iResult=pRawReader->GetEquipmentId();
267 offset=1;
268 } else {
269 offset=0;
270 }
271 if (haveData && iResult==ddlid) {
272 cerr << "error: size missmatch in ddl " << ddlid << ": still data available after " << ddl.GetSize() << " byte(s)"<< endl;
273 return -1;
274 }
275 return iResult;
276}
277
278int main(int /*argc*/, const char** /*argv*/)
279{
280 return testAliRawReaderMemory();
281}