/[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.6 - (hide annotations) (download)
Sun Feb 1 16:38:26 2009 UTC (15 years, 10 months ago) by mocchiut
Branch: MAIN
CVS Tags: v1r02
Changes since 1.5: +16 -5 lines
Option -gpamela added, dir cleanup

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

  ViewVC Help
Powered by ViewVC 1.1.23