BOSS 7.1.1
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
48DECLARE_COMPONENT(VertexDbSvc)
49VertexDbSvc::VertexDbSvc( const string& name, ISvcLocator* svcloc) :
50 base_class(name, svcloc){
51 // declare properties
52 declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
53 declareProperty("DbName" , dbName = std::string("offlinedb"));
54 declareProperty("UserName" , userName = std::string("guest"));
55 declareProperty("Password" , password = std::string("guestpass"));
56 declareProperty("BossVer" , m_bossver = std::string("default"));
57 declareProperty("VerPar" , m_verpar = std::string("default"));
58 declareProperty("BossRelease",m_bossRelease = std::string("default"));
59 declareProperty("ReadOneTime",m_readOneTime=false);
60 declareProperty("RunFrom", m_runFrom=8093);
61 declareProperty("RunTo",m_runTo=9025);
62 declareProperty("RunIdList",m_runIdList);
63}
64
67
68/*StatusCode VertexDbSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
69 if( IID_IVertexDbSvc.versionMatch(riid) ){
70 *ppvInterface = static_cast<IVertexDbSvc*> (this);
71 } else{
72 return Service::queryInterface(riid, ppvInterface);
73 }
74 return StatusCode::SUCCESS;
75}*/
76
78 MsgStream log(messageService(), name());
79 log << MSG::INFO << "VertexDbSvc::initialize()" << endreq;
80
81 StatusCode sc = Service::initialize();
82 if( sc.isFailure() ) return sc;
83
84
85 IIncidentSvc* incsvc;
86 sc = service("IncidentSvc", incsvc);
87 int priority = 100;
88 if( sc.isSuccess() ){
89 incsvc -> addListener(this, "NewRun", priority);
90 }
91
92 sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
93 if (sc .isFailure() ) {
94 log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
95 return sc;
96 }
97
98 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
99 if (sc .isFailure() ) {
100 log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
101 return sc;
102 }
103 /*if(m_readOneTime){
104 if(m_runFrom>=8093){
105 getReadBunchInfo(m_runFrom, m_runTo);
106 }
107 else
108 std::cout<<"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
109 }*/
110 for(unsigned int i = 0; i < m_runIdList.size(); i++) {
111 m_runFrom=m_runIdList[i];
112 i=i+2;
113 m_runTo=m_runIdList[i];
114 if(m_readOneTime){
115 if(m_runFrom>=8093){
116 getReadBunchInfo(m_runFrom, m_runTo);
117 }
118 else
119 std::cout<<"VertexDbSvc, invalid RunFrom, RunFrom should be >=8093"<<std::endl;
120 }
121 }
122
123 return StatusCode::SUCCESS;
124}
125
127 MsgStream log(messageService(), name());
128 log << MSG::INFO << "VertexDbSvc::finalize()" << endreq;
129 // if(m_connect_offline) delete m_connect_offline;
130 return StatusCode::SUCCESS;
131}
132
133void VertexDbSvc::handle(const Incident& inc){
134 MsgStream log( messageService(), name() );
135 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
136
137 if ( inc.type() == "NewRun" ){
138 log << MSG::DEBUG << "NewRun" << endreq;
139 if(!m_readOneTime) {
140 getVertexTableInfo();
141 } else {
142 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
143 int run = eventHeader->runNumber();
144 //cout << endl << "New Run: " << run << " VertexDB vertex= ";
145 if(run<0) run=-run;
146 if((m_mapPrimaryVertex[run]).size()>0) {
147 m_isRunNumberValid = true;
148 m_primaryVertex[0] = (m_mapPrimaryVertex[run])[0];
149 m_primaryVertex[1] = (m_mapPrimaryVertex[run])[1];
150 m_primaryVertex[2] = (m_mapPrimaryVertex[run])[2];
151 m_sigmaPrimaryVertex[0] = (m_mapPrimaryVertex[run])[3];
152 m_sigmaPrimaryVertex[1] = (m_mapPrimaryVertex[run])[4];
153 m_sigmaPrimaryVertex[2] = (m_mapPrimaryVertex[run])[5];
154 //cout << m_primaryVertex[0] << "," << m_primaryVertex[1] << "," << m_primaryVertex[2] << " sigma= " << m_sigmaPrimaryVertex[0] << "," << m_sigmaPrimaryVertex[1] << "," << m_sigmaPrimaryVertex[2] << endl;
155 } else {
156 std::cout<<"VertexDbSvc, could not get vertex infor in handle new run"<<std::endl;
157 }
158 }
159 }
160}
161
162#else
163// -------------------------- BEAN ------------------------------------
164
165VertexDbSvc* VertexDbSvc::m_vdb = 0;
166
167//-----------------------------------------------------------------------------
169//-----------------------------------------------------------------------------
170{
171 // use functions instead of "declareProperty"
172 dbName = "offlinedb";
173#ifdef ROOTEVENTDATAVERSION
174 m_bossver = ROOTEVENTDATAVERSION;
175 m_bossRelease = ROOTEVENTDATAVERSION;
176#else
177 m_bossver = "default";
178 m_bossRelease = "default";
179#endif
180 m_verpar = "default";
181
182 m_dbsvc = DatabaseSvc::instance();
183}
184
185//-----------------------------------------------------------------------------
186void VertexDbSvc::handle(int new_run)
187//-----------------------------------------------------------------------------
188{
189 static int save_run = 0;
190 if( new_run == save_run ) return;
191 getVertexTableInfo(new_run);
192 save_run = new_run;
193}
194#endif
195
197{
198 if( !m_isRunNumberValid ) {
199 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
200 memset(m_primaryVertex,0,sizeof(m_primaryVertex));
201 }
202 return m_primaryVertex;
203}
204
206{
207 if( !m_isRunNumberValid ) {
208 cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
209 memset(m_sigmaPrimaryVertex,0,sizeof(m_sigmaPrimaryVertex));
210 }
211 return m_sigmaPrimaryVertex;
212}
213
214#ifndef BEAN
215StatusCode VertexDbSvc::getVertexTableInfo(){
216 MsgStream log(messageService(), name());
217 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
218 int run = eventHeader->runNumber();
219#else
220//-----------------------------------------------------------------------------
221void VertexDbSvc::getVertexTableInfo(int run)
222//-----------------------------------------------------------------------------
223{
224#endif
225 m_isRunNumberValid = false;
226 int save_run = run;
227
228 if( run < 0 ) {
229#ifndef BEAN
230 log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
231#else
232 cout << "This data is the MC sample with the Run Number: " << run << endl;
233#endif
234 run = -run;
235 }
236//#ifndef BEAN
237// if(m_bossver=="default") m_bossver = getenv("BES_RELEASE");
238//#endif
239
240 bool ret_vtx = getReadBunchInfo(run);
241
242 if( !ret_vtx ) {
243 cout << " VertexDbSvc:: can not found vertex information for run:"
244 << run << ", boss version " << m_bossver << endl;
245 exit(1);
246 }
247
248/* if( !ret_vtx && save_run<0 ) {
249 bool ret = false;
250 int real_run = run;
251 for(int kk = 1; kk <= 10000; kk++) {
252 real_run = run+kk;
253 if( (ret = getReadBunchInfo(real_run)) ) break;
254
255 if( run-kk > 0 ) {
256 real_run = run-kk;
257 if( (ret = getReadBunchInfo(real_run)) ) break;
258 }
259 }
260 if( !ret ) {
261#ifndef BEAN
262 log << MSG::ERROR << "Can not find vertex information for run:" <<run<< endreq;
263#else
264 cout << "Can not find vertex information for run:" << run << endl;
265#endif
266 exit(1);
267 }
268#ifndef BEAN
269 log << MSG::INFO << "Use Bunch infor. of run " << real_run
270 << " instead of run " << run << endreq;
271#else
272 cout << "Use Bunch infor. of run " << real_run
273 << " instead of run " << run << endl;
274#endif
275 }
276*/
277 m_isRunNumberValid = true;
278#ifndef BEAN
279 log << MSG::INFO << "Successfully fetch the vertex information for run: "
280 << save_run << endreq;
281 return StatusCode::SUCCESS;
282#else
283 cout << "Successfully fetch the vertex information for run: "
284 << save_run << endl;
285#endif
286}
287
288//-----------------------------------------------------------------------------
289bool VertexDbSvc::getReadBunchInfo(int run)
290//-----------------------------------------------------------------------------
291{
292 if(m_bossver == "default") {
293 if(m_bossRelease == "default") {
294#ifndef BEAN
295 MsgStream log(messageService(), name());
296 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
297 << m_bossRelease << "." << endreq;
298#else
299 cout << "ERROR BossRelease must be set! Current value is "
300 << m_bossRelease << "." << endl;
301#endif
302 exit(1);
303 }
304 else {
305 char stmt1[400];
306 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);
307
308 DatabaseRecordVector records;
309 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
310 if(rowNo == 0) {
311#ifndef BEAN
312 MsgStream log(messageService(), name());
313 log << MSG::ERROR << "ERROR: can not find records for run = " << run
314 << " and BossRelease = " << m_bossRelease << endreq;
315#else
316 cout << "ERROR: can not find records for run = " << run
317 << " and BossRelease = " << m_bossRelease << endl;
318#endif
319 exit(1);
320 }
321 DatabaseRecord* recordst = records[0];
322 m_bossver = recordst->GetString("SftVer");
323 m_verpar = recordst->GetString("ParVer");
324 cout << "Using the SftVer and ParVer (" << m_bossver
325 << ", " << m_verpar << ") for run " << run << ". " << endl;
326 }
327 }
328
329 string stmt = "select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
330 stringstream tmp;
331 tmp << "from BeamPar where RunNo = " << run
332 << " and SftVer=\'" << m_bossver << "\'";
333 if( m_verpar == "default" ) {
334 tmp << " group by ParVer";
335 } else {
336 tmp << " and ParVer = " << m_verpar;
337 }
338 stmt += tmp.str();
339 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
340
342 int row_no = m_dbsvc->query(dbName,stmt,res);
343
344 if( row_no > 0 ) {
345 DatabaseRecord& dbrec = *res[row_no-1];
346 m_primaryVertex[0] = dbrec.GetDouble("Vx");
347 m_primaryVertex[1] = dbrec.GetDouble("Vy");
348 m_primaryVertex[2] = dbrec.GetDouble("Vz");
349 m_sigmaPrimaryVertex[0] = dbrec.GetDouble("SigmaVx");
350 m_sigmaPrimaryVertex[1] = dbrec.GetDouble("SigmaVy");
351 m_sigmaPrimaryVertex[2] = dbrec.GetDouble("SigmaVz");
352 return true;
353 }
354
355 return false;
356}
357
358bool VertexDbSvc::getReadBunchInfo(int runFrom, int runTo)
359//-----------------------------------------------------------------------------
360{
361 if(m_bossver == "default") {
362 if(m_bossRelease == "default") {
363#ifndef BEAN
364 MsgStream log(messageService(), name());
365 log << MSG::FATAL << "ERROR BossRelease must be set! Current value is "
366 << m_bossRelease << "." << endreq;
367#else
368 cout << "ERROR BossRelease must be set! Current value is "
369 << m_bossRelease << "." << endl;
370#endif
371 exit(1);
372 }
373 else {
374 char stmt1[400];
375 sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", m_bossRelease.c_str(), runFrom, runTo);
376
377 DatabaseRecordVector records;
378 int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
379 if(rowNo == 0) {
380#ifndef BEAN
381 MsgStream log(messageService(), name());
382 log << MSG::ERROR << "ERROR: can not find records for run = " << runFrom
383 << " and BossRelease = " << m_bossRelease << endreq;
384#else
385 cout << "ERROR: can not find records for run = " << runFrom
386 << " and BossRelease = " << m_bossRelease << endl;
387#endif
388 exit(1);
389 }
390 DatabaseRecord* recordst = records[0];
391 m_bossver = recordst->GetString("SftVer");
392 m_verpar = recordst->GetString("ParVer");
393 cout << "Using the SftVer and ParVer (" << m_bossver
394 << ", " << m_verpar << ") for run " << runFrom << ". " << endl;
395 }
396 }
397
398 string stmt = "select RunNo, Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
399 stringstream tmp;
400 tmp << "from BeamPar where RunNo >= " << runFrom <<" and RunNo <= "<< runTo
401 << " and SftVer=\'" << m_bossver << "\'";
402 if( m_verpar == "default" ) {
403 tmp << " group by ParVer";
404 } else {
405 tmp << " and ParVer = " << m_verpar;
406 }
407 stmt += tmp.str();
408 // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
409
411 int row_no = m_dbsvc->query(dbName,stmt,res);
412
413 std::cout<<"VertexDbSvc get all vertex info, row_no: "<<row_no<<std::endl;
414 if( row_no > 0 ) {
415 for(int i=0;i<row_no;i++){
416 DatabaseRecord& dbrec = *res[i];
417 int run = dbrec.GetInt("RunNo");
418 std::vector<double> vertex;
419 vertex.push_back(dbrec.GetDouble("Vx"));
420 vertex.push_back(dbrec.GetDouble("Vy"));
421 vertex.push_back(dbrec.GetDouble("Vz"));
422 vertex.push_back(dbrec.GetDouble("SigmaVx"));
423 vertex.push_back(dbrec.GetDouble("SigmaVy"));
424 vertex.push_back(dbrec.GetDouble("SigmaVz"));
425 m_mapPrimaryVertex[run]=vertex;
426 }
427 return true;
428 }
429
430 return false;
431}
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)
double GetDouble(std::string key)
int GetInt(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)
void handle(const Incident &)
virtual StatusCode initialize()
double * PrimaryVertex()
double * SigmaPrimaryVertex()
virtual StatusCode finalize()