/[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.7 by mocchiut, Fri Jul 24 13:53:38 2009 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.6 2008/12/23 20:43:08 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]; // Emiliano: che si vuole fare con questa riga? cosi` non fa nulla...
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 318  void PacketUser::setTimeSync(char* packe Line 325  void PacketUser::setTimeSync(char* packe
325    
326          stringstream oss;          stringstream oss;
327          oss.str()="";          oss.str()="";
328          oss<<"In download: "<<download<<" timeOffset: "<<timeOffset<<" obt_time_sync: "<<obt_time_sync<<" last_time_sync_info: "<<last_time_sync_info<<" using Packet Type: "<<type->GetName().c_str();          oss<<"In download: "<<download<<" timeOffset: "<<timeOffset<<" obt_time_sync: "<<obt_time_sync<<" last_time_sync_info: "<<last_time_sync_info<<" using Packet Type: "<<type->GetName();
329            //      oss<<"In download: "<<download<<" timeOffset: "<<timeOffset<<" obt_time_sync: "<<obt_time_sync<<" last_time_sync_info: "<<last_time_sync_info<<" using Packet Type: "<<type->GetName().c_str();
330          string msg = oss.str();          string msg = oss.str();
331          mainLogUtil->logAll(msg);          mainLogUtil->logAll(msg);
332  }  }
# Line 326  void PacketUser::setTimeSync(char* packe Line 334  void PacketUser::setTimeSync(char* packe
334  //Boot Number  //Boot Number
335  void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)  void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)
336  {  {
337          //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'    
338          if(boot_number) return;          if(boot_number) return;
339  //todo: controlla se algo giusto: preso da VarDumpReader.cpp          
 //mmm e' sbagliatissimo cambiare !!!      
340          if(type==PacketType::VarDump)          if(type==PacketType::VarDump)
341          {                                {      
342                    int b_offset = 34;//4+5*6
343                    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);
344    
345    /*
346                  long int  dataLength = pktLength - 2; //the block of data                  long int  dataLength = pktLength - 2; //the block of data
347                  int b_offset = 4;                  int b_offset = 4;              
348                  while (b_offset < dataLength){                  while (b_offset < dataLength){
349                   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);
350                   b_offset = b_offset + 5;                   b_offset = b_offset + 5;
351                  }                  }
352    */              
353                  stringstream oss;                  stringstream oss;
354                  oss.str()="";                  oss.str()="";
355                  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();
356                    oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName();
357                  string msg = oss.str();                  string msg = oss.str();
358                  mainLogUtil->logAll(msg);                  mainLogUtil->logAll(msg);
359                  return;                  return;
# Line 354  void PacketUser::setBootNumber(char* pac Line 367  void PacketUser::setBootNumber(char* pac
367  //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
368  void PacketUser::setReal_Time()  void PacketUser::setReal_Time()
369  {  {
370          if((obt_time_sync)||(last_time_sync_info))  
371          {                        if(!obt_time_sync  && !last_time_sync_info && numPKTSaved > 1000 )
372                  real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;          {
373                  real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;                  time_is_estimated=true;
374              obt_time_sync=obt_time_sync_prevvalue;
375              last_time_sync_info=last_time_sync_info_prevvalue;
376            }  
377            if(obt_time_sync  || last_time_sync_info)
378              {                                                                                
379                real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
380                    //se obt si e' resettato
381                    if(obt_last<obt_init)
382                    {
383                            real_time_last=(max_pkt_obt/1000+ obt_last/1000-obt_time_sync)+last_time_sync_info;    
384                            stringstream oss;
385                            oss.str()="";
386                            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";
387                            string msg = oss.str();
388                            mainLogUtil->logWarning(msg);
389                    }
390                    else
391                            real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
392                  real_time_init+=timeOffset;                  real_time_init+=timeOffset;
393                  real_time_last+=timeOffset;                              real_time_last+=timeOffset;    
394          }                }
395  }  }
396    
397  //timeOffset  //timeOffset
# Line 371  unsigned long int PacketUser::retrieveTi Line 402  unsigned long int PacketUser::retrieveTi
402                    
403          //if(!table){table=Table_GL_RESURS_OFFSET}          //if(!table){table=Table_GL_RESURS_OFFSET}
404    
         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
         //marco_new_01    
405          if(!single_connection){          if(!single_connection){
406                  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
407          }          }
408    
409          UInt_t t0 = 0;  //toffset                        UInt_t t0 = 0;//toffset                                
                           
410          stringstream oss;          stringstream oss;
411          oss.str("");          oss.str("");
412          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 403  unsigned long int PacketUser::retrieveTi Line 431  unsigned long int PacketUser::retrieveTi
431    
432          oss.str("");          oss.str("");
433          oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<<      table <<" WHERE FROM_ORBIT < "          oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "<<      table <<" WHERE FROM_ORBIT < "
434          << orbit_number << " order by FROM_ORBIT desc limit 1;";          << orbit_number << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
435                    
436                  string msg2 = oss.str();                  string msg2 = oss.str();
437                  mainLogUtil->logAll(msg2);                  mainLogUtil->logAll(msg2);
# Line 429  unsigned long int PacketUser::retrieveTi Line 457  unsigned long int PacketUser::retrieveTi
457          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
458          if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}        
459    
         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
         //marco_new_01    
460          if(!single_connection){          if(!single_connection){
461                  CloseDBConnection();                              CloseDBConnection();            
462          }          }
# Line 444  void PacketUser::FinishLastGroup() Line 470  void PacketUser::FinishLastGroup()
470          setReal_Time();          setReal_Time();
471          FinishGroup(rootfilename);          FinishGroup(rootfilename);
472          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    
473          if(single_connection){          if(single_connection){
474                  CloseDBConnection();                              CloseDBConnection();            
475          }          }
# Line 474  void PacketUser::FinishGroup(char * file Line 497  void PacketUser::FinishGroup(char * file
497                  oss.str()="";                  oss.str()="";
498                                    
499                  //TODO: check when I don't want to log in DB informations                  //TODO: check when I don't want to log in DB informations
   
500                  // se non ho salvato nessun pacchetto                  // se non ho salvato nessun pacchetto
501                  if(numPKTSaved==0){                                      if(numPKTSaved==0){                    
502                          stringstream err;                          stringstream err;
# Line 496  void PacketUser::FinishGroup(char * file Line 518  void PacketUser::FinishGroup(char * file
518  //                      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???
519                  }                  }
520    
                 //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
                 //marco_new_01    
521                  if(!single_connection){                  if(!single_connection){
522                          OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle                          OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
523                  }                  }
# Line 506  void PacketUser::FinishGroup(char * file Line 526  void PacketUser::FinishGroup(char * file
526                  if((!real_time_init)&&(!real_time_last))                  if((!real_time_init)&&(!real_time_last))
527                  {                  {
528                          //saved in another table for future study                          //saved in another table for future study
529                          if(saveROOT_DB(Table_ROOT_Bad, outDir,                          if(saveROOT_DB(Table_ROOT_Bad, outDir,// here outDir is good, no expand EMI
530                                                                  filename,                                                                  filename,
531                                                             pkt_number_init, pkt_number_last,                                                             pkt_number_init, pkt_number_last,
532                                                             obt_init, obt_last,                                                             obt_init, obt_last,
# Line 515  void PacketUser::FinishGroup(char * file Line 535  void PacketUser::FinishGroup(char * file
535                                                             boot_number,                                                             boot_number,
536                                                             timeOffset,                                                             timeOffset,
537                                                             bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,                                                             bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
538                                                             fni)==true)                                                             fni,time_is_estimated)==true)
539                                  {                                  {
540                                          oss.str()="";                                          oss.str()="";
541                                          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 549  void PacketUser::FinishGroup(char * file
549                                          msg = oss.str();                                          msg = oss.str();
550                                          mainLogUtil->logError(msg);                                                      mainLogUtil->logError(msg);            
551                                  }                                                        }                      
552                    
                         //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve  
                                 //marco_new_01    
553                          if(!single_connection){                          if(!single_connection){
554                                  CloseDBConnection();                                              CloseDBConnection();            
555                          }                          }
# Line 540  void PacketUser::FinishGroup(char * file Line 558  void PacketUser::FinishGroup(char * file
558                  }                  }
559                                                                                    
560                  //saves info in DB in table Table_ROOT_Good                  //saves info in DB in table Table_ROOT_Good
561                  if(saveROOT_DB(Table_ROOT_Good, outDir,                  if(boot_number==0)
562                      boot_number=boot_number_prevvalue;
563    
564                    if(saveROOT_DB(Table_ROOT_Good, outDir, // here outDir is good, no expand EMI
565                                          filename,                                          filename,
566                                     pkt_number_init, pkt_number_last,                                     pkt_number_init, pkt_number_last,
567                                     obt_init, obt_last,                                     obt_init, obt_last,
# Line 549  void PacketUser::FinishGroup(char * file Line 570  void PacketUser::FinishGroup(char * file
570                                     boot_number,                                     boot_number,
571                                     timeOffset,                                     timeOffset,
572                                     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,                                     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
573                                     fni)==true)                                     fni,time_is_estimated)==true)
574                  {                                                                {                                              
575                          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;
576                          msg = oss.str();                          msg = oss.str();
577                          mainLogUtil->logInfo(msg);                                mainLogUtil->logInfo(msg);      
578                                                    
                         //TODO : work on this ....                        
579                          if(tryMerge){                                                    if(tryMerge){                          
580                              merge_ROOTfiles();                              merge_ROOTfiles();
581                          }                          }
# Line 567  void PacketUser::FinishGroup(char * file Line 587  void PacketUser::FinishGroup(char * file
587                          mainLogUtil->logError(msg);                              mainLogUtil->logError(msg);    
588                  }                                                }                              
589    
                 //marco_new_31:metto qui se voglio apriree chiudere Connessione solo quando serve  
                         //marco_new_01    
590                  if(!single_connection){                  if(!single_connection){
591                          CloseDBConnection();                                      CloseDBConnection();            
592                  }                  }
# Line 576  void PacketUser::FinishGroup(char * file Line 594  void PacketUser::FinishGroup(char * file
594          }//pRun          }//pRun
595  }  }
596    
   
