BOSS 7.1.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Metadata.cxx
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/calibUtil/src/Metadata.cxx,v 1.45 2020/09/28 05:22:41 maqm Exp $
2
3/*
4#ifdef WIN32
5#include <windows.h>
6#endif
7*/
8
10#include "facilities/Util.h"
14#include "GaudiKernel/Bootstrap.h"
15#include "GaudiKernel/ISvcLocator.h"
16
17
18
21#include "rdbModel/Rdb.h"
26#include <iostream>
27#include <cstdio>
28#include <mysql.h>
29#include "TTree.h"
30#include "TBuffer.h"
31using namespace std;
32
33namespace calibUtil {
34
35 // Might be getting rid of this
36 // const unsigned int Metadata::s_rowReady = Metadata::eOpened
37 // | Metadata::eValid | Metadata::eInputDesc | Metadata::eComment;
38
39 Metadata::Metadata(const std::string& host, const std::string& table,
40 const std::string& dbName)
41 : m_readCxt(0), m_writeCxt(0), // m_row(""), m_rowStatus(0),
42 m_host(host), m_table(table), m_dbName(dbName), m_man(0), m_rdb(0),
43 m_match(false) {
44 if (table.compare("*") == 0) m_table = std::string("$(MYSQL_METATABLE)");
45 if (host.compare("*") == 0) m_host = std::string("$(MYSQL_HOST)");
46
47 int nsub = facilities::Util::expandEnvVar(&m_table);
48 // If this doesn't work, use default
49 if (nsub < 0) m_table = std::string("metadata");
50 StatusCode sc=Gaudi::svcLocator()->service("DatabaseSvc", m_dbsvc, true);
51 if ( !sc.isSuccess() ) {
52 std::cout <<"Metadata ERROR ::Could not retrieve the DataBaseSvc" << std::endl;;
53 exit(1);
54 }
55 }
56
60 if (m_man) delete m_man;
61 }
62
63
64 Metadata::eRet Metadata::fetchUser(std::string& user) {
65 // WARNING: Windows and Linux/Solaris use different standard variables
66 // for login name. The test used below isn't quite right
67 // since one could conceivably compile with gcc on cygwin/Windows.
68#ifdef __GNUG__
69 user = std::string("$(USER)");
70#else
71 user = std::string("$(USERNAME)");
72#endif
73
74 int nsub = facilities::Util::expandEnvVar(&user);
75 if (nsub == 1) {
76 return RETOk;
77 }
78 else {
79 user = std::string("");
80 return RETBadValue;
81 }
82 }
83
84 // The next 5 methods concern connection to the server
85 bool Metadata::connect(rdbModel::Connection* cxt, std::string& host,
86 const std::string& user,
87 const std::string& pw, eRet& err,
88 const std::string& dbName) {
89
90 int nSub = facilities::Util::expandEnvVar(&host);
91 if (nSub < 0) {
92 err = RETBadHost;
93 return false;
94 }
95
96 bool connected = cxt->open(host, user, pw, dbName);
97 if (connected) {
98 err = RETOk;
99 return true;
100 }
101 else {
102 err = RETNoConnect;
103 return false;
104 }
105 }
106
108 if (m_readCxt == 0) {
109 // for now use std::cout, std::cerr
110 m_readCxt = new rdbModel::MysqlConnection();
111 // bool ok = connect(m_readCxt, m_host, std::string("maqm"),
112 // std::string("maqm_offline"), err, m_dbName);
113 bool ok = connect(m_readCxt, m_host, std::string("guest"),
114 std::string("guestpass"), err, m_dbName);
115 if (!ok) {
116 delete m_readCxt;
117 m_readCxt = 0;
118 } /* else { // look for compatible schema
119 std::string schema =
120 std::string("$(RDBMODELROOT)/xml/")+ m_dbName + ".xml";
121 err = compareSchema(m_readCxt, schema);
122 }*/
123 return ok;
124 }
125
126 else return true;
127 }
128
129 bool Metadata::connectWrite(eRet& err) {
130 if (m_writeCxt == 0) {
131 m_writeCxt = new rdbModel::MysqlConnection();
132 bool ok;
133 if (m_dbName == std::string("calib_test") ) {
134 ok = connect(m_writeCxt, m_host, std::string("calib_tester"),
135 std::string("udine"), err, m_dbName);
136 }
137 else {
138 ok = connect(m_writeCxt, m_host, std::string("calibrator"),
139 std::string("calibrator"), err, m_dbName);
140 }
141 if (!ok) {
142 delete m_readCxt;
143 m_readCxt = 0;
144 } else { // look for compatible schema
145 std::string schema =
146 std::string("$(RDBMODELROOT)/xml/")+ m_dbName + ".xml";
147 err = compareSchema(m_writeCxt, schema);
148 }
149
150 return ok;
151 }
152 else return true;
153 }
154
156 if (m_readCxt) {
157 m_readCxt->close();
158 delete m_readCxt;
159 m_readCxt = 0;
160 }
161 }
162
164 if (m_writeCxt) {
165 m_writeCxt->close();
166 delete m_writeCxt;
167 m_writeCxt = 0;
168 }
169 }
170
172 Metadata::findSoonAfter(unsigned int *ser,
173 const std::string& calibType,
174 const std::string& SftVer,
175 const std::string& cal_ver,
176 const std::string& cal_par,
177 const std::string& rec_alg,
178 const std::string& rec_par,
179 const std::string& machine,
180 const std::string& flavor) {
181 using namespace rdbModel;
182
183 eRet ret;
184 *ser = 0;
185 if (!m_readCxt) {
186 connectRead(ret);
187 if (ret != RETOk) return ret;
188 }
189
190 StringVector orderBy;
191
192 std::vector<Assertion::Operator *> conditions;
193 conditions.reserve(8);
194
195 Assertion::Operator completeOp(OPTYPEequal, "status", "OK",
196 FIELDTYPEold, FIELDTYPElit);
197 // false, true);
198 Assertion::Operator calibTypeOp(OPTYPEequal, "calib_type", calibType,
199 FIELDTYPEold, FIELDTYPElit);
200 // false, true);
201 // Assertion::Operator flavorOp(OPTYPEequal, "flavor", flavor,
202 // FIELDTYPEold, FIELDTYPElit);
203 //false, true);
204 Assertion::Operator sftverOp(OPTYPEequal, "SftVer",SftVer,
205 FIELDTYPEold, FIELDTYPElit);
206 Assertion::Operator calverOp(OPTYPEequal, "cal_ver",cal_ver,
207 FIELDTYPEold, FIELDTYPElit);
208 Assertion::Operator calparOp(OPTYPEequal, "cal_par",cal_par,
209 FIELDTYPEold, FIELDTYPElit);
210 Assertion::Operator recalgOp(OPTYPEequal, "rec_alg",rec_alg,
211 FIELDTYPEold, FIELDTYPElit);
212 Assertion::Operator recparOp(OPTYPEequal, "rec_par",rec_par,
213 FIELDTYPEold, FIELDTYPElit);
214 Assertion::Operator machineOp(OPTYPEequal, "machine",machine,
215 FIELDTYPEold, FIELDTYPElit);
216
217 int run=10005;
218 std::string s_run;
219 std::cout<<"run no is::"<<s_run<<std::endl;
220 facilities::Util::itoa(run,s_run);
221 Assertion::Operator runfrmOp(OPTYPElessOrEqual,"RunFrom",s_run,
222 FIELDTYPEold, FIELDTYPElit);
223
224 Assertion::Operator runtoOp(OPTYPEgreaterOrEqual,"RunTo",s_run,
225 FIELDTYPEold, FIELDTYPElit);
226
227 conditions.push_back(&completeOp);
228 conditions.push_back(&calibTypeOp);
229 // conditions.push_back(&flavorOp);
230 conditions.push_back(&sftverOp);
231 conditions.push_back(&calverOp);
232 conditions.push_back(&calparOp);
233 conditions.push_back(&recalgOp);
234 conditions.push_back(&recparOp);
235 conditions.push_back(&machineOp);
236 conditions.push_back(&runfrmOp);
237 conditions.push_back(&runtoOp);
238 ret = doSelect(conditions, orderBy, ser);
239
240 return ret;
241 }
242
244 const std::string& calibType,
245 const std::string& SftVer,
246 const std::string& cal_ver,
247 const std::string& cal_par,
248 const std::string& rec_alg,
249 const std::string& rec_par,
250 const std::string& machine,
251 const std::string& flavor)
252 {
253 using namespace rdbModel;
254
255 eRet ret;
256 *ser = 0;
257 if (!m_readCxt) {
258 connectRead(ret);
259 if (ret != RETOk) return ret;
260 }
261
262 // Sort rows by timestamp. Would like most recent first
263 StringVector orderBy;
264
265 std::vector<Assertion::Operator *> conditions;
266 conditions.reserve(8);
267 Assertion::Operator completeOp(OPTYPEequal, "status", "OK",
268 FIELDTYPEold, FIELDTYPElit);
269 // false, true);
270
271 Assertion::Operator calibTypeOp(OPTYPEequal, "calib_type", calibType,
272 FIELDTYPEold, FIELDTYPElit);
273 // false, true);
274 // Assertion::Operator flavorOp(OPTYPEequal, "flavor", flavor,
275 // FIELDTYPEold, FIELDTYPElit);
276 // false, true);
277 Assertion::Operator sftverOp(OPTYPEequal, "SftVer",SftVer,
278 FIELDTYPEold, FIELDTYPElit);
279 Assertion::Operator calverOp(OPTYPEequal, "cal_ver",cal_ver,
280 FIELDTYPEold, FIELDTYPElit);
281 Assertion::Operator calparOp(OPTYPEequal, "cal_par",cal_par,
282 FIELDTYPEold, FIELDTYPElit);
283 Assertion::Operator recalgOp(OPTYPEequal, "rec_alg",rec_alg,
284 FIELDTYPEold, FIELDTYPElit);
285 Assertion::Operator recparOp(OPTYPEequal, "rec_par",rec_par,
286 FIELDTYPEold, FIELDTYPElit);
287 Assertion::Operator machineOp(OPTYPEequal, "machine",machine,
288 FIELDTYPEold, FIELDTYPElit);
289 int run=10005;
290 std::string s_run;
291 facilities::Util::itoa(run,s_run);
292 Assertion::Operator runfrmOp(OPTYPElessOrEqual,"RunFrom",s_run,
293 FIELDTYPEold, FIELDTYPElit);
294
295 Assertion::Operator runtoOp(OPTYPEgreaterOrEqual,"RunTo",s_run,
296 FIELDTYPEold, FIELDTYPElit);
297
298 conditions.push_back(&completeOp);
299 conditions.push_back(&calibTypeOp);
300 // conditions.push_back(&flavorOp);
301 conditions.push_back(&sftverOp);
302 conditions.push_back(&calverOp);
303 conditions.push_back(&calparOp);
304 conditions.push_back(&recalgOp);
305 conditions.push_back(&recparOp);
306 conditions.push_back(&machineOp);
307 conditions.push_back(&runfrmOp);
308 conditions.push_back(&runtoOp);
309 // Finally, set a value for PROC_LEVEL and make the query (ies)
310 return doSelect(conditions, orderBy, ser);
311 }
312
313
314 Metadata::eRet Metadata::getReadInfo(unsigned int serialNo,
315 int *runFrm,
316 int *runTo,
317 std::string& dataFmt,
318 std::string& filename) {
319 using namespace rdbModel;
320 eRet ret;
321 if (!m_readCxt) {
322 connectRead(ret);
323 if (ret != RETOk) {
324 return ret;
325 }
326 }
327 StringVector colNames(4);
328 colNames[0] = "data_fmt";
329 colNames[1] = "data_ident";
330 colNames[2] = "RunFrom";
331 colNames[3] = "RunTo";
332
333 std::string serNoVal;
334 facilities::Util::itoa(serialNo, serNoVal);
335 Assertion::Operator* serOp =
336 new Assertion::Operator(OPTYPEequal, "ser_no", serNoVal,
337 FIELDTYPEold, FIELDTYPElit);
338 //false, true);
339 Assertion whereClause(serOp);
340 StringVector orderBy;
341 orderBy.clear();
342
343 // make the query
344 ResultHandle* results = 0;
345 try {
346
347 results = m_readCxt->select(m_table, colNames, orderBy, &whereClause);
348
349 }
350 catch (RdbException ex) {
351 std::cout << ex.getMsg();
352 return RETMySQLError;
353 }
354
355 if (!results) return RETMySQLError;
356 if (!results->getNRows() ) return RETBadValue;
357
358 std::vector<std::string> fields;
359 std::cout<<"test id \\"<<std::endl;
360 results->getRow(fields);
361 dataFmt = fields[0];
362 filename = fields[1];
363 std::string runfrm,runto;
364 runfrm=fields[2];
365 runto=fields[3];
366 *runFrm=facilities::Util::atoi(runfrm);
367 *runTo=facilities::Util::atoi(runto);
368 delete results;
369 //runFrm=fields[2];
370 //runTo=fields[3];
371 // std::cout<<"runfrm is:"<<runFrm<<"runto is:"<<runTo<<std::endl;
372 if ((dataFmt == "") || (filename == "")) return RETBadValue;
373 return RETOk;
374 }
375
376//InjSigInterval Qiumei Ma
378 int *runFrm,
379 int *runTo,
380 std::string& calParVer,
382 int runNo,
383 std::string& sftver) {
384 using namespace rdbModel;
385 eRet ret;
386 char stmt1[200];
387 int run_No =runNo;
388
389 if(sftver=="default")
390 sftver = getenv("BES_RELEASE");
391 const char* SftVer = sftver.c_str();
392
393 if(calParVer!="default"){
394 const char* calpar = calParVer.c_str();
395 sprintf(stmt1,"select SigInterval,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from InjSigInterval where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = '%s'",SftVer,run_No,run_No,calpar);}
396
397 if(calParVer=="default"){
398 sprintf(stmt1,"select SigInterval,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from InjSigInterval where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);}
399 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
400 if(row_no<1){
401 std::cout<<"ERROR:error searching InjSigInterval calibration Data in the database with: "<<stmt1<<std::endl; return RETMySQLError;
402 }
403 int RunFrom,RunTo;
404 DatabaseRecord& records = *res[0];
405 sscanf(records["RunFrom"], "%d", &RunFrom);
406 sscanf(records["RunTo"], "%d", &RunTo);
407 cout << "@table SigInterval: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
408 return RETOk;
409 }
410
411
413 int *runFrm,
414 int *runTo,
415 std::string& calParVer,
417 int runNo,
418 std::string& sftver) {
419 using namespace rdbModel;
420 eRet ret;
421 char stmt1[200];
422 int run_No =runNo;
423
424 if(sftver=="default")
425 sftver = getenv("BES_RELEASE");
426 const char* SftVer = sftver.c_str();
427 cout<<"getReadInjSigTimeInfo start:::::"<<endl;
428 if(calParVer!="default"){
429 const char* calpar = calParVer.c_str();
430 sprintf(stmt1,"select ist,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from InjSigTime where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = '%s'",SftVer,run_No,run_No,calpar);}
431
432 if(calParVer=="default"){
433 sprintf(stmt1,"select ist,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from InjSigTime where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);}
434 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
435 if(row_no<1){
436 std::cout<<"ERROR:error searching InjSigTime calibration Data in the database with: "<<stmt1<<std::endl; return RETMySQLError;
437 }
438 int RunFrom,RunTo;
439 DatabaseRecord& records = *res[0];
440 sscanf(records["RunFrom"], "%d", &RunFrom);
441 sscanf(records["RunTo"], "%d", &RunTo);
442 cout << "@table InjSigTimeInfo: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
443 return RETOk;
444
445 }
446
448 int *runFrm,
449 int *runTo,
450 std::string& calParVer,
452 int runNo,
453 std::string& sftver) {
454 using namespace rdbModel;
455 eRet ret;
456 char stmt1[200];
457 int run_No =runNo;
458
459 if(sftver=="default")
460 sftver = getenv("BES_RELEASE");
461 const char* SftVer = sftver.c_str();
462 if(calParVer!="default"){
463 const char* calpar = calParVer.c_str();
464 sprintf(stmt1,"select data,oef,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from OffEvtFilter where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = '%s'",SftVer,run_No,run_No,calpar);}
465
466 if(calParVer=="default"){
467 sprintf(stmt1,"select data,oef,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from OffEvtFilter where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);}
468 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
469 if(row_no<1){
470 std::cout<<"ERROR:error searching OffEvtFilter calibration Data in the database with: "<<stmt1<<std::endl; return RETMySQLError;
471 }
472 int RunFrom,RunTo;
473 DatabaseRecord& records = *res[0];
474 sscanf(records["RunFrom"], "%d", &RunFrom);
475 sscanf(records["RunTo"], "%d", &RunTo);
476 cout << "@table OffEvtFilter: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
477
478 return RETOk;
479
480 }
481
482
484 int run,
485 int FirstEvent,
486 std::string& Version,
488 {
489 using namespace rdbModel;
490 eRet ret;
491 char sql[200];
492 const char* parVer = Version.c_str();
493 sprintf(sql,"select Run,FinalETS,FileName,FilePath from CorrectedETS where Run= %d and FirstEvent = %d and Version = %d",run,FirstEvent,1);
494
495 int row_no = m_dbsvc->query("offlinedb", sql, res);
496 string FileName;
497 FileName=(*res[0])["FileName"];
498 std::cout<<"FileName is::::"<<FileName<<std::endl;
499 if(row_no<1){
500 std::cout<<"ERROR:error searching CorrectedETS calibration Data in the database with: "<<sql<<std::endl;
501 return RETMySQLError;
502 }
503
504 return RETOk;
505 }
506
508 int *runFrm,
509 int *runTo,
510 std::string& calParVer,
512 int runNo,
513 std::string& sftver) {
514 using namespace rdbModel;
515 eRet ret;
516 char stmt1[300];
517 int run_No =runNo;
518 if(sftver=="default")
519 sftver = getenv("BES_RELEASE");
520 const char* SftVer = sftver.c_str();
521
522 if(calParVer!="default"){
523 const char* calpar = calParVer.c_str();
524 sprintf(stmt1,"select BarTofPar,EndTofPar,TofConPar,BarTof,EtfTofPar,EtfTofBunch,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from TofCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
525 }
526 if(calParVer=="default"){
527 sprintf(stmt1,"select BarTofPar,EndTofPar,TofConPar,BarTof,EtfTofPar,EtfTofBunch,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from TofCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
528 }
529
530 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
531 if(row_no<1){
532 std::cout<<"ERROR:error searching TOF calibration Data in the database with: "<<stmt1<<std::endl;
533 return RETMySQLError;
534 }
535 int RunFrom,RunTo;
536 DatabaseRecord& records = *res[0];
537 sscanf(records["RunFrom"], "%d", &RunFrom);
538 sscanf(records["RunTo"], "%d", &RunTo);
539 cout << "@table TofCalConst: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
540 return RETOk;
541 }
542
544 int *runFrm,
545 int *runTo,
546 std::string& calParVer,
548 int runNo,
549 std::string& sftver) {
550 using namespace rdbModel;
551 eRet ret;
552 char stmt1[300];
553 int run_No =runNo;
554 //char* SftVer = getenv("BES_RELEASE");
555 if(sftver=="default")
556 sftver = getenv("BES_RELEASE");
557 const char* SftVer = sftver.c_str();
558
559 if(calParVer!="default"){
560 const char* calpar = calParVer.c_str();
561 sprintf(stmt1,"select LayTree,BoxTree,StrTree,ResTree,ClsTree,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MucCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
562 }
563
564 if(calParVer=="default"){
565 // sprintf(stmt1,"select SftVer,LayTree,BoxTree,StrTree,ResTree,ClsTree,RunFrom,RunTo,max(CalParVer),FileName from MucCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by CalParVer",SftVer,run_No,run_No);
566 sprintf(stmt1,"select LayTree,BoxTree,StrTree,ResTree,ClsTree,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MucCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
567
568 }
569
570 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
571 if(row_no<1){
572 std::cout<<"ERROR:error searching MUC calibration Data in the database with: "<<stmt1<<std::endl;
573 return RETMySQLError;
574 }
575 int RunFrom,RunTo;
576 DatabaseRecord& records = *res[0];
577 sscanf(records["RunFrom"], "%d", &RunFrom);
578 sscanf(records["RunTo"], "%d", &RunTo);
579 cout << "@table Muc: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
580 return RETOk;
581 }
582
584 int *runFrm,
585 int *runTo,
586 std::string& calParVer,
588 int runNo,
589 std::string& sftver) {
590 using namespace rdbModel;
591 eRet ret;
592 char stmt1[200];
593 int run_No =runNo;
594
595 if(sftver=="default")
596 sftver = getenv("BES_RELEASE");
597 const char* SftVer = sftver.c_str();
598
599
600 if(calParVer!="default"){
601 const char* calpar = calParVer.c_str();
602 sprintf(stmt1,"select NewXtTrees,XtTree,QtTree,T0Tree,SdTree,R2tTrees,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MdcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = '%s'",SftVer,run_No,run_No,calpar);}
603
604 if(calParVer=="default"){
605 //sprintf(stmt1,"select NewXtTrees,XtTree,QtTree,T0Tree,SdTree,R2tTrees,RunFrom,RunTo,max(CalParVer),FileName from MdcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by CalParVer",SftVer,run_No,run_No);}
606 sprintf(stmt1,"select NewXtTrees,XtTree,QtTree,T0Tree,SdTree,R2tTrees,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MdcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);}
607
608 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
609 if(row_no<1){
610 std::cout<<"ERROR:error searching Mdc calibration Data in the database with: "<<stmt1<<std::endl;
611 return RETMySQLError;
612 }
613 int RunFrom,RunTo;
614 DatabaseRecord& records = *res[0];
615 sscanf(records["RunFrom"], "%d", &RunFrom);
616 sscanf(records["RunTo"], "%d", &RunTo);
617 cout << "@table Mdc: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
618
619 return RETOk;
620}
621
622
623
625 int *runFrm,
626 int *runTo,
627 std::string& calParVer,
629 int runNo,
630 std::string& sftver) {
631 using namespace rdbModel;
632 eRet ret;
633 char stmt1[300];
634 int run_No =runNo;
635 if(sftver=="default")
636 sftver = getenv("BES_RELEASE");
637 const char* SftVer = sftver.c_str();
638
639 if(calParVer!="default"){
640 const char* calpar = calParVer.c_str();
641 sprintf(stmt1,"select DigiCalibConst,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EmcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
642 }
643
644 if(calParVer=="default"){
645 //sprintf(stmt1,"select DigiCalibConst,RunFrom,RunTo,max(CalParVer),FileName from EmcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by CalParVer",SftVer,run_No,run_No);
646 sprintf(stmt1,"select DigiCalibConst,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EmcCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
647 }
648
649 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
650 if(row_no<1){
651 std::cout<<"ERROR:error searching EMC calibration Data in the database with: "<<stmt1<<std::endl;
652 return RETMySQLError;
653 }
654 int RunFrom,RunTo;
655 DatabaseRecord& records = *res[0];
656 sscanf(records["RunFrom"], "%d", &RunFrom);
657 sscanf(records["RunTo"], "%d", &RunTo);
658 cout << "@table Emc: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
659 return RETOk;
660}
661
662
664 int *runFrm,
665 int *runTo,
666 std::string& calParVer,
668 int runNo,
669 std::string& sftver) {
670 using namespace rdbModel;
671 eRet ret;
672 char stmt1[400];
673 int run_No =runNo;
674 if(sftver=="default")
675 sftver = getenv("BES_RELEASE");
676 const char* SftVer = sftver.c_str();
677
678 if(calParVer!="default"){
679 const char* calpar = calParVer.c_str();
680 sprintf(stmt1,"select DriftDist,EntranceAng,MeanGain,GasGain,LayerGain,Resolution,WireGain,ZDep,RunGain,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from DedxCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
681
682 }
683 if(calParVer=="default"){
684 // sprintf(stmt1,"select DriftDist,EntranceAng,MeanGain,GasGain,LayerGain,Resolution,WireGain,ZDep,RunGain,RunFrom,RunTo,max(CalParVer),FileName from DedxCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by CalParVer",SftVer,run_No,run_No);
685 sprintf(stmt1,"select DriftDist,EntranceAng,MeanGain,GasGain,LayerGain,Resolution,WireGain,ZDep,RunGain,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from DedxCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
686 }
687
688 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
689 if(row_no<1){
690 std::cout<<"ERROR:error searching Dedx calibration Data in the database with: "<<stmt1<<std::endl;
691 return RETMySQLError;
692 }
693 int RunFrom,RunTo;
694 DatabaseRecord& records = *res[0];
695 sscanf(records["RunFrom"], "%d", &RunFrom);
696 sscanf(records["RunTo"], "%d", &RunTo);
697 cout << "@table Dedx: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
698 return RETOk;
699}
700
701
703 int *runFrm,
704 int *runTo,
705 std::string& calParVer,
707 int runNo,
708 std::string& sftver) {
709 using namespace rdbModel;
710 eRet ret;
711 char stmt1[400];
712 int run_No =runNo;
713 if(sftver=="default")
714 sftver = getenv("BES_RELEASE");
715 const char* SftVer = sftver.c_str();
716
717 if(calParVer!="default"){
718 const char* calpar = calParVer.c_str();
719 sprintf(stmt1,"select EsTimeH2,EsTimeH9,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EsTimeCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
720 }
721
722 if(calParVer=="default"){
723 sprintf(stmt1,"select EsTimeH2,EsTimeH9,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EsTimeCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
724 }
725 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
726 if(row_no<1){
727 std::cout<<"ERROR:error searching EsTime calibration Data in the database with: "<<stmt1<<std::endl;
728 return RETMySQLError;
729 }
730 int RunFrom,RunTo;
731 DatabaseRecord& records = *res[0];
732 sscanf(records["RunFrom"], "%d", &RunFrom);
733 sscanf(records["RunTo"], "%d", &RunTo);
734 cout << "@table EsTime: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
735 return RETOk;
736}
737
738
739//get EstTof Information from mysql Database
741 int *runFrm,
742 int *runTo,
743 std::string& calParVer,
745 int runNo,
746 std::string& sftver) {
747 using namespace rdbModel;
748 eRet ret;
749 char stmt1[200];
750 int run_No =runNo;
751 if(sftver=="default")
752 sftver = getenv("BES_RELEASE");
753 const char* SftVer = sftver.c_str();
754
755 if(calParVer!="default"){
756 const char* calpar = calParVer.c_str();
757 sprintf(stmt1,"select BarTofPar,EndTofPar,TofConPar,BarTof,EtfTofPar,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EstTofCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
758 }
759 if(calParVer=="default"){
760 // sprintf(stmt1,"select BarTofPar,EndTofPar,TofConPar,BarTof,RunFrom,RunTo,max(CalParVer),FileName from EstTofCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by CalParVer",SftVer,run_No,run_No);
761 sprintf(stmt1,"select BarTofPar,EndTofPar,TofConPar,BarTof,EtfTofPar,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from EstTofCalConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
762 }
763
764 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
765 if(row_no<1){
766 std::cout<<"ERROR:error searching EstTOF calibration Data in the database with: "<<stmt1<<std::endl;
767 return RETMySQLError;
768 }
769 int RunFrom,RunTo;
770 DatabaseRecord& records = *res[0];
771 sscanf(records["RunFrom"], "%d", &RunFrom);
772 sscanf(records["RunTo"], "%d", &RunTo);
773 cout << "@table EstTof: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
774 return RETOk;
775}
776
777
779 int *runFrm,
780 int *runTo,
781 std::string& calParVer,
783 int runNo,
784 std::string& sftver) {
785 using namespace rdbModel;
786 eRet ret;
787 char stmt1[200];
788 int run_No =runNo;
789 if(sftver=="default")
790 sftver = getenv("BES_RELEASE");
791 const char* SftVer = sftver.c_str();
792
793 if(calParVer!="default"){
794 const char* calpar = calParVer.c_str();
795 sprintf(stmt1,"select BTofSim,ETofSim,SimConstants,RunFrom,RunTo,TofSimParVer,FileName,Status,SftVer from TofSimSvc where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and TofSimParVer = %s",SftVer,run_No,run_No,calpar);
796 }
797 if(calParVer=="default"){
798 // sprintf(stmt1,"select BTofSim,ETofSim,SimConstants,RunFrom,RunTo,max(TofSimParVer),FileName from TofSimSvc where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by TofSimParVer",SftVer,run_No,run_No);
799 sprintf(stmt1,"select BTofSim,ETofSim,SimConstants,RunFrom,RunTo,TofSimParVer,FileName,Status,SftVer from TofSimSvc where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by TofSimParVer desc",SftVer,run_No,run_No);
800 }
801
802
803 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
804 if(row_no<1){
805 std::cout<<"ERROR:error searching TOFSim calibration Data in the database with: "<<stmt1<<std::endl;
806 return RETMySQLError;
807 }
808 int RunFrom,RunTo;
809 DatabaseRecord& records = *res[0];
810 sscanf(records["RunFrom"], "%d", &RunFrom);
811 sscanf(records["RunTo"], "%d", &RunTo);
812 cout << "@table TofSim: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
813 return RETOk;
814}
815
816
818 int *runFrm,
819 int *runTo,
820 std::string& calParVer,
822 int runNo,
823 std::string& sftver) {
824 using namespace rdbModel;
825 eRet ret;
826 char stmt1[200];
827 int run_No =runNo;
828 if(sftver=="default")
829 sftver = getenv("BES_RELEASE");
830 const char* SftVer = sftver.c_str();
831
832 if(calParVer!="default"){
833 const char* calpar = calParVer.c_str();
834 sprintf(stmt1,"select TH1F_Col,bin,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from DedxSim where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
835 }
836 if(calParVer=="default"){
837 sprintf(stmt1,"select TH1F_Col,bin,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from DedxSim where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
838 }
839
840 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
841 if(row_no<1){
842 std::cout<<"ERROR:error searching DedxSim calibration Data in the database with: "<<stmt1<<std::endl;
843 return RETMySQLError;
844 }
845 int RunFrom,RunTo;
846 DatabaseRecord& records = *res[0];
847 sscanf(records["RunFrom"], "%d", &RunFrom);
848 sscanf(records["RunTo"], "%d", &RunTo);
849 cout << "@table DedxSim: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
850 return RETOk;
851}
852
853
854
856 int *runFrm,
857 int *runTo,
858 std::string& calParVer,
860 int runNo,
861 std::string& sftver) {
862 using namespace rdbModel;
863 eRet ret;
864 char stmt1[200];
865 int run_No =runNo;
866 if(sftver=="default")
867 sftver = getenv("BES_RELEASE");
868 const char* SftVer = sftver.c_str();
869
870 if(calParVer!="default"){
871 const char* calpar = calParVer.c_str();
872 sprintf(stmt1,"select AlignEndPlate,WirePos,WireTension,RunFrom,RunTo,MdcAlignVer,AlignEPFileName,Status,SftVer from MdcAlignment where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and MdcAlignVer = %s",SftVer,run_No,run_No,calpar);
873 }
874 if(calParVer=="default"){
875 // sprintf(stmt1,"select AlignEndPlate,WirePos,WireTension,RunFrom,RunTo,max(MdcAlignVer),AlignEPFileName from MdcAlignment where SftVer = '%s' and RunFrom <= %d and RunTo >= %d group by MdcAlignVer",SftVer,run_No,run_No);
876 sprintf(stmt1,"select AlignEndPlate,WirePos,WireTension,RunFrom,RunTo,MdcAlignVer,AlignEPFileName,Status,SftVer from MdcAlignment where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by MdcAlignVer desc",SftVer,run_No,run_No);
877 }
878
879 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
880 if(row_no<1){
881 std::cout<<"ERROR:error searching MdcAlignment calibration Data in the database with: "<<stmt1<<std::endl;
882 return RETMySQLError;
883 }
884 int RunFrom,RunTo;
885 DatabaseRecord& records = *res[0];
886 sscanf(records["RunFrom"], "%d", &RunFrom);
887 sscanf(records["RunTo"], "%d", &RunTo);
888 cout << "@table MdcAligment: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
889 return RETOk;
890}
891
893 int *runFrm,
894 int *runTo,
895 std::string& calParVer,
897 int runNo,
898 std::string& sftver) {
899 using namespace rdbModel;
900 eRet ret;
901 char stmt1[200];
902 int run_No =runNo;
903 if(sftver=="default")
904 sftver = getenv("BES_RELEASE");
905 const char* SftVer = sftver.c_str();
906
907 if(calParVer!="default"){
908 const char* calpar = calParVer.c_str();
909 sprintf(stmt1,"select WireEff,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MdcDataConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
910 }
911 if(calParVer=="default"){
912 sprintf(stmt1,"select WireEff,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from MdcDataConst where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
913 }
914
915 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
916 if(row_no<1){
917 std::cout<<"ERROR:error searching MdcDataConst Data in the database with: "<<stmt1<<std::endl;
918 return RETMySQLError;
919 }
920 int RunFrom,RunTo;
921 DatabaseRecord& records = *res[0];
922 sscanf(records["RunFrom"], "%d", &RunFrom);
923 sscanf(records["RunTo"], "%d", &RunTo);
924 cout << "@table MdcData: RunFrom is:" << RunFrom << " RunTo is:" << RunTo << endl;
925 return RETOk;
926}
927
929 int *runFrm,
930 int *runTo,
931 std::string& calParVer,
933 int runNo,
934 std::string& sftver) {
935 using namespace rdbModel;
936 eRet ret;
937 char stmt1[400];
938 int run_No =runNo;
939 if(sftver=="default")
940 sftver = getenv("BES_RELEASE");
941 const char* SftVer = sftver.c_str();
942
943 if(calParVer!="default"){
944 const char* calpar = calParVer.c_str();
945 sprintf(stmt1,"select BarBoardNum,EndBoardNum,QELecBarParEast,QELecBarParWest,QELecEndPar,SimQELecBarParEast,SimQELecBarParWest,SimQELecEndPar,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from TofQELec where SftVer = '%s' and RunFrom <= %d and RunTo >= %d and CalParVer = %s",SftVer,run_No,run_No,calpar);
946 }
947 if(calParVer=="default"){
948 sprintf(stmt1,"select BarBoardNum,EndBoardNum,QELecBarParEast,QELecBarParWest,QELecEndPar,SimQELecBarParEast,SimQELecBarParWest,SimQELecEndPar,RunFrom,RunTo,CalParVer,FileName,Status,SftVer from TofQELec where SftVer = '%s' and RunFrom <= %d and RunTo >= %d order by CalParVer desc",SftVer,run_No,run_No);
949 }
950 int row_no = m_dbsvc->query("offlinedb", stmt1, res);
951 if(row_no<1){
952 std::cout<<"ERROR:error searching TofQElec calibration Data in the database with: "<<stmt1<<std::endl;
953 return RETMySQLError;
954 }
955 int RunFrom,RunTo;
956 DatabaseRecord& records = *res[0];
957 sscanf(records["RunFrom"], "%d", &RunFrom);
958 sscanf(records["RunTo"], "%d", &RunTo);
959 cout << "Read from DB TofQElec: RunFrom " << RunFrom << " RunTo= " << RunTo << endl;
960 std::cout<<"metadata ok"<<std::endl;
961
962 return RETOk;
963}
964
965
966/*
967
968 | Field | Type | Null | Key | Default | How set |
969 +-------------+--------------------+------+-----+---------+------------------+
970 | ser_no | mediumint(9) | | PRI | NULL | auto_increment |
971 | instrument | varchar(16) | | | | [openRecord] |
972 | calib_type | varchar(20) | | | | [openRecord |
973 | data_fmt | varchar(10) | | | | [openRecord] |
974 | data_size | int(11) | YES | | NULL | [optional] |
975 | vstart | datetime | YES | | NULL |[addValidInterval]|
976 | vend | datetime | YES | | NULL |[addValidInterval]|
977 | enter_time | timestamp(14) | YES | | NULL | automatic |
978 | fmt_version | varchar(12) | YES | | NULL | [openRecord] |
979 | completion | enum('OK','INC','ABORT')
980 | YES | MUL | NULL | [openRecord] |
981 | proc_level | enum('PROD','TEST','DEV', 'SUPSED')
982 | | | TEST | [openRecord] |
983 | creator | varchar(255) | YES | | NULL | [addCreator] |
984 | uid | varchar(12) | | | |[insertRecord/
985 addUser] |
986 | data_ident | varchar(255) | | | | [openRecord] |
987 | input_desc | varchar(255) | YES | | NULL |[addInputDesc] |
988 | notes | varchar(255) | YES | | NULL | [addNotes] |
989 +-------------+--------------------+------+-----+---------+------------------+
990
991*/
992
994Metadata::doSelect(std::vector<rdbModel::Assertion::Operator *>& conditions,
995 rdbModel::StringVector& orderBy,
996 unsigned*& ser) {
997 using namespace rdbModel;
998 *ser = 0;
999
1000 StringVector colNames(1);
1001 colNames[0] = "ser_no";
1002
1003 // make the combined operator
1004 Assertion::Operator andOp(OPTYPEand, conditions, true);
1005
1006 //Following creates an assertion such that creator (us) continues
1007 // to own the associated operator.
1008 Assertion whereClause(&andOp, 0, true);
1009 ResultHandle* results = 0;
1010
1011 if(results){
1012 delete results;
1013 }
1014
1015 try { // make the query
1016 results = m_readCxt->select(m_table, colNames, orderBy, &whereClause);
1017
1018 }
1019 catch (RdbException ex) {
1020 std::cout << ex.getMsg();
1021 }
1022
1023 if (!results) { // Error. Should have ResultHandle even if 0 rows.
1024 std::cout<<"no results"<<std::endl;
1025 return RETMySQLError;
1026 }
1027
1028
1029
1030 std::cout<<"There are results"<<std::endl;
1031 // handle result.
1032 if (results->getNRows() == 0) {
1033 conditions.pop_back();
1034 std::cout<<"no results"<<std::endl;
1035 }
1036 else{
1037 std::vector<std::string> fields;
1038 results->getRow(fields);
1039
1040 if(results){
1041 delete results;
1042 }
1043 *ser = facilities::Util::stringToInt(fields[0]);
1044 return RETOk;
1045 }
1046
1047}
1048
1049int Metadata::registerCalib(const std::string& inst,
1050 const std::string& flavor,
1051 const std::string& calib_type,
1052 const std::string& data_ident,
1053 const std::string& data_fmt,
1054 unsigned int& runfrm,
1055 unsigned int& runto,
1056 const std::string& input_desc,
1057 const std::string& notes,
1058 const std::string& proc_level,
1059 const std::string& locale,
1060 const std::string& fmt_version,
1061 const std::string& completion)
1062{
1063
1064 using namespace rdbModel;
1065
1066 eRet ret;
1067
1068 if (!m_writeCxt) {
1069 connectWrite(ret);
1070 if (ret != RETOk) return 0; // we or connectWrite should throw exception
1071 }
1072 StringVector cols;
1073 StringVector vals;
1074 StringVector nullCols;
1075
1076 cols.reserve(24);
1077 vals.reserve(24);
1078 nullCols.reserve(16);
1079
1080 if (inst.size() * calib_type.size() * flavor.size() * data_fmt.size()
1081 * data_ident.size() * proc_level.size() * completion.size()
1082 * locale.size() == 0) { // something is null that shouldn't be
1083 return 0; // should perhaps throw exception
1084 }
1085 cols.push_back("calib_type"); vals.push_back(calib_type);
1086 // cols.push_back("flavor"); vals.push_back(flavor);
1087 cols.push_back("data_fmt"); vals.push_back(data_fmt);
1088 cols.push_back("data_ident"); vals.push_back(data_ident);
1089 cols.push_back("status"); vals.push_back(completion);
1090 std::string s_runfrm,s_runto;
1091 facilities::Util::itoa(runfrm,s_runfrm);
1092 facilities::Util::itoa(runto,s_runto);
1093 cols.push_back("RunFrom"); vals.push_back(s_runfrm);
1094 cols.push_back("RunTo"); vals.push_back(s_runto);
1095
1096 // These, however, may be null
1097 if (input_desc.size() > 0 ) {
1098 cols.push_back("input_desc"); vals.push_back(input_desc);
1099 } else nullCols.push_back("input_desc");
1100
1101 if (notes.size() > 0 ) {
1102 cols.push_back("notes"); vals.push_back(notes);
1103 } else nullCols.push_back("notes");
1104
1105 if (fmt_version.size() > 0 )
1106 {
1107 cols.push_back("fmt_version");
1108 vals.push_back(fmt_version);
1109 }
1110
1111 // The service -- that's us -- is responsible for creator, uid, enter_time
1112 cols.push_back("creator"); vals.push_back("Metadata::registerCalib");
1113 std::string uid;
1114 fetchUser(uid);
1115 cols.push_back("uid"); vals.push_back(uid);
1116 facilities::Timestamp curTime;
1117 cols.push_back("enter_time");vals.push_back(curTime.getString());
1118 // update_time is set automatically by MySQL, but MySQL uses
1119 // local timezone rather than gmt, so we have set it explicitly
1120 cols.push_back("update_time");vals.push_back(curTime.getString());
1121
1122
1123 if (m_rdb) {
1124 bool ok = checkValues(cols, vals);
1125 if (ok) checkNulls(nullCols);
1126 if (!ok) return 0;
1127 }
1128
1129 // ser_no gets set automatically by MySQL
1130 int ser_no;
1131 if (!(m_writeCxt->insertRow(m_table, cols, vals, &ser_no, &nullCols)) ) {
1132 return 0;
1133 } else {
1134 adjustVend(ser_no);
1135 return ser_no;
1136 }
1137}
1138
1139Metadata::eRet Metadata::compareSchema(rdbModel::Connection* conn,
1140 const std::string& schema) {
1141 using namespace rdbModel;
1142
1143
1144 if (m_man) { // already did this
1145 return (m_match) ? RETOk : RETNoSchemaMatch;
1146 }
1148
1150 m_man->setInputSource(schema);
1151
1152 // Maybe first check if file exists? E.g., try opening for read
1153
1154 // good errcode is 0
1155 int errcode = m_man->build();
1156
1157 if (errcode) {
1158 std::cerr << "Error in database description file " << schema
1159 << std::endl;
1160 std::cerr << "Parse failed with error " << errcode << std::endl;
1161 return RETBadCnfFile;
1162 }
1163 m_rdb = m_man->getRdb();
1164
1165 rdbModel::MATCH match = conn->matchSchema(m_rdb, false);
1166
1167 switch (match) {
1170 m_match = true;
1171 return RETOk;
1173 std::cout << "XML schema and MySQL database are NOT compatible"
1174 << std::endl;
1175 return RETBadCnfFile;
1177 std::cout << "Connection failed while attempting match" << std::endl;
1178 return RETNoConnect;
1179 }
1180 return RETBadValue;
1181}
1182
1184 const rdbModel::StringVector& vals) const {
1185 unsigned nCol = cols.size();
1186 rdbModel::Table* table = m_rdb->getTable(m_table);
1187
1188
1189 for (unsigned iCol = 0; iCol < nCol; iCol++) {
1190 rdbModel::Column* col = table->getColumnByName(cols[iCol]);
1191 if (!col->okValue(vals[iCol])) {
1192 std::cerr << "Value " << vals[iCol] << " not allowed for column "
1193 << cols[iCol] << " in table " << m_table << std::endl;
1194 return false;
1195 }
1196 }
1197 return true;
1198}
1199
1201 unsigned nCol = cols.size();
1202 rdbModel::Table* table = m_rdb->getTable(m_table);
1203
1204
1205 for (unsigned iCol = 0; iCol < nCol; iCol++) {
1206 rdbModel::Column* col = table->getColumnByName(cols[iCol]);
1207 if (!col->nullAllowed()) {
1208 std::cerr << "Column "
1209 << cols[iCol] << " in table " << m_table
1210 << " is not nullable" << std::endl;
1211 return false;
1212 }
1213 }
1214 return true;
1215}
1216
1217
1218unsigned Metadata::adjustVend(int newSer) {
1219 using namespace rdbModel;
1220
1221 StringVector getCols;
1222 StringVector orderBy;
1223
1224 orderBy.clear();
1225 getCols.reserve(7);
1226
1227 std::string serString;
1228 facilities::Util::itoa(newSer, serString);
1229
1230 getCols.push_back("flavor");
1231 getCols.push_back("calib_type");
1232 getCols.push_back("completion");
1233
1234 ResultHandle* results = 0;
1235 eRet err;
1236 Assertion* where = 0;
1237 try {
1238 if (!m_writeCxt) {
1239 if (!connectWrite(err)) return 0;
1240 }
1241
1242 Assertion::Operator* serOp =
1243 new Assertion::Operator(OPTYPEequal, "ser_no", serString,
1244 FIELDTYPEold, FIELDTYPElit);
1245 //false, true);
1246
1247 where = new Assertion(serOp);
1248
1249 // Fetch information for new row: vstart, flavor, completion,
1250 // proc_level, calib_type, flavor
1251 results = m_writeCxt->select(m_table, getCols, orderBy, where);
1252 // results = m_writeCxt->select("metadata_v0", getCols, orderBy, where);
1253 delete where;
1254 where = 0;
1255 }
1256 catch (RdbException ex) {
1257 std::cout << ex.getMsg();
1258 delete where; // return heap memory
1259 return 0;
1260 }
1261 if (!results) { // This is an error. Should be non-null even if no rows
1262 std::cout << "MySQL failure in SELECT" << std::endl;
1263 return 0; // nothing to fix
1264 }
1265 if (results->getNRows() != 1) { // also a problem
1266 std::cout << "Look-up of serial# " << serString << " failed"
1267 << std::endl;
1268 return 0;
1269 }
1270 std::vector<std::string> fields;
1271 results->getRow(fields);
1272 if (fields[2] != "OK") return 0; // don't bother fixing in this case
1273
1274 // Now do an update on rows satisfying
1275 // ((flavor="f") && (calib_type = "c") && (completion= "OK") &&
1276 // (instrument = "i") && (proc_level = "p") &&
1277 // (vstart < "new-start" (vend > "new-vstart") );
1278 std::vector<Assertion::Operator *> conditions;
1279 conditions.reserve(7);
1280 for (unsigned ix = 0; ix < 5; ix++) {
1281 conditions.push_back(new Assertion::Operator(OPTYPEequal, getCols[ix],
1282 fields[ix],
1283 FIELDTYPEold, FIELDTYPElit));
1284 // false, true));
1285 }
1286 conditions.push_back(new Assertion::Operator(OPTYPElessThan, "vstart",
1287 fields[5],
1288 FIELDTYPEold, FIELDTYPElit));
1289 //false, true));
1290 conditions.push_back(new Assertion::Operator(OPTYPEgreaterThan, "vend",
1291 fields[5],
1292 FIELDTYPEold, FIELDTYPElit));
1293 // false, true));
1294
1295 Assertion::Operator* andOp =
1296 new Assertion::Operator(OPTYPEand, conditions);
1297 where = new Assertion(andOp);
1298
1299 StringVector toUpdate;
1300 toUpdate.push_back("vend");
1301 StringVector newVal;
1302 newVal.push_back(fields[5]);
1303 // also update update_time. If we leave it to MySQL, won't be GMT
1304 facilities::Timestamp curTime;
1305 toUpdate.push_back("update_time");newVal.push_back(curTime.getString());
1306
1307 unsigned nModified = m_writeCxt->update(m_table, toUpdate, newVal, where);
1308 delete where;
1309 return nModified;
1310}
1311
1312}
1313
1314
std::vector< std::string > StringVector
Simple algorithm to test functioning of "the other" TDS.
Definition: CheckMySQL.h:50
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
char * SftVer
Definition: DQA_TO_DB.cxx:18
int runNo
Definition: DQA_TO_DB.cxx:12
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
eRet getReadMdcDataConstInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:892
bool checkValues(const rdbModel::StringVector &cols, const rdbModel::StringVector &vals) const
Definition: Metadata.cxx:1183
eRet getReadEsTimeInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:702
bool checkNulls(const rdbModel::StringVector &cols) const
Definition: Metadata.cxx:1200
eRet getReadInjSigTimeInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:412
eRet findBest(unsigned int *ser, const std::string &calibType, const std::string &sft_ver, const std::string &cal_ver, const std::string &cal_par, const std::string &rec_alg, const std::string &rec_par, const std::string &machine, const std::string &flavor="VANILLA")
Definition: Metadata.cxx:243
eRet getReadTOFInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:507
eRet getReadInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &dataFmt, std::string &dataIdent)
Definition: Metadata.cxx:314
bool connectRead(eRet &err)
Definition: Metadata.cxx:107
eRet getReadDedxSimInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:817
Metadata(const std::string &host="bes3db2.ihep.ac.cn", const std::string &table="*", const std::string &dbName="calib")
Constructor keeps track of table of interest.
Definition: Metadata.cxx:39
eRet getReadMUCInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:543
eRet getReadEstTofInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:740
eRet getReadTofSimInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:778
eRet getReadOffEvtFilterInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:447
eRet getReadDedxInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:663
eRet getReadInjSigIntervalInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:377
eRet getReadCorrectedETSInfo(unsigned int serialNo, int runNo, int FirstEvent, std::string &Version, DatabaseRecordVector &res)
Definition: Metadata.cxx:483
eRet getReadMdcAlignInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:855
int registerCalib(const std::string &inst, const std::string &flavor, const std::string &calib_type, const std::string &data_ident, const std::string &data_format, unsigned int &runfrm, unsigned int &runto, const std::string &input_desc, const std::string &notes, const std::string &proc_level, const std::string &locale, const std::string &fmt_version="", const std::string &completion="OK")
Definition: Metadata.cxx:1049
eRet getReadMDCInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:583
eRet getReadTofQElecInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:928
eRet findSoonAfter(unsigned int *ser, const std::string &calibType, const std::string &sft_ver, const std::string &cal_ver, const std::string &cal_par, const std::string &rec_alg, const std::string &rec_par, const std::string &machine, const std::string &flavor="VANILLA")
Definition: Metadata.cxx:172
eRet getReadEMCInfo(unsigned int serialNo, int *runFrm, int *runTo, std::string &calParVer, DatabaseRecordVector &res, int runNo, std::string &sftver)
Definition: Metadata.cxx:624
std::string getString() const
Return string representation of time, not including nanoseconds;.
Definition: Timestamp.cxx:92
static int expandEnvVar(std::string *toExpand, const std::string &openDel=std::string("$("), const std::string &closeDel=std::string(")"))
static int atoi(const std::string &InStr)
converts an std::string to an integer
static int stringToInt(const std::string &InStr)
static const char * itoa(int val, std::string &outStr)
bool nullAllowed() const
Returns true if column may take on value NULL.
Definition: Column.h:80
bool okValue(const std::string &val, bool set=true) const
Definition: Column.cxx:21
virtual ResultHandle * select(const std::string &tableName, const StringVector &getCols, const StringVector &orderCols, const Assertion *where=0, int rowLimit=0, int rowOffset=0)=0
virtual MATCH matchSchema(Rdb *rdb, bool matchDbName=true)=0
virtual unsigned int update(const std::string &tableName, const StringVector &colNames, const StringVector &values, const Assertion *where=0, const StringVector *nullCols=0)=0
virtual bool open(const std::string &host, const std::string &userid, const std::string &password, const std::string &dbName)=0
virtual bool insertRow(const std::string &tableName, const StringVector &colNames, const StringVector &values, int *auto_value=0, const StringVector *nullCols=0)=0
virtual bool close()=0
Rdb * getRdb()
Definition: Manager.h:48
void setInputSource(std::string pname)
Definition: Manager.h:51
void setBuilder(Builder *b)
Definition: Manager.cxx:37
static Manager * getManager()
Definition: Manager.cxx:24
virtual std::string getMsg()
Definition: RdbException.h:14
Table * getTable(const std::string &name) const
Definition: Rdb.cxx:16
virtual bool getRow(std::vector< std::string > &fields, unsigned int i=0, bool clear=true)=0
virtual unsigned int getNRows() const =0
Return number of rows in results.
Column * getColumnByName(const std::string &name) const
Definition: Table.cxx:56
#define ix(i)
Module implements methods for clients to get generic services.
@ MATCHequivalent
Definition: Connection.h:26
@ MATCHnoConnection
Definition: Connection.h:29
@ MATCHfail
Definition: Connection.h:28
@ MATCHcompatible
Definition: Connection.h:27
std::vector< std::string > StringVector
Definition: Connection.h:52