| 1 | 
 //============================================================================ | 
 //============================================================================ | 
| 2 | 
 // $Id: PacketUser.h,v 1.38 2008-06-10 08:49:18 messineo Exp $  | 
 // $Id: PacketUser.h,v 1.4 2008-12-18 14:47:00 mocchiut Exp $  | 
| 3 | 
 // Description :  | 
 // Description :  | 
| 4 | 
 //============================================================================ | 
 //============================================================================ | 
| 5 | 
 #ifndef PACKETUSER_H_ | 
 #ifndef PACKETUSER_H_ | 
| 10 | 
 #include "EventReader.h" | 
 #include "EventReader.h" | 
| 11 | 
  | 
  | 
| 12 | 
 #include "TROOT.h" //ROOT version > 5.14 | 
 #include "TROOT.h" //ROOT version > 5.14 | 
| 13 | 
  | 
 #include "TSystem.h" // EMI | 
| 14 | 
 using namespace pamela; | 
 using namespace pamela; | 
| 15 | 
 using namespace pamela::techmodel; | 
 using namespace pamela::techmodel; | 
| 16 | 
  | 
  | 
| 17 | 
 namespace PamOffLineSW | 
 namespace PamOffLineSW { | 
 | 
 { | 
  | 
| 18 | 
  | 
  | 
| 19 | 
 class PacketUser | 
 class PacketUser { | 
 | 
 { | 
  | 
| 20 | 
  | 
  | 
| 21 | 
 public:  | 
 public: | 
| 22 | 
         //destructor | 
   //destructor | 
| 23 | 
         virtual ~PacketUser(); | 
   virtual ~PacketUser(); | 
| 24 | 
         //the interface called by the external module: saves packets in ROOT files using the YODA part  | 
   //the interface called by the external module: saves packets in ROOT files using the YODA part | 
| 25 | 
         void usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type, | 
   void usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type, | 
| 26 | 
                         unsigned long int counter, unsigned long int obt); | 
       unsigned long int counter, unsigned long int obt); | 
| 27 | 
  | 
  | 
| 28 | 
         //return the instance of the singleton | 
   //return the instance of the singleton | 
| 29 | 
         static PacketUser& getInstance();        | 
   static PacketUser& getInstance(); | 
| 30 | 
         //to be called at the end of the game | 
   //to be called at the end of the game | 
| 31 | 
         void FinishLastGroup(); | 
   void FinishLastGroup(); | 
| 32 | 
  | 
  | 
| 33 | 
 private: | 
 private: | 
| 34 | 
         //the Event Reader handler | 
   //the Event Reader handler | 
| 35 | 
         pamela::techmodel::EventReader* reader;  | 
   pamela::techmodel::EventReader* reader; | 
| 36 | 
         //the Pamela Run handler,  one for ech ROOT file created | 
   //the Pamela Run handler,  one for ech ROOT file created | 
| 37 | 
         pamela::PamelaRun* pRun; | 
   pamela::PamelaRun* pRun; | 
| 38 | 
         //Constructor | 
   //Constructor | 
| 39 | 
         PacketUser(); | 
   PacketUser(); | 
| 40 | 
         //The istance | 
   //The istance | 
| 41 | 
         static PacketUser instance; | 
   static PacketUser instance; | 
| 42 | 
         //How many times I found a discontinuity | 
   //How many times I found a discontinuity | 
| 43 | 
         static int numDiscontinity; | 
   static int numDiscontinity; | 
| 44 | 
         //How many packets arrived here,         | 
   //How many packets arrived here, | 
| 45 | 
         static int numPKT;  | 
   static int numPKT; | 
| 46 | 
         //How many packetS saved in each ROOT files | 
   //How many packetS saved in each ROOT files | 
| 47 | 
         static int numPKTSaved;  | 
   static int numPKTSaved; | 
| 48 | 
         //Name of the final ROOT file | 
   //Name of the final ROOT file | 
| 49 | 
         char rootfilename[80];   | 
   char rootfilename[80]; | 
| 50 | 
         //pkt counter of the first packet of the group | 
   //pkt counter of the first packet of the group | 
| 51 | 
         unsigned long int pkt_number_init; | 
   unsigned long int pkt_number_init; | 
| 52 | 
         //pkt OBT of the first packet of the group | 
   //pkt OBT of the first packet of the group | 
| 53 | 
         unsigned long int obt_init; | 
   unsigned long int obt_init; | 
| 54 | 
         //pkt counter of the last packet of the group | 
   //pkt counter of the last packet of the group | 
| 55 | 
         unsigned long int pkt_number_last; | 
   unsigned long int pkt_number_last; | 
| 56 | 
         //pkt OBT of the last packet of the group | 
   //pkt OBT of the last packet of the group | 
| 57 | 
         unsigned long int obt_last; | 
   unsigned long int obt_last; | 
| 58 | 
         //the first packet's absolute time | 
   //the first packet's absolute time | 
| 59 | 
         unsigned long int real_time_init; | 
   unsigned long int real_time_init; | 
| 60 | 
         //the last packet's absolute time | 
   //the last packet's absolute time | 
| 61 | 
         unsigned long int real_time_last; | 
   unsigned long int real_time_last; | 
| 62 | 
         //tymesync and OBT informations  | 
   //tymesync and OBT informations | 
| 63 | 
         unsigned long int obt_time_sync; | 
   unsigned long int obt_time_sync; | 
| 64 | 
         unsigned long int last_time_sync_info; | 
   unsigned long int last_time_sync_info; | 
| 65 | 
         //TimeOffset used to obtain absolute time  | 
   // (tassa) keep previous value of time_sync | 
| 66 | 
         unsigned long int timeOffset; | 
   unsigned long int obt_time_sync_prevvalue; | 
| 67 | 
         //part of the ROOT filename used to retrieve timeOffset for special files | 
   unsigned long int last_time_sync_info_prevvalue; | 
| 68 | 
         char nnnn_mmm_ppp[80]; | 
   bool time_is_estimated; | 
| 69 | 
         //BOOT Number  | 
   //TimeOffset used to obtain absolute time | 
| 70 | 
         unsigned long int boot_number; | 
   unsigned long int timeOffset; | 
| 71 | 
  | 
   //part of the ROOT filename used to retrieve timeOffset for special files | 
| 72 | 
         //number of packets with problems( in general CRC problems) detected in EventReader:  | 
   char nnnn_mmm_ppp[80]; | 
| 73 | 
         int bad_pkt_EventReader; | 
   //boot number | 
| 74 | 
         //num  ber of Calibration packets with problems( in general CRC problems) detected in EventReader: | 
   unsigned long int boot_number; | 
| 75 | 
         int bad_pkt_CalibReader; | 
   unsigned long int boot_number_prevvalue; | 
| 76 | 
         //number of good packets that comes from one or more corrupted cadres | 
   //(tassa) | 
| 77 | 
         int bad_pkt; | 
   unsigned int id_to_recover[1000]; | 
| 78 | 
          | 
   int id_to_recover_index; | 
| 79 | 
         //marco_new:  | 
   //number of packets with problems( in general CRC problems) detected in EventReader: | 
| 80 | 
    //the ID of the current ROOT file in table Table_ROOT_Good | 
   int bad_pkt_EventReader; | 
| 81 | 
          unsigned int my_id; | 
   //num  ber of Calibration packets with problems( in general CRC problems) detected in EventReader: | 
| 82 | 
  | 
   int bad_pkt_CalibReader; | 
| 83 | 
         //number of good Calibration packets in the ROOT files  | 
   //number of good packets that comes from one or more corrupted cadres | 
| 84 | 
 //      int good_pkt_Calib;//maybe I will remove this in future | 
   int bad_pkt; | 
| 85 | 
  | 
  | 
| 86 | 
         //the table name in our DB for the ROOT files  | 
   //marco_new: | 
| 87 | 
         char* Table_ROOT_Good; | 
   //the ID of the current ROOT file in table Table_ROOT_Good | 
| 88 | 
         //the table name in our DB where I put the ROOT files that have not Real time associated | 
   unsigned int my_id; | 
| 89 | 
         char* Table_ROOT_Bad; | 
  | 
| 90 | 
         //table used to retrieve TimeOffset | 
   //number of good Calibration packets in the ROOT files | 
| 91 | 
         char* Table_GL_RESURS_OFFSET;    | 
   //    int good_pkt_Calib;//maybe I will use this in future | 
| 92 | 
         //the table name in our DB for MERGING | 
  | 
| 93 | 
         char* Table_ROOT_Merging;  | 
   //the table name in our DB for the ROOT files | 
| 94 | 
          | 
   const char* Table_ROOT_Good; | 
| 95 | 
         //starts a new root file | 
   //the table name in our DB where I put the ROOT files that have not Real time associated | 
| 96 | 
         void StartGroup();       | 
   const char* Table_ROOT_Bad; | 
| 97 | 
         //set the value of the counter and obt of the first packet of the group | 
   //table used to retrieve TimeOffset | 
| 98 | 
         void setInit(unsigned long int counter, unsigned long int obt); | 
   const char* Table_GL_RESURS_OFFSET; | 
| 99 | 
         //finish the old ROOT file | 
   //the table name in our DB for MERGING | 
| 100 | 
         void FinishGroup(char * filename); | 
   const char* Table_ROOT_Merging; | 
| 101 | 
         //set the value of the counter and obt of the last packet of the group  | 
  | 
| 102 | 
         void setLast(unsigned long int counter, unsigned long int obt);          | 
   //starts a new root file | 
| 103 | 
         //retrieve OBT_TIME_SYNC and LAST_TIME_SYNC_INFO from packet | 
   void StartGroup(); | 
| 104 | 
         void setTimeSync(char* packet, long int pktLenght, const PacketType* type); | 
   //set the value of the counter and obt of the first packet of the group | 
| 105 | 
         //retrieve boot number | 
   void setInit(unsigned long int counter, unsigned long int obt); | 
| 106 | 
         void setBootNumber(char* packet, long int pktLenght, const PacketType* type); | 
   //finish the old ROOT file | 
| 107 | 
         //set real_time_init and real_time_last | 
   void FinishGroup(char * filename); | 
| 108 | 
         void setReal_Time(); | 
   //set the value of the counter and obt of the last packet of the group | 
| 109 | 
         //retrieve the timeOffset from table=Table_GL_RESURS_OFFSET | 
   void setLast(unsigned long int counter, unsigned long int obt); | 
| 110 | 
         unsigned long int retrieveTimeOffset(char * table); | 
   //retrieve OBT_TIME_SYNC and LAST_TIME_SYNC_INFO from packet | 
| 111 | 
  | 
   void setTimeSync(char* packet, long int pktLenght, const PacketType* type); | 
| 112 | 
         unsigned  int select_maxIDN_DB(char* table_name); | 
   //retrieve boot number | 
| 113 | 
  | 
   void setBootNumber(char* packet, long int pktLenght, const PacketType* type); | 
| 114 | 
         //function that saves informations in our DB     | 
   //set real_time_init and real_time_last | 
| 115 | 
         bool saveROOT_DB(char* table_name, char* folder_name, char* file_name,  | 
   void setReal_Time(); | 
| 116 | 
                         unsigned long int pkt_number_in, unsigned long int pkt_number_fin, | 
   // | 
| 117 | 
                         unsigned long int obt_in, unsigned long int obt_fin, | 
   void recover_boot_number(); | 
| 118 | 
                         unsigned long int  oT_sync, unsigned long int lT_sync_info,         | 
  | 
| 119 | 
                         unsigned long int mtime_init,   unsigned long int mtime_last, | 
   //retrieve the timeOffset from table=Table_GL_RESURS_OFFSET | 
| 120 | 
                         unsigned long int mboot_num, | 
   unsigned long int retrieveTimeOffset(const char * table); | 
| 121 | 
                         unsigned long int time_offset,  | 
  | 
| 122 | 
                         int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, | 
   unsigned int select_maxIDN_DB(const char* table_name); | 
| 123 | 
                         char* nome_input); | 
  | 
| 124 | 
          | 
   //function that saves informations in our DB | 
| 125 | 
         ///new part merging | 
   bool saveROOT_DB(const char* table_name, char* folder_name, char* file_name, unsigned long int pkt_number_in, | 
| 126 | 
  | 
       unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, unsigned long int oT_sync, | 
| 127 | 
         //tipi di relazioni che il ROOT file puo' avere con un ROOT gia salvatto in DB | 
       unsigned long int lT_sync_info, unsigned long int mtime_init, unsigned long int mtime_last, | 
| 128 | 
                 enum type_Rel_ROOT{ | 
       unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, | 
| 129 | 
                 AFTER,     //se inizia dentro e finisce fuori | 
       int num_PKT_Saved, char* nome_input, bool _time_is_estimated); | 
| 130 | 
                         BEFORE,    //inizia prima ma finisce dentro | 
  | 
| 131 | 
                         SMALLER,    //se inizia e finisce dentro  | 
   ///new part merging | 
| 132 | 
                         BIGGER      //se inizia prima e finisce dopo                                             | 
  | 
| 133 | 
                 }; | 
   //tipi di relazioni che il ROOT file puo' avere con un ROOT gia salvatto in DB | 
| 134 | 
          | 
   //0 =AFTER  se c'e' una sovrapposizione temporale parziale, inizia dopo l'inizio di quello associato e finisce dopo la sua fine | 
| 135 | 
         /*       | 
   //1 =BEFORE se c'e' una sovrapposizione temporale parziale, ossia finisce prima della fine di quello associato ma inizia prima | 
| 136 | 
                 0 =AFTER  se c'e' una sovrapposizione temporale parziale, inizia dopo l'inizio di quello associato e finisce dopo la sua fine | 
   //2 =SMALLER se c'e' una sovrapposizione temporale parziale: e' contenuto nel ROOT file associato | 
| 137 | 
             1 =BEFORE se c'e' una sovrapposizione temporale parziale, ossia finisce prima della fine di quello associato ma inizia prima | 
   //3 =BIGGER  se c'e' una sovrapposizione temporale parziale: contiene il ROOT file associato | 
| 138 | 
                 2 =SMALLER se c'e' una sovrapposizione temporale parziale: e' contenuto nel ROOT file associato | 
   enum type_Rel_ROOT { | 
| 139 | 
                 3 =BIGGER  se c'e' una sovrapposizione temporale parziale: contiene il ROOT file associato       | 
     AFTER, //se inizia dentro e finisce fuori | 
| 140 | 
         */ | 
     BEFORE, //inizia prima ma finisce dentro | 
| 141 | 
  | 
     SMALLER, //se inizia e finisce dentro | 
| 142 | 
  | 
     BIGGER | 
| 143 | 
         bool merge_ROOTfiles();  | 
   //se inizia prima e finisce dopo | 
| 144 | 
  | 
   }; | 
| 145 | 
  | 
  | 
| 146 | 
         bool saveMergeROOT_DB(char* table_name,  | 
   bool merge_ROOTfiles(); | 
| 147 | 
                            unsigned  int root_id, | 
  | 
| 148 | 
                                 unsigned long int pkt_number_in, unsigned long int pkt_number_fin, | 
   bool saveMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, | 
| 149 | 
                                 unsigned long int obt_in, unsigned long int obt_fin, | 
       unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, | 
| 150 | 
                         unsigned long int mtime_init,   unsigned long int mtime_last,  | 
       unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double percentage); | 
| 151 | 
                         unsigned long int mboot_num, | 
  | 
| 152 | 
                                 double percentage); | 
   bool updateMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, | 
