/[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.1 by mocchiut, Tue Sep 23 07:20:12 2008 UTC revision 1.5 by mocchiut, Mon Dec 22 16:50:10 2008 UTC
# Line 1  Line 1 
1  //============================================================================  //============================================================================
2  // $Id: PacketUser.cpp,v 1.67 2008-09-05 14:33:48 messineo Exp $  // $Id: PacketUser.cpp,v 1.4 2008/12/18 14:46:56 mocchiut Exp $
3  // Description :  // Description :
4  //============================================================================  //============================================================================
5  #include "PacketUser.h"  #include "PacketUser.h"
# Line 32  extern char*  db_user; Line 32  extern char*  db_user;
32  extern char*  db_pwd;  extern char*  db_pwd;
33  extern char* connection;          extern char* connection;        
34    
35    //per tenere conto reset obt
36    extern unsigned long int  max_pkt_obt;
37    
38    #define TAGVALUELEN 4
39    extern  char tag_value[TAGVALUELEN];
40    
41  PacketUser PacketUser::instance;  PacketUser PacketUser::instance;
42                    
43  int PacketUser::numDiscontinity=1;  int PacketUser::numDiscontinity=1;
# Line 55  PacketUser::PacketUser() Line 61  PacketUser::PacketUser()
61          obt_last=0;          obt_last=0;
62          obt_time_sync=0;          obt_time_sync=0;
63          last_time_sync_info=0;          last_time_sync_info=0;
64    
65            //(tassa)
66            obt_time_sync_prevvalue=0;
67            last_time_sync_info_prevvalue=0;
68            time_is_estimated=false;
69            
70    
71          real_time_init=0;          real_time_init=0;
72          real_time_last=0;          real_time_last=0;
73          bad_pkt=0;          bad_pkt=0;
# Line 70  PacketUser::PacketUser() Line 83  PacketUser::PacketUser()
83  //      good_pkt_Calib=0;  //      good_pkt_Calib=0;
84          my_id=0;          my_id=0;
85          boot_number=0;          boot_number=0;
86            //(tassa)      
87            boot_number_prevvalue=0;
88    
89            //(tassa)
90            id_to_recover[1000];
91            id_to_recover_index=0;  
92  }  }
93    
94  PacketUser::~PacketUser()  PacketUser::~PacketUser()
# Line 84  PacketUser::~PacketUser() Line 103  PacketUser::~PacketUser()
103          obt_last=0;          obt_last=0;
104          obt_time_sync = 0;          obt_time_sync = 0;
105          last_time_sync_info = 0;          last_time_sync_info = 0;
106            obt_time_sync_prevvalue=0;
107            last_time_sync_info_prevvalue=0;
108            time_is_estimated=false;
109    
110          real_time_init=0;          real_time_init=0;
111          real_time_last=0;          real_time_last=0;
112          bad_pkt=0;          bad_pkt=0;
# Line 92  PacketUser::~PacketUser() Line 115  PacketUser::~PacketUser()
115  //      good_pkt_Calib=0;  //      good_pkt_Calib=0;
116          my_id=0;                  my_id=0;        
117          boot_number=0;          boot_number=0;
118            boot_number_prevvalue=0;
119  }  }
120    
121    
122  //Put the packet in a root file. Create a new ROOT file for each group of packets  //Put the packet in a root file. Create a new ROOT file for each group of packets
123  void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type,  void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type,
124                  unsigned long int counter, unsigned long int obt)                  unsigned long int counter, unsigned long int obt)
125  {                        {                      
126          //If the packet type was not recognised before          //If the packet type was not recognised before
127          if(!type){return;}          if(!type){return;}
128                              
           
 /*      //zzzzzz marco per debug mi salvo il pacchetto vardump  
         if(type==PacketType::VarDump)  
         {                
                 long int  dataLength = length - 2; //the block of data  
                 int b_offset = 4;  
                 while (b_offset < dataLength){  
                  boot_number  = (((UINT32)pamPkt[1+b_offset]<<24)&0xFF000000) + (((UINT32)pamPkt[2+b_offset]<<16)&0x00FF0000) +  (((UINT32)pamPkt[3+b_offset]<<8)&0x0000FF00) + (((UINT32)pamPkt[4+b_offset])&0x000000FF);  
                  cout<<"IN "<<numPKTSaved<<" boot_number "<<boot_number<<" al b_offset "<<b_offset<<" di "<<dataLength<<endl;  
                   
                  b_offset = b_offset + 5;  
                 }  
                   
                  cout<<"IN "<<numPKTSaved<<" FINAL boot_number "<<boot_number<<endl;  
                 savePKT_file(headerPkt,pamPkt,length,false,"vardump");  
                 numPKTSaved++;  
         }  
         return;  
 */  
           
           
129          //in order to start ...          //in order to start ...
130          if(numPKT==0)          if(numPKT==0)
131          {                        {              
                 //marco_new_31: metto qui se voglio avere come in origine apertura Connessione all'inizio e chiusura alla fine  
                 //marco_new_01    
132                  if(single_connection){                  if(single_connection){
133                          OpenDBConnection(NULL);                          OpenDBConnection(NULL);
134                  }                  }
# Line 139  void PacketUser::usePKT(char*& headerPkt Line 141  void PacketUser::usePKT(char*& headerPkt
141          if((!isCons)&&(numPKT!=0))          if((!isCons)&&(numPKT!=0))
142          {                                {                      
143                  setReal_Time();                                          setReal_Time();                        
144                  //closing the  group in file rootfilename                  //closing the group in file rootfilename
145                  FinishGroup(rootfilename);                                FinishGroup(rootfilename);              
146                                    
147                  //numDiscontinity++;                  //se gia' e' cambiato download non cambio numdisco
148                  //se gia' e' cambiato download nn cambio numdisco                  if(!is_new_route){numDiscontinity++;}
149                  if(!is_new_route) numDiscontinity++;  
                   
150                  //the current pkt will be placed in a new group: rootfilename                            //the current pkt will be placed in a new group: rootfilename          
151                  setInit(counter,obt);                                    setInit(counter,obt);                  
152                  StartGroup();                  StartGroup();
# Line 166  void PacketUser::usePKT(char*& headerPkt Line 167  void PacketUser::usePKT(char*& headerPkt
167          ret = 10 if the packet is good but comes from a cadre with VRL problems          ret = 10 if the packet is good but comes from a cadre with VRL problems
168          */          */
169                    
170          //here we know if the packet comes from one or more corrupted cadres          //here we also know if the packet comes from one or more corrupted cadres
         //if(!isPKTGood){bad_pkt++;}  
171          if((!isPKTGood)&&(!ret)){ret=10;}                if((!isPKTGood)&&(!ret)){ret=10;}      
           
172          switch (ret)          switch (ret)
173          {          {
174                  case 0: {numPKTSaved++; break;}                  case 0: {numPKTSaved++; break;}
# Line 201  void PacketUser::usePKT(char*& headerPkt Line 200  void PacketUser::usePKT(char*& headerPkt
200  //      if((ret==0)||(ret==10))//I want to use only good packet  //      if((ret==0)||(ret==10))//I want to use only good packet
201          if(ret>=0)//I want to use only accepted packet          if(ret>=0)//I want to use only accepted packet
202          {                {      
203                  setTimeSync(pamPkt, length, type);//marco_new: here I retrieve also the boot_number if packet type is vardump                  setTimeSync(pamPkt, length, type);//here I retrieve also the boot_number if packet type is vardump
204          }          }
205    
206          //just to be sure ...          //just to be sure ...
207          if(pamPkt){delete[] pamPkt; pamPkt = NULL;}              if(pamPkt){delete[] pamPkt; pamPkt = NULL;}    
         //just to be sure ...  
208          if(headerPkt){delete[] headerPkt; headerPkt = NULL;}          if(headerPkt){delete[] headerPkt; headerPkt = NULL;}
209    
210          numPKT++;                        numPKT++;              
# Line 230  void PacketUser::setInit(unsigned long i Line 228  void PacketUser::setInit(unsigned long i
228                    
229          //marco_NB:          //marco_NB:
230          //ogni volta che trovo una discontinuit� resetto come se mi trovassi in un nuovo download oppure uso i vecchi valori se esistono?          //ogni volta che trovo una discontinuit� resetto come se mi trovassi in un nuovo download oppure uso i vecchi valori se esistono?
231          obt_time_sync=0; last_time_sync_info=0; boot_number=0;  
232            obt_time_sync_prevvalue=obt_time_sync;
233            last_time_sync_info_prevvalue=last_time_sync_info;
234            time_is_estimated=false;
235            obt_time_sync=0; last_time_sync_info=0;
236            boot_number_prevvalue=boot_number;
237            boot_number=0;
238    
239  //      NB: se ho discontinuita in genere non so a che download appartiene potrebbe proprio essere sbagliato tenere i vecchi valori  //      NB: se ho discontinuita in genere non so a che download appartiene potrebbe proprio essere sbagliato tenere i vecchi valori
240    
241  }  }
# Line 238  void PacketUser::setInit(unsigned long i Line 243  void PacketUser::setInit(unsigned long i
243  //starts a new root file  //starts a new root file
244  void PacketUser::StartGroup()  void PacketUser::StartGroup()
245  {        {      
246          pRun = new PamelaRun(rootfilename, outDir , multiFile, compression);          pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI
         //marco_new: aggiunta estensione .root  
247          strcat(rootfilename,".root");          strcat(rootfilename,".root");
248          reader->Init(pRun);                              reader->Init(pRun);                    
249          stringstream oss;          stringstream oss;
# Line 258  void PacketUser::setLast(unsigned long i Line 262  void PacketUser::setLast(unsigned long i
262    
263    
264    
265  //retrieve obt_time_sync and last_time_sync_info from packet if type has special values  //retrieve obt_time_sync and last_time_sync_info from packet if type has special values and BOOT_NUMBER
266  void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type)  void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type)
267  {  {
268      //do nothing      //do nothing
# Line 269  void PacketUser::setTimeSync(char* packe Line 273  void PacketUser::setTimeSync(char* packe
273          }          }
274                    
275  //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0  //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0
276  //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi  //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi                
277            // devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download
278            //When I found a new download I need to reset all values
         // devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download  
           
         //When I found a new download I need to reset all values  
279          if(is_new_route)          if(is_new_route)
280          {            {
281            //reset                  //reset
282                  is_new_route=false;                  is_new_route=false;
283                    //(tassa) ci ho ripensato se stiamo in un secondo download non prendiamo i bootnumber e timesync del precedente!  
284                    obt_time_sync_prevvalue=0;//obt_time_sync;
285                    last_time_sync_info_prevvalue=0;//last_time_sync_info;
286            
287                  obt_time_sync=0; last_time_sync_info=0;                  obt_time_sync=0; last_time_sync_info=0;
288                    boot_number_prevvalue=0;//boot_number;
289                  boot_number=0;                  boot_number=0;
290                    time_is_estimated=false;
291                  if(time_Offset)                  if(time_Offset)
292                          timeOffset=time_Offset;                          timeOffset=time_Offset;
293                  else                              else            
# Line 326  void PacketUser::setTimeSync(char* packe Line 333  void PacketUser::setTimeSync(char* packe
333  //Boot Number  //Boot Number
334  void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)  void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)
335  {  {
336          //se gia calcolato esco: lo calcolo una volta per download e per continuit�            //se gia calcolato esco: lo calcolo una volta per download e per continuita'    
337          if(boot_number) return;          if(boot_number) return;
338  //todo: controlla se algo giusto: preso da VarDumpReader.cpp          
 //mmm e' sbagliatissimo cambiare !!!      
339          if(type==PacketType::VarDump)          if(type==PacketType::VarDump)
340          {                                {      
341                    int b_offset = 34;//4+5*6
342                    boot_number  = (((UINT32)packet[1+b_offset]<<24)&0xFF000000) + (((UINT32)packet[2+b_offset]<<16)&0x00FF0000) +  (((UINT32)packet[3+b_offset]<<8)&0x0000FF00) + (((UINT32)packet[4+b_offset])&0x000000FF);
343    
344    /*
345                  long int  dataLength = pktLength - 2; //the block of data                  long int  dataLength = pktLength - 2; //the block of data
346                  int b_offset = 4;                  int b_offset = 4;              
347                  while (b_offset < dataLength){                  while (b_offset < dataLength){
348                   boot_number  = (((UINT32)packet[1+b_offset]<<24)&0xFF000000) + (((UINT32)packet[2+b_offset]<<16)&0x00FF0000) +  (((UINT32)packet[3+b_offset]<<8)&0x0000FF00) + (((UINT32)packet[4+b_offset])&0x000000FF);                   boot_number  = (((UINT32)packet[1+b_offset]<<24)&0xFF000000) + (((UINT32)packet[2+b_offset]<<16)&0x00FF0000) +  (((UINT32)packet[3+b_offset]<<8)&0x0000FF00) + (((UINT32)packet[4+b_offset])&0x000000FF);
349                   b_offset = b_offset + 5;                   b_offset = b_offset + 5;
350                  }                  }
351    */              
352                  stringstream oss;                  stringstream oss;
353                  oss.str()="";                  oss.str()="";
354                  oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();                  oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();
# Line 354  void PacketUser::setBootNumber(char* pac Line 365  void PacketUser::setBootNumber(char* pac
365  //set the real time of the first packet and the last packet oif a group of packet if possible  //set the real time of the first packet and the last packet oif a group of packet if possible
366  void PacketUser::setReal_Time()  void PacketUser::setReal_Time()
367  {  {
368          if((obt_time_sync)||(last_time_sync_info))  
369          {                        if(!obt_time_sync  && !last_time_sync_info && numPKTSaved > 1000 )
370                  real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;          {
371                  real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;                  time_is_estimated=true;
372              obt_time_sync=obt_time_sync_prevvalue;
373              last_time_sync_info=last_time_sync_info_prevvalue;
374            }  
375            if(obt_time_sync  || last_time_sync_info)
376              {                                                                                
377                real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
378                    //se obt si e' resettato
379                    if(obt_last<obt_init)
380                    {
381                            real_time_last=(max_pkt_obt/1000+ obt_last/1000-obt_time_sync)+last_time_sync_info;    
382                            stringstream oss;
383                            oss.str()="";
384                            oss<<"obt_last("<<obt_last<<") < obt_init("<<obt_init<<"). It is due to Pamela Reset. Adding max_pkt_obt("<<max_pkt_obt<<") to obt_last in order to compute real_time_last";
385                            string msg = oss.str();
386                            mainLogUtil->logWarning(msg);
387                    }
388                    else
389                            real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
390                  real_time_init+=timeOffset;                  real_time_init+=timeOffset;
391                  real_time_last+=timeOffset;                              real_time_last+=timeOffset;    
392          }                }
393  }  }
394    
395  //timeOffset  //timeOffset
# Line 371  unsigned long int PacketUser::retrieveTi Line 400  unsigned long int PacketUser::retrieveTi
400                    
401          //if(!table){table=Table_GL_RESURS_OFFSET}          //if(!table){table=Table_GL_RESURS_OFFSET}
402    
         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
         //marco_new_01    
403          if(!single_connection){          if(!single_connection){
404                  OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte                  OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte
405          }          }
406    
407          UInt_t t0 = 0;  //toffset                        UInt_t t0 = 0;//toffset                                
                           
408          stringstream oss;          stringstream oss;
409          oss.str("");          oss.str("");
410          oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<<      table <<" WHERE SPECIAL_FILE='"          oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<<      table <<" WHERE SPECIAL_FILE='"
# Line 429  unsigned long int PacketUser::retrieveTi Line 455  unsigned long int PacketUser::retrieveTi
455          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
456          if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}        
457    
         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
         //marco_new_01    
458          if(!single_connection){          if(!single_connection){
459                  CloseDBConnection();                              CloseDBConnection();            
460          }          }
# Line 444  void PacketUser::FinishLastGroup() Line 468  void PacketUser::FinishLastGroup()
468          setReal_Time();          setReal_Time();
469          FinishGroup(rootfilename);          FinishGroup(rootfilename);
470          mainLogUtil->logAll("######################### Closed the last group ############################");          mainLogUtil->logAll("######################### Closed the last group ############################");
           
         //marco_new_31:metto qui se voglio avere come in origine apertura Connessione all'inizio e chiusura alla fine  
         //marco_new_01    
471          if(single_connection){          if(single_connection){
472                  CloseDBConnection();                              CloseDBConnection();            
473          }          }
# Line 474  void PacketUser::FinishGroup(char * file Line 495  void PacketUser::FinishGroup(char * file
495                  oss.str()="";                  oss.str()="";
496                                    
497                  //TODO: check when I don't want to log in DB informations                  //TODO: check when I don't want to log in DB informations
   
498                  // se non ho salvato nessun pacchetto                  // se non ho salvato nessun pacchetto
499                  if(numPKTSaved==0){                                      if(numPKTSaved==0){                    
500                          stringstream err;                          stringstream err;
# Line 496  void PacketUser::FinishGroup(char * file Line 516  void PacketUser::FinishGroup(char * file
516  //                      return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???  //                      return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???
517                  }                  }
518    
                 //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
                 //marco_new_01    
519                  if(!single_connection){                  if(!single_connection){
520                          OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle                          OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
521                  }                  }
# Line 506  void PacketUser::FinishGroup(char * file Line 524  void PacketUser::FinishGroup(char * file
524                  if((!real_time_init)&&(!real_time_last))                  if((!real_time_init)&&(!real_time_last))
525                  {                  {
526                          //saved in another table for future study                          //saved in another table for future study
527                          if(saveROOT_DB(Table_ROOT_Bad, outDir,                          if(saveROOT_DB(Table_ROOT_Bad, outDir,// here outDir is good, no expand EMI
528                                                                  filename,                                                                  filename,
529                                                             pkt_number_init, pkt_number_last,                                                             pkt_number_init, pkt_number_last,
530                                                             obt_init, obt_last,                                                             obt_init, obt_last,
# Line 515  void PacketUser::FinishGroup(char * file Line 533  void PacketUser::FinishGroup(char * file
533                                                             boot_number,                                                             boot_number,
534                                                             timeOffset,                                                             timeOffset,
535                                                             bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,                                                             bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
536                                                             fni)==true)                                                             fni,time_is_estimated)==true)
537                                  {                                  {
538                                          oss.str()="";                                          oss.str()="";
539                                          oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;                                          oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;
# Line 529  void PacketUser::FinishGroup(char * file Line 547  void PacketUser::FinishGroup(char * file
547                                          msg = oss.str();                                          msg = oss.str();
548                                          mainLogUtil->logError(msg);                                                      mainLogUtil->logError(msg);            
549                                  }                                                        }                      
550                    
                         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
                                 //marco_new_01    
551                          if(!single_connection){                          if(!single_connection){
552                                  CloseDBConnection();                                              CloseDBConnection();            
553                          }                          }
# Line 540  void PacketUser::FinishGroup(char * file Line 556  void PacketUser::FinishGroup(char * file
556                  }                  }
557                                                                                    
558                  //saves info in DB in table Table_ROOT_Good                  //saves info in DB in table Table_ROOT_Good
559                  if(saveROOT_DB(Table_ROOT_Good, outDir,                  if(boot_number==0)
560                      boot_number=boot_number_prevvalue;
561    
562                    if(saveROOT_DB(Table_ROOT_Good, outDir, // here outDir is good, no expand EMI
563                                          filename,                                          filename,
564                                     pkt_number_init, pkt_number_last,                                     pkt_number_init, pkt_number_last,
565                                     obt_init, obt_last,                                     obt_init, obt_last,
# Line 549  void PacketUser::FinishGroup(char * file Line 568  void PacketUser::FinishGroup(char * file
568                                     boot_number,                                     boot_number,
569                                     timeOffset,                                     timeOffset,
570                                     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,                                     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
571                                     fni)==true)                                     fni,time_is_estimated)==true)
572                  {                                                                {                                              
573                          oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good<< " id= "<<my_id;                          oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good<< " id= "<<my_id;
574                          msg = oss.str();                          msg = oss.str();
575                          mainLogUtil->logInfo(msg);                                mainLogUtil->logInfo(msg);      
576                                                    
                         //TODO : work on this ....                        
577                          if(tryMerge){                                                    if(tryMerge){                          
578                              merge_ROOTfiles();                              merge_ROOTfiles();
579                          }                          }
# Line 567  void PacketUser::FinishGroup(char * file Line 585  void PacketUser::FinishGroup(char * file
585                          mainLogUtil->logError(msg);                              mainLogUtil->logError(msg);    
586                  }                                                }                              
587    
                 //marco_new_31:metto qui se voglio apriree chiudere Connessione solo quando serve  
                         //marco_new_01    
588                  if(!single_connection){                  if(!single_connection){
589                          CloseDBConnection();                                      CloseDBConnection();            
590                  }                  }
# Line 576  void PacketUser::FinishGroup(char * file Line 592  void PacketUser::FinishGroup(char * file
592          }//pRun          }//pRun
593  }  }
594    
   
595  //save in Table_ROOT_Good or in Table_ROOT_Bad  //save in Table_ROOT_Good or in Table_ROOT_Bad
596  bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,  bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,
597                  unsigned long int pkt_number_in, unsigned long int pkt_number_fin,                  unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
# Line 586  bool PacketUser::saveROOT_DB(char* table Line 601  bool PacketUser::saveROOT_DB(char* table
601                  unsigned long int mboot_num,                  unsigned long int mboot_num,
602                  unsigned long int time_offset,                    unsigned long int time_offset,  
603                  int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,                            int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,          
604                  char* nome_input){                                        char* nome_input, bool _time_is_estimated){                    
605          //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?        
606          stringstream oss;          stringstream oss;
607          oss.str("");              oss.str("");    
608          oss << "INSERT INTO "<< table_name <<" (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)"          oss << "INSERT INTO "<< table_name <<" (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)"
609      << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"      << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
610      << oT_sync << "','" << lT_sync_info << "','"      << oT_sync << "','" << lT_sync_info << "','"
611      << mtime_init << "','" << mtime_last << "','"      << mtime_init << "','" << mtime_last << "','"
612          << mboot_num << "','"          << mboot_num << "','"
613      << time_offset << "','"      << time_offset << "','"
614      << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< numPKTSaved<<"','"      << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< num_PKT_Saved<<"','"
615      << nome_input <<"',"      << nome_input <<"',"
616      <<"NULL"<<            <<"NULL,"<<_time_is_estimated<<  
617      ")";                  ");";          
618          string msg = oss.str();          string msg = oss.str();
619          mainLogUtil->logAll(msg);                mainLogUtil->logAll(msg);      
620                    
621          stringstream oss1;          stringstream oss1;
622          oss1.str()="";          oss1.str()="";
623          string msg1;          string msg1;
624            string query;
625        
626     TSQLResult* res=NULL;             TSQLResult* res=NULL;
627     res= sqlServer->Query(oss.str().c_str());  
628       query=oss.str();
629       msg1="SaveROOT_DB query: ";
630       msg1 += query;      
631       mainLogUtil->logInfo(msg1);  
632              
633       res= sqlServer->Query(query.c_str());
634     if(!res)     if(!res)
635     {         {    
636             oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;               oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;  
# Line 617  bool PacketUser::saveROOT_DB(char* table Line 639  bool PacketUser::saveROOT_DB(char* table
639             return false;             return false;
640     }     }
641    
    //marco_new: si pu� ottimizzare ...  
642     //the ID of the current ROOT file in table Table_ROOT_Good     //the ID of the current ROOT file in table Table_ROOT_Good
643           my_id = select_maxIDN_DB(table_name);           my_id = select_maxIDN_DB(table_name);
644             if(!boot_number){
645               if(id_to_recover_index<1000)
646                 id_to_recover[id_to_recover_index++]=my_id;
647             }else if (boot_number && id_to_recover_index && !is_new_route){
648               recover_boot_number();
649             }
650  //      cout<<"DBG: my_id = "<<my_id<<endl;  //      cout<<"DBG: my_id = "<<my_id<<endl;
651     if(res){delete res; res = NULL;}     if(res){delete res; res = NULL;}
652     return true;     return true;
653  }  }
654    
655    
656    void PacketUser::recover_boot_number(){
657      string msg1;
658      stringstream oss1;
659      stringstream oss;
660      oss.str("");  
661      string query="";
662      TSQLResult* res=NULL;
663      if(!boot_number || !id_to_recover_index)
664        return;
665      
666      for (int i = 0; i<id_to_recover_index;i++){
667        oss.str("");
668        oss << "UPDATE "<<  Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i] << ";";  
669       query = oss.str();
670       res= sqlServer->Query(query.c_str());
671       if(!res)
672       {    
673               oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;  
674               msg1=oss1.str();
675               mainLogUtil->logError(msg1);  
676               return ;
677       }
678      }
679    
680      for (int i = 0; i<id_to_recover_index;i++){
681        oss.str("");
682        oss << "UPDATE " <<  Table_ROOT_Merging << " SET BOOT_NUMBER=" <<  boot_number << " WHERE ROOT_ID_N=" << id_to_recover[i] << ";";  
683        query=oss.str();
684        res= sqlServer->Query(query.c_str());
685        if(!res)
686          {
687               oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;  
688               msg1=oss1.str();
689               mainLogUtil->logError(msg1);  
690               return ;
691       }
692      }
693    
694      id_to_recover_index=0;
695    }
696    
697    
698    
699  /**********************************************************************************************/  /**********************************************************************************************/
700  /*###########################################################################################################*/  /*###########################################################################################################*/
701  /**********************************         MERGING          *************************************************/  /**********************************         MERGING          *************************************************/
702  /*###########################################################################################################*/  /*###########################################################################################################*/
703  //      Merge ROOT files: found other ROOT files in the same temporal ranges  //      Merge ROT files: find other ROOT files in the same temporal ranges
704  //    it finds ROOT files that covers particular temporal range of interest and save this info in DB  //    it finds ROOT files that covers particular temporal range of interest and save this info in DB
705  /**********************************************************************************************/  /**********************************************************************************************/
706  //try to merge current ROOT file with files in DB  //try to merge current ROOT file with files in DB
707  bool PacketUser::merge_ROOTfiles()  bool PacketUser::merge_ROOTfiles()
708  {  {
709          mainLogUtil->logInfo("Trying merging ROOT files");     stringstream log;
710       log.str("");
711       string slog;
712    
713    //marco_new: controlla    mainLogUtil->logInfo("Trying merging ROOT files");
714    double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;    double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;
715                                    
716    TSQLResult* res=NULL;      TSQLResult* res=NULL;  
717    TSQLRow* row =NULL;    TSQLRow* row =NULL;
718    //numero di ROOT files trovati sul DB che hanno relaz di tipo dato
719      unsigned int num_rows=0;
720    
721            //Record su DB:
722            unsigned int idN = 0;      
723            unsigned int root_id_db=0;
724            unsigned long int pkt_num_in_db=0;
725            unsigned long int pkt_num_fin_db=0;
726            unsigned long int pkt_obt_in_db=0;
727            unsigned long int pkt_obt_fin_db=0;
728            unsigned long int time_in_db=0;
729            unsigned long int time_fin_db=0;
730            unsigned int boot_num_db=0;
731            double perc=0;                  
732    
733    
734    //marco_new: non considero mai caso di continuit�(mio finale=DB iniz o viceversa), non mi interessa    //marco_new: non considero mai caso di continuita'(mio finale=DB iniz o viceversa), non mi interessa
735    //marco_new: after e before contengono anche smaller ma con un bordo comune    //marco_new: after e before contengono anche smaller ma con un bordo comune
736    for(int relaz=AFTER; relaz<=BIGGER; relaz++)    for(int relaz=AFTER; relaz<=BIGGER; relaz++)
737    {                {    
         //marco_new: controlla tutto  
738          //marco_new: esci se sono arrivato a zero          //marco_new: esci se sono arrivato a zero
739          if(real_time_last==real_time_init){              if(real_time_last==real_time_init)
740            {
741                    log.str("");
742                    log<<"Current File Lenght=0; real_time_last=real_time_init= "<<real_time_last;
743                    slog=log.str();
744                    mainLogUtil->logAll(slog);
745                  mainLogUtil->logInfo("Finish merging ROOT files");                                mainLogUtil->logInfo("Finish merging ROOT files");              
746                  if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}
747                  if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}
748                  return true;                                              return true;                            
749          }          }
750    
751          res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);          num_rows=0;
752            res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);
753                    
         unsigned int num_rows=0;  
754           if (!res)           if (!res)
755           {           {
756                  mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");                  mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");
# Line 666  bool PacketUser::merge_ROOTfiles() Line 758  bool PacketUser::merge_ROOTfiles()
758           }           }
759    
760           num_rows=res->GetRowCount();           num_rows=res->GetRowCount();
761  //       cout<<"MMMMM trovati num_rows "<< num_rows<< " per relaz= "<<relaz;           log.str("");
762             log<<"Found "<<num_rows<<" ROOT file in DB with relation of kind = "<<relaz;
763             slog=log.str();
764             mainLogUtil->logAll(slog);
765    
766           if(num_rows>0)           if(num_rows>0)
767           {                                 {
768                   for(unsigned int i=0; i<num_rows; i++)                  for(unsigned int i=0; i<num_rows; i++)
769                   {                   {
770                           row=res->Next();                           row=res->Next();
771                           if (!row)                           if (!row)
772                           {                           {
773                                   mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");                                  mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");
774                                  if(res){delete res; res = NULL;}                                  if(res){delete res; res = NULL;}
775                                  return false;                                  return false;
776                           }                           }
777                                                    
778                            //Leggi Record dal DB:
779                          //TODO ricavare tutte le info che servono                          idN=atoll(row->GetField(0));    
780                          unsigned int idN = 0;                                root_id_db=atoll(row->GetField(1));
781                          idN=atoi(row->GetField(0));                          pkt_num_in_db=atoll(row->GetField(2));
782                                            pkt_num_fin_db=atoll(row->GetField(3));
783                          double perc =0;                                          pkt_obt_in_db=atoll(row->GetField(4));
784                          //marco_new:                          pkt_obt_fin_db=atoll(row->GetField(5));
785                            time_in_db=atoll(row->GetField(6));
786                            time_fin_db=atoll(row->GetField(7));
787                            boot_num_db=atoll(row->GetField(8));            
788                          perc=atof(row->GetField(9));                          perc=atof(row->GetField(9));
789                          //unsigned long int M_pkt_number_in, M_pkt_number_fin, M_obt_in, M_obt_fin, M_mtime_init, M_mtime_last;                          
790                          if(perc>percentage){                          log.str("");
791                            log<<"*** ROOT file found in DB has idN= "<<idN<<"  root_id_db= "<<root_id_db;
792                            log<<" pkt_num_in_db= "<<pkt_num_in_db<<"  pkt_num_fin_db= "<<pkt_num_fin_db<<"  pkt_obt_in_db= "<<pkt_obt_in_db<<"  pkt_obt_fin_db= "<<pkt_obt_fin_db;
793                            log<<"  time_in_db= "<<time_in_db<<"  time_fin_db= "<<time_fin_db<<" boot_num_db= "<<boot_num_db<<"  perc= "<<perc<<"  ***";                    
794                            log<<"\n*** Current ROOT file has pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
795                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
796                            slog=log.str();
797                            mainLogUtil->logAll(slog);
798                            if(perc>percentage)
799                            {
800                                  mainLogUtil->logInfo("Current ROOT file is better than the one in DB");                                                  mainLogUtil->logInfo("Current ROOT file is better than the one in DB");                
801                                  //modifica DB                                  //modifica DB
802                                  updateMergeROOT_DB(Table_ROOT_Merging,                                  updateMergeROOT_DB(Table_ROOT_Merging,
803                                          atoi(row->GetField(1)), atoi(row->GetField(2)),                                                                          root_id_db,
804                                          atoi(row->GetField(3)), atoi(row->GetField(4)),                                                                          pkt_num_in_db, pkt_num_fin_db,
805                                          atoi(row->GetField(5)), atoi(row->GetField(6)),                                                                          pkt_obt_in_db, pkt_obt_fin_db,
806                                          atoi(row->GetField(7)), atoi(row->GetField(8)),                                                                          time_in_db, time_fin_db,
807                                          perc,                                                                          boot_num_db,
808                                          idN,(type_Rel_ROOT)relaz);                                                                          perc,
809                                                                            idN,(type_Rel_ROOT)relaz);
810                          }                          }
811                          else{                          else
812                                  mainLogUtil->logInfo("Found ROOT files in DB better than current ROOT file");                                    {
813                                    mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file");        
814                                  if(relaz == AFTER){                                  if(relaz == AFTER)
815                                    {
816                                          mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");                                          mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
817                                          pkt_number_init= atoi(row->GetField(3));                                          pkt_number_init =  (pkt_num_fin_db+1);
818                                          obt_init= atoi(row->GetField(5));                                          obt_init = pkt_obt_fin_db;
819                                          real_time_init= atoi(row->GetField(7));                                                                          real_time_init = time_fin_db;  
820                                            log.str("");
821                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
822                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
823                                            slog=log.str();
824                                            mainLogUtil->logAll(slog);
825                                  }                                  }
826                                  else if(relaz == BEFORE){                                  else if(relaz == BEFORE)
827                                          mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");                                            {
828                                          pkt_number_last= atoi(row->GetField(2));                                          mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");                                  
829                                          obt_last= atoi(row->GetField(4));                                          pkt_number_last = (pkt_num_in_db-1);
830                                          real_time_last= atoi(row->GetField(6));                                          obt_last = pkt_obt_in_db;
831                                            real_time_last = time_in_db;
832    
833                                            log.str("");
834                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
835                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
836                                            slog=log.str();
837                                            mainLogUtil->logAll(slog);
838                                  }                                  }
839                                  else if(relaz == SMALLER)                                  else if(relaz == SMALLER)
840                                  {                                  {
841                                          //non devo proprio salvarlo ed esco ...                                                                  //non devo proprio salvarlo ed esco ...                        
842                                          mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");                                          mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
843                                          mainLogUtil->logInfo("Finish merging ROOT files");                                          mainLogUtil->logInfo("\nFinish merging ROOT files");
844                                          if(res){delete res; res = NULL;}                                          if(res){delete res; res = NULL;}
845                                          if(row){delete row; row = NULL;}                                          if(row){delete row; row = NULL;}                        
846                                          return true;                                          return true;
847                                  }                                  }
848                                  else if(relaz == BIGGER)//spezzettamento                                  else if(relaz == BIGGER)//spezzettamento
849                                  {                                        {      
850                                          mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");                                                                                                mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");                                                      
851                                          saveMergeROOT_DB(Table_ROOT_Merging, my_id,                                          saveMergeROOT_DB(Table_ROOT_Merging, my_id,
852                                                  pkt_number_init, atoi(row->GetField(2)),                                                  pkt_number_init, (pkt_num_in_db-1),
853                                                  obt_init, atoi(row->GetField(4)),                                                  obt_init, pkt_obt_in_db,
854                                                  real_time_init, atoi(row->GetField(6)),                                                  real_time_init, time_in_db,
855                                                  boot_number,                                                  boot_number,
856                                                  percentage);                                                  percentage);
857                                                                                            
858                                          mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");                                          mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");
859                                          pkt_number_init= atoi(row->GetField(3));                                          pkt_number_init = (pkt_num_fin_db+1);
860                                          obt_init= atoi(row->GetField(5));                                          obt_init= pkt_obt_fin_db;
861                                          real_time_init= atoi(row->GetField(7));                                          real_time_init= time_fin_db;
862                                  }                                          
863                          }                                          log.str("");
864                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
865                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
866                                            slog=log.str();
867                                            mainLogUtil->logAll(slog);
868                                    }//if relaz                    
869                            }//if perc
870                  }//for num_rows                      }//for num_rows    
871            }           }//if num_rows
         else{  
                 //marco_new:  
                 mainLogUtil->logInfo("Found 0 ROOT files in DB that can be merged with current ROOT file");              
   
         }  
