QwAnalysis
QwParityDB.cc
Go to the documentation of this file.
1 /*!
2  * \file QwParityDB.cc
3  * \brief A class for handling connections to the Qweak database.
4  *
5  * \author Damon Spayde
6  * \date 2010-01-07
7  */
8 
9 
10 #define EXPAND_QWPARITYSSQLS_STATICS
11 //#define MYSQLPP_SSQLS_NO_STATICS
12 #include "QwParitySSQLS.h"
13 #include "QwParityDB.h"
14 
15 // System headers
16 
17 // Qweak headers
18 #include "QwEventBuffer.h"
19 #include "QwRunCondition.h"
20 using namespace QwParitySSQLS;
21 
22 // Specific template instantiations
23 template void QwDBInterface::AddThisEntryToList<QwParitySSQLS::md_data>(std::vector<QwParitySSQLS::md_data> &list);
24 template void QwDBInterface::AddThisEntryToList<QwParitySSQLS::lumi_data>(std::vector<QwParitySSQLS::lumi_data> &list);
25 template void QwDBInterface::AddThisEntryToList<QwParitySSQLS::beam>(std::vector<QwParitySSQLS::beam> &list);
26 
27 // Definition of static class members in QwParityDB
28 std::map<string, unsigned int> QwParityDB::fMonitorIDs;
29 std::map<string, unsigned int> QwParityDB::fMainDetectorIDs;
30 std::map<string, unsigned int> QwParityDB::fLumiDetectorIDs;
31 std::vector<string> QwParityDB::fMeasurementIDs;
32 std::map<string, unsigned int> QwParityDB::fSlowControlDetectorIDs;// for epics
33 std::map<string, unsigned char> QwParityDB::fErrorCodeIDs;
34 
36  public:
37  void operator() (QwParitySSQLS::monitor elem) {
38  QwDebug << "StoreMonitorID: monitor_id = " << elem.monitor_id << " quantity = " << elem.quantity << QwLog::endl;
39  QwParityDB::fMonitorIDs.insert(std::make_pair(elem.quantity, elem.monitor_id));
40  }
41 };
42 
44  public:
45  void operator() (QwParitySSQLS::main_detector elem) {
46  QwDebug << "StoreMainDetectorID: main_detector_id = " << elem.main_detector_id << " quantity = " << elem.quantity << QwLog::endl;
47  QwParityDB::fMainDetectorIDs.insert(std::make_pair(elem.quantity, elem.main_detector_id));
48  }
49 };
50 
52  public:
53  void operator() (QwParitySSQLS::lumi_detector elem) {
54  QwDebug << "StoreLumiDetectorID: lumi_detector_id = " << elem.lumi_detector_id << " quantity = " << elem.quantity << QwLog::endl;
55  QwParityDB::fLumiDetectorIDs.insert(std::make_pair(elem.quantity, elem.lumi_detector_id));
56  }
57 };
58 
60  public:
61  void operator() (QwParitySSQLS::measurement_type elem) {
62  QwDebug << "StoreMeasurementID: measurement_type = " << elem.measurement_type_id << QwLog::endl;
63  QwParityDB::fMeasurementIDs.push_back( (std::string) elem.measurement_type_id );
64  }
65 };
66 
67 
69  public:
70  void operator() (QwParitySSQLS::sc_detector elem) {
71  QwDebug << "StoreSlowControlDetectorID: sc_detector_id = " << elem.sc_detector_id << " name = " << elem.name << QwLog::endl;
72  QwParityDB::fSlowControlDetectorIDs.insert(std::make_pair(elem.name, elem.sc_detector_id));
73  }
74 };
75 
77  public:
78  void operator() (QwParitySSQLS::error_code elem) {
79  QwDebug << "StoreErrorCodeID: error_code_id = " << elem.error_code_id << " quantity = " << elem.quantity << QwLog::endl;
80  QwParityDB::fErrorCodeIDs.insert(std::make_pair(elem.quantity, elem.error_code_id));
81  }
82 };
83 
84 
85 /*! The simple constructor initializes member fields. This class is not
86  * used to establish the database connection. It sets up a
87  * mysqlpp::Connection() object that has exception throwing disabled.
88  */
89 //QwDatabase::QwDatabase() : Connection(false)
90 QwParityDB::QwParityDB() : QwDatabase("01", "04", "0000")
91 {
92  QwDebug << "Greetings from QwParityDB simple constructor." << QwLog::endl;
93  // Initialize member fields
94 
95  fRunNumber = 0;
96  fRunID = 0;
97  fRunletID = 0;
98  fAnalysisID = 0;
99  fSegmentNumber = -1;
100  fDisableAnalysisCheck = false;
101 
102 }
103 
104 /*! The constructor initializes member fields using the values in
105  * the QwOptions object.
106  * @param options The QwOptions object.
107  */
108 QwParityDB::QwParityDB(QwOptions &options) : QwDatabase(options, "01", "04", "0000")
109 {
110  QwDebug << "Greetings from QwParityDB extended constructor." << QwLog::endl;
111 
112  // Initialize member fields
113  fRunNumber = 0;
114  fRunID = 0;
115  fRunletID = 0;
116  fAnalysisID = 0;
117  fSegmentNumber = -1;
118  fDisableAnalysisCheck = false;
119 
120  ProcessAdditionalOptions(options);
121 
122 }
123 
124 /*! The destructor says "Good-bye World!"
125  */
127 {
128  QwDebug << "QwParityDB::~QwParityDB() : Good-bye World from QwParityDB destructor!" << QwLog::endl;
129  if( Connected() ) Disconnect();
130 }
131 
132 /*!
133  * Sets run number for subsequent database interactions. Makes sure correct
134  * entry exists in run table and retrieves run_id.
135  */
137 {
138  if (this->AllowsReadAccess()) {
139  UInt_t run_id = this->GetRunID(qwevt);
140  UInt_t runlet_id = this->GetRunletID(qwevt);
141  UInt_t analysis_id = this->GetAnalysisID(qwevt);
142 
143  // Write from the datebase
144  QwMessage << "QwParityDB::SetupOneRun::"
145  << " Run Number " << QwColor(Qw::kBoldMagenta) << qwevt.GetRunNumber() << QwColor(Qw::kNormal)
146  << " Run ID " << QwColor(Qw::kBoldMagenta) << run_id << QwColor(Qw::kNormal)
147  << " Runlet ID " << QwColor(Qw::kBoldMagenta) << runlet_id << QwColor(Qw::kNormal)
148  << " Analysis ID " << QwColor(Qw::kBoldMagenta) << analysis_id
149  << QwColor(Qw::kNormal)
150  << QwLog::endl;
151  }
152 }
153 
154 /*!
155  * Sets run number for subsequent database interactions. Makes sure correct
156  * entry exists in run table and retrieves run_id.
157  */
158 bool QwParityDB::SetRunNumber(const UInt_t runnum)
159 {
160 
161  QwDebug << "Made it into QwParityDB::SetRunNumber()" << QwLog::endl;
162 
163  try {
164 
165  this->Connect();
166 
167  mysqlpp::Query query= this->Query();
168  query << "SELECT * FROM run WHERE run_number = " << runnum;
169  std::vector<run> res;
170  query.storein(res);
171 
172  QwDebug << "Number of rows returned: " << res.size() << QwLog::endl;
173 
174  if (res.size()!=1) {
175  QwError << "Unable to find unique run number " << runnum << " in database." << QwLog::endl;
176  QwError << "Run number query returned " << res.size() << "rows." << QwLog::endl;
177  QwError << "Please make sure that the database contains one unique entry for this run." << QwLog::endl;
178  return false;
179  }
180 
181  QwDebug << "Run ID = " << res.at(0).run_id << QwLog::endl;
182 
183  fRunNumber = runnum;
184  fRunID = res.at(0).run_id;
185 
186  this->Disconnect();
187  }
188  catch (const mysqlpp::Exception& er) {
189  QwError << er.what() << QwLog::endl;
190  return false;
191  }
192 
193  return true;
194 
195 }
196 
197 /*!
198  * This function sets the fRunID for the run being replayed as determined by the QwEventBuffer class.
199  */
201 {
202 
203  // Check to see if run is already in database. If so retrieve run ID and exit.
204  try
205  {
206  this->Connect();
207  mysqlpp::Query query = this->Query();
208 
209  query << "SELECT * FROM run WHERE run_number = " << qwevt.GetRunNumber();
210  std::vector<run> res;
211  query.storein(res);
212  QwDebug << "QwParityDB::SetRunID => Number of rows returned: " << res.size() << QwLog::endl;
213 
214  // If there is more than one run in the DB with the same run number, then there will be trouble later on. Catch and bomb out.
215  if (res.size()>1)
216  {
217  QwError << "Unable to find unique run number " << qwevt.GetRunNumber() << " in database." << QwLog::endl;
218  QwError << "Run number query returned " << res.size() << "rows." << QwLog::endl;
219  QwError << "Please make sure that the database contains one unique entry for this run." << QwLog::endl;
220  this->Disconnect();
221  return 0;
222  }
223 
224  // Run already exists in database. Pull run_id and move along.
225  if (res.size()==1)
226  {
227  QwDebug << "QwParityDB::SetRunID => Run ID = " << res.at(0).run_id << QwLog::endl;
228 
229  fRunNumber = qwevt.GetRunNumber();
230  fRunID = res.at(0).run_id;
231  this->Disconnect();
232  return fRunID;
233  }
234  this->Disconnect();
235  }
236  catch (const mysqlpp::Exception& er)
237  {
238 
239  QwError << er.what() << QwLog::endl;
240  this->Disconnect();
241  return 0;
242  }
243 
244  // Run is not in database so insert pertinent data and retrieve run ID
245  // Right now this does not insert start/stop times or info on number of events.
246  try
247  {
248 
249  this->Connect();
250  run row(0);
251  row.run_number = qwevt.GetRunNumber();
252  row.run_type = "good"; // qwevt.GetRunType(); RunType is the confused name because we have also a CODA run type.
253  row.start_time = mysqlpp::DateTime(qwevt.GetStartUnixTime());
254  row.end_time = mysqlpp::DateTime(qwevt.GetEndUnixTime());
255  row.n_mps = 0;
256  row.n_qrt = 0;
257  // Set following quantities to 9999 as "uninitialized value". DTS 8/3/2012
258  row.slug = 9999;
259  row.wien_slug = 9999;
260  row.injector_slug = 9999;
261 // row.n_mps=10; // This works
262  // row.start_time = mysqlpp::null; // This works
263  // row.start_time = qwevt.GetStartSQLTime().Data(); // This does not work
264  mysqlpp::Query query=this->Query();
265  query.insert(row);
266  QwDebug<< "QwParityDB::SetRunID() => Run Insert Query = " << query.str() << QwLog::endl;
267 
268  query.execute();
269 
270  if (query.insert_id()!=0)
271  {
272  fRunNumber = qwevt.GetRunNumber();
273  fRunID = query.insert_id();
274  }
275  this->Disconnect();
276  return fRunID;
277  }
278  catch (const mysqlpp::Exception& er)
279  {
280  QwError << er.what() << QwLog::endl;
281  this->Disconnect();
282  return 0;
283  }
284 
285 }
286 
287 /*!
288  * This is a getter for run_id in the run table. Should be used in subsequent queries to retain key relationships between tables.
289  */
291 {
292  // If the stored run number does not agree with the CODA run number
293  // or if fRunID is not set, then retrieve data from database and update if necessary.
294 
295  // GetRunNumber() in QwEventBuffer returns Int_t, thus
296  // we should convert it to UInt_t here. I think, it is OK.
297 
298  if (fRunID == 0 || fRunNumber != (UInt_t) qwevt.GetRunNumber() ) {
299  QwDebug << "QwParityDB::GetRunID() set fRunID to " << SetRunID(qwevt) << QwLog::endl;
300  fRunletID = 0;
301  fAnalysisID = 0;
302  }
303 
304  return fRunID;
305 
306 }
307 
308 /*!
309  * This function sets the fRunletID for the run being replayed as determined by the QwEventBuffer class.
310  *
311  * Runlets are differentiated by file segment number at the moment, not by event range or start/stop time. This function will need to be altered if we opt to differentiate between runlets in a different way.
312  */
314 {
315 
316  // Make sure 'run' table has been populated and retrieve run_id
317  // UInt_t runid = this->GetRunID(qwevt);
318 
319  // Check to see if runlet is already in database. If so retrieve runlet_id and exit.
320  try
321  {
322  this->Connect();
323  mysqlpp::Query query = this->Query();
324 
325  // Query is slightly different if file segments are being chained together for replay or not.
326  if (qwevt.AreRunletsSplit()) {
328  query << "SELECT * FROM runlet WHERE run_id = " << fRunID << " AND full_run = 'false' AND segment_number = " << fSegmentNumber;
329  } else {
330  query << "SELECT * FROM runlet WHERE run_id = " << fRunID << " AND full_run = 'true'";
331  }
332 
333  std::vector<runlet> res;
334  query.storein(res);
335  QwDebug << "QwParityDB::SetRunletID => Number of rows returned: " << res.size() << QwLog::endl;
336 
337  // If there is more than one run in the DB with the same runlet number, then there will be trouble later on. Catch and bomb out.
338  if (res.size()>1)
339  {
340  QwError << "Unable to find unique runlet number " << qwevt.GetRunNumber() << " in database." << QwLog::endl;
341  QwError << "Run number query returned " << res.size() << "rows." << QwLog::endl;
342  QwError << "Please make sure that the database contains one unique entry for this run." << QwLog::endl;
343  this->Disconnect();
344  return 0;
345  }
346 
347  // Run already exists in database. Pull runlet_id and move along.
348  if (res.size()==1)
349  {
350  QwDebug << "QwParityDB::SetRunletID => Runlet ID = " << res.at(0).runlet_id << QwLog::endl;
351 
352  fRunletID = res.at(0).runlet_id;
353  this->Disconnect();
354  return fRunletID;
355  }
356  this->Disconnect();
357  }
358  catch (const mysqlpp::Exception& er)
359  {
360 
361  QwError << er.what() << QwLog::endl;
362  this->Disconnect();
363  return 0;
364  }
365 
366  // Runlet is not in database so insert pertinent data and retrieve run ID
367  // Right now this does not insert start/stop times or info on number of events.
368  try
369  {
370 
371  this->Connect();
372  runlet row(0);
373  row.run_id = fRunID;
374  row.run_number = qwevt.GetRunNumber();
375  row.start_time = mysqlpp::null;
376  row.end_time = mysqlpp::null;
377  row.first_mps = 0;
378  row.last_mps = 0;
379  if (qwevt.AreRunletsSplit()) {
380  row.segment_number = fSegmentNumber;
381  row.full_run = "false";
382  } else {
383  row.segment_number = mysqlpp::null;
384  row.full_run = "true";
385  }
386 
387  mysqlpp::Query query=this->Query();
388  query.insert(row);
389  QwDebug<< "QwParityDB::SetRunletID() => Run Insert Query = " << query.str() << QwLog::endl;
390 
391  query.execute();
392 
393  if (query.insert_id()!=0)
394  {
395  fRunletID = query.insert_id();
396  }
397  this->Disconnect();
398  return fRunletID;
399  }
400  catch (const mysqlpp::Exception& er)
401  {
402  QwError << er.what() << QwLog::endl;
403  this->Disconnect();
404  return 0;
405  }
406 
407 }
408 
409 /*!
410  * This is a getter for runlet_id in the runlet table. Should be used in subsequent queries to retain key relationships between tables.
411  */
413 {
414  // If the stored run number does not agree with the CODA run number
415  // or if fRunID is not set, then retrieve data from database and update if necessary.
416 
417  if (fRunletID == 0 || (qwevt.AreRunletsSplit() && fSegmentNumber!=qwevt.GetSegmentNumber()) || fRunNumber != (UInt_t) qwevt.GetRunNumber() ) {
418  QwDebug << "QwParityDB::GetRunletID() set fRunletID to " << SetRunletID(qwevt) << QwLog::endl;
419  fAnalysisID = 0;
420  }
421 
422  return fRunletID;
423 
424 }
425 
426 /*!
427  * This is used to set the appropriate analysis_id for this run. Must be a valid runlet_id in the runlet table before proceeding. Will insert an entry into the analysis table if necessary.
428  */
430 {
431 
432  // If there is already an analysis_id for this run, then let's bomb out.
433 
434  try {
435  this->Connect();
436  mysqlpp::Query query= this->Query();
437  query << "SELECT analysis_id FROM analysis WHERE beam_mode=" << mysqlpp::quote << "nbm";
438  query << " AND slope_calculation=" << mysqlpp::quote << "off";
439  query << " AND slope_correction=" << mysqlpp::quote << "off";
440  query << " AND runlet_id = " << mysqlpp::quote << this->GetRunletID(qwevt);
441 
442  mysqlpp::StoreQueryResult res = query.store();
443 
444  if (res.num_rows() != 0) {
445  QwError << "This runlet has already been analyzed by the engine!" << QwLog::endl;
446  QwError << "The following analysis_id values already exist in the database: ";
447  for (size_t i=0; i<res.num_rows(); i++) {
448  QwError << res[i][0] << " ";
449  }
450  QwError << QwLog::endl;
451 
452  if (fDisableAnalysisCheck==false) {
453  QwError << "Analysis of this run will now be terminated." << QwLog::endl;
454 
455  return 0;
456  } else {
457  QwWarning << "Analysis will continue. A duplicate entry with new analysis_id will be added to the analysis table." << QwLog::endl;
458  }
459  }
460 
461  this->Disconnect();
462  }
463  catch (const mysqlpp::Exception& er) {
464  QwError << er.what() << QwLog::endl;
465  QwError << "Unable to determine if there are other database entries for this run. Exiting." << QwLog::endl;
466  this->Disconnect();
467  return 0;
468  }
469 
470 
471  try {
472 
473  analysis analysis_row(0);
474 
475  analysis_row.runlet_id = GetRunletID(qwevt);
476  analysis_row.seed_id = 1;
477 
478  std::pair<UInt_t, UInt_t> event_range;
479  event_range = qwevt.GetEventRange();
480 
481  analysis_row.time = mysqlpp::DateTime::now();
482  analysis_row.bf_checksum = "empty"; // we will match this as a real one later
483  analysis_row.beam_mode = "nbm"; // we will match this as a real one later
484  analysis_row.n_mps = 0; // we will match this as a real one later
485  analysis_row.n_qrt = 4; // we will match this as a real one later
486  analysis_row.first_event = event_range.first;
487  analysis_row.last_event = event_range.second;
488  analysis_row.segment = 0; // we will match this as a real one later
489  analysis_row.slope_calculation = "off"; // we will match this as a real one later
490  analysis_row.slope_correction = "off"; // we will match this as a real one later
491 
492  // Analyzer Information Parsing
493  QwRunCondition run_condition(
496  "run_condition"
497  );
498 
499  run_condition.Get()->Print();
500 
501  TIter next(run_condition.Get());
502  TObjString *obj_str;
503  TString str_val, str_var;
504  Ssiz_t location;
505 
506  // Iterate over each entry in run_condition
507  while ((obj_str = (TObjString *) next())) {
508  QwMessage << obj_str->GetName() << QwLog::endl;
509 
510  // Store string contents for parsing
511  str_var = str_val = obj_str->GetString();
512  location = str_val.First(":"); // The first : separates variable from value
513  location = location + 2; // Value text starts two characters after :
514  str_val.Remove(0,location); //str_val stores value to go in DB
515 
516  // Decision tree to figure out which variable to store in
517  if (str_var.BeginsWith("ROOT Version")) {
518  analysis_row.root_version = str_val;
519  } else if (str_var.BeginsWith("ROOT file creating time")) {
520  analysis_row.root_file_time = str_val;
521  } else if (str_var.BeginsWith("ROOT file created on Hostname")) {
522  analysis_row.root_file_host = str_val;
523  } else if (str_var.BeginsWith("ROOT file created by the user")) {
524  analysis_row.root_file_user = str_val;
525  } else if (str_var.BeginsWith("QwAnalyzer Name")) {
526  analysis_row.analyzer_name = str_val;
527  } else if (str_var.BeginsWith("QwAnalyzer Options")) {
528  analysis_row.analyzer_argv = str_val;
529  } else if (str_var.BeginsWith("QwAnalyzer SVN Revision")) {
530  analysis_row.analyzer_svn_rev = str_val;
531  } else if (str_var.BeginsWith("QwAnalyzer SVN Last Changed Revision")) {
532  analysis_row.analyzer_svn_lc_rev = str_val;
533  } else if (str_var.BeginsWith("QwAnalyzer SVN URL")) {
534  analysis_row.analyzer_svn_url = str_val;
535  } else if (str_var.BeginsWith("DAQ ROC flags when QwAnalyzer runs")) {
536  analysis_row.roc_flags = str_val;
537  } else {
538  }
539  }
540  this->Connect();
541  mysqlpp::Query query= this->Query();
542  query.insert(analysis_row);
543  //QwMessage << "\nQwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl<< QwLog::endl;
544  query.execute();
545 
546  if (query.insert_id()!=0)
547  {
548  fAnalysisID = query.insert_id();
549  }
550 
551  this->Disconnect();
552  return fAnalysisID;
553  }
554  catch (const mysqlpp::Exception& er) {
555  QwError << er.what() << QwLog::endl;
556  this->Disconnect();
557  return 0;
558  }
559 
560 
561 }
562 
564  TList* param_file_list = subsys.GetParamFileNameList("mapfiles");
565  try {
566  this->Connect();
567  mysqlpp::Query query = this->Query();
568  parameter_files parameter_file_row(0);
569  parameter_file_row.analysis_id = GetAnalysisID();
570 
571  param_file_list->Print();
572  TIter next(param_file_list);
573  TList *pfl_elem;
574  while ((pfl_elem = (TList *) next())) {
575  parameter_file_row.filename = pfl_elem->GetName();
576  query.insert(parameter_file_row);
577  query.execute();
578  }
579 
580  this->Disconnect();
581 
582  delete param_file_list;
583  }
584  catch (const mysqlpp::Exception& er) {
585  QwError << er.what() << QwLog::endl;
586  this->Disconnect();
587  delete param_file_list;
588  }
589 }
590 
591 /*!
592  * This is a getter for analysis_id in the analysis table. Required by all queries on cerenkov, beam, etc. tables. Will return 0 if fRunID has not been successfully set. If fAnalysisID is not set, then calls code to insert into analysis table and retrieve analysis_id.
593  */
595 {
596  // Sanity check to make sure not calling this before runlet_id has been retrieved.
597  if (fRunletID == 0) {
598  QwDebug << "QwParityDB::GetAnalysisID() : fRunletID must be set before proceeding. Check to make sure run exists in database." << QwLog::endl;
599  return 0;
600  }
601 
602  if (fAnalysisID == 0 || fRunNumber != (UInt_t) qwevt.GetRunNumber()
603  || (qwevt.AreRunletsSplit() && fSegmentNumber!=qwevt.GetSegmentNumber())) {
604  QwDebug << "QwParityDB::GetAnalysisID() set fAnalysisID to " << SetAnalysisID(qwevt) << QwLog::endl;
605  if (fAnalysisID==0) {
606  QwError << "QwParityDB::SetAnalysisID() unable to set valid fAnalysisID for this run. Exiting." <<QwLog::endl;
607  exit(1);
608  }
609  }
610 
611  return fAnalysisID;
612 }
613 
614 
615 /*
616  * This function retrieves the monitor table key 'monitor_id' for a given beam monitor.
617  */
618 UInt_t QwParityDB::GetMonitorID(const string& name, Bool_t zero_id_is_error)
619 {
620  if (fMonitorIDs.size() == 0) {
621  StoreMonitorIDs();
622  }
623 
624  UInt_t monitor_id = fMonitorIDs[name];
625 
626  if (zero_id_is_error && monitor_id==0) {
627  // monitor_id = 6; // only for QwMockDataAnalysis
628  QwError << "QwParityDB::GetMonitorID() => Unable to determine valid ID for beam monitor " << name << QwLog::endl;
629  }
630 
631  return monitor_id;
632 
633 }
634 
635 /*
636  * Stores monitor table keys in an associative array indexed by monitor name.
637  */
639 {
640  try {
641 
642  this->Connect();
643  mysqlpp::Query query=this->Query();
644  query.for_each(monitor(), StoreMonitorID());
645 
646 // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
647  this->Disconnect();
648  }
649 
650  catch (const mysqlpp::Exception& er) {
651  QwError << er.what() << QwLog::endl;
652  Disconnect();
653  exit(1);
654  }
655  return;
656 }
657 
658 /*
659  * This function retrieves the main_detector table key 'main_detector_id' for a given beam main_detector.
660  */
661 UInt_t QwParityDB::GetMainDetectorID(const string& name, Bool_t zero_id_is_error)
662 {
663  if (fMainDetectorIDs.size() == 0) {
665  }
666 
667  UInt_t main_detector_id = fMainDetectorIDs[name];
668 
669  if (zero_id_is_error && main_detector_id==0) {
670  // main_detector_id = 19; // only for QwMockDataAnalysis
671  QwError << "QwParityDB::GetMainDetectorID() => Unable to determine valid ID for beam main_detector " << name << QwLog::endl;
672  }
673 
674  return main_detector_id;
675 
676 }
677 
678 
679 /*
680  * Stores main_detector table keys in an associative array indexed by main_detector name.
681  */
683 {
684 
685  try {
686  this->Connect();
687  mysqlpp::Query query=this->Query();
688  query.for_each(main_detector(), StoreMainDetectorID());
689 
690 // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
691 
692  this->Disconnect();
693  }
694  catch (const mysqlpp::Exception& er) {
695  QwError << er.what() << QwLog::endl;
696  Disconnect();
697  exit(1);
698  }
699  return;
700 }
701 
702 
703 /*
704  * This function retrieves the slow control detector table key 'sc_detector_id' for a given epics variable.
705  */
706 UInt_t QwParityDB::GetSlowControlDetectorID(const string& name)
707 {
708  if (fSlowControlDetectorIDs.size() == 0) {
710  }
711 
712  UInt_t sc_detector_id = fSlowControlDetectorIDs[name];
713 
714  if (sc_detector_id==0) {
715  QwError << "QwParityDB::GetSlowControlDetectorID() => Unable to determine valid ID for the epics variable " << name << QwLog::endl;
716  }
717 
718  return sc_detector_id;
719 
720 }
721 
722 /*
723  * This function retrieves the error code table key 'error_code_id' for a given error code name.
724  */
725 UInt_t QwParityDB::GetErrorCodeID(const string& name)
726 {
727  if (fErrorCodeIDs.size() == 0) {
729  }
730 
731  UInt_t error_code_id = fErrorCodeIDs[name];
732 
733  if (error_code_id==0) {
734  QwError << "QwParityDB::GetErrorCodeID() => Unable to determine valid ID for the error code " << name << QwLog::endl;
735  }
736 
737  return error_code_id;
738 
739 }
740 
741 /*
742  * Stores slow control detector table keys in an associative array indexed by slow_controls_data name.
743  */
745 {
746 
747  try {
748  this->Connect();
749  mysqlpp::Query query=this->Query();
750  query.for_each(sc_detector(), StoreSlowControlDetectorID());
751 
752 // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
753 
754  this->Disconnect();
755  }
756  catch (const mysqlpp::Exception& er) {
757  QwError << er.what() << QwLog::endl;
758  Disconnect();
759  exit(1);
760  }
761  return;
762 }
763 
764 /*
765  * Stores error_code table keys in an associative array indexed by error_code quantity.
766  */
768 {
769 
770  try {
771  this->Connect();
772  mysqlpp::Query query=this->Query();
773  query.for_each(error_code(), StoreErrorCodeID());
774 
775 // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
776 
777  this->Disconnect();
778  }
779  catch (const mysqlpp::Exception& er) {
780  QwError << er.what() << QwLog::endl;
781  Disconnect();
782  exit(1);
783  }
784  return;
785 }
786 
787 /*
788  * This function retrieves the lumi_detector table key 'lumi_detector_id' for a given beam lumi_detector.
789  */
790 UInt_t QwParityDB::GetLumiDetectorID(const string& name, Bool_t zero_id_is_error)
791 {
792  if (fLumiDetectorIDs.size() == 0) {
794  }
795 
796  UInt_t lumi_detector_id = fLumiDetectorIDs[name];
797 
798  if (zero_id_is_error && lumi_detector_id==0) {
799  QwError << "QwParityDB::GetLumiDetectorID() => Unable to determine valid ID for beam lumi_detector " << name << QwLog::endl;
800  }
801 
802  return lumi_detector_id;
803 }
804 
805 /*
806  * Stores lumi_detector table keys in an associative array indexed by lumi_detector name.
807  */
809 {
810 
811  try {
812  this->Connect();
813 
814  mysqlpp::Query query=this->Query();
815  query.for_each(lumi_detector(), StoreLumiDetectorID());
816 
817 // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
818 
819  this->Disconnect();
820 
821  }
822  catch (const mysqlpp::Exception& er) {
823  QwError << er.what() << QwLog::endl;
824  Disconnect();
825  exit(1);
826  }
827  return;
828 }
829 
830 
831 
832 
833 const string QwParityDB::GetMeasurementID(const Int_t index)
834 {
835  if (fMeasurementIDs.size() == 0) {
837  }
838 
839  string measurement_type = fMeasurementIDs[index];
840 
841  if (measurement_type.empty()) {
842  QwError << "QwParityDB::GetMeasurementID() => Unable to determine valid ID for measurement type with " << index << QwLog::endl;
843  }
844 
845  return measurement_type;
846 }
847 
849 {
850 
851  try {
852  this->Connect();
853 
854  mysqlpp::Query query=this->Query();
855  query.for_each(measurement_type(), StoreMeasurementID());
856 
857  // QwDebug<< "QwParityDB::SetAnalysisID() => Analysis Insert Query = " << query.str() << QwLog::endl;
858 
859  this->Disconnect();
860 
861  }
862  catch (const mysqlpp::Exception& er) {
863  QwError << er.what() << QwLog::endl;
864  Disconnect();
865  exit(1);
866  }
867  return;
868 }
869 
870 /*!
871  * Defines configuration options for QwParityDB class using QwOptions
872  * functionality.
873  *
874  * Should apparently by called by QwOptions::DefineOptions() in
875  * QwParityOptions.h
876  */
878 {
879  // Specify command line options for use by QwParityDB
880  options.AddOptions("Parity Analyzer Database options")
881  ("QwParityDB.disable-analysis-check",
882  po::value<bool>()->default_bool_value(false),
883  "disable check of pre-existing analysis_id");
884 }
885 
886 /*!
887  * Loads the configuration options for QwParityDB class into this instance of
888  * QwParityDB from the QwOptions object.
889  * @param options Options object
890  */
892 {
893  if (options.GetValue<bool>("QwParityDB.disable-analysis-check"))
895 
896  return;
897 }
898 
899 
UInt_t GetRunletID()
Definition: QwParityDB.h:70
#define QwMessage
Predefined log drain for regular messages.
Definition: QwLog.h:50
UInt_t GetErrorCodeID(const string &name)
Definition: QwParityDB.cc:725
QwParityDB()
Simple constructor.
Definition: QwParityDB.cc:90
UInt_t fRunletID
runlet_id of current run
Definition: QwParityDB.h:97
UInt_t GetMonitorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:618
friend class StoreSlowControlDetectorID
Definition: QwParityDB.h:112
Int_t fSegmentNumber
CODA file segment number of current run.
Definition: QwParityDB.h:95
void StoreMonitorIDs()
Definition: QwParityDB.cc:638
#define default_bool_value(b)
Definition: QwOptions.h:51
void StoreMainDetectorIDs()
Definition: QwParityDB.cc:682
UInt_t SetRunID(QwEventBuffer &qwevt)
Definition: QwParityDB.cc:200
Int_t GetSegmentNumber() const
Return CODA file segment number.
Definition: QwEventBuffer.h:82
void Disconnect()
Definition: QwDatabase.h:59
UInt_t GetMainDetectorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:661
const string GetMeasurementID(const Int_t index)
Definition: QwParityDB.cc:833
Bool_t Connect()
Open a connection to the database using the predefined parameters.
Definition: QwDatabase.cc:175
bool fDisableAnalysisCheck
Flag to disable pre-existing analysis_id check.
Definition: QwParityDB.h:99
Bool_t Connected()
Definition: QwDatabase.h:60
static std::vector< string > fMeasurementIDs
Definition: QwParityDB.h:106
An options class.
Definition: QwOptions.h:133
Bool_t SetRunNumber(const UInt_t runnum)
Definition: QwParityDB.cc:158
TList * GetParamFileNameList(TString name) const
Get list of parameter files.
UInt_t SetAnalysisID(QwEventBuffer &qwevt)
Definition: QwParityDB.cc:429
A database interface class.
Definition: QwDatabase.h:45
UInt_t fRunID
run_id of current run
Definition: QwParityDB.h:96
UInt_t GetLumiDetectorID(const string &name, Bool_t zero_id_is_error=kTRUE)
Definition: QwParityDB.cc:790
static std::map< string, unsigned int > fSlowControlDetectorIDs
Associative array of slow controls data IDs. This declaration will be a problem if QwDatabase is used...
Definition: QwParityDB.h:104
QwOptions gQwOptions
Definition: QwOptions.cc:27
UInt_t GetAnalysisID()
Definition: QwParityDB.h:71
static std::map< string, unsigned int > fLumiDetectorIDs
Associative array of LUMI detector IDs. This declaration will be a problem if QwDatabase is used to c...
Definition: QwParityDB.h:103
UInt_t SetRunletID(QwEventBuffer &qwevt)
Definition: QwParityDB.cc:313
char ** GetArgv()
Get the vector of command line arguments.
Definition: QwOptions.h:278
friend class StoreMainDetectorID
Definition: QwParityDB.h:109
po::options_description_easy_init AddOptions(const std::string &blockname="Specialized options")
Add an option to a named block or create new block.
Definition: QwOptions.h:164
std::pair< UInt_t, UInt_t > GetEventRange() const
Definition: QwEventBuffer.h:86
T GetValue(const std::string &key)
Get a templated value.
Definition: QwOptions.h:240
Virtual base class for the parity subsystems.
UInt_t GetRunID()
Definition: QwParityDB.h:69
#define QwDebug
Predefined log drain for debugging output.
Definition: QwLog.h:60
static void DefineAdditionalOptions(QwOptions &options)
Defines QwParityDB-specific class options for QwOptions.
Definition: QwParityDB.cc:877
friend class StoreMeasurementID
Definition: QwParityDB.h:111
Bool_t AllowsReadAccess()
Definition: QwDatabase.h:55
UInt_t fAnalysisID
analysis_id of current analysis pass
Definition: QwParityDB.h:98
void StoreLumiDetectorIDs()
Definition: QwParityDB.cc:808
void StoreSlowControlDetectorIDs()
Definition: QwParityDB.cc:744
void StoreErrorCodeIDs()
Definition: QwParityDB.cc:767
mysqlpp::Query Query(const char *qstr=0)
Definition: QwDatabase.h:66
Int_t GetRunNumber() const
Return CODA file run number.
Definition: QwEventBuffer.h:80
friend class StoreErrorCodeID
Definition: QwParityDB.h:113
friend class StoreLumiDetectorID
Definition: QwParityDB.h:110
int GetArgc()
Get the number of command line arguments.
Definition: QwOptions.h:276
void FillParameterFiles(QwSubsystemArrayParity &subsys)
Definition: QwParityDB.cc:563
void SetupOneRun(QwEventBuffer &qwevt)
Definition: QwParityDB.cc:136
A color changing class for the output stream.
Definition: QwColor.h:98
static std::ostream & endl(std::ostream &)
End of the line.
Definition: QwLog.cc:299
void StoreMeasurementIDs()
Definition: QwParityDB.cc:848
virtual ~QwParityDB()
Destructor.
Definition: QwParityDB.cc:126
static std::map< string, unsigned char > fErrorCodeIDs
Associative array of error code IDs. This declaration will be a problem if QwDatabase is used to conn...
Definition: QwParityDB.h:105
UInt_t fRunNumber
Run number of current run.
Definition: QwParityDB.h:94
static std::map< string, unsigned int > fMonitorIDs
Associative array of beam monitor IDs. This declaration will be a problem if QwDatabase is used to co...
Definition: QwParityDB.h:101
#define QwWarning
Predefined log drain for warnings.
Definition: QwLog.h:45
Bool_t AreRunletsSplit() const
Return true if file segments are being separated for.
Definition: QwEventBuffer.h:76
UInt_t GetSlowControlDetectorID(const string &name)
Definition: QwParityDB.cc:706
friend class StoreMonitorID
Definition: QwParityDB.h:108
void ProcessAdditionalOptions(QwOptions &options)
Processes the options contained in the QwOptions object.
Definition: QwParityDB.cc:891
static std::map< string, unsigned int > fMainDetectorIDs
Associative array of main detector IDs. This declaration will be a problem if QwDatabase is used to c...
Definition: QwParityDB.h:102
#define QwError
Predefined log drain for errors.
Definition: QwLog.h:40