/[PAMELA software]/chewbacca/PamOffLineSW/PacketUser.cpp
ViewVC logotype

Diff of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.12 by mocchiut, Wed Dec 30 14:49:51 2009 UTC revision 1.19 by mocchiut, Tue Feb 16 13:38:12 2010 UTC
# Line 1  Line 1 
1  //============================================================================  //============================================================================
2  // $Id: PacketUser.cpp,v 1.11 2009/12/30 10:46:33 mocchiut Exp $  // $Id: PacketUser.cpp,v 1.18 2010/02/11 09:02:09 mocchiut Exp $
3  // Description :  // Description :
4  //============================================================================  //============================================================================
5  #include "PacketUser.h"  #include "PacketUser.h"
# Line 31  extern bool do_cont_check; Line 31  extern bool do_cont_check;
31  //marco_new_31:  //marco_new_31:
32  extern char* db_user;  extern char* db_user;
33  extern char* db_pwd;  extern char* db_pwd;
34  extern char* connection;  extern char* pelosconnection;
35    
36  //per tenere conto reset obt  //per tenere conto reset obt
37  extern unsigned long int max_pkt_obt;  extern unsigned long int max_pkt_obt;
# Line 275  void PacketUser::setInit(unsigned long i Line 275  void PacketUser::setInit(unsigned long i
275  void PacketUser::StartGroup() {  void PacketUser::StartGroup() {
276    pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI    pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI
277    strcat(rootfilename, ".root");    strcat(rootfilename, ".root");
278      //if ( !pRun ) printf(" horror! -%s- \n",rootfilename);
279    reader->Init(pRun);    reader->Init(pRun);
280    stringstream oss;    stringstream oss;
281    oss.str() = "";    oss.str() = "";
# Line 333  void PacketUser::setTimeSync(char* packe Line 334  void PacketUser::setTimeSync(char* packe
334    }    }
335    else if (type == PacketType::Mcmd) {    else if (type == PacketType::Mcmd) {
336      //          mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");      //          mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");
337      return;      //    return;
338        offset = -1;
339    }    }
340    else {    else {
341      return;      return;
# Line 343  void PacketUser::setTimeSync(char* packe Line 345  void PacketUser::setTimeSync(char* packe
345    //  if ((obt_time_sync) || (last_time_sync_info))    //  if ((obt_time_sync) || (last_time_sync_info))
346    //    return;    //    return;
347    // no, do it, since there is a (small indeed) time drift due to different clock of PAMELA CPU and RESURS CPU...    // no, do it, since there is a (small indeed) time drift due to different clock of PAMELA CPU and RESURS CPU...
348      //  printf(" %s %lu %lu %lu %lu \n", rootfilename, pkt_number_init, pkt_number_last, obt_init, obt_last);
349      if ( offset >= 0 ){
350        // use Runheader or RunTrailer
351        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);
352        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);
353        //
354        // if you arrive here and RH/RT timesync is zero try to get the correct info from DB (dangerous??)
355        //
356        //    printf("qui\n");
357        if ( !obt_time_sync && !last_time_sync_info ){
358          //    if ( true ){
359          stringstream os;
360          os.str() = "";
361          os << " NEW (EM) AGH! NO GOOD TIMESYNC INFOS IN RH OR RT!! TRY TO LOOK IN DB";
362          string msg = os.str();
363          mainLogUtil->logAll(msg);
364    
365    // 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;        
366    // +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+
367    // | ID_N | FOLDER_NAME | FILE_NAME                  | PKT_NUMBER_INIT | PKT_NUMBER_FINAL | BAD_PKT | NUM_PKT_SAVED | INPUT_NAME                                              | INSERT_TIME         | INSERTED_BY |
368    // +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+
369    // | 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          |
370    // | 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          |
371    // | 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          |
372    // |  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          |
373    // |  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          |
374    // |  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          |
375    // |  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          |
376    // | 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          |
377    // | 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          |
378    // | 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          |
379    // |  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          |
380    // | 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          |
381    // |  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          |
382    // |  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          |
383    // |  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          |
384    // |  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          |
385    // |  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          |
386    // | 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          |
387    // | 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          |
388    // | 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          |
389    // +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+
390    // 20 rows in set (0.05 sec)
391    // 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%";
392    // +-----------------------------+-----------------------------+-------------------------+
393    // | FILE_NAME                   | abs(PKT_NUMBER_INIT-123139) | abs(0-PKT_NUMBER_FINAL) |
394    // +-----------------------------+-----------------------------+-------------------------+
395    // | L0PAM_02173_001_001_11.root |                      123103 |                     251 |
396    // +-----------------------------+-----------------------------+-------------------------+
397    // 1 row in set (0.07 sec)
398    //
399          TString *nomino=new TString(rootfilename);
400          nomino->Remove(9);
401          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());
402          delete nomino;
403          stringstream os3;
404          os3.str() = "";
405          os3 << " NEW (EM) TRY TO FIND TSYNC IN DB QUERY: " << mmyy.Data();
406          string msg3 = os3.str();
407          mainLogUtil->logAll(msg3);
408          //      
409          TSQLResult* res = NULL;
410          TSQLRow* row = NULL;
411          Bool_t apro = false;
412          if (!single_connection && !sqlServer ) {
413            OpenDBConnection(NULL);// no, locak tutto Nota qui ho deciso di lockare solo questa tabella e non tutte
414            apro = true;
415          }
416          res = sqlServer->Query(mmyy.Data());
417          if (!res) {
418            mainLogUtil->logError("DBError retrieve time sync");
419            return;
420          };
421          row = res->Next();
422          if ( row ){
423            obt_time_sync = (ULong_t)atoi(row->GetField(0));
424            last_time_sync_info = (ULong_t)atoi(row->GetField(1));
425            //
426            stringstream os2;
427            os2.str() = "";
428            os2 << " NEW (EM) FOUND TSYNC IN DB ";
429            string msg2 = os2.str();
430            mainLogUtil->logAll(msg2);
431            //
432          };
433          if (res) {
434            delete res;
435            res = NULL;
436          };
437          //
438          if (!single_connection && apro) {
439            CloseDBConnection();
440          };
441        };
442    
443    obt_time_sync = (((UINT32) packet[5 + offset] << 24) & 0xFF000000) + (((UINT32) packet[6 + offset] << 16)    } else {
444        & 0x00FF0000) + (((UINT32) packet[7 + offset] << 8) & 0x0000FF00) + (((UINT32) packet[8 + offset]) & 0x000000FF);      // Use Packet Mcmd now is not used to retrieve Absolute time
445    last_time_sync_info = (((UINT32) packet[9 + offset] << 24) & 0xFF000000) + (((UINT32) packet[10 + offset] << 16)      //    printf(" Pointer to Mcmd obj is %p \n",reader->GetPtr("Mcmd"));
446        & 0x00FF0000) + (((UINT32) packet[11 + offset] << 8) & 0x0000FF00)  
447        + (((UINT32) packet[12 + offset]) & 0x000000FF);      UInt_t recEntries = 0;
448        Double_t minimum = 0.;
449        Double_t maximum = 0.;
450        Double_t minimum2 = 0.;
451        Double_t maximum2 = 0.;
452        UInt_t TSYNC = 0;
453        UInt_t OBT = 0;
454        Bool_t existsts = false;
455        //
456        pamela::McmdEvent *mc = (pamela::McmdEvent*)(((pamela::techmodel::McmdReader*)reader->GetPtr("Mcmd"))->GetMcmd());
457        pamela::McmdRecord *mcrc = 0;
458        TArrayC *mcmddata = 0;
459        //
460        minimum = numeric_limits<Double_t>::max();
461        maximum = numeric_limits<Double_t>::min();
462        minimum2 = numeric_limits<Double_t>::max();
463        maximum2 = numeric_limits<Double_t>::min();
464        //
465        recEntries = mc->Records->GetEntries();
466        //    printf(" recEntries is %i \n",recEntries);
467    
468        for (UInt_t j = 0; j < recEntries; j++){
469          mcrc = (pamela::McmdRecord*)mc->Records->At(j);
470          mcmddata = mcrc->McmdData;
471          //
472          if (mcrc->ID1 == 0xE0 && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd timesync
473            //
474            OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
475            //
476            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);
477            //
478            //        printf("mcmd tsync tsync %u obt %u \n",TSYNC,OBT);
479            //
480            if ( TSYNC && OBT ){
481              existsts = true;
482              stringstream os;
483              os.str() = "";
484              os << " NEW (EM) FOUND TIMESYNC IN TIMESYNC MCMD";
485              string msg = os.str();
486              mainLogUtil->logAll(msg);
487              break;
488            };
489            //
490          };
491        };
492        //
493        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
494          //
495          Double_t timesync = 0.;
496          //
497          for (UInt_t j = 0; j < recEntries; j++){
498            mcrc = (pamela::McmdRecord*)mc->Records->At(j);
499            mcmddata = mcrc->McmdData;
500            //
501            if (mcrc->ID1 == 0xE2  && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd inclination  
502              timesync = 0.;
503              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);
504              //
505              if ( timesync > maximum2){
506                maximum2 = timesync;
507                OBT = (Int_t)(mcrc->MCMD_RECORD_OBT);
508              };
509            };
510            //
511          };
512          if ( maximum2 > numeric_limits<Double_t>::min() ){
513            TSYNC = (UInt_t)(maximum2 + 0.5);
514            if ( TSYNC && OBT ){
515              existsts = true;
516              stringstream os;
517              os.str() = "";
518              os << " NEW (EM) FOUND TIMESYNC IN INCLINATION MCMD";
519              string msg = os.str();
520              mainLogUtil->logAll(msg);
521            };
522          };
523          if ( labs(labs(last_time_sync_info_prevvalue-obt_time_sync_prevvalue)-labs(TSYNC-int(OBT/1000))) > 10 ) return;
524        };
525        if ( !existsts ) return;
526        //    printf(" TIMESYNC FROM MCMD FOUND: TSYNC = %i OBT = %i \n",TSYNC,OBT);    
527        obt_time_sync = int(OBT/1000);
528        last_time_sync_info = TSYNC;
529    
530      };
531    
532    stringstream oss;    stringstream oss;
533    oss.str() = "";    oss.str() = "";
# Line 457  unsigned long int PacketUser::retrieveTi Line 636  unsigned long int PacketUser::retrieveTi
636    //if(!table){table=Table_GL_RESURS_OFFSET}    //if(!table){table=Table_GL_RESURS_OFFSET}
637    
638    if (!single_connection) {    if (!single_connection) {
639      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
640    }    }
641    
642    UInt_t t0 = 0;//toffset    UInt_t t0 = 0;//toffset
# Line 632  void PacketUser::FinishGroup(char * file Line 811  void PacketUser::FinishGroup(char * file
811            msg = err.str();            msg = err.str();
812            mainLogUtil->logInfo(msg);            mainLogUtil->logInfo(msg);
813                        
814            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;"); //  <=========================
815              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); //  <=========================
816            TSQLResult *result = sqlServer->Query(qu.Data());            TSQLResult *result = sqlServer->Query(qu.Data());
817            TSQLRow    *row = result->Next();            TSQLRow    *row = result->Next();
818            Int_t eid = -1;            Int_t eid = -1;
# Line 647  void PacketUser::FinishGroup(char * file Line 827  void PacketUser::FinishGroup(char * file
827          }          }
828                    
829          CleanDisk();          CleanDisk();
830          if (!single_connection) {          //      if (!single_connection) {
831            CloseDBConnection();          //        CloseDBConnection();
832          }          //      }
833          return;          //      return; // commented here...
834    
835        };        };
836      }      }
837    
838      if ( real_time_init || real_time_last) {      if ( real_time_init || real_time_last) {
839      //saves info in DB in table Table_ROOT        //saves info in DB in table Table_ROOT
840      if (boot_number == 0)        if (boot_number == 0)
841        boot_number = boot_number_prevvalue;          boot_number = boot_number_prevvalue;
   
     if (saveROOT_DB(  
                     Table_ROOT_Good,  
                     outDir, // here outDir is good, no expand EMI  
                     filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info,  
                     real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader,  
                     numPKTSaved, fni, time_is_estimated) == true) {  
       oss.str() = "";  
       oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Good << " id= " << my_id;  
       msg = oss.str();  
       mainLogUtil->logInfo(msg);  
842                
843        if (tryMerge) {        if (saveROOT_DB(
844          merge_ROOTfiles();                        Table_ROOT_Good,
845        }                        outDir, // here outDir is good, no expand EMI
846      } else {                        filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info,
847                          real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader,
848                          numPKTSaved, fni, time_is_estimated) == true) {
849            oss.str() = "";
850            oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Good << " id= " << my_id;
851            msg = oss.str();
852            mainLogUtil->logInfo(msg);
853            
854            if (tryMerge) {
855              merge_ROOTfiles();
856            }
857          } else {
858        oss.str() = "";        oss.str() = "";
859        oss << "Problem storing information in DB regarding file: " << filename;        oss << "Problem storing information in DB regarding file: " << filename;
860        msg = oss.str();        msg = oss.str();
861        mainLogUtil->logError(msg);        mainLogUtil->logError(msg);
862      }        }
863      };      };
864    
865      // if dbinfo is not empty than      // if dbinfo is not empty than
866      if ( obt_time_sync && last_time_sync_info && arsize > 0 ){      if ( arsize > 0 ){
         
867    
868   //    if(!m_dbinfos.empty()){        if ( !obt_time_sync || !last_time_sync_info ){
869  //       while(!m_dbinfos.empty()){          TString *nomino=new TString(rootfilename);
870  //      dbinfo *app = m_dbinfos.back();          nomino->Remove(15);
871  //      m_dbinfos.pop_back();          TString qu = Form("select OBT_TIME_SYNC,LAST_TIME_SYNC_INFO from ROOT_TABLE where FILE_NAME like '%s_%%' and (abs(PKT_NUMBER_INIT-%lu)<50000 or abs(PKT_OBT_INIT-%lu)<3000000) limit 1;",nomino->Data(),pkt_number_init,obt_init);
872        Int_t ez=0;          //
873        while ( ez < arsize ){          oss.str() = "";
874          Int_t eid = idtorecover->At(ez);          oss << "(EM) Trying to find obt_time_sync and last_time_sync_info from DB, query is: " << qu.Data();
875          if ( eid > -1 ){          msg = oss.str();
876            mainLogUtil->logInfo(msg);
877            //
878            TSQLResult *result = sqlServer->Query(qu.Data());
879            TSQLRow    *row = result->Next();
880            if ( row && result->GetRowCount() == 1 ){
881              obt_time_sync = (unsigned long int)atoll(row->GetField(0));
882              last_time_sync_info = (unsigned long int)atoll(row->GetField(1));
883              oss.str() = "";
884              oss << "(EM) found obt_time_sync and last_time_sync_info from DB! obt_time_sync =  " <<obt_time_sync <<" last_time_sync_info = " << last_time_sync_info;
885              msg = oss.str();
886              mainLogUtil->logInfo(msg);
887            };      
888            delete nomino;
889          };      
890    
891          if ( obt_time_sync && last_time_sync_info ){
892            //    if(!m_dbinfos.empty()){
893            //       while(!m_dbinfos.empty()){
894            //      dbinfo *app = m_dbinfos.back();
895            //      m_dbinfos.pop_back();
896            Int_t ez=0;
897            while ( ez < arsize ){
898              Int_t eid = idtorecover->At(ez);
899              if ( eid > -1 ){
900                        
901            TString qu = Form("select FOLDER_NAME,FILE_NAME,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,BOOT_NUMBER,TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,TIME_IS_ESTIMATED from ROOT_TABLE_BAD where ID_N=%i;",eid);              TString qu = Form("select FOLDER_NAME,FILE_NAME,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,BOOT_NUMBER,TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,TIME_IS_ESTIMATED from ROOT_TABLE_BAD where ID_N=%i;",eid);
902            TSQLResult *result = sqlServer->Query(qu.Data());              TSQLResult *result = sqlServer->Query(qu.Data());
903            TSQLRow    *row = result->Next();              TSQLRow    *row = result->Next();
904                                        
905            TString efolder_name="";              TString efolder_name="";
906            TString efile_name="";              TString efile_name="";
907            unsigned long int epkt_number_in=0ULL;              unsigned long int epkt_number_in=0ULL;
908            unsigned long int epkt_number_fin=0ULL;              unsigned long int epkt_number_fin=0ULL;
909            unsigned long int eobt_in=0ULL;              unsigned long int eobt_in=0ULL;
910            unsigned long int eobt_fin=0ULL;              unsigned long int eobt_fin=0ULL;
911            unsigned long int emboot_num=0ULL;              unsigned long int emboot_num=0ULL;
912            unsigned long int etime_offset=0ULL;              unsigned long int etime_offset=0ULL;
913            int ebad_pkt=0;              int ebad_pkt=0;
914            int ebad_pkt_read=0;              int ebad_pkt_read=0;
915            int ebad_pkt_CalRead=0;              int ebad_pkt_CalRead=0;
916            int enum_PKT_Saved=0;              int enum_PKT_Saved=0;
917            TString enome_input="";              TString enome_input="";
918            bool e_time_is_estimated=false;              bool e_time_is_estimated=false;
919    
920            if ( row ){              if ( row ){
921              efolder_name =                      (TString)row->GetField(0);                efolder_name =                      (TString)row->GetField(0);
922              efile_name =                        (TString)row->GetField(1);                efile_name =                        (TString)row->GetField(1);
923              epkt_number_in =    (unsigned long int)atoll(row->GetField(2));                epkt_number_in =    (unsigned long int)atoll(row->GetField(2));
924              epkt_number_fin =   (unsigned long int)atoll(row->GetField(3));                epkt_number_fin =   (unsigned long int)atoll(row->GetField(3));
925              eobt_in =           (unsigned long int)atoll(row->GetField(4));                eobt_in =           (unsigned long int)atoll(row->GetField(4));
926              eobt_fin =          (unsigned long int)atoll(row->GetField(5));                eobt_fin =          (unsigned long int)atoll(row->GetField(5));
927              emboot_num =        (unsigned long int)atoll(row->GetField(6));                emboot_num =        (unsigned long int)atoll(row->GetField(6));
928              etime_offset =      (unsigned long int)atoll(row->GetField(7));                etime_offset =      (unsigned long int)atoll(row->GetField(7));
929              ebad_pkt =                         (int)atoi(row->GetField(8));                ebad_pkt =                         (int)atoi(row->GetField(8));
930              ebad_pkt_read =                    (int)atoi(row->GetField(9));                ebad_pkt_read =                    (int)atoi(row->GetField(9));
931              ebad_pkt_CalRead =                (int)atoi(row->GetField(10));                ebad_pkt_CalRead =                (int)atoi(row->GetField(10));
932              enum_PKT_Saved =                  (int)atoi(row->GetField(11));                enum_PKT_Saved =                  (int)atoi(row->GetField(11));
933              enome_input =                      (TString)row->GetField(12);                enome_input =                      (TString)row->GetField(12);
934              e_time_is_estimated =            (bool)atoi(row->GetField(13));                e_time_is_estimated =            (bool)atoi(row->GetField(13));
935            };              };
936            delete result;              delete result;
937    
938            setReal_TimeRECOVERY(              setReal_TimeRECOVERY(
939                                 real_time_init,                                   real_time_init,
940                                 real_time_last,                                   real_time_last,
941                                 eobt_in,                                   eobt_in,
942                                 eobt_fin                                   eobt_fin
943                                 );                                   );
944            //              //
945                        
946            if (saveROOT_DB(              if (saveROOT_DB(
947                            Table_ROOT_Good,                              Table_ROOT_Good,
948                            efolder_name.Data(),                              efolder_name.Data(),
949                            efile_name.Data(),                              efile_name.Data(),
950                            epkt_number_in,                              epkt_number_in,
951                            epkt_number_fin, eobt_in, eobt_fin, obt_time_sync, last_time_sync_info,                              epkt_number_fin, eobt_in, eobt_fin, obt_time_sync, last_time_sync_info,
952                            real_time_init, real_time_last, emboot_num, etime_offset, ebad_pkt, ebad_pkt_read, ebad_pkt_CalRead,                              real_time_init, real_time_last, emboot_num, etime_offset, ebad_pkt, ebad_pkt_read, ebad_pkt_CalRead,
953                            enum_PKT_Saved, enome_input.Data(), e_time_is_estimated) == true) {                              enum_PKT_Saved, enome_input.Data(), e_time_is_estimated) == true) {
954              oss.str() = "";                oss.str() = "";
955              oss << "\nTAG(tassa): RECOVERED!!!! Saved information regarding file: " << efile_name << " in table " << Table_ROOT_Good << " id= " << my_id;                oss << "\nTAG(tassa): RECOVERED!!!! Saved information regarding file: " << efile_name << " in table " << Table_ROOT_Good << " id= " << my_id;
956              msg = oss.str();                msg = oss.str();
957              mainLogUtil->logInfo(msg);                mainLogUtil->logInfo(msg);
958              /*merging we use global variable. we haveto set with current value*/                /*merging we use global variable. we haveto set with current value*/
959                            
960              strcpy(outDir,efolder_name.Data() );                strcpy(outDir,efolder_name.Data() );
961              //      outDir=efolder_name.Data();                //            outDir=efolder_name.Data();
962              strcpy(filename,efile_name.Data() );                strcpy(filename,efile_name.Data() );
963              //      filename=efile_name.Data();                //            filename=efile_name.Data();
964              pkt_number_init=epkt_number_in;                pkt_number_init=epkt_number_in;
965              pkt_number_last=epkt_number_fin;                pkt_number_last=epkt_number_fin;
966              obt_init=eobt_in;                obt_init=eobt_in;
967              obt_last=eobt_fin;                obt_last=eobt_fin;
968              boot_number=emboot_num;                boot_number=emboot_num;
969              timeOffset=etime_offset;                timeOffset=etime_offset;
970              bad_pkt=ebad_pkt;                bad_pkt=ebad_pkt;
971              bad_pkt_EventReader=ebad_pkt_read;                bad_pkt_EventReader=ebad_pkt_read;
972              bad_pkt_CalibReader=ebad_pkt_CalRead;                bad_pkt_CalibReader=ebad_pkt_CalRead;
973              numPKTSaved=enum_PKT_Saved;                numPKTSaved=enum_PKT_Saved;
974              strcpy(fni,enome_input.Data() );                strcpy(fni,enome_input.Data() );
975              //      fni=enome_input.Data();                //            fni=enome_input.Data();
976              time_is_estimated=e_time_is_estimated;                time_is_estimated=e_time_is_estimated;
977                            
978              if (tryMerge) {                if (tryMerge) {
979                merge_ROOTfiles();                  merge_ROOTfiles();
980              }                }
981                            
982              TString qu2 = Form("update ROOT_TABLE_BAD set ON_DISK='MVD' where ID_N=%i;",eid);                TString qu2 = Form("update ROOT_TABLE_BAD set ON_DISK='MVD' where ID_N=%i;",eid);
983              sqlServer->Query(qu2.Data());                sqlServer->Query(qu2.Data());
984    
985                            
986            }              }
987            else {              else {
988              oss.str() = "";                oss.str() = "";
989              oss << "Problem storing information in DB regarding file: " << filename;                oss << "Problem storing information in DB regarding file: " << filename;
990              msg = oss.str();                msg = oss.str();
991              mainLogUtil->logError(msg);                mainLogUtil->logError(msg);
992            }              }
993                        
994                        
995          };            };
996          ez++;            ez++;
997        }          }
998        //          //
999        idtorecover->Reset();          idtorecover->Reset();
1000        delete idtorecover;          delete idtorecover;
1001        idtorecover = new TArrayI(100);          idtorecover = new TArrayI(100);
1002        arsize =  0;          arsize =  0;
1003        //        delete app;          //      delete app;
1004      }        };
1005        };
1006            
1007      CleanDisk();      CleanDisk();
1008      if (!single_connection) {      if (!single_connection) {
# Line 807  void PacketUser::FinishGroup(char * file Line 1011  void PacketUser::FinishGroup(char * file
1011    
1012    }//pRun    }//pRun
1013  }  }
1014      
1015        
1016    void PacketUser::CleanDisk(){    void PacketUser::CleanDisk(){
1017      if ( !candelete ) return;      if ( !candelete ) return;
# Line 844  bool PacketUser::saveROOT_DB(const char* Line 1048  bool PacketUser::saveROOT_DB(const char*
1048      unsigned long int mtime_last, unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt,      unsigned long int mtime_last, unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt,
1049      int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, const char* nome_input, bool _time_is_estimated) {      int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, const char* nome_input, bool _time_is_estimated) {
1050    //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?    //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
1051      
1052      // 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
1053      stringstream oss1;
1054      oss1.str() = "";
1055      string msg1;
1056      stringstream ass;
1057      ass.str("");
1058      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 <<"';";
1059      TSQLResult* ares = NULL;
1060      ares = sqlServer->Query(ass.str().c_str());
1061      if (!ares) {
1062        oss1 << "DBError UNABLE to: " << ass.str().c_str() << endl;
1063        msg1 = oss1.str();
1064        mainLogUtil->logError(msg1);
1065        return false;
1066      }
1067      TSQLRow *arow = ares->Next();
1068      if ( arow ){
1069            TString bla=file_name;
1070            TString *blabla=new TString(file_name);
1071          blabla->Remove(15);
1072      stringstream oss;
1073      oss.str("");
1074    //  oss << "UPDATE " << table_name
1075    //      << " 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
1076    //      << "',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
1077    //      << "',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
1078    //      << "',INPUT_NAME='" << nome_input << "',INSERT_TIME=NULL,TIME_IS_ESTIMATED='"<< _time_is_estimated << "',INSERTED_BY='" << tag_value << "',ON_DISK='YES' where FILE_NAME='"<<file_name<<"';";
1079            oss << " DELETE FROM ROOT_TABLE_BAD WHERE FILE_NAME LIKE '"<<blabla->Data()<<"%';";
1080      string msg = oss.str();
1081      mainLogUtil->logAll(msg);
1082      string query;
1083    
1084      TSQLResult* res = NULL;
1085    
1086      query = oss.str();
1087      msg1 = "DeleteROOT_DB_BAD query: ";
1088      msg1 += query;
1089      mainLogUtil->logInfo(msg1);
1090    
1091      res = sqlServer->Query(query.c_str());
1092      if (!res) {
1093        oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1094        msg1 = oss1.str();
1095        mainLogUtil->logError(msg1);
1096        return false;
1097      }
1098    
1099       stringstream iss;
1100       iss.str("");
1101       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 '"<<blabla->Data()<<"%';";
1102       string msg2;
1103       string query2;
1104       TSQLResult* res2 = NULL;
1105       query2 = iss.str();
1106       msg2 = "SaveROOT_DB query: ";
1107       msg2 += query;
1108       mainLogUtil->logInfo(msg2);
1109       res2 = sqlServer->Query(query2.c_str());
1110       if (!res2) {
1111         oss1 << "DBError UNABLE to: " << iss.str().c_str() << endl;
1112         msg1 = oss1.str();
1113         mainLogUtil->logError(msg1);
1114         return false;
1115       }
1116       TSQLRow *row2 = res2->Next();
1117       while ( row2 ){
1118            TString qq=Form("DELETE FROM ROOT_TABLE_MERGING where ID_N=%s;",row2->GetField(0));
1119            TSQLResult* res2b = NULL;
1120            res2b = sqlServer->Query(qq.Data());
1121            delete res2b;
1122            row2=res2->Next();      
1123       };
1124    
1125    
1126    
1127      oss.str("");
1128      oss << " DELETE FROM ROOT_TABLE WHERE FILE_NAME LIKE '"<<blabla->Data()<<"%';";
1129      string ms = oss.str();
1130      mainLogUtil->logAll(ms);
1131      string quer;
1132      TSQLResult* re = NULL;
1133      quer = oss.str();
1134      msg1 = "DeleteROOT_DB query: ";
1135      msg1 += quer;
1136      mainLogUtil->logInfo(msg1);
1137    
1138      re = sqlServer->Query(quer.c_str());
1139      if (!re) {
1140        oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1141        msg1 = oss1.str();
1142        mainLogUtil->logError(msg1);
1143        return false;
1144      }
1145    
1146            delete blabla;
1147            
1148      }
1149    
1150    //  } else {
1151      if ( true ){
1152    stringstream oss;    stringstream oss;
1153    oss.str("");    oss.str("");
1154    oss << "INSERT INTO " << table_name    oss << "INSERT INTO " << table_name
# Line 856  bool PacketUser::saveROOT_DB(const char* Line 1161  bool PacketUser::saveROOT_DB(const char*
1161    string msg = oss.str();    string msg = oss.str();
1162    mainLogUtil->logAll(msg);    mainLogUtil->logAll(msg);
1163    
   stringstream oss1;  
   oss1.str() = "";  
   string msg1;  
1164    string query;    string query;
1165    
1166    TSQLResult* res = NULL;    TSQLResult* res = NULL;
# Line 874  bool PacketUser::saveROOT_DB(const char* Line 1176  bool PacketUser::saveROOT_DB(const char*
1176      msg1 = oss1.str();      msg1 = oss1.str();
1177      mainLogUtil->logError(msg1);      mainLogUtil->logError(msg1);
1178      return false;      return false;
1179      } else {
1180        delete res;
1181        res = NULL;
1182      }
1183    }    }
   
