/[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.10 - (show annotations) (download)
Thu Feb 11 09:02:10 2010 UTC (14 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.9: +60 -4 lines
9R bugs fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23