BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
RealizationSvc.cxx
Go to the documentation of this file.
1
2#include "CLHEP/Random/RandGauss.h"
3#include "GaudiKernel/DataSvc.h"
4#include "GaudiKernel/IDataProviderSvc.h"
5#include "GaudiKernel/IIncidentListener.h"
6#include "GaudiKernel/IIncidentSvc.h"
7#include "GaudiKernel/IInterface.h"
8#include "GaudiKernel/Incident.h"
9#include "GaudiKernel/Kernel.h"
10#include "GaudiKernel/MsgStream.h"
11#include "GaudiKernel/SmartDataPtr.h"
12#include "GaudiKernel/StatusCode.h"
13#include <stdint.h>
14#include <vector>
15
16#include "DetVerSvc/IDetVerSvc.h"
17#include "EventModel/EventHeader.h"
18#include "EventModel/EventModel.h"
19
20#include "RealizationSvc.h"
21
22using namespace std;
23
25RealizationSvc::RealizationSvc( const string& name, ISvcLocator* svcloc )
26 : base_class( name, svcloc ) {
27 m_CaliConst = 100;
28 declareProperty( "RunIdList", m_runNoList );
29 declareProperty( "IfUseDB", m_useDB = true );
30 declareProperty( "IfReadBunch", m_ifReadBunch = true );
31 declareProperty( "IfUseTrg", m_ifReadTrg = false );
32 declareProperty( "IfReadRanTrg", m_ifReadRandTrg = false );
33 declareProperty( "IfReadTFEE", m_ifReadTFEE = false );
34 declareProperty( "IfReadRunInfo", m_ifReadRunInfo = false );
35 declareProperty( "BossRelease", m_bossRelease = "default" );
36 declareProperty( "SftVer", m_SftVer = "default" );
37 declareProperty( "ParVer", m_ParVer = "default" );
38 declareProperty( "InitEvtID", m_initEvtID = 0 );
39}
40
42 MsgStream log( msgSvc(), name() );
43 log << MSG::INFO << "RealizationSvc::initialize()" << endmsg;
44
45 StatusCode sc = Service::initialize();
46 if ( sc.isFailure() ) return sc;
47
48 if ( m_SftVer == "NONE" ) m_SftVer = getenv( "BES_RELEASE" );
49
50 if ( m_runNoList.size() == 0 )
51 {
52 log << MSG::ERROR << "Please set the run id list" << endmsg;
53 return StatusCode::FAILURE;
54 }
55
56 for ( unsigned int i = 0; i < m_runNoList.size(); i++ )
57 {
58 if ( m_runNoList[i] != 0 )
59 {
60 std::cout << "run id : " << m_runNoList[i] << std::endl;
61 if ( find( m_runIdList.begin(), m_runIdList.end(), m_runNoList[i] ) ==
62 m_runIdList.end() )
63 { m_runIdList.push_back( m_runNoList[i] ); }
64 else
65 {
66 log << MSG::ALWAYS << "RunNo " << m_runNoList[i] << " repeated in run list" << endmsg;
67 }
68 }
69 else
70 {
71 if ( i == 0 || ( i + 1 ) >= m_runNoList.size() )
72 log << MSG::ERROR << "Please check your run id list, it is wrong" << endmsg;
73 if ( m_runNoList[i + 1] < m_runNoList[i - 1] )
74 {
75 for ( int j = m_runNoList[i + 1] + 1; j < m_runNoList[i - 1]; j++ )
76 {
77 if ( find( m_runIdList.begin(), m_runIdList.end(), j ) == m_runIdList.end() )
78 m_runIdList.push_back( j );
79 else log << MSG::ALWAYS << "RunNo " << j << " repeated in run list" << endmsg;
80 }
81 }
82 else
83 {
84 for ( int j = m_runNoList[i - 1] + 1; j < m_runNoList[i + 1]; j++ )
85 {
86 if ( find( m_runIdList.begin(), m_runIdList.end(), j ) == m_runIdList.end() )
87 m_runIdList.push_back( j );
88 else log << MSG::ALWAYS << "RunNo " << j << " repeated in run list" << endmsg;
89 }
90 }
91 }
92 }
93
94 m_runID = m_runIdList[0];
95
96 int runMin = m_runID, runMax = m_runID;
97 for ( unsigned int i = 1; i < m_runIdList.size(); ++i )
98 {
99 int _run = m_runIdList[i];
100 if ( _run < runMin ) { runMin = _run; }
101 else if ( _run > runMax ) { runMax = _run; }
102 }
103 IDetVerSvc* idvSvc;
104 sc = service( "DetVerSvc", idvSvc );
105 if ( idvSvc->fromRun( abs( runMin ) ) != idvSvc->fromRun( abs( runMax ) ) )
106 {
107 log << MSG::ERROR << "wrong run range [" << runMin << " to " << runMax
108 << "], crossed detector configuration" << endmsg;
109 return StatusCode::FAILURE;
110 }
111
112 m_connect = new RealDBUtil::ConnectionProvider();
113 if ( !m_connect ) { log << MSG::ERROR << "Could not open connection to database" << endmsg; }
114
115 IIncidentSvc* incsvc;
116 sc = service( "IncidentSvc", incsvc );
117 int priority = 100;
118 if ( sc.isSuccess() ) { incsvc->addListener( this, "NewRun", priority ); }
119
120 sc = serviceLocator()->service( "EventDataSvc", m_eventSvc, true );
121 if ( sc.isFailure() )
122 {
123 log << MSG::ERROR << "Unable to find EventDataSvc " << endmsg;
124 return sc;
125 }
126
127 return StatusCode::SUCCESS;
128}
129
131 MsgStream log( msgSvc(), name() );
132 log << MSG::INFO << "RealizationSvc::finalize()" << endmsg;
133
134 return StatusCode::SUCCESS;
135}
136
137/*StatusCode RealizationSvc::queryInterface(const InterfaceID& riid, void** ppvInterface)
138{
139 if ( IRealizationSvc::interfaceID().versionMatch(riid) ) {
140 *ppvInterface = (IRealizationSvc*)this;
141 }else{
142 return Service::queryInterface(riid, ppvInterface);
143 }
144 addRef();
145 return StatusCode::SUCCESS;
146}*/
147
148void RealizationSvc::handle( const Incident& inc ) {
149 MsgStream log( msgSvc(), name() );
150 log << MSG::DEBUG << "handle: " << inc.type() << endmsg;
151 if ( inc.type() == "NewRun" )
152 {
153 log << MSG::DEBUG << "Begin New Run" << endmsg;
154 if ( m_useDB == true )
155 {
156 int runNo = 0;
157
158 SmartDataPtr<Event::EventHeader> evt( m_eventSvc, "/Event/EventHeader" );
159 if ( evt )
160 {
161 runNo = evt->runNumber();
162 log << MSG::INFO << "The runNumber of current event is " << runNo << endmsg;
163 }
164 else { log << MSG::ERROR << "Can not get EventHeader!" << endmsg; }
165 readDB( std::abs( runNo ) );
166 }
167 }
168}
169
172
173 MsgStream log( msgSvc(), name() );
174 std::string sLum;
175 ConnectionProvider::eRet e =
176 m_connect->getReadLumInfo( sLum, runNo, m_SftVer, m_ParVer, m_bossRelease );
177 if ( e != 0 )
178 {
179 log << MSG::ERROR << "Could not find Luminosity infor., exit." << endmsg;
180 exit( 1 );
181 }
182 m_lumi = std::atof( sLum.c_str() );
183 log << MSG::INFO << "Luminosity is " << m_lumi << " in Run " << runNo << endmsg;
184 return m_lumi;
185}
186
187std::vector<std::string> RealizationSvc::getBgFileName( std::string query ) {
188 MsgStream log( msgSvc(), name() );
190
191 std::vector<std::string> Bgfilename;
192 Bgfilename.clear();
193
194 std::vector<std::string> fileInfor;
195 ConnectionProvider::eRet e = m_connect->getReadBackgroundInfo( fileInfor, query );
196 if ( e != 0 )
197 {
198 log << MSG::ERROR << "Could not find background infor., exit." << endmsg;
199 exit( 1 );
200 }
201
202 for ( unsigned int i = 0; i < fileInfor.size(); i += 2 )
203 { Bgfilename.push_back( fileInfor[i] + "/" + fileInfor[i + 1] ); }
204 fileInfor.clear();
205
206 for ( unsigned int i = 0; i < Bgfilename.size(); i++ )
207 { log << MSG::INFO << "Background file name: " << Bgfilename[i] << endmsg; }
208
209 return Bgfilename;
210}
211/*
212std::vector<int> RealizationSvc::getRunEvtNum(std::vector<int> inrunList, int EvtMax) {
213 std::vector<int> outrunList;
214 std::vector<int> vRunEvtNum;
215 outrunList.clear();
216 vRunEvtNum.clear();
217
218 for(int i = 0; i < inrunList.size(); i++) {
219 if(inrunList[i] != 0) {
220 std::cout <<"run id : " << inrunList[i] << std::endl;
221 if(find(outrunList.begin(),outrunList.end(),inrunList[i]) == outrunList.end())
222outrunList.push_back(inrunList[i]); else std::cout << "RunNo "<<inrunList[i]<<" repeated in run
223list"<< std::endl;
224 }
225 else {
226 if(i == 0 || (i+1) >= inrunList.size()) std::cerr << "Please check your run id list, it
227is wrong" << std::endl; if(inrunList[i+1] < inrunList[i-1]) { for(int j = inrunList[i+1]+1; j <
228inrunList[i-1]; j++) { if(find(outrunList.begin(),outrunList.end(),j) == outrunList.end())
229outrunList.push_back(j); else std::cout <<"RunNo "<<j<<" repeated in run list"<< std::endl;
230 }
231 }
232 else {
233 for(int j = inrunList[i-1]+1; j < inrunList[i+1]; j++) {
234 if(find(outrunList.begin(),outrunList.end(),j) == outrunList.end())
235outrunList.push_back(j); else std::cout <<"RunNo "<<j<<" repeated in run list"<< std::endl;
236 }
237 }
238 }
239 }
240
241 std::vector<float> lumi;
242 lumi.clear();
243 float totLumi = 0;
244 for(unsigned int i = 0; i < outrunList.size(); i++)
245 {
246 float lumi_value = getLuminosity(outrunList[i]);
247 lumi.push_back(lumi_value);
248 totLumi += lumi_value;
249 }
250
251 int totSimEvt = 0;
252 int evtSubNo = 0;
253 if(totLumi != 0) {
254 for(unsigned int i = 0; i < (lumi.size() - 1); i++) {
255 //if(i == 0) m_evtNoList.push_back(0);
256 double ratio = lumi[i]/totLumi*EvtMax;
257 evtSubNo = int (ratio);
258 if((ratio-evtSubNo) >= 0.5) evtSubNo = evtSubNo + 1;
259 totSimEvt += evtSubNo;
260 if(evtSubNo == 0) {
261 std::cout << "The run " <<outrunList[i]<<" is not simulated, due to the luminosity is
262too small!" << std::endl;
263 }
264 else {
265 vRunEvtNum.push_back(outrunList[i]);
266 vRunEvtNum.push_back(evtSubNo);
267 }
268 std::cout <<"Total "<< evtSubNo <<" events need to be simulated in run "
269<<outrunList[i]<< std::endl;
270 }
271 vRunEvtNum.push_back(outrunList[lumi.size() - 1]); //set the last run id
272 vRunEvtNum.push_back(EvtMax - totSimEvt);
273 std::cout <<"Total "<< EvtMax - totSimEvt <<" events need to be simulated in run "
274<<outrunList[lumi.size() - 1]<< std::endl;
275 }
276 else {
277 std::cerr << "ERORR: " << "Total luminosity is ZERO!!! Please check your run list. " <<
278std::endl; std::abort();
279 }
280
281 return vRunEvtNum;
282}
283*/
285 MsgStream log( msgSvc(), name() );
286 log << MSG::DEBUG << "In getCaliConst" << endmsg;
287
289
290 StatusCode sc;
291
292 // Get bunch infor
293 if ( m_ifReadBunch == true )
294 {
295 std::vector<std::string> sbunch;
296 std::vector<double> bunchInfo;
297 ConnectionProvider::eRet e =
298 m_connect->getReadBunchInfo( sbunch, runNo, m_SftVer, m_ParVer, m_bossRelease );
299 if ( e == 7 )
300 {
301 bool last = true;
302 for ( int id = 1; true; id++ )
303 {
304 e = m_connect->getReadBunchInfo( sbunch, runNo + id, m_SftVer, m_ParVer,
305 m_bossRelease );
306 last = true;
307 if ( e == 7 )
308 {
309 if ( ( runNo - id ) > 0 )
310 {
311 e = m_connect->getReadBunchInfo( sbunch, runNo - id, m_SftVer, m_ParVer,
312 m_bossRelease );
313 last = false;
314 }
315 }
316 if ( e == 0 )
317 {
318 if ( last == true )
319 log << MSG::INFO << "Use Bunch infor. of run " << runNo + id << " instead of run "
320 << runNo << endmsg;
321 if ( last == false )
322 log << MSG::INFO << "Use Bunch infor. of run " << runNo - id << " instead of run "
323 << runNo << endmsg;
324 break;
325 }
326 }
327 }
328 if ( e != 0 && e != 7 )
329 {
330 log << MSG::ERROR << "Could not find Bunch infor., exit." << endmsg;
331 exit( 1 );
332 }
333 for ( unsigned int i = 0; i < sbunch.size(); i++ )
334 { bunchInfo.push_back( atof( sbunch[i].c_str() ) ); }
335 sbunch.clear();
336
337 if ( bunchInfo.size() == 6 )
338 {
339 // convert from cm to mm
340 m_bunch_x = 10 * bunchInfo[0];
341 m_bunch_y = 10 * bunchInfo[1];
342 m_bunch_z = 10 * bunchInfo[2];
343 m_sigma_x = 10 * bunchInfo[3];
344 m_sigma_y = 10 * bunchInfo[4];
345 m_sigma_z = 10 * bunchInfo[5];
346 log << MSG::INFO << "BunchPosX: " << m_bunch_x << " BunchPosY: " << m_bunch_y
347 << " BunchPosZ: " << m_bunch_z << " in Run " << runNo << endmsg;
348 log << MSG::INFO << "BunchSigmaX: " << m_sigma_x << " BunchSigmaY: " << m_sigma_y
349 << " BunchSigmaZ: " << m_sigma_z << " in Run " << runNo << endmsg;
350 }
351 else
352 {
353 log << MSG::ERROR << "Please check the bunch information, the size is "
354 << bunchInfo.size() << endmsg;
355 }
356 bunchInfo.clear();
357 }
358
359 // get trigger table
360 if ( m_ifReadTrg == true )
361 {
362 m_trgTable.clear();
363 std::vector<std::string> strgTable;
364 ConnectionProvider::eRet e = m_connect->getReadTrgTableInfo( strgTable, runNo );
365 if ( e != 0 )
366 {
367 log << MSG::ERROR << "Could not find TrgTable infor., exit." << endmsg;
368 exit( 1 );
369 }
370 for ( unsigned int i = 0; i < strgTable.size(); i++ )
371 {
372 long long value = 0;
373 for ( unsigned int j = 0; j < ( strgTable[i] ).length(); j++ )
374 { value = value * 10 + ( strgTable[i][j] - '0' ); }
375 m_trgTable.push_back( uint32_t( value ) );
376 }
377 strgTable.clear();
378 if ( m_trgTable.size() != 65 )
379 log << MSG::ERROR << "Please check the TrgTable information, the size is "
380 << m_trgTable.size() << endmsg;
381
382 std::vector<double> vtrgGain;
383 e = m_connect->getEmcGain( vtrgGain, runNo );
384 if ( e != 0 )
385 {
386 log << MSG::ERROR << "Could not find emc gain infor., exit." << endmsg;
387 exit( 1 );
388 }
389 for ( unsigned int i = 0; i < vtrgGain.size(); i++ ) { m_trgGain[i] = vtrgGain[i]; }
390
391 // get trigger config information
392 e = m_connect->getTrgConfigInfo( runNo );
393 if ( e != 0 )
394 {
395 log << MSG::ERROR << "Could not find Trigger config infor., exit." << endmsg;
396 exit( 1 );
397 }
398 m_EtotDataSteps = m_connect->getEtotDataSteps();
399 m_VthBEtotH = m_connect->getVthBEtotH();
400 m_VthEEtotH = m_connect->getVthEEtotH();
401 m_VthEtotL = m_connect->getVthEtotL();
402 m_VthEtotM = m_connect->getVthEtotM();
403 m_VthBLZ = m_connect->getVthBLZ();
404 m_VthDiffB = m_connect->getVthDiffB();
405 m_VthDiffE = m_connect->getVthDiffE();
406 m_VthBalBLK = m_connect->getVthBalBLK();
407 m_VthBalEEMC = m_connect->getVthBalEEMC();
408 m_VthDiffMin = m_connect->getVthDiffMin();
409 }
410
411 // get background file
412 if ( m_ifReadRandTrg == true )
413 {
414 std::vector<std::string> fileInfor;
415 ConnectionProvider::eRet e = m_connect->getReadBackgroundInfo( fileInfor, runNo );
416 if ( e == 7 )
417 {
418 bool last = true;
419 for ( int id = 1; true; id++ )
420 {
421 e = m_connect->getReadBackgroundInfo( fileInfor, runNo + id );
422 last = true;
423 if ( e == 7 )
424 {
425 if ( ( runNo - id ) > 0 )
426 {
427 e = m_connect->getReadBackgroundInfo( fileInfor, runNo - id );
428 last = false;
429 }
430 }
431 if ( e == 0 )
432 {
433 if ( last == true )
434 log << MSG::INFO << "Use Bg files of run " << runNo + id << " instead of run "
435 << runNo << endmsg;
436 if ( last == false )
437 log << MSG::INFO << "Use Bg files of run " << runNo - id << " instead of run "
438 << runNo << endmsg;
439 break;
440 }
441 }
442 }
443 if ( e != 0 && e != 7 )
444 {
445 log << MSG::ERROR << "Could not find background infor., exit." << endmsg;
446 exit( 1 );
447 }
448 m_bgfilename.clear();
449 for ( unsigned int i = 0; i < fileInfor.size(); i += 2 )
450 { m_bgfilename.push_back( fileInfor[i] + "/" + fileInfor[i + 1] ); }
451 fileInfor.clear();
452 for ( unsigned int i = 0; i < m_bgfilename.size(); i++ )
453 {
454 log << MSG::INFO << "Background file name: " << m_bgfilename[i] << " in run " << runNo
455 << endmsg;
456 }
457
458 // get luminosity curve parameters
459 std::string srunTime;
460 std::string stau_value;
461 e = m_connect->getLumCurvePar( srunTime, stau_value, runNo, m_SftVer, m_ParVer,
462 m_bossRelease );
463 if ( e != 0 )
464 {
465 log << MSG::ERROR << "Could not find Luminosity curve parameters, exit." << endmsg;
466 exit( 1 );
467 }
468 m_runTotalTime = std::atof( srunTime.c_str() );
469 m_tauValue = std::atof( stau_value.c_str() );
470 log << MSG::INFO << "Total time is " << m_runTotalTime << ", tau is " << m_tauValue
471 << " in run " << runNo << ". " << endmsg;
472 }
473
474 // get tof threshold in table TFEE
475 if ( m_ifReadTFEE == true )
476 {
477 m_tfee.clear();
478 ConnectionProvider::eRet e = m_connect->getReadTofThreshInfo( m_tfee, runNo );
479 if ( e != 0 )
480 {
481 log << MSG::ERROR << "Could not find TFEE infor., exit." << endmsg;
482 exit( 1 );
483 }
484
485 for ( unsigned int i = 0; i < m_tfee.size(); i++ )
486 { log << MSG::INFO << "TFEE ----> " << m_tfee[i] << endmsg; }
487 }
488
489 // get run infor.
490 if ( m_ifReadRunInfo == true )
491 {
492 m_runInfo.clear();
493 std::vector<std::string> srunInfo;
494 ConnectionProvider::eRet e = m_connect->getRunInfo( srunInfo, runNo );
495 if ( e != 0 )
496 {
497 log << MSG::ERROR << "Could not find run infor., exit." << endmsg;
498 exit( 1 );
499 }
500 for ( unsigned int i = 0; i < srunInfo.size(); i++ )
501 { m_runInfo.push_back( atof( srunInfo[i].c_str() ) ); }
502 srunInfo.clear();
503
504 for ( unsigned int i = 0; i < m_runInfo.size(); i++ )
505 { log << MSG::INFO << "runInfo ----> " << m_runInfo[i] << endmsg; }
506 }
507}
508
DECLARE_COMPONENT(BesBdkRc)
int runNo
Definition DQA_TO_DB.cxx:13
IMessageSvc * msgSvc()
virtual int fromRun(unsigned int run)=0
RealizationSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode finalize()
double getLuminosity()
virtual StatusCode initialize()
std::vector< std::string > getBgFileName()
void handle(const Incident &)
void readDB(int runNo)