1184    
1185    if(!strcmp(table_name,Table_ROOT_Good)) {    if(!strcmp(table_name,Table_ROOT_Good)) {
1186      //the ID of the current ROOT file in table Table_ROOT_Good      //the ID of the current ROOT file in table Table_ROOT_Good
1187      my_id = select_maxIDN_DB(table_name);      my_id = select_maxIDN_DB(table_name,file_name);
1188      if (!boot_number) {      if (!boot_number) {
1189        if (id_to_recover_index < 1000)        if (id_to_recover_index < 1000)
1190          id_to_recover[id_to_recover_index++] = my_id;          id_to_recover[id_to_recover_index++] = my_id;
# Line 887  bool PacketUser::saveROOT_DB(const char* Line 1192  bool PacketUser::saveROOT_DB(const char*
1192      else if (boot_number && id_to_recover_index && !is_new_route) {      else if (boot_number && id_to_recover_index && !is_new_route) {
1193        recover_boot_number();        recover_boot_number();
1194      }      }
   }  
1195    //    cout<<"DBG: my_id = "<<my_id<<endl;    //    cout<<"DBG: my_id = "<<my_id<<endl;
1196    if (res) {    }
1197      delete res;    if (ares) {
1198      res = NULL;      delete ares;
1199        ares = NULL;
1200    }    }
1201    return true;    return true;
1202  }  }
# Line 1301  bool PacketUser::saveMergeROOT_DB(const Line 1606  bool PacketUser::saveMergeROOT_DB(const
1606  }  }
1607    
1608  //Utility: return max(IDN)  //Utility: return max(IDN)
1609  unsigned int PacketUser::select_maxIDN_DB(const char* table_name) {  unsigned int PacketUser::select_maxIDN_DB(const char* table_name, const char* file_name) {
1610    
1611    if (!strcmp(table_name, Table_ROOT_Bad)) {    if (!strcmp(table_name, Table_ROOT_Bad)) {
1612      return 0;      return 0;
# Line 1310  unsigned int PacketUser::select_maxIDN_D Line 1615  unsigned int PacketUser::select_maxIDN_D
1615    TSQLResult* res = NULL;    TSQLResult* res = NULL;
1616    stringstream oss;    stringstream oss;
1617    oss.str("");    oss.str("");
1618    oss << "select max(ID_N) from " << table_name;    oss << "select ID_N from " << table_name << " where FILE_NAME='" << file_name <<"';";
1619    //    string msg = oss.str();    //    string msg = oss.str();
1620    //    mainLogUtil->logAll(msg);    //    mainLogUtil->logAll(msg);
1621    res = sqlServer->Query(oss.str().c_str());    res = sqlServer->Query(oss.str().c_str());
# Line 1363  int PacketUser::LockTables(const char* t Line 1668  int PacketUser::LockTables(const char* t
1668    TSQLResult* res = NULL;    TSQLResult* res = NULL;
1669    stringstream oss;    stringstream oss;
1670    oss.str("");    oss.str("");
1671    if (!tableTobeLocked)    //  if (!tableTobeLocked)
1672      oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET    oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET
1673          << " write, " << Table_ROOT_Merging << " write;";        << " write, " << Table_ROOT_Merging << " write;";
1674    else      //  else
1675      oss << "lock table " << tableTobeLocked << " write; ";      //    oss << "lock table " << tableTobeLocked << " write; ";
1676    
1677    string msg = oss.str();    string msg = oss.str();
1678    mainLogUtil->logAll(msg);    mainLogUtil->logAll(msg);
# Line 1437  void PacketUser::OpenDBConnection(const Line 1742  void PacketUser::OpenDBConnection(const
1742    if (do_cont_check) {    if (do_cont_check) {
1743      //controllo che non sia gia' aperta      //controllo che non sia gia' aperta
1744      if (!sqlServer) {      if (!sqlServer) {
1745        sqlServer = TSQLServer::Connect(connection, db_user, db_pwd);        sqlServer = TSQLServer::Connect(pelosconnection, db_user, db_pwd);
1746        if ((!sqlServer) || (!(sqlServer->IsConnected()))) {        if ((!sqlServer) || (!(sqlServer->IsConnected()))) {
1747          cout << "Can not connect with MYSQL sever" << endl;          cout << "Can not connect with MYSQL sever" << endl;
1748        }        }

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.19

  ViewVC Help
Powered by ViewVC 1.1.23