/[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.13 - (hide annotations) (download)
Fri Feb 12 12:45:49 2010 UTC (15 years ago) by mocchiut
Branch: MAIN
Changes since 1.12: +15 -3 lines
Last try...

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

  ViewVC Help
Powered by ViewVC 1.1.23