BOSS 7.0.9
BESIII Offline Software System
Loading...
Searching...
No Matches
ConnectionDB.cxx
Go to the documentation of this file.
1#ifndef BEAN
2#include "GaudiKernel/MsgStream.h"
3#include "GaudiKernel/SvcFactory.h"
4#include "GaudiKernel/ISvcLocator.h"
5#include "GaudiKernel/SmartDataPtr.h"
6#include "GaudiKernel/Bootstrap.h"
7
8#include "facilities/Util.h"
9#else
11#endif
12
13#include <iostream>
14#include <cstdio>
15#include <cstdlib>
16
18
19using namespace std;
20
21namespace FieldDBUtil {
22
23#ifndef BEAN
25 StatusCode sc = Gaudi::svcLocator()->service("DatabaseSvc", m_dbsvc, true);
26 if (sc .isFailure() ) {
27 std::cout << "ERROR: In ConnectionDB()--> Unable to find DatabaseSvc " << std::endl;
28 }
29 }
30#endif
31
32 ConnectionDB::eRet ConnectionDB::getReadSC_MagnetInfo( std::vector<double>& current, int runNo) {
33
34#ifdef BEAN
35 static DatabaseSvc* m_dbsvc = 0;
36
37 if( !m_dbsvc ) {
38 m_dbsvc = DatabaseSvc::instance();
39 }
40#endif
41
42 //Read magnetic field map
43 char stmt1[200];
44 int run_No = std::abs(runNo);
45
46 sprintf(stmt1,"select Magnet_Current,SCQL,SCQR from SC_magnet where run_number = %d ",run_No);
47 //cout<<stmt1<<endl;
48 DatabaseRecordVector results;
49 results.clear();
50 int status = m_dbsvc->query("run",stmt1,results);
51
52 if(status<0){
53 std::cout << "ERROR Read the SSM and SCQ current from the Database" << endl;
54 exit(1);
55 }
56
57 int RowNumber = results.size();
58 if(RowNumber!=1){
59 std::cout<<"ERROR:error searching SC_Magnet Data in the database, check your selection criterions"<<std::endl;
60 return RETMySQLError;
61 }
62
63 DatabaseRecord& rec = *results[0];
64 double ssm_curr = rec.GetDouble("Magnet_Current");
65 double scql_curr = rec.GetDouble("SCQL");
66 double scqr_curr = rec.GetDouble("SCQR");
67
68 // save results in vector
69 current.resize(3);
70 current[0] = ssm_curr;
71 current[1] = scql_curr;
72 current[2] = scqr_curr;
73 //cout<<"run:"<<run_No<<"Magnet_Current:"<<current[0]<<"SCQL:"<<current[1]<<"SCQR:"<<current[2]<<endl;
74 return RETOk;
75 }
76
77bool ConnectionDB::getBeamEnergy( std::map<int, std::vector<double> >& m_mapBeamEnergy, int runFrom, int runTo)
78{
79 char stmt1[200];
80 int run_From = std::abs(runFrom);
81 int run_To = std::abs(runTo);
83 results.clear();
84
85 IDatabaseSvc* m_dbsvc;
86 StatusCode sc = Gaudi::svcLocator()->service("DatabaseSvc",m_dbsvc,true);
87 if (sc .isFailure() ) {
88 std::cout<< "MSG::ERROR " << "Unable to find DatabaseSvc " << std::endl;
89 exit(1) ;
90 }
91
92 sprintf(stmt1,"select run_number,BPR_PRB,BER_PRB from RunParams where run_number >= %d and run_number <= %d ",run_From,run_To);
93 //cout<<stmt1<<endl;
94 int row_no = m_dbsvc->query("run",stmt1,results);
95 if(row_no<=0){
96 std::cout <<"ERROR:"<< "Run:"<<run_From<<" Can not read the beam energy from the Database" << endl;
97 exit(1); }
98
99 /*int RowNumber = results.size();
100 if(RowNumber == 0) {
101 beamE.push_back(1.843); // for positron
102 beamE.push_back(1.843); // for electron
103 std::cout << "No beam energy, so set BPR_PRB=1.843, BER_PRB=1.843"<<std::endl;
104 exit(1);
105 }*/
106 if( row_no > 0 )
107 {
108 for(int i=0;i<row_no;i++)
109 {
110 DatabaseRecord& dbrec = *results[i];
111 int run_No = dbrec.GetInt("run_number");
112 std::vector<double> beamEnergy;
113 beamEnergy.push_back(dbrec.GetDouble("BPR_PRB"));
114 beamEnergy.push_back(dbrec.GetDouble("BER_PRB"));
115 m_mapBeamEnergy[run_No] = beamEnergy;
116 float beam1,beam2;
117 beam1=beamEnergy[0];
118 beam2=beamEnergy[1];
119 cout<<"map of run:"<<run_No<<"BPR_PRB:"<<beam1<<"BER_PRB:"<<beam2<<endl;
120
121 }
122 return true;
123 }
124}
125
126
127 bool ConnectionDB::getReadSC_MagnetInfo(std::map<int, std::vector<double> >& m_mapMagnetInfo, int runFrom, int runTo)
128 {
129 char stmt1[200];
130 int run_From = std::abs(runFrom);
131 int run_To = std::abs(runTo);
132 DatabaseRecordVector results;
133 results.clear();
134
135 IDatabaseSvc* m_dbsvc;
136 StatusCode sc = Gaudi::svcLocator()->service("DatabaseSvc",m_dbsvc,true);
137 if (sc .isFailure() ) {
138 std::cout<< "MSG::ERROR " << "Unable to find DatabaseSvc " << std::endl;
139 exit(1) ;
140 }
141 sprintf(stmt1,"select run_number,Magnet_Current,SCQL,SCQR from SC_magnet where run_number >= %d and run_number<=%d ",run_From,run_To);
142 //cout<<stmt1<<endl;
143 int row_no = m_dbsvc->query("run",stmt1,results);
144 if(row_no<=0){
145 std::cout << "ERROR Read the SSM and SCQ current from the Database"<< " Run:"<<run_From << endl; exit(1); }
146 if( row_no > 0 )
147 {
148 for(int i=0;i<row_no;i++)
149 {
150 DatabaseRecord& dbrec = *results[i];
151 int run_No = dbrec.GetInt("run_number");
152 std::vector<double> SCMagnet;
153 SCMagnet.push_back(dbrec.GetDouble("Magnet_Current"));
154 SCMagnet.push_back(dbrec.GetDouble("SCQL"));
155 SCMagnet.push_back(dbrec.GetDouble("SCQR"));
156 m_mapMagnetInfo[run_No] = SCMagnet;
157 //cout<<"map of run:"<<run_No<<"Magnet_Current:"<<(m_mapMagnetInfo[run_No])[0]<<"SCQL:"<<(m_mapMagnetInfo[run_No])[1]<<"SCQR:"<<(m_mapMagnetInfo[run_No])[2]<<endl;
158 }
159
160 }
161 return true;
162
163 }
164 ConnectionDB::eRet ConnectionDB::getBeamEnergy( std::vector<double>& beamE, int runNo) {
165 //Read magnetic field map
166 char stmt1[200];
167 int run_No = std::abs(runNo);
168
169 sprintf(stmt1,"select BPR_PRB,BER_PRB from RunParams where run_number = %d ",run_No);
170 //cout<<stmt1<<endl;
171 DatabaseRecordVector results;
172 results.clear();
173 int status = m_dbsvc->query("run",stmt1,results);
174 if(status<0){
175 std::cout << "ERROR Read the beam energy from the Database" << endreq;
176 exit(1);
177 }
178
179 int RowNumber = results.size();
180
181 if(RowNumber == 0) {
182 beamE.push_back(1.843); // for positron
183 beamE.push_back(1.843); // for electron
184
185 return RETOk;
186 }
187
188 if(RowNumber!=1){
189 std::cout<<"ERROR:error searching beam energy in the database, check your selection criterions"<<std::endl;
190 return RETMySQLError;
191 }
192
193 beamE.push_back(atof((*results[0])["BPR_PRB"])); // for positron
194 beamE.push_back(atof((*results[0])["BER_PRB"])); // for electron
195 float beam1,beam2;
196 beam1=atof((*results[0])["BPR_PRB"]);
197 beam2=atof((*results[0])["BER_PRB"]);
198 //cout<<"map of run:"<<run_No<<"BPR_PRB:"<<beam1<<"BER_PRB:"<<beam2<<endl;
199 return RETOk;
200 }
201
202}
sprintf(cut,"kal_costheta0_em>-0.93&&kal_costheta0_em<0.93&&kal_pxy0_em>=0.05+%d*0.1&&kal_pxy0_em<0.15+%d*0.1&&NGch>=2", j, j)
int runNo
Definition: DQA_TO_DB.cxx:12
double GetDouble(std::string key)
int GetInt(std::string key)
int query(const std::string &dbName, const std::string &sql)
ConnectionDB()
Constructor keeps track of table of interest.
ConnectionDB::eRet getReadSC_MagnetInfo(std::vector< double > &current, int runNo)
ConnectionDB::eRet getBeamEnergy(std::vector< double > &beamE, int runNo)
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0