fdhwlib  2.0.25
/home/kopmann/git-mirror/fdhwlib/fdhwlib/Hw/eventloop.h
Go to the documentation of this file.
00001 /***************************************************************************
00002                           eventloop.h  -  description
00003                              -------------------
00004     begin                : Di Jun 20 2006
00005     copyright            : (C) 2006 by 
00006     email                : 
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #ifndef EVENTLOOP_H
00019 #define EVENTLOOP_H
00020 
00021 #include <fdhwlib.h>
00022 
00023 
00024 #include <cstdio>
00025 #include <string>
00026 
00027 #ifdef USE_MYSQL
00028 #include <mysql/mysql.h>
00029 #endif // of USE_MYSQL
00030 
00031 
00032 #define PAGE_STACK_LEN 100 // Must be larger than the physical available number
00033                            // of pages.
00034 
00035 #include <Hw/SltTime.h>                           
00036 #include <Hw/sltpagestatus.h>
00037 
00038 #include <akutil/simpleserver.h>
00039 
00040 
00041 class keyboard;
00042 class sharedMemory;
00043 class SimpleSocket;
00044 class Slt;
00045 
00150 class EventLoop : public SimpleServer {
00151 public:
00153   EventLoop(Slt *slt, unsigned short port = 0);
00154 
00158   //EventLoop(char *host, unsigned short port);
00159   
00160   ~EventLoop();
00161 
00163   void readInifile(char *inifile);
00164 
00165   /* Set number of stored pages */
00166   void setNStoredPages(int n);         
00167 
00169   void clearPageBuffer();
00170 
00171   /* Check the if dead time counters are active */
00172   bool isDeadTimeEnabled();
00173 
00175   void clear();
00176   
00178   virtual void initRun(int n=0);
00179 
00181   virtual int stopRun();
00182 
00187   void startServer(FILE *fout);
00188 
00191   void stopServer();
00192   
00194   int handle_timeout();
00195 
00198   int read_from_keyboard();
00199   
00215   void executeCmd(int client, short cmd, unsigned long *arg, short n);
00216     
00222   void run(FILE *fout, int n=0);
00223 
00225   void handleKeyboard(FILE *fout, keyboard *kb);  
00226 
00228   virtual void keyboard_cmds(FILE *fout, char *line);
00229   
00231   virtual void displayHeader(FILE *fout);
00232 
00245   virtual int getEvent(FILE *fout);
00246 
00248   virtual int getEventData(FILE *fout);
00249 
00251   void freeMemory();
00252 
00254   virtual void displayEvent(FILE *fout);
00255 
00257   virtual void displaySummary(FILE *fout);
00258 
00260   void analyse();
00261 
00263   virtual void help(FILE *fout);
00264 
00268   //void fprintf(FILE *fout, const char *fmt, ...);
00269 
00270   void print(const char *fmt, ...);
00271   
00272     
00273 #ifdef USE_MYSQL
00274 
00275   void enableDatabase();
00276 
00278   void disableDatabase();
00279 
00281   void setPassword(char *passwd);
00282   
00284   int openDatabase(FILE *fout);
00285 
00287   void closeDatabase();
00288 
00291   void setDatabase(EventLoop *loop);  
00292   
00294   void displayDatabase(FILE *fout);
00295 
00297   void displayRuns(FILE *fout, int nStart=0, int nRow=0);
00298 
00305   void displayEventsFromDb(FILE *fout, int runId, char *param = 0);
00306 
00308   void storeRunHeader(FILE *fout);
00309 
00311   void updateRunHeader(FILE *fout);
00312 
00314   virtual void storeConfig();
00315 
00317   virtual void storeEvent();
00318 
00320   void setRunDuration(int sec);
00321 
00323   int getRunDuration();  
00324 
00326   int getActualRunDuration();
00327     
00328 #endif // of MYSQL
00329     
00330 protected:
00333   //int isClient;
00334 
00336   //SimpleSocket *readout;
00337 
00340   fd_set raw_data_fd_set;
00341   
00343   int runDaemon;
00344 
00349   FILE *floop;  
00350   
00352   int numEvents;
00353 
00355   int maxEvents;
00356 
00358   int filledPage[PAGE_STACK_LEN];
00359 
00361   int nextFreeSlot;
00362 
00364   int nextPage;
00365 
00367   int nextPageToFree;
00368 
00370   SltPageStatus lastPageStatus;
00371 
00373   unsigned long sumDelay;
00374 
00376   unsigned long sumPagesLeft;
00377 
00380   unsigned long sumStopped;
00381 
00384   sharedMemory *eventBuffer;
00385   
00386 
00387   // Eventloop control switches
00388   bool evlStopOnError;        // Flag wether to stop on errors seen in the diagnostics
00389   int evlStoredPages;         // Keep a number of pages in memory without clearing
00390   int evlSampleTime;          // Sample time [ms]
00391   bool evlRateAnalysis;       // Calculate trigger rate ...
00392   bool evlLoadAnalysis;       // Display eventloop load parameter
00393   bool evlHistogramming;      // Enable histogramming
00394 
00395   float evlRateUpdateFactor;  // Update factor for sliding rate calculation
00396 
00397   std::string evlGuiHost;          // Host where the eventloop GUI is running
00398   int evlGuiPort;             // Port of the GUI
00399 
00400   // Histogram data collected during the event loop
00401   int hisCol;                 // selected column
00402   int hisPix;                 // selected pixel
00403 
00404   int hisN[24];               // number of samples
00405   float hisPedestal[24];      // Pedestal
00406   float hisPeak[24];          // Peak
00407   float hisBin[24];           // Bin of the peak
00408   float hisRelPeak[24];       // Relative peak height
00409                               // pedestal = 0% ... mean peak = 100%
00410   float hisNorm[24];          // calibration factor
00411   float hisStaticNorm[24];    // static calibration factor
00412                               // has to be loaded from the ini file
00413   bool hisUseStaticNorm;      // use the static calibration data
00414 
00415   int hisLight[100];          // distribution of light level
00416   int hisImpact[100];         // distribution of center of gravity
00417 
00418   
00419 protected:
00420   char lastKey;  // Last key typed at the keyboard
00421   int i, j, n, err, page, offset, nReadout, parameter;
00422   int statOffset;
00423   bool running;
00424   bool first;
00425   bool firstTime;
00426   bool slt;
00427   bool tlt;
00428   bool analysis;
00429   bool adc;
00430   //bool clear;
00431   bool paused;
00432   bool hwStopped;
00433   bool syntilator;
00434   SltPageStatus pageStatus;
00435   SltPageStatus mask;
00436   SltTime actTime;
00437   SltTime timeStamp;
00438   SltTime startTime;
00439   SltTime stopTime;
00440   SltTime firstEvTime;
00441   SltTime lastTimeStamp;
00442   SltTime deltaTimeStamp;
00443   SltTime deltaTime;
00444   SltTime delayTime;
00445   unsigned long delay; // [us]
00446   float meanDelay;
00447   unsigned long realTime, realFirst;
00449   SltTime timeAct, timeLast, timeFirst;
00450   float actRate, meanRate, meanPagesLeft, meanStopped, realRate;
00451   float slidingRate;
00452   float slidingFactor;
00453   float diffTime;
00454   int firstEvent;
00455   int trigAddr;
00456   bool stopped;
00457   int lastHeader;
00458   unsigned long nSamples;
00459   float pedestal[20][22];
00460   //procDuration dur;
00461   //const char *source[] = {"0", "Sw", "R", "3", "L", "5", "6", "7", "Int",
00462   //                 "9", "10", "11", "12", "13", "14", "15", "Ext"};
00463   SimpleSocket *gui;
00464   int numStoredEvents;
00465 
00466 
00467   // Dead time counters
00468   bool checkDeadTime;
00469   unsigned long long deadTimeRef, deadTime, deadTimeLast, deadTimeEnd;
00470   //unsigned long long vetoTimeRef, vetoTime;
00471 
00472   //EventBase ev;
00473 
00474   // Pointer to the Slt
00475   Slt *theSlt;
00476 
00477   //
00478   // Database parameter
00479   //
00480 #ifdef USE_MYSQL
00481 
00483   bool dbRecord;
00484 
00486   bool dbRecordPixel;
00487 
00489   std::string dbServer;
00490 
00492   std::string dbUser;
00493 
00495   std::string dbPassword;
00496 
00498   std::string dbName;
00499 
00501   std::string dbRunTable;
00502 
00504   std::string dbEventTable;
00505 
00507   std::string dbAdcTable;
00508 
00510   unsigned long dbRunId;
00511 
00513   unsigned long dbEventId;
00514   
00516   MYSQL *db;
00517 
00519   int runDuration;
00520   
00521 #endif // of USE_MYSQL
00522 
00523 };
00524 
00525 #endif