#ifdef SLS_RECEIVER_FUNCTION_LIST /********************************************//** * @file slsReceiverFunctionList.cpp * @short does all the functions for a receiver, set/get parameters, start/stop etc. ***********************************************/ #include "slsReceiverFunctionList.h" #include "moenchCommonMode.h" #include // SIGINT #include // stat #include // socket(), bind(), listen(), accept(), shut down #include // sock_addr_in, htonl, INADDR_ANY #include // exit() #include //set precision #include //munmap #include #include using namespace std; slsReceiverFunctionList::slsReceiverFunctionList(detectorType det): myDetectorType(det), status(IDLE), udpSocket(NULL), server_port(DEFAULT_UDP_PORTNO), eth(NULL), maxPacketsPerFile(0), enableFileWrite(1), fileIndex(0), frameIndexNeeded(0), acqStarted(false), measurementStarted(false), startFrameIndex(0), frameIndex(0), packetsCaught(0), totalPacketsCaught(0), packetsInFile(0), startAcquisitionIndex(0), acquisitionIndex(0), packetsPerFrame(GOTTHARD_PACKETS_PER_FRAME), frameIndexMask(GOTTHARD_FRAME_INDEX_MASK), packetIndexMask(GOTTHARD_PACKET_INDEX_MASK), frameIndexOffset(GOTTHARD_FRAME_INDEX_OFFSET), acquisitionPeriod(SAMPLE_TIME_IN_NS), shortFrame(-1), currframenum(0), prevframenum(0), bufferSize(GOTTHARD_BUFFER_SIZE), latestData(NULL), guiDataReady(0), guiData(NULL), guiFileName(NULL), nFrameToGui(0), fifosize(GOTTHARD_FIFO_SIZE), numJobsPerThread(-1), mem0(NULL), dataCompression(false), fifo(NULL), fifoFree(NULL), buffer(NULL), numWriterThreads(1), thread_started(0), currentWriterThreadIndex(-1), totalListeningFrameCount(0), commonModeSubtractionEnable(false), sfilefd(NULL), writerthreads_mask(0x0), listening_thread_running(0), cbAction(DO_EVERYTHING), startAcquisitionCallBack(NULL), pStartAcquisition(NULL), acquisitionFinishedCallBack(NULL), pAcquisitionFinished(NULL), rawDataReadyCallBack(NULL), pRawDataReady(NULL){ maxPacketsPerFile = MAX_FRAMES_PER_FILE * packetsPerFrame; //moench variables if(myDetectorType == MOENCH){ fifosize = MOENCH_FIFO_SIZE; bufferSize = MOENCH_BUFFER_SIZE; packetsPerFrame = MOENCH_PACKETS_PER_FRAME; maxPacketsPerFile = MOENCH_MAX_FRAMES_PER_FILE * MOENCH_PACKETS_PER_FRAME; frameIndexMask = MOENCH_FRAME_INDEX_MASK; frameIndexOffset = MOENCH_FRAME_INDEX_OFFSET; packetIndexMask = MOENCH_PACKET_INDEX_MASK; } //variable initialization onePacketSize = bufferSize/packetsPerFrame; eth = new char[MAX_STR_LENGTH]; guiFileName = new char[MAX_STR_LENGTH]; latestData = new char[bufferSize]; strcpy(eth,""); strcpy(guiFileName,""); strcpy(latestData,""); strcpy(savefilename,""); strcpy(filePath,""); strcpy(fileName,"run"); for(int i=0;i /proc/sys/net/core/rmem_max")) cout << "\nWARNING: Could not change socket receiver buffer size in file /proc/sys/net/core/rmem_max" << endl; else if(system("echo 250000 > /proc/sys/net/core/netdev_max_backlog")) cout << "\nWARNING: Could not change max length of input queue in file /proc/sys/net/core/netdev_max_backlog" << endl; /** permanent setting heiner net.core.rmem_max = 104857600 # 100MiB net.core.netdev_max_backlog = 250000 sysctl -p // from the manual sysctl -w net.core.rmem_max=16777216 sysctl -w net.core.netdev_max_backlog=250000 */ /* if(createThreads() == FAIL){ cout << "ERROR: Could not create writer threads" << endl; exit (-1); } */ } slsReceiverFunctionList::~slsReceiverFunctionList(){ closeFile(-1); for(int i=0;i=0) fileIndex = i; return getFileIndex(); } int slsReceiverFunctionList::setEnableFileWrite(int i){ if(i!=-1) enableFileWrite=i; return enableFileWrite; } void slsReceiverFunctionList::resetTotalFramesCaught(){ acqStarted = false; startAcquisitionIndex = 0; totalPacketsCaught = 0; } int slsReceiverFunctionList::setShortFrame(int i){ shortFrame=i; if(shortFrame!=-1){ bufferSize = GOTTHARD_SHORT_BUFFER_SIZE; maxPacketsPerFile = SHORT_MAX_FRAMES_PER_FILE * GOTTHARD_SHORT_PACKETS_PER_FRAME; packetsPerFrame = GOTTHARD_SHORT_PACKETS_PER_FRAME; frameIndexMask = GOTTHARD_SHORT_FRAME_INDEX_MASK; frameIndexOffset = GOTTHARD_SHORT_FRAME_INDEX_OFFSET; }else{ bufferSize = GOTTHARD_BUFFER_SIZE; maxPacketsPerFile = MAX_FRAMES_PER_FILE * GOTTHARD_PACKETS_PER_FRAME; packetsPerFrame = GOTTHARD_PACKETS_PER_FRAME; frameIndexMask = GOTTHARD_FRAME_INDEX_MASK; frameIndexOffset = GOTTHARD_FRAME_INDEX_OFFSET; } onePacketSize = bufferSize/packetsPerFrame; /*setupFilter();*/ return shortFrame; } int slsReceiverFunctionList::setNFrameToGui(int i){ if(i>=0){ nFrameToGui = i; setupFifoStructure(); } return nFrameToGui; } int64_t slsReceiverFunctionList::setAcquisitionPeriod(int64_t index){ if(index >= 0){ if(index != acquisitionPeriod){ acquisitionPeriod = index; setupFifoStructure(); } } return acquisitionPeriod; } /******************* need to look at exit strategy **************************/ void slsReceiverFunctionList::enableDataCompression(bool enable){ dataCompression = enable; pthread_mutex_lock(&status_mutex); listening_thread_running = false; writerthreads_mask = 0x0; pthread_mutex_unlock(&(status_mutex)); /*createThreads(true);*/ if(enable) numWriterThreads = MAX_NUM_WRITER_THREADS; else numWriterThreads = 1; createThreads(); setupFilter(); } void slsReceiverFunctionList::setupFilter(){ for(int i=0;i(mdecoder[i], 3, 5, sign, cmSub); } } } } void slsReceiverFunctionList::readFrame(char* c,char** raw){ //point to gui data if (guiData == NULL) guiData = latestData; //copy data and filename strcpy(c,guiFileName); //could not get gui data if(!guiDataReady){ *raw = NULL; } //data ready, set guidata to receive new data else{ *raw = guiData; guiData = NULL; pthread_mutex_lock(&dataReadyMutex); guiDataReady = 0; pthread_mutex_unlock(&dataReadyMutex); if((nFrameToGui) && (writerthreads_mask)){ //release after getting data sem_post(&smp); } } } void slsReceiverFunctionList::copyFrameToGui(char* startbuf){ //random read when gui not ready if((!nFrameToGui) && (!guiData)){ pthread_mutex_lock(&dataReadyMutex); guiDataReady=0; pthread_mutex_unlock(&dataReadyMutex); } //random read or nth frame read, gui needs data now else{ //nth frame read, block current process if the guireader hasnt read it yet if(nFrameToGui) sem_wait(&smp); pthread_mutex_lock(&dataReadyMutex); guiDataReady=0; //send the first one memcpy(latestData,startbuf,bufferSize); strcpy(guiFileName,savefilename); guiDataReady=1; pthread_mutex_unlock(&dataReadyMutex); } } void slsReceiverFunctionList::setupFifoStructure(){ int64_t i; int oldn = numJobsPerThread; //if every nth frame mode if(nFrameToGui) numJobsPerThread = nFrameToGui; //random nth frame mode else{ if(!acquisitionPeriod) i = SAMPLE_TIME_IN_NS; else i = SAMPLE_TIME_IN_NS/acquisitionPeriod; if (i > MAX_JOBS_PER_THREAD) numJobsPerThread = MAX_JOBS_PER_THREAD; else if (i < 1) numJobsPerThread = 1; else numJobsPerThread = i; } //if same, return if(oldn == numJobsPerThread) return; //otherwise memory too much if numjobsperthread is at max = 1000 fifosize = GOTTHARD_FIFO_SIZE; if(myDetectorType == MOENCH) fifosize = MOENCH_FIFO_SIZE; if(fifosize % numJobsPerThread) fifosize = (fifosize/numJobsPerThread)+1; else fifosize = fifosize/numJobsPerThread; cout << "Number of Frames per buffer:" << numJobsPerThread << endl; cout << "Fifo Size:" << fifosize << endl; /* //for testing numJobsPerThread = 3; fifosize = 11; */ //deleting old structure and creating fifo structure if(fifoFree){ while(!fifoFree->isEmpty()) fifoFree->pop(buffer); delete fifoFree; } if(fifo) delete fifo; if(mem0) free(mem0); fifoFree = new CircularFifo(fifosize); fifo = new CircularFifo(fifosize); //allocate memory mem0=(char*)malloc((bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS)*fifosize); /** shud let the client know about this */ if (mem0==NULL) cout<<"++++++++++++++++++++++ COULD NOT ALLOCATE MEMORY FOR LISTENING !!!!!!!+++++++++++++++++++++" << endl; buffer=mem0; //push the addresses into freed fifoFree and writingFifoFree while (buffer<(mem0+(bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS)*(fifosize-1))) { fifoFree->push(buffer); buffer+=(bufferSize * numJobsPerThread + HEADER_SIZE_NUM_TOT_PACKETS); } cout << "Fifo structure reconstructed" << endl; } int slsReceiverFunctionList::createUDPSocket(){ if(udpSocket) udpSocket->ShutDownSocket(); //if eth is mistaken with ip address if (strchr(eth,'.')!=NULL) strcpy(eth,""); //if no eth, listen to all if(!strlen(eth)){ cout<<"warning:eth is empty.listening to all"<getErrorStatus(); if (iret){ //#ifdef VERBOSE cout << "Could not create UDP socket on port " << server_port << " error:" << iret << endl; //#endif return FAIL; } return OK; } void slsReceiverFunctionList::freeFifoBufferCallBack (char* fbuffer, void *this_pointer){ ((slsReceiverFunctionList*)this_pointer)->freeFifoBuffer(fbuffer); } void slsReceiverFunctionList::freeFifoBuffer(char* fbuffer){ fifoFree->push(fbuffer); } int slsReceiverFunctionList::createThreads(bool destroy){ int i; if(!destroy){ pthread_mutex_lock(&status_mutex); status = IDLE; listening_thread_running = 0; writerthreads_mask = 0x0; pthread_mutex_unlock(&(status_mutex)); //listening thread sem_init(&listensmp,0,0); if(pthread_create(&listening_thread, NULL,startListeningThread, (void*) this)){ cout << "Could not create listening thread" << endl; return FAIL; } //#ifdef VERBOSE cout << "Listening thread created successfully." << endl; //#endif //start writer threads cout << "Creating Writer Threads"; currentWriterThreadIndex = -1; for(i = 0; i < numWriterThreads; ++i){ sem_init(&writersmp[i],0,0); thread_started = 0; currentWriterThreadIndex = i; if(pthread_create(&writing_thread[i], NULL,startWritingThread, (void*) this)){ cout << "Could not create writer thread with index " << i << endl; return FAIL; } while(!thread_started); cout << "."; cout << flush; } //#ifdef VERBOSE cout << endl << "Writer threads created successfully." << endl; //#endif //assign priorities struct sched_param tcp_param, listen_param, write_param; int policy= SCHED_RR; bool rights = true; tcp_param.sched_priority = 50; listen_param.sched_priority = 99; write_param.sched_priority = 90; if (pthread_setschedparam(listening_thread, policy, &listen_param) == EPERM) rights = false; for(i = 0; i < numWriterThreads; ++i) if(rights) if (pthread_setschedparam(writing_thread[i], policy, &write_param) == EPERM){ rights = false; break; } if (pthread_setschedparam(pthread_self(),5 , &tcp_param) == EPERM) rights = false; if(!rights) cout << "WARNING: Could not prioritize threads. You need to be super user for that." << endl; } else{cout<<"DESTROYNG THREADS"<initEventTree(savefilename, &iframe); //resets the pedestalSubtraction array and the commonModeSubtraction singlePhotonDet[ithr]->newDataSet(); if(myFile[ithr]==NULL){ cout<<"file not null"<IsOpen()){ cout<<"file not open"< DO_NOTHING){ //close if(sfilefd){ fclose(sfilefd); sfilefd = NULL; } //open file if (NULL == (sfilefd = fopen((const char *) (savefilename), "w"))){ cout << "Error: Could not create file " << savefilename << endl; return FAIL; } //setting buffer setvbuf(sfilefd,NULL,_IOFBF,BUF_SIZE); //printing packet losses and file names if(!packetsCaught) cout << savefilename << endl; else{ cout << savefilename << "\tpacket loss " << setw(4)<Write(tall->GetName(),TObject::kOverwrite);*/ myFile[ithr] = myTree[ithr]->GetCurrentFile(); if(myFile[ithr]->Write()) cout << "Thread " << ithr <<" wrote frames to file" << endl; else cout << "Thread " << ithr << " could not write frames to file" << endl; }else cout << "Thread " << ithr << " could not write frames to file: No file or No Tree" << endl; /* packetsInFile = 0; }*/ //close file if(myTree[ithr] && myFile[ithr]) myFile[ithr] = myTree[ithr]->GetCurrentFile(); if(myFile[ithr]) myFile[ithr]->Close(); myFile[ithr] = NULL; myTree[ithr] = NULL; } #endif } } int slsReceiverFunctionList::startReceiver(char message[]){ //#ifdef VERBOSE cout << "Starting Receiver" << endl; //#endif //reset listening thread variables measurementStarted = false; startFrameIndex = 0; totalListeningFrameCount = 0; //udp socket if(createUDPSocket() == FAIL){ strcpy(message,"Could not create UDP Socket.\n"); cout << endl << message << endl; return FAIL; } cout << "UDP socket created successfully on port " << server_port << endl; if(setupWriter() == FAIL){ //stop udp socket if(udpSocket) udpSocket->ShutDownSocket(); sprintf(message,"Could not create file %s.\n",savefilename); return FAIL; } //done to give the gui some proper name instead of always the last file name if(dataCompression) sprintf(savefilename, "%s/%s_fxxx_%d_xx.root", filePath,fileName,fileIndex); //initialize semaphore sem_init(&smp,0,1); //status pthread_mutex_lock(&status_mutex); status = RUNNING; for(int i=0;iShutDownSocket(); } void* slsReceiverFunctionList::startListeningThread(void* this_pointer){ ((slsReceiverFunctionList*)this_pointer)->startListening(); return this_pointer; } void* slsReceiverFunctionList::startWritingThread(void* this_pointer){ ((slsReceiverFunctionList*)this_pointer)->startWriting(); return this_pointer; } int slsReceiverFunctionList::startListening(){ #ifdef VERYVERBOSE cout << "In startListening()" << endl; #endif int lastpacketoffset, expected, rc, packetcount, maxBufferSize, carryonBufferSize; int lastframeheader;// for moench to check for all the packets in last frame char* tempchar = NULL; while(1){ //variables that need to be checked/set before each acquisition carryonBufferSize = 0; maxBufferSize = packetsPerFrame * numJobsPerThread * onePacketSize; if(tempchar) {delete [] tempchar;tempchar = NULL;} tempchar = new char[onePacketSize * (packetsPerFrame - 1)]; //gotthard: 1packet size, moench:39 packet size while(listening_thread_running){ //pop fifoFree->pop(buffer); #ifdef VERYDEBUG cout << "*** popped from fifo free" << (void*)buffer << endl; #endif //receive if(!carryonBufferSize){ rc = udpSocket->ReceiveDataOnly(buffer + HEADER_SIZE_NUM_TOT_PACKETS, maxBufferSize); expected = maxBufferSize; }else{ #ifdef VERYDEBUG cout << "***carry on buffer" << carryonBufferSize << endl; cout<<"framennum in temochar:"<<((((uint32_t)(*((uint32_t*)tempchar))) & (frameIndexMask)) >> frameIndexOffset)<ReceiveDataOnly((buffer + HEADER_SIZE_NUM_TOT_PACKETS + carryonBufferSize),maxBufferSize - carryonBufferSize); expected = maxBufferSize - carryonBufferSize; } #ifdef VERYDEBUG cout << "*** rc:" << dec << rc << endl; cout << "*** expected:" << dec << expected << endl; #endif //start indices //start of scan if((!measurementStarted) && (rc > 0)){ //gotthard has +1 for frame number if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) startFrameIndex = (((((uint32_t)(*((uint32_t*)(buffer + HEADER_SIZE_NUM_TOT_PACKETS))))+1) & (frameIndexMask)) >> frameIndexOffset); else startFrameIndex = ((((uint32_t)(*((uint32_t*)(buffer+HEADER_SIZE_NUM_TOT_PACKETS)))) & (frameIndexMask)) >> frameIndexOffset); cout<<"startFrameIndex:"<push(buffer); exit(-1); continue; } //push the last buffer into fifo if(rc > 0){ packetcount = (rc/onePacketSize); #ifdef VERYDEBUG cout << "*** last packetcount:" << packetcount << endl; #endif (*((uint16_t*)(buffer))) = packetcount; totalListeningFrameCount += packetcount; while(!fifo->push(buffer)); #ifdef VERYDEBUG cout << "*** last lbuf1:" << (void*)buffer << endl; #endif } //push dummy buffer for(int i=0;ipop(buffer); (*((uint16_t*)(buffer))) = 0xFFFF; while(!fifo->push(buffer)); #ifdef VERYDEBUG cout << "pushed in dummy buffer:" << (void*)buffer << endl; #endif } cout << "Total count listened to " << totalListeningFrameCount/packetsPerFrame << endl; pthread_mutex_lock(&status_mutex); listening_thread_running = 0; pthread_mutex_unlock(&(status_mutex)); break; } //reset packetcount = packetsPerFrame * numJobsPerThread; carryonBufferSize = 0; //check if last packet valid and calculate packet count switch(myDetectorType){ case MOENCH: lastpacketoffset = (((numJobsPerThread * packetsPerFrame - 1) * onePacketSize) + HEADER_SIZE_NUM_TOT_PACKETS); #ifdef VERYDEBUG cout <<"first packet:"<< ((((uint32_t)(*((uint32_t*)(buffer+HEADER_SIZE_NUM_TOT_PACKETS))))) & (packetIndexMask)) << endl; cout <<"first header:"<< (((((uint32_t)(*((uint32_t*)(buffer+HEADER_SIZE_NUM_TOT_PACKETS))))) & (frameIndexMask)) >> frameIndexOffset) << endl; cout << "last packet offset:" << lastpacketoffset << endl; cout <<"last packet:"<< ((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))) & (packetIndexMask)) << endl; cout <<"last header:"<< (((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif //moench last packet value is 0 if( ((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))) & (packetIndexMask))){ lastframeheader = ((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset; carryonBufferSize += onePacketSize; lastpacketoffset -= onePacketSize; --packetcount; while (lastframeheader == (((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))) & (frameIndexMask)) >> frameIndexOffset)){ carryonBufferSize += onePacketSize; lastpacketoffset -= onePacketSize; --packetcount; } memcpy(tempchar, buffer+(lastpacketoffset+onePacketSize), carryonBufferSize); #ifdef VERYDEBUG cout << "tempchar header:" << (((((uint32_t)(*((uint32_t*)(tempchar))))) & (frameIndexMask)) >> frameIndexOffset) << endl; cout <<"tempchar packet:"<< ((((uint32_t)(*((uint32_t*)(tempchar))))) & (packetIndexMask)) << endl; #endif } break; default: if(shortFrame == -1){ lastpacketoffset = (((numJobsPerThread * packetsPerFrame - 1) * onePacketSize) + HEADER_SIZE_NUM_TOT_PACKETS); #ifdef VERYDEBUG cout << "last packet offset:" << lastpacketoffset << endl; #endif if((packetsPerFrame -1) != ((((uint32_t)(*((uint32_t*)(buffer+lastpacketoffset))))+1) & (packetIndexMask))){ memcpy(tempchar,buffer+lastpacketoffset, onePacketSize); #ifdef VERYDEBUG cout << "tempchar header:" << (((((uint32_t)(*((uint32_t*)(tempchar))))+1) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif carryonBufferSize = onePacketSize; --packetcount; } } #ifdef VERYDEBUG cout << "header:" << (((((uint32_t)(*((uint32_t*)(buffer + HEADER_SIZE_NUM_TOT_PACKETS))))+1) & (frameIndexMask)) >> frameIndexOffset) << endl; #endif break; } #ifdef VERYDEBUG cout << "*** packetcount:" << packetcount << " carryonbuffer:" << carryonBufferSize << endl; #endif //write packet count and push (*((uint16_t*)(buffer))) = packetcount; totalListeningFrameCount += packetcount; while(!fifo->push(buffer)); #ifdef VERYDEBUG cout << "*** pushed into listening fifo" << endl; #endif } sem_wait(&listensmp); } return OK; } int slsReceiverFunctionList::startWriting(){ int ithread = currentWriterThreadIndex; #ifdef VERYVERBOSE cout << ithread << "In startWriting()" <pop(wbuf); numpackets = (uint16_t)(*((uint16_t*)wbuf)); #ifdef VERYDEBUG cout << "numpackets:" << dec << numpackets << endl; cout << ithread << "*** popped from fifo " << numpackets << endl; #endif //last dummy packet if(numpackets == 0xFFFF){ #ifdef VERYDEBUG cout << "**********************popped last dummy frame:" << (void*)wbuf << " from thread " << ithread << endl; #endif //free fifo while(!fifoFree->push(wbuf)); #ifdef VERYDEBUG cout << "fifo freed:" << (void*)wbuf << " from thread " << ithread << endl; #endif //all threads need to close file, reset mask and exit loop pthread_mutex_lock(&status_mutex); closeFile(ithread); writerthreads_mask^=(1<> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(wbuf + HEADER_SIZE_NUM_TOT_PACKETS))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ pthread_mutex_lock(&progress_mutex); if(tempframenum > currframenum) currframenum = tempframenum; pthread_mutex_unlock(&progress_mutex); } #ifdef VERYDEBUG cout << ithread << " tempframenum:" << dec << tempframenum << " curframenum:" << currframenum << endl; #endif //without datacompression: write datacall back, or write data, free fifo if(!dataCompression){ if (cbAction < DO_EVERYTHING) rawDataReadyCallBack(currframenum, wbuf, numpackets * onePacketSize, sfilefd, guiData,pRawDataReady); else if (numpackets > 0){ if(numWriterThreads >1) pthread_mutex_lock(&progress_mutex); writeToFile_withoutCompression(wbuf, numpackets); if(numWriterThreads >1) pthread_mutex_unlock(&progress_mutex); } //copy to gui copyFrameToGui(wbuf + HEADER_SIZE_NUM_TOT_PACKETS); while(!fifoFree->push(wbuf)); #ifdef VERYVERBOSE cout<<"buf freed:"<<(void*)wbuf<findNextFrame(data,ndata,remainingsize )){/**need mutex??????????*/ np = ndata/onePacketSize; //cout<<"buff framnum:"<> frameIndexOffset)<newFrame(); if(commonModeSubtractionEnable){ for(ix = xmin - 1; ix < xmax + 1; ix++){ for(iy = ymin - 1; iy < ymax + 1; iy++){ thisEvent = singlePhotonDet[ithread]->getEventType(buff, ix, iy, 0); } } } for(ix = xmin - 1; ix < xmax + 1; ix++) for(iy = ymin - 1; iy < ymax + 1; iy++){ thisEvent=singlePhotonDet[ithread]->getEventType(buff, ix, iy, commonModeSubtractionEnable); if (nf>1000) { tot=0; tl=0; tr=0; bl=0; br=0; if (thisEvent==PHOTON_MAX) { iFrame=mdecoder[ithread]->getFrameNumber(buff);/**need mutex??????????*/ pthread_mutex_lock(&write_mutex); myTree[ithread]->Fill(); pthread_mutex_unlock(&write_mutex); //cout << "Fill in event: frmNr: " << iFrame << " ix " << ix << " iy " << iy << " type " << thisEvent << endl; } } } nf++; pthread_mutex_lock(&write_mutex); packetsInFile += packetsPerFrame; packetsCaught += packetsPerFrame; totalPacketsCaught += packetsPerFrame; pthread_mutex_unlock(&write_mutex); if(!once){ copyFrameToGui(buff); //cout<<"buff framnum:"<> frameIndexOffset)< (wbuf + HEADER_SIZE_NUM_TOT_PACKETS + numpackets * onePacketSize) ) cout <<" **************WE HAVE A PROBLEM!"<push(wbuf)); #ifdef VERYVERBOSE cout<<"buf freed:"<<(void*)wbuf< 0){ //for progress and packet loss calculation(new files) if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) tempframenum = (((((uint32_t)(*((uint32_t*)(buf + HEADER_SIZE_NUM_TOT_PACKETS))))+1)& (frameIndexMask)) >> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(buf + HEADER_SIZE_NUM_TOT_PACKETS))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ if(tempframenum > currframenum) currframenum = tempframenum; } #ifdef VERYDEBUG cout << "tempframenum:" << dec << tempframenum << " curframenum:" << currframenum << endl; #endif //to create new file when max reached packetsToSave = maxPacketsPerFile - packetsInFile; if(packetsToSave > numpackets) packetsToSave = numpackets; fwrite(buf+offset, 1, packetsToSave * onePacketSize, sfilefd); packetsInFile += packetsToSave; packetsCaught += packetsToSave; totalPacketsCaught += packetsToSave; //new file if(packetsInFile >= maxPacketsPerFile){ lastpacket = (((packetsToSave - 1) * onePacketSize) + offset); //for packet loss if ((myDetectorType == GOTTHARD) && (shortFrame == -1)) tempframenum = (((((uint32_t)(*((uint32_t*)(buf + lastpacket))))+1)& (frameIndexMask)) >> frameIndexOffset); else tempframenum = ((((uint32_t)(*((uint32_t*)(buf + lastpacket))))& (frameIndexMask)) >> frameIndexOffset); if(numWriterThreads == 1) currframenum = tempframenum; else{ if(tempframenum > currframenum) currframenum = tempframenum; } #ifdef VERYDEBUG cout << "tempframenum:" << dec << tempframenum << " curframenum:" << currframenum << endl; #endif createNewFile(); } offset += (packetsToSave * onePacketSize); numpackets -= packetsToSave; } } //no file write else{ packetsInFile += numpackets; packetsCaught += numpackets; totalPacketsCaught += numpackets; } } #endif