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

Contents of /chewbacca/PamOffLineSW/PacketUser.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations) (download)
Thu Dec 24 10:31:12 2009 UTC (15 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.9: +269 -36 lines
New version, packages recovering tools implemented, new DB, bugs fixed

1 //============================================================================
2 // $Id: PacketUser.cpp,v 1.9 2009/10/01 08:55:42 mocchiut Exp $
3 // Description :
4 //============================================================================
5 #include "PacketUser.h"
6 #include <sys/time.h>
7 #include <iostream>
8
9 namespace PamOffLineSW {
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 extern bool candelete; // EM
25 //marco_new_01
26 extern bool single_connection;
27
28 //don't perform check about continuity: it means don't use DB.
29 extern bool do_cont_check;
30
31 //marco_new_31:
32 extern char* db_user;
33 extern char* db_pwd;
34 extern char* connection;
35
36 //per tenere conto reset obt
37 extern unsigned long int max_pkt_obt;
38
39 #define TAGVALUELEN 4
40 extern char tag_value[TAGVALUELEN];
41
42 PacketUser PacketUser::instance;
43
44 int PacketUser::numDiscontinity = 1;
45 int PacketUser::numPKT = 0;
46 int PacketUser::numPKTSaved = 0;
47
48 PacketUser& PacketUser::getInstance() {
49 return instance;
50 }
51
52 PacketUser::PacketUser() {
53 gROOT->SetBatch(kTRUE);
54 numDiscontinity = 1;
55 numPKTSaved = 0;
56 numPKT = 0;
57 pkt_number_init = 0;
58 obt_init = 0;
59 pkt_number_last = 0;
60 obt_last = 0;
61 obt_time_sync = 0;
62 last_time_sync_info = 0;
63
64 //(tassa)
65 obt_time_sync_prevvalue = 0;
66 last_time_sync_info_prevvalue = 0;
67 time_is_estimated = false;
68
69 //EM
70 recoverlimit = 400;
71 idtorecover = new TArrayI(100);
72 arsize = 0;
73
74 real_time_init = 0;
75 real_time_last = 0;
76 bad_pkt = 0;
77 bad_pkt_EventReader = 0;
78 bad_pkt_CalibReader = 0;
79 reader = NULL;
80 pRun = NULL;
81 reader = new pamela::techmodel::EventReader();
82 Table_ROOT_Good = "ROOT_TABLE";
83 Table_ROOT_Bad = "ROOT_TABLE_BAD";
84 Table_GL_RESURS_OFFSET = "GL_RESURS_OFFSET";
85 Table_ROOT_Merging = "ROOT_TABLE_MERGING";
86 // good_pkt_Calib=0;
87 my_id = 0;
88 boot_number = 0;
89 //(tassa)
90 boot_number_prevvalue = 0;
91
92 //(tassa)
93 // id_to_recover[1000]; // Emiliano: che si vuole fare con questa riga? cosi` non fa nulla...
94 id_to_recover_index = 0;
95 }
96
97 PacketUser::~PacketUser() {
98 if (reader) {
99 delete reader;
100 reader = NULL;
101 }
102 numDiscontinity = 1;
103 numPKTSaved = 0;
104 numPKT = 0;
105 pkt_number_init = 0;
106 obt_init = 0;
107 pkt_number_last = 0;
108 obt_last = 0;
109 obt_time_sync = 0;
110 last_time_sync_info = 0;
111 obt_time_sync_prevvalue = 0;
112 last_time_sync_info_prevvalue = 0;
113 time_is_estimated = false;
114
115 real_time_init = 0;
116 real_time_last = 0;
117 bad_pkt = 0;
118 bad_pkt_EventReader = 0;
119 bad_pkt_CalibReader = 0;
120 // good_pkt_Calib=0;
121 my_id = 0;
122 boot_number = 0;
123 boot_number_prevvalue = 0;
124 }
125
126 //Put the packet in a root file. Create a new ROOT file for each group of packets
127 void PacketUser::usePKT(char*& headerPkt, char*& pamPkt, long int length, bool isCons, bool isPKTGood,
128 const PacketType* type, unsigned long int counter, unsigned long int obt) {
129 //If the packet type was not recognised before
130 if (!type) {
131 return;
132 }
133
134 //in order to start ...
135 if (numPKT == 0) {
136 if (single_connection) {
137 OpenDBConnection(NULL);
138 }
139
140 setInit(counter, obt);
141 StartGroup();
142 }
143
144 //here we know if the incoming packet can be considered consecutive to the previuos one
145 if ((!isCons) && (numPKT != 0)) {
146 setReal_Time();
147 //closing the group in file rootfilename
148 FinishGroup(rootfilename);
149
150 //se gia' e' cambiato download non cambio numdisco
151 if (!is_new_route) {
152 numDiscontinity++;
153 }
154
155 //the current pkt will be placed in a new group: rootfilename
156 setInit(counter, obt);
157 StartGroup();
158 }
159
160 //add current packet to the group and calculate bad_pkt_EventReader and bad_pkt_CalibReader
161 int ret = reader->PKT_RunEvent(headerPkt, pamPkt, length, type);
162 /*
163 ret
164 0 packet good
165 1 exception but used //never happen
166 2 CRC exception but used
167 3 CALIBRATION PACKET with error but used
168 -1 CRC exception packet DISCARDED
169 -2 FATAL exception packet DISCARDED //never happen
170 -3 No way to read events of this type. packet DISCARDED( es OLD CalibCal)
171 ret = 10 if the packet is good but comes from a cadre with VRL problems
172 */
173
174 //here we also know if the packet comes from one or more corrupted cadres
175 if ((!isPKTGood) && (!ret)) {
176 ret = 10;
177 }
178 switch (ret) {
179 case 0: {
180 numPKTSaved++;
181 break;
182 }
183 case 1: {
184 bad_pkt_EventReader++;
185 numPKTSaved++;
186 break;
187 }
188 case 2: {
189 bad_pkt_EventReader++;
190 numPKTSaved++;
191 break;
192 }
193 case 3: {
194 bad_pkt_CalibReader++;
195 numPKTSaved++;
196 break;
197 }
198 case 10: {
199 bad_pkt++;
200 numPKTSaved++;
201 break;
202 }
203 default: {
204 stringstream oss;
205 oss.str() = "";
206 oss << "Packet DISCARDED, reason = " << ret;
207 string msg = oss.str();
208 mainLogUtil->logAll(msg);
209 break;
210 }
211 }
212
213 /*
214 if ((ret==0)&&((type==PacketType::CalibTrk1)||(type==PacketType::CalibTrk2)|| (type==PacketType::CalibCalPed)))
215 {
216 good_pkt_Calib++;//maybe I will remove this in future
217 }
218 */
219
220 if (ret >= 0)// only the packet not discarded of course
221 setLast(counter, obt);
222
223 //TODO: decide what kind of packet I can use here
224 // if((ret==0)||(ret==10))//I want to use only good packet
225 if (ret >= 0)//I want to use only accepted packet
226 {
227 setTimeSync(pamPkt, length, type);//here I retrieve also the boot_number if packet type is vardump
228 }
229
230 //just to be sure ...
231 if (pamPkt) {
232 delete[] pamPkt;
233 pamPkt = NULL;
234 }
235 if (headerPkt) {
236 delete[] headerPkt;
237 headerPkt = NULL;
238 }
239
240 numPKT++;
241 }
242
243 //set the value of the counter and obt of the first packet of the group
244 void PacketUser::setInit(unsigned long int counter, unsigned long int obt) {
245 sprintf(nnnn_mmm_ppp, "%05d_%03d_%03d", orbit_number, mmm_number, download);
246 sprintf(rootfilename, "%s_%s_%d", nome_output, nnnn_mmm_ppp, numDiscontinity);
247 pkt_number_init = counter;
248 obt_init = obt;
249 pkt_number_last = 0;
250 obt_last = 0;
251 real_time_init = 0;
252 real_time_last = 0;
253 bad_pkt = 0;
254 numPKTSaved = 0;
255 bad_pkt_EventReader = 0;
256 bad_pkt_CalibReader = 0;
257 //good_pkt_Calib=0;
258
259 //marco_NB:
260 //ogni volta che trovo una discontinuita` resetto come se mi trovassi in un nuovo download oppure uso i vecchi valori se esistono?
261
262 if ( obt_time_sync ) obt_time_sync_prevvalue = obt_time_sync; // save the last NOT NULL obt_time_sync! EM
263 if ( last_time_sync_info ) last_time_sync_info_prevvalue = last_time_sync_info; // save the last NOT NULL last_time_sync
264 time_is_estimated = false;
265 obt_time_sync = 0;
266 last_time_sync_info = 0;
267 boot_number_prevvalue = boot_number;
268 boot_number = 0;
269
270 // NB: se ho discontinuita in genere non so a che download appartiene potrebbe proprio essere sbagliato tenere i vecchi valori
271
272 }
273
274 //starts a new root file
275 void PacketUser::StartGroup() {
276 pRun = new PamelaRun(rootfilename, gSystem->ExpandPathName(outDir), multiFile, compression); // EMI
277 strcat(rootfilename, ".root");
278 reader->Init(pRun);
279 stringstream oss;
280 oss.str() = "";
281 oss << "######################### Start a new group in file: " << rootfilename << " ############################";
282 string msg = oss.str();
283 mainLogUtil->logInfo(msg);
284 }
285
286 //set the value of the counter and obt of the last packet of the group
287 void PacketUser::setLast(unsigned long int counter, unsigned long int obt) {
288 pkt_number_last = counter;
289 obt_last = obt;
290 }
291
292 //retrieve obt_time_sync and last_time_sync_info from packet if type has special values and BOOT_NUMBER
293 void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type) {
294 //do nothing
295 if (!do_cont_check) {
296 if (is_new_route)
297 is_new_route = false;
298 return;
299 }
300
301 //TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0
302 //In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi
303 // devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download
304 //When I found a new download I need to reset all values
305 if (is_new_route) {
306 //reset
307 is_new_route = false;
308 //(tassa) ci ho ripensato se stiamo in un secondo download non prendiamo i bootnumber e timesync del precedente!
309 obt_time_sync_prevvalue = 0;//obt_time_sync;
310 last_time_sync_info_prevvalue = 0;//last_time_sync_info;
311
312 obt_time_sync = 0;
313 last_time_sync_info = 0;
314 boot_number_prevvalue = 0;//boot_number;
315 boot_number = 0;
316 time_is_estimated = false;
317 if (time_Offset)
318 timeOffset = time_Offset;
319 else
320 timeOffset = retrieveTimeOffset(Table_GL_RESURS_OFFSET);
321 }
322
323 //retrieve boot number if possible
324 setBootNumber(packet, pktLength, type);
325
326 //Retrieve if possible obt_time_sync and last_time_sync_info
327 int offset = 0;
328 if (type == PacketType::RunHeader) {
329 offset = 0;
330 }
331 else if (type == PacketType::RunTrailer) {
332 offset = 3;
333 }
334 else if (type == PacketType::Mcmd) {
335 // mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere.");
336 return;
337 }
338 else {
339 return;
340 }
341
342 //I don't need to retrieve them again if they are good
343 // if ((obt_time_sync) || (last_time_sync_info))
344 // return;
345 // no, do it, since there is a (small indeed) time drift due to different clock of PAMELA CPU and RESURS CPU...
346
347 obt_time_sync = (((UINT32) packet[5 + offset] << 24) & 0xFF000000) + (((UINT32) packet[6 + offset] << 16)
348 & 0x00FF0000) + (((UINT32) packet[7 + offset] << 8) & 0x0000FF00) + (((UINT32) packet[8 + offset]) & 0x000000FF);
349 last_time_sync_info = (((UINT32) packet[9 + offset] << 24) & 0xFF000000) + (((UINT32) packet[10 + offset] << 16)
350 & 0x00FF0000) + (((UINT32) packet[11 + offset] << 8) & 0x0000FF00)
351 + (((UINT32) packet[12 + offset]) & 0x000000FF);
352
353 stringstream oss;
354 oss.str() = "";
355 oss << "In download: " << download << " timeOffset: " << timeOffset << " obt_time_sync: " << obt_time_sync
356 << " last_time_sync_info: " << last_time_sync_info << " using Packet Type: " << type->GetName();
357 // 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();
358 string msg = oss.str();
359 mainLogUtil->logAll(msg);
360 }
361
362 //Boot Number
363 void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type) {
364 //se gia calcolato esco: lo calcolo una volta per download e per continuita'
365 if (boot_number)
366 return;
367
368 if (type == PacketType::VarDump) {
369 int b_offset = 34;//4+5*6
370 boot_number = (((UINT32) packet[1 + b_offset] << 24) & 0xFF000000) + (((UINT32) packet[2 + b_offset] << 16)
371 & 0x00FF0000) + (((UINT32) packet[3 + b_offset] << 8) & 0x0000FF00) + (((UINT32) packet[4 + b_offset])
372 & 0x000000FF);
373
374 /*
375 long int dataLength = pktLength - 2; //the block of data
376 int b_offset = 4;
377 while (b_offset < dataLength){
378 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);
379 b_offset = b_offset + 5;
380 }
381 */
382 stringstream oss;
383 oss.str() = "";
384 // oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str();
385 oss << "In download: " << download << " boot_number: " << boot_number << " using Packet Type: " << type->GetName();
386 string msg = oss.str();
387 mainLogUtil->logAll(msg);
388 return;
389 }
390 else {
391 return;
392 }
393
394 }
395 //set the real time of the first packet and the last packet oif a group of packet if possible
396 void PacketUser::setReal_Time() {
397
398 if (!obt_time_sync && !last_time_sync_info && numPKTSaved > recoverlimit) {
399 time_is_estimated = true;
400 obt_time_sync = obt_time_sync_prevvalue;
401 last_time_sync_info = last_time_sync_info_prevvalue;
402 }
403 if (obt_time_sync || last_time_sync_info) {
404 real_time_init = (obt_init / 1000 - obt_time_sync) + last_time_sync_info;
405 //se obt si e' resettato
406 if (obt_last < obt_init) {
407 real_time_last = (max_pkt_obt / 1000 + obt_last / 1000 - obt_time_sync) + last_time_sync_info;
408 stringstream oss;
409 oss.str() = "";
410 oss << "obt_last(" << obt_last << ") < obt_init(" << obt_init
411 << "). It is due to Pamela Reset. Adding max_pkt_obt(" << max_pkt_obt
412 << ") to obt_last in order to compute real_time_last";
413 string msg = oss.str();
414 mainLogUtil->logWarning(msg);
415 }
416 else
417 real_time_last = (obt_last / 1000 - obt_time_sync) + last_time_sync_info;
418 real_time_init += timeOffset;
419 real_time_last += timeOffset;
420 }
421 }
422
423 void PacketUser::setReal_TimeRECOVERY(
424 unsigned long int & _real_time_init,
425 unsigned long int & _real_time_last,
426 unsigned long int _obt_init,
427 unsigned long int _obt_last
428 ) {
429
430 if (obt_time_sync || last_time_sync_info) {
431 _real_time_init = (_obt_init / 1000 - obt_time_sync) + last_time_sync_info;
432 //se obt si e' resettato
433 if (_obt_last < _obt_init) {
434 _real_time_last = (max_pkt_obt / 1000 + _obt_last / 1000 - obt_time_sync) + last_time_sync_info;
435 stringstream oss;
436 oss.str() = "";
437 oss << "obt_last(" << obt_last << ") < obt_init(" << obt_init
438 << "). It is due to Pamela Reset. Adding max_pkt_obt(" << max_pkt_obt
439 << ") to obt_last in order to compute real_time_last";
440 string msg = oss.str();
441 mainLogUtil->logWarning(msg);
442 }
443 else
444 _real_time_last = (_obt_last / 1000 - obt_time_sync) + last_time_sync_info;
445 _real_time_init += timeOffset;
446 _real_time_last += timeOffset;
447 }
448 }
449
450
451 //timeOffset
452 //retrieve from table Table_GL_RESURS_OFFSET timeOffset
453 unsigned long int PacketUser::retrieveTimeOffset(const char * table) {
454 if (!do_cont_check)
455 return 0;
456
457 //if(!table){table=Table_GL_RESURS_OFFSET}
458
459 if (!single_connection) {
460 OpenDBConnection(table);//Nota qui ho deciso di lockare solo questa tabella e non tutte
461 }
462
463 UInt_t t0 = 0;//toffset
464 stringstream oss;
465 oss.str("");
466 oss
467 << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "
468 << table << " WHERE SPECIAL_FILE='" << nnnn_mmm_ppp << "';";
469
470 string msg = oss.str();
471 mainLogUtil->logAll(msg);
472
473 TSQLResult* res = NULL;
474 TSQLRow* row = NULL;
475 res = sqlServer->Query(oss.str().c_str());
476 if (!res) {
477 mainLogUtil->logError("DBError retrieveTimeOffset 1");
478 return 0;
479 }
480
481 row = res->Next();
482 if (!row) {
483 if (res) {
484 delete res;
485 res = NULL;
486 }
487
488 oss.str("");
489 oss
490 << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM "
491 << table << " WHERE FROM_ORBIT < " << orbit_number << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;";
492
493 string msg2 = oss.str();
494 mainLogUtil->logAll(msg2);
495
496 res = sqlServer->Query(oss.str().c_str());
497 if (!res) {
498 mainLogUtil->logError("DBError retrieveTimeOffset 2");
499 return 0;
500 }
501 row = res->Next();
502 if (!row) {
503 mainLogUtil->logError("DBError retrieveTimeOffset 3");
504 return 0;
505 }
506
507 }
508
509 TTimeStamp tu = TTimeStamp((UInt_t) atoi(row->GetField(0)), (UInt_t) atoi(row->GetField(1)), (UInt_t) atoi(
510 row->GetField(2)), (UInt_t) atoi(row->GetField(3)), (UInt_t) atoi(row->GetField(4)), (UInt_t) atoi(row->GetField(
511 5)), 0, true, 0);
512 t0 = (UInt_t) tu.GetSec();
513
514 if (res) {
515 delete res;
516 res = NULL;
517 }
518 if (row) {
519 delete row;
520 row = NULL;
521 }
522
523 if (!single_connection) {
524 CloseDBConnection();
525 }
526 return t0;
527 }
528
529 //this is public and can be called from the main at the end of the game
530 void PacketUser::FinishLastGroup() {
531 mainLogUtil->logAll("######################### Closing the last group ############################");
532 setReal_Time();
533 FinishGroup(rootfilename);
534 mainLogUtil->logAll("######################### Closed the last group ############################");
535 //
536 if (single_connection) {
537 CloseDBConnection();
538 }
539 }
540
541 //Finish the old root file
542 void PacketUser::FinishGroup(char * filename) {
543 if (pRun) {
544 if ( !( !real_time_init && !real_time_last && numPKTSaved <= recoverlimit ) && candelete ) pRun->WriteFiles();
545 delete pRun;
546 pRun = NULL;
547 stringstream close;
548 close.str() = "";
549 string msg;
550
551 close << "TAG XX ######################### Close group in file: " << rootfilename << " ############################";
552 msg = close.str();
553 mainLogUtil->logInfo(msg);
554
555 // se non faccio proprio controlli su continuita' neanche salvo su DB
556 if (!do_cont_check)
557 return;
558
559 stringstream oss;
560 oss.str() = "";
561
562 //TODO: check when I don't want to log in DB informations
563 // se non ho salvato nessun pacchetto
564 if (numPKTSaved == 0) {
565 stringstream err;
566 err.str() = "";
567 err << "NO PACKET SAVED in file: " << rootfilename << " The group is empty: you can remove this file.";
568 msg = err.str();
569 mainLogUtil->logError(msg);
570 // return;
571 }
572
573 //se non ho salvato nessun pacchetto buono
574 if ((numPKTSaved - bad_pkt_EventReader - bad_pkt_CalibReader - bad_pkt) == 0) {
575 stringstream err;
576 err.str() = "";
577 err << "NO GOOD PACKET SAVED in file: " << rootfilename << " The group is BAD: you may want to remove this file.";
578 msg = err.str();
579 mainLogUtil->logInfo(msg);
580 // mainLogUtil->logError(msg);
581 // return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD???
582 }
583
584 if (!single_connection) {
585 OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle
586 }
587
588
589 //don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range
590 if ((!real_time_init) && (!real_time_last)) {
591 //saved in another table for future study
592 if (saveROOT_DB(
593 Table_ROOT_Bad,
594 outDir,// here outDir is good, no expand EMI
595 filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info,
596 real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader,
597 numPKTSaved, fni, time_is_estimated) == true) {
598 oss.str() = "";
599 oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Bad;
600 msg = oss.str();
601 mainLogUtil->logInfo(msg);
602 }
603 else {
604 oss.str() = "";
605 oss << "Problem storing information in DB regarding file: " << filename;
606 msg = oss.str();
607 mainLogUtil->logError(msg);
608 }
609
610
611
612 if ( numPKTSaved > recoverlimit){ // EM
613 //
614 if ( obt_time_sync && last_time_sync_info ){
615 stringstream err;
616 err.str() = "";
617 err << "(EM): -- RECOVERING WHEN NUMPACKET > "<< recoverlimit <<"!! -- file=<" << filename << ">. We have a previous time sync, immediate recovery!\n";
618 msg = err.str();
619 mainLogUtil->logInfo(msg);
620 //
621 setReal_TimeRECOVERY(
622 real_time_init,
623 real_time_last,
624 obt_init,
625 obt_last
626 );
627 //
628 } else {
629 stringstream err;
630 err.str() = "";
631 err << "TAG(tassa): -- RECOVERING WHEN NUMPACKET > "<< recoverlimit <<"!! -- file=<" << filename << ">. Saving in a queue to try alfer to recover it!\n";
632 msg = err.str();
633 mainLogUtil->logInfo(msg);
634
635 TString qu = Form("select ID_N from ROOT_TABLE_BAD order by INSERT_TIME desc limit 1;");
636 TSQLResult *result = sqlServer->Query(qu.Data());
637 TSQLRow *row = result->Next();
638 Int_t eid = -1;
639 if ( row ) eid = (Int_t)atoi(row->GetField(0));
640 idtorecover->AddAt(eid,arsize);
641 arsize++;
642 if ( arsize > 100 ) idtorecover->Set(arsize);
643 // dbinfo *app = new dbinfo( outDir,
644 // filename, pkt_number_init, pkt_number_last, obt_init, obt_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader,
645 // numPKTSaved, fni, time_is_estimated);
646 // m_dbinfos.push_back(app);
647 }
648
649 CleanDisk();
650 if (!single_connection) {
651 CloseDBConnection();
652 }
653 return;
654
655 };
656 }
657
658 if ( real_time_init || real_time_last) {
659 //saves info in DB in table Table_ROOT
660 if (boot_number == 0)
661 boot_number = boot_number_prevvalue;
662
663 if (saveROOT_DB(
664 Table_ROOT_Good,
665 outDir, // here outDir is good, no expand EMI
666 filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info,
667 real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader,
668 numPKTSaved, fni, time_is_estimated) == true) {
669 oss.str() = "";
670 oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Good << " id= " << my_id;
671 msg = oss.str();
672 mainLogUtil->logInfo(msg);
673
674 if (tryMerge) {
675 merge_ROOTfiles();
676 }
677 } else {
678 oss.str() = "";
679 oss << "Problem storing information in DB regarding file: " << filename;
680 msg = oss.str();
681 mainLogUtil->logError(msg);
682 }
683 };
684
685 // if dbinfo is not empty than
686 if ( obt_time_sync && last_time_sync_info && arsize > 0 ){
687
688
689 // if(!m_dbinfos.empty()){
690 // while(!m_dbinfos.empty()){
691 // dbinfo *app = m_dbinfos.back();
692 // m_dbinfos.pop_back();
693 Int_t ez=0;
694 while ( ez < arsize ){
695 Int_t eid = idtorecover->At(ez);
696 if ( eid > -1 ){
697
698 TString qu = Form("select FOLDER_NAME,FILE_NAME,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,PKT_OBT_INIT,PKT_OBT_FINAL,BOOT_NUMBER,TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,TIME_IS_ESTIMATED from ROOT_TABLE_BAD where ID_N=%i;",eid);
699 TSQLResult *result = sqlServer->Query(qu.Data());
700 TSQLRow *row = result->Next();
701
702 TString efolder_name="";
703 TString efile_name="";
704 unsigned long int epkt_number_in=0ULL;
705 unsigned long int epkt_number_fin=0ULL;
706 unsigned long int eobt_in=0ULL;
707 unsigned long int eobt_fin=0ULL;
708 unsigned long int emboot_num=0ULL;
709 unsigned long int etime_offset=0ULL;
710 int ebad_pkt=0;
711 int ebad_pkt_read=0;
712 int ebad_pkt_CalRead=0;
713 int enum_PKT_Saved=0;
714 TString enome_input="";
715 bool e_time_is_estimated=false;
716
717 if ( row ){
718 efolder_name = (TString)row->GetField(0);
719 efile_name = (TString)row->GetField(1);
720 epkt_number_in = (unsigned long int)atoll(row->GetField(2));
721 epkt_number_fin = (unsigned long int)atoll(row->GetField(3));
722 eobt_in = (unsigned long int)atoll(row->GetField(4));
723 eobt_fin = (unsigned long int)atoll(row->GetField(5));
724 emboot_num = (unsigned long int)atoll(row->GetField(6));
725 etime_offset = (unsigned long int)atoll(row->GetField(7));
726 ebad_pkt = (int)atoi(row->GetField(8));
727 ebad_pkt_read = (int)atoi(row->GetField(9));
728 ebad_pkt_CalRead = (int)atoi(row->GetField(10));
729 enum_PKT_Saved = (int)atoi(row->GetField(11));
730 enome_input = (TString)row->GetField(12);
731 e_time_is_estimated = (bool)atoi(row->GetField(13));
732 };
733 delete result;
734
735 setReal_TimeRECOVERY(
736 real_time_init,
737 real_time_last,
738 eobt_in,
739 eobt_fin
740 );
741 //
742
743 if (saveROOT_DB(
744 Table_ROOT_Good,
745 efolder_name.Data(),
746 efile_name.Data(),
747 epkt_number_in,
748 epkt_number_fin, eobt_in, eobt_fin, obt_time_sync, last_time_sync_info,
749 real_time_init, real_time_last, emboot_num, etime_offset, ebad_pkt, ebad_pkt_read, ebad_pkt_CalRead,
750 enum_PKT_Saved, enome_input.Data(), e_time_is_estimated) == true) {
751 oss.str() = "";
752 oss << "\nTAG(tassa): RECOVERED!!!! Saved information regarding file: " << efile_name << " in table " << Table_ROOT_Good << " id= " << my_id;
753 msg = oss.str();
754 mainLogUtil->logInfo(msg);
755 /*merging we use global variable. we haveto set with current value*/
756
757 strcpy(outDir,efolder_name.Data() );
758 // outDir=efolder_name.Data();
759 strcpy(filename,efile_name.Data() );
760 // filename=efile_name.Data();
761 pkt_number_init=epkt_number_in;
762 pkt_number_last=epkt_number_fin;
763 obt_init=eobt_in;
764 obt_last=eobt_fin;
765 boot_number=emboot_num;
766 timeOffset=etime_offset;
767 bad_pkt=ebad_pkt;
768 bad_pkt_EventReader=ebad_pkt_read;
769 bad_pkt_CalibReader=ebad_pkt_CalRead;
770 numPKTSaved=enum_PKT_Saved;
771 strcpy(fni,enome_input.Data() );
772 // fni=enome_input.Data();
773 time_is_estimated=e_time_is_estimated;
774
775 if (tryMerge) {
776 merge_ROOTfiles();
777 }
778
779 TString qu2 = Form("update ROOT_TABLE_BAD set ON_DISK='MVD' where ID_N=%i;",eid);
780 sqlServer->Query(qu2.Data());
781
782
783 }
784 else {
785 oss.str() = "";
786 oss << "Problem storing information in DB regarding file: " << filename;
787 msg = oss.str();
788 mainLogUtil->logError(msg);
789 }
790
791
792 };
793 ez++;
794 }
795 //
796 idtorecover->Reset();
797 delete idtorecover;
798 idtorecover = new TArrayI(100);
799 arsize = 0;
800 // delete app;
801 }
802
803 CleanDisk();
804 if (!single_connection) {
805 CloseDBConnection();
806 }
807
808 }//pRun
809 }
810
811
812 void PacketUser::CleanDisk(){
813 if ( !candelete ) return;
814 //
815 // delete small files present in root_table_bad from disk:
816 //
817 TString qu = Form("select FOLDER_NAME,FILE_NAME from ROOT_TABLE_BAD where NUM_PKT_SAVED<=%i and INSERTED_BY='%s' and ON_DISK='YES';",recoverlimit,tag_value);
818 TSQLResult *result = sqlServer->Query(qu.Data());
819 TSQLRow *row = result->Next();
820 while ( row ){
821 TString tbdel = (TString)gSystem->ExpandPathName(row->GetField(0))+"/"+ (TString)row->GetField(1);
822 stringstream err;
823 string msg;
824 err.str() = "";
825 err << " Deleting file " << tbdel.Data() << " from disk! ";
826 msg = err.str();
827 mainLogUtil->logInfo(msg);
828 gSystem->Unlink(tbdel.Data());
829 // printf("ciccio %s \n",tbdel.Data());
830 TString q2u = Form("UPDATE ROOT_TABLE_BAD set ON_DISK='NO' where FOLDER_NAME='%s' and FILE_NAME='%s' and NUM_PKT_SAVED<=%i and INSERTED_BY='%s';",row->GetField(0),row->GetField(1),recoverlimit,tag_value);
831 // printf("pluto %s \n",q2u.Data());
832 sqlServer->Query(q2u.Data());
833 // printf("paperoga \n");
834 row=result->Next();
835 //
836 };
837
838 }
839
840 //save in Table_ROOT_Good or in Table_ROOT_Bad
841 bool PacketUser::saveROOT_DB(const char* table_name, const char* folder_name, const char* file_name,
842 unsigned long int pkt_number_in, unsigned long int pkt_number_fin, unsigned long int obt_in,
843 unsigned long int obt_fin, unsigned long int oT_sync, unsigned long int lT_sync_info, unsigned long int mtime_init,
844 unsigned long int mtime_last, unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt,
845 int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, const char* nome_input, bool _time_is_estimated) {
846 //TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false?
847 stringstream oss;
848 oss.str("");
849 oss << "INSERT INTO " << table_name
850 << " (ID_N, FOLDER_NAME, FILE_NAME, PKT_NUMBER_INIT, PKT_NUMBER_FINAL, PKT_OBT_INIT, PKT_OBT_FINAL, OBT_TIME_SYNC, LAST_TIME_SYNC_INFO, REAL_TIME_INIT, REAL_TIME_LAST, BOOT_NUMBER, TIME_OFFSET,BAD_PKT,BAD_PKT_READ,BAD_PKT_CALREAD,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME,TIME_IS_ESTIMATED,INSERTED_BY)"
851 << " VALUES ('" << 0 << "','" << folder_name << "','" << file_name << "','" << pkt_number_in << "','"
852 << pkt_number_fin << "','" << obt_in << "','" << obt_fin << "','" << oT_sync << "','" << lT_sync_info << "','"
853 << mtime_init << "','" << mtime_last << "','" << mboot_num << "','" << time_offset << "','" << bad_pkt << "','"
854 << bad_pkt_read << "','" << bad_pkt_CalRead << "','" << num_PKT_Saved << "','" << nome_input << "'," << "NULL,"
855 << _time_is_estimated << ",'" << tag_value << "'" << ");";
856 string msg = oss.str();
857 mainLogUtil->logAll(msg);
858
859 stringstream oss1;
860 oss1.str() = "";
861 string msg1;
862 string query;
863
864 TSQLResult* res = NULL;
865
866 query = oss.str();
867 msg1 = "SaveROOT_DB query: ";
868 msg1 += query;
869 mainLogUtil->logInfo(msg1);
870
871 res = sqlServer->Query(query.c_str());
872 if (!res) {
873 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
874 msg1 = oss1.str();
875 mainLogUtil->logError(msg1);
876 return false;
877 }
878
879
880 if(!strcmp(table_name,Table_ROOT_Good)) {
881 //the ID of the current ROOT file in table Table_ROOT_Good
882 my_id = select_maxIDN_DB(table_name);
883 if (!boot_number) {
884 if (id_to_recover_index < 1000)
885 id_to_recover[id_to_recover_index++] = my_id;
886 }
887 else if (boot_number && id_to_recover_index && !is_new_route) {
888 recover_boot_number();
889 }
890 }
891 // cout<<"DBG: my_id = "<<my_id<<endl;
892 if (res) {
893 delete res;
894 res = NULL;
895 }
896 return true;
897 }
898
899 void PacketUser::recover_boot_number() {
900 string msg1;
901 stringstream oss1;
902 stringstream oss;
903 oss.str("");
904 string query = "";
905 TSQLResult* res = NULL;
906 if (!boot_number || !id_to_recover_index)
907 return;
908
909 for (int i = 0; i < id_to_recover_index; i++) {
910 oss.str("");
911 oss << "UPDATE " << Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i]
912 << ";";
913 query = oss.str();
914 res = sqlServer->Query(query.c_str());
915 if (!res) {
916 oss1 << "DBError UNABLE to: " << query.c_str() << endl;
917 msg1 = oss1.str();
918 mainLogUtil->logError(msg1);
919 return;
920 }
921 }
922
923 for (int i = 0; i < id_to_recover_index; i++) {
924 oss.str("");
925 oss << "UPDATE " << Table_ROOT_Merging << " SET BOOT_NUMBER=" << boot_number << " WHERE ROOT_ID_N="
926 << id_to_recover[i] << ";";
927 query = oss.str();
928 res = sqlServer->Query(query.c_str());
929 if (!res) {
930 oss1 << "DBError UNABLE to: " << query.c_str() << endl;
931 msg1 = oss1.str();
932 mainLogUtil->logError(msg1);
933 return;
934 }
935 }
936
937 id_to_recover_index = 0;
938 }
939
940 /**********************************************************************************************/
941 /*###########################################################################################################*/
942 /********************************** MERGING *************************************************/
943 /*###########################################################################################################*/
944 // Merge ROT files: find other ROOT files in the same temporal ranges
945 // it finds ROOT files that covers particular temporal range of interest and save this info in DB
946 /**********************************************************************************************/
947 //try to merge current ROOT file with files in DB
948 bool PacketUser::merge_ROOTfiles() {
949 stringstream log;
950 log.str("");
951 string slog;
952
953 mainLogUtil->logInfo("Trying merging ROOT files");
954 double percentage = (double) (bad_pkt + bad_pkt_EventReader + bad_pkt_CalibReader) / numPKTSaved;
955
956 TSQLResult* res = NULL;
957 TSQLRow* row = NULL;
958 //numero di ROOT files trovati sul DB che hanno relaz di tipo dato
959 unsigned int num_rows = 0;
960
961 //Record su DB:
962 unsigned int idN = 0;
963 unsigned int root_id_db = 0;
964 unsigned long int pkt_num_in_db = 0;
965 unsigned long int pkt_num_fin_db = 0;
966 unsigned long int pkt_obt_in_db = 0;
967 unsigned long int pkt_obt_fin_db = 0;
968 unsigned long int time_in_db = 0;
969 unsigned long int time_fin_db = 0;
970 unsigned int boot_num_db = 0;
971 double perc = 0;
972
973 //marco_new: non considero mai caso di continuita'(mio finale=DB iniz o viceversa), non mi interessa
974 //marco_new: after e before contengono anche smaller ma con un bordo comune
975 for (int relaz = AFTER; relaz <= BIGGER; relaz++) {
976 //marco_new: esci se sono arrivato a zero
977 if (real_time_last == real_time_init) {
978 log.str("");
979 log << "Current File Lenght=0; real_time_last=real_time_init= " << real_time_last;
980 slog = log.str();
981 mainLogUtil->logAll(slog);
982 mainLogUtil->logInfo("Finish merging ROOT files");
983 if (res) {
984 delete res;
985 res = NULL;
986 }
987 if (row) {
988 delete row;
989 row = NULL;
990 }
991 return true;
992 }
993
994 num_rows = 0;
995 res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT) relaz);
996
997 if (!res) {
998 mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null");
999 return false;
1000 }
1001
1002 num_rows = res->GetRowCount();
1003 log.str("");
1004 log << "Found " << num_rows << " ROOT file in DB with relation of kind = " << relaz;
1005 slog = log.str();
1006 mainLogUtil->logAll(slog);
1007
1008 if (num_rows > 0) {
1009 for (unsigned int i = 0; i < num_rows; i++) {
1010 row = res->Next();
1011 if (!row) {
1012 mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null");
1013 if (res) {
1014 delete res;
1015 res = NULL;
1016 }
1017 return false;
1018 }
1019
1020 //Leggi Record dal DB:
1021 idN = atoll(row->GetField(0));
1022 root_id_db = atoll(row->GetField(1));
1023 pkt_num_in_db = atoll(row->GetField(2));
1024 pkt_num_fin_db = atoll(row->GetField(3));
1025 pkt_obt_in_db = atoll(row->GetField(4));
1026 pkt_obt_fin_db = atoll(row->GetField(5));
1027 time_in_db = atoll(row->GetField(6));
1028 time_fin_db = atoll(row->GetField(7));
1029 boot_num_db = atoll(row->GetField(8));
1030 perc = atof(row->GetField(9));
1031
1032 log.str("");
1033 log << "*** ROOT file found in DB has idN= " << idN << " root_id_db= " << root_id_db;
1034 log << " pkt_num_in_db= " << pkt_num_in_db << " pkt_num_fin_db= " << pkt_num_fin_db << " pkt_obt_in_db= "
1035 << pkt_obt_in_db << " pkt_obt_fin_db= " << pkt_obt_fin_db;
1036 log << " time_in_db= " << time_in_db << " time_fin_db= " << time_fin_db << " boot_num_db= " << boot_num_db
1037 << " perc= " << perc << " ***";
1038 log << "\n*** Current ROOT file has pkt_number_init= " << pkt_number_init << " obt_init= " << obt_init
1039 << " real_time_init= " << real_time_init;
1040 log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= "
1041 << real_time_last << " ***";
1042 slog = log.str();
1043 mainLogUtil->logAll(slog);
1044 if (perc > percentage) {
1045 mainLogUtil->logInfo("Current ROOT file is better than the one in DB");
1046 //modifica DB
1047 updateMergeROOT_DB(Table_ROOT_Merging, root_id_db, pkt_num_in_db, pkt_num_fin_db, pkt_obt_in_db,
1048 pkt_obt_fin_db, time_in_db, time_fin_db, boot_num_db, perc, idN, (type_Rel_ROOT) relaz);
1049 }
1050 else {
1051 mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file");
1052 if (relaz == AFTER) {
1053 mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file");
1054 pkt_number_init = (pkt_num_fin_db + 1);
1055 obt_init = pkt_obt_fin_db;
1056 real_time_init = time_fin_db;
1057 log.str("");
1058 log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= "
1059 << obt_init << " real_time_init= " << real_time_init;
1060 log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= "
1061 << real_time_last << " ***";
1062 slog = log.str();
1063 mainLogUtil->logAll(slog);
1064 }
1065 else if (relaz == BEFORE) {
1066 mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file");
1067 pkt_number_last = (pkt_num_in_db - 1);
1068 obt_last = pkt_obt_in_db;
1069 real_time_last = time_in_db;
1070
1071 log.str("");
1072 log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= "
1073 << obt_init << " real_time_init= " << real_time_init;
1074 log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= "
1075 << real_time_last << " ***";
1076 slog = log.str();
1077 mainLogUtil->logAll(slog);
1078 }
1079 else if (relaz == SMALLER) {
1080 //non devo proprio salvarlo ed esco ...
1081 mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save");
1082 mainLogUtil->logInfo("\nFinish merging ROOT files");
1083 if (res) {
1084 delete res;
1085 res = NULL;
1086 }
1087 if (row) {
1088 delete row;
1089 row = NULL;
1090 }
1091 return true;
1092 }
1093 else if (relaz == BIGGER)//spezzettamento
1094 {
1095 mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file");
1096 saveMergeROOT_DB(Table_ROOT_Merging, my_id, pkt_number_init, (pkt_num_in_db - 1), obt_init, pkt_obt_in_db,
1097 real_time_init, time_in_db, boot_number, percentage);
1098
1099 mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file");
1100 pkt_number_init = (pkt_num_fin_db + 1);
1101 obt_init = pkt_obt_fin_db;
1102 real_time_init = time_fin_db;
1103
1104 log.str("");
1105 log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= "
1106 << obt_init << " real_time_init= " << real_time_init;
1107 log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= "
1108 << real_time_last << " ***";
1109 slog = log.str();
1110 mainLogUtil->logAll(slog);
1111 }//if relaz
1112 }//if perc
1113 }//for num_rows
1114 }//if num_rows
1115
1116 if (res) {
1117 delete res;
1118 res = NULL;
1119 }
1120 if (row) {
1121 delete row;
1122 row = NULL;
1123 }
1124
1125 }//for relaz
1126
1127 mainLogUtil->logInfo("Saving Current ROOT file");
1128 saveMergeROOT_DB(Table_ROOT_Merging, my_id, pkt_number_init, pkt_number_last, obt_init, obt_last, real_time_init,
1129 real_time_last, boot_number, percentage);
1130
1131 mainLogUtil->logInfo("Finish merging ROOT files");
1132 return true;
1133
1134 }
1135
1136 //unsigned int root_id, percentage non le devo modificare
1137 bool PacketUser::updateMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in,
1138 unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin,
1139 unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double bad_perc,
1140 unsigned int ID_record, type_Rel_ROOT type_rel) {
1141
1142 stringstream oss;
1143 oss.str("");
1144
1145 if (type_rel == AFTER) {
1146 mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file");
1147 //marco_new:tolti tutti gli apici che qui non devono essere
1148 oss << "UPDATE " << table_name << " SET PKT_NUMBER_FINAL =" << (pkt_number_init - 1) << ", PKT_OBT_FINAL ="
1149 << obt_init << ", REAL_TIME_LAST =" << real_time_init << " WHERE ID_N =" << ID_record << ";";
1150
1151 }
1152 else if (type_rel == BEFORE) {
1153 mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file");
1154 oss << "UPDATE " << table_name << " SET PKT_NUMBER_INIT =" << (pkt_number_last + 1) << ", PKT_OBT_INIT ="
1155 << obt_last << ", REAL_TIME_INIT =" << real_time_last << " WHERE ID_N =" << ID_record << ";";
1156 }
1157 else if (type_rel == SMALLER) {
1158 //spezzettamentento
1159 mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file");
1160
1161 oss << "UPDATE " << table_name << " SET PKT_NUMBER_FINAL=" << (pkt_number_init - 1) << ", PKT_OBT_FINAL ="
1162 << obt_init << ", REAL_TIME_LAST =" << real_time_init << " WHERE ID_N =" << ID_record << ";";
1163
1164 mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file");
1165
1166 saveMergeROOT_DB(table_name, root_id, (pkt_number_last + 1), pkt_number_fin, obt_last, obt_fin, real_time_last,
1167 mtime_last, mboot_num, bad_perc);
1168 }
1169 else if (type_rel == BIGGER) {
1170 //marco_new: ok anche se coincidenti etc
1171 // oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO
1172 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
1173 mainLogUtil->logInfo("Record deleted from merging table");
1174 }
1175 else
1176 return false;
1177
1178 string msg = oss.str();
1179 mainLogUtil->logInfo(msg);
1180
1181 stringstream oss1;
1182 oss1.str() = "";
1183 string msg1;
1184
1185 TSQLResult* res = NULL;
1186 res = sqlServer->Query(oss.str().c_str());
1187 if (!res) {
1188 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1189 msg1 = oss1.str();
1190 mainLogUtil->logError(msg1);
1191 return false;
1192 }
1193 if (res) {
1194 delete res;
1195 res = NULL;
1196 }
1197 return true;
1198 }
1199
1200 //cerca eventuali ROOT files in DB che possono venire mergiati
1201 //marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune
1202 //marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente
1203 //marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx
1204 TSQLResult* PacketUser::Select_merging(const char* table_name, unsigned long int mtime_init, unsigned long int mtime_last,
1205 type_Rel_ROOT type_rel) {
1206 TSQLResult* res = NULL;
1207 stringstream query;
1208 query.str("");
1209
1210 if (type_rel == AFTER)
1211 query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_LAST > " << mtime_init
1212 << " and REAL_TIME_LAST <= " << mtime_last << " and REAL_TIME_INIT < " << mtime_init; // EMI
1213 else if (type_rel == BEFORE)
1214 query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT >= " << mtime_init
1215 << " and REAL_TIME_INIT < " << mtime_last << " and REAL_TIME_LAST > " << mtime_last; // EMI
1216 else if (type_rel == SMALLER)
1217 query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT < " << mtime_init
1218 << " and REAL_TIME_LAST > " << mtime_last; // EMI
1219 else if (type_rel == BIGGER)
1220 query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT >= " << mtime_init
1221 << " and REAL_TIME_LAST <= " << mtime_last; // EMI
1222 else
1223 return 0;
1224
1225 query << " order by REAL_TIME_INIT ASC";//marco_new: inutile
1226 //se volessi mettere un filtro sulla qualita'
1227 // query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ;
1228 string msg = query.str();
1229 mainLogUtil->logAll(msg);
1230 res = sqlServer->Query(query.str().c_str());
1231 stringstream oss1;
1232 oss1.str() = "";
1233 string msg1;
1234
1235 if (!res) {
1236 oss1 << "DBError UNABLE to: " << query.str().c_str() << endl;
1237 msg1 = oss1.str();
1238 mainLogUtil->logError(msg1);
1239 }
1240
1241 return res;
1242 }
1243
1244 bool PacketUser::saveMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in,
1245 unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin,
1246 unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double percentage) {
1247
1248 stringstream oss;
1249 oss.str("");
1250 oss << "INSERT INTO " << table_name
1251 << " (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)"
1252 << " VALUES ('" << 0 << "','" << root_id << "','" << pkt_number_in << "','" << pkt_number_fin << "','" << obt_in
1253 << "','" << obt_fin << "','" << mtime_init << "','" << mtime_last << "','" << mboot_num << "','" << percentage
1254 << "'," << "NULL" << ",'" << tag_value << "'" << ");";
1255
1256 string msg = oss.str();
1257 mainLogUtil->logAll(msg);
1258
1259 //marco_new: controlla
1260 //marco_new: esci se sono arrivato a zero
1261 // if(real_time_last==real_time_init){
1262 if (mtime_init == mtime_last) {
1263 mainLogUtil->logAll("Nothing to be saved");
1264
1265 // if(res){delete res; res = NULL;}
1266 // if(row){delete row; row = NULL;}
1267 return true;
1268 }
1269
1270 stringstream oss1;
1271 oss1.str() = "";
1272 string msg1;
1273
1274 TSQLResult* res = NULL;
1275 res = sqlServer->Query(oss.str().c_str());
1276 if (!res) {
1277 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1278 msg1 = oss1.str();
1279 mainLogUtil->logError(msg1);
1280 return false;
1281 }
1282 if (res) {
1283 delete res;
1284 res = NULL;
1285 }
1286 return true;
1287 }
1288
1289 //Utility: return max(IDN)
1290 unsigned int PacketUser::select_maxIDN_DB(const char* table_name) {
1291
1292 if (!strcmp(table_name, Table_ROOT_Bad)) {
1293 return 0;
1294 }
1295 unsigned int idN = 0;
1296 TSQLResult* res = NULL;
1297 stringstream oss;
1298 oss.str("");
1299 oss << "select max(ID_N) from " << table_name;
1300 // string msg = oss.str();
1301 // mainLogUtil->logAll(msg);
1302 res = sqlServer->Query(oss.str().c_str());
1303
1304 stringstream oss1;
1305 oss1.str() = "";
1306 string msg1;
1307
1308 if (!res) {
1309 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1310 msg1 = oss1.str();
1311 mainLogUtil->logError(msg1);
1312 return 0;
1313 }
1314
1315 TSQLRow* row = NULL;
1316 row = res->Next();
1317 if (!row) {
1318 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1319 msg1 = oss1.str();
1320 mainLogUtil->logError(msg1);
1321 return 0;
1322 }
1323
1324 idN = atoi(row->GetField(0));
1325 if (row) {
1326 delete row;
1327 row = NULL;
1328 }
1329 if (res) {
1330 delete res;
1331 res = NULL;
1332 }
1333
1334 oss << ". idN = " << idN;
1335 string msg = oss.str();
1336 mainLogUtil->logAll(msg);
1337
1338 return idN;
1339 }
1340
1341 /**
1342 * Lock par=table, if par=null lock all tables
1343 */
1344 int PacketUser::LockTables(const char* tableTobeLocked) {
1345 //se non ho il DB :)
1346 if (!do_cont_check)
1347 return 999;
1348
1349 TSQLResult* res = NULL;
1350 stringstream oss;
1351 oss.str("");
1352 if (!tableTobeLocked)
1353 oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET
1354 << " write, " << Table_ROOT_Merging << " write;";
1355 else
1356 oss << "lock table " << tableTobeLocked << " write; ";
1357
1358 string msg = oss.str();
1359 mainLogUtil->logAll(msg);
1360
1361 res = sqlServer->Query(oss.str().c_str());
1362
1363 stringstream oss1;
1364 oss1.str() = "";
1365 string msg1;
1366
1367 if (!res) {
1368 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1369 msg1 = oss1.str();
1370 mainLogUtil->logError(msg1);
1371 return 1;
1372 }
1373
1374 if (res) {
1375 delete res;
1376 res = NULL;
1377 }
1378 mainLogUtil->logAll("TABLES LOCKED");
1379 return 0;
1380 }
1381
1382 /**
1383 * UNLock tables
1384 */
1385 int PacketUser::UnLockTables() {
1386 //se non ho il DB :)
1387 if (!do_cont_check)
1388 return 999;
1389
1390 TSQLResult* res = NULL;
1391 stringstream oss;
1392 oss.str("");
1393 oss << "unlock tables;";
1394
1395 string msg = oss.str();
1396 mainLogUtil->logAll(msg);
1397
1398 res = sqlServer->Query(oss.str().c_str());
1399
1400 stringstream oss1;
1401 oss1.str() = "";
1402 string msg1;
1403
1404 if (!res) {
1405 oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl;
1406 msg1 = oss1.str();
1407 mainLogUtil->logError(msg1);
1408 return 1;
1409 }
1410
1411 if (res) {
1412 delete res;
1413 res = NULL;
1414 }
1415 mainLogUtil->logAll("TABLES UNLOCKED");
1416
1417 return 0;
1418 }
1419
1420 /*********************************************************/
1421 //marco_new_31
1422 void PacketUser::OpenDBConnection(const char* tableTobeLocked) {
1423 if (do_cont_check) {
1424 //controllo che non sia gia' aperta
1425 if (!sqlServer) {
1426 sqlServer = TSQLServer::Connect(connection, db_user, db_pwd);
1427 if ((!sqlServer) || (!(sqlServer->IsConnected()))) {
1428 cout << "Can not connect with MYSQL sever" << endl;
1429 }
1430 mainLogUtil->logAll("Connected to DB");
1431 stringstream myquery;
1432 myquery.str("");
1433 myquery << "SET time_zone='+0:00'";
1434 sqlServer->Query(myquery.str().c_str());
1435 //blocca le tabelle
1436 LockTables(tableTobeLocked);
1437 }
1438 }
1439
1440 }
1441
1442 //marco_new_31
1443 void PacketUser::CloseDBConnection() {
1444 if (do_cont_check) {
1445 //controllo che ce ne sia una aperta
1446 if (sqlServer && sqlServer->IsConnected()) {
1447 //marco_new: sblocca tutte le tabelle
1448 UnLockTables();
1449 sqlServer->Close();
1450 delete sqlServer;
1451 sqlServer = NULL;
1452 mainLogUtil->logAll("Closed Connection to DB");
1453 }
1454 }
1455 }
1456
1457 /***********************************************************************************************************************/
1458 /*#################################################### HIC SUNT LEONES ################################################*/
1459 /***********************************************************************************************************************/
1460 //UNUSED: dbg functions...
1461
1462 //Return the system time in ms
1463 unsigned long long PacketUser::Record_Time() {
1464 timeval tv;
1465 gettimeofday(&tv, NULL);
1466 unsigned long long timems = (unsigned long long) tv.tv_sec * (unsigned long long) 1000 + tv.tv_usec / 1000;
1467 return timems;
1468 }
1469
1470 //dbg_functions:
1471
1472 //UNUSED. save a packet in a file
1473 void PacketUser::savePKT_file(char* headerPkt, char* pamPkt, long int length, bool append, char* nomefile) {
1474 char fno[80] = "";
1475 ofstream fout;
1476 sprintf(fno, "%s/pkt%d_of_%s.pkt", gSystem->ExpandPathName(outDir), numPKTSaved, nomefile); // EMI
1477
1478 if (append == true)
1479 fout.open(fno, ios::binary | ios::app);
1480 else
1481 fout.open(fno, ios::binary);
1482
1483 if (!fout) {
1484 cout << "can not open output file " << fno << endl;
1485 return;
1486 }
1487 fout.write(headerPkt, LENGTH_HEADER_PKT);
1488 fout.write(pamPkt, length);
1489 fout.close();
1490 }
1491
1492 //UNUSED
1493 void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append) {
1494 char fno[80] = "";
1495 ofstream fout;
1496 if (append == true) {
1497 sprintf(fno, "%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI
1498 fout.open(fno, ios::binary | ios::app);
1499 }
1500 else {
1501 sprintf(fno, "%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT); // EMI
1502 fout.open(fno, ios::binary);
1503 }
1504
1505 if (!fout) {
1506 cout << "can not open output file " << fno << endl;
1507 return;
1508 }
1509 fout.write(headerPkt, LENGTH_HEADER_PKT);
1510 fout.write(pamPkt, length);
1511 fout.close();
1512 }
1513
1514 }

  ViewVC Help
Powered by ViewVC 1.1.23