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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Tue Sep 23 07:20:12 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
Initial revision

1 mocchiut 1.1 //============================================================================
2     // $Id: PacketUser.cpp,v 1.67 2008-09-05 14:33:48 messineo Exp $
3     // Description :
4     //============================================================================
5     #include "PacketUser.h"
6     #include <sys/time.h>
7    
8     namespace PamOffLineSW
9     {
10     extern LogUtil* mainLogUtil;
11     extern short compression ;
12     extern char *outDir;
13     extern TSQLServer *sqlServer;
14     extern bool multiFile;
15     extern char* fni;
16     extern char * nome_output;
17     extern bool is_new_route;
18     extern unsigned int download;
19    
20     extern unsigned int mmm_number;
21     extern unsigned int orbit_number;
22     extern unsigned long int time_Offset;
23     extern bool tryMerge;
24     //marco_new_01
25     extern bool single_connection;
26    
27     //don't perform check about continuity: it means don't use DB.
28     extern bool do_cont_check;
29    
30     //marco_new_31:
31     extern char* db_user;
32     extern char* db_pwd;
33     extern char* connection;
34    
35     PacketUser PacketUser::instance;
36    
37     int PacketUser::numDiscontinity=1;
38     int PacketUser::numPKT=0;
39     int PacketUser::numPKTSaved=0;
40    
41     PacketUser& PacketUser::getInstance()
42     {
43     return instance;
44     }
45    
46     PacketUser::PacketUser()
47     {
48     gROOT->SetBatch(kTRUE);
49     numDiscontinity=1;
50     numPKTSaved=0;
51     numPKT=0;
52     pkt_number_init=0;
53     obt_init=0;
54     pkt_number_last=0;
55     obt_last=0;
56     obt_time_sync=0;
57     last_time_sync_info=0;
58     real_time_init=0;
59     real_time_last=0;
60     bad_pkt=0;
61     bad_pkt_EventReader=0;
62     bad_pkt_CalibReader=0;
63     reader= NULL;
64     pRun=NULL;
65     reader=new pamela::techmodel::EventReader();
66     Table_ROOT_Good="ROOT_TABLE";
67     Table_ROOT_Bad="ROOT_TABLE_BAD";
68     Table_GL_RESURS_OFFSET="GL_RESURS_OFFSET";
69     Table_ROOT_Merging="ROOT_TABLE_MERGING";
70     // good_pkt_Calib=0;
71     my_id=0;
72     boot_number=0;
73     }
74    
75     PacketUser::~PacketUser()
76     {
77     if(reader){delete reader; reader = NULL;}
78     numDiscontinity=1;
79     numPKTSaved=0;
80     numPKT=0;
81     pkt_number_init=0;
82     obt_init=0;
83     pkt_number_last=0;
84     obt_last=0;
85     obt_time_sync = 0;
86     last_time_sync_info = 0;
87     real_time_init=0;
88     real_time_last=0;
89     bad_pkt=0;
90     bad_pkt_EventReader=0;
91     bad_pkt_CalibReader=0;
92     // good_pkt_Calib=0;
93     my_id=0;
94     boot_number=0;
95     }
96    
97     //Put the packet in a root file. Create a new ROOT file for each group of packets
98     void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood, const PacketType* type,
99     unsigned long int counter, unsigned long int obt)
100     {
101     //If the packet type was not recognised before
102     if(!type){return;}
103    
104    
105     /* //zzzzzz marco per debug mi salvo il pacchetto vardump
106     if(type==PacketType::VarDump)
107     {
108     long int dataLength = length - 2; //the block of data
109     int b_offset = 4;
110     while (b_offset < dataLength){
111     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);
112     cout<<"IN "<<numPKTSaved<<" boot_number "<<boot_number<<" al b_offset "<<b_offset<<" di "<<dataLength<<endl;
113    
114     b_offset = b_offset + 5;
115     }
116    
117     cout<<"IN "<<numPKTSaved<<" FINAL boot_number "<<boot_number<<endl;
118     savePKT_file(headerPkt,pamPkt,length,false,"vardump");
119     numPKTSaved++;
120     }
121     return;
122     */
123    
124    
125     //in order to start ...
126     if(numPKT==0)
127     {
128     //marco_new_31: metto qui se voglio avere come in origine apertura Connessione all'inizio e chiusura alla fine
129     //marco_new_01
130     if(single_connection){
131     OpenDBConnection(NULL);
132     }
133    
134     setInit(counter,obt);
135     StartGroup();
136     }
137    
138     //here we know if the incoming packet can be considered consecutive to the previuos one
139     if((!isCons)&&(numPKT!=0))
140     {
141     setReal_Time();
142     //closing the group in file rootfilename
143     FinishGroup(rootfilename);
144    
145     //numDiscontinity++;
146     //se gia' e' cambiato download nn cambio numdisco
147     if(!is_new_route) numDiscontinity++;
148    
149     //the current pkt will be placed in a new group: rootfilename
150     setInit(counter,obt);
151     StartGroup();
152     }
153    
154     //add current packet to the group and calculate bad_pkt_EventReader and bad_pkt_CalibReader
155     int ret=
156     reader->PKT_RunEvent(headerPkt, pamPkt, length, type);
157     /*
158     ret
159     0 packet good
160     1 exception but used //never happen
161     2 CRC exception but used
162     3 CALIBRATION PACKET with error but used
163     -1 CRC exception packet DISCARDED
164     -2 FATAL exception packet DISCARDED //never happen
165     -3 No way to read events of this type. packet DISCARDED( es OLD CalibCal)
166     ret = 10 if the packet is good but comes from a cadre with VRL problems
167     */
168    
169     //here we know if the packet comes from one or more corrupted cadres
170     //if(!isPKTGood){bad_pkt++;}
171     if((!isPKTGood)&&(!ret)){ret=10;}
172    
173     switch (ret)
174     {
175     case 0: {numPKTSaved++; break;}
176     case 1: {bad_pkt_EventReader++; numPKTSaved++; break;}
177     case 2: {bad_pkt_EventReader++;numPKTSaved++; break;}
178     case 3: {bad_pkt_CalibReader++; numPKTSaved++; break;}
179     case 10: {bad_pkt++; numPKTSaved++; break;}
180     default:{
181     stringstream oss;
182     oss.str()="";
183     oss<<"Packet DISCARDED, reason = "<<ret;
184     string msg = oss.str();
185     mainLogUtil->logAll(msg);
186     break;
187     }
188     }
189    
190     /*
191     if ((ret==0)&&((type==PacketType::CalibTrk1)||(type==PacketType::CalibTrk2)|| (type==PacketType::CalibCalPed)))
192     {
193     good_pkt_Calib++;//maybe I will remove this in future
194     }
195     */
196    
197     if(ret>=0)// only the packet not discarded of course
198     setLast(counter,obt);
199    
200     //TODO: decide what kind of packet I can use here
201     // if((ret==0)||(ret==10))//I want to use only good packet
202     if(ret>=0)//I want to use only accepted packet
203     {
204     setTimeSync(pamPkt, length, type);//marco_new: here I retrieve also the boot_number if packet type is vardump
205     }
206    
207     //just to be sure ...
208     if(pamPkt){delete[] pamPkt; pamPkt = NULL;}
209     //just to be sure ...
210     if(headerPkt){delete[] headerPkt; headerPkt = NULL;}
211    
212     numPKT++;
213     }
214    
215     //set the value of the counter and obt of the first packet of the group
216     void PacketUser::setInit(unsigned long int counter, unsigned long int obt)
217     {
218     sprintf(nnnn_mmm_ppp,"%05d_%03d_%03d", orbit_number, mmm_number, download);
219     sprintf(rootfilename,"%s_%s_%d", nome_output, nnnn_mmm_ppp, numDiscontinity);
220     pkt_number_init=counter;
221     obt_init=obt;
222     pkt_number_last=0;
223     obt_last=0;
224     real_time_init=0;
225     real_time_last=0;
226     bad_pkt=0;
227     numPKTSaved=0;
228     bad_pkt_EventReader=0; bad_pkt_CalibReader=0;
229     //good_pkt_Calib=0;
230    
231     //marco_NB:
232     //ogni volta che trovo una discontinuit� resetto come se mi trovassi in un nuovo download oppure uso i vecchi valori se esistono?
233     obt_time_sync=0; last_time_sync_info=0; boot_number=0;
234     // NB: se ho discontinuita in genere non so a che download appartiene potrebbe proprio essere sbagliato tenere i vecchi valori
235    
236     }
237    
238     //starts a new root file
239     void PacketUser::StartGroup()
240     {
241     pRun = new PamelaRun(rootfilename, outDir , multiFile, compression);
242     //marco_new: aggiunta estensione .root
243     strcat(rootfilename,".root");
244     reader->Init(pRun);
245     stringstream oss;
246     oss.str()="";
247     oss<<"######################### Start a new group in file: "<< rootfilename <<" ############################";
248     string msg = oss.str();
249     mainLogUtil->logInfo(msg);
250     }
251    
252     //set the value of the counter and obt of the last packet of the group
253     void PacketUser::setLast(unsigned long int counter, unsigned long int obt)
254     {
255     pkt_number_last=counter;
256     obt_last=obt;
257     }
258    
259    
260    
261     //retrieve obt_time_sync and last_time_sync_info from packet if type has special values
262     void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type)
263     {
264     //do nothing
265     if(!do_cont_check) {
266     if(is_new_route)
267     is_new_route=false;
268     return;
269     }
270    
271     //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0
272     //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi
273    
274    
275     // devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download
276    
277     //When I found a new download I need to reset all values
278     if(is_new_route)
279     {
280     //reset
281     is_new_route=false;
282     obt_time_sync=0; last_time_sync_info=0;
283     boot_number=0;
284     if(time_Offset)
285     timeOffset=time_Offset;
286     else
287     timeOffset=retrieveTimeOffset(Table_GL_RESURS_OFFSET);
288     }
289    
290     //retrieve boot number if possible
291     setBootNumber(packet,pktLength,type);
292    
293     //Retrieve if possible obt_time_sync and last_time_sync_info
294     int offset=0;
295     if(type==PacketType::RunHeader)
296     {
297     offset=0;
298     }
299     else if(type==PacketType::RunTrailer)
300     {
301     offset=3;
302     }
303     else if(type==PacketType::Mcmd)
304     {
305     // mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");
306     return;
307     }
308     else
309     {
310     return;
311     }
312    
313     //I don't need to retrieve them again if they are good
314     if((obt_time_sync)||(last_time_sync_info)) return;
315    
316     obt_time_sync = (((UINT32)packet[5+offset]<<24)&0xFF000000) + (((UINT32)packet[6+offset]<<16)&0x00FF0000) + (((UINT32)packet[7+offset]<<8)&0x0000FF00) + (((UINT32)packet[8+offset])&0x000000FF);
317     last_time_sync_info = (((UINT32)packet[9+offset]<<24)&0xFF000000) + (((UINT32)packet[10+offset]<<16)&0x00FF0000) + (((UINT32)packet[11+offset]<<8)&0x0000FF00) + (((UINT32)packet[12+offset])&0x000000FF);
318    
319     stringstream oss;
320     oss.str()="";
321     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();
322     string msg = oss.str();
323     mainLogUtil->logAll(msg);
324     }
325    
326     //Boot Number
327     void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type)
328     {
329     //se gia calcolato esco: lo calcolo una volta per download e per continuit�
330     if(boot_number) return;
331     //todo: controlla se algo giusto: preso da VarDumpReader.cpp
332     //mmm e' sbagliatissimo cambiare !!!
333     if(type==PacketType::VarDump)
334     {
335     long int dataLength = pktLength - 2; //the block of data
336     int b_offset = 4;
337     while (b_offset < dataLength){
338     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);
339     b_offset = b_offset + 5;
340     }
341     stringstream oss;
342     oss.str()="";
343     oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();
344     string msg = oss.str();
345     mainLogUtil->logAll(msg);
346     return;
347     }
348     else
349     {
350     return;
351     }
352    
353     }
354     //set the real time of the first packet and the last packet oif a group of packet if possible
355     void PacketUser::setReal_Time()
356     {
357     if((obt_time_sync)||(last_time_sync_info))
358     {
359     real_time_init=(obt_init/1000-obt_time_sync)+last_time_sync_info;
360     real_time_last=(obt_last/1000-obt_time_sync)+last_time_sync_info;
361     real_time_init+=timeOffset;
362     real_time_last+=timeOffset;
363     }
364     }
365    
366     //timeOffset
367     //retrieve from table Table_GL_RESURS_OFFSET timeOffset
368     unsigned long int PacketUser::retrieveTimeOffset(char * table)
369     {
370     if(!do_cont_check) return 0;
371    
372     //if(!table){table=Table_GL_RESURS_OFFSET}
373    
374     //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve
375     //marco_new_01
376     if(!single_connection){
377     OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte
378     }
379    
380     UInt_t t0 = 0; //toffset
381    
382     stringstream oss;
383     oss.str("");
384     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='"
385     << nnnn_mmm_ppp << "';";
386    
387     string msg = oss.str();
388     mainLogUtil->logAll(msg);
389    
390     TSQLResult* res=NULL;
391     TSQLRow* row=NULL;
392     res= sqlServer->Query(oss.str().c_str());
393     if(!res)
394     {
395     mainLogUtil->logError("DBError retrieveTimeOffset 1");
396     return 0;
397     }
398    
399     row=res->Next();
400     if (!row )
401     {
402     if(res){delete res; res = NULL;}
403    
404     oss.str("");
405     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 < "
406     << orbit_number << " order by FROM_ORBIT desc limit 1;";
407    
408     string msg2 = oss.str();
409     mainLogUtil->logAll(msg2);
410    
411     res= sqlServer->Query(oss.str().c_str());
412     if(!res)
413     {
414     mainLogUtil->logError("DBError retrieveTimeOffset 2");
415     return 0;
416     }
417     row=res->Next();
418     if (!row)
419     {
420     mainLogUtil->logError("DBError retrieveTimeOffset 3");
421     return 0;
422     }
423    
424     }
425    
426     TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
427     t0 = (UInt_t)tu.GetSec();
428    
429     if(res){delete res; res = NULL;}
430     if(row){delete row; row = NULL;}
431    
432     //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve
433     //marco_new_01
434     if(!single_connection){
435     CloseDBConnection();
436     }
437     return t0;
438     }
439    
440     //this is public and can be called from the main at the end of the game
441     void PacketUser::FinishLastGroup()
442     {
443     mainLogUtil->logAll("######################### Closing the last group ############################");
444     setReal_Time();
445     FinishGroup(rootfilename);
446     mainLogUtil->logAll("######################### Closed the last group ############################");
447    
448     //marco_new_31:metto qui se voglio avere come in origine apertura Connessione all'inizio e chiusura alla fine
449     //marco_new_01
450     if(single_connection){
451     CloseDBConnection();
452     }
453     }
454    
455     //Finish the old root file
456     void PacketUser::FinishGroup(char * filename)
457     {
458     if(pRun)
459     {
460     pRun->WriteFiles();
461     delete pRun; pRun = NULL;
462     stringstream close;
463     close.str()="";
464     string msg;
465    
466     close<<"######################### Close group in file: "<< rootfilename <<" ############################";
467     msg = close.str();
468     mainLogUtil->logInfo(msg);
469    
470     // se non faccio proprio controlli su continuita' neanche salvo su DB
471     if(!do_cont_check) return;
472    
473     stringstream oss;
474     oss.str()="";
475    
476     //TODO: check when I don't want to log in DB informations
477    
478     // se non ho salvato nessun pacchetto
479     if(numPKTSaved==0){
480     stringstream err;
481     err.str()="";
482     err<<"NO PACKET SAVED in file: "<< rootfilename <<" The group is empty: you can remove this file.";
483     msg = err.str();
484     mainLogUtil->logError(msg);
485     return;
486     }
487    
488     //se non ho salvato nessun pacchetto buono
489     if((numPKTSaved-bad_pkt_EventReader-bad_pkt_CalibReader-bad_pkt)==0){
490     stringstream err;
491     err.str()="";
492     err<<"NO GOOD PACKET SAVED in file: "<< rootfilename <<" The group is BAD: you may want to remove this file.";
493     msg = err.str();
494     mainLogUtil->logInfo(msg);
495     // mainLogUtil->logError(msg);
496     // return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???
497     }
498    
499     //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve
500     //marco_new_01
501     if(!single_connection){
502     OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
503     }
504    
505     //don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range
506     if((!real_time_init)&&(!real_time_last))
507     {
508     //saved in another table for future study
509     if(saveROOT_DB(Table_ROOT_Bad, outDir,
510     filename,
511     pkt_number_init, pkt_number_last,
512     obt_init, obt_last,
513     obt_time_sync, last_time_sync_info,
514     real_time_init, real_time_last,
515     boot_number,
516     timeOffset,
517     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
518     fni)==true)
519     {
520     oss.str()="";
521     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Bad;
522     msg = oss.str();
523     mainLogUtil->logInfo(msg);
524     }
525     else
526     {
527     oss.str()="";
528     oss<<"Problem storing information in DB regarding file: "<<filename;
529     msg = oss.str();
530     mainLogUtil->logError(msg);
531     }
532    
533     //marco_new_31:metto qui se voglio aprire e chiudere Connessione solo quando serve
534     //marco_new_01
535     if(!single_connection){
536     CloseDBConnection();
537     }
538    
539     return;
540     }
541    
542     //saves info in DB in table Table_ROOT_Good
543     if(saveROOT_DB(Table_ROOT_Good, outDir,
544     filename,
545     pkt_number_init, pkt_number_last,
546     obt_init, obt_last,
547     obt_time_sync, last_time_sync_info,
548     real_time_init, real_time_last,
549     boot_number,
550     timeOffset,
551     bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, numPKTSaved,
552     fni)==true)
553     {
554     oss<<"Saved information regarding file: "<<filename<<" in table "<<Table_ROOT_Good<< " id= "<<my_id;
555     msg = oss.str();
556     mainLogUtil->logInfo(msg);
557    
558     //TODO : work on this ....
559     if(tryMerge){
560     merge_ROOTfiles();
561     }
562     }
563     else
564     {
565     oss<<"Problem storing information in DB regarding file: "<<filename;
566     msg = oss.str();
567     mainLogUtil->logError(msg);
568     }
569    
570     //marco_new_31:metto qui se voglio apriree chiudere Connessione solo quando serve
571     //marco_new_01
572     if(!single_connection){
573     CloseDBConnection();
574     }
575    
576     }//pRun
577     }
578    
579    
580     //save in Table_ROOT_Good or in Table_ROOT_Bad
581     bool PacketUser::saveROOT_DB(char* table_name, char* folder_name, char* file_name,
582     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
583     unsigned long int obt_in, unsigned long int obt_fin,
584     unsigned long int oT_sync, unsigned long int lT_sync_info,
585     unsigned long int mtime_init, unsigned long int mtime_last,
586     unsigned long int mboot_num,
587     unsigned long int time_offset,
588     int bad_pkt, int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved,
589     char* nome_input){
590     //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
591     stringstream oss;
592     oss.str("");
593     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)"
594     << " VALUES ('"<<0<< "','" <<folder_name<<"','" <<file_name<< "','" << pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
595     << oT_sync << "','" << lT_sync_info << "','"
596     << mtime_init << "','" << mtime_last << "','"
597     << mboot_num << "','"
598     << time_offset << "','"
599     << bad_pkt <<"','" << bad_pkt_read <<"','"<< bad_pkt_CalRead <<"','"<< numPKTSaved<<"','"
600     << nome_input <<"',"
601     <<"NULL"<<
602     ")";
603     string msg = oss.str();
604     mainLogUtil->logAll(msg);
605    
606     stringstream oss1;
607     oss1.str()="";
608     string msg1;
609    
610     TSQLResult* res=NULL;
611     res= sqlServer->Query(oss.str().c_str());
612     if(!res)
613     {
614     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
615     msg1=oss1.str();
616     mainLogUtil->logError(msg1);
617     return false;
618     }
619    
620     //marco_new: si pu� ottimizzare ...
621     //the ID of the current ROOT file in table Table_ROOT_Good
622     my_id = select_maxIDN_DB(table_name);
623     // cout<<"DBG: my_id = "<<my_id<<endl;
624     if(res){delete res; res = NULL;}
625     return true;
626     }
627    
628     /**********************************************************************************************/
629     /*###########################################################################################################*/
630     /********************************** MERGING *************************************************/
631     /*###########################################################################################################*/
632     // Merge ROOT files: found other ROOT files in the same temporal ranges
633     // it finds ROOT files that covers particular temporal range of interest and save this info in DB
634     /**********************************************************************************************/
635     //try to merge current ROOT file with files in DB
636     bool PacketUser::merge_ROOTfiles()
637     {
638     mainLogUtil->logInfo("Trying merging ROOT files");
639    
640     //marco_new: controlla
641     double percentage=(double)(bad_pkt+bad_pkt_EventReader+bad_pkt_CalibReader)/numPKTSaved;
642    
643     TSQLResult* res=NULL;
644     TSQLRow* row =NULL;
645    
646     //marco_new: non considero mai caso di continuit�(mio finale=DB iniz o viceversa), non mi interessa
647     //marco_new: after e before contengono anche smaller ma con un bordo comune
648     for(int relaz=AFTER; relaz<=BIGGER; relaz++)
649     {
650     //marco_new: controlla tutto
651     //marco_new: esci se sono arrivato a zero
652     if(real_time_last==real_time_init){
653     mainLogUtil->logInfo("Finish merging ROOT files");
654     if(res){delete res; res = NULL;}
655     if(row){delete row; row = NULL;}
656     return true;
657     }
658    
659     res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT)relaz);
660    
661     unsigned int num_rows=0;
662     if (!res)
663     {
664     mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");
665     return false;
666     }
667    
668     num_rows=res->GetRowCount();
669     // cout<<"MMMMM trovati num_rows "<< num_rows<< " per relaz= "<<relaz;
670    
671     if(num_rows>0)
672     {
673     for(unsigned int i=0; i<num_rows; i++)
674     {
675     row=res->Next();
676     if (!row)
677     {
678     mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");
679     if(res){delete res; res = NULL;}
680     return false;
681     }
682    
683    
684     //TODO ricavare tutte le info che servono
685     unsigned int idN = 0;
686     idN=atoi(row->GetField(0));
687    
688     double perc =0;
689     //marco_new:
690     perc=atof(row->GetField(9));
691     //unsigned long int M_pkt_number_in, M_pkt_number_fin, M_obt_in, M_obt_fin, M_mtime_init, M_mtime_last;
692     if(perc>percentage){
693     mainLogUtil->logInfo("Current ROOT file is better than the one in DB");
694     //modifica DB
695     updateMergeROOT_DB(Table_ROOT_Merging,
696     atoi(row->GetField(1)), atoi(row->GetField(2)),
697     atoi(row->GetField(3)), atoi(row->GetField(4)),
698     atoi(row->GetField(5)), atoi(row->GetField(6)),
699     atoi(row->GetField(7)), atoi(row->GetField(8)),
700     perc,
701     idN,(type_Rel_ROOT)relaz);
702     }
703     else{
704     mainLogUtil->logInfo("Found ROOT files in DB better than current ROOT file");
705    
706     if(relaz == AFTER){
707     mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
708     pkt_number_init= atoi(row->GetField(3));
709     obt_init= atoi(row->GetField(5));
710     real_time_init= atoi(row->GetField(7));
711     }
712     else if(relaz == BEFORE){
713     mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");
714     pkt_number_last= atoi(row->GetField(2));
715     obt_last= atoi(row->GetField(4));
716     real_time_last= atoi(row->GetField(6));
717     }
718     else if(relaz == SMALLER)
719     {
720     //non devo proprio salvarlo ed esco ...
721     mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
722     mainLogUtil->logInfo("Finish merging ROOT files");
723     if(res){delete res; res = NULL;}
724     if(row){delete row; row = NULL;}
725     return true;
726     }
727     else if(relaz == BIGGER)//spezzettamento
728     {
729     mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");
730     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
731     pkt_number_init, atoi(row->GetField(2)),
732     obt_init, atoi(row->GetField(4)),
733     real_time_init, atoi(row->GetField(6)),
734     boot_number,
735     percentage);
736    
737     mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");
738     pkt_number_init= atoi(row->GetField(3));
739     obt_init= atoi(row->GetField(5));
740     real_time_init= atoi(row->GetField(7));
741     }
742     }
743     }//for num_rows
744     }
745     else{
746     //marco_new:
747     mainLogUtil->logInfo("Found 0 ROOT files in DB that can be merged with current ROOT file");
748    
749     }
750    
751     //marco_new:???
752     if(res){delete res; res = NULL;}
753     if(row){delete row; row = NULL;}
754     }//for
755    
756     mainLogUtil->logInfo("Saving Current ROOT file");
757     saveMergeROOT_DB(Table_ROOT_Merging, my_id,
758     pkt_number_init, pkt_number_last,
759     obt_init, obt_last,
760     real_time_init, real_time_last,
761     boot_number,
762     percentage);
763    
764    
765     mainLogUtil->logInfo("Finish merging ROOT files");
766     return true;
767     }
768    
769     //unsigned int root_id, percentage non le devo modificare
770     bool PacketUser::updateMergeROOT_DB(char* table_name,
771     unsigned int root_id,
772     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
773     unsigned long int obt_in, unsigned long int obt_fin,
774     unsigned long int mtime_init, unsigned long int mtime_last,
775     unsigned long int mboot_num,
776     double bad_perc,
777     unsigned int ID_record, type_Rel_ROOT type_rel){
778     stringstream oss;
779     oss.str("");
780    
781     if(type_rel == AFTER){
782     mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
783     //marco_new:tolti tutti gli apici che qui non devono essere
784     oss << "UPDATE "<< table_name
785     << " SET PKT_NUMBER_FINAL ="<< pkt_number_init
786     <<", PKT_OBT_FINAL ="<< obt_init
787     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record <<";";
788     }
789     else if(type_rel == BEFORE){
790     mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");
791     oss << "UPDATE "<< table_name
792     << " SET PKT_NUMBER_INIT ="<< pkt_number_last
793     <<", PKT_OBT_INIT ="<< obt_last
794     <<", REAL_TIME_INIT ="<< real_time_last<<" WHERE ID_N ="<< ID_record<<";";
795     }
796     else if(type_rel == SMALLER){
797     //spezzettamentento
798     mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");
799    
800     oss << "UPDATE "<< table_name
801     << " SET PKT_NUMBER_FINAL="<< pkt_number_init
802     <<", PKT_OBT_FINAL ="<< obt_init
803     <<", REAL_TIME_LAST ="<< real_time_init<<" WHERE ID_N ="<< ID_record<<";";
804    
805     mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");
806    
807     saveMergeROOT_DB(table_name, root_id,
808     pkt_number_last, pkt_number_fin,
809     obt_last, obt_fin,
810     real_time_last, mtime_last,
811     mboot_num,
812     bad_perc);
813     }
814     else if(type_rel == BIGGER){
815     //marco_new: ok anche se coincidenti etc
816     oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";";
817     mainLogUtil->logInfo("Rimosso record in Tabella merging");
818     }
819     else
820     return false;
821    
822    
823    
824     string msg = oss.str();
825     mainLogUtil->logInfo(msg);
826    
827     stringstream oss1;
828     oss1.str()="";
829     string msg1;
830    
831     TSQLResult* res=NULL;
832     res= sqlServer->Query(oss.str().c_str());
833     if(!res)
834     {
835     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
836     msg1=oss1.str();
837     mainLogUtil->logError(msg1);
838     return false;
839     }
840     if(res){delete res; res = NULL;}
841     return true;
842     }
843    
844     //marco_new: check here
845     TSQLResult* PacketUser::Select_merging(char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, type_Rel_ROOT type_rel)
846     {
847    
848     // unsigned int idN=0;
849     TSQLResult* res=NULL;
850     stringstream query;
851     query.str("");
852    
853     //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
854     //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
855     //marco_new bordi: smaller resta solo caso in cui DB � piu largo sia a dx che a sx
856     if(type_rel == AFTER)
857     query<<"select * from "<<table_name<<" where REAL_TIME_LAST > "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last <<" and REAL_TIME_INIT < "<< mtime_init;
858     else if(type_rel == BEFORE)
859     query<<"select * from "<<table_name<<" where REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_INIT < "<< mtime_last <<" and REAL_TIME_LAST > "<< mtime_last;
860     else if(type_rel == SMALLER)
861     query<<"select * from "<<table_name<<" where REAL_TIME_INIT < "<< mtime_init <<" and REAL_TIME_LAST > "<< mtime_last;
862     else if(type_rel == BIGGER)
863     query<<"select * from "<<table_name<<" where REAL_TIME_INIT >= "<< mtime_init <<" and REAL_TIME_LAST <= "<< mtime_last;
864     else
865     return 0;
866    
867     query<< " order by REAL_TIME_INIT ASC";//marco_new: inutile
868     // query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
869    
870    
871     string msg = query.str();
872     mainLogUtil->logAll(msg);
873    
874     res= sqlServer->Query(query.str().c_str());
875    
876     stringstream oss1;
877     oss1.str()="";
878     string msg1;
879    
880     if (!res)
881     {
882     oss1<<"DBError UNABLE to: "<<query.str().c_str()<<endl;
883     msg1=oss1.str();
884     mainLogUtil->logError(msg1);
885     }
886    
887     return res;
888     }
889    
890     bool PacketUser::saveMergeROOT_DB(char* table_name, unsigned int root_id,
891     unsigned long int pkt_number_in, unsigned long int pkt_number_fin,
892     unsigned long int obt_in, unsigned long int obt_fin,
893     unsigned long int mtime_init, unsigned long int mtime_last,
894     unsigned long int mboot_num,
895     double percentage)
896     {
897    
898     stringstream oss;
899     oss.str("");
900     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)"
901     << " VALUES ('"<<0<< "','" <<root_id<<"','"<< pkt_number_in <<"','"<< pkt_number_fin << "','"<< obt_in <<"','"<< obt_fin<< "','"
902     << mtime_init << "','" << mtime_last << "','"
903     << mboot_num <<"','"
904     << percentage <<"',"
905     <<"NULL"<<
906     ")";
907    
908     string msg = oss.str();
909     mainLogUtil->logAll(msg);
910    
911     //marco_new: controlla
912     //marco_new: esci se sono arrivato a zero
913     // if(real_time_last==real_time_init){
914     if(mtime_init==mtime_last){
915     mainLogUtil->logAll("Nothing to be saved");
916    
917     // if(res){delete res; res = NULL;}
918     // if(row){delete row; row = NULL;}
919     return true;
920     }
921    
922    
923     stringstream oss1;
924     oss1.str()="";
925     string msg1;
926    
927     TSQLResult* res=NULL;
928     res= sqlServer->Query(oss.str().c_str());
929     if(!res)
930     {
931     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
932     msg1=oss1.str();
933     mainLogUtil->logError(msg1);
934     return false;
935     }
936     if(res){delete res; res = NULL;}
937     return true;
938     }
939    
940     //Utility: return max(IDN)
941     unsigned int PacketUser::select_maxIDN_DB(char* table_name)
942     {
943    
944     if (!strcmp(table_name,Table_ROOT_Bad))
945     {
946     return 0;
947     }
948     unsigned int idN=0;
949     TSQLResult* res=NULL;
950     stringstream oss;
951     oss.str("");
952     oss<<"select max(ID_N) from "<<table_name;
953     string msg = oss.str();
954     mainLogUtil->logAll(msg);
955     res= sqlServer->Query(oss.str().c_str());
956    
957     stringstream oss1;
958     oss1.str()="";
959     string msg1;
960    
961     if(!res)
962     {
963     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
964     msg1=oss1.str();
965     mainLogUtil->logError(msg1);
966     return 0;
967     }
968    
969     TSQLRow* row=NULL;
970     row=res->Next();
971     if (!row )
972     {
973     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
974     msg1=oss1.str();
975     mainLogUtil->logError(msg1);
976     return 0;
977     }
978    
979     idN=atoi(row->GetField(0));
980     if(row){delete row; row = NULL;}
981     if(res){delete res; res = NULL;}
982     return idN;
983     }
984    
985     /**
986     * Lock par=table, if par=null lock all tables
987     */
988     int PacketUser::LockTables(char* tableTobeLocked)
989     {
990     //se non ho il DB :)
991     if(!do_cont_check) return 999;
992    
993     TSQLResult* res=NULL;
994     stringstream oss;
995     oss.str("");
996     if(!tableTobeLocked)
997     oss << "lock table "<<Table_ROOT_Good<<" write, "<<Table_ROOT_Bad<<" write, "<<Table_GL_RESURS_OFFSET<<" write, "<<Table_ROOT_Merging<<" write;";
998     else
999     oss << "lock table "<<tableTobeLocked<<" write; ";
1000    
1001     string msg = oss.str();
1002     mainLogUtil->logAll(msg);
1003    
1004     res= sqlServer->Query(oss.str().c_str());
1005    
1006     stringstream oss1;
1007     oss1.str()="";
1008     string msg1;
1009    
1010     if(!res)
1011     {
1012     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1013     msg1=oss1.str();
1014     mainLogUtil->logError(msg1);
1015     return 1;
1016     }
1017    
1018     if(res){delete res; res = NULL;}
1019     mainLogUtil->logAll("######################### TABLES LOCKED ############################");
1020     return 0;
1021     }
1022    
1023     /**
1024     * UNLock tables
1025     */
1026     int PacketUser::UnLockTables(){
1027     //se non ho il DB :)
1028     if(!do_cont_check) return 999;
1029    
1030     TSQLResult* res=NULL;
1031     stringstream oss;
1032     oss.str("");
1033     oss << "unlock tables;";
1034    
1035     string msg = oss.str();
1036     mainLogUtil->logAll(msg);
1037    
1038     res= sqlServer->Query(oss.str().c_str());
1039    
1040     stringstream oss1;
1041     oss1.str()="";
1042     string msg1;
1043    
1044     if(!res)
1045     {
1046     oss1<<"DBError UNABLE to: "<<oss.str().c_str()<<endl;
1047     msg1=oss1.str();
1048     mainLogUtil->logError(msg1);
1049     return 1;
1050     }
1051    
1052     if(res){delete res; res = NULL;}
1053     mainLogUtil->logAll("######################### TABLES UNLOCKED ############################");
1054    
1055     return 0;
1056     }
1057    
1058     /*********************************************************/
1059     //marco_new_31
1060     void PacketUser::OpenDBConnection(char* tableTobeLocked){
1061     mainLogUtil->logAll("######################### OpenDBConnection ############################");
1062     if(do_cont_check){
1063     //controllo che non sia gi� aperta
1064     if(!sqlServer){
1065     sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
1066     if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl;}
1067    
1068     //marco_new: blocca tutte le tabelle
1069     LockTables(tableTobeLocked);
1070     mainLogUtil->logAll("######################### OpenDBConnection OK ############################");
1071     }
1072     }
1073    
1074     }
1075    
1076     //marco_new_31
1077     void PacketUser::CloseDBConnection(){
1078     mainLogUtil->logAll("######################### CloseDBConnection ############################");
1079     if(do_cont_check){
1080     //controllo che ce ne sia una aperta
1081     if(sqlServer && sqlServer->IsConnected()){
1082     //marco_new: sblocca tutte le tabelle
1083     UnLockTables();
1084     sqlServer->Close();
1085    
1086     //marco_new_01: ??
1087     delete sqlServer;
1088    
1089     sqlServer=NULL;
1090     mainLogUtil->logAll("######################### CloseDBConnection OK ############################");
1091     }
1092     }
1093     }
1094    
1095    
1096    
1097     /***********************************************************************************************************************/
1098     /*#################################################### HIC SUNT LEONES ################################################*/
1099     /***********************************************************************************************************************/
1100     //UNUSED: dbg functions...
1101    
1102     //Return the system time in ms
1103     unsigned long long PacketUser::Record_Time(){
1104     timeval tv;
1105     gettimeofday(&tv,NULL);
1106     unsigned long long timems = (unsigned long long)tv.tv_sec * (unsigned long long)1000 + tv.tv_usec / 1000;
1107     return timems;
1108     }
1109    
1110     //dbg_functions:
1111    
1112     //UNUSED. save a packet in a file
1113     void PacketUser::savePKT_file(char* headerPkt,
1114     char* pamPkt,
1115     long int length,
1116     bool append,
1117     char* nomefile)
1118     {
1119     char fno[80]="";
1120     ofstream fout;
1121     sprintf(fno,"%s/pkt%d_of_%s.pkt", outDir, numPKTSaved, nomefile);
1122    
1123     if(append==true)
1124     fout.open(fno, ios::binary | ios::app);
1125     else
1126     fout.open(fno, ios::binary);
1127    
1128     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1129     fout.write(headerPkt,LENGTH_HEADER_PKT);
1130     fout.write(pamPkt,length);
1131     fout.close();
1132     }
1133    
1134     //UNUSED
1135     void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append)
1136     {
1137     char fno[80]="";
1138     ofstream fout;
1139     if(append==true)
1140     {
1141     sprintf(fno,"%s/packets.pkt", outDir);
1142     fout.open(fno, ios::binary | ios::app);
1143     }
1144     else{
1145     sprintf(fno,"%s/packet%d.pkt", outDir, numPKT );
1146     fout.open(fno, ios::binary);
1147     }
1148    
1149     if (!fout) {cout<<"can not open output file "<<fno<<endl; return;}
1150     fout.write(headerPkt,LENGTH_HEADER_PKT);
1151     fout.write(pamPkt,length);
1152     fout.close();
1153     }
1154    
1155    
1156     }

  ViewVC Help
Powered by ViewVC 1.1.23