-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyFileChecker.cpp
153 lines (125 loc) · 3.3 KB
/
myFileChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#include "myDecode.h"
#include "myFIFO-IOp.h"
#define BLOCKSIZE 1000000 //in words
uint32_t buffer[BLOCKSIZE];
using namespace std;
int32_t main(int32_t argc, char* argv[]){
FILE* datafile=NULL;
struct stat filestat;
uint32_t totalwords=0;
uint32_t readwords=0;
int32_t verb=0;
RunHeader myRH;
if(argc>3 || argc<2){
cout << "Usage: "<< argv[0] << " [-v] datafile" << endl;
return 1;
}else if(argc==3){
if(strcmp(argv[1],"-v")==0)
verb=1;
else{
cout << "Usage: "<< argv[0] << " [-v]" << endl;
return 1;
}
datafile=fopen(argv[2],"r");
if(datafile==NULL){
cout << "Cannot open file " << argv[2] << endl;
return 1;
}
stat(argv[2],&filestat);
}else if(argc==2){
datafile=fopen(argv[1],"r");
if(datafile==NULL){
cout << "Cannot open file " << argv[1] << endl;
return 1;
}
stat(argv[1],&filestat);
}
if(fseek(datafile,0,SEEK_SET)!=0){
cout << "Wrong fseek" << endl;
fclose(datafile);
return 1;
}
readwords=fread(&myRH,sizeof(RunHeader),1,datafile);
readwords=readwords*sizeof(RunHeader)/sizeof(uint32_t);
if(myRH.magic!=0xAABBCCDD ||
(myRH.ruhsiz/sizeof(uint32_t))!=readwords){
cout << "Wrong Run Header" << endl;
fclose(datafile);
return 1;
}
if(verb){
cout << "@@@@@ Run " << myRH.runnumber << " @@@@@";
cout << "\nRun Header size " << myRH.ruhsiz;
cout << "\nEvents " << myRH.evtsinrun;
cout << "\nStart Time " << myRH.begtim;
cout << "\nEnd Time " << myRH.endtim;
cout << "\n @@@@@@@@@@" << endl;
}
totalwords+=readwords;
readwords=1;
uint32_t error=0;
uint32_t events=0;
while(readwords!=0 && !error){
uint32_t i;
uint32_t endpointer;
EventHeader *head;
readwords=fread(buffer,sizeof(uint32_t),BLOCKSIZE,datafile);
if(ferror(datafile)){
cout << "Wrong read" << endl;
fclose(datafile);
return 1;
}
totalwords+=readwords;
i=0;
while(i!=readwords){
uint32_t evsizeword;
head=(EventHeader *) &buffer[i];
if(head->evmark!=0xCAFECAFE){
cout << "Cannot find the event marker. Something is wrong." << endl;
fclose(datafile);
return 1;
}
evsizeword=head->evsiz/sizeof(uint32_t);
if(evsizeword>BLOCKSIZE){
cout << "Event is bigger than the block size" << endl;
fclose(datafile);
return 1;
}
endpointer=i+evsizeword;
if(endpointer>readwords){
break;
}
if(myEvent(&buffer[i],verb)!=0){
cout << "Something is wrong in event "<< 0 << " stopping here" << endl;
fclose(datafile);
return 1;
}
events++;
i=endpointer;
}
if(fseek(datafile,(i-readwords)*sizeof(uint32_t),SEEK_CUR)!=0){
cout << "Wrong fseek" << endl;
fclose(datafile);
return 1;
}
totalwords+=i-readwords;
}
cout << "\nByte read: " << totalwords*sizeof(uint32_t);
cout << "\nFile size: " << filestat.st_size << endl;
fclose(datafile);
cout << endl;
if(events==myRH.evtsinrun)
cout << "The file is OK!!" << endl;
else{
cout << "Wrong event count";
cout << "\nRun header says: " << myRH.evtsinrun;
cout << "\nI found: " << events << endl;
}
return 0;
}