/** @file * $Author: kusanagi $ * $Date: 2004/09/24 11:57:49 $ * $Revision: 2.2 $ * * Implementation of the functions of a sample Algorithm class. * This file can be used as a templace to develop your own algorithm. */ #include #include #include "TechmodelAlgorithm.h" #include "EventReader.h" #include "ReaderAlgorithms.h" #include "Exception.h" #include "stdio.h" extern "C" { #include "CRC.h" } #define BYTE unsigned char using namespace pamela; using namespace pamela::techmodel; static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.EventReader")); unsigned int EventReader::maxPackets = 0; unsigned int EventReader::prevPckCounter = 0; unsigned int EventReader::prevPckOBT = 0; /** * Constructor. */ EventReader::EventReader(int packetsLimit = -1): TechmodelAlgorithm(0, "TechmodelEventReader"){ //EventReader::EventReader(void): // TechmodelAlgorithm(0, "TechmodelEventReader") { EventReader::maxPackets = packetsLimit; logger->debug(_T("Constructor")); Header = new EventHeader(); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun, new PhysEndRunReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse1, new CalibCalPulse1Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse2, new CalibCalPulse2Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Physics, new PhysicsReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrkBoth, new CalibTrkBothReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk1, new CalibTrk1Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk2, new CalibTrk2Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrd, new CalibTrdReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTof, new CalibTofReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibS4, new CalibS4Reader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPed, new CalibCalPedReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibAc, new CalibAcReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunHeader, new RunHeaderReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunTrailer, new RunTrailerReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibHeader, new CalibHeaderReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrailer, new CalibTrailerReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitHeader, new InitHeaderReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer, new InitTrailerReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk, new EventTrkReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk, new TestTrkReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTof, new TestTofReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TabDump, new TabDumpReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Tmtc, new TmtcReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Mcmd, new McmdReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ForcedFECmd, new ForcedFECmdReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcInit, new AcInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalInit, new CalInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit, new TrkInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit, new TofInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new TrgInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new NdInitReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalAlarm, new CalAlarmReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcAlarm, new AcAlarmReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm, new TrkAlarmReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm, new TrgAlarmReader())); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm, new TofAlarmReader())); } /** * Get a string with the version info of the algorithm. */ std::string EventReader::GetVersionInfo(void) const { return "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 2.2 2004/09/24 11:57:49 kusanagi Exp $\n"; } /** * Initialize the algorithm with a special run. This will initialize the * event reader routines for all packet types. */ void EventReader::Init(PamelaRun *run) { std::stringstream oss; logger->debug(_T("Initialize")); SetInputStream(run); //Create the structure of directories and create xxx.Header.root files run->WriteHeaders(this, &Header); //Create the xxx.root in it's specific directory for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); i != TechmodelAlgorithmMap.end(); i++) { oss.flush(); oss << "Initializing algo " << i->second->GetAlgorithmName(); //cout << "Initializing algo " << i->second->GetAlgorithmName(); logger->debug(oss.str().c_str()); i->second->Init(run); } Run = dynamic_cast(run); } static void SkipToNextHeader(ifstream *); /** * Read the next event header, call the reader algorithms that * correspond to its packet type, and read the event trailer. */ // 15 June 2004 ---note------------------ // A nice refacoring would require to not read again the packet data (which is // read in the UnpackPscuHeader() and put directly in the // EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... // 15 June 2004 ---note------------------ void EventReader::RunEvent(int EventNumber) { //for now i'll suppose that the raw file starts immediately with the right bytes //to insert a GetPacketStart() stringstream oss; int step = 0; while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ try { if (FindStart()) { if(UnpackPscuHeader()){ const PacketType* type = Header->GetPscuHeader()->GetPacketType(); //if (filter->CheckPacket(type)) AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); if (i != TechmodelAlgorithmMap.end()) { TechmodelAlgorithm *EventAlgorithm = i->second; EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); Run->FillTrees(type); Header->GetCounter()->Increment(type); } else { oss.flush(); oss << "No way to read events of type" << type->GetName(); logger->info(oss.str().c_str()); throw Exception("No way to read events of type " + type->GetName()); } } //else { //**TO BE DONE** <>// //Have to be defined a "WrongCRC" packet::Type? //} } } catch (...) { //This have to be more detailed. More exceptions type are needed. logger->error("Couldn't read the event. Skipping to the next header."); } if ((step%1000) == 0) std::cout << step/1000 << "K \n"; } Header->GetCounter()->PrintCounters(); } static void SkipToNextHeader(ifstream* TechmodelFile) { // yeah. } /** * Unpack the PSCU header from a file into the structure. */ int EventReader::UnpackPscuHeader(void) throw (std::exception) { stringstream oss; int response; char buff[16]; InputFile->read(buff, sizeof(buff)); unsigned char PacketId1 = buff[3]; unsigned char PacketId2 = buff[4]; unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); unsigned char CRC = buff[15]; unsigned char FileOffset = buff[15]; if (Counter < prevPckCounter){ oss.flush(); oss << "Packet counter is less than before of " << (prevPckCounter - Counter); logger->info(oss.str().c_str()); } if (OrbitalTime < prevPckOBT){ oss.flush(); oss << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime); logger->info(oss.str().c_str()); } if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){ prevPckCounter = Counter; prevPckOBT = OrbitalTime; long int initPos = InputFile->tellg(); long int finalPos; Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); Header->GetPscuHeader()->SetCounter(Counter); Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); //PacketLength is the length of the whole DATApacket starting from the first byte after the header //plus the CRC legth (which varies for each type of packet) Header->GetPscuHeader()->SetPacketLenght(PacketLenght); Header->GetPscuHeader()->SetCRC(CRC); Header->GetPscuHeader()->SetFileOffset(((long int)(InputFile->tellg()) - 16)); //commented out because of the above test code InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur); FindStart(); finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); if(finalPos == 0){ logger->debug(_T("Correct packet length")); } if (finalPos > 0 && finalPos < 64) { oss.flush(); oss << " Correct packet length: Padded of " << finalPos << " bytes"; logger->warn(oss.str().c_str()); } if (finalPos > 64){ logger->warn(_T(" Wrong packet length? (because of wrong padding?)")); } InputFile->seekg(initPos, std::ios::beg); response = true; } else { logger->warn(_T(" WRONG CRC FOR HEADER ")); InputFile->seekg(-(13), std::ios::cur); response = false; } char tmpId1[4]; char tmpId2[4]; char tmpLength[12]; char tmpStart[100]; char tmpCRC[4]; sprintf(tmpId1, "%02X", PacketId1); sprintf(tmpId2, "%02X", PacketId2); sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset()); sprintf(tmpCRC, "%02X", CRC); oss.flush(); oss << "\n Packet Counter (decimal) : " << Counter << "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 << "\n Orbital Time (decimal) : " << OrbitalTime << "\n Lenght : " << tmpLength << "\n CRC : " << tmpCRC << "\n Header Start Position : " << tmpStart; logger->info(oss.str().c_str()); return response; } /** * Unpack the trailer of a PSCU event into the structure. */ void EventReader::UnpackPscuTrailer(void) throw (std::exception) { } /** * Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence */ int EventReader::FindStart(void) throw (std::exception) { //search an hexadecimal sequence in a file //subSign ------> pointer to the sequence buffer //subSignDim ------> dimension of the buffer // at exit // return true if founds a match (else false) // subF point rigth after the match, if found. Else EOF. //Maurizio 15/11/2002----------------------- const unsigned char subSign[3]={0xFA, 0xFE, 0xDE}; int subSignDim = 3; //------------------------------------------ int subIndex = 0; char dataByte; int buffSize = 100; int index = 0; int loop = 0; char buffer[buffSize]; /* original version while (!InputFile->eof()) { InputFile->get(dataByte); if (dataByte == (char)(*(subSign+subIndex))){ if (subIndex++ == (subSignDim-1)) { InputFile->seekg(-(subIndex), std::ios::cur); return 1; } } else { if (InputFile->eof()) { throw Exception("Extra bytes over the last packets"); } else { subIndex = 0; } } } */ while (!InputFile->eof()) { InputFile->read(buffer, sizeof(buffer)); index = 0; ++loop; while (index < buffSize){ dataByte = buffer[index++]; if (dataByte == (char)(*(subSign+subIndex))){ if (subIndex++ == (subSignDim-1)) { InputFile->seekg( (index - subIndex - (buffSize*loop)), std::ios::cur); return 1; } } else { subIndex = 0; } } } return 0; } ClassImp(EventReader)