1 |
/** @file |
/** @file |
2 |
* $Author: kusanagi $ |
* $Author: kusanagi $ |
3 |
* $Date: 2004/09/24 11:57:49 $ |
* $Date: 2004/10/17 12:28:38 $ |
4 |
* $Revision: 2.2 $ |
* $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. |
9 |
|
|
10 |
#include <log4cxx/logger.h> |
#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 |
|
|
32 |
*/ |
*/ |
33 |
EventReader::EventReader(int packetsLimit = -1): |
EventReader::EventReader(int packetsLimit = -1): |
34 |
TechmodelAlgorithm(0, "TechmodelEventReader"){ |
TechmodelAlgorithm(0, "TechmodelEventReader"){ |
|
//EventReader::EventReader(void): |
|
|
// TechmodelAlgorithm(0, "TechmodelEventReader") { |
|
35 |
EventReader::maxPackets = packetsLimit; |
EventReader::maxPackets = packetsLimit; |
36 |
logger->debug(_T("Constructor")); |
logger->debug(_T("Constructor")); |
37 |
Header = new EventHeader(); |
Header = new EventHeader(); |
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 2.2 2004/09/24 11:57:49 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 |
/** |
/** |
90 |
* event reader routines for all packet types. |
* event reader routines for all packet types. |
91 |
*/ |
*/ |
92 |
void EventReader::Init(PamelaRun *run) { |
void EventReader::Init(PamelaRun *run) { |
|
std::stringstream oss; |
|
|
logger->debug(_T("Initialize")); |
|
93 |
SetInputStream(run); |
SetInputStream(run); |
94 |
|
|
95 |
//Create the structure of directories and create xxx.Header.root files |
//Create the structure of directories and create xxx.Header.root files |
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 |
oss.flush(); |
oss.str(""); |
102 |
oss << "Initializing algo " << i->second->GetAlgorithmName(); |
oss << "Initializing algo " << i->second->GetAlgorithmName(); |
|
//cout << "Initializing algo " << i->second->GetAlgorithmName(); |
|
103 |
logger->debug(oss.str().c_str()); |
logger->debug(oss.str().c_str()); |
104 |
i->second->Init(run); |
i->second->Init(run); |
105 |
} |
} |
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) { |
126 |
while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ |
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(); |
|
//if (filter->CheckPacket(type)) |
|
131 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
132 |
if (i != TechmodelAlgorithmMap.end()) { |
if (i != TechmodelAlgorithmMap.end()) { |
133 |
TechmodelAlgorithm *EventAlgorithm = i->second; |
TechmodelAlgorithm *EventAlgorithm = i->second; |
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 |
oss.flush(); |
throw NotExistingAlgorithmException(type->GetName().c_str()); //to exctract to an higher level and delete the logger! |
|
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()); |
|
140 |
} |
} |
|
} //else { |
|
|
//**TO BE DONE** <<WRONG CRC l --- LOST PACKET>>// |
|
|
//Have to be defined a "WrongCRC" packet::Type? |
|
|
//} |
|
141 |
} |
} |
142 |
|
// In case of exception have to save the packet in a specific root file?? |
143 |
|
} catch (NotExistingAlgorithmException exc) { |
144 |
|
char *temp; |
145 |
|
sprintf(temp, "%s %s \n", exc.print(), Header->GetPscuHeader()->Print()); |
146 |
|
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"); |
|
logger->error("Couldn't read the event. Skipping to the next header."); |
|
170 |
} |
} |
171 |
if ((step%1000) == 0) std::cout << step/1000 << "K \n"; |
if ((step%1000) == 0) std::cout << step/1000 << "K \n"; |
172 |
} |
} |
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 |
stringstream oss; |
stringstream oss; |
187 |
int response; |
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 |
oss.flush(); |
oss.str(""); |
202 |
oss << "Packet counter is less than before of " << (prevPckCounter - Counter); |
oss << "Packet counter is less than before of " << (prevPckCounter - Counter); |
203 |
logger->info(oss.str().c_str()); |
logger->warn(oss.str().c_str()); |
204 |
} |
} |
205 |
|
|
206 |
if (OrbitalTime < prevPckOBT){ |
if (OrbitalTime < prevPckOBT){ |
207 |
oss.flush(); |
oss.str(""); |
208 |
oss << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime); |
oss << " Onboard Time is less than before of " << (prevPckOBT - OrbitalTime); |
209 |
logger->info(oss.str().c_str()); |
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)){ |
231 |
logger->debug(_T("Correct packet length")); |
logger->debug(_T("Correct packet length")); |
232 |
} |
} |
233 |
if (finalPos > 0 && finalPos < 64) { |
if (finalPos > 0 && finalPos < 64) { |
234 |
oss.flush(); |
oss.str(""); |
235 |
oss << " Correct packet length: Padded of " << finalPos << " bytes"; |
oss << " Correct packet length: Padded of " << finalPos << " bytes"; |
236 |
logger->warn(oss.str().c_str()); |
logger->debug(oss.str().c_str()); |
237 |
} |
} |
238 |
if (finalPos > 64){ |
if (finalPos > 64){ |
239 |
logger->warn(_T(" Wrong packet length? (because of wrong padding?)")); |
oss.str(""); |
240 |
|
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 { |
|
logger->warn(_T(" WRONG CRC FOR HEADER ")); |
|
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]; |
266 |
<< "\n CRC : " << tmpCRC |
<< "\n CRC : " << tmpCRC |
267 |
<< "\n Header Start Position : " << tmpStart; |
<< "\n Header Start Position : " << tmpStart; |
268 |
logger->info(oss.str().c_str()); |
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 |
|
|
296 |
int buffSize = 100; |
int buffSize = 100; |
297 |
int index = 0; |
int index = 0; |
298 |
int loop = 0; |
int loop = -1; |
299 |
char buffer[buffSize]; |
char buffer[buffSize]; |
300 |
|
|
|
/* 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; |
|
|
} |
|
|
} |
|
|
} */ |
|
301 |
while (!InputFile->eof()) { |
while (!InputFile->eof()) { |
302 |
InputFile->read(buffer, sizeof(buffer)); |
InputFile->read(buffer, sizeof(buffer)); |
303 |
index = 0; |
index = 0; |
304 |
++loop; |
loop++; |
305 |
while (index < buffSize){ |
while (index < buffSize){ |
306 |
dataByte = buffer[index++]; |
dataByte = buffer[index++]; |
307 |
if (dataByte == (char)(*(subSign+subIndex))){ |
if (dataByte == (char)(*(subSign+subIndex))){ |
308 |
if (subIndex++ == (subSignDim-1)) { |
if (subIndex++ == (subSignDim-1)) { |
309 |
InputFile->seekg( (index - subIndex - (buffSize*loop)), std::ios::cur); |
InputFile->seekg( (index - (subIndex + buffSize)), std::ios::cur); |
310 |
return 1; |
return 1; |
311 |
} |
} |
312 |
} else { |
} else { |
313 |
|
index = index - (subIndex); |
314 |
subIndex = 0; |
subIndex = 0; |
315 |
} |
} |
316 |
} |
} |
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 |
} |
} |