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

Annotation of /yoda/techmodel/EventReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.8 - (hide annotations) (download)
Tue Aug 24 13:19:59 2004 UTC (20 years, 3 months ago) by kusanagi
Branch: MAIN
Changes since 1.7: +6 -4 lines
*** empty log message ***

1 kusanagi 1.1 /** @file
2 kusanagi 1.2 * $Author: kusanagi $
3 kusanagi 1.8 * $Date: 2004/08/20 15:01:42 $
4     * $Revision: 1.7 $
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.8 "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.7 2004/08/20 15:01:42 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.8 InputFile->seekg(-(13), std::ios::cur);
222 kusanagi 1.1 response = false;
223     }
224     char tmpId1[4];
225     char tmpId2[4];
226     char tmpLength[12];
227     char tmpStart[100];
228     char tmpCRC[4];
229     sprintf(tmpId1, "%02X", PacketId1);
230     sprintf(tmpId2, "%02X", PacketId2);
231 kusanagi 1.4 sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());
232 kusanagi 1.1 sprintf(tmpStart, "%X", ((long int)(InputFile->tellg()) - 16));
233     sprintf(tmpCRC, "%02X", CRC);
234     cat << log4cpp::Priority::INFO
235     << "\n Packet Counter (decimal) : " << Counter
236     << "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2
237     << "\n Orbital Time (decimal) : " << OrbitalTime
238     << "\n Lenght : " << tmpLength
239     << "\n CRC : " << tmpCRC
240     << "\n Header Start Position : " << tmpStart
241     << "\n " << log4cpp::CategoryStream::ENDLINE;
242     return response;
243     }
244    
245     /**
246     * Unpack the trailer of a PSCU event into the structure.
247     */
248     void EventReader::UnpackPscuTrailer(void) throw (std::exception) {
249    
250     }
251    
252     /**
253     * Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence
254     */
255     int EventReader::FindStart(void) throw (std::exception) {
256     //search an hexadecimal sequence in a file
257     //subSign ------> pointer to the sequence buffer
258     //subSignDim ------> dimension of the buffer
259     // at exit
260     // return true if founds a match (else false)
261     // subF point rigth after the match, if found. Else EOF.
262     //Maurizio 15/11/2002-----------------------
263     const unsigned char subSign[3]={0xFA, 0xFE, 0xDE};
264     int subSignDim = 3;
265     //------------------------------------------
266     int subIndex = 0;
267     char dataByte;
268     /*InputFile->read(fileByte, 1);
269     if ( *fileByte == *(subSign+subIndex)){
270     if (subIndex++ == (subSignDim-1)) {
271     InputFile->seekg(-(subIndex), std::ios::cur);
272     return 1;
273     }
274     }*/
275     while (!InputFile->eof()) {
276     InputFile->get(dataByte);
277     if (dataByte == (char)(*(subSign+subIndex))){
278     if (subIndex++ == (subSignDim-1)) {
279     InputFile->seekg(-(subIndex), std::ios::cur);
280     return 1;
281     }
282     } else {
283     if (InputFile->eof()) {
284     throw Exception("Extra bytes over the last packets");
285     } else {
286 kusanagi 1.8 subIndex = 0;
287     //return 0;
288 kusanagi 1.1 }
289     //InputFile->seekg(-(subIndex+1), std::ios::cur);
290     //subIndex = 0;
291     }
292     }
293     return 0;
294     }
295    
296     ClassImp(EventReader)

  ViewVC Help
Powered by ViewVC 1.1.23