//============================================================================ // $Id: PamOffLineSW_Main.cpp,v 1.10 2010/02/11 09:02:10 mocchiut Exp $ // Description : Pamela Off-Line Software //============================================================================ #include #include "PamInclude.h" #include "StateManager.h" #include "PacketUser.h" #include extern "C" { #include } using namespace PamOffLineSW; // dbg purposes bool exitdbg = false; void exitDBG(){exitdbg=true;} //function needed only for dbg purposes: void split_input(char fni[]); #define LENGTH_DATA_SIMU 1024 #define LENGTH_CADRE_VRL 1024 #define LENGTH_HEADER_VRL 8 #define LENGTH_DATA_CADRE (LENGTH_CADRE_VRL-LENGTH_HEADER_VRL-1)//1015 //main functions void readOptions(int argc, char *argv[]); void init(); void firstLog(); void DB_config(); void mainRead_RealData(char fni[]); void mainRead_SimulatedData(char fni[]); void finish(); void deleteAll(); //utilities functions bool VRL_Header_Check(char * headVRL, int length); short int CRC_Cadre_Check(char dataVRL[], int length); //global variables bool simulated_data = false; //bool do_vrl_check = false; //EMI bool do_vrl_check = true; //EMI const char* db_host = ""; int db_port = 0; const char* db_name = ""; char conn[100]=""; LogUtil::logLevel loglevel=LogUtil::LOGERROR; const char* logfilename = "chewbacca.log"; //current route int route = 999; //previous route int old_route = 999; Long64_t filelength = 0LL; Long64_t streamposi = 0LL; Long64_t streamposisaved = 0LL; Int_t numposi = 10; Long64_t downcount = 1LL; TArrayL64 *dwin=new TArrayL64(12); Long64_t cadcount = 0LL; Int_t horrorcount = 0; Int_t hc = 0; bool skip_cadre = false; long int iNumGoodCadres=0; //total number of good cadres // global variables used in all the project namespace PamOffLineSW { const char* db_user = ""; const char* db_pwd = ""; char* pelosconnection = NULL; //marco_new_01 bool single_connection=false; long int iNumCadres=0; //cadre's number unsigned long long int iByte_tot=0;// how many bytes I have read till now bool isCadreGood = true;//if the cadre is good char* fni; //path completo short compression = 3; // char *outDir = "."; const char *outDir = ""; // char * nome_output="chewbacca"; //EMI const char * nome_output="L0PAM";//EMI bool multiFile = 0; // unsigned long int step_pkt_number=0; // EMI unsigned long int step_pkt_number=3000; // unsigned long int step_pkt_obt=0; // EMI unsigned long int step_pkt_obt=7200000; // EMI LogUtil* mainLogUtil = NULL; TSQLServer* sqlServer = NULL; unsigned long int max_pkt_number = 16777215;//biggest value before reset unsigned long int max_pkt_obt = 4294967295u;//biggest value before reset // bool is_new_route = false; // EMI bool is_new_route = true; // EMI ??????????? VA BENE? SECONDO ME SI` unsigned int download = 0; unsigned int orbit_number=0; unsigned int mmm_number = 0;//session_number unsigned long int time_Offset=0; // bool tryMerge = false; bool tryMerge = true; bool candelete = true; //EM bool do_cont_check=true;//if do_cont_check is false do not use a DB ... //TODO: now it is unused unsigned long int delta_Time=0;//in seconds #define TAGVALUELEN 4 char tag_value[TAGVALUELEN]; } // main: // int main(int argc, char *argv[]) { // read command line options readOptions(argc,argv); cout<<"Please wait"<logAll("Using simulated data"); mainRead_SimulatedData(fni); } else { mainLogUtil->logAll("Using real data"); mainRead_RealData(fni); } finish(); timeval tv2; gettimeofday(&tv2,NULL); unsigned long long timems2 = (unsigned long long)tv2.tv_sec * (unsigned long long)1000 + tv2.tv_usec / 1000; unsigned long long timems = timems2 -timems1; string msg = "The analisys took: " + mainLogUtil->value2string(timems) + " ms"; mainLogUtil->logAlways(msg); deleteAll(); cout<<"The end "<= argc){ cerr << "-time_Offset needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { time_Offset = atoll(argv[i]); } else { cerr << "-time_Offset needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-orbit_number")){ if (++i >= argc){ cerr << "-orbit_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { orbit_number = atoi(argv[i]); } else { cerr << "-orbit_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-session_number")){ if (++i >= argc){ cerr << "-session_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { mmm_number = atoi(argv[i]); } else { cerr << "-session_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-max_pkt_number")){ if (++i >= argc){ cerr << "-max_pkt_number needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { max_pkt_number = atoll(argv[i]); } else { cerr << "-max_pkt_number needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-max_pkt_obt")){ if (++i >= argc){ cerr << "-max_pkt_obt needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { max_pkt_obt = atoll(argv[i]); } else { cerr << "-max_pkt_obt needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-simu")) { simulated_data = true; continue; } if (!strcmp(argv[i], "-gpamela")) { simulated_data = true; do_vrl_check = false; nome_output = (char *)gSystem->BaseName(fni); tryMerge = false; candelete = false; continue; } if (!strcmp(argv[i], "-no-vrl")) //EMI { do_vrl_check = false; } if (!strcmp(argv[i], "-vrl")) { do_vrl_check = true; if(simulated_data) { cout<<"Impossible to perform VRL check with simulated data. Don't use -simu option"<= argc){ cerr << "-filelog needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } logfilename=argv[i]; continue; } if (!strcmp(argv[i], "-loglevel")){ if (++i >= argc){ cerr << "-loglevel needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 3))) { loglevel = (LogUtil::logLevel) atoi(argv[i]); } else { cerr << "-loglevel needs an integer value beetween 0 and 3. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-c")){ if (++i >= argc){ cerr << "-c needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) && (atoi(argv[i]) <= 9))) { compression = atoi(argv[i]); } else { cerr << "-c needs an integer value beetween 0 and 9. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-outDir")){ if (++i >= argc){ cerr << "-outDir needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } DIR* tempdir; if ((tempdir = opendir(gSystem->ExpandPathName(argv[i]))) != 0) { // EMI outDir = argv[i]; closedir(tempdir); } else { cerr << "-outDir needs an existing/accessable directory. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-root_name")){ if (++i >= argc){ cerr << "-root_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } nome_output = argv[i]; continue; } if (!strcmp(argv[i], "-delta_counter")){ if (++i >= argc){ cerr << "-delta_counter needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { step_pkt_number = atoi(argv[i]); } else { cerr << "-delta_counter needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-delta_obt")){ if (++i >= argc){ cerr << "-delta_obt needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { step_pkt_obt = atoi(argv[i]); } else { cerr << "-delta_obt needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-delta_Time")){ if (++i >= argc){ cerr << "-delta_Time needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { delta_Time = atoi(argv[i]); } else { cerr << "-delta_Time needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-db_host")){ if (++i >= argc){ cerr << "-db_host needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_host = argv[i]; continue; } if (!strcmp(argv[i], "-db_port")){ if (++i >= argc){ cerr << "-db_port needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } if ((isdigit(*argv[i])) && ( (atoi(argv[i]) >= 0) )) { db_port = atoi(argv[i]); } else { cerr << "-db_port needs an integer value. \n"; cout << "Try '-help' for more information. \n"; exit(1); } continue; } if (!strcmp(argv[i], "-db_name")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_name = argv[i]; continue; } if (!strcmp(argv[i], "-db_user")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_user = argv[i]; continue; } if (!strcmp(argv[i], "-db_pwd")){ if (++i >= argc){ cerr << "-db_name needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } db_pwd = argv[i]; continue; } if (!strcmp(argv[i], "-tag")){ if (++i >= argc){ cerr << "-tag needs arguments. \n"; cout << "Try '-help' for more information. \n"; exit(1); } int len=strlen(argv[i])>TAGVALUELEN?TAGVALUELEN:strlen(argv[i]); strncpy(tag_value,argv[i],len); tag_value[len]=0; continue; } //TODO: check that multiFile is always 0 // if (!strcmp(argv[i], "-multi")){multiFile = 1; cout<<"debug: multi= "<IsConnected()))){cout<<"Can not connect with MYSQL sever"<logAlways(msg); } void firstLog(){ string msg = "Input file: " + (string)fni; mainLogUtil->logAlways(msg); msg = "Output directory for the generated root file(s): " + (string)outDir; mainLogUtil->logAlways(msg); msg = "Output directory for the generated root file(s) expanded: " + (string)gSystem->ExpandPathName(outDir); // EMI mainLogUtil->logAlways(msg); //EMI msg = "Output root name for the generated root file(s): " + (string)nome_output; mainLogUtil->logAlways(msg); stringstream oss1; oss1.str()=""; oss1<< "Compression level for the generated root file(s): " <logInfo(msg); stringstream oss2; oss2.str()=""; oss2<< "Max value for Packet Counter: " <logAll(msg); if(do_vrl_check) { mainLogUtil->logInfo("VRL check set to true"); } else { mainLogUtil->logWarning("VRL check skipped"); } if(step_pkt_number) { stringstream oss; oss.str()=""; oss<<"The allowed difference in the Packet Counter between two continuos packets: "<logAlways(msg); } if(step_pkt_obt) { stringstream oss; oss.str()=""; oss<<"The allowed difference in the Packet Orbital Time between two continuos packets: " <logAlways(msg); } if((!step_pkt_number)&&(!step_pkt_obt)){ mainLogUtil->logAlways("No check about continuity; DB unused. No Merging ..."); do_cont_check=false; tryMerge=false; //return; } /* if(do_cont_check){ sprintf(connection,"mysql://%s:%d/%s",db_host,db_port,db_name); string msg = "Using DB: " + (string)connection; mainLogUtil->logAlways(msg); } */ if(tryMerge) { mainLogUtil->logInfo("Try to Merge ROOT files"); } /* if(tryMerge){ stringstream oss1; oss1.str()=""; oss1<<"The allowed difference in the Real Time between two ROOT files: " <logAlways(msg1); } */ } void finish() { PacketUser::getInstance().FinishLastGroup(); } void deleteAll() { if(mainLogUtil){delete mainLogUtil; mainLogUtil = NULL;} //marco_new_01: questo non serve sicuramente ... if(sqlServer){delete sqlServer; sqlServer = NULL;} } //read simulated data // without vrl header void mainRead_SimulatedData(char fni[]) { //we read LENGTH_DATA_SIMU bytes each time ... char ccData[LENGTH_DATA_SIMU]={0}; ifstream fin; fin.open(fni, ifstream::binary); if (!fin) {cout<<"Can not open input file "<logAll(msg); //I want try to use it ... for(int i=0;ireadInput(ccData[i]); iByte_tot++; } //anyhow then exit break; } //main processing of the data, searching for Pamela Packets for(int i=0;ireadInput(ccData[i]); iByte_tot++; } iNumCadres++; } fin.close(); stringstream oss1; oss1<<"Total bytes read "<logInfo(msg); } //read real data void mainRead_RealData(char fni[]) { //header VRL char ccHeader_VRL[LENGTH_HEADER_VRL]={0}; //data in each cadre, it can contain the PAMELA PKT char ccData[LENGTH_DATA_CADRE]={0}; //last byte of each cadre, it is used to check CRC of the cadre char cCrc_VRL=0; //file di input = fni[] ifstream fin; fin.open(fni, ifstream::binary); if (!fin) {cout<<"Can not open input file "< 5LL ) dwin->Set(2LL+2LL*downcount); dwin->Reset(); dwin->AddAt(0LL,0); // Int_t dco = 1; // for (Int_t ee=1; ee<(-1+2+2*downcount); ee+=2){ dwin->AddAt(((downsize*(dco-1))+hwint),ee); dwin->AddAt(((downsize*dco)-hwint),ee+1); dco++; }; // fin.seekg(0,std::ios::beg); // dwin->AddAt(filelength,(-1LL+2LL+2LL*downcount)); // // printf(" file lenght is %L downcount is %L downsize is %L hwint is %L \n",filelength,downcount,downsize,hwint); stringstream ess; ess.str() = ""; ess << " (EM) " << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint; string emsg = ess.str(); mainLogUtil->logInfo(emsg); // // cout << " file lenght is "<< filelength<< " downcount is "<< downcount <<" downsize is "<< downsize <<" hwint is " << hwint << endl; // for (Int_t ee=0; ee "<< dwin->At(ee) << endl; // //a printf(" %i ==> %L \n",ee,dwin->At(ee)); // }; //main read loop while(fin.eof()==0) { isCadreGood = true; //start reading the file streamposi = (Long64_t)fin.tellg(); // stream position in the file before reading the vrl header if ( iNumCadres > 9 && numposi > 9 ){ streamposisaved = streamposi; numposi = 0; }; numposi++; // fin.read(ccHeader_VRL, LENGTH_HEADER_VRL); // printf(" position in file is %llu \n",streamposi); stringstream ss; ss.str() = ""; ss << " position in file is "<logError(sms); // if (!fin.good()) { //TODO: check here if (fin.eof())//if we reach the end of file { break; } stringstream oss; oss<<"The cadre: "<logError(msg); if(do_vrl_check){isCadreGood=false;} break; } skip_cadre = false; //check of the VRL header e setta route number if(!VRL_Header_Check(ccHeader_VRL,LENGTH_HEADER_VRL)) { isCadreGood=false;//Check if it is better set it to true ... stringstream oss; oss<<"The cadre: "<logWarning(msg); // printf(" streamposi %llu \n",streamposi); } // // we completely missed more than 10 VRL packets // if ( horrorcount > 10 && hc < 500 ){ hc++; //if ( horrorcount > 1 ){ stringstream os; os<<" NEW (EM) THIS IS TOO MUCH, LOST SYNC WITH VRL HEADERS! try to recover horrorcount = "<logAll(ms); // printf(" fin.(streamposi-10*1025-7) %llu \n",streamposi-(10LL*1025LL)-7LL); if ( (streamposi-(10LL*1024LL)+1LL) < streamposisaved ){ fin.seekg(streamposisaved+1LL); stringstream oss; oss<<" Problems repositioning stream reader... streamposisaved "<logWarning(msg); } else { fin.seekg(streamposi-(10LL*1024LL)+1LL); }; // fin.seekg(streamposi-(1LL*1024LL)+1LL); horrorcount = 0; continue; }; //we have read and analysed the header VRL of this cadre iByte_tot+=fin.gcount(); //reading data in the cadre fin.read(ccData,LENGTH_DATA_CADRE); if (!fin.good()) { stringstream oss; oss<<"The cadre: "<logWarning(msg); //I am sure that this cadre is not good ... if(do_vrl_check) { isCadreGood=false; } //... but maybe I want try to use it ... for(int i=0;ireadInput(ccData[i]); iByte_tot++; } //... anyhow then exit break; } //reading the last byte of the cadre fin.get(cCrc_VRL); if (!fin.good()) { //if the last byte representing the CRC_VRL is not present //or if we are not able to read it stringstream oss; oss<<"The cadre: "<logWarning(msg); //I am sure that this cadre is not good ... if(do_vrl_check) { isCadreGood=false; } //don't exit now, we need to keep it alive to process data //we will exit the main reading cicle after, } if((do_vrl_check)&&(!skip_cadre)) { //check CRC for the cadre short int CalcCheckSum; CalcCheckSum=CRC_Cadre_Check(ccData,(int)LENGTH_DATA_CADRE); if (CalcCheckSum!=(short int)(cCrc_VRL)) { isCadreGood=false; stringstream oss; oss<<"The cadre: "<logError(msg); //non mi fido del contatore del route stringstream oss1; oss1<<"Route number unused = "<logAll(msg); route=old_route; } } if(!skip_cadre) { if(route!=old_route) { is_new_route=true; cadcount = 1LL; download++; stringstream oss; oss<<"Found a new download n: "<logAll(msg); } // is_new_route will be reset to false in PacketUser after the packet is used and stored //main processing of the data, searching for Pamela Packets for(int i=0; ireadInput(ccData[i]); iByte_tot++; if(exitdbg==true){cout<<"**** DBG EXIT FOR****"<=0){cout<<"################DBG EXIT ##################"<logInfo(msg); } //function that check the header VRL bool VRL_Header_Check(char* headVRL, int length) { /* stringstream oss; oss<<"Pippooooooo: at byte+ "<logAll(msg); return false; } if( ((int)(unsigned char)headVRL[0]!=CODE_FF)|| ((int)(unsigned char)headVRL[1]!=CODE_46)|| ((int)(unsigned char)headVRL[2]!=CODE_D5) ) { stringstream oss; oss<<"The cadre does not start with FF 46 D5"<< " This error occurred after byte: "<logAll(msg); if ( ((int)(unsigned char)headVRL[0]!=CODE_FF)&&((int)(unsigned char)headVRL[1]!=CODE_46)&& ((int)(unsigned char)headVRL[2]!=CODE_D5) ){ stringstream os; os<<" Horror movie, the cadre does not start with FF 46 D5 at all! "<< " This error occurred after byte: "<logAll(ms); horrorcount++; }; return false; } horrorcount = 0; // EM qui condizione su posizione file Bool_t downchangeallowed = false; for (Int_t ee=0; ee<(-1+2+2*downcount); ee+=2){ if ( streamposi >= dwin->At(ee) && streamposi < dwin->At(ee+1) ) downchangeallowed = true; // printf(" %i ==> %i \n",ee,dwin->At(ee)); }; if ( cadcount < 10000LL && cadcount > 0LL) downchangeallowed = false; cadcount++; // cout << " cadcount " << cadcount << endl; // printf("cadcount %L \n",cadcount); if ( downchangeallowed ){ // printf(" change allowed! \n"); route = (int)headVRL[3]; if(route!=old_route) { stringstream oss; oss<<"Changing ROUTE at byte: "<logAll(msg); } }; //TODO: check the cadre number and save this info? //TODO check that the lenght is 8 return true; } //function that computes the CRC for each cadre short int CRC_Cadre_Check(char dataVRL[], int length) { //function used only if(do_vrl_check) short int Summ; int i; int CSum, tmp; tmp=(int)dataVRL[0]; CSum=tmp; for (i=1;i