Changeset 92 for drsdaq/DAQReadout.cc


Ignore:
Timestamp:
07/28/09 09:13:41 (15 years ago)
Author:
ogrimm
Message:
Added some configuration parameters. Log file written outside of repository.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • drsdaq/DAQReadout.cc

    r89 r92  
    1212#include "SlowData.h"
    1313
    14 #define TMPNAME "/tmp/__tmp__drsdaq__"
     14#define TMPNAME "/tmp/__tmp__drsdaq__"  // ..for log file truncation
    1515
    1616static const char* daq_state_str[] = {"active", "stopped"};
     
    2626   {"status", &DAQReadout::cmd_status, false, "[daq|drs]", "Show DAQ/DRS status information"},
    2727   {"freq", &DAQReadout::cmd_freq, true, "<GHz> [reg]", "Set DRS sampling frequency (regulated)"},
    28    {"calib", &DAQReadout::cmd_calib, true, "<t_f> <c_f> [dir]", "Response calibration"},
     28   {"calib", &DAQReadout::cmd_calib, true, "<t_f> <c_f>", "Response calibration"},
    2929   {"trigger", &DAQReadout::cmd_trigger, true, "<on|off>", "Hardware trigger on or off"},
    3030   {"delayed", &DAQReadout::cmd_delayed, true, "<on|off>", "Switch delayed start on or off"},
     
    7676  LastBoard          = -1;
    7777  CmdFromSocket      = false;
    78      
    79   // Read configuration file
     78  ConfigOK           = true;
     79 
     80  // Read configuration file (if any item missing, program will be terminated in main() routine)
    8081  FILE *File;
    8182  if ((File = fopen(Configfile,"r")) == NULL) {
    8283    printf("Error: Could not open drsdaq configuration file '%s'\n", Configfile);
     84    ConfigOK = false;
     85    return;
    8386  }
    8487  else {
    8588    printf("Reading drsdaq configuration file %s\n", Configfile);
    86     ReadCard("LogFile",             fLogFile,           's', File);
    87     ReadCard("MaxLogLines",        &fMaxLogLines,       'U', File);
    88     ReadCard("RawDataPath",         fRawDataPath,       's', File);
    89     ReadCard("FirstSample",        &fFirstSample,       'I', File);
    90     ReadCard("Samples",            &fSamples,           'U', File);
    91     ReadCard("MinDiskSpaceMB",     &fMinDiskSpaceMB,    'U', File);
    92     ReadCard("MaxFileSizeMB",      &fMaxFileSizeMB,     'I', File);
    93     ReadCard("CCPort",             &fCCPort,            'I', File);
    94     ReadCard("FirstVMESlot",       &fFirstVMESlot,      'I', File);
    95     ReadCard("LastVMESlot",        &fLastVMESlot,       'I', File);
    96     ReadCard("HVFeedbackConfig",    fHVFeedbackConfig,  's', File);
     89    ConfigOK &= ReadCard("LogFile",             fLogFile,           's', File);
     90    ConfigOK &= ReadCard("SlowDataPath",        fSlowDataPath,      's', File);
     91    ConfigOK &= ReadCard("MaxLogLines",        &fMaxLogLines,       'U', File);
     92    ConfigOK &= ReadCard("RawDataPath",         fRawDataPath,       's', File);
     93    ConfigOK &= ReadCard("FirstSample",        &fFirstSample,       'I', File);
     94    ConfigOK &= ReadCard("Samples",            &fSamples,           'U', File);
     95    ConfigOK &= ReadCard("MinDiskSpaceMB",     &fMinDiskSpaceMB,    'U', File);
     96    ConfigOK &= ReadCard("MaxFileSizeMB",      &fMaxFileSizeMB,     'I', File);
     97    ConfigOK &= ReadCard("CCPort",             &fCCPort,            'I', File);
     98    ConfigOK &= ReadCard("FirstVMESlot",       &fFirstVMESlot,      'I', File);
     99    ConfigOK &= ReadCard("LastVMESlot",        &fLastVMESlot,       'I', File);
     100    ConfigOK &= ReadCard("HVFeedbackConfig",    fHVFeedbackConfig,  's', File);
     101    ConfigOK &= ReadCard("DefaultFrequency",   &fDefaultFrequency , 'd', File);
    97102    fclose(File);
     103    if (!ConfigOK) return;
    98104  }
    99105  if (fFirstSample < 0 || fFirstSample > kNumberOfBins || fSamples > kNumberOfBins) {
     
    108114  if (system(ShellCmd) != 0) printf("Warning: Could not truncate log file '%s' to %u lines\n", fLogFile, fMaxLogLines);
    109115
    110   // Open log file
     116  // Open log file and log configuration
    111117  if ((Logfile = fopen(fLogFile, "a")) == NULL) printf("Warning: Could not open log file '%s'\n", fLogFile);
    112118  PrintMessage(MsgToLog,"********** Logging started **********\n");
    113  
    114   // Print configuration
    115   cmd_config();
     119  PrintConfig(MsgToLog);
    116120
    117121  // Create DRS instance and perform initial scan
     
    151155 
    152156  // Create instance of slow data class for DAQ
    153   char Filename[MAX_PATH];
    154   snprintf(Filename,sizeof(Filename),"%s/SlowData/", fRawDataPath);
    155   SlowDataClass = new SlowData("DAQ", Filename);
     157  SlowDataClass = new SlowData("DAQ", fSlowDataPath);
    156158  if(SlowDataClass->ErrorCode != 0) {
    157159   PrintMessage("Warning: Could not open DAQ slowdata file (%s)\n", strerror(SlowDataClass->ErrorCode));
    158160  }
     161  SlowDataClass->NewEntry("Runinfo-Info", "Run information written after completion or termination of run (Status can be OK or Error): Runnumber Status Runtype Events Files Description ");
    159162}
    160163
     
    170173  delete[] WaveForm;  delete[] TriggerCell;
    171174 
    172   PrintMessage(MsgToLog,"********** Logging ended **********\n\n");
    173175  if(Logfile) {
    174176    if(!fclose(Logfile)) printf("Closing logfile\n");
     
    224226// Print DAQ configuration
    225227void DAQReadout::cmd_config() {
    226   PrintMessage("LogFile: %s\tMaxLogLines: %u\tRawDataPath: %s\n"
    227                "FirstSample: %d\tSamples: %u\n"
    228                "MinDiskSpaceMB: %u\tMaxFileSizeMB: %d\tCCPort: %d\n"
    229                "FirstVMESlot: %d\t\tLastVMESlot: %d\n"
    230                "HVFeedbackConfig: %s\n",
    231     fLogFile,fMaxLogLines,fRawDataPath,fFirstSample,fSamples,fMinDiskSpaceMB,
    232     fMaxFileSizeMB,fCCPort,fFirstVMESlot,fLastVMESlot,fHVFeedbackConfig);
     228  PrintConfig(CmdFromSocket ? MsgToSocket : MsgToConsole);
    233229}
    234230
     
    241237      return;
    242238    }
    243     if (!IsDRSFreqSet()) return;
     239    if (!IsDRSFreqSet()) {
     240      PrintMessage("Setting default frequency\n");
     241      SetDRSFrequency(fDefaultFrequency, false);
     242      CalibrationRead = false;
     243    }
     244   
    244245    if (!CalibrationRead && !ReadCalibration()) {
    245246      PrintMessage("Cannot start run if response calibration not read.\n");
     
    445446// Do internal calibration
    446447void DAQReadout::cmd_calib() {
    447   if (NParam==4 && atof(Param[1]) && atof(Param[2]))
    448     CalibrateDRS((char *) Param[3],atof(Param[1]),atof(Param[2]));
    449   else if (NParam==3 && atof(Param[1]) && atof(Param[2]))
    450     CalibrateDRS(NULL,atof(Param[1]),atof(Param[2]));
    451   else PrintUsage();
     448  char str[MAX_COM_SIZE];
     449
     450  if (NParam!=3 || !atof(Param[1]) || !atof(Param[2])) {
     451    PrintUsage();
     452    return;
     453  }
     454     
     455  getcwd(str, sizeof(str));
     456  strcat(str,"/calib");
     457  PrintMessage("Writing calibration data to directory '%s'\n",str);
     458
     459  for (int i=FirstBoard; i<=LastBoard; i++) {
     460    drs->GetBoard(i)->Init();
     461    drs->GetBoard(i)->SetFrequency(atof(Param[2]));
     462    drs->GetBoard(i)->SoftTrigger();
     463
     464    PrintMessage("Creating calibration of board %d (%d)\n", i, drs->GetBoard(i)->GetCMCSerialNumber());
     465
     466    drs->GetBoard(i)->EnableTcal(1);
     467    PrintMessage("Tcal enabled\n");
     468
     469    if (drs->GetBoard(i)->GetChipVersion() == 3) drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,21,0,20,0,0,0,0,0);
     470    else drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,36,110,20,19,40,15,atof(Param[1]),0);
     471    drs->GetBoard(i)->SetCalibrationDirectory(str);
     472
     473    for (int j=0; j<2; j++) {
     474      drs->GetBoard(i)->GetResponseCalibration()->ResetCalibration();
     475      PrintMessage("Calibration reset done.\n");
     476
     477      while (!drs->GetBoard(i)->GetResponseCalibration()->RecordCalibrationPoints(j)) {}
     478      PrintMessage("Record calibration points done.\n");
     479      while (!drs->GetBoard(i)->GetResponseCalibration()->FitCalibrationPoints(j)) {}
     480      PrintMessage("Calibration points fitted.\n");
     481      while (!drs->GetBoard(i)->GetResponseCalibration()->OffsetCalibration(j)) {}
     482      PrintMessage("Offset calibration done.\n");
     483
     484      if (!drs->GetBoard(i)->GetResponseCalibration()->WriteCalibration(j)) break;
     485    }       
     486    drs->GetBoard(i)->Init(); // Reset linear range -0.2 ... 0.8 V
     487  } // Loop over boards
    452488}
    453489
     
    650686// Print feedback configuration
    651687void DAQReadout::cmd_fconfig() {
    652   HVFB->PrintConfig();
     688  HVFB->PrintConfig(CmdFromSocket ? MsgToSocket : MsgToConsole);
    653689}
    654690
     
    799835}
    800836
    801 // Do internal calibration
    802 void DAQReadout::CalibrateDRS(char *dir, double trigfreq, double calibfreq) {
    803 
    804   int i,j;
    805   char str[MAX_COM_SIZE];
    806   DIR *pdir;
    807      
    808   if (NumBoards) {
    809     if(dir!=NULL) {
    810       if ((pdir=opendir(str))==0){
    811         PrintMessage("Error: target directory \"%s\" does not exist!\n",str);
    812         return;
    813       }
    814       closedir(pdir);
    815       snprintf(str,sizeof(str),"%s",dir);
    816       PrintMessage("Target: \"%s\"\n",str);
    817     }
    818     else {
    819       getcwd(str, sizeof(str));
    820       strcat(str,"/calib");
    821       PrintMessage("Taking default target: \"%s/\"\n",str);
    822     }
    823        
    824     for (i=FirstBoard; i<=LastBoard; i++) {
    825       drs->GetBoard(i)->Init();
    826       drs->GetBoard(i)->SetFrequency(calibfreq);
    827       drs->GetBoard(i)->SoftTrigger();
    828            
    829       PrintMessage("Creating calibration of board %d\n", drs->GetBoard(i)->GetCMCSerialNumber());
    830 
    831       drs->GetBoard(i)->EnableTcal(1);
    832       PrintMessage("Tcal enabled");
    833 
    834       if (drs->GetBoard(i)->GetChipVersion() == 3)
    835         drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,21,0,20,0,0,0,0,0);
    836       else
    837         drs->GetBoard(i)->GetResponseCalibration()->SetCalibrationParameters(1,36,110,20,19,40,15,trigfreq,0);
    838            
    839       drs->GetBoard(i)->SetCalibrationDirectory(str);
    840       PrintMessage("Storage directory \"%s\"\n",str);
    841 
    842       for (j=0;j<2;j++) {
    843         drs->GetBoard(i)->GetResponseCalibration()->ResetCalibration();
    844         PrintMessage("Calibration reset done.\n");
    845 
    846         while (!drs->GetBoard(i)->GetResponseCalibration()->RecordCalibrationPoints(j)) {}
    847         PrintMessage("Record calibration points done.\n");
    848         while (!drs->GetBoard(i)->GetResponseCalibration()->FitCalibrationPoints(j)) {}
    849         PrintMessage("Calibration points fitted.\n");
    850         while (!drs->GetBoard(i)->GetResponseCalibration()->OffsetCalibration(j)) {}
    851         PrintMessage("Offset calibration done.\n");
    852 
    853         if (!drs->GetBoard(i)->GetResponseCalibration()->WriteCalibration(j))
    854           break;
    855       }       
    856       drs->GetBoard(i)->Init(); // Reset linear range -0.2 ... 0.8 V
    857     } // Loop over boards   
    858   }
    859   else PrintMessage("No DRS boards available\n");
    860 }
    861 
    862837// Check if DRS is sampling
    863838bool DAQReadout::IsDRSBusy() {
     
    871846bool DAQReadout::IsDRSFreqSet() {
    872847
    873   for (int i=FirstBoard;i<=LastBoard;i++)
     848  for (int i=FirstBoard; i<=LastBoard; i++)
    874849    if (DRSFreq[i]==0) {
    875850      PrintMessage("DRS sampling frequency of board %d not set!\n",i);
     
    992967}
    993968
    994 // Write event header
    995 bool DAQReadout::WriteEventHeader() {
    996 
     969// Write event
     970bool DAQReadout::WriteEvent() {
     971
     972  // Event header
    997973  struct timeval Time;
    998974
     
    1000976 
    1001977  EHeader->EventNumber = NumEvents;
    1002   EHeader->TriggerType = (daq_runtype == data) ? 1 : 0;
     978  EHeader->TriggerType = daq_runtype==data ? 0 : 1;
    1003979  EHeader->Second = Time.tv_sec;
    1004980  EHeader->Microsecond = Time.tv_usec;
     
    1010986    return false;
    1011987  }
    1012   return true;
    1013 }
    1014 
    1015 // Write event data (It is required that at least three chunks can be written with writev(), therefore
    1016 // IOV_MAX>=3 is checked at startup
    1017 bool DAQReadout::WriteEventData() {
    1018 
     988 
     989  // Event data (It is required that at least three chunks can be written with writev(), therefore
     990  // IOV_MAX>=3 is checked at startup
     991 
    1019992  unsigned int Start, Count = 0;
    1020993  ssize_t WriteResult, Size = 0;
     
    1022995
    1023996  // First chunk: trigger cells
    1024   DataPart[Count].iov_base = TriggerCell + FirstBoard*kNumberOfChips;
     997  DataPart[Count].iov_base = (char *) TriggerCell + FirstBoard*kNumberOfChips*sizeof(int); // TriggerCell is without cast a pointer to an 8-byte unit (two ints) !
    1025998  DataPart[Count++].iov_len = RHeader->NBoards * kNumberOfChips * sizeof(int);
    1026999  Size += DataPart[Count-1].iov_len;
     
    10541027    } // Chips
    10551028  } // Boards
     1029 
    10561030  return true;
     1031}
     1032
     1033// Print configuration to target
     1034void DAQReadout::PrintConfig(int Target) {
     1035  PrintMessage(Target, "LogFile: %s\tMaxLogLines: %u\tRawDataPath: %s\n"
     1036               "DefaultFrequency: %.2f\tFirstSample: %d\tSamples: %u\n"
     1037               "MinDiskSpaceMB: %u\tMaxFileSizeMB: %d\tCCPort: %d\n"
     1038               "FirstVMESlot: %d\t\tLastVMESlot: %d\n"
     1039               "SlowDataPath: %s\tHVFeedbackConfig: %s\n",
     1040    fLogFile,fMaxLogLines,fRawDataPath,fDefaultFrequency,fFirstSample,fSamples,fMinDiskSpaceMB,
     1041    fMaxFileSizeMB,fCCPort,fFirstVMESlot,fLastVMESlot,fSlowDataPath,fHVFeedbackConfig);
    10571042}
    10581043
     
    12381223      else if (m->daq_runtype == pedestal) m->StopDRS();   // ..or for software trigger
    12391224
    1240       EventsInFile++;      m->NumEvents++;     
    1241       WriteError |= !m->WriteEventHeader();
    1242 
    12431225      // Read event data via VME or generate test data (for one board if no boards available)
    12441226      if (m->daq_runtype != test) {
     
    12521234      }
    12531235
    1254       // Write data to disk and update file size
    1255       WriteError |= !m->WriteEventData();
     1236      // Write event to disk and update file size
     1237      EventsInFile++;      m->NumEvents++;     
     1238      WriteError |= !m->WriteEvent();
    12561239
    12571240      if((FileSize = lseek(m->Rawfile, 0, SEEK_CUR)) == -1) {
     
    12731256  } while((m->NumEvents<m->NumEventsRequested || m->NumEventsRequested==0) && !m->Stop && !WriteError);
    12741257
    1275   // Print run summary to slow data file and to screen
    12761258  m->StopDRS();
    1277   if(!WriteError) {
    1278     m->PrintMessage("\rRun #%d (%s) %s, %d events\n",m->RunNumber,daq_runtype_str[m->daq_runtype],(m->NumEvents == m->NumEventsRequested) ? "completed":"stopped",m->NumEvents);
    1279     m->SlowDataClass->NewEntry("Runinfo");
    1280     m->SlowDataClass->AddToEntry("%d %s %s %d %d", m->RunNumber, daq_runtype_str[m->daq_runtype], m->RHeader->Description, m->NumEvents, m->FileNumber);
    1281     if(m->SlowDataClass->ErrorCode != 0) {
    1282       m->PrintMessage("Error, could not write DAQ data to file (%s), file closed\n", strerror(m->SlowDataClass->ErrorCode));
    1283     }
    1284 
    1285   }
     1259 
     1260  // Print run summary to screen
     1261  if(!WriteError) m->PrintMessage("\rRun #%d (%s) %s, %d events\n",m->RunNumber,daq_runtype_str[m->daq_runtype],(m->NumEvents == m->NumEventsRequested) ? "completed":"stopped",m->NumEvents);
    12861262  else m->PrintMessage("\rRun #%d (%s) aborted due to error after %d events\n",m->RunNumber,daq_runtype_str[m->daq_runtype],m->NumEvents);
     1263
     1264  // Write run summary to slow data file
     1265  m->SlowDataClass->NewEntry("Runinfo");
     1266  m->SlowDataClass->AddToEntry("%d %s %s %d %d %s", m->RunNumber, WriteError?"Error":"OK", daq_runtype_str[m->daq_runtype], m->NumEvents, m->FileNumber, m->RHeader->Description);
     1267  if(m->SlowDataClass->ErrorCode != 0) {
     1268    m->PrintMessage("Error, could not write DAQ data to file (%s), file closed\n", strerror(m->SlowDataClass->ErrorCode));
     1269  }
    12871270
    12881271  // Print run statistics
Note: See TracChangeset for help on using the changeset viewer.