BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
VertexDbSvc.cxx
Go to the documentation of this file.
1// This service is used to read the vertex information from the database
2//
3// an example to use this service is shown in test/test_read.cxx
4//
5// the joboption for the example is shown in share/job-test.txt
6//
7// the joboption for this service is shown in share/jobOptions_VertexDbSvc.txt
8
9
10#include <iostream>
11#include <sstream>
12#include <cstring>
13#include <cstdlib>
14#include <cstdio>
15
16#ifndef BEAN
18#include "GaudiKernel/Kernel.h"
19#include "GaudiKernel/IInterface.h"
20#include "GaudiKernel/StatusCode.h"
21#include "GaudiKernel/SvcFactory.h"
22#include "GaudiKernel/MsgStream.h"
23#include "GaudiKernel/ISvcLocator.h"
24#include "GaudiKernel/SmartDataPtr.h"
25#include "GaudiKernel/IDataProviderSvc.h"
26#include "GaudiKernel/PropertyMgr.h"
27
28
29#include "GaudiKernel/IIncidentSvc.h"
30#include "GaudiKernel/Incident.h"
31#include "GaudiKernel/IIncidentListener.h"
32
33#include "GaudiKernel/ISvcLocator.h"
34#include "GaudiKernel/Bootstrap.h"
36#include "EventModel/Event.h"
38
39#include <TMath.h>
40#else
42#endif
43
44using namespace std;
45
46
47#ifndef BEAN
48VertexDbSvc::VertexDbSvc( const string& name, ISvcLocator* svcloc) :
49 Service (name, svcloc){
50 // declare properties
51 declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
52 declareProperty("DbName" , dbName = std::string("offlinedb"));
53 declareProperty("UserName" , userName = std::string("guest"));
54 declareProperty("Password" , password = std::string("guestpass"));
55 declareProperty("BossVer" , m_bossver = std::string("default"));
56 declareProperty("VerPar" , m_verpar = std::string("default"));
57 declareProperty("BossRelease",m_bossRelease = std::string("default"));
58}
59
61}
62
63StatusCode VertexDbSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
64 if( IID_IVertexDbSvc.versionMatch(riid) ){
65 *ppvInterface = static_cast<IVertexDbSvc*> (this);
66 } else{
67 return Service::queryInterface(riid, ppvInterface);
68 }
69 return StatusCode::SUCCESS;
70}
71
73 MsgStream log(messageService(), name());
74 log << MSG::INFO << "VertexDbSvc::initialize()" << endreq;
75
76 StatusCode sc = Service::initialize();
77 if( sc.isFailure() ) return sc;
78
79
80 IIncidentSvc* incsvc;
81 sc = service("IncidentSvc", incsvc);
82 int priority = 100;
83 if( sc.isSuccess() ){
84 incsvc -> addListener(this, "NewRun", priority);
85 }
86
87 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
88 if (sc .isFailure() ) {
89 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
90 return sc;
91 }
92
93 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
94 if (sc .isFailure() ) {
95 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
96 return sc;
97 }
98 return StatusCode::SUCCESS;
99}
100
102 MsgStream log(messageService(), name());
103 log << MSG::INFO << "VertexDbSvc::finalize()" << endreq;
104 // if(m_connect_offline) delete m_connect_offline;
105 return StatusCode::SUCCESS;
106}
107
108void VertexDbSvc::handle(const Incident& inc){
109 MsgStream log( messageService(), name() );
110 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
111
112 if ( inc.type() == "NewRun" ){
113 log << MSG::DEBUG << "NewRun" << endreq;
114 getVertexTableInfo();
115 }
116}
117
118
119#else
120// -------------------------- BEAN ------------------------------------
121
122VertexDbSvc* VertexDbSvc::m_vdb = 0;
123
124//-----------------------------------------------------------------------------
126//-----------------------------------------------------------------------------
127{
128 // use functions instead of "declareProperty"
129 dbName = "offlinedb";
130#ifdef ROOTEVENTDATAVERSION
131 m_bossver = ROOTEVENTDATAVERSION;
132 m_bossRelease = ROOTEVENTDATAVERSION;
133#else
134 m_bossver = "default";
135 m_bossRelease = "default";
136#endif
137 m_verpar = "default";
138
139 m_dbsvc = DatabaseSvc::instance();
140}
141
142//-----------------------------------------------------------------------------
143void VertexDbSvc::handle(int new_run)
144//-----------------------------------------------------------------------------
145{
146 static int save_run = 0;
147 if( new_run == save_run ) return;
148
149 cout << "Begin New Run " << new_run << endl;
150 getVertexTableInfo(new_run);
151 save_run = new_run;
152}
153#endif
154
156{
157 if( !m_isRunNumberValid ) {
158 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
159 memset(m_primaryVertex,0,sizeof(m_primaryVertex));
160 }
161 return m_primaryVertex;
162}
163
165{
166 if( !m_isRunNumberValid ) {
167 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
168 memset(m_sigmaPrimaryVertex,0,sizeof(m_sigmaPrimaryVertex));
169 }
170 return m_sigmaPrimaryVertex;
171}
172
173#ifndef BEAN
174StatusCode VertexDbSvc::getVertexTableInfo(){
175 MsgStream log(messageService(), name());
176 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
177 int run = eventHeader->runNumber();
178#else
179//-----------------------------------------------------------------------------
180void VertexDbSvc::getVertexTableInfo(int run)
181//-----------------------------------------------------------------------------
182{
183#endif
184 m_isRunNumberValid = false;
185 int save_run = run;
186
187 if( run < 0 ) {
188#ifndef BEAN
189 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
190#else
191 cout << "This data is the MC sample with the Run Number: " << run << endl;
192#endif
193 run = -run;
194 }
195//#ifndef BEAN
196// if(m_bossver=="default") m_bossver = getenv("BES_RELEASE");
197//#endif
198
199 bool ret_vtx = getReadBunchInfo(run);
200
201 if( !ret_vtx ) {
202 cout << " VertexDbSvc:: can not found vertex information for run:"
203 << run << ", boss version " << m_bossver << endl;
204 exit(1);
205 }
206
207/* if( !ret_vtx && save_run<0 ) {
208 bool ret = false;
209 int real_run = run;
210 for(int kk = 1; kk <= 10000; kk++) {
211 real_run = run+kk;
212 if( (ret = getReadBunchInfo(real_run)) ) break;
213
214 if( run-kk > 0 ) {
215 real_run = run-kk;
216 if( (ret = getReadBunchInfo(real_run)) ) break;
217 }
218 }
219 if( !ret ) {
220#ifndef BEAN
221 log << MSG::ERROR << "Can not find vertex information for run:" <<run<< endreq;
222#else
223 cout << "Can not find vertex information for run:" << run << endl;
224#endif
225 exit(1);
226 }
227#ifndef BEAN
228 log << MSG::INFO << "Use Bunch infor. of run " << real_run
229 << " instead of run " << run << endreq;
230#else
231 cout << "Use Bunch infor. of run " << real_run
232 << " instead of run " << run << endl;
233#endif
234 }
235*/
236 m_isRunNumberValid = true;
237#ifndef BEAN
238 log << MSG::INFO << "Successfully fetch the vertex information for run: "
239 << save_run << endreq;
240 return StatusCode::SUCCESS;
241#else
242 cout << "Successfully fetch the vertex information for run: "
243 << save_run << endl;
244#endif
245}
246
247//-----------------------------------------------------------------------------
248bool VertexDbSvc::getReadBunchInfo(int run)
249//-----------------------------------------------------------------------------
250{
251 if(m_bossver == "default") {
252 if(m_bossRelease == "default") {
253#ifndef BEAN
254 MsgStream log(messageService(), name());
255 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
256 << m_bossRelease << "." << endreq;
257#else
258 cout << "ERROR BossRelease must be set! Current value is "
259 << m_bossRelease << "." << endl;
260#endif
261 exit(1);
262 }
263 else {
264 char stmt1[400];
265 sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", m_bossRelease.c_str(), run, run);
266
267 DatabaseRecordVector records;
268 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
269 if(rowNo == 0) {
270#ifndef BEAN
271 MsgStream log(messageService(), name());
272 log << MSG::ERROR << "ERROR: can not find records for run = " << run
273 << " and BossRelease = " << m_bossRelease << endreq;
274#else
275 cout << "ERROR: can not find records for run = " << run
276 << " and BossRelease = " << m_bossRelease << endl;
277#endif
278 exit(1);
279 }
280 DatabaseRecord* recordst = records[0];
281 m_bossver = recordst->GetString("SftVer");
282 m_verpar = recordst->GetString("ParVer");
283 cout << "Using the SftVer and ParVer (" << m_bossver
284 << ", " << m_verpar << ") for run " << run << ". " << endl;
285 }
286 }
287
288 string stmt = "select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
289 stringstream tmp;
290 tmp << "from BeamPar where RunNo = " << run
291 << " and SftVer=\'" << m_bossver << "\'";
292 if( m_verpar == "default" ) {
293 tmp << " group by ParVer";
294 } else {
295 tmp << " and ParVer = " << m_verpar;
296 }
297 stmt += tmp.str();
298 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
299
301 int row_no = m_dbsvc->query(dbName,stmt,res);
302
303 if( row_no > 0 ) {
304 DatabaseRecord& dbrec = *res[row_no-1];
305 m_primaryVertex[0] = dbrec.GetDouble("Vx");
306 m_primaryVertex[1] = dbrec.GetDouble("Vy");
307 m_primaryVertex[2] = dbrec.GetDouble("Vz");
308 m_sigmaPrimaryVertex[0] = dbrec.GetDouble("SigmaVx");
309 m_sigmaPrimaryVertex[1] = dbrec.GetDouble("SigmaVy");
310 m_sigmaPrimaryVertex[2] = dbrec.GetDouble("SigmaVz");
311 return true;
312 }
313
314 return false;
315}
double GetDouble(std::string key)
std::string GetString(std::string key)
virtual int query(const std::string &dbName, const std::string &sql, DatabaseRecordVector &res)=0
VertexDbSvc(const std::string &name, ISvcLocator *svcloc)
Definition: VertexDbSvc.cxx:48
void handle(const Incident &)
virtual StatusCode initialize()
Definition: VertexDbSvc.cxx:72
double * PrimaryVertex()
virtual StatusCode queryInterface(const InterfaceID &riid, void **ppvUnknown)
Definition: VertexDbSvc.cxx:63
double * SigmaPrimaryVertex()
virtual StatusCode finalize()