00001 #include "mEmcRawToFEM.h"
00002 #include "emlLib.h"
00003
00015 long mEmcRawToFEM_(
00016 TABLE_HEAD_ST *dEmcRawData_h, DEMCRAWDATA_ST *dEmcRawData ,
00017 TABLE_HEAD_ST *dEmcFEMData_h, DEMCFEMDATA_ST *dEmcFEMData )
00018 {
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 short sector;
00038 short i,j,k;
00039 long rawdata[5][4608][4][2];
00040 long activemodule[32][4][2];
00041 int ll;
00042 long iarm,isector,iz,iy,itower,imod,ichan,imody,imodz,ichany,ichanz;
00043 long swkey;
00044 long FEMmodule;
00045
00046 static unsigned short header = 0xFFFF;
00047 static short trailer = 0x0000;
00048
00049 short timecell = 1;
00050 short precell = 2;
00051 short postcell = 3;
00052 short iflag,ievno,iclock,idetid;
00053 short ioffset;
00054
00055 short i_FEMData;
00056
00057 long userword[8];
00058
00059
00060 int tempsize;
00061
00062
00063
00064 if(dEmcRawData_h->nok <= 0)
00065 {
00066 printf("Error in mEmcRawToFEM: raw data table empty \n");
00067 return ( STAFCV_BAD);
00068 }
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 for (ll = 0; ll < sizeof(rawdata)/sizeof(rawdata[0][0][0][0]); ll++)
00079 {
00080 *((long *)rawdata + ll) = 0;
00081 }
00082 for (ll = 0; ll < sizeof(activemodule)/sizeof(activemodule[0][0][0]); ll++)
00083 {
00084 *((long *)activemodule + ll) = 0;
00085 }
00086 for (ll = 0; ll < sizeof(userword)/sizeof(userword[0]); ll++)
00087 {
00088 *(userword + ll) = 0;
00089 }
00090
00091
00092
00093 for (i=0;i<8;i++) userword[i] = 0x00005a00;
00094
00095
00096
00097 ievno = dEmcRawData[0].evno & 0xFFFF;
00098 iclock = ievno - 1;
00099 iflag = 0x0001;
00100 idetid = 0x0080;
00101 idetid = 0x0004;
00102
00103 i_FEMData = 0;
00104
00105
00106 for ( i = 0; i < dEmcRawData_h->nok ; i++)
00107 {
00108
00109
00110
00111
00112 swkey = dEmcRawData[i].swkey;
00113 iarm = swkey / 100000; swkey = swkey - 100000 * iarm;
00114 isector = swkey / 10000; swkey = swkey - 10000 * isector;
00115 iy = swkey / 100;
00116 iz = swkey - 100 * iy;
00117
00118
00119 if(iarm == 0 || isector > 1)
00120
00121 {
00122
00123 imody = iy / 12;
00124 imodz = iz / 12;
00125 imod = 6 * imody + imodz;
00126
00127 ichany = iy % 12;
00128 ichanz = iz % 12;
00129 ichan = 12 * ichany + ichanz;
00130 itower = 144 * imod + ichan;
00131 }
00132 else
00133
00134 {
00135
00136 imody = iy / 12;
00137 imodz = iz / 12;
00138 imod = 8 * imody + imodz;
00139
00140 ichany = iy % 12;
00141 ichanz = iz % 12;
00142 ichan = 12 * ichany + ichanz;
00143 itower = 144 * imod + ichan;
00144 }
00145 activemodule[imod][isector][iarm] = 1;
00146
00147
00148
00149
00150
00151
00152
00153 rawdata[0][itower][isector][iarm] = dEmcRawData[i].tdc & 0x00000FFF;
00154 rawdata[1][itower][isector][iarm] =
00155 (dEmcRawData[i].adchipost & 0x00000FFF) + 0x00008000;
00156 rawdata[2][itower][isector][iarm] = dEmcRawData[i].adclopost & 0x00000FFF;
00157 rawdata[3][itower][isector][iarm] =
00158 (dEmcRawData[i].adchipre & 0x00000FFF) + 0x00008000;
00159 rawdata[4][itower][isector][iarm] = dEmcRawData[i].adclopre & 0x00000FFF;
00160
00161 }
00162
00163
00164
00165
00166
00167
00168 for ( isector = 0; isector < 4; isector++)
00169 {
00170 for (iarm = 0; iarm < 2; iarm++)
00171 {
00172 for ( imod = 0; imod < 32; imod++)
00173 {
00174 if( activemodule[imod][isector][iarm] > 0 )
00175 {
00176 dEmcFEMData[i_FEMData].header = header;
00177 dEmcFEMData[i_FEMData].detid = idetid;
00178 dEmcFEMData[i_FEMData].evno = ievno;
00179
00180
00181
00182
00183
00184
00185
00186
00187 if(iarm == 0)
00188 {
00189 FEMmodule = 18 * isector + imod;
00190 }
00191 else
00192 {
00193 if(isector > 1)
00194 {
00195 FEMmodule = 72 + 18 * (isector - 2) + imod;
00196 }
00197 else
00198 {
00199 FEMmodule = 108 + 32 * isector + imod;
00200 }
00201 }
00202 dEmcFEMData[i_FEMData].module = FEMmodule;
00203
00204
00205 dEmcFEMData[i_FEMData].flag = iflag;
00206 dEmcFEMData[i_FEMData].clock = iclock;
00207 dEmcFEMData[i_FEMData].timecell = timecell;
00208 dEmcFEMData[i_FEMData].precell = precell;
00209 dEmcFEMData[i_FEMData].postcell = postcell;
00210 ioffset = imod * 144;
00211 i = 0;
00212
00213 for( itower = ioffset; itower < ioffset + 144; itower++)
00214 {
00215 for ( j = 0; j < 5; j++)
00216 {
00217 dEmcFEMData[i_FEMData].data[i] =
00218 rawdata[j][itower][isector][iarm] & 0x0000FFFF;
00219 i = i + 1;
00220
00221 if( i > 720)
00222 {
00223 printf("Error in mEmcRawToFEM: index overflow \n");
00224 }
00225 }
00226
00227 }
00228
00229 for ( i = 0; i < 8; i++)
00230 {
00231 dEmcFEMData[i_FEMData].userword[i] =
00232 userword[i] & 0x0000FFFF;
00233 }
00234 dEmcFEMData[i_FEMData].longparity = 0;
00235 dEmcFEMData[i_FEMData].trailer = trailer;
00236
00237
00238
00239 i_FEMData = i_FEMData + 1;
00240 }
00241
00242 }
00243 }
00244
00245 }
00246
00247 dEmcFEMData_h->nok = i_FEMData;
00248
00249
00250 return STAFCV_OK;
00251 }