Back to home page

sPhenix code displayed by LXR

 
 

    


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

0001 #include "SpinDBOutput.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 #include <limits>
0014 #include <sstream>
0015 
0016 //////////////////////////////////////////////////////////
0017 
0018 void SpinDBOutput::Initialize()
0019 {
0020   // default get from spin table from spin DB
0021   db_name = "spinDB";
0022   user_name = "phnxrc";
0023   table_name = "spin";
0024 
0025   spin_cont_store.clear();
0026   return;
0027 }
0028 
0029 //////////////////////////////////////////////////////////
0030 
0031 void SpinDBOutput::SetDBName(const std::string &dbname)
0032 {
0033   // Only DB to read from is "spinDB"
0034   if (dbname == "spinDB")
0035   {
0036     std::cout << std::format(" Database name is changed from {} to {}", db_name, dbname) << std::endl;
0037     db_name = dbname;
0038   }
0039   else
0040   {
0041     std::cout << std::format(" Your input database name, {}, is invalid", dbname) << std::endl;
0042     std::cout << std::format(" No change, try it as default, {}", db_name) << std::endl;
0043   }
0044 
0045   return;
0046 }
0047 
0048 //////////////////////////////////////////////////////////
0049 
0050 void SpinDBOutput::SetTableName(const std::string &tname)
0051 {
0052   // Only table is "spin"
0053   if (tname == "spin")
0054   {
0055     std::cout << std::format(" Table_name is changed from {} to {}", table_name, tname) << std::endl;
0056     table_name = tname;
0057   }
0058   else
0059   {
0060     std::cout << std::format(" Your input table name, {}, is invalid", tname) << std::endl;
0061     std::cout << std::format(" No change, try it as default, {}", db_name) << std::endl;
0062   }
0063   return;
0064 }
0065 
0066 //////////////////////////////////////////////////////////
0067 
0068 odbc::Connection *SpinDBOutput::ConnectDB()
0069 {
0070   odbc::Connection *con = nullptr;
0071   try
0072   {
0073     con = odbc::DriverManager::getConnection(db_name, user_name, "");
0074   }
0075   catch (odbc::SQLException &e)
0076   {
0077     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0078     return (nullptr);
0079   }
0080 
0081   return (con);
0082 }
0083 
0084 /////////////////////////////////////////////////////////////
0085 
0086 int SpinDBOutput::PrintDBColumn()
0087 {
0088   odbc::Connection *con = ConnectDB();
0089   if (con == nullptr)
0090   {
0091     return (0);
0092   }
0093 
0094   std::stringstream cmd;
0095   cmd << "select * from " << table_name << ";";
0096   odbc::Statement *stmt = con->createStatement();
0097   odbc::ResultSet *rs = nullptr;
0098   try
0099   {
0100     rs = stmt->executeQuery(cmd.str());
0101   }
0102   catch (odbc::SQLException &e)
0103   {
0104     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0105     delete rs;
0106     delete stmt;
0107     delete con;
0108     return (ERROR_VALUE);
0109   }
0110 
0111   int ncol = rs->getMetaData()->getColumnCount();
0112   for (int icol = 0; icol < ncol; icol++)
0113   {
0114     int col_type = rs->getMetaData()->getColumnType(icol + 1);
0115     std::string col_type_name = rs->getMetaData()->getColumnTypeName(icol + 1);
0116     std::string col_name = rs->getMetaData()->getColumnName(icol + 1);
0117     std::cout << std::format("{:2} : {:2} {:7s} {}", icol, col_type, col_type_name, col_name) << std::endl;
0118   }
0119 
0120   delete rs;
0121   delete stmt;
0122   delete con;
0123   return (1);
0124 }
0125 
0126 ///////////////////////////////////////////////////////////
0127 int SpinDBOutput::PrintDBRawContent(int runnum)
0128 {
0129   odbc::Connection *con = ConnectDB();
0130   if (con == nullptr)
0131   {
0132     return (0);
0133   }
0134 
0135   std::stringstream cmd;
0136   cmd << "select * from " << table_name << " where runnumber=" << runnum << " and is_default=TRUE;";
0137   odbc::Statement *stmt = con->createStatement();
0138   odbc::ResultSet *rs = nullptr;
0139   try
0140   {
0141     rs = stmt->executeQuery(cmd.str());
0142   }
0143   catch (odbc::SQLException &e)
0144   {
0145     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0146     delete rs;
0147     delete stmt;
0148     delete con;
0149     return (ERROR_VALUE);
0150   }
0151 
0152   if (rs->next() == 0)
0153   {
0154     std::cout << std::format("Error : Can't find data for run {} ", runnum) << std::endl;
0155     delete rs;
0156     delete stmt;
0157     delete con;
0158     return (0);
0159   }
0160 
0161   int ncol = rs->getMetaData()->getColumnCount();
0162   for (int icol = 0; icol < ncol; icol++)
0163   {
0164     std::string col_name = rs->getMetaData()->getColumnName(icol + 1);
0165     std::string cont = rs->getString(col_name);
0166     std::cout << std::format("{:2} : {} = {}", icol, col_name, cont) << std::endl;
0167   }
0168 
0169   delete rs;
0170   delete stmt;
0171   delete con;
0172   return (1);
0173 }
0174 
0175 int SpinDBOutput::PrintDBRawContent(int runnum, int qa_level)
0176 {
0177   odbc::Connection *con = ConnectDB();
0178   if (con == nullptr)
0179   {
0180     return (0);
0181   }
0182 
0183   std::stringstream cmd;
0184   cmd << "select * from " << table_name << " where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0185   odbc::Statement *stmt = con->createStatement();
0186   odbc::ResultSet *rs = nullptr;
0187   try
0188   {
0189     rs = stmt->executeQuery(cmd.str());
0190   }
0191   catch (odbc::SQLException &e)
0192   {
0193     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0194     delete rs;
0195     delete stmt;
0196     delete con;
0197     return (ERROR_VALUE);
0198   }
0199 
0200   if (rs->next() == 0)
0201   {
0202     std::cout << std::format("Error : Can't find data for run {} (with qa_level {})", runnum, qa_level) << std::endl;
0203     delete rs;
0204     delete stmt;
0205     delete con;
0206     return (0);
0207   }
0208 
0209   int ncol = rs->getMetaData()->getColumnCount();
0210   for (int icol = 0; icol < ncol; icol++)
0211   {
0212     std::string col_name = rs->getMetaData()->getColumnName(icol + 1);
0213     std::string cont = rs->getString(col_name);
0214     std::cout << std::format("{:2} : {} = {}", icol, col_name, cont) << std::endl;
0215   }
0216 
0217   delete rs;
0218   delete stmt;
0219   delete con;
0220   return (1);
0221 }
0222 
0223 ///////////////////////////////////////////////////////////
0224 int SpinDBOutput::CheckRunRow(int runnum)
0225 {
0226   if (CheckRunRowStore(runnum) == 1)
0227   {
0228     return (1);
0229   }
0230 
0231   odbc::Connection *con = ConnectDB();
0232   if (con == nullptr)
0233   {
0234     std::cout << "No Connection to the Database!";
0235     return (0);
0236   }
0237 
0238   std::stringstream cmd;
0239   cmd << "select * from " << table_name << " where runnumber=" << runnum << " and is_default=TRUE;";
0240   odbc::Statement *stmt = con->createStatement();
0241   odbc::ResultSet *rs = nullptr;
0242   try
0243   {
0244     rs = stmt->executeQuery(cmd.str());
0245   }
0246   catch (odbc::SQLException &e)
0247   {
0248     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0249     delete rs;
0250     delete stmt;
0251     delete con;
0252     return (ERROR_VALUE);
0253   }
0254 
0255   if (rs->next() == 0)
0256   {
0257     delete rs;
0258     delete stmt;
0259     delete con;
0260     return (0);
0261   }
0262 
0263   delete rs;
0264   delete stmt;
0265   delete con;
0266 
0267   return (1);
0268 }
0269 
0270 int SpinDBOutput::CheckRunRow(int runnum, int qa_level)
0271 {
0272   if (CheckRunRowStore(runnum) == 1)
0273   {
0274     return (1);
0275   }
0276 
0277   odbc::Connection *con = ConnectDB();
0278   if (con == nullptr)
0279   {
0280     std::cout << "No Connection to the Database!";
0281     return (0);
0282   }
0283 
0284   std::stringstream cmd;
0285   cmd << "select * from " << table_name << " where runnumber=" << runnum << " and qa_level=" << qa_level << ";";
0286   odbc::Statement *stmt = con->createStatement();
0287   odbc::ResultSet *rs = nullptr;
0288   try
0289   {
0290     rs = stmt->executeQuery(cmd.str());
0291   }
0292   catch (odbc::SQLException &e)
0293   {
0294     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0295     delete rs;
0296     delete stmt;
0297     delete con;
0298     return (ERROR_VALUE);
0299   }
0300 
0301   if (rs->next() == 0)
0302   {
0303     delete rs;
0304     delete stmt;
0305     delete con;
0306     return (0);
0307   }
0308 
0309   delete rs;
0310   delete stmt;
0311   delete con;
0312 
0313   return (1);
0314 }
0315 
0316 ///////////////////////////////////////////////////////////
0317 
0318 int SpinDBOutput::CheckRunRowStore(int runnum)
0319 {
0320   std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0321   if (it == spin_cont_store.end())
0322   {
0323     return 0;
0324   }
0325   return 1;
0326 }
0327 
0328 /////////////////////////////////////////////////////////////
0329 int SpinDBOutput::StoreDBContent(int run1, int run2)
0330 {
0331   odbc::Connection *con = ConnectDB();
0332   if (con == nullptr)
0333   {
0334     return (0);
0335   }
0336 
0337   std::stringstream cmd;
0338   cmd << "select * from " << table_name << " where runnumber>" << run1 - 1;
0339   cmd << " and runnumber<" << run2 + 1 << " and is_default=TRUE;";
0340 
0341   odbc::Statement *stmt = con->createStatement();
0342   odbc::ResultSet *rs = nullptr;
0343   try
0344   {
0345     rs = stmt->executeQuery(cmd.str());
0346   }
0347   catch (odbc::SQLException &e)
0348   {
0349     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0350     delete rs;
0351     delete stmt;
0352     delete con;
0353     return (ERROR_VALUE);
0354   }
0355 
0356   while (rs->next() != 0)
0357   {
0358     std::unique_ptr<SpinDBContent> spin_cont(new SpinDBContentv1());
0359     GetDBContent(*spin_cont, rs);
0360 
0361     int runnum = spin_cont->GetRunNumber();
0362     std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0363     if (it != spin_cont_store.end())
0364     {
0365       it->second = std::move(spin_cont);
0366     }
0367     else
0368     {
0369       spin_cont_store.insert(std::make_pair(runnum, std::move(spin_cont)));
0370     }
0371   }
0372 
0373   delete rs;
0374   delete stmt;
0375   delete con;
0376 
0377   return (1);
0378 }
0379 
0380 int SpinDBOutput::StoreDBContent(int run1, int run2, int qa_level)
0381 {
0382   odbc::Connection *con = ConnectDB();
0383   if (con == nullptr)
0384   {
0385     return (0);
0386   }
0387 
0388   std::stringstream cmd;
0389   cmd << "select * from " << table_name << " where runnumber>" << run1 - 1;
0390   cmd << " and runnumber<" << run2 + 1 << " and qa_level=" << qa_level << ";";
0391 
0392   odbc::Statement *stmt = con->createStatement();
0393   odbc::ResultSet *rs = nullptr;
0394   try
0395   {
0396     rs = stmt->executeQuery(cmd.str());
0397   }
0398   catch (odbc::SQLException &e)
0399   {
0400     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0401     delete rs;
0402     delete stmt;
0403     delete con;
0404     return (ERROR_VALUE);
0405   }
0406 
0407   while (rs->next() != 0)
0408   {
0409     std::unique_ptr<SpinDBContent> spin_cont(new SpinDBContentv1());
0410     GetDBContent(*spin_cont, rs);
0411 
0412     int runnum = spin_cont->GetRunNumber();
0413     std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0414     if (it != spin_cont_store.end())
0415     {
0416       it->second = std::move(spin_cont);
0417     }
0418     else
0419     {
0420       spin_cont_store.insert(std::make_pair(runnum, std::move(spin_cont)));
0421     }
0422   }
0423 
0424   delete rs;
0425   delete stmt;
0426   delete con;
0427 
0428   return (1);
0429 }
0430 
0431 /////////////////////////////////////////////////////////////////
0432 
0433 void SpinDBOutput::ClearDBContent()
0434 {
0435   spin_cont_store.clear();
0436   return;
0437 }
0438 
0439 //////////////////////////////////////////////////////////////
0440 int SpinDBOutput::GetDBContent(SpinDBContent *&spin_cont, int runnum)
0441 {
0442   std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0443   if (it != spin_cont_store.end())
0444   {
0445     spin_cont = it->second.get();
0446     return 1;
0447   }
0448 
0449   odbc::Connection *con = ConnectDB();
0450   if (con == nullptr)
0451   {
0452     return (0);
0453   }
0454 
0455   std::stringstream cmd;
0456   cmd << "select * from " << table_name << " where runnumber=" << runnum << " and is_default=TRUE;";
0457   odbc::Statement *stmt = con->createStatement();
0458   odbc::ResultSet *rs = nullptr;
0459   try
0460   {
0461     rs = stmt->executeQuery(cmd.str());
0462   }
0463   catch (odbc::SQLException &e)
0464   {
0465     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0466     delete rs;
0467     delete stmt;
0468     delete con;
0469     return (ERROR_VALUE);
0470   }
0471 
0472   if (rs->next() == 0)
0473   {
0474     std::cout << std::format("Error : Can't find data for run {}", runnum) << std::endl;
0475     delete rs;
0476     delete stmt;
0477     delete con;
0478     return (0);
0479   }
0480 
0481   std::unique_ptr<SpinDBContent> spin_cont_ptr(new SpinDBContentv1());
0482   GetDBContent(*spin_cont_ptr, rs);
0483   spin_cont_store[runnum] = std::move(spin_cont_ptr);
0484   CopyDBContent(*spin_cont_store[runnum], *spin_cont);
0485 
0486   delete rs;
0487   delete stmt;
0488   delete con;
0489 
0490   return (1);
0491 }
0492 
0493 int SpinDBOutput::GetDBContent(SpinDBContent *&spin_cont, int runnum, int qa_level)
0494 {
0495   std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0496   if (it != spin_cont_store.end())
0497   {
0498     spin_cont = it->second.get();
0499     return 1;
0500   }
0501 
0502   odbc::Connection *con = ConnectDB();
0503   if (con == nullptr)
0504   {
0505     return (0);
0506   }
0507 
0508   std::stringstream cmd;
0509   cmd << "select * from " << table_name << " where runnumber=" << runnum << " with qa_level=" << qa_level << ";";
0510   odbc::Statement *stmt = con->createStatement();
0511   odbc::ResultSet *rs = nullptr;
0512   try
0513   {
0514     rs = stmt->executeQuery(cmd.str());
0515   }
0516   catch (odbc::SQLException &e)
0517   {
0518     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0519     delete rs;
0520     delete stmt;
0521     delete con;
0522     return (ERROR_VALUE);
0523   }
0524 
0525   if (rs->next() == 0)
0526   {
0527     std::cout << std::format("Error : Can't find data for run {} (with qa_level {})", runnum, qa_level) << std::endl;
0528     delete rs;
0529     delete stmt;
0530     delete con;
0531     return (0);
0532   }
0533 
0534   std::unique_ptr<SpinDBContent> spin_cont_ptr(new SpinDBContentv1());
0535   GetDBContent(*spin_cont_ptr, rs);
0536   spin_cont_store[runnum] = std::move(spin_cont_ptr);
0537   CopyDBContent(*spin_cont_store[runnum], *spin_cont);
0538 
0539   delete rs;
0540   delete stmt;
0541   delete con;
0542 
0543   return (1);
0544 }
0545 
0546 /////////////////////////////////////////////////////////////
0547 
0548 int SpinDBOutput::GetDBContentStore(SpinDBContent *&spin_cont, int runnum)
0549 {
0550   std::map<int, std::unique_ptr<SpinDBContent>>::iterator it = spin_cont_store.find(runnum);
0551   if (it != spin_cont_store.end())
0552   {
0553     CopyDBContent(*it->second, *spin_cont);
0554     return 1;
0555   }
0556 
0557   std::cout << std::format("Error : Can't find row for run {}.", runnum) << std::endl;
0558 
0559   return 0;
0560 }
0561 
0562 //////////////////////////////////////////////////////////////
0563 int SpinDBOutput::CopyDBContent(SpinDBContent &spin_cont, SpinDBContent &spin_cont_copy)
0564 {
0565   int ncross = SpinDBContent::GetNCrossing();
0566   spin_cont_copy.SetRunNumber(spin_cont.GetRunNumber());
0567   spin_cont_copy.SetQALevel(spin_cont.GetQALevel());
0568   spin_cont_copy.SetFillNumber(spin_cont.GetFillNumber());
0569   spin_cont_copy.SetBadRunFlag(spin_cont.GetBadRunFlag());
0570   spin_cont_copy.SetCrossingShift(spin_cont.GetCrossingShift());
0571 
0572   for (int i = 0; i < ncross; i++)
0573   {
0574     float b_pol;
0575     float b_pol_err;
0576     float b_pol_sys;
0577     float y_pol;
0578     float y_pol_err;
0579     float y_pol_sys;
0580     spin_cont.GetPolarizationBlue(i, b_pol, b_pol_err, b_pol_sys);
0581     spin_cont.GetPolarizationYellow(i, y_pol, y_pol_err, y_pol_sys);
0582     spin_cont_copy.SetPolarizationBlue(i, b_pol, b_pol_err, b_pol_sys);
0583     spin_cont_copy.SetPolarizationYellow(i, y_pol, y_pol_err, y_pol_sys);
0584     spin_cont_copy.SetSpinPatternBlue(i, spin_cont.GetSpinPatternBlue(i));
0585     spin_cont_copy.SetSpinPatternYellow(i, spin_cont.GetSpinPatternYellow(i));
0586     spin_cont_copy.SetScalerMbdVertexCut(i, spin_cont.GetScalerMbdVertexCut(i));
0587     spin_cont_copy.SetScalerMbdNoCut(i, spin_cont.GetScalerMbdNoCut(i));
0588     spin_cont_copy.SetScalerZdcNoCut(i, spin_cont.GetScalerZdcNoCut(i));
0589     spin_cont_copy.SetBadBunchFlag(i, spin_cont.GetBadBunchFlag(i));
0590   }
0591 
0592   float asym_bf;
0593   float asymerr_bf;
0594   float asym_bb;
0595   float asymerr_bb;
0596   float asym_yf;
0597   float asymerr_yf;
0598   float asym_yb;
0599   float asymerr_yb;
0600   float phase_bf;
0601   float phaseerr_bf;
0602   float phase_bb;
0603   float phaseerr_bb;
0604   float phase_yf;
0605   float phaseerr_yf;
0606   float phase_yb;
0607   float phaseerr_yb;
0608   spin_cont.GetAsymBlueForward(asym_bf, asymerr_bf);
0609   spin_cont.GetAsymBlueBackward(asym_bb, asymerr_bb);
0610   spin_cont.GetAsymYellowForward(asym_yf, asymerr_yf);
0611   spin_cont.GetAsymYellowBackward(asym_yb, asymerr_yb);
0612   spin_cont.GetPhaseBlueForward(phase_bf, phaseerr_bf);
0613   spin_cont.GetPhaseBlueBackward(phase_bb, phaseerr_bb);
0614   spin_cont.GetPhaseYellowForward(phase_yf, phaseerr_yf);
0615   spin_cont.GetPhaseYellowBackward(phase_yb, phaseerr_yb);
0616 
0617   spin_cont_copy.SetAsymBlueForward(asym_bf, asymerr_bf);
0618   spin_cont_copy.SetAsymBlueBackward(asym_bb, asymerr_bb);
0619   spin_cont_copy.SetAsymYellowForward(asym_yf, asymerr_yf);
0620   spin_cont_copy.SetAsymYellowBackward(asym_yb, asymerr_yb);
0621   spin_cont_copy.SetPhaseBlueForward(phase_bf, phaseerr_bf);
0622   spin_cont_copy.SetPhaseBlueBackward(phase_bb, phaseerr_bb);
0623   spin_cont_copy.SetPhaseYellowForward(phase_yf, phaseerr_yf);
0624   spin_cont_copy.SetPhaseYellowBackward(phase_yb, phaseerr_yb);
0625 
0626   spin_cont_copy.SetCrossAngle(spin_cont.GetCrossAngle());
0627   spin_cont_copy.SetCrossAngleStd(spin_cont.GetCrossAngleStd());
0628   spin_cont_copy.SetCrossAngleMin(spin_cont.GetCrossAngleMin());
0629   spin_cont_copy.SetCrossAngleMax(spin_cont.GetCrossAngleMax());
0630 
0631   return (1);
0632 }
0633 
0634 int SpinDBOutput::GetDBContent(SpinDBContent &spin_cont, odbc::ResultSet *rs)
0635 {
0636   int ncross = SpinDBContent::GetNCrossing();
0637 
0638   spin_cont.SetRunNumber(rs->getInt("runnumber"));
0639   spin_cont.SetQALevel(rs->getInt("qa_level"));
0640   spin_cont.SetFillNumber(rs->getInt("fillnumber"));
0641   spin_cont.SetBadRunFlag(rs->getInt("badrunqa"));
0642   spin_cont.SetCrossingShift(rs->getInt("crossingshift"));
0643 
0644   // float bpol[ncross], bpolerr[ncross], bpolsys[ncross], ypol[ncross], ypolerr[ncross], ypolsys[ncross];
0645   // int bpat[ncross], ypat[ncross], bad_bunch[ncross];
0646   // long long mbd_vtxcut[ncross], mbd_nocut[ncross];
0647   // long long zdc_nocut[ncross];
0648 
0649   float *bpol = new float[ncross];
0650   float *bpolerr = new float[ncross];
0651   float *bpolsys = new float[ncross];
0652   float *ypol = new float[ncross];
0653   float *ypolerr = new float[ncross];
0654   float *ypolsys = new float[ncross];
0655 
0656   int *bpat = new int[ncross];
0657   int *ypat = new int[ncross];
0658   int *bad_bunch = new int[ncross];
0659 
0660   long long *mbd_vtxcut = new long long[ncross];
0661   long long *mbd_nocut = new long long[ncross];
0662   long long *zdc_nocut = new long long[ncross];
0663 
0664   GetArray(rs, "polarblue", bpol, ncross);
0665   GetArray(rs, "polarblueerror", bpolerr, ncross);
0666   if (table_name == "spin")
0667   {
0668     GetArray(rs, "polarblueerrorsys", bpolsys, ncross);
0669   }
0670 
0671   GetArray(rs, "polaryellow", ypol, ncross);
0672   GetArray(rs, "polaryellowerror", ypolerr, ncross);
0673   if (table_name == "spin")
0674   {
0675     GetArray(rs, "polaryellowerrorsys", ypolsys, ncross);
0676   }
0677   else
0678   {
0679     for (int i = 0; i < ncross; i++)
0680     {
0681       ypolsys[i] = std::numeric_limits<float>::quiet_NaN();
0682     }
0683   }
0684   GetArray(rs, "spinpatternblue", bpat, ncross);
0685   GetArray(rs, "spinpatternyellow", ypat, ncross);
0686   GetArray(rs, "mbdvtx", mbd_vtxcut, ncross);
0687   GetArray(rs, "mbdns", mbd_nocut, ncross);
0688   GetArray(rs, "zdcns", zdc_nocut, ncross);
0689   GetArray(rs, "badbunchqa", bad_bunch, ncross);
0690 
0691   for (int i = 0; i < ncross; i++)
0692   {
0693     spin_cont.SetPolarizationBlue(i, bpol[i], bpolerr[i], bpolsys[i]);
0694     spin_cont.SetPolarizationYellow(i, ypol[i], ypolerr[i], ypolsys[i]);
0695     spin_cont.SetSpinPatternBlue(i, bpat[i]);
0696     spin_cont.SetSpinPatternYellow(i, ypat[i]);
0697     spin_cont.SetScalerMbdVertexCut(i, mbd_vtxcut[i]);
0698     spin_cont.SetScalerMbdNoCut(i, mbd_nocut[i]);
0699     spin_cont.SetScalerZdcNoCut(i, zdc_nocut[i]);
0700     spin_cont.SetBadBunchFlag(i, bad_bunch[i]);
0701   }
0702 
0703   spin_cont.SetAsymBlueForward(rs->getFloat("asymbf"), rs->getFloat("asymerrbf"));
0704   spin_cont.SetAsymBlueBackward(rs->getFloat("asymbb"), rs->getFloat("asymerrbb"));
0705   spin_cont.SetAsymYellowForward(rs->getFloat("asymyf"), rs->getFloat("asymerryf"));
0706   spin_cont.SetAsymYellowBackward(rs->getFloat("asymyb"), rs->getFloat("asymerryb"));
0707   spin_cont.SetPhaseBlueForward(rs->getFloat("phasebf"), rs->getFloat("phaseerrbf"));
0708   spin_cont.SetPhaseBlueBackward(rs->getFloat("phasebb"), rs->getFloat("phaseerrbb"));
0709   spin_cont.SetPhaseYellowForward(rs->getFloat("phaseyf"), rs->getFloat("phaseerryf"));
0710   spin_cont.SetPhaseYellowBackward(rs->getFloat("phaseyb"), rs->getFloat("phaseerryb"));
0711 
0712   spin_cont.SetCrossAngle(rs->getFloat("crossingangle"));
0713   spin_cont.SetCrossAngleStd(rs->getFloat("crossanglestd"));
0714   spin_cont.SetCrossAngleMin(rs->getFloat("crossanglemin"));
0715   spin_cont.SetCrossAngleMax(rs->getFloat("crossanglemax"));
0716 
0717   delete[] bpol;
0718   delete[] bpolerr;
0719   delete[] bpolsys;
0720   delete[] ypol;
0721   delete[] ypolerr;
0722   delete[] ypolsys;
0723 
0724   delete[] bpat;
0725   delete[] ypat;
0726   delete[] bad_bunch;
0727 
0728   delete[] mbd_vtxcut;
0729   delete[] mbd_nocut;
0730   delete[] zdc_nocut;
0731   return (1);
0732 }
0733 
0734 ///////////////////////////////////////////////////////////////
0735 
0736 int SpinDBOutput::GetArray(odbc::ResultSet *rs, const std::string &name, std::vector<std::string> &value) const
0737 {
0738   std::string cvalue;
0739   try
0740   {
0741     cvalue = rs->getString(name);
0742   }
0743   catch (odbc::SQLException &e)
0744   {
0745     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0746     return (ERROR_VALUE);
0747   }
0748 
0749   int length = cvalue.size();
0750   if (length)
0751   {
0752     if (cvalue.compare(0, 1, "{") != 0 || cvalue.compare(length - 1, 1, "}") != 0)
0753     {
0754       if (verbosity > 0)
0755       {
0756         std::cout << std::format("Error : Is this array? ({}), length = {}", name, length) << std::endl;
0757       }
0758       return 0;
0759     }
0760   }
0761   else
0762   {
0763     if (verbosity > 0)
0764     {
0765       std::cout << std::format("Error : Is this array? ({}), length = {}", name, length) << std::endl;
0766     }
0767     return 0;
0768   }
0769 
0770   cvalue = cvalue.substr(1, cvalue.size() - 1) + ",";
0771 
0772   //  int nvalue = 0;
0773   std::vector<std::string> value1;
0774   while (true)
0775   {
0776     size_t pos = cvalue.find(std::string(","));
0777     if (pos == std::string::npos)
0778     {
0779       break;
0780     }
0781 
0782     value1.push_back(cvalue.substr(0, pos));
0783     cvalue.erase(0, pos + 1);
0784     //    nvalue++;
0785   }
0786 
0787   std::stringstream cerror;
0788   cerror << SpinDBContent::GetErrorValue();
0789   for (int i = 0; i < (int) value.size(); i++)
0790   {
0791     if (i < (int) value1.size())
0792     {
0793       value[i] = value1[i];
0794     }
0795     else
0796     {
0797       value[i] = cerror.str();
0798     }
0799   }
0800 
0801   if (value1.size() != value.size())
0802   {
0803     std::cout << "Error : Number of values are inconsistent with expected.";
0804     std::cout << " (" << name << " : " << value1.size() << " != " << value.size() << ")" << std::endl;
0805     return (0);
0806   }
0807 
0808   return (1);
0809 }
0810 
0811 //////////////////////////////////////////////////////////////
0812 
0813 int SpinDBOutput::GetArray(odbc::ResultSet *rs, const std::string &name, float *value, int nvalue)
0814 {
0815   std::vector<std::string> svalue(nvalue, "");
0816   int ret = GetArray(rs, name, svalue);
0817   for (int i = 0; i < nvalue; i++)
0818   {
0819     value[i] = atof(svalue[i].c_str());
0820   }
0821   return (ret);
0822 }
0823 
0824 /////////////////////////////////////////////////////////////////
0825 
0826 int SpinDBOutput::GetArray(odbc::ResultSet *rs, const std::string &name, unsigned int *value, int nvalue)
0827 {
0828   std::vector<std::string> svalue(nvalue, "");
0829   int ret = GetArray(rs, name, svalue);
0830   for (int i = 0; i < nvalue; i++)
0831   {
0832     value[i] = (unsigned int) atoi(svalue[i].c_str());
0833   }
0834   return (ret);
0835 }
0836 
0837 /////////////////////////////////////////////////////////////////
0838 
0839 int SpinDBOutput::GetArray(odbc::ResultSet *rs, const std::string &name, int *value, int nvalue)
0840 {
0841   std::vector<std::string> svalue(nvalue, "");
0842   int ret = GetArray(rs, name, svalue);
0843   for (int i = 0; i < nvalue; i++)
0844   {
0845     value[i] = atoi(svalue[i].c_str());
0846   }
0847   return (ret);
0848 }
0849 
0850 /////////////////////////////////////////////////////////////////
0851 
0852 int SpinDBOutput::GetArray(odbc::ResultSet *rs, const std::string &name, long long *value, int nvalue)
0853 {
0854   std::vector<std::string> svalue(nvalue, "");
0855   int ret = GetArray(rs, name, svalue);
0856   for (int i = 0; i < nvalue; i++)
0857   {
0858     value[i] = 0;
0859   }
0860   for (int i = 0; i < nvalue; i++)
0861   {
0862     std::istringstream iss(svalue[i]);
0863     iss >> value[i];
0864     // sscanf(svalue[i].c_str(),"%lld",&value[i]);
0865   }
0866   return (ret);
0867 }
0868 
0869 /////////////////////////////////////////////////////////////////
0870 
0871 int SpinDBOutput::GetDefaultQA(int runnum)
0872 {
0873   odbc::Connection *con = ConnectDB();
0874   if (con == nullptr)
0875   {
0876     return (0);
0877   }
0878 
0879   std::stringstream cmd;
0880   cmd << "select qa_level from " << table_name << " where runnumber=" << runnum << " and is_default=TRUE;";
0881   odbc::Statement *stmt = con->createStatement();
0882   odbc::ResultSet *rs = nullptr;
0883   try
0884   {
0885     rs = stmt->executeQuery(cmd.str());
0886   }
0887   catch (odbc::SQLException &e)
0888   {
0889     std::cout << std::format("Error: {}.", e.getMessage()) << std::endl;
0890     delete rs;
0891     delete stmt;
0892     delete con;
0893     return (ERROR_VALUE);
0894   }
0895 
0896   if (rs->next() == 0)
0897   {
0898     std::cout << std::format("Error : Can't find data for run {}", runnum) << std::endl;
0899     delete rs;
0900     delete stmt;
0901     delete con;
0902     return (0);
0903   }
0904 
0905   int default_qa_level = rs->getInt("qa_level");
0906 
0907   delete rs;
0908   delete stmt;
0909   delete con;
0910 
0911   return (default_qa_level);
0912 }