Changeset 10101 for fact/FADctrl


Ignore:
Timestamp:
01/13/11 11:56:40 (14 years ago)
Author:
ogrimm
Message:
Amplitude calibration information stored in file
Location:
fact/FADctrl
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10100 r10101  
    3232   {"address", &FAD::cmd_address, true, 2, "<range> <value>", "Set addresses in range to value"},
    3333   {"send", &FAD::cmd_send, true, 1, "<value>", "Set arbitrary data to board"},
    34    {"acalib", &FAD::cmd_acalib, true, 0, "[n]", "Amplitude calibration (n events)"},
     34   {"acalib", &FAD::cmd_acalib, true, 0, "[n|invalidate|file]", "Perform or read amplitude calibration (n events)"},
    3535   //{"wmode", &FAD::cmd_wmode, 0, "<run|stop>", "Domino wave running or stopped during read out"},
    3636   //{"rmode", &FAD::cmd_rmode, 0, "<first|stop>", "Readout start at first bin or stop position (DRS4)"},
     
    6161  // DIM console service used in PrintMessage()
    6262  ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
    63 
    64   // Get configuration data (static needed for c_str() pointers to remain valid after constructor finished)
    65   //static string _CalibDataPath = GetConfig("CalibDataPath");
    66   //fCalibDataPath = _CalibDataPath.c_str();
    67 
    68   //snprintf(CalibInfoFilename,sizeof(CalibInfoFilename), "%s/CalibInfo", fCalibDataPath);
    6963
    7064  // Construct boards
     
    164158          // Check if idle mode required
    165159          if (CommandList[i].NeedIdle && Mode != idle) {
    166                 PrintMessage("Current mode is not idle ('cancel' will stop current operation)");
     160                PrintMessage("Current mode is not idle ('cancel' will stop current operation)\n");
    167161                return;
    168162          }
     
    449443
    450444  pthread_t Thread;
    451   int Code;
     445  int Code, Count;
     446  FILE *File;
     447  vector<string> Items;
    452448
    453449  // Invalidate calibration?
     
    456452        return;
    457453  }
    458    
     454
     455  // Read calibration data from file?
     456  if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumCalibEvents)) {
     457    // Open file
     458        if ((File = fopen(Parameter[1].c_str(), "r")) == NULL) {
     459          PrintMessage("Error opening file '%s'\n", Parameter[1].c_str());
     460          return;
     461        }
     462        PrintMessage("Reading amplitude calibration information from file '%s'\n", Parameter[1].c_str());
     463
     464        // Read file into buffer and close file
     465        string Buffer;
     466    while (feof(File)==0 && ferror(File)==0) Buffer.push_back((char) fgetc(File));
     467        if (Buffer.size() > 0) Buffer = Buffer.substr(0, Buffer.size()-1);
     468        if (fclose(File) != 0) PrintMessage("Could not close file '%s'\n", Parameter[1].c_str());
     469
     470        // Search for calibration data for boards
     471        vector<string> Result = Tokenize(Buffer, "\n");
     472
     473        for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
     474          for (unsigned int Line=0; Line<Result.size(); Line++) {
     475            if (Result[Line].find(Boards[Brd]->Name) == string::npos) continue;
     476
     477                PrintMessage("Found calibration data for board '%s'\n", Boards[Brd]->Name);
     478                Items = Tokenize(Result[Line]);
     479               
     480                // Check if correct number of items
     481                if (Items.size() != NChips*NChannels*NBins*2 + 3) {
     482                  PrintMessage("Error, data format invalid\n", Parameter[1].c_str());
     483                  return;
     484                }
     485
     486                // Extract data
     487                Boards[Brd]->ACalibTemp = atof(Items[1].c_str());
     488                Boards[Brd]->ACalibTime = atoi(Items[2].c_str());
     489
     490                Count = 3;
     491                for (unsigned int i=0; i<NChips; i++) {
     492                  for (unsigned int j=0; j<NChannels; j++) {
     493                        for (unsigned int k=0; k<NBins; k++) {
     494                          Boards[Brd]->Baseline[i][j][k] = atoi(Items[Count++].c_str());
     495                          Boards[Brd]->Gain[i][j][k] = atof(Items[Count++].c_str());
     496                        }
     497                  }
     498                }
     499          }
     500        } // Loop over boards
     501
     502        return;
     503  } // Reading calibration from file
     504
    459505  // Set number of events required for calibration
    460506  if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumCalibEvents) || NumCalibEvents<=0) {
     
    518564        struct FADBoard::BoardStatus S = Boards[i]->GetStatus();
    519565
    520         PrintMessage("Board #%d (%sactive)    Communication %s\n", i, Boards[i]->Active ? "":"in", Boards[i]->CommError ? "ERROR":"OK");
     566        PrintMessage("Board #%d - %s (%sactive)    Communication %s\n", i, Boards[i]->Name, Boards[i]->Active ? "":"in", Boards[i]->CommError ? "ERROR":"OK");
    521567        PrintMessage("DAC %d %d %d %d   %d %d %d %d\n", S.DAC[0], S.DAC[1], S.DAC[2], S.DAC[3], S.DAC[4], S.DAC[5], S.DAC[6], S.DAC[7] );
    522568        PrintMessage("Temperature %.2f %.2f %.2f %.2f", S.Temp[0], S.Temp[1], S.Temp[2], S.Temp[3]);
     
    654700  /* Procedure
    655701
    656   1. Register sampling frequency from FTM!
    657   ...
    658702  5. Issue single trigger and verify settings
    659703  ...
     
    682726        Boards[Brd]->Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    683727
    684     // Set DAC first value
     728    // Set first DAC value
    685729    DACCmd[1] = htons(0);
    686730    DACCmd[3] = htons(0);
     
    688732        Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    689733
    690                 // Switch off SCLK
    691                 //Boards[Brd]->Send(CMD_SCLK_OFF);
     734        // Switch off SCLK
     735        //Boards[Brd]->Send(CMD_SCLK_OFF);
    692736
    693737        // Start accumulation
    694738    Boards[Brd]->AccumulateSum(NumCalibEvents);
    695         //Boards[Brd]->Send(CMD_Trigger_C);
    696739  }
    697740
     
    723766        Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    724767
    725                 // Switch off SCLK
    726                 //Boards[Brd]->Send(CMD_SCLK_OFF);
     768        // Switch off SCLK
     769        //Boards[Brd]->Send(CMD_SCLK_OFF);
    727770
    728771        // Start accumulation
     
    739782        }
    740783  }
    741         for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    742                 // Switch on SCLK
    743                 Boards[Brd]->Send(CMD_SCLK_ON);
    744         }
    745784
    746785  // Stop triggering, write back original ROI and DAC settings
    747786  for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    748         //Boards[Brd]->Send(CMD_Trigger_S);
     787        // Switch on SCLK
     788        Boards[Brd]->Send(CMD_SCLK_ON);
    749789
    750790        // Determine gain
     
    773813  }
    774814
     815  Mode = idle;
     816 
     817  if (Cancel) {
     818    PrintMessage("Amplitude calibration cancelled\n");
     819        return;
     820  }
     821 
    775822  PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
    776   Mode = idle;
    777  
    778   // Write short calibration information
    779   /*time_t Time = time(NULL);
    780   FILE *InfoFile = fopen(CalibInfoFilename, "w");
    781   if (InfoFile != NULL) {
    782         fprintf(InfoFile, "# Calibration information as of %s\n", ctime(&Time));
    783         for (int i=0; i<GetNumberOfBoards(); i++) {
    784           fprintf(InfoFile, "%d %d %.1f %d %.2f\n", GetBoard(i)->GetBoardSerialNumber(), ACalib[i], ACalibTemp[i], TCalib[i], DRSFreq[i]);
    785         }
    786         fclose(InfoFile);
    787   }
    788   else PrintMessage("Could not write calibration information to file '%s'\n", CalibInfoFilename);
    789 */
     823 
     824  // Write calibration data to file
     825  time_t Time = time(NULL);
     826  struct tm *TimeInfo;
     827  char Buffer[200];
     828
     829  // Generate filename
     830  TimeInfo = localtime(&Time);
     831  if (strftime(Buffer, sizeof(Buffer), "/FADcalib_%y-%m-%jT%X.txt", TimeInfo) == 0) {
     832        PrintMessage("Could not generate calibration data file name, strftime() failed\n");
     833        return;
     834  }
     835  string Filename = string(getenv("HOME"))+Buffer;
     836
     837  FILE *File = fopen(Filename.c_str(), "w");
     838  if (File == NULL) {
     839        PrintMessage("Could not open calibration data file '%s'\n", Filename.c_str());
     840        return;
     841  }
     842 
     843  // Fix: Should actually contain serial number!
     844  for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
     845        fprintf(File, "\n%s: %f %d ", Boards[Brd]->Name, Boards[Brd]->ACalibTemp, (int) Boards[Brd]->ACalibTime);
     846        for (unsigned int i=0; i<NChips; i++) {
     847          for (unsigned int j=0; j<NChannels; j++) {
     848                for (unsigned int k=0; k<NBins; k++) {
     849                  fprintf(File, "%d %lf ", Boards[Brd]->Baseline[i][j][k], Boards[Brd]->Gain[i][j][k]);
     850                }
     851          }
     852        }
     853  }
     854
     855  // Close file
     856  if (fclose(File) != 0) {
     857    PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
     858  }
     859 
     860  PrintMessage("Wrote amplitude calibration information to file '%s'\n", Filename.c_str());
    790861}
    791862
     
    896967                        if (Boards[Brd]->ACalibTime == -1) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i];
    897968                        else {
    898                         //Data[Count++] = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]) * Boards[Brd]->Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins];
    899969                      Temp = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]);
    900970                          Temp *= Boards[Brd]->Gain[Chip][Chan][0]/Boards[Brd]->Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins];
     
    926996}
    927997
     998
    928999/*
    929 // Read calibration data
    930 bool FAD::ReadCalibration() {
    931 
    932   static char Buffer[MAX_COM_SIZE];
    933   int Serial, Calib;
    934   float Temp, Freq;
    935 
    936   for (int i=FirstBoard; i<=LastBoard; i++) {
    937     if (GetBoard(i)->GetDRSType() == 4) {
    938       if (ACalib[i] == false) {
    939             // Check calibration info file if EEPROM data on DRS board still valild
    940                 FILE *CalibInfo = fopen(CalibInfoFilename, "r");
    941                 if (CalibInfo == NULL) return false;
    942                 fgets(Buffer, sizeof(Buffer), CalibInfo); // skip first two lines
    943                 fgets(Buffer, sizeof(Buffer), CalibInfo);
    944 
    945                 while (fgets(Buffer, sizeof(Buffer), CalibInfo) != NULL) {
    946                   if (sscanf(Buffer, "%d %d %f %*d %f", &Serial, &Calib, &Temp, &Freq) != 4) {
    947                         fclose(CalibInfo);
    948                         return false;
    949                   }
    950 
    951                   if (Serial==GetBoard(i)->GetBoardSerialNumber() && int(Freq*100)==int(DRSFreq[i]*100) && Calib==1) {
    952                         ACalib[i] = true;
    953                         ACalibTemp[i] = Temp;
    954                         break;
    955                   }
    956                 }
    957                 fclose(CalibInfo);
    958           }
    959     }
    960     else {
    961       if (!ACalib[i]) {
    962         GetBoard(i)->SetCalibrationDirectory(fCalibDataPath);
    963         PrintMessage("Reading response calibration file for board %d from: \"%s\"\n", i, fCalibDataPath);
    964         for (int Chip=0; Chip<GetBoard(i)->GetNumberOfChips(); Chip++) {
    965           if (GetBoard(i)->GetResponseCalibration()->ReadCalibration(Chip) == false) return false;
    966         }
    967         ACalib[i] = true;
    968       }
    969     }
    970         if (fabs(ACalibTemp[i]-GetBoard(i)->GetTemperature())>2) PrintMessage("Warning: Large difference to calibration temperature for board %d\n", i);
    971   } // Loop over boards
    972   return true;
    973 }
    974 
    9751000// Set DOMINO mode
    9761001void FAD::SetDOMINOMode(int mode) {
  • fact/FADctrl/FAD.h

    r10100 r10101  
    2121
    2222const int DEFAULT_NUM_CALIB_EVENTS = 100;
     23const char CALIB_DIRECTORY[] = "~/";
    2324
    2425enum ModeType {idle, acalib, tcalib};
     
    4445
    4546        int NumCalibEvents;
    46     //const char *fCalibDataPath;
    47     //char CalibInfoFilename[MAX_PATH];
    4847       
    4948        std::vector<std::string> BoardList;
  • fact/FADctrl/FADBoard.cc

    r10099 r10101  
    220220        // Check if buffer starts with start_package_flag, remove data if not
    221221        Temp = 0;
    222         while (ntohs(Header->start_package_flag) != 0xfb01 && Pos > 0) {
    223           memmove(Buffer, Buffer+1, Pos-1);
    224           Pos--;
    225           Temp++;
    226         }
     222        while (ntohs(*((unsigned short *) (Buffer+Temp))) != 0xfb01 && Temp<Pos) Temp++;
    227223        if (Temp != 0) {
    228           printf("Removed %d bytes because of start_package_flag not found\n", Temp);
     224          memmove(Buffer, Buffer+Temp, Pos-Temp);
     225          Pos -= Temp;
     226          m->PrintMessage("Removed %d bytes because of start_package_flag not found\n", Temp);
    229227          continue;
    230228        }
  • fact/FADctrl/FADBoard.h

    r10080 r10101  
    2525        class FAD *m;
    2626        int Socket;
    27         char *Name;
    2827        pthread_t Thread;
    2928        pthread_mutex_t Mutex;
     
    6968        void Unlock();
    7069
     70        char *Name;
    7171        bool InitOK;
    7272        bool CommError;
  • fact/FADctrl/FADctrl.cc

    r10099 r10101  
    55#include <stdio.h>
    66#include <readline/history.h>
     7#include <string>
    78
    89#include "FAD.h"
    910
    10 const char READLINE_HIST_FILE[] = "/tmp/.history.FADctrl";
     11using namespace std;
    1112
     13const string READLINE_HIST_FILE = string(getenv("HOME"))+"/.history_FADctrl";
    1214void OpenOtherSockets();
    1315
     
    2224 
    2325  // Load history buffer
    24   read_history(READLINE_HIST_FILE);
     26  read_history(READLINE_HIST_FILE.c_str());
    2527
    2628  system("clear");
     
    7375  DimClient::sendCommand(SERVER_NAME"/Command", "dwrite on");
    7476  DimClient::sendCommand(SERVER_NAME"/Command", "roi all 1024");
    75   sleep (1);
    7677
    77   //EmptySockets(SocketDescriptor, 8, 750000L);
    7878  M.PrintMessage("Finished initalizing all boards\n");
    7979 
     
    100100 
    101101  // Save history buffer 
    102   int Ret = write_history(READLINE_HIST_FILE);
    103   if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE, strerror(Ret));
     102  int Ret = write_history(READLINE_HIST_FILE.c_str());
     103  if (Ret != 0 ) printf("Error writing history file to '%s' (%s)\n", READLINE_HIST_FILE.c_str(), strerror(Ret));
    104104
    105105  // Terminate thread for other sockets 
  • fact/FADctrl/History.txt

    r10083 r10101  
    3322/10/2010      First check-in of FADctrl. Initial version derived from drsdaq revision 10007.
    445/1/2011        First version of amplitude calibration (no secondary calibration, yet). New 'socketmode' command.
     513/1/2011       Amplitude calibration data is written to text file.
Note: See TracChangeset for help on using the changeset viewer.