00001 #include "DCCDataParser.h"
00002
00003
00004
00005
00006
00007
00008
00009 DCCTBDataParser::DCCTBDataParser(std::vector<ulong> parserParameters, bool parseInternalData,bool debug):
00010 buffer_(0),parseInternalData_(parseInternalData),debug_(debug), parameters(parserParameters){
00011
00012 mapper_ = new DCCTBDataMapper(this);
00013 resetErrorCounters();
00014 computeBlockSizes();
00015
00016 }
00017
00018
00019
00020
00021
00022
00023 void DCCTBDataParser::resetErrorCounters(){
00024
00025 errors_["DCC::BOE"] = 0;
00026 errors_["DCC::EOE"] = 0;
00027 errors_["DCC::EVENT LENGTH"] = 0;
00028 }
00029
00030
00031
00032
00033
00034
00035 void DCCTBDataParser::computeBlockSizes(){
00036 ulong nTT = numbTTs();
00037 ulong tSamples = numbTriggerSamples();
00038 ulong nSr = numbSRF();
00039
00040 ulong tf(0), srf(0);
00041
00042 if( (nTT*tSamples)<4 || (nTT*tSamples)%4 ) tf=1;
00043 else tf=0;
00044
00045
00046 if( srf<16 || srf%16 ) srf=1;
00047 else srf=0;
00048
00049
00050 tccBlockSize_ = 8 + ((nTT*tSamples)/4)*8 + tf*8 ;
00051
00052
00053 srpBlockSize_ = 8 + (nSr/16)*8 + srf*8;
00054 }
00055
00056
00057
00058
00059
00060
00061 void DCCTBDataParser::parseFile(std::string fileName, bool singleEvent){
00062
00063 std::ifstream inputFile;
00064 inputFile.open(fileName.c_str());
00065
00066 resetErrorCounters();
00067
00068
00069
00070
00071
00072
00073
00074 if( !inputFile.fail() ){
00075
00076 std::string myWord;
00077 std::vector<std::string> dataVector;
00078
00079
00080 while( inputFile >> myWord ){
00081 dataVector.push_back( myWord );
00082 }
00083
00084 bufferSize_ = (dataVector.size() ) * 4 ;
00085 if( buffer_ ){ delete [] buffer_; }
00086 buffer_ = new ulong[dataVector.size()];
00087
00088 ulong *myData_ = (ulong *) buffer_;
00089
00090
00091 for(ulong i = 1; i <= dataVector.size() ; i++, myData_++ ){
00092 sscanf((dataVector[i-1]).c_str(),"%x",(uint *)myData_);
00093
00094
00095
00096 }
00097
00098 inputFile.close();
00099
00100 parseBuffer( buffer_,bufferSize_,singleEvent);
00101
00102 }
00103 else{
00104 std::string errorMessage = std::string(" Error::Unable to open file :") + fileName;
00105 throw ECALTBParserException(errorMessage);
00106 }
00107 }
00108
00109
00110
00111
00112
00113
00114 void DCCTBDataParser::parseBuffer(ulong * buffer, ulong bufferSize, bool singleEvent){
00115
00116 resetErrorCounters();
00117
00118 buffer_ = buffer;
00119
00120
00121 processedEvent_ = 0;
00122 events_.clear();
00123 std::vector<DCCTBEventBlock *>::iterator it;
00124 for( it = dccEvents_.begin(); it!=dccEvents_.end(); it++ ) { delete *it; }
00125 dccEvents_.clear();
00126 eventErrors_ = "";
00127
00128
00129
00130
00131
00132
00133 if( bufferSize%8 ){
00134 std::string fatalError ;
00135 fatalError += "\n ======================================================================";
00136 fatalError += "\n Fatal error at event = " + getDecString(events_.size()+1);
00137 fatalError += "\n Buffer Size of = "+ getDecString(bufferSize) + "[bytes] is not divisible by 8 ... ";
00138 fatalError += "\n ======================================================================";
00139 throw ECALTBParserException(fatalError);
00140 }
00141 if ( bufferSize < EMPTYEVENTSIZE ){
00142 std::string fatalError ;
00143 fatalError += "\n ======================================================================";
00144 fatalError += "\n Fatal error at event = " + getDecString(events_.size()+1);
00145 fatalError += "\n Buffer Size of = "+ getDecString(bufferSize) + "[bytes] is less than an empty event ... ";
00146 fatalError += "\n ======================================================================";
00147 throw ECALTBParserException(fatalError);
00148 }
00149
00150 ulong *myPointer = buffer_;
00151
00152
00153 ulong processedBytes(0), wordIndex(0), eventLength(0), errorMask(0);
00154
00155
00156 while( processedBytes + EMPTYEVENTSIZE <= bufferSize ){
00157
00158
00159
00160
00161
00162
00163
00164
00165 ulong bytesToEnd = bufferSize - processedBytes;
00166 std::pair<ulong,ulong> eventD = checkEventLength(myPointer,bytesToEnd,singleEvent);
00167 eventLength = eventD.second;
00168 errorMask = eventD.first;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 if (parseInternalData_){
00184
00185 DCCTBEventBlock *myBlock = new DCCTBEventBlock(this,myPointer,eventLength*8, eventLength*2 -1 ,wordIndex,0);
00186
00187
00188 dccEvents_.push_back(myBlock);
00189 }
00190
00191
00192 std::pair<ulong *, ulong> eventPointer(myPointer,eventLength);
00193 std::pair<ulong, std::pair<ulong*, ulong> > eventPointerWithErrorMask(errorMask,eventPointer);
00194 events_.push_back(eventPointerWithErrorMask);
00195
00196
00197 processedEvent_++;
00198 processedBytes += eventLength*8;
00199
00200
00201
00202 myPointer += eventLength*2;
00203 wordIndex += eventLength*2;
00204 }
00205 }
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 std::pair<ulong,ulong> DCCTBDataParser::checkEventLength(ulong *pointerToEvent, ulong bytesToEnd, bool singleEvent){
00219
00220 std::pair<ulong,ulong> result;
00221 ulong errorMask(0);
00222
00223
00224
00225 ulong *boePointer = pointerToEvent + 1;
00226 if( ((*boePointer)>>BOEBEGIN)& BOEMASK != BOE ) {
00227 (errors_["DCC::BOE"])++; errorMask = 1;
00228 }
00229
00230
00231
00232 ulong * myPointer = pointerToEvent + 2;
00233 ulong eventLength = (*myPointer)&EVENTLENGTHMASK;
00234
00235
00236
00237 bool eoeError = false;
00238
00239
00240 if( singleEvent && eventLength != bytesToEnd/8 ){
00241 eventLength = bytesToEnd/8;
00242 (errors_["DCC::EVENT LENGTH"])++;
00243 errorMask = errorMask | (1<<1);
00244 }
00245
00246 else if( eventLength == 0 || eventLength > (bytesToEnd / 8) || eventLength < (EMPTYEVENTSIZE/8) ){
00247
00248
00249
00250 std::string fatalError;
00251
00252 fatalError +="\n ======================================================================";
00253 fatalError +="\n Fatal error at event = " + getDecString(events_.size()+1);
00254 fatalError +="\n Decoded event length = " + getDecString(eventLength);
00255 fatalError +="\n bytes to buffer end = " + getDecString(bytesToEnd);
00256 fatalError +="\n Unable to procead the data decoding ...";
00257
00258 if(eventLength > (bytesToEnd / 8)){ fatalError +=" (eventLength > (bytesToEnd / 8)";}
00259 else{ fatalError += "\n event length not big enough heaven to build an empty event ( 4x8 bytes)";}
00260
00261 fatalError +="\n ======================================================================";
00262
00263 throw ECALTBParserException(fatalError);
00264 }
00265
00266
00267
00268 ulong *endOfEventPointer = pointerToEvent + eventLength*2 -1;
00269 if (((*endOfEventPointer) >> EOEBEGIN & EOEMASK != EOEMASK) && !eoeError ){
00270 (errors_["DCC::EOE"])++;
00271 errorMask = errorMask | (1<<2);
00272 }
00273
00274
00275 result.first = errorMask;
00276 result.second = eventLength;
00277
00278 return result;
00279 }
00280
00281
00282
00283
00284
00285
00286
00287 std::string DCCTBDataParser::index(ulong position){
00288
00289 char indexBuffer[20];
00290 sprintf(indexBuffer,"W[%08lu]",position);
00291
00292 return std::string(indexBuffer);
00293 }
00294
00295
00296
00297
00298
00299
00300 std::string DCCTBDataParser::getDecString(ulong data){
00301
00302 char buffer[10];
00303 sprintf(buffer,"%lu",data);
00304
00305 return std::string(buffer);
00306 }
00307
00308
00309
00310
00311
00312
00313 std::string DCCTBDataParser::getHexString(ulong data){
00314
00315 char buffer[10];
00316 sprintf(buffer,"0x%08x",(uint)(data));
00317
00318 return std::string(buffer);
00319 }
00320
00321
00322
00323
00324
00325
00326 std::string DCCTBDataParser::getIndexedData(ulong position, ulong *pointer){
00327 std::string ret;
00328
00329
00330
00331
00332
00333
00334
00335 ret = index(position) + getHexString(*pointer);
00336
00337 return ret;
00338 }
00339
00340
00341
00342
00343
00344
00345 DCCTBDataParser::~DCCTBDataParser(){
00346
00347
00348 std::vector<DCCTBEventBlock *>::iterator it;
00349 for(it=dccEvents_.begin();it!=dccEvents_.end();it++){delete *it;}
00350 dccEvents_.clear();
00351
00352 delete mapper_;
00353 }