1 |
/** @file |
2 |
* $Author: kusanagi $ |
3 |
* $Date: 2005/01/11 14:59:31 $ |
4 |
* $Revision: 2.8 $ |
5 |
* |
6 |
* Implementation of the functions of a sample Algorithm class. |
7 |
* This file can be used as a templace to develop your own algorithm. |
8 |
*/ |
9 |
|
10 |
#include <log4cxx/logger.h> |
11 |
#include <fstream> |
12 |
#include "EventReader.h" |
13 |
#include "ReaderAlgorithms.h" |
14 |
|
15 |
|
16 |
extern "C" { |
17 |
#include "CRC.h" |
18 |
} |
19 |
|
20 |
using namespace pamela; |
21 |
using namespace pamela::techmodel; |
22 |
|
23 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.EventReader")); |
24 |
|
25 |
unsigned int EventReader::maxPackets = 0; |
26 |
unsigned int EventReader::prevPckCounter = 0; |
27 |
unsigned int EventReader::prevPckOBT = 0; |
28 |
|
29 |
|
30 |
/** |
31 |
* Constructor. |
32 |
*/ |
33 |
EventReader::EventReader(int packetsLimit = -1): |
34 |
TechmodelAlgorithm(0, "TechmodelEventReader"){ |
35 |
EventReader::maxPackets = packetsLimit; |
36 |
logger->debug(_T("Constructor")); |
37 |
Header = new EventHeader(); |
38 |
|
39 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun, new PhysEndRunReader)); |
40 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse1, new CalibCalPulse1Reader)); |
41 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse2, new CalibCalPulse2Reader)); |
42 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Physics, new PhysicsReader)); |
43 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrkBoth, new CalibTrkBothReader)); |
44 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk1, new CalibTrk1Reader)); |
45 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk2, new CalibTrk2Reader)); |
46 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrd, new CalibTrdReader)); |
47 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTof, new CalibTofReader)); |
48 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibS4, new CalibS4Reader)); |
49 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPed, new CalibCalPedReader)); |
50 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib1_Ac1, new Calib1_Ac1Reader)); |
51 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib2_Ac1, new Calib2_Ac1Reader)); |
52 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib1_Ac2, new Calib1_Ac2Reader)); |
53 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib2_Ac2, new Calib2_Ac2Reader)); |
54 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunHeader, new RunHeaderReader)); |
55 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunTrailer, new RunTrailerReader)); |
56 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibHeader, new CalibHeaderReader)); |
57 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrailer, new CalibTrailerReader)); |
58 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitHeader, new InitHeaderReader)); |
59 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::InitTrailer, new InitTrailerReader)); |
60 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::EventTrk, new EventTrkReader)); |
61 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTrk, new TestTrkReader)); |
62 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTof, new TestTofReader)); |
63 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader)); |
64 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader)); |
65 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader)); |
66 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TabDump, new TabDumpReader)); |
67 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Tmtc, new TmtcReader)); |
68 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Mcmd, new McmdReader)); |
69 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ForcedFECmd, new ForcedFECmdReader)); |
70 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac1Init, new Ac1InitReader)); |
71 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalInit, new CalInitReader)); |
72 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit, new TrkInitReader)); |
73 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit, new TofInitReader)); |
74 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new TrgInitReader)); |
75 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::NdInit, new NdInitReader)); |
76 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Init, new S4InitReader)); |
77 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac2Init, new Ac2InitReader)); |
78 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalAlarm, new CalAlarmReader)); |
79 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::AcAlarm, new AcAlarmReader)); |
80 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm, new TrkAlarmReader)); |
81 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm, new TrgAlarmReader)); |
82 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm, new TofAlarmReader)); |
83 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Alarm, new S4AlarmReader)); |
84 |
} |
85 |
|
86 |
/** |
87 |
* Get a string with the version info of the algorithm. |
88 |
*/ |
89 |
std::string EventReader::GetVersionInfo(void) const { |
90 |
return |
91 |
"$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 2.8 2005/01/11 14:59:31 kusanagi Exp $\n"; |
92 |
} |
93 |
|
94 |
/** |
95 |
* Initialize the algorithm with a special run. This will initialize the |
96 |
* event reader routines for all packet types. |
97 |
*/ |
98 |
void EventReader::Init(PamelaRun *run) { |
99 |
SetInputStream(run); |
100 |
|
101 |
//Create the structure of directories and create xxx.Header.root files |
102 |
run->WriteHeaders(this, &Header); |
103 |
|
104 |
//Create the xxx.root in it's specific directory |
105 |
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
106 |
i != TechmodelAlgorithmMap.end(); i++) { |
107 |
oss.str(""); |
108 |
oss << "Initializing algo " << i->second->GetAlgorithmName(); |
109 |
logger->debug(oss.str().c_str()); |
110 |
i->second->Init(run); |
111 |
} |
112 |
Run = dynamic_cast<TechmodelPamelaRun*>(run); |
113 |
} |
114 |
|
115 |
static void SkipToNextHeader(ifstream *); |
116 |
|
117 |
/** |
118 |
* Read the next event header, call the reader algorithms that |
119 |
* correspond to its packet type, and read the event trailer. |
120 |
*/ |
121 |
|
122 |
// 15 June 2004 ---note------------------ |
123 |
// A nice refacoring would require to not read again the packet data (which is |
124 |
// read in the UnpackPscuHeader() and put directly in the |
125 |
// EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... |
126 |
// 15 June 2004 ---note------------------ |
127 |
void EventReader::RunEvent(int EventNumber) { |
128 |
//for now i'll suppose that the raw file starts immediately with the right bytes |
129 |
//to insert a GetPacketStart() |
130 |
stringstream oss; |
131 |
int step = 0; |
132 |
const PacketType* type; |
133 |
while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ |
134 |
try { |
135 |
if (FindStart()) { |
136 |
UnpackPscuHeader(); |
137 |
type = Header->GetPscuHeader()->GetPacketType(); |
138 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
139 |
if (i != TechmodelAlgorithmMap.end()) { |
140 |
TechmodelAlgorithm *EventAlgorithm(i->second); |
141 |
EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); |
142 |
Run->FillTrees(type); |
143 |
Header->GetCounter()->Increment(type); |
144 |
logger->info(Header->GetPscuHeader()->Print()); |
145 |
} else { |
146 |
oss.str(""); |
147 |
oss << "\n No way to read events of type " << type->GetName().c_str() << Header->GetPscuHeader()->Print(); |
148 |
throw NotExistingAlgorithmException(oss.str().c_str()); //to exctract to an higher level and delete the logger! |
149 |
} |
150 |
} |
151 |
// In case of exception have to save the packet in a specific root file?? |
152 |
} catch (NotExistingAlgorithmException exc) { |
153 |
oss.str(""); |
154 |
oss << exc.print() << " " << Header->GetPscuHeader()->Print(); |
155 |
logger->error(oss.str().c_str()); |
156 |
} catch (WrongCRCHeaderException exc) { |
157 |
oss.str(""); |
158 |
oss << exc.print(); |
159 |
logger->error(oss.str().c_str()); |
160 |
} catch (WrongCRCException exc) { |
161 |
oss.str(""); |
162 |
oss << exc.print() << " " << Header->GetPscuHeader()->Print(); |
163 |
logger->error(oss.str().c_str()); |
164 |
} catch (UnidentifiedPacketException exc) { |
165 |
oss.str(""); |
166 |
oss << exc.print() << " " << Header->GetPscuHeader()->Print(); |
167 |
logger->error(oss.str().c_str()); |
168 |
} catch (NotExistingCounterException exc) { |
169 |
oss.str(""); |
170 |
oss << exc.print() << " " << Header->GetPscuHeader()->Print(); |
171 |
logger->error(oss.str().c_str()); |
172 |
} catch (LengthException exc) { |
173 |
oss.str(""); |
174 |
oss << exc.print() << " " << Header->GetPscuHeader()->Print(); |
175 |
logger->error(oss.str().c_str()); |
176 |
} catch (...) { |
177 |
logger->error("Couldn't read the event. Skipping to the next header. \n"); |
178 |
} |
179 |
if ((step%1000) == 0) std::cout << step/1000 << "K \n"; |
180 |
} |
181 |
Header->GetCounter()->PrintCounters(); |
182 |
} |
183 |
|
184 |
static void SkipToNextHeader(ifstream* TechmodelFile) { |
185 |
// yeah. |
186 |
} |
187 |
|
188 |
|
189 |
|
190 |
/** |
191 |
* Unpack the PSCU header from a file into the structure. |
192 |
*/ |
193 |
void EventReader::UnpackPscuHeader(void) throw (WrongCRCHeaderException, LengthException) { |
194 |
stringstream oss; |
195 |
int response = false; |
196 |
char buff[16]; |
197 |
InputFile->read(buff, sizeof(buff)); |
198 |
|
199 |
|
200 |
unsigned char PacketId1 = buff[3]; |
201 |
unsigned char PacketId2 = buff[4]; |
202 |
unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); |
203 |
unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); |
204 |
unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); |
205 |
unsigned char CRC = buff[15]; |
206 |
unsigned char FileOffset = buff[15]; |
207 |
|
208 |
if (Counter < prevPckCounter){ |
209 |
oss.str(""); |
210 |
oss << "Packet counter is less than before of " << (prevPckCounter - Counter); |
211 |
logger->warn(oss.str().c_str()); |
212 |
} |
213 |
|
214 |
if (OrbitalTime < prevPckOBT){ |
215 |
oss.str(""); |
216 |
oss << " Onboard Time is less than before of " << (prevPckOBT - OrbitalTime); |
217 |
logger->warn(oss.str().c_str()); |
218 |
} |
219 |
|
220 |
if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){ |
221 |
prevPckCounter = Counter; |
222 |
prevPckOBT = OrbitalTime; |
223 |
long int initPos = InputFile->tellg(); |
224 |
long int finalPos; |
225 |
Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); |
226 |
Header->GetPscuHeader()->SetCounter(Counter); |
227 |
Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); |
228 |
//PacketLength is the length of the whole DATApacket starting from the first byte after the header |
229 |
//plus the CRC legth (which varies for each type of packet) |
230 |
Header->GetPscuHeader()->SetPacketLenght(PacketLenght); |
231 |
Header->GetPscuHeader()->SetCRC(CRC); |
232 |
Header->GetPscuHeader()->SetFileOffset(((long int)(InputFile->tellg()) - 16)); |
233 |
|
234 |
//commented out because of the above test code |
235 |
InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur); |
236 |
if (FindStart()) { |
237 |
finalPos = (long int)InputFile->tellg() - (initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); |
238 |
if(finalPos == 0){ |
239 |
logger->debug(_T(" Correct packet length")); |
240 |
} |
241 |
if (finalPos > 0 && finalPos < 64) { |
242 |
oss.str(""); |
243 |
oss << "\n Correct packet length: Padded of " << finalPos << " bytes"; |
244 |
logger->debug(oss.str().c_str()); |
245 |
} |
246 |
if (finalPos > 64){ |
247 |
oss.str(""); |
248 |
oss << "\n The begin of the next packet is far more than 64 byte from the end of the previous." |
249 |
<< Header->GetPscuHeader()->Print(); |
250 |
logger->error(oss.str().c_str()); |
251 |
//throw LengthException("The begin of the next packet is far more than 64 byte from the end of the previous."); |
252 |
} |
253 |
} |
254 |
else { |
255 |
logger->debug(_T(" END OF FILE")); |
256 |
} |
257 |
|
258 |
InputFile->seekg(initPos, std::ios::beg); |
259 |
} else { |
260 |
/*Here i should extract the block of Data for later analysis */ |
261 |
InputFile->seekg(-(13), std::ios::cur); |
262 |
oss.str(""); |
263 |
oss << "CRC Header Error on packet:" << PscuHeader::Print(buff); |
264 |
throw WrongCRCHeaderException(oss.str().c_str()); |
265 |
} |
266 |
|
267 |
/* char tmpId1[4]; |
268 |
char tmpId2[4]; |
269 |
char tmpLength[12]; |
270 |
char tmpStart[100]; |
271 |
char tmpCRC[4]; |
272 |
sprintf(tmpId1, "%02X", PacketId1); |
273 |
sprintf(tmpId2, "%02X", PacketId2); |
274 |
sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); |
275 |
sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset()); |
276 |
sprintf(tmpCRC, "%02X", CRC); |
277 |
oss.flush(); |
278 |
oss << "\n Packet Counter (decimal) : " << Counter |
279 |
<< "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 |
280 |
<< "\n Orbital Time (decimal) : " << OrbitalTime |
281 |
<< "\n Lenght : " << tmpLength |
282 |
<< "\n CRC : " << tmpCRC |
283 |
<< "\n Header Start Position : " << tmpStart; |
284 |
logger->info(oss.str().c_str()); |
285 |
*/ |
286 |
} |
287 |
|
288 |
/** |
289 |
* Unpack the trailer of a PSCU event into the structure. |
290 |
*/ |
291 |
void EventReader::UnpackPscuTrailer(void) throw (std::exception) { |
292 |
|
293 |
} |
294 |
|
295 |
/** |
296 |
* Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence |
297 |
*/ |
298 |
bool EventReader::FindStart(void) throw (std::exception) { |
299 |
//search an hexadecimal sequence in a file |
300 |
//subSign ------> pointer to the sequence buffer |
301 |
//subSignDim ------> dimension of the buffer |
302 |
// at exit |
303 |
// return true if founds a match (else false) |
304 |
// subF point rigth after the match, if found. Else EOF. |
305 |
//Maurizio 15/11/2002----------------------- |
306 |
const int subSignDim = 3; |
307 |
const unsigned char subSign[subSignDim]={0xFA, 0xFE, 0xDE}; |
308 |
//------------------------------------------ |
309 |
int subIndex = 0; |
310 |
char dataByte; |
311 |
|
312 |
int buffSize = 100; |
313 |
int index = 0; |
314 |
int loop = -1; |
315 |
char buffer[buffSize]; |
316 |
|
317 |
while (!InputFile->eof()) { |
318 |
InputFile->read(buffer, sizeof(buffer)); |
319 |
index = 0; |
320 |
loop++; |
321 |
while (index < buffSize){ |
322 |
dataByte = buffer[index++]; |
323 |
if (dataByte == (char)(*(subSign+subIndex))){ |
324 |
if (subIndex++ == (subSignDim-1)) { |
325 |
InputFile->seekg( (index - (subIndex + buffSize)), std::ios::cur); |
326 |
return true; |
327 |
} |
328 |
} else { |
329 |
index = index - (subIndex); |
330 |
subIndex = 0; |
331 |
} |
332 |
} |
333 |
//Needs to guarantee the overap of the buffer(s) in several loop |
334 |
InputFile->seekg( (-1)*(subSignDim + 1) , std::ios::cur); |
335 |
} |
336 |
return false; |
337 |
} |
338 |
|
339 |
ClassImp(EventReader) |