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

Annotation of /chewbacca/PamOffLineSW/PamOffLineSW_Main.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download)
Tue Sep 23 07:20:14 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.23