597  //save in Table_ROOT_Good or in Table_ROOT_Bad  //save in Table_ROOT_Good or in Table_ROOT_Bad
598  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,
599                  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 603  bool PacketUser::saveROOT_DB(char* table
603                  unsigned long int mboot_num,                  unsigned long int mboot_num,
604                  unsigned long int time_offset,                    unsigned long int time_offset,  
605                  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,          
606                  char* nome_input){                                        char* nome_input, bool _time_is_estimated){                    
607          //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?        
608          stringstream oss;          stringstream oss;
609          oss.str("");              oss.str("");    
610          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)"
611      << " 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<< "','"
612      << oT_sync << "','" << lT_sync_info << "','"      << oT_sync << "','" << lT_sync_info << "','"
613      << mtime_init << "','" << mtime_last << "','"      << mtime_init << "','" << mtime_last << "','"
614          << mboot_num << "','"          << mboot_num << "','"
615      << time_offset << "','"      << time_offset << "','"
616      << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< numPKTSaved<<"','"      << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< num_PKT_Saved<<"','"
617      << nome_input <<"',"      << nome_input <<"',"
618      <<"NULL"<<            <<"NULL,"<<_time_is_estimated<<  
619      ")";                  ");";          
620          string msg = oss.str();          string msg = oss.str();
621          mainLogUtil->logAll(msg);                mainLogUtil->logAll(msg);      
622                    
623          stringstream oss1;          stringstream oss1;
624          oss1.str()="";          oss1.str()="";
625          string msg1;          string msg1;
626            string query;
627        
628     TSQLResult* res=NULL;             TSQLResult* res=NULL;
629     res= sqlServer->Query(oss.str().c_str());  
630       query=oss.str();
631       msg1="SaveROOT_DB query: ";
632       msg1 += query;      
633       mainLogUtil->logInfo(msg1);  
634              
635       res= sqlServer->Query(query.c_str());
636     if(!res)     if(!res)
637     {         {    
638             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 641  bool PacketUser::saveROOT_DB(char* table
641             return false;             return false;
642     }     }
643    
    //marco_new: si pu� ottimizzare ...  
644     //the ID of the current ROOT file in table Table_ROOT_Good     //the ID of the current ROOT file in table Table_ROOT_Good
645           my_id = select_maxIDN_DB(table_name);           my_id = select_maxIDN_DB(table_name);
646             if(!boot_number){
647               if(id_to_recover_index<1000)
648                 id_to_recover[id_to_recover_index++]=my_id;
649             }else if (boot_number && id_to_recover_index && !is_new_route){
650               recover_boot_number();
651             }
652  //      cout<<"DBG: my_id = "<<my_id<<endl;  //      cout<<"DBG: my_id = "<<my_id<<endl;
653     if(res){delete res; res = NULL;}     if(res){delete res; res = NULL;}
654     return true;     return true;
655  }  }
656    
657    
658    void PacketUser::recover_boot_number(){
659      string msg1;
660      stringstream oss1;
661      stringstream oss;
662      oss.str("");  
663      string query="";
664      TSQLResult* res=NULL;
665      if(!boot_number || !id_to_recover_index)
666        return;
667      
668      for (int i = 0; i<id_to_recover_index;i++){
669        oss.str("");
670        oss << "UPDATE "<<  Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i] << ";";  
671       query = oss.str();
672       res= sqlServer->Query(query.c_str());
673       if(!res)
674       {    
675               oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;  
676               msg1=oss1.str();
677               mainLogUtil->logError(msg1);  
678               return ;
679       }
680      }
681    
682      for (int i = 0; i<id_to_recover_index;i++){
683        oss.str("");
684        oss << "UPDATE " <<  Table_ROOT_Merging << " SET BOOT_NUMBER=" <<  boot_number << " WHERE ROOT_ID_N=" << id_to_recover[i] << ";";  
685        query=oss.str();
686        res= sqlServer->Query(query.c_str());
687        if(!res)
688          {
689               oss1<<"DBError UNABLE to: "<<query.c_str()<<endl;  
690               msg1=oss1.str();
691               mainLogUtil->logError(msg1);  
692               return ;
693       }
694      }
695    
696      id_to_recover_index=0;
697    }
698    
699    
700    
701  /**********************************************************************************************/  /**********************************************************************************************/
702  /*###########################################################################################################*/  /*###########################################################################################################*/
703  /**********************************         MERGING          *************************************************/  /**********************************         MERGING          *************************************************/
704  /*###########################################################################################################*/  /*###########################################################################################################*/
705  //      Merge ROOT files: found other ROOT files in the same temporal ranges  //      Merge ROT files: find other ROOT files in the same temporal ranges
706  //    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
707  /**********************************************************************************************/  /**********************************************************************************************/
708  //try to merge current ROOT file with files in DB  //try to merge current ROOT file with files in DB
709  bool PacketUser::merge_ROOTfiles()  bool PacketUser::merge_ROOTfiles()
710  {  {
711          mainLogUtil->logInfo("Trying merging ROOT files");     stringstream log;
712       log.str("");
713       string slog;
714    
715    //marco_new: controlla    mainLogUtil->logInfo("Trying merging ROOT files");
716    double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;    double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;
717                                    
718    TSQLResult* res=NULL;      TSQLResult* res=NULL;  
719    TSQLRow* row =NULL;    TSQLRow* row =NULL;
720    //numero di ROOT files trovati sul DB che hanno relaz di tipo dato
721      unsigned int num_rows=0;
722    
723            //Record su DB:
724            unsigned int idN = 0;      
725            unsigned int root_id_db=0;
726            unsigned long int pkt_num_in_db=0;
727            unsigned long int pkt_num_fin_db=0;
728            unsigned long int pkt_obt_in_db=0;
729            unsigned long int pkt_obt_fin_db=0;
730            unsigned long int time_in_db=0;
731            unsigned long int time_fin_db=0;
732            unsigned int boot_num_db=0;
733            double perc=0;                  
734    
735    
736    //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
737    //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
738    for(int relaz=AFTER; relaz<=BIGGER; relaz++)    for(int relaz=AFTER; relaz<=BIGGER; relaz++)
739    {                {    
         //marco_new: controlla tutto  
740          //marco_new: esci se sono arrivato a zero          //marco_new: esci se sono arrivato a zero
741          if(real_time_last==real_time_init){              if(real_time_last==real_time_init)
742            {
743                    log.str("");
744                    log<<"Current File Lenght=0; real_time_last=real_time_init= "<<real_time_last;
745                    slog=log.str();
746                    mainLogUtil->logAll(slog);
747                  mainLogUtil->logInfo("Finish merging ROOT files");                                mainLogUtil->logInfo("Finish merging ROOT files");              
748                  if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}
749                  if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}
750                  return true;                                              return true;                            
751          }          }
752    
753          res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);          num_rows=0;
754            res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);
755                    
         unsigned int num_rows=0;  
