BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
MdcTunningSvc.cc
Go to the documentation of this file.
1#include <fstream>
2#include <iomanip>
3#include <iostream>
4#include <math.h>
5
6#include "GaudiKernel/Bootstrap.h"
7#include "GaudiKernel/IIncidentListener.h"
8#include "GaudiKernel/IIncidentSvc.h"
9#include "GaudiKernel/IInterface.h"
10#include "GaudiKernel/ISvcLocator.h"
11#include "GaudiKernel/Incident.h"
12#include "GaudiKernel/Kernel.h"
13#include "GaudiKernel/MsgStream.h"
14#include "GaudiKernel/SmartDataPtr.h"
15#include "GaudiKernel/StatusCode.h"
16
17#include "DatabaseSvc/IDatabaseSvc.h"
18#include "EventModel/Event.h"
19#include "EventModel/EventHeader.h"
20#include "EventModel/EventModel.h"
21
22#include "MdcTunningSvc.h"
23
24using namespace std;
25
27
28MdcTunningSvc::MdcTunningSvc( const string& name, ISvcLocator* svcloc )
29 : base_class( name, svcloc ) {
30 m_BesMdcRes = 0;
31 // declare properties
32 declareProperty( "UseDatabase", m_dbFlag = false );
33 declareProperty( "UseEndcapTuning",
34 m_EndcapTuning = 1 ); // 0:no endcap Tuning , 1: using endcap Tuning
35 declareProperty( "EffFile", m_effFile = std::string( "no path" ) );
36 declareProperty( "ResFile", m_resFile = std::string( "no path" ) );
37 declareProperty( "EffFile_endcap", m_effFile_endcap = std::string( "no path" ) );
38 declareProperty( "ResFile_endcap", m_resFile_endcap = std::string( "no path" ) );
39 declareProperty( "path_mdc", m_path = std::string( "no path" ) );
40 declareProperty( "Host", host = std::string( "bes3db2.ihep.ac.cn" ) );
41 declareProperty( "DbName", dbName = std::string( "offlinedb" ) );
42 declareProperty( "UserName", userName = std::string( "guest" ) );
43 declareProperty( "Password", password = std::string( "guestpass" ) );
44 declareProperty( "SerialNo", serialNo = 0 );
45 declareProperty( "fromDB", m_fromDB = true );
46 declareProperty( "ParBossVer", m_ParBossVer = std::string( "unknown" ) );
47
48 int no[43] = { 40, 44, 48, 56, 64, 72, 80, 80, 76, 76, 88, 88, 100, 100, 112,
49 112, 128, 128, 140, 140, 160, 160, 160, 160, 176, 176, 176, 176, 208, 208,
50 208, 208, 240, 240, 240, 240, 256, 256, 256, 256, 288, 288, 288 };
51
52 for ( int i = 0; i < 43; i++ ) { cellNo[i] = no[i]; }
53}
54
56 if ( m_BesMdcRes ) delete m_BesMdcRes;
57}
58
59/*StatusCode MdcTunningSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
60 if( IID_IMdcTunningSvc.versionMatch(riid) ){
61 *ppvInterface = static_cast<IMdcTunningSvc*> (this);
62 } else{
63 return Service::queryInterface(riid, ppvInterface);
64 }
65 return StatusCode::SUCCESS;
66}*/
67
69 MsgStream log( msgSvc(), name() );
70 log << MSG::INFO << "========== MdcTunningSvc::initialize() ==========" << endmsg;
71
72 // m_ParBossVer = getenv( "BES_RELEASE" );
73
74 StatusCode sc = Service::initialize();
75 if ( sc.isFailure() ) return sc;
76
77 if ( m_fromDB ) cout << " MdcTunningSvc read from database. " << endl;
78 else if ( !m_fromDB ) cout << " MdcTunningSvc read from localfile. " << endl;
79
80 if ( m_fromDB == true )
81 {
82
83 sc = serviceLocator()->service( "DatabaseSvc", m_dbsvc, true );
84 if ( sc.isFailure() )
85 {
86 log << MSG::ERROR << " ERROR: unable to find DatabaseSvc " << endmsg;
87 return sc;
88 }
89
90 // MYSQL *conn;
91 // char *opt_host_name = "202.122.33.53";
92 // char *opt_user_name = "maqm";
93 // char *opt_password = "maqm_offline";
94 //// unsigned int opt_port_num = 3306;
95 //// char *opt_socket_name = NULL;
96 // char *opt_db_name = "offlinedb";
97 ////conn = mysql_init(NULL);
98 ////unsigned int opt_flags = 0;
99 ////mysql_real_connect(conn, host.c_str(), userName.c_str(), password.c_str(),
100 //// dbName.c_str(), opt_port_num, opt_socket_name, opt_flags);
101 // mysql_real_connect(conn, opt_host_name, opt_user_name, opt_password,
102 // opt_db_name, opt_port_num, opt_socket_name, opt_flags);
103
104 IIncidentSvc* incsvc;
105 sc = service( "IncidentSvc", incsvc );
106 int priority = 100;
107 if ( sc.isSuccess() ) { incsvc->addListener( this, "NewRun", priority ); }
108 sc = serviceLocator()->service( "EventDataSvc", m_eventSvc, true );
109 if ( sc.isFailure() )
110 {
111 log << MSG::ERROR << " ERROR: unable to find EventDataSvc " << endmsg;
112 return sc;
113 }
114 }
115
116 if ( m_fromDB != true )
117 {
118 bool initStat = initTuningConst();
119 // if(m_path!=std::string("no path")) setMdcRes(m_path);
120 if ( !initStat )
121 {
122 cout << "========== MdcTunningSvc::initialize() failure ! ==========" << endl;
123 return StatusCode::FAILURE;
124 }
125 }
126
127 return StatusCode::SUCCESS;
128}
129
131 MsgStream log( msgSvc(), name() );
132 log << MSG::INFO << "========== MdcTunningSvc::finalize() ==========" << endmsg;
133 //// mysql_close(conn);
134
135 return StatusCode::SUCCESS;
136}
137
138void MdcTunningSvc::handle( const Incident& inc ) {
139 cout << "========== MdcTunningSvc::handle() ==========" << endl;
140
141 MsgStream log( msgSvc(), name() );
142 log << MSG::DEBUG << "handle: " << inc.type() << endmsg;
143
144 if ( inc.type() == "NewRun" )
145 {
146 log << MSG::DEBUG << "NewRun" << endmsg;
147 if ( m_fromDB == true )
148 {
149 cout << " Start getMdcTuningTableInfo. " << endl;
150 StatusCode sc = getMdcTuningTableInfo();
151 if ( sc.isFailure() )
152 {
153 cout << " ERROR: can not get MdcTuning data from the database. " << endl;
154 log << MSG::ERROR << " ERROR: can not get MdcTuning data from the database. "
155 << endmsg;
156 exit( 1 );
157 }
158 }
159 }
160}
161
163 std::string FilePath = getenv( "MDCTUNNINGSVCROOT" );
164
165 if ( m_effFile == std::string( "no path" ) )
166 {
167 if ( !m_fromDB )
168 cout << " ERROR: no mdc tuning eff file, please check the input! " << endl;
169 // m_effFile=FilePath+"/dat/mc_eff.dat";
170 // m_effFile = FilePath+"/dat/mc_eff_psipp.dat";
171 return false;
172 }
173 bool setMcEffStat = setMcEff( m_effFile );
174
175 if ( m_resFile == std::string( "no path" ) )
176 {
177 if ( !m_fromDB )
178 cout << " ERROR: no mdc tuning res file, please check the input! " << endl;
179 // m_resFile=FilePath+"/dat/mc_res.dat";
180 // m_resFile = FilePath+"/dat/mc_res_psipp.dat";
181 return false;
182 }
183 // setMcRes2(m_resFile);
184 bool setMcRes3Stat = setMcRes3( m_resFile );
185
186 if ( !( setMcRes3Stat && setMcEffStat ) ) return false;
187
188 return true;
189}
190
191bool MdcTunningSvc::setMcEff( std::string eff_con ) {
192 int i, j;
193 string line;
194 double lay, bin, expect, hit;
195 // ifstream readMCEff(eff_con.c_str());
196 std::istringstream readMCEff;
197 // (eff_con);
198 if ( m_fromDB ) { readMCEff.str( eff_con ); }
199 if ( !m_fromDB )
200 {
201 ifstream in( eff_con.c_str() );
202 // char ch;
203 // string hhh;
204 // while(ift.get(ch)) hhh.putback(ch);
205 // std::cout<<"hhh:"<<hhh<<std::endl;
206 // stringstream strBuf ;
207 istreambuf_iterator<char> iter( in );
208 string strCache = string( iter, ( istreambuf_iterator<char>() ) );
209 readMCEff.str( strCache );
210 // std::cout<<"strCache:"<<strCache<<std::endl;
211 }
212
213 ifstream fin( eff_con.c_str() );
214 if ( !m_fromDB )
215 if ( !fin )
216 {
217 cout << " ERROR: the mdc tunning eff file " << m_effFile
218 << " not exist, please check the input! " << endl;
219 return false;
220 }
221 // ifstream readMCEff(eff_con);
222 if ( !readMCEff.good() )
223 {
224 cout << " ERROR: mdc tuning eff file " << m_effFile << " not exist. " << endl;
225 return false;
226 }
227 else
228 {
229 if ( !m_fromDB )
230 if ( fin ) cout << " Open mdc tuning eff file: " << m_effFile << endl;
231 for ( i = 0; i < 43; i++ )
232 {
233 readMCEff >> lay;
234 getline( readMCEff, line );
235 for ( j = 0; j < docaNo; j++ ) { readMCEff >> bin >> docaEff[i][j] >> expect >> hit; }
236 readMCEff >> lay;
237 getline( readMCEff, line );
238 for ( j = 0; j < thetaNo; j++ ) { readMCEff >> bin >> thetaEff[i][j] >> expect >> hit; }
239 readMCEff >> lay;
240 getline( readMCEff, line );
241 for ( j = 0; j < cellNo[i]; j++ ) { readMCEff >> bin >> cellEff[i][j] >> expect >> hit; }
242 }
243 for ( i = 0; i < 43; i++ )
244 {
245 readMCEff >> lay;
246 getline( readMCEff, line );
247 for ( j = 0; j < docaNo; j++ ) { readMCEff >> bin >> docaEff_2[i][j] >> expect >> hit; }
248 readMCEff >> lay;
249 getline( readMCEff, line );
250 for ( j = 0; j < thetaNo; j++ )
251 { readMCEff >> bin >> thetaEff_2[i][j] >> expect >> hit; }
252 readMCEff >> lay;
253 getline( readMCEff, line );
254 for ( j = 0; j < cellNo[i]; j++ )
255 { readMCEff >> bin >> cellEff_2[i][j] >> expect >> hit; }
256 }
257 }
258 return true;
259}
260
262
263 int i, j;
264 string line;
265 double lay, bin;
266 ifstream readMCRes( m_resFile.c_str() );
267 if ( !readMCRes.good() )
268 {
269 cout << " ERROR: mdc tuning file: " << m_resFile << " not exist. " << endl;
270 return false;
271 }
272 else
273 {
274 cout << " Open mdc tuning file: " << m_resFile << endl;
275 for ( i = 0; i < 43; i++ )
276 {
277 readMCRes >> lay;
278 getline( readMCRes, line );
279 getline( readMCRes, line );
280 for ( j = 0; j < docaNo; j++ )
281 {
282 readMCRes >> bin >> docaRes[i][j][0][0] >> docaRes[i][j][0][1]; // entranceAngle<0
283 }
284 readMCRes >> lay;
285 getline( readMCRes, line );
286 getline( readMCRes, line );
287 for ( j = 0; j < docaNo; j++ )
288 {
289 readMCRes >> bin >> docaRes[i][j][1][0] >> docaRes[i][j][1][1]; // entranceAngle>0
290
291 /*
292 if(i==0||i==42){
293 cout<<"lay "<<i<<" docaNo "<<j<<" <0 mean "<<docaRes[i][j][0][0]<<" sigma
294 "<<docaRes[i][j][0][1]<<" >0 mean
295 "<<docaRes[i][j][1][0]<<" sigma "<<docaRes[i][j][1][1]<<endl;
296 }
297 */
298 }
299 }
300 }
301 return true;
302}
303
304bool MdcTunningSvc::setMcRes2( std::string res_con ) {
305
306 int i, j;
307 string line;
308 double lay, bin;
309 // ifstream readMCRes(m_resFile.c_str());
310 std::istringstream readMCRes;
311 if ( m_fromDB ) { readMCRes.str( res_con ); }
312 if ( !m_fromDB )
313 {
314 ifstream in( res_con.c_str() );
315 istreambuf_iterator<char> iter( in );
316 string strCache = string( iter, ( istreambuf_iterator<char>() ) );
317 readMCRes.str( strCache );
318 }
319 if ( !readMCRes.good() )
320 {
321 cout << " ERROR: mdc tuning file: " << m_resFile << " not exist. " << endl;
322 return false;
323 }
324 else
325 {
326 cout << " MdcTunningSvc::setMcRes2() Open mdc tuning resfile " << m_resFile << endl;
327 for ( i = 0; i < 43; i++ )
328 {
329 readMCRes >> lay;
330 getline( readMCRes, line );
331 getline( readMCRes, line );
332 for ( j = 0; j < docaNo; j++ )
333 {
334 readMCRes >> bin >> docaF[i][j][0] >> docaMean1[i][j][0] >> docaSigma1[i][j][0] >>
335 docaMean2[i][j][0] >> docaSigma2[i][j][0]; // entranceAngle<0
336 }
337 readMCRes >> lay;
338 getline( readMCRes, line );
339 getline( readMCRes, line );
340 for ( j = 0; j < docaNo; j++ )
341 {
342 readMCRes >> bin >> docaF[i][j][1] >> docaMean1[i][j][1] >> docaSigma1[i][j][1] >>
343 docaMean2[i][j][1] >> docaSigma2[i][j][1]; // entranceAngle>0
344 }
345 }
346 for ( i = 0; i < 43; i++ )
347 {
348 readMCRes >> lay;
349 getline( readMCRes, line );
350 getline( readMCRes, line );
351 for ( j = 0; j < docaNo; j++ )
352 {
353 readMCRes >> bin >> docaF_2[i][j][0] >> docaMean1_2[i][j][0] >>
354 docaSigma1_2[i][j][0] >> docaMean2_2[i][j][0] >>
355 docaSigma2_2[i][j][0]; // entranceAngle<0
356 }
357 readMCRes >> lay;
358 getline( readMCRes, line );
359 getline( readMCRes, line );
360 for ( j = 0; j < docaNo; j++ )
361 {
362 readMCRes >> bin >> docaF_2[i][j][1] >> docaMean1_2[i][j][1] >>
363 docaSigma1_2[i][j][1] >> docaMean2_2[i][j][1] >>
364 docaSigma2_2[i][j][1]; // entranceAngle>0
365 }
366 }
367 }
368 return true;
369}
370
371bool MdcTunningSvc::setMcRes3( std::string res_con ) {
372
373 int i, j;
374 string line;
375 double lay, bin;
376 // ifstream readMCRes(m_resFile.c_str());
377 std::istringstream readMCRes;
378 if ( m_fromDB ) { readMCRes.str( res_con ); }
379 if ( !m_fromDB )
380 {
381 ifstream in( res_con.c_str() );
382 istreambuf_iterator<char> iter( in );
383 string strCache = string( iter, ( istreambuf_iterator<char>() ) );
384 readMCRes.str( strCache );
385 }
386
387 ifstream fin( res_con.c_str() );
388
389 if ( !m_fromDB )
390 if ( !fin )
391 {
392 cout << " ERROR: the mdc tunning res file " << m_resFile
393 << " not exist, please check the input!" << endl;
394 return false;
395 }
396 if ( !readMCRes.good() )
397 {
398 cout << " ERROR: the mdc tuning res file: " << m_resFile
399 << " not exist, please check the input! " << endl;
400 return false;
401 }
402 else
403 {
404 if ( !m_fromDB )
405 if ( fin )
406 cout << " MdcTunningSvc::setMcRes3() Open mdc tuning resfile: " << m_resFile << endl;
407 for ( i = 0; i < 43; i++ )
408 {
409 readMCRes >> lay;
410 getline( readMCRes, line );
411 getline( readMCRes, line );
412 for ( j = 0; j < docaNo; j++ )
413 {
414 readMCRes >> bin >> docaF[i][j][0] >> docaMean1[i][j][0] >> docaSigma1[i][j][0] >>
415 docaMean2[i][j][0] >> docaSigma2[i][j][0] >> resLargest[i][j][0] >>
416 resSmallest[i][j][0] >> resRatio[i][j][0]; // entranceAngle<0
417 }
418 readMCRes >> lay;
419 getline( readMCRes, line );
420 getline( readMCRes, line );
421 for ( j = 0; j < docaNo; j++ )
422 {
423 readMCRes >> bin >> docaF[i][j][1] >> docaMean1[i][j][1] >> docaSigma1[i][j][1] >>
424 docaMean2[i][j][1] >> docaSigma2[i][j][1] >> resLargest[i][j][1] >>
425 resSmallest[i][j][1] >> resRatio[i][j][1]; // entranceAngle>0
426 }
427 }
428 for ( i = 0; i < 43; i++ )
429 {
430 readMCRes >> lay;
431 getline( readMCRes, line );
432 getline( readMCRes, line );
433 for ( j = 0; j < docaNo; j++ )
434 {
435 readMCRes >> bin >> docaF_2[i][j][0] >> docaMean1_2[i][j][0] >>
436 docaSigma1_2[i][j][0] >> docaMean2_2[i][j][0] >> docaSigma2_2[i][j][0] >>
437 resLargest_2[i][j][0] >> resSmallest_2[i][j][0] >>
438 resRatio_2[i][j][0]; // entranceAngle<0
439 }
440 readMCRes >> lay;
441 getline( readMCRes, line );
442 getline( readMCRes, line );
443 for ( j = 0; j < docaNo; j++ )
444 {
445
446 readMCRes >> bin >> docaF_2[i][j][1] >> docaMean1_2[i][j][1] >>
447 docaSigma1_2[i][j][1] >> docaMean2_2[i][j][1] >> docaSigma2_2[i][j][1] >>
448 resLargest_2[i][j][1] >> resSmallest_2[i][j][1] >>
449 resRatio_2[i][j][1]; // entranceAngle>0
450 }
451 }
452 }
453
454 return true;
455}
456
457BesMdcRes* MdcTunningSvc::getMdcRes() { return m_BesMdcRes; }
458
459void MdcTunningSvc::setMdcRes( std::string path ) {
460 if ( m_BesMdcRes ) delete m_BesMdcRes;
461 m_BesMdcRes = new BesMdcRes( path );
462}
463
464double MdcTunningSvc::NewSig( int layerId, double driftD ) {
465 int bindD = 0;
466 double mindD = 0.0;
467 double maxdD = 9.0;
468 int maxbin = 8.0;
469
470 if ( ( driftD < mindD ) || ( driftD > maxdD ) ) { bindD = maxbin; }
471 else
472 {
473 for ( int kk = 0; kk < 9; kk++ )
474 {
475 if ( ( driftD >= (double)kk ) && ( driftD < (double)( kk + 1 ) ) ) { bindD = kk; }
476 }
477 }
478
479 double sigma1 = 0;
480
481 sigma1 = ( m_BesMdcRes->getD_dD( layerId, bindD ) );
482
483 return sigma1;
484}
485
486double MdcTunningSvc::DeldriftD( int layerId, double driftD ) {
487 int bindD = 0;
488 int maxbin = 8;
489 double mindD = 0.0;
490 double maxdD = 9.0;
491
492 for ( int jj = 0; jj < 9; jj++ )
493 {
494 if ( ( driftD < mindD ) || ( driftD > maxdD ) ) { bindD = maxbin; }
495 else if ( ( driftD >= jj ) && ( driftD < ( jj + 1 ) ) ) { bindD = jj; }
496 }
497 double y0D = ( m_BesMdcRes->getD_dD( layerId, bindD ) );
498 double y1D = ( m_BesMdcRes->getD_dD( layerId, bindD + 1 ) );
499 double yD = y0D + ( y1D - y0D ) * ( driftD - bindD ); // calculate the data
500 double y0M = ( m_BesMdcRes->getM_dD( layerId, bindD ) );
501 double y1M = ( m_BesMdcRes->getM_dD( layerId, bindD + 1 ) );
502 double yM = y0M + ( y1M - y0M ) * ( driftD - bindD ); // calculate the mc data
503 double dely = yD - yM;
504
505 /*if((bindD>=1)&&(bindD<=4)){
506 // cout<<"layerId :"<<layerId<<" dritfD :"<<driftD<<" dely*0.618:"<<dely*0.618<<endl;
507 return dely*0.618 ;
508 }else if((bindD>=5)&&(bindD<7)||(bindD ==8)){
509 // cout<<"layerId :"<<layerId<<" dritfD :"<<driftD<<" dely*0.418:"<<dely*0.418<<endl;
510 return dely*0.418 ;
511 }else {
512 // cout<<"layerId :"<<layerId<<" dritfD :"<<driftD<<" dely:"<<dely<<endl;
513 return dely;
514 }*/
515 return dely;
516}
517
518double MdcTunningSvc::Delcostta( int layerId, double costta ) {
519 int binTa = 0;
520 int maxTa = 15;
521 double minCos = -0.8;
522 double minCos2 = -0.7;
523 double maxCos = 0.8;
524
525 for ( int ii = 0; ii < 16; ii++ )
526 {
527 if ( ( costta < minCos ) || ( costta > maxCos ) ) { binTa = maxTa; }
528 else if ( ( costta >= ( minCos + ii * 0.1 ) ) && ( costta < ( minCos2 + ii * 0.1 ) ) )
529 { binTa = ii; }
530 }
531
532 double y0D = ( m_BesMdcRes->getD_theta( layerId, binTa ) );
533 double y1D = ( m_BesMdcRes->getD_theta( layerId, binTa + 1 ) );
534 double y0M = ( m_BesMdcRes->getM_theta( layerId, binTa ) );
535 double y1M = ( m_BesMdcRes->getM_theta( layerId, binTa + 1 ) );
536
537 double yD[16], yM[16], Del[16];
538 for ( int ll = 0; ll < 16; ll++ )
539 {
540 yD[ll] = y0D + ( y1D - y0D ) / 0.1 * ( costta - ( minCos + ll * 0.1 ) );
541 yM[ll] = y0M + ( y1M - y0D ) / 0.1 * ( costta - ( minCos + ll * 0.1 ) );
542 Del[ll] = yD[ll] - yM[ll];
543 }
544
545 double delTha = 0;
546
547 if ( ( binTa >= 0 ) && ( binTa <= 5 ) ) { delTha = Del[binTa] * 0.118; }
548 else if ( ( binTa > 5 ) && ( binTa <= 10 ) ) { delTha = Del[binTa] * 0.518; }
549 else if ( ( binTa > 10 ) && ( binTa <= 15 ) ) { delTha = Del[binTa] * 0.218; }
550
551 return delTha;
552}
553
554double MdcTunningSvc::GetEff( int layerId, int cellId, double driftD, double cosTheta,
555 int posFlag ) {
556 driftD = fabs( driftD );
557 if ( driftD > 12 )
558 {
559 // std::cout<<"MdcTuningSvc: driftD overflow "<<driftD<<std::endl;
560 driftD = 12;
561 }
562 if ( posFlag == 0 ) driftD *= -1;
563
564 if ( layerId < 0 || layerId > 42 )
565 std::cout << " MdcTuningSvc:wrong LayerId " << layerId << std::endl;
566 if ( cellId < 0 || cellId >= cellNo[layerId] )
567 std::cout << "MdcTuningSvc:wrong cellId " << cellId << std::endl;
568
569 if ( fabs( cosTheta ) > 1 )
570 {
571 std::cout << "MdcTuningSvc:wrong coseTheta " << cosTheta << std::endl;
572 cosTheta = 1;
573 }
574 double eff;
575 int thetaBin = (int)floor( ( cosTheta + 1 ) * thetaNo / 2. );
576 // debug
577 // std::cout<<"cosTheta "<<cosTheta<<" caled "<<(cosTheta+1)*thetaNo/2.<<" floor
578 // "<<thetaBin<<std::endl;
579 int docaBin = (int)floor( ( driftD + 12 ) * docaNo / 24. );
580 if ( m_EndcapTuning == 0 )
581 eff = docaEff[layerId][docaBin] * thetaEff[layerId][thetaBin] * cellEff[layerId][cellId];
582 else
583 {
584 if ( fabs( cosTheta ) <= 0.83 )
585 eff = docaEff[layerId][docaBin] * thetaEff[layerId][thetaBin] * cellEff[layerId][cellId];
586 else
587 eff = docaEff_2[layerId][docaBin] * thetaEff_2[layerId][thetaBin] *
588 cellEff_2[layerId][cellId];
589 }
590 // debug
591 // std::cout<<"tuning svc layer "<<layerId<<"doca"<<docaBin<<" theta
592 // "<<thetaBin<<"cell"<<cellId<<" eff
593 // "<<eff<<std::endl;
594
595 return eff;
596}
597
598double MdcTunningSvc::GetRes( int layerId, int cellId, double driftD, double cosTheta,
599 int posFlag, double entranceAngle, double& mean,
600 double& sigma ) {
601
602 driftD = fabs( driftD );
603 if ( driftD > 12 )
604 {
605 // std::cout<<"MdcTuningSvc: driftD overflow "<<driftD<<std::endl;
606 driftD = 12;
607 }
608 if ( posFlag == 0 ) driftD *= -1;
609
610 if ( layerId < 0 || layerId > 42 )
611 std::cout << " MdcTuningSvc:wrong LayerId " << layerId << std::endl;
612 if ( cellId < 0 || cellId >= cellNo[layerId] )
613 std::cout << "MdcTuningSvc:wrong cellId " << cellId << std::endl;
614
615 if ( fabs( cosTheta ) > 1 )
616 {
617 std::cout << "MdcTuningSvc:wrong cosTheta " << cosTheta << std::endl;
618 cosTheta = 1;
619 }
620
621 //// int thetaBin=(int)floor((cosTheta+1)*thetaNo/2.);
622 // debug
623 // std::cout<<"cosTheta "<<cosTheta<<" caled "<<(cosTheta+1)*thetaNo/2.<<" floor
624 // "<<thetaBin<<std::endl;
625 int docaBin = (int)floor( ( driftD + 12 ) * docaNo / 24. );
626 if ( entranceAngle < 0 )
627 {
628 mean = docaRes[layerId][docaBin][0][0];
629 sigma = docaRes[layerId][docaBin][0][1];
630 }
631 else
632 {
633 mean = docaRes[layerId][docaBin][1][0];
634 sigma = docaRes[layerId][docaBin][1][1];
635 }
636
637 // debug
638 // std::cout<<"tuning svc layer "<<layerId<<" driftD "<<driftD<<" posFlag "<<posFlag<<" doca
639 // "<<docaBin<<" theta
640 // "<<thetaBin<<" angle "<<entranceAngle<<" mean "<<mean<<" sigma "<<sigma<<std::endl;
641
642 return 1;
643}
644
645double MdcTunningSvc::GetRes2( int layerId, int cellId, double driftD, double cosTheta,
646 int posFlag, double entranceAngle, double& f, double& mean1,
647 double& sigma1, double& mean2, double& sigma2 ) {
648
649 driftD = fabs( driftD );
650 if ( driftD > 12 )
651 {
652 // std::cout<<"MdcTuningSvc: driftD overflow "<<driftD<<std::endl;
653 driftD = 12;
654 }
655 if ( posFlag == 0 ) driftD *= -1;
656
657 if ( layerId < 0 || layerId > 42 )
658 std::cout << " MdcTuningSvc:wrong LayerId " << layerId << std::endl;
659 if ( cellId < 0 || cellId >= cellNo[layerId] )
660 std::cout << "MdcTuningSvc:wrong cellId " << cellId << std::endl;
661
662 if ( fabs( cosTheta ) > 1 )
663 {
664 std::cout << "MdcTuningSvc:wrong cosTheta " << cosTheta << std::endl;
665 cosTheta = 1;
666 }
667
668 //// int thetaBin=(int)floor((cosTheta+1)*thetaNo/2.);
669 // debug
670 // std::cout<<"cosTheta "<<cosTheta<<" caled "<<(cosTheta+1)*thetaNo/2.<<" floor
671 // "<<thetaBin<<std::endl;
672 int docaBin = (int)floor( ( driftD + 12 ) * docaNo / 24. );
673 if ( m_EndcapTuning == 0 )
674 {
675 if ( entranceAngle < 0 )
676 {
677 f = docaF[layerId][docaBin][0];
678 mean1 = docaMean1[layerId][docaBin][0];
679 sigma1 = docaSigma1[layerId][docaBin][0];
680 mean2 = docaMean2[layerId][docaBin][0];
681 sigma2 = docaSigma2[layerId][docaBin][0];
682 }
683 else
684 {
685 f = docaF[layerId][docaBin][1];
686 mean1 = docaMean1[layerId][docaBin][1];
687 sigma1 = docaSigma1[layerId][docaBin][1];
688 mean2 = docaMean2[layerId][docaBin][1];
689 sigma2 = docaSigma2[layerId][docaBin][1];
690 }
691 }
692 else
693 {
694 if ( fabs( cosTheta ) <= 0.83 )
695 {
696 if ( entranceAngle < 0 )
697 {
698 f = docaF[layerId][docaBin][0];
699 mean1 = docaMean1[layerId][docaBin][0];
700 sigma1 = docaSigma1[layerId][docaBin][0];
701 mean2 = docaMean2[layerId][docaBin][0];
702 sigma2 = docaSigma2[layerId][docaBin][0];
703 }
704 else
705 {
706 f = docaF[layerId][docaBin][1];
707 mean1 = docaMean1[layerId][docaBin][1];
708 sigma1 = docaSigma1[layerId][docaBin][1];
709 mean2 = docaMean2[layerId][docaBin][1];
710 sigma2 = docaSigma2[layerId][docaBin][1];
711 }
712 }
713 else
714 {
715 if ( entranceAngle < 0 )
716 {
717 f = docaF_2[layerId][docaBin][0];
718 mean1 = docaMean1_2[layerId][docaBin][0];
719 sigma1 = docaSigma1_2[layerId][docaBin][0];
720 mean2 = docaMean2_2[layerId][docaBin][0];
721 sigma2 = docaSigma2_2[layerId][docaBin][0];
722 }
723 else
724 {
725 f = docaF_2[layerId][docaBin][1];
726 mean1 = docaMean1_2[layerId][docaBin][1];
727 sigma1 = docaSigma1_2[layerId][docaBin][1];
728 mean2 = docaMean2_2[layerId][docaBin][1];
729 sigma2 = docaSigma2_2[layerId][docaBin][1];
730 }
731 }
732 }
733
734 // debug
735 // std::cout<<"tuning svc layer "<<layerId<<" driftD "<<driftD<<" posFlag "<<posFlag<<" doca
736 // "<<docaBin<<" theta
737 // "<<thetaBin<<" angle "<<entranceAngle<<" f "<<f<<" mean1 "<<mean1<<" sigma1 "<<sigma1<<"
738 // mean2 "<<mean2<<" sigma2
739 // "<<sigma2<<std::endl;
740
741 return 1;
742}
743
744double MdcTunningSvc::GetRes3( int layerId, int cellId, double driftD, double cosTheta,
745 int posFlag, double entranceAngle, double& f, double& mean1,
746 double& sigma1, double& mean2, double& sigma2,
747 double& ResLargest, double& ResSmallest, double& ResRatio ) {
748
749 driftD = fabs( driftD );
750 if ( driftD > 12 )
751 {
752 // std::cout<<"MdcTuningSvc: driftD overflow "<<driftD<<std::endl;
753 driftD = 12;
754 }
755 if ( posFlag == 0 ) driftD *= -1;
756
757 if ( layerId < 0 || layerId > 42 )
758 std::cout << " MdcTuningSvc:wrong LayerId " << layerId << std::endl;
759 if ( cellId < 0 || cellId >= cellNo[layerId] )
760 std::cout << "MdcTuningSvc:wrong cellId " << cellId << std::endl;
761
762 if ( fabs( cosTheta ) > 1 )
763 {
764 std::cout << "MdcTuningSvc:wrong cosTheta " << cosTheta << std::endl;
765 cosTheta = 1;
766 }
767
768 ////int thetaBin=(int)floor((cosTheta+1)*thetaNo/2.);
769 // debug
770 // std::cout<<"cosTheta "<<cosTheta<<" caled "<<(cosTheta+1)*thetaNo/2.<<" floor
771 // "<<thetaBin<<std::endl;
772 int docaBin = (int)floor( ( driftD + 12 ) * docaNo / 24. );
773 if ( m_EndcapTuning == 0 )
774 {
775 if ( entranceAngle < 0 )
776 {
777 f = docaF[layerId][docaBin][0];
778 mean1 = docaMean1[layerId][docaBin][0];
779 sigma1 = docaSigma1[layerId][docaBin][0];
780 mean2 = docaMean2[layerId][docaBin][0];
781 sigma2 = docaSigma2[layerId][docaBin][0];
782 ResLargest = resLargest[layerId][docaBin][0];
783 ResSmallest = resSmallest[layerId][docaBin][0];
784 ResRatio = resRatio[layerId][docaBin][0];
785 }
786 else
787 {
788 f = docaF[layerId][docaBin][1];
789 mean1 = docaMean1[layerId][docaBin][1];
790 sigma1 = docaSigma1[layerId][docaBin][1];
791 mean2 = docaMean2[layerId][docaBin][1];
792 sigma2 = docaSigma2[layerId][docaBin][1];
793 ResLargest = resLargest[layerId][docaBin][1];
794 ResSmallest = resSmallest[layerId][docaBin][1];
795 ResRatio = resRatio[layerId][docaBin][1];
796 }
797 }
798 else
799 {
800 if ( fabs( cosTheta ) <= 0.83 )
801 {
802 if ( entranceAngle < 0 )
803 {
804 f = docaF[layerId][docaBin][0];
805 mean1 = docaMean1[layerId][docaBin][0];
806 sigma1 = docaSigma1[layerId][docaBin][0];
807 mean2 = docaMean2[layerId][docaBin][0];
808 sigma2 = docaSigma2[layerId][docaBin][0];
809 ResLargest = resLargest[layerId][docaBin][0];
810 ResSmallest = resSmallest[layerId][docaBin][0];
811 ResRatio = resRatio[layerId][docaBin][0];
812 }
813 else
814 {
815 f = docaF[layerId][docaBin][1];
816 mean1 = docaMean1[layerId][docaBin][1];
817 sigma1 = docaSigma1[layerId][docaBin][1];
818 mean2 = docaMean2[layerId][docaBin][1];
819 sigma2 = docaSigma2[layerId][docaBin][1];
820 ResLargest = resLargest[layerId][docaBin][1];
821 ResSmallest = resSmallest[layerId][docaBin][1];
822 ResRatio = resRatio[layerId][docaBin][1];
823 }
824 }
825 else
826 {
827 if ( entranceAngle < 0 )
828 {
829 f = docaF_2[layerId][docaBin][0];
830 mean1 = docaMean1_2[layerId][docaBin][0];
831 sigma1 = docaSigma1_2[layerId][docaBin][0];
832 mean2 = docaMean2_2[layerId][docaBin][0];
833 sigma2 = docaSigma2_2[layerId][docaBin][0];
834 ResLargest = resLargest_2[layerId][docaBin][0];
835 ResSmallest = resSmallest_2[layerId][docaBin][0];
836 ResRatio = resRatio_2[layerId][docaBin][0];
837 }
838 else
839 {
840 f = docaF_2[layerId][docaBin][1];
841 mean1 = docaMean1_2[layerId][docaBin][1];
842 sigma1 = docaSigma1_2[layerId][docaBin][1];
843 mean2 = docaMean2_2[layerId][docaBin][1];
844 sigma2 = docaSigma2_2[layerId][docaBin][1];
845 ResLargest = resLargest_2[layerId][docaBin][1];
846 ResSmallest = resSmallest_2[layerId][docaBin][1];
847 ResRatio = resRatio_2[layerId][docaBin][1];
848 }
849 }
850 }
851
852 // debug
853 // std::cout<<"tuning svc layer "<<layerId<<" driftD "<<driftD<<" posFlag "<<posFlag<<" doca
854 // "<<docaBin<<" theta
855 // "<<thetaBin<<" angle "<<entranceAngle<<" f "<<f<<" mean1 "<<mean1<<" sigma1 "<<sigma1<<"
856 // mean2 "<<mean2<<" sigma2
857 // "<<sigma2<<std::endl; debug information std::cout<<"MdcTunningSvc::GetRes3() debug
858 // Info."<<endl
859 // <<"layer docaBin thetaBin entranceAngle f
860 // mean1 sigma1
861 // mean2 sigma2 largest smallest ratio"<<endl
862 // <<setw(5)<<layerId<<setw(5)<<docaBin<<setw(5)<<thetaBin<<setw(18)<<entranceAngle<<setw(15)<<f<<setw(15)<<mean1<<setw(15)<<sigma1<<setw(15)<<mean2<<setw(15)<<sigma2<<setw(15)<<ResLargest<<setw(15)<<ResSmallest<<ResRatio<<endl;
863
864 return 1;
865}
866
867double MdcTunningSvc::ResvEntr( int layerId, double enterA, int ilr, double driftD ) {
868 int bindD = 0;
869 int maxbin = 17;
870 int iEntr = 0;
871 int ll = -1;
872 double mindD = -9.;
873 double maxdD = 9.;
874 double sigmaE = 0.0;
875
876 if ( enterA < 0 ) { iEntr = 0; }
877 else { iEntr = 1; }
878
879 if ( ilr == 0 ) { driftD = -1. * driftD; }
880 else { driftD = driftD; }
881
882 if ( ( driftD < mindD ) || ( driftD > maxdD ) ) { bindD = maxbin; }
883 else
884 {
885 for ( double dd = -9.; dd < 9.; dd++ )
886 {
887 ll++;
888 if ( ( driftD >= dd ) && ( driftD < ( dd + 1. ) ) ) { bindD = ll; }
889 }
890 }
891
892 sigmaE = ( m_BesMdcRes->getD_iEntr( layerId, iEntr, bindD ) );
893 // cout<<"Svc lay : "<<layerId<<" iEntr : "<<iEntr<<" bindD : "<<bindD<<" sigmaE
894 // :"<<sigmaE<<endl;
895 return sigmaE;
896}
897
898double MdcTunningSvc::DelEtr_Sig( int lay, double enterA, int ilr, double driftD ) {
899 int bindD = 0;
900 int maxbin = 17;
901 int iEntr = 0;
902 int ll = -1;
903 double mindD = -9.;
904 double maxdD = 9.0;
905 //// double delsigE =0;
906
907 if ( enterA < 0 ) { iEntr = 0; }
908 else { iEntr = 1; }
909
910 if ( ilr == 0 ) { driftD = -1. * driftD; }
911 else { driftD = driftD; }
912
913 if ( ( driftD < mindD ) || ( driftD > maxdD ) ) { bindD = maxbin; }
914 else
915 {
916 for ( double dd = -9.; dd < 9.; dd++ )
917 {
918 ll++;
919 if ( ( driftD >= dd ) && ( driftD < ( dd + 1. ) ) )
920 {
921 bindD = ll;
922 dD[bindD] = dd;
923 }
924 }
925 }
926
927 double y0D = ( m_BesMdcRes->getD_iEntr( lay, iEntr, bindD ) );
928 double y1D = ( m_BesMdcRes->getD_iEntr( lay, iEntr, bindD + 1 ) );
929 double yD = y0D + ( y1D - y0D ) * ( driftD - dD[bindD] ); // calculate the data
930 double y0M = ( m_BesMdcRes->getM_iEntr( lay, iEntr, bindD ) );
931 double y1M = ( m_BesMdcRes->getM_iEntr( lay, iEntr, bindD + 1 ) );
932 double yM = y0M + ( y1M - y0M ) * ( driftD - dD[bindD] ); // calculate the mc data
933 double dely = yD - yM;
934
935 // cout<<"Svc lay:"<<lay<<" iEntr :"<<iEntr<<" bindD :"<<bindD
936 // <<" dD["<<bindD<<"] : "<<dD[bindD]
937 // <<" y0D : "<<y0D<<" y1D : "<<y1D<<" yD :"<<yD
938 // <<" y0M : "<<y0M<<" y1M : "<<y1M<<" yM :"<<yM<<" dely : "<<dely<<endl;
939
940 return dely;
941}
942
944 MsgStream log( msgSvc(), name() );
945 SmartDataPtr<Event::EventHeader> eventHeader( m_eventSvc, "/Event/EventHeader" );
946
947 int run = eventHeader->runNumber();
948
949 log << MSG::INFO << "MdcTuningSvc::getMdcTuningTableInfo() run =" << run << endmsg;
950 if ( m_ParBossVer == std::string( "unknown" ) )
951 cout << "MdcTuningSvc::getMdcTuningTableInfo() : ERROR: there is no ParBossVer " << endl;
952 else
953 log << MSG::INFO << "MdcTuningSvc::getMdcTuningTableInfo() : ParBossVer = " << m_ParBossVer
954 << endl;
955
956 run = fabs( run );
957 ////unsigned long *lengths;
958 //// MYSQL_RES *res_set;
959 //// MYSQL_ROW row;
960 int j = 0;
961 int cnt = 0;
963 for ( int i = 0; i < 1000; i++ )
964 {
965 char stmt1[200];
966 cnt = i;
967 int run1 = run + i;
968 cout << " ==================== " << endl;
969 // sprintf(stmt1,"select MdcRes,MdcEff,dEdxTuning from MdcTuning where RunFrom <= %d and
970 // RunTo >= %d ",run1,run1);
971 sprintf( stmt1,
972 "select MdcRes,MdcEff from MdcTuning where RunFrom <= %d and RunTo >= %d and "
973 "SftVer = \"%s\"",
974 run1, run1, m_ParBossVer.c_str() );
975 cout << " stmt1: " << stmt1 << endl;
976
977 result.clear();
978 int status = m_dbsvc->query( "offlinedb", stmt1, result );
979 if ( status < 0 )
980 {
981 cout << " ERROR: can not read MdcRes, MdcEff from the MdcTuning table " << endl;
982 log << MSG::ERROR << " ERROR Read MdcRes, MdcEff from the MdcTuning table " << endmsg;
983 return StatusCode::FAILURE;
984 }
985
986 if ( result.size() >= 1 )
987 {
988
989 //// int aaa = -1;
990 ////aaa = mysql_real_query(conn, stmt1, strlen(stmt1));
991
992 ////std::cout<<" mysql_real_query: "<<aaa<<std::endl;
993 //// if(aaa){
994 // if(mysql_real_query(conn, stmt1, strlen(stmt1)))
995 ////fprintf(stderr, "query error\n");
996 ////return StatusCode::FAILURE;
997 ////}
998 ////res_set = mysql_store_result (conn);
999 ////row = mysql_fetch_row(res_set);
1000 ////int row_no = mysql_num_rows(res_set);
1001 ////std::cout<<"row_no="<<row_no<<std::endl;
1002 ////if (row_no) {}
1003 j = 1;
1004 break;
1005 }
1006
1007 int run2 = run - i;
1008 char stmt2[200];
1009 // sprintf(stmt2,"select MdcRes,MdcEff.dEdxTuning from MdcTuning where RunFrom <= %d and
1010 // RunTo >= %d ",run2,run2);
1011 sprintf( stmt2,
1012 "select MdcRes,MdcEff from MdcTuning where RunFrom <= %d and RunTo >= %d and "
1013 "SftVer = \"%s\"",
1014 run2, run2, m_ParBossVer.c_str() );
1015
1016 result.clear();
1017 status = m_dbsvc->query( "offlinedb", stmt2, result );
1018 if ( status < 0 )
1019 {
1020 log << MSG::ERROR << " ERROR Read MdcRes, MdcEff.dEdxTuning from the MdcTuning table "
1021 << endmsg;
1022 return StatusCode::FAILURE;
1023 }
1024
1025 if ( result.size() >= 1 )
1026 {
1027 //// mysql_real_query(conn, stmt2, strlen(stmt2));
1028 ////res_set = mysql_store_result (conn);
1029 ////row = mysql_fetch_row(res_set);
1030 ////row_no = mysql_num_rows(res_set);
1031 ////std::cout<<"row_no="<<row_no<<std::endl;
1032 ////if (row_no) {}
1033 j = -1;
1034 break;
1035 }
1036 }
1037
1038 if ( cnt != 0 && cnt != 1000 )
1039 {
1040 log << MSG::INFO << " get MDC tuning data from run " << run + cnt * j << " instead of run "
1041 << run << endmsg;
1042 //// std::cout<<"get MDC tuning data form run " <<run+cnt*j<<" instread of run"<<
1043 /// run<<std::endl;
1044 }
1045 cout << " cnt = " << cnt << endl;
1046 if ( cnt == 1000 )
1047 {
1048 log << MSG::ERROR << " can not read Data from DB" << endmsg;
1049 //// mysql_close(conn);
1050 return StatusCode::FAILURE;
1051 }
1052
1053 //// mysql_field_seek (res_set, 0);
1054
1055 //// lengths = mysql_fetch_lengths(res_set);
1056 // string fff = std::string(row[0]);
1057 // m_BesMdcRes->setMdcRes(fff);
1058 ////string ggg = std::string(row[1]);
1059 ////string fff = std::string(row[0]);
1060
1061 // get last row
1062 int row = result.size() - 1;
1063 cout << " row = " << row << endl;
1064
1065 if ( row < 0 )
1066 {
1067 cout << " ERROR: can not read Data from DB, please check MdcTunningSvc Version. " << endl;
1068 return StatusCode::FAILURE;
1069 }
1070
1071 string ggg = result[row]->GetString( "MdcEff" );
1072 string fff = result[row]->GetString( "MdcRes" );
1073
1074 log << MSG::DEBUG << " MdcTunning Data: MdcEff: " << ggg << " MdcRes: " << fff << endmsg;
1075 // cout << " MdcTunning Data: MdcEff: " << ggg << " MdcRes: " << fff << endl;
1076
1077 bool stRes = setMcRes3( fff );
1078 // bool stRes = setMcRes2(fff);
1079 bool stEff = setMcEff( ggg );
1080
1081 if ( !stEff ) return StatusCode::FAILURE;
1082 if ( !stRes ) return StatusCode::FAILURE;
1083 return StatusCode::SUCCESS;
1084}
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)
TFile f("ana_bhabha660a_dqa_mcPat_zy_old.root")
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
*******INTEGER m_nBinMax INTEGER m_NdiMax !No of bins in histogram for cell exploration division $ !Last vertex $ !Last active cell $ !Last cell in buffer $ !No of sampling when dividing cell $ !No of function total $ !Flag for random ceel for $ !Flag for type of for WtMax $ !Flag which decides whether vertices are included in the sampling $ entire domain is hyp !Maximum effective eevents per bin
Definition FoamA.h:85
IMessageSvc * msgSvc()
double DelEtr_Sig(int lay, double enterA, int ilr, double driftD)
void setMdcRes(std::string path)
bool setMcRes2(std::string res_con)
bool setMcRes3(std::string res_con)
StatusCode getMdcTuningTableInfo()
double Delcostta(int layerId, double costta)
void handle(const Incident &)
BesMdcRes * getMdcRes()
double ResvEntr(int layerId, double enterA, int ilr, double driftD)
bool setMcEff(std::string eff_con)
double NewSig(int layerId, double driftD)
double DeldriftD(int layerId, double driftD)
double GetRes(int layerId, int cellId, double driftD, double cosTheta, int posFlag, double entranceAngle, double &mean, double &sigma)
double GetEff(int layerId, int cellId, double driftD, double cosTheta, int posFlag)
MdcTunningSvc(const std::string &name, ISvcLocator *svcloc)
virtual StatusCode finalize()
double GetRes2(int layerId, int cellId, double driftD, double cosTheta, int posFlag, double entranceAngle, double &f, double &mean1, double &sigma1, double &mean2, double &sigma2)
double GetRes3(int layerId, int cellId, double driftD, double cosTheta, int posFlag, double entranceAngle, double &f, double &mean1, double &sigma1, double &mean2, double &sigma2, double &ResLargest, double &ResSmallest, double &ResRatio)
virtual StatusCode initialize()