Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:18:39

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