| 153 | 
  | 
       unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, | 
| 154 | 
          | 
       unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double bad_perc, | 
| 155 | 
         bool updateMergeROOT_DB(char* table_name, | 
       unsigned int ID_record, type_Rel_ROOT type_rel); | 
| 156 | 
                         unsigned  int root_id, | 
  | 
| 157 | 
                         unsigned long int pkt_number_in, unsigned long int pkt_number_fin, | 
   //search in DB if there are ROOT files in the temporal range of interest and with the given relation | 
| 158 | 
                         unsigned long int obt_in, unsigned long int obt_fin, | 
   TSQLResult* Select_merging(const char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, | 
| 159 | 
                 unsigned long int mtime_init,   unsigned long int mtime_last, | 
       type_Rel_ROOT type_rel); | 
| 160 | 
                         unsigned long int mboot_num, | 
  | 
| 161 | 
                         double bad_perc, | 
   //Lock the tableTobeLocked table or all the tables I know if par=NULL | 
| 162 | 
                         unsigned int ID_record, type_Rel_ROOT type_rel); | 
   int LockTables(const char* tableTobeLocked); | 
| 163 | 
          | 
   //Unlock all the lockedtables | 
| 164 | 
         //unsigned int Return_IDN_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel); | 
   int UnLockTables(); | 