872    
         //marco_new:???  
873          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
874          if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}        
875  }//for          
876    }//for relaz
877    
878          mainLogUtil->logInfo("Saving Current ROOT file");                mainLogUtil->logInfo("Saving Current ROOT file");      
879          saveMergeROOT_DB(Table_ROOT_Merging, my_id,          saveMergeROOT_DB(Table_ROOT_Merging, my_id,
# Line 761  bool PacketUser::merge_ROOTfiles() Line 883  bool PacketUser::merge_ROOTfiles()
883                           boot_number,                           boot_number,
884                           percentage);                           percentage);
885    
   
886          mainLogUtil->logInfo("Finish merging ROOT files");                mainLogUtil->logInfo("Finish merging ROOT files");      
887    return true;    return true;
888    
889  }  }
890    
891  //unsigned int root_id,  percentage non le devo modificare  //unsigned int root_id,  percentage non le devo modificare
# Line 775  bool PacketUser::updateMergeROOT_DB(char Line 897  bool PacketUser::updateMergeROOT_DB(char
897                          unsigned long int mboot_num,                          unsigned long int mboot_num,
898                          double bad_perc,                          double bad_perc,
899                          unsigned int ID_record, type_Rel_ROOT type_rel){                          unsigned int ID_record, type_Rel_ROOT type_rel){
         stringstream oss;  
         oss.str("");      
900                    
901                                    stringstream oss;
902                                    oss.str("");    
903    
904          if(type_rel == AFTER){          if(type_rel == AFTER){
905                  mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");                  mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
906                  //marco_new:tolti tutti gli apici che qui non devono essere                  //marco_new:tolti tutti gli apici che qui non devono essere
907                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
908                  << " SET PKT_NUMBER_FINAL ="<< pkt_number_init                        << " SET PKT_NUMBER_FINAL ="<< (pkt_number_init - 1)        
909                          <<", PKT_OBT_FINAL ="<< obt_init                          <<", PKT_OBT_FINAL ="<< obt_init
910                      <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";                      <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";
911    
912          }          }
913          else if(type_rel == BEFORE){                      else if(type_rel == BEFORE){            
914                          mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");                          mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");        
915                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
916                  << " SET PKT_NUMBER_INIT ="<< pkt_number_last                        << " SET PKT_NUMBER_INIT ="<< (pkt_number_last+1)  
917                          <<", PKT_OBT_INIT ="<< obt_last                          <<", PKT_OBT_INIT ="<< obt_last
918                      <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";                      <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";
919          }          }
# Line 798  bool PacketUser::updateMergeROOT_DB(char Line 922  bool PacketUser::updateMergeROOT_DB(char
922                  mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");                                                                      mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");                                                    
923                    
924                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
925                          << " SET PKT_NUMBER_FINAL="<< pkt_number_init                        << " SET PKT_NUMBER_FINAL="<< (pkt_number_init-1)  
926                          <<", PKT_OBT_FINAL ="<< obt_init                          <<", PKT_OBT_FINAL ="<< obt_init
927                              <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record<<";";                              <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record<<";";
                   
                         mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");                                            
