--- chewbacca/PamOffLineSW/PacketUser.cpp 2010/02/02 15:26:09 1.16 +++ chewbacca/PamOffLineSW/PacketUser.cpp 2010/02/16 13:38:12 1.19 @@ -1,5 +1,5 @@ //============================================================================ -// $Id: PacketUser.cpp,v 1.15 2010/01/15 14:31:17 mocchiut Exp $ +// $Id: PacketUser.cpp,v 1.18 2010/02/11 09:02:09 mocchiut Exp $ // Description : //============================================================================ #include "PacketUser.h" @@ -275,6 +275,7 @@ void PacketUser::StartGroup() { pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI strcat(rootfilename, ".root"); + //if ( !pRun ) printf(" horror! -%s- \n",rootfilename); reader->Init(pRun); stringstream oss; oss.str() = ""; @@ -333,7 +334,8 @@ } else if (type == PacketType::Mcmd) { // mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere."); - return; + // return; + offset = -1; } else { return; @@ -343,12 +345,189 @@ // if ((obt_time_sync) || (last_time_sync_info)) // return; // no, do it, since there is a (small indeed) time drift due to different clock of PAMELA CPU and RESURS CPU... + // printf(" %s %lu %lu %lu %lu \n", rootfilename, pkt_number_init, pkt_number_last, obt_init, obt_last); + if ( offset >= 0 ){ + // use Runheader or RunTrailer + obt_time_sync = (((UINT32) packet[5 + offset] << 24) & 0xFF000000) + (((UINT32) packet[6 + offset] << 16) & 0x00FF0000) + (((UINT32) packet[7 + offset] << 8) & 0x0000FF00) + (((UINT32) packet[8 + offset]) & 0x000000FF); + last_time_sync_info = (((UINT32) packet[9 + offset] << 24) & 0xFF000000) + (((UINT32) packet[10 + offset] << 16) & 0x00FF0000) + (((UINT32) packet[11 + offset] << 8) & 0x0000FF00) + (((UINT32) packet[12 + offset]) & 0x000000FF); + // + // if you arrive here and RH/RT timesync is zero try to get the correct info from DB (dangerous??) + // + // printf("qui\n"); + if ( !obt_time_sync && !last_time_sync_info ){ + // if ( true ){ + stringstream os; + os.str() = ""; + os << " NEW (EM) AGH! NO GOOD TIMESYNC INFOS IN RH OR RT!! TRY TO LOOK IN DB"; + string msg = os.str(); + mainLogUtil->logAll(msg); + +// mysql> select ID_N,FOLDER_NAME,FILE_NAME,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,BAD_PKT,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME,INSERTED_BY from ROOT_TABLE_BAD where ON_DISK='YES' order by NUM_PKT_SAVED asc; +// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ +// | ID_N | FOLDER_NAME | FILE_NAME | PKT_NUMBER_INIT | PKT_NUMBER_FINAL | BAD_PKT | NUM_PKT_SAVED | INPUT_NAME | INSERT_TIME | INSERTED_BY | +// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ +// | 6493 | $PAM_L0FIA | L0PAM_00540_002_012_4.root | 12253479 | 12254111 | 626 | 626 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:50:15 | FI | +// | 6463 | $PAM_L0FIA | L0PAM_00540_002_010_4.root | 12252097 | 12252730 | 627 | 627 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:47:16 | FI | +// | 6525 | $PAM_L0FIA | L0PAM_00540_002_014_4.root | 12254906 | 12255539 | 627 | 629 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:53:24 | FI | +// | 321 | $PAM_L0FIA | L0PAM_00383_001_001_2.root | 601639 | 602281 | 489 | 639 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:49:55 | FI | +// | 306 | $PAM_L0FIA | L0PAM_00383_002_001_2.root | 601572 | 602213 | 0 | 642 | /wizard/03/pamela_data/preRawreader/00383002.pam | 2010-02-03 16:48:55 | FI | +// | 400 | $PAM_L0FIA | L0PAM_00383_001_005_2.root | 605234 | 605893 | 499 | 656 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:57:08 | FI | +// | 372 | $PAM_L0FIA | L0PAM_00383_001_003_2.root | 603600 | 604258 | 290 | 659 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:53:44 | FI | +// | 6483 | $PAM_L0FIA | L0PAM_00540_002_011_4.root | 12252731 | 12253478 | 739 | 739 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:48:52 | FI | +// | 6428 | $PAM_L0FIA | L0PAM_00540_002_009_4.root | 12251326 | 12252096 | 764 | 765 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:45:51 | FI | +// | 6507 | $PAM_L0FIA | L0PAM_00540_002_013_4.root | 12254112 | 12254905 | 781 | 781 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:52:01 | FI | +// | 378 | $PAM_L0FIA | L0PAM_00374_001_001_1.root | 368888 | 369754 | 0 | 867 | /wizard/03/pamela_data/preRawreader/00374001.pam | 2010-02-03 16:54:30 | FI | +// | 6565 | $PAM_L0FIA | L0PAM_00540_002_015_4.root | 12255540 | 12256457 | 907 | 907 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:55:22 | FI | +// | 413 | $PAM_L0FIA | L0PAM_00383_001_006_2.root | 605894 | 606807 | 417 | 913 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:58:52 | FI | +// | 390 | $PAM_L0FIA | L0PAM_00383_001_004_2.root | 604259 | 605233 | 684 | 975 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:55:45 | FI | +// | 358 | $PAM_L0FIA | L0PAM_00383_001_002_2.root | 602282 | 603599 | 649 | 1316 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:52:23 | FI | +// | 185 | $PAM_L0NAA | L0PAM_01737_001_001_1.root | 9373563 | 9375405 | 0 | 1843 | /storage02/pamela/pamela_data/preRawreader/01737001.pam | 2010-02-03 16:24:50 | NA | +// | 375 | $PAM_L0FIA | L0PAM_00383_002_002_2.root | 602214 | 608541 | 0 | 6328 | /wizard/03/pamela_data/preRawreader/00383002.pam | 2010-02-03 16:54:08 | FI | +// | 1419 | $PAM_L0FIA | L0PAM_00383_001_007_2.root | 606808 | 620604 | 9086 | 13764 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 17:26:39 | FI | +// | 6118 | $PAM_L0NAA | L0PAM_02144_002_001_1.root | 0 | 123139 | 6 | 123139 | /storage02/pamela/pamela_data/preRawreader/02144002.pam | 2010-02-03 21:29:51 | NA | +// | 4470 | $PAM_L0FIA | L0PAM_00614_001_001_1.root | 1409 | 130146 | 6 | 128722 | /wizard/03/pamela_data/preRawreader/00614001.pam | 2010-02-03 18:47:26 | FI | +// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ +// 20 rows in set (0.05 sec) +// mysql> select FILE_NAME, abs(PKT_NUMBER_INIT-123139), abs(0-PKT_NUMBER_FINAL) from ROOT_TABLE where (abs(PKT_NUMBER_INIT-123139)<10000 or abs(0-PKT_NUMBER_FINAL)<10000) and FILE_NAME like "L0PAM_021%"; +// +-----------------------------+-----------------------------+-------------------------+ +// | FILE_NAME | abs(PKT_NUMBER_INIT-123139) | abs(0-PKT_NUMBER_FINAL) | +// +-----------------------------+-----------------------------+-------------------------+ +// | L0PAM_02173_001_001_11.root | 123103 | 251 | +// +-----------------------------+-----------------------------+-------------------------+ +// 1 row in set (0.07 sec) +// + TString *nomino=new TString(rootfilename); + nomino->Remove(9); + TString mmyy=Form("select OBT_TIME_SYNC, LAST_TIME_SYNC_INFO from ROOT_TABLE where (abs(PKT_NUMBER_INIT-%lu)<10000 or abs(%lu-PKT_NUMBER_FINAL)<10000) and FILE_NAME like '%s%%' limit 1;",pkt_number_last, pkt_number_init,nomino->Data()); + delete nomino; + stringstream os3; + os3.str() = ""; + os3 << " NEW (EM) TRY TO FIND TSYNC IN DB QUERY: " << mmyy.Data(); + string msg3 = os3.str(); + mainLogUtil->logAll(msg3); + // + TSQLResult* res = NULL; + TSQLRow* row = NULL; + Bool_t apro = false; + if (!single_connection && !sqlServer ) { + OpenDBConnection(NULL);// no, locak tutto Nota qui ho deciso di lockare solo questa tabella e non tutte + apro = true; + } + res = sqlServer->Query(mmyy.Data()); + if (!res) { + mainLogUtil->logError("DBError retrieve time sync"); + return; + }; + row = res->Next(); + if ( row ){ + obt_time_sync = (ULong_t)atoi(row->GetField(0)); + last_time_sync_info = (ULong_t)atoi(row->GetField(1)); + // + stringstream os2; + os2.str() = ""; + os2 << " NEW (EM) FOUND TSYNC IN DB "; + string msg2 = os2.str(); + mainLogUtil->logAll(msg2); + // + }; + if (res) { + delete res; + res = NULL; + }; + // + if (!single_connection && apro) { + CloseDBConnection(); + }; + }; - obt_time_sync = (((UINT32) packet[5 + offset] << 24) & 0xFF000000) + (((UINT32) packet[6 + offset] << 16) - & 0x00FF0000) + (((UINT32) packet[7 + offset] << 8) & 0x0000FF00) + (((UINT32) packet[8 + offset]) & 0x000000FF); - last_time_sync_info = (((UINT32) packet[9 + offset] << 24) & 0xFF000000) + (((UINT32) packet[10 + offset] << 16) - & 0x00FF0000) + (((UINT32) packet[11 + offset] << 8) & 0x0000FF00) - + (((UINT32) packet[12 + offset]) & 0x000000FF); + } else { + // Use Packet Mcmd now is not used to retrieve Absolute time + // printf(" Pointer to Mcmd obj is %p \n",reader->GetPtr("Mcmd")); + + UInt_t recEntries = 0; + Double_t minimum = 0.; + Double_t maximum = 0.; + Double_t minimum2 = 0.; + Double_t maximum2 = 0.; + UInt_t TSYNC = 0; + UInt_t OBT = 0; + Bool_t existsts = false; + // + pamela::McmdEvent *mc = (pamela::McmdEvent*)(((pamela::techmodel::McmdReader*)reader->GetPtr("Mcmd"))->GetMcmd()); + pamela::McmdRecord *mcrc = 0; + TArrayC *mcmddata = 0; + // + minimum = numeric_limits::max(); + maximum = numeric_limits::min(); + minimum2 = numeric_limits::max(); + maximum2 = numeric_limits::min(); + // + recEntries = mc->Records->GetEntries(); + // printf(" recEntries is %i \n",recEntries); + + for (UInt_t j = 0; j < recEntries; j++){ + mcrc = (pamela::McmdRecord*)mc->Records->At(j); + mcmddata = mcrc->McmdData; + // + if (mcrc->ID1 == 0xE0 && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd timesync + // + OBT = (Int_t)(mcrc->MCMD_RECORD_OBT); + // + TSYNC = (((UInt_t)mcmddata->At(0)<<24)&0xFF000000) + (((UInt_t)mcmddata->At(1)<<16)&0x00FF0000) + (((UInt_t)mcmddata->At(2)<<8)&0x0000FF00) + (((UInt_t)mcmddata->At(3))&0x000000FF); + // + // printf("mcmd tsync tsync %u obt %u \n",TSYNC,OBT); + // + if ( TSYNC && OBT ){ + existsts = true; + stringstream os; + os.str() = ""; + os << " NEW (EM) FOUND TIMESYNC IN TIMESYNC MCMD"; + string msg = os.str(); + mainLogUtil->logAll(msg); + break; + }; + // + }; + }; + // + if ( !existsts && !obt_time_sync && !last_time_sync_info ){ // try with inclination mcmd but only if everything else fails and check that the result is good + // + Double_t timesync = 0.; + // + for (UInt_t j = 0; j < recEntries; j++){ + mcrc = (pamela::McmdRecord*)mc->Records->At(j); + mcmddata = mcrc->McmdData; + // + if (mcrc->ID1 == 0xE2 && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd inclination + timesync = 0.; + timesync = (Double_t)(((((UInt_t)mcmddata->At(0) << 24) & 0xFF000000) + (((UInt_t)mcmddata->At(1) << 16) & 0x00FF0000) + (((UInt_t)mcmddata->At(2) << 8) & 0x0000FF00) + ((UInt_t)mcmddata->At(3) & 0x000000FF))/128.0); + // + if ( timesync > maximum2){ + maximum2 = timesync; + OBT = (Int_t)(mcrc->MCMD_RECORD_OBT); + }; + }; + // + }; + if ( maximum2 > numeric_limits::min() ){ + TSYNC = (UInt_t)(maximum2 + 0.5); + if ( TSYNC && OBT ){ + existsts = true; + stringstream os; + os.str() = ""; + os << " NEW (EM) FOUND TIMESYNC IN INCLINATION MCMD"; + string msg = os.str(); + mainLogUtil->logAll(msg); + }; + }; + if ( labs(labs(last_time_sync_info_prevvalue-obt_time_sync_prevvalue)-labs(TSYNC-int(OBT/1000))) > 10 ) return; + }; + if ( !existsts ) return; + // printf(" TIMESYNC FROM MCMD FOUND: TSYNC = %i OBT = %i \n",TSYNC,OBT); + obt_time_sync = int(OBT/1000); + last_time_sync_info = TSYNC; + + }; stringstream oss; oss.str() = ""; @@ -457,7 +636,7 @@ //if(!table){table=Table_GL_RESURS_OFFSET} if (!single_connection) { - OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte + OpenDBConnection(NULL);// no, locak tutto Nota qui ho deciso di lockare solo questa tabella e non tutte } UInt_t t0 = 0;//toffset @@ -632,7 +811,8 @@ msg = err.str(); mainLogUtil->logInfo(msg); - TString qu = Form("select ID_N from ROOT_TABLE_BAD order by INSERT_TIME desc limit 1;"); + // TString qu = Form("select ID_N from ROOT_TABLE_BAD order by INSERT_TIME desc limit 1;"); // <========================= + TString qu = Form("select ID_N from ROOT_TABLE_BAD where FILE_NAME='%s' and PKT_NUMBER_INIT=%i and INSERTED_BY='%s';",filename,pkt_number_init,tag_value); // <========================= TSQLResult *result = sqlServer->Query(qu.Data()); TSQLRow *row = result->Next(); Int_t eid = -1; @@ -831,7 +1011,7 @@ }//pRun } - + void PacketUser::CleanDisk(){ if ( !candelete ) return; @@ -868,6 +1048,107 @@ unsigned long int mtime_last, unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, const char* nome_input, bool _time_is_estimated) { //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false? + + // EM: check is file has already been inserted, if so, update the informations (also in ROOT_TABLE_MERGING if processing ROOT_TABLE) instead of adding entries + stringstream oss1; + oss1.str() = ""; + string msg1; + stringstream ass; + ass.str(""); + ass << "SELECT ID_N, FOLDER_NAME, FILE_NAME, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, OBT_TIME_SYNC, LAST_TIME_SYNC_INFO, REAL_TIME_INIT, REAL_TIME_LAST, BOOT_NUMBER, TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME,TIME_IS_ESTIMATED,INSERTED_BY FROM " << table_name << " WHERE FILE_NAME='"<< file_name <<"';"; + TSQLResult* ares = NULL; + ares = sqlServer->Query(ass.str().c_str()); + if (!ares) { + oss1 << "DBError UNABLE to: " << ass.str().c_str() << endl; + msg1 = oss1.str(); + mainLogUtil->logError(msg1); + return false; + } + TSQLRow *arow = ares->Next(); + if ( arow ){ + TString bla=file_name; + TString *blabla=new TString(file_name); + blabla->Remove(15); + stringstream oss; + oss.str(""); +// oss << "UPDATE " << table_name +// << " set FOLDER_NAME='" << folder_name << "', PKT_NUMBER_INIT='" << pkt_number_in << "',PKT_NUMBER_FINAL='" << pkt_number_fin << "',PKT_OBT_INIT='" << obt_in << "',PKT_OBT_FINAL='" << obt_fin +// << "',OBT_TIME_SYNC='" << oT_sync << "',LAST_TIME_SYNC_INFO='" << lT_sync_info << "',REAL_TIME_INIT='"<< mtime_init << "',REAL_TIME_LAST='" << mtime_last << "',BOOT_NUMBER='" << mboot_num +// << "',TIME_OFFSET='" << time_offset << "',BAD_PKT='" << bad_pkt << "',BAD_PKT_READ='"<< bad_pkt_read << "',BAD_PKT_CALREAD='" << bad_pkt_CalRead << "',NUM_PKT_SAVED='" << num_PKT_Saved +// << "',INPUT_NAME='" << nome_input << "',INSERT_TIME=NULL,TIME_IS_ESTIMATED='"<< _time_is_estimated << "',INSERTED_BY='" << tag_value << "',ON_DISK='YES' where FILE_NAME='"<Data()<<"%';"; + string msg = oss.str(); + mainLogUtil->logAll(msg); + string query; + + TSQLResult* res = NULL; + + query = oss.str(); + msg1 = "DeleteROOT_DB_BAD query: "; + msg1 += query; + mainLogUtil->logInfo(msg1); + + res = sqlServer->Query(query.c_str()); + if (!res) { + oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; + msg1 = oss1.str(); + mainLogUtil->logError(msg1); + return false; + } + + stringstream iss; + iss.str(""); + iss << "select ROOT_TABLE_MERGING.ID_N from ROOT_TABLE_MERGING left join ROOT_TABLE on ROOT_TABLE_MERGING.ROOT_ID_N=ROOT_TABLE.ID_N where ROOT_TABLE.FILE_NAME like '"<Data()<<"%';"; + string msg2; + string query2; + TSQLResult* res2 = NULL; + query2 = iss.str(); + msg2 = "SaveROOT_DB query: "; + msg2 += query; + mainLogUtil->logInfo(msg2); + res2 = sqlServer->Query(query2.c_str()); + if (!res2) { + oss1 << "DBError UNABLE to: " << iss.str().c_str() << endl; + msg1 = oss1.str(); + mainLogUtil->logError(msg1); + return false; + } + TSQLRow *row2 = res2->Next(); + while ( row2 ){ + TString qq=Form("DELETE FROM ROOT_TABLE_MERGING where ID_N=%s;",row2->GetField(0)); + TSQLResult* res2b = NULL; + res2b = sqlServer->Query(qq.Data()); + delete res2b; + row2=res2->Next(); + }; + + + + oss.str(""); + oss << " DELETE FROM ROOT_TABLE WHERE FILE_NAME LIKE '"<Data()<<"%';"; + string ms = oss.str(); + mainLogUtil->logAll(ms); + string quer; + TSQLResult* re = NULL; + quer = oss.str(); + msg1 = "DeleteROOT_DB query: "; + msg1 += quer; + mainLogUtil->logInfo(msg1); + + re = sqlServer->Query(quer.c_str()); + if (!re) { + oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; + msg1 = oss1.str(); + mainLogUtil->logError(msg1); + return false; + } + + delete blabla; + + } + +// } else { + if ( true ){ stringstream oss; oss.str(""); oss << "INSERT INTO " << table_name @@ -880,9 +1161,6 @@ string msg = oss.str(); mainLogUtil->logAll(msg); - stringstream oss1; - oss1.str() = ""; - string msg1; string query; TSQLResult* res = NULL; @@ -898,12 +1176,15 @@ msg1 = oss1.str(); mainLogUtil->logError(msg1); return false; + } else { + delete res; + res = NULL; + } } - if(!strcmp(table_name,Table_ROOT_Good)) { //the ID of the current ROOT file in table Table_ROOT_Good - my_id = select_maxIDN_DB(table_name); + my_id = select_maxIDN_DB(table_name,file_name); if (!boot_number) { if (id_to_recover_index < 1000) id_to_recover[id_to_recover_index++] = my_id; @@ -911,11 +1192,11 @@ else if (boot_number && id_to_recover_index && !is_new_route) { recover_boot_number(); } - } // cout<<"DBG: my_id = "<logAll(msg); res = sqlServer->Query(oss.str().c_str()); @@ -1387,11 +1668,11 @@ TSQLResult* res = NULL; stringstream oss; oss.str(""); - if (!tableTobeLocked) - oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET - << " write, " << Table_ROOT_Merging << " write;"; - else - oss << "lock table " << tableTobeLocked << " write; "; + // if (!tableTobeLocked) + oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET + << " write, " << Table_ROOT_Merging << " write;"; + // else + // oss << "lock table " << tableTobeLocked << " write; "; string msg = oss.str(); mainLogUtil->logAll(msg);