ROOT logo
// $Id$

/**************************************************************************
 * This file is property of and copyright by the ALICE HLT Project        * 
 * ALICE Experiment at CERN, All rights reserved.                         *
 *                                                                        *
 * Primary Authors: Matthias Richter <Matthias.Richter@ift.uib.no>        *
 *                  for The ALICE HLT Project.                            *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/

/** @file   testAliHLTAltroGenerator.C
    @author Matthias Richter
    @date   
    @brief  Test macro/program for the AliHLTAltroGenerator
 */

#ifndef __CINT__
#include "TSystem.h"
#include "AliHLTSystem.h"
#include "AliRawDataHeader.h"
#include "AliAltroDecoder.h"
#include "AliAltroData.h"
#include "AliAltroBunch.h"
#include "AliHLTAltroGenerator.h"
#include <ostream>
#endif //__CINT__

#ifndef __CINT__
const int sizeofAliRawDataHeader=sizeof(AliRawDataHeader);
#else
// cint does not handle sizeof correctly
const int sizeofAliRawDataHeader=32;
#endif

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
//
// configuration of the test program
//

// printouts or not
const bool bVerbose=false;

// some defaults
const int maxChannels=1000;
const int maxBunches=50;
const int maxBunchLength=10;
const int maxTimebin=1024;
const int maxSignal=1024;

/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////

