]> git.uio.no Git - u/mrichter/AliRoot.git/blob - HLT/RCU/test/testAliHLTAltroGenerator.C
8a9799cea737f1b466e5a28c6d0fb55a2a4e8c59
[u/mrichter/AliRoot.git] / HLT / RCU / test / testAliHLTAltroGenerator.C
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   testAliHLTAltroGenerator.C
20     @author Matthias Richter
21     @date   
22     @brief  Test macro/program for the AliHLTAltroGenerator
23  */
24
25 #ifndef __CINT__
26 #include "TSystem.h"
27 #include "AliHLTSystem.h"
28 #include "AliRawDataHeader.h"
29 #include "AliAltroDecoder.h"
30 #include "AliAltroData.h"
31 #include "AliAltroBunch.h"
32 #include "AliHLTAltroGenerator.h"
33 #include <ostream>
34 #endif //__CINT__
35
36 #ifndef __CINT__
37 const int sizeofAliRawDataHeader=sizeof(AliRawDataHeader);
38 #else
39 // cint does not handle sizeof correctly
40 const int sizeofAliRawDataHeader=32;
41 #endif
42
43 /////////////////////////////////////////////////////////////////
44 /////////////////////////////////////////////////////////////////
45 /////////////////////////////////////////////////////////////////
46 //
47 // configuration of the test program
48 //
49
50 // printouts or not
51 const bool bVerbose=false;
52
53 // some defaults
54 const int maxChannels=1000;
55 const int maxBunches=50;
56 const int maxBunchLength=10;
57 const int maxTimebin=1024;
58 const int maxSignal=1024;
59
60 /////////////////////////////////////////////////////////////////
61 /////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////
63
64 int testAliHLTAltroGenerator()
65 {
66   int iResult=0;
67 #ifdef __CINT__
68   gSystem->Load("libAliHLTRCU.so");
69 #endif
70   AliHLTSystem gHLT;
71
72   AliHLTAltroGenerator g(maxChannels, maxBunches, maxBunchLength, maxTimebin, maxSignal);
73   //g.SetDirection(AliHLTAltroGenerator::kForwards);
74   if ((iResult=g.Generate())<0) return iResult;
75
76   if (bVerbose) {
77     cout << "***************************************************************" << endl;
78     cout << "************** Dumping simulated Altro data *******************" << endl;
79     g.Print();
80     cout << endl;
81   }
82
83   ///////////////////////////////////////////////////////////////////////////////////////////////////
84   if (bVerbose) {
85     g.Rewind();
86     cout << "***************************************************************" << endl;
87     cout << "********************** reading bunch model  *******************" << endl;
88     while (iResult>=0 && g.NextChannel()) {
89     cout << "***************************************************************" << endl;
90       cout << "channel address: " << g.GetHwAddress() << "    " << g.GetBunchCount() << " bunch(es)" << endl;
91
92       while (iResult>=0 && g.NextBunch()) {
93         int bunchLength=g.GetBunchSize();
94         cout << "   length " << bunchLength << " start time " << g.GetStartTime() << ":     ";
95         const Short_t* pData=g.GetSignals();
96         while (bunchLength-->0 && pData) {
97           cout << " " << *pData++;
98         }
99         cout << "      -> end time " << g.GetEndTime() << endl;
100       }
101     }
102     cout << endl;
103   }
104
105   if (bVerbose) {
106     g.Rewind();
107     int lastChannel=-1;
108     int lastTime=-1;
109     cout << "***************************************************************" << endl;
110     cout << "********************** reading stream model *******************" << endl;
111     while (iResult>=0 && g.Next()) {
112       if (lastTime>=0 && lastTime!=g.GetStartTime()+1 && lastTime!=g.GetStartTime()-1)
113         cout << endl;
114
115       if (lastChannel<0 || lastChannel!=g.GetHwAddress()) {
116         cout << "***************************************************************" << endl;
117         cout << "channel address: " << g.GetHwAddress() << endl;
118       }
119
120       if (lastTime<0 || (lastTime!=g.GetStartTime()+1 && lastTime!=g.GetStartTime()-1))
121         cout << " time " << g.GetStartTime() << ":     ";
122
123       cout << " " << g.GetSignal();
124
125       lastChannel=g.GetHwAddress();
126       lastTime=g.GetStartTime();
127     }
128     cout << endl;
129     cout << endl;
130   }
131
132   ///////////////////////////////////////////////////////////////////////////////////////////////////
133   AliRawDataHeader cdh;
134   g.SetCDH(&cdh, 32);
135
136   UInt_t trailer=0;
137   g.SetRCUTrailer((UChar_t*)&trailer, 4);
138
139   UChar_t* pBuffer=NULL;
140   Int_t size=g.GetData(pBuffer);
141
142   /*
143   ios::openmode filemode=(ios::openmode)0;
144   ofstream rawfile("/tmp/altro-enc.dat", filemode);
145   if (rawfile.good()) {
146     rawfile.write(reinterpret_cast<const char*>(pBuffer), size);
147   }
148   rawfile.close();
149   */
150
151   ///////////////////////////////////////////////////////////////////////////////////////////////////
152   // can not have a static AltroDecoder, crash when function
153   // is called. I had a similar problem in the AliHLTAltroChannelSelectorComponent
154
155   if (bVerbose) {
156     cout << "***************************************************************" << endl;
157     cout << "********************* comparing encoded data ******************" << endl;
158     cout << "***************************************************************" << endl;
159   }
160
161   g.Rewind();
162   AliAltroDecoder* decoder=new AliAltroDecoder;
163   if (iResult>=0 && decoder->SetMemory(pBuffer, size)<0) {
164     cout << "error setting up decoder " << endl;
165     iResult=-1;
166   }
167
168   if (iResult>=0 && !decoder->Decode()) {
169     cout << "error decoding data" << endl;
170     iResult=-1;    
171   }
172
173   AliAltroData altrochannel;
174   while (iResult>=0 && decoder->NextChannel(&altrochannel)) {
175     if (!g.NextChannel()) {
176       cout << "error getting next simulated channel" << endl;
177       iResult=-1;
178       break;
179     }
180     int hwadd=altrochannel.GetHadd();
181     if (hwadd!=g.GetHwAddress()) {
182       cout << "channel address missmatch: simulated " << g.GetHwAddress() << " encoded " << hwadd << endl;
183       iResult=-1;
184       break;
185     }
186
187     if (bVerbose) cout << "comparing channel " << hwadd << endl;
188
189     AliAltroBunch altrobunch;
190     while (iResult>=0 && altrochannel.NextBunch(&altrobunch)) {
191       if (!g.NextBunch()) {
192         cout << "error getting bunch in simulated data" <<endl;
193         iResult=-1;
194         break;
195       }
196       int bunchLength=altrobunch.GetBunchSize();
197       if (bunchLength!=(int)g.GetBunchSize()) {
198         cout << "bunch length missmatch: simulated " << g.GetBunchSize() << " encoded " << bunchLength << hex << " (" << bunchLength << ")" << dec << endl;
199         iResult=-1;
200         break;
201       }
202       int bunchEndTime=altrobunch.GetEndTimeBin();
203         if (bunchEndTime!=(int)g.GetEndTime()) {
204         cout << "bunch end time missmatch: simulated " << g.GetEndTime() << " encoded " << bunchEndTime << endl;
205         iResult=-1;
206         break;
207       }
208       if (bVerbose) cout << " bunch length " << bunchLength << ", end time " << bunchEndTime << endl;
209       const  UInt_t* bunchData=altrobunch.GetData();
210       const  Short_t* simData=g.GetSignals();
211       for (int bin=0; bin<bunchLength; bin++) {
212         if ((Short_t)bunchData[bin]!=simData[bin]) {
213           cout << "data missmatch at bunch position " << bin << " : simulated " << simData[bin] << " encoded " << bunchData[bin] << endl;
214           iResult=-1;
215           break;
216         }
217       }
218     }
219
220   }
221   
222   delete decoder;
223
224   return 0;
225 }
226
227 int main(int /*argc*/, const char** /*argv*/)
228 {
229   int iResult=0;
230   // this test takes ~20 times longer than the testAliHLTAltroEncoder
231   // no clue why, has to be traced with vtune
232   int iCount=500;
233   for (int i=0; i<iCount; i++) {
234     if ((iResult=testAliHLTAltroGenerator())<0) {
235       cout << "missmatch in cycle no " << i << endl;
236       return iResult;
237     }
238   }
239   cout << "checking: "<< iCount << " encoding cycle(s) successfully tested" << endl;
240   return 0;
241 }