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