Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:15:35

0001 #ifndef ONCAL_ONCALSERVER_H
0002 #define ONCAL_ONCALSERVER_H
0003 
0004 #include <fun4all/Fun4AllServer.h>
0005 #include <phool/PHTimeStamp.h>
0006 
0007 #include <time.h>  // for time_t
0008 #include <map>
0009 #include <set>
0010 #include <string>
0011 #include <vector>
0012 
0013 class OnCal;
0014 class SubsysReco;
0015 class TH1;
0016 
0017 namespace fetchrun
0018 {
0019   enum
0020   {
0021     CLOSEST,
0022     PREVIOUS
0023   };
0024 };
0025 
0026 class OnCalServer : public Fun4AllServer
0027 {
0028  public:
0029   static OnCalServer *instance();
0030   ~OnCalServer() override;
0031   using Fun4AllServer::registerHisto;
0032   void registerHisto(TH1 *h1d, OnCal *Calibrator, const int replace = 0);
0033   void unregisterHisto(const std::string &calibratorname);
0034   void Print(const std::string &what = "ALL") const override;
0035 
0036   void dumpHistos();
0037   int process_event() override;
0038   int BeginRun(const int runno) override;
0039   int EndRun(const int /*runno*/) override { return 0; }  // do not execute EndRun
0040   int End() override;
0041 
0042   PHTimeStamp *GetEndValidityTS();
0043 
0044   PHTimeStamp *GetBeginValidityTS();
0045   void printStamps();
0046   PHTimeStamp *GetLastGoodRunTS(OnCal *calibrator, const int irun);
0047 
0048   void recordDataBase(const bool bookkeep = false);
0049 
0050   // RunNumber() tells the server which run is being analyzed.
0051   // and if recordDB is true, this will insert the run number in
0052   // calprocess_stat table in calBookKeep database.
0053   // All updates are made to the row in the database containing this runNum.
0054   // Note that the run number is the primary key in the tables.
0055   // If calBookKeep database is not to be updated, this function
0056   // should not be called.
0057   void RunNumber(const int runnum);
0058   int RunNumber() const { return runNum; }
0059 
0060   void BeginTimeStamp(const PHTimeStamp &TimeStp);
0061   void EndTimeStamp(const PHTimeStamp &TimeStp);
0062 
0063   int SyncCalibTimeStampsToOnCal(const OnCal *calibrator, const std::string &table, const int commit = 0);
0064   int SyncCalibTimeStampsToOnCal(const OnCal *calibrator, const int commit = 0);
0065   int SyncOncalTimeStampsToRunDB(const int commit = 0);
0066   int ClosestGoodRun(OnCal *calibrator, const int irun, const int previous = fetchrun::CLOSEST);
0067   static int CopyTables(const OnCal *calibrator, const int FromRun, const int ToRun, const int commit = 0);
0068   static int OverwriteCalibration(OnCal *calibrator, const int runno, const int commit = 0, const int fromrun = -1);
0069   int FixMissingCalibration(OnCal *calibrator, const int runno, const int commit = 0, const int fromrun = -1);
0070 
0071   int SetBorTime(const int runno);
0072   int SetEorTime(const int runno);
0073   int requiredCalibration(SubsysReco *reco, const std::string &calibratorname);
0074   int FindClosestCalibratedRun(const int irun);
0075   int FillRunListFromFileList();
0076   int AdjustRichTimeStampForMultipleRuns();
0077   int CreateCalibration(OnCal *calibrator, const int myrunnumber, const std::string &what, const int commit = 0);
0078   int GetCalibStatus(const std::string &calibname, const int runno);
0079   static int DisconnectDB();
0080   void TestMode(const int i = 1);
0081   // need to be able to call this from the outside
0082   bool updateDBRunRange(const std::string &table, const std::string &column, const int entry, const int firstrun, const int lastrun);
0083   void EventCheckFrequency(const unsigned int i) { eventcheckfrequency = i; }
0084 
0085  protected:
0086   //-------------------------------------
0087   // following functions access DB using odbc++ library
0088   // these are designed to insert status in calBookKeep (or success) database.
0089   // setDB() sets the name of the database to connect to. e.g., calibration
0090   // this database should exist in the odbc.ini file.
0091   // void setDB(const char* DBname){database = DBname;}
0092   bool connectDB();
0093 
0094   // insertRunNumInDB enters the run number in the calBookKeep database.
0095   // All other updates are made to rows in the database containing the runNum.
0096   // This function should be called before any updates are made.
0097   // Returns true on successful DB insert.
0098   bool insertRunNumInDB(const std::string &DBtable, const int runno);
0099 
0100   bool findRunNumInDB(const std::string &DBtable, const int runno);
0101 
0102   // these functions update different columns in the success database tables.
0103   // Ony the row with the run number set by setRunNum() is updated.
0104 
0105   bool updateDB(const std::string &table, const std::string &column, int entry);
0106   bool updateDB(const std::string &table, const std::string &column, bool entry);
0107   bool updateDB(const std::string &table, const std::string &column, const std::string &entry,
0108                 const int runno, const bool append = false);
0109   int updateDB(const std::string &table, const std::string &column, const time_t ticks);
0110 
0111   int check_create_subsystable(const std::string &tablename);
0112   int check_create_successtable(const std::string &tablename);
0113   int add_calibrator_to_statustable(const std::string &calibratorname);
0114   int check_calibrator_in_statustable(const std::string &calibratorname);
0115   static int GetRunTimeTicks(const int runno, time_t &borticks, time_t &eorticks);
0116   void CreateCalibrationUpdateStatus(OnCal *calibrator, const std::string &table, const std::string &tablecomment, const int dbcode);
0117   OnCalServer(const std::string &name = "OnCalServer");
0118   PHTimeStamp beginTimeStamp;  // begin run timestamp of run analysing
0119   PHTimeStamp endTimeStamp;    // end run timestamp of run analysing
0120   int testmode{0};
0121   bool recordDB{false};
0122   TH1 *OnCalServerVars{nullptr};
0123   std::map<std::string, TH1 *> Histo;
0124   std::map<std::string, std::set<std::string> > calibratorhistomap;
0125   bool SetEndTimeStampByHand{false};
0126   bool SetBeginTimeStampByHand{false};
0127 
0128   std::string successTable;
0129   unsigned int runNum{0};
0130   unsigned int nEvents{0};
0131   unsigned int eventcheckfrequency{1000};
0132   std::string database{"calBookKeep"};  // this holds the name of the database
0133                                         // should be set to calibrations for normal running
0134   std::map<std::string, std::set<SubsysReco *> > requiredCalibrators;
0135   std::vector<int> analysed_runs;
0136   std::vector<std::string> inputfilelist;
0137   std::set<int> runlist;
0138 };
0139 
0140 #endif /* __ONCALSERVER_H */