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