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[120]; |
9 |
|
extern char database[120]; |
10 |
|
extern char arina[120]; |
11 |
|
extern int print_flag; |
12 |
|
|
13 |
struct Names |
struct Names |
14 |
{ |
{ |
15 |
char a[80]; /*Full name for mmm_nnnnn_counter_xx.bin*/ |
char a[100]; /*Full name for nnnnn_mmm_xxx_AA.bin*/ |
16 |
char a_short[80]; /*Short name for mmm_nnnnn_counter_xx.bin*/ |
char a_short[80]; /*Short name for nnnnn_mmm_xxx_AA.bin*/ |
17 |
FILE *fo_a; |
FILE *fo_a; |
18 |
}; |
}; |
19 |
|
|
21 |
char file_init[], int packet_type[]) |
char file_init[], int packet_type[]) |
22 |
|
|
23 |
{ |
{ |
24 |
FILE *fo_log, *fo_ql, *fo_work2, *f_temp; |
FILE *fo_log, *fo_ql, *fo_work2; |
25 |
|
|
26 |
Names data[NumberPack+1]; |
Names data[NumberPack+1]; |
27 |
char fno2[80]; /* Out help file mm_nnnnn_work2.dat*/ |
char fno2[80]; /* Out help file mm_nnnnn_work2.dat*/ |
28 |
char ql[80]; |
char ql[80]; |
29 |
char tmp[80], tmp1[80], numer[10]; |
char tmp[80], tmp1[80], numer[10]; |
30 |
char arina[80]; |
char connection[80]; |
31 |
|
char query[500]; |
32 |
|
long int orbit=0, route=0, count=0; |
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; |
52 |
for (i=0;i<4;i++) |
for (i=0;i<4;i++) |
53 |
{Hex_first[i]=0; |
{Hex_first[i]=0; |
54 |
Hex_last[i]=0;} |
Hex_last[i]=0;} |
|
|
|
55 |
for (i=0;i<NumberPack+1;i++) |
for (i=0;i<NumberPack+1;i++) |
56 |
{counter[i]=0; |
{counter[i]=0; |
57 |
first[i]=0; |
first[i]=0; |
58 |
last[i]=0;} |
last[i]=0;} |
59 |
|
Length=(int)(strlen(file_init)); |
|
Length=(int)(strlen(file_init)); |
|
60 |
strncat(strcpy(tmp,"\0"),file_init,Length-8); |
strncat(strcpy(tmp,"\0"),file_init,Length-8); |
61 |
strcat(strcpy(tmp1,path),"tmp.dat"); |
|
62 |
f_temp=fopen(tmp1,"wb+"); |
stringstream oss; |
63 |
|
|
64 |
|
strcpy(connection,MYSQL); |
65 |
|
strcat(connection,database); |
66 |
|
TSQLServer *sqlServer = TSQLServer::Connect(connection, "pamelaprod", "rawprod"); |
67 |
|
if (sqlServer==0) {cerr<<"can not connect with MYSQL server\n"; exit(1);} |
68 |
|
|
69 |
for (i=0;i<NumberPack+1;i++) |
for (i=0;i<NumberPack+1;i++) |
70 |
{ |
{ |
71 |
if (i==0) {fprintf(f_temp,"%02i",i);} |
if (i==0) |
72 |
else {fprintf(f_temp,"%02X",packet_type[i-1]);} |
{sprintf(tmp1,"%02i",i); |
73 |
fseek(f_temp,0,0); |
sscanf(tmp1,"%02s",numer); |
74 |
fgets(numer,10,f_temp); |
numer[2]='\0'; |
75 |
fseek(f_temp,0,0); |
} |
76 |
/*************Open files /home/pamelaprod/rawreader/level0/nnnnn/mmm_counter/packets/mmm_nnnnn_counter_xx.bin*******/ |
else |
77 |
|
{sprintf(tmp1,"%02X",packet_type[i-1]); |
78 |
|
sscanf(tmp1,"%02s",numer); |
79 |
|
numer[2]='\0'; |
80 |
|
} |
81 |
|
|
82 |
|
/*************Open files /home/pamelaprod/rawreader/level0/nnnnn/xxx_mmm/packets/nnnnn_mmm_xxx_AA.bin*******/ |
83 |
strncat(strcpy(data[i].a,path3),file_init,Length-8); |
strncat(strcpy(data[i].a,path3),file_init,Length-8); |
84 |
strcat(data[i].a,numer); |
strcat(data[i].a,numer); |
85 |
strcat(data[i].a,".bin"); |
strcat(data[i].a,".bin"); |
86 |
//form the 'mmm_' |
//form the 'nnnnn_mmm_xxx_AA.bin' |
87 |
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)]; |
88 |
//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********************/ |
|
89 |
for (j=0;j<20;j++) |
for (j=0;j<20;j++) |
90 |
{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'; |
91 |
data[i].a[strlen(data[i].a)-(20-j)]=tmp[j]; |
data[i].a[strlen(data[i].a)-(20-j)]=tmp[j]; |
92 |
} |
} |
93 |
|
|
94 |
data[i].fo_a=fopen(data[i].a,"wb"); |
data[i].fo_a=fopen(data[i].a,"wb"); |
95 |
if(data[i].fo_a==NULL) |
if(data[i].fo_a==NULL) |
96 |
{cerr<<"can not open _%s.dat file",numer; exit(1);} |
{cerr<<"can not open _%s.dat file",numer; exit(1);} |
97 |
|
|
98 |
} |
} |
|
fclose(f_temp); |
|
|
remove(tmp1); |
|
99 |
/**********************OPEN NECESSARY FILES************************/ |
/**********************OPEN NECESSARY FILES************************/ |
100 |
/******************************************************************/ |
/******************************************************************/ |
101 |
ifstream fo_pam; |
ifstream fo_pam; |
102 |
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" |
103 |
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);} |
104 |
|
|
105 |
ofstream fo_arina; |
ofstream fo_arina; |
106 |
strcpy(arina,"/home/arina/orientation/"); |
strcat(arina,data[28].a_short); |
107 |
strcat(arina,data[28].a_short); cout<<arina<<"\n"; |
if (print_flag>0) cout<<"arina="<<arina<<"\n"; |
108 |
fo_arina.open(arina,ios::binary); |
fo_arina.open(arina,ios::binary); //open file for arina |
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])+256*(long int)(unsigned char)(packet[6])+ |
162 |
256*(long int)(unsigned char)(packet[6])+ |
256*256*(long int)(unsigned char)(packet[5]); |
163 |
256*256*(long int)(unsigned char)(packet[5]); |
if ((print_flag>0)&&(fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n"; |
164 |
if ((fmod((float)(temp),(float)(10000.))==0.)&&(temp!=0)) cout<<"sorting "<<temp<<"\n"; |
|
165 |
/*unpacking several packets.... mikhailov |
/*unpacking several packets.... mikhailov |
166 |
unpackingPamelaPacket(packet,type)*/ |
unpackingPamelaPacket(packet,type)*/ |
167 |
|
if( type == 11) |
168 |
// if (type == 10) RunHeaderReader(packet); |
{ |
169 |
if( type == 11){RunHeaderReader(packet);} |
RunHeaderReader(packet); |
170 |
|
if (flag_time==0) time_differ=Timesync-OBTtimesync; |
171 |
|
flag_time=1; |
172 |
|
} |
173 |
/*******************Packet with type "type"***********************/ |
/*******************Packet with type "type"***********************/ |
174 |
counter[type]+=1; |
counter[type]+=1; |
175 |
//write PAMELA packet in mmm_nnnnn_counter_xx.bin |
//write PAMELA packet in nnnnn_mmm_xxx_AA.bin |
176 |
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); |
177 |
//write for ARINA |
//write for ARINA |
178 |
if (type==28) fo_arina.write(packet,adr2-adr1+1); |
if (type==28) fo_arina.write(packet,adr2-adr1+1); |
193 |
temp=temp-(long int)pow(256.,3-i)* |
temp=temp-(long int)pow(256.,3-i)* |
194 |
(int)(temp/(long int)(pow(256.,3-i)));} |
(int)(temp/(long int)(pow(256.,3-i)));} |
195 |
} |
} |
196 |
//write information about PAMELA packet in mmm_counter_nnnnn_ql.log |
//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log |
197 |
if (type!=4) |
if (type!=4) |
198 |
{ |
{ |
199 |
fprintf(fo_ql,"%1c",char(type)); |
fprintf(fo_ql,"%1c",char(type)); |
214 |
{ |
{ |
215 |
/*******************Packets with errors***************************/ |
/*******************Packets with errors***************************/ |
216 |
char *er_pack=new char [adr2-adr1+1]; |
char *er_pack=new char [adr2-adr1+1]; |
217 |
/*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*/ |
218 |
fo_pam.seekg(adr1-1,ios::beg); |
fo_pam.seekg(adr1-1,ios::beg); |
219 |
//read PAMELA error packet from mmm_counter_nnnnn_cln2.pam |
//read PAMELA error packet from nnnnn_mmm_xxx_cln2.pam |
220 |
fo_pam.read(er_pack,adr2-adr1+1); |
fo_pam.read(er_pack,adr2-adr1+1); |
221 |
time=(long int)(unsigned char)(er_pack[11])+ |
time=(long int)(unsigned char)(er_pack[11])+ |
222 |
256*(long int)(unsigned char)(er_pack[10])+ |
256*(long int)(unsigned char)(er_pack[10])+ |
223 |
256*256*(long int)(unsigned char)(er_pack[9])+ |
256*256*(long int)(unsigned char)(er_pack[9])+ |
224 |
256*256*256*(long int)(unsigned char)(er_pack[8]); |
256*256*256*(long int)(unsigned char)(er_pack[8]); |
225 |
counter[0]+=1; |
counter[0]+=1; |
226 |
//write PAMELA packet in mmm_nnnnn_conter_00.bin |
//write PAMELA packet in nnnnn_mmm_xxx_00.bin |
227 |
fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a); |
fwrite(er_pack,adr2-adr1+1,1,data[0].fo_a); |
228 |
first[0]=last[0]+1; |
first[0]=last[0]+1; |
229 |
last[0]+=adr2-adr1+1; |
last[0]+=adr2-adr1+1; |
244 |
(int)(temp/(long int)(pow(256.,3-i)));} |
(int)(temp/(long int)(pow(256.,3-i)));} |
245 |
} |
} |
246 |
if (type!=4) |
if (type!=4) |
247 |
{//write information about PAMELA packet in mmm_counter_nnnnn_ql.log |
{//write information about PAMELA packet in nnnnn_mmm_xxx_ql.log |
248 |
fprintf(fo_ql,"%1c",(char)(type)); |
fprintf(fo_ql,"%1c",(char)(type)); |
249 |
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])); |
250 |
// 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 |
263 |
|
|
264 |
for (i=0;i<NumberPack+1;i++) |
for (i=0;i<NumberPack+1;i++) |
265 |
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]); |
266 |
|
if (print_flag>0) cout<<file_init<<"\n"; |
267 |
|
for(i=0;i<5;i++) {tmp1[i]=file_init[i];} tmp1[5]='\0'; |
268 |
|
sscanf(tmp1,"%5u",&orbit); |
269 |
|
for(i=6;i<9;i++) {tmp1[i-6]=file_init[i];} tmp1[3]='\0'; |
270 |
|
sscanf(tmp1,"%3u",&route); |
271 |
|
for(i=10;i<13;i++) {tmp1[i-10]=file_init[i];} tmp1[3]='\0'; |
272 |
|
sscanf(tmp1,"%3u",&count); |
273 |
|
oss.str(""); |
274 |
|
oss << "UPDATE FILES1 set time_dif="<<time_differ<<" where cirquit_n="<<orbit<<" and route_n="<<route<< |
275 |
|
" and counter="<<count; |
276 |
|
if (print_flag>0) cout<<oss.str().c_str()<<" table FILES1\n"; |
277 |
|
TSQLResult *res=sqlServer->Query(oss.str().c_str()); |
278 |
|
|
279 |
|
// Form the query string to write PAMELA packets number in Packets |
280 |
|
query[0]='\0'; |
281 |
|
for(i=0;i<NumberPack;i++) |
282 |
|
{ |
283 |
|
sprintf(tmp,"%i",counter[i]); |
284 |
|
strcat(tmp,"','"); |
285 |
|
strcat(query,tmp); |
286 |
|
} |
287 |
|
sprintf(tmp,"%i",counter[i]); |
288 |
|
strcat(tmp,"')"); |
289 |
|
strcat(query,tmp); |
290 |
|
|
291 |
|
oss.str(""); |
292 |
|
oss << "INSERT INTO PACKETS (cirquit_n, route_n, counter, P00, P07, P08, P09, P10, P11, P12, P13, P16, P17, P18, P19, P1A," |
293 |
|
<< " P1B, P1C, P1D, P20, P21, P22, P23, P24, P25, P30, P50, P51, P52, P53, P54, P55, P60, P70, P71, P72, P73, P74," |
294 |
|
<< " P75, P76, P77, P81, P82, P83, P84, P85, P86, P88, P89, P8A, PA1, PAB, PF0, PF1, PF2, PF3, PF4)" |
295 |
|
<< " VALUES ('"<< orbit << "','"<< route << "','"<<count<<"','"<<query; |
296 |
|
if (print_flag>0) cout<<oss.str().c_str()<<" table PACKETS\n"; |
297 |
|
TSQLResult *res1=sqlServer->Query(oss.str().c_str()); |
298 |
|
|
299 |
fo_pam.close(); |
fo_pam.close(); |
300 |
fo_arina.close(); |
fo_arina.close(); |