1 |
|
2 |
#define BYTE unsigned char |
3 |
#include <string> |
4 |
#include <log4cpp/Category.hh> |
5 |
#include <fstream> |
6 |
#include "stdio.h" |
7 |
extern "C" { |
8 |
#include "CRC.h" |
9 |
//Struct per il passaggio di dati da e verso la chiamata fortran |
10 |
extern struct { |
11 |
int IEV2; |
12 |
int calped[4][11][96]; |
13 |
int calgood[4][11][96]; |
14 |
int calthr[4][11][6]; |
15 |
int calrms[4][11][96]; |
16 |
int calbase[4][11][6]; |
17 |
int calvar[4][11][96]; |
18 |
int calpuls[4][11][96]; |
19 |
} calib_; |
20 |
|
21 |
//external declaration of the Fortran function |
22 |
void calpedestal_(short[], long int*, int*); |
23 |
} |
24 |
|
25 |
#include "ReaderAlgorithms.h" |
26 |
|
27 |
#include "event/CalibCalPedEvent.h" |
28 |
|
29 |
using namespace pamela; |
30 |
using namespace pamela::techmodel; |
31 |
|
32 |
static log4cpp::Category& cat = log4cpp::Category::getInstance("pamela.techmodel.CalibCalPedReader"); |
33 |
|
34 |
/** |
35 |
* Constructor. |
36 |
*/ |
37 |
CalibCalPedReader::CalibCalPedReader(void): |
38 |
TechmodelAlgorithm(PacketType::Log, "TechmodelCalibCalPedReader") { |
39 |
cat << log4cpp::Priority::DEBUG |
40 |
<< "Constructor " |
41 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
42 |
calibCalPed = new CalibCalPedEvent(); |
43 |
} |
44 |
|
45 |
/** |
46 |
* Get a string with the version info of the algorithm. |
47 |
*/ |
48 |
std::string CalibCalPedReader::GetVersionInfo(void) const { |
49 |
return |
50 |
"$Header: /home/cvsmanager/yoda/techmodel/CalibCalPedReader.cpp,v 1.5 2004/08/20 15:01:41 kusanagi Exp $\n"; |
51 |
} |
52 |
|
53 |
/** |
54 |
* Initialize the algorithm with a special run. This will initialize the |
55 |
* event reader routines for all packet types. |
56 |
*/ |
57 |
void CalibCalPedReader::Init(PamelaRun *run) { |
58 |
SetInputStream(run); |
59 |
run->WriteSubPacket(this, &calibCalPed, calibCalPed->Class()); |
60 |
} |
61 |
|
62 |
/** |
63 |
* Unpack the CalibCalPed event from an input file. |
64 |
*/ |
65 |
void CalibCalPedReader::RunEvent(int EventNumber, long int length) { |
66 |
|
67 |
char *packetData; |
68 |
char CRCevent[2]; |
69 |
UINT16 calculatedCRC = 0; //calculated CRC |
70 |
UINT16 readCRC = 0; //read CRC |
71 |
long int dataLength; |
72 |
int ERROR; |
73 |
|
74 |
dataLength = length - 2; |
75 |
packetData = new char[dataLength]; |
76 |
InputFile->read(packetData, sizeof(packetData)); |
77 |
InputFile->read(CRCevent, sizeof(CRCevent)); |
78 |
|
79 |
calculatedCRC = CM_Compute_CRC16(0, (BYTE*)packetData, dataLength); |
80 |
readCRC = ((UINT16)(CRCevent[0]<<8)&0xFF00) + ((UINT16)(CRCevent[1])&0x00FF); |
81 |
|
82 |
if (calculatedCRC == readCRC) { |
83 |
calpedestal_((short*)packetData, &dataLength, &ERROR); |
84 |
if (ERROR != 0) { |
85 |
char *errmsg; |
86 |
switch (ERROR){ |
87 |
case 1: errmsg = "CALORIMETER NOT FOUND"; |
88 |
} |
89 |
cat << log4cpp::Priority::ERROR |
90 |
<< "Fortran77 function calpedestal error code = " << ERROR |
91 |
<< errmsg |
92 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
93 |
} else { |
94 |
//Store the unpacked data |
95 |
calibCalPed->IEV2 = calib_.IEV2; |
96 |
//--------have to invert array because of FORTRAN <-> C different management of the indexes |
97 |
int tempCalped[96][11][4]; |
98 |
int tempCalgood[96][11][4]; |
99 |
int tempCalthr[6][11][4]; |
100 |
int tempCalrms[96][11][4]; |
101 |
int tempCalbase[6][11][4]; |
102 |
int tempCalvar[6][11][4]; |
103 |
int tempCalpuls[96][11][4]; |
104 |
|
105 |
memcpy(tempCalped, calib_.calped, sizeof(tempCalped)); |
106 |
memcpy(tempCalgood, calib_.calgood, sizeof(tempCalgood)); |
107 |
memcpy(tempCalthr, calib_.calthr, sizeof(tempCalthr)); |
108 |
memcpy(tempCalrms, calib_.calrms, sizeof(tempCalrms)); |
109 |
memcpy(tempCalbase, calib_.calbase, sizeof(tempCalbase)); |
110 |
memcpy(tempCalvar, calib_.calvar, sizeof(tempCalvar)); |
111 |
memcpy(tempCalpuls, calib_.calpuls, sizeof(tempCalpuls)); |
112 |
|
113 |
for (int i = 0; i < 4; i++){ |
114 |
for (int j = 0; j <11; j++){ |
115 |
for (int z = 0; z < 96; z++){ |
116 |
calibCalPed->calped[i][j][z] = tempCalped[z][j][i]; |
117 |
calibCalPed->calgood[i][j][z] = tempCalgood[z][j][i]; |
118 |
calibCalPed->calrms[i][j][z] = tempCalrms[z][j][i]; |
119 |
calibCalPed->calpuls[i][j][z] = tempCalpuls[z][j][i]; |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
for (int i = 0; i < 4; i++){ |
125 |
for (int j = 0; j <11; j++){ |
126 |
for (int z = 0; z < 6; z++){ |
127 |
calibCalPed->calthr[i][j][z] = tempCalthr[z][j][i]; |
128 |
calibCalPed->calbase[i][j][z] = tempCalbase[z][j][i]; |
129 |
calibCalPed->calvar[i][j][z] = tempCalvar[z][j][i]; |
130 |
} |
131 |
} |
132 |
} |
133 |
//----------------------------------------------------------------------------------------- |
134 |
} |
135 |
} else { |
136 |
cat << log4cpp::Priority::ERROR |
137 |
<< "The test of calculated CRC with one wrote on file FAILED!!" |
138 |
<< "\n " << log4cpp::CategoryStream::ENDLINE; |
139 |
} |
140 |
delete [] packetData; |
141 |
} |
142 |
|
143 |
|