| 165 | 
         TSQLResult* Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel); | 
   //Open DB connection and lock table | 
| 166 | 
  | 
   void OpenDBConnection(const char* tableTobeLocked); | 
| 167 | 
         //Lock the tableTobeLocked table or all the tables I know if par=NULL | 
   //Close DB connection and UNlock tables | 
| 168 | 
         int LockTables(char* tableTobeLocked); | 
   void CloseDBConnection(); | 
| 169 | 
         //Unlock all the lockedtables | 
  | 
| 170 | 
         int UnLockTables();  | 
   /***************************************************************************************/ | 
| 171 | 
         //Open DB connection and lock table | 
   //UNUSED: return the system time in ms | 
| 172 | 
         void OpenDBConnection(char* tableTobeLocked); | 
   unsigned long long Record_Time(); | 
| 173 | 
         //Close DB connection and UNlock tables | 
   //DBG functions that saves pkt | 
| 174 | 
         void CloseDBConnection();  | 
   void saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append); | 
| 175 | 
  | 
   //function used to save in a file the packet | 
| 176 | 
 /***************************************************************************************/ | 
   void savePKT_file(char* headerPkt, char* pamPkt, long int length, bool append, char* nomefile); | 
 | 
         //UNUSED: return the system time in ms | 
  | 
 | 
         unsigned long long Record_Time(); | 
  | 
 | 
         //DBG functions that saves pkt | 
  | 
 | 
         void saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append); | 
  | 
 | 
         //function used to save in a file the packet | 
  | 
 | 
         void savePKT_file(char* headerPkt,  | 
  | 
 | 
                         char* pamPkt,  | 
  | 
 | 
                         long int length,  | 
  | 
 | 
                         bool append, | 
  | 
 | 
                         char* nomefile); | 
  | 
| 177 | 
  | 
  | 
| 178 | 
 }; | 
 }; | 
| 179 | 
  | 
  |