/[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.8 - (show annotations) (download)
Thu Dec 24 10:31:13 2009 UTC (15 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.7: +93 -13 lines
New version, packages recovering tools implemented, new DB, bugs fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23