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

Annotation of /chewbacca/PamOffLineSW/PacketUser.cpp_29

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.23