1 |
/** @file |
/** @file |
2 |
* $Author: kusanagi $ |
* $Author: kusanagi $ |
3 |
* $Date: 2004/08/24 13:19:59 $ |
* $Date: 2004/10/17 12:28:38 $ |
4 |
* $Revision: 1.8 $ |
* $Revision: 2.3 $ |
5 |
* |
* |
6 |
* Implementation of the functions of a sample Algorithm class. |
* Implementation of the functions of a sample Algorithm class. |
7 |
* This file can be used as a templace to develop your own algorithm. |
* This file can be used as a templace to develop your own algorithm. |
8 |
*/ |
*/ |
9 |
|
|
10 |
#include <log4cpp/Category.hh> |
#include <log4cxx/logger.h> |
11 |
#include <fstream> |
#include <fstream> |
|
#include "TechmodelAlgorithm.h" |
|
12 |
#include "EventReader.h" |
#include "EventReader.h" |
13 |
#include "ReaderAlgorithms.h" |
#include "ReaderAlgorithms.h" |
14 |
#include "Exception.h" |
|
15 |
#include "stdio.h" |
|
16 |
extern "C" { |
extern "C" { |
17 |
#include "CRC.h" |
#include "CRC.h" |
18 |
} |
} |
19 |
|
|
|
#define BYTE unsigned char |
|
|
|
|
20 |
using namespace pamela; |
using namespace pamela; |
21 |
using namespace pamela::techmodel; |
using namespace pamela::techmodel; |
22 |
|
|
23 |
static log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.EventReader"); |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.EventReader")); |
24 |
|
|
25 |
|
unsigned int EventReader::maxPackets = 0; |
26 |
unsigned int EventReader::prevPckCounter = 0; |
unsigned int EventReader::prevPckCounter = 0; |
27 |
unsigned int EventReader::prevPckOBT = 0; |
unsigned int EventReader::prevPckOBT = 0; |
28 |
|
|
29 |
|
|
30 |
/** |
/** |
31 |
* Constructor. |
* Constructor. |
32 |
*/ |
*/ |
33 |
EventReader::EventReader(void): |
EventReader::EventReader(int packetsLimit = -1): |
34 |
TechmodelAlgorithm(0, "TechmodelEventReader") { |
TechmodelAlgorithm(0, "TechmodelEventReader"){ |
35 |
cat << log4cpp::Priority::DEBUG |
EventReader::maxPackets = packetsLimit; |
36 |
<< "Constructor " |
logger->debug(_T("Constructor")); |
|
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
|
37 |
Header = new EventHeader(); |
Header = new EventHeader(); |
38 |
|
|
39 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun, new PhysEndRunReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun, new PhysEndRunReader())); |
56 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer, new InitTrailerReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer, new InitTrailerReader())); |
57 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk, new EventTrkReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk, new EventTrkReader())); |
58 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk, new TestTrkReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk, new TestTrkReader())); |
59 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTof, new TestTofReader())); |
60 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader())); |
61 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader())); |
62 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader())); |
69 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit, new TrkInitReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit, new TrkInitReader())); |
70 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit, new TofInitReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit, new TofInitReader())); |
71 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new TrgInitReader())); |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new TrgInitReader())); |
72 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new NdInitReader())); |
73 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalAlarm, new CalAlarmReader())); |
74 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcAlarm, new AcAlarmReader())); |
75 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm, new TrkAlarmReader())); |
76 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm, new TrgAlarmReader())); |
77 |
|
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm, new TofAlarmReader())); |
78 |
} |
} |
79 |
|
|
80 |
/** |
/** |
82 |
*/ |
*/ |
83 |
std::string EventReader::GetVersionInfo(void) const { |
std::string EventReader::GetVersionInfo(void) const { |
84 |
return |
return |
85 |
"$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.8 2004/08/24 13:19:59 kusanagi Exp $\n"; |
"$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 2.3 2004/10/17 12:28:38 kusanagi Exp $\n"; |
86 |
} |
} |
87 |
|
|
88 |
/** |
/** |
98 |
//Create the xxx.root in it's specific directory |
//Create the xxx.root in it's specific directory |
99 |
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
100 |
i != TechmodelAlgorithmMap.end(); i++) { |
i != TechmodelAlgorithmMap.end(); i++) { |
101 |
cat << log4cpp::Priority::DEBUG |
oss.str(""); |
102 |
<< " Initializing algo " << i->second->GetAlgorithmName() |
oss << "Initializing algo " << i->second->GetAlgorithmName(); |
103 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
logger->debug(oss.str().c_str()); |
104 |
i->second->Init(run); |
i->second->Init(run); |
105 |
} |
} |
106 |
Run = dynamic_cast<TechmodelPamelaRun*>(run); |
Run = dynamic_cast<TechmodelPamelaRun*>(run); |
115 |
|
|
116 |
// 15 June 2004 ---note------------------ |
// 15 June 2004 ---note------------------ |
117 |
// A nice refacoring would require to not read again the packet data (which is |
// A nice refacoring would require to not read again the packet data (which is |
118 |
// read in the UnpackPscuHeader() and put directly in the |
// read in the UnpackPscuHeader() and put directly in the |
119 |
// EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... |
// EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... |
120 |
// 15 June 2004 ---note------------------ |
// 15 June 2004 ---note------------------ |
121 |
void EventReader::RunEvent(int EventNumber) { |
void EventReader::RunEvent(int EventNumber) { |
|
|
|
|
cat << log4cpp::Priority::INFO << "Start Unpacking........" |
|
|
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
|
122 |
//for now i'll suppose that the raw file starts immediately with the right bytes |
//for now i'll suppose that the raw file starts immediately with the right bytes |
123 |
//to insert a GetPacketStart() |
//to insert a GetPacketStart() |
124 |
while (!InputFile->eof()){ |
stringstream oss; |
125 |
|
int step = 0; |
126 |
|
while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ |
127 |
try { |
try { |
128 |
if (FindStart()) { |
if (FindStart()) { |
129 |
if(UnpackPscuHeader()){ |
UnpackPscuHeader(); |
130 |
const PacketType* type = Header->GetPscuHeader()->GetPacketType(); |
const PacketType* type = Header->GetPscuHeader()->GetPacketType(); |
131 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
132 |
if (i != TechmodelAlgorithmMap.end()) { |
if (i != TechmodelAlgorithmMap.end()) { |
134 |
EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); |
EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); |
135 |
Run->FillTrees(type); |
Run->FillTrees(type); |
136 |
Header->GetCounter()->Increment(type); |
Header->GetCounter()->Increment(type); |
137 |
|
logger->info(Header->GetPscuHeader()->Print()); |
138 |
} else { |
} else { |
139 |
cat << log4cpp::Priority::ERROR |
throw NotExistingAlgorithmException(type->GetName().c_str()); //to exctract to an higher level and delete the logger! |
140 |
<< "No way to read events of type" << type->GetName() |
} |
141 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
} |
142 |
throw Exception("No way to read events of type " + type->GetName()); |
// In case of exception have to save the packet in a specific root file?? |
143 |
} |
} catch (NotExistingAlgorithmException exc) { |
144 |
} //else { |
char *temp; |
145 |
//**TO BE DONE** <<WRONG CRC l --- LOST PACKET>>// |
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
146 |
//Have to be defined a "WrongCRC" packet::Type? |
logger->warn(temp); |
147 |
//} |
} catch (WrongCRCHeaderException exc) { |
148 |
} |
char *temp; |
149 |
|
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
150 |
|
logger->warn(temp); |
151 |
|
} catch (WrongCRCException exc) { |
152 |
|
//char *temp; |
153 |
|
//sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
154 |
|
//logger->warn(temp); |
155 |
|
logger->warn(Header->GetPscuHeader()->Print()); |
156 |
|
} catch (UnidentifiedPacketException exc) { |
157 |
|
char *temp; |
158 |
|
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
159 |
|
logger->warn(temp); |
160 |
|
} catch (NotExistingCounterException exc) { |
161 |
|
char *temp; |
162 |
|
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
163 |
|
logger->warn(temp); |
164 |
|
} catch (LengthException exc) { |
165 |
|
char *temp; |
166 |
|
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
167 |
|
logger->warn(temp); |
168 |
} catch (...) { |
} catch (...) { |
169 |
//This have to be more detailed. More exceptions type are needed. |
logger->error("Couldn't read the event. Skipping to the next header. \n"); |
|
cat << log4cpp::Priority::ERROR << "Couldn't read the event. Skipping to the next header." |
|
|
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
|
170 |
} |
} |
171 |
|
if ((step%1000) == 0) std::cout << step/1000 << "K \n"; |
172 |
} |
} |
173 |
Header->GetCounter()->PrintCounters(); |
Header->GetCounter()->PrintCounters(); |
174 |
} |
} |
175 |
|
|
176 |
static void SkipToNextHeader(ifstream* TechmodelFile) { |
static void SkipToNextHeader(ifstream* TechmodelFile) { |
182 |
/** |
/** |
183 |
* Unpack the PSCU header from a file into the structure. |
* Unpack the PSCU header from a file into the structure. |
184 |
*/ |
*/ |
185 |
int EventReader::UnpackPscuHeader(void) throw (std::exception) { |
void EventReader::UnpackPscuHeader(void) throw (WrongCRCException, LengthException) { |
186 |
int response; |
stringstream oss; |
187 |
|
int response = false; |
188 |
char buff[16]; |
char buff[16]; |
189 |
InputFile->read(buff, sizeof(buff)); |
InputFile->read(buff, sizeof(buff)); |
190 |
|
|
198 |
unsigned char FileOffset = buff[15]; |
unsigned char FileOffset = buff[15]; |
199 |
|
|
200 |
if (Counter < prevPckCounter){ |
if (Counter < prevPckCounter){ |
201 |
cat << log4cpp::Priority::WARN |
oss.str(""); |
202 |
<< " Packet counter is less than before of " << (prevPckCounter - Counter) |
oss << "Packet counter is less than before of " << (prevPckCounter - Counter); |
203 |
<< " " << log4cpp::CategoryStream::ENDLINE; |
logger->warn(oss.str().c_str()); |
204 |
} |
} |
205 |
|
|
206 |
if (OrbitalTime < prevPckOBT){ |
if (OrbitalTime < prevPckOBT){ |
207 |
cat << log4cpp::Priority::WARN |
oss.str(""); |
208 |
<< " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime) |
oss << " Onboard Time is less than before of " << (prevPckOBT - OrbitalTime); |
209 |
<< " " << log4cpp::CategoryStream::ENDLINE; |
logger->warn(oss.str().c_str()); |
210 |
} |
} |
211 |
|
|
|
|
|
212 |
if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){ |
if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){ |
213 |
prevPckCounter = Counter; |
prevPckCounter = Counter; |
214 |
prevPckOBT = OrbitalTime; |
prevPckOBT = OrbitalTime; |
228 |
FindStart(); |
FindStart(); |
229 |
finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); |
finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); |
230 |
if(finalPos == 0){ |
if(finalPos == 0){ |
231 |
cat << log4cpp::Priority::DEBUG << " Correct packet length" |
logger->debug(_T("Correct packet length")); |
|
<< " " << log4cpp::CategoryStream::ENDLINE; |
|
232 |
} |
} |
233 |
if (finalPos > 0 && finalPos < 64) { |
if (finalPos > 0 && finalPos < 64) { |
234 |
cat << log4cpp::Priority::WARN |
oss.str(""); |
235 |
<< " Correct packet length: Padded of " << finalPos << " bytes" |
oss << " Correct packet length: Padded of " << finalPos << " bytes"; |
236 |
<< " " << log4cpp::CategoryStream::ENDLINE; |
logger->debug(oss.str().c_str()); |
237 |
} |
} |
238 |
if (finalPos > 64){ |
if (finalPos > 64){ |
239 |
cat << log4cpp::Priority::ERROR << " Wrong packet length? (because of wrong padding?) " |
oss.str(""); |
240 |
<< " " << log4cpp::CategoryStream::ENDLINE; |
oss << "The begin of the next packet is far more than 64 byte from the end of the previous." |
241 |
|
<< Header->GetPscuHeader()->Print(); |
242 |
|
logger->warn(oss.str().c_str()); |
243 |
|
//throw LengthException("The begin of the next packet is far more than 64 byte from the end of the previous."); |
244 |
} |
} |
245 |
InputFile->seekg(initPos, std::ios::beg); |
InputFile->seekg(initPos, std::ios::beg); |
|
response = true; |
|
246 |
} else { |
} else { |
|
cat << log4cpp::Priority::ERROR << " WRONG CRC FOR HEADER " |
|
|
<< " " << log4cpp::CategoryStream::ENDLINE; |
|
247 |
InputFile->seekg(-(13), std::ios::cur); |
InputFile->seekg(-(13), std::ios::cur); |
248 |
response = false; |
throw WrongCRCHeaderException(); |
249 |
} |
} |
250 |
char tmpId1[4]; |
|
251 |
|
/* char tmpId1[4]; |
252 |
char tmpId2[4]; |
char tmpId2[4]; |
253 |
char tmpLength[12]; |
char tmpLength[12]; |
254 |
char tmpStart[100]; |
char tmpStart[100]; |
258 |
sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); |
sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); |
259 |
sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset()); |
sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset()); |
260 |
sprintf(tmpCRC, "%02X", CRC); |
sprintf(tmpCRC, "%02X", CRC); |
261 |
cat << log4cpp::Priority::INFO |
oss.flush(); |
262 |
<< "\n Packet Counter (decimal) : " << Counter |
oss << "\n Packet Counter (decimal) : " << Counter |
263 |
<< "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 |
<< "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 |
264 |
<< "\n Orbital Time (decimal) : " << OrbitalTime |
<< "\n Orbital Time (decimal) : " << OrbitalTime |
265 |
<< "\n Lenght : " << tmpLength |
<< "\n Lenght : " << tmpLength |
266 |
<< "\n CRC : " << tmpCRC |
<< "\n CRC : " << tmpCRC |
267 |
<< "\n Header Start Position : " << tmpStart |
<< "\n Header Start Position : " << tmpStart; |
268 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
logger->info(oss.str().c_str()); |
269 |
return response; |
*/ |
270 |
} |
} |
271 |
|
|
272 |
/** |
/** |
287 |
// return true if founds a match (else false) |
// return true if founds a match (else false) |
288 |
// subF point rigth after the match, if found. Else EOF. |
// subF point rigth after the match, if found. Else EOF. |
289 |
//Maurizio 15/11/2002----------------------- |
//Maurizio 15/11/2002----------------------- |
290 |
const unsigned char subSign[3]={0xFA, 0xFE, 0xDE}; |
const int subSignDim = 3; |
291 |
int subSignDim = 3; |
const unsigned char subSign[subSignDim]={0xFA, 0xFE, 0xDE}; |
292 |
//------------------------------------------ |
//------------------------------------------ |
293 |
int subIndex = 0; |
int subIndex = 0; |
294 |
char dataByte; |
char dataByte; |
295 |
/*InputFile->read(fileByte, 1); |
|
296 |
if ( *fileByte == *(subSign+subIndex)){ |
int buffSize = 100; |
297 |
if (subIndex++ == (subSignDim-1)) { |
int index = 0; |
298 |
InputFile->seekg(-(subIndex), std::ios::cur); |
int loop = -1; |
299 |
return 1; |
char buffer[buffSize]; |
300 |
} |
|
|
}*/ |
|
301 |
while (!InputFile->eof()) { |
while (!InputFile->eof()) { |
302 |
InputFile->get(dataByte); |
InputFile->read(buffer, sizeof(buffer)); |
303 |
if (dataByte == (char)(*(subSign+subIndex))){ |
index = 0; |
304 |
if (subIndex++ == (subSignDim-1)) { |
loop++; |
305 |
InputFile->seekg(-(subIndex), std::ios::cur); |
while (index < buffSize){ |
306 |
return 1; |
dataByte = buffer[index++]; |
307 |
} |
if (dataByte == (char)(*(subSign+subIndex))){ |
308 |
} else { |
if (subIndex++ == (subSignDim-1)) { |
309 |
if (InputFile->eof()) { |
InputFile->seekg( (index - (subIndex + buffSize)), std::ios::cur); |
310 |
throw Exception("Extra bytes over the last packets"); |
return 1; |
311 |
} else { |
} |
312 |
subIndex = 0; |
} else { |
313 |
//return 0; |
index = index - (subIndex); |
314 |
} |
subIndex = 0; |
315 |
//InputFile->seekg(-(subIndex+1), std::ios::cur); |
} |
316 |
//subIndex = 0; |
} |
317 |
} |
//Needs to guarantee the overap of the buffer(s) in several loop |
318 |
} |
InputFile->seekg( (-1)*(subSignDim + 1) , std::ios::cur); |
319 |
|
} |
320 |
return 0; |
return 0; |
321 |
} |
} |
322 |
|
|