/[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.12 - (hide annotations) (download)
Fri Feb 12 05:22:04 2010 UTC (14 years, 9 months ago) by mocchiut
Branch: MAIN
Changes since 1.11: +2 -2 lines
9R bugs fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23