/* read adc125 data with random trigger determine the pedestal and average every 1000 events. Then change the DAC settings for each channel. Do this iteratively till reaching convergens to the setvalue baseline. The code starts out with the channels set to DAC value 0x9000. In the second step it set the DAC values to 0x8a00. This serves as basis for calibrating the DAC of each channel to the base line. the base line is to be either 100 or 20 times the pedestal width, whichever is gerater. */ #include #include #include using namespace std; extern "C"{ #include #include #include #include #include #include #include #include "math.h" #include "time.h" #include "sys/types.h" #include extern int nfa125; extern int fa125A32Base; extern int tiA32Base; DMA_MEM_ID vmeIN,vmeOUT; extern unsigned int *dma_dabufp; extern DMANODE *the_event; } //UINT32 F125DATA[40000]; #define FA125_ADDR (5<<19) #define TI_ADDR (21<<19) ofstream OUTF; ofstream OUTFPed; ofstream DACval; void myReadoutCode(); void setDACvalues(); void mkpedestals(int); char DETECTOR[128]; unsigned int START; int HOSTROC = 0; int NADCSAMPLES = 100; int LATENCY = 300; int THRESHOLD = 300; int NADCS = 1; int TIMETEST = 0; int NEVENT = 0; int RAWDATA = 0; int DEBUG =0; int DEBUGGER = 0; int NInRange = 0; int NConverged = 0; int MINMEAN = 1000; int MAXMEAN = 4000; int DETECTOR_FACTOR = 1; unsigned int timelaps = 0; unsigned int Pedestal[20][72]; unsigned int PedestalOLD[2][20][72]; int ADCSamples[20][72][200]; float Ped[20][72]; int EventCounter = 0; float DACGain[20][72]; int BASELINE = 20; int EVCOUNTER = 20; unsigned short int CurrentDACValue[20][72]; unsigned short int PreviousDACValue[20][72]; float PreviousMean[20][72]; float CurrentMean[20][72]; bool BaselineInRange[20][72]; bool BaselineConverged[20][72]; float PedMean[20][72]; float Q[20][72]; unsigned int NSamples[20][72]; float PedestalSigma[20][72]; float PedestalValues[20][72]; int BaseLine[20][72]; int ReferenceBaseLine = 100; int PedestalCounter = 0; int ERRORCounter = 0; int ERRORLOG = 10000; int main(int argc, char *argv[]) { int Nevents = 1000; int iFlag; int stat; char ofnam[128]; char ofnamped[128]; char ofnamdac[128]; char CrateName[128] = "One"; char ifnam[128]; char DataDir[128]; printf("\nJLAB f125ADC pedestal Tests\n"); printf("----------------------------\n"); int k=0; int SLOT=3; if (argc<2) { cout<10){ VMESlot +=2; } for (int j=0;j<72;j++){ float locMean = PreviousMean[k][j]; float locPedestalSigma = PedestalSigma[k][j]; int locDACValue = CurrentDACValue[k][j]; float locDACGain = DACGain[k][j]; if(locDACGain == 0){ printf("ERROR: DAC gain 0 (DAC=%i, mean=%f, sigma=%f, crate=%i, slot=%i, channel=%i) Setting sigma = 250\n", locDACValue, locMean, locPedestalSigma, HOSTROC, VMESlot, j); //locDACValue = 0; locPedestalSigma = 250.0; if (locMean = 0.0) locMean = 4095; } else if (locDACValue < 0){ printf("ERROR: DAC Value Negative (DAC=%i, mean=%f, sigma=%f, crate=%i, slot=%i, channel=%i) Setting sigma = 250\n", locDACValue, locMean, locPedestalSigma, HOSTROC, VMESlot, j); //locDACValue = 0; locPedestalSigma = 250.0; //if (locMean < 0.0) locMean = 0.0; } else if ((locPedestalSigma != locPedestalSigma) || locPedestalSigma == 0.0){ printf("ERROR: Sigma nan or Equals 0 (DAC=%i, mean=%f, sigma=%f, crate=%i, slot=%i, channel=%i) Setting sigma = 250\n", locDACValue, locMean, locPedestalSigma, HOSTROC, VMESlot, j); //locDACValue = 0; locPedestalSigma = 250.0; //if (locMean < 0.0) locMean = 0.0; } else if ((locMean > (ReferenceBaseLine + 100))|| (locMean < 20)){ printf("ERROR: locMean not within [20,%i] (DAC=%i, mean=%f, sigma=%f, crate=%i, slot=%i, channel=%i) Setting sigma = 250\n", ReferenceBaseLine + 100, locDACValue, locMean, locPedestalSigma, HOSTROC, VMESlot, j); locPedestalSigma = 250.0; } DACval<length; int SLOTNUM = 0; int EVENT = -1; int CHANNEL = -1; int WSize = 0; int idx = 0; int pedcnt = 0; int adc1, adc2; int DATAReady=0; int EventsInBlock=0; int TriggerTime_1=0; int TriggerTime_2=0; int TrueSize=0; if (DEBUGGER){ OUTF<<"EVENT "<(DataSize+40))){ //if ((eventLen<(NADCS*3866-40)) || (eventLen>(NADCS*3866+40))){ //if ((eventLen<7730) || (eventLen>7800)){ //if ((eventLen<950) || (eventLen>970)){ OUTF<<"EVENT "<>27) == 0x10) { // Block Header SLOTNUM = ((data& 0x7C00000)>>22); if (SLOTNUM<11){ SLOTNUM -=3; } else { SLOTNUM -= 5; } EventsInBlock = (data & 0x000FF); if (DEBUG) OUTF<<"Block Header"<>27) == 0x12) { // Event Header EVENT = data & 0x003FFFFF; if (DEBUG) OUTF<<"Event Header"<>27) == 0x13) { // Trigger Time TriggerTime_1 = (data & 0x00FFFFFF); data = (unsigned int)LSWAP(outEvent->data[Indx+1]); //OUTF<>27) == 0x14) { // Window Raw Data header CHANNEL = ((data & 0x7F00000)>>20); // flash is couning channels from 1 to 72 need 0 to 71 WSize = (data & 0xFFF); if (DEBUGGER){ if (DATAReady!=0){ OUTF<<"EVENT "<>27) == 0x1d){// End of event if (DEBUG) OUTF<<"End of EVENT"<>27) == 0x1f){ if (DEBUG) OUTF<<"Filler Word"<>27) == 0x11){ if (DEBUG) OUTF<<"Block Trailer"<>27) == 0x18){ if (DEBUG) OUTF<<"0x18 Unknown type"<>27) == 0x19){ if (DEBUG) OUTF<<"Streaming RAW DATA!! WHY???"<>27) == 0x16){ if (DEBUG) OUTF<<"Puls RAW DATA!! WHY???"<>27) == 0x15){ if (DEBUG) OUTF<<"Window SUM!! WHY???"<>27) == 0x1e){ if (DEBUG) OUTF<<"DATA NOT VALID!! WHY???"<>27) == 0x17){ if (DEBUG) OUTF<<"PULSE INTEGRAL!! WHY???"<>27) == 0x16){ if (DEBUG) OUTF<<"Puls RAW DATA!! WHY???"<>27) == 0x15){ if (DEBUG) OUTF<<"Window SUM!! WHY???"<>27) == 0x1a){ if (DEBUG) OUTF<<"UNKNOWN!! WHY???"<>27) == 0x1b){ if (DEBUG) OUTF<<"UNDEFINED TYPE!! WHY???"<>27) == 0x1c){ if (DEBUG) OUTF<<"SCALER HEADER!! WHY???"<0) { // Window Raw Data values if (DEBUG){ OUTF<<"The actual DATA!"<> 16; ADCSamples[SLOTNUM][CHANNEL][idx++] = (data & 0x1FFF); adc1 = ((data & 0x1FFF0000) >> 16); adc2 = (data & 0x1FFF); //cout< 10){ VMESlot +=2; } for (int k=0;k<72;k++){ float mu=0,r=0,sig=0; /* if (PedSumCntr[n][k]>1){ mu = ((double)PedSum[n][k]) / PedSumCntr[n][k]; sig = sqrt( (PedSumCntr[n][k] * (double)PedSum2[n][k] - (double)PedSum[n][k] * PedSum[n][k]) / (PedSumCntr[n][k]*(PedSumCntr[n][k]-1)) ); } */ //cout << Q[n][k] << "<=Q[n][k] "<< mu << " NSamples[n][k]=>"< 50)) { if (( mu > (ReferenceBaseLine + 100))|| (mu < 20)) cout << "Pedestal Converged far from target: dped " << dped << " ddac " << ddac << endl; BaselineConverged[n][k]=true; NConverged++; PreviousMean[n][k] = CurrentMean[n][k]; PreviousDACValue[n][k] = CurrentDACValue[n][k]; continue; } //cout << ddaq << "<=ddaq dped=>" << dped << endl; float dacGain; if (dped!= 0.0){ dacGain = ddac/dped; } else{ dacGain = 1.; } if(fabs(dacGain - DACGain[n][k]) > 10 && DACGain[n][k] != 0.0) dacGain = DACGain[n][k]; else if(ddac == 1000 / DETECTOR_FACTOR ){ DACGain[n][k] = dacGain; } if (dacGain!=0.0 && mu != 4095){ float newval = ((mu - (float)BaseLine[n][k]) * dacGain) ; //cout<