Diana Software
QCalibrationParametersHandle.cc
Go to the documentation of this file.
3 #include "QDianaDb.hh"
4 
5 using namespace Diana;
6 
8 {
9 
11 
13  calpar.Clear();
14 
15  std::string bareOwner, extraLabel;
16  GetLabel().GetBareOwnerAndExtraLabel(bareOwner,extraLabel);
17  if (extraLabel=="") extraLabel="None";
18 
19  std::string name, formula;
20  int npar =0,npeaks = 0;
21  std::vector<double> params, err_params, peak_energies, residuals, err_residuals;
22  double chi_square = Q_INT_DEFAULT, ndf = Q_INT_DEFAULT;
23  std::vector<int> sruns;
24 
25  QDianaDb *db = QDianaDb::Get();
26 
27 
28  std::stringstream squery;
29 
30  // query function name
31 
32  squery<<"select calib_function_name from "<<bareOwner<<"_calib_algo"
33  << " where calib_version = '"<<GetVersion()
34  << "' and calib_extralabel= '"<<extraLabel << "'";
35 
36  try {
37  name = db->DoQueryString(squery.str());
38  }
39 
40  catch(const QError& cerr) {
41  if(cerr!=QERR_SUCCESS) {
42  err = cerr;
43  return err;
44  }
45  }
46 
47  squery.str("");
48 
49  // query function formula
50 
51  squery<<"select calib_function_formula from "<<bareOwner<<"_calib_algo"
52  << " where calib_version = '"<<GetVersion()
53  << "' and calib_extralabel= '"<<extraLabel << "'";
54 
55  try {
56  formula = db->DoQueryString(squery.str());
57  }
58 
59  catch(const QError& cerr) {
60  if(cerr!=QERR_SUCCESS) {
61  err = cerr;
62  return err;
63  }
64  }
65 
66  squery.str("");
67 
68  // query source
69  squery<<"select calib_source from "<<bareOwner<<"_calib_algo"
70  << " where calib_version = '"<<GetVersion()
71  << "' and calib_extralabel= '"<<extraLabel << "'";
72 
73  try {
74  calpar.fSource = db->DoQueryString(squery.str());
75  }
76 
77  catch(const QError& cerr) {
78  if(cerr!=QERR_SUCCESS) {
79  err = cerr;
80  return err;
81  }
82  }
83 
84  squery.str("");
85 
86  // query npar
87  squery<<"select calib_function_par_number from "<<bareOwner<<"_calib_algo"
88  << " where calib_version = '"<<GetVersion()
89  << "' and calib_extralabel= '"<<extraLabel << "'";
90 
91  try {
92  npar = db->DoQueryInt(squery.str());
93  }
94 
95  catch(const QError& cerr) {
96  if(cerr!=QERR_SUCCESS) {
97  err = cerr;
98  return err;
99  }
100  }
101 
102  squery.str("");
103 
104  // query params
105  squery<<"select params from calibrations where calib_algo = '"<<bareOwner
106  << "' and calib_version = '"<<GetVersion()
107  << "' and calib_extralabel= '"<<extraLabel
108  << "' and channel = "<<GetChannel()
109  << " and data_set = "<<GetDataset();
110 
111  try {
112  params = db->DoQueryVectorDouble(squery.str());
113  }
114 
115  catch(const QError& cerr) {
116  if(cerr!=QERR_SUCCESS) {
117  err = cerr;
118  return err;
119  }
120  }
121 
122  squery.str("");
123 
124  // query err_params
125  squery<<"select err_params from calibrations where calib_algo = '"<<bareOwner
126  << "' and calib_version = '"<<GetVersion()
127  << "' and calib_extralabel= '"<<extraLabel
128  << "' and channel = "<<GetChannel()
129  << " and data_set = "<<GetDataset();
130 
131  try {
132  err_params = db->DoQueryVectorDouble(squery.str());
133  }
134 
135  catch(const QError& cerr) {
136  if(cerr!=QERR_SUCCESS) {
137  err = cerr;
138  return err;
139  }
140  }
141 
142  squery.str("");
143 
144  // query npeaks
145  squery<<"select npeaks from calibrations where calib_algo = '"<<bareOwner
146  << "' and calib_version = '"<<GetVersion()
147  << "' and calib_extralabel= '"<<extraLabel
148  << "' and channel = "<<GetChannel()
149  << " and data_set = "<<GetDataset();
150 
151  try {
152  npeaks = db->DoQueryInt(squery.str());
153  }
154 
155  catch(const QError& cerr) {
156  if(cerr!=QERR_SUCCESS) {
157  err = cerr;
158  return err;
159  }
160  }
161 
162  squery.str("");
163 
164  // query peak_energies
165  squery<<"select peak_energies from calibrations where calib_algo = '"<<bareOwner
166  << "' and calib_version = '"<<GetVersion()
167  << "' and calib_extralabel= '"<<extraLabel
168  << "' and channel = "<<GetChannel()
169  << " and data_set = "<<GetDataset();
170 
171  try {
172  peak_energies = db->DoQueryVectorDouble(squery.str());
173  }
174 
175  catch(const QError& cerr) {
176  if(cerr!=QERR_SUCCESS) {
177  err = cerr;
178  return err;
179  }
180  }
181 
182  squery.str("");
183 
184  // query residuals
185  squery<<"select residuals from calibrations where calib_algo = '"<<bareOwner
186  << "' and calib_version = '"<<GetVersion()
187  << "' and calib_extralabel= '"<<extraLabel
188  << "' and channel = "<<GetChannel()
189  << " and data_set = "<<GetDataset();
190 
191  try {
192  residuals = db->DoQueryVectorDouble(squery.str());
193  }
194 
195  catch(const QError& cerr) {
196  if(cerr!=QERR_SUCCESS) {
197  err = cerr;
198  return err;
199  }
200  }
201 
202  squery.str("");
203 
204  // query err_residuals
205  squery<<"select err_residuals from calibrations where calib_algo = '"<<bareOwner
206  << "' and calib_version = '"<<GetVersion()
207  << "' and calib_extralabel= '"<<extraLabel
208  << "' and channel = "<<GetChannel()
209  << " and data_set = "<<GetDataset();
210 
211  try {
212  err_residuals = db->DoQueryVectorDouble(squery.str());
213  }
214 
215  catch(const QError& cerr) {
216  if(cerr!=QERR_SUCCESS) {
217  err = cerr;
218  return err;
219  }
220  }
221 
222  squery.str("");
223 
224  // query chi_square
225  squery<<"select chi_square from calibrations where calib_algo = '"<<bareOwner
226  << "' and calib_version = '"<<GetVersion()
227  << "' and calib_extralabel= '"<<extraLabel
228  << "' and channel = "<<GetChannel()
229  << " and data_set = "<<GetDataset();
230 
231  try {
232  chi_square = db->DoQueryDouble(squery.str());
233  }
234 
235  catch(const QError& cerr) {
236  if(cerr!=QERR_SUCCESS) {
237  err = cerr;
238  return err;
239  }
240  }
241 
242  squery.str("");
243 
244  // query ndf
245  squery<<"select ndf from calibrations where calib_algo = '"<<bareOwner
246  << "' and calib_version = '"<<GetVersion()
247  << "' and calib_extralabel= '"<<extraLabel
248  << "' and channel = "<<GetChannel()
249  << " and data_set = "<<GetDataset();
250 
251  try {
252  ndf = db->DoQueryDouble(squery.str());
253  }
254 
255  catch(const QError& cerr) {
256  if(cerr!=QERR_SUCCESS) {
257  err = cerr;
258  return err;
259  }
260  }
261 
262  squery.str("");
263 
264  // query source_runs
265  squery<<"select source_runs from calibrations where calib_algo = '"<<bareOwner
266  <<"' and calib_version= '"<<GetVersion()
267  <<"' and calib_extralabel= '"<<extraLabel
268  <<"' and channel= "<< GetChannel()
269  << " and data_set = "<<GetDataset();
270 
271  try {
272  sruns = db->DoQueryVectorInt(squery.str());
273  }
274 
275  catch(const QError& cerr) {
276  if(cerr!=QERR_SUCCESS) {
277  err = cerr;
278  return err;
279  }
280  }
281 
282  squery.str("");
283 
284  TF1 func(name.c_str(),formula.c_str());
285  calpar.fFunction = func;
286 
287  for(Int_t i=0; i<npar; i++) {
288  calpar.fFunction.SetParameter(i,params[i]);
289  calpar.fFunction.SetParError(i,err_params[i]);
290  }
291 
292  calpar.fFunction.SetChisquare(chi_square);
293  calpar.fChisquareNdf = ndf;
294  calpar.fResiduals.resize(npeaks);
295 
296  for(Int_t i=0; i<npeaks; i++) {
297  calpar.fResiduals[i].PeakEnergy = peak_energies[i];
298  calpar.fResiduals[i].Residual = residuals[i];
299  calpar.fResiduals[i].ErrorResidual = err_residuals[i];
300  }
301 
302  for(size_t i=0; i<sruns.size(); i++)
303  {
304  calpar.fSourceRuns.push_back(sruns[i]);
305  }
306 
307  Set(calpar);
308 
309  // fill fStabLabel from table calibrations
310 
311  std::string stab_algo, stab_version, stab_extralabel;
312 
313  QDb::QDbTable table;
314 
315  squery.str("");
316 
317  squery<<"select stab_algo, stab_version, stab_extralabel from calibrations where calib_algo = '"<<bareOwner
318  <<"' and calib_version= '"<<GetVersion()
319  <<"' and calib_extralabel= '"<<extraLabel
320  <<"' and channel= "<< GetChannel()
321  <<" and data_set= "<<GetDataset();
322 
323  try {
324  table = db->DoQuery(squery.str());
325  }
326 
327  catch(const QError& cerr) {
328  err = cerr;
329  return err;
330  }
331 
332  try {
333  if(table["stab_algo"].size() == 1) {
334  stab_algo = table["stab_algo"].begin()->GetString();
335  stab_version = table["stab_version"].begin()->GetString();
336  stab_extralabel = table["stab_extralabel"].begin()->GetString();
337  } else {
339  std::string errMsg("The primary key was not found in DB; Check you input.\nFill the object with default values.\nQuery: ");
340  errMsg +=squery.str();
341  err.SetDescription(__FILE__, __LINE__, errMsg);
342  return err;
343  }
344  }
345  catch(const QError& cerr) {
346  if(cerr!=QERR_SUCCESS) {
347  err = cerr;
348  return err;
349  }
350  }
351 
352  fStabLabel = stab_algo + "_" + stab_extralabel;
353  if(stab_extralabel=="None") fStabLabel = stab_algo;
354  fStabVersion = stab_version;
355 
356  return err;
357 }
358 
360 {
362 
363  const QCalibrationParameters& cp = Get();
364 
365  std::string bareOwner, extraLabel;
366  GetLabel().GetBareOwnerAndExtraLabel(bareOwner,extraLabel);
367  if (extraLabel=="") extraLabel="None";
368 
369  std::string stab_algo, stab_extralabel;
370  QGlobalLabel stab_label = fStabLabel;
371 
372  if (stab_label.GetStringLabel()!=Q_STRING_DEFAULT) {
373  stab_label.GetBareOwnerAndExtraLabel(stab_algo,stab_extralabel);
374  }
375  if (stab_extralabel=="") stab_extralabel = "None";
376  if(stab_algo.empty() && fStabLabel.find_first_of("@") == std::string::npos) stab_algo = fStabLabel;
377 
378  // function infos
379 
380  const std::string &name = cp.fFunction.GetName();
381  const std::string &formula = cp.fFunction.GetTitle();
382  const std::string &source = cp.fSource;
383  const int npar = cp.fFunction.GetNpar();
384 
385  // creates a string containing all components of the vector params and err_params
386 
387  std::stringstream list_par;
388  list_par<< "'{";
389 
390  std::stringstream list_par_err;
391  list_par_err <<"'{";
392 
393  for(Int_t i=0; i<npar; i++) {
394  if(i==npar-1){
395  list_par<<cp.fFunction.GetParameter(i)<<"}'";
396  list_par_err<<cp.fFunction.GetParError(i)<<"}'";
397  }else{
398  list_par<<cp.fFunction.GetParameter(i)<<",";
399  list_par_err<<cp.fFunction.GetParError(i)<<",";
400  }
401  }
402 
403  if (npar==0) {
404  list_par.str("NULL");
405  list_par_err.str("NULL");
406  }
407 
408  // residuals
409 
410  const int npeaks = cp.fResiduals.size();
411 
412  // creates a string containing all components of the vector peak_energies, residuals and err_residuals
413 
414  std::stringstream list_en;
415  list_en<< "'{";
416 
417  std::stringstream list_res;
418  list_res<< "'{";
419 
420  std::stringstream list_res_err;
421  list_res_err<< "'{";
422 
423  for(Int_t i=0; i<npeaks; i++) {
424  if(i==npeaks-1){
425  list_en<<cp.fResiduals[i].PeakEnergy<<"}'";
426  list_res<< cp.fResiduals[i].Residual<<"}'";;
427  list_res_err<< cp.fResiduals[i].ErrorResidual<<"}'";
428  }else{
429  list_en<<cp.fResiduals[i].PeakEnergy<<",";
430  list_res<< cp.fResiduals[i].Residual<<",";;
431  list_res_err<< cp.fResiduals[i].ErrorResidual<<",";
432  }
433  }
434  if (npeaks==0) {
435  list_en.str("NULL");
436  list_res.str("NULL");
437  list_res_err.str("NULL");
438  }
439 
440  std::stringstream squery;
441 
442  // query insert into xxx_calib_algo where xxx is the name of the calibration module (now CalCoefficients)
443 
444  // first check if the table is already filled
445  squery<<"select calib_function_name from "<<bareOwner<<"_calib_algo"
446  << " where calib_version = '"<<GetVersion()
447  << "' and calib_extralabel= '"<<extraLabel<<"'";
448 
449  try {
450  QDianaDb *db = QDianaDb::Get();
451  std::string el = db->DoQueryString(squery.str());
452  }
453 
454  catch(const QError& cerr) {
455  if(cerr!=QERR_SUCCESS) {
456 
457  squery.str("");
458 
459  squery<<"INSERT into "<<bareOwner<<"_calib_algo (calib_version, calib_extralabel, calib_source, calib_function_name, calib_function_formula, calib_function_par_number) values "
460  <<"('"
461  << GetVersion()<<"','"
462  << extraLabel<<"','"
463  << source<<"','"
464  << name<<"','"
465  << formula<<"',"
466  << npar<<")";
467 
468  try {
469  QDianaDb *db = QDianaDb::Get();
470  db->DoExec(squery.str());
471  }
472 
473  catch(const QError& cerr) {
474  if(cerr!=QERR_SUCCESS) {
475  err = cerr;
476  return err;
477  }
478  }
479  }
480  }
481 
482  squery.str("");
483 
484  // creates a string containing source runs list
485  std::stringstream list_run;
486  list_run<< "'{";
487 
488  for(size_t i=0; i<cp.fSourceRuns.size(); i++)
489  {
490  if(i==cp.fSourceRuns.size()-1) {
491  list_run<<cp.fSourceRuns[i]<<"}'";
492  } else{
493  list_run<<cp.fSourceRuns[i]<<",";
494  }
495  }
496 
497  // query insert into calibrations
498 
499  QVdt::QVdt_vector fields,values;
500  fields.push_back((std::string)"calib_algo");
501  fields.push_back((std::string)"calib_version");
502  fields.push_back((std::string)"calib_extralabel");
503  fields.push_back((std::string)"channel");
504  fields.push_back((std::string)"data_set");
505  fields.push_back((std::string)"stab_algo");
506  fields.push_back((std::string)"stab_version");
507  fields.push_back((std::string)"stab_extralabel");
508  fields.push_back((std::string)"params");
509  fields.push_back((std::string)"err_params");
510  fields.push_back((std::string)"npeaks");
511  fields.push_back((std::string)"peak_energies");
512  fields.push_back((std::string)"residuals");
513  fields.push_back((std::string)"err_residuals");
514  fields.push_back((std::string)"chi_square");
515  fields.push_back((std::string)"ndf");
516  fields.push_back((std::string)"source_runs");
517  values.push_back("'"+bareOwner+"'");
518  values.push_back("'"+GetVersion()+"'");
519  values.push_back("'"+extraLabel+"'");
520  values.push_back(GetChannel());
521  values.push_back(GetDataset());
522  values.push_back("'"+stab_algo+"'");
523  values.push_back("'"+fStabVersion+"'");
524  values.push_back("'"+stab_extralabel+"'");
525  values.push_back(list_par.str());
526  values.push_back(list_par_err.str());
527  values.push_back(npeaks);
528  values.push_back(list_en.str());
529  values.push_back(list_res.str());
530  values.push_back(list_res_err.str());
531  values.push_back(cp.fFunction.GetChisquare());
532  values.push_back(cp.fChisquareNdf);
533  values.push_back(list_run.str());
534 
535  try {
536  QDianaDb *db = QDianaDb::Get();
537  db->Insert("calibrations",fields,values);
538  }
539  catch(const QError& cerr) {
540  if(cerr!=QERR_SUCCESS) {
541  err = cerr;
542  return err;
543  }
544  }
545 
546 
547  return err;
548 }
err
Definition: CheckOF.C:114
dm Get("DAQ",&rHandle,"DB")
hvec Set(vec)
#define Q_STRING_DEFAULT
Definition: QDiana.hh:38
#define Q_INT_DEFAULT
Definition: QDiana.hh:26
@ QERR_DB_NULL_RESULT
Definition: QError.hh:104
@ QERR_SUCCESS
Definition: QError.hh:27
object containing calibration data (function, coefficients, source and residuals) and run numbers of ...
std::vector< int > fSourceRuns
std::vector< QCalibrationResiduals > fResiduals
std::vector< double > DoQueryVectorDouble(const std::string &query)
Definition: QDb.cc:260
std::vector< int > DoQueryVectorInt(const std::string &query)
Definition: QDb.cc:245
QError DoQuery(const std::string &query, QDbTable &table)
Definition: QDb.cc:409
std::map< std::string, column > QDbTable
Definition: QDb.hh:34
int DoQueryInt(const std::string &query)
Definition: QDb.cc:197
double DoQueryDouble(const std::string &query)
Definition: QDb.cc:181
int DoExec(const std::string &Query)
Execute an INSERT, UPDATE, DELETE, FETCH, or MOVE statement.
Definition: QDb.cc:305
std::string DoQueryString(const std::string &query)
Definition: QDb.cc:229
int Insert(const std::string &tableName, const column &fields, const column &values)
Definition: QDb.cc:320
static QDianaDb * Get()
Definition: QDianaDb.cc:21
error class with error type and description
Definition: QError.hh:115
Label for global QObject's.
Definition: QGlobalLabel.hh:19
void GetBareOwnerAndExtraLabel(std::string &bOwner, std::string &el) const
split extra label and bare module name
std::string GetStringLabel() const
convert label to string
std::vector< QVdt > QVdt_vector
Definition: QVdt.hh:38
the Diana namespace is needed because sometimes we use Qt libraries, that use same class names of our...