From b906823ac3ea5786ccf7e64196fa2312d78f7664 Mon Sep 17 00:00:00 2001 From: Philipp Klaus Date: Tue, 4 Nov 2014 20:13:02 +0100 Subject: [PATCH] unpacker for s-curve measurements: read the new file format --- s-curves/cpp_based-on-dabc-lib/unpacker.cxx | 426 ++++++-------------- 1 file changed, 124 insertions(+), 302 deletions(-) diff --git a/s-curves/cpp_based-on-dabc-lib/unpacker.cxx b/s-curves/cpp_based-on-dabc-lib/unpacker.cxx index e276aed..9a8928d 100644 --- a/s-curves/cpp_based-on-dabc-lib/unpacker.cxx +++ b/s-curves/cpp_based-on-dabc-lib/unpacker.cxx @@ -6,10 +6,12 @@ #include "TROOT.h" #include "TApplication.h" +#include + +using namespace std; + void readSubEvents(hadaq::RawEvent* evnt); signed analyzeData(hadaq::RawSubevent* sub, unsigned ix, unsigned datalen, unsigned source); -unsigned getSensor(unsigned sensorId); -void writeResults(); void save_tree(); #define DEBUG 0 @@ -45,20 +47,22 @@ int main(int argc, char** argv) { if (argc>1) filename = argv[1]; if (argc>2) picPath = argv[2]; - if (argc>3) mySystem = argv[3]; + if (argc>3) mySystem = argv[3]; if (argc>4) rootOutfile = argv[4]; //---------------------------------- TApplication theApp("Analysis", &argc, argv); - pixelprob = new Float_t[1152*576]; for(int i=0;i<1152*576;i++) {pixelprob[i]=0;} - rowcount = new UShort_t[576]; for(int i=0;i<576;i++) {rowcount[i]=0;} + pixelprob = new Float_t[1152*576]; + for(int i=0;i<1152*576;i++) {pixelprob[i]=0;} + rowcount = new UShort_t[576]; + for(int i=0;i<576;i++) {rowcount[i]=0;} - outFile = new TFile (rootOutfile,"RECREATE"); + outFile = new TFile(rootOutfile,"RECREATE"); scurveTree = new TTree("scurvesall", "scurvesall"); - scurveTree -> Branch("threshold", &tree_threshold, "threshold/s", 32000); - scurveTree -> Branch("pixelprob", pixelprob, "pixelprob[663552]/F", 32000); - scurveTree -> Branch("frames", rowcount, "frames[576]/s", 32000); + scurveTree->Branch("threshold", &tree_threshold, "threshold/s", 32000); + scurveTree->Branch("pixelprob", pixelprob, "pixelprob[663552]/F", 32000); + scurveTree->Branch("frames", rowcount, "frames[576]/s", 32000); //---------------------------------- @@ -69,11 +73,9 @@ int main(int argc, char** argv) { readSubEvents(evnt); #if DEBUG>=1 printf("------------------------\n"); -#endif +#endif } - - writeResults(); save_tree(); @@ -90,18 +92,6 @@ int main(int argc, char** argv) { } -unsigned getSensor(unsigned sensorId) { - for(unsigned i = 0; i < numSensors; i++) { - if (sensors[i] == sensorId) { - return i; - } - } - if (numSensors == MAX_SENSORS) { return -1;} - - sensors[numSensors] = sensorId; - return numSensors++; - } - void readSubEvents(hadaq::RawEvent* evnt) { hadaq::RawSubevent* sub = 0; @@ -113,300 +103,133 @@ void readSubEvents(hadaq::RawEvent* evnt) { unsigned datalen = (data >> 16) & 0xFFFF; unsigned source = (data >> 0) & 0xFFFF; #if DEBUG>=1 - printf("Source\t%04x\tLength\t%04x\n", + printf("Source\t0x%04x\tLength\t0x%04x\n", source, datalen); -#endif +#endif signed ret = analyzeData(sub, ix, datalen, source); - ix+=datalen; - if(ret == -32) {break;} - } + ix += datalen; + if(ret == -32) break; + } } } -signed analyzeData(hadaq::RawSubevent* sub, unsigned ix, unsigned datalen, unsigned source) { - if (source == 0x5555) return -32; - if (datalen == 0) return -33; - unsigned RocEnd = ix + datalen -1; - int v2 = 0; - - unsigned rocHead = sub->Data(ix++); - if(((rocHead>>24) & 0xFF) != 1 || (rocHead & 0xFF) != 1) { - ix--; //assume it's old format +struct C0_data { + // will usually occupy X bytes + // word 1 / Header + unsigned frame_length : 16; + unsigned sensor_id : 8; + // word 2 / Info + unsigned threshold : 8; + unsigned bank : 4; + unsigned row : 12; + unsigned run_number : 8; + // words 3 - 38 / Sensor Data + uint32_t data[36]; +}; + +signed analyze_C0(hadaq::RawSubevent* sub, unsigned ix, unsigned framelen, unsigned source) { + + C0_data sf; // scurve frame + + unsigned header_1 = sub->Data(ix++); + sf.frame_length = header_1 >> 16; + sf.sensor_id = header_1 & 0xFF; + unsigned header_2 = sub->Data(ix++); + sf.threshold = header_2 >> 24; + sf.bank = (header_2 >> 20) & 0xF; + sf.row = (header_2 >> 8) & 0xFFF; + sf.run_number = header_2 & 0xFF; + + #if DEBUG>=1 + cout << "Frame length: " << sf.frame_length << endl; + cout << "Sensor ID: " << sf.sensor_id << endl; + cout << "Threshold: " << sf.threshold << endl; + cout << "Bank: " << sf.bank << endl; + cout << "Row: " << sf.row << endl; + cout << "Run Number: " << sf.run_number << endl; + #endif + + if (sf.frame_length != 0x25) { + printf("The framelength should match 37 = 0x25\n"); + return -1; } - else { - unsigned externalTimer = sub->Data(ix++); - unsigned externalInput = (externalTimer&0x80000000)?1:0; - if(externalInput) countMarkedFrames++; - externalTimer &= 0x7FFFFFFF; - } - while(1) { - unsigned sensorHead = sub->Data(ix++); - unsigned testmode = 0x0; - unsigned sensorId = 0x0; - unsigned sensorStatus = 0x0; - unsigned external = 0x0; - unsigned threshold = 0x0; - unsigned run = 0x0; - unsigned internal_fr_num = 0x0; - unsigned mySensor = 0x0; - unsigned sensorError = 0x0; - unsigned sensorDebug = 0x0; - - if (sub->Data(ix)>>16 == 0xf002){ - // Version 2 - testmode = sub->Data(ix) & 0xffff; - ix++; - sensorId = (source << 16)|(sub->Data(ix++)>>16); - sensorStatus = sub->Data(ix++); - ix += 2; // skip reserved words - external = sub->Data(ix++); - threshold = external >> 16; - run = external & 0xFF; - internal_fr_num = sub->Data(ix++); - sensorError = 0x0; - sensorDebug = 0x0; - v2 = 1; - } - else{ - sensorId = (source << 16)|(sub->Data(ix++)>>16); - sensorStatus = sub->Data(ix++); - sensorError = sub->Data(ix++); - sensorDebug = sub->Data(ix++); - ix += 2; //Skip time - v2 = 0; - } - - mySensor = getSensor(sensorId); - - #if DEBUG>=1 - printf("Head\t%08x\tID\t%08x\tStatus\t%08x\tError\t%08x\tDebug\t%08x\n", - sensorHead, sensorId, sensorStatus, sensorError, sensorDebug); - #endif - - if( (testmode != 0x01) && (sensorHead != 0xffffffff) ) { - //Something is really wrong with data. Skip SubEvent! - printf("Broken Sensor Header\n"); - statistics[mySensor][FRREALBROKEN]++; - #if DEBUG==0 - printf("Head\t%08x\tID\t%08x\tStatus\t%08x\tError\t%08x\tDebug\t%08x\n", - sensorHead, sensorId, sensorStatus, sensorError, sensorDebug); - #endif - return -1; - } - - //Check Status Word - unsigned sensorIsValid = 0; - if((sensorStatus & 0xf000000f) == 0xf000000f) { - sensorIsValid = 1; - statistics[mySensor][FRVALID]++; - } - else { - sensorIsValid = 0; - statistics[mySensor][FRBROKEN]++; - } - if(sensorIsValid){ - //Hey Sensor, tell me who you are! - unsigned sensorDummy = sub->Data(ix++); - unsigned sensorNumber = sub->Data(ix++); - unsigned sensorLength = sub->Data(ix++) & 0xffff; - - - unsigned frameEndPos = ix + sensorLength; - unsigned d,line = 0,column,pixels,statecnt = 0,ovf = 0; - #if DEBUG>=1 - printf("\t\t\tHeader\t%08x\tFrame\t%08x\tLength\t%i\n", - sensorDummy, sensorNumber, sensorLength); - #endif - if(sensorLength > 580) {printf("Invalid lenght. Something wrong."); statistics[mySensor][FRBROKEN]++; return -1;} - if(sensorLength == 0) { - goto FrameEnd; - } - - - while(1) { - for(unsigned i = 0; i<=1; i++) { - - if (v2==1){ - if(i==0) d = (sub->Data(ix) << 16) >> 16; - else d = sub->Data(ix++) >> 16; - } - else{ - if(i==0) d = sub->Data(ix) >> 16; - else d = sub->Data(ix++); - } - - if(statecnt-- == 0) { - ovf += (d >> 15) & 1; - line = (d >> 4) & 0x7FF; - statecnt = d & 0xF; - pixelcount[mySensor][1152]++; - } - else { - pixels = (d & 0x3); - column = (d >> 2) & 0x7FF; - #if DEBUG>1 - printf("\t%d, %d x %d\n",line,column,pixels+1); - #endif - pixelcount[mySensor][column] += pixels+1; - - pixelMap[mySensor][line][column]++; - if (pixels) { - pixelMap[mySensor][line][column+1]++; - if (pixels > 1) { - pixelMap[mySensor][line][column+2]++; - if (pixels > 2) { - pixelMap[mySensor][line][column+3]++; - } - } - } - } - if(ix >= frameEndPos) {goto FrameEnd;} - if(ix >= RocEnd) {break;} - } - } - FrameEnd: - //Read end of frame marker without check - ix += 1; - } - else if (testmode == 0x01) { - if (prev_threshold == 0xFFFF) prev_threshold = 0xFFFE; // prevent first save_tree() call - else if (threshold != prev_threshold) { - save_tree(); - prev_threshold = threshold; - } - unsigned d = 0; - unsigned pos = 0; - unsigned row = internal_fr_num - 1; - rowcount[row]++; - while(1) { - d = sub->Data(ix++); - //////loop over all bits, optimized for all-set and all-clear states - unsigned start = 1152 * row + pos*0x20; - if(d == 0) {} - else if(d == 0xFFFFFFFF) { - for(unsigned i = start; i < start + 0x20; i++) { - pixelprob[i] ++; - } - } - else { - for(unsigned i = start; i < start + 0x20; i++) { - pixelprob[i] += d & 0x1; - d >>= 1; - } - } - /////////////end add loop - - pos++; - if(ix >= RocEnd) break; + if (prev_threshold == 0xFFFF) prev_threshold = 0xFFFE; // prevent first save_tree() call + else if (sf.threshold != prev_threshold) { + save_tree(); + prev_threshold = sf.threshold; + } + rowcount[sf.row]++; + + unsigned j; + for (j = 0; ix + j < ix + 0x25; j++) { + unsigned d = sub->Data(ix+j); + sf.data[j] = d; + //////loop over all bits, optimized for all-set and all-clear states + unsigned start = 1152 * sf.row + j*0x20; + if(d == 0) {} + else if(d == 0xFFFFFFFF) { + for(unsigned i = start; i < start + 0x20; i++) { + pixelprob[i] ++; } } else { - return -2; + for(unsigned i = start; i < start + 0x20; i++) { + pixelprob[i] += d & 0x1; + d >>= 1; + } } - if(ix >= RocEnd){break;} } - return 0; + ix += j; + return ix; } - - - - -void writeResults() { - printf ("===============\n"); - printf ("== Summary ==\n"); - printf ("===============\n"); - printf ("Sensor\t\tStates\t\tGood\tBroken\tInvalid\tMarked\tBank0\t\tBank1\t\tBank2\t\tBank3\n"); - for(unsigned i = 0; i < numSensors; i++) { - if(statistics[i][FRVALID] == 0) {continue;} - double hitrates[4]; - unsigned pixcolcnt[MAX_SENSORS][4] = {{0}}; - unsigned hitcount[MAX_SENSORS] = {0}; - for(unsigned j = 0; j < 288; j++) { - pixcolcnt[i][0] += pixelcount[i][j]; - pixcolcnt[i][1] += pixelcount[i][j+288]; - pixcolcnt[i][2] += pixelcount[i][j+288+288]; - pixcolcnt[i][3] += pixelcount[i][j+288+288+288]; - } - hitcount[i] = pixcolcnt[i][0] + pixcolcnt[i][1] + pixcolcnt[i][2] + pixcolcnt[i][3]; - - printf("%08x\t%10i\t%i\t%i\t%i\t%i\t%10i\t%10i\t%10i\t%10i\n", - sensors[i], - hitcount[i], - statistics[i][FRVALID], - statistics[i][FRBROKEN], - statistics[i][FRREALBROKEN], - countMarkedFrames, - pixcolcnt[i][0], - pixcolcnt[i][1], - pixcolcnt[i][2], - pixcolcnt[i][3] - ); - //printf ("\t\t\t\t\t\tHR Bank0\tHR Bank1\tHR Bank2\tHR Bank3\n"); - hitrates[0] = (pixcolcnt[i][0]) /(288.*576*(statistics[i][FRVALID])); - hitrates[1] = (pixcolcnt[i][1]) /(288.*576*(statistics[i][FRVALID])); - hitrates[2] = (pixcolcnt[i][2]) /(288.*576*(statistics[i][FRVALID])); - hitrates[3] = (pixcolcnt[i][3]) /(288.*576*(statistics[i][FRVALID])); - printf("\t\t\t\t\t\t\t%e\t%e\t%e\t%e\n", - hitrates[0], - hitrates[1], - hitrates[2], - hitrates[3] - ); - - FILE* cvt; - int status; - cvt = popen("gnuplot", "w"); - if (!cvt) { - printf("couldn't open a pipe; quitting\n"); - exit(1); + + + +signed analyzeData(hadaq::RawSubevent* sub, unsigned ix, unsigned datalen, unsigned source) { + if (source == 0x5555) return -32; + if (datalen == 0) return -33; + unsigned RocEnd = ix + datalen -1; + + unsigned rocHead = sub->Data(ix++); + if (((rocHead >> 16) & 0xFF) != 0xC0 && ((rocHead >> 16) & 0xFF) != 0x00) { +#if DEBUG>=1 + printf("Cannot handle this data format: 0x%02x\n", (rocHead >> 16) & 0xFF); + return -1; +#endif + } +#if DEBUG>=1 + cout << "ROC header version: " << ((rocHead >> 24) & 0xFF) << endl; + cout << "Frame data version: " << ((rocHead >> 16) & 0xFF) << endl; + cout << "Header size: " << (rocHead & 0xFF) << endl; +#endif + + unsigned headerSize = rocHead & 0xFF; + + //unsigned externalTimer = sub->Data(ix++); + //unsigned externalInput = (externalTimer&0x80000000)?1:0; + //if(externalInput) countMarkedFrames++; + //externalTimer &= 0x7FFFFFFF; + + ix += headerSize; + + while(1) { + unsigned framelen = (sub->Data(ix) >> 16) & 0xFFFF; + + signed ret = analyze_C0(sub, ix, framelen, source); + if (ret < 0) { } - fprintf(cvt, "set terminal png size 800,400 font \",9\";\n"); - fprintf(cvt, "set palette model RGB;\n"); - fprintf(cvt, "set xrange [0:1152];\n"); - fprintf(cvt, "set yrange [0:576];\n"); - fprintf(cvt, "set cbrange [0:%i];\n",statistics[i][FRVALID]); - fprintf(cvt, "set palette defined ( 0 'white', 1 'red', 5 'black', 10 'blue', %i 'green');\n",statistics[i][FRVALID]); - - time_t rawtime; - struct tm * timeinfo; - time (&rawtime); - timeinfo = localtime (&rawtime); - char ts[200]; - strftime (ts,200,"set label 100 \"%H:%M:%S\" at screen 0.98,0.02 right tc rgb \"#000044\" font \"monospace,8\"\n",timeinfo); - fprintf(cvt,ts); - - fprintf(cvt, "set label 101 \"%i States, %i good, %i broken, %i invalid\" at screen 0.02,0.01 left tc rgb \"#000044\" font \"monospace,8\"\n", - hitcount[i], - statistics[i][FRVALID], - statistics[i][FRBROKEN], - statistics[i][FRREALBROKEN]); - - fprintf(cvt, "set label 102 \"hit rates: A: %.3e B: %.3e C: %.3e D:%.3e\" at screen 0.4,0.01 left tc rgb \"#000044\" font \"monospace,8\"\n", - hitrates[0], - hitrates[1], - hitrates[2], - hitrates[3]); - fprintf(cvt, "set output '%s/%s_%08x.png';\n",picPath,mySystem,sensors[i]); - printf("\nset output '%s/%s_%08x.png';\n",picPath,mySystem,sensors[i]); - fprintf(cvt, "plot '-' matrix with image\n"); - - for(unsigned r = 0; r < 576; r++) { - for(unsigned c = 0; c < 1152; c++) { - fprintf(cvt,"%d ",pixelMap[i][r][c]); - } - fprintf(cvt,"\n"); + else if (ret > 0) { + ix = ret; } - - - fprintf(cvt, "e\n\n"); - fflush(cvt); - status=pclose(cvt); - if (!WIFEXITED(status)) printf("error on closing the pipe\n"); + + if(ix >= RocEnd) break; } - } + return 0; + } + - void save_tree() { +void save_tree() { if(firstout) { tree_threshold = prev_threshold; for(int i=0;i<1152*576;i++) { @@ -432,4 +255,3 @@ void writeResults() { // printf("Entries: %i \n", scurveTree->GetEntries()); } - -- 2.43.0