int testAliHLTAltroGenerator()
{
  int iResult=0;
#ifdef __CINT__
  string ld_library_path="../.libs:";
  ld_library_path+=gSystem->GetDynamicPath();
  gSystem->SetDynamicPath(ld_library_path.c_str());
  gSystem->Load("libAliHLTRCU.so");
#endif
  AliHLTSystem gHLT;

  AliHLTAltroGenerator g(maxChannels, maxBunches, maxBunchLength, maxTimebin, maxSignal);
  //g.SetDirection(AliHLTAltroGenerator::kForwards);
  if ((iResult=g.Generate())<0) return iResult;

  if (bVerbose) {
    cout << "***************************************************************" << endl;
    cout << "************** Dumping simulated Altro data *******************" << endl;
    g.Print();
    cout << endl;
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////
  if (bVerbose) {
    g.Rewind();
    cout << "***************************************************************" << endl;
    cout << "********************** reading bunch model  *******************" << endl;
    while (iResult>=0 && g.NextChannel()) {
    cout << "***************************************************************" << endl;
      cout << "channel address: " << g.GetHwAddress() << "    " << g.GetBunchCount() << " bunch(es)" << endl;

      while (iResult>=0 && g.NextBunch()) {
	int bunchLength=g.GetBunchSize();
	cout << "   length " << bunchLength << " start time " << g.GetStartTime() << ":     ";
	const Short_t* pData=g.GetSignals();
	while (bunchLength-->0 && pData) {
	  cout << " " << *pData++;
	}
	cout << "      -> end time " << g.GetEndTime() << endl;
      }
    }
    cout << endl;
  }

  if (bVerbose) {
    g.Rewind();
    int lastChannel=-1;
    int lastTime=-1;
    cout << "***************************************************************" << endl;
    cout << "********************** reading stream model *******************" << endl;
    while (iResult>=0 && g.Next()) {
      if (lastTime>=0 && lastTime!=g.GetStartTime()+1 && lastTime!=g.GetStartTime()-1)
	cout << endl;

      if (lastChannel<0 || lastChannel!=g.GetHwAddress()) {
	cout << "***************************************************************" << endl;
	cout << "channel address: " << g.GetHwAddress() << endl;
      }

      if (lastTime<0 || (lastTime!=g.GetStartTime()+1 && lastTime!=g.GetStartTime()-1))
	cout << " time " << g.GetStartTime() << ":     ";

      cout << " " << g.GetSignal();

      lastChannel=g.GetHwAddress();
      lastTime=g.GetStartTime();
    }
    cout << endl;
    cout << endl;
  }

  ///////////////////////////////////////////////////////////////////////////////////////////////////
  AliRawDataHeader cdh;
  g.SetCDH(&cdh, 32);

  UInt_t trailer=0;
  g.SetRCUTrailer((UChar_t*)&trailer, 4);

  UChar_t* pBuffer=NULL;
  Int_t size=g.GetData(pBuffer);

  /*
  ios::openmode filemode=(ios::openmode)0;
  ofstream rawfile("/tmp/altro-enc.dat", filemode);
  if (rawfile.good()) {
    rawfile.write(reinterpret_cast<const char*>(pBuffer), size);
  }
  rawfile.close();
  */

  ///////////////////////////////////////////////////////////////////////////////////////////////////
  // can not have a static AltroDecoder, crash when function
  // is called. I had a similar problem in the AliHLTAltroChannelSelectorComponent

  if (bVerbose) {
    cout << "***************************************************************" << endl;
    cout << "********************* comparing encoded data ******************" << endl;
    cout << "***************************************************************" << endl;
  }

  g.Rewind();
  AliAltroDecoder* decoder=new AliAltroDecoder;
  if (iResult>=0 && decoder->SetMemory(pBuffer, size)<0) {
    cout << "error setting up decoder " << endl;
    iResult=-1;
  }

  if (iResult>=0 && !decoder->Decode()) {
    cout << "error decoding data" << endl;
    iResult=-1;    
  }

  AliAltroData altrochannel;
  while (iResult>=0 && decoder->NextChannel(&altrochannel)) {
    if (!g.NextChannel()) {
      cout << "error getting next simulated channel" << endl;
      iResult=-1;
      break;
    }
    int hwadd=altrochannel.GetHadd();
    if (hwadd!=g.GetHwAddress()) {
      cout << "channel address missmatch: simulated " << g.GetHwAddress() << " encoded " << hwadd << endl;
      iResult=-1;
      break;
    }

    if (bVerbose) cout << "comparing channel " << hwadd << endl;

    AliAltroBunch altrobunch;
    while (iResult>=0 && altrochannel.NextBunch(&altrobunch)) {
      if (!g.NextBunch()) {
	cout << "error getting bunch in simulated data" <<endl;
	iResult=-1;
	break;
      }
      int bunchLength=altrobunch.GetBunchSize();
      if (bunchLength!=(int)g.GetBunchSize()) {
	cout << "bunch length missmatch: simulated " << g.GetBunchSize() << " encoded " << bunchLength << hex << " (" << bunchLength << ")" << dec << endl;
	iResult=-1;
	break;
      }
      int bunchEndTime=altrobunch.GetEndTimeBin();
	if (bunchEndTime!=(int)g.GetEndTime()) {
	cout << "bunch end time missmatch: simulated " << g.GetEndTime() << " encoded " << bunchEndTime << endl;
	iResult=-1;
	break;
      }
      if (bVerbose) cout << " bunch length " << bunchLength << ", end time " << bunchEndTime << endl;
      const  UInt_t* bunchData=altrobunch.GetData();
      const  Short_t* simData=g.GetSignals();
      for (int bin=0; bin<bunchLength; bin++) {
	if ((Short_t)bunchData[bin]!=simData[bin]) {
	  cout << "data missmatch at bunch position " << bin << " : simulated " << simData[bin] << " encoded " << bunchData[bin] << endl;
	  iResult=-1;
	  break;
	}
      }
    }

  }
  
  delete decoder;

  return iResult;
}

int main(int /*argc*/, const char** /*argv*/)
{
  int iResult=0;
  // this test takes ~20 times longer than the testAliHLTAltroEncoder
  // no clue why, has to be traced with vtune
  int iCount=500;
  for (int i=0; i<iCount; i++) {
    if ((iResult=testAliHLTAltroGenerator())<0) {
      cout << "missmatch in cycle no " << i << endl;
      return iResult;
    }
  }
  cout << "checking: "<< iCount << " encoding cycle(s) successfully tested" << endl;
  return 0;
}
 testAliHLTAltroGenerator.C:1
 testAliHLTAltroGenerator.C:2
 testAliHLTAltroGenerator.C:3
 testAliHLTAltroGenerator.C:4
 testAliHLTAltroGenerator.C:5
 testAliHLTAltroGenerator.C:6
 testAliHLTAltroGenerator.C:7
 testAliHLTAltroGenerator.C:8
 testAliHLTAltroGenerator.C:9
 testAliHLTAltroGenerator.C:10
 testAliHLTAltroGenerator.C:11
 testAliHLTAltroGenerator.C:12
 testAliHLTAltroGenerator.C:13
 testAliHLTAltroGenerator.C:14
 testAliHLTAltroGenerator.C:15
 testAliHLTAltroGenerator.C:16
 testAliHLTAltroGenerator.C:17
 testAliHLTAltroGenerator.C:18
 testAliHLTAltroGenerator.C:19
 testAliHLTAltroGenerator.C:20
 testAliHLTAltroGenerator.C:21
 testAliHLTAltroGenerator.C:22
 testAliHLTAltroGenerator.C:23
 testAliHLTAltroGenerator.C:24
 testAliHLTAltroGenerator.C:25
 testAliHLTAltroGenerator.C:26
 testAliHLTAltroGenerator.C:27
 testAliHLTAltroGenerator.C:28
 testAliHLTAltroGenerator.C:29
 testAliHLTAltroGenerator.C:30
 testAliHLTAltroGenerator.C:31
 testAliHLTAltroGenerator.C:32
 testAliHLTAltroGenerator.C:33
 testAliHLTAltroGenerator.C:34
 testAliHLTAltroGenerator.C:35
 testAliHLTAltroGenerator.C:36
 testAliHLTAltroGenerator.C:37
 testAliHLTAltroGenerator.C:38
 testAliHLTAltroGenerator.C:39
 testAliHLTAltroGenerator.C:40
 testAliHLTAltroGenerator.C:41
 testAliHLTAltroGenerator.C:42
 testAliHLTAltroGenerator.C:43
 testAliHLTAltroGenerator.C:44
 testAliHLTAltroGenerator.C:45
 testAliHLTAltroGenerator.C:46
 testAliHLTAltroGenerator.C:47
 testAliHLTAltroGenerator.C:48
 testAliHLTAltroGenerator.C:49
 testAliHLTAltroGenerator.C:50
 testAliHLTAltroGenerator.C:51
 testAliHLTAltroGenerator.C:52
 testAliHLTAltroGenerator.C:53
 testAliHLTAltroGenerator.C:54
 testAliHLTAltroGenerator.C:55
 testAliHLTAltroGenerator.C:56
 testAliHLTAltroGenerator.C:57
 testAliHLTAltroGenerator.C:58
 testAliHLTAltroGenerator.C:59
 testAliHLTAltroGenerator.C:60
 testAliHLTAltroGenerator.C:61
 testAliHLTAltroGenerator.C:62
 testAliHLTAltroGenerator.C:63
 testAliHLTAltroGenerator.C:64
 testAliHLTAltroGenerator.C:65
 testAliHLTAltroGenerator.C:66
 testAliHLTAltroGenerator.C:67
 testAliHLTAltroGenerator.C:68
 testAliHLTAltroGenerator.C:69
 testAliHLTAltroGenerator.C:70
 testAliHLTAltroGenerator.C:71
 testAliHLTAltroGenerator.C:72
 testAliHLTAltroGenerator.C:73
 testAliHLTAltroGenerator.C:74
 testAliHLTAltroGenerator.C:75
 testAliHLTAltroGenerator.C:76
 testAliHLTAltroGenerator.C:77
 testAliHLTAltroGenerator.C:78
 testAliHLTAltroGenerator.C:79
 testAliHLTAltroGenerator.C:80
 testAliHLTAltroGenerator.C:81
 testAliHLTAltroGenerator.C:82
 testAliHLTAltroGenerator.C:83
 testAliHLTAltroGenerator.C:84
 testAliHLTAltroGenerator.C:85
 testAliHLTAltroGenerator.C:86
 testAliHLTAltroGenerator.C:87
 testAliHLTAltroGenerator.C:88
 testAliHLTAltroGenerator.C:89
 testAliHLTAltroGenerator.C:90
 testAliHLTAltroGenerator.C:91
 testAliHLTAltroGenerator.C:92
 testAliHLTAltroGenerator.C:93
 testAliHLTAltroGenerator.C:94
 testAliHLTAltroGenerator.C:95
 testAliHLTAltroGenerator.C:96
 testAliHLTAltroGenerator.C:97
 testAliHLTAltroGenerator.C:98
 testAliHLTAltroGenerator.C:99
 testAliHLTAltroGenerator.C:100
 testAliHLTAltroGenerator.C:101
 testAliHLTAltroGenerator.C:102
 testAliHLTAltroGenerator.C:103
 testAliHLTAltroGenerator.C:104
 testAliHLTAltroGenerator.C:105
 testAliHLTAltroGenerator.C:106
 testAliHLTAltroGenerator.C:107
 testAliHLTAltroGenerator.C:108
 testAliHLTAltroGenerator.C:109
 testAliHLTAltroGenerator.C:110
 testAliHLTAltroGenerator.C:111
 testAliHLTAltroGenerator.C:112
 testAliHLTAltroGenerator.C:113
 testAliHLTAltroGenerator.C:114
 testAliHLTAltroGenerator.C:115
 testAliHLTAltroGenerator.C:116
 testAliHLTAltroGenerator.C:117
 testAliHLTAltroGenerator.C:118
 testAliHLTAltroGenerator.C:119
 testAliHLTAltroGenerator.C:120
 testAliHLTAltroGenerator.C:121
 testAliHLTAltroGenerator.C:122
 testAliHLTAltroGenerator.C:123
 testAliHLTAltroGenerator.C:124
 testAliHLTAltroGenerator.C:125
 testAliHLTAltroGenerator.C:126
 testAliHLTAltroGenerator.C:127
 testAliHLTAltroGenerator.C:128
 testAliHLTAltroGenerator.C:129
 testAliHLTAltroGenerator.C:130
 testAliHLTAltroGenerator.C:131
 testAliHLTAltroGenerator.C:132
 testAliHLTAltroGenerator.C:133
 testAliHLTAltroGenerator.C:134
 testAliHLTAltroGenerator.C:135
 testAliHLTAltroGenerator.C:136
 testAliHLTAltroGenerator.C:137
 testAliHLTAltroGenerator.C:138
 testAliHLTAltroGenerator.C:139
 testAliHLTAltroGenerator.C:140
 testAliHLTAltroGenerator.C:141
 testAliHLTAltroGenerator.C:142
 testAliHLTAltroGenerator.C:143
 testAliHLTAltroGenerator.C:144
 testAliHLTAltroGenerator.C:145
 testAliHLTAltroGenerator.C:146
 testAliHLTAltroGenerator.C:147
 testAliHLTAltroGenerator.C:148
 testAliHLTAltroGenerator.C:149
 testAliHLTAltroGenerator.C:150
 testAliHLTAltroGenerator.C:151
 testAliHLTAltroGenerator.C:152
 testAliHLTAltroGenerator.C:153
 testAliHLTAltroGenerator.C:154
 testAliHLTAltroGenerator.C:155
 testAliHLTAltroGenerator.C:156
 testAliHLTAltroGenerator.C:157
 testAliHLTAltroGenerator.C:158
 testAliHLTAltroGenerator.C:159
 testAliHLTAltroGenerator.C:160
 testAliHLTAltroGenerator.C:161
 testAliHLTAltroGenerator.C:162
 testAliHLTAltroGenerator.C:163
 testAliHLTAltroGenerator.C:164
 testAliHLTAltroGenerator.C:165
 testAliHLTAltroGenerator.C:166
 testAliHLTAltroGenerator.C:167
 testAliHLTAltroGenerator.C:168
 testAliHLTAltroGenerator.C:169
 testAliHLTAltroGenerator.C:170
 testAliHLTAltroGenerator.C:171
 testAliHLTAltroGenerator.C:172
 testAliHLTAltroGenerator.C:173
 testAliHLTAltroGenerator.C:174
 testAliHLTAltroGenerator.C:175
 testAliHLTAltroGenerator.C:176
 testAliHLTAltroGenerator.C:177
 testAliHLTAltroGenerator.C:178
 testAliHLTAltroGenerator.C:179
 testAliHLTAltroGenerator.C:180
 testAliHLTAltroGenerator.C:181
 testAliHLTAltroGenerator.C:182
 testAliHLTAltroGenerator.C:183
 testAliHLTAltroGenerator.C:184
 testAliHLTAltroGenerator.C:185
 testAliHLTAltroGenerator.C:186
 testAliHLTAltroGenerator.C:187
 testAliHLTAltroGenerator.C:188
 testAliHLTAltroGenerator.C:189
 testAliHLTAltroGenerator.C:190
 testAliHLTAltroGenerator.C:191
 testAliHLTAltroGenerator.C:192
 testAliHLTAltroGenerator.C:193
 testAliHLTAltroGenerator.C:194
 testAliHLTAltroGenerator.C:195
 testAliHLTAltroGenerator.C:196
 testAliHLTAltroGenerator.C:197
 testAliHLTAltroGenerator.C:198
 testAliHLTAltroGenerator.C:199
 testAliHLTAltroGenerator.C:200
 testAliHLTAltroGenerator.C:201
 testAliHLTAltroGenerator.C:202
 testAliHLTAltroGenerator.C:203
 testAliHLTAltroGenerator.C:204
 testAliHLTAltroGenerator.C:205
 testAliHLTAltroGenerator.C:206
 testAliHLTAltroGenerator.C:207
 testAliHLTAltroGenerator.C:208
 testAliHLTAltroGenerator.C:209
 testAliHLTAltroGenerator.C:210
 testAliHLTAltroGenerator.C:211
 testAliHLTAltroGenerator.C:212
 testAliHLTAltroGenerator.C:213
 testAliHLTAltroGenerator.C:214
 testAliHLTAltroGenerator.C:215
 testAliHLTAltroGenerator.C:216
 testAliHLTAltroGenerator.C:217
 testAliHLTAltroGenerator.C:218
 testAliHLTAltroGenerator.C:219
 testAliHLTAltroGenerator.C:220
 testAliHLTAltroGenerator.C:221
 testAliHLTAltroGenerator.C:222
 testAliHLTAltroGenerator.C:223
 testAliHLTAltroGenerator.C:224
 testAliHLTAltroGenerator.C:225
 testAliHLTAltroGenerator.C:226
 testAliHLTAltroGenerator.C:227
 testAliHLTAltroGenerator.C:228
 testAliHLTAltroGenerator.C:229
 testAliHLTAltroGenerator.C:230
 testAliHLTAltroGenerator.C:231
 testAliHLTAltroGenerator.C:232
 testAliHLTAltroGenerator.C:233
 testAliHLTAltroGenerator.C:234
 testAliHLTAltroGenerator.C:235
 testAliHLTAltroGenerator.C:236
 testAliHLTAltroGenerator.C:237
 testAliHLTAltroGenerator.C:238
 testAliHLTAltroGenerator.C:239
 testAliHLTAltroGenerator.C:240
 testAliHLTAltroGenerator.C:241
 testAliHLTAltroGenerator.C:242
 testAliHLTAltroGenerator.C:243
 testAliHLTAltroGenerator.C:244
 testAliHLTAltroGenerator.C:245