00001 #include "DCCEventBlock.h"
00002 #include "DCCDataParser.h"
00003 #include "DCCDataMapper.h"
00004 #include "DCCTowerBlock.h"
00005 #include "ECALParserException.h"
00006 #include "ECALParserBlockException.h"
00007 #include "DCCSRPBlock.h"
00008 #include "DCCTCCBlock.h"
00009 #include "DCCXtalBlock.h"
00010 #include "DCCTrailerBlock.h"
00011
00012 #include <iomanip>
00013 #include <sstream>
00014
00015 DCCTBEventBlock::DCCTBEventBlock(
00016 DCCTBDataParser * parser,
00017 uint32_t * buffer,
00018 uint32_t numbBytes,
00019 uint32_t wordsToEnd,
00020 uint32_t wordBufferOffset ,
00021 uint32_t wordEventOffset
00022 ) :
00023 DCCTBBlockPrototype(parser,"DCCHEADER", buffer, numbBytes,wordsToEnd)
00024 ,dccTrailerBlock_(0),srpBlock_(0),wordBufferOffset_(wordBufferOffset) {
00025
00026
00027
00028 errors_["DCC::HEADER"] = 0;
00029 errors_["DCC::EVENT LENGTH"] = 0;
00031
00032 uint32_t wToEnd(0);
00033
00034 try{
00035
00036
00037 if(numbBytes == DCCTBDataParser::EMPTYEVENTSIZE ){
00038 mapperFields_ = parser_->mapper()->emptyEventFields();
00039 emptyEvent = true;
00040 }else{
00041 mapperFields_ = parser_->mapper()->dccFields();
00042 emptyEvent = false;
00043 }
00044
00045 try{ parseData(); }
00046 catch (ECALTBParserBlockException &e){}
00048
00049
00050
00051
00052 if(parser_->debug()){ dataCheck(); }
00054
00055
00056
00057
00058 if( !emptyEvent && getDataField("DCC ERRORS")!= DCCERROR_EMPTYEVENT ){
00059
00060
00061
00062 bool srp(false);
00063 uint32_t sr_ch = getDataField("SR_CHSTATUS");
00064 if( sr_ch!=CH_TIMEOUT && sr_ch != CH_DISABLED ){
00065
00066
00067 increment(1," (while trying to create a SR Block !)");
00068 wToEnd = numbBytes/4-wordCounter_-1;
00069
00070
00071 srpBlock_ = new DCCTBSRPBlock( this, parser_, dataP_, parser_->srpBlockSize(), wToEnd,wordCounter_);
00073
00074 increment((parser_->srpBlockSize())/4-1);
00075 if(getDataField("SR")){ srp=true; }
00076 }
00077
00079
00080
00081
00082 for(uint32_t i=1; i<=4;i++){
00083 uint32_t tcc_ch=0; uint32_t tccId=0;
00084 if( i == 1){ tccId = parser_->tcc1Id();}
00085 if( i == 2){ tccId = parser_->tcc2Id();}
00086 if( i == 3){ tccId = parser_->tcc3Id();}
00087 if( i == 4){ tccId = parser_->tcc4Id();}
00088
00089 std::string tcc = std::string("TCC_CHSTATUS#") + parser_->getDecString(i);
00090 tcc_ch = getDataField(tcc);
00091
00092 if( tcc_ch != CH_TIMEOUT && tcc_ch != CH_DISABLED){
00093
00094
00095
00096
00097 increment(1," (while trying to create a"+tcc+" Block !)");
00098
00099 wToEnd = numbBytes/4-wordCounter_-1;
00100
00101
00102
00103
00104
00105 tccBlocks_.push_back( new DCCTBTCCBlock( this, parser_, dataP_,parser_->tccBlockSize(), wToEnd,wordCounter_, tccId));
00107
00108 increment((parser_->tccBlockSize())/4-1);
00109 }
00110 }
00112
00113
00114
00115
00116
00117
00118 uint32_t triggerType = getDataField("TRIGGER TYPE");
00119 uint32_t numbChannels;
00120 if( triggerType == PHYSICTRIGGER ) { numbChannels = 68; }
00121 else if (triggerType == CALIBRATIONTRIGGER ){ numbChannels = 70; }
00122
00123 else{
00124 std::string error = std::string("\n DCC::HEADER TRIGGER TYPE = ")+parser_->getDecString(triggerType)+std::string(" is not a valid type !");
00125 ECALTBParserBlockException a(error);
00126 throw a;
00127 }
00129
00130
00131
00132
00133 uint32_t srFlag;
00134 bool suppress(false);
00135
00136 for( uint32_t i=1; i<=numbChannels; i++){
00137
00138 std::string chStatusId = std::string("FE_CHSTATUS#") + parser_->getDecString(i);
00139 uint32_t chStatus = getDataField(chStatusId);
00140
00141
00142 if(srp){
00143 srFlag = srpBlock_->getDataField( std::string("SR#") + parser_->getDecString(i));
00144 if(srFlag == SR_NREAD){ suppress = true; }
00145 else{ suppress = false; }
00146 }
00148
00149
00150 if( chStatus != CH_TIMEOUT && chStatus != CH_DISABLED && !suppress && chStatus !=CH_SUPPRESS){
00151
00152
00153
00154 increment(1," (while trying to create a TOWERHEADER Block for channel "+parser_->getDecString(i)+" !)" );
00156
00157
00158
00159 wToEnd = numbBytes/4-wordCounter_-1;
00160 DCCTBTowerBlock * towerBlock = new DCCTBTowerBlock(this,parser_,dataP_,TOWERHEADER_SIZE,wToEnd,wordCounter_,i);
00161 towerBlocks_.push_back (towerBlock);
00162 towerBlock->parseXtalData();
00164
00165
00166
00167 increment((towerBlock->getDataField("BLOCK LENGTH"))*2 - 1);
00169
00170 }
00171 }
00173
00174
00175
00176 increment(1," (while trying to create a DCC TRAILER Block !)");
00177 wToEnd = numbBytes/4-wordCounter_-1;
00178 dccTrailerBlock_ = new DCCTBTrailerBlock(parser_,dataP_,TRAILER_SIZE,wToEnd,wordCounter_,blockSize_/8,0);
00180
00181 }
00182
00183 }catch( ECALTBParserException & e){}
00184 catch( ECALTBParserBlockException & e){
00185
00186 errorString_ += std::string(e.what());
00187 blockError_=true;
00188
00189 }
00190
00191
00192
00193 }
00194
00195
00196
00197 DCCTBEventBlock::~DCCTBEventBlock(){
00198
00199 std::vector<DCCTBTCCBlock *>::iterator it1;
00200 for(it1=tccBlocks_.begin();it1!=tccBlocks_.end();it1++){ delete (*it1);}
00201 tccBlocks_.clear();
00202
00203 std::vector<DCCTBTowerBlock *>::iterator it2;
00204 for(it2=towerBlocks_.begin();it2!=towerBlocks_.end();it2++){ delete (*it2);}
00205 towerBlocks_.clear();
00206
00207 if(srpBlock_ != 0 ) { delete srpBlock_; }
00208 if(dccTrailerBlock_ != 0 ) { delete dccTrailerBlock_;}
00209
00210 }
00211
00212
00213
00214 void DCCTBEventBlock::dataCheck(){
00215
00216
00217 std::string checkErrors("");
00218
00219
00220
00221 std::pair<bool,std::string> res = checkDataField("BOE",BOE);
00222 if(!res.first){ checkErrors += res.second; (errors_["DCC::HEADER"])++; }
00224
00225
00226
00227 std::string hField= std::string("H");
00228 res = checkDataField(hField,1);
00229 if(!res.first){ checkErrors += res.second; (errors_["DCC::HEADER"])++; }
00231
00232
00233
00234 uint32_t dccHeaderWords = 0;
00235
00236 if(emptyEvent){ dccHeaderWords = 2;}
00237 else if(!emptyEvent){ dccHeaderWords = 7;}
00238
00239 for(uint32_t i = 1; i<=dccHeaderWords ; i++){
00240
00241 std::string header= std::string("H") + parser_->getDecString(i);
00242 res = checkDataField(header,i);
00243 if(!res.first){ checkErrors += res.second; (errors_["DCC::HEADER"])++; }
00244 }
00246
00247
00248
00249 res = checkDataField("EVENT LENGTH",blockSize_/8);
00250 if(!res.first){ checkErrors += res.second; (errors_["DCC::EVENT LENGTH"])++; }
00252
00253
00254 if(checkErrors!=""){
00255 errorString_ +="\n ======================================================================\n";
00256 errorString_ += std::string(" ") + name_ + std::string(" data fields checks errors : ") ;
00257 errorString_ += checkErrors ;
00258 errorString_ += "\n ======================================================================";
00259 blockError_ = true;
00260 }
00261
00262
00263 }
00264
00265
00266
00267 void DCCTBEventBlock::displayEvent(std::ostream & os){
00268
00269 os << "\n\n\n\n\n >>>>>>>>>>>>>>>>>>>> Event started at word position " << std::dec << wordBufferOffset_ <<" <<<<<<<<<<<<<<<<<<<<"<<std::endl;
00270
00271
00272 displayData(os);
00274
00275
00276
00277 if(srpBlock_){ srpBlock_->displayData(os);}
00279
00280
00281
00282 std::vector<DCCTBTCCBlock *>::iterator it1;
00283 for( it1 = tccBlocks_.begin(); it1!= tccBlocks_.end(); it1++){
00284 (*it1)->displayData(os);
00285 }
00286
00287
00288 std::vector<DCCTBTowerBlock *>::iterator it2;
00289 for(it2 = towerBlocks_.begin();it2!=towerBlocks_.end();it2++){
00290
00291 (*it2)->displayData(os);
00292
00293
00294 std::vector<DCCTBXtalBlock * > &xtalBlocks = (*it2)->xtalBlocks();
00295 std::vector<DCCTBXtalBlock * >::iterator it3;
00296 for(it3 = xtalBlocks.begin();it3!=xtalBlocks.end();it3++){
00297 (*it3)->displayData(os);
00298 }
00299 }
00300
00301
00302 if(dccTrailerBlock_){ dccTrailerBlock_->displayData(os);}
00303
00304 }
00305
00306
00307 std::pair<bool,std::string> DCCTBEventBlock::compare(DCCTBEventBlock * block){
00308
00309
00310 std::pair<bool,std::string> ret(DCCTBBlockPrototype::compare(block));
00312
00313 std::stringstream out;
00314
00315
00316 if( srpBlock_ && block->srpBlock() ){
00317 std::pair<bool,std::string> temp( srpBlock_->compare(block->srpBlock()));
00318 ret.first = ret.first & temp.first;
00319 out<<temp.second;
00320 }else if( !srpBlock_ && block->srpBlock() ){
00321 ret.first = false;
00322 out<<"\n ====================================================================="
00323 <<"\n ERROR SR block identified in the ORIGINAL BLOCK ... "
00324 <<"\n ... but the block is not present in the COMPARISION BLOCK !"
00325 <<"\n =====================================================================";
00326 }else if( srpBlock_ && !(block->srpBlock()) ){
00327 ret.first = false;
00328 out<<"\n ====================================================================="
00329 <<"\n ERROR SR block identified in the COMPARISION BLOCK ... "
00330 <<"\n ... but the block is not present in the ORIGINAL BLOCK !"
00331 <<"\n =====================================================================";
00332 }
00334
00335
00336
00337
00338
00339 int numbTccBlocks_a = tccBlocks_.size();
00340 int numbTccBlocks_b = block->tccBlocks().size();
00341
00342 if( numbTccBlocks_a != numbTccBlocks_b ){
00343 ret.first = false;
00344 out<<"\n ====================================================================="
00345 <<"\n ERROR number of TCC blocks in the ORIGINAL BLOCK( ="<<numbTccBlocks_a<<" )"
00346 <<"\n and in the COMPARISION BLOCK( = "<<numbTccBlocks_b<<" is different !"
00347 <<"\n =====================================================================";
00348 }
00349
00350 std::vector<DCCTBTCCBlock *>::iterator it1Tcc = tccBlocks_.begin();
00351 std::vector<DCCTBTCCBlock *>::iterator it1TccEnd = tccBlocks_.end();
00352 std::vector<DCCTBTCCBlock *>::iterator it2Tcc = block->tccBlocks().begin();
00353 std::vector<DCCTBTCCBlock *>::iterator it2TccEnd = block->tccBlocks().end();
00354
00355 for( ; it1Tcc!=it1TccEnd && it2Tcc!=it2TccEnd; it1Tcc++, it2Tcc++){
00356 std::pair<bool,std::string> temp( (*it1Tcc)->compare( *it2Tcc ) );
00357 ret.first = ret.first & temp.first;
00358 out<<temp.second;
00359 }
00361
00362
00363
00364
00365 int numbTowerBlocks_a = towerBlocks_.size();
00366 int numbTowerBlocks_b = block->towerBlocks().size();
00367
00368 if( numbTowerBlocks_a != numbTowerBlocks_b ){
00369 ret.first = false;
00370 out<<"\n ====================================================================="
00371 <<"\n ERROR number of Tower blocks in the ORIGINAL BLOCK( ="<<numbTowerBlocks_a<<" )"
00372 <<"\n and in the COMPARISION BLOCK( = "<<numbTowerBlocks_b<<" is different !"
00373 <<"\n =====================================================================";
00374 }
00375
00376 std::vector<DCCTBTowerBlock *>::iterator it1Tower = towerBlocks_.begin();
00377 std::vector<DCCTBTowerBlock *>::iterator it1TowerEnd = towerBlocks_.end();
00378 std::vector<DCCTBTowerBlock *>::iterator it2Tower = (block->towerBlocks()).begin();
00379 std::vector<DCCTBTowerBlock *>::iterator it2TowerEnd = (block->towerBlocks()).end();
00380
00381 for( ; it1Tower!=it1TowerEnd && it2Tower!=it2TowerEnd; it1Tower++, it2Tower++){
00382
00383 std::pair<bool,std::string> temp( (*it1Tower)->compare( *it2Tower ) );
00384 ret.first = ret.first & temp.first;
00385 out<<temp.second;
00386
00387
00388 std::vector<DCCTBXtalBlock *> xtalBlocks1( (*it1Tower)->xtalBlocks());
00389 std::vector<DCCTBXtalBlock *> xtalBlocks2( (*it2Tower)->xtalBlocks());
00390
00391 int numbXtalBlocks_a = xtalBlocks1.size();
00392 int numbXtalBlocks_b = xtalBlocks2.size();
00393
00394 if( numbXtalBlocks_a != numbXtalBlocks_b ){
00395 ret.first = false;
00396 out<<"\n ====================================================================="
00397 <<"\n ERROR number of Xtal blocks in this TOWER ORIGINAL BLOCK( ="<<numbXtalBlocks_a<<" )"
00398 <<"\n and in the TOWER COMPARISION BLOCK( = "<<numbXtalBlocks_b<<" is different !"
00399 <<"\n =====================================================================";
00400 }
00401
00402 std::vector<DCCTBXtalBlock *>::iterator it1Xtal = xtalBlocks1.begin();
00403 std::vector<DCCTBXtalBlock *>::iterator it1XtalEnd = xtalBlocks1.end();
00404 std::vector<DCCTBXtalBlock *>::iterator it2Xtal = xtalBlocks1.begin();
00405 std::vector<DCCTBXtalBlock *>::iterator it2XtalEnd = xtalBlocks2.end();
00406
00407 for( ; it1Xtal!=it1XtalEnd && it2Xtal!=it2XtalEnd; it1Xtal++, it2Xtal++){
00408 std::pair<bool,std::string> temp( (*it1Xtal)->compare( *it2Xtal ) );
00409 ret.first = ret.first & temp.first;
00410 out<<temp.second;
00411 }
00412
00413 }
00414
00415
00416
00417 if( block->trailerBlock() && trailerBlock() ){
00418 std::pair<bool,std::string> temp( trailerBlock()->compare(block->trailerBlock()));
00419 ret.first = ret.first & temp.first;
00420 out<<temp.second;
00421 }
00422
00423 ret.second += out.str();
00424
00425 return ret;
00426 }
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436 bool DCCTBEventBlock::eventHasErrors(){
00437
00438 bool ret(false);
00439 ret = blockError() ;
00440
00441
00442
00443 std::vector<DCCTBTCCBlock *>::iterator it1;
00444 for( it1 = tccBlocks_.begin(); it1!= tccBlocks_.end(); it1++){
00445 ret |= (*it1)->blockError();
00446 }
00448
00449
00450 if(srpBlock_){ ret |= srpBlock_->blockError(); }
00452
00453
00454
00455 std::vector<DCCTBTowerBlock *>::iterator it2;
00456 for(it2 = towerBlocks_.begin();it2!=towerBlocks_.end();it2++){
00457
00458 ret |= (*it2)->blockError();
00459
00460
00461 std::vector<DCCTBXtalBlock * > & xtalBlocks = (*it2)->xtalBlocks();
00462 std::vector<DCCTBXtalBlock * >::iterator it3;
00463 for(it3 = xtalBlocks.begin();it3!=xtalBlocks.end();it3++){
00464 ret |= (*it3)->blockError();
00465 }
00467 }
00469
00470
00471
00472 if(dccTrailerBlock_){ ret |= dccTrailerBlock_->blockError();}
00474
00475
00476 return ret;
00477
00478 }
00479
00480
00481 std::string DCCTBEventBlock::eventErrorString(){
00482
00483 std::string ret("");
00484
00485 if( eventHasErrors() ){
00486
00487
00488 ret +="\n ======================================================================\n";
00489 ret += std::string(" Event Erros occurred for L1A ( decoded value ) = ") ;
00490 ret += parser_->getDecString(getDataField("LV1"));
00491 ret += "\n ======================================================================";
00492
00493
00494 ret += errorString();
00495
00496
00497
00498 std::vector<DCCTBTCCBlock *>::iterator it1;
00499 for( it1 = tccBlocks_.begin(); it1!= tccBlocks_.end(); it1++){
00500 ret += (*it1)->errorString();
00501 }
00503
00504 if(srpBlock_){ ret += srpBlock_->errorString(); }
00506
00507
00508
00509 std::vector<DCCTBTowerBlock *>::iterator it2;
00510
00511 for(it2 = towerBlocks_.begin();it2!=towerBlocks_.end();it2++){
00512
00513 ret += (*it2)->errorString();
00514
00515
00516 std::vector<DCCTBXtalBlock * > & xtalBlocks = (*it2)->xtalBlocks();
00517 std::vector<DCCTBXtalBlock * >::iterator it3;
00518
00519
00520 std::string temp;
00521 for(it3 = xtalBlocks.begin();it3!=xtalBlocks.end();it3++){ temp += (*it3)->errorString();}
00522
00523 if(temp!=""){
00524 ret += "\n Fine grain data Errors found ...";
00525 ret += "\n( Tower ID = " + parser_->getDecString( (*it2)->getDataField("TT/SC ID"));
00526 ret += ", LV1 = " + parser_->getDecString( (*it2)->getDataField("LV1"));
00527 ret += ", BX = " + parser_->getDecString( (*it2)->getDataField("BX")) + " )";
00528 ret += temp;
00529 }
00531
00532 }
00533
00534
00535
00536 if(dccTrailerBlock_){ ret += dccTrailerBlock_->errorString();}
00538
00539 }
00540
00541 return ret;
00542
00543 }
00544
00545
00546
00547
00548 std::vector< DCCTBTowerBlock * > DCCTBEventBlock::towerBlocksById(uint32_t towerId){
00549 std::vector<DCCTBTowerBlock *> myVector;
00550 std::vector<DCCTBTowerBlock *>::iterator it;
00551
00552 for( it = towerBlocks_.begin(); it!= towerBlocks_.end(); it++ ){
00553 try{
00554
00555 std::pair<bool,std::string> idCheck = (*it)->checkDataField("TT/SC ID",towerId);
00556
00557 if(idCheck.first ){ myVector.push_back( (*it) ); }
00558 }catch (ECALTBParserBlockException &e){}
00559 }
00560
00561 return myVector;
00562 }