| 1 | //============================================================================ | //============================================================================ | 
| 2 | // $Id: PacketUser.h,v 1.39 2008-09-05 14:33:48 messineo Exp $ | // $Id: PacketUser.h,v 1.5 2009/08/05 18:48:42 pam-fi 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 |  | #include "TArrayI.h" // EMI | 
| 15 |  |  | 
| 16 |  | #include <vector> | 
| 17 |  |  | 
| 18 | using namespace pamela; | using namespace pamela; | 
| 19 | using namespace pamela::techmodel; | using namespace pamela::techmodel; | 
| 20 |  |  | 
| 21 | namespace PamOffLineSW | namespace PamOffLineSW { | 
|  | { |  | 
|  |  |  | 
|  | class PacketUser |  | 
|  | { |  | 
| 22 |  |  | 
| 23 | public: |  | 
| 24 | //destructor | /* class dbinfo{ */ | 
| 25 | virtual ~PacketUser(); | /*   public: */ | 
| 26 | //the interface called by the external module: saves packets in ROOT files using the YODA part | /*   dbinfo( */ | 
| 27 | void usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type, | /*   char * v_folder_name,  */ | 
| 28 | unsigned long int counter, unsigned long int obt); | /*   char* v_file_name, */ | 
| 29 |  | /*   unsigned long int v_pkt_number_in,  */ | 
| 30 | //return the instance of the singleton | /*   unsigned long int v_pkt_number_fin,  */ | 
| 31 | static PacketUser& getInstance(); | /*   unsigned long int v_obt_in, */ | 
| 32 | //to be called at the end of the game | /*   unsigned long int v_obt_fin,  */ | 
| 33 | void FinishLastGroup(); | /*   unsigned long int v_mboot_num,  */ | 
| 34 |  | /*   unsigned long int v_time_offset,  */ | 
| 35 |  | /*   int v_bad_pkt, */ | 
| 36 |  | /*   int v_bad_pkt_read,  */ | 
| 37 |  | /*   int v_bad_pkt_CalRead,  */ | 
| 38 |  | /*   int v_num_PKT_Saved,  */ | 
| 39 |  | /*   char* v_nome_input,  */ | 
| 40 |  | /*   bool v__time_is_estimated */ | 
| 41 |  | /*   ){ */ | 
| 42 |  | /*     folder_name = strdup(v_folder_name);  */ | 
| 43 |  | /*     file_name = strdup(v_file_name); */ | 
| 44 |  | /*     pkt_number_in = v_pkt_number_in;  */ | 
| 45 |  | /*     pkt_number_fin = v_pkt_number_fin;  */ | 
| 46 |  | /*     obt_in = v_obt_in; */ | 
| 47 |  | /*     obt_fin = v_obt_fin;  */ | 
| 48 |  | /*     mboot_num = v_mboot_num;  */ | 
| 49 |  | /*     time_offset = v_time_offset;  */ | 
| 50 |  | /*     bad_pkt = v_bad_pkt; */ | 
| 51 |  | /*     bad_pkt_read = v_bad_pkt_read;  */ | 
| 52 |  | /*     bad_pkt_CalRead = v_bad_pkt_CalRead;  */ | 
| 53 |  | /*     num_PKT_Saved = v_num_PKT_Saved;  */ | 
| 54 |  | /*     nome_input = v_nome_input;  */ | 
| 55 |  | /*     _time_is_estimated = v__time_is_estimated; */ | 
| 56 |  | /*   } */ | 
| 57 |  | /*   ~dbinfo(){ */ | 
| 58 |  | /*     free(folder_name);  */ | 
| 59 |  | /*     free(file_name); */ | 
| 60 |  | /*   } */ | 
| 61 |  | /*   char* folder_name;  */ | 
| 62 |  | /*   char* file_name; */ | 
| 63 |  | /*   unsigned long int pkt_number_in;  */ | 
| 64 |  | /*   unsigned long int pkt_number_fin;  */ | 
| 65 |  | /*   unsigned long int obt_in; */ | 
| 66 |  | /*   unsigned long int obt_fin;  */ | 
| 67 |  | /*   unsigned long int mboot_num;  */ | 
| 68 |  | /*   unsigned long int time_offset;  */ | 
| 69 |  | /*   int bad_pkt; */ | 
| 70 |  | /*   int bad_pkt_read;  */ | 
| 71 |  | /*   int bad_pkt_CalRead;  */ | 
| 72 |  | /*   int num_PKT_Saved;  */ | 
| 73 |  | /*   char* nome_input;  */ | 
| 74 |  | /*   bool _time_is_estimated; */ | 
| 75 |  | /* }; */ | 
| 76 |  |  | 
| 77 |  | class PacketUser { | 
| 78 |  |  | 
| 79 |  | public: | 
| 80 |  | //destructor | 
| 81 |  | virtual ~PacketUser(); | 
| 82 |  | //the interface called by the external module: saves packets in ROOT files using the YODA part | 
| 83 |  | void usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type, | 
| 84 |  | unsigned long int counter, unsigned long int obt); | 
| 85 |  |  | 
| 86 |  | //return the instance of the singleton | 
| 87 |  | static PacketUser& getInstance(); | 
| 88 |  | //to be called at the end of the game | 
| 89 |  | void FinishLastGroup(); | 
| 90 |  |  | 
| 91 | private: | private: | 
| 92 | //the Event Reader handler |  | 
| 93 | pamela::techmodel::EventReader* reader; | // store dbinfo when file is bad but num. packet more than 1000 | 
| 94 | //the Pamela Run handler,  one for ech ROOT file created | //  vector <dbinfo*> m_dbinfos; | 
| 95 | pamela::PamelaRun* pRun; |  | 
| 96 | //Constructor | //the Event Reader handler | 
| 97 | PacketUser(); | pamela::techmodel::EventReader* reader; | 
| 98 | //The istance | //the Pamela Run handler,  one for ech ROOT file created | 
| 99 | static PacketUser instance; | pamela::PamelaRun* pRun; | 
| 100 | //How many times I found a discontinuity | //Constructor | 
| 101 | static int numDiscontinity; | PacketUser(); | 
| 102 | //How many packets arrived here, | //The istance | 
| 103 | static int numPKT; | static PacketUser instance; | 
| 104 | //How many packetS saved in each ROOT files | //How many times I found a discontinuity | 
| 105 | static int numPKTSaved; | static int numDiscontinity; | 
| 106 | //Name of the final ROOT file | //How many packets arrived here, | 
| 107 | char rootfilename[80]; | static int numPKT; | 
| 108 | //pkt counter of the first packet of the group | //How many packetS saved in each ROOT files | 
| 109 | unsigned long int pkt_number_init; | static int numPKTSaved; | 
| 110 | //pkt OBT of the first packet of the group | //Name of the final ROOT file | 
| 111 | unsigned long int obt_init; | char rootfilename[80]; | 
| 112 | //pkt counter of the last packet of the group | //pkt counter of the first packet of the group | 
| 113 | unsigned long int pkt_number_last; | unsigned long int pkt_number_init; | 
| 114 | //pkt OBT of the last packet of the group | //pkt OBT of the first packet of the group | 
| 115 | unsigned long int obt_last; | unsigned long int obt_init; | 
| 116 | //the first packet's absolute time | //pkt counter of the last packet of the group | 
| 117 | unsigned long int real_time_init; | unsigned long int pkt_number_last; | 
| 118 | //the last packet's absolute time | //pkt OBT of the last packet of the group | 
| 119 | unsigned long int real_time_last; | unsigned long int obt_last; | 
| 120 | //tymesync and OBT informations | //the first packet's absolute time | 
| 121 | unsigned long int obt_time_sync; | unsigned long int real_time_init; | 
| 122 | unsigned long int last_time_sync_info; | //the last packet's absolute time | 
| 123 | // (tassa) keep previous value of time_sync | unsigned long int real_time_last; | 
| 124 | unsigned long int obt_time_sync_prevvalue; | //tymesync and OBT informations | 
| 125 | unsigned long int last_time_sync_info_prevvalue; | unsigned long int obt_time_sync; | 
| 126 | bool time_is_estimated; | unsigned long int last_time_sync_info; | 
| 127 | //TimeOffset used to obtain absolute time | // (tassa) keep previous value of time_sync | 
| 128 | unsigned long int timeOffset; | unsigned long int obt_time_sync_prevvalue; | 
| 129 | //part of the ROOT filename used to retrieve timeOffset for special files | unsigned long int last_time_sync_info_prevvalue; | 
| 130 | char nnnn_mmm_ppp[80]; | bool time_is_estimated; | 
| 131 | //boot number | //TimeOffset used to obtain absolute time | 
| 132 | unsigned long int boot_number; | unsigned long int timeOffset; | 
| 133 | unsigned long int boot_number_prevvalue; | //part of the ROOT filename used to retrieve timeOffset for special files | 
| 134 | //(tassa) | char nnnn_mmm_ppp[80]; | 
| 135 | unsigned int id_to_recover[1000]; | //boot number | 
| 136 | int id_to_recover_index; | unsigned long int boot_number; | 
| 137 | //number of packets with problems( in general CRC problems) detected in EventReader: | unsigned long int boot_number_prevvalue; | 
| 138 | int bad_pkt_EventReader; | //(tassa) | 
| 139 | //num  ber of Calibration packets with problems( in general CRC problems) detected in EventReader: | unsigned int id_to_recover[1000]; | 
| 140 | int bad_pkt_CalibReader; | int id_to_recover_index; | 
| 141 | //number of good packets that comes from one or more corrupted cadres | //number of packets with problems( in general CRC problems) detected in EventReader: | 
| 142 | int bad_pkt; | int bad_pkt_EventReader; | 
| 143 |  | //num  ber of Calibration packets with problems( in general CRC problems) detected in EventReader: | 
| 144 | //marco_new: | int bad_pkt_CalibReader; | 
| 145 | //the ID of the current ROOT file in table Table_ROOT_Good | //number of good packets that comes from one or more corrupted cadres | 
| 146 | unsigned int my_id; | int bad_pkt; | 
| 147 |  |  | 
| 148 | //number of good Calibration packets in the ROOT files | //marco_new: | 
| 149 | //      int good_pkt_Calib;//maybe I will use this in future | //the ID of the current ROOT file in table Table_ROOT_Good | 
| 150 |  | unsigned int my_id; | 
| 151 | //the table name in our DB for the ROOT files |  | 
| 152 | char* Table_ROOT_Good; | //number of good Calibration packets in the ROOT files | 
| 153 | //the table name in our DB where I put the ROOT files that have not Real time associated | //    int good_pkt_Calib;//maybe I will use this in future | 
| 154 | char* Table_ROOT_Bad; |  | 
| 155 | //table used to retrieve TimeOffset | //the table name in our DB for the ROOT files | 
| 156 | char* Table_GL_RESURS_OFFSET; | const char* Table_ROOT_Good; | 
| 157 | //the table name in our DB for MERGING | //the table name in our DB where I put the ROOT files that have not Real time associated | 
| 158 | char* Table_ROOT_Merging; | const char* Table_ROOT_Bad; | 
| 159 |  | //table used to retrieve TimeOffset | 
| 160 | //starts a new root file | const char* Table_GL_RESURS_OFFSET; | 
| 161 | void StartGroup(); | //the table name in our DB for MERGING | 
| 162 | //set the value of the counter and obt of the first packet of the group | const char* Table_ROOT_Merging; | 
| 163 | void setInit(unsigned long int counter, unsigned long int obt); |  | 
| 164 | //finish the old ROOT file | // | 
| 165 | void FinishGroup(char * filename); | Int_t recoverlimit; | 
| 166 | //set the value of the counter and obt of the last packet of the group | // list of ID in root_table_bad with more than XXX packets | 
| 167 | void setLast(unsigned long int counter, unsigned long int obt); | TArrayI *idtorecover; | 
| 168 | //retrieve OBT_TIME_SYNC and LAST_TIME_SYNC_INFO from packet | // | 
| 169 | void setTimeSync(char* packet, long int pktLenght, const PacketType* type); | Int_t arsize; | 
| 170 | //retrieve boot number | // delete small files in root_table_bad from disk | 
| 171 | void setBootNumber(char* packet, long int pktLenght, const PacketType* type); | void CleanDisk(); | 
| 172 | //set real_time_init and real_time_last |  | 
| 173 | void setReal_Time(); | //starts a new root file | 
| 174 | // | void StartGroup(); | 
| 175 | void recover_boot_number(); | //set the value of the counter and obt of the first packet of the group | 
| 176 |  | void setInit(unsigned long int counter, unsigned long int obt); | 
| 177 | //retrieve the timeOffset from table=Table_GL_RESURS_OFFSET | //finish the old ROOT file | 
| 178 | unsigned long int retrieveTimeOffset(char * table); | void FinishGroup(char * filename); | 
| 179 |  | //set the value of the counter and obt of the last packet of the group | 
| 180 | unsigned  int select_maxIDN_DB(char* table_name); | void setLast(unsigned long int counter, unsigned long int obt); | 
| 181 |  | //retrieve OBT_TIME_SYNC and LAST_TIME_SYNC_INFO from packet | 
| 182 | //function that saves informations in our DB | void setTimeSync(char* packet, long int pktLenght, const PacketType* type); | 
| 183 | bool saveROOT_DB(char* table_name, char* folder_name, char* file_name, | //retrieve boot number | 
| 184 | unsigned long int pkt_number_in, unsigned long int pkt_number_fin, | void setBootNumber(char* packet, long int pktLenght, const PacketType* type); | 
| 185 | unsigned long int obt_in, unsigned long int obt_fin, | //set real_time_init and real_time_last | 
| 186 | unsigned long int  oT_sync, unsigned long int lT_sync_info, | void setReal_Time(); | 
| 187 | unsigned long int mtime_init,   unsigned long int mtime_last, |  | 
| 188 | unsigned long int mboot_num, | void setReal_TimeRECOVERY( | 
| 189 | unsigned long int time_offset, | unsigned long int & _real_time_init, | 
| 190 | int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, | unsigned long int & _real_time_last, | 
| 191 | char* nome_input,bool _time_is_estimated); | unsigned long int _obt_init, | 
| 192 |  | unsigned long int _obt_last | 
| 193 | ///new part merging | ); | 
| 194 |  |  | 
| 195 | //tipi di relazioni che il ROOT file puo' avere con un ROOT gia salvatto in DB | // | 
| 196 | //0 =AFTER  se c'e' una sovrapposizione temporale parziale, inizia dopo l'inizio di quello associato e finisce dopo la sua fine | void recover_boot_number(); | 
| 197 | //1 =BEFORE se c'e' una sovrapposizione temporale parziale, ossia finisce prima della fine di quello associato ma inizia prima |  | 
| 198 | //2 =SMALLER se c'e' una sovrapposizione temporale parziale: e' contenuto nel ROOT file associato | //retrieve the timeOffset from table=Table_GL_RESURS_OFFSET | 
| 199 | //3 =BIGGER  se c'e' una sovrapposizione temporale parziale: contiene il ROOT file associato | unsigned long int retrieveTimeOffset(const char * table); | 
| 200 | enum type_Rel_ROOT{ |  | 
| 201 | AFTER,     //se inizia dentro e finisce fuori | unsigned int select_maxIDN_DB(const char* table_name); | 
| 202 | BEFORE,    //inizia prima ma finisce dentro |  | 
| 203 | SMALLER,    //se inizia e finisce dentro | //function that saves informations in our DB | 
| 204 | BIGGER      //se inizia prima e finisce dopo | bool saveROOT_DB(const char* table_name, const char* folder_name, const char* file_name, unsigned long int pkt_number_in, | 
| 205 | }; | unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, unsigned long int oT_sync, | 
| 206 |  | unsigned long int lT_sync_info, unsigned long int mtime_init, unsigned long int mtime_last, | 
| 207 |  | unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, | 
| 208 |  | int num_PKT_Saved, const char* nome_input, bool _time_is_estimated); | 
| 209 |  |  | 
| 210 | bool merge_ROOTfiles(); | ///new part merging | 
| 211 |  |  | 
| 212 |  | //tipi di relazioni che il ROOT file puo' avere con un ROOT gia salvatto in DB | 
| 213 | bool saveMergeROOT_DB(char* table_name, | //0 =AFTER  se c'e' una sovrapposizione temporale parziale, inizia dopo l'inizio di quello associato e finisce dopo la sua fine | 
| 214 | unsigned  int root_id, | //1 =BEFORE se c'e' una sovrapposizione temporale parziale, ossia finisce prima della fine di quello associato ma inizia prima | 
| 215 | unsigned long int pkt_number_in, unsigned long int pkt_number_fin, | //2 =SMALLER se c'e' una sovrapposizione temporale parziale: e' contenuto nel ROOT file associato | 
| 216 | unsigned long int obt_in, unsigned long int obt_fin, | //3 =BIGGER  se c'e' una sovrapposizione temporale parziale: contiene il ROOT file associato | 
| 217 | unsigned long int mtime_init,   unsigned long int mtime_last, | enum type_Rel_ROOT { | 
| 218 | unsigned long int mboot_num, | AFTER, //se inizia dentro e finisce fuori | 
| 219 | double percentage); | BEFORE, //inizia prima ma finisce dentro | 
| 220 |  | SMALLER, //se inizia e finisce dentro | 
| 221 |  | BIGGER | 
| 222 | bool updateMergeROOT_DB(char* table_name, | //se inizia prima e finisce dopo | 
| 223 | unsigned  int root_id, | }; | 
| 224 | unsigned long int pkt_number_in, unsigned long int pkt_number_fin, |  | 
| 225 | unsigned long int obt_in, unsigned long int obt_fin, | bool merge_ROOTfiles(); | 
| 226 | unsigned long int mtime_init,   unsigned long int mtime_last, |  | 
| 227 | unsigned long int mboot_num, | bool saveMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, | 
| 228 | double bad_perc, | unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, | 
| 229 | unsigned int ID_record, type_Rel_ROOT type_rel); | unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double percentage); | 
| 230 |  |  | 
| 231 | //search in DB if there are ROOT files in the temporal range of interest and with the given relation | bool updateMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, | 
| 232 | TSQLResult* Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel); | unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, | 
| 233 |  | unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double bad_perc, | 
| 234 | //Lock the tableTobeLocked table or all the tables I know if par=NULL | unsigned int ID_record, type_Rel_ROOT type_rel); | 
| 235 | int LockTables(char* tableTobeLocked); |  | 
| 236 | //Unlock all the lockedtables | //search in DB if there are ROOT files in the temporal range of interest and with the given relation | 
| 237 | int UnLockTables(); | TSQLResult* Select_merging(const char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, | 
| 238 | //Open DB connection and lock table | type_Rel_ROOT type_rel); | 
| 239 | void OpenDBConnection(char* tableTobeLocked); |  | 
| 240 | //Close DB connection and UNlock tables | //Lock the tableTobeLocked table or all the tables I know if par=NULL | 
| 241 | void CloseDBConnection(); | int LockTables(const char* tableTobeLocked); | 
| 242 |  | //Unlock all the lockedtables | 
| 243 | /***************************************************************************************/ | int UnLockTables(); | 
| 244 | //UNUSED: return the system time in ms | //Open DB connection and lock table | 
| 245 | unsigned long long Record_Time(); | void OpenDBConnection(const char* tableTobeLocked); | 
| 246 | //DBG functions that saves pkt | //Close DB connection and UNlock tables | 
| 247 | void saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append); | void CloseDBConnection(); | 
| 248 | //function used to save in a file the packet |  | 
| 249 | void savePKT_file(char* headerPkt, | /***************************************************************************************/ | 
| 250 | char* pamPkt, | //UNUSED: return the system time in ms | 
| 251 | long int length, | unsigned long long Record_Time(); | 
| 252 | bool append, | //DBG functions that saves pkt | 
| 253 | char* nomefile); | void saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append); | 
| 254 |  | //function used to save in a file the packet | 
| 255 |  | void savePKT_file(char* headerPkt, char* pamPkt, long int length, bool append, char* nomefile); | 
| 256 |  |  | 
| 257 | }; | }; | 
| 258 |  |  |