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

Contents of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations) (download)
Tue Feb 16 13:38:13 2010 UTC (14 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.13: +14 -3 lines
150 seconds bug fixed

1 //============================================================================
2 // $Id: PamOffLineSW_Main.cpp,v 1.13 2010/02/12 12:45:49 mocchiut Exp $
3 // Description : Pamela Off-Line Software
4 //============================================================================
5
6 #include <sys/time.h>
7 #include "PamInclude.h"
8 #include "StateManager.h"
9
10 #include "PacketUser.h"
11 #include <TArrayL64.h>
12
13 extern "C" {
14 #include <dirent.h>
15 }
16
17 using namespace PamOffLineSW;
18
19 // dbg purposes
20 bool exitdbg = false;
21 void exitDBG(){exitdbg=true;}
22 //function needed only for dbg purposes:
23 void split_input(char fni[]);
24
25 #define LENGTH_DATA_SIMU 1024
26 #define LENGTH_CADRE_VRL 1024
27 #define LENGTH_HEADER_VRL 8
28 #define LENGTH_DATA_CADRE (LENGTH_CADRE_VRL-LENGTH_HEADER_VRL-1)//1015
29
30 //main functions
31 void readOptions(int argc, char *argv[]);
32 void init();
33 void firstLog();
34 void DB_config();
35 void mainRead_RealData(char fni[]);
36 void mainRead_SimulatedData(char fni[]);
37 void finish();
38 void deleteAll();
39
40 //utilities functions
41 bool VRL_Header_Check(char * headVRL, int length);
42 short int CRC_Cadre_Check(char dataVRL[], int length);
43
44 //global variables
45 bool simulated_data = false;
46 //bool do_vrl_check = false; //EMI
47 bool do_vrl_check = true; //EMI
48 int cclung = 0;
49 Bool_t headok = true;
50 Bool_t crcok = true;
51
52 const char* db_host = "";
53 int db_port = 0;
54 const char* db_name = "";
55 char conn[100]="";
56
57 LogUtil::logLevel loglevel=LogUtil::LOGERROR;
58 const char* logfilename = "chewbacca.log";
59 //current route
60 int route = 999;
61 //previous route
62 int old_route = 999;
63
64 Long64_t filelength = 0LL;
65 Long64_t streamposi = 0LL;
66 Long64_t streamposisaved = 0LL;
67 Long64_t streamposiorig = 0LL;
68 Int_t numposi = 10;
69 Long64_t downcount = 1LL;
70 TArrayL64 *dwin=new TArrayL64(12);
71 Long64_t cadcount = 0LL;
72 Int_t horrorcount = 0;
73 Int_t hc = 0;
74
75 bool skip_cadre = false;
76 long int iNumGoodCadres=0; //total number of good cadres
77
78 // global variables used in all the project
79 namespace PamOffLineSW
80 {
81 const char* db_user = "";
82 const char* db_pwd = "";
83 char* pelosconnection = NULL;
84 //marco_new_01
85 bool single_connection=false;
86 long int iNumCadres=0; //cadre's number
87 unsigned long long int iByte_tot=0;// how many bytes I have read till now
88
89 bool isCadreGood = true;//if the cadre is good
90 char* fni; //path completo
91 short compression = 3;
92 // char *outDir = ".";
93 const char *outDir = "";
94
95 // char * nome_output="chewbacca"; //EMI
96 const char * nome_output="L0PAM";//EMI
97 bool multiFile = 0;
98 // unsigned long int step_pkt_number=0; // EMI
99 unsigned long int step_pkt_number=3000;
100 // unsigned long int step_pkt_obt=0; // EMI
101 unsigned long int step_pkt_obt=7200000; // EMI
102 LogUtil* mainLogUtil = NULL;
103 TSQLServer* sqlServer = NULL;
104 unsigned long int max_pkt_number = 16777215;//biggest value before reset
105 unsigned long int max_pkt_obt = 4294967295u;//biggest value before reset
106
107 // bool is_new_route = false; // EMI
108 bool is_new_route = true; // EMI ??????????? VA BENE? SECONDO ME SI`
109 unsigned int download = 0;
110 unsigned int orbit_number=0;
111 unsigned int mmm_number = 0;//session_number
112 unsigned long int time_Offset=0;
113 // bool tryMerge = false;
114 bool tryMerge = true;
115 bool candelete = true; //EM
116
117 bool do_cont_check=true;//if do_cont_check is false do not use a DB ...
118
119 //TODO: now it is unused
120 unsigned long int delta_Time=0;//in seconds
121 #define TAGVALUELEN 4
122 char tag_value[TAGVALUELEN];
123 }
124
125 // main:
126 //
127 int main(int argc, char *argv[])
128 {
129 // read command line options
130 readOptions(argc,argv);
131
132 cout<<"Please wait"<<endl;
133
134 // initialize everything and write few things in the log file
135 init();
136
137 // take time to measure the performance.
138 timeval tv1;
139 gettimeofday(&tv1,NULL);
140 unsigned long long timems1 = (unsigned long long)tv1.tv_sec * (unsigned long long)1000 + tv1.tv_usec / 1000;
141
142 if(simulated_data)
143 {
144 mainLogUtil->logAll("Using simulated data");
145 mainRead_SimulatedData(fni);
146 }
147 else
148 {
149 mainLogUtil->logAll("Using real data");
150 mainRead_RealData(fni);
151 }
152
153 finish();
154
155 timeval tv2;
156 gettimeofday(&tv2,NULL);
157 unsigned long long timems2 = (unsigned long long)tv2.tv_sec * (unsigned long long)1000 + tv2.tv_usec / 1000;
158 unsigned long long timems = timems2 -timems1;
159
160 string msg = "The analisys took: " + mainLogUtil->value2string(timems) + " ms";
161 mainLogUtil->logAlways(msg);
162
163 deleteAll();
164
165 cout<<"The end "<<endl;
166 return 0;
167 }
168
169 void readOptions(int argc, char *argv[])
170 {
171 if (argc < 2)
172 {
173 cout << "You have forgotten the file name. \n";
174 cout << "Try '-help' for more information. \n";
175 exit(1);
176 }
177
178 if((!strcmp(argv[1], "-help"))||(!strcmp(argv[1], "--help"))||(!strcmp(argv[1], "-h")))
179 {
180 // cout << "Usage: PamOffLineSW INPUT_FILE [OPTIONS] \n";
181 cout << "Usage: chewbacca INPUT_FILE [OPTIONS] \n";
182 cout << "\t (-help | --help | -h) print this help and exit \n";
183 cout << "\t -simu if the input file contains simulated data instead of real data\n";
184 cout << "\t -vrl if you want to perform the vrl check\n";
185 cout << "\t -no-vrl if you DO NOT want to perform the vrl check\n";
186 cout << "\t -gpamela gpamela data as input\n";
187 cout << "\t -filelog set the log filename. [default: chewbacca.log]\n";
188 cout << "\t -loglevel set the log level. Values: [0,3] (error,warning,info,all) [default:0]\n";
189 cout << "\t -c set the compression level for the generated ROOT file(s). Values: [0,9] [default = 3]\n";
190 cout << "\t -root_name set the root-name for the generated ROOT file(s). [default = chewbacca]\n";
191 cout << "\t -outDir set the output directory for the generated root file(s). [default = .]\n";
192 cout << "\t -delta_counter set the allowed difference in the Packet Counter between two continuos packets. If 0 all packets are considered continuos. [default = 0]\n";
193 cout << "\t -delta_obt set the allowed difference (ms) in the Packet Orbital Time between two continuos packets. If 0 all packets are considered continuos. [default = 0]\n";
194 cout << "\t -db_host set the host-name of the DataBase. [default = localhost]\n";
195 cout << "\t -db_port set the port of the DataBase. [default = 3306]\n";
196 cout << "\t -db_name set the name of the DataBase. [default = chewbacca_db]\n";
197 cout << "\t -db_user set the user of the DataBase. [default = chewbacca_user]\n";
198 cout << "\t -db_pwd set the user of the DataBase. [default = chewbacca_pwd]\n";
199 cout << "\t -max_pkt_number Maximum value for Packet Counter, after this value it is resetted. [default = 2^24 - 1]\n";
200 cout << "\t -max_pkt_obt Maximum value for Packet OBT, after this value it is resetted. [default = 2^32 - 1]\n";
201 cout << "\t -orbit_number Value of the orbital number. If 0 this is retrieved from the input file name. [default = 0]\n";
202 cout << "\t -session_number Value of the session number. If 0 this is retrieved from the input file name. [default = 0]\n";
203 cout << "\t -time_Offset Value of the timeOffset. If 0 this is retrieved using the orbital number. [default = 0]\n";
204 cout << "\t -tryMerge if you want to try to Merge ROOT files\n";
205 cout << "\t -dontMerge if you do not want to try to Merge ROOT files\n";
206 cout << "\t -dontDelete if you do not want to delete bad ROOT files from disks\n";
207 //marco_new_01
208 cout << "\t -single_connection if you want to open only one connection to DB.\n";
209 cout << "\t -tag if you want add the 'The owner of the file' - the tag lenght is 4 char\n";
210 // cout << "\t -delta_Time set the allowed difference (seconds) in the Real Time between two root file. [default = 0]\n";
211 //cout << "\t -multi generate multiple root files \n";
212 exit(1);
213 }
214
215 if(!strcmp(argv[1], "-usage")){
216 cout << "\t chewbacca INPUT_FILE [OPTIONS] \n";
217 cout << "Try '-help' for more information. \n";
218 exit(1);
219 }
220
221 //http://en.wikipedia.org/wiki/Chewbacca_defense
222 //http://www.urbandictionary.com/define.php?term=chewbacca+defense
223 if(!strcmp(argv[1], "-defense")){
224 cout << "\n Why would a Wookiee, an eight-foot tall Wookiee, want to live on Endor,\n with a bunch of two-foot tall Ewoks? That does not make sense!\n But more important, you have to ask yourself:\n What does this have to do with this case? \n Nothing. Ladies and gentlemen, it has nothing to do with this case! \n It does not make sense!\n Look at me. I'm a lawyer defending a major record company, \n and I'm talkin' about Chewbacca! Does that make sense? \n Ladies and gentlemen, I am not making any sense! \n None of this makes sense! \n And so you have to remember, when you're in that jury room\n deliberatin' and conjugatin' the Emancipation Proclamation,\n does it make sense? \n No! Ladies and gentlemen of this supposed jury, it does not make sense!\n If Chewbacca lives on Endor, you must acquit! \n The defense rests."<<endl;
225 cout << "\nTry '-help' for more information. \n";
226 exit(1);
227 }
228
229 //input file
230 fni= argv[1];
231
232 for (int i = 2; i < argc; i++)
233 {
234 //marco_new_01
235 if (!strcmp(argv[i], "-single_connection"))
236 {
237 single_connection = true;
238 continue;
239 }
240
241
242 if (!strcmp(argv[i], "-tryMerge"))
243 {
244 tryMerge = true;
245 continue;
246 }
247
248 if (!strcmp(argv[i], "-dontMerge")) // EMI
249 {
250 tryMerge = false;
251 continue;
252 }
253
254 if (!strcmp(argv[i], "-dontDelete")) // EMI
255 {
256 candelete = false;
257 continue;
258 }
259
260 if (!strcmp(argv[i], "-time_Offset")){
261 if (++i >= argc){
262 cerr << "-time_Offset needs arguments. \n";
263 cout << "Try '-help' for more information. \n";
264 exit(1);
265 }
266 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
267 time_Offset = atoll(argv[i]);
268 } else {
269 cerr << "-time_Offset needs an integer value. \n";
270 cout << "Try '-help' for more information. \n";
271 exit(1);
272 }
273 continue;
274 }
275
276 if (!strcmp(argv[i], "-orbit_number")){
277 if (++i >= argc){
278 cerr << "-orbit_number needs arguments. \n";
279 cout << "Try '-help' for more information. \n";
280 exit(1);
281 }
282 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
283 orbit_number = atoi(argv[i]);
284 } else {
285 cerr << "-orbit_number needs an integer value. \n";
286 cout << "Try '-help' for more information. \n";
287 exit(1);
288 }
289 continue;
290 }
291
292 if (!strcmp(argv[i], "-session_number")){
293 if (++i >= argc){
294 cerr << "-session_number needs arguments. \n";
295 cout << "Try '-help' for more information. \n";
296 exit(1);
297 }
298 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
299 mmm_number = atoi(argv[i]);
300 } else {
301 cerr << "-session_number needs an integer value. \n";
302 cout << "Try '-help' for more information. \n";
303 exit(1);
304 }
305 continue;
306 }
307
308 if (!strcmp(argv[i], "-max_pkt_number")){
309 if (++i >= argc){
310 cerr << "-max_pkt_number needs arguments. \n";
311 cout << "Try '-help' for more information. \n";
312 exit(1);
313 }
314 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
315 max_pkt_number = atoll(argv[i]);
316 } else {
317 cerr << "-max_pkt_number needs an integer value. \n";
318 cout << "Try '-help' for more information. \n";
319 exit(1);
320 }
321 continue;
322 }
323
324 if (!strcmp(argv[i], "-max_pkt_obt")){
325 if (++i >= argc){
326 cerr << "-max_pkt_obt needs arguments. \n";
327 cout << "Try '-help' for more information. \n";
328 exit(1);
329 }
330 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
331 max_pkt_obt = atoll(argv[i]);
332 } else {
333 cerr << "-max_pkt_obt needs an integer value. \n";
334 cout << "Try '-help' for more information. \n";
335 exit(1);
336 }
337 continue;
338 }
339
340
341 if (!strcmp(argv[i], "-simu"))
342 {
343 simulated_data = true;
344 continue;
345 }
346
347 if (!strcmp(argv[i], "-gpamela"))
348 {
349 simulated_data = true;
350 do_vrl_check = false;
351 nome_output = (char *)gSystem->BaseName(fni);
352 tryMerge = false;
353 candelete = false;
354 continue;
355 }
356
357 if (!strcmp(argv[i], "-no-vrl")) //EMI
358 {
359 do_vrl_check = false;
360 }
361 if (!strcmp(argv[i], "-vrl"))
362 {
363 do_vrl_check = true;
364 if(simulated_data)
365 {
366 cout<<"Impossible to perform VRL check with simulated data. Don't use -simu option"<<endl;
367 cout << "Try '-help' for more information. \n";
368 exit(1);
369 }
370
371 continue;
372 }
373
374 if (!strcmp(argv[i], "-filelog")){
375 if (++i >= argc){
376 cerr << "-filelog needs arguments. \n";
377 cout << "Try '-help' for more information. \n";
378 exit(1);
379 }
380 logfilename=argv[i];
381 continue;
382 }
383
384 if (!strcmp(argv[i], "-loglevel")){
385 if (++i >= argc){
386 cerr << "-loglevel needs arguments. \n";
387 cout << "Try '-help' for more information. \n";
388 exit(1);
389 }
390 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 3))) {
391 loglevel = (LogUtil::logLevel) atoi(argv[i]);
392 } else {
393 cerr << "-loglevel needs an integer value beetween 0 and 3. \n";
394 cout << "Try '-help' for more information. \n";
395 exit(1);
396 }
397 continue;
398 }
399
400
401 if (!strcmp(argv[i], "-c")){
402 if (++i >= argc){
403 cerr << "-c needs arguments. \n";
404 cout << "Try '-help' for more information. \n";
405 exit(1);
406 }
407 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 9))) {
408 compression = atoi(argv[i]);
409 } else {
410 cerr << "-c needs an integer value beetween 0 and 9. \n";
411 cout << "Try '-help' for more information. \n";
412 exit(1);
413 }
414 continue;
415 }
416
417 if (!strcmp(argv[i], "-outDir")){
418 if (++i >= argc){
419 cerr << "-outDir needs arguments. \n";
420 cout << "Try '-help' for more information. \n";
421 exit(1);
422 }
423 DIR* tempdir;
424 if ((tempdir = opendir(gSystem->ExpandPathName(argv[i]))) != 0) { // EMI
425 outDir = argv[i];
426 closedir(tempdir);
427 } else {
428 cerr << "-outDir needs an existing/accessable directory. \n";
429 cout << "Try '-help' for more information. \n";
430 exit(1);
431 }
432 continue;
433 }
434
435 if (!strcmp(argv[i], "-root_name")){
436 if (++i >= argc){
437 cerr << "-root_name needs arguments. \n";
438 cout << "Try '-help' for more information. \n";
439 exit(1);
440 }
441
442 nome_output = argv[i];
443 continue;
444 }
445
446 if (!strcmp(argv[i], "-delta_counter")){
447 if (++i >= argc){
448 cerr << "-delta_counter needs arguments. \n";
449 cout << "Try '-help' for more information. \n";
450 exit(1);
451 }
452 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
453 step_pkt_number = atoi(argv[i]);
454 } else {
455 cerr << "-delta_counter needs an integer value. \n";
456 cout << "Try '-help' for more information. \n";
457 exit(1);
458 }
459 continue;
460 }
461
462 if (!strcmp(argv[i], "-delta_obt")){
463 if (++i >= argc){
464 cerr << "-delta_obt needs arguments. \n";
465 cout << "Try '-help' for more information. \n";
466 exit(1);
467 }
468 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
469 step_pkt_obt = atoi(argv[i]);
470 } else {
471 cerr << "-delta_obt needs an integer value. \n";
472 cout << "Try '-help' for more information. \n";
473 exit(1);
474 }
475 continue;
476 }
477 if (!strcmp(argv[i], "-delta_Time")){
478 if (++i >= argc){
479 cerr << "-delta_Time needs arguments. \n";
480 cout << "Try '-help' for more information. \n";
481 exit(1);
482 }
483 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
484 delta_Time = atoi(argv[i]);
485 } else {
486 cerr << "-delta_Time needs an integer value. \n";
487 cout << "Try '-help' for more information. \n";
488 exit(1);
489 }
490 continue;
491 }
492
493 if (!strcmp(argv[i], "-db_host")){
494 if (++i >= argc){
495 cerr << "-db_host needs arguments. \n";
496 cout << "Try '-help' for more information. \n";
497 exit(1);
498 }
499 db_host = argv[i];
500 continue;
501 }
502
503
504 if (!strcmp(argv[i], "-db_port")){
505 if (++i >= argc){
506 cerr << "-db_port needs arguments. \n";
507 cout << "Try '-help' for more information. \n";
508 exit(1);
509 }
510 if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) {
511 db_port = atoi(argv[i]);
512 } else {
513 cerr << "-db_port needs an integer value. \n";
514 cout << "Try '-help' for more information. \n";
515 exit(1);
516 }
517 continue;
518 }
519
520 if (!strcmp(argv[i], "-db_name")){
521 if (++i >= argc){
522 cerr << "-db_name needs arguments. \n";
523 cout << "Try '-help' for more information. \n";
524 exit(1);
525 }
526 db_name = argv[i];
527 continue;
528 }
529 if (!strcmp(argv[i], "-db_user")){
530 if (++i >= argc){
531 cerr << "-db_name needs arguments. \n";
532 cout << "Try '-help' for more information. \n";
533 exit(1);
534 }
535 db_user = argv[i];
536 continue;
537 }
538
539 if (!strcmp(argv[i], "-db_pwd")){
540 if (++i >= argc){
541 cerr << "-db_name needs arguments. \n";
542 cout << "Try '-help' for more information. \n";
543 exit(1);
544 }
545 db_pwd = argv[i];
546 continue;
547 }
548 if (!strcmp(argv[i], "-tag")){
549 if (++i >= argc){
550 cerr << "-tag needs arguments. \n";
551 cout << "Try '-help' for more information. \n";
552 exit(1);
553 }
554 int len=strlen(argv[i])>TAGVALUELEN?TAGVALUELEN:strlen(argv[i]);
555 strncpy(tag_value,argv[i],len);
556 tag_value[len]=0;
557 continue;
558 }
559
560 //TODO: check that multiFile is always 0
561 // if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<<multiFile<<endl;}
562 }
563
564 //TODO: maybe if simulated data I don't need nothig so I can skip the following lines
565 // and use instead something similar to:
566 if (simulated_data) {orbit_number=99999; mmm_number=999; download=999; }
567
568 //I need the following lines only to retrieve orbit_number and mmm_number (session_number)from the filename
569 if((!orbit_number)||(!mmm_number))
570 {
571 std::string input = fni;
572 std::string fileName;
573 int pos = input.find_last_of("/");
574 fileName = input.substr(pos+1);
575 pos = fileName.find_last_of(".");
576 fileName = fileName.substr(0,pos);
577
578 if(fileName.length()<8){
579 cout<<"Wrong filename unable to retrieve orbit_number and session_number."<<endl;
580 cout<<"Rename input in format nnnnnmmm.pam or set orbit_number and session_number."<<endl;
581 cout << "Try '-help' for more information. \n";
582 exit(1);
583 }
584
585 if(!orbit_number){
586 std::string downlink;
587 downlink = fileName.substr(0,5);
588 orbit_number= atoi(downlink.c_str());
589 }
590
591 if(!mmm_number){
592 std::string mmm;
593 mmm = fileName.substr(5,3);
594 mmm_number= atoi(mmm.c_str());
595 }
596 }
597
598 }
599
600 void init()
601 {
602 //initial condition of automaton
603 StateManager::getInstance().init();
604 mainLogUtil = new LogUtil(logfilename,loglevel);
605
606 //TODO check if you like this
607 if (!strcmp(outDir,""))
608 {
609 char *pam_out = getenv("PAM_L0");
610 if (pam_out)
611 {
612 outDir = pam_out;
613 }
614 else
615 {
616 outDir = ".";
617 }
618
619 }
620 // write some initial things in the Log file
621 firstLog();
622
623 if(do_cont_check){
624 DB_config();
625 //marco_new_31:spostata logica in PacketUser.cpp
626 ////sqlServer = TSQLServer::Connect(connection,db_user,db_pwd);
627 ////if ((!sqlServer)||(!(sqlServer->IsConnected()))){cout<<"Can not connect with MYSQL sever"<<endl; exit(1);}
628 }
629
630 }
631
632 void DB_config()
633 {
634 if( (!strcmp(db_host,""))||(db_port==0)||(!strcmp(db_name,"")))
635 {
636 char *pamdbhost = getenv("PAM_DBHOST");
637 if (pamdbhost)
638 {
639 pelosconnection = pamdbhost;
640 }
641 else
642 {
643 if (!strcmp(db_host,""))
644 {
645 db_host="localhost";
646 }
647
648 if (db_port==0)
649 {
650 db_port=3306;
651 }
652
653 if (!strcmp(db_name,""))
654 {
655 db_name="chewbacca_db";
656 }
657 sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name);
658 pelosconnection=conn;
659 }
660 }
661 else{
662 sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name);
663 pelosconnection=conn;
664 }
665
666 if (!strcmp(db_user,""))
667 {
668 char *pamdbuser = getenv("PAM_DBUSER");
669 if (pamdbuser)
670 {
671 db_user = pamdbuser;
672 }
673 else
674 {
675 db_user="chewbacca_user";
676 }
677
678 }
679
680 if(!strcmp(db_pwd,""))
681 {
682 char *pamdbpsw = getenv("PAM_DBPSW");
683 if (pamdbpsw)
684 {
685 db_pwd = pamdbpsw;
686 }
687 else
688 {
689 db_pwd="chewbacca_pwd";
690 }
691 }
692
693 string msg = "Using DB: " + string(pelosconnection);// +" "+ string(db_user) +" "+ string(db_pwd);
694 mainLogUtil->logAlways(msg);
695 }
696
697
698 void firstLog(){
699 string msg = "Input file: " + (string)fni;
700 mainLogUtil->logAlways(msg);
701
702 msg = "Output directory for the generated root file(s): " + (string)outDir;
703 mainLogUtil->logAlways(msg);
704 msg = "Output directory for the generated root file(s) expanded: " + (string)gSystem->ExpandPathName(outDir); // EMI
705 mainLogUtil->logAlways(msg); //EMI
706
707 msg = "Output root name for the generated root file(s): " + (string)nome_output;
708 mainLogUtil->logAlways(msg);
709
710 stringstream oss1;
711 oss1.str()="";
712 oss1<< "Compression level for the generated root file(s): " <<compression;
713 msg = oss1.str();
714 mainLogUtil->logInfo(msg);
715
716 stringstream oss2;
717 oss2.str()="";
718 oss2<< "Max value for Packet Counter: " <<max_pkt_number<<" - Max value for Packet OBT: " <<max_pkt_obt;
719 msg = oss2.str();
720 mainLogUtil->logAll(msg);
721
722 if(do_vrl_check)
723 {
724 mainLogUtil->logInfo("VRL check set to true");
725 }
726 else
727 {
728 mainLogUtil->logWarning("VRL check skipped");
729 }
730
731 if(step_pkt_number)
732 {
733 stringstream oss;
734 oss.str()="";
735 oss<<"The allowed difference in the Packet Counter between two continuos packets: "<<step_pkt_number;
736 msg = oss.str();
737 mainLogUtil->logAlways(msg);
738 }
739
740 if(step_pkt_obt)
741 {
742 stringstream oss;
743 oss.str()="";
744 oss<<"The allowed difference in the Packet Orbital Time between two continuos packets: " <<step_pkt_obt;
745 msg = oss.str();
746 mainLogUtil->logAlways(msg);
747 }
748
749 if((!step_pkt_number)&&(!step_pkt_obt)){
750 mainLogUtil->logAlways("No check about continuity; DB unused. No Merging ...");
751 do_cont_check=false;
752 tryMerge=false;
753 //return;
754 }
755
756 /*
757 if(do_cont_check){
758 sprintf(connection,"mysql://%s:%d/%s",db_host,db_port,db_name);
759 string msg = "Using DB: " + (string)connection;
760 mainLogUtil->logAlways(msg);
761 }
762 */
763
764 if(tryMerge)
765 {
766 mainLogUtil->logInfo("Try to Merge ROOT files");
767 }
768
769 /*
770 if(tryMerge){
771 stringstream oss1;
772 oss1.str()="";
773 oss1<<"The allowed difference in the Real Time between two ROOT files: " <<delta_Time<<" s";
774 string msg1=oss1.str();
775 mainLogUtil->logAlways(msg1);
776 }
777 */
778
779 }
780
781 void finish()
782 {
783 PacketUser::getInstance().FinishLastGroup();
784 }
785
786 void deleteAll()
787 {
788 if(mainLogUtil){delete mainLogUtil; mainLogUtil = NULL;}
789 //marco_new_01: questo non serve sicuramente ...
790 if(sqlServer){delete sqlServer; sqlServer = NULL;}
791 }
792
793 //read simulated data
794 // without vrl header
795 void mainRead_SimulatedData(char fni[])
796 {
797 //we read LENGTH_DATA_SIMU bytes each time ...
798 char ccData[LENGTH_DATA_SIMU]={0};
799 ifstream fin;
800 fin.open(fni, ifstream::binary);
801 if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
802 //main read loop
803 isCadreGood = true;//all the cadres are good ;-)
804
805 while(fin.eof()==0)
806 {
807 //reading data in the cadre
808 fin.read(ccData,LENGTH_DATA_SIMU);
809 stringstream oss;
810
811 if (!fin.good())
812 {
813 // if (fin.eof()){break;}//if we reach the end of file
814 stringstream oss;
815 oss<<"Read only "<<fin.gcount()<<" bytes instead of "<<LENGTH_DATA_SIMU<<
816 ". This error occurred after byte: "<<iByte_tot
817 <<" ... use the data read, then ... exit ";
818 string msg = oss.str();
819 mainLogUtil->logAll(msg);
820 //I want try to use it ...
821 for(int i=0;i<fin.gcount();i++)
822 {
823 StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
824 iByte_tot++;
825 }
826 //anyhow then exit
827 break;
828 }
829
830 //main processing of the data, searching for Pamela Packets
831 for(int i=0;i<LENGTH_DATA_SIMU;i++)
832 {
833 StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
834 iByte_tot++;
835 }
836 iNumCadres++;
837 }
838
839 fin.close();
840
841 stringstream oss1;
842 oss1<<"Total bytes read "<<iByte_tot;
843 string msg = oss1.str();
844 mainLogUtil->logInfo(msg);
845 }
846
847 //read real data
848 void mainRead_RealData(char fni[])
849 {
850 //header VRL
851 char ccHeader_VRL[LENGTH_HEADER_VRL]={0};
852 //data in each cadre, it can contain the PAMELA PKT
853 char ccData[LENGTH_DATA_CADRE]={0};
854 //last byte of each cadre, it is used to check CRC of the cadre
855 char cCrc_VRL=0;
856
857 //file di input = fni[]
858 ifstream fin;
859 fin.open(fni, ifstream::binary);
860 if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
861
862
863 //
864 // EM determine file length, number of downloads (a part small fractions of download due to VRL memory) and interval window for allowed change in download number
865 //
866 fin.seekg(0,std::ios::end);
867 filelength = (Long64_t)fin.tellg();
868 //
869 // filelength = 3840214016LL;
870 // filelength = 5763760128LL;
871 // filelength = 15370027008LL;
872 //
873 downcount = (Long64_t)round((Double_t)filelength/2000000000.);
874 if ( downcount < 1LL ) downcount = 1LL;
875 Long64_t downsize = (Long64_t)round((Double_t)filelength/(Double_t)downcount);
876 Long64_t hwint = Long64_t((Double_t)downsize/25.);
877 //
878 if ( downcount > 5LL ) dwin->Set(2LL+2LL*downcount);
879 dwin->Reset();
880 dwin->AddAt(0LL,0);
881 //
882 Int_t dco = 1;
883 //
884 for (Int_t ee=1; ee<(-1+2+2*downcount); ee+=2){
885 dwin->AddAt(((downsize*(dco-1))+hwint),ee);
886 dwin->AddAt(((downsize*dco)-hwint),ee+1);
887 dco++;
888 };
889 //
890 fin.seekg(0,std::ios::beg);
891 //
892 dwin->AddAt(filelength,(-1LL+2LL+2LL*downcount));
893 //
894 // printf(" file lenght is %L downcount is %L downsize is %L hwint is %L \n",filelength,downcount,downsize,hwint);
895 stringstream ess;
896 ess.str() = "";
897 ess << " (EM) " << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint;
898 string emsg = ess.str();
899 mainLogUtil->logInfo(emsg);
900 //
901 // cout << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint << endl;
902 // for (Int_t ee=0; ee<TMath::Max((2+2*downcount),12LL); ee++){
903 // cout << " " << ee << " ==> "<< dwin->At(ee) << endl;
904 // //a printf(" %i ==> %L \n",ee,dwin->At(ee));
905 // };
906
907 //main read loop
908 while(fin.eof()==0)
909 {
910 isCadreGood = true;
911 //start reading the file
912 streamposi = (Long64_t)fin.tellg(); // stream position in the file before reading the vrl header
913 if ( iNumCadres > 9 && numposi > 9 ){
914 streamposisaved = streamposi;
915 numposi = 0;
916 };
917 numposi++;
918 //
919 fin.read(ccHeader_VRL, LENGTH_HEADER_VRL);
920 // printf(" position in file is %llu \n",streamposi);
921 stringstream ss;
922 ss.str() = "";
923 ss << " position in file is "<<streamposi;
924 string sms = ss.str();
925 // mainLogUtil->logError(sms);
926 //
927 headok = true;
928 crcok = true;
929 if (!fin.good())
930 {
931 //TODO: check here
932 if (fin.eof())//if we reach the end of file
933 {
934 break;
935 }
936
937 stringstream oss;
938 oss<<"The cadre: "<<iNumCadres<<" has something wrong in the lenght of its header VRL."<<
939 " This error occurred after byte: "<<iByte_tot<<" ... exit";
940 string msg = oss.str();
941 mainLogUtil->logError(msg);
942
943 if(do_vrl_check){isCadreGood=false;}
944 break;
945 }
946 skip_cadre = false;
947 //check of the VRL header e setta route number
948 if(!VRL_Header_Check(ccHeader_VRL,LENGTH_HEADER_VRL))
949 {
950 isCadreGood=false;//Check if it is better set it to true ...
951 stringstream oss;
952 oss<<"The cadre: "<<iNumCadres<<" has something wrong in its header VRL."<<
953 " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully";
954 string msg = oss.str();
955 mainLogUtil->logWarning(msg);
956 // printf(" streamposi %llu \n",streamposi);
957 }
958
959 //
960 // we completely missed more than 10 VRL packets
961 //
962 if ( hc == 1100 ){
963 fin.seekg(streamposiorig);
964 stringstream os;
965 os<<" NEW (EM) UN-RECOVERABLE SYNC WITH VRL HEADERS, SIG! ";
966 os<<" new position in file, go back to "<<streamposiorig;
967 string ms = os.str();
968 mainLogUtil->logAll(ms);
969 hc++;
970 continue;
971 };
972 if ( horrorcount > 10 && hc < 1100 ){
973 hc++;
974 //if ( horrorcount > 1 ){
975 stringstream os;
976 os<<" NEW (EM) THIS IS TOO MUCH, LOST SYNC WITH VRL HEADERS! try to recover horrorcount = "<<horrorcount;
977 os<<" new position in file "<<(streamposi-(10LL*1024LL)+1LL);
978 // os<<" new position in file "<<(streamposi-(1LL*1024LL)+1LL);
979 string ms = os.str();
980 mainLogUtil->logAll(ms);
981 // printf(" fin.(streamposi-10*1025-7) %llu \n",streamposi-(10LL*1025LL)-7LL);
982 if ( (streamposi-(10LL*1024LL)+1LL) < streamposisaved ){
983 fin.seekg(streamposisaved+1LL);
984 stringstream oss;
985 oss<<" Problems repositioning stream reader... streamposisaved "<<streamposisaved;
986 string msg = oss.str();
987 mainLogUtil->logWarning(msg);
988
989 } else {
990 fin.seekg(streamposi-(10LL*1024LL)+1LL);
991 };
992 // fin.seekg(streamposi-(1LL*1024LL)+1LL);
993 horrorcount = 0;
994 continue;
995 };
996
997
998 //we have read and analysed the header VRL of this cadre
999 iByte_tot+=fin.gcount();
1000
1001 //reading data in the cadre
1002 fin.read(ccData,LENGTH_DATA_CADRE);
1003 cclung=fin.gcount();
1004 if (!fin.good())
1005 {
1006 stringstream oss;
1007 oss<<"The cadre: "<<iNumCadres<<" has something wrong in its data"<<
1008 " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully then ... exit ";
1009 string msg = oss.str();
1010 mainLogUtil->logWarning(msg);
1011
1012 //I am sure that this cadre is not good ...
1013 if(do_vrl_check)
1014 {
1015 isCadreGood=false;
1016 }
1017
1018 //... but maybe I want try to use it ...
1019 for(int i=0;i<fin.gcount();i++)
1020 {
1021 StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
1022 iByte_tot++;
1023 }
1024
1025 //... anyhow then exit
1026 break;
1027 }
1028
1029 //reading the last byte of the cadre
1030 fin.get(cCrc_VRL);
1031 if (!fin.good())
1032 {
1033 //if the last byte representing the CRC_VRL is not present
1034 //or if we are not able to read it
1035 stringstream oss;
1036 oss<<"The cadre: "<<iNumCadres<<" has something wrong in its last byte."<<
1037 " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully then ... exit ";
1038 string msg = oss.str();
1039 mainLogUtil->logWarning(msg);
1040
1041 //I am sure that this cadre is not good ...
1042 if(do_vrl_check)
1043 {
1044 isCadreGood=false;
1045 }
1046 crcok = false;
1047 //don't exit now, we need to keep it alive to process data
1048 //we will exit the main reading cicle after,
1049 }
1050
1051 if((do_vrl_check)&&(!skip_cadre))
1052 {
1053 //check CRC for the cadre
1054 short int CalcCheckSum;
1055 CalcCheckSum=CRC_Cadre_Check(ccData,(int)LENGTH_DATA_CADRE);
1056
1057 if (CalcCheckSum!=(short int)(cCrc_VRL))
1058 {
1059 isCadreGood=false;
1060 stringstream oss;
1061 oss<<"The cadre: "<<iNumCadres<<" failed the CRC check."<<
1062 " This error occurred after byte: "<<iByte_tot<<" ... use the data carefully";
1063 string msg = oss.str();
1064 mainLogUtil->logError(msg);
1065
1066 //non mi fido del contatore del route
1067 stringstream oss1;
1068 oss1<<"Route number unused = "<<route<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")";
1069 msg = oss1.str();
1070 mainLogUtil->logAll(msg);
1071 crcok = false;
1072
1073 route=old_route;
1074
1075 }
1076 }
1077
1078 // if ( !crcok && !headok ) skip_cadre = true; // useless
1079 // if ( cclung != LENGTH_DATA_CADRE ) skip_cadre = true; // useless
1080
1081 if(!skip_cadre)
1082 {
1083 if(route!=old_route)
1084 {
1085 is_new_route=true;
1086 cadcount = 1LL;
1087 download++;
1088 stringstream oss;
1089 oss<<"Found a new download n: "<<download<<". Header VRL ends at byte: "<<iByte_tot;
1090 string msg = oss.str();
1091 mainLogUtil->logAll(msg);
1092 }
1093 // is_new_route will be reset to false in PacketUser after the packet is used and stored
1094
1095
1096 //main processing of the data, searching for Pamela Packets
1097 // printf(" cc lung %i lung def %i \n",cclung,LENGTH_DATA_CADRE);
1098 for(int i=0; i<LENGTH_DATA_CADRE; i++)
1099 {
1100 StateManager::getInstance().getCurrentState()->readInput(ccData[i]);
1101 iByte_tot++;
1102 if(exitdbg==true){cout<<"**** DBG EXIT FOR****"<<endl; break;}
1103 }
1104 }
1105 else{
1106 iByte_tot+=LENGTH_DATA_CADRE;
1107 }
1108
1109 if(exitdbg==true){cout<<"**** DBG EXIT WHILE***##*#*#"<<endl; break;}
1110
1111 //taking in account the cCrc_VRL read before
1112 iByte_tot++;
1113
1114 //increment the number of cadre processed
1115 if(!skip_cadre)
1116 iNumCadres++;
1117
1118 if(isCadreGood)
1119 {
1120 if(!skip_cadre)
1121 iNumGoodCadres++;
1122 }
1123
1124
1125 // if(iNumCadres>=0){cout<<"################DBG EXIT ##################"<<endl; break;}
1126
1127 }//end reading cycle
1128
1129 fin.close();
1130
1131 stringstream oss1;
1132 oss1<<"Total bytes read "<<iByte_tot
1133 <<" Total number of cadres processed "<<iNumCadres
1134 <<" Total number of good cadres "<<iNumGoodCadres;
1135 string msg = oss1.str();
1136 mainLogUtil->logInfo(msg);
1137 }
1138
1139 //function that check the header VRL
1140 bool VRL_Header_Check(char* headVRL, int length)
1141 {
1142
1143 /* stringstream oss;
1144 oss<<"Pippooooooo: at byte+ "<<iByte_tot<<" cadre ... route number = "<<(int)headVRL[3];
1145 string msg = oss.str();
1146 mainLogUtil->logAll(msg);*/
1147
1148 old_route = route;
1149
1150 //a volte capita che ho header tutti 0 e poi anche la cadre e' tutti zeri da buttare
1151 //il CRC invece viene buono allora uso skip_cadre
1152 if(((int)(unsigned char)headVRL[0]==0)&&((int)(unsigned char)headVRL[1]==0)&&((int)(unsigned char)headVRL[2]==0)&&((int)(unsigned char)headVRL[3]==0)&&((int)(unsigned char)headVRL[7]==0))
1153 {
1154 skip_cadre=true;
1155 stringstream oss;
1156 oss<<"The cadre is all zeros"<<
1157 " This error occurred after byte: "<<iByte_tot<<" ... Cadre DISCARDED and not computed ";
1158 string msg = oss.str();
1159 mainLogUtil->logAll(msg);
1160 return false;
1161 }
1162
1163 if(
1164 ((int)(unsigned char)headVRL[0]!=CODE_FF)||
1165 ((int)(unsigned char)headVRL[1]!=CODE_46)||
1166 ((int)(unsigned char)headVRL[2]!=CODE_D5)
1167 )
1168 {
1169
1170 stringstream oss;
1171 oss<<"The cadre does not start with FF 46 D5"<<
1172 " This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")";
1173 string msg = oss.str();
1174 mainLogUtil->logAll(msg);
1175
1176 if ( ((int)(unsigned char)headVRL[0]!=CODE_FF)&&((int)(unsigned char)headVRL[1]!=CODE_46)&& ((int)(unsigned char)headVRL[2]!=CODE_D5) ){
1177 stringstream os;
1178 os<<" Horror movie, the cadre does not start with FF 46 D5 at all! "<<
1179 " This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")";
1180 string ms = os.str();
1181 mainLogUtil->logAll(ms);
1182 if ( !hc && !horrorcount ) streamposiorig = streamposi;
1183 horrorcount++;
1184 };
1185 headok = false;
1186
1187 return false;
1188 }
1189
1190 horrorcount = 0;
1191
1192 // EM qui condizione su posizione file
1193 Bool_t downchangeallowed = false;
1194 for (Int_t ee=0; ee<(-1+2+2*downcount); ee+=2){
1195 if ( streamposi >= dwin->At(ee) && streamposi < dwin->At(ee+1) ) downchangeallowed = true;
1196 // printf(" %i ==> %i \n",ee,dwin->At(ee));
1197 };
1198 if ( cadcount < 10000LL && cadcount > 0LL) downchangeallowed = false;
1199 cadcount++;
1200 // cout << " cadcount " << cadcount << endl;
1201 // printf("cadcount %L \n",cadcount);
1202 if ( downchangeallowed ){
1203 // printf(" change allowed! \n");
1204
1205 route = (int)headVRL[3];
1206
1207 if(route!=old_route)
1208 {
1209 stringstream oss;
1210 oss<<"Changing ROUTE at byte: "<<iByte_tot<<" route = "<<route<<" old value = "<<old_route<<" (download="<<download<<")";
1211 string msg = oss.str();
1212 mainLogUtil->logAll(msg);
1213 }
1214
1215 };
1216
1217 //TODO: check the cadre number and save this info?
1218 //TODO check that the lenght is 8
1219 return true;
1220 }
1221
1222 //function that computes the CRC for each cadre
1223 short int CRC_Cadre_Check(char dataVRL[], int length)
1224 {
1225 //function used only if(do_vrl_check)
1226 short int Summ;
1227 int i;
1228 int CSum, tmp;
1229
1230 tmp=(int)dataVRL[0];
1231 CSum=tmp;
1232 for (i=1;i<length;i++) {tmp=dataVRL[i]; CSum=CSum^tmp;}
1233 Summ=(short int) (CSum);
1234 return Summ;
1235 }
1236
1237
1238 //TODO: funzione per debug, rimuovere marco
1239 void split_input(char fni[])
1240 {
1241 cout<<"splitting input file"<<endl;
1242
1243 system("mkdir splitted");
1244
1245 ifstream fin;
1246 fin.open(fni, ifstream::binary);
1247 if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);}
1248
1249 ofstream fout;
1250 static int num=0;
1251
1252 int cic = 1024;
1253 int mille =1000;
1254 int quanti = 50;
1255 int tanti =quanti*mille*cic;
1256 char * tmp = new char[tanti];
1257 while(fin.eof()==0)
1258 {
1259 fin.read(tmp,tanti);
1260 stringstream fno;
1261 fno<<"./splitted/splitted_"<<++num;
1262 fout.open(fno.str().c_str(), ios::binary);
1263 fout.write(tmp,tanti);
1264 fout.close();
1265 }
1266
1267 delete[] tmp;
1268 fin.close();
1269 cout<<"done, output in folder splitted"<<endl;
1270 }
1271
1272

  ViewVC Help
Powered by ViewVC 1.1.23