| 1 |
//============================================================================ |
//============================================================================ |
| 2 |
// $Id: PamOffLineSW_Main.cpp,v 1.6 2009-02-01 16:38:26 mocchiut Exp $ |
// $Id: PamOffLineSW_Main.cpp,v 1.12 2010/02/12 05:22:04 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 |
|
|
| 50 |
const char* db_host = ""; |
const char* db_host = ""; |
| 51 |
int db_port = 0; |
int db_port = 0; |
| 52 |
const char* db_name = ""; |
const char* db_name = ""; |
| 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 |
|
Long64_t streamposiorig = 0LL; |
| 66 |
|
Int_t numposi = 10; |
| 67 |
|
Long64_t downcount = 1LL; |
| 68 |
|
TArrayL64 *dwin=new TArrayL64(12); |
| 69 |
|
Long64_t cadcount = 0LL; |
| 70 |
|
Int_t horrorcount = 0; |
| 71 |
|
Int_t hc = 0; |
| 72 |
|
|
| 73 |
bool skip_cadre = false; |
bool skip_cadre = false; |
| 74 |
long int iNumGoodCadres=0; //total number of good cadres |
long int iNumGoodCadres=0; //total number of good cadres |
| 75 |
|
|
| 78 |
{ |
{ |
| 79 |
const char* db_user = ""; |
const char* db_user = ""; |
| 80 |
const char* db_pwd = ""; |
const char* db_pwd = ""; |
| 81 |
char* connection = NULL; |
char* pelosconnection = NULL; |
| 82 |
//marco_new_01 |
//marco_new_01 |
| 83 |
bool single_connection=false; |
bool single_connection=false; |
| 84 |
long int iNumCadres=0; //cadre's number |
long int iNumCadres=0; //cadre's number |
| 110 |
unsigned long int time_Offset=0; |
unsigned long int time_Offset=0; |
| 111 |
// bool tryMerge = false; |
// bool tryMerge = false; |
| 112 |
bool tryMerge = true; |
bool tryMerge = true; |
| 113 |
|
bool candelete = true; //EM |
| 114 |
|
|
| 115 |
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 ... |
| 116 |
|
|
| 200 |
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"; |
| 201 |
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"; |
| 202 |
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"; |
| 203 |
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"; |
| 204 |
|
cout << "\t -dontDelete if you do not want to delete bad ROOT files from disks\n"; |
| 205 |
//marco_new_01 |
//marco_new_01 |
| 206 |
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"; |
| 207 |
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"; |
| 248 |
tryMerge = false; |
tryMerge = false; |
| 249 |
continue; |
continue; |
| 250 |
} |
} |
| 251 |
|
|
| 252 |
|
if (!strcmp(argv[i], "-dontDelete")) // EMI |
| 253 |
|
{ |
| 254 |
|
candelete = false; |
| 255 |
|
continue; |
| 256 |
|
} |
| 257 |
|
|
| 258 |
if (!strcmp(argv[i], "-time_Offset")){ |
if (!strcmp(argv[i], "-time_Offset")){ |
| 259 |
if (++i >= argc){ |
if (++i >= argc){ |
| 348 |
do_vrl_check = false; |
do_vrl_check = false; |
| 349 |
nome_output = (char *)gSystem->BaseName(fni); |
nome_output = (char *)gSystem->BaseName(fni); |
| 350 |
tryMerge = false; |
tryMerge = false; |
| 351 |
|
candelete = false; |
| 352 |
continue; |
continue; |
| 353 |
} |
} |
| 354 |
|
|
| 634 |
char *pamdbhost = getenv("PAM_DBHOST"); |
char *pamdbhost = getenv("PAM_DBHOST"); |
| 635 |
if (pamdbhost) |
if (pamdbhost) |
| 636 |
{ |
{ |
| 637 |
connection = pamdbhost; |
pelosconnection = pamdbhost; |
| 638 |
} |
} |
| 639 |
else |
else |
| 640 |
{ |
{ |
| 653 |
db_name="chewbacca_db"; |
db_name="chewbacca_db"; |
| 654 |
} |
} |
| 655 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
| 656 |
connection=conn; |
pelosconnection=conn; |
| 657 |
} |
} |
| 658 |
} |
} |
| 659 |
else{ |
else{ |
| 660 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
| 661 |
connection=conn; |
pelosconnection=conn; |
| 662 |
} |
} |
| 663 |
|
|
| 664 |
if (!strcmp(db_user,"")) |
if (!strcmp(db_user,"")) |
| 688 |
} |
} |
| 689 |
} |
} |
| 690 |
|
|
| 691 |
string msg = "Using DB: " + string(connection);// +" "+ string(db_user) +" "+ string(db_pwd); |
string msg = "Using DB: " + string(pelosconnection);// +" "+ string(db_user) +" "+ string(db_pwd); |
| 692 |
mainLogUtil->logAlways(msg); |
mainLogUtil->logAlways(msg); |
| 693 |
} |
} |
| 694 |
|
|
| 856 |
ifstream fin; |
ifstream fin; |
| 857 |
fin.open(fni, ifstream::binary); |
fin.open(fni, ifstream::binary); |
| 858 |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
| 859 |
|
|
| 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 |
//main read loop |
//main read loop |
| 906 |
while(fin.eof()==0) |
while(fin.eof()==0) |
| 907 |
{ |
{ |
| 908 |
isCadreGood = true; |
isCadreGood = true; |
| 909 |
//start reading the file |
//start reading the file |
| 910 |
|
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 |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
| 918 |
|
// 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 |
if (!fin.good()) |
if (!fin.good()) |
| 926 |
{ |
{ |
| 927 |
//TODO: check here |
//TODO: check here |
| 949 |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
| 950 |
string msg = oss.str(); |
string msg = oss.str(); |
| 951 |
mainLogUtil->logWarning(msg); |
mainLogUtil->logWarning(msg); |
| 952 |
|
// printf(" streamposi %llu \n",streamposi); |
| 953 |
} |
} |
| 954 |
|
|
| 955 |
|
// |
| 956 |
|
// we completely missed more than 10 VRL packets |
| 957 |
|
// |
| 958 |
|
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 |
|
if ( horrorcount > 10 && hc < 1100 ){ |
| 969 |
|
hc++; |
| 970 |
|
//if ( horrorcount > 1 ){ |
| 971 |
|
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 |
//we have read and analysed the header VRL of this cadre |
//we have read and analysed the header VRL of this cadre |
| 995 |
iByte_tot+=fin.gcount(); |
iByte_tot+=fin.gcount(); |
| 996 |
|
|
| 1074 |
if(route!=old_route) |
if(route!=old_route) |
| 1075 |
{ |
{ |
| 1076 |
is_new_route=true; |
is_new_route=true; |
| 1077 |
|
cadcount = 1LL; |
| 1078 |
download++; |
download++; |
| 1079 |
stringstream oss; |
stringstream oss; |
| 1080 |
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; |
| 1162 |
" 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<<")"; |
| 1163 |
string msg = oss.str(); |
string msg = oss.str(); |
| 1164 |
mainLogUtil->logAll(msg); |
mainLogUtil->logAll(msg); |
| 1165 |
|
|
| 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 |
|
if ( !hc && !horrorcount ) streamposiorig = streamposi; |
| 1173 |
|
horrorcount++; |
| 1174 |
|
}; |
| 1175 |
|
|
| 1176 |
return false; |
return false; |
| 1177 |
} |
} |
| 1178 |
|
|
| 1179 |
route = (int)headVRL[3]; |
horrorcount = 0; |
| 1180 |
|
|
| 1181 |
if(route!=old_route) |
// EM qui condizione su posizione file |
| 1182 |
{ |
Bool_t downchangeallowed = false; |
| 1183 |
stringstream oss; |
for (Int_t ee=0; ee<(-1+2+2*downcount); ee+=2){ |
| 1184 |
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; |
| 1185 |
string msg = oss.str(); |
// printf(" %i ==> %i \n",ee,dwin->At(ee)); |
| 1186 |
mainLogUtil->logAll(msg); |
}; |
| 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 |
|
|
| 1206 |
//TODO: check the cadre number and save this info? |
//TODO: check the cadre number and save this info? |
| 1207 |
//TODO check that the lenght is 8 |
//TODO check that the lenght is 8 |