00001 #include <math.h>
00002 #include <iostream>
00003 #include <iomanip>
00004 #include <fstream>
00005 #include <iostream>
00006
00007 #include "MdcTunningSvc.h"
00008 #include "GaudiKernel/Kernel.h"
00009 #include "GaudiKernel/IInterface.h"
00010 #include "GaudiKernel/StatusCode.h"
00011 #include "GaudiKernel/SvcFactory.h"
00012 #include "GaudiKernel/MsgStream.h"
00013
00014 #include "GaudiKernel/IIncidentSvc.h"
00015 #include "GaudiKernel/Incident.h"
00016 #include "GaudiKernel/IIncidentListener.h"
00017
00018 #include "GaudiKernel/ISvcLocator.h"
00019 #include "GaudiKernel/Bootstrap.h"
00020
00021
00022 #include "EventModel/EventModel.h"
00023 #include "EventModel/Event.h"
00024 #include "EventModel/EventHeader.h"
00025 #include "GaudiKernel/SmartDataPtr.h"
00026
00027 #include "DatabaseSvc/DatabaseSvc.h"
00028
00029 using namespace std;
00030
00031 MdcTunningSvc::MdcTunningSvc( const string& name, ISvcLocator* svcloc) :
00032 Service (name, svcloc){
00033 m_BesMdcRes=0;
00034
00035 declareProperty("UseDatabase",m_dbFlag = false);
00036 declareProperty("UseEndcapTuning",m_EndcapTuning = 1);
00037 declareProperty("EffFile", m_effFile = std::string("no path"));
00038 declareProperty("ResFile", m_resFile = std::string("no path"));
00039 declareProperty("EffFile_endcap", m_effFile_endcap = std::string("no path"));
00040 declareProperty("ResFile_endcap", m_resFile_endcap = std::string("no path"));
00041 declareProperty("path_mdc", m_path = std::string("no path"));
00042 declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
00043 declareProperty("DbName" , dbName = std::string("offlinedb"));
00044 declareProperty("UserName" , userName = std::string("guest"));
00045 declareProperty("Password" , password = std::string("guestpass"));
00046 declareProperty("SerialNo" , serialNo = 0);
00047 declareProperty("fromDB", m_fromDB = true);
00048 declareProperty("ParBossVer", m_ParBossVer = std::string("unknown"));
00049
00050 int no[43]={40,44,48,56,64,72,80,80,76,76,88,88,100,100,112,112,128,128,140,140,160,160,160,160,176,176,176,176,208,208,208,208,240,240,240,240,256,256,256,256,288,288,288};
00051
00052 for(int i=0;i<43;i++){
00053 cellNo[i]=no[i];
00054 }
00055
00056 }
00057
00058 MdcTunningSvc::~MdcTunningSvc(){
00059 if(m_BesMdcRes) delete m_BesMdcRes;
00060 }
00061
00062 StatusCode MdcTunningSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
00063 if( IID_IMdcTunningSvc.versionMatch(riid) ){
00064 *ppvInterface = static_cast<IMdcTunningSvc*> (this);
00065 } else{
00066 return Service::queryInterface(riid, ppvInterface);
00067 }
00068 return StatusCode::SUCCESS;
00069 }
00070
00071
00072 StatusCode MdcTunningSvc::initialize(){
00073 MsgStream log(messageService(), name());
00074 log << MSG::INFO << "========== MdcTunningSvc::initialize() ==========" << endreq;
00075
00076 m_ParBossVer=getenv("BES_RELEASE");
00077
00078 StatusCode sc = Service::initialize();
00079 if( sc.isFailure() ) return sc;
00080
00081 if(m_fromDB)
00082 cout << " MdcTunningSvc read from database. " << endl;
00083 else if(!m_fromDB)
00084 cout << " MdcTunningSvc read from localfile. " << endl;
00085
00086 if(m_fromDB==true){
00087
00088 sc = serviceLocator()->service("DatabaseSvc", m_dbsvc, true);
00089 if (sc .isFailure() ) {
00090 log << MSG::ERROR << " ERROR: unable to find DatabaseSvc " << endreq;
00091 return sc;
00092 }
00093
00094
00095
00096
00097
00100
00105
00106
00107
00108
00109 IIncidentSvc* incsvc;
00110 sc = service("IncidentSvc", incsvc);
00111 int priority = 100;
00112 if( sc.isSuccess() ){
00113 incsvc -> addListener(this, "NewRun", priority);
00114 }
00115 sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00116 if (sc .isFailure() ) {
00117 log << MSG::ERROR << " ERROR: unable to find EventDataSvc " << endreq;
00118 return sc;
00119 }
00120 }
00121
00122 if(m_fromDB!=true){
00123 bool initStat = initTuningConst();
00124
00125 if(!initStat){
00126 cout << "========== MdcTunningSvc::initialize() failure ! ==========" << endl;
00127 return StatusCode::FAILURE;
00128 }
00129 }
00130
00131 return StatusCode::SUCCESS;
00132 }
00133
00134 StatusCode MdcTunningSvc::finalize(){
00135 MsgStream log(messageService(), name());
00136 log << MSG::INFO << "========== MdcTunningSvc::finalize() ==========" << endreq;
00138
00139 return StatusCode::SUCCESS;
00140 }
00141
00142 void MdcTunningSvc::handle(const Incident& inc){
00143 cout << "========== MdcTunningSvc::handle() ==========" << endl;
00144
00145 MsgStream log( messageService(), name() );
00146 log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00147
00148 if ( inc.type() == "NewRun" ){
00149 log << MSG::DEBUG << "NewRun" << endreq;
00150 if(m_fromDB==true) {
00151 cout << " Start getMdcTuningTableInfo. " << endl;
00152 StatusCode sc = getMdcTuningTableInfo();
00153 if(sc.isFailure()) {
00154 cout << " ERROR: can not get MdcTuning data from the database. " << endl;
00155 log << MSG::ERROR << " ERROR: can not get MdcTuning data from the database. " << endreq;
00156 exit(1);
00157 }
00158 }
00159 }
00160 }
00161
00162 bool MdcTunningSvc::initTuningConst(){
00163 std::string FilePath = getenv("MDCTUNNINGSVCROOT");
00164
00165 if(m_effFile==std::string("no path")){
00166 if(!m_fromDB) cout << " ERROR: no mdc tuning eff file, please check the input! " << endl;
00167
00168
00169 return false;
00170 }
00171 bool setMcEffStat = setMcEff(m_effFile);
00172
00173 if(m_resFile==std::string("no path")){
00174 if(!m_fromDB) cout << " ERROR: no mdc tuning res file, please check the input! " << endl;
00175
00176
00177 return false;
00178 }
00179
00180 bool setMcRes3Stat = setMcRes3(m_resFile);
00181
00182 if(!(setMcRes3Stat&&setMcEffStat))
00183 return false;
00184
00185 return true;
00186 }
00187
00188 bool MdcTunningSvc::setMcEff(std::string eff_con){
00189 int i,j;
00190 string line;
00191 double lay,bin,expect,hit;
00192
00193 std::istringstream readMCEff;
00194
00195 if(m_fromDB){
00196 readMCEff.str(eff_con);
00197 }
00198 if(!m_fromDB){
00199 ifstream in(eff_con.c_str());
00200
00201
00202
00203
00204
00205 istreambuf_iterator<char> iter(in) ;
00206 string strCache = string( iter, (istreambuf_iterator<char>()) );
00207 readMCEff.str(strCache);
00208
00209 }
00210
00211
00212 ifstream fin(eff_con.c_str());
00213 if(!m_fromDB)
00214 if(!fin){
00215 cout << " ERROR: the mdc tunning eff file " << m_effFile << " not exist, please check the input! " << endl;
00216 return false;
00217 }
00218
00219 if(!readMCEff.good()){
00220 cout << " ERROR: mdc tuning eff file " << m_effFile << " not exist. " << endl;
00221 return false;
00222 }else{
00223 if(!m_fromDB)
00224 if(fin)
00225 cout << " Open mdc tuning eff file: " << m_effFile << endl;
00226 for(i=0;i<43;i++){
00227 readMCEff>>lay;
00228 getline(readMCEff,line);
00229 for(j=0;j<docaNo;j++){
00230 readMCEff>>bin>>docaEff[i][j]>>expect>>hit;
00231 }
00232 readMCEff>>lay;
00233 getline(readMCEff,line);
00234 for(j=0;j<thetaNo;j++){
00235 readMCEff>>bin>>thetaEff[i][j]>>expect>>hit;
00236 }
00237 readMCEff>>lay;
00238 getline(readMCEff,line);
00239 for(j=0;j<cellNo[i];j++){
00240 readMCEff>>bin>>cellEff[i][j]>>expect>>hit;
00241
00242 }
00243 }
00244 for(i=0;i<43;i++){
00245 readMCEff>>lay;
00246 getline(readMCEff,line);
00247 for(j=0;j<docaNo;j++){
00248 readMCEff>>bin>>docaEff_2[i][j]>>expect>>hit;
00249 }
00250 readMCEff>>lay;
00251 getline(readMCEff,line);
00252 for(j=0;j<thetaNo;j++){
00253 readMCEff>>bin>>thetaEff_2[i][j]>>expect>>hit;
00254
00255 }
00256 readMCEff>>lay;
00257 getline(readMCEff,line);
00258 for(j=0;j<cellNo[i];j++){
00259 readMCEff>>bin>>cellEff_2[i][j]>>expect>>hit;
00260
00261 }
00262 }
00263 }
00264 return true;
00265 }
00266
00267 bool MdcTunningSvc::setMcRes(){
00268
00269
00270 int i,j;
00271 string line;
00272 double lay,bin;
00273 ifstream readMCRes(m_resFile.c_str());
00274 if(!readMCRes.good()){
00275 cout << " ERROR: mdc tuning file: " << m_resFile << " not exist. " << endl;
00276 return false;
00277 }else{
00278 cout << " Open mdc tuning file: " << m_resFile << endl;
00279 for(i=0;i<43;i++){
00280 readMCRes>>lay;
00281 getline(readMCRes,line);
00282 getline(readMCRes,line);
00283 for(j=0;j<docaNo;j++){
00284 readMCRes>>bin>>docaRes[i][j][0][0]>>docaRes[i][j][0][1];
00285 }
00286 readMCRes>>lay;
00287 getline(readMCRes,line);
00288 getline(readMCRes,line);
00289 for(j=0;j<docaNo;j++){
00290 readMCRes>>bin>>docaRes[i][j][1][0]>>docaRes[i][j][1][1];
00291
00292
00293
00294
00295
00296 }
00297 }
00298 }
00299 return true;
00300 }
00301
00302 bool MdcTunningSvc::setMcRes2(std::string res_con){
00303
00304 int i,j;
00305 string line;
00306 double lay,bin;
00307
00308 std::istringstream readMCRes;
00309 if(m_fromDB)
00310 {
00311 readMCRes.str(res_con);
00312 }
00313 if(!m_fromDB)
00314 {
00315 ifstream in(res_con.c_str());
00316 istreambuf_iterator<char> iter(in) ;
00317 string strCache = string( iter, (istreambuf_iterator<char>()) );
00318 readMCRes.str(strCache);
00319 }
00320 if(!readMCRes.good()){
00321 cout << " ERROR: mdc tuning file: " << m_resFile << " not exist. " << endl;
00322 return false;
00323 }else{
00324 cout << " MdcTunningSvc::setMcRes2() Open mdc tuning resfile " << m_resFile << endl;
00325 for(i=0;i<43;i++){
00326 readMCRes>>lay;
00327 getline(readMCRes,line);
00328 getline(readMCRes,line);
00329 for(j=0;j<docaNo;j++){
00330 readMCRes>>bin>>docaF[i][j][0]>>docaMean1[i][j][0]>>docaSigma1[i][j][0]>>docaMean2[i][j][0]>>docaSigma2[i][j][0];
00331 }
00332 readMCRes>>lay;
00333 getline(readMCRes,line);
00334 getline(readMCRes,line);
00335 for(j=0;j<docaNo;j++){
00336 readMCRes>>bin>>docaF[i][j][1]>>docaMean1[i][j][1]>>docaSigma1[i][j][1]>>docaMean2[i][j][1]>>docaSigma2[i][j][1];
00337 }
00338 }
00339 for(i=0;i<43;i++){
00340 readMCRes>>lay;
00341 getline(readMCRes,line);
00342 getline(readMCRes,line);
00343 for(j=0;j<docaNo;j++){
00344 readMCRes>>bin>>docaF_2[i][j][0]>>docaMean1_2[i][j][0]>>docaSigma1_2[i][j][0]>>docaMean2_2[i][j][0]>>docaSigma2_2[i][j][0];
00345 }
00346 readMCRes>>lay;
00347 getline(readMCRes,line);
00348 getline(readMCRes,line);
00349 for(j=0;j<docaNo;j++){
00350 readMCRes>>bin>>docaF_2[i][j][1]>>docaMean1_2[i][j][1]>>docaSigma1_2[i][j][1]>>docaMean2_2[i][j][1]>>docaSigma2_2[i][j][1];
00351 }
00352 }
00353 }
00354 return true;
00355 }
00356
00357 bool MdcTunningSvc::setMcRes3(std::string res_con){
00358
00359 int i,j;
00360 string line;
00361 double lay,bin;
00362
00363 std::istringstream readMCRes;
00364 if(m_fromDB){
00365 readMCRes.str(res_con);
00366 }
00367 if(!m_fromDB){
00368 ifstream in(res_con.c_str());
00369 istreambuf_iterator<char> iter(in) ;
00370 string strCache = string( iter, (istreambuf_iterator<char>()) );
00371 readMCRes.str(strCache);
00372 }
00373
00374 ifstream fin(res_con.c_str());
00375
00376 if(!m_fromDB)
00377 if(!fin){
00378 cout << " ERROR: the mdc tunning res file " << m_resFile << " not exist, please check the input!" << endl;
00379 return false;
00380 }
00381 if(!readMCRes.good()){
00382 cout << " ERROR: the mdc tuning res file: " << m_resFile << " not exist, please check the input! " << endl;
00383 return false;
00384 }else{
00385 if(!m_fromDB)
00386 if(fin)
00387 cout << " MdcTunningSvc::setMcRes3() Open mdc tuning resfile: " << m_resFile << endl;
00388 for(i=0;i<43;i++){
00389 readMCRes>>lay;
00390 getline(readMCRes,line);
00391 getline(readMCRes,line);
00392 for(j=0;j<docaNo;j++){
00393 readMCRes>>bin>>docaF[i][j][0]>>docaMean1[i][j][0]>>docaSigma1[i][j][0]>>docaMean2[i][j][0]>>docaSigma2[i][j][0]>>resLargest[i][j][0]>>resSmallest[i][j][0]>>resRatio[i][j][0];
00394 }
00395 readMCRes>>lay;
00396 getline(readMCRes,line);
00397 getline(readMCRes,line);
00398 for(j=0;j<docaNo;j++){
00399 readMCRes>>bin>>docaF[i][j][1]>>docaMean1[i][j][1]>>docaSigma1[i][j][1]>>docaMean2[i][j][1]>>docaSigma2[i][j][1]>>resLargest[i][j][1]>>resSmallest[i][j][1]>>resRatio[i][j][1];
00400
00401 }
00402 }
00403 for(i=0;i<43;i++){
00404 readMCRes>>lay;
00405 getline(readMCRes,line);
00406 getline(readMCRes,line);
00407 for(j=0;j<docaNo;j++){
00408 readMCRes>>bin>>docaF_2[i][j][0]>>docaMean1_2[i][j][0]>>docaSigma1_2[i][j][0]>>docaMean2_2[i][j][0]>>docaSigma2_2[i][j][0]>>resLargest_2[i][j][0]>>resSmallest_2[i][j][0]>>resRatio_2[i][j][0];
00409 }
00410 readMCRes>>lay;
00411 getline(readMCRes,line);
00412 getline(readMCRes,line);
00413 for(j=0;j<docaNo;j++){
00414
00415 readMCRes>>bin>>docaF_2[i][j][1]>>docaMean1_2[i][j][1]>>docaSigma1_2[i][j][1]>>docaMean2_2[i][j][1]>>docaSigma2_2[i][j][1]>>resLargest_2[i][j][1]>>resSmallest_2[i][j][1]>>resRatio_2[i][j][1];
00416
00417
00418 }
00419 }
00420 }
00421
00422 return true;
00423 }
00424
00425
00426
00427 BesMdcRes* MdcTunningSvc::getMdcRes(){
00428 return m_BesMdcRes;
00429 }
00430
00431 void MdcTunningSvc::setMdcRes(std::string path){
00432 if(m_BesMdcRes) delete m_BesMdcRes;
00433 m_BesMdcRes = new BesMdcRes(path);
00434 }
00435
00436 double MdcTunningSvc::NewSig(int layerId,double driftD){
00437 int bindD = 0;
00438 double mindD = 0.0 ;
00439 double maxdD = 9.0 ;
00440 int maxbin =8.0 ;
00441
00442 if((driftD<mindD)||(driftD>maxdD)){
00443 bindD = maxbin ;
00444 }else {
00445 for(int kk = 0; kk < 9; kk++){
00446 if((driftD>=(double)kk)&&(driftD<(double)(kk+1))){
00447 bindD = kk ; }
00448 }
00449 }
00450
00451 double sigma1 = 0 ;
00452
00453 sigma1 = (m_BesMdcRes -> getD_dD(layerId ,bindD)) ;
00454
00455 return sigma1;
00456 }
00457
00458
00459 double MdcTunningSvc::DeldriftD(int layerId,double driftD){
00460 int bindD = 0;
00461 int maxbin =8 ;
00462 double mindD = 0.0 ;
00463 double maxdD = 9.0 ;
00464
00465 for(int jj =0;jj<9;jj++){
00466 if((driftD<mindD)||(driftD>maxdD)){
00467 bindD = maxbin;
00468 }else if((driftD>=jj)&&(driftD<(jj+1))){
00469 bindD = jj ;
00470 }
00471 }
00472 double y0D = (m_BesMdcRes -> getD_dD(layerId ,bindD)) ;
00473 double y1D = (m_BesMdcRes -> getD_dD(layerId, bindD+1)) ;
00474 double yD = y0D + (y1D-y0D)*(driftD - bindD);
00475 double y0M = (m_BesMdcRes -> getM_dD(layerId ,bindD)) ;
00476 double y1M = (m_BesMdcRes -> getM_dD(layerId ,bindD+1));
00477 double yM = y0M + (y1M-y0M)*(driftD - bindD);
00478 double dely = yD - yM ;
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491 return dely;
00492
00493 }
00494
00495 double MdcTunningSvc::Delcostta(int layerId,double costta){
00496 int binTa = 0;
00497 int maxTa = 15;
00498 double minCos = -0.8 ;
00499 double minCos2 = -0.7 ;
00500 double maxCos = 0.8 ;
00501
00502 for(int ii = 0; ii <16; ii++){
00503 if((costta<minCos)||(costta>maxCos)){
00504 binTa = maxTa;
00505 }else if((costta>=(minCos + ii*0.1))&&(costta<(minCos2 + ii*0.1))){
00506 binTa = ii;
00507 }
00508 }
00509
00510 double y0D = (m_BesMdcRes -> getD_theta(layerId ,binTa));
00511 double y1D = (m_BesMdcRes -> getD_theta(layerId ,binTa+1));
00512 double y0M = (m_BesMdcRes -> getM_theta(layerId,binTa));
00513 double y1M = (m_BesMdcRes -> getM_theta(layerId,binTa+1));
00514
00515 double yD[16],yM[16],Del[16];
00516 for(int ll =0;ll<16;ll++){
00517 yD[ll] = y0D + (y1D - y0D)/0.1*(costta - (minCos + ll*0.1));
00518 yM[ll] = y0M + (y1M - y0D)/0.1*(costta - (minCos + ll*0.1));
00519 Del[ll] = yD[ll] - yM[ll] ;
00520 }
00521
00522 double delTha = 0;
00523
00524 if((binTa>=0)&&(binTa<=5)){
00525 delTha = Del[binTa] * 0.118 ;
00526 }else if((binTa>5)&&(binTa<=10)){
00527 delTha = Del[binTa] * 0.518 ;
00528 }else if((binTa>10)&&(binTa<=15)){
00529 delTha = Del[binTa] * 0.218 ;
00530 }
00531
00532 return delTha ;
00533
00534 }
00535
00536
00537 double MdcTunningSvc::GetEff(int layerId,int cellId,double driftD,double cosTheta,int posFlag){
00538 driftD=fabs(driftD);
00539 if(driftD>12){
00540
00541 driftD=12;
00542 }
00543 if(posFlag==0)driftD *= -1;
00544
00545 if(layerId<0 || layerId>42)std::cout<<" MdcTuningSvc:wrong LayerId "<<layerId<<std::endl;
00546 if(cellId<0 || cellId>=cellNo[layerId])std::cout<<"MdcTuningSvc:wrong cellId "<<cellId<<std::endl;
00547
00548 if(fabs(cosTheta)>1){
00549 std::cout<<"MdcTuningSvc:wrong coseTheta "<<cosTheta<<std::endl;
00550 cosTheta=1;
00551 }
00552 double eff;
00553 int thetaBin=(int)floor((cosTheta+1)*thetaNo/2.);
00554
00555
00556 int docaBin=(int)floor((driftD+12)*docaNo/24.);
00557 if(m_EndcapTuning==0)
00558 eff = docaEff[layerId][docaBin] * thetaEff[layerId][thetaBin] * cellEff[layerId][cellId];
00559 else {
00560 if(fabs(cosTheta)<=0.83)
00561 eff = docaEff[layerId][docaBin] * thetaEff[layerId][thetaBin] * cellEff[layerId][cellId];
00562 else
00563 eff = docaEff_2[layerId][docaBin] * thetaEff_2[layerId][thetaBin] * cellEff_2[layerId][cellId];
00564
00565 }
00566
00567
00568
00569 return eff;
00570 }
00571
00572
00573
00574 double MdcTunningSvc::GetRes(int layerId,int cellId,double driftD,double cosTheta,int posFlag,double entranceAngle,double& mean,double& sigma){
00575
00576 driftD=fabs(driftD);
00577 if(driftD>12){
00578
00579 driftD=12;
00580 }
00581 if(posFlag==0)driftD *= -1;
00582
00583 if(layerId<0 || layerId>42)std::cout<<" MdcTuningSvc:wrong LayerId "<<layerId<<std::endl;
00584 if(cellId<0 || cellId>=cellNo[layerId])std::cout<<"MdcTuningSvc:wrong cellId "<<cellId<<std::endl;
00585
00586 if(fabs(cosTheta)>1){
00587 std::cout<<"MdcTuningSvc:wrong cosTheta "<<cosTheta<<std::endl;
00588 cosTheta=1;
00589 }
00590
00592
00593
00594 int docaBin=(int)floor((driftD+12)*docaNo/24.);
00595 if(entranceAngle<0){
00596 mean=docaRes[layerId][docaBin][0][0];
00597 sigma=docaRes[layerId][docaBin][0][1];
00598 }else{
00599 mean=docaRes[layerId][docaBin][1][0];
00600 sigma=docaRes[layerId][docaBin][1][1];
00601 }
00602
00603
00604
00605
00606 return 1;
00607 }
00608
00609 double MdcTunningSvc::GetRes2(int layerId,int cellId,double driftD,double cosTheta,int posFlag,double entranceAngle,double& f,double& mean1,double& sigma1,double& mean2,double& sigma2){
00610
00611 driftD=fabs(driftD);
00612 if(driftD>12){
00613
00614 driftD=12;
00615 }
00616 if(posFlag==0)driftD *= -1;
00617
00618 if(layerId<0 || layerId>42)std::cout<<" MdcTuningSvc:wrong LayerId "<<layerId<<std::endl;
00619 if(cellId<0 || cellId>=cellNo[layerId])std::cout<<"MdcTuningSvc:wrong cellId "<<cellId<<std::endl;
00620
00621 if(fabs(cosTheta)>1){
00622 std::cout<<"MdcTuningSvc:wrong cosTheta "<<cosTheta<<std::endl;
00623 cosTheta=1;
00624 }
00625
00627
00628
00629 int docaBin=(int)floor((driftD+12)*docaNo/24.);
00630 if(m_EndcapTuning==0) {
00631 if(entranceAngle<0){
00632 f=docaF[layerId][docaBin][0];
00633 mean1=docaMean1[layerId][docaBin][0];
00634 sigma1=docaSigma1[layerId][docaBin][0];
00635 mean2=docaMean2[layerId][docaBin][0];
00636 sigma2=docaSigma2[layerId][docaBin][0];
00637 }else{
00638 f=docaF[layerId][docaBin][1];
00639 mean1=docaMean1[layerId][docaBin][1];
00640 sigma1=docaSigma1[layerId][docaBin][1];
00641 mean2=docaMean2[layerId][docaBin][1];
00642 sigma2=docaSigma2[layerId][docaBin][1];
00643 }
00644 }else {
00645 if(fabs(cosTheta)<=0.83) {
00646 if(entranceAngle<0){
00647 f=docaF[layerId][docaBin][0];
00648 mean1=docaMean1[layerId][docaBin][0];
00649 sigma1=docaSigma1[layerId][docaBin][0];
00650 mean2=docaMean2[layerId][docaBin][0];
00651 sigma2=docaSigma2[layerId][docaBin][0];
00652 }else{
00653 f=docaF[layerId][docaBin][1];
00654 mean1=docaMean1[layerId][docaBin][1];
00655 sigma1=docaSigma1[layerId][docaBin][1];
00656 mean2=docaMean2[layerId][docaBin][1];
00657 sigma2=docaSigma2[layerId][docaBin][1];
00658 }
00659 } else {
00660 if(entranceAngle<0){
00661 f=docaF_2[layerId][docaBin][0];
00662 mean1=docaMean1_2[layerId][docaBin][0];
00663 sigma1=docaSigma1_2[layerId][docaBin][0];
00664 mean2=docaMean2_2[layerId][docaBin][0];
00665 sigma2=docaSigma2_2[layerId][docaBin][0];
00666 }else{
00667 f=docaF_2[layerId][docaBin][1];
00668 mean1=docaMean1_2[layerId][docaBin][1];
00669 sigma1=docaSigma1_2[layerId][docaBin][1];
00670 mean2=docaMean2_2[layerId][docaBin][1];
00671 sigma2=docaSigma2_2[layerId][docaBin][1];
00672 }
00673 }
00674 }
00675
00676
00677
00678
00679 return 1;
00680 }
00681
00682 double MdcTunningSvc::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){
00683
00684 driftD=fabs(driftD);
00685 if(driftD>12){
00686
00687 driftD=12;
00688 }
00689 if(posFlag==0)driftD *= -1;
00690
00691 if(layerId<0 || layerId>42)std::cout<<" MdcTuningSvc:wrong LayerId "<<layerId<<std::endl;
00692 if(cellId<0 || cellId>=cellNo[layerId])std::cout<<"MdcTuningSvc:wrong cellId "<<cellId<<std::endl;
00693
00694 if(fabs(cosTheta)>1){
00695 std::cout<<"MdcTuningSvc:wrong cosTheta "<<cosTheta<<std::endl;
00696 cosTheta=1;
00697 }
00698
00700
00701
00702 int docaBin=(int)floor((driftD+12)*docaNo/24.);
00703 if(m_EndcapTuning==0) {
00704 if(entranceAngle<0){
00705 f=docaF[layerId][docaBin][0];
00706 mean1=docaMean1[layerId][docaBin][0];
00707 sigma1=docaSigma1[layerId][docaBin][0];
00708 mean2=docaMean2[layerId][docaBin][0];
00709 sigma2=docaSigma2[layerId][docaBin][0];
00710 ResLargest=resLargest[layerId][docaBin][0];
00711 ResSmallest=resSmallest[layerId][docaBin][0];
00712 ResRatio=resRatio[layerId][docaBin][0];
00713
00714 }else{
00715 f=docaF[layerId][docaBin][1];
00716 mean1=docaMean1[layerId][docaBin][1];
00717 sigma1=docaSigma1[layerId][docaBin][1];
00718 mean2=docaMean2[layerId][docaBin][1];
00719 sigma2=docaSigma2[layerId][docaBin][1];
00720 ResLargest=resLargest[layerId][docaBin][1];
00721 ResSmallest=resSmallest[layerId][docaBin][1];
00722 ResRatio=resRatio[layerId][docaBin][1];
00723 }
00724 }else {
00725 if(fabs(cosTheta)<=0.83) {
00726 if(entranceAngle<0){
00727 f=docaF[layerId][docaBin][0];
00728 mean1=docaMean1[layerId][docaBin][0];
00729 sigma1=docaSigma1[layerId][docaBin][0];
00730 mean2=docaMean2[layerId][docaBin][0];
00731 sigma2=docaSigma2[layerId][docaBin][0];
00732 ResLargest=resLargest[layerId][docaBin][0];
00733 ResSmallest=resSmallest[layerId][docaBin][0];
00734 ResRatio=resRatio[layerId][docaBin][0];
00735 }else{
00736 f=docaF[layerId][docaBin][1];
00737 mean1=docaMean1[layerId][docaBin][1];
00738 sigma1=docaSigma1[layerId][docaBin][1];
00739 mean2=docaMean2[layerId][docaBin][1];
00740 sigma2=docaSigma2[layerId][docaBin][1];
00741 ResLargest=resLargest[layerId][docaBin][1];
00742 ResSmallest=resSmallest[layerId][docaBin][1];
00743 ResRatio=resRatio[layerId][docaBin][1];
00744 }
00745 } else {
00746 if(entranceAngle<0){
00747 f=docaF_2[layerId][docaBin][0];
00748 mean1=docaMean1_2[layerId][docaBin][0];
00749 sigma1=docaSigma1_2[layerId][docaBin][0];
00750 mean2=docaMean2_2[layerId][docaBin][0];
00751 sigma2=docaSigma2_2[layerId][docaBin][0];
00752 ResLargest=resLargest_2[layerId][docaBin][0];
00753 ResSmallest=resSmallest_2[layerId][docaBin][0];
00754 ResRatio=resRatio_2[layerId][docaBin][0];
00755 }else{
00756 f=docaF_2[layerId][docaBin][1];
00757 mean1=docaMean1_2[layerId][docaBin][1];
00758 sigma1=docaSigma1_2[layerId][docaBin][1];
00759 mean2=docaMean2_2[layerId][docaBin][1];
00760 sigma2=docaSigma2_2[layerId][docaBin][1];
00761 ResLargest=resLargest_2[layerId][docaBin][1];
00762 ResSmallest=resSmallest_2[layerId][docaBin][1];
00763 ResRatio=resRatio_2[layerId][docaBin][1];
00764
00765 }
00766 }
00767 }
00768
00769
00770
00771
00772
00773
00774
00775
00776 return 1;
00777 }
00778
00779 double MdcTunningSvc::ResvEntr(int layerId,double enterA,int ilr ,double driftD){
00780 int bindD =0;
00781 int maxbin = 17;
00782 int iEntr = 0;
00783 int ll = -1;
00784 double mindD = -9.;
00785 double maxdD = 9.;
00786 double sigmaE = 0.0;
00787
00788 if(enterA < 0){
00789 iEntr = 0 ;
00790 }else{ iEntr = 1 ;}
00791
00792 if(ilr == 0 ){
00793 driftD = -1.*driftD;
00794 }else{ driftD = driftD ;}
00795
00796 if( (driftD < mindD) || (driftD > maxdD) ){
00797 bindD = maxbin ;
00798 }else{
00799 for(double dd=-9.;dd<9.;dd++){
00800 ll++;
00801 if( (driftD>= dd ) && (driftD < (dd+1.)) ){
00802 bindD = ll ;
00803 }
00804 }
00805 }
00806
00807 sigmaE = (m_BesMdcRes -> getD_iEntr(layerId,iEntr,bindD) );
00808
00809 return sigmaE;
00810 }
00811
00812 double MdcTunningSvc::DelEtr_Sig(int lay,double enterA,int ilr,double driftD){
00813 int bindD = 0;
00814 int maxbin =17;
00815 int iEntr =0;
00816 int ll = -1;
00817 double mindD = -9.;
00818 double maxdD =9.0;
00820
00821 if(enterA < 0){
00822 iEntr = 0;
00823 }else {iEntr = 1;}
00824
00825 if(ilr == 0 ){
00826 driftD = -1.*driftD;
00827 }else{ driftD = driftD ;}
00828
00829 if( (driftD < mindD) || (driftD > maxdD) ){
00830 bindD = maxbin;
00831 }else {
00832 for(double dd =-9.; dd<9.;dd++){
00833 ll++;
00834 if( (driftD>=dd ) && (driftD < (dd+1.)) ){
00835 bindD = ll;
00836 dD[bindD] = dd;
00837 }
00838 }
00839 }
00840
00841 double y0D = (m_BesMdcRes -> getD_iEntr(lay,iEntr,bindD) );
00842 double y1D = (m_BesMdcRes -> getD_iEntr(lay,iEntr,bindD+1) );
00843 double yD = y0D + (y1D-y0D)*(driftD - dD[bindD]);
00844 double y0M = (m_BesMdcRes -> getM_iEntr(lay,iEntr,bindD) );
00845 double y1M = (m_BesMdcRes -> getM_iEntr(lay,iEntr,bindD+1));
00846 double yM = y0M + (y1M-y0M)*(driftD - dD[bindD]);
00847 double dely = yD - yM ;
00848
00849
00850
00851
00852
00853
00854 return dely;
00855
00856 }
00857
00858 StatusCode MdcTunningSvc::getMdcTuningTableInfo(){
00859 MsgStream log(messageService(), name());
00860 SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00861 int run=eventHeader->runNumber();
00862
00863 log << MSG::INFO << "MdcTuningSvc::getMdcTuningTableInfo() run =" << run << endreq;
00864 if(m_ParBossVer==std::string("unknown"))
00865 cout << "MdcTuningSvc::getMdcTuningTableInfo() : ERROR: there is no ParBossVer " << endl;
00866 else log << MSG::INFO << "MdcTuningSvc::getMdcTuningTableInfo() : ParBossVer = " << m_ParBossVer << endl;
00867
00868 run = fabs(run);
00872 int j=0;
00873 int cnt=0;
00874 DatabaseRecordVector result;
00875 for(int i=0;i<1000;i++){
00876 char stmt1[200];
00877 cnt = i;
00878 int run1 = run+i;
00879 cout << " ==================== " << endl;
00880
00881 sprintf(stmt1,"select MdcRes,MdcEff from MdcTuning where RunFrom <= %d and RunTo >= %d and SftVer = \"%s\"",run1,run1,m_ParBossVer.c_str());
00882 cout << " stmt1: " << stmt1 << endl;
00883
00884 result.clear();
00885 int status = m_dbsvc->query("offlinedb",stmt1,result);
00886 if(status<0){
00887 cout << " ERROR: can not read MdcRes, MdcEff from the MdcTuning table " << endl;
00888 log << MSG::ERROR << " ERROR Read MdcRes, MdcEff from the MdcTuning table " << endreq;
00889 return StatusCode::FAILURE;
00890 }
00891
00892 if(result.size()>=1){
00893
00896
00899
00908 j=1;
00909 break;
00910 }
00911
00912 int run2 = run-i;
00913 char stmt2[200];
00914
00915 sprintf(stmt2,"select MdcRes,MdcEff from MdcTuning where RunFrom <= %d and RunTo >= %d and SftVer = \"%s\"",run2,run2,m_ParBossVer.c_str());
00916
00917 result.clear();
00918 status = m_dbsvc->query("offlinedb",stmt2,result);
00919 if(status<0){
00920 log << MSG::ERROR << " ERROR Read MdcRes, MdcEff.dEdxTuning from the MdcTuning table " << endreq;
00921 return StatusCode::FAILURE;
00922 }
00923
00924
00925 if(result.size()>=1){
00932 j=-1;
00933 break;
00934 }
00935 }
00936
00937 if(cnt!=0&&cnt!=1000) {
00938 log << MSG::INFO << " get MDC tuning data from run " << run + cnt*j << " instead of run " << run<< endreq;
00940 }
00941 cout << " cnt = " << cnt << endl;
00942 if(cnt==1000){
00943 log << MSG::ERROR << " can not read Data from DB" << endreq;
00945 return StatusCode::FAILURE;
00946 }
00947
00949
00951
00952
00955
00956
00957 int row = result.size()-1;
00958 cout << " row = " << row << endl;
00959
00960 if(row<0){
00961 cout << " ERROR: can not read Data from DB, please check MdcTunningSvc Version. " << endl;
00962 return StatusCode::FAILURE;
00963 }
00964
00965 string ggg = result[row]->GetString("MdcEff");
00966 string fff = result[row]->GetString("MdcRes");
00967
00968 log << MSG::DEBUG << " MdcTunning Data: MdcEff: " << ggg << " MdcRes: " << fff << endreq;
00969
00970
00971 bool stRes = setMcRes3(fff);
00972
00973 bool stEff = setMcEff(ggg);
00974
00975 if(!stEff) return StatusCode::FAILURE;
00976 if(!stRes) return StatusCode::FAILURE;
00977 return StatusCode::SUCCESS;
00978 }