/[PAMELA software]/yoda/techmodel/EventReader.cpp
ViewVC logotype

Annotation of /yoda/techmodel/EventReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Thu Jul 8 12:31:42 2004 UTC (20 years, 5 months ago) by kusanagi
Branch: MAIN
Changes since 1.2: +17 -15 lines
*** empty log message ***

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

  ViewVC Help
Powered by ViewVC 1.1.23