| 1 |
///////////////PAMELA PACKETS SORT/////////////////////////////////// |
///////////////PAMELA PACKETS SORT/////////////////////////////////// |
| 2 |
#include "descript.h" |
#include "descript.h" |
| 3 |
|
#include "flag.h" |
| 4 |
|
|
| 5 |
void RunHeaderReader(char *buf); |
void RunHeaderReader(char *buf); |
| 6 |
extern long int OBTtimesync; |
extern long int OBTtimesync; |
| 7 |
extern long int Timesync; |
extern long int Timesync; |
| 8 |
|
extern char MYSQL[80]; |
| 9 |
|
extern char database[80]; |
| 10 |
|
extern int print_flag; |
| 11 |
|
|
| 12 |
struct Names |
struct Names |
| 13 |
{ |
{ |
| 14 |
char a[80]; /*Full name for mmm_nnnnn_counter_xx.bin*/ |
char a[80]; /*Full name for nnnnn_mmm_xxx_AA.bin*/ |
| 15 |
char a_short[80]; /*Short name for mmm_nnnnn_counter_xx.bin*/ |
char a_short[80]; /*Short name for nnnnn_mmm_xxx_AA.bin*/ |
| 16 |
FILE *fo_a; |
FILE *fo_a; |
| 17 |
}; |
}; |
| 18 |
|
|
| 27 |
char ql[80]; |
char ql[80]; |
| 28 |
char tmp[80], tmp1[80], numer[10]; |
char tmp[80], tmp1[80], numer[10]; |
| 29 |
char arina[80]; |
char arina[80]; |
| 30 |
|
char connection[80]; |
| 31 |
|
char query[500]; |
| 32 |
|
long int orbit, route, count; |
| 33 |
// char buffer[10]; |
// char buffer[10]; |
| 34 |
char *packet=new char[Nmax]; |
char *packet=new char[Nmax]; |
| 35 |
unsigned int type=0; /*PAMELA packet type*/ |
unsigned int type=0; /*PAMELA packet type*/ |
| 43 |
long int first[NumberPack+1]; /*First byte for PAMELA packets of given type*/ |
long int first[NumberPack+1]; /*First byte for PAMELA packets of given type*/ |
| 44 |
long int last[NumberPack+1]; /*Last byte for PAMELA packets of given type*/ |
long int last[NumberPack+1]; /*Last byte for PAMELA packets of given type*/ |
| 45 |
long int temp=0; |
long int temp=0; |
| 46 |
|
long int time_differ=0; |
| 47 |
|
int flag_time=0; |
| 48 |
int Hex_first[4]; |
int Hex_first[4]; |
| 49 |
int Hex_last[4]; |
int Hex_last[4]; |
| 50 |
int i=0, j=0, stop=0; |
int i=0, j=0, stop=0; |
| 58 |
first[i]=0; |
first[i]=0; |
| 59 |
last[i]=0;} |
last[i]=0;} |
| 60 |
|
|
| 61 |
Length=(int)(strlen(file_init)); |
Length=(int)(strlen(file_init)); |
| 62 |
strncat(strcpy(tmp,"\0"),file_init,Length-8); |
strncat(strcpy(tmp,"\0"),file_init,Length-8); |
| 63 |
strcat(strcpy(tmp1,path),"tmp.dat"); |
strcat(strcpy(tmp1,path),"tmp.dat"); |
| 64 |
f_temp=fopen(tmp1,"wb+"); |
f_temp=fopen(tmp1,"wb+"); |
| 65 |
|
|
| 66 |
|
stringstream oss; |
| 67 |
|
|
| 68 |
|
strcpy(connection,MYSQL); |
| 69 |
|
strcat(connection,database); |
| 70 |
|
TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod"); |
| 71 |
|
|
| 72 |
for (i=0;i<NumberPack+1;i++) |
for (i=0;i<NumberPack+1;i++) |
| 73 |
{ |
{ |
| 74 |
if (i==0) {fprintf(f_temp,"%02i",i);} |
if (i==0) {fprintf(f_temp,"%02i",i);} |
| 76 |
fseek(f_temp,0,0); |
fseek(f_temp,0,0); |
| 77 |
fgets(numer,10,f_temp); |
fgets(numer,10,f_temp); |
| 78 |
fseek(f_temp,0,0); |
fseek(f_temp,0,0); |
| 79 |
/*************Open files /home/pamelaprod/rawreader/level0/nnnnn/mmm_counter/packets/mmm_nnnnn_counter_xx.bin*******/ |
/*************Open files /home/pamelaprod/rawreader/level0/nnnnn/xxx_mmm/packets/nnnnn_mmm_xxx_AA.bin*******/ |
| 80 |
strncat(strcpy(data[i].a,path3),file_init,Length-8); |
strncat(strcpy(data[i].a,path3),file_init,Length-8); |
| 81 |
strcat(data[i].a,numer); |
strcat(data[i].a,numer); |
| 82 |
strcat(data[i].a,".bin"); |
strcat(data[i].a,".bin"); |
| 83 |
//form the 'mmm_' |
//form the 'nnnnn_mmm_xxx_AA.bin' |
| 84 |
for (j=0;j<4;j++) tmp[j]=data[i].a[strlen(data[i].a)-(20-j)]; |
for (j=0;j<20;j++) tmp[j]=data[i].a[strlen(data[i].a)-(20-j)]; |
| 85 |
//form the 'mmm_nnnnn_' |
/*********************Create names nnnnn_mmm_xxx_AA.bin********************/ |
|
for (j=0;j<6;j++) tmp[j+4]=data[i].a[strlen(data[i].a)-(12-j)]; |
|
|
//form the 'mmm_nnnnn_ccc_' |
|
|
for (j=0;j<4;j++) tmp[j+10]=data[i].a[strlen(data[i].a)-(16-j)]; |
|
|
//form the 'mmm_nnnnn_ccc_XX.bin' |
|
|
for (j=0;j<6;j++) tmp[j+14]=data[i].a[strlen(data[i].a)-(6-j)]; tmp[20]='\0'; |
|
|
/*********************Create names mmm_nnnnn_counter_xx.bin********************/ |
|
| 86 |
for (j=0;j<20;j++) |
for (j=0;j<20;j++) |
| 87 |
{data[i].a_short[j]=tmp[j]; data[i].a_short[20]='\0'; |
{data[i].a_short[j]=tmp[j]; data[i].a_short[20]='\0'; |
| 88 |
data[i].a[strlen(data[i].a)-(20-j)]=tmp[j]; |
data[i].a[strlen(data[i].a)-(20-j)]=tmp[j]; |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
data[i].fo_a=fopen(data[i].a,"wb"); |
data[i].fo_a=fopen(data[i].a,"wb"); |
| 92 |
if(data[i].fo_a==NULL) |
if(data[i].fo_a==NULL) |
| 98 |
/**********************OPEN NECESSARY FILES************************/ |
/**********************OPEN NECESSARY FILES************************/ |
| 99 |
/******************************************************************/ |
/******************************************************************/ |
| 100 |
ifstream fo_pam; |
ifstream fo_pam; |
| 101 |
fo_pam.open(pam,ios::binary); //open input file "mmm_counter_nnnnn_cln2.pam" |
fo_pam.open(pam,ios::binary); //open input file "nnnnn_mmm_xxx_cln2.pam" |
| 102 |
if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);} |
if(fo_pam.good()==0) {cerr<<"can not open pam file"; exit(1);} |
| 103 |
|
|
| 104 |
ofstream fo_arina; |
ofstream fo_arina; |
| 105 |
strcpy(arina,"/home/arina/orientation/"); |
strcpy(arina,"/home/arina/orientation/"); |
| 106 |
strcat(arina,data[28].a_short); cout<<arina<<"\n"; |
strcat(arina,data[28].a_short); |
| 107 |
|
if (print_flag>0) cout<<arina<<"\n"; |
| 108 |
fo_arina.open(arina,ios::binary); |
fo_arina.open(arina,ios::binary); |
| 109 |
if(fo_arina.good()==0) {cerr<<"can not open arina file"; exit(1);} |
if(fo_arina.good()==0) {cerr<<"can not open arina file"; exit(1);} |
| 110 |
|
|
| 111 |
fo_log=fopen(log, "rb"); //open input file "mmm_counter_nnnnn_cln2.log" |
fo_log=fopen(log, "rb"); //open input file "nnnnn_mmm_xxx_cln2.log" |
| 112 |
if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);} |
if(fo_log==NULL) {cerr<<"can not open log file"; exit(1);} |
| 113 |
|
|
| 114 |
strcpy(fno2,path3); |
strcpy(fno2,path3); |
| 115 |
strncat(fno2,file_init,strlen(file_init)-8); |
strncat(fno2,file_init,strlen(file_init)-8); |
| 116 |
fo_work2=fopen(strcat(fno2,"work2.dat"), "wb"); |
strcat(fno2,"work2.dat"); |
| 117 |
fo_work2=fopen(fno2, "wb"); //open output help file "work_cln2.dat" |
fo_work2=fopen(fno2, "wb"); //open output help file "nnnnn_mmm_xxx_work2.dat" |
| 118 |
if(fo_work2==NULL) {cerr<<"can not open outputfile"; exit(1);} |
if(fo_work2==NULL) {cerr<<"can not open outputfile"; exit(1);} |
| 119 |
|
|
| 120 |
strncat(strcpy(ql,path3),file_init,Length-8); |
strncat(strcpy(ql,path3),file_init,Length-8); |
| 121 |
strcat(ql,"ql.log"); |
strcat(ql,"ql.log"); |
| 122 |
fo_ql=fopen(ql, "wb"); //open output file "mmm_counter_nnnnn_ql.log" |
fo_ql=fopen(ql, "wb"); //open output file "nnnnn_mmm_xxx_ql.log" |
| 123 |
if(fo_ql==NULL) {cerr<<"can not open ql file"; exit(1);} |
if(fo_ql==NULL) {cerr<<"can not open ql file"; exit(1);} |
| 124 |
|
|
| 125 |
/******************************************************************/ |
/******************************************************************/ |
| 126 |
/******************************************************************/ |
/******************************************************************/ |
| 127 |
while(feof(fo_log)==0) |
while(feof(fo_log)==0) |
| 128 |
{ |
{ |
| 129 |
//read PAMELA packet information from Route_cln2.log |
//read PAMELA packet information from nnnnn_mmm_xxx_cln2.log |
| 130 |
fscanf(fo_log,"%1c",&type); |
fscanf(fo_log,"%1c",&type); |
| 131 |
if (feof(fo_log)!=0) break; |
if (feof(fo_log)!=0) break; |
| 132 |
fscanf(fo_log,"%4c",&adr1); |
fscanf(fo_log,"%4c",&adr1); |
| 136 |
fscanf(fo_log,"%1c",&error); |
fscanf(fo_log,"%1c",&error); |
| 137 |
if (feof(fo_log)!=0) break; |
if (feof(fo_log)!=0) break; |
| 138 |
|
|
|
// cout<<bitset<8>(error)<<"\n"; |
|
|
// cout<<bitset<8>(error).test(0)<<"\n"; |
|
| 139 |
if ( ( (bitset<8>(error).test(7))||(error==0)|| |
if ( ( (bitset<8>(error).test(7))||(error==0)|| |
| 140 |
((bitset<8>(error).test(0))&&(!bitset<8>(error).test(1))&&(!bitset<8>(error).test(2))&& |
((bitset<8>(error).test(0))&&(!bitset<8>(error).test(1))&&(!bitset<8>(error).test(2))&& |
| 141 |
(!bitset<8>(error).test(3))&&(!bitset<8>(error).test(4))&&(!bitset<8>(error).test(5))&& |
(!bitset<8>(error).test(3))&&(!bitset<8>(error).test(4))&&(!bitset<8>(error).test(5))&& |
| 142 |
(!bitset<8>(error).test(6))) )&& |
(!bitset<8>(error).test(6))) )&& |
| 143 |
(type>0)&&(type<NumberPack+1) ) |
(type>0)&&(type<NumberPack+1) ) |
| 144 |
{ |
{ |
| 145 |
/*Place file pointer in Route_cln2.pam on packet begin*/ |
/*Place file pointer in nnnnn_mmm_xxx_cln2.pam on packet begin*/ |
| 146 |
fo_pam.seekg(adr1-1,ios::beg); |
fo_pam.seekg(adr1-1,ios::beg); |
| 147 |
//read PAMELA packet from Route_cln2.pam |
//read PAMELA packet from nnnnn_mmm_xxx_cln2.pam |
| 148 |
fo_pam.read(packet,adr2-adr1+1); |
fo_pam.read(packet,adr2-adr1+1); |
| 149 |
|
|
| 150 |
/*OBT of Pamela*/ |
/*OBT of Pamela*/ |
| 151 |
time=(long int)(unsigned char)(packet[11])+ |
time=(long int)(unsigned char)(packet[11])+ |
| 152 |
256*(long int)(unsigned char)(packet[10])+ |
256*(long int)(unsigned char)(packet[10])+ |
| 153 |
256*256*(long int)(unsigned char)(packet[9])+ |
256*256*(long int)(unsigned char)(packet[9])+ |
| 154 |
256*256*256*(long int)(unsigned char)(packet[8]); |
256*256*256*(long int)(unsigned char)(packet[8]); |
| 155 |
|
|
| 157 |
|
|
| 158 |
moscowtime=time-(OBTtimesync-Timesync); |
moscowtime=time-(OBTtimesync-Timesync); |
| 159 |
|
|
|
if(type == 11) cout<<"time ="<<time<<" moscowtime ="<<moscowtime<<"\n"; |
|
| 160 |
/*temp is packet number*/ |
/*temp is packet number*/ |
| 161 |
temp=(long int)(unsigned char)(packet[7])+ |
temp=(long int)(unsigned char)(packet[7])+ |
| 162 |
256*(long int)(unsigned char)(packet[6])+ |
256*(long int)(unsigned char)(packet[6])+ |
| 163 |
256*256*(long int)(unsigned char)(packet[5]); |
256*256*(long int)(unsigned char)(packet[5]); |
| 164 |
if ((fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n"; |
if ((print_flag>0)&&(fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n"; |
| 165 |
|
|
| 166 |
/*unpacking several packets.... mikhailov |
/*unpacking several packets.... mikhailov |
| 167 |
unpackingPamelaPacket(packet,type)*/ |
unpackingPamelaPacket(packet,type)*/ |
| 168 |
|
if( type == 11) |
| 169 |
// if (type == 10) RunHeaderReader(packet); |
{ |
| 170 |
if( type == 11){RunHeaderReader(packet);} |
RunHeaderReader(packet); |
| 171 |
|
if (flag_time==0) time_differ=Timesync-OBTtimesync; |
| 172 |
|
flag_time=1; |
| 173 |
|
} |
| 174 |
/*******************Packet with type "type"***********************/ |
/*******************Packet with type "type"***********************/ |
| 175 |
counter[type]+=1; |
counter[type]+=1; |
| 176 |
//write PAMELA packet in mmm_nnnnn_counter_xx.bin |
//write PAMELA packet in nnnnn_mmm_xxx_AA.bin |
| 177 |
if (type!=4) fwrite(packet,adr2-adr1+1,1,data[type].fo_a); |
if (type!=4) fwrite(packet,adr2-adr1+1,1,data[type].fo_a); |
| 178 |
//write for ARINA |
//write for ARINA |
| 179 |
if (type==28) fo_arina.write(packet,adr2-adr1+1); |
if (type==28) fo_arina.write(packet,adr2-adr1+1); |
| 194 |
temp=temp-(long int)pow(256.,3-i)* |
temp=temp-(long int)pow(256.,3-i)* |
| 195 |
(int)(temp/(long int)(pow(256.,3-i)));} |
(int)(temp/(long int)(pow(256.,3-i)));} |
| 196 |
} |
} |
| 197 |
//write information about PAMELA packet in mmm_counter_nnnnn_ql.log |
//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log |
| 198 |
if (type!=4) |
if (type!=4) |
| 199 |
{ |
{ |
| 200 |
fprintf(fo_ql,"%1c",char(type)); |
fprintf(fo_ql,"%1c",char(type)); |
| 215 |
{ |
{ |
| 216 |
/*******************Packets with errors***************************/ |
/*******************Packets with errors***************************/ |
| 217 |
char *er_pack=new char [adr2-adr1+1]; |
char *er_pack=new char [adr2-adr1+1]; |
| 218 |
/*Place file pointer in mmm_counter_nnnnn_cln2.pam on error packet begin*/ |
/*Place file pointer in nnnnn_mmm_xxx_cln2.pam on error packet begin*/ |
| 219 |
fo_pam.seekg(adr1-1,ios::beg); |
fo_pam.seekg(adr1-1,ios::beg); |
| 220 |
//read PAMELA error packet from mmm_counter_nnnnn_cln2.pam |
//read PAMELA error packet from nnnnn_mmm_xxx_cln2.pam |
| 221 |
fo_pam.read(er_pack,adr2-adr1+1); |
fo_pam.read(er_pack,adr2-adr1+1); |
| 222 |
time=(long int)(unsigned char)(er_pack[11])+ |
time=(long int)(unsigned char)(er_pack[11])+ |
| 223 |
256*(long int)(unsigned char)(er_pack[10])+ |
256*(long int)(unsigned char)(er_pack[10])+ |
| 224 |
256*256*(long int)(unsigned char)(er_pack[9])+ |
256*256*(long int)(unsigned char)(er_pack[9])+ |
| 225 |
256*256*256*(long int)(unsigned char)(er_pack[8]); |
256*256*256*(long int)(unsigned char)(er_pack[8]); |
| 226 |
counter[0]+=1; |
counter[0]+=1; |
| 227 |
//write PAMELA packet in mmm_nnnnn_conter_00.bin |
//write PAMELA packet in nnnnn_mmm_xxx_00.bin |
| 228 |
fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a); |
fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a); |
| 229 |
first[0]=last[0]+1; |
first[0]=last[0]+1; |
| 230 |
last[0]+=adr2-adr1+1; |
last[0]+=adr2-adr1+1; |
| 245 |
(int)(temp/(long int)(pow(256.,3-i)));} |
(int)(temp/(long int)(pow(256.,3-i)));} |
| 246 |
} |
} |
| 247 |
if (type!=4) |
if (type!=4) |
| 248 |
{//write information about PAMELA packet in mmm_counter_nnnnn_ql.log |
{//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log |
| 249 |
fprintf(fo_ql,"%1c",(char)(type)); |
fprintf(fo_ql,"%1c",(char)(type)); |
| 250 |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i])); |
for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_first[i])); |
| 251 |
// for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i])); //temporary |
// for (i=0;i<4;i++) fprintf(fo_ql,"%1c",char(Hex_last[i])); //temporary |
| 265 |
for (i=0;i<NumberPack+1;i++) |
for (i=0;i<NumberPack+1;i++) |
| 266 |
fprintf(fo_work2,"Number of packets in %s file = %i\n",data[i].a_short,counter[i]); |
fprintf(fo_work2,"Number of packets in %s file = %i\n",data[i].a_short,counter[i]); |
| 267 |
|
|
| 268 |
|
for(i=0;i<5;i++) {tmp1[i]=file_init[i];} tmp1[5]=' '; |
| 269 |
|
sscanf(tmp1,"%5d",&orbit); |
| 270 |
|
|
| 271 |
|
for(i=6;i<9;i++) {tmp1[i-6]=file_init[i];} tmp1[3]=' '; |
| 272 |
|
sscanf(tmp1,"%3d",&route); |
| 273 |
|
|
| 274 |
|
for(i=10;i<13;i++) {tmp1[i-10]=file_init[i];} tmp1[3]=' '; |
| 275 |
|
sscanf(tmp1,"%3d",&count); |
| 276 |
|
|
| 277 |
|
oss.str(""); |
| 278 |
|
oss << "UPDATE FILES1 set time_dif="<<time_differ<<" where cirquit_n="<<orbit<<" and route_n="<<route<< |
| 279 |
|
" and counter="<<count; |
| 280 |
|
if (print_flag>0) cout<<oss.str().c_str()<<" table FILES1\n"; |
| 281 |
|
TSQLResult *res=sqlServer->Query(oss.str().c_str()); |
| 282 |
|
|
| 283 |
|
// Form the query string to write PAMELA packets number in Packets |
| 284 |
|
query[0]='\0'; |
| 285 |
|
for(i=0;i<NumberPack;i++) |
| 286 |
|
{ |
| 287 |
|
sprintf(tmp,"%i",counter[i]); |
| 288 |
|
strcat(tmp,"','"); |
| 289 |
|
strcat(query,tmp); |
| 290 |
|
} |
| 291 |
|
sprintf(tmp,"%i",counter[i]); |
| 292 |
|
strcat(tmp,"')"); |
| 293 |
|
strcat(query,tmp); |
| 294 |
|
|
| 295 |
|
oss.str(""); |
| 296 |
|
oss << "INSERT INTO PACKETS (cirquit_n, route_n, counter, P00, P07, P08, P09, P10, P11, P12, P13, P16, P17, P18, P19, P1A," |
| 297 |
|
<< " P1B, P1C, P1D, P20, P21, P22, P23, P24, P25, P30, P50, P51, P52, P53, P54, P55, P60, P70, P71, P72, P73, P74," |
| 298 |
|
<< " P75, P76, P77, P81, P82, P83, P84, P85, P86, P88, P89, P8A, PA1, PAB, PF0, PF1, PF2, PF3, PF4)" |
| 299 |
|
<< " VALUES ('"<< orbit << "','"<< route << "','"<<count<<"','"<<query; |
| 300 |
|
if (print_flag>0) cout<<oss.str().c_str()<<" table PACKETS\n"; |
| 301 |
|
TSQLResult *res1=sqlServer->Query(oss.str().c_str()); |
| 302 |
|
|
| 303 |
fo_pam.close(); |
fo_pam.close(); |
| 304 |
fo_arina.close(); |
fo_arina.close(); |
| 305 |
fclose(fo_log); |
fclose(fo_log); |