/* 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 200 or 20 times the pedestal width, whichever is gerater. */ #include #include #include using namespace std; extern "C"{ #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; } //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 = 500; int THRESHOLD = 300; int NADCS = 1; int TIMETEST = 0; int NEVENT = 0; int RAWDATA = 0; int DEBUG =0; int DEBUGGER = 0; 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; int DACValues[20][72]; float DACGain[20][72]; int BASELINE = 100; int EVCOUNTER = 100; float PedSum[20][72]; float PedSum2[20][72]; float PedSumCntr[20][72]; float PedestalSigma[20][72]; float PedestalValues[20][72]; int BaseLine[20][72]; int ReferenceBaseLine = 350; 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++){ DACval<length; int SLOTNUM = 0; int EVENT = -1; int CHANNEL = -1; int WSize = 0; int idx = 0; int pedcnt = 0; float 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 "<data[Indx]); if (DEBUG) OUTF<>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 = (float) ((data & 0x1FFF0000) >> 16); adc2 = (float) (data & 0x1FFF); //cout<20){ Ped[SLOTNUM][CHANNEL] /= 20.; PedSum[SLOTNUM][CHANNEL] += Ped[SLOTNUM][CHANNEL]; PedSum2[SLOTNUM][CHANNEL] += (Ped[SLOTNUM][CHANNEL]*Ped[SLOTNUM][CHANNEL]); PedSumCntr[SLOTNUM][CHANNEL] += 1.; } } } else { if (DEBUG) OUTF<<"Other stuff"< 10){ VMESlot +=2; } for (int k=0;k<72;k++){ //float mu = PedSum[n][k]/(float)EventCounter; //float r = PedSum[n][k]/((float)EventCounter-1.); //float sig = sqrt( PedSum2[n][k]/((float)EventCounter-1) - mu*r ); float mu=0,r=0,sig=0; if (PedSumCntr[n][k]>1){ mu = PedSum[n][k] / PedSumCntr[n][k]; r = PedSum[n][k] / ( PedSumCntr[n][k] - 1. ); sig = sqrt( PedSum2[n][k]/( PedSumCntr[n][k] - 1. ) - mu*r ); } if (PedSumCntr[n][k] >50){ PedestalSigma[n][k] = sig; PedestalValues[n][k] = mu; } OUTFPed<< std::setprecision(3) << std::fixed <ReferenceBaseLine){ BaseLine[n][k] = int(sig+0.5)*20; } DACValues[n][k] = 0xa000; int S = fa125Slot(n); fa125SetOffset(S,k,DACValues[n][k]); } else if (PedestalCounter == 2){ PedestalOLD[1][n][k] = int(mu+0.5); float dped = (PedestalOLD[0][n][k] - PedestalOLD[1][n][k]); if (dped!= 0.0){ DACGain[n][k] = (float)(0xb000 - 0xa000)/float(PedestalOLD[0][n][k] - PedestalOLD[1][n][k]); } else{ DACGain[n][k] = 1.; } if (DACGain[n][k]!=0.0){ int newval = (int)(((mu+0.5) - (float)BaseLine[n][k]) * DACGain[n][k]) ; //cout<