1 |
/** @file |
/** @file |
2 |
* $Source: /home/cvsmanager/yoda/techmodel/CalibS4Reader.cpp,v $ |
* $Source: /afs/ba.infn.it/user/pamela/src/CVS/yoda/techmodel/CalibS4Reader.cpp,v $ |
3 |
* $Id: CalibS4Reader.cpp,v 1.3 2004/09/21 20:24:33 kusanagi Exp $ |
* $Id: CalibS4Reader.cpp,v 6.2 2006/05/30 19:10:03 kusanagi Exp $ |
4 |
* $Author: kusanagi $ |
* $Author: kusanagi $ |
5 |
* |
* |
6 |
* Implementation of the LogReader class. |
* Implementation of the LogReader class. |
8 |
* Control the CRC for the entire data Packet not just for single records |
* Control the CRC for the entire data Packet not just for single records |
9 |
*/ |
*/ |
10 |
|
|
11 |
#define UINT unsigned int |
|
|
#define BYTE unsigned char |
|
|
#include <string> |
|
|
#include <log4cxx/logger.h> |
|
12 |
extern "C" { |
extern "C" { |
|
#include <sys/time.h> |
|
13 |
#include "CRC.h" |
#include "CRC.h" |
14 |
} |
} |
|
|
|
|
#include <fstream> |
|
|
#include "stdio.h" |
|
15 |
#include "ReaderAlgorithms.h" |
#include "ReaderAlgorithms.h" |
16 |
|
|
|
#include "event/CalibS4Event.h" |
|
|
|
|
|
using namespace pamela; |
|
17 |
using namespace pamela::techmodel; |
using namespace pamela::techmodel; |
18 |
|
|
19 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.CalibS4Reader")); |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.CalibS4Reader")); |
24 |
CalibS4Reader::CalibS4Reader(void): |
CalibS4Reader::CalibS4Reader(void): |
25 |
TechmodelAlgorithm(PacketType::Log, "TechmodelCalibS4Reader") { |
TechmodelAlgorithm(PacketType::Log, "TechmodelCalibS4Reader") { |
26 |
logger->debug(_T("Constructor")); |
logger->debug(_T("Constructor")); |
27 |
CalibS4 = new CalibS4Event(); |
calibS4 = new CalibS4Event(); |
28 |
} |
} |
29 |
|
|
30 |
/** |
/** |
32 |
*/ |
*/ |
33 |
std::string CalibS4Reader::GetVersionInfo(void) const { |
std::string CalibS4Reader::GetVersionInfo(void) const { |
34 |
return |
return |
35 |
"$Header: /home/cvsmanager/yoda/techmodel/CalibS4Reader.cpp,v 1.3 2004/09/21 20:24:33 kusanagi Exp $\n"; |
"$Header: /afs/ba.infn.it/user/pamela/src/CVS/yoda/techmodel/CalibS4Reader.cpp,v 6.2 2006/05/30 19:10:03 kusanagi Exp $\n"; |
36 |
} |
} |
37 |
|
|
38 |
/** |
/** |
42 |
void CalibS4Reader::Init(PamelaRun *run) { |
void CalibS4Reader::Init(PamelaRun *run) { |
43 |
logger->debug(_T("Initialize")); |
logger->debug(_T("Initialize")); |
44 |
SetInputStream(run); |
SetInputStream(run); |
45 |
run->WriteSubPacket(this, &CalibS4, CalibS4->Class()); |
run->WriteSubPacket(this, &calibS4, calibS4->Class()); |
46 |
} |
} |
47 |
|
|
48 |
/** |
/** |
49 |
* Unpack the CalibS4 event from an input file. |
* Unpack the CalibS4 event from an input file. |
50 |
*/ |
*/ |
51 |
void CalibS4Reader::RunEvent(int EventNumber, long int length) { |
void CalibS4Reader::RunEvent(int EventNumber, long int length) throw (WrongCRCException){ |
52 |
char *subData; |
|
53 |
char eventCRC[2]; |
int offset; |
54 |
|
char subData[length]; //impulse*(11 + 10 + 01 + 00)*eventLength + CRC = 128*4*6 + UINT16 |
55 |
|
memset(subData, 0, length*sizeof(char)); |
56 |
UINT16 subCRC; //CRC of the data |
UINT16 subCRC; //CRC of the data |
57 |
UINT16 readCRC; //CRC read from the end of the subpacket |
UINT16 readCRC; //CRC read from the end of the subpacket |
58 |
long int dataLength; |
|
59 |
|
int numRecords = ((length - 2)/6); |
60 |
//the 2 bytes subtracted belong to the final event CRC bytes |
|
61 |
dataLength = length - (long int)2; |
S4::S4Event* rec; |
62 |
|
calibS4->Records->Clear(); |
63 |
subData = new char[dataLength]; |
TClonesArray &recs = *(calibS4->Records); |
64 |
InputFile->read(subData, sizeof(unsigned char)*dataLength); |
|
65 |
subCRC = CM_Compute_CRC16(0, (BYTE*)subData, dataLength); |
InputFile->read(subData, sizeof(unsigned char)*length); |
66 |
|
subCRC = CM_Compute_CRC16(0, (BYTE*)subData, length - 2); |
67 |
//took the final CRC to compare it with the previous calculated CRC of the data |
readCRC = (((UINT16)(subData[length - 2]<<8))&0xFF00) + (((UINT16)subData[length - 1])&0x00FF); |
|
InputFile->read(eventCRC, sizeof(eventCRC)); |
|
|
readCRC = (((UINT16)(eventCRC[0]<<8))&0xFF00) + (((UINT16)eventCRC[1])&0x00FF); |
|
68 |
|
|
69 |
if (subCRC == readCRC){ |
if (subCRC != readCRC) throw WrongCRCException(" Wrong CRC for CalibS4 Packet "); |
70 |
//put here the reader |
|
71 |
} else { |
for(int i = 0; i < numRecords; i++) { |
72 |
logger->warn(_T("Wrong CRC for CalibS4 Packet ")); |
offset = i*6; |
73 |
|
rec = new(recs[i]) S4::S4Event(); //add a new S4Event |
74 |
|
rec->S4_REG_STATUS = (((UINT8)subData[offset])&0xF0); |
75 |
|
rec->S4_DATA = ((((UINT16)subData[offset]<<8)&0x0FFF) + (((UINT16)subData[offset+1])&0x00FF)); |
76 |
|
rec->S4_CMD_NUM = (((UINT8)subData[offset+2])&0xFF); |
77 |
|
rec->S4_RESP_LENGHT = ((((UINT16)subData[offset+3]<<8)&0xFF00) + (((UINT16)subData[offset+4])&0x00FF)); |
78 |
|
rec->S4_OVERALL_CHKCODE = (((UINT8)subData[offset+5])&0xFF); |
79 |
} |
} |
|
|
|
80 |
} |
} |
81 |
|
|