756           if (!res)           if (!res)
757           {           {
758                  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 760  bool PacketUser::merge_ROOTfiles()
760           }           }
761    
762           num_rows=res->GetRowCount();           num_rows=res->GetRowCount();
763  //       cout<<"MMMMM trovati num_rows "<< num_rows<< " per relaz= "<<relaz;           log.str("");
764             log<<"Found "<<num_rows<<" ROOT file in DB with relation of kind = "<<relaz;
765             slog=log.str();
766             mainLogUtil->logAll(slog);
767    
768           if(num_rows>0)           if(num_rows>0)
769           {                                 {
770                   for(unsigned int i=0; i<num_rows; i++)                  for(unsigned int i=0; i<num_rows; i++)
771                   {                   {
772                           row=res->Next();                           row=res->Next();
773                           if (!row)                           if (!row)
774                           {                           {
775                                   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");
776                                  if(res){delete res; res = NULL;}                                  if(res){delete res; res = NULL;}
777                                  return false;                                  return false;
778                           }                           }
779                                                    
780                            //Leggi Record dal DB:
781                          //TODO ricavare tutte le info che servono                          idN=atoll(row->GetField(0));    
782                          unsigned int idN = 0;                                root_id_db=atoll(row->GetField(1));
783                          idN=atoi(row->GetField(0));                          pkt_num_in_db=atoll(row->GetField(2));
784                                            pkt_num_fin_db=atoll(row->GetField(3));
785                          double perc =0;                                          pkt_obt_in_db=atoll(row->GetField(4));
786                          //marco_new:                          pkt_obt_fin_db=atoll(row->GetField(5));
787                            time_in_db=atoll(row->GetField(6));
788                            time_fin_db=atoll(row->GetField(7));
789                            boot_num_db=atoll(row->GetField(8));            
790                          perc=atof(row->GetField(9));                          perc=atof(row->GetField(9));
791                          //unsigned long int M_pkt_number_in, M_pkt_number_fin, M_obt_in, M_obt_fin, M_mtime_init, M_mtime_last;                          
792                          if(perc>percentage){                          log.str("");
793                            log<<"*** ROOT file found in DB has idN= "<<idN<<"  root_id_db= "<<root_id_db;
794                            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;
795                            log<<"  time_in_db= "<<time_in_db<<"  time_fin_db= "<<time_fin_db<<" boot_num_db= "<<boot_num_db<<"  perc= "<<perc<<"  ***";                    
796                            log<<"\n*** Current ROOT file has pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
797                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
798                            slog=log.str();
799                            mainLogUtil->logAll(slog);
800                            if(perc>percentage)
801                            {
802                                  mainLogUtil->logInfo("Current ROOT file is better than the one in DB");                                                  mainLogUtil->logInfo("Current ROOT file is better than the one in DB");                
803                                  //modifica DB                                  //modifica DB
804                                  updateMergeROOT_DB(Table_ROOT_Merging,                                  updateMergeROOT_DB(Table_ROOT_Merging,
805                                          atoi(row->GetField(1)), atoi(row->GetField(2)),                                                                          root_id_db,
806                                          atoi(row->GetField(3)), atoi(row->GetField(4)),                                                                          pkt_num_in_db, pkt_num_fin_db,
807                                          atoi(row->GetField(5)), atoi(row->GetField(6)),                                                                          pkt_obt_in_db, pkt_obt_fin_db,
808                                          atoi(row->GetField(7)), atoi(row->GetField(8)),                                                                          time_in_db, time_fin_db,
809                                          perc,                                                                          boot_num_db,
810                                          idN,(type_Rel_ROOT)relaz);                                                                          perc,
811                                                                            idN,(type_Rel_ROOT)relaz);
812                          }                          }
813                          else{                          else
814                                  mainLogUtil->logInfo("Found ROOT files in DB better than current ROOT file");                                    {
815                                    mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file");        
816                                  if(relaz == AFTER){                                  if(relaz == AFTER)
817                                    {
818                                          mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");                                          mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
819                                          pkt_number_init= atoi(row->GetField(3));                                          pkt_number_init =  (pkt_num_fin_db+1);
820                                          obt_init= atoi(row->GetField(5));                                          obt_init = pkt_obt_fin_db;
821                                          real_time_init= atoi(row->GetField(7));                                                                          real_time_init = time_fin_db;  
822                                            log.str("");
823                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
824                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
825                                            slog=log.str();
826                                            mainLogUtil->logAll(slog);
827                                  }                                  }
828                                  else if(relaz == BEFORE){                                  else if(relaz == BEFORE)
829                                          mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");                                            {
830                                          pkt_number_last= atoi(row->GetField(2));                                          mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");                                  
831                                          obt_last= atoi(row->GetField(4));                                          pkt_number_last = (pkt_num_in_db-1);
832                                          real_time_last= atoi(row->GetField(6));                                          obt_last = pkt_obt_in_db;
833                                            real_time_last = time_in_db;
834    
835                                            log.str("");
836                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
837                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
838                                            slog=log.str();
839                                            mainLogUtil->logAll(slog);
840                                  }                                  }
841                                  else if(relaz == SMALLER)                                  else if(relaz == SMALLER)
842                                  {                                  {
843                                          //non devo proprio salvarlo ed esco ...                                                                  //non devo proprio salvarlo ed esco ...                        
844                                          mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");                                          mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
845                                          mainLogUtil->logInfo("Finish merging ROOT files");                                          mainLogUtil->logInfo("\nFinish merging ROOT files");
846                                          if(res){delete res; res = NULL;}                                          if(res){delete res; res = NULL;}
847                                          if(row){delete row; row = NULL;}                                          if(row){delete row; row = NULL;}                        
848                                          return true;                                          return true;
849                                  }                                  }
850                                  else if(relaz == BIGGER)//spezzettamento                                  else if(relaz == BIGGER)//spezzettamento
851                                  {                                        {      
852                                          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");                                                      
853                                          saveMergeROOT_DB(Table_ROOT_Merging, my_id,                                          saveMergeROOT_DB(Table_ROOT_Merging, my_id,
854                                                  pkt_number_init, atoi(row->GetField(2)),                                                  pkt_number_init, (pkt_num_in_db-1),
855                                                  obt_init, atoi(row->GetField(4)),                                                  obt_init, pkt_obt_in_db,
856                                                  real_time_init, atoi(row->GetField(6)),                                                  real_time_init, time_in_db,
857                                                  boot_number,                                                  boot_number,
858                                                  percentage);                                                  percentage);
859                                                                                            
860                                          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");
861                                          pkt_number_init= atoi(row->GetField(3));                                          pkt_number_init = (pkt_num_fin_db+1);
862                                          obt_init= atoi(row->GetField(5));                                          obt_init= pkt_obt_fin_db;
863                                          real_time_init= atoi(row->GetField(7));                                          real_time_init= time_fin_db;
864                                  }                                          
865                          }                                          log.str("");
866                                            log<<"\n*** Current ROOT file becomes: pkt_number_init= "<<pkt_number_init<<"  obt_init= "<<obt_init<<"  real_time_init= "<<real_time_init;
867                                            log<<" pkt_number_last= "<<pkt_number_last<<"  obt_last= "<<obt_last<<"  real_time_last= "<<real_time_last<<" ***";
868                                            slog=log.str();
869                                            mainLogUtil->logAll(slog);
870                                    }//if relaz                    
871                            }//if perc
872                  }//for num_rows                      }//for num_rows    
873            }           }//if num_rows
         else{  
                 //marco_new:  
                 mainLogUtil->logInfo("Found 0 ROOT files in DB that can be merged with current ROOT file");              
   
         }  
