Changeset 55 for drsdaq


Ignore:
Timestamp:
06/08/09 14:30:46 (16 years ago)
Author:
ogrimm
Message:
Config file reading updated, RawDataCTX class takes varying event size into account
Location:
drsdaq
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • drsdaq/DAQReadout.cc

    r51 r55  
    1111#include "DAQReadout.h"
    1212#include "SlowData.h"
     13
     14#define TMPNAME "/tmp/__tmp__drsdaq__"
    1315
    1416static const char* daq_state_str[] = {"active", "stopped"};
     
    6466
    6567  // Initialize status structure
    66   daq_state         = stopped;
    67   daq_runtype       = data;
    68   Socket            = -1;
    69   Exit              = false;
    70   CalibrationRead   = false;
    71   NumEvents             = 0;
    72   NumEventsRequested    = 100;
    73   NumBoards             = 0;
    74   FirstBoard            = 0;
    75   LastBoard             = -1;
    76  
     68  daq_state          = stopped;
     69  daq_runtype        = data;
     70  Socket             = -1;
     71  Exit               = false;
     72  CalibrationRead    = false;
     73  NumEvents          = 0;
     74  NumEventsRequested = 100;
     75  NumBoards          = 0;
     76  FirstBoard         = 0;
     77  LastBoard          = -1;
     78  CmdFromSocket      = false;
     79     
    7780  // Read configuration file
    7881  FILE *File;
     
    8386    printf("Reading drsdaq configuration file %s\n", Configfile);
    8487    ReadCard("LogFile",             fLogFile,           's', File);
     88    ReadCard("MaxLogLines",        &fMaxLogLines,       'U', File);
    8589    ReadCard("RawDataPath",         fRawDataPath,       's', File);
    8690    ReadCard("RotateWave",         &fRotateWave,        'I', File);
     
    100104    fLastSample = kNumberOfBins;
    101105  }
    102 
    103   // Open log file and print configuration
    104   if ((Logfile = fopen(fLogFile, "a")) == NULL)
    105     fprintf(stderr,"Warning: Could not open log file '%s'\n", fLogFile);
    106   else PrintMessage(MsgToLog,"********** Logging started **********\n");
     106 
     107  // Truncate log file to given number of lines
     108  char ShellCmd[MAX_COM_SIZE];
     109  snprintf(ShellCmd, sizeof(ShellCmd), "tail --lines=%u %s >%s; cp %s %s; rm %s", fMaxLogLines, fLogFile, TMPNAME, TMPNAME, fLogFile, TMPNAME);
     110  if (system(ShellCmd) != 0) printf("Warning: Could not truncate log file '%s' to %u lines\n", fLogFile, fMaxLogLines);
     111
     112  // Open log file
     113  if ((Logfile = fopen(fLogFile, "a")) == NULL) printf("Warning: Could not open log file '%s'\n", fLogFile);
     114  PrintMessage(MsgToLog,"********** Logging started **********\n");
     115 
     116  // Print configuration
    107117  cmd_config();
    108118
     
    165175// --------------------------------
    166176
    167 int DAQReadout::CommandControl(char *Command, bool FromSocket) {
    168 
    169   if (strlen(Command) < 2 ) return 0;  // Ignore commands with only '/n'
    170   if (Command[strlen(Command)-1]=='\n') Command[strlen(Command)-1]='\0';  // Remove '/n'
    171  
     177void DAQReadout::CommandControl(char *Command) {
     178
     179  if (strlen(Command)==0) return;  // Ignore empty commands
     180
    172181  if(Command[0]=='.') {   // Shell command
    173182    system(&(Command[1]));
    174     return 0;
     183    return;
    175184  }
    176185
    177186  for(int i=0; i<MAX_NUM_TOKEN; i++) Param[i] = "";  // All pointers point initially to empty string
    178187  NParam = ParseInput(Command, Param);
    179        
    180   CmdFromSocket = FromSocket;
     188
    181189  for(CmdNumber=0; CmdNumber<sizeof(CommandList)/sizeof(CL_Struct); CmdNumber++)
    182190    if (Match(Param[0], CommandList[CmdNumber].Name)) {
     
    184192      else if(CommandList[CmdNumber].NeedNotBusy && NumBoards==0) PrintMessage("No mezzanine boards available\n");
    185193      else (this->*CommandList[CmdNumber].CommandPointer)();
    186       return 0
    187     }
    188   PrintMessage("Unknown command: %s\n",Param[0]);
    189   return 0;
     194      return
     195    }
     196  PrintMessage("Unknown command '%s'\n",Param[0]);
     197  return;
    190198}
    191199         
     
    194202  time_t ActualT;
    195203  time (&ActualT);
    196   PrintMessage(CmdFromSocket ? MsgToSocket:MsgToConsole, "%d:%02d:%02d\n", (int) difftime(ActualT, StartTime)/3600, ((int) difftime(ActualT, StartTime)/60)%60, (int) difftime(ActualT, StartTime)%60);
     204  PrintMessage("%d:%02d:%02d\n", (int) difftime(ActualT, StartTime)/3600, ((int) difftime(ActualT, StartTime)/60)%60, (int) difftime(ActualT, StartTime)%60);
    197205}
    198206
    199207// Print disk space
    200208void DAQReadout::cmd_disk() {
    201   PrintMessage(CmdFromSocket ? MsgToSocket:MsgToConsole, "Free disk space (%s) [MB]: %lu\n", fRawDataPath, CheckDisk(fRawDataPath));
     209  PrintMessage("Free disk space (%s) [MB]: %lu\n", fRawDataPath, CheckDisk(fRawDataPath));
    202210}
    203211
     
    210218// Print DAQ configuration
    211219void DAQReadout::cmd_config() {
    212   PrintMessage("LogFile: %s\tRawDataPath: %s\n"
     220  PrintMessage("LogFile: %s\tMaxLogLines: %u\tRawDataPath: %s\n"
    213221               "RotateWave: %d\t\tFirstSample: %d\t\tLastSample: %d\n"
    214222               "MinDiskSpaceMB: %u\tMaxFileSizeMB: %d\tCCPort: %d\n"
    215                "FirstVMESlot: %d\t\tLastVMESlot: %d\n"
    216                "HVFeedbackConfig: \t%s\n",
    217     fLogFile,fRawDataPath,fRotateWave,fFirstSample,fLastSample,fMinDiskSpaceMB,
     223               "FirstVMESlot: %d\t\tLastVMESlot: %d\t\tHVFeedbackConfig: %s\n",
     224    fLogFile,fMaxLogLines,fRawDataPath,fRotateWave,fFirstSample,fLastSample,fMinDiskSpaceMB,
    218225    fMaxFileSizeMB,fCCPort,fFirstVMESlot,fLastVMESlot,fHVFeedbackConfig);
    219226}
     
    278285  // Create DAQ thread
    279286  if ((pthread_create(&thread_DAQ, NULL, (void * (*)(void *)) DAQ,(void *) this)) != 0)
    280     perror("pthread_create failed with DAQ thread");
     287    PrintMessage("pthread_create failed with DAQ thread (%s)\n",strerror(errno));
    281288  else {
    282289    daq_state = active;
     
    552559    LastBoard = drs->GetNumberOfBoards()-1;
    553560  }
    554   else if (NParam==2 && atoi(Param[1]) >= 0 && atoi(Param[1]) < NumBoards) {
     561  else if (NParam==2 && atoi(Param[1])>=0 && atoi(Param[1])<NumBoards) {
    555562    FirstBoard = atoi(Param[1]);
    556563    LastBoard = FirstBoard;
     
    565572}
    566573
    567 // Print help
     574// Print help (only to console or socket, not to log file)
    568575void DAQReadout::cmd_help() {
    569576  char Buffer[MAX_COM_SIZE];
     
    978985 
    979986  EHeader->EventNumber = NumEvents;
    980   EHeader->TriggerType = 0x0;
     987  EHeader->TriggerType = (daq_runtype == data) ? 1 : 0;
    981988  EHeader->Second = Time.tv_sec;
    982989  EHeader->Microsecond = Time.tv_usec;
     
    10031010}
    10041011
    1005 // Print message to screen, log file and socket
     1012// Print message to log file, and screen or socket (depending on command origin)
    10061013void DAQReadout::PrintMessage(const char *Format, ...) {
    10071014  va_list ArgumentPointer;
    1008   va_start(ArgumentPointer, Format);
    1009   PrintMessage(MsgToConsole|MsgToLog|MsgToSocket, Format, ArgumentPointer);
     1015  va_start(ArgumentPointer, Format);
     1016  if(CmdFromSocket) PrintMessage(MsgToSocket|MsgToLog, Format, ArgumentPointer);
     1017  else PrintMessage(MsgToConsole|MsgToLog, Format, ArgumentPointer);
    10101018  va_end(ArgumentPointer);
    10111019}
     
    10691077      while(!isspace(*Command) && *Command!='\0') Command++;
    10701078    }
    1071     *Command++ = '\0';
     1079    if(*Command != '\0') *Command++ = '\0';
    10721080    Count++;
    1073   };
     1081  }
    10741082  return Count;
    10751083}
    10761084
    1077 // ReadCard function by F. Goebel
    1078 int ReadCard(const char *card_flag, void *store, char type, FILE *fptr) {
    1079  
    1080   char *card_name, *card_val, line[160];
    1081 
    1082   rewind(fptr);
    1083 
    1084   while (fgets(line, 160, fptr) != NULL) {    // Read line by line
    1085     card_name = strtok(line," \t\n");
    1086     card_val  = strtok(NULL," \t\n");
     1085// ReadCard function (original version by F. Goebel)
     1086// Data is read into an array if MaxNum is larger than 1
     1087bool ReadCard(const char *card_flag, void *store, char Type, FILE *File, unsigned int MaxNum) {
     1088 
     1089  char *card_name, *card_val, Buffer[MAX_COM_SIZE];
     1090  unsigned int Count=0;
     1091 
     1092  rewind(File);
     1093
     1094  while (fgets(Buffer, sizeof(Buffer), File) != NULL) {    // Read line by line
     1095    card_name = strtok(Buffer," \t\n");
    10871096   
    1088     if (   card_name    != NULL  && card_val     != NULL   // Comment or empty line?
    1089         && card_name[0] != '*'   && card_name[0] != '#') {
    1090      
    1091       if (strcmp(card_name, card_flag)!=0) {   // Is this the card name we are looking for?
    1092         continue; 
     1097     // Ignore empty lines, comments, and skip if card name does not match
     1098    if (card_name==NULL || card_name[0]=='#' || strcmp(card_name, card_flag)!=0) continue;
     1099
     1100    // Read numbers of given type (if MaxNum>1 read array)
     1101    while ((card_val=strtok(NULL," \t\n")) != NULL && Count++<MaxNum) {
     1102      switch (Type) {
     1103        case 'I': *(((int *&) store)++) = (int) strtol(card_val, NULL, 10);
     1104                  break;
     1105        case 'i': *(((short *&) store)++) = (short) strtol(card_val, NULL, 10);
     1106                  break;
     1107        case 'U': *(((unsigned int *&) store)++) = (unsigned int) strtoul(card_val, NULL, 10);
     1108                  break;
     1109        case 'u': *(((unsigned short *&) store)++) = (unsigned short) strtoul(card_val, NULL, 10);
     1110                  break;
     1111        case 'f': *(((float *&) store)++) = atof(card_val);
     1112                  break;
     1113        case 'd': *(((double *&) store)++) = atof(card_val);
     1114                  break;
     1115        case 's': sprintf((char *) store, "%s", card_val);
     1116                  break;
     1117        case 'c': *((char *) store) = card_val[0];
     1118                  break;
     1119        default:  fprintf(stderr,"Warning: Unknown type '%c' for reading of configuration file\n", Type);
     1120                  return false;
    10931121      }
    1094 
    1095       switch (type) {
    1096       case 'I': *((int *) store)   = (int) strtol(card_val, (char**)NULL, 10);
    1097                 break;
    1098       case 'i': *((short *) store) = (short) strtol(card_val, (char**)NULL, 10);
    1099                 break;
    1100       case 'U': *((unsigned int *) store) = (unsigned int) strtoul(card_val, (char**)NULL, 10);
    1101                 break;
    1102       case 'u': *((unsigned short *) store) = (unsigned short) strtoul(card_val, (char**)NULL, 10);
    1103                 break;
    1104       case 'f': *((float *) store) = atof(card_val);
    1105                 break;
    1106       case 'd': *((double *) store) = atof(card_val);
    1107                 break;
    1108       case 's': sprintf((char *) store,"%s",card_val);
    1109                 break;
    1110       case 'c': *((char *) store) = card_val[0];
    1111                 break;
    1112       default:  fprintf(stderr,"WARNING: ReadCard: unknown type: %c\n", type);
    1113                 return -2;
    1114       }
    1115       return 0; // Found card name
    1116     }
    1117   }
    1118   fprintf(stderr,"WARNING: ReadCard: card: %s not found\n", card_flag);
    1119   return -1;
     1122    }
     1123    return true;  // Finished reading data for card name
     1124   
     1125  }
     1126  fprintf(stderr,"Warning: Configuration value %s not found\n", card_flag);
     1127  return false;
    11201128}
    11211129
  • drsdaq/DAQReadout.h

    r51 r55  
    3636    unsigned int CmdNumber;
    3737    FILE *Logfile;   
    38     bool CmdFromSocket;         // Current command issued via socket
    3938    void PrintUsage();
    4039    bool CalibrationRead;
     
    5251    // Configuration data
    5352    char fLogFile[MAX_PATH];
     53    unsigned int fMaxLogLines;
    5454    char fRawDataPath[MAX_PATH];
    5555    int fMinDiskSpaceMB;   // Minimum required disk space in MBytes
     
    6464   
    6565    // Status variables   
    66     int NParam;                 // Number of parameters
     66    int NParam;                 // Number of command parameters
    6767    const char *Param[MAX_NUM_TOKEN]; // Pointers to parameters
     68    bool CmdFromSocket;         // Current command issued via socket
    6869    int NumBoards;
    6970    int FirstBoard;
     
    104105    void cmd_fresponse(); void cmd_fconfig();
    105106
    106     int CommandControl(char*, bool); 
     107    void CommandControl(char*); 
    107108    void StartDRS();
    108109    void StopDRS();
     
    133134int ParseInput(char*, const char *Param[]);
    134135int CheckDisk(char*);
    135 int ReadCard(const char *, void *, char, FILE *);
     136bool ReadCard(const char *, void *, char, FILE *, unsigned int=1);
    136137
    137138#endif
  • drsdaq/HVFeedback.cc

    r49 r55  
    3131  snprintf(Filename,sizeof(Filename),"%s/SlowData/", m->fRawDataPath);
    3232  SlowDataClass = new SlowData(m, "HVFB", Filename);
     33
     34  // Initialise with zero content
     35  Average    = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
     36  Sigma      = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
     37  Response   = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
     38  Target     = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
     39  Buffer     = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels](); 
    3340
    3441  // Read configuration file
     
    5158    ReadCard("HVControlPort",       &fHVControlPort,     'I', File);
    5259    ReadCard("MaxCmdAckDelay",      &fMaxCmdAckDelay,    'I', File);
     60    ReadCard("DefaultResponse",      Response,           'f', File, m->NumBoards*kNumberOfChips*kNumberOfChannels);
    5361    fclose(File);
    5462  }
    5563  PrintConfig();
    56 
    57   // Initialise with zero content
    58   Average    = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
    59   Sigma      = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
    60   Response   = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
    61   Target     = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels]();
    62   Buffer     = new float [m->NumBoards][kNumberOfChips][kNumberOfChannels](); 
    63 
     64 
     65  // Initial state
    6466  Gain = 1;
    6567  SetFBMode(FB_Off);
  • drsdaq/History.txt

    r51 r55  
    9926/3/2009   Added PixelMap translation to communication with HV control.
    101030/3/2009   Added feedback gain. Checked into repository.
    11 1/4/2009    Changes many 'char *' to 'const char *' to honour compiler warning
     111/4/2009    Changed many 'char *' to 'const char *' to honour compiler warning
    1212            on ihp-pc26 (newer version of g++ than on eth-vme02)
    13 2/4/2009    Intoduced check for magic number in RawDataCTX.cc
     132/4/2009    Introduced check for magic number in RawDataCTX.cc
    14143/4/2009    Added frequency and scale factor transmission to socket protocol
    15156/4/2009    Allowed reading of data via read command also while DAQ is active.
     
    303028/5/2009   Replaced NCMCBoards by NBoards for clarity. Added line editing
    3131            capability with the readline library.
     3229/5/2009   Number of lines in log file limited to configurable number.
     334/6/2009    Config file can contain array of numbers. Reading of config file now
     34            safe for buffer overflow. RawDataCTX class take the possibility of
     35            varying event data size into account.
  • drsdaq/RawDataCTX.cc

    r49 r55  
    105105      fprintf(fptr, "*** Board %d ***\n", i);
    106106      fprintf(fptr, "Serial number:            %d\n", BStruct[i].SerialNo);
    107       fprintf(fptr, "Sampling frequency [GHz]: %.3f\n", BStruct[i].NomFreq);
    108       fprintf(fptr, "Temperature [C]:          %.3f\n", BStruct[i].BoardTemp); 
     107      fprintf(fptr, "Sampling frequency:       %.3f GHz\n", BStruct[i].NomFreq);
     108      fprintf(fptr, "Temperature:              %.3f deg C\n", BStruct[i].BoardTemp); 
    109109      fprintf(fptr, "Scale factor:             %.3f\n", BStruct[i].ScaleFactor);
    110110    }     
     
    138138  }
    139139 
    140 /*   // Move file pointer to desired event header (if zero read next event)
    141   if(EventNo!=0 && fseek(Rawfile, RHeader->RunHeaderSize+RHeader->BoardStructureSize*RHeader->NBoards+(EventNo-1)*(RHeader->EventHeaderSize+RHeader->NBoards*RHeader->NChips*
    142       RHeader->NChannels*RHeader->Samples*sizeof(short)), SEEK_SET)!=0) {
     140  // Move file pointer to desired event header (if zero read next event)
     141  bool SEEK_OK=true;
     142  if (EventNo!=0 && fseek(Rawfile, RHeader->RunHeaderSize+RHeader->BoardStructureSize*RHeader->NBoards, SEEK_SET) != 0) SEEK_OK=false;
     143  while(SEEK_OK) {
     144    if (fread(EHeader, RHeader->EventHeaderSize, 1, Rawfile) != 1) {
     145      if (feof(Rawfile)==0) {
     146        if (!Silent) printf("Error: Could not read event header\n");
     147        return CTX_EHEADER;
     148      }
     149      else return CTX_EOF; 
     150    }
     151    else {
     152      if (EHeader->EventNumber==EventNo || EventNo==0) break;
     153      if (fseek(Rawfile, EHeader->EventSize, SEEK_CUR) != 0) SEEK_OK = false;
     154    }
     155  }
     156  if(!SEEK_OK) {
    143157    if(!Silent) printf("Error: Could not move to requested event\n");
    144158    return CTX_SEEK;
    145159  }
    146  */
    147160
    148   // Move file pointer to desired event header (if zero read next event)
    149   if (EventNo != 0) {
    150     bool SEEK_OK=true;
    151     if (fseek(Rawfile, RHeader->RunHeaderSize+RHeader->BoardStructureSize*RHeader->NBoards, SEEK_SET) != 0) SEEK_OK=false;
    152     while(SEEK_OK) {
    153       if (fread(EHeader, RHeader->EventHeaderSize, 1, Rawfile) != 0) SEEK_OK = false;
    154       else {
    155         if (EHeader->EventNumber==EventNo) break;
    156         if (fseek(Rawfile, EHeader->EventSize, SEEK_CUR) != 0) SEEK_OK = false;
    157       }
    158     }
    159     if(!SEEK_OK) {
    160       if(!Silent) printf("Error: Could not move to requested event\n");
    161       return CTX_SEEK;
    162     }
    163   }
    164   // Read event header
    165   else if (fread(EHeader, RHeader->EventHeaderSize, 1, Rawfile) != 1) {
    166     if (feof(Rawfile)==0) {
    167       if (!Silent) printf("Error: Could not read event header\n");
    168       return CTX_EHEADER;
    169     }
    170     else return CTX_EOF; 
    171   }
     161  // Read event data
    172162  if(fread(Data, 1, EHeader->EventSize, Rawfile) != EHeader->EventSize) {
    173163    if(!Silent) printf("Error: Could not read (all) event data\n");
     
    191181  return FileOpen;
    192182}
    193 
  • drsdaq/drsdaq.cpp

    r51 r55  
    142142
    143143    strftime(Buf,MAX_COM_SIZE, "%d/%m/%y %X", localtime(&(Time=time(NULL))));
    144     m->PrintMessage(MsgToLog, "CONSOLE(%s)> %s", Buf, Command);
    145 
     144    m->PrintMessage(MsgToLog, "CONSOLE(%s)> %s\n", Buf, Command);
     145
     146    // Process command
    146147    pthread_mutex_lock(&m->control_mutex);
    147     m->CommandControl(Command, false);
     148    m->CommandControl(Command);
    148149    pthread_mutex_unlock(&m->control_mutex);
    149150
     
    224225        break;
    225226      }
     227      if (Command[strlen(Command)-1]=='\n') Command[strlen(Command)-1]='\0';  // Remove trailing newline
    226228     
    227       strftime(Buf,MAX_COM_SIZE,"%d/%m/%y %X", localtime(&(Time=time(NULL))));
    228       m->PrintMessage(MsgToLog,"SOCKET(%s)> %s%s",Buf, Command, Command[strlen(Command)-1]=='\n' ? "":"\n");
    229       m->PrintMessage(MsgToConsole,"SOCKET> %s%s",Command, Command[strlen(Command)-1]=='\n' ? "":"\n");
    230      
    231       pthread_mutex_lock(&m->control_mutex);       
    232       m->CommandControl(Command, true); // Process CC command
     229      strftime(Buf, MAX_COM_SIZE, "%d/%m/%y %X", localtime(&(Time=time(NULL))));
     230      m->PrintMessage(MsgToConsole|MsgToLog, "SOCKET(%s)> %s\n", Buf, Command);
     231
     232      // Process command     
     233      pthread_mutex_lock(&m->control_mutex);
     234      m->CmdFromSocket = true;       
     235      m->CommandControl(Command);
     236      m->CmdFromSocket = false; 
    233237      pthread_mutex_unlock(&m->control_mutex);
    234238    }
Note: See TracChangeset for help on using the changeset viewer.