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

Annotation of /yoda/techmodel/EventReader.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2.0 - (hide annotations) (download)
Tue Sep 21 20:50:54 2004 UTC (20 years, 2 months ago) by kusanagi
Branch: MAIN
Changes since 1.10: +3 -3 lines
Major release

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

  ViewVC Help
Powered by ViewVC 1.1.23