Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:10:28

0001 //  PROGRAM:    dduBinExaminer  v 1

0002 //  Authors:    V. Barashko, A. Korytov, K. Kotov, July 13, 2004

0003 //=============================================================================================

0004 //  This program checks DDU binary datafile for event format integrity.

0005 //  It reads a requested binary file and:

0006 //  1) searches for Control Words (Headers, Trailers or other Signatures)

0007 //  2) checks for their proper sequence

0008 //  3) counts words and checks for consistency with 

0009 //      a) the expected nominal count (CFEB samples, ALCT, TMB)

0010 //      b) the number of words counted by various boards (ALCT, TMB, DDU)

0011 //  4) prints out structure for each event (can be suppressed by redirecting it to /dev/null)

0012 //  5) prints out warnings for each event (can be suppressed by redirecting it to /dev/null)

0013 //  6) print out errors for each event

0014 //  6) prints out the finals statistics of errors and warnings for the whole file

0015 //  7) produces a few histograms (ad hoc feature)

0016 //==============================================================================================

0017 // To compile: g++ -DLOCAL_UNPACK -o dduBinExaminerTest dduBinExaminerTest.cpp -I../src -I../interface/ -I../../../
0018 
0019 #include <iostream>
0020 #include <fstream>
0021 #include <iomanip>
0022 #include <string>
0023 #include <map>
0024 #include <set>
0025 #include <vector>
0026 
0027 #include <getopt.h>
0028 #include "CSCDCCExaminer.cc"
0029 
0030 using namespace std;
0031 // == Prints four 16bits words in Hex
0032 void printb(unsigned short* buf){
0033     for (int i=0; i<4; i++)
0034         cout << " " << setw(4)<< setfill('0') << hex << buf[i];
0035     cout << dec << endl;
0036 }
0037 
0038 // == Main =======================================================================================
0039 
0040 int main(int argc, char **argv){
0041     CSCDCCExaminer examiner;
0042     examiner.modeDDU(true);
0043     //typedef int32_t ERRARR[examiner.nERRORS];
0044     //typedef int32_t WRNARR[examiner.nWARNINGS];
0045 
0046     int32_t cntERROR[examiner.nERRORS], cntWARNING[examiner.nWARNINGS], cntBadEvent=0, unknownChamber=0;
0047     map< int, map<int,int32_t> > cntChambERROR;
0048     map< int, map<int,int32_t> > cntChambWARNING;
0049     map< int, map<int,int32_t> > cntChambPAYLOAD;
0050     map< int, map<int,int32_t> > cntChambSTATUS;
0051 
0052     // set ERROR and WARNING counters and flags to zero
0053     bzero(cntERROR,   sizeof(cntERROR)  );
0054     bzero(cntWARNING, sizeof(cntWARNING));
0055 
0056     examiner.output1().show();
0057     examiner.crcALCT(true);
0058     examiner.crcTMB (true);
0059     examiner.crcCFEB(true);
0060 
0061 // For error's timelines
0062     const int tlbinsize=100;
0063     vector<int> *tlerror[examiner.nERRORS], *tlwarn[examiner.nWARNINGS];
0064     bzero(tlerror,sizeof(tlerror));
0065     for(int nerr=0; nerr<examiner.nERRORS;   nerr++) tlerror[nerr] = new vector<int>(1000000/tlbinsize);
0066     for(int nwrn=0; nwrn<examiner.nWARNINGS; nwrn++) tlwarn [nwrn] = new vector<int>(1000000/tlbinsize);
0067 
0068     string datafile="";
0069     string rootfile="dduBinExaminer.root";
0070 
0071 
0072     // == Process command line options

0073     if( argc<2 ) { printf("Nothing to be done\nType -h for help\n"); exit(0); } else { datafile = argv[1]; }
0074     ofstream log_stat;
0075     ofstream log_err [examiner.nERRORS], log_warn [examiner.nWARNINGS];
0076     set<int> errEvent[examiner.nERRORS], warnEvent[examiner.nWARNINGS], badEvent;
0077     static struct option options[] = {
0078         {"help"      ,0, 0, 'h'}, {"statistic" ,0, 0, 's'}, {"error"    ,1, 0, 'e'},
0079         {"warning"   ,1, 0, 'w'}, {"datafile"  ,1, 0, 'd'}, {"rootfile" ,1, 0, 'r'},
0080         {"noALCTCRC" ,0, 0, 'n'}, {"noCFEBCRC" ,0, 0, 'n'}, {"noTMBCRC" ,0, 0, 'n'},
0081         {0, 0, 0, 0}
0082     };
0083     while (1) {
0084         int index=0; char *tmp,*token; int errnum,warnnum; string tmpname;
0085         int c = getopt_long(argc, argv, "nhse:w:d:r:",options, &index);
0086         if (c == -1)    break;
0087         switch (c){
0088             case 'h':
0089                 printf("Usage:\n");
0090                 printf("-h         ,   --help              shows this message\n");
0091                 printf("-d INPUT   ,   --datafile=INPUT    raw DDU file\n");
0092                 printf("-r OUTPUT  ,   --rootfile=OUTPUT   output with histograms\n");
0093                 printf("-s [NAME]  ,   --statistic=[NAME]  log information about bad events into runErrStat.all or [NAME] if specified\n");
0094                 printf("-e E1,..,En,   --error=E1,..En     log information about particular errors into runErrStat.[NUMBER]\n");
0095                 printf("-w W1,..,Wn,   --warning=W1,..Wn   log information about particular warnings into runWarnStat.[NUMBER]\n");
0096                 printf("--noALCTCRC                        disable ALCT CRC code\n");
0097                 printf("--noCFEBCRC                        disable CFEB CRC code\n");
0098                 printf("--noTMBCRC                         disable TMB CRC code\n");
0099                 printf("\nFirst two non-key arguments are considered to be 'datafile' and 'rootfile'");
0100                 printf("\n");
0101                 exit(0);
0102             case 's':
0103                 tmpname = ( optarg && strlen(optarg) ? optarg : "runErrStat.all" ) ;
0104                 log_stat.open(tmpname.c_str());
0105                 break;
0106             case 'e':
0107                 token = tmp = strdup(optarg);
0108                 while(1){
0109                     if((token = strrchr(tmp,','))) *token++ = '\0'; else token = tmp;
0110                     if(!strlen(token)) continue;
0111                     errnum = atoi(token);
0112                     if( errnum<0 || errnum>=19 ){
0113                         printf("Wrong error number: %s  ( 0 <= sould_be <= 19 )\n",token);
0114                         exit(0);
0115                     }
0116                     tmpname = "runErrStat.";
0117                     tmpname.append(token);
0118                     log_err[errnum].open(tmpname.c_str());
0119                     if(token == tmp)break;
0120                 }
0121                 free(tmp);
0122                 break;
0123             case 'w':
0124                 token = tmp = strdup(optarg);
0125                 while(1){
0126                     if((token = strrchr(tmp,','))) *token++ = '\0'; else token = tmp;
0127                     if(!strlen(token)) continue;
0128                     warnnum = atoi(token);
0129                     if( warnnum<0 || warnnum>=5 ){
0130                         printf("Wrong warning number: %s  ( 0 <= sould_be <= 5 )\n",token);
0131                         exit(0);
0132                     }
0133                     tmpname = "runWarnStat.";
0134                     tmpname.append(token);
0135                     log_warn[warnnum].open(tmpname.c_str());
0136                     if(token == tmp)break;
0137                 }
0138                 free(tmp);
0139                 break;
0140             case 'd': datafile = optarg; break;
0141             case 'r': rootfile = optarg; break;
0142             case 'n': break;
0143             default : printf("Type -h for help\n"); return 0;
0144         }
0145     }
0146     for(int arg=1; arg<argc; arg++){
0147         if( !strcmp("--noALCTCRC",argv[arg]) )
0148             examiner.crcALCT(false);
0149         if( !strcmp("--noTMBCRC", argv[arg]) )
0150             examiner.crcTMB(false);
0151         if( !strcmp("--noCFEBCRC",argv[arg]) )
0152             examiner.crcCFEB(false);
0153     }
0154 
0155     // == Open input data file
0156     ifstream input(datafile.c_str());
0157     if (!input) { perror(datafile.c_str()); return -1; }
0158     cerr << datafile << " Opened" << endl;
0159 
0160     const int bufferSize = 116384;
0161     unsigned short buffer[bufferSize];
0162 
0163     unsigned long iteration=1;
0164 
0165 //------------------------------------------------------------------------------------------------

0166 
0167     // == Read from datafile 4 16bit words into buf till end-of-file found
0168     while(!input.eof()){
0169 
0170         input.read((char *)(buffer), bufferSize*sizeof(short));
0171 
0172         const unsigned short *buf = buffer;
0173 
0174         int32_t length = input.gcount()/sizeof(short);
0175 
0176         while( (length = examiner.check(buf,length)) >= 0 ){
0177             // increment statistics Errors and Warnings
0178             for(int err=0; err<examiner.nERRORS; err++){
0179                 set<int> chamb_err = examiner.chambersWithError(err);
0180                 set<int>::const_iterator chamber = chamb_err.begin();
0181                 while( chamber != chamb_err.end() ) cntChambERROR[*chamber++][err]++;
0182                 if( examiner.error(err) ){
0183                     cntERROR[err]++;
0184                     errEvent[err].insert(iteration);
0185                 }
0186                 badEvent.insert(iteration);
0187             }
0188 
0189             for(int wrn=0; wrn<examiner.nWARNINGS; wrn++){
0190                 set<int> chamb_wrn = examiner.chambersWithWarning(wrn);
0191                 set<int>::const_iterator chamber = chamb_wrn.begin();
0192                 while( chamber != chamb_wrn.end() ) cntChambWARNING[*chamber++][wrn]++;
0193                 if( examiner.warning(wrn) ){
0194                     cntWARNING[wrn]++;
0195                     warnEvent[wrn].insert(iteration);
0196                 }
0197             }
0198 
0199             // std::map<int,long> payloads = examiner.payloadDetailed();
0200             std::map<CSCIdType, ExaminerStatusType> payloads = examiner.payloadDetailed();
0201             for(std::map<CSCIdType, ExaminerStatusType>::const_iterator csc=payloads.begin(); csc!=payloads.end(); csc++)
0202                 for(int bit=0; bit<examiner.nPAYLOADS; bit++)
0203                     if( csc->second & (1<<bit) ) cntChambPAYLOAD[csc->first][bit]++;
0204 
0205             // std::map<int,long> statuses = examiner.statusDetailed();
0206             std::map<CSCIdType, ExaminerStatusType> statuses = examiner.statusDetailed();
0207             for(std::map<CSCIdType, ExaminerStatusType>::const_iterator csc=statuses.begin(); csc!=statuses.end(); csc++)
0208                 for(int bit=0; bit<examiner.nSTATUSES; bit++)
0209                     if( csc->second & (1<<bit) ) cntChambSTATUS[csc->first][bit]++;
0210 
0211             if( examiner.errors() ) cntBadEvent++;
0212             if( examiner.errorsForChamber(-2) ) unknownChamber++;
0213 
0214             if( iteration % tlbinsize == 0 )
0215                 for(int err=0; err<examiner.nERRORS; err++){
0216                     if( tlerror[err] && tlerror[err]->size() <= iteration/tlbinsize )
0217                         tlerror[err]->resize( tlerror[err]->size() + iteration/tlbinsize );
0218                     if( tlerror[err] ) (*tlerror[err])[iteration/tlbinsize] = cntERROR[err];
0219                 }
0220             if( iteration % tlbinsize == 0 )
0221                 for(int wrn=0; wrn<examiner.nWARNINGS; wrn++){
0222                     if( tlwarn[wrn] && tlwarn[wrn]->size() <= iteration/tlbinsize )
0223                         tlwarn[wrn]->resize( tlwarn[wrn]->size() + iteration/tlbinsize );
0224                     if( tlwarn[wrn] ) (*tlwarn [wrn])[iteration/tlbinsize] = cntWARNING[wrn];
0225                 }
0226 
0227             // == Print some intermediate stats after so many trailers read
0228             if( (iteration%5000)==0 ){
0229                 cerr << endl << "DDU Headers: " << examiner.cntDDU_Headers << endl;
0230                 //cerr << "Trailers:    " << cntDDU_Trailers << endl;
0231                 cerr << "Bad Events:  " << cntBadEvent
0232                     << "   or " << (cntBadEvent/examiner.cntDDU_Headers) << " x Number of DDU Headers" << endl;
0233 
0234                 for(int err=0; err<examiner.nERRORS; err++)
0235                     if( cntERROR[err] != 0 )
0236                         cerr << "ERROR " << err << "  " << examiner.errName(err) << "    " << cntERROR[err] << endl;
0237 
0238                 for(int wrn=0; wrn<examiner.nWARNINGS; wrn++)
0239                     if( cntWARNING[wrn] != 0 )
0240                         cerr << "WARNING " << wrn << "  " << examiner.wrnName(wrn) << "    " << cntWARNING[wrn] << endl;
0241             }
0242 
0243             if( examiner.error(18) ) cout<<"ERROR 18"<<endl;
0244             iteration++;
0245         }
0246 
0247     }
0248 
0249     // == Print out the final stats

0250     cerr << endl << endl << "  " << datafile << endl;
0251     cerr << endl << endl << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl;
0252     cerr << " DDU Headers:                  " << examiner.cntDDU_Headers << endl;
0253     cerr << " DDUTrailers:                  " << examiner.cntDDU_Trailers << endl;
0254     cerr << " Bad Events:                   " << cntBadEvent
0255          << "  or  " << (float(cntBadEvent)/examiner.cntDDU_Headers) << " x Number of DDU Headers" << endl;
0256 
0257     const char *descr;
0258 
0259     cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING ERRORS: " << endl;
0260     for(int err=0; err<examiner.nERRORS; err++)
0261         if( (descr=examiner.errName(err)) && strlen(descr) )
0262             cerr << "  ERROR " << err << "  " << descr << "    " << cntERROR[err] << endl;
0263 
0264     cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING WARNINGS: " << endl;
0265     for(int wrn=0; wrn<examiner.nWARNINGS; wrn++)
0266         if( (descr=examiner.wrnName(wrn)) && strlen(descr) )
0267             cerr << "  WARNING " << wrn << "  " << descr << "    " << cntWARNING[wrn] << endl;
0268 
0269     cerr << "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$" << endl << endl;
0270     // == Close input data file
0271     input.close();
0272     cerr << datafile << " Closed" << endl;
0273 
0274     //hash_map< int, hash_map<int,int32_t> >::iterator chamber = cntChambERROR.begin();
0275     //while( chamber != cntChambERROR.end() ){
0276     std::map<CSCIdType, ExaminerStatusType>::const_iterator chamber = examiner.cntCHAMB_Headers.begin();
0277     while( chamber != examiner.cntCHAMB_Headers.end() ){
0278         cerr << endl << endl << endl;
0279         cerr << "-------------------------------------------------------------------------" << endl << endl;
0280         cerr << " Chamber: "<< chamber->first << " ( crate: " << (chamber->first>>4) << " slot: " << (chamber->first&0x0F) << " )" << endl;
0281         cerr << chamber->first << "DMB Headers:                  " << examiner.cntCHAMB_Headers[chamber->first] << endl;
0282         cerr << chamber->first << "DMBTrailers:                  " << examiner.cntCHAMB_Trailers[chamber->first] << endl;
0283         //cerr << " Bad Events:                   " << cntBadEvent
0284         //   << "  or  " << (float(cntBadEvent)/examiner.cntDDU_Headers) << " x Number of DDU Headers" << endl;
0285 
0286         const char *descr;
0287 
0288         cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING ERRORS: " << endl;
0289         if( (descr=examiner.errName(0)) && strlen(descr) )
0290             cerr << chamber->first << " ERROR " << 0 << "  " << descr << "    " << cntChambERROR[chamber->first][0] << endl;
0291         for(int err=5; err<24; err++){
0292             if( err==19 ) continue;
0293             if( (descr=examiner.errName(err)) && strlen(descr) )
0294                 cerr << chamber->first << " ERROR " << err << "  " << descr << "    " << cntChambERROR[chamber->first][err] << endl;
0295         }
0296 //      cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING WARNINGS: " << endl;
0297 //      for(int wrn=1; wrn<examiner.nWARNINGS; wrn++)
0298 //          if( (descr=examiner.wrnName(wrn)) && strlen(descr) )
0299 //              cerr << chamber->first << " WARNING " << wrn << "  " << descr << "    " << cntChambWARNING[chamber->first][wrn] << endl;
0300 
0301         cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING PAYLOADS: " << endl;
0302         for(int pl=0; pl<examiner.nPAYLOADS; pl++){
0303             if( (descr=examiner.payloadName(pl)) && strlen(descr) )
0304                 cerr << chamber->first << " PAYLOAD " << pl << "  " << std::setw(50) << std::left << descr << cntChambPAYLOAD[chamber->first][pl] << endl;
0305         }
0306 
0307         cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING STATUSES: " << endl;
0308         for(int st=0; st<examiner.nSTATUSES; st++){
0309             if( (descr=examiner.statusName(st)) && strlen(descr) )
0310                 cerr << chamber->first << " STATUS  " << st << "  " << std::setw(50) << std::left << descr << cntChambSTATUS[chamber->first][st] << endl;
0311         }
0312 
0313         chamber++;
0314     }
0315 
0316         cerr << endl << endl << endl;
0317         cerr << "-------------------------------------------------------------------------" << endl << endl;
0318         cerr << " Chamber: -1" << endl;
0319         cerr << "-1DMB Headers:                  " << unknownChamber << endl;
0320         cerr << "-1DMBTrailers:                  " << unknownChamber << endl;
0321 
0322         //const char *descr;
0323 
0324         cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING ERRORS: " << endl;
0325         if( (descr=examiner.errName(0)) && strlen(descr) )
0326             cerr << "-1 ERROR " << 0 << "  " << descr << "    " << cntChambERROR[-2][0] << endl;
0327         for(int err=5; err<24; err++){
0328             if( err==19 ) continue;
0329             if( (descr=examiner.errName(err)) && strlen(descr) )
0330                 cerr << "-1 ERROR " << err << "  " << descr << "    " << cntChambERROR[-2][err] << endl;
0331         }
0332         cerr << endl << " NUMBER OF EVENTS WITH FOLLOWING WARNINGS: " << endl;
0333         for(int wrn=1; wrn<examiner.nWARNINGS; wrn++)
0334             if( (descr=examiner.wrnName(wrn)) && strlen(descr) )
0335                 cerr << "-1 WARNING " << wrn << "  " << descr << "    " << cntChambWARNING[-2][wrn] << endl;
0336 
0337 
0338     cerr << "-------------------------------------------------------------------------" << endl << endl;
0339 
0340 
0341     ////////////////////////////// output files
0342     for(int err=0; err<examiner.nERRORS; err++){ // Errors
0343         if( log_err[err].is_open() ){
0344             set<int>::const_iterator iter = errEvent[err].begin();
0345             while( iter != errEvent[err].end() ){ log_err[err]<<(*iter)<<endl; iter++; }
0346         }
0347     }
0348     for(int wrn=0; wrn<examiner.nWARNINGS; wrn++){ // Warnings
0349         if( log_warn[wrn].is_open() ){
0350             set<int>::const_iterator iter = warnEvent[wrn].begin();
0351             while( iter != warnEvent[wrn].end() ){ log_warn[wrn]<<(*iter)<<endl; iter++; }
0352         }
0353     }
0354     if( log_stat.is_open() ){
0355         set<int>::const_iterator iter = badEvent.begin();
0356         while( iter != badEvent.end() ){
0357             log_stat<<(*iter)<<"    ";
0358             for(int err=0; err<examiner.nERRORS; err++){ // Errors
0359                 set<int>::const_iterator occurance = errEvent[err].find((*iter));
0360                 if( occurance != errEvent[err].end() ) log_stat<<"*"; else log_stat<<".";
0361             }
0362             log_stat<<endl;
0363             iter++;
0364         }
0365     }
0366 
0367 // == Exit from program
0368     return 0;
0369 }