1 |
// |
2 |
// C/C++ headers |
3 |
// |
4 |
#include <iostream> |
5 |
#include <sstream> |
6 |
// |
7 |
// ROOT headers |
8 |
// |
9 |
#include <TString.h> |
10 |
#include <TSQLServer.h> |
11 |
#include <TFile.h> |
12 |
#include <TSystem.h> |
13 |
// |
14 |
// Detector's package headers |
15 |
// |
16 |
#include <GLTables.h> |
17 |
// |
18 |
using namespace std; |
19 |
// |
20 |
// |
21 |
// |
22 |
#include <DarthVaderVerl2.h> |
23 |
// |
24 |
// Usage subroutine |
25 |
// |
26 |
void r2d2usage(){ |
27 |
printf("\nUsage:\n"); |
28 |
printf("\n DarthVader [-h | --help] [--version] [-idRun ID_RUN] [-filename filename]\n"); |
29 |
printf("\n [-host host] [-user username] [-psw password] \n"); |
30 |
printf("\n --version print informations about compilation and exit\n"); |
31 |
printf("\n -h | --help print this help and exit \n"); |
32 |
printf("\n -v | --verbose be verbose [default: print nothing on STDOUT]\n"); |
33 |
printf("\n -idRun ID_RUN: ID number of the run \n"); |
34 |
printf("\n -filename output yoda filename \n"); |
35 |
printf("\n -host name for the host [default = mysql://localhost/pamelaprod]\n"); |
36 |
printf("\n -user username for the DB [default = anonymous] \n"); |
37 |
printf("\n -psw password for the DB [default = \"\"]\n"); |
38 |
printf("\n -convert [ -tzone timezone -dbtime dbtime ] \n"); |
39 |
printf("\n convert the dbtime given in seconds (from the DB) to a string for\n"); |
40 |
printf("\n the given time zone (UTC,GMT,MSK,MSD,CET,CEST are accepted) \n"); |
41 |
printf("\n -runat [ -tzone timezone -time \"yyyy-mm-dd hh:mm:ss\" ] \n"); |
42 |
printf("\n returns run number which contains the given date in the given time zone \n"); |
43 |
printf("\nExamples: \n"); |
44 |
printf("\n R2-D2 -idRun 1085 \n"); |
45 |
printf("\n R2-D2 -filename DW_050208_00900.root \n"); |
46 |
printf("\n R2-D2 -idRun 1085 -filename DW_050208_00900.root \n"); |
47 |
}; |
48 |
// |
49 |
// Here the main |
50 |
// |
51 |
int main(int numinp, char *inps[]){ |
52 |
// |
53 |
// Variables booking |
54 |
// |
55 |
TString message; |
56 |
Int_t error = 0; |
57 |
// |
58 |
UInt_t run = 0ULL; |
59 |
// |
60 |
TString filename = ""; |
61 |
// |
62 |
TSQLServer *dbc = 0; |
63 |
TString host = "mysql://localhost/pamelaprod"; |
64 |
TString user = "anonymous"; |
65 |
TString psw = ""; |
66 |
TString tzone = "MSK"; |
67 |
TString tzone2 = "MSK"; |
68 |
TString runtime = "1970-01-01 00:00:00"; |
69 |
UInt_t dbti = 0; |
70 |
Bool_t convert = false; |
71 |
Bool_t ruti = false; |
72 |
// |
73 |
TSQLResult *pResult; |
74 |
TSQLRow *Row; |
75 |
int t; |
76 |
int r; |
77 |
stringstream myquery; |
78 |
// |
79 |
// Checking input parameters |
80 |
// |
81 |
Int_t i = 0; |
82 |
if ( numinp > 1 ){ |
83 |
while ( i < numinp ){ |
84 |
if ( !strcmp(inps[i],"--version") ){ |
85 |
DarthVaderInfo(true); |
86 |
exit(0); |
87 |
}; |
88 |
if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){ |
89 |
r2d2usage(); |
90 |
exit(0); |
91 |
}; |
92 |
if ( !strcmp(inps[i],"-idRun") ) { |
93 |
if ( numinp-1 < i+1 ) { |
94 |
r2d2usage(); |
95 |
exit(-3); |
96 |
}; |
97 |
run = (UInt_t)atoll(inps[i+1]); |
98 |
}; |
99 |
if ( !strcmp(inps[i],"-filename") ) { |
100 |
if ( numinp-1 < i+1 ){ |
101 |
r2d2usage(); |
102 |
exit(-3); |
103 |
}; |
104 |
filename = (TString)inps[i+1]; |
105 |
}; |
106 |
if ( !strcmp(inps[i],"-host") ) { |
107 |
if ( numinp-1 < i+1 ){ |
108 |
r2d2usage(); |
109 |
exit(-3); |
110 |
}; |
111 |
host = (TString)inps[i+1]; |
112 |
}; |
113 |
if ( !strcmp(inps[i],"-user") ) { |
114 |
if ( numinp-1 < i+1 ){ |
115 |
r2d2usage(); |
116 |
exit(-3); |
117 |
}; |
118 |
user = (TString)inps[i+1]; |
119 |
}; |
120 |
if ( !strcmp(inps[i],"-psw") ) { |
121 |
if ( numinp-1 < i+1 ){ |
122 |
r2d2usage(); |
123 |
exit(-3); |
124 |
}; |
125 |
psw = (TString)inps[i+1]; |
126 |
}; |
127 |
// |
128 |
if ( !strcmp(inps[i],"-convert") ) { |
129 |
convert = true; |
130 |
if ( numinp >= i+2 && !strcmp(inps[i+1],"[") ){ |
131 |
if ( numinp < i+2 ){ |
132 |
r2d2usage(); |
133 |
throw -3; |
134 |
}; |
135 |
i += 2; |
136 |
while ( strcmp(inps[i],"]") ){ |
137 |
if ( !strcmp(inps[i],"-tzone") ) { |
138 |
if ( numinp-1 < i+1 ){ |
139 |
r2d2usage(); |
140 |
exit(-3); |
141 |
}; |
142 |
tzone = (TString)inps[i+1]; |
143 |
}; |
144 |
if ( !strcmp(inps[i],"-dbtime") ) { |
145 |
if ( numinp-1 < i+1 ){ |
146 |
r2d2usage(); |
147 |
exit(-3); |
148 |
}; |
149 |
dbti = (UInt_t)atoll(inps[i+1]); |
150 |
}; |
151 |
i++; |
152 |
if ( i > numinp-1 ){ |
153 |
r2d2usage(); |
154 |
throw -3; |
155 |
}; |
156 |
}; |
157 |
}; |
158 |
}; |
159 |
// |
160 |
if ( !strcmp(inps[i],"-runat") ) { |
161 |
ruti = true; |
162 |
if ( numinp >= i+2 && !strcmp(inps[i+1],"[") ){ |
163 |
if ( numinp < i+2 ){ |
164 |
r2d2usage(); |
165 |
throw -3; |
166 |
}; |
167 |
i += 2; |
168 |
while ( strcmp(inps[i],"]") ){ |
169 |
if ( !strcmp(inps[i],"-tzone") ) { |
170 |
if ( numinp-1 < i+1 ){ |
171 |
r2d2usage(); |
172 |
exit(-3); |
173 |
}; |
174 |
tzone2 = (TString)inps[i+1]; |
175 |
}; |
176 |
if ( !strcmp(inps[i],"-time") ) { |
177 |
if ( numinp-1 < i+1 ){ |
178 |
r2d2usage(); |
179 |
exit(-3); |
180 |
}; |
181 |
runtime = (TString)inps[i+1]; |
182 |
}; |
183 |
i++; |
184 |
if ( i > numinp-1 ){ |
185 |
r2d2usage(); |
186 |
throw -3; |
187 |
}; |
188 |
}; |
189 |
}; |
190 |
}; |
191 |
// |
192 |
i++; |
193 |
}; |
194 |
// |
195 |
} else { |
196 |
// |
197 |
// no input parameters exit with error, we need at least the run id. |
198 |
// |
199 |
r2d2usage(); |
200 |
exit(-2); |
201 |
}; |
202 |
// |
203 |
// Connect to the DB |
204 |
// |
205 |
dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); |
206 |
if( !dbc ) throw -2; |
207 |
// |
208 |
bool connect = dbc->IsConnected(); |
209 |
// |
210 |
if( !connect ){ |
211 |
printf(" Error, not connected to DB\n"); |
212 |
exit(-1); |
213 |
}; |
214 |
// |
215 |
GL_ROOT *glroot = new GL_ROOT(); |
216 |
GL_RUN *glrun = new GL_RUN(); |
217 |
GL_TIMESYNC *dbtime = new GL_TIMESYNC(); |
218 |
// |
219 |
// At which date correspond the DB time "dbti"? |
220 |
// |
221 |
if ( convert ){ |
222 |
printf("\n DB time %u is %s %s \n",dbti,dbtime->ConvertTime(tzone.Data(),dbti).Data(),tzone.Data()); |
223 |
}; |
224 |
// |
225 |
// Which run contains the date "runtime"? |
226 |
// |
227 |
if ( ruti ){ |
228 |
// |
229 |
TDatime *time = new TDatime(runtime.Data()); |
230 |
UInt_t dbti = time->Convert(); |
231 |
// |
232 |
TString thetime = dbtime->UnConvertTime(tzone2.Data(),dbti); |
233 |
// |
234 |
TDatime *time2 = new TDatime(thetime.Data()); |
235 |
UInt_t mytime = time2->Convert(); |
236 |
// |
237 |
myquery.str(""); |
238 |
myquery << "select "; |
239 |
myquery << " ID "; |
240 |
myquery << " from GL_RUN where RUNHEADER_TIME<=" << mytime << " AND " |
241 |
<< " RUNTRAILER_TIME>=" << mytime << " ;"; |
242 |
// printf("myquery is %s \n",myquery.str().c_str()); |
243 |
pResult = dbc->Query(myquery.str().c_str()); |
244 |
for( r=0; r < 1000; r++){ |
245 |
Row = pResult->Next(); |
246 |
if ( !r && !Row ){ |
247 |
printf("\n No run contains date %s %s (DB time %u )\n",runtime.Data(),tzone2.Data(),mytime); |
248 |
}; |
249 |
if( Row == NULL ) break; |
250 |
printf("\n Date %s %s (DB time %u ) is contained in run %u \n",runtime.Data(),tzone2.Data(),mytime,(UInt_t)atoll(Row->GetField(0))); |
251 |
}; |
252 |
}; |
253 |
// |
254 |
// To which file the run "run" belongs? |
255 |
// |
256 |
if ( run != 0 ){ |
257 |
glrun->Clear(); |
258 |
error = glrun->Query_GL_RUN(run,dbc); |
259 |
glroot->Clear(); |
260 |
error = glroot->Query_GL_ROOT(glrun->ID_ROOT_L0,dbc); |
261 |
if ( error ){ |
262 |
printf(" Error querying the DB! \n"); |
263 |
exit(-4); |
264 |
}; |
265 |
if ( !glrun->ID ){ |
266 |
printf("\n No run with ID=%u in the DB!\n",run); |
267 |
} else { |
268 |
printf("\n Run %u belongs to file %s \n",run,(glroot->PATH+glroot->NAME).Data()); |
269 |
}; |
270 |
}; |
271 |
// |
272 |
// Which runs are contained in the file "filename"? |
273 |
// |
274 |
if ( strcmp(filename.Data(),"") ){ |
275 |
// ---------------- |
276 |
Int_t ID = 0; |
277 |
Int_t ID_RAW = 0; |
278 |
// |
279 |
const char *rawpath = ""; |
280 |
const char *rawname = ""; |
281 |
// |
282 |
myquery.str(""); |
283 |
myquery << "select "; |
284 |
myquery << " ID"; |
285 |
myquery << ",ID_RAW"; |
286 |
myquery << ",PATH"; |
287 |
myquery << ",NAME"; |
288 |
myquery << " from GL_ROOT where NAME=\"" << filename.Data() << "\";"; |
289 |
pResult = dbc->Query(myquery.str().c_str()); |
290 |
for( r=0; r < 1000; r++){ |
291 |
Row = pResult->Next(); |
292 |
if( Row == NULL ) break; |
293 |
for( t = 0; t < pResult->GetFieldCount(); t++){ |
294 |
if(t==0) ID = atoi(Row->GetField(t)); |
295 |
if(t==1) ID_RAW = atoi(Row->GetField(t)); |
296 |
}; |
297 |
}; |
298 |
delete pResult; |
299 |
if ( !ID && !ID_RAW ){ |
300 |
printf("\n No file with name %s in the DB!\n",filename.Data()); |
301 |
} else { |
302 |
myquery.str(""); |
303 |
myquery << "select "; |
304 |
myquery << " ID,RUNHEADER_TIME,RUNTRAILER_TIME "; |
305 |
myquery << " from GL_RUN where ID_ROOT_L0=" << ID << ";"; |
306 |
pResult = dbc->Query(myquery.str().c_str()); |
307 |
for( r=0; r < 1000; r++){ |
308 |
Row = pResult->Next(); |
309 |
if ( !r && !Row ){ |
310 |
printf("\n No run associated to the file %s \n",filename.Data()); |
311 |
}; |
312 |
if( Row == NULL ) break; |
313 |
if ( !r ) printf("\n File %s contains the following runs: \n\n",filename.Data()); |
314 |
printf(" => ID = %i _-_-_ the run started at %s UTC ended at %s UTC \n\n",(UInt_t)atoll(Row->GetField(0)),dbtime->ConvertTime("UTC",(UInt_t)atoll(Row->GetField(1))).Data(),dbtime->ConvertTime("UTC",(UInt_t)atoll(Row->GetField(2))).Data()); |
315 |
}; |
316 |
delete pResult; |
317 |
myquery.str(""); |
318 |
myquery << "select "; |
319 |
myquery << " PATH,NAME"; |
320 |
myquery << " from GL_RAW where ID=" << ID_RAW << ";"; |
321 |
pResult = dbc->Query(myquery.str().c_str()); |
322 |
for( r=0; r < 1000; r++){ |
323 |
Row = pResult->Next(); |
324 |
if( Row == NULL ) break; |
325 |
for( t = 0; t < pResult->GetFieldCount(); t++){ |
326 |
if(t==0) rawpath = Row->GetField(t); |
327 |
if(t==1) rawname = Row->GetField(t); |
328 |
}; |
329 |
}; |
330 |
delete pResult; |
331 |
printf("\n File %s belongs to raw data file %s/%s \n",filename.Data(),rawpath,rawname); |
332 |
}; |
333 |
}; |
334 |
// |
335 |
// Close the DB connection |
336 |
// |
337 |
if ( dbc ) dbc->Close(); |
338 |
// |
339 |
printf("\n"); |
340 |
// |
341 |
exit(0); |
342 |
} |