| 1 |
//============================================================================ |
//============================================================================ |
| 2 |
// $Id: PamOffLineSW_Main.cpp,v 1.5 2008/12/18 14:47:02 mocchiut Exp $ |
// $Id: PamOffLineSW_Main.cpp,v 1.14 2010/02/16 13:38:13 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> |
| 45 |
bool simulated_data = false; |
bool simulated_data = false; |
| 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 |
|
int cclung = 0; |
| 49 |
|
Bool_t headok = true; |
| 50 |
|
Bool_t crcok = true; |
| 51 |
|
|
| 52 |
char* db_host = ""; |
const char* db_host = ""; |
| 53 |
int db_port = 0; |
int db_port = 0; |
| 54 |
char* db_name = ""; |
const char* db_name = ""; |
| 55 |
char conn[100]=""; |
char conn[100]=""; |
| 56 |
|
|
| 57 |
LogUtil::logLevel loglevel=LogUtil::LOGERROR; |
LogUtil::logLevel loglevel=LogUtil::LOGERROR; |
| 58 |
char* logfilename = "chewbacca.log"; |
const char* logfilename = "chewbacca.log"; |
| 59 |
//current route |
//current route |
| 60 |
int route = 999; |
int route = 999; |
| 61 |
//previous route |
//previous route |
| 62 |
int old_route = 999; |
int old_route = 999; |
| 63 |
|
|
| 64 |
|
Long64_t filelength = 0LL; |
| 65 |
|
Long64_t streamposi = 0LL; |
| 66 |
|
Long64_t streamposisaved = 0LL; |
| 67 |
|
Long64_t streamposiorig = 0LL; |
| 68 |
|
Int_t numposi = 10; |
| 69 |
|
Long64_t downcount = 1LL; |
| 70 |
|
TArrayL64 *dwin=new TArrayL64(12); |
| 71 |
|
Long64_t cadcount = 0LL; |
| 72 |
|
Int_t horrorcount = 0; |
| 73 |
|
Int_t hc = 0; |
| 74 |
|
|
| 75 |
bool skip_cadre = false; |
bool skip_cadre = false; |
| 76 |
long int iNumGoodCadres=0; //total number of good cadres |
long int iNumGoodCadres=0; //total number of good cadres |
| 77 |
|
|
| 78 |
// global variables used in all the project |
// global variables used in all the project |
| 79 |
namespace PamOffLineSW |
namespace PamOffLineSW |
| 80 |
{ |
{ |
| 81 |
char* db_user = ""; |
const char* db_user = ""; |
| 82 |
char* db_pwd = ""; |
const char* db_pwd = ""; |
| 83 |
char* connection = NULL; |
char* pelosconnection = NULL; |
| 84 |
//marco_new_01 |
//marco_new_01 |
| 85 |
bool single_connection=false; |
bool single_connection=false; |
| 86 |
long int iNumCadres=0; //cadre's number |
long int iNumCadres=0; //cadre's number |
| 90 |
char* fni; //path completo |
char* fni; //path completo |
| 91 |
short compression = 3; |
short compression = 3; |
| 92 |
// char *outDir = "."; |
// char *outDir = "."; |
| 93 |
char *outDir = ""; |
const char *outDir = ""; |
| 94 |
|
|
| 95 |
// char * nome_output="chewbacca"; //EMI |
// char * nome_output="chewbacca"; //EMI |
| 96 |
char * nome_output="L0PAM";//EMI |
const char * nome_output="L0PAM";//EMI |
| 97 |
bool multiFile = 0; |
bool multiFile = 0; |
| 98 |
// unsigned long int step_pkt_number=0; // EMI |
// unsigned long int step_pkt_number=0; // EMI |
| 99 |
unsigned long int step_pkt_number=3000; |
unsigned long int step_pkt_number=3000; |
| 112 |
unsigned long int time_Offset=0; |
unsigned long int time_Offset=0; |
| 113 |
// bool tryMerge = false; |
// bool tryMerge = false; |
| 114 |
bool tryMerge = true; |
bool tryMerge = true; |
| 115 |
|
bool candelete = true; //EM |
| 116 |
|
|
| 117 |
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 ... |
| 118 |
|
|
| 202 |
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"; |
| 203 |
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"; |
| 204 |
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"; |
| 205 |
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"; |
| 206 |
|
cout << "\t -dontDelete if you do not want to delete bad ROOT files from disks\n"; |
| 207 |
//marco_new_01 |
//marco_new_01 |
| 208 |
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"; |
| 209 |
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"; |
| 250 |
tryMerge = false; |
tryMerge = false; |
| 251 |
continue; |
continue; |
| 252 |
} |
} |
| 253 |
|
|
| 254 |
|
if (!strcmp(argv[i], "-dontDelete")) // EMI |
| 255 |
|
{ |
| 256 |
|
candelete = false; |
| 257 |
|
continue; |
| 258 |
|
} |
| 259 |
|
|
| 260 |
if (!strcmp(argv[i], "-time_Offset")){ |
if (!strcmp(argv[i], "-time_Offset") || !strcmp(argv[i], "-time_offset") ){ |
| 261 |
if (++i >= argc){ |
if (++i >= argc){ |
| 262 |
cerr << "-time_Offset needs arguments. \n"; |
cerr << "-time_Offset needs arguments. \n"; |
| 263 |
cout << "Try '-help' for more information. \n"; |
cout << "Try '-help' for more information. \n"; |
| 350 |
do_vrl_check = false; |
do_vrl_check = false; |
| 351 |
nome_output = (char *)gSystem->BaseName(fni); |
nome_output = (char *)gSystem->BaseName(fni); |
| 352 |
tryMerge = false; |
tryMerge = false; |
| 353 |
|
candelete = false; |
| 354 |
continue; |
continue; |
| 355 |
} |
} |
| 356 |
|
|
| 636 |
char *pamdbhost = getenv("PAM_DBHOST"); |
char *pamdbhost = getenv("PAM_DBHOST"); |
| 637 |
if (pamdbhost) |
if (pamdbhost) |
| 638 |
{ |
{ |
| 639 |
connection = pamdbhost; |
pelosconnection = pamdbhost; |
| 640 |
} |
} |
| 641 |
else |
else |
| 642 |
{ |
{ |
| 655 |
db_name="chewbacca_db"; |
db_name="chewbacca_db"; |
| 656 |
} |
} |
| 657 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
| 658 |
connection=conn; |
pelosconnection=conn; |
| 659 |
} |
} |
| 660 |
} |
} |
| 661 |
else{ |
else{ |
| 662 |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
sprintf(conn,"mysql://%s:%d/%s",db_host,db_port,db_name); |
| 663 |
connection=conn; |
pelosconnection=conn; |
| 664 |
} |
} |
| 665 |
|
|
| 666 |
if (!strcmp(db_user,"")) |
if (!strcmp(db_user,"")) |
| 690 |
} |
} |
| 691 |
} |
} |
| 692 |
|
|
| 693 |
string msg = "Using DB: " + string(connection);// +" "+ string(db_user) +" "+ string(db_pwd); |
string msg = "Using DB: " + string(pelosconnection);// +" "+ string(db_user) +" "+ string(db_pwd); |
| 694 |
mainLogUtil->logAlways(msg); |
mainLogUtil->logAlways(msg); |
| 695 |
} |
} |
| 696 |
|
|
| 858 |
ifstream fin; |
ifstream fin; |
| 859 |
fin.open(fni, ifstream::binary); |
fin.open(fni, ifstream::binary); |
| 860 |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
if (!fin) {cout<<"Can not open input file "<<fni<<endl; exit(1);} |
| 861 |
|
|
| 862 |
|
|
| 863 |
|
// |
| 864 |
|
// 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 |
| 865 |
|
// |
| 866 |
|
fin.seekg(0,std::ios::end); |
| 867 |
|
filelength = (Long64_t)fin.tellg(); |
| 868 |
|
// |
| 869 |
|
// filelength = 3840214016LL; |
| 870 |
|
// filelength = 5763760128LL; |
| 871 |
|
// filelength = 15370027008LL; |
| 872 |
|
// |
| 873 |
|
downcount = (Long64_t)round((Double_t)filelength/2000000000.); |
| 874 |
|
if ( downcount < 1LL ) downcount = 1LL; |
| 875 |
|
Long64_t downsize = (Long64_t)round((Double_t)filelength/(Double_t)downcount); |
| 876 |
|
Long64_t hwint = Long64_t((Double_t)downsize/25.); |
| 877 |
|
// |
| 878 |
|
if ( downcount > 5LL ) dwin->Set(2LL+2LL*downcount); |
| 879 |
|
dwin->Reset(); |
| 880 |
|
dwin->AddAt(0LL,0); |
| 881 |
|
// |
| 882 |
|
Int_t dco = 1; |
| 883 |
|
// |
| 884 |
|
for (Int_t ee=1; ee<(-1+2+2*downcount); ee+=2){ |
| 885 |
|
dwin->AddAt(((downsize*(dco-1))+hwint),ee); |
| 886 |
|
dwin->AddAt(((downsize*dco)-hwint),ee+1); |
| 887 |
|
dco++; |
| 888 |
|
}; |
| 889 |
|
// |
| 890 |
|
fin.seekg(0,std::ios::beg); |
| 891 |
|
// |
| 892 |
|
dwin->AddAt(filelength,(-1LL+2LL+2LL*downcount)); |
| 893 |
|
// |
| 894 |
|
// printf(" file lenght is %L downcount is %L downsize is %L hwint is %L \n",filelength,downcount,downsize,hwint); |
| 895 |
|
stringstream ess; |
| 896 |
|
ess.str() = ""; |
| 897 |
|
ess << " (EM) " << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint; |
| 898 |
|
string emsg = ess.str(); |
| 899 |
|
mainLogUtil->logInfo(emsg); |
| 900 |
|
// |
| 901 |
|
// cout << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint << endl; |
| 902 |
|
// for (Int_t ee=0; ee<TMath::Max((2+2*downcount),12LL); ee++){ |
| 903 |
|
// cout << " " << ee << " ==> "<< dwin->At(ee) << endl; |
| 904 |
|
// //a printf(" %i ==> %L \n",ee,dwin->At(ee)); |
| 905 |
|
// }; |
| 906 |
|
|
| 907 |
//main read loop |
//main read loop |
| 908 |
while(fin.eof()==0) |
while(fin.eof()==0) |
| 909 |
{ |
{ |
| 910 |
isCadreGood = true; |
isCadreGood = true; |
| 911 |
//start reading the file |
//start reading the file |
| 912 |
|
streamposi = (Long64_t)fin.tellg(); // stream position in the file before reading the vrl header |
| 913 |
|
if ( iNumCadres > 9 && numposi > 9 ){ |
| 914 |
|
streamposisaved = streamposi; |
| 915 |
|
numposi = 0; |
| 916 |
|
}; |
| 917 |
|
numposi++; |
| 918 |
|
// |
| 919 |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); |
| 920 |
|
// printf(" position in file is %llu \n",streamposi); |
| 921 |
|
stringstream ss; |
| 922 |
|
ss.str() = ""; |
| 923 |
|
ss << " position in file is "<<streamposi; |
| 924 |
|
string sms = ss.str(); |
| 925 |
|
// mainLogUtil->logError(sms); |
| 926 |
|
// |
| 927 |
|
headok = true; |
| 928 |
|
crcok = true; |
| 929 |
if (!fin.good()) |
if (!fin.good()) |
| 930 |
{ |
{ |
| 931 |
//TODO: check here |
//TODO: check here |
| 953 |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
" This error occurred after byte: "<<iByte_tot<<" ... use the data carefully"; |
| 954 |
string msg = oss.str(); |
string msg = oss.str(); |
| 955 |
mainLogUtil->logWarning(msg); |
mainLogUtil->logWarning(msg); |
| 956 |
|
// printf(" streamposi %llu \n",streamposi); |
| 957 |
} |
} |
| 958 |
|
|
| 959 |
|
// |
| 960 |
|
// we completely missed more than 10 VRL packets |
| 961 |
|
// |
| 962 |
|
if ( hc == 1100 ){ |
| 963 |
|
fin.seekg(streamposiorig); |
| 964 |
|
stringstream os; |
| 965 |
|
os<<" NEW (EM) UN-RECOVERABLE SYNC WITH VRL HEADERS, SIG! "; |
| 966 |
|
os<<" new position in file, go back to "<<streamposiorig; |
| 967 |
|
string ms = os.str(); |
| 968 |
|
mainLogUtil->logAll(ms); |
| 969 |
|
hc++; |
| 970 |
|
continue; |
| 971 |
|
}; |
| 972 |
|
if ( horrorcount > 10 && hc < 1100 ){ |
| 973 |
|
hc++; |
| 974 |
|
//if ( horrorcount > 1 ){ |
| 975 |
|
stringstream os; |
| 976 |
|
os<<" NEW (EM) THIS IS TOO MUCH, LOST SYNC WITH VRL HEADERS! try to recover horrorcount = "<<horrorcount; |
| 977 |
|
os<<" new position in file "<<(streamposi-(10LL*1024LL)+1LL); |
| 978 |
|
// os<<" new position in file "<<(streamposi-(1LL*1024LL)+1LL); |
| 979 |
|
string ms = os.str(); |
| 980 |
|
mainLogUtil->logAll(ms); |
| 981 |
|
// printf(" fin.(streamposi-10*1025-7) %llu \n",streamposi-(10LL*1025LL)-7LL); |
| 982 |
|
if ( (streamposi-(10LL*1024LL)+1LL) < streamposisaved ){ |
| 983 |
|
fin.seekg(streamposisaved+1LL); |
| 984 |
|
stringstream oss; |
| 985 |
|
oss<<" Problems repositioning stream reader... streamposisaved "<<streamposisaved; |
| 986 |
|
string msg = oss.str(); |
| 987 |
|
mainLogUtil->logWarning(msg); |
| 988 |
|
|
| 989 |
|
} else { |
| 990 |
|
fin.seekg(streamposi-(10LL*1024LL)+1LL); |
| 991 |
|
}; |
| 992 |
|
// fin.seekg(streamposi-(1LL*1024LL)+1LL); |
| 993 |
|
horrorcount = 0; |
| 994 |
|
continue; |
| 995 |
|
}; |
| 996 |
|
|
| 997 |
|
|
| 998 |
//we have read and analysed the header VRL of this cadre |
//we have read and analysed the header VRL of this cadre |
| 999 |
iByte_tot+=fin.gcount(); |
iByte_tot+=fin.gcount(); |
| 1000 |
|
|
| 1001 |
//reading data in the cadre |
//reading data in the cadre |
| 1002 |
fin.read(ccData,LENGTH_DATA_CADRE); |
fin.read(ccData,LENGTH_DATA_CADRE); |
| 1003 |
|
cclung=fin.gcount(); |
| 1004 |
if (!fin.good()) |
if (!fin.good()) |
| 1005 |
{ |
{ |
| 1006 |
stringstream oss; |
stringstream oss; |
| 1043 |
{ |
{ |
| 1044 |
isCadreGood=false; |
isCadreGood=false; |
| 1045 |
} |
} |
| 1046 |
|
crcok = false; |
| 1047 |
//don't exit now, we need to keep it alive to process data |
//don't exit now, we need to keep it alive to process data |
| 1048 |
//we will exit the main reading cicle after, |
//we will exit the main reading cicle after, |
| 1049 |
} |
} |
| 1068 |
oss1<<"Route number unused = "<<route<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
oss1<<"Route number unused = "<<route<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
| 1069 |
msg = oss1.str(); |
msg = oss1.str(); |
| 1070 |
mainLogUtil->logAll(msg); |
mainLogUtil->logAll(msg); |
| 1071 |
|
crcok = false; |
| 1072 |
|
|
| 1073 |
route=old_route; |
route=old_route; |
| 1074 |
|
|
| 1075 |
} |
} |
| 1076 |
} |
} |
| 1077 |
|
|
| 1078 |
|
// if ( !crcok && !headok ) skip_cadre = true; // useless |
| 1079 |
|
// if ( cclung != LENGTH_DATA_CADRE ) skip_cadre = true; // useless |
| 1080 |
|
|
| 1081 |
if(!skip_cadre) |
if(!skip_cadre) |
| 1082 |
{ |
{ |
| 1083 |
if(route!=old_route) |
if(route!=old_route) |
| 1084 |
{ |
{ |
| 1085 |
is_new_route=true; |
is_new_route=true; |
| 1086 |
|
cadcount = 1LL; |
| 1087 |
download++; |
download++; |
| 1088 |
stringstream oss; |
stringstream oss; |
| 1089 |
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; |
| 1094 |
|
|
| 1095 |
|
|
| 1096 |
//main processing of the data, searching for Pamela Packets |
//main processing of the data, searching for Pamela Packets |
| 1097 |
|
// printf(" cc lung %i lung def %i \n",cclung,LENGTH_DATA_CADRE); |
| 1098 |
for(int i=0; i<LENGTH_DATA_CADRE; i++) |
for(int i=0; i<LENGTH_DATA_CADRE; i++) |
| 1099 |
{ |
{ |
| 1100 |
StateManager::getInstance().getCurrentState()->readInput(ccData[i]); |
StateManager::getInstance().getCurrentState()->readInput(ccData[i]); |
| 1172 |
" 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<<")"; |
| 1173 |
string msg = oss.str(); |
string msg = oss.str(); |
| 1174 |
mainLogUtil->logAll(msg); |
mainLogUtil->logAll(msg); |
| 1175 |
|
|
| 1176 |
|
if ( ((int)(unsigned char)headVRL[0]!=CODE_FF)&&((int)(unsigned char)headVRL[1]!=CODE_46)&& ((int)(unsigned char)headVRL[2]!=CODE_D5) ){ |
| 1177 |
|
stringstream os; |
| 1178 |
|
os<<" Horror movie, the cadre does not start with FF 46 D5 at all! "<< |
| 1179 |
|
" This error occurred after byte: "<<iByte_tot<<" ... Route number unused= "<<(int)headVRL[3]<<" Resetting ROUTE to old value = "<<old_route<<" (download="<<download<<")"; |
| 1180 |
|
string ms = os.str(); |
| 1181 |
|
mainLogUtil->logAll(ms); |
| 1182 |
|
if ( !hc && !horrorcount ) streamposiorig = streamposi; |
| 1183 |
|
horrorcount++; |
| 1184 |
|
}; |
| 1185 |
|
headok = false; |
| 1186 |
|
|
| 1187 |
return false; |
return false; |
| 1188 |
} |
} |
| 1189 |
|
|
| 1190 |
route = (int)headVRL[3]; |
horrorcount = 0; |
| 1191 |
|
|
| 1192 |
if(route!=old_route) |
// EM qui condizione su posizione file |
| 1193 |
{ |
Bool_t downchangeallowed = false; |
| 1194 |
stringstream oss; |
for (Int_t ee=0; ee<(-1+2+2*downcount); ee+=2){ |
| 1195 |
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; |
| 1196 |
string msg = oss.str(); |
// printf(" %i ==> %i \n",ee,dwin->At(ee)); |
| 1197 |
mainLogUtil->logAll(msg); |
}; |
| 1198 |
} |
if ( cadcount < 10000LL && cadcount > 0LL) downchangeallowed = false; |
| 1199 |
|
cadcount++; |
| 1200 |
|
// cout << " cadcount " << cadcount << endl; |
| 1201 |
|
// printf("cadcount %L \n",cadcount); |
| 1202 |
|
if ( downchangeallowed ){ |
| 1203 |
|
// printf(" change allowed! \n"); |
| 1204 |
|
|
| 1205 |
|
route = (int)headVRL[3]; |
| 1206 |
|
|
| 1207 |
|
if(route!=old_route) |
| 1208 |
|
{ |
| 1209 |
|
stringstream oss; |
| 1210 |
|
oss<<"Changing ROUTE at byte: "<<iByte_tot<<" route = "<<route<<" old value = "<<old_route<<" (download="<<download<<")"; |
| 1211 |
|
string msg = oss.str(); |
| 1212 |
|
mainLogUtil->logAll(msg); |
| 1213 |
|
} |
| 1214 |
|
|
| 1215 |
|
}; |
| 1216 |
|
|
| 1217 |
//TODO: check the cadre number and save this info? |
//TODO: check the cadre number and save this info? |
| 1218 |
//TODO check that the lenght is 8 |
//TODO check that the lenght is 8 |