Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:21:14

0001 #include "SpinDBInput.h"
0002 #include "SpinDBContent.h"
0003 #include "SpinDBContentv1.h"
0004 
0005 #include <odbc++/connection.h>
0006 #include <odbc++/drivermanager.h>
0007 #include <odbc++/resultset.h>
0008 #include <odbc++/resultsetmetadata.h>
0009 
0010 #include <cstdlib>
0011 #include <format>
0012 #include <iostream>
0013 
0014 SpinDBInput::~SpinDBInput()
0015 {
0016   delete con;
0017 }
0018 
0019 ///////////////////////////////////////////////////////
0020 
0021 void SpinDBInput::Initialize()
0022 {
0023   con = nullptr;
0024   run_check = -1;
0025   qa_check = -1;
0026 
0027   try
0028   {
0029     con = odbc::DriverManager::getConnection(DB_NAME, "phnxrc", "");
0030   }
0031   catch (odbc::SQLException &e)
0032   {
0033     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0034     exit(1);
0035   }
0036 
0037   if (con != nullptr)
0038   {
0039     std::cout << std::format("Connected to {} DB.", TABLE_NAME) << std::endl;
0040   }
0041 
0042   return;
0043 }
0044 
0045 ////////////////////////////////////////////////////////
0046 
0047 int SpinDBInput::IsConnected()
0048 {
0049   if (con == nullptr)
0050   {
0051     std::cout << "Error : No connection to the DB.";
0052     return (0);
0053   }
0054 
0055   return (1);
0056 }
0057 
0058 ////////////////////////////////////////////////////////////
0059 
0060 int SpinDBInput::CheckRunRow(int runnum, int qa_level, const std::string &opt)
0061 {
0062   if ("simple" == opt && run_check == runnum && qa_check == qa_level)
0063   {
0064     return (1);
0065   }
0066 
0067   if (IsConnected() != 1)
0068   {
0069     return (ERROR_VALUE);
0070   }
0071 
0072   std::stringstream cmd;
0073   cmd << "select * from spin where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0074   odbc::Statement *stmt = con->createStatement();
0075   odbc::ResultSet *rs = nullptr;
0076   try
0077   {
0078     rs = stmt->executeQuery(cmd.str());
0079   }
0080   catch (odbc::SQLException &e)
0081   {
0082     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0083     delete rs;
0084     delete stmt;
0085     return (ERROR_VALUE);
0086   }
0087 
0088   int flag = 0;
0089   if (rs->next() != 0)
0090   {
0091     flag = 1;
0092     run_check = runnum;
0093     qa_check = qa_level;
0094   }
0095 
0096   delete rs;
0097   delete stmt;
0098   return (flag);
0099 }
0100 
0101 //////////////////////////////////////////////////////////
0102 
0103 int SpinDBInput::CreateRunRow(int runnum, int qa_level)
0104 {
0105   if (CheckRunRow(runnum, qa_level, "simple") == 1)
0106   {
0107     std::cout << std::format("SpinDBInput::CreateRunRow() Error : Row for run {} with qa level {} seems to exist, check again.", runnum, qa_level) << std::endl;
0108     return (0);
0109   }
0110 
0111   std::stringstream cmd;
0112   cmd << "insert into spin (runnumber,qa_level) values(" << runnum << "," << qa_level << ");";
0113   odbc::Statement *stmt = con->createStatement();
0114   try
0115   {
0116     stmt->execute(cmd.str());
0117   }
0118   catch (odbc::SQLException &e)
0119   {
0120     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0121     return (ERROR_VALUE);
0122   }
0123   delete stmt;
0124 
0125   SpinDBContentv1 spin_cont_temp;
0126   spin_cont_temp.SetRunNumber(runnum);
0127   spin_cont_temp.SetQALevel(qa_level);
0128   InitializeRunRow(spin_cont_temp);
0129 
0130   return (1);
0131 }
0132 
0133 ///////////////////////////////////////////////////////////
0134 
0135 int SpinDBInput::DeleteRunRow(int runnum, int qa_level)
0136 {
0137   if (IsConnected() != 1)
0138   {
0139     return (0);
0140   }
0141 
0142   if (CheckRunRow(runnum, qa_level, "simple") != 1)
0143   {
0144     std::cout << std::format("SpinDBInput::DeleteRunRow() Error : Row for run {} with qa level {} seems not to exist, check again.", runnum, qa_level) << std::endl;
0145     return (0);
0146   }
0147 
0148   std::stringstream cmd;
0149   cmd << "delete from spin where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0150   odbc::Statement *stmt = con->createStatement();
0151   try
0152   {
0153     stmt->execute(cmd.str());
0154   }
0155   catch (odbc::SQLException &e)
0156   {
0157     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0158     delete stmt;
0159     return (ERROR_VALUE);
0160   }
0161 
0162   run_check = -1;
0163   qa_check = -1;
0164   delete stmt;
0165   return (1);
0166 }
0167 
0168 ///////////////////////////////////////////////////////////
0169 
0170 int SpinDBInput::InitializeRunRow(SpinDBContent &spin_cont)
0171 {
0172   if (IsConnected() != 1)
0173   {
0174     return (0);
0175   }
0176 
0177   if (CheckRunRow(spin_cont.GetRunNumber(), spin_cont.GetQALevel(), "simple") != 1)
0178   {
0179     std::cout << std::format("SpinDBInput::UpdateDBContent() Error : Row for run {} with qa level {} seems not to exist, check again.", spin_cont.GetRunNumber(), spin_cont.GetQALevel()) << std::endl;
0180     return (0);
0181   }
0182 
0183   int runnum = spin_cont.GetRunNumber();
0184   int qa_level = spin_cont.GetQALevel();
0185 
0186   int ncross = SpinDBContent::GetNCrossing();
0187 
0188   InitializeValue(runnum, qa_level, "fillnumber");
0189   InitializeValue(runnum, qa_level, "badrunqa");
0190   InitializeValue(runnum, qa_level, "crossingshift");
0191 
0192   InitializeArray(runnum, qa_level, "polarblue", ncross);
0193   InitializeArray(runnum, qa_level, "polarblueerror", ncross);
0194   InitializeArray(runnum, qa_level, "polarblueerrorsys", ncross);
0195   InitializeArray(runnum, qa_level, "polaryellow", ncross);
0196   InitializeArray(runnum, qa_level, "polaryellowerror", ncross);
0197   InitializeArray(runnum, qa_level, "polaryellowerrorsys", ncross);
0198   InitializeArray(runnum, qa_level, "spinpatternblue", ncross);
0199   InitializeArray(runnum, qa_level, "spinpatternyellow", ncross);
0200   InitializeArray(runnum, qa_level, "mbdvtx", ncross);
0201   InitializeArray(runnum, qa_level, "mbdns", ncross);
0202   InitializeArray(runnum, qa_level, "zdcns", ncross);
0203   InitializeArray(runnum, qa_level, "badbunchqa", ncross);
0204 
0205   InitializeValue(runnum, qa_level, "asymbf");
0206   InitializeValue(runnum, qa_level, "asymbb");
0207   InitializeValue(runnum, qa_level, "asymyf");
0208   InitializeValue(runnum, qa_level, "asymyb");
0209   InitializeValue(runnum, qa_level, "asymerrbf");
0210   InitializeValue(runnum, qa_level, "asymerrbb");
0211   InitializeValue(runnum, qa_level, "asymerryf");
0212   InitializeValue(runnum, qa_level, "asymerryb");
0213 
0214   InitializeValue(runnum, qa_level, "phasebf");
0215   InitializeValue(runnum, qa_level, "phasebb");
0216   InitializeValue(runnum, qa_level, "phaseyf");
0217   InitializeValue(runnum, qa_level, "phaseyb");
0218   InitializeValue(runnum, qa_level, "phaseerrbf");
0219   InitializeValue(runnum, qa_level, "phaseerrbb");
0220   InitializeValue(runnum, qa_level, "phaseerryf");
0221   InitializeValue(runnum, qa_level, "phaseerryb");
0222 
0223   InitializeValue(runnum, qa_level, "crossingangle");
0224   InitializeValue(runnum, qa_level, "crossanglestd");
0225   InitializeValue(runnum, qa_level, "crossanglemin");
0226   InitializeValue(runnum, qa_level, "crossanglemax");
0227 
0228   return (1);
0229 }
0230 
0231 ///////////////////////////////////////////////////////////
0232 
0233 int SpinDBInput::UpdateDBContent(SpinDBContent &spin_cont)
0234 {
0235   if (IsConnected() != 1)
0236   {
0237     return (0);
0238   }
0239 
0240   if (CheckRunRow(spin_cont.GetRunNumber(), spin_cont.GetQALevel(), "simple") != 1)
0241   {
0242     std::cout << std::format("SpinDBInput::UpdateDBContent() Error : Row for run {} with qa level {} seems not to exist, check again.", spin_cont.GetRunNumber(), spin_cont.GetQALevel()) << std::endl;
0243     return (0);
0244   }
0245 
0246   int qa_level = spin_cont.GetQALevel();
0247 
0248   if (qa_level == ERROR_VALUE)
0249   {
0250     std::cout << "You did not set a qa_level.  Please do so with SpinDBContent::SetQALevel(int qa_level).  Check that the qa level you set does not exist for this run before trying again.";
0251     return (0);
0252   }
0253 
0254   int runnum = spin_cont.GetRunNumber();
0255   int ncross = SpinDBContent::GetNCrossing();
0256   int fillnum = spin_cont.GetFillNumber();
0257   int badrun = spin_cont.GetBadRunFlag();
0258   int xingshift = spin_cont.GetCrossingShift();
0259   if (fillnum != ERROR_VALUE)
0260   {
0261     UpdateValue(runnum, qa_level, "fillnumber", fillnum);
0262   }
0263   if (badrun != ERROR_VALUE)
0264   {
0265     UpdateValue(runnum, qa_level, "badrunqa", badrun);
0266   }
0267   if (xingshift != ERROR_VALUE)
0268   {
0269     UpdateValue(runnum, qa_level, "crossingshift", xingshift);
0270   }
0271 
0272   // float bpol[ncross], bpolerr[ncross], bpolsys[ncross], ypol[ncross], ypolerr[ncross], ypolsys[ncross];
0273   // int bpat[ncross], ypat[ncross], bad_bunch[ncross];
0274   // long long mbd_vtxcut[ncross], mbd_nocut[ncross];
0275   // long long zdc_nocut[ncross];
0276   float *bpol = new float[ncross];
0277   float *bpolerr = new float[ncross];
0278   float *bpolsys = new float[ncross];
0279   float *ypol = new float[ncross];
0280   float *ypolerr = new float[ncross];
0281   float *ypolsys = new float[ncross];
0282 
0283   int *bpat = new int[ncross];
0284   int *ypat = new int[ncross];
0285   int *bad_bunch = new int[ncross];
0286 
0287   long long *mbd_vtxcut = new long long[ncross];
0288   long long *mbd_nocut = new long long[ncross];
0289   long long *zdc_nocut = new long long[ncross];
0290 
0291   for (int i = 0; i < ncross; i++)
0292   {
0293     spin_cont.GetPolarizationBlue(i, bpol[i], bpolerr[i], bpolsys[i]);
0294     spin_cont.GetPolarizationYellow(i, ypol[i], ypolerr[i], ypolsys[i]);
0295     bpat[i] = spin_cont.GetSpinPatternBlue(i);
0296     ypat[i] = spin_cont.GetSpinPatternYellow(i);
0297     mbd_vtxcut[i] = spin_cont.GetScalerMbdVertexCut(i);
0298     mbd_nocut[i] = spin_cont.GetScalerMbdNoCut(i);
0299     zdc_nocut[i] = spin_cont.GetScalerZdcNoCut(i);
0300     bad_bunch[i] = spin_cont.GetBadBunchFlag(i);
0301   }
0302 
0303   bool cbpol = false;
0304   bool cbpolerr = false;
0305   bool cbpolsys = false;
0306   bool cypol = false;
0307   bool cypolerr = false;
0308   bool cypolsys = false;
0309   bool cbpat = false;
0310   bool cypat = false;
0311   bool cmbd_vtxcut = false;
0312   bool cmbd_nocut = false;
0313   bool czdc_nocut = false;
0314   bool cbad_bunch = false;
0315 
0316   for (int i = 0; i < ncross; i++)
0317   {
0318     if (bpol[i] != ERROR_VALUE)
0319     {
0320       cbpol = true;
0321     }
0322     if (bpolerr[i] != ERROR_VALUE)
0323     {
0324       cbpolerr = true;
0325     }
0326     if (bpolsys[i] != ERROR_VALUE)
0327     {
0328       cbpolsys = true;
0329     }
0330     if (ypol[i] != ERROR_VALUE)
0331     {
0332       cypol = true;
0333     }
0334     if (ypolerr[i] != ERROR_VALUE)
0335     {
0336       cypolerr = true;
0337     }
0338     if (ypolsys[i] != ERROR_VALUE)
0339     {
0340       cypolsys = true;
0341     }
0342     if (bpat[i] != ERROR_VALUE)
0343     {
0344       cbpat = true;
0345     }
0346     if (ypat[i] != ERROR_VALUE)
0347     {
0348       cypat = true;
0349     }
0350     if (mbd_vtxcut[i] != ERROR_VALUE)
0351     {
0352       cmbd_vtxcut = true;
0353     }
0354     if (mbd_nocut[i] != ERROR_VALUE)
0355     {
0356       cmbd_nocut = true;
0357     }
0358     if (zdc_nocut[i] != ERROR_VALUE)
0359     {
0360       czdc_nocut = true;
0361     }
0362     if (bad_bunch[i] != ERROR_VALUE)
0363     {
0364       cbad_bunch = true;
0365     }
0366   }
0367 
0368   if (cbpol)
0369   {
0370     UpdateArray(runnum, qa_level, "polarblue", bpol, ncross);
0371   }
0372   if (cbpolerr)
0373   {
0374     UpdateArray(runnum, qa_level, "polarblueerror", bpolerr, ncross);
0375   }
0376   if (cbpolsys)
0377   {
0378     UpdateArray(runnum, qa_level, "polarblueerrorsys", bpolsys, ncross);
0379   }
0380   if (cypol)
0381   {
0382     UpdateArray(runnum, qa_level, "polaryellow", ypol, ncross);
0383   }
0384   if (cypolerr)
0385   {
0386     UpdateArray(runnum, qa_level, "polaryellowerror", ypolerr, ncross);
0387   }
0388   if (cypolsys)
0389   {
0390     UpdateArray(runnum, qa_level, "polaryellowerrorsys", ypolsys, ncross);
0391   }
0392   if (cbpat)
0393   {
0394     UpdateArray(runnum, qa_level, "spinpatternblue", bpat, ncross);
0395   }
0396   if (cypat)
0397   {
0398     UpdateArray(runnum, qa_level, "spinpatternyellow", ypat, ncross);
0399   }
0400   if (cmbd_vtxcut)
0401   {
0402     UpdateArray(runnum, qa_level, "mbdvtx", mbd_vtxcut, ncross);
0403   }
0404   if (cmbd_nocut)
0405   {
0406     UpdateArray(runnum, qa_level, "mbdns", mbd_nocut, ncross);
0407   }
0408   if (czdc_nocut)
0409   {
0410     UpdateArray(runnum, qa_level, "zdcns", zdc_nocut, ncross);
0411   }
0412   if (cbad_bunch)
0413   {
0414     UpdateArray(runnum, qa_level, "badbunchqa", bad_bunch, ncross);
0415   }
0416 
0417   float a_bf;
0418   float a_bb;
0419   float a_yf;
0420   float a_yb;
0421   float a_bf_err;
0422   float a_bb_err;
0423   float a_yf_err;
0424   float a_yb_err;
0425   float p_bf;
0426   float p_bb;
0427   float p_yf;
0428   float p_yb;
0429   float p_bf_err;
0430   float p_bb_err;
0431   float p_yf_err;
0432   float p_yb_err;
0433   spin_cont.GetAsymBlueForward(a_bf, a_bf_err);
0434   spin_cont.GetAsymBlueBackward(a_bb, a_bb_err);
0435   spin_cont.GetAsymYellowForward(a_yf, a_yf_err);
0436   spin_cont.GetAsymYellowBackward(a_yb, a_yb_err);
0437   spin_cont.GetPhaseBlueForward(p_bf, p_bf_err);
0438   spin_cont.GetPhaseBlueBackward(p_bb, p_bb_err);
0439   spin_cont.GetPhaseYellowForward(p_yf, p_yf_err);
0440   spin_cont.GetPhaseYellowBackward(p_yb, p_yb_err);
0441 
0442   if (a_bf != ERROR_VALUE)
0443   {
0444     UpdateValue(runnum, qa_level, "asymbf", a_bf);
0445   }
0446   if (a_bf_err != ERROR_VALUE)
0447   {
0448     UpdateValue(runnum, qa_level, "asymerrbf", a_bf_err);
0449   }
0450   if (a_bb != ERROR_VALUE)
0451   {
0452     UpdateValue(runnum, qa_level, "asymbb", a_bb);
0453   }
0454   if (a_bb_err != ERROR_VALUE)
0455   {
0456     UpdateValue(runnum, qa_level, "asymerrbb", a_bb_err);
0457   }
0458   if (a_yf != ERROR_VALUE)
0459   {
0460     UpdateValue(runnum, qa_level, "asymyf", a_yf);
0461   }
0462   if (a_yf_err != ERROR_VALUE)
0463   {
0464     UpdateValue(runnum, qa_level, "asymerryf", a_yf_err);
0465   }
0466   if (a_yb != ERROR_VALUE)
0467   {
0468     UpdateValue(runnum, qa_level, "asymyb", a_yb);
0469   }
0470   if (a_yb_err != ERROR_VALUE)
0471   {
0472     UpdateValue(runnum, qa_level, "asymerryb", a_yb_err);
0473   }
0474 
0475   if (p_bf != ERROR_VALUE)
0476   {
0477     UpdateValue(runnum, qa_level, "phasebf", p_bf);
0478   }
0479   if (p_bf_err != ERROR_VALUE)
0480   {
0481     UpdateValue(runnum, qa_level, "phaseerrbf", p_bf_err);
0482   }
0483   if (p_bb != ERROR_VALUE)
0484   {
0485     UpdateValue(runnum, qa_level, "phasebb", p_bb);
0486   }
0487   if (p_bb_err != ERROR_VALUE)
0488   {
0489     UpdateValue(runnum, qa_level, "phaseerrbb", p_bb_err);
0490   }
0491   if (p_yf != ERROR_VALUE)
0492   {
0493     UpdateValue(runnum, qa_level, "phaseyf", p_yf);
0494   }
0495   if (p_yf_err != ERROR_VALUE)
0496   {
0497     UpdateValue(runnum, qa_level, "phaseerryf", p_yf_err);
0498   }
0499   if (p_yb != ERROR_VALUE)
0500   {
0501     UpdateValue(runnum, qa_level, "phaseyb", p_yb);
0502   }
0503   if (p_yb_err != ERROR_VALUE)
0504   {
0505     UpdateValue(runnum, qa_level, "phaseerryb", p_yb_err);
0506   }
0507 
0508   float cross_angle = spin_cont.GetCrossAngle();
0509   float cross_angle_std = spin_cont.GetCrossAngleStd();
0510   float cross_angle_min = spin_cont.GetCrossAngleMin();
0511   float cross_angle_max = spin_cont.GetCrossAngleMax();
0512 
0513   if (cross_angle != ERROR_VALUE)
0514   {
0515     UpdateValue(runnum, qa_level, "crossingangle", cross_angle);
0516   }
0517   if (cross_angle_std != ERROR_VALUE)
0518   {
0519     UpdateValue(runnum, qa_level, "crossanglestd", cross_angle_std);
0520   }
0521   if (cross_angle_min != ERROR_VALUE)
0522   {
0523     UpdateValue(runnum, qa_level, "crossanglemin", cross_angle_min);
0524   }
0525   if (cross_angle_max != ERROR_VALUE)
0526   {
0527     UpdateValue(runnum, qa_level, "crossanglemax", cross_angle_max);
0528   }
0529   delete[] bpol;
0530   delete[] bpolerr;
0531   delete[] bpolsys;
0532   delete[] ypol;
0533   delete[] ypolerr;
0534   delete[] ypolsys;
0535 
0536   delete[] bpat;
0537   delete[] ypat;
0538   delete[] bad_bunch;
0539 
0540   delete[] mbd_vtxcut;
0541   delete[] mbd_nocut;
0542   delete[] zdc_nocut;
0543 
0544   return (1);
0545 }
0546 
0547 /////////////////////////////////////////////////////////
0548 
0549 int SpinDBInput::UpdateValue(int runnum, int qa_level, const std::string &cmd)
0550 {
0551   if (IsConnected() != 1)
0552   {
0553     return (0);
0554   }
0555 
0556   if (CheckRunRow(runnum, qa_level, "simple") != 1)
0557   {
0558     std::cout << std::format("SpinDBInput::UpdateDBContent() Error : Row for run {} with qa level {} seems not to exist, check again.", runnum, qa_level) << std::endl;
0559     return (0);
0560   }
0561 
0562   odbc::Statement *stmt = con->createStatement();
0563   try
0564   {
0565     stmt->execute(cmd);
0566   }
0567   catch (odbc::SQLException &e)
0568   {
0569     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0570     delete stmt;
0571     return (ERROR_VALUE);
0572   }
0573   delete stmt;
0574   std::cout << cmd << '\n';
0575 
0576   return (1);
0577 }
0578 
0579 ////////////////////////////////////////////////////////
0580 
0581 template <class T>
0582 int SpinDBInput::UpdateValueTemp(int runnum, int qa_level, const std::string &name, T value)
0583 {
0584   std::stringstream cmd;
0585   cmd << "update " << TABLE_NAME << " set " << name << "=" << value;
0586   cmd << " where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0587   return (UpdateValue(runnum, qa_level, cmd.str()));
0588 }
0589 
0590 //////////////////////////////////////////////////////////
0591 
0592 int SpinDBInput::UpdateValue(int runnum, int qa_level, const std::string &name, int value)
0593 {
0594   return (UpdateValueTemp(runnum, qa_level, name, value));
0595 }
0596 
0597 //////////////////////////////////////////////////////////
0598 
0599 int SpinDBInput::UpdateValue(int runnum, int qa_level, const std::string &name, float value)
0600 {
0601   return (UpdateValueTemp(runnum, qa_level, name, value));
0602 }
0603 
0604 //////////////////////////////////////////////////////////
0605 
0606 int SpinDBInput::UpdateValue(int runnum, int qa_level, const std::string &name, double value)
0607 {
0608   return (UpdateValueTemp(runnum, qa_level, name, value));
0609 }
0610 
0611 //////////////////////////////////////////////////////////
0612 
0613 template <class T>
0614 int SpinDBInput::UpdateArrayTemp(int runnum, int qa_level, const std::string &name, T *value, int nvalue)
0615 {
0616   std::stringstream cmd;
0617   cmd << "update " << TABLE_NAME << " set " << name << "='{";
0618   for (int i = 0; i < nvalue; i++)
0619   {
0620     cmd << value[i];
0621     if (i < nvalue - 1)
0622     {
0623       cmd << ",";
0624     }
0625   }
0626   cmd << "}' where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0627   return (UpdateValue(runnum, qa_level, cmd.str()));
0628 }
0629 
0630 /////////////////////////////////////////////////////////////////
0631 
0632 int SpinDBInput::UpdateArray(int runnum, int qa_level, const std::string &name, int *value, int nvalue)
0633 {
0634   return (UpdateArrayTemp(runnum, qa_level, name, value, nvalue));
0635 }
0636 
0637 /////////////////////////////////////////////////////////
0638 
0639 int SpinDBInput::UpdateArray(int runnum, int qa_level, const std::string &name, float *value, int nvalue)
0640 {
0641   return (UpdateArrayTemp(runnum, qa_level, name, value, nvalue));
0642 }
0643 
0644 /////////////////////////////////////////////////////////
0645 
0646 int SpinDBInput::UpdateArray(int runnum, int qa_level, const std::string &name, double *value, int nvalue)
0647 {
0648   return (UpdateArrayTemp(runnum, qa_level, name, value, nvalue));
0649 }
0650 
0651 /////////////////////////////////////////////////////////
0652 
0653 int SpinDBInput::UpdateArray(int runnum, int qa_level, const std::string &name, unsigned int *value, int nvalue)
0654 {
0655   return (UpdateArrayTemp(runnum, qa_level, name, value, nvalue));
0656 }
0657 
0658 /////////////////////////////////////////////////////////
0659 
0660 int SpinDBInput::UpdateArray(int runnum, int qa_level, const std::string &name, long long *value, int nvalue)
0661 {
0662   return (UpdateArrayTemp(runnum, qa_level, name, value, nvalue));
0663 }
0664 
0665 //////////////////////////////////////////////////////////
0666 
0667 int SpinDBInput::InitializeValue(int runnum, int qa_level, const std::string &name)
0668 {
0669   return (UpdateValue(runnum, qa_level, name, ERROR_VALUE));
0670 }
0671 
0672 //////////////////////////////////////////////////////////
0673 
0674 int SpinDBInput::InitializeArray(int runnum, int qa_level, const std::string &name, int nvalue)
0675 {
0676   int *ERROR_ARRAY = new int[nvalue];
0677   for (int i = 0; i < nvalue; i++)
0678   {
0679     ERROR_ARRAY[i] = ERROR_VALUE;
0680   }
0681   int iret = UpdateArray(runnum, qa_level, name, ERROR_ARRAY, nvalue);
0682   delete[] ERROR_ARRAY;
0683   return (iret);
0684 }
0685 
0686 ////////////////////////////////////////////////////////////
0687 int SpinDBInput::SetDefaultQA(SpinDBContent &spin_cont)
0688 {
0689   if (IsConnected() != 1)
0690   {
0691     return (0);
0692   }
0693 
0694   int qa_level = spin_cont.GetQALevel();
0695 
0696   if (qa_level == ERROR_VALUE)
0697   {
0698     std::cout << "You did not set a qa_level.  Please do so with SpinDBContent::SetQALevel(int qa_level).  Check that the qa level you set does not exist for this run before trying again.";
0699     return (0);
0700   }
0701 
0702   int runnum = spin_cont.GetRunNumber();
0703 
0704   if (CheckRunRow(runnum, qa_level, "simple") != 1)
0705   {
0706     std::cout << std::format("SpinDBInput::DeleteRunRow() Error : Row for run {} with qa level {} seems not to exist, check again.", runnum, qa_level) << std::endl;
0707     return (0);
0708   }
0709 
0710   std::stringstream cmd1;
0711   cmd1 << " update spin set is_default = FALSE where runnumber=" << runnum << " and is_default = TRUE;";
0712   odbc::Statement *stmt1 = con->createStatement();
0713   try
0714   {
0715     stmt1->execute(cmd1.str());
0716   }
0717   catch (odbc::SQLException &e)
0718   {
0719     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0720     delete stmt1;
0721     return (ERROR_VALUE);
0722   }
0723 
0724   std::stringstream cmd2;
0725   cmd2 << " update spin set is_default = TRUE where runnumber=" << runnum << " and qa_level = " << qa_level << ";";
0726   odbc::Statement *stmt2 = con->createStatement();
0727   try
0728   {
0729     stmt2->execute(cmd2.str());
0730   }
0731   catch (odbc::SQLException &e)
0732   {
0733     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0734     delete stmt2;
0735     return (ERROR_VALUE);
0736   }
0737 
0738   run_check = -1;
0739   qa_check = -1;
0740   delete stmt1;
0741   delete stmt2;
0742   return (1);
0743 }