1 |
//============================================================================ |
//============================================================================ |
2 |
// $Id: PamOffLineSW_Main.cpp,v 1.4 2008/12/18 12:58:37 mocchiut Exp $ |
// $Id: PamOffLineSW_Main.cpp,v 1.9 2010/02/02 15:26:09 mocchiut Exp $ |
3 |
// Description : Pamela Off-Line Software |
// Description : Pamela Off-Line Software |
4 |
//============================================================================ |
//============================================================================ |
5 |
|
|
8 |
#include "StateManager.h" |
#include "StateManager.h" |
9 |
|
|
10 |
#include "PacketUser.h" |
#include "PacketUser.h" |
11 |
|
#include <TArrayL64.h> |
12 |
|
|
13 |
extern "C" { |
extern "C" { |
14 |
#include <dirent.h> |
#include <dirent.h> |
46 |
//bool do_vrl_check = false; //EMI |
//bool do_vrl_check = false; //EMI |
47 |
bool do_vrl_check = true; //EMI |
bool do_vrl_check = true; //EMI |
48 |
|
|
49 |
char* db_host = ""; |
|
50 |
|
const char* db_host = ""; |
51 |
int db_port = 0; |
int db_port = 0; |
52 |
char* db_name = ""; |
const char* db_name = ""; |
53 |
char conn[100]=""; |
char conn[100]=""; |
54 |
|
|
55 |
LogUtil::logLevel loglevel=LogUtil::LOGERROR; |
LogUtil::logLevel loglevel=LogUtil::LOGERROR; |
56 |
char* logfilename = "chewbacca.log"; |
const char* logfilename = "chewbacca.log"; |
57 |
//current route |
//current route |
58 |
int route = 999; |
int route = 999; |
59 |
//previous route |
//previous route |
60 |
int old_route = 999; |
int old_route = 999; |
61 |
|
|
62 |
|
Long64_t filelength = 0LL; |
63 |
|
Long64_t streamposi = 0LL; |
64 |
|
Long64_t streamposisaved = 0LL; |
65 |
|
Int_t numposi = 10; |
66 |
|
Long64_t downcount = 1LL; |
67 |
|
TArrayL64 *dwin=new TArrayL64(12); |
68 |
|
Long64_t cadcount = 0LL; |
69 |
|
Int_t horrorcount = 0; |
70 |
|
|
71 |
bool skip_cadre = false; |
bool skip_cadre = false; |
72 |
long int iNumGoodCadres=0; //total number of good cadres |
long int iNumGoodCadres=0; //total number of good cadres |
73 |
|
|
74 |
// global variables used in all the project |
// global variables used in all the project |
75 |
namespace PamOffLineSW |
namespace PamOffLineSW |
76 |
{ |
{ |
77 |
char* db_user = ""; |
const char* db_user = ""; |
78 |
char* db_pwd = ""; |
const char* db_pwd = ""; |
79 |
char* connection = NULL; |
char* pelosconnection = NULL; |
80 |
//marco_new_01 |
//marco_new_01 |
81 |
bool single_connection=false; |
bool single_connection=false; |
82 |
long int iNumCadres=0; //cadre's number |
long int iNumCadres=0; //cadre's number |
86 |
char* fni; //path completo |
char* fni; //path completo |
87 |
short compression = 3; |
short compression = 3; |
88 |
// char *outDir = "."; |
// char *outDir = "."; |
89 |
char *outDir = ""; |
const char *outDir = ""; |
90 |
|
|
91 |
// char * nome_output="chewbacca"; //EMI |
// char * nome_output="chewbacca"; //EMI |
92 |
char * nome_output="L0PAM";//EMI |
const char * nome_output="L0PAM";//EMI |
93 |
bool multiFile = 0; |
bool multiFile = 0; |
94 |
// unsigned long int step_pkt_number=0; // EMI |
// unsigned long int step_pkt_number=0; // EMI |
95 |
unsigned long int step_pkt_number=3000; |
unsigned long int step_pkt_number=3000; |
108 |
unsigned long int time_Offset=0; |
unsigned long int time_Offset=0; |
109 |
// bool tryMerge = false; |
// bool tryMerge = false; |
110 |
bool tryMerge = true; |
bool tryMerge = true; |
111 |
|
bool candelete = true; //EM |
112 |
|
|
113 |
bool do_cont_check=true;//if do_cont_check is false do not use a DB ... |
bool do_cont_check=true;//if do_cont_check is false do not use a DB ... |
114 |
|
|
178 |
cout << "\t (-help | --help | -h) print this help and exit \n"; |
cout << "\t (-help | --help | -h) print this help and exit \n"; |
179 |
cout << "\t -simu if the input file contains simulated data instead of real data\n"; |
cout << "\t -simu if the input file contains simulated data instead of real data\n"; |
180 |
cout << "\t -vrl if you want to perform the vrl check\n"; |
cout << "\t -vrl if you want to perform the vrl check\n"; |
181 |
|
cout << "\t -no-vrl if you DO NOT want to perform the vrl check\n"; |
182 |
|
cout << "\t -gpamela gpamela data as input\n"; |
183 |
cout << "\t -filelog set the log filename. [default: chewbacca.log]\n"; |
cout << "\t -filelog set the log filename. [default: chewbacca.log]\n"; |
184 |
cout << "\t -loglevel set the log level. Values: [0,3] (error,warning,info,all) [default:0]\n"; |
cout << "\t -loglevel set the log level. Values: [0,3] (error,warning,info,all) [default:0]\n"; |
185 |
cout << "\t -c set the compression level for the generated ROOT file(s). Values: [0,9] [default = 3]\n"; |
cout << "\t -c set the compression level for the generated ROOT file(s). Values: [0,9] [default = 3]\n"; |
198 |
cout << "\t -session_number Value of the session number. If 0 this is retrieved from the input file name. [default = 0]\n"; |
cout << "\t -session_number Value of the session number. If 0 this is retrieved from the input file name. [default = 0]\n"; |
199 |
cout << "\t -time_Offset Value of the timeOffset. If 0 this is retrieved using the orbital number. [default = 0]\n"; |
cout << "\t -time_Offset Value of the timeOffset. If 0 this is retrieved using the orbital number. [default = 0]\n"; |
200 |
cout << "\t -tryMerge if you want to try to Merge ROOT files\n"; |
cout << "\t -tryMerge if you want to try to Merge ROOT files\n"; |
201 |
cout << "\t -dontMerge if you don not want to try to Merge ROOT files\n"; |
cout << "\t -dontMerge if you do not want to try to Merge ROOT files\n"; |
202 |
|
cout << "\t -dontDelete if you do not want to delete bad ROOT files from disks\n"; |
203 |
//marco_new_01 |
//marco_new_01 |
204 |
cout << "\t -single_connection if you want to open only one connection to DB.\n"; |
cout << "\t -single_connection if you want to open only one connection to DB.\n"; |
205 |
cout << "\t -tag if you want add the 'The owner of the file' - the tag lenght is 4 char\n"; |
cout << "\t -tag if you want add the 'The owner of the file' - the tag lenght is 4 char\n"; |
222 |
exit(1); |
exit(1); |
223 |
} |
} |
224 |
|
|
225 |
|
//input file |
226 |
|
fni= argv[1]; |
227 |
|
|
228 |
for (int i = 2; i < argc; i++) |
for (int i = 2; i < argc; i++) |
229 |
{ |
{ |
230 |
//marco_new_01 |
//marco_new_01 |
246 |
tryMerge = false; |
tryMerge = false; |
247 |
continue; |
continue; |
248 |
} |
} |
249 |
|
|
250 |
|
if (!strcmp(argv[i], "-dontDelete")) // EMI |
251 |
|
{ |
252 |
|
candelete = false; |
253 |
|
continue; |
254 |
|
} |
255 |
|
|
256 |
if (!strcmp(argv[i], "-time_Offset")){ |
if (!strcmp(argv[i], "-time_Offset")){ |
257 |
if (++i >= argc){ |
if (++i >= argc){ |
339 |
simulated_data = true; |
simulated_data = true; |
340 |
continue; |
continue; |
341 |
} |
} |
342 |
|
|
343 |
|
if (!strcmp(argv[i], "-gpamela")) |
344 |
|
{ |
345 |
|
simulated_data = true; |
346 |
|
do_vrl_check = false; |
347 |
|
nome_output = (char *)gSystem->BaseName(fni); |
348 |
|
tryMerge = false; |
349 |
|
candelete = false; |
350 |
|
continue; |
351 |
|
} |
352 |
|
|
353 |
if (!strcmp(argv[i], "-no-vrl")) //EMI |
if (!strcmp(argv[i], "-no-vrl")) //EMI |
354 |
{ |
{ |
557 |
// if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<<multiFile<<endl;} |
// if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<<multiFile<<endl;} |
558 |
} |
} |
559 |
|
|
|
//input file |
|
|
fni= argv[1]; |
|
|
|
|
560 |
//TODO: maybe if simulated data I don't need nothig so I can skip the following lines |
//TODO: maybe if simulated data I don't need nothig so I can skip the following lines |
561 |
// and use instead something similar to: |
// and use instead something similar to: |
562 |
//if(simulated data){orbit_number=99999; mmm_number=999; download=999; } |
if (simulated_data) {orbit_number=99999; mmm_number=999; download=999; } |
563 |
|
|
564 |
//I need the following lines only to retrieve orbit_number and mmm_number (session_number)from the filename |
//I need the following lines only to retrieve orbit_number and mmm_number (session_number)from the filename |
565 |
if((!orbit_number)||(!mmm_number)) |
if((!orbit_number)||(!mmm_number)) |
632 |
char *pamdbhost = getenv("PAM_DBHOST"); |
char *pamdbhost = getenv("PAM_DBHOST"); |
633 |
if (pamdbhost) |
if (pamdbhost) |
634 |
{ |
{ |
635 |
connection = pamdbhost; |
pelosconnection = pamdbhost; |
636 |
} |
} |
637 |
else |
else |
638 |
{ |
{ |
651 |
db_name="chewbacca_db"; |
db_name="chewbacca_db"; |
652 |
} |
} |
653 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
654 |
connection=conn; |
pelosconnection=conn; |
655 |
} |
} |
656 |
} |
} |
657 |
else{ |
else{ |
658 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
659 |
connection=conn; |
pelosconnection=conn; |
660 |
} |
} |
661 |
|
|
662 |
if (!strcmp(db_user,"")) |
if (!strcmp(db_user,"")) |
686 |
} |
} |
687 |
} |
} |
688 |
|
|
689 |
string msg = "Using DB: " + string(connection);// +" "+ string(db_user) +" "+ string(db_pwd); |
string msg = "Using DB: " + string(pelosconnection);// +" "+ string(db_user) +" "+ string(db_pwd); |
690 |
mainLogUtil->logAlways(msg); |
mainLogUtil->logAlways(msg); |
691 |
} |
} |
692 |
|
|
854 |
ifstream fin; |
ifstream fin; |
855 |
fin.open(fni, ifstream::binary); |
fin.open(fni, ifstream::binary); |
856 |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
857 |
|
|
858 |
|
|
859 |
|
// |
860 |
|
// EM determine file length, number of downloads (a part small fractions of download due to VRL memory) and interval window for allowed change in download number |
861 |
|
// |
862 |
|
fin.seekg(0,std::ios::end); |
863 |
|
filelength = (Long64_t)fin.tellg(); |
864 |
|
// |
865 |
|
// filelength = 3840214016LL; |
866 |
|
// filelength = 5763760128LL; |
867 |
|
// filelength = 15370027008LL; |
868 |
|
// |
869 |
|
downcount = (Long64_t)round((Double_t)filelength/2000000000.); |
870 |
|
if ( downcount < 1LL ) downcount = 1LL; |
871 |
|
Long64_t downsize = (Long64_t)round((Double_t)filelength/(Double_t)downcount); |
872 |
|
Long64_t hwint = Long64_t((Double_t)downsize/25.); |
873 |
|
// |
874 |
|
if ( downcount > 5LL ) dwin->Set(2LL+2LL*downcount); |
875 |
|
dwin->Reset(); |
876 |
|
dwin->AddAt(0LL,0); |
877 |
|
// |
878 |
|
Int_t dco = 1; |
879 |
|
// |
880 |
|
for (Int_t ee=1; ee<(-1+2+2*downcount); ee+=2){ |
881 |
|
dwin->AddAt(((downsize*(dco-1))+hwint),ee); |
882 |
|
dwin->AddAt(((downsize*dco)-hwint),ee+1); |
883 |
|
dco++; |
884 |
|
}; |
885 |
|
// |
886 |
|
fin.seekg(0,std::ios::beg); |
887 |
|
// |
888 |
|
dwin->AddAt(filelength,(-1LL+2LL+2LL*downcount)); |
889 |
|
// |
890 |
|
// printf(" file lenght is %L downcount is %L downsize is %L hwint is %L \n",filelength,downcount,downsize,hwint); |
891 |
|
stringstream ess; |
892 |
|
ess.str() = ""; |
893 |
|
ess << " (EM) " << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint; |
894 |
|
string emsg = ess.str(); |
895 |
|
mainLogUtil->logInfo(emsg); |
896 |
|
// |
897 |
|
// cout << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint << endl; |
898 |
|
// for (Int_t ee=0; ee<TMath::Max((2+2*downcount),12LL); ee++){ |
899 |
|
// cout << " " << ee << " ==> "<< dwin->At(ee) << endl; |
900 |
|
// //a printf(" %i ==> %L \n",ee,dwin->At(ee)); |
901 |
|
// }; |
902 |
|
|
903 |
//main read loop |
//main read loop |
904 |
while(fin.eof()==0) |
while(fin.eof()==0) |
905 |
{ |
{ |
906 |
isCadreGood = true; |
isCadreGood = true; |
907 |
//start reading the file |
//start reading the file |
908 |
|
streamposi = (Long64_t)fin.tellg(); // stream position in the file before reading the vrl header |
909 |
|
if ( iNumCadres > 9 && numposi > 9 ){ |
910 |
|
streamposisaved = streamposi; |
911 |
|
numposi = 0; |
912 |
|
}; |
913 |
|
numposi++; |
914 |
|
// |
915 |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
916 |
|
// printf(" position in file is %llu \n",streamposi); |
917 |
|
stringstream ss; |
918 |
|
ss.str() = ""; |
919 |
|
ss << " position in file is "<<streamposi; |
920 |
|
string sms = ss.str(); |
921 |
|
// mainLogUtil->logError(sms); |
922 |
|
// |
923 |
if (!fin.good()) |
if (!fin.good()) |
924 |
{ |
{ |
925 |
//TODO: check here |
//TODO: check here |
947 |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
948 |
string msg = oss.str(); |
string msg = oss.str(); |
949 |
mainLogUtil->logWarning(msg); |
mainLogUtil->logWarning(msg); |
950 |
|
// printf(" streamposi %llu \n",streamposi); |
951 |
} |
} |
952 |
|
|
953 |
|
// |
954 |
|
// we completely missed more than 10 VRL packets |
955 |
|
// |
956 |
|
// if ( horrorcount > 10 ){ |
957 |
|
if ( horrorcount > 1 ){ |
958 |
|
stringstream os; |
959 |
|
os<<" NEW (EM) THIS IS TOO MUCH, LOST SYNC WITH VRL HEADERS! try to recover horrorcount = "<<horrorcount; |
960 |
|
os<<" new position in file "<<(streamposi-(10LL*1024LL)+1LL); |
961 |
|
// os<<" new position in file "<<(streamposi-(1LL*1024LL)+1LL); |
962 |
|
string ms = os.str(); |
963 |
|
mainLogUtil->logAll(ms); |
964 |
|
// printf(" fin.(streamposi-10*1025-7) %llu \n",streamposi-(10LL*1025LL)-7LL); |
965 |
|
if ( (streamposi-(10LL*1024LL)+1LL) < streamposisaved ){ |
966 |
|
fin.seekg(streamposisaved+1LL); |
967 |
|
stringstream oss; |
968 |
|
oss<<" Problems repositioning stream reader... streamposisaved "<<streamposisaved; |
969 |
|
string msg = oss.str(); |
970 |
|
mainLogUtil->logWarning(msg); |
971 |
|
|
972 |
|
} else { |
973 |
|
fin.seekg(streamposi-(10LL*1024LL)+1LL); |
974 |
|
}; |
975 |
|
// fin.seekg(streamposi-(1LL*1024LL)+1LL); |
976 |
|
horrorcount = 0; |
977 |
|
continue; |
978 |
|
}; |
979 |
|
|
980 |
|
|
981 |
//we have read and analysed the header VRL of this cadre |
//we have read and analysed the header VRL of this cadre |
982 |
iByte_tot+=fin.gcount(); |
iByte_tot+=fin.gcount(); |
983 |
|
|
1061 |
if(route!=old_route) |
if(route!=old_route) |
1062 |
{ |
{ |
1063 |
is_new_route=true; |
is_new_route=true; |
1064 |
|
cadcount = 1LL; |
1065 |
download++; |
download++; |
1066 |
stringstream oss; |
stringstream oss; |
1067 |
oss<<"Found a new download n: "<<download<<". Header VRL ends at byte: "<<iByte_tot; |
oss<<"Found a new download n: "<<download<<". Header VRL ends at byte: "<<iByte_tot; |
1149 |
" This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
" This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
1150 |
string msg = oss.str(); |
string msg = oss.str(); |
1151 |
mainLogUtil->logAll(msg); |
mainLogUtil->logAll(msg); |
1152 |
|
|
1153 |
|
if ( ((int)(unsigned char)headVRL[0]!=CODE_FF)&&((int)(unsigned char)headVRL[1]!=CODE_46)&& ((int)(unsigned char)headVRL[2]!=CODE_D5) ){ |
1154 |
|
stringstream os; |
1155 |
|
os<<" Horror movie, the cadre does not start with FF 46 D5 at all! "<< |
1156 |
|
" This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
1157 |
|
string ms = os.str(); |
1158 |
|
mainLogUtil->logAll(ms); |
1159 |
|
horrorcount++; |
1160 |
|
}; |
1161 |
|
|
1162 |
return false; |
return false; |
1163 |
} |
} |
1164 |
|
|
1165 |
route = (int)headVRL[3]; |
horrorcount = 0; |
1166 |
|
|
1167 |
if(route!=old_route) |
// EM qui condizione su posizione file |
1168 |
{ |
Bool_t downchangeallowed = false; |
1169 |
stringstream oss; |
for (Int_t ee=0; ee<(-1+2+2*downcount); ee+=2){ |
1170 |
oss<<"Changing ROUTE at byte: "<<iByte_tot<<" route = "<<route<<" old value = "<<old_route<<" (download="<<download<<")"; |
if ( streamposi >= dwin->At(ee) && streamposi < dwin->At(ee+1) ) downchangeallowed = true; |
1171 |
string msg = oss.str(); |
// printf(" %i ==> %i \n",ee,dwin->At(ee)); |
1172 |
mainLogUtil->logAll(msg); |
}; |
1173 |
} |
if ( cadcount < 10000LL && cadcount > 0LL) downchangeallowed = false; |
1174 |
|
cadcount++; |
1175 |
|
// cout << " cadcount " << cadcount << endl; |
1176 |
|
// printf("cadcount %L \n",cadcount); |
1177 |
|
if ( downchangeallowed ){ |
1178 |
|
// printf(" change allowed! \n"); |
1179 |
|
|
1180 |
|
route = (int)headVRL[3]; |
1181 |
|
|
1182 |
|
if(route!=old_route) |
1183 |
|
{ |
1184 |
|
stringstream oss; |
1185 |
|
oss<<"Changing ROUTE at byte: "<<iByte_tot<<" route = "<<route<<" old value = "<<old_route<<" (download="<<download<<")"; |
1186 |
|
string msg = oss.str(); |
1187 |
|
mainLogUtil->logAll(msg); |
1188 |
|
} |
1189 |
|
|
1190 |
|
}; |
1191 |
|
|
1192 |
//TODO: check the cadre number and save this info? |
//TODO: check the cadre number and save this info? |
1193 |
//TODO check that the lenght is 8 |
//TODO check that the lenght is 8 |