00001
00002
00003 #include "RawTofConverter.h"
00004
00005 #include "OnlineUtil/mdBlockDefinitions.h"
00006 #include "Validity/VldTimeStamp.h"
00007 #include "OnlineUtil/rdChecksum.h"
00008
00009 RawTofConverter::RawTofConverter(RawRecord* rr)
00010 :
00011 fRR(rr), fTDC(0), fNBlocks(0)
00012 {
00013 if(fRR!=0){
00014 fTDC = static_cast<const RawRopTdcRawDataBlock*>
00015 (fRR->FindRawBlock("RawRopTdcRawDataBlock"));
00016 }
00017 if(fTDC) ConstructTofBlocks();
00018 }
00019
00020 RawTofConverter::~RawTofConverter()
00021 {
00022 for(unsigned int blk=0; blk<fPackedRawTOFBlocks.size(); blk++)
00023 {
00024 delete [] fPackedRawTOFBlocks[blk];
00025 }
00026 }
00027 RawTOFBlock* RawTofConverter::AbandonRawTOFBlock(int n, long* varcp)
00028 {
00029
00030
00031
00032 RawTOFBlock* result = 0;
00033 if(n>=fNBlocks) return result;
00034
00035 int* p = fPackedRawTOFBlocks[n];
00036 p[7] = *varcp;
00037 p[8] = *(varcp+1);
00038
00039
00040 rdxsum_fill(reinterpret_cast<long*>(p), 0);
00041
00042
00043 result = new RawTOFBlock(fPackedRawTOFBlocks[n]);
00044 return result;
00045 }
00046
00047 int* RawTofConverter::GetPackedRawTOFBlock(int n, long* varcp)
00048 {
00049
00050 int* result = 0;
00051 if(n>=fNBlocks) return result;
00052
00053 int* p = fPackedRawTOFBlocks[n];
00054 p[7] = *varcp;
00055 p[8] = *(varcp+1);
00056
00057
00058 rdxsum_fill(reinterpret_cast<long*>(p), 0);
00059
00060 result = p;
00061
00062 return result;
00063 }
00064
00065 RawTOFBlock* RawTofConverter::AbandonRawTOFBlock(int n, const RawDigit& rd)
00066 {
00067 RawTOFBlock* result = 0;
00068 if(n>=fNBlocks) return result;
00069
00070 int rcid_encoded= rd.GetChannel().GetEncoded();
00071 int adc = rd.GetADC();
00072 int tdc = rd.GetTDC();
00073
00074
00075
00076
00077
00078 int varc1 = 0x80000000;
00079
00080 varc1|=0x20000000;
00081
00082 varc1|=(((rcid_encoded&0x00001fff)<<16)&0x1fff0000);
00083
00084 varc1|=0x00008000;
00085
00086 varc1|=(adc&0x00007fff);
00087
00088
00089 int varc2=0x0;
00090 varc2|=(tdc&0x3fffffff);
00091
00092
00093
00094
00095 if(!ParityOK(varc1)) varc1|=0x40000000;
00096
00097 if(!ParityOK(varc2)) varc2|=0x40000000;
00098
00099
00100
00101 int* p = fPackedRawTOFBlocks[n];
00102 p[7]=varc1;
00103 p[8]=varc2;
00104
00105
00106 rdxsum_fill(reinterpret_cast<long*>(p), 0);
00107
00108
00109 result = new RawTOFBlock(fPackedRawTOFBlocks[n]);
00110 return result;
00111
00112 }
00113
00114 bool RawTofConverter::ParityOK(int i) const
00115 {
00116 int sum = 0;
00117 for(int j=0; j<32; j++){
00118 sum+=(i&1);
00119 i=i>>1;
00120 }
00121 if(sum%2==0) return true;
00122 else return false;
00123 }
00124
00125 void RawTofConverter::ReadDataWord(int input, int& v, int& un, int& ov, int& tdc)
00126 {
00127
00128
00129 const int vmask = 0x4000;
00130 const int unmask = 0x2000;
00131 const int ovmask = 0x1000;
00132 const int tdcmask = 0xfff;
00133 v = (input&vmask)>>14;
00134 un = (input&unmask)>>13;
00135 ov = (input&ovmask)>>12;
00136 tdc = input&tdcmask;
00137
00138 }
00139
00140
00141 int RawTofConverter::ConstructTofBlocks()
00142 {
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153 if(fTDC==0) return 0;
00154
00155
00156 const int orig_blkid = fTDC->GetBlockId().GetEncoded();
00157 const int orig_crid = fTDC->GetCrateId();
00158
00159
00160
00161
00162 const VldTimeStamp orig_ts = fTDC->GetTimeStamp();
00163 const int orig_t0_0 = orig_ts.GetSec();
00164 const int orig_t0_1 = orig_ts.GetNanoSec();
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175 const int orig_bytes = fTDC->GetNumberOfTdcReadoutBytes();
00176 const char* orig_cdata = fTDC->GetTdcData();
00177 const int orig_nints = orig_bytes/sizeof(unsigned long);
00178
00179
00180 const int* orig_idata = reinterpret_cast<const int*>(orig_cdata);
00181
00182
00183
00184 const int* iptr = orig_idata;
00185
00186
00187 int* raw_tof_blk = 0;
00188 while (iptr<(orig_idata+orig_nints))
00189 {
00190
00191
00192 const int* const hptr = iptr;
00193
00194
00195
00196 const int nchanmask = 0x3f00;
00197 const int nchan = ((*iptr)&nchanmask)>>8;
00198
00199 const int* const dptr = hptr+1;
00200
00201 const int* const tptr = dptr+nchan;
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 const int tof_blk_hdr_words = 9;
00215
00216
00217
00218
00219
00220
00221 const int tof_blk_tdc_words = 2 + nchan;
00222
00223
00224
00225
00226 const int tof_blk_trl_words = 1;
00227
00228 const int tof_blk_words = tof_blk_hdr_words
00229 + tof_blk_tdc_words
00230 + tof_blk_trl_words;
00231
00232
00233
00234 raw_tof_blk = new int[tof_blk_words];
00235
00236
00237
00238
00239
00240 fNBlocks++;
00241
00242
00244
00245
00246
00247
00248
00249 raw_tof_blk[0] = tof_blk_words-1;
00250
00251
00252
00253 raw_tof_blk[1] = 0;
00254
00255
00256 int newblkid = orig_blkid;
00257 newblkid&=0xfff000ff;
00258
00259 newblkid|=((kMdBlockTofReadout<<8)&0x000fff00);
00260 raw_tof_blk[2] = newblkid;
00261
00262
00263
00264 raw_tof_blk[3] = orig_crid;
00265
00266
00267
00268
00269
00270 raw_tof_blk[4] = 1;
00271
00272
00273
00274
00275 raw_tof_blk[5] = orig_t0_0;
00276 raw_tof_blk[6] = orig_t0_1;
00277
00278
00279
00280
00281 raw_tof_blk[7] = 0;
00282 raw_tof_blk[8] = 0;
00283
00284
00285
00286 raw_tof_blk[9] = *hptr;
00287
00288
00289
00290
00291 for(int tdc_data_idx = 0; tdc_data_idx<nchan; tdc_data_idx++)
00292 {
00293
00294
00295
00296 raw_tof_blk[10+tdc_data_idx]=*(dptr+tdc_data_idx);
00297 }
00298
00299
00300
00301 raw_tof_blk[10+nchan] = *tptr;
00302
00303
00304
00305
00306 iptr+=(2+nchan);
00307
00308
00309 fPackedRawTOFBlocks.push_back(raw_tof_blk);
00310
00311 }
00312 return fNBlocks;
00313 }
00314
00315
00316 void RawTofConverter::Dump(ostream& os)
00317 {
00318
00319 os<<"Dumping the contents of RawTofConverter:"<<std::endl;
00320 for(unsigned int blk = 0; blk<fPackedRawTOFBlocks.size(); blk++)
00321 {
00322 RawTOFBlock rtb(fPackedRawTOFBlocks[blk]);
00323 rtb.FormatToOStream(os,"");
00324 }
00325 }
00326
00327
00328
00329
00330
00331
00332
00333