928    
929                          saveMergeROOT_DB(table_name, root_id,                  mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");                                          
930                           pkt_number_last, pkt_number_fin,  
931                           obt_last, obt_fin,                  saveMergeROOT_DB(table_name, root_id,
932                           real_time_last, mtime_last,                   (pkt_number_last+1), pkt_number_fin,
933                           mboot_num,                   obt_last, obt_fin,
934                           bad_perc);                   real_time_last, mtime_last,
935                     mboot_num,
936                     bad_perc);
937          }          }
938          else if(type_rel == BIGGER){          else if(type_rel == BIGGER){
939          //marco_new: ok anche se coincidenti etc          //marco_new: ok anche se coincidenti etc
940            oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";";            //      oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO
941            mainLogUtil->logInfo("Rimosso record in Tabella merging");                oss << "UPDATE "<< table_name <<" SET GOOD=0 WHERE ID_N ="<< ID_record<<";"; // EMILIANO DO NOT DELETE FROM ROOT_TABLE_MERGING JUST SET GOOD FLAG TO ZERO
942              mainLogUtil->logInfo("Record deleted from merging table");    
943          }          }
944          else          else
945                  return false;                  return false;
946    
   
   
947          string msg = oss.str();          string msg = oss.str();
948          mainLogUtil->logInfo(msg);                mainLogUtil->logInfo(msg);      
949                    
# Line 841  bool PacketUser::updateMergeROOT_DB(char Line 964  bool PacketUser::updateMergeROOT_DB(char
964     return true;     return true;
965  }  }
966    
967  //marco_new: check here  //cerca eventuali ROOT files in DB che possono venire mergiati
968    //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
969    //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
970    //marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx
971  TSQLResult* PacketUser::Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel)  TSQLResult* PacketUser::Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel)
972  {  {
   
 //      unsigned  int idN=0;  
973          TSQLResult* res=NULL;          TSQLResult* res=NULL;
974          stringstream query;          stringstream query;
975          query.str("");          query.str("");
976    
         //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune  
         //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente  
         //marco_new bordi: smaller resta solo caso in cui DB � piu largo sia a dx che a sx  
977          if(type_rel == AFTER)          if(type_rel == AFTER)
978            query<<"select * from "<<table_name<<" where REAL_TIME_LAST > "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last <<" and REAL_TIME_INIT < "<< mtime_init;            query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_LAST > "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last <<" and REAL_TIME_INIT < "<< mtime_init; // EMI
979          else if(type_rel == BEFORE)          else if(type_rel == BEFORE)
980            query<<"select * from "<<table_name<<" where REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_INIT < "<< mtime_last <<" and REAL_TIME_LAST > "<< mtime_last;            query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_INIT < "<< mtime_last <<" and REAL_TIME_LAST > "<< mtime_last; // EMI
981          else if(type_rel == SMALLER)          else if(type_rel == SMALLER)
982            query<<"select * from "<<table_name<<" where REAL_TIME_INIT < "<< mtime_init <<" and REAL_TIME_LAST > "<< mtime_last;            query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT < "<< mtime_init <<" and REAL_TIME_LAST > "<< mtime_last; // EMI
983          else if(type_rel == BIGGER)          else if(type_rel == BIGGER)
984                  query<<"select * from "<<table_name<<" where REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last;            query<<"select * from "<<table_name<<" where GOOD=1 and REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last; // EMI
985          else          else
986                  return 0;                  return 0;
987                    
988          query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile          query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile
989            //se volessi mettere un filtro sulla qualita'
990          //      query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;          //      query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
           
     
991          string msg = query.str();          string msg = query.str();
992          mainLogUtil->logAll(msg);          mainLogUtil->logAll(msg);
993            res= sqlServer->Query(query.str().c_str());      
         res= sqlServer->Query(query.str().c_str());  
           
994          stringstream oss1;          stringstream oss1;
995           oss1.str()="";          oss1.str()="";
996           string msg1;          string msg1;
997        
998           if (!res)           if (!res)
999           {           {
# Line 897  bool PacketUser::saveMergeROOT_DB(char* Line 1015  bool PacketUser::saveMergeROOT_DB(char*
1015    
1016          stringstream oss;          stringstream oss;
1017          oss.str("");              oss.str("");    
1018          oss << "INSERT INTO "<< table_name <<" (ID_N, ROOT_ID_N, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, REAL_TIME_INIT, REAL_TIME_LAST, BOOT_NUMBER, BAD_PKT_PERCENTAGE, INSERT_TIME)"          oss << "INSERT INTO "<< table_name <<" (ID_N, ROOT_ID_N, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, REAL_TIME_INIT, REAL_TIME_LAST, BOOT_NUMBER, BAD_PKT_PERCENTAGE, INSERT_TIME, INSERTED_BY)"
1019      << " VALUES ('"<<0<< "','" <<root_id<<"','"<< pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"      << " VALUES ('"<<0<< "','" <<root_id<<"','"<< pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
1020      << mtime_init << "','" << mtime_last << "','"      << mtime_init << "','" << mtime_last << "','"
1021          << mboot_num <<"','"          << mboot_num <<"','"
1022          << percentage <<"',"                << percentage <<"',"      
1023      <<"NULL"<<                    <<"NULL"<<",'"
1024      ")";              << tag_value<<"'"
1025                << ")";
1026    
1027          string msg = oss.str();          string msg = oss.str();
1028          mainLogUtil->logAll(msg);                mainLogUtil->logAll(msg);      
1029            
1030          //marco_new: controlla          //marco_new: controlla
1031          //marco_new: esci se sono arrivato a zero          //marco_new: esci se sono arrivato a zero
1032  //      if(real_time_last==real_time_init){      //      if(real_time_last==real_time_init){    
# Line 919  bool PacketUser::saveMergeROOT_DB(char* Line 1038  bool PacketUser::saveMergeROOT_DB(char*
1038                  return true;                                              return true;                            
1039          }          }
1040    
   
1041          stringstream oss1;          stringstream oss1;
1042          oss1.str()="";          oss1.str()="";
1043          string msg1;          string msg1;
# Line 950  if (!strcmp(table_name,Table_ROOT_Bad)) Line 1068  if (!strcmp(table_name,Table_ROOT_Bad))
1068          stringstream oss;          stringstream oss;
1069      oss.str("");      oss.str("");
1070      oss<<"select max(ID_N) from "<<table_name;      oss<<"select max(ID_N) from "<<table_name;
1071          string msg = oss.str();  //      string msg = oss.str();
1072          mainLogUtil->logAll(msg);        //      mainLogUtil->logAll(msg);      
1073      res= sqlServer->Query(oss.str().c_str());      res= sqlServer->Query(oss.str().c_str());
1074                                    
1075          stringstream oss1;          stringstream oss1;
# Line 979  if (!strcmp(table_name,Table_ROOT_Bad)) Line 1097  if (!strcmp(table_name,Table_ROOT_Bad))
1097          idN=atoi(row->GetField(0));          idN=atoi(row->GetField(0));
1098          if(row){delete row; row = NULL;}          if(row){delete row; row = NULL;}
1099          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
1100    
1101        oss<<". idN = "<<idN;
1102            string msg = oss.str();
1103            mainLogUtil->logAll(msg);      
1104    
1105          return idN;          return idN;
1106  }  }
1107    
# Line 1016  int PacketUser::LockTables(char* tableTo Line 1139  int PacketUser::LockTables(char* tableTo
1139          }          }
1140                        
1141          if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}        
1142          mainLogUtil->logAll("######################### TABLES LOCKED ############################");                              mainLogUtil->logAll("TABLES LOCKED");                  
1143          return 0;                return 0;      
1144  }  }
1145    
# Line 1050  int PacketUser::UnLockTables(){   Line 1173  int PacketUser::UnLockTables(){  
1173          }          }
1174                                    
1175          if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}        
1176          mainLogUtil->logAll("######################### TABLES UNLOCKED ############################");            mainLogUtil->logAll("TABLES UNLOCKED");
1177    
1178          return 0;                return 0;      
1179  }  }
# Line 1058  int PacketUser::UnLockTables(){   Line 1181  int PacketUser::UnLockTables(){  
1181  /*********************************************************/  /*********************************************************/
1182  //marco_new_31  //marco_new_31
1183  void PacketUser::OpenDBConnection(char* tableTobeLocked){  void PacketUser::OpenDBConnection(char* tableTobeLocked){
         mainLogUtil->logAll("######################### OpenDBConnection ############################");  
1184          if(do_cont_check){          if(do_cont_check){
1185                  //controllo che non sia gi� aperta                  //controllo che non sia gia' aperta
1186                  if(!sqlServer){                  if(!sqlServer){
1187                          sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);                          sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
1188                          if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}                                if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}      
1189                            mainLogUtil->logAll("Connected to DB");
1190                          //marco_new: blocca tutte le tabelle                          //blocca le tabelle
1191                          LockTables(tableTobeLocked);                          LockTables(tableTobeLocked);            
                         mainLogUtil->logAll("######################### OpenDBConnection OK ############################");        
1192                  }                  }
1193          }          }
1194    
# Line 1075  void PacketUser::OpenDBConnection(char* Line 1196  void PacketUser::OpenDBConnection(char*
1196    
1197  //marco_new_31  //marco_new_31
1198  void PacketUser::CloseDBConnection(){  void PacketUser::CloseDBConnection(){
         mainLogUtil->logAll("######################### CloseDBConnection ############################");          
1199          if(do_cont_check){                                                if(do_cont_check){                                      
1200                  //controllo che ce ne sia una aperta                  //controllo che ce ne sia una aperta
1201                  if(sqlServer && sqlServer->IsConnected()){                  if(sqlServer && sqlServer->IsConnected()){
1202                          //marco_new: sblocca tutte le tabelle                          //marco_new: sblocca tutte le tabelle
1203                          UnLockTables();                                  UnLockTables();        
1204                          sqlServer->Close();                          sqlServer->Close();
1205                            delete sqlServer;                      
                         //marco_new_01: ??  
                         delete sqlServer;  
                           
1206                          sqlServer=NULL;                          sqlServer=NULL;
1207                          mainLogUtil->logAll("######################### CloseDBConnection OK ############################");                              mainLogUtil->logAll("Closed Connection to DB");
1208                  }                  }
1209          }          }
1210  }  }
# Line 1118  void PacketUser::savePKT_file(char* head Line 1235  void PacketUser::savePKT_file(char* head
1235  {  {
1236          char fno[80]="";          char fno[80]="";
1237          ofstream fout;          ofstream fout;
1238          sprintf(fno,"%s/pkt%d_of_%s.pkt", outDir, numPKTSaved, nomefile);          sprintf(fno,"%s/pkt%d_of_%s.pkt", gSystem->ExpandPathName(outDir), numPKTSaved, nomefile); // EMI
1239    
1240          if(append==true)          if(append==true)
1241                  fout.open(fno, ios::binary | ios::app);                  fout.open(fno, ios::binary | ios::app);
# Line 1138  void PacketUser::saveALL_PKT(char* heade Line 1255  void PacketUser::saveALL_PKT(char* heade
1255          ofstream fout;          ofstream fout;
1256          if(append==true)          if(append==true)
1257          {                        {              
1258                  sprintf(fno,"%s/packets.pkt", outDir);                  sprintf(fno,"%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI
1259                  fout.open(fno, ios::binary | ios::app);                  fout.open(fno, ios::binary | ios::app);
1260          }          }
1261          else{          else{
1262                  sprintf(fno,"%s/packet%d.pkt", outDir, numPKT );                  sprintf(fno,"%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT ); // EMI
1263                  fout.open(fno, ios::binary);                                      fout.open(fno, ios::binary);                    
1264          }          }
1265                                    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.5

  ViewVC Help
Powered by ViewVC 1.1.23