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