1 |
/** @file |
2 |
* $Author: mocchiut $ |
3 |
* $Date: 2009/07/24 13:53:45 $ |
4 |
* $Revision: 1.3 $ |
5 |
* |
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 "EventReader.h" |
11 |
#include "ReaderAlgorithms.h" |
12 |
|
13 |
extern "C" { |
14 |
#include "CRC.h" |
15 |
} |
16 |
|
17 |
using namespace pamela; |
18 |
using namespace pamela::techmodel; |
19 |
|
20 |
EventReader::~EventReader(){ |
21 |
if(Header){delete Header; Header=NULL;} |
22 |
} |
23 |
|
24 |
/** |
25 |
* Constructor. |
26 |
*/ |
27 |
|
28 |
EventReader::EventReader(): |
29 |
TechmodelAlgorithm(0, "TechmodelEventReader"){ |
30 |
Header = new EventHeader(); |
31 |
|
32 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::PhysEndRun, new PhysEndRunReader)); |
33 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse1, new CalibCalPulse1Reader)); |
34 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPulse2, new CalibCalPulse2Reader)); |
35 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Physics, new PhysicsReader)); |
36 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrkBoth, new CalibTrkBothReader)); |
37 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk1, new CalibTrk1Reader)); |
38 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTrk2, new CalibTrk2Reader)); |
39 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibTof, new CalibTofReader)); |
40 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibS4, new CalibS4Reader)); |
41 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCalPed, new CalibCalPedReader)); |
42 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib1_Ac1, new Calib1_Ac1Reader)); |
43 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib2_Ac1, new Calib2_Ac1Reader)); |
44 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib1_Ac2, new Calib1_Ac2Reader)); |
45 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib2_Ac2, new Calib2_Ac2Reader)); |
46 |
//TODO: marco maybe a bug?: due volte Calib2_Ac2. ?? |
47 |
// TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Calib2_Ac2, new CalibCalReader)); |
48 |
//TODO: marco maybe a bug? |
49 |
// questa dovrebbe essere cosi ma poi c'e' un errore in Pamela Run: in WriteHeaders non ho CalibCal |
50 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalibCal, new CalibCalReader)); |
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::Log, new LogReader)); |
59 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::VarDump, new VarDumpReader)); |
60 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ArrDump, new ArrDumpReader)); |
61 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TabDump, new TabDumpReader)); |
62 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Tmtc, new TmtcReader)); |
63 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Mcmd, new McmdReader)); |
64 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::ForcedFECmd, new ForcedFECmdReader)); |
65 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac1Init, new Ac1InitReader)); |
66 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalInit, new CalInitReader)); |
67 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkInit, new TrkInitReader)); |
68 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofInit, new TofInitReader)); |
69 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgInit, new TrgInitReader)); |
70 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::NdInit, new NdInitReader)); |
71 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Init, new S4InitReader)); |
72 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac2Init, new Ac2InitReader)); |
73 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::CalAlarm, new CalAlarmReader)); |
74 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac1Alarm, new Ac1AlarmReader)); |
75 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrkAlarm, new TrkAlarmReader)); |
76 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TrgAlarm, new TrgAlarmReader)); |
77 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TofAlarm, new TofAlarmReader)); |
78 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::S4Alarm, new S4AlarmReader)); |
79 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::Ac2Alarm, new Ac2AlarmReader)); |
80 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TsbT, new TsbTReader)); |
81 |
TechmodelAlgorithmMap.insert(AlgorithmMap::value_type(PacketType::TsbB, new TsbBReader)); |
82 |
} |
83 |
|
84 |
/** |
85 |
* Initialize the algorithm with a special run. This will initialize the |
86 |
* event reader routines for all packet types. |
87 |
*/ |
88 |
void EventReader::Init(PamelaRun *run) { |
89 |
//Create the structure of directories and create xxx.Header.root files |
90 |
run->WriteHeaders(this, &Header); |
91 |
//Create the xxx.root in it's specific directory |
92 |
for (AlgorithmMap::iterator i = TechmodelAlgorithmMap.begin(); |
93 |
i != TechmodelAlgorithmMap.end(); i++) { |
94 |
// cout << " Initializing algo " << i->second->GetAlgorithmName()<<endl; |
95 |
i->second->Init(run); |
96 |
} |
97 |
|
98 |
if ( Header ) { delete Header; Header = new EventHeader(); }; // EMILIANO, reset Counter when opening a new file |
99 |
|
100 |
Run=run; |
101 |
} |
102 |
|
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 |
* if the packet is saved return 0. |
108 |
* |
109 |
* return values: meaning: |
110 |
0 packet good |
111 |
1 exception but used //never happen |
112 |
2 CRC exception but used |
113 |
3 CALIBRATION PACKET with error but used |
114 |
-1 CRC exception packet DISCARDED |
115 |
-2 FATAL exception packet DISCARDED //never happen |
116 |
-3 No way to read events of this type. packet DISCARDED( es CalibCal --old) |
117 |
//in PacketUser I will put ret = 10 |
118 |
ret = 10 if the packet is good but comes from a cadre with VRL problems |
119 |
|
120 |
* |
121 |
*/ |
122 |
int EventReader::PKT_RunEvent(char* headerPkt, char* pamPkt, long int length, const PacketType* type){ |
123 |
stringstream oss; |
124 |
string msg; |
125 |
int ret=0; |
126 |
PKT_UnpackPscuHeader(headerPkt); |
127 |
AlgorithmMap::iterator i = TechmodelAlgorithmMap.find(type); |
128 |
if (i != TechmodelAlgorithmMap.end()) |
129 |
{ |
130 |
TechmodelAlgorithm* EventAlgorithm(i->second); |
131 |
try{ |
132 |
EventAlgorithm->PKT_RunEvent(pamPkt, length); |
133 |
} |
134 |
catch (WrongCRCException_PKTUsed &exc){ |
135 |
//questo tipo di eccezione NON mi preoccupa, |
136 |
//il pacchetto e' stato utilizzato e salvato ugualmente |
137 |
ret=2; |
138 |
oss.str(""); |
139 |
oss << exc.print(); |
140 |
oss << " Return "<<ret; |
141 |
msg=oss.str(); |
142 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
143 |
} |
144 |
catch (CalibException &exc){ |
145 |
//questo tipo di eccezione mi preoccupa un pochino, |
146 |
//il pacchetto e' stato utilizzato e salvato ugualmente |
147 |
ret=3; |
148 |
oss.str(""); |
149 |
oss << exc.print(); |
150 |
oss << " Return "<<ret; |
151 |
msg=oss.str(); |
152 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
153 |
} |
154 |
catch (WrongCRCException &exc){ |
155 |
//questo tipo di eccezione mi preoccupa, |
156 |
//il pacchetto non e' stato utilizzato ne salvato |
157 |
ret=-1; |
158 |
oss.str(""); |
159 |
oss << exc.print(); |
160 |
oss << " The Packet is Discarded "; |
161 |
oss << " Return "<<ret; |
162 |
msg=oss.str(); |
163 |
PamOffLineSW::mainLogUtil->logError(msg); |
164 |
return ret; |
165 |
} |
166 |
catch (FatalException &exc){ |
167 |
ret=-2; |
168 |
oss.str(""); |
169 |
oss << exc.print(); |
170 |
oss << " The Packet is Discarded "; |
171 |
oss << " Return "<<ret; |
172 |
msg=oss.str(); |
173 |
PamOffLineSW::mainLogUtil->logError(msg); |
174 |
return ret; |
175 |
} |
176 |
catch (Exception &exc){ |
177 |
ret=1; |
178 |
oss.str(""); |
179 |
oss << exc.print(); |
180 |
oss << " Return "<<ret; |
181 |
msg=oss.str(); |
182 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
183 |
} |
184 |
// catch (NotExistingAlgorithmException exc) {} |
185 |
|
186 |
Run->FillTrees(type); |
187 |
// cout << Header->GetCounter()->Get(type) << endl; |
188 |
Header->GetCounter()->Increment(type); |
189 |
// cout << Header->GetCounter()->Get(type) << endl; |
190 |
} |
191 |
else |
192 |
{ |
193 |
ret=-3; |
194 |
oss.str(""); |
195 |
// oss << " No way to read events of type " << type->GetName().c_str()<<". The Packet is skipped "; |
196 |
oss << " No way to read events of type " << type->GetName()<<". The Packet is skipped "; |
197 |
oss << " Return "<<ret; |
198 |
msg=oss.str(); |
199 |
PamOffLineSW::mainLogUtil->logWarning(msg); |
200 |
return ret; |
201 |
} |
202 |
return ret; |
203 |
} |
204 |
|
205 |
/** |
206 |
* Unpack the PSCU header from the pkt into the structure. |
207 |
*/ |
208 |
//fill the header structure, |
209 |
void EventReader::PKT_UnpackPscuHeader(char* buff) |
210 |
{ |
211 |
unsigned char PacketId1 = buff[3]; |
212 |
unsigned char PacketId2 = buff[4]; |
213 |
unsigned int Counter = (((UINT32)buff[5]<<16)&0x00FF0000) + (((UINT32)buff[6]<<8)&0x0000FF00) + (((UINT32)buff[7])&0x000000FF); |
214 |
unsigned int OrbitalTime = (((UINT32)buff[8]<<24)&0xFF000000) + (((UINT32)buff[9]<<16)&0x00FF0000) + (((UINT32)buff[10]<<8)&0x0000FF00) + (((UINT32)buff[11])&0x000000FF); |
215 |
unsigned int PacketLenght = (((UINT32)buff[12]<<16)&0x00FF0000) + (((UINT32)buff[13]<<8)&0x0000FF00) + (((UINT32)buff[14])&0x000000FF); |
216 |
unsigned char CRC = buff[15]; |
217 |
// unsigned char FileOffset = 0;// ?? |
218 |
|
219 |
Header->GetPscuHeader()->SetPacketId(PacketId1, PacketId2); |
220 |
Header->GetPscuHeader()->SetCounter(Counter); |
221 |
Header->GetPscuHeader()->SetOrbitalTime(OrbitalTime); |
222 |
//PacketLength is the length of the whole DATApacket starting from the first byte after the header |
223 |
//plus the CRC legth (which varies for each type of packet) |
224 |
Header->GetPscuHeader()->SetPacketLenght(PacketLenght); |
225 |
|
226 |
Header->GetPscuHeader()->SetCRC(CRC); |
227 |
Header->GetPscuHeader()->SetFileOffset(0);//I don't need this !! |
228 |
} |
229 |
|
230 |
|
231 |
|
232 |
/** |
233 |
* Get a string with the version info of the algorithm. |
234 |
*/ |
235 |
std::string EventReader::GetVersionInfo(void) const { |
236 |
return |
237 |
"$Header: /afs/ba.infn.it/user/pamela/src/CVS/chewbacca/PamOffLineSW/techmodel/EventReader.cpp,v 1.3 2009/07/24 13:53:45 mocchiut Exp $\n"; |
238 |
} |
239 |
|
240 |
|
241 |
ClassImp(EventReader) |