| 1 | 
/** @file | 
| 2 | 
 * $Source: /home/cvsmanager/yoda/techmodel/CalibAcReader.cpp,v $ | 
| 3 | 
 * $Id: CalibAcReader.cpp,v 1.3 2004/08/24 16:01:57 kusanagi Exp $ | 
| 4 | 
 * $Author: kusanagi $ | 
| 5 | 
 *  | 
| 6 | 
 * Implementation of the ArrDumpReader class. | 
| 7 | 
 */ | 
| 8 | 
 | 
| 9 | 
#include <string> | 
| 10 | 
#include <log4cpp/Category.hh> | 
| 11 | 
#include <fstream> | 
| 12 | 
#include "stdio.h" | 
| 13 | 
extern "C" { | 
| 14 | 
    #include "CRC.h" | 
| 15 | 
    #include "forroutines/anticounter/ACcalib.h" | 
| 16 | 
    extern int ACcalib(int length, unsigned short* datapointer, struct datastruct* calibpointer); | 
| 17 | 
} | 
| 18 | 
 | 
| 19 | 
#include "ReaderAlgorithms.h" | 
| 20 | 
 | 
| 21 | 
using namespace pamela; | 
| 22 | 
using namespace pamela::techmodel; | 
| 23 | 
 | 
| 24 | 
 | 
| 25 | 
static log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.CalibAcReader");   | 
| 26 | 
 | 
| 27 | 
/** | 
| 28 | 
 * Constructor.  | 
| 29 | 
 */ | 
| 30 | 
CalibAcReader::CalibAcReader(void):  | 
| 31 | 
  TechmodelAlgorithm(PacketType::CalibAc, "TechmodelCalibAc") {  | 
| 32 | 
  CalibAc = new CalibAcEvent(); | 
| 33 | 
} | 
| 34 | 
 | 
| 35 | 
/** | 
| 36 | 
 * Get a string with the version info of the algorithm. | 
| 37 | 
 */ | 
| 38 | 
std::string CalibAcReader::GetVersionInfo(void) const { | 
| 39 | 
  return "$Header: /home/cvsmanager/yoda/techmodel/CalibAcReader.cpp,v 1.3 2004/08/24 16:01:57 kusanagi Exp $\n"; | 
| 40 | 
} | 
| 41 | 
 | 
| 42 | 
/** | 
| 43 | 
 * Initialize the algorithm with a special run. This will initialize the | 
| 44 | 
 * event reader routines for all packet types. | 
| 45 | 
 */ | 
| 46 | 
void CalibAcReader::Init(PamelaRun *run) { | 
| 47 | 
  SetInputStream(run); | 
| 48 | 
  run->WriteSubPacket(this, &CalibAc, CalibAc->Class()); | 
| 49 | 
} | 
| 50 | 
 | 
| 51 | 
/** | 
| 52 | 
 * Unpack the CalibAc event from an input file. | 
| 53 | 
 */ | 
| 54 | 
void CalibAcReader::RunEvent(int EventNumber, long int length) { | 
| 55 | 
   | 
| 56 | 
  char     *subData; | 
| 57 | 
  char      eventCRC[2]; | 
| 58 | 
  UINT16    subCRC;      //CRC of the data | 
| 59 | 
  UINT16    readCRC;     //CRC read from the end of the subpacket | 
| 60 | 
  long int  dataLength; | 
| 61 | 
   | 
| 62 | 
    //the 2 bytes subtracted belong to the final event CRC bytes | 
| 63 | 
    dataLength = length - (long int)2; | 
| 64 | 
 | 
| 65 | 
    subData = new char[dataLength]; | 
| 66 | 
    InputFile->read(subData, sizeof(unsigned char)*dataLength); | 
| 67 | 
    subCRC = CM_Compute_CRC16(0, (BYTE*)subData, dataLength); | 
| 68 | 
 | 
| 69 | 
    //took the final CRC to compare it with the previous calculated CRC of the data | 
| 70 | 
    InputFile->read(eventCRC, sizeof(eventCRC)); | 
| 71 | 
    readCRC = (((UINT16)(eventCRC[0]<<8))&0xFF00) + (((UINT16)eventCRC[1])&0x00FF); | 
| 72 | 
     | 
| 73 | 
    if (subCRC == readCRC){ | 
| 74 | 
        datastruct output; | 
| 75 | 
        int ERROR; | 
| 76 | 
        ERROR = ACcalib(dataLength, (unsigned short*)subData, &output); | 
| 77 | 
     | 
| 78 | 
        CalibAc->header = output.header; | 
| 79 | 
        memcpy(CalibAc->status,     output.status,     sizeof(CalibAc->status)); | 
| 80 | 
        memcpy(CalibAc->temp,       output.temp,       sizeof(CalibAc->temp)); | 
| 81 | 
        memcpy(CalibAc->DAC1,       output.DAC1,       sizeof(CalibAc->DAC1)); | 
| 82 | 
        memcpy(CalibAc->DAC2,       output.DAC2,       sizeof(CalibAc->DAC2)); | 
| 83 | 
        memcpy(CalibAc->regist,     output.regist,     sizeof(CalibAc->regist)); | 
| 84 | 
        memcpy(CalibAc->time,       output.time,       sizeof(CalibAc->time)); | 
| 85 | 
        CalibAc->n_tr = output.n_tr; | 
| 86 | 
        memcpy(CalibAc->hitmap_tr,  output.hitmap_tr,  sizeof(CalibAc->hitmap_tr)); | 
| 87 | 
        memcpy(CalibAc->curve1,     output.curve1,     sizeof(CalibAc->curve1)); | 
| 88 | 
        memcpy(CalibAc->curve2,     output.curve2,     sizeof(CalibAc->curve2)); | 
| 89 | 
        CalibAc->iCRC = output.iCRC; | 
| 90 | 
        CalibAc->tail = output.tail; | 
| 91 | 
        CalibAc->CRC  = output.CRC; | 
| 92 | 
    } else { | 
| 93 | 
        cat <<  log4cpp::Priority::ERROR | 
| 94 | 
            <<  "Wrong CRC on Subpacket in ArrDump Packet " | 
| 95 | 
            <<  "\n " << log4cpp::CategoryStream::ENDLINE; | 
| 96 | 
    } | 
| 97 | 
    delete [] subData; | 
| 98 | 
  } |