-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwudbtest.cpp
200 lines (141 loc) · 4.22 KB
/
wudbtest.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <iostream>
#include <sqlite3.h>
#include <cmath>
#include <ctime>
#include "DataTables.h"
using namespace std;
void addEZCutsFunctions( sqlite3 *db );
void ezwidth_func(sqlite3_context* context,int n,sqlite3_value** val);
double getTime();
int main(int argc, char* argv[]) {
try {
Database db("test.db");
HeaderRecord h;
ParamRecord p;
SimShowerRecord s;
MuonRecord m;
EZParamRecord e;
h.setDatabaseHandle( db.getHandle() ); // must be called before anything works!
p.setDatabaseHandle( db.getHandle() ); // must be called before anything works!
s.setDatabaseHandle( db.getHandle() ); // must be called before anything works!
m.setDatabaseHandle( db.getHandle() );
e.setDatabaseHandle( db.getHandle() );
addEZCutsFunctions(db.getHandle() );
h.clearTable();
p.clearTable();
s.clearTable();
m.clearTable();
e.clearTable();
h.sourcename="sgra*";
h.nadc=490;
h.writeToDatabase();
for (int i=0; i<1000; i++) {
for (int j=0; j<4; j++) {
p.event_number = i;
p.telescope_id = j;
p.size = i*4.0+j;
p.writeToDatabase();
}
}
for (int i=0; i<100; i++) {
for (int j=0; j<4; j++) {
s.event_number = m.event_number = i;
s.telescope_id = m.telescope_id = j;
s.primary_energy = i*0.1;
m.muonness = i*101;
s.writeToDatabase();
m.writeToDatabase();
}
}
p.prepareToRead();
while (p.readFromDatabase()) {
e.event_number = p.event_number;
e.telescope_id = p.telescope_id;
e.ezwidth = p.width*2+1;
e.ezlength = p.length*2+1;
e.ezsize = p.size*2+1;
e.writeToDatabase();
}
double start,end;
int count;
for (int k=0; k<10; k++) {
cout << "TEST: count(): "<< endl;
start = getTime();
count = p.count();
end= getTime();
cout << "\telapsed="<<end-start<<endl;
cout << "\tcount="<<count << endl;
cout << "TEST: iterate: "<< endl;
count =0;
p.prepareToRead();
start = getTime();
while (p.readFromDatabase()) {
count++;
}
end= getTime();
cout << "\telapsed="<<end-start<<endl;
cout << "\tcount="<<count << endl;
}
cout << "FINISHING"<<endl;
s.finish();
p.finish();
h.finish();
e.finish();
}
catch (runtime_error &e) {
cerr << "RUNTIME ERROR: "<<e.what()<<endl;
}
}
void addEZCutsFunctions( sqlite3 *db ) {
sqlite3_create_function(
db, // database handle
"ezwidth", // function name
3, // number of arguments
SQLITE_UTF8, // text representation
NULL, // arbitrary pointer
ezwidth_func,
NULL, // xStep
NULL // xFinal
);
}
void ezwidth_func(sqlite3_context* context,int n,sqlite3_value** val) {
const double WA = 0.003;
const double WB = 0.04679;
const double WC = 9.866;
const double WD = 0.1832;
const double WE = 0.01534;
const double WF = 0.00248;
const double WGAMMA = 0.949;
const double WCOSPOW = 1.5;
double wid = sqlite3_value_double( val[0] );
double siz = sqlite3_value_double( val[1] );
double zen = sqlite3_value_double( val[2] );
//============================================================
// Calculate ezwidth , ezlength
//============================================================
const double cos60 = cos(60.0*M_PI/180.0);
double ezwidth2, ezlength2;
double ezwidth, ezlength, wzenithfactor;
double x,wshift2,lshift2,lterm1,wterm1;
wzenithfactor = ( (pow(cos60, WGAMMA)/pow(cos60,WCOSPOW))
/pow( cos(zen), WGAMMA ));
x=log(siz/0.4489); // need to divide out .4489 since fit
// values assume 490 camera with no
// corrections
wshift2 = wid*wid - WA;
wterm1 = (wshift2*wzenithfactor>1e-20)?
sqrt(wshift2*wzenithfactor): 1e-20;
ezwidth2 = WA + pow( wterm1 - WB*(x-WC) - WE*pow(x-WC,2)
- WF*pow(x-WC,3) ,2 );
ezwidth = (ezwidth2>0.0)? sqrt(ezwidth2) : 0.0;
cout << "DEBUG: ezwidth="<<ezwidth<<endl;
sqlite3_result_double( context, ezwidth );
};
double getTime() {
struct timeval tv;
struct timezone tz;
double time;
gettimeofday( &tv,&tz);
time = tv.tv_sec + (double)tv.tv_usec/1.0e6;
return time;
}