874    
         //marco_new:???  
875          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
876          if(row){delete row; row = NULL;}                  if(row){delete row; row = NULL;}        
877  }//for          
878    }//for relaz
879    
880          mainLogUtil->logInfo("Saving Current ROOT file");                mainLogUtil->logInfo("Saving Current ROOT file");      
881          saveMergeROOT_DB(Table_ROOT_Merging, my_id,          saveMergeROOT_DB(Table_ROOT_Merging, my_id,
# Line 761  bool PacketUser::merge_ROOTfiles() Line 885  bool PacketUser::merge_ROOTfiles()
885                           boot_number,                           boot_number,
886                           percentage);                           percentage);
887    
   
888          mainLogUtil->logInfo("Finish merging ROOT files");                mainLogUtil->logInfo("Finish merging ROOT files");      
889    return true;    return true;
890    
891  }  }
892    
893  //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 899  bool PacketUser::updateMergeROOT_DB(char
899                          unsigned long int mboot_num,                          unsigned long int mboot_num,
900                          double bad_perc,                          double bad_perc,
901                          unsigned int ID_record, type_Rel_ROOT type_rel){                          unsigned int ID_record, type_Rel_ROOT type_rel){
         stringstream oss;  
         oss.str("");      
902                    
903                                    stringstream oss;
904                                    oss.str("");    
905    
906          if(type_rel == AFTER){          if(type_rel == AFTER){
907                  mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");                  mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
908                  //marco_new:tolti tutti gli apici che qui non devono essere                  //marco_new:tolti tutti gli apici che qui non devono essere
909                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
910                  << " SET PKT_NUMBER_FINAL ="<< pkt_number_init                        << " SET PKT_NUMBER_FINAL ="<< (pkt_number_init - 1)        
911                          <<", PKT_OBT_FINAL ="<< obt_init                          <<", PKT_OBT_FINAL ="<< obt_init
912                      <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";                      <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";
913    
914          }          }
915          else if(type_rel == BEFORE){                      else if(type_rel == BEFORE){            
916                          mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");                          mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");        
917                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
918                  << " SET PKT_NUMBER_INIT ="<< pkt_number_last                        << " SET PKT_NUMBER_INIT ="<< (pkt_number_last+1)  
919                          <<", PKT_OBT_INIT ="<< obt_last                          <<", PKT_OBT_INIT ="<< obt_last
920                      <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";                      <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";
921          }          }
# Line 798  bool PacketUser::updateMergeROOT_DB(char Line 924  bool PacketUser::updateMergeROOT_DB(char
924                  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");                                                    
925                    
926                  oss << "UPDATE "<< table_name                  oss << "UPDATE "<< table_name
927                          << " SET PKT_NUMBER_FINAL="<< pkt_number_init                        << " SET PKT_NUMBER_FINAL="<< (pkt_number_init-1)  
928                          <<", PKT_OBT_FINAL ="<< obt_init                          <<", PKT_OBT_FINAL ="<< obt_init
929                              <<", 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");                                            
930    
931                          saveMergeROOT_DB(table_name, root_id,                  mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");                                          
932                           pkt_number_last, pkt_number_fin,  
933                           obt_last, obt_fin,                  saveMergeROOT_DB(table_name, root_id,
934                           real_time_last, mtime_last,                   (pkt_number_last+1), pkt_number_fin,
935                           mboot_num,                   obt_last, obt_fin,
936                           bad_perc);                   real_time_last, mtime_last,
937                     mboot_num,
938                     bad_perc);
939          }          }
940          else if(type_rel == BIGGER){          else if(type_rel == BIGGER){
941          //marco_new: ok anche se coincidenti etc          //marco_new: ok anche se coincidenti etc
942            oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";";            //      oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO
943            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
944              mainLogUtil->logInfo("Record deleted from merging table");    
945          }          }
946          else          else
947                  return false;                  return false;
948    
   
   
949          string msg = oss.str();          string msg = oss.str();
950          mainLogUtil->logInfo(msg);                mainLogUtil->logInfo(msg);      
951                    
# Line 841  bool PacketUser::updateMergeROOT_DB(char Line 966  bool PacketUser::updateMergeROOT_DB(char
966     return true;     return true;
967  }  }
968    
969  //marco_new: check here  //cerca eventuali ROOT files in DB che possono venire mergiati
970    //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
971    //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
972    //marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx
973  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)
974  {  {
   
 //      unsigned  int idN=0;  
975          TSQLResult* res=NULL;          TSQLResult* res=NULL;
976          stringstream query;          stringstream query;
977          query.str("");          query.str("");
978    
         //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  
979          if(type_rel == AFTER)          if(type_rel == AFTER)
980            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
981          else if(type_rel == BEFORE)          else if(type_rel == BEFORE)
982            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
983          else if(type_rel == SMALLER)          else if(type_rel == SMALLER)
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 if(type_rel == BIGGER)          else if(type_rel == BIGGER)
986                  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
987          else          else
988                  return 0;                  return 0;
989                    
990          query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile          query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile
991            //se volessi mettere un filtro sulla qualita'
992          //      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" ;
           
     
993          string msg = query.str();          string msg = query.str();
994          mainLogUtil->logAll(msg);          mainLogUtil->logAll(msg);
995            res= sqlServer->Query(query.str().c_str());      
         res= sqlServer->Query(query.str().c_str());  
           
996          stringstream oss1;          stringstream oss1;
997           oss1.str()="";          oss1.str()="";
998           string msg1;          string msg1;
999        
1000           if (!res)           if (!res)
1001           {           {
# Line 897  bool PacketUser::saveMergeROOT_DB(char* Line 1017  bool PacketUser::saveMergeROOT_DB(char*
1017    
1018          stringstream oss;          stringstream oss;
1019          oss.str("");              oss.str("");    
1020          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)"
1021      << " 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<< "','"
1022      << mtime_init << "','" << mtime_last << "','"      << mtime_init << "','" << mtime_last << "','"
1023          << mboot_num <<"','"          << mboot_num <<"','"
1024          << percentage <<"',"                << percentage <<"',"      
1025      <<"NULL"<<                    <<"NULL"<<",'"
1026      ")";              << tag_value<<"'"
1027                << ")";
1028    
1029          string msg = oss.str();          string msg = oss.str();
1030          mainLogUtil->logAll(msg);                mainLogUtil->logAll(msg);      
1031            
1032          //marco_new: controlla          //marco_new: controlla
1033          //marco_new: esci se sono arrivato a zero          //marco_new: esci se sono arrivato a zero
1034  //      if(real_time_last==real_time_init){      //      if(real_time_last==real_time_init){    
# Line 919  bool PacketUser::saveMergeROOT_DB(char* Line 1040  bool PacketUser::saveMergeROOT_DB(char*
1040                  return true;                                              return true;                            
1041          }          }
1042    
   
1043          stringstream oss1;          stringstream oss1;
1044          oss1.str()="";          oss1.str()="";
1045          string msg1;          string msg1;
# Line 950  if (!strcmp(table_name,Table_ROOT_Bad)) Line 1070  if (!strcmp(table_name,Table_ROOT_Bad))
1070          stringstream oss;          stringstream oss;
1071      oss.str("");      oss.str("");
1072      oss<<"select max(ID_N) from "<<table_name;      oss<<"select max(ID_N) from "<<table_name;
1073          string msg = oss.str();  //      string msg = oss.str();
1074          mainLogUtil->logAll(msg);        //      mainLogUtil->logAll(msg);      
1075      res= sqlServer->Query(oss.str().c_str());      res= sqlServer->Query(oss.str().c_str());
1076                                    
1077          stringstream oss1;          stringstream oss1;
# Line 979  if (!strcmp(table_name,Table_ROOT_Bad)) Line 1099  if (!strcmp(table_name,Table_ROOT_Bad))
1099          idN=atoi(row->GetField(0));          idN=atoi(row->GetField(0));
1100          if(row){delete row; row = NULL;}          if(row){delete row; row = NULL;}
1101          if(res){delete res; res = NULL;}          if(res){delete res; res = NULL;}
1102    
1103        oss<<". idN = "<<idN;
1104            string msg = oss.str();
1105            mainLogUtil->logAll(msg);      
1106    
1107          return idN;          return idN;
1108  }  }
1109    
# Line 1016  int PacketUser::LockTables(char* tableTo Line 1141  int PacketUser::LockTables(char* tableTo
1141          }          }
1142                        
1143          if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}        
1144          mainLogUtil->logAll("######################### TABLES LOCKED ############################");                              mainLogUtil->logAll("TABLES LOCKED");                  
1145          return 0;                return 0;      
1146  }  }
1147    
# Line 1050  int PacketUser::UnLockTables(){   Line 1175  int PacketUser::UnLockTables(){  
1175          }          }
1176                                    
1177          if(res){delete res; res = NULL;}                  if(res){delete res; res = NULL;}        
1178          mainLogUtil->logAll("######################### TABLES UNLOCKED ############################");            mainLogUtil->logAll("TABLES UNLOCKED");
1179    
1180          return 0;                return 0;      
1181  }  }
# Line 1058  int PacketUser::UnLockTables(){   Line 1183  int PacketUser::UnLockTables(){  
1183  /*********************************************************/  /*********************************************************/
1184  //marco_new_31  //marco_new_31
1185  void PacketUser::OpenDBConnection(char* tableTobeLocked){  void PacketUser::OpenDBConnection(char* tableTobeLocked){
         mainLogUtil->logAll("######################### OpenDBConnection ############################");  
1186          if(do_cont_check){          if(do_cont_check){
1187                  //controllo che non sia gi� aperta                  //controllo che non sia gia' aperta
1188                  if(!sqlServer){                  if(!sqlServer){
1189                          sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);                          sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
1190                          if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}                                if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}      
1191                            mainLogUtil->logAll("Connected to DB");
1192                          //marco_new: blocca tutte le tabelle                          //blocca le tabelle
1193                          LockTables(tableTobeLocked);                          LockTables(tableTobeLocked);            
                         mainLogUtil->logAll("######################### OpenDBConnection OK ############################");        
1194                  }                  }
1195          }          }
1196    
# Line 1075  void PacketUser::OpenDBConnection(char* Line 1198  void PacketUser::OpenDBConnection(char*
1198    
1199  //marco_new_31  //marco_new_31
1200  void PacketUser::CloseDBConnection(){  void PacketUser::CloseDBConnection(){
         mainLogUtil->logAll("######################### CloseDBConnection ############################");          
1201          if(do_cont_check){                                                if(do_cont_check){                                      
1202                  //controllo che ce ne sia una aperta                  //controllo che ce ne sia una aperta
1203                  if(sqlServer && sqlServer->IsConnected()){                  if(sqlServer && sqlServer->IsConnected()){
1204                          //marco_new: sblocca tutte le tabelle                          //marco_new: sblocca tutte le tabelle
1205                          UnLockTables();                                  UnLockTables();        
1206                          sqlServer->Close();                          sqlServer->Close();
1207                            delete sqlServer;                      
                         //marco_new_01: ??  
                         delete sqlServer;  
                           
1208                          sqlServer=NULL;                          sqlServer=NULL;
1209                          mainLogUtil->logAll("######################### CloseDBConnection OK ############################");                              mainLogUtil->logAll("Closed Connection to DB");
1210                  }                  }
1211          }          }
1212  }  }
# Line 1118  void PacketUser::savePKT_file(char* head Line 1237  void PacketUser::savePKT_file(char* head
1237  {  {
1238          char fno[80]="";          char fno[80]="";
1239          ofstream fout;          ofstream fout;
1240          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
1241    
1242          if(append==true)          if(append==true)
1243                  fout.open(fno, ios::binary | ios::app);                  fout.open(fno, ios::binary | ios::app);
# Line 1138  void PacketUser::saveALL_PKT(char* heade Line 1257  void PacketUser::saveALL_PKT(char* heade
1257          ofstream fout;          ofstream fout;
1258          if(append==true)          if(append==true)
1259          {                        {              
1260                  sprintf(fno,"%s/packets.pkt", outDir);                  sprintf(fno,"%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI
1261                  fout.open(fno, ios::binary | ios::app);                  fout.open(fno, ios::binary | ios::app);
1262          }          }
1263          else{          else{
1264                  sprintf(fno,"%s/packet%d.pkt", outDir, numPKT );                  sprintf(fno,"%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT ); // EMI
1265                  fout.open(fno, ios::binary);                                      fout.open(fno, ios::binary);                    
1266          }          }
1267                                    

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

  ViewVC Help
Powered by ViewVC 1.1.23