Changeset 254 for drsdaq


Ignore:
Timestamp:
07/20/10 13:20:47 (14 years ago)
Author:
ogrimm
Message:
Replaced mutex by Lock()/Unlock() from Evidence class, added feedback state service
Location:
drsdaq
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • drsdaq/DAQReadout.cc

    r230 r254  
    5757
    5858// Global pointer for thread entry routines
    59 class DAQReadout *This;
     59class DAQReadout *ThisClass;
    6060
    6161// -----------------------------------------------
     
    6767
    6868  // Initialization
    69   This = this;
    70   MainThread = getpid();
     69  ThisClass = this;
     70  MainThread = pthread_self();
    7171  ConsoleText = NULL;
    7272  time(&StartTime);
     
    7474  // DIM console service used in PrintMessage()
    7575  ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
    76 
    77   // Initialize mutex for thread synchronisation
    78   if (pthread_mutex_init(&Mutex, NULL) != 0) {
    79     Message(FATAL, "pthread_mutex_init() failed");
    80   }
    8176
    8277  // Initialize status structure
     
    169164  delete ConsoleOut;
    170165  free(ConsoleText);
    171  
    172   if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");
    173166}
    174167
     
    202195  else if(CommandList[Count].NeedNotBusy && NumBoards==0) PrintMessage("No boards available\n");
    203196  else {
    204         int Ret;
    205 
    206         // Lock (Execute() runs in thread spawned by commandHandler())
    207         if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
    208           Message(FATAL, "pthread_mutex_lock() failed (%s)", strerror(Ret));
    209         }
    210         // Run command
    211197        CmdNumber = Count;
    212198        (this->*CommandList[CmdNumber].CommandPointer)();
    213         // Unlock
    214         if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
    215           Message(FATAL, "pthread_mutex_unlock() failed (%s)", strerror(Ret));
    216         }
    217199  }
    218200}
     
    238220// Print DAQ configuration
    239221void DAQReadout::cmd_config() {
    240   PrintConfig(MsgToConsole);
     222  PrintConfig();
    241223}
    242224
     
    411393  }
    412394
    413   PrintMessage(MsgToConsole, "==START== %d %.2f %.2f ",kNumberOfBins+2,DRSFreq[Board],GetBoard(Board)->GetPrecision());
     395  PrintMessage("==START== %d %.2f %.2f ",kNumberOfBins+2,DRSFreq[Board],GetBoard(Board)->GetPrecision());
    414396  for (int k=0; k<kNumberOfBins; k++) {
    415     PrintMessage(MsgToConsole, "%.1f ", (float) WaveForm[Board][Chip][Channel][k]);
    416   }
    417   PrintMessage(MsgToConsole, "==END==");
    418   PrintMessage(MsgToConsole, "\n");
    419   PrintMessage(MsgToConsole, "Trigger cell: %d\n", TriggerCell[Board][Chip]); 
     397    PrintMessage("%.1f ", (float) WaveForm[Board][Chip][Channel][k]);
     398  }
     399  PrintMessage("==END==");
     400  PrintMessage("\n");
     401  PrintMessage("Trigger cell: %d\n", TriggerCell[Board][Chip]); 
    420402}
    421403
     
    757739  for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
    758740    snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters);
    759     PrintMessage(MsgToConsole, "%-28s%s\n", Buffer, CommandList[i].Help);
     741    PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help);
    760742  }     
    761   PrintMessage(MsgToConsole,".<command>                  Execute shell command\n\n"
     743  PrintMessage(".<command>                  Execute shell command\n\n"
    762744   "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n"
    763745   "Test data can also be written if no DRS boards are available.\n"
     
    770752
    771753  if (daq_state == active) PrintMessage("Issue 'stop' first to stop daq\n");
    772   else kill(MainThread, SIGTERM); 
     754  else pthread_kill(MainThread, SIGTERM); 
    773755}
    774756
     
    777759
    778760  if(Match(Param[1],"off")) HVFB->SetFBMode(FB_Off);
    779   if(Match(Param[1],"active")) HVFB->SetFBMode(FB_Active);
    780   if(Match(Param[1],"targets")) HVFB->SetFBMode(FB_Targets);
    781   HVFB->GetFBMode();
     761  else if(Match(Param[1],"active")) HVFB->SetFBMode(FB_Active);
     762  else if(Match(Param[1],"targets")) HVFB->SetFBMode(FB_Targets);
     763  else HVFB->GetFBMode();
    782764}
    783765
     
    823805void DAQReadout::cmd_fconfig() {
    824806
    825   HVFB->PrintConfig(MsgToConsole);
     807  HVFB->PrintConfig();
    826808}
    827809
     
    11751157
    11761158// Print configuration to target
    1177 void DAQReadout::PrintConfig(int Target) {
    1178   PrintMessage(Target, "RawDataPath: %s\n"
     1159void DAQReadout::PrintConfig() {
     1160  PrintMessage("RawDataPath: %s\n"
    11791161               "DefaultFrequency: %.2f\tFirstSample: %d\tSamples: %u\n"
    11801162               "MinDiskSpaceMB: %u\tMaxFileSizeMB: %d\n"
     
    11911173// Print progress (used in DRS class)
    11921174void DAQReadout::Progress(int Progress) {
    1193   PrintMessage(MsgToConsole, "\rProgress: %d%%              ", Progress);
     1175  PrintMessage("\rProgress: %d%%              ", Progress);
    11941176  fflush(stdout);
    11951177};
    1196  
    1197 // Print message to selected target
    1198 void DAQReadout::PrintMessage(int Target, const char *Format, ...) {
    1199 
    1200   va_list ArgumentPointer;
    1201   va_start(ArgumentPointer, Format);
    1202   DoPrintMessage(Format, ArgumentPointer, Target);
    1203   va_end(ArgumentPointer);
    1204 }
    12051178
    12061179// Print message to console only
    12071180void DAQReadout::PrintMessage(const char *Format, ...) {
     1181
     1182  static char Error[] = "vasprintf() failed in PrintMessage()";
     1183  char *Text;
     1184
     1185  // Evaluate arguments
    12081186  va_list ArgumentPointer;
    12091187  va_start(ArgumentPointer, Format);
    1210   DoPrintMessage(Format, ArgumentPointer, MsgToConsole);
     1188  if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
    12111189  va_end(ArgumentPointer);
    1212 }
    1213 
    1214 // Function doing the actual printing work
    1215 // Note: Be careful when overloading variadic functions. va_list is
    1216 // in gcc an int, which can be interpreted as char *...
    1217 void DAQReadout::DoPrintMessage(const char *Format, va_list ArgumentPointer, int Target) {
    1218 
    1219   static char Error[] = "vasprintf() failed in DoPrintMessage()";
    1220   char *Text;
    1221 
    1222   // Evaluate arguments   
    1223   if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
    12241190 
    12251191  // Print to console
    1226   if(Target & MsgToConsole) {
    1227     if(strlen(Text)>0 && Text[strlen(Text)-1]=='\n') printf("\r%s%s", Text, Prompt);  // New prompt
    1228     else printf("%s", Text);
    1229         fflush(stdout);
    1230   }
    1231  
    1232   // Send to DIM console service and to log if requested
    1233   ConsoleOut->updateService(Text);
    1234   if(Target & MsgToLog) SendToLog("%s %s", SERVER_NAME, Text);
     1192  if(strlen(Text)>0 && Text[strlen(Text)-1]=='\n') printf("\r%s%s", Text, Prompt); // New prompt
     1193  else printf("%s", Text);
     1194  fflush(stdout);
     1195
     1196  // Send to DIM text service
     1197  ConsoleOut->updateService(Text);
    12351198
    12361199  // Free old text
     
    12461209          *((char *) getCommand()->getData()+getCommand()->getSize()-1) != '\0' ||
    12471210          strlen(getCommand()->getString()) == 0) return;
    1248 
    1249   // Log command reception
    1250   SendToLog("Command '%s' from %s (ID %d)", getCommand()->getString(), getClientName(), getClientId());
    12511211
    12521212  // Copy command to new buffer (will be freed by global Execute() function)
     
    12631223    PrintMessage("pthread_create() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    12641224  }
    1265   else {
    1266     if ((Code = pthread_detach(Thread)) != 0) {
    1267           PrintMessage("pthread_detach() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    1268         }
     1225  else if ((Code = pthread_detach(Thread)) != 0) {
     1226        PrintMessage("pthread_detach() failed in DRSReadout::commandHandler() (%s)\n", strerror(Code));
    12691227  } 
    12701228}
     
    14591417void Execute(char *Command) {
    14601418
    1461  This->Execute(Command);
    1462  free(Command);
    1463 }
     1419  ThisClass->Lock();
     1420  ThisClass->Execute(Command);
     1421  free(Command);
     1422  ThisClass->Unlock();
     1423}
  • drsdaq/DAQReadout.h

    r229 r254  
    1212#include <unistd.h>
    1313#include <sys/socket.h>
    14 #include <pthread.h>
    1514#include <fcntl.h>
    1615#include <dirent.h>
    1716#include <sys/vfs.h>
    18 #include <signal.h>
    1917#include <sys/time.h>
    2018
     
    2624#define MAX_COM_SIZE 10000
    2725#define MAX_NUM_TOKEN 10
    28 
    29 #define MsgToConsole 1
    30 #define MsgToLog 2
    3126
    3227enum state_enum {active, stopped};
     
    4742        void commandHandler();
    4843
    49     pthread_mutex_t Mutex;
    5044    int Rawfile;
    5145    class HVFeedback* HVFB;
     
    127121    bool ReadCalibration();
    128122    void ReadCalibratedDRSData();
    129     void PrintConfig(int);
    130     void PrintMessage(int, const char*, ...);
     123    void PrintConfig();
    131124    void PrintMessage(const char*, ...);
    132     void DoPrintMessage(const char*, va_list, int);
    133125    bool OpenRawFile();
    134126    bool WriteRunHeader();
  • drsdaq/HVFeedback.cc

    r229 r254  
    3030
    3131  m = DAQClass;
    32 
    3332  fNumberOfChannels = m->GetBoard(0)->GetNumberOfChannels();
    3433  fNumberOfChips = m->GetBoard(0)->GetNumberOfChips(); 
     
    7776  }
    7877 
    79   PrintConfig(MsgToLog);
    80 
    8178  // Provide DIM services
    82   FeedbackAverage = new DimService (SERVER_NAME"/Average", "F", DIMAverage, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
    83   FeedbackSigma = new DimService (SERVER_NAME"/Sigma", "F", DIMSigma, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
    84   FeedbackResponse = new DimService (SERVER_NAME"/Response", "F", Response, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
    85   FeedbackTarget = new DimService (SERVER_NAME"/Target", "F", Target, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
    86   CountService = new DimService (SERVER_NAME"/Count", Count);
     79  FeedbackAverage = new DimService ("Feedback/Average", "F", DIMAverage, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
     80  FeedbackSigma = new DimService ("Feedback/Sigma", "F", DIMSigma, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
     81  FeedbackResponse = new DimService ("Feedback/Response", "F", Response, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
     82  FeedbackTarget = new DimService ("Feedback/Target", "F", Target, m->NumBoards*kNumberOfChipsMax*kNumberOfChannelsMax*sizeof(float));
     83  CountService = new DimService ("Feedback/Count", Count);
     84  FeedbackState = new DimService ("Feedback/State", "I:1;C", NULL, 0);
    8785
    8886  // Initial state
     
    9896HVFeedback::~HVFeedback() {
    9997
     98  delete FeedbackState;
    10099  delete CountService;
    101100  delete FeedbackAverage;
     
    105104
    106105  delete[] Average;     delete[] Response;
    107   delete[] DIMAverage;          delete[] DIMSigma;
     106  delete[] DIMAverage; delete[] DIMSigma;
    108107  delete[] Sigma;
    109108  delete[] Target;      delete[] Buffer;
     
    159158       
    160159        switch (FBMode) {
    161           case FB_Active:   // Determine correction from response maxtrix and change voltages
     160          case FB_Active:
     161                // Determine correction from response maxtrix and change voltages
    162162            Correction = -(Target[i][j][k] - Average[i][j][k])*Response[i][j][k]*Gain;
    163         if (fabs(Correction) > 0.1) Correction = fabs(Correction)/Correction*0.1;   // Limit changes to 100 mV
     163        // Limit voltage steps
     164                if (fabs(Correction) > 0.1) Correction = fabs(Correction)/Correction*0.1;   // Limit changes to 100 mV
     165                // Check if voltage change command possible
    164166                if(Correction==0 || Target[i][j][k]==0 || PixMap->DRS_to_Pixel(i,j,k).empty()) break;
    165         printf("Average of board %d, chip %d, channel %d is %.2f +/- %.2f    Correction %.3f\n",i,j,k,Average[i][j][k],Sigma[i][j][k],Correction);
    166 
    167                 if(fabs(Average[i][j][k]) < 2*Sigma[i][j][k]) printf("Too noisy!\n");
    168                 else {
     167                // Add voltage change command if not too noisy
     168                if(fabs(Average[i][j][k]) > 2*Sigma[i][j][k]) {
    169169                  Cmd << PixMap->DRS_to_Pixel(i,j,k)+" " << std::showpos << Correction << " ";
    170170                }
     
    184184          case FB_ResponseSecond: // Determine response from signal variation
    185185            if(Buffer[i][j][k] == Average[i][j][k]) {
    186               m->PrintMessage("HV Feedback: Warning, response singular for board %d, chip %d, channel %d.\n",i,j,k);
     186              m->PrintMessage("HV Feedback: Warning, response singular for board %d, chip %d, channel %d\n",i,j,k);
    187187              Response[i][j][k] = 0;
    188188            }
     
    211211    case FB_Targets:
    212212          FeedbackTarget->updateService();
    213       m->PrintMessage("HV Feedback: New targets set, switching off.\n");
     213      m->PrintMessage("HV Feedback: New targets set, switching off\n");
    214214      SetFBMode(FB_Off, true);
    215215      break;
    216216    case FB_ResponseFirst:
    217217      SetFBMode(FB_ResponseSecond, true);
    218       m->PrintMessage("HV Feedback: Increasing voltages by %f for response measurement, acquiring data.\n", DiffVoltage);
     218      m->PrintMessage("HV Feedback: Increasing voltages by %f for response measurement, acquiring data\n", DiffVoltage);
    219219      break;
    220220    case FB_ResponseSecond:
    221221          FeedbackResponse->updateService();
    222       m->PrintMessage("HV Feedback: Response measurements finished, original voltages set, switching off.\n");
     222      m->PrintMessage("HV Feedback: Response measurements finished, original voltages set, switching off\n");
    223223      SetFBMode(FB_Off, true);
    224224      break;
     
    277277void HVFeedback::SetFBMode(FBState Mode, bool Internal) {
    278278  if((Mode==FB_ResponseFirst || Mode==FB_ResponseFirst) && !Internal)
    279       m->PrintMessage("Start reponse measurement by calling MeasureResponse().\n");
     279      m->PrintMessage("Start reponse measurement by calling MeasureResponse()\n");
    280280  else {
    281281    FBMode = Mode;
    282         if (Mode != FB_ResponseFirst) m->Message(m->INFO, "%s", FBState_Description[FBMode]);
    283         else m->Message(m->INFO, "%s (voltage difference %.3f)", FBState_Description[FBMode], DiffVoltage);
     282        if (Mode != FB_ResponseFirst) m->PrintMessage("%s\n", FBState_Description[FBMode]);
     283        else m->PrintMessage("%s (voltage difference %.3f)\n", FBState_Description[FBMode], DiffVoltage);
    284284    ClearAverages();
     285       
     286        // Update state service
     287        State.State = FBMode;
     288        strncpy(State.Text, FBState_Description[FBMode], sizeof(State.Text));
     289        FeedbackState->updateService(&State, sizeof(State));
    285290  }
    286291}
     
    374379// Print feedback configuration
    375380//
    376 void HVFeedback::PrintConfig(int Target) {
    377 
    378   m->PrintMessage(Target, "LedTrigBoard: %d\t\tLedTrigChip: %d\t\tLedTrigChannel: %d\n"
     381void HVFeedback::PrintConfig() {
     382
     383  m->PrintMessage("LedTrigBoard: %d\t\tLedTrigChip: %d\t\tLedTrigChannel: %d\n"
    379384        "LedTrigSample: %d\tLedTrigThreshold: %.2f\n"
    380385        "LedSignalSample: %d\tLedBaselineSample: %d\tDefaultNumAverage: %d\n"
  • drsdaq/HVFeedback.h

    r229 r254  
    3333        DimService *FeedbackTarget;
    3434        DimService *CountService;
     35        DimService *FeedbackState;
     36
     37        struct {
     38      int State;
     39          char Text[BUF_LENGTH];
     40        } State;
    3541
    3642        int NumAverages;        // Events to take before feedback acts
     
    7278    void GetResponse();
    7379    void ClearAverages();
    74     void PrintConfig(int);
     80    void PrintConfig();
    7581};
    7682
  • drsdaq/History.txt

    r211 r254  
    585816/12/2009  Removed automatic gain adaption in feedback (caused spikes in the correction)
    59599/3/2010        Feedback now depended on DIM for communication with bias server.
    60                         Started migration to DRS4 (last tested revision as daqct3 for DRS2 is 161). DRS class will not run anymore with DRS2 FPGA firmware.
     60                        Started migration to DRS4 (last tested revision as daqct3 for DRS2 is 161).
     61                        DRS class will not run anymore with DRS2 FPGA firmware.
    616211/3/2010       Removed SlowData class.                 
    626312/3/2010       Removed local configuration and logging.
     
    676818/5/2010       Rate of event service adjustable with command 'update'.
    686921/5/2010       Fix so that 'exit' command also works as DimCommand.
     7020/7/2010       Replaced mutex with Lock()/Unlock() from Evidence class, introduced DIM feedback state service,
     71                        streamlined PrintMessage().
  • drsdaq/drsdaq.cpp

    r226 r254  
    9191        DimClient::sendCommand(SERVER_NAME"/Command", Command);
    9292    free(Command);
    93   } 
     93  }
    9494}
    9595
     
    9797// Remove lock file before running default signal code
    9898void CrashHandler(int Signal) {
     99
    99100  remove(LOCKFILE);
    100101  printf("Caught signal number %d. Removed lockfile and performing standard signal action. Good luck.\n",Signal);
Note: See TracChangeset for help on using the changeset viewer.