1 |
kusanagi |
1.1 |
/** @file |
2 |
kusanagi |
1.2 |
* $Author: kusanagi $ |
3 |
kusanagi |
2.3 |
* $Date: 2004/09/24 11:57:49 $ |
4 |
|
|
* $Revision: 2.2 $ |
5 |
kusanagi |
1.1 |
* |
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 |
kusanagi |
1.10 |
#include <log4cxx/logger.h> |
11 |
kusanagi |
1.1 |
#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 |
kusanagi |
1.10 |
static log4cxx::LoggerPtr logger = log4cxx::Logger::getLogger(_T("pamela.techmodel.EventReader")); |
27 |
|
|
|
28 |
|
|
unsigned int EventReader::maxPackets = 0; |
29 |
kusanagi |
1.7 |
unsigned int EventReader::prevPckCounter = 0; |
30 |
|
|
unsigned int EventReader::prevPckOBT = 0; |
31 |
kusanagi |
1.10 |
|
32 |
|
|
|
33 |
kusanagi |
1.1 |
/** |
34 |
|
|
* Constructor. |
35 |
|
|
*/ |
36 |
kusanagi |
1.10 |
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 |
kusanagi |
1.1 |
Header = new EventHeader(); |
43 |
kusanagi |
1.5 |
|
44 |
kusanagi |
1.6 |
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 |
kusanagi |
1.10 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TestTof, new TestTofReader())); |
65 |
kusanagi |
1.6 |
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 |
kusanagi |
2.2 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new NdInitReader())); |
78 |
kusanagi |
1.10 |
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 |
kusanagi |
1.1 |
} |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Get a string with the version info of the algorithm. |
87 |
|
|
*/ |
88 |
|
|
std::string EventReader::GetVersionInfo(void) const { |
89 |
|
|
return |
90 |
kusanagi |
2.3 |
"$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 2.2 2004/09/24 11:57:49 kusanagi Exp $\n"; |
91 |
kusanagi |
1.1 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
|
|
* Initialize the algorithm with a special run. This will initialize the |
95 |
|
|
* event reader routines for all packet types. |
96 |
|
|
*/ |
97 |
|
|
void EventReader::Init(PamelaRun *run) { |
98 |
kusanagi |
1.10 |
std::stringstream oss; |
99 |
|
|
logger->debug(_T("Initialize")); |
100 |
kusanagi |
1.1 |
SetInputStream(run); |
101 |
|
|
|
102 |
|
|
//Create the structure of directories and create xxx.Header.root files |
103 |
|
|
run->WriteHeaders(this, &Header); |
104 |
|
|
|
105 |
|
|
//Create the xxx.root in it's specific directory |
106 |
|
|
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
107 |
|
|
i != TechmodelAlgorithmMap.end(); i++) { |
108 |
kusanagi |
1.10 |
oss.flush(); |
109 |
|
|
oss << "Initializing algo " << i->second->GetAlgorithmName(); |
110 |
|
|
//cout << "Initializing algo " << i->second->GetAlgorithmName(); |
111 |
|
|
logger->debug(oss.str().c_str()); |
112 |
kusanagi |
1.1 |
i->second->Init(run); |
113 |
|
|
} |
114 |
|
|
Run = dynamic_cast<TechmodelPamelaRun*>(run); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
static void SkipToNextHeader(ifstream *); |
118 |
|
|
|
119 |
|
|
/** |
120 |
|
|
* Read the next event header, call the reader algorithms that |
121 |
|
|
* correspond to its packet type, and read the event trailer. |
122 |
|
|
*/ |
123 |
|
|
|
124 |
|
|
// 15 June 2004 ---note------------------ |
125 |
|
|
// A nice refacoring would require to not read again the packet data (which is |
126 |
|
|
// read in the UnpackPscuHeader() and put directly in the |
127 |
|
|
// EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... |
128 |
|
|
// 15 June 2004 ---note------------------ |
129 |
|
|
void EventReader::RunEvent(int EventNumber) { |
130 |
|
|
//for now i'll suppose that the raw file starts immediately with the right bytes |
131 |
|
|
//to insert a GetPacketStart() |
132 |
kusanagi |
1.10 |
stringstream oss; |
133 |
|
|
int step = 0; |
134 |
|
|
while (!InputFile->eof() && ((step++ < maxPackets) || (maxPackets == 0))){ |
135 |
kusanagi |
1.1 |
try { |
136 |
|
|
if (FindStart()) { |
137 |
|
|
if(UnpackPscuHeader()){ |
138 |
|
|
const PacketType* type = Header->GetPscuHeader()->GetPacketType(); |
139 |
kusanagi |
2.3 |
//if (filter->CheckPacket(type)) |
140 |
kusanagi |
1.1 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
141 |
|
|
if (i != TechmodelAlgorithmMap.end()) { |
142 |
|
|
TechmodelAlgorithm *EventAlgorithm = i->second; |
143 |
|
|
EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); |
144 |
|
|
Run->FillTrees(type); |
145 |
|
|
Header->GetCounter()->Increment(type); |
146 |
|
|
} else { |
147 |
kusanagi |
1.10 |
oss.flush(); |
148 |
|
|
oss << "No way to read events of type" << type->GetName(); |
149 |
|
|
logger->info(oss.str().c_str()); |
150 |
kusanagi |
2.3 |
throw Exception("No way to read events of type " + type->GetName()); |
151 |
|
|
} |
152 |
|
|
} //else { |
153 |
|
|
//**TO BE DONE** <<WRONG CRC l --- LOST PACKET>>// |
154 |
|
|
//Have to be defined a "WrongCRC" packet::Type? |
155 |
|
|
//} |
156 |
|
|
} |
157 |
kusanagi |
1.1 |
} catch (...) { |
158 |
|
|
//This have to be more detailed. More exceptions type are needed. |
159 |
kusanagi |
1.10 |
logger->error("Couldn't read the event. Skipping to the next header."); |
160 |
kusanagi |
1.1 |
} |
161 |
kusanagi |
2.1 |
if ((step%1000) == 0) std::cout << step/1000 << "K \n"; |
162 |
kusanagi |
1.1 |
} |
163 |
kusanagi |
1.10 |
Header->GetCounter()->PrintCounters(); |
164 |
kusanagi |
1.1 |
} |
165 |
|
|
|
166 |
|
|
static void SkipToNextHeader(ifstream* TechmodelFile) { |
167 |
|
|
// yeah. |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
|
171 |
|
|
|
172 |
|
|
/** |
173 |
|
|
* Unpack the PSCU header from a file into the structure. |
174 |
|
|
*/ |
175 |
|
|
int EventReader::UnpackPscuHeader(void) throw (std::exception) { |
176 |
kusanagi |
1.10 |
stringstream oss; |
177 |
kusanagi |
1.1 |
int response; |
178 |
|
|
char buff[16]; |
179 |
|
|
InputFile->read(buff, sizeof(buff)); |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
unsigned char PacketId1 = buff[3]; |
183 |
|
|
unsigned char PacketId2 = buff[4]; |
184 |
|
|
unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); |
185 |
|
|
unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); |
186 |
|
|
unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); |
187 |
|
|
unsigned char CRC = buff[15]; |
188 |
kusanagi |
1.9 |
unsigned char FileOffset = buff[15]; |
189 |
kusanagi |
1.1 |
|
190 |
|
|
if (Counter < prevPckCounter){ |
191 |
kusanagi |
1.10 |
oss.flush(); |
192 |
|
|
oss << "Packet counter is less than before of " << (prevPckCounter - Counter); |
193 |
|
|
logger->info(oss.str().c_str()); |
194 |
kusanagi |
1.1 |
} |
195 |
|
|
|
196 |
|
|
if (OrbitalTime < prevPckOBT){ |
197 |
kusanagi |
1.10 |
oss.flush(); |
198 |
|
|
oss << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime); |
199 |
|
|
logger->info(oss.str().c_str()); |
200 |
kusanagi |
1.1 |
} |
201 |
|
|
|
202 |
kusanagi |
1.7 |
if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){ |
203 |
|
|
prevPckCounter = Counter; |
204 |
|
|
prevPckOBT = OrbitalTime; |
205 |
kusanagi |
1.1 |
long int initPos = InputFile->tellg(); |
206 |
|
|
long int finalPos; |
207 |
|
|
Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); |
208 |
|
|
Header->GetPscuHeader()->SetCounter(Counter); |
209 |
|
|
Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); |
210 |
|
|
//PacketLength is the length of the whole DATApacket starting from the first byte after the header |
211 |
|
|
//plus the CRC legth (which varies for each type of packet) |
212 |
|
|
Header->GetPscuHeader()->SetPacketLenght(PacketLenght); |
213 |
|
|
Header->GetPscuHeader()->SetCRC(CRC); |
214 |
kusanagi |
1.9 |
Header->GetPscuHeader()->SetFileOffset(((long int)(InputFile->tellg()) - 16)); |
215 |
kusanagi |
1.1 |
|
216 |
|
|
//commented out because of the above test code |
217 |
|
|
InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur); |
218 |
|
|
FindStart(); |
219 |
|
|
finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); |
220 |
|
|
if(finalPos == 0){ |
221 |
kusanagi |
1.10 |
logger->debug(_T("Correct packet length")); |
222 |
kusanagi |
1.1 |
} |
223 |
|
|
if (finalPos > 0 && finalPos < 64) { |
224 |
kusanagi |
1.10 |
oss.flush(); |
225 |
|
|
oss << " Correct packet length: Padded of " << finalPos << " bytes"; |
226 |
|
|
logger->warn(oss.str().c_str()); |
227 |
kusanagi |
1.1 |
} |
228 |
|
|
if (finalPos > 64){ |
229 |
kusanagi |
1.10 |
logger->warn(_T(" Wrong packet length? (because of wrong padding?)")); |
230 |
kusanagi |
1.1 |
} |
231 |
|
|
InputFile->seekg(initPos, std::ios::beg); |
232 |
|
|
response = true; |
233 |
|
|
} else { |
234 |
kusanagi |
1.10 |
logger->warn(_T(" WRONG CRC FOR HEADER ")); |
235 |
kusanagi |
1.8 |
InputFile->seekg(-(13), std::ios::cur); |
236 |
kusanagi |
1.1 |
response = false; |
237 |
|
|
} |
238 |
|
|
char tmpId1[4]; |
239 |
|
|
char tmpId2[4]; |
240 |
|
|
char tmpLength[12]; |
241 |
|
|
char tmpStart[100]; |
242 |
|
|
char tmpCRC[4]; |
243 |
|
|
sprintf(tmpId1, "%02X", PacketId1); |
244 |
|
|
sprintf(tmpId2, "%02X", PacketId2); |
245 |
kusanagi |
1.4 |
sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); |
246 |
kusanagi |
1.9 |
sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset()); |
247 |
kusanagi |
1.1 |
sprintf(tmpCRC, "%02X", CRC); |
248 |
kusanagi |
1.10 |
oss.flush(); |
249 |
|
|
oss << "\n Packet Counter (decimal) : " << Counter |
250 |
kusanagi |
1.1 |
<< "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 |
251 |
|
|
<< "\n Orbital Time (decimal) : " << OrbitalTime |
252 |
|
|
<< "\n Lenght : " << tmpLength |
253 |
|
|
<< "\n CRC : " << tmpCRC |
254 |
kusanagi |
1.10 |
<< "\n Header Start Position : " << tmpStart; |
255 |
|
|
logger->info(oss.str().c_str()); |
256 |
kusanagi |
1.1 |
return response; |
257 |
|
|
} |
258 |
|
|
|
259 |
|
|
/** |
260 |
|
|
* Unpack the trailer of a PSCU event into the structure. |
261 |
|
|
*/ |
262 |
|
|
void EventReader::UnpackPscuTrailer(void) throw (std::exception) { |
263 |
|
|
|
264 |
|
|
} |
265 |
|
|
|
266 |
|
|
/** |
267 |
|
|
* Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence |
268 |
|
|
*/ |
269 |
|
|
int EventReader::FindStart(void) throw (std::exception) { |
270 |
|
|
//search an hexadecimal sequence in a file |
271 |
|
|
//subSign ------> pointer to the sequence buffer |
272 |
|
|
//subSignDim ------> dimension of the buffer |
273 |
|
|
// at exit |
274 |
|
|
// return true if founds a match (else false) |
275 |
|
|
// subF point rigth after the match, if found. Else EOF. |
276 |
|
|
//Maurizio 15/11/2002----------------------- |
277 |
|
|
const unsigned char subSign[3]={0xFA, 0xFE, 0xDE}; |
278 |
|
|
int subSignDim = 3; |
279 |
|
|
//------------------------------------------ |
280 |
|
|
int subIndex = 0; |
281 |
|
|
char dataByte; |
282 |
kusanagi |
1.10 |
|
283 |
|
|
int buffSize = 100; |
284 |
|
|
int index = 0; |
285 |
|
|
int loop = 0; |
286 |
|
|
char buffer[buffSize]; |
287 |
|
|
|
288 |
|
|
/* original version |
289 |
kusanagi |
1.1 |
while (!InputFile->eof()) { |
290 |
|
|
InputFile->get(dataByte); |
291 |
|
|
if (dataByte == (char)(*(subSign+subIndex))){ |
292 |
|
|
if (subIndex++ == (subSignDim-1)) { |
293 |
|
|
InputFile->seekg(-(subIndex), std::ios::cur); |
294 |
|
|
return 1; |
295 |
|
|
} |
296 |
|
|
} else { |
297 |
|
|
if (InputFile->eof()) { |
298 |
|
|
throw Exception("Extra bytes over the last packets"); |
299 |
|
|
} else { |
300 |
kusanagi |
1.8 |
subIndex = 0; |
301 |
kusanagi |
1.1 |
} |
302 |
|
|
} |
303 |
kusanagi |
1.10 |
} */ |
304 |
|
|
while (!InputFile->eof()) { |
305 |
|
|
InputFile->read(buffer, sizeof(buffer)); |
306 |
|
|
index = 0; |
307 |
|
|
++loop; |
308 |
|
|
while (index < buffSize){ |
309 |
|
|
dataByte = buffer[index++]; |
310 |
|
|
if (dataByte == (char)(*(subSign+subIndex))){ |
311 |
|
|
if (subIndex++ == (subSignDim-1)) { |
312 |
|
|
InputFile->seekg( (index - subIndex - (buffSize*loop)), std::ios::cur); |
313 |
|
|
return 1; |
314 |
|
|
} |
315 |
|
|
} else { |
316 |
|
|
subIndex = 0; |
317 |
|
|
} |
318 |
|
|
} |
319 |
|
|
} |
320 |
kusanagi |
1.1 |
return 0; |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
ClassImp(EventReader) |