1 |
/** @file |
2 |
* $Author: kusanagi $ |
3 |
* $Date: 2004/07/17 20:03:38 $ |
4 |
* $Revision: 1.4 $ |
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 <log4cpp/Category.hh> |
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 log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.EventReader"); |
27 |
static unsigned int prevPckCounter = 0; |
28 |
static unsigned int prevPckOBT = 0; |
29 |
/** |
30 |
* Constructor. |
31 |
*/ |
32 |
EventReader::EventReader(void): |
33 |
TechmodelAlgorithm(0, "TechmodelEventReader") { |
34 |
cat << log4cpp::Priority::DEBUG |
35 |
<< "Constructor " |
36 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
37 |
Header = new EventHeader(); |
38 |
|
39 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Physics, new PhysicsReader())); |
40 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunHeader, new RunHeaderReader())); |
41 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::RunTrailer, new RunTrailerReader())); |
42 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Log, new LogReader())); |
43 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader())); |
44 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader())); |
45 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TabDump, new TabDumpReader())); |
46 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Tmtc, new TmtcReader())); |
47 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Mcmd, new McmdReader())); |
48 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCal, new CalibCalReader())); |
49 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPed,new CalibCalPedReader())); |
50 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCal, new CalibAcReader())); |
51 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk1, new CalibTrk1Reader())); |
52 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk2, new CalibTrk2Reader())); |
53 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrd, new CalibTrdReader())); |
54 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTof, new CalibTofReader())); |
55 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibS4, new CalibS4Reader())); |
56 |
/* |
57 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::HA_Header_E5, |
58 |
new E5Reader())); |
59 |
*/ |
60 |
} |
61 |
|
62 |
/** |
63 |
* Get a string with the version info of the algorithm. |
64 |
*/ |
65 |
std::string EventReader::GetVersionInfo(void) const { |
66 |
return |
67 |
"$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.4 2004/07/17 20:03:38 kusanagi Exp $\n"; |
68 |
} |
69 |
|
70 |
/** |
71 |
* Initialize the algorithm with a special run. This will initialize the |
72 |
* event reader routines for all packet types. |
73 |
*/ |
74 |
void EventReader::Init(PamelaRun *run) { |
75 |
SetInputStream(run); |
76 |
|
77 |
//Create the structure of directories and create xxx.Header.root files |
78 |
run->WriteHeaders(this, &Header); |
79 |
|
80 |
//Create the xxx.root in it's specific directory |
81 |
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
82 |
i != TechmodelAlgorithmMap.end(); i++) { |
83 |
cat << log4cpp::Priority::DEBUG |
84 |
<< " Initializing algo " << i->second->GetAlgorithmName() |
85 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
86 |
i->second->Init(run); |
87 |
} |
88 |
Run = dynamic_cast<TechmodelPamelaRun*>(run); |
89 |
} |
90 |
|
91 |
static void SkipToNextHeader(ifstream *); |
92 |
|
93 |
/** |
94 |
* Read the next event header, call the reader algorithms that |
95 |
* correspond to its packet type, and read the event trailer. |
96 |
*/ |
97 |
|
98 |
// 15 June 2004 ---note------------------ |
99 |
// A nice refacoring would require to not read again the packet data (which is |
100 |
// read in the UnpackPscuHeader() and put directly in the |
101 |
// EventAlgorithm->RunEvent(....) directly the data inside the packet.... will see later.... |
102 |
// 15 June 2004 ---note------------------ |
103 |
void EventReader::RunEvent(int EventNumber) { |
104 |
|
105 |
cat << log4cpp::Priority::ERROR << "Start Unpacking........" |
106 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
107 |
//for now i'll suppose that the raw file starts immediately with the right bytes |
108 |
//to insert a GetPacketStart() |
109 |
while (!InputFile->eof()){ |
110 |
try { |
111 |
if (FindStart()) { |
112 |
if(UnpackPscuHeader()){ |
113 |
const PacketType* type = Header->GetPscuHeader()->GetPacketType(); |
114 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
115 |
if (i != TechmodelAlgorithmMap.end()) { |
116 |
TechmodelAlgorithm *EventAlgorithm = i->second; |
117 |
EventAlgorithm->RunEvent(EventNumber, Header->GetPscuHeader()->GetPacketLenght()); |
118 |
Run->FillTrees(type); |
119 |
Header->GetCounter()->Increment(type); |
120 |
} else { |
121 |
cat << log4cpp::Priority::ERROR |
122 |
<< "No way to read events of type" << type->GetName() |
123 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
124 |
throw Exception("No way to read events of type " + type->GetName()); |
125 |
} |
126 |
} //else { |
127 |
//**TO BE DONE** <<WRONG CRC l --- LOST PACKET>>// |
128 |
//Have to be defined a "WrongCRC" packet::Type? |
129 |
//} |
130 |
} |
131 |
} catch (...) { |
132 |
//This have to be more detailed. More exceptions type are needed. |
133 |
cat << log4cpp::Priority::ERROR << "Couldn't read the event. Skipping to the next header." |
134 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
135 |
} |
136 |
} |
137 |
Header->GetCounter()->PrintCounters(); |
138 |
} |
139 |
|
140 |
static void SkipToNextHeader(ifstream* TechmodelFile) { |
141 |
// yeah. |
142 |
} |
143 |
|
144 |
|
145 |
|
146 |
/** |
147 |
* Unpack the PSCU header from a file into the structure. |
148 |
*/ |
149 |
int EventReader::UnpackPscuHeader(void) throw (std::exception) { |
150 |
int response; |
151 |
char buff[16]; |
152 |
InputFile->read(buff, sizeof(buff)); |
153 |
|
154 |
prevPckCounter = 0; |
155 |
prevPckOBT = 0; |
156 |
|
157 |
unsigned char PacketId1 = buff[3]; |
158 |
unsigned char PacketId2 = buff[4]; |
159 |
unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); |
160 |
unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); |
161 |
unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); |
162 |
unsigned char CRC = buff[15]; |
163 |
|
164 |
if (Counter < prevPckCounter){ |
165 |
cat << log4cpp::Priority::ERROR |
166 |
<< " Packet counter is less than before of " << (prevPckCounter - Counter) |
167 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
168 |
} |
169 |
|
170 |
if (OrbitalTime < prevPckOBT){ |
171 |
cat << log4cpp::Priority::WARN |
172 |
<< " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime) |
173 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
174 |
} |
175 |
|
176 |
|
177 |
if ((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]){ |
178 |
long int initPos = InputFile->tellg(); |
179 |
long int finalPos; |
180 |
Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); |
181 |
Header->GetPscuHeader()->SetCounter(Counter); |
182 |
Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); |
183 |
//PacketLength is the length of the whole DATApacket starting from the first byte after the header |
184 |
//plus the CRC legth (which varies for each type of packet) |
185 |
Header->GetPscuHeader()->SetPacketLenght(PacketLenght); |
186 |
Header->GetPscuHeader()->SetCRC(CRC); |
187 |
|
188 |
//commented out because of the above test code |
189 |
InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur); |
190 |
FindStart(); |
191 |
finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght())); |
192 |
if(finalPos == 0){ |
193 |
cat << log4cpp::Priority::INFO << " Correct packet length \n" |
194 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
195 |
} |
196 |
if (finalPos > 0 && finalPos < 64) { |
197 |
cat << log4cpp::Priority::WARN |
198 |
<< " Correct packet length: Padded of " << finalPos << " bytes \n" |
199 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
200 |
} |
201 |
if (finalPos > 64){ |
202 |
cat << log4cpp::Priority::ERROR << " Wrong packet length? (because of wrong padding?) \n" |
203 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
204 |
} |
205 |
InputFile->seekg(initPos, std::ios::beg); |
206 |
response = true; |
207 |
} else { |
208 |
cat << log4cpp::Priority::ERROR << " WRONG CRC FOR HEADER " |
209 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
210 |
response = false; |
211 |
} |
212 |
|
213 |
char tmpId1[4]; |
214 |
char tmpId2[4]; |
215 |
char tmpLength[12]; |
216 |
char tmpStart[100]; |
217 |
char tmpCRC[4]; |
218 |
sprintf(tmpId1, "%02X", PacketId1); |
219 |
sprintf(tmpId2, "%02X", PacketId2); |
220 |
sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght()); |
221 |
sprintf(tmpStart, "%X", ((long int)(InputFile->tellg()) - 16)); |
222 |
sprintf(tmpCRC, "%02X", CRC); |
223 |
cat << log4cpp::Priority::INFO |
224 |
<< "\n Packet Counter (decimal) : " << Counter |
225 |
<< "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2 |
226 |
<< "\n Orbital Time (decimal) : " << OrbitalTime |
227 |
<< "\n Lenght : " << tmpLength |
228 |
<< "\n CRC : " << tmpCRC |
229 |
<< "\n Header Start Position : " << tmpStart |
230 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
231 |
return response; |
232 |
} |
233 |
|
234 |
/** |
235 |
* Unpack the trailer of a PSCU event into the structure. |
236 |
*/ |
237 |
void EventReader::UnpackPscuTrailer(void) throw (std::exception) { |
238 |
|
239 |
} |
240 |
|
241 |
/** |
242 |
* Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence |
243 |
*/ |
244 |
int EventReader::FindStart(void) throw (std::exception) { |
245 |
//search an hexadecimal sequence in a file |
246 |
//subSign ------> pointer to the sequence buffer |
247 |
//subSignDim ------> dimension of the buffer |
248 |
// at exit |
249 |
// return true if founds a match (else false) |
250 |
// subF point rigth after the match, if found. Else EOF. |
251 |
//Maurizio 15/11/2002----------------------- |
252 |
const unsigned char subSign[3]={0xFA, 0xFE, 0xDE}; |
253 |
int subSignDim = 3; |
254 |
//------------------------------------------ |
255 |
int subIndex = 0; |
256 |
char dataByte; |
257 |
/*InputFile->read(fileByte, 1); |
258 |
if ( *fileByte == *(subSign+subIndex)){ |
259 |
if (subIndex++ == (subSignDim-1)) { |
260 |
InputFile->seekg(-(subIndex), std::ios::cur); |
261 |
return 1; |
262 |
} |
263 |
}*/ |
264 |
while (!InputFile->eof()) { |
265 |
InputFile->get(dataByte); |
266 |
if (dataByte == (char)(*(subSign+subIndex))){ |
267 |
if (subIndex++ == (subSignDim-1)) { |
268 |
InputFile->seekg(-(subIndex), std::ios::cur); |
269 |
return 1; |
270 |
} |
271 |
} else { |
272 |
if (InputFile->eof()) { |
273 |
throw Exception("Extra bytes over the last packets"); |
274 |
} else { |
275 |
return 0; |
276 |
} |
277 |
//InputFile->seekg(-(subIndex+1), std::ios::cur); |
278 |
//subIndex = 0; |
279 |
} |
280 |
} |
281 |
return 0; |
282 |
} |
283 |
|
284 |
ClassImp(EventReader) |