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