| 1 | /** @file | 
| 2 | * $Source: /home/cvsmanager/yoda/techmodel/physics/S4Reader.cpp,v $ | 
| 3 | * $Id: S4Reader.cpp,v 2.5 2005/02/11 16:40:19 kusanagi Exp $ | 
| 4 | * $Author: kusanagi $ | 
| 5 | * | 
| 6 | * Implementation of the S4Reader class. | 
| 7 | */ | 
| 8 |  | 
| 9 | #include <string> | 
| 10 | #include <log4cxx/logger.h> | 
| 11 | #include "S4Reader.h" | 
| 12 |  | 
| 13 | using namespace pamela; | 
| 14 | using namespace pamela::S4; | 
| 15 |  | 
| 16 | static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.S4.S4Reader")); | 
| 17 |  | 
| 18 | /** | 
| 19 | * Constructor. | 
| 20 | */ | 
| 21 | S4Reader::S4Reader(void): | 
| 22 | TechmodelAlgorithm(PacketType::Physics, "TechmodelS4Reader") { | 
| 23 | logger->debug(_T("Constructor")); | 
| 24 | s4 = new S4Event(); | 
| 25 | } | 
| 26 |  | 
| 27 | /** | 
| 28 | * Get a string with the version info of the algorithm. | 
| 29 | */ | 
| 30 | std::string S4Reader::GetVersionInfo(void) const { | 
| 31 | return | 
| 32 | "$Header: /home/cvsmanager/yoda/techmodel/physics/S4Reader.cpp,v 2.5 2005/02/11 16:40:19 kusanagi Exp $"; | 
| 33 | } | 
| 34 |  | 
| 35 | /** | 
| 36 | * Initialize the algorithm with a special run. This will initialize the | 
| 37 | * event reader routines for all packet types. | 
| 38 | */ | 
| 39 | void S4Reader::Init(PamelaRun *run) { | 
| 40 | logger->debug(_T("Initialize")); | 
| 41 | SetInputStream(run); | 
| 42 | run->WriteSubPacket(this, &s4, s4->Class()); | 
| 43 | } | 
| 44 |  | 
| 45 | /** | 
| 46 | * Unpack the S4 event from an input file. | 
| 47 | */ | 
| 48 | void S4Reader::RunEvent(int EventNumber) { | 
| 49 |  | 
| 50 | } | 
| 51 |  | 
| 52 | /** | 
| 53 | * Unpack the S4 data event from the physical packet. | 
| 54 | * Unfortunately the only definition available for S4 is "Rigth before the Neutron detector data" | 
| 55 | * consequently supposing the Neutron data (12 Bytes) always present S4 start 18 Bytes before the end of the packet | 
| 56 | */ | 
| 57 | void S4Reader::RunEvent(int EventNumber, const char subData[], long int length) { | 
| 58 | const int lenS4Data = 6; | 
| 59 | char *data = new char[length]; | 
| 60 | int offset; | 
| 61 | int index = haveData(data, length); | 
| 62 |  | 
| 63 | if (index > -1){ | 
| 64 | int offset = length - index; | 
| 65 | s4->S4_REG_STATUS        = (((UINT8)subData[offset])&0xF0); | 
| 66 | s4->S4_DATA              = ((((UINT16)subData[offset]<<8)&0x0FFF) + (((UINT16)subData[offset+1])&0x00FF)); | 
| 67 | s4->S4_CMD_NUM           = (((UINT8)subData[offset+2])&0xFF); | 
| 68 | s4->S4_RESP_LENGHT       = ((((UINT16)subData[offset+3]<<8)&0xFF00) + (((UINT16)subData[offset+4])&0x00FF)); | 
| 69 | s4->S4_OVERALL_CHKCODE   = (((UINT8)subData[offset+5])&0xFF); | 
| 70 | s4->unpackError          = 0; | 
| 71 | } else { | 
| 72 | s4->unpackError = 1; | 
| 73 | } | 
| 74 | delete[] data; | 
| 75 | } | 
| 76 |  | 
| 77 | /* | 
| 78 | * | 
| 79 | * @return int | 
| 80 | * It return the index, starting from the end of data[] parameter, which | 
| 81 | * is the starting byte of the S4 data. | 
| 82 | * | 
| 83 | */ | 
| 84 | /** | 
| 85 | * Check if S4Data are present. | 
| 86 | * @param const char data[] - Physics data | 
| 87 | * @param long int lenght - Lenght of data[] | 
| 88 | * @return int - -1 if S4 has not been found, otherwise it represent the | 
| 89 | *               index from the end of data[] from which S4 data starts | 
| 90 | */ | 
| 91 | int S4Reader::haveData(const char data[], long int length){ | 
| 92 | int ret = -1; | 
| 93 | /* I start 20 bytes from the end because we have three cases: | 
| 94 | * | 
| 95 | * 1) S4 (4bytes) + NeutronDetector (12bytes) //END PACKET | 
| 96 | * 2) S4 (4bytes) //END PACKET | 
| 97 | * 3) //END PACKET | 
| 98 | * | 
| 99 | * so in anycase I expect to find the data into the last 20 bytes | 
| 100 | * Anyway, it will be a MUST to implements also the check of the CRC | 
| 101 | * on the S4 data to have a better confidence on the data; the crc algorithm | 
| 102 | * sholud be the same of the Tracker | 
| 103 | * | 
| 104 | */ | 
| 105 | int  index = 0; | 
| 106 | char dataByte; | 
| 107 | int subIndex = 0; | 
| 108 | const int subDataDepth = 20; | 
| 109 | int offset = length - subDataDepth; | 
| 110 | const int subSignDim = 3; | 
| 111 | const unsigned char subSign[subSignDim]={0xD8, 0x00, 0x03}; | 
| 112 | while (index < subDataDepth){ | 
| 113 | dataByte = data[offset + index++]; | 
| 114 | if (dataByte == (char)(*(subSign+subIndex))){ | 
| 115 | if (subIndex++ == (subSignDim-1)) { | 
| 116 | //If here i catch it!!! | 
| 117 | return subDataDepth - index + subSignDim + 2; | 
| 118 | } | 
| 119 | } else { | 
| 120 | index = index - (subIndex); | 
| 121 | subIndex = 0; | 
| 122 | } | 
| 123 | } | 
| 124 | return ret; | 
| 125 | } |