1 |
//============================================================================ |
2 |
// $Id: PacketUser.cpp,v 1.20 2012/03/27 09:50:34 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* pelosconnection; |
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 |
//if ( !pRun ) printf(" horror! -%s- \n",rootfilename); |
279 |
reader->Init(pRun); |
280 |
stringstream oss; |
281 |
oss.str() = ""; |
282 |
oss << "######################### Start a new group in file: " << rootfilename << " ############################"; |
283 |
string msg = oss.str(); |
284 |
mainLogUtil->logInfo(msg); |
285 |
} |
286 |
|
287 |
//set the value of the counter and obt of the last packet of the group |
288 |
void PacketUser::setLast(unsigned long int counter, unsigned long int obt) { |
289 |
pkt_number_last = counter; |
290 |
obt_last = obt; |
291 |
} |
292 |
|
293 |
//retrieve obt_time_sync and last_time_sync_info from packet if type has special values and BOOT_NUMBER |
294 |
void PacketUser::setTimeSync(char* packet, long int pktLength, const PacketType* type) { |
295 |
//do nothing |
296 |
if (!do_cont_check) { |
297 |
if (is_new_route) |
298 |
is_new_route = false; |
299 |
return; |
300 |
} |
301 |
|
302 |
//TODO: usare anche MCMD ... NOTA: questo da sempre errore ret != 0 |
303 |
//In RunHeader e RunTrailer sono in secondi(?), controllare se aggiungo altri tipi di paccheti se invece sono in secondi |
304 |
// devo ricalcolare tutto non per ogni gruppetto (ROOT file) ma solo se cambia download |
305 |
//When I found a new download I need to reset all values |
306 |
if (is_new_route) { |
307 |
//reset |
308 |
is_new_route = false; |
309 |
//(tassa) ci ho ripensato se stiamo in un secondo download non prendiamo i bootnumber e timesync del precedente! |
310 |
obt_time_sync_prevvalue = 0;//obt_time_sync; |
311 |
last_time_sync_info_prevvalue = 0;//last_time_sync_info; |
312 |
|
313 |
obt_time_sync = 0; |
314 |
last_time_sync_info = 0; |
315 |
boot_number_prevvalue = 0;//boot_number; |
316 |
boot_number = 0; |
317 |
time_is_estimated = false; |
318 |
if (time_Offset) |
319 |
timeOffset = time_Offset; |
320 |
else |
321 |
timeOffset = retrieveTimeOffset(Table_GL_RESURS_OFFSET); |
322 |
} |
323 |
|
324 |
//retrieve boot number if possible |
325 |
setBootNumber(packet, pktLength, type); |
326 |
|
327 |
//Retrieve if possible obt_time_sync and last_time_sync_info |
328 |
int offset = 0; |
329 |
if (type == PacketType::RunHeader) { |
330 |
offset = 0; |
331 |
} |
332 |
else if (type == PacketType::RunTrailer) { |
333 |
offset = 3; |
334 |
} |
335 |
else if (type == PacketType::Mcmd) { |
336 |
// mainLogUtil->logAll("TODO: Packet Mcmd now is not used to retrieve Absolute time. This may cause problems somewhere."); |
337 |
// return; |
338 |
offset = -1; |
339 |
} |
340 |
else { |
341 |
return; |
342 |
} |
343 |
|
344 |
//I don't need to retrieve them again if they are good |
345 |
// if ((obt_time_sync) || (last_time_sync_info)) |
346 |
// return; |
347 |
// no, do it, since there is a (small indeed) time drift due to different clock of PAMELA CPU and RESURS CPU... |
348 |
// printf(" %s %lu %lu %lu %lu \n", rootfilename, pkt_number_init, pkt_number_last, obt_init, obt_last); |
349 |
if ( offset >= 0 ){ |
350 |
// use Runheader or RunTrailer |
351 |
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); |
352 |
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); |
353 |
// |
354 |
// if you arrive here and RH/RT timesync is zero try to get the correct info from DB (dangerous??) |
355 |
// |
356 |
// printf("qui\n"); |
357 |
if ( !obt_time_sync && !last_time_sync_info ){ |
358 |
// if ( true ){ |
359 |
stringstream os; |
360 |
os.str() = ""; |
361 |
os << " NEW (EM) AGH! NO GOOD TIMESYNC INFOS IN RH OR RT!! TRY TO LOOK IN DB"; |
362 |
string msg = os.str(); |
363 |
mainLogUtil->logAll(msg); |
364 |
|
365 |
// mysql> select ID_N,FOLDER_NAME,FILE_NAME,PKT_NUMBER_INIT,PKT_NUMBER_FINAL,BAD_PKT,NUM_PKT_SAVED,INPUT_NAME,INSERT_TIME,INSERTED_BY from ROOT_TABLE_BAD where ON_DISK='YES' order by NUM_PKT_SAVED asc; |
366 |
// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ |
367 |
// | ID_N | FOLDER_NAME | FILE_NAME | PKT_NUMBER_INIT | PKT_NUMBER_FINAL | BAD_PKT | NUM_PKT_SAVED | INPUT_NAME | INSERT_TIME | INSERTED_BY | |
368 |
// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ |
369 |
// | 6493 | $PAM_L0FIA | L0PAM_00540_002_012_4.root | 12253479 | 12254111 | 626 | 626 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:50:15 | FI | |
370 |
// | 6463 | $PAM_L0FIA | L0PAM_00540_002_010_4.root | 12252097 | 12252730 | 627 | 627 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:47:16 | FI | |
371 |
// | 6525 | $PAM_L0FIA | L0PAM_00540_002_014_4.root | 12254906 | 12255539 | 627 | 629 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:53:24 | FI | |
372 |
// | 321 | $PAM_L0FIA | L0PAM_00383_001_001_2.root | 601639 | 602281 | 489 | 639 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:49:55 | FI | |
373 |
// | 306 | $PAM_L0FIA | L0PAM_00383_002_001_2.root | 601572 | 602213 | 0 | 642 | /wizard/03/pamela_data/preRawreader/00383002.pam | 2010-02-03 16:48:55 | FI | |
374 |
// | 400 | $PAM_L0FIA | L0PAM_00383_001_005_2.root | 605234 | 605893 | 499 | 656 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:57:08 | FI | |
375 |
// | 372 | $PAM_L0FIA | L0PAM_00383_001_003_2.root | 603600 | 604258 | 290 | 659 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:53:44 | FI | |
376 |
// | 6483 | $PAM_L0FIA | L0PAM_00540_002_011_4.root | 12252731 | 12253478 | 739 | 739 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:48:52 | FI | |
377 |
// | 6428 | $PAM_L0FIA | L0PAM_00540_002_009_4.root | 12251326 | 12252096 | 764 | 765 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:45:51 | FI | |
378 |
// | 6507 | $PAM_L0FIA | L0PAM_00540_002_013_4.root | 12254112 | 12254905 | 781 | 781 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:52:01 | FI | |
379 |
// | 378 | $PAM_L0FIA | L0PAM_00374_001_001_1.root | 368888 | 369754 | 0 | 867 | /wizard/03/pamela_data/preRawreader/00374001.pam | 2010-02-03 16:54:30 | FI | |
380 |
// | 6565 | $PAM_L0FIA | L0PAM_00540_002_015_4.root | 12255540 | 12256457 | 907 | 907 | /wizard/03/pamela_data/preRawreader/00540002.pam | 2010-02-03 21:55:22 | FI | |
381 |
// | 413 | $PAM_L0FIA | L0PAM_00383_001_006_2.root | 605894 | 606807 | 417 | 913 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:58:52 | FI | |
382 |
// | 390 | $PAM_L0FIA | L0PAM_00383_001_004_2.root | 604259 | 605233 | 684 | 975 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:55:45 | FI | |
383 |
// | 358 | $PAM_L0FIA | L0PAM_00383_001_002_2.root | 602282 | 603599 | 649 | 1316 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 16:52:23 | FI | |
384 |
// | 185 | $PAM_L0NAA | L0PAM_01737_001_001_1.root | 9373563 | 9375405 | 0 | 1843 | /storage02/pamela/pamela_data/preRawreader/01737001.pam | 2010-02-03 16:24:50 | NA | |
385 |
// | 375 | $PAM_L0FIA | L0PAM_00383_002_002_2.root | 602214 | 608541 | 0 | 6328 | /wizard/03/pamela_data/preRawreader/00383002.pam | 2010-02-03 16:54:08 | FI | |
386 |
// | 1419 | $PAM_L0FIA | L0PAM_00383_001_007_2.root | 606808 | 620604 | 9086 | 13764 | /wizard/03/pamela_data/preRawreader/00383001.pam | 2010-02-03 17:26:39 | FI | |
387 |
// | 6118 | $PAM_L0NAA | L0PAM_02144_002_001_1.root | 0 | 123139 | 6 | 123139 | /storage02/pamela/pamela_data/preRawreader/02144002.pam | 2010-02-03 21:29:51 | NA | |
388 |
// | 4470 | $PAM_L0FIA | L0PAM_00614_001_001_1.root | 1409 | 130146 | 6 | 128722 | /wizard/03/pamela_data/preRawreader/00614001.pam | 2010-02-03 18:47:26 | FI | |
389 |
// +------+-------------+----------------------------+-----------------+------------------+---------+---------------+---------------------------------------------------------+---------------------+-------------+ |
390 |
// 20 rows in set (0.05 sec) |
391 |
// mysql> select FILE_NAME, abs(PKT_NUMBER_INIT-123139), abs(0-PKT_NUMBER_FINAL) from ROOT_TABLE where (abs(PKT_NUMBER_INIT-123139)<10000 or abs(0-PKT_NUMBER_FINAL)<10000) and FILE_NAME like "L0PAM_021%"; |
392 |
// +-----------------------------+-----------------------------+-------------------------+ |
393 |
// | FILE_NAME | abs(PKT_NUMBER_INIT-123139) | abs(0-PKT_NUMBER_FINAL) | |
394 |
// +-----------------------------+-----------------------------+-------------------------+ |
395 |
// | L0PAM_02173_001_001_11.root | 123103 | 251 | |
396 |
// +-----------------------------+-----------------------------+-------------------------+ |
397 |
// 1 row in set (0.07 sec) |
398 |
// |
399 |
TString *nomino=new TString(rootfilename); |
400 |
nomino->Remove(9); |
401 |
TString mmyy=Form("select OBT_TIME_SYNC, LAST_TIME_SYNC_INFO from ROOT_TABLE where (abs(PKT_NUMBER_INIT-%lu)<10000 or abs(%lu-PKT_NUMBER_FINAL)<10000) and FILE_NAME like '%s%%' limit 1;",pkt_number_last, pkt_number_init,nomino->Data()); |
402 |
delete nomino; |
403 |
stringstream os3; |
404 |
os3.str() = ""; |
405 |
os3 << " NEW (EM) TRY TO FIND TSYNC IN DB QUERY: " << mmyy.Data(); |
406 |
string msg3 = os3.str(); |
407 |
mainLogUtil->logAll(msg3); |
408 |
// |
409 |
TSQLResult* res = NULL; |
410 |
TSQLRow* row = NULL; |
411 |
Bool_t apro = false; |
412 |
if (!single_connection && !sqlServer ) { |
413 |
OpenDBConnection(NULL);// no, locak tutto Nota qui ho deciso di lockare solo questa tabella e non tutte |
414 |
apro = true; |
415 |
} |
416 |
res = sqlServer->Query(mmyy.Data()); |
417 |
if (!res) { |
418 |
mainLogUtil->logError("DBError retrieve time sync"); |
419 |
return; |
420 |
}; |
421 |
row = res->Next(); |
422 |
if ( row ){ |
423 |
obt_time_sync = (ULong_t)atoi(row->GetField(0)); |
424 |
last_time_sync_info = (ULong_t)atoi(row->GetField(1)); |
425 |
// |
426 |
stringstream os2; |
427 |
os2.str() = ""; |
428 |
os2 << " NEW (EM) FOUND TSYNC IN DB "; |
429 |
string msg2 = os2.str(); |
430 |
mainLogUtil->logAll(msg2); |
431 |
// |
432 |
}; |
433 |
if (res) { |
434 |
delete res; |
435 |
res = NULL; |
436 |
}; |
437 |
// |
438 |
if (!single_connection && apro) { |
439 |
CloseDBConnection(); |
440 |
}; |
441 |
}; |
442 |
|
443 |
} else { |
444 |
// Use Packet Mcmd now is not used to retrieve Absolute time |
445 |
// printf(" Pointer to Mcmd obj is %p \n",reader->GetPtr("Mcmd")); |
446 |
|
447 |
UInt_t recEntries = 0; |
448 |
// Double_t minimum = 0.; |
449 |
// Double_t maximum = 0.; |
450 |
// Double_t minimum2 = 0.; |
451 |
Double_t maximum2 = 0.; |
452 |
UInt_t TSYNC = 0; |
453 |
UInt_t OBT = 0; |
454 |
Bool_t existsts = false; |
455 |
// |
456 |
pamela::McmdEvent *mc = (pamela::McmdEvent*)(((pamela::techmodel::McmdReader*)reader->GetPtr("Mcmd"))->GetMcmd()); |
457 |
pamela::McmdRecord *mcrc = 0; |
458 |
TArrayC *mcmddata = 0; |
459 |
// |
460 |
// minimum = numeric_limits<Double_t>::max(); |
461 |
// maximum = numeric_limits<Double_t>::min(); |
462 |
// minimum2 = numeric_limits<Double_t>::max(); |
463 |
maximum2 = numeric_limits<Double_t>::min(); |
464 |
// |
465 |
recEntries = mc->Records->GetEntries(); |
466 |
// printf(" recEntries is %i \n",recEntries); |
467 |
|
468 |
for (UInt_t j = 0; j < recEntries; j++){ |
469 |
mcrc = (pamela::McmdRecord*)mc->Records->At(j); |
470 |
mcmddata = mcrc->McmdData; |
471 |
// |
472 |
if (mcrc->ID1 == 0xE0 && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd timesync |
473 |
// |
474 |
OBT = (Int_t)(mcrc->MCMD_RECORD_OBT); |
475 |
// |
476 |
TSYNC = (((UInt_t)mcmddata->At(0)<<24)&0xFF000000) + (((UInt_t)mcmddata->At(1)<<16)&0x00FF0000) + (((UInt_t)mcmddata->At(2)<<8)&0x0000FF00) + (((UInt_t)mcmddata->At(3))&0x000000FF); |
477 |
// |
478 |
// printf("mcmd tsync tsync %u obt %u \n",TSYNC,OBT); |
479 |
// |
480 |
if ( TSYNC && OBT ){ |
481 |
existsts = true; |
482 |
stringstream os; |
483 |
os.str() = ""; |
484 |
os << " NEW (EM) FOUND TIMESYNC IN TIMESYNC MCMD"; |
485 |
string msg = os.str(); |
486 |
mainLogUtil->logAll(msg); |
487 |
break; |
488 |
}; |
489 |
// |
490 |
}; |
491 |
}; |
492 |
// |
493 |
if ( !existsts && !obt_time_sync && !last_time_sync_info ){ // try with inclination mcmd but only if everything else fails and check that the result is good |
494 |
// |
495 |
Double_t timesync = 0.; |
496 |
// |
497 |
for (UInt_t j = 0; j < recEntries; j++){ |
498 |
mcrc = (pamela::McmdRecord*)mc->Records->At(j); |
499 |
mcmddata = mcrc->McmdData; |
500 |
// |
501 |
if (mcrc->ID1 == 0xE2 && mcrc->Mcmd_Block_crc_ok == 1 ){ // mcmd inclination |
502 |
timesync = 0.; |
503 |
timesync = (Double_t)(((((UInt_t)mcmddata->At(0) << 24) & 0xFF000000) + (((UInt_t)mcmddata->At(1) << 16) & 0x00FF0000) + (((UInt_t)mcmddata->At(2) << 8) & 0x0000FF00) + ((UInt_t)mcmddata->At(3) & 0x000000FF))/128.0); |
504 |
// |
505 |
if ( timesync > maximum2){ |
506 |
maximum2 = timesync; |
507 |
OBT = (Int_t)(mcrc->MCMD_RECORD_OBT); |
508 |
}; |
509 |
}; |
510 |
// |
511 |
}; |
512 |
if ( maximum2 > numeric_limits<Double_t>::min() ){ |
513 |
TSYNC = (UInt_t)(maximum2 + 0.5); |
514 |
if ( TSYNC && OBT ){ |
515 |
existsts = true; |
516 |
stringstream os; |
517 |
os.str() = ""; |
518 |
os << " NEW (EM) FOUND TIMESYNC IN INCLINATION MCMD"; |
519 |
string msg = os.str(); |
520 |
mainLogUtil->logAll(msg); |
521 |
}; |
522 |
}; |
523 |
if ( labs(labs(last_time_sync_info_prevvalue-obt_time_sync_prevvalue)-labs(TSYNC-int(OBT/1000))) > 10 ) return; |
524 |
}; |
525 |
if ( !existsts ) return; |
526 |
// printf(" TIMESYNC FROM MCMD FOUND: TSYNC = %i OBT = %i \n",TSYNC,OBT); |
527 |
obt_time_sync = int(OBT/1000); |
528 |
last_time_sync_info = TSYNC; |
529 |
|
530 |
}; |
531 |
|
532 |
stringstream oss; |
533 |
oss.str() = ""; |
534 |
oss << "In download: " << download << " timeOffset: " << timeOffset << " obt_time_sync: " << obt_time_sync |
535 |
<< " last_time_sync_info: " << last_time_sync_info << " using Packet Type: " << type->GetName(); |
536 |
// 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(); |
537 |
string msg = oss.str(); |
538 |
mainLogUtil->logAll(msg); |
539 |
} |
540 |
|
541 |
//Boot Number |
542 |
void PacketUser::setBootNumber(char* packet, long int pktLength, const PacketType* type) { |
543 |
//se gia calcolato esco: lo calcolo una volta per download e per continuita' |
544 |
if (boot_number) |
545 |
return; |
546 |
|
547 |
if (type == PacketType::VarDump) { |
548 |
int b_offset = 34;//4+5*6 |
549 |
boot_number = (((UINT32) packet[1 + b_offset] << 24) & 0xFF000000) + (((UINT32) packet[2 + b_offset] << 16) |
550 |
& 0x00FF0000) + (((UINT32) packet[3 + b_offset] << 8) & 0x0000FF00) + (((UINT32) packet[4 + b_offset]) |
551 |
& 0x000000FF); |
552 |
|
553 |
/* |
554 |
long int dataLength = pktLength - 2; //the block of data |
555 |
int b_offset = 4; |
556 |
while (b_offset < dataLength){ |
557 |
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); |
558 |
b_offset = b_offset + 5; |
559 |
} |
560 |
*/ |
561 |
stringstream oss; |
562 |
oss.str() = ""; |
563 |
// oss<<"In download: "<<download<<" boot_number: "<<boot_number<<" using Packet Type: "<<type->GetName().c_str(); |
564 |
oss << "In download: " << download << " boot_number: " << boot_number << " using Packet Type: " << type->GetName(); |
565 |
string msg = oss.str(); |
566 |
mainLogUtil->logAll(msg); |
567 |
return; |
568 |
} |
569 |
else { |
570 |
return; |
571 |
} |
572 |
|
573 |
} |
574 |
//set the real time of the first packet and the last packet oif a group of packet if possible |
575 |
void PacketUser::setReal_Time() { |
576 |
|
577 |
if (!obt_time_sync && !last_time_sync_info && numPKTSaved > recoverlimit) { |
578 |
time_is_estimated = true; |
579 |
obt_time_sync = obt_time_sync_prevvalue; |
580 |
last_time_sync_info = last_time_sync_info_prevvalue; |
581 |
} |
582 |
if (obt_time_sync || last_time_sync_info) { |
583 |
real_time_init = (obt_init / 1000 - obt_time_sync) + last_time_sync_info; |
584 |
//se obt si e' resettato |
585 |
if (obt_last < obt_init) { |
586 |
real_time_last = (max_pkt_obt / 1000 + obt_last / 1000 - obt_time_sync) + last_time_sync_info; |
587 |
stringstream oss; |
588 |
oss.str() = ""; |
589 |
oss << "obt_last(" << obt_last << ") < obt_init(" << obt_init |
590 |
<< "). It is due to Pamela Reset. Adding max_pkt_obt(" << max_pkt_obt |
591 |
<< ") to obt_last in order to compute real_time_last"; |
592 |
string msg = oss.str(); |
593 |
mainLogUtil->logWarning(msg); |
594 |
} |
595 |
else |
596 |
real_time_last = (obt_last / 1000 - obt_time_sync) + last_time_sync_info; |
597 |
real_time_init += timeOffset; |
598 |
real_time_last += timeOffset; |
599 |
} |
600 |
} |
601 |
|
602 |
void PacketUser::setReal_TimeRECOVERY( |
603 |
unsigned long int & _real_time_init, |
604 |
unsigned long int & _real_time_last, |
605 |
unsigned long int _obt_init, |
606 |
unsigned long int _obt_last |
607 |
) { |
608 |
|
609 |
if (obt_time_sync || last_time_sync_info) { |
610 |
_real_time_init = (_obt_init / 1000 - obt_time_sync) + last_time_sync_info; |
611 |
//se obt si e' resettato |
612 |
if (_obt_last < _obt_init) { |
613 |
_real_time_last = (max_pkt_obt / 1000 + _obt_last / 1000 - obt_time_sync) + last_time_sync_info; |
614 |
stringstream oss; |
615 |
oss.str() = ""; |
616 |
oss << "obt_last(" << obt_last << ") < obt_init(" << obt_init |
617 |
<< "). It is due to Pamela Reset. Adding max_pkt_obt(" << max_pkt_obt |
618 |
<< ") to obt_last in order to compute real_time_last"; |
619 |
string msg = oss.str(); |
620 |
mainLogUtil->logWarning(msg); |
621 |
} |
622 |
else |
623 |
_real_time_last = (_obt_last / 1000 - obt_time_sync) + last_time_sync_info; |
624 |
_real_time_init += timeOffset; |
625 |
_real_time_last += timeOffset; |
626 |
} |
627 |
} |
628 |
|
629 |
|
630 |
//timeOffset |
631 |
//retrieve from table Table_GL_RESURS_OFFSET timeOffset |
632 |
unsigned long int PacketUser::retrieveTimeOffset(const char * table) { |
633 |
if (!do_cont_check) |
634 |
return 0; |
635 |
|
636 |
//if(!table){table=Table_GL_RESURS_OFFSET} |
637 |
|
638 |
if (!single_connection) { |
639 |
OpenDBConnection(NULL);// no, locak tutto Nota qui ho deciso di lockare solo questa tabella e non tutte |
640 |
} |
641 |
|
642 |
UInt_t t0 = 0;//toffset |
643 |
stringstream oss; |
644 |
oss.str(""); |
645 |
oss |
646 |
<< "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM " |
647 |
<< table << " WHERE SPECIAL_FILE='" << nnnn_mmm_ppp << "';"; |
648 |
|
649 |
string msg = oss.str(); |
650 |
mainLogUtil->logAll(msg); |
651 |
|
652 |
TSQLResult* res = NULL; |
653 |
TSQLRow* row = NULL; |
654 |
res = sqlServer->Query(oss.str().c_str()); |
655 |
if (!res) { |
656 |
mainLogUtil->logError("DBError retrieveTimeOffset 1"); |
657 |
return 0; |
658 |
} |
659 |
|
660 |
row = res->Next(); |
661 |
if (!row) { |
662 |
if (res) { |
663 |
delete res; |
664 |
res = NULL; |
665 |
} |
666 |
|
667 |
oss.str(""); |
668 |
oss |
669 |
<< "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM " |
670 |
<< table << " WHERE FROM_ORBIT < " << orbit_number << " AND SPECIAL_FILE='' order by FROM_ORBIT desc limit 1;"; |
671 |
|
672 |
string msg2 = oss.str(); |
673 |
mainLogUtil->logAll(msg2); |
674 |
|
675 |
res = sqlServer->Query(oss.str().c_str()); |
676 |
if (!res) { |
677 |
mainLogUtil->logError("DBError retrieveTimeOffset 2"); |
678 |
return 0; |
679 |
} |
680 |
row = res->Next(); |
681 |
if (!row) { |
682 |
mainLogUtil->logError("DBError retrieveTimeOffset 3"); |
683 |
return 0; |
684 |
} |
685 |
|
686 |
} |
687 |
|
688 |
TTimeStamp tu = TTimeStamp((UInt_t) atoi(row->GetField(0)), (UInt_t) atoi(row->GetField(1)), (UInt_t) atoi( |
689 |
row->GetField(2)), (UInt_t) atoi(row->GetField(3)), (UInt_t) atoi(row->GetField(4)), (UInt_t) atoi(row->GetField( |
690 |
5)), 0, true, 0); |
691 |
t0 = (UInt_t) tu.GetSec(); |
692 |
|
693 |
if (res) { |
694 |
delete res; |
695 |
res = NULL; |
696 |
} |
697 |
if (row) { |
698 |
delete row; |
699 |
row = NULL; |
700 |
} |
701 |
|
702 |
if (!single_connection) { |
703 |
CloseDBConnection(); |
704 |
} |
705 |
return t0; |
706 |
} |
707 |
|
708 |
//this is public and can be called from the main at the end of the game |
709 |
void PacketUser::FinishLastGroup() { |
710 |
mainLogUtil->logAll("######################### Closing the last group ############################"); |
711 |
setReal_Time(); |
712 |
FinishGroup(rootfilename); |
713 |
mainLogUtil->logAll("######################### Closed the last group ############################"); |
714 |
// |
715 |
if (single_connection) { |
716 |
CloseDBConnection(); |
717 |
} |
718 |
} |
719 |
|
720 |
//Finish the old root file |
721 |
void PacketUser::FinishGroup(char * filename) { |
722 |
if (pRun) { |
723 |
if ( !( !real_time_init && !real_time_last && numPKTSaved <= recoverlimit ) || !candelete ) pRun->WriteFiles(); |
724 |
delete pRun; |
725 |
pRun = NULL; |
726 |
stringstream close; |
727 |
close.str() = ""; |
728 |
string msg; |
729 |
|
730 |
close << "TAG XX ######################### Close group in file: " << rootfilename << " ############################"; |
731 |
msg = close.str(); |
732 |
mainLogUtil->logInfo(msg); |
733 |
|
734 |
// se non faccio proprio controlli su continuita' neanche salvo su DB |
735 |
if (!do_cont_check) |
736 |
return; |
737 |
|
738 |
stringstream oss; |
739 |
oss.str() = ""; |
740 |
|
741 |
//TODO: check when I don't want to log in DB informations |
742 |
// se non ho salvato nessun pacchetto |
743 |
if (numPKTSaved == 0) { |
744 |
stringstream err; |
745 |
err.str() = ""; |
746 |
err << "NO PACKET SAVED in file: " << rootfilename << " The group is empty: you can remove this file."; |
747 |
msg = err.str(); |
748 |
mainLogUtil->logError(msg); |
749 |
// return; |
750 |
} |
751 |
|
752 |
//se non ho salvato nessun pacchetto buono |
753 |
if ((numPKTSaved - bad_pkt_EventReader - bad_pkt_CalibReader - bad_pkt) == 0) { |
754 |
stringstream err; |
755 |
err.str() = ""; |
756 |
err << "NO GOOD PACKET SAVED in file: " << rootfilename << " The group is BAD: you may want to remove this file."; |
757 |
msg = err.str(); |
758 |
mainLogUtil->logInfo(msg); |
759 |
// mainLogUtil->logError(msg); |
760 |
// return; //TODO: decidi se nn vuoi affatto usarlo o addirittura salvarlo tra i BAD??? |
761 |
} |
762 |
|
763 |
if (!single_connection) { |
764 |
OpenDBConnection(NULL);//Nota qui ho deciso di lockare tutte le tabelle |
765 |
} |
766 |
|
767 |
|
768 |
//don't save in Table_ROOT_Good and don't search for ROOT files in the same temporal range |
769 |
if ((!real_time_init) && (!real_time_last)) { |
770 |
//saved in another table for future study |
771 |
if (saveROOT_DB( |
772 |
Table_ROOT_Bad, |
773 |
outDir,// here outDir is good, no expand EMI |
774 |
filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info, |
775 |
real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, |
776 |
numPKTSaved, fni, time_is_estimated) == true) { |
777 |
oss.str() = ""; |
778 |
oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Bad; |
779 |
msg = oss.str(); |
780 |
mainLogUtil->logInfo(msg); |
781 |
} |
782 |
else { |
783 |
oss.str() = ""; |
784 |
oss << "Problem storing information in DB regarding file: " << filename; |
785 |
msg = oss.str(); |
786 |
mainLogUtil->logError(msg); |
787 |
} |
788 |
|
789 |
|
790 |
|
791 |
if ( numPKTSaved > recoverlimit){ // EM |
792 |
// |
793 |
if ( obt_time_sync && last_time_sync_info ){ |
794 |
stringstream err; |
795 |
err.str() = ""; |
796 |
err << "(EM): -- RECOVERING WHEN NUMPACKET > "<< recoverlimit <<"!! -- file=<" << filename << ">. We have a previous time sync, immediate recovery!\n"; |
797 |
msg = err.str(); |
798 |
mainLogUtil->logInfo(msg); |
799 |
// |
800 |
setReal_TimeRECOVERY( |
801 |
real_time_init, |
802 |
real_time_last, |
803 |
obt_init, |
804 |
obt_last |
805 |
); |
806 |
// |
807 |
} else { |
808 |
stringstream err; |
809 |
err.str() = ""; |
810 |
err << "TAG(tassa): -- RECOVERING WHEN NUMPACKET > "<< recoverlimit <<"!! -- file=<" << filename << ">. Saving in a queue to try alfer to recover it!\n"; |
811 |
msg = err.str(); |
812 |
mainLogUtil->logInfo(msg); |
813 |
|
814 |
// TString qu = Form("select ID_N from ROOT_TABLE_BAD order by INSERT_TIME desc limit 1;"); // <========================= |
815 |
TString qu = Form("select ID_N from ROOT_TABLE_BAD where FILE_NAME='%s' and PKT_NUMBER_INIT=%lu and INSERTED_BY='%s';",filename,pkt_number_init,tag_value); // <========================= |
816 |
TSQLResult *result = sqlServer->Query(qu.Data()); |
817 |
TSQLRow *row = result->Next(); |
818 |
Int_t eid = -1; |
819 |
if ( row ) eid = (Int_t)atoi(row->GetField(0)); |
820 |
idtorecover->AddAt(eid,arsize); |
821 |
arsize++; |
822 |
if ( arsize > 100 ) idtorecover->Set(arsize); |
823 |
// dbinfo *app = new dbinfo( outDir, |
824 |
// filename, pkt_number_init, pkt_number_last, obt_init, obt_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, |
825 |
// numPKTSaved, fni, time_is_estimated); |
826 |
// m_dbinfos.push_back(app); |
827 |
} |
828 |
|
829 |
CleanDisk(); |
830 |
// if (!single_connection) { |
831 |
// CloseDBConnection(); |
832 |
// } |
833 |
// return; // commented here... |
834 |
|
835 |
}; |
836 |
} |
837 |
|
838 |
if ( real_time_init || real_time_last) { |
839 |
//saves info in DB in table Table_ROOT |
840 |
if (boot_number == 0) |
841 |
boot_number = boot_number_prevvalue; |
842 |
|
843 |
if (saveROOT_DB( |
844 |
Table_ROOT_Good, |
845 |
outDir, // here outDir is good, no expand EMI |
846 |
filename, pkt_number_init, pkt_number_last, obt_init, obt_last, obt_time_sync, last_time_sync_info, |
847 |
real_time_init, real_time_last, boot_number, timeOffset, bad_pkt, bad_pkt_EventReader, bad_pkt_CalibReader, |
848 |
numPKTSaved, fni, time_is_estimated) == true) { |
849 |
oss.str() = ""; |
850 |
oss << "Saved information regarding file: " << filename << " in table " << Table_ROOT_Good << " id= " << my_id; |
851 |
msg = oss.str(); |
852 |
mainLogUtil->logInfo(msg); |
853 |
|
854 |
if (tryMerge) { |
855 |
merge_ROOTfiles(); |
856 |
} |
857 |
} else { |
858 |
oss.str() = ""; |
859 |
oss << "Problem storing information in DB regarding file: " << filename; |
860 |
msg = oss.str(); |
861 |
mainLogUtil->logError(msg); |
862 |
} |
863 |
}; |
864 |
|
865 |
// if dbinfo is not empty than |
866 |
if ( arsize > 0 ){ |
867 |
|
868 |
if ( !obt_time_sync || !last_time_sync_info ){ |
869 |
TString *nomino=new TString(rootfilename); |
870 |
nomino->Remove(15); |
871 |
TString qu = Form("select OBT_TIME_SYNC,LAST_TIME_SYNC_INFO from ROOT_TABLE where FILE_NAME like '%s_%%' and (abs(PKT_NUMBER_INIT-%lu)<50000 or abs(PKT_OBT_INIT-%lu)<3000000) limit 1;",nomino->Data(),pkt_number_init,obt_init); |
872 |
// |
873 |
oss.str() = ""; |
874 |
oss << "(EM) Trying to find obt_time_sync and last_time_sync_info from DB, query is: " << qu.Data(); |
875 |
msg = oss.str(); |
876 |
mainLogUtil->logInfo(msg); |
877 |
// |
878 |
TSQLResult *result = sqlServer->Query(qu.Data()); |
879 |
TSQLRow *row = result->Next(); |
880 |
if ( row && result->GetRowCount() == 1 ){ |
881 |
obt_time_sync = (unsigned long int)atoll(row->GetField(0)); |
882 |
last_time_sync_info = (unsigned long int)atoll(row->GetField(1)); |
883 |
oss.str() = ""; |
884 |
oss << "(EM) found obt_time_sync and last_time_sync_info from DB! obt_time_sync = " <<obt_time_sync <<" last_time_sync_info = " << last_time_sync_info; |
885 |
msg = oss.str(); |
886 |
mainLogUtil->logInfo(msg); |
887 |
}; |
888 |
delete nomino; |
889 |
}; |
890 |
|
891 |
if ( obt_time_sync && last_time_sync_info ){ |
892 |
// if(!m_dbinfos.empty()){ |
893 |
// while(!m_dbinfos.empty()){ |
894 |
// dbinfo *app = m_dbinfos.back(); |
895 |
// m_dbinfos.pop_back(); |
896 |
Int_t ez=0; |
897 |
while ( ez < arsize ){ |
898 |
Int_t eid = idtorecover->At(ez); |
899 |
if ( eid > -1 ){ |
900 |
|
901 |
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); |
902 |
TSQLResult *result = sqlServer->Query(qu.Data()); |
903 |
TSQLRow *row = result->Next(); |
904 |
|
905 |
TString efolder_name=""; |
906 |
TString efile_name=""; |
907 |
unsigned long int epkt_number_in=0ULL; |
908 |
unsigned long int epkt_number_fin=0ULL; |
909 |
unsigned long int eobt_in=0ULL; |
910 |
unsigned long int eobt_fin=0ULL; |
911 |
unsigned long int emboot_num=0ULL; |
912 |
unsigned long int etime_offset=0ULL; |
913 |
int ebad_pkt=0; |
914 |
int ebad_pkt_read=0; |
915 |
int ebad_pkt_CalRead=0; |
916 |
int enum_PKT_Saved=0; |
917 |
TString enome_input=""; |
918 |
bool e_time_is_estimated=false; |
919 |
|
920 |
if ( row ){ |
921 |
efolder_name = (TString)row->GetField(0); |
922 |
efile_name = (TString)row->GetField(1); |
923 |
epkt_number_in = (unsigned long int)atoll(row->GetField(2)); |
924 |
epkt_number_fin = (unsigned long int)atoll(row->GetField(3)); |
925 |
eobt_in = (unsigned long int)atoll(row->GetField(4)); |
926 |
eobt_fin = (unsigned long int)atoll(row->GetField(5)); |
927 |
emboot_num = (unsigned long int)atoll(row->GetField(6)); |
928 |
etime_offset = (unsigned long int)atoll(row->GetField(7)); |
929 |
ebad_pkt = (int)atoi(row->GetField(8)); |
930 |
ebad_pkt_read = (int)atoi(row->GetField(9)); |
931 |
ebad_pkt_CalRead = (int)atoi(row->GetField(10)); |
932 |
enum_PKT_Saved = (int)atoi(row->GetField(11)); |
933 |
enome_input = (TString)row->GetField(12); |
934 |
e_time_is_estimated = (bool)atoi(row->GetField(13)); |
935 |
}; |
936 |
delete result; |
937 |
|
938 |
setReal_TimeRECOVERY( |
939 |
real_time_init, |
940 |
real_time_last, |
941 |
eobt_in, |
942 |
eobt_fin |
943 |
); |
944 |
// |
945 |
|
946 |
if (saveROOT_DB( |
947 |
Table_ROOT_Good, |
948 |
efolder_name.Data(), |
949 |
efile_name.Data(), |
950 |
epkt_number_in, |
951 |
epkt_number_fin, eobt_in, eobt_fin, obt_time_sync, last_time_sync_info, |
952 |
real_time_init, real_time_last, emboot_num, etime_offset, ebad_pkt, ebad_pkt_read, ebad_pkt_CalRead, |
953 |
enum_PKT_Saved, enome_input.Data(), e_time_is_estimated) == true) { |
954 |
oss.str() = ""; |
955 |
oss << "\nTAG(tassa): RECOVERED!!!! Saved information regarding file: " << efile_name << " in table " << Table_ROOT_Good << " id= " << my_id; |
956 |
msg = oss.str(); |
957 |
mainLogUtil->logInfo(msg); |
958 |
/*merging we use global variable. we haveto set with current value*/ |
959 |
|
960 |
strcpy(outDir,efolder_name.Data() ); |
961 |
// outDir=efolder_name.Data(); |
962 |
strcpy(filename,efile_name.Data() ); |
963 |
// filename=efile_name.Data(); |
964 |
pkt_number_init=epkt_number_in; |
965 |
pkt_number_last=epkt_number_fin; |
966 |
obt_init=eobt_in; |
967 |
obt_last=eobt_fin; |
968 |
boot_number=emboot_num; |
969 |
timeOffset=etime_offset; |
970 |
bad_pkt=ebad_pkt; |
971 |
bad_pkt_EventReader=ebad_pkt_read; |
972 |
bad_pkt_CalibReader=ebad_pkt_CalRead; |
973 |
numPKTSaved=enum_PKT_Saved; |
974 |
strcpy(fni,enome_input.Data() ); |
975 |
// fni=enome_input.Data(); |
976 |
time_is_estimated=e_time_is_estimated; |
977 |
|
978 |
if (tryMerge) { |
979 |
merge_ROOTfiles(); |
980 |
} |
981 |
|
982 |
TString qu2 = Form("update ROOT_TABLE_BAD set ON_DISK='MVD' where ID_N=%i;",eid); |
983 |
sqlServer->Query(qu2.Data()); |
984 |
|
985 |
|
986 |
} |
987 |
else { |
988 |
oss.str() = ""; |
989 |
oss << "Problem storing information in DB regarding file: " << filename; |
990 |
msg = oss.str(); |
991 |
mainLogUtil->logError(msg); |
992 |
} |
993 |
|
994 |
|
995 |
}; |
996 |
ez++; |
997 |
} |
998 |
// |
999 |
idtorecover->Reset(); |
1000 |
delete idtorecover; |
1001 |
idtorecover = new TArrayI(100); |
1002 |
arsize = 0; |
1003 |
// delete app; |
1004 |
}; |
1005 |
}; |
1006 |
|
1007 |
CleanDisk(); |
1008 |
if (!single_connection) { |
1009 |
CloseDBConnection(); |
1010 |
} |
1011 |
|
1012 |
}//pRun |
1013 |
} |
1014 |
|
1015 |
|
1016 |
void PacketUser::CleanDisk(){ |
1017 |
if ( !candelete ) return; |
1018 |
// |
1019 |
// delete small files present in root_table_bad from disk: |
1020 |
// |
1021 |
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); |
1022 |
TSQLResult *result = sqlServer->Query(qu.Data()); |
1023 |
TSQLRow *row = result->Next(); |
1024 |
while ( row ){ |
1025 |
TString tbdel = (TString)gSystem->ExpandPathName(row->GetField(0))+"/"+ (TString)row->GetField(1); |
1026 |
stringstream err; |
1027 |
string msg; |
1028 |
err.str() = ""; |
1029 |
err << " Deleting file " << tbdel.Data() << " from disk! "; |
1030 |
msg = err.str(); |
1031 |
mainLogUtil->logInfo(msg); |
1032 |
gSystem->Unlink(tbdel.Data()); |
1033 |
// printf("ciccio %s \n",tbdel.Data()); |
1034 |
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); |
1035 |
// printf("pluto %s \n",q2u.Data()); |
1036 |
sqlServer->Query(q2u.Data()); |
1037 |
// printf("paperoga \n"); |
1038 |
row=result->Next(); |
1039 |
// |
1040 |
}; |
1041 |
|
1042 |
} |
1043 |
|
1044 |
//save in Table_ROOT_Good or in Table_ROOT_Bad |
1045 |
bool PacketUser::saveROOT_DB(const char* table_name, const char* folder_name, const char* file_name, |
1046 |
unsigned long int pkt_number_in, unsigned long int pkt_number_fin, unsigned long int obt_in, |
1047 |
unsigned long int obt_fin, unsigned long int oT_sync, unsigned long int lT_sync_info, unsigned long int mtime_init, |
1048 |
unsigned long int mtime_last, unsigned long int mboot_num, unsigned long int time_offset, int bad_pkt, |
1049 |
int bad_pkt_read, int bad_pkt_CalRead, int num_PKT_Saved, const char* nome_input, bool _time_is_estimated) { |
1050 |
//TODO: forse far ritornare ID della cosa appena inserita se e' andatato tutto ok invece che true false? |
1051 |
|
1052 |
// EM: check is file has already been inserted, if so, update the informations (also in ROOT_TABLE_MERGING if processing ROOT_TABLE) instead of adding entries |
1053 |
stringstream oss1; |
1054 |
oss1.str() = ""; |
1055 |
string msg1; |
1056 |
stringstream ass; |
1057 |
ass.str(""); |
1058 |
ass << "SELECT 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 FROM " << table_name << " WHERE FILE_NAME='"<< file_name <<"';"; |
1059 |
TSQLResult* ares = NULL; |
1060 |
ares = sqlServer->Query(ass.str().c_str()); |
1061 |
if (!ares) { |
1062 |
oss1 << "DBError UNABLE to: " << ass.str().c_str() << endl; |
1063 |
msg1 = oss1.str(); |
1064 |
mainLogUtil->logError(msg1); |
1065 |
return false; |
1066 |
} |
1067 |
TSQLRow *arow = ares->Next(); |
1068 |
if ( arow ){ |
1069 |
TString bla=file_name; |
1070 |
TString *blabla=new TString(file_name); |
1071 |
blabla->Remove(15); |
1072 |
stringstream oss; |
1073 |
oss.str(""); |
1074 |
// oss << "UPDATE " << table_name |
1075 |
// << " set FOLDER_NAME='" << folder_name << "', PKT_NUMBER_INIT='" << pkt_number_in << "',PKT_NUMBER_FINAL='" << pkt_number_fin << "',PKT_OBT_INIT='" << obt_in << "',PKT_OBT_FINAL='" << obt_fin |
1076 |
// << "',OBT_TIME_SYNC='" << oT_sync << "',LAST_TIME_SYNC_INFO='" << lT_sync_info << "',REAL_TIME_INIT='"<< mtime_init << "',REAL_TIME_LAST='" << mtime_last << "',BOOT_NUMBER='" << mboot_num |
1077 |
// << "',TIME_OFFSET='" << time_offset << "',BAD_PKT='" << bad_pkt << "',BAD_PKT_READ='"<< bad_pkt_read << "',BAD_PKT_CALREAD='" << bad_pkt_CalRead << "',NUM_PKT_SAVED='" << num_PKT_Saved |
1078 |
// << "',INPUT_NAME='" << nome_input << "',INSERT_TIME=NULL,TIME_IS_ESTIMATED='"<< _time_is_estimated << "',INSERTED_BY='" << tag_value << "',ON_DISK='YES' where FILE_NAME='"<<file_name<<"';"; |
1079 |
oss << " DELETE FROM ROOT_TABLE_BAD WHERE FILE_NAME LIKE '"<<blabla->Data()<<"%';"; |
1080 |
string msg = oss.str(); |
1081 |
mainLogUtil->logAll(msg); |
1082 |
string query; |
1083 |
|
1084 |
TSQLResult* res = NULL; |
1085 |
|
1086 |
query = oss.str(); |
1087 |
msg1 = "DeleteROOT_DB_BAD query: "; |
1088 |
msg1 += query; |
1089 |
mainLogUtil->logInfo(msg1); |
1090 |
|
1091 |
res = sqlServer->Query(query.c_str()); |
1092 |
if (!res) { |
1093 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1094 |
msg1 = oss1.str(); |
1095 |
mainLogUtil->logError(msg1); |
1096 |
return false; |
1097 |
} |
1098 |
|
1099 |
stringstream iss; |
1100 |
iss.str(""); |
1101 |
iss << "select ROOT_TABLE_MERGING.ID_N from ROOT_TABLE_MERGING left join ROOT_TABLE on ROOT_TABLE_MERGING.ROOT_ID_N=ROOT_TABLE.ID_N where ROOT_TABLE.FILE_NAME like '"<<blabla->Data()<<"%';"; |
1102 |
string msg2; |
1103 |
string query2; |
1104 |
TSQLResult* res2 = NULL; |
1105 |
query2 = iss.str(); |
1106 |
msg2 = "SaveROOT_DB query: "; |
1107 |
msg2 += query; |
1108 |
mainLogUtil->logInfo(msg2); |
1109 |
res2 = sqlServer->Query(query2.c_str()); |
1110 |
if (!res2) { |
1111 |
oss1 << "DBError UNABLE to: " << iss.str().c_str() << endl; |
1112 |
msg1 = oss1.str(); |
1113 |
mainLogUtil->logError(msg1); |
1114 |
return false; |
1115 |
} |
1116 |
TSQLRow *row2 = res2->Next(); |
1117 |
while ( row2 ){ |
1118 |
TString qq=Form("DELETE FROM ROOT_TABLE_MERGING where ID_N=%s;",row2->GetField(0)); |
1119 |
TSQLResult* res2b = NULL; |
1120 |
res2b = sqlServer->Query(qq.Data()); |
1121 |
delete res2b; |
1122 |
row2=res2->Next(); |
1123 |
}; |
1124 |
|
1125 |
|
1126 |
|
1127 |
oss.str(""); |
1128 |
oss << " DELETE FROM ROOT_TABLE WHERE FILE_NAME LIKE '"<<blabla->Data()<<"%';"; |
1129 |
string ms = oss.str(); |
1130 |
mainLogUtil->logAll(ms); |
1131 |
string quer; |
1132 |
TSQLResult* re = NULL; |
1133 |
quer = oss.str(); |
1134 |
msg1 = "DeleteROOT_DB query: "; |
1135 |
msg1 += quer; |
1136 |
mainLogUtil->logInfo(msg1); |
1137 |
|
1138 |
re = sqlServer->Query(quer.c_str()); |
1139 |
if (!re) { |
1140 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1141 |
msg1 = oss1.str(); |
1142 |
mainLogUtil->logError(msg1); |
1143 |
return false; |
1144 |
} |
1145 |
|
1146 |
delete blabla; |
1147 |
|
1148 |
} |
1149 |
|
1150 |
// } else { |
1151 |
if ( true ){ |
1152 |
stringstream oss; |
1153 |
oss.str(""); |
1154 |
oss << "INSERT INTO " << table_name |
1155 |
<< " (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)" |
1156 |
<< " VALUES ('" << 0 << "','" << folder_name << "','" << file_name << "','" << pkt_number_in << "','" |
1157 |
<< pkt_number_fin << "','" << obt_in << "','" << obt_fin << "','" << oT_sync << "','" << lT_sync_info << "','" |
1158 |
<< mtime_init << "','" << mtime_last << "','" << mboot_num << "','" << time_offset << "','" << bad_pkt << "','" |
1159 |
<< bad_pkt_read << "','" << bad_pkt_CalRead << "','" << num_PKT_Saved << "','" << nome_input << "'," << "NULL," |
1160 |
<< _time_is_estimated << ",'" << tag_value << "'" << ");"; |
1161 |
string msg = oss.str(); |
1162 |
mainLogUtil->logAll(msg); |
1163 |
|
1164 |
string query; |
1165 |
|
1166 |
TSQLResult* res = NULL; |
1167 |
|
1168 |
query = oss.str(); |
1169 |
msg1 = "SaveROOT_DB query: "; |
1170 |
msg1 += query; |
1171 |
mainLogUtil->logInfo(msg1); |
1172 |
|
1173 |
res = sqlServer->Query(query.c_str()); |
1174 |
if (!res) { |
1175 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1176 |
msg1 = oss1.str(); |
1177 |
mainLogUtil->logError(msg1); |
1178 |
return false; |
1179 |
} else { |
1180 |
delete res; |
1181 |
res = NULL; |
1182 |
} |
1183 |
} |
1184 |
|
1185 |
if(!strcmp(table_name,Table_ROOT_Good)) { |
1186 |
//the ID of the current ROOT file in table Table_ROOT_Good |
1187 |
my_id = select_maxIDN_DB(table_name,file_name); |
1188 |
if (!boot_number) { |
1189 |
if (id_to_recover_index < 1000) |
1190 |
id_to_recover[id_to_recover_index++] = my_id; |
1191 |
} |
1192 |
else if (boot_number && id_to_recover_index && !is_new_route) { |
1193 |
recover_boot_number(); |
1194 |
} |
1195 |
// cout<<"DBG: my_id = "<<my_id<<endl; |
1196 |
} |
1197 |
if (ares) { |
1198 |
delete ares; |
1199 |
ares = NULL; |
1200 |
} |
1201 |
return true; |
1202 |
} |
1203 |
|
1204 |
void PacketUser::recover_boot_number() { |
1205 |
string msg1; |
1206 |
stringstream oss1; |
1207 |
stringstream oss; |
1208 |
oss.str(""); |
1209 |
string query = ""; |
1210 |
TSQLResult* res = NULL; |
1211 |
if (!boot_number || !id_to_recover_index) |
1212 |
return; |
1213 |
|
1214 |
for (int i = 0; i < id_to_recover_index; i++) { |
1215 |
oss.str(""); |
1216 |
oss << "UPDATE " << Table_ROOT_Good << " SET BOOT_NUMBER=" << boot_number << " WHERE ID_N=" << id_to_recover[i] |
1217 |
<< ";"; |
1218 |
query = oss.str(); |
1219 |
res = sqlServer->Query(query.c_str()); |
1220 |
if (!res) { |
1221 |
oss1 << "DBError UNABLE to: " << query.c_str() << endl; |
1222 |
msg1 = oss1.str(); |
1223 |
mainLogUtil->logError(msg1); |
1224 |
return; |
1225 |
} |
1226 |
} |
1227 |
|
1228 |
for (int i = 0; i < id_to_recover_index; i++) { |
1229 |
oss.str(""); |
1230 |
oss << "UPDATE " << Table_ROOT_Merging << " SET BOOT_NUMBER=" << boot_number << " WHERE ROOT_ID_N=" |
1231 |
<< id_to_recover[i] << ";"; |
1232 |
query = oss.str(); |
1233 |
res = sqlServer->Query(query.c_str()); |
1234 |
if (!res) { |
1235 |
oss1 << "DBError UNABLE to: " << query.c_str() << endl; |
1236 |
msg1 = oss1.str(); |
1237 |
mainLogUtil->logError(msg1); |
1238 |
return; |
1239 |
} |
1240 |
} |
1241 |
|
1242 |
id_to_recover_index = 0; |
1243 |
} |
1244 |
|
1245 |
/**********************************************************************************************/ |
1246 |
/*###########################################################################################################*/ |
1247 |
/********************************** MERGING *************************************************/ |
1248 |
/*###########################################################################################################*/ |
1249 |
// Merge ROT files: find other ROOT files in the same temporal ranges |
1250 |
// it finds ROOT files that covers particular temporal range of interest and save this info in DB |
1251 |
/**********************************************************************************************/ |
1252 |
//try to merge current ROOT file with files in DB |
1253 |
bool PacketUser::merge_ROOTfiles() { |
1254 |
stringstream log; |
1255 |
log.str(""); |
1256 |
string slog; |
1257 |
|
1258 |
mainLogUtil->logInfo("Trying merging ROOT files"); |
1259 |
double percentage = (double) (bad_pkt + bad_pkt_EventReader + bad_pkt_CalibReader) / numPKTSaved; |
1260 |
|
1261 |
TSQLResult* res = NULL; |
1262 |
TSQLRow* row = NULL; |
1263 |
//numero di ROOT files trovati sul DB che hanno relaz di tipo dato |
1264 |
unsigned int num_rows = 0; |
1265 |
|
1266 |
//Record su DB: |
1267 |
unsigned int idN = 0; |
1268 |
unsigned int root_id_db = 0; |
1269 |
unsigned long int pkt_num_in_db = 0; |
1270 |
unsigned long int pkt_num_fin_db = 0; |
1271 |
unsigned long int pkt_obt_in_db = 0; |
1272 |
unsigned long int pkt_obt_fin_db = 0; |
1273 |
unsigned long int time_in_db = 0; |
1274 |
unsigned long int time_fin_db = 0; |
1275 |
unsigned int boot_num_db = 0; |
1276 |
double perc = 0; |
1277 |
|
1278 |
//marco_new: non considero mai caso di continuita'(mio finale=DB iniz o viceversa), non mi interessa |
1279 |
//marco_new: after e before contengono anche smaller ma con un bordo comune |
1280 |
for (int relaz = AFTER; relaz <= BIGGER; relaz++) { |
1281 |
//marco_new: esci se sono arrivato a zero |
1282 |
if (real_time_last == real_time_init) { |
1283 |
log.str(""); |
1284 |
log << "Current File Lenght=0; real_time_last=real_time_init= " << real_time_last; |
1285 |
slog = log.str(); |
1286 |
mainLogUtil->logAll(slog); |
1287 |
mainLogUtil->logInfo("Finish merging ROOT files"); |
1288 |
if (res) { |
1289 |
delete res; |
1290 |
res = NULL; |
1291 |
} |
1292 |
if (row) { |
1293 |
delete row; |
1294 |
row = NULL; |
1295 |
} |
1296 |
return true; |
1297 |
} |
1298 |
|
1299 |
num_rows = 0; |
1300 |
res = Select_merging(Table_ROOT_Merging, real_time_init, real_time_last, (type_Rel_ROOT) relaz); |
1301 |
|
1302 |
if (!res) { |
1303 |
mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged RES=null"); |
1304 |
return false; |
1305 |
} |
1306 |
|
1307 |
num_rows = res->GetRowCount(); |
1308 |
log.str(""); |
1309 |
log << "Found " << num_rows << " ROOT file in DB with relation of kind = " << relaz; |
1310 |
slog = log.str(); |
1311 |
mainLogUtil->logAll(slog); |
1312 |
|
1313 |
if (num_rows > 0) { |
1314 |
for (unsigned int i = 0; i < num_rows; i++) { |
1315 |
row = res->Next(); |
1316 |
if (!row) { |
1317 |
mainLogUtil->logError("DB_ERROR: Unable to find ROOT files to be merged. ROW=null"); |
1318 |
if (res) { |
1319 |
delete res; |
1320 |
res = NULL; |
1321 |
} |
1322 |
return false; |
1323 |
} |
1324 |
|
1325 |
//Leggi Record dal DB: |
1326 |
idN = atoll(row->GetField(0)); |
1327 |
root_id_db = atoll(row->GetField(1)); |
1328 |
pkt_num_in_db = atoll(row->GetField(2)); |
1329 |
pkt_num_fin_db = atoll(row->GetField(3)); |
1330 |
pkt_obt_in_db = atoll(row->GetField(4)); |
1331 |
pkt_obt_fin_db = atoll(row->GetField(5)); |
1332 |
time_in_db = atoll(row->GetField(6)); |
1333 |
time_fin_db = atoll(row->GetField(7)); |
1334 |
boot_num_db = atoll(row->GetField(8)); |
1335 |
perc = atof(row->GetField(9)); |
1336 |
|
1337 |
log.str(""); |
1338 |
log << "*** ROOT file found in DB has idN= " << idN << " root_id_db= " << root_id_db; |
1339 |
log << " pkt_num_in_db= " << pkt_num_in_db << " pkt_num_fin_db= " << pkt_num_fin_db << " pkt_obt_in_db= " |
1340 |
<< pkt_obt_in_db << " pkt_obt_fin_db= " << pkt_obt_fin_db; |
1341 |
log << " time_in_db= " << time_in_db << " time_fin_db= " << time_fin_db << " boot_num_db= " << boot_num_db |
1342 |
<< " perc= " << perc << " ***"; |
1343 |
log << "\n*** Current ROOT file has pkt_number_init= " << pkt_number_init << " obt_init= " << obt_init |
1344 |
<< " real_time_init= " << real_time_init; |
1345 |
log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= " |
1346 |
<< real_time_last << " ***"; |
1347 |
slog = log.str(); |
1348 |
mainLogUtil->logAll(slog); |
1349 |
if (perc > percentage) { |
1350 |
mainLogUtil->logInfo("Current ROOT file is better than the one in DB"); |
1351 |
//modifica DB |
1352 |
updateMergeROOT_DB(Table_ROOT_Merging, root_id_db, pkt_num_in_db, pkt_num_fin_db, pkt_obt_in_db, |
1353 |
pkt_obt_fin_db, time_in_db, time_fin_db, boot_num_db, perc, idN, (type_Rel_ROOT) relaz); |
1354 |
} |
1355 |
else { |
1356 |
mainLogUtil->logInfo("Found ROOT file in DB is better than current ROOT file"); |
1357 |
if (relaz == AFTER) { |
1358 |
mainLogUtil->logInfo("Relaz:AFTER = Changing Init values of the Current ROOT file"); |
1359 |
pkt_number_init = (pkt_num_fin_db + 1); |
1360 |
obt_init = pkt_obt_fin_db; |
1361 |
real_time_init = time_fin_db; |
1362 |
log.str(""); |
1363 |
log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= " |
1364 |
<< obt_init << " real_time_init= " << real_time_init; |
1365 |
log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= " |
1366 |
<< real_time_last << " ***"; |
1367 |
slog = log.str(); |
1368 |
mainLogUtil->logAll(slog); |
1369 |
} |
1370 |
else if (relaz == BEFORE) { |
1371 |
mainLogUtil->logInfo("Relaz:BEFORE = Changing Last values of the Current ROOT file"); |
1372 |
pkt_number_last = (pkt_num_in_db - 1); |
1373 |
obt_last = pkt_obt_in_db; |
1374 |
real_time_last = time_in_db; |
1375 |
|
1376 |
log.str(""); |
1377 |
log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= " |
1378 |
<< obt_init << " real_time_init= " << real_time_init; |
1379 |
log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= " |
1380 |
<< real_time_last << " ***"; |
1381 |
slog = log.str(); |
1382 |
mainLogUtil->logAll(slog); |
1383 |
} |
1384 |
else if (relaz == SMALLER) { |
1385 |
//non devo proprio salvarlo ed esco ... |
1386 |
mainLogUtil->logInfo("Relaz:SMALLER = Nothing to save"); |
1387 |
mainLogUtil->logInfo("\nFinish merging ROOT files"); |
1388 |
if (res) { |
1389 |
delete res; |
1390 |
res = NULL; |
1391 |
} |
1392 |
if (row) { |
1393 |
delete row; |
1394 |
row = NULL; |
1395 |
} |
1396 |
return true; |
1397 |
} |
1398 |
else if (relaz == BIGGER)//spezzettamento |
1399 |
{ |
1400 |
mainLogUtil->logInfo("Relaz:BIGGER = (1st part) Saving the first part of the Current ROOT file"); |
1401 |
saveMergeROOT_DB(Table_ROOT_Merging, my_id, pkt_number_init, (pkt_num_in_db - 1), obt_init, pkt_obt_in_db, |
1402 |
real_time_init, time_in_db, boot_number, percentage); |
1403 |
|
1404 |
mainLogUtil->logInfo("Relaz:BIGGER = (2nd part)Changing Init values of the Current ROOT file"); |
1405 |
pkt_number_init = (pkt_num_fin_db + 1); |
1406 |
obt_init = pkt_obt_fin_db; |
1407 |
real_time_init = time_fin_db; |
1408 |
|
1409 |
log.str(""); |
1410 |
log << "\n*** Current ROOT file becomes: pkt_number_init= " << pkt_number_init << " obt_init= " |
1411 |
<< obt_init << " real_time_init= " << real_time_init; |
1412 |
log << " pkt_number_last= " << pkt_number_last << " obt_last= " << obt_last << " real_time_last= " |
1413 |
<< real_time_last << " ***"; |
1414 |
slog = log.str(); |
1415 |
mainLogUtil->logAll(slog); |
1416 |
}//if relaz |
1417 |
}//if perc |
1418 |
}//for num_rows |
1419 |
}//if num_rows |
1420 |
|
1421 |
if (res) { |
1422 |
delete res; |
1423 |
res = NULL; |
1424 |
} |
1425 |
if (row) { |
1426 |
delete row; |
1427 |
row = NULL; |
1428 |
} |
1429 |
|
1430 |
}//for relaz |
1431 |
|
1432 |
mainLogUtil->logInfo("Saving Current ROOT file"); |
1433 |
saveMergeROOT_DB(Table_ROOT_Merging, my_id, pkt_number_init, pkt_number_last, obt_init, obt_last, real_time_init, |
1434 |
real_time_last, boot_number, percentage); |
1435 |
|
1436 |
mainLogUtil->logInfo("Finish merging ROOT files"); |
1437 |
return true; |
1438 |
|
1439 |
} |
1440 |
|
1441 |
//unsigned int root_id, percentage non le devo modificare |
1442 |
bool PacketUser::updateMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, |
1443 |
unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, |
1444 |
unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double bad_perc, |
1445 |
unsigned int ID_record, type_Rel_ROOT type_rel) { |
1446 |
|
1447 |
stringstream oss; |
1448 |
oss.str(""); |
1449 |
|
1450 |
if (type_rel == AFTER) { |
1451 |
mainLogUtil->logInfo("Relaz:AFTER = Updating Last values of the DB ROOT file"); |
1452 |
//marco_new:tolti tutti gli apici che qui non devono essere |
1453 |
|
1454 |
int good = 1; |
1455 |
if ( pkt_number_in >= (pkt_number_init - 1) && abs((int)(pkt_number_in-(pkt_number_init-1)))<10 ) good = 0; |
1456 |
|
1457 |
oss << "UPDATE " << table_name << " SET PKT_NUMBER_FINAL =" << (pkt_number_init - 1) << ", PKT_OBT_FINAL =" |
1458 |
<< obt_init << ", REAL_TIME_LAST =" << real_time_init << ", GOOD="<< good << " WHERE ID_N =" << ID_record << ";"; |
1459 |
|
1460 |
} |
1461 |
else if (type_rel == BEFORE) { |
1462 |
mainLogUtil->logInfo("Relaz:BEFORE = Updating Init values of the DB ROOT file"); |
1463 |
|
1464 |
int good = 1; |
1465 |
if ( (pkt_number_last+1) >= pkt_number_fin && abs((int)((pkt_number_last+1)-pkt_number_fin))<10 ) good = 0; |
1466 |
|
1467 |
oss << "UPDATE " << table_name << " SET PKT_NUMBER_INIT =" << (pkt_number_last + 1) << ", PKT_OBT_INIT =" |
1468 |
<< obt_last << ", REAL_TIME_INIT =" << real_time_last << ", GOOD="<< good << " WHERE ID_N =" << ID_record << ";"; |
1469 |
} |
1470 |
else if (type_rel == SMALLER) { |
1471 |
//spezzettamentento |
1472 |
mainLogUtil->logInfo("Relaz:SMALLER = (1st part) Updating Last values of the DB ROOT file"); |
1473 |
|
1474 |
int good = 1; |
1475 |
if ( pkt_number_in >= (pkt_number_init - 1) && abs((int)(pkt_number_in-(pkt_number_init-1)))<10 ) good = 0; |
1476 |
|
1477 |
oss << "UPDATE " << table_name << " SET PKT_NUMBER_FINAL=" << (pkt_number_init - 1) << ", PKT_OBT_FINAL =" |
1478 |
<< obt_init << ", REAL_TIME_LAST =" << real_time_init << ", GOOD="<< good << " WHERE ID_N =" << ID_record << ";"; |
1479 |
|
1480 |
mainLogUtil->logInfo("Relaz:SMALLER = (2nd part) Saving the second part of the DB ROOT file"); |
1481 |
|
1482 |
saveMergeROOT_DB(table_name, root_id, (pkt_number_last + 1), pkt_number_fin, obt_last, obt_fin, real_time_last, |
1483 |
mtime_last, mboot_num, bad_perc); |
1484 |
} |
1485 |
else if (type_rel == BIGGER) { |
1486 |
//marco_new: ok anche se coincidenti etc |
1487 |
// oss << "DELETE FROM "<< table_name <<" WHERE ID_N ="<< ID_record<<";"; // EMILIANO |
1488 |
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 |
1489 |
mainLogUtil->logInfo("Record deleted from merging table"); |
1490 |
} |
1491 |
else |
1492 |
return false; |
1493 |
|
1494 |
string msg = oss.str(); |
1495 |
mainLogUtil->logInfo(msg); |
1496 |
|
1497 |
stringstream oss1; |
1498 |
oss1.str() = ""; |
1499 |
string msg1; |
1500 |
|
1501 |
TSQLResult* res = NULL; |
1502 |
res = sqlServer->Query(oss.str().c_str()); |
1503 |
if (!res) { |
1504 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1505 |
msg1 = oss1.str(); |
1506 |
mainLogUtil->logError(msg1); |
1507 |
return false; |
1508 |
} |
1509 |
if (res) { |
1510 |
delete res; |
1511 |
res = NULL; |
1512 |
} |
1513 |
return true; |
1514 |
} |
1515 |
|
1516 |
//cerca eventuali ROOT files in DB che possono venire mergiati |
1517 |
//marco_new bordi: messo in after a before casi in cui smaller con un bordo in comune |
1518 |
//marco_new bordi: messo in bigger casi con bordi in comune; tra questi anche caso particolare di coincidente |
1519 |
//marco_new bordi: smaller resta solo caso in cui DB e' piu largo sia a dx che a sx |
1520 |
TSQLResult* PacketUser::Select_merging(const char* table_name, unsigned long int mtime_init, unsigned long int mtime_last, |
1521 |
type_Rel_ROOT type_rel) { |
1522 |
TSQLResult* res = NULL; |
1523 |
stringstream query; |
1524 |
query.str(""); |
1525 |
|
1526 |
if (type_rel == AFTER) |
1527 |
query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_LAST > " << mtime_init |
1528 |
<< " and REAL_TIME_LAST <= " << mtime_last << " and REAL_TIME_INIT < " << mtime_init; // EMI |
1529 |
else if (type_rel == BEFORE) |
1530 |
query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT >= " << mtime_init |
1531 |
<< " and REAL_TIME_INIT < " << mtime_last << " and REAL_TIME_LAST > " << mtime_last; // EMI |
1532 |
else if (type_rel == SMALLER) |
1533 |
query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT < " << mtime_init |
1534 |
<< " and REAL_TIME_LAST > " << mtime_last; // EMI |
1535 |
else if (type_rel == BIGGER) |
1536 |
query << "select * from " << table_name << " where GOOD=1 and REAL_TIME_INIT >= " << mtime_init |
1537 |
<< " and REAL_TIME_LAST <= " << mtime_last; // EMI |
1538 |
else |
1539 |
return 0; |
1540 |
|
1541 |
query << " order by REAL_TIME_INIT ASC;";//marco_new: inutile |
1542 |
//se volessi mettere un filtro sulla qualita' |
1543 |
// query<<" and NUM_PKT_SAVED - BAD_PKT_CALREAD - BAD_PKT_READ - BAD_PKT >= 2" ; |
1544 |
string msg = query.str(); |
1545 |
mainLogUtil->logAll(msg); |
1546 |
res = sqlServer->Query(query.str().c_str()); |
1547 |
stringstream oss1; |
1548 |
oss1.str() = ""; |
1549 |
string msg1; |
1550 |
|
1551 |
if (!res) { |
1552 |
oss1 << "DBError UNABLE to: " << query.str().c_str() << endl; |
1553 |
msg1 = oss1.str(); |
1554 |
mainLogUtil->logError(msg1); |
1555 |
} |
1556 |
|
1557 |
return res; |
1558 |
} |
1559 |
|
1560 |
bool PacketUser::saveMergeROOT_DB(const char* table_name, unsigned int root_id, unsigned long int pkt_number_in, |
1561 |
unsigned long int pkt_number_fin, unsigned long int obt_in, unsigned long int obt_fin, |
1562 |
unsigned long int mtime_init, unsigned long int mtime_last, unsigned long int mboot_num, double percentage) { |
1563 |
|
1564 |
int good = 1; |
1565 |
if ( pkt_number_in >= pkt_number_fin && abs((int)(pkt_number_fin-pkt_number_in))<10 ) good = 0; |
1566 |
|
1567 |
stringstream oss; |
1568 |
oss.str(""); |
1569 |
oss << "INSERT INTO " << table_name |
1570 |
<< " (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,GOOD)" |
1571 |
<< " VALUES ('" << 0 << "','" << root_id << "','" << pkt_number_in << "','" << pkt_number_fin << "','" << obt_in |
1572 |
<< "','" << obt_fin << "','" << mtime_init << "','" << mtime_last << "','" << mboot_num << "','" << percentage |
1573 |
<< "'," << "NULL" << ",'" << tag_value << "','" << good <<"');"; |
1574 |
|
1575 |
string msg = oss.str(); |
1576 |
mainLogUtil->logAll(msg); |
1577 |
|
1578 |
//marco_new: controlla |
1579 |
//marco_new: esci se sono arrivato a zero |
1580 |
// if(real_time_last==real_time_init){ |
1581 |
if (mtime_init == mtime_last) { |
1582 |
mainLogUtil->logAll("Nothing to be saved"); |
1583 |
|
1584 |
// if(res){delete res; res = NULL;} |
1585 |
// if(row){delete row; row = NULL;} |
1586 |
return true; |
1587 |
} |
1588 |
|
1589 |
stringstream oss1; |
1590 |
oss1.str() = ""; |
1591 |
string msg1; |
1592 |
|
1593 |
TSQLResult* res = NULL; |
1594 |
res = sqlServer->Query(oss.str().c_str()); |
1595 |
if (!res) { |
1596 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1597 |
msg1 = oss1.str(); |
1598 |
mainLogUtil->logError(msg1); |
1599 |
return false; |
1600 |
} |
1601 |
if (res) { |
1602 |
delete res; |
1603 |
res = NULL; |
1604 |
} |
1605 |
return true; |
1606 |
} |
1607 |
|
1608 |
//Utility: return max(IDN) |
1609 |
unsigned int PacketUser::select_maxIDN_DB(const char* table_name, const char* file_name) { |
1610 |
|
1611 |
if (!strcmp(table_name, Table_ROOT_Bad)) { |
1612 |
return 0; |
1613 |
} |
1614 |
unsigned int idN = 0; |
1615 |
TSQLResult* res = NULL; |
1616 |
stringstream oss; |
1617 |
oss.str(""); |
1618 |
oss << "select ID_N from " << table_name << " where FILE_NAME='" << file_name <<"';"; |
1619 |
// string msg = oss.str(); |
1620 |
// mainLogUtil->logAll(msg); |
1621 |
res = sqlServer->Query(oss.str().c_str()); |
1622 |
|
1623 |
stringstream oss1; |
1624 |
oss1.str() = ""; |
1625 |
string msg1; |
1626 |
|
1627 |
if (!res) { |
1628 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1629 |
msg1 = oss1.str(); |
1630 |
mainLogUtil->logError(msg1); |
1631 |
return 0; |
1632 |
} |
1633 |
|
1634 |
TSQLRow* row = NULL; |
1635 |
row = res->Next(); |
1636 |
if (!row) { |
1637 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1638 |
msg1 = oss1.str(); |
1639 |
mainLogUtil->logError(msg1); |
1640 |
return 0; |
1641 |
} |
1642 |
|
1643 |
idN = atoi(row->GetField(0)); |
1644 |
if (row) { |
1645 |
delete row; |
1646 |
row = NULL; |
1647 |
} |
1648 |
if (res) { |
1649 |
delete res; |
1650 |
res = NULL; |
1651 |
} |
1652 |
|
1653 |
oss << ". idN = " << idN; |
1654 |
string msg = oss.str(); |
1655 |
mainLogUtil->logAll(msg); |
1656 |
|
1657 |
return idN; |
1658 |
} |
1659 |
|
1660 |
/** |
1661 |
* Lock par=table, if par=null lock all tables |
1662 |
*/ |
1663 |
int PacketUser::LockTables(const char* tableTobeLocked) { |
1664 |
//se non ho il DB :) |
1665 |
if (!do_cont_check) |
1666 |
return 999; |
1667 |
|
1668 |
TSQLResult* res = NULL; |
1669 |
stringstream oss; |
1670 |
oss.str(""); |
1671 |
// if (!tableTobeLocked) |
1672 |
oss << "lock table " << Table_ROOT_Good << " write, " << Table_ROOT_Bad << " write, " << Table_GL_RESURS_OFFSET |
1673 |
<< " write, " << Table_ROOT_Merging << " write;"; |
1674 |
// else |
1675 |
// oss << "lock table " << tableTobeLocked << " write; "; |
1676 |
|
1677 |
string msg = oss.str(); |
1678 |
mainLogUtil->logAll(msg); |
1679 |
|
1680 |
res = sqlServer->Query(oss.str().c_str()); |
1681 |
|
1682 |
stringstream oss1; |
1683 |
oss1.str() = ""; |
1684 |
string msg1; |
1685 |
|
1686 |
if (!res) { |
1687 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1688 |
msg1 = oss1.str(); |
1689 |
mainLogUtil->logError(msg1); |
1690 |
return 1; |
1691 |
} |
1692 |
|
1693 |
if (res) { |
1694 |
delete res; |
1695 |
res = NULL; |
1696 |
} |
1697 |
mainLogUtil->logAll("TABLES LOCKED"); |
1698 |
return 0; |
1699 |
} |
1700 |
|
1701 |
/** |
1702 |
* UNLock tables |
1703 |
*/ |
1704 |
int PacketUser::UnLockTables() { |
1705 |
//se non ho il DB :) |
1706 |
if (!do_cont_check) |
1707 |
return 999; |
1708 |
|
1709 |
TSQLResult* res = NULL; |
1710 |
stringstream oss; |
1711 |
oss.str(""); |
1712 |
oss << "unlock tables;"; |
1713 |
|
1714 |
string msg = oss.str(); |
1715 |
mainLogUtil->logAll(msg); |
1716 |
|
1717 |
res = sqlServer->Query(oss.str().c_str()); |
1718 |
|
1719 |
stringstream oss1; |
1720 |
oss1.str() = ""; |
1721 |
string msg1; |
1722 |
|
1723 |
if (!res) { |
1724 |
oss1 << "DBError UNABLE to: " << oss.str().c_str() << endl; |
1725 |
msg1 = oss1.str(); |
1726 |
mainLogUtil->logError(msg1); |
1727 |
return 1; |
1728 |
} |
1729 |
|
1730 |
if (res) { |
1731 |
delete res; |
1732 |
res = NULL; |
1733 |
} |
1734 |
mainLogUtil->logAll("TABLES UNLOCKED"); |
1735 |
|
1736 |
return 0; |
1737 |
} |
1738 |
|
1739 |
/*********************************************************/ |
1740 |
//marco_new_31 |
1741 |
void PacketUser::OpenDBConnection(const char* tableTobeLocked) { |
1742 |
if (do_cont_check) { |
1743 |
//controllo che non sia gia' aperta |
1744 |
if (!sqlServer) { |
1745 |
sqlServer = TSQLServer::Connect(pelosconnection, db_user, db_pwd); |
1746 |
if ((!sqlServer) || (!(sqlServer->IsConnected()))) { |
1747 |
cout << "Can not connect with MYSQL sever" << endl; |
1748 |
} |
1749 |
mainLogUtil->logAll("Connected to DB"); |
1750 |
stringstream myquery; |
1751 |
myquery.str(""); |
1752 |
myquery << "SET time_zone='+0:00'"; |
1753 |
sqlServer->Query(myquery.str().c_str()); |
1754 |
//blocca le tabelle |
1755 |
LockTables(tableTobeLocked); |
1756 |
} |
1757 |
} |
1758 |
|
1759 |
} |
1760 |
|
1761 |
//marco_new_31 |
1762 |
void PacketUser::CloseDBConnection() { |
1763 |
if (do_cont_check) { |
1764 |
//controllo che ce ne sia una aperta |
1765 |
if (sqlServer && sqlServer->IsConnected()) { |
1766 |
//marco_new: sblocca tutte le tabelle |
1767 |
UnLockTables(); |
1768 |
sqlServer->Close(); |
1769 |
delete sqlServer; |
1770 |
sqlServer = NULL; |
1771 |
mainLogUtil->logAll("Closed Connection to DB"); |
1772 |
} |
1773 |
} |
1774 |
} |
1775 |
|
1776 |
/***********************************************************************************************************************/ |
1777 |
/*#################################################### HIC SUNT LEONES ################################################*/ |
1778 |
/***********************************************************************************************************************/ |
1779 |
//UNUSED: dbg functions... |
1780 |
|
1781 |
//Return the system time in ms |
1782 |
unsigned long long PacketUser::Record_Time() { |
1783 |
timeval tv; |
1784 |
gettimeofday(&tv, NULL); |
1785 |
unsigned long long timems = (unsigned long long) tv.tv_sec * (unsigned long long) 1000 + tv.tv_usec / 1000; |
1786 |
return timems; |
1787 |
} |
1788 |
|
1789 |
//dbg_functions: |
1790 |
|
1791 |
//UNUSED. save a packet in a file |
1792 |
void PacketUser::savePKT_file(char* headerPkt, char* pamPkt, long int length, bool append, char* nomefile) { |
1793 |
char fno[80] = ""; |
1794 |
ofstream fout; |
1795 |
sprintf(fno, "%s/pkt%d_of_%s.pkt", gSystem->ExpandPathName(outDir), numPKTSaved, nomefile); // EMI |
1796 |
|
1797 |
if (append == true) |
1798 |
fout.open(fno, ios::binary | ios::app); |
1799 |
else |
1800 |
fout.open(fno, ios::binary); |
1801 |
|
1802 |
if (!fout) { |
1803 |
cout << "can not open output file " << fno << endl; |
1804 |
return; |
1805 |
} |
1806 |
fout.write(headerPkt, LENGTH_HEADER_PKT); |
1807 |
fout.write(pamPkt, length); |
1808 |
fout.close(); |
1809 |
} |
1810 |
|
1811 |
//UNUSED |
1812 |
void PacketUser::saveALL_PKT(char* headerPkt, char* pamPkt, long int length, bool append) { |
1813 |
char fno[80] = ""; |
1814 |
ofstream fout; |
1815 |
if (append == true) { |
1816 |
sprintf(fno, "%s/packets.pkt", gSystem->ExpandPathName(outDir)); // EMI |
1817 |
fout.open(fno, ios::binary | ios::app); |
1818 |
} |
1819 |
else { |
1820 |
sprintf(fno, "%s/packet%d.pkt", gSystem->ExpandPathName(outDir), numPKT); // EMI |
1821 |
fout.open(fno, ios::binary); |
1822 |
} |
1823 |
|
1824 |
if (!fout) { |
1825 |
cout << "can not open output file " << fno << endl; |
1826 |
return; |
1827 |
} |
1828 |
fout.write(headerPkt, LENGTH_HEADER_PKT); |
1829 |
fout.write(pamPkt, length); |
1830 |
fout.close(); |
1831 |
} |
1832 |
|
1833 |
} |