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

Annotation of /yoda/techmodel/EventReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.9 - (hide annotations) (download)
Wed Aug 25 10:14:26 2004 UTC (20 years, 3 months ago) by kusanagi
Branch: MAIN
Changes since 1.8: +6 -4 lines
*** empty log message ***

1 kusanagi 1.1 /** @file
2 kusanagi 1.2 * $Author: kusanagi $
3 kusanagi 1.9 * $Date: 2004/08/24 13:19:59 $
4     * $Revision: 1.8 $
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.9 "$Header: /home/cvsmanager/yoda/techmodel/EventReader.cpp,v 1.8 2004/08/24 13:19:59 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 kusanagi 1.9 unsigned char FileOffset = buff[15];
173 kusanagi 1.1
174     if (Counter < prevPckCounter){
175 kusanagi 1.6 cat << log4cpp::Priority::WARN
176 kusanagi 1.1 << " Packet counter is less than before of " << (prevPckCounter - Counter)
177 kusanagi 1.7 << " " << log4cpp::CategoryStream::ENDLINE;
178 kusanagi 1.1 }
179    
180     if (OrbitalTime < prevPckOBT){
181     cat << log4cpp::Priority::WARN
182     << " Orbital Time is less than before of " << (prevPckOBT - OrbitalTime)
183 kusanagi 1.7 << " " << log4cpp::CategoryStream::ENDLINE;
184 kusanagi 1.1 }
185    
186    
187 kusanagi 1.7 if (((BYTE)CM_Compute_CRC16(0, (BYTE*)&buff, 15) == (BYTE)buff[15]) && (PacketId1 == PacketId2)){
188     prevPckCounter = Counter;
189     prevPckOBT = OrbitalTime;
190 kusanagi 1.1 long int initPos = InputFile->tellg();
191     long int finalPos;
192     Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2);
193     Header->GetPscuHeader()->SetCounter(Counter);
194     Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime);
195     //PacketLength is the length of the whole DATApacket starting from the first byte after the header
196     //plus the CRC legth (which varies for each type of packet)
197     Header->GetPscuHeader()->SetPacketLenght(PacketLenght);
198     Header->GetPscuHeader()->SetCRC(CRC);
199 kusanagi 1.9 Header->GetPscuHeader()->SetFileOffset(((long int)(InputFile->tellg()) - 16));
200 kusanagi 1.1
201     //commented out because of the above test code
202     InputFile->seekg(Header->GetPscuHeader()->GetPacketLenght(), std::ios::cur);
203     FindStart();
204     finalPos = (long int)InputFile->tellg() - (1 + initPos + (long int)(Header->GetPscuHeader()->GetPacketLenght()));
205     if(finalPos == 0){
206 kusanagi 1.7 cat << log4cpp::Priority::DEBUG << " Correct packet length"
207     << " " << log4cpp::CategoryStream::ENDLINE;
208 kusanagi 1.1 }
209     if (finalPos > 0 && finalPos < 64) {
210     cat << log4cpp::Priority::WARN
211 kusanagi 1.7 << " Correct packet length: Padded of " << finalPos << " bytes"
212     << " " << log4cpp::CategoryStream::ENDLINE;
213 kusanagi 1.1 }
214     if (finalPos > 64){
215 kusanagi 1.7 cat << log4cpp::Priority::ERROR << " Wrong packet length? (because of wrong padding?) "
216     << " " << log4cpp::CategoryStream::ENDLINE;
217 kusanagi 1.1 }
218     InputFile->seekg(initPos, std::ios::beg);
219     response = true;
220     } else {
221     cat << log4cpp::Priority::ERROR << " WRONG CRC FOR HEADER "
222 kusanagi 1.7 << " " << log4cpp::CategoryStream::ENDLINE;
223 kusanagi 1.8 InputFile->seekg(-(13), std::ios::cur);
224 kusanagi 1.1 response = false;
225     }
226     char tmpId1[4];
227     char tmpId2[4];
228     char tmpLength[12];
229     char tmpStart[100];
230     char tmpCRC[4];
231     sprintf(tmpId1, "%02X", PacketId1);
232     sprintf(tmpId2, "%02X", PacketId2);
233 kusanagi 1.4 sprintf(tmpLength, "%06X", Header->GetPscuHeader()->GetPacketLenght());
234 kusanagi 1.9 sprintf(tmpStart, "%X", Header->GetPscuHeader()->GetFileOffset());
235 kusanagi 1.1 sprintf(tmpCRC, "%02X", CRC);
236     cat << log4cpp::Priority::INFO
237     << "\n Packet Counter (decimal) : " << Counter
238     << "\n Id1 - Id2 : " << tmpId1 << " - " << tmpId2
239     << "\n Orbital Time (decimal) : " << OrbitalTime
240     << "\n Lenght : " << tmpLength
241     << "\n CRC : " << tmpCRC
242     << "\n Header Start Position : " << tmpStart
243     << "\n " << log4cpp::CategoryStream::ENDLINE;
244     return response;
245     }
246    
247     /**
248     * Unpack the trailer of a PSCU event into the structure.
249     */
250     void EventReader::UnpackPscuTrailer(void) throw (std::exception) {
251    
252     }
253    
254     /**
255     * Find the next starting poin for the PSCU event looking for a {0xFA, 0xFE, 0xDE} sequence
256     */
257     int EventReader::FindStart(void) throw (std::exception) {
258     //search an hexadecimal sequence in a file
259     //subSign ------> pointer to the sequence buffer
260     //subSignDim ------> dimension of the buffer
261     // at exit
262     // return true if founds a match (else false)
263     // subF point rigth after the match, if found. Else EOF.
264     //Maurizio 15/11/2002-----------------------
265     const unsigned char subSign[3]={0xFA, 0xFE, 0xDE};
266     int subSignDim = 3;
267     //------------------------------------------
268     int subIndex = 0;
269     char dataByte;
270     /*InputFile->read(fileByte, 1);
271     if ( *fileByte == *(subSign+subIndex)){
272     if (subIndex++ == (subSignDim-1)) {
273     InputFile->seekg(-(subIndex), std::ios::cur);
274     return 1;
275     }
276     }*/
277     while (!InputFile->eof()) {
278     InputFile->get(dataByte);
279     if (dataByte == (char)(*(subSign+subIndex))){
280     if (subIndex++ == (subSignDim-1)) {
281     InputFile->seekg(-(subIndex), std::ios::cur);
282     return 1;
283     }
284     } else {
285     if (InputFile->eof()) {
286     throw Exception("Extra bytes over the last packets");
287     } else {
288 kusanagi 1.8 subIndex = 0;
289     //return 0;
290 kusanagi 1.1 }
291     //InputFile->seekg(-(subIndex+1), std::ios::cur);
292     //subIndex = 0;
293     }
294     }
295     return 0;
296     }
297    
298     ClassImp(EventReader)

  ViewVC Help
Powered by ViewVC 1.1.23