/** @file * $Author: kusanagi $ * $Date: 2004/12/03 22:08:00 $ * $Revision: 2.4 $ * * 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 "EventReader.h" #include "ReaderAlgorithms.h" extern "C" { #include "CRC.h" } 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::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::NdInit, new NdInitReader)); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Init, new S4InitReader)); 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)); TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Alarm, new S4AlarmReader)); } /** * 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.4 2004/12/03 22:08:00 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) { 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.str(""); oss << "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; const PacketType* type; while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ try { if (FindStart()) { UnpackPscuHeader(); type = Header->GetPscuHeader()->GetPacketType(); 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); logger->info(Header->GetPscuHeader()->Print()); } else { throw NotExistingAlgorithmException(type->GetName().c_str()); //to exctract to an higher level and delete the logger! } } // In case of exception have to save the packet in a specific root file?? } catch (NotExistingAlgorithmException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (WrongCRCHeaderException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (WrongCRCException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (UnidentifiedPacketException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (NotExistingCounterException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (LengthException exc) { oss.str(""); oss << exc.print() << " " << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); } catch (...) { logger->error("Couldn't read the event. Skipping to the next header. \n"); } 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. */ void EventReader::UnpackPscuHeader(void) throw (WrongCRCException, LengthException) { stringstream oss; int response = false; 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.str(""); oss << "Packet counter is less than before of " << (prevPckCounter - Counter); logger->warn(oss.str().c_str()); } if (OrbitalTime < prevPckOBT){ oss.str(""); oss << " Onboard Time is less than before of " << (prevPckOBT - OrbitalTime); logger->warn(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.str(""); oss << " Correct packet length: Padded of " << finalPos << " bytes"; logger->debug(oss.str().c_str()); } if (finalPos > 64){ oss.str(""); oss << "The begin of the next packet is far more than 64 byte from the end of the previous." << Header->GetPscuHeader()->Print(); logger->error(oss.str().c_str()); //throw LengthException("The begin of the next packet is far more than 64 byte from the end of the previous."); } InputFile->seekg(initPos, std::ios::beg); } else { InputFile->seekg(-(13), std::ios::cur); throw WrongCRCHeaderException(); } /* 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()); */ } /** * 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 int subSignDim = 3; const unsigned char subSign[subSignDim]={0xFA, 0xFE, 0xDE}; //------------------------------------------ int subIndex = 0; char dataByte; int buffSize = 100; int index = 0; int loop = -1; char buffer[buffSize]; 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)), std::ios::cur); return 1; } } else { index = index - (subIndex); subIndex = 0; } } //Needs to guarantee the overap of the buffer(s) in several loop InputFile->seekg( (-1)*(subSignDim + 1) , std::ios::cur); } return 0; } ClassImp(EventReader)