BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MeasuredEcmsSvc.cxx
Go to the documentation of this file.
1/***********************************************************************
2 * BES III Software *
3 * Copyright(C) - BES III Collaboration *
4 * *
5 * Author: The BESIII Collaboration *
6 * Contributor: Lianjin Wu *
7 * *
8 ***********************************************************************/
9
10#include "MeasuredEcmsSvc.h"
11
12using namespace std;
13
15// Constructor
16MeasuredEcmsSvc::MeasuredEcmsSvc( const std::string& name, ISvcLocator* svcLoc )
17 : base_class( name, svcLoc ) {
18 // declare properties
19 declareProperty( "ReadOneTime", m_readOneTime = false );
20 declareProperty( "RunFrom", m_runFrom = 8093 );
21 declareProperty( "RunTo", m_runTo = 9025 );
22 m_runflag = -1;
23}
24
25// Destructor
27 MsgStream log( msgSvc(), name() );
28 log << MSG::INFO << "destructor" << endmsg;
29}
30
31// Initialize
33 MsgStream log( msgSvc(), name() );
34 log << MSG::INFO << "initialize begin here" << endmsg;
35
36 StatusCode status = Service::initialize();
37 if ( !status.isSuccess() )
38 {
39 log << MSG::ERROR << "can not initialize" << endmsg;
40 return status;
41 }
42 IIncidentSvc* incsvc;
43 status = service( "IncidentSvc", incsvc );
44 int priority = 100;
45 if ( status.isSuccess() ) incsvc->addListener( this, "NewRun", priority );
46
47 status = serviceLocator()->service( "DatabaseSvc", m_dbSvc, true );
48 if ( !status.isSuccess() )
49 {
50 log << MSG::ERROR << "unable to find DatabaseSvc" << endmsg;
51 return status;
52 }
53
54 status = serviceLocator()->service( "EventDataSvc", m_eventSvc, true );
55 if ( !status.isSuccess() )
56 {
57 log << MSG::ERROR << "unable to find EventDataSvc " << endmsg;
58 return status;
59 }
60 if ( m_readOneTime )
61 {
62 if ( m_runFrom >= 8093 ) { readDBTable( m_runFrom, m_runTo ); }
63 else
64 std::cout << "MeasuredEcmsSvc, invalid RunFrom, RunFrom should be >=8093" << std::endl;
65 }
66 m_sele = 0;
67 log << MSG::INFO << "initialized successfully" << endmsg;
68 return StatusCode::SUCCESS;
69}
70
71// Finalize
73 MsgStream log( msgSvc(), name() );
74 log << MSG::INFO << "finalize begin here" << endmsg;
75
76 StatusCode status = Service::finalize();
77 if ( !status.isSuccess() )
78 {
79 log << MSG::ERROR << "can not finalize" << endmsg;
80 return status;
81 }
82
83 log << MSG::INFO << "finalized successfully" << endmsg;
84 return StatusCode::SUCCESS;
85}
86
87// Query interface
88/*StatusCode MeasuredEcmsSvc::queryInterface(const InterfaceID &riid, void **ppvIF)
89{
90 if (IMeasuredEcmsSvc::interfaceID().versionMatch(riid)) *ppvIF =
91dynamic_cast<IMeasuredEcmsSvc*>(this); else return Service::queryInterface(riid, ppvIF);
92
93 addRef();
94
95 return StatusCode::SUCCESS;
96}*/
97
98// Handle
99void MeasuredEcmsSvc::handle( const Incident& inc ) {
100 MsgStream log( msgSvc(), name() );
101 log << MSG::DEBUG << "handle: " << inc.type() << endmsg;
102
103 SmartDataPtr<Event::EventHeader> eventHeader( m_eventSvc, "/Event/EventHeader" );
104 int runNo = eventHeader->runNumber();
105 m_runNo = std::abs( runNo );
106 if ( inc.type() == "NewRun" )
107 {
108 log << MSG::INFO << "RunNo is new, call the readMeasuredEcms again" << endmsg;
109 if ( !m_readOneTime )
110 {
111 m_sele = 0;
113 }
114 else
115 {
116 SmartDataPtr<Event::EventHeader> eventHeader( m_eventSvc, "/Event/EventHeader" );
117 int run = eventHeader->runNumber();
118 if ( run < 0 ) run = -run;
119 if ( m_mapBeamE[run] >= 0 )
120 {
121 m_isRunNoValid = true;
122 m_sele = m_mapBeamE[run];
123 }
124 else
125 {
126 std::cout << "MeasuredEcmsSvc, could not get Ecm infor in handle new run" << std::endl;
127 }
128 }
129 }
130}
131
132// Return Ecms ( Ecms = beam_energy * 2 )
134 switch ( m_tableOption )
135 {
136 case 1:
137 return ( m_rowNo1 != 0 ) ? (double)m_dbRecordVector[m_sele]->GetDouble( "Ecms" ) : 0.00;
138 break;
139
140 case 2:
141 return ( m_rowNo2 != 0 )
142 ? (double)m_dbRecordVector[m_sele]->GetDouble( "beam_energy" ) * 2.00
143 : 0.00;
144 break;
145
146 default:
147 if ( m_rowNo1 != 0 ) return (double)m_dbRecordVector[m_sele]->GetDouble( "Ecms" );
148 else if ( m_rowNo2 != 0 )
149 return (double)m_dbRecordVector[m_sele]->GetDouble( "beam_energy" ) * 2.00;
150 else return 0.00;
151 break;
152 }
153}
154
155// Return Ecms depend on runNo only.
157 m_runNo = std::abs( runNo );
158
160
161 switch ( m_tableOption )
162 {
163 case 1:
164 return ( m_rowNo1 != 0 ) ? (double)m_dbRecordVector[m_sele]->GetDouble( "Ecms" ) : 0.00;
165 break;
166
167 case 2:
168 return ( m_rowNo2 != 0 )
169 ? (double)m_dbRecordVector[m_sele]->GetDouble( "beam_energy" ) * 2.00
170 : 0.00;
171 break;
172
173 default:
174 if ( m_rowNo1 != 0 ) return (double)m_dbRecordVector[m_sele]->GetDouble( "Ecms" );
175 else if ( m_rowNo2 != 0 )
176 return (double)m_dbRecordVector[m_sele]->GetDouble( "beam_energy" ) * 2.00;
177 else return 0.00;
178 break;
179 }
180}
181
182// read database table
184 if ( runNo == m_runflag ) return;
185 m_runflag = runNo;
186 MsgStream log( msgSvc(), name() );
187 log << MSG::INFO << "begin read database MeasuredEcmsSvc" << endmsg;
188
189 m_dbRecordVector.clear();
190
191 m_rowNo1 = 0;
192 m_rowNo2 = 0;
193
194 m_isRunNoValid = false;
195 int runNum = std::abs( runNo );
196
197 if ( runNo < 0 ) log << MSG::INFO << "MeasuredEcmsSvc for MC events" << endmsg;
198 else log << MSG::INFO << "MeasuredEcmsSvc for DT events" << endmsg;
199
200 triggerDBTable( runNum );
201
202 char stmt[400];
203
204 char stmt1[400];
205 char stmt2[400];
206
207 switch ( m_tableOption )
208 {
209 case 1:
210 snprintf( stmt, 1024,
211 "select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from "
212 "MeasuredEcms2 where RunFrom <= %d and RunTo >= %d",
213 runNum, runNum );
214 break;
215
216 case 2:
217 snprintf(
218 stmt, 1024,
219 "select beam_energy, px, py, pz, beam_energy_err from RunParams where run_number = %d",
220 runNum );
221 break;
222
223 default:
224 snprintf( stmt1, 1024,
225 "select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from "
226 "MeasuredEcms2 where RunFrom <= %d and RunTo >= %d",
227 runNum, runNum );
228 snprintf(
229 stmt2, 1024,
230 "select beam_energy, px, py, pz, beam_energy_err from RunParams where run_number = %d",
231 runNum );
232 break;
233 }
234
235 if ( m_tableOption == 1 || m_tableOption == 2 )
236 {
237 int rowNo = m_dbSvc->query( "offlinedb", stmt, m_dbRecordVector );
238
239 if ( rowNo == 0 )
240 { log << MSG::ERROR << "RowNo can not be 0, failed to read DB for " << runNo << endmsg; }
241 else
242 {
243 if ( m_tableOption == 1 ) m_rowNo1 = 1;
244 if ( m_tableOption == 2 ) m_rowNo2 = 1;
245 log << MSG::INFO << "initialize the DatabaseRecordVector OK for " << runNo << endmsg;
246 m_isRunNoValid = true;
247 }
248 }
249 else
250 {
251 m_rowNo1 = m_dbSvc->query( "offlinedb", stmt1, m_dbRecordVector );
252 if ( m_rowNo1 == 0 )
253 {
254 m_dbRecordVector.clear();
255 m_rowNo2 = m_dbSvc->query( "offlinedb", stmt2, m_dbRecordVector );
256 }
257
258 if ( m_rowNo1 == 0 && m_rowNo2 == 0 )
259 { log << MSG::ERROR << "RowNo can not be 0, failed to read DB for " << runNo << endmsg; }
260 else
261 {
262 log << MSG::INFO << "initialize the DatabaseRecordVector OK for " << runNo << endmsg;
263 m_isRunNoValid = true;
264 }
265 }
266}
267
268// read database table
269void MeasuredEcmsSvc::readDBTable( int runFrom, int runTo ) {
270 MsgStream log( msgSvc(), name() );
271 log << MSG::INFO << "begin read database MeasuredEcmsSvc from " << m_runFrom << " to "
272 << m_runTo << endmsg;
273 m_dbRecordVector.clear();
274
275 m_rowNo1 = 0;
276 m_rowNo2 = 0;
277 m_isRunNoValid = false;
278 char stmt[400];
279
280 if ( ( runFrom >= 23463 && runFrom <= 38140 ) || ( runFrom >= 47543 && runFrom <= 51498 ) )
281 {
282 if ( ( runTo >= 23463 && runTo <= 38140 ) || ( runTo >= 47543 && runTo <= 51498 ) )
283 {
284 log << MSG::INFO << "read table MeasuredEcms2 for " << runFrom << ":" << runTo << endmsg;
285 snprintf( stmt, 1024,
286 "select sample, RunFrom, RunTo, Ecms, Ecms_err, Ave_Ecms, Ave_Ecms_err from "
287 "MeasuredEcms2 where RunFrom >= %d and RunTo <= %d",
288 runFrom, runTo );
289 m_tableOption = 1;
290 }
291 }
292 else if ( ( runFrom >= 11414 && runFrom <= 23454 ) ||
293 ( runFrom >= 43716 && runFrom <= 47066 ) ||
294 ( runFrom >= 70522 && runFrom <= 73929 ) ||
295 ( runFrom >= 74031 && runFrom <= 78536 ) ||
296 ( runFrom >= 78615 && runFrom <= 81631 ) )
297 {
298 if ( ( runTo >= 11414 && runTo <= 23454 ) || ( runTo >= 43716 && runTo <= 47066 ) ||
299 ( runFrom >= 70522 && runFrom <= 73929 ) ||
300 ( runFrom >= 74031 && runFrom <= 78536 ) || ( runFrom >= 78615 && runFrom <= 81631 ) )
301 {
302 log << MSG::INFO << "read table RunParams for " << runFrom << ":" << runTo << endmsg;
303 snprintf( stmt, 1024,
304 "select run_number, beam_energy, px, py, pz, beam_energy_err from RunParams "
305 "where run_number >= %d and run_number <= %d",
306 runFrom, runTo );
307 m_tableOption = 2;
308 }
309 }
310 else { m_tableOption = -1; }
311
312 if ( m_tableOption == 1 || m_tableOption == 2 )
313 {
314 int rowNo = m_dbSvc->query( "offlinedb", stmt, m_dbRecordVector );
315 if ( rowNo == 0 )
316 {
317 log << MSG::ERROR << "RowNo can not be 0, failed to read DB for " << runFrom << ":"
318 << runTo << endmsg;
319 cout << "RowNo can not be 0, failed to read DB for " << runFrom << ":" << runTo << endl;
320 }
321 else
322 {
323 if ( m_tableOption == 1 ) m_rowNo1 = 1;
324 if ( m_tableOption == 2 ) m_rowNo2 = 1;
325 log << MSG::INFO << "initialize the DatabaseRecordVector OK for " << runFrom << ":"
326 << runTo << endmsg;
327 cout << "MeasuredEcmsSvc: initialize the DatabaseRecordVector OK for " << runFrom << ":"
328 << runTo << ", Nrecord = " << rowNo << "." << endl;
329 m_isRunNoValid = true;
330 }
331 if ( m_rowNo2 == 1 )
332 {
333 for ( int i = 0; i < rowNo; i++ )
334 {
335 int runnumber = m_dbRecordVector[i]->GetInt( "run_number" );
336 m_mapBeamE[runnumber] = i;
337 }
338 }
339 if ( m_rowNo1 == 1 )
340 {
341 for ( int i = 0; i < rowNo; i++ )
342 {
343 int run0 = m_dbRecordVector[i]->GetInt( "RunFrom" );
344 int run1 = m_dbRecordVector[i]->GetInt( "RunTo" );
345 for ( int j = run0; j <= run1; j++ ) { m_mapBeamE[j] = i; }
346 }
347 }
348 }
349}
350
351// triggerDBTable based on runNo
353 MsgStream log( msgSvc(), name() );
354 log << MSG::INFO << "triggerDBTable based on runNo" << endl;
355
356 int runNum = std::abs( runNo );
357
358 if ( ( runNum >= 23463 && runNum <= 38140 ) || ( runNum >= 47543 && runNum <= 51498 ) )
359 {
360 log << MSG::INFO << "read table MeasuredEcms2 for " << runNo << endmsg;
361 m_tableOption = 1;
362 }
363
364 else if ( ( runNum >= 11414 && runNum <= 23454 ) || ( runNum >= 43716 && runNum <= 47066 ) ||
365 ( runNum >= 70522 && runNum <= 73929 ) || ( runNum >= 74031 && runNum <= 78536 ) ||
366 ( runNum >= 78615 && runNum <= 81631 ) )
367 {
368 log << MSG::INFO << "read table RunParams for " << runNo << endmsg;
369 m_tableOption = 2;
370 }
371
372 else m_tableOption = -1;
373
374 return m_tableOption;
375}
376
377// Return the string depend on the parameters
378std::string MeasuredEcmsSvc::getRecord( int runNo, char* tab, char* col, char* min_col,
379 char* max_col ) {
380 std::string strRecord;
381 m_isRunNoValid = false;
382
383 MsgStream log( msgSvc(), name() );
384 log << MSG::INFO << "MeasuredEcmsSvc begin read database " << tab << "-" << col << endmsg;
385
386 int runNum = std::abs( runNo );
387
388 if ( runNo < 0 ) log << MSG::INFO << "MeasuredEcmsSvc for MC events" << endmsg;
389 else log << MSG::INFO << "MeasuredEcmsSvc for DT events" << endmsg;
390
391 char stmt[500];
392 snprintf( stmt, 1024, "select %s from %s where %s <= %d and %s >= %d", col, tab, min_col,
393 runNum, max_col, runNum );
394
396 int row_no = m_dbSvc->query( "offlinedb", stmt, res );
397
398 if ( row_no == 0 )
399 {
400 log << MSG::ERROR << "RowNo can not be 0 for " << tab << ", failed to read DB for "
401 << runNo << endmsg;
402 }
403 else
404 {
405 log << MSG::INFO << "MeasuredEcmsSvc first read the runNo, read the " << tab
406 << " database " << runNo << endmsg;
407 strRecord = res[m_sele]->GetString( col );
408 m_isRunNoValid = true;
409 }
410
411 return strRecord;
412}
DECLARE_COMPONENT(BesBdkRc)
int runNo
Definition DQA_TO_DB.cxx:13
IMessageSvc * msgSvc()
std::map< int, int > m_mapBeamE
virtual ~MeasuredEcmsSvc()
void handle(const Incident &)
int triggerDBTable(int runNo)
virtual StatusCode initialize()
MeasuredEcmsSvc(const std::string &name, ISvcLocator *svcLoc)
virtual StatusCode finalize()
std::string getRecord(int runNo, char *tab, char *col, char *min_col, char *max_col)
void readDBTable(int runNo)