Changeset 10212


Ignore:
Timestamp:
03/02/11 13:35:50 (14 years ago)
Author:
ogrimm
Message:
Implemented new FAD data format
Location:
fact/FADctrl
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10187 r10212  
    7070  // Initialise configuration information (later non-blocking access in commandHandler())
    7171  GetConfig("CalibTempDiffWarn", "0");
     72  GetConfig("CalibFreqDiffWarn", "0");
    7273
    7374  // Construct boards
     
    492493
    493494  // Check conditions for run of all active boards
    494   float MaxDiff = atof(GetConfig("CalibTempDiffWarn").c_str());
     495  float MaxTempDiff = atof(GetConfig("CalibTempDiffWarn").c_str());
     496  float MaxFreqDiff = atof(GetConfig("CalibFreqDiffWarn").c_str());
    495497
    496498  for (unsigned int i=0; i<Boards.size(); i++) {
    497499        if (!Boards[i]->Active) continue;
    498500
    499         if (Boards[i]->ACalibTime == -1) PrintMessage("Warning: Amplitude calibration missing for board %d\n", i);
     501        if (Boards[i]->ACalib.Time == -1) PrintMessage("Warning: Amplitude calibration missing for board %d\n", i);
    500502        else {
    501503          Temp = 0;
    502504          for (unsigned int j=0; j<NTemp; j++) Temp += Boards[i]->GetStatus().Temp[j] / NTemp;
    503           if (fabs(Boards[i]->ACalibTemp-Temp) > MaxDiff) PrintMessage("Warning: Amplitude calibration to current temperature difference larger than %.1f K for board %d\n", MaxDiff, i);
     505          if (fabs(Boards[i]->ACalib.Temp-Temp) > MaxTempDiff) PrintMessage("Warning: Calibration to current temperature difference larger than %.1f K for board %d\n", MaxTempDiff, i);
     506          if (fabs(Boards[i]->ACalib.Frequency-Boards[i]->GetStatus().Frequency) > MaxFreqDiff) PrintMessage("Warning: Calibration to current frequency difference larger than %.1f GHz for board %d\n", MaxFreqDiff, i);
    504507        }
    505508  }
     
    515518void FAD::cmd_acalib() {
    516519
    517   int Count;
    518   FILE *File;
    519   vector<string> Items;
    520 
    521520  // Invalidate calibration?
    522521  if (Parameter.size() == 2 && Match(Parameter[1], "invalidate")) {
    523         for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->ACalibTime = -1;
     522        for (unsigned int i=0; i<Boards.size(); i++) Boards[i]->ACalib.Time = -1;
    524523        return;
    525524  }
     
    527526  // Read calibration data from file?
    528527  if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumEventsRequested)) {
     528        FILE *File;
     529        struct FADBoard::CalibData Data;
     530
    529531    // Open file
    530532        if ((File = fopen(Parameter[1].c_str(), "r")) == NULL) {
     
    532534          return;
    533535        }
    534         PrintMessage("Reading amplitude calibration information from file '%s'\n", Parameter[1].c_str());
    535 
    536         // Read file into buffer and close file
    537         string Buffer;
    538     while (feof(File)==0 && ferror(File)==0) Buffer.push_back((char) fgetc(File));
    539         if (Buffer.size() > 0) Buffer = Buffer.substr(0, Buffer.size()-1);
     536        // Read data and check if it applies to any board
     537        while (fread(&Data, sizeof(Data), 1, File) == 1) {
     538          for (unsigned int i=0; i<Boards.size(); i++) if (Data.DNA == Boards[i]->GetStatus().DNA) {
     539            PrintMessage("Found calibration data for board %d (%s)\n", i, ctime(&Data.Time));
     540            Boards[i]->ACalib = Data;
     541          }
     542        }
     543        //Close file
    540544        if (fclose(File) != 0) PrintMessage("Could not close file '%s'\n", Parameter[1].c_str());
    541 
    542         // Search for calibration data for boards
    543         vector<string> Result = Tokenize(Buffer, "\n");
    544 
    545         for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    546           for (unsigned int Line=0; Line<Result.size(); Line++) {
    547             if (Result[Line].find(Boards[Brd]->Name) == string::npos) continue;
    548 
    549                 PrintMessage("Found calibration data for board '%s'\n", Boards[Brd]->Name);
    550                 Items = Tokenize(Result[Line]);
    551                
    552                 // Check if correct number of items
    553                 if (Items.size() != NChips*NChannels*NBins*3 + 3) {
    554                   PrintMessage("Error, data format invalid\n", Parameter[1].c_str());
    555                   return;
    556                 }
    557 
    558                 // Extract data
    559                 Boards[Brd]->ACalibTemp = atof(Items[1].c_str());
    560                 Boards[Brd]->ACalibTime = atoi(Items[2].c_str());
    561 
    562                 Count = 3;
    563                 for (unsigned int i=0; i<NChips; i++) {
    564                   for (unsigned int j=0; j<NChannels; j++) {
    565                         for (unsigned int k=0; k<NBins; k++) {
    566                           Boards[Brd]->Baseline[i][j][k] = atoi(Items[Count++].c_str());
    567                           Boards[Brd]->Gain[i][j][k] = atof(Items[Count++].c_str());
    568                           Boards[Brd]->SecondaryBaseline[i][j][k] = atof(Items[Count++].c_str());
    569                         }
    570                   }
    571                 }
    572           }
    573         } // Loop over boards
    574 
    575545        return;
    576546  } // Reading calibration from file
     
    602572        }         
    603573
    604         PrintMessage("\rTotal boards: %d    (%d communication errors)     Active boards: %d\n", Boards.size(), Error, Count);
     574        PrintMessage("\rTotal boards: %d    (%d communication errors)\n", Boards.size(), Error);
    605575
    606576        // Print list of active boards
    607         if (Count != 0) {
    608           PrintMessage("Active are ");
    609           for (unsigned int i=0; i<Boards.size(); i++) if (Boards[i]->Active) PrintMessage(" %d", i);
    610           PrintMessage("\n");
    611         }
     577        PrintMessage("Active are %d boards(s)   ", Count);
     578        for (unsigned int i=0; i<Boards.size(); i++) {
     579          if (Boards[i]->Active) PrintMessage(" %d", i);
     580          if (!Boards[i]->Active) PrintMessage("!");
     581        }
     582        PrintMessage("\n");
    612583
    613584        // Current mode
     
    633604
    634605        // Calibration information
    635         if (Boards[i]->ACalibTime == -1) PrintMessage("No amplitude calibration available\n");
    636         else PrintMessage("Calibration temperature %.1f     Calibration time %s" , Boards[i]->ACalibTemp, ctime(&Boards[i]->ACalibTime));
     606        if (Boards[i]->ACalib.Time == -1) PrintMessage("No amplitude calibration available\n");
     607        else PrintMessage("Calibration data: Temperature %.1f     Frequency %.2f     Time %s" , Boards[i]->ACalib.Temp, Boards[i]->ACalib.Frequency, ctime(&Boards[i]->ACalib.Time));
    637608
    638609        // Status information
     
    645616        PrintMessage("Last event received %s", ctime(&S.Update.tv_sec));
    646617
    647         PrintMessage("Board ID %d               Firmware revision %d\n", S.BoardID, S.FirmwareRevision);
     618        // Board identification
     619        PrintMessage("Board ID %d      Firmware revision %d      Serial %llx\n", S.BoardID, S.FirmwareRevision, S.DNA);
     620        PrintMessage("Board time %d      Event counter %d\n", S.BoardTime, S.EventCounter);
     621
     622        // Slow data
     623        PrintMessage("Frequency %.2f GHz    Phase shift %d    PLL lock %d %d %d %d\n", S.Frequency, S.PhaseShift, S.Lock[0], S.Lock[1], S.Lock[2], S.Lock[3]);
    648624        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] );
    649625        PrintMessage("Temperature %.2f %.2f %.2f %.2f", S.Temp[0], S.Temp[1], S.Temp[2], S.Temp[3]);
     
    654630        }
    655631        PrintMessage("\n");
    656         /*PrintMessage("Serial %d, firmware %d\n"
    657                     GetBoard(i)->GetBoardSerialNumber(),
    658 
     632        /*
    659633        if (GetBoard(i)->GetStatusReg() & BIT_RUNNING)
    660634          PrintMessage("   Domino wave running\n");
     
    799773//
    800774// Exit programm
    801 // SIGTERM sets ExitRequest flag, and also makes
    802 // readline() return (in case command came over network)
     775// SIGTERM makes readline() return (in case command came over network)
    803776//
    804777void FAD::cmd_exit() {
     
    806779  if (Mode != idle) cmd_stop();
    807780
     781  ExitRequest = true;
     782
     783  // Wait to allow console input to arrive at readline()
     784  usleep(10000);
    808785  pthread_kill(MainThread, SIGTERM);
    809786}
     
    830807  PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
    831808 
    832   // Write calibration data to file
    833   time_t Time = time(NULL);
    834   struct tm *TimeInfo;
    835   char Buffer[200];
    836 
    837   // Generate filename
    838   TimeInfo = localtime(&Time);
    839   if (strftime(Buffer, sizeof(Buffer), "/FADcalib_%y-%m-%dT%X.txt", TimeInfo) == 0) {
    840         PrintMessage("Could not generate calibration data file name, strftime() failed\n");
    841         return;
    842   }
    843   string Filename = string(getenv("HOME"))+Buffer;
    844 
    845   FILE *File = fopen(Filename.c_str(), "w");
     809  // Open calibration data file
     810  string Filename = string(getenv("HOME"))+"/FAD_ACal";
     811  FILE *File = fopen(Filename.c_str(), "ab");
     812
    846813  if (File == NULL) {
    847814        PrintMessage("Could not open calibration data file '%s'\n", Filename.c_str());
     
    849816  }
    850817 
    851   // Fix: Should actually contain serial number!
    852   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    853         fprintf(File, "\n%s: %f %d ", Boards[Brd]->Name, Boards[Brd]->ACalibTemp, (int) Boards[Brd]->ACalibTime);
    854         for (unsigned int i=0; i<NChips; i++) {
    855           for (unsigned int j=0; j<NChannels; j++) {
    856                 for (unsigned int k=0; k<NBins; k++) {
    857                   fprintf(File, "%d %lf %lf ", Boards[Brd]->Baseline[i][j][k], Boards[Brd]->Gain[i][j][k], Boards[Brd]->SecondaryBaseline[i][j][k]);
    858                 }
    859           }
     818  // Write calibration information
     819  for (unsigned int i=0; i<Boards.size(); i++) {
     820        if (fwrite(&Boards[i]->ACalib, sizeof(Boards[i]->ACalib), 1, File) != 1) {
     821          PrintMessage("Could not write to calibration file '%s'\n", Filename.c_str());
     822          break;
    860823        }
    861824  }
    862825
    863826  // Close file
    864   if (fclose(File) != 0) {
    865     PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
    866   }
    867  
    868   PrintMessage("Wrote amplitude calibration information to file '%s'\n", Filename.c_str());
     827  if (fclose(File) != 0) PrintMessage("Could not close calibration file '%s'\n", Filename.c_str());
     828 
     829  PrintMessage("Appended amplitude calibration information to file '%s'\n", Filename.c_str());
    869830}
    870831
     
    1002963          S = Boards[Brd]->GetStatus();
    1003964          BStruct[Brd]->SerialNo = S.BoardID;
    1004           BStruct[Brd]->NomFreq = 2;
     965          BStruct[Brd]->NomFreq = S.Frequency;
    1005966          BStruct[Brd]->BoardTemp = 0;       
    1006967          for (unsigned int i=0; i<NTemp; i++) BStruct[Brd]->BoardTemp += S.Temp[i]/NTemp;
     
    1024985          for (unsigned int Chip=0; Chip<NChips; Chip++) for (unsigned int Chan=0; Chan<NChannels; Chan++) {
    1025986                for (int i=0; i<S.ROI[Chip][Chan]; i++) {
    1026                   if (Boards[Brd]->ACalibTime == -1) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i];
     987                  if (Boards[Brd]->ACalib.Time == -1) Data[Count++] = Boards[Brd]->Data[Chip][Chan][i];
    1027988                  else {
    1028                     Temp = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]);
    1029                         Temp *= Boards[Brd]->Gain[Chip][Chan][0]/Boards[Brd]->Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins];
    1030                         //Temp -= Boards[Brd]->SecondaryBaseline[Chip][Chan][i];
     989                    Temp = (Boards[Brd]->Data[Chip][Chan][i] - Boards[Brd]->ACalib.Baseline[Chip][Chan][(i+S.TriggerCell[Chip])%NBins]);
     990                        Temp *= Boards[Brd]->ACalib.Gain[Chip][Chan][0]/Boards[Brd]->ACalib.Gain[Chip][Chan][(i+S.TriggerCell[Chip])%NBins];
     991                        //Temp -= Boards[Brd]->ACalib.Secondary[Chip][Chan][i];
    1031992                        Data[Count++] = (short) Temp;
    1032993                  }
  • fact/FADctrl/FAD.h

    r10187 r10212  
    2020
    2121#include "FADBoard.h"
     22#include "../drsdaq/RawDataCTX.h"
    2223
    2324const unsigned int PORT = 5000;
  • fact/FADctrl/FADBoard.cc

    r10187 r10212  
    2020  Continue = true;
    2121  CommOK = false;
    22   ACalibTime = -1;
     22  ACalib.Time = -1;
    2323  Status.Update.tv_sec = -1;
    2424  Port = ServerPort;
     
    166166  case init:
    167167        // Invalidate current calibration
    168         ACalibTime = -1;
     168        ACalib.Time = -1;
    169169
    170170        // Save initial board status, set all ROIs to 1024 and set DAC values
     
    203203                for (unsigned int j=0; j<NChannels; j++) {
    204204                  for (unsigned int k=0; k<NBins; k++) {
    205                         Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
     205                        ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
    206206                  }
    207207                }
     
    237237                for (unsigned int j=0; j<NChannels; j++) {
    238238                  for (unsigned int k=0; k<NBins; k++) {
    239                         Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - Baseline[i][j][k];
     239                        ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
    240240                  }
    241241                }
     
    260260          for (unsigned int Chan=0; Chan<NChannels; Chan++) {
    261261                for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
    262                   Sum[Chip][Chan][i] = Data[Chip][Chan][i] - Baseline[Chip][Chan][(i-Status.TriggerCell[Chip]) % NBins];
     262                  Sum[Chip][Chan][i] = Data[Chip][Chan][i] - ACalib.Baseline[Chip][Chan][(i-Status.TriggerCell[Chip]) % NBins];
    263263                }
    264264          }
     
    271271                for (unsigned int j=0; j<NChannels; j++) {
    272272                  for (unsigned int k=0; k<NBins; k++) {
    273                         SecondaryBaseline[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
     273                        ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
    274274                  }
    275275                }
     
    290290
    291291          // Store calibration time and temperature
    292           ACalibTime = time(NULL);
    293           ACalibTemp = 0;
    294           for (unsigned int i=0; i<NTemp; i++) ACalibTemp += Status.Temp[i] / NTemp;
     292          ACalib.DNA = Status.DNA;
     293          ACalib.Frequency = Status.Frequency;
     294          ACalib.Time = time(NULL);
     295          ACalib.Temp = 0;
     296          for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
    295297         
    296298          // Inform event thread that calibration is finished
     
    415417          gettimeofday(&Status.Update, NULL);
    416418
    417           // Extract ID and type information
    418           Status.BoardID = ntohl(Header->board_id);
     419          // Extract board and trigger information
     420          Status.BoardID = ntohl(Header->board_id);       
    419421          Status.FirmwareRevision = ntohl(Header->version_no);
     422          Status.BoardTime = ntohl(Header->time);
     423          Status.EventCounter = ntohl(Header->fad_evt_counter);
    420424          Status.TriggerID = ntohl(Header->trigger_id);
    421425          Status.TriggerType = ntohs(Header->trigger_type);
     426          Status.TriggerCRC = ntohs(Header->trigger_crc);
     427          Status.DNA = Header->DNA;
     428
     429          // Extract frequency related information
     430          Status.Frequency = ntohl(Header->REFCLK_frequency)/1.0e6;
     431          Status.PhaseShift = ntohs(Header->adc_clock_phase_shift);
     432          for (unsigned int i=0; i<NChips; i++) {
     433                if ((Header->PLLLCK & (1<<i)) != 0) Status.Lock[i] = true;
     434                else Status.Lock[i] = false;
     435          }
    422436
    423437          // Extract temperatures (MSB indicates if temperature is positive or negative)
  • fact/FADctrl/FADBoard.h

    r10164 r10212  
    3838
    3939        struct BoardStatus {
    40           unsigned short BoardID;
    41           unsigned short FirmwareRevision;
    42           unsigned long TriggerID;
    43           unsigned char TriggerType;
    44           unsigned short TriggerCell[NChips];
    45           unsigned short ROI[NChips][NChannels];
    46           unsigned short DAC[NDAC];
     40          uint16_t BoardID;
     41          uint64_t DNA;
     42          uint16_t FirmwareRevision;
     43          uint32_t BoardTime;
     44          uint32_t EventCounter;
     45          uint32_t TriggerID;
     46          uint16_t TriggerType;
     47          uint16_t TriggerCRC;
     48          uint16_t TriggerCell[NChips];
     49          float Frequency;
     50          bool Lock[NChips];
     51          uint16_t PhaseShift;
     52          uint16_t ROI[NChips][NChannels];
     53          uint16_t DAC[NDAC];
    4754          float Temp[NTemp];
    4855          struct timeval Update;
     
    5158        short Data[NChips][NChannels][NBins];
    5259        long int Sum[NChips][NChannels][NBins];
    53         short Baseline[NChips][NChannels][NBins];
    54         double Gain[NChips][NChannels][NBins];
    55         double SecondaryBaseline[NChips][NChannels][NBins];
    56         float ACalibTemp;
    57         time_t ACalibTime;
    5860
     61        struct CalibData {
     62          uint64_t DNA;
     63          short Baseline[NChips][NChannels][NBins];
     64          double Gain[NChips][NChannels][NBins];
     65          double Secondary[NChips][NChannels][NBins];
     66          float Temp;
     67          float Frequency;
     68          time_t Time;
     69        } ACalib;
    5970
    6071        void Send(const void *, size_t);
  • fact/FADctrl/FADFormat.h

    r10187 r10212  
    22#define FADFORMAT_H_SEEN
    33
    4 #include "../drsdaq/RawDataCTX.h"
     4#include <stdint.h>
    55
    66//
     
    3030#define CMD_Write 0x0500                // write to Config-RAM
    3131
    32 #define CMD_TRIGGERS_ON 0x1800  // enables the trigger line -- incoming triggers can now be accepted
    33 #define CMD_TRIGGERS_OFF 0x1900 // disables the trigger line -- no trigger will be accepted
     32#define CMD_TRIGGERS_ON 0x1800  // enables the trigger line
     33#define CMD_TRIGGERS_OFF 0x1900 // disables the trigger line
    3434
    3535//
    3636// Other definitions
    3737//
    38 #define MAX_ADDR 0xFF // highest address in Config-RAM
    39 #define BADDR_ROI 0x00 // Baseaddress ROI-Values
    40 #define BADDR_DAC 0x24 // Baseaddress DAC-Values
     38#define MAX_ADDR 0xFF           // highest address in Config-RAM
     39#define BADDR_ROI 0x00          // Baseaddress ROI-Values
     40#define BADDR_DAC 0x24          // Baseaddress DAC-Values
    4141#define MAX_VAL 65535
    4242#define MAX_ROIVAL 1024
     
    5353//
    5454typedef struct {
    55 // ------------------------------
    56 uint16_t start_package_flag;
    57 uint16_t package_length;
    58 uint16_t version_no;
    59 uint16_t PLLLCK;
    60 // ------------------------------
    61 uint16_t trigger_crc;
    62 uint16_t trigger_type;
    63 uint32_t trigger_id;
    64 // ------------------------------
    65 uint32_t fad_evt_counter;
    66 uint32_t REFCLK_frequency;
    67 // ------------------------------
    68 uint16_t board_id;
    69 uint16_t adc_clock_phase_shift;
    70 uint16_t number_of_triggers_to_generate;
    71 uint16_t trigger_generator_prescaler;
    72 // ------------------------------
    73 uint8_t DNA[8]; // Xilinx DNA
    74 // ------------------------------
    75 uint32_t time;
    76 uint32_t fad_status;
    77 // ------------------------------
    78 int16_t drs_temperature[NTemp];
    79 // ------------------------------
    80 uint16_t dac[NDAC];
    81 // ------------------------------
     55
     56  uint16_t start_package_flag;
     57  uint16_t package_length;
     58  uint16_t version_no;
     59  uint16_t PLLLCK;
     60
     61  uint16_t trigger_crc;
     62  uint16_t trigger_type;
     63  uint32_t trigger_id;
     64
     65  uint32_t fad_evt_counter;
     66  uint32_t REFCLK_frequency;
     67
     68  uint16_t board_id;
     69  uint16_t adc_clock_phase_shift;
     70  uint16_t number_of_triggers_to_generate;
     71  uint16_t trigger_generator_prescaler;
     72
     73  uint64_t DNA;
     74
     75  uint32_t time;
     76  uint32_t fad_status;
     77
     78  int16_t drs_temperature[NTemp];
     79
     80  uint16_t dac[NDAC];
     81
    8282} __attribute__((__packed__)) PEVNT_HEADER;
     83
    8384typedef struct {
    8485uint16_t id;
  • fact/FADctrl/History.txt

    r10186 r10212  
    161628/2/2011       Removed 'static' keywords in FADBoard::ReadLoop()
    17171/3/2011        Board list can be passed via command line
     182/3/2011        Implemented new FAD data format
Note: See TracChangeset for help on using the changeset viewer.