/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Mdc/DedxCorrecSvc/DedxCorrecSvc-00-02-52/src/DedxCorrecSvc.cxx

Go to the documentation of this file.
00001 #include "DedxCorrecSvc/DedxCorrecSvc.h"
00002 #include "DedxCorrecSvc/DedxCorrParameters.h"
00003 #include "GaudiKernel/Kernel.h"
00004 #include "GaudiKernel/IInterface.h"
00005 #include "GaudiKernel/StatusCode.h"
00006 //#include "GaudiKernel/ISvcFactory.h"
00007 #include "GaudiKernel/SvcFactory.h"
00008 #include "GaudiKernel/MsgStream.h"
00009 #include "GaudiKernel/SmartDataPtr.h"
00010 #include "GaudiKernel/DataSvc.h"
00011 
00012 #include "GaudiKernel/IIncidentSvc.h"
00013 #include "GaudiKernel/Incident.h"
00014 #include "GaudiKernel/IIncidentListener.h"
00015 
00016 #include "Identifier/Identifier.h"
00017 #include "Identifier/MdcID.h"
00018 #include "CLHEP/Vector/ThreeVector.h"
00019 #include "CalibData/Dedx/DedxCalibData.h"
00020 #include "CalibData/CalibModel.h"
00021 #include <math.h>
00022 #include <vector>
00023 #include <iostream>
00024 #include <fstream>
00025 #include <string>
00026 
00027 #include "TFile.h"
00028 #include "TTree.h"
00029 #include "TLeafD.h"
00030 
00031 using namespace std;
00032 using CLHEP::Hep3Vector;
00033 
00034 //static SvcFactory<DedxCorrecSvc> s_factory;
00035 //const ISvcFactory& DedxCorrecSvcFactory = s_factory;
00036 //double Iner_Stepdoca = (Iner_DocaMax-Iner_DocaMin)/NumSlicesDoca;
00037 //double Out_Stepdoca = (Out_DocaMax-Out_DocaMin)/NumSlicesDoca;
00038 
00039 DedxCorrecSvc::DedxCorrecSvc( const string& name, ISvcLocator* svcloc) :
00040     geosvc(0),Service (name, svcloc) {
00041         declareProperty("Run",m_run=1);
00042         declareProperty("init",m_init=1);
00043         declareProperty("par_flag",m_par_flag=0);
00044         declareProperty("Debug",m_debug=false);
00045         declareProperty("DebugI",m_debug_i=1);
00046         declareProperty("DebugJ",m_debug_j=1);
00047         m_initConstFlg = false;
00048         // cout<<"DedxCorrecSvc::DedxCorrecSvc"<<endl;
00049     }
00050 
00051 DedxCorrecSvc::~DedxCorrecSvc(){
00052 }
00053 
00054 StatusCode DedxCorrecSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
00055     //cout<<"DedxCorrecSvc::queryInterface"<<endl;
00056     if( IID_IDedxCorrecSvc.versionMatch(riid) ){
00057         *ppvInterface = static_cast<IDedxCorrecSvc*> (this);
00058     } else{
00059         return Service::queryInterface(riid, ppvInterface);
00060     }
00061     return StatusCode::SUCCESS;
00062 }
00063 
00064 StatusCode DedxCorrecSvc::initialize(){
00065     // cout<<"DedxCorrecSvc::initialize"<<endl;
00066     MsgStream log(messageService(), name());
00067     log << MSG::INFO << name() << "DedxCorrecSvc::initialize()" << endreq;
00068 
00069     StatusCode sc = Service::initialize();
00070     if( sc.isFailure() ) return sc;
00071 
00072     IIncidentSvc* incsvc;
00073     sc = service("IncidentSvc", incsvc);
00074     int priority = 100;
00075     if( sc.isSuccess() ){
00076         //incsvc -> addListener(this, "BeginEvent", priority);
00077         incsvc -> addListener(this, "NewRun", priority);
00078     }
00079 
00080     StatusCode scgeo = service("MdcGeomSvc", geosvc);
00081     if(scgeo.isFailure() ) {
00082         log << MSG::ERROR << "GeoSvc failing!!!!!!!" << endreq;
00083         return scgeo;
00084     }
00085 
00086     StatusCode scmgn = service ("MagneticFieldSvc",m_pIMF);
00087     if(scmgn!=StatusCode::SUCCESS) { 
00088         log << MSG::ERROR << "Unable to open Magnetic field service"<<endreq;
00089     }
00090 
00091     return StatusCode::SUCCESS;
00092 }
00093 
00094 StatusCode DedxCorrecSvc::finalize(){
00095     // cout<<"DedxCorrecSvc::finalize()"<<endl;
00096     MsgStream log(messageService(), name());
00097     log << MSG::INFO << name() << "DedxCorrecSvc::finalize()" << endreq;
00098     return StatusCode::SUCCESS;
00099 }
00100 
00101 void DedxCorrecSvc::handle(const Incident& inc){
00102     // cout<<"DedxCorrecSvc::handle"<<endl;
00103     MsgStream log( messageService(), name() );
00104     log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00105 
00106     if ( inc.type() == "NewRun" ){
00107         log << MSG::DEBUG << "New Run" << endreq;
00108 
00109         if( ! m_initConstFlg  ) 
00110         {
00111             if( m_init == 0 ) { init_param(); }
00112             else if( m_init ==1 ) { init_param_svc(); }
00113             /* if( ! init_param() ){
00114                log << MSG::ERROR 
00115                << "can not initilize Mdc Calib Constants" << endreq;
00116                }*/
00117         }
00118     } 
00119 }
00120 
00121 double DedxCorrecSvc::f_larcos(double x, int nbin) {
00122   if(nbin!=80) return x;  // temporally for only nbin = 80
00123   double m_absx(fabs(x)); 
00124   double m_charge(x/m_absx);
00125   if(m_absx>0.925 && m_absx<=0.950) return 0.9283*m_charge; // these numbers are from the mean values 
00126   if(m_absx>0.900 && m_absx<=0.925) return 0.9115*m_charge; // of each bin in cos(theta) distribution
00127   if(m_absx>0.875 && m_absx<=0.900) return 0.8877*m_charge;
00128   if(m_absx>0.850 && m_absx<=0.875) return 0.8634*m_charge;
00129   if(m_absx>0.825 && m_absx<=0.850) return 0.8460*m_charge;
00130   if(m_absx>0.800 && m_absx<=0.825) return 0.8089*m_charge;
00131 }
00132 
00133 void
00134 DedxCorrecSvc::init_param_svc() {
00135     // cout<<"DedxCorrecSvc::init_param_svc()"<<endl;
00136     MsgStream log(messageService(), name());
00137     IDataProviderSvc* pCalibDataSvc;
00138     StatusCode sc = service("CalibDataSvc", pCalibDataSvc, true);
00139     if ( !sc.isSuccess() ) {
00140         log << MSG::ERROR 
00141             << "Could not get IDataProviderSvc interface of CalibXmlCnvSvc" 
00142             << endreq;
00143         return ;
00144     } else {
00145         log << MSG::DEBUG 
00146             << "Retrieved IDataProviderSvc interface of CalibXmlCnvSvc" 
00147             << endreq;
00148     }
00149 
00150     Iner_Stepdoca = (Iner_DocaMax-Iner_DocaMin)/NumSlicesDoca;
00151     Out_Stepdoca = (Out_DocaMax-Out_DocaMin)/NumSlicesDoca; 
00152     //cout<<"Out_DocaMax:   "<<Out_DocaMax<<"         Out_DocaMin:  "<<Out_DocaMin<<"        Out_Stepdoca : "<<Out_Stepdoca<<"        NumSlicesDoca : "<<NumSlicesDoca<<endl;
00153     std::string fullPath = "/Calib/DedxCal";
00154 
00155     SmartDataPtr<CalibData::DedxCalibData> test(pCalibDataSvc, fullPath);
00156 
00157     //rung par----------------------------------------------------------
00158     N_run = test->getrunNO();
00159     int run_temp = 0;
00160     cout<<"N_run = "<<N_run<<endl; 
00161     for(int j=0;j<10000;j++)
00162     {
00163         if(j<N_run)
00164         {
00165             for(int i=0;i<4;i++)
00166             {
00167                 m_rung[i][j] = test->getrung(i,j);
00168                 if(i==2 && m_rung[2][j]>run_temp) run_temp = m_rung[2][j];
00169             }
00170         }
00171         else
00172         {
00173             run_temp++;
00174             m_rung[0][j] = 1;
00175             m_rung[1][j] = 550;
00176             m_rung[2][j] = run_temp;
00177             m_rung[3][j] = 26.8;
00178         }
00179     }
00180 
00181     //for(int i=0;i<4;i++) 
00182     //{
00183     //    for(int j=0;j<10000;j++)
00184     //    {
00185     //        std::cout<<"rung["<<i<<"]["<<j<<"]=  "<<m_rung[i][j]<<std::endl;
00186     //    }
00187     //}
00188 
00189     //TFile* fruncalib=new TFile("/home/bes/xcao/besd26/Calib/651_Calib/Psai_cal/rung_calib/first_calib/DedxConst.root");
00190     /*TFile* fruncalib=new TFile("/home/bes/xcao/besd26/Calib/651_Calib/Psai_cal/rung_calib/fourth_calib/temp/DedxConst.root");
00191 
00192       double rungain, runmean, runresol;
00193       int runno;
00194       TTree *rungtree= (TTree*) fruncalib-> Get("runcalib");
00195       rungtree -> SetBranchAddress("rungain", &rungain);
00196       rungtree -> SetBranchAddress("runmean", &runmean);
00197       rungtree -> SetBranchAddress("runno", &runno);
00198       rungtree -> SetBranchAddress("runresol", &runresol);
00199       rungtree -> GetEntry(0);
00200       int NRUN = rungtree -> GetEntries();
00201       N_run = rungtree -> GetEntries();
00202     //cout<<"N_run = "<<N_run<<endl;
00203     //cout<<"NRUN = "<<NRUN<<endl;
00204     for(int runid =0; runid<NRUN; runid++) {
00205     rungtree->GetEntry(runid);
00206     m_rung[0][runid] = rungain;
00207     m_rung[1][runid] = runmean;
00208     m_rung[2][runid] = runno;
00209     m_rung[3][runid] = runresol;
00210     //cout<<"runid :"<<runid<<"     run_no =  "<<m_rung[2][runid]<<"      run_mean = "<<m_rung[1][runid]<<"      run_gain = "<<m_rung[0][runid]<<"      runresol = "<<m_rung[3][runid]<<endl;
00211     }
00212 
00213     //cout<<"run by run const ------------------------------------------------- "<<endl;
00214     for(int i=0;i<4;i++) {
00215     for(int j=0;j<NRUN;j++) {
00216     //std::cout<<"rung["<<i<<"]["<<j<<"]="<<m_rung[i][j]<<endl;
00217     //std::cout<<"\n";
00218     }
00219     }*/
00220 
00221     for(int i=0;i<4;i++) {
00222         for(int j=0;j<43;j++) {
00223             m_ddg[i][j] = test->getddg(i,j);
00224             m_ggs[i][j] = test->getggs(i,j);
00225             m_zdep[i][j] = test->getzdep(i,j);
00226             //      m_enta[i][j] = test->getenta(i,j);
00227             //std::cout<<"ddg["<<i<<"]["<<j<<"]="<<m_ddg[i][j];
00228             //std::cout<<"         ggs["<<i<<"]["<<j<<"]="<<m_ggs[i][j];
00229             //std::cout<<"        zdep["<<i<<"]["<<j<<"]="<<m_zdep[i][j];
00230             //std::cout<<"\n";
00231         }
00232     }
00233 
00234     m_dedx_gain = test->getgain();
00235     std::cout<<"gain="<<m_dedx_gain<<"\n";
00236 
00237     m_dedx_resol = test->getresol();
00238     std::cout<<"resol="<<m_dedx_resol<<"\n";
00239 
00240     for(int i=0;i<43;i++) { 
00241         m_layer_g[i] = test -> getlayerg(i);
00242         if(m_layer_g[i]>2.0 || m_layer_g[i]<0.5) m_layer_g[i] =1;
00243         //std::cout<<"layerg["<<i<<"]="<<m_layer_g[i]<<endl;
00244     }
00245 
00246     /*const int n = 6796;
00247       double wireg[n];
00248       TFile* fwiregcalib=new TFile("/home/bes/xcao/besd26/Calib/651_Calib/Psai_cal/wireg_calib/wiregcalib.root");
00249       TTree *wiregtree= (TTree*)fwiregcalib -> Get("wiregcalib");
00250       wiregtree -> SetBranchAddress("wireg", &wireg);
00251       wiregtree -> GetEntry(0);
00252     //cout<<"wire gain ------------------------------------------------- "<<endl;
00253     for(int i=0; i<n; i++){
00254     m_wire_g[i] = wireg[i];
00255     //std::cout<<"wireg["<<i<<"] = "<<m_wire_g[i]<<"\n";
00256     }*/
00257 
00258     for(int i=0;i<6796;i++) { 
00259         m_wire_g[i] = test -> getwireg(i);
00260         //std::cout<<"wireg["<<i<<"] = "<<m_wire_g[i]<<"\n";
00261     } 
00262 
00263     //int kk=0;
00264     //if(N_run<11397) kk=0;
00265     //else if(N_run<12739) kk=1;
00266     //else if(N_run<14395) kk=2;
00267     //else kk=3;
00268     int kk=3;
00269     for(int i=0;i<6796;i++) {
00270         m_valid[i] = 1;
00271         /*if(i<=483) {m_valid[i] =0;}*/
00272         for(int j=0; j<25; j++){
00273             if( i == dead_wire[kk][j] ) 
00274             {m_valid[i] = 0;
00275                 //cout<<"N_run: "<<N_run<<" kk: "<<kk<<endl;
00276                 //cout<<"m_valid["<<i<<"]:  "<<m_valid[i]<<endl;
00277             }
00278         }
00279         //std::cout<<"valid["<<i<<"]="<<m_valid[i]<<"\n";
00280     } 
00281 
00282     //tempoary way to get costheta constants
00283     cos_k.clear();
00284     cos_b.clear();
00285     //double cos1[80];
00286     if(true){
00287         const int nbin=80;
00288         vector<double> cos;
00289         /*TFile* fcosthecalib=new TFile("/home/bes/xcao/besd26/Calib/651_Calib/Psai_cal/costhe_calib/costhetacalib2.root");
00290           TTree *costhetree= (TTree*)fcosthecalib -> Get("costhetacalib");
00291           TBranch* b_costheta;
00292           double costheta;
00293           costhetree->SetBranchAddress("costheta",&costheta,&b_costheta);
00294         //const int nbin=tc->GetEntries();
00295         //cout<<"costheta gain ------------------------------------------------- "<<endl;
00296         for(int i=0; i<nbin; i++){
00297         costhetree->GetEntry(i);
00298         cos.push_back(costheta);
00299         //cout<<"i : "<<i<<"      costheta gain : "<<costheta<<endl; 
00300         }*/
00301 
00302         for(int i=0; i<nbin; i++){
00303             cos.push_back(test -> get_costheta(i));
00304         }
00305         for(int i=0;i<nbin-1;i++){
00306             double k=0;
00307             double b=0;
00308             if(cos[i]>0.0001){ // not empty bin corresponding to large angle
00309                 if(cos[i+1]>0.0001){
00310                     double x1=-1.00+(0.5+i)*(2.00/nbin);
00311                     double y1=cos[i];
00312                     double x2=-1.00+(0.5+i+1)*(2.00/nbin);
00313                     double y2=cos[i+1];
00314                     // correct around absolute large cos(theta)  angle
00315                     if(fabs(x1)>0.8) x1 = f_larcos(x1, nbin);
00316                     if(fabs(x2)>0.8) x2 = f_larcos(x2, nbin);
00317                     k=(y2-y1)/(x2-x1); // k is the slope
00318                     b=y1-k*x1; 
00319                     //cout << "x1 " << x1 << "  x2 " << x2 << "  y1 " <<  y1 << "  y2 " << y2 << endl;
00320                 }
00321                 else if(i>0){
00322                     if(cos[i-1]>0.0001){
00323                         double x1=-1.00+(0.5+i-1)*(2.00/nbin);
00324                         double y1=cos[i-1];
00325                         double x2=-1.00+(0.5+i)*(2.00/nbin);
00326                         double y2=cos[i];
00327                         // correct around absolute large cos(theta)  angle
00328                         if(fabs(x1)>0.8) x1 = f_larcos(x1, nbin);
00329                         if(fabs(x2)>0.8) x2 = f_larcos(x2, nbin);
00330                         k=(y2-y1)/(x2-x1);
00331                         b=y1-k*x1;
00332                         //cout << "x1 " << x1 << "  x2 " << x2 << "  y1 " <<  y1 << "  y2 " << y2 << endl;
00333                     }
00334                 }
00335             }
00336             else {
00337                 if(i>0){
00338                     if(cos[i-1]>0.0001 && cos[i+1]>0.0001){
00339                         double x1=-1.00+(0.5+i-1)*(2.00/nbin);
00340                         double y1=cos[i-1];
00341                         double x2=-1.00+(0.5+i+1)*(2.00/nbin);
00342                         double y2=cos[i+1];
00343                         // correct around absolute large cos(theta)  angle
00344                         if(fabs(x1)>0.8) x1 = f_larcos(x1, nbin);
00345                         if(fabs(x2)>0.8) x2 = f_larcos(x2, nbin);
00346                         k=(y2-y1)/(x2-x1);
00347                         b=y1-k*x1;
00348                         //cout << "x1 " << x1 << "  x2 " << x2 << "  y1 " <<  y1 << "  y2 " << y2 << endl;
00349                     }
00350                 }
00351             }
00352             //cout<<"i : "<<i<<"      costheta gain : "<<cos[i] << "  k=" << k << "  b=" << b <<endl; 
00353             cos_k.push_back(k);
00354             cos_b.push_back(b);
00355         }
00356     }
00357 
00358     t0_k.clear();
00359     t0_b.clear();
00360     if(true){
00361         //const int nbin=35;
00362         /*vector<double> xbin;
00363           vector<double> ybin;
00364           TFile* ft0calib=new TFile("/ihepbatch/besd13/chunlei/calib/t0calib.root");
00365           TTree *tree_t0=(TTree*)ft0calib->Get("t0calib");
00366           TBranch* b_t0;
00367           TBranch* b_dedx;
00368           double t0, dedx;
00369           tree_t0->SetBranchAddress("t0",&t0,&b_t0);
00370           tree_t0->SetBranchAddress("dedx",&dedx,&b_dedx);
00371           const int nbin=tree_t0->GetEntries();
00372         //cout<<"costheta t0 ------------------------------------------------- "<<endl;
00373         for(int i=0; i<tree_t0->GetEntries(); i++){
00374         tree_t0->GetEntry(i);
00375         xbin.push_back(t0);
00376         ybin.push_back(dedx);
00377         //cout<<"xbin = "<<t0<<"      ybin = "<<dedx<<endl;
00378         }*/
00379 
00380         const int nbin=35;
00381         vector<double> xbin;
00382         vector<double> ybin;
00383         for(int i=0; i<35; i++){
00384             xbin.push_back(test ->get_t0(i));
00385             ybin.push_back(test ->get_dedx(i));
00386             //cout<<"xbin = "<<test ->get_t0(i)<<"      ybin = "<<test ->get_dedx(i)<<endl;
00387         }
00388         for(int i=0;i<nbin-1;i++){
00389             double k=0;
00390             double b=0;
00391             if(ybin[i]>0){
00392                 if(ybin[i+1]>0){
00393                     double x1=xbin[i];
00394                     double y1=ybin[i];
00395                     double x2=xbin[i+1];
00396                     double y2=ybin[i+1];
00397                     k=(y2-y1)/(x2-x1);
00398                     b=(y1*x2-y2*x1)/(x2-x1);
00399                 }
00400                 else if(i>0){
00401                     if(ybin[i-1]>0){
00402                         double x1=xbin[i-1];
00403                         double y1=ybin[i-1];
00404                         double x2=xbin[i];
00405                         double y2=ybin[i];
00406                         k=(y2-y1)/(x2-x1);
00407                         b=(y1*x2-y2*x1)/(x2-x1);
00408                     }
00409                 }
00410             }
00411             else {
00412                 if(i>0){
00413                     if(ybin[i-1]>0 && ybin[i+1]>0){
00414                         double x1=xbin[i-1];
00415                         double y1=ybin[i-1];
00416                         double x2=xbin[i+1];
00417                         double y2=ybin[i+1];
00418                         k=(y2-y1)/(x2-x1);
00419                         b=(y1*x2-y2*x1)/(x2-x1);
00420                     }
00421                 }
00422             }
00423             t0_k.push_back(k);
00424             t0_b.push_back(b);
00425         }
00426     }
00427 
00428     if(true){
00429         /*TFile fconst9("/home/bes/xcao/besd26/Calib/651_Calib/Psai_cal/doca_eangle_calib/check/doca_eangle_kal_doca110.root");
00430           const int n = 1600;
00431           double Iner_gain[n], Iner_chi[n], Iner_hits[n];
00432           double Out_gain[n], Out_chi[n], Out_hits[n];
00433           double Id_doca[n], Ip_eangle[n];
00434 
00435           TTree *tree_docasin= (TTree*)fconst9.Get("docasincalib");
00436           tree_docasin -> SetBranchAddress("Iner_gain", &Iner_gain);
00437           tree_docasin -> SetBranchAddress("Iner_chi",  &Iner_chi);
00438           tree_docasin -> SetBranchAddress("Iner_hits", &Iner_hits);
00439           tree_docasin -> SetBranchAddress("Out_gain",  &Out_gain);
00440           tree_docasin -> SetBranchAddress("Out_chi",   &Out_chi);
00441           tree_docasin -> SetBranchAddress("Out_hits",  &Out_hits);
00442           tree_docasin -> SetBranchAddress("Id_doca",   &Id_doca);
00443           tree_docasin -> SetBranchAddress("Ip_eangle", &Ip_eangle);
00444           tree_docasin -> GetEntry(0);
00445           double docaeangle_gain[n];
00446           double docaeangle_chisq[n];
00447           double docaeangle_entries[n];
00448           double cell[n];
00449         //cout<<"doca eangle  gain ------------------------------------------------- "<<endl;
00450         for(int i=0; i<n; i++){
00451         tree_docasin->GetEntry(i);
00452         cell[i] = i;
00453         docaeangle_gain[i] = Out_gain[i];
00454         docaeangle_chisq[i] = Out_chi[i];
00455         docaeangle_entries[i] = Out_hits[i]; 
00456         int Id_Doca_temp = Id_doca[i];
00457         int Ip_Eangle_temp = Ip_eangle[i];
00458         m_docaeangle[Id_Doca_temp][Ip_Eangle_temp] = Out_gain[i];
00459         //cout<<i<<"       "<<Id_Doca_temp<<"      "<<Ip_Eangle_temp<<"        "<<docaeangle_gain[i]<<endl; 
00460         //m_docaeangle[Id_Doca_temp][Ip_Eangle_temp] = docaeangle_gain[i];   
00461         //cout<<i<<"       "<<Id_doca[i]<<"       "<<Ip_eangle[i]<<"     Entries : "<<docaeangle_entries[i]<<"      Gain value : "<<docaeangle_gain[i]<<"     chisq : "<<docaeangle_chisq[i] <<endl;
00462 
00463         } 
00464         m_docaeangle[38][28]=0.72805;*/ 
00465 
00466         const int n = 1600;
00467         double docaeangle_gain[n];
00468         double docaeangle_chisq[n];
00469         double docaeangle_entries[n];
00470         double cell[n];
00471         for(int i=0; i<n; i++){
00472             cell[i] = i;
00473             docaeangle_gain[i] = test -> get_out_gain(i);
00474             docaeangle_chisq[i] = test -> get_out_chi(i);
00475             docaeangle_entries[i] = test -> get_out_hits(i);
00476             int Id_Doca_temp = test -> get_id_doca(i);
00477             int Ip_Eangle_temp = test -> get_ip_eangle(i);
00478             m_docaeangle[Id_Doca_temp][Ip_Eangle_temp] = docaeangle_gain[i];
00479             if(m_debug && (Id_Doca_temp==m_debug_i) && (Ip_Eangle_temp==m_debug_j)) std::cout<<"docaeangle_gain["<<Id_Doca_temp<<"]["<<Ip_Eangle_temp<<"]="<<m_docaeangle[m_debug_i][m_debug_j] << std::endl;
00480             //cout<<i<<"       "<<Id_Doca_temp<<"      "<<Ip_Eangle_temp<<"        "<<docaeangle_gain[i]<<endl; 
00481         }
00482     }  
00483 
00484 
00485     //get 1d entrance angle constants
00486     int onedsize=test->get_enanglesize();
00487     m_venangle.clear();
00488     for(int i=0; i< onedsize; i++){
00489         m_venangle.push_back(test->get_enangle(i));
00490     }
00491 
00492 
00493     //temporary way to get hadron saturation constants
00494     //for Psai hadron saturation
00495 
00496     //only valide for jpsi data now !!!!!
00497     //have to find ways pass constans for different data 
00498     /*  m_alpha= 1.35630e-02;
00499         m_gamma= -6.78907e-04;
00500         m_delta= 1.18037e-02;
00501         m_power= 1.66268e+00;
00502         m_ratio= 9.94775e-01;*/
00503 
00504     const int hadronNo=test -> get_hadronNo();
00505     // cout<<"@@@hadronNO===="<<hadronNo<<endl;
00506     m_alpha=test -> get_hadron(0);
00507     // cout<<"@@@@m_alpha===="<<m_alpha<<endl;
00508     m_gamma=test -> get_hadron(1);
00509     // cout<<"@@@@m_gamma===="<<m_gamma<<endl;
00510     m_delta=test -> get_hadron(2);
00511     // cout<<"@@@@m_delta====="<<m_delta<<endl;
00512     m_power=test -> get_hadron(3);
00513     // cout<<"@@@@m_power====="<<m_power<<endl;
00514     m_ratio=test -> get_hadron(4);
00515     // cout<<"@@@m_ratio======"<<m_ratio<<endl;
00516 
00517 
00518 
00519     /*
00520        m_delta=0.1;
00521        m_alpha=0.0282;
00522        m_gamma=-0.030;
00523        m_power=1.0;
00524        m_ratio=1.0;
00525     //m_delta=0.1;
00526     //m_alpha=0.0382;
00527     //m_gamma=-0.0438;
00528      */
00529 
00530     //m_initConstFlg =true;
00531 }
00532 
00533 double DedxCorrecSvc::WireGainCorrec(int wireid, double ex) const{
00534     if( m_wire_g[wireid] > 0 ){
00535         double ch_dedx = (ex/m_wire_g[wireid])*m_valid[wireid];
00536         return ch_dedx;
00537     }     
00538     else if( m_wire_g[wireid] == 0 ){
00539         return ex;
00540     }
00541     else return 0;
00542 }
00543 
00544 double
00545 DedxCorrecSvc::SaturCorrec( int layer, double costheta,  double dedx ) const {
00546     //cout<<"DedxCorrecSvc::SaturCorrec"<<endl;
00547     double dedx_ggs;
00548     //cout<<"costheta vaule is = "<<costheta<<endl;
00549     costheta = fabs(costheta);   
00550     if(m_par_flag == 1) {
00551         dedx_ggs = m_ggs[0][layer] + m_ggs[1][layer]*costheta +
00552             m_ggs[2][layer]*pow(costheta,2) + m_ggs[3][layer]*pow(costheta,3);
00553     } else if(m_par_flag == 0) {
00554         dedx_ggs = m_ggs[0][layer] + m_ggs[1][layer]*T1(costheta) +
00555             m_ggs[2][layer]*T2(costheta) + m_ggs[3][layer]*T3(costheta);
00556     }
00557     //cout<<"dedx_ggs is = "<<dedx_ggs<<endl;
00558     dedx_ggs = dedx/dedx_ggs;
00559     if(dedx_ggs>0.0) return dedx_ggs;
00560     else return dedx;
00561 }
00562 
00563 
00564 double 
00565 DedxCorrecSvc::CosthetaCorrec( double costheta,  double dedx ) const {
00566   //cout<<"DedxCorrecSvc::CosthetaCorrec"<<endl;
00567   double dedx_cos;
00568   //cout<<"costheta vaule is = "<<costheta<< " dedx = " << dedx << endl;
00569   if(fabs(costheta)>1.0)  return dedx;
00570 
00571   const int nbin=cos_k.size()+1;
00572   const double step=2.00/nbin;
00573   int n= (int)((costheta+1.00-0.5*step)/step);
00574   if(costheta>(1.00-0.5*step))
00575     n=nbin-2;
00576 
00577   if(costheta>-0.5*step && costheta<=0)
00578     dedx_cos=cos_k[n-1]*costheta+cos_b[n-1];
00579   else if(costheta>0 && costheta<0.5*step)
00580     dedx_cos=cos_k[n+1]*costheta+cos_b[n+1];
00581   else
00582     dedx_cos=cos_k[n]*costheta+cos_b[n];
00583 
00584   //cout << "cos_k[n]="<<cos_k[n] << "  cos_b[n]=" << cos_b[n] << 
00585   //  "  dedx_cos=" << dedx_cos << "  final dedx=" << dedx/dedx_cos << endl;
00586 
00587   //conside physical edge around 0.93
00588   if(nbin==80){ // temporally for only nbin = 80 
00589     if(costheta>0.80 && costheta<0.95){
00590       n = 77;
00591       if(costheta<0.9282) n--;
00592       if(costheta<0.9115) n--;
00593       if(costheta<0.8877) n--;
00594       if(costheta<0.8634) n--;
00595       if(costheta<0.8460) n--;
00596       if(costheta<0.8089) n--;
00597       dedx_cos=cos_k[n]*costheta+cos_b[n];
00598     }
00599     else if(costheta<-0.80 && costheta>-0.95){
00600       n = 2;
00601       if(costheta>-0.9115) n++;
00602       if(costheta>-0.8877) n++;
00603       if(costheta>-0.8634) n++;
00604       if(costheta>-0.8460) n++;
00605       if(costheta>-0.8089) n++;
00606       dedx_cos=cos_k[n]*costheta+cos_b[n];
00607     }
00608   }
00609 
00610   if(dedx_cos>0){
00611     dedx_cos = dedx/dedx_cos;
00612     return dedx_cos;
00613   }
00614   else return dedx;
00615 }
00616 
00617 
00618 double
00619 DedxCorrecSvc::T0Correc( double t0,  double dedx ) const {
00620     //  cout<<"DedxCorrecSvc::T0Correc"<<endl;
00621     double dedx_t0;
00622     const int nbin=t0_k.size()+1;
00623     if(nbin!=35)
00624       cout<<"there should be 35 bins for t0 correction, check it!"<<endl;
00625 
00626     int n=0;
00627     if(t0<575)
00628       n=0;
00629     else if(t0<610)
00630       n=1;
00631     else if(t0>=610 && t0<1190){
00632         n=(int)( (t0-610.0)/20.0 ) + 2;
00633     }
00634     else if(t0>=1190 && t0<1225)
00635       n=31;
00636     else if(t0>=1225 && t0<1275)
00637       n=32;
00638     else if(t0>=1275)
00639       n=33;
00640 
00641     dedx_t0=t0_k[n]*t0+t0_b[n];
00642 
00643     if(dedx_t0>0){
00644         dedx_t0 = dedx/dedx_t0;
00645         return dedx_t0;
00646     }
00647     else return dedx;
00648 }
00649 
00650 double
00651 DedxCorrecSvc::HadronCorrec( double costheta,  double dedx ) const {
00652     //  cout<<"DedxCorrecSvc::HadronCorrec"<<endl;
00653     //constant 1.00 in the following function is the mean dedx of normalized electrons.
00654   dedx=dedx/550.00;
00655   return D2I(costheta, I2D(costheta,1.00)/1.00*dedx)*550;
00656 }
00657 double
00658 DedxCorrecSvc::LayerGainCorrec( int layid, double dedx ) const {
00659     //  cout<<"DedxCorrecSvc::LayerGainCorrec"<<endl;
00660     if( m_layer_g[layid] > 0 ){
00661         double ch_dedx = dedx/m_layer_g[layid];
00662         return ch_dedx;
00663     }     
00664     else if( m_layer_g[layid] == 0 ){
00665         return dedx;
00666     }
00667     else return 0;
00668 }
00669 
00670 
00671 double 
00672 DedxCorrecSvc::RungCorrec( int runNO, double dedx ) const{
00673     //  cout<<"DedxCorrecSvc::RungCorrec"<<endl;
00674     double dedx_rung;  
00675     int run_ture =0;
00676     //cout<<"N_run : "<<N_run<<"       runNO : "<<runNO<<endl;
00677 
00678     for(int j=0;j<10000;j++) {
00679         if(runNO == m_rung[2][j]) {
00680             dedx_rung = dedx/m_rung[0][j];
00681             run_ture =1;
00682             return dedx_rung;     
00683         }    
00684     }
00685     if(run_ture ==0)
00686     {
00687         cout<<"Warning!!!  in DedxCorrecSvc::RungCorrec(): no rungain to "<<runNO<<endl;
00688         exit(1);
00689     }
00690 }
00691 
00692 
00693 double
00694 DedxCorrecSvc::DriftDistCorrec( int layer, double dd,  double dedx ) const {
00695     //  cout<<"DedxCorrecSvc::DriftDistCorrec"<<endl;
00696     double dedx_ddg;
00697     //cout<<"m_par_flag = "<<m_par_flag<<endl;
00698     //cout<<"dd vaule is = "<<dd<<endl;
00699     dd = fabs(dd); 
00700     if(m_par_flag == 1) {
00701         dedx_ddg = m_ddg[0][layer] + m_ddg[1][layer]*dd + 
00702             m_ddg[2][layer]*dd*dd + m_ddg[3][layer]*pow(dd,3);
00703     } else if(m_par_flag == 0) {
00704         dedx_ddg = m_ddg[0][layer] + m_ddg[1][layer]*T1(dd) + 
00705             m_ddg[2][layer]*T2(dd) + m_ddg[3][layer]*T3(dd);
00706     }
00707     //cout<<"dedx_ddg is = "<<dedx_ddg<<endl;
00708     dedx_ddg = dedx/dedx_ddg;
00709     if (dedx_ddg>0.0)  return dedx_ddg;
00710     else return dedx;
00711 }
00712 
00713 
00714 double
00715 DedxCorrecSvc::EntaCorrec( int layer, double eangle,  double dedx ) const {
00716     // cout<<"DedxCorrecSvc::EntaCorrec"<<endl;
00717 //    double dedx_enta;
00718 //    if(m_par_flag == 1) {
00719 //        dedx_enta = m_enta[0][layer] + m_enta[1][layer]*sinenta + 
00720 //            m_enta[2][layer]*sinenta*sinenta + m_enta[3][layer]*pow(sinenta,3);
00721 //    } else if(m_par_flag == 0) {
00722 //        dedx_enta = m_enta[0][layer] + m_enta[1][layer]*T1(sinenta) + 
00723 //            m_enta[2][layer]*T2(sinenta) + m_enta[3][layer]*T3(sinenta);
00724 //    }
00725 //    dedx_enta = dedx/dedx_enta;
00726 //    if (dedx_enta>0.0)  return dedx_enta;
00727 //    else return dedx;
00728 
00729     if(eangle>-0.25 && eangle<0.25){
00730         double stepsize= 0.5/m_venangle.size();
00731         int nsize= m_venangle.size();
00732         double slope=0;
00733         double offset=1;
00734         double y1=0,y2=0,x1=0,x2=0;
00735 
00736         if(eangle>=-0.25+0.5*stepsize && eangle<=0.25-0.5*stepsize){
00737             int bin = (int)( (eangle-(-0.25+0.5*stepsize))/stepsize);
00738             y1=m_venangle[bin];
00739             x1=-0.25+0.5*stepsize+bin*stepsize;
00740             y2=m_venangle[bin+1];
00741             x2=-0.25+1.5*stepsize+bin*stepsize;
00742         }
00743         else if(eangle<=-0.25+0.5*stepsize){
00744             y1=m_venangle[0];
00745             x1=-0.25+0.5*stepsize;
00746             y2=m_venangle[1];
00747             x2=-0.25+1.5*stepsize;
00748         }
00749         else if( eangle>=0.25-0.5*stepsize){
00750             y1=m_venangle[nsize-2];
00751             x1=0.25-1.5*stepsize;
00752             y2=m_venangle[nsize-1];
00753             x2=0.25-0.5*stepsize;
00754         }
00755         double kcof= (y2-y1)/(x2-x1);
00756         double bcof= y1-kcof*x1; 
00757         double ratio = kcof*eangle+bcof;
00758         dedx=dedx/ratio;
00759     }
00760 
00761     return dedx;
00762 }
00763 
00764 
00765 double
00766 DedxCorrecSvc::ZdepCorrec( int layer, double z,  double dedx ) const {
00767     // cout<<"DedxCorrecSvc::ZdepCorrec"<<endl;
00768     double dedx_zdep;
00769     if(m_par_flag == 1) {
00770         dedx_zdep = m_zdep[0][layer] + m_zdep[1][layer]*z + 
00771             m_zdep[2][layer]*z*z + m_zdep[3][layer]*pow(z,3);
00772     } else if(m_par_flag == 0) {
00773         dedx_zdep = m_zdep[0][layer] + m_zdep[1][layer]*T1(z) + 
00774             m_zdep[2][layer]*T2(z) + m_zdep[3][layer]*T3(z);
00775     }
00776     dedx_zdep = dedx/dedx_zdep;
00777     if (dedx_zdep>0.0)  return dedx_zdep;
00778     else return dedx;
00779 
00780     //return dedx;
00781 }
00782 
00783 
00784 double
00785 DedxCorrecSvc::DocaSinCorrec( int layer,double doca, double eangle, double dedx ) const {
00786   if(m_debug) cout<<"DedxCorrecSvc::DocaSinCorrec"<<endl;
00787   // cout<<"doca = "<<doca<<"       eangle = "<<eangle<<endl;
00788 
00789   if(eangle>0.25) eangle = eangle -0.5;
00790   else if(eangle < -0.25) eangle = eangle +0.5;
00791   int iDoca;
00792   int iEAng = 0;
00793   doca = doca/doca_norm[layer];
00794   iDoca =(Int_t)floor((doca-Out_DocaMin)/Out_Stepdoca);
00795   if(doca<Out_DocaMin) iDoca=0;
00796   if(doca>Out_DocaMax) iDoca=NumSlicesDoca-1;
00797   if(iDoca >=(Int_t)floor((Out_DocaMax-Out_DocaMin)/Out_Stepdoca) ) 
00798     iDoca = (Int_t)floor((Out_DocaMax-Out_DocaMin)/Out_Stepdoca)-1;
00799   if(iDoca<0) iDoca = 0;
00800   if(m_debug) cout<<"iDoca : "<<iDoca<<"      doca : "<<doca<<"       Out_DocaMin : "<<Out_DocaMin<<"      Out_Stepdoca : "<<Out_Stepdoca<<endl;   
00801 
00802   //iEAng = (Int_t)floor((eangle-Phi_Min)/IEangle_step);
00803   for(int i =0; i<14; i++){
00804     //iEAng =0;
00805     if(eangle <Eangle_value_cut[i] || eangle > Eangle_value_cut[i+1]) continue;
00806     else if(eangle>= Eangle_value_cut[i] && eangle < Eangle_value_cut[i+1]) {
00807       for(int k =0; k<i; k++){
00808         iEAng+= Eangle_cut_bin[k];
00809       }
00810       double eangle_bin_cut_step = (Eangle_value_cut[i+1] - Eangle_value_cut[i])/Eangle_cut_bin[i];
00811       int temp_bin = int((eangle-Eangle_value_cut[i])/eangle_bin_cut_step);
00812       iEAng+= temp_bin;
00813     }
00814   }
00815   //cout<<iDoca <<"         "<<iEAng<<endl;
00816   if(m_docaeangle[iDoca][iEAng]>0) {
00817     if(m_debug && (iDoca==m_debug_i) && (iEAng==m_debug_j)) 
00818       cout << "doca: " << doca << "  eang"  << eangle << "  dedx before: " << dedx << endl;
00819     dedx = dedx/m_docaeangle[iDoca][iEAng];
00820     if(m_debug && (iDoca==m_debug_i) && (iEAng==m_debug_j))
00821       cout << "gain: " << m_docaeangle[iDoca][iEAng] << "  dedx after: " << dedx << endl;
00822   } 
00823   return dedx;
00824 }
00825 
00826 
00827 double
00828 DedxCorrecSvc::DipAngCorrec(int layer, double costheta, double dedx) const {
00829 }
00830 
00831 double
00832 DedxCorrecSvc::GlobalCorrec( double dedx) const{
00833     if( m_mdcg_flag == 0 ) return dedx;
00834     double calib_ex = dedx;
00835     if( m_dedx_gain > 0 ) calib_ex /= m_dedx_gain;
00836     return calib_ex;
00837 }
00838 
00839 
00840 
00841 double 
00842 DedxCorrecSvc::CellCorrec( int ser, double adc, double dd, double sinenta,
00843             double z, double costheta ) const {
00844 
00845     if( m_rung_flag == 0 && m_wireg_flag == 0 && m_ddg_flag == 0 
00846                 && m_layerg_flag == 0 && m_zdep_flag == 0 && 
00847                 m_ggs_flag == 0) return adc;
00848     adc = m_valid[ser]*m_wire_g[ser]*adc;
00849     //  int lyr = Wire2Lyr(ser); 
00850     int lyr = ser; 
00851     double ex = adc;
00852     double correct1_ex , correct2_ex, correct3_ex, correct4_ex, correct5_ex;
00853 
00854     if( m_ggs_flag) {
00855         correct1_ex =  SaturCorrec( lyr, costheta, adc );
00856         ex = correct1_ex;
00857     } else {
00858         correct1_ex = adc;
00859     }
00860 
00861     if( m_ddg_flag)  {
00862         correct2_ex = DriftDistCorrec( lyr, dd, correct1_ex );
00863         ex = correct2_ex;
00864     } else {
00865         correct2_ex =correct1_ex;
00866     }
00867     if( m_enta_flag)  {
00868         correct3_ex = DriftDistCorrec( lyr, sinenta, correct2_ex );
00869         ex = correct3_ex;
00870     } else {
00871         correct3_ex =correct2_ex;
00872     }
00873 
00874     if( m_zdep_flag) {
00875         correct4_ex = ZdepCorrec( lyr, z, correct3_ex );
00876         ex = correct4_ex;
00877     } else {
00878         correct4_ex = correct3_ex; 
00879     }
00880 
00881     if( m_layerg_flag)  {
00882         correct5_ex = LayerGainCorrec( lyr, correct4_ex );
00883         ex = correct5_ex;
00884     } else {
00885         correct5_ex = correct4_ex;
00886     }
00887     return ex;
00888 
00889 }
00890 
00891 double
00892 DedxCorrecSvc::LayerCorrec( int layer, double z, double costheta, double ex ) const{
00893     //cout<<"DedxCorrecSvc::LayerCorrec"<<endl;
00894 
00895     if( m_zdep_flag == 0 && m_ggs_flag == 0 ) return ex;
00896 
00897     double calib_ex = ZdepCorrec( layer, z, ex );
00898     if( m_ggs_flag != 0 ) calib_ex =  SaturCorrec( layer, costheta, calib_ex );
00899     return calib_ex;
00900 
00901 }
00902 
00903 double 
00904 DedxCorrecSvc::TrkCorrec( double costheta, double dedx ) const{
00905     //  cout<<"DedxCorrecSvc::TrkCorrec"<<endl;
00906     if( m_mdcg_flag == 0 ) return dedx;
00908     double calib_ex = dedx;
00909 
00910     double run_const = m_dedx_gain;
00911     if( run_const > 0 && m_mdcg_flag != 0 ) calib_ex /= run_const;
00912 
00913     return calib_ex;
00914 
00915 }
00916 
00917 
00918 double 
00919 DedxCorrecSvc::StandardCorrec( int runFlag, int ntpFlag, int runNO, double pathl, int wid, int layid, double adc, double dd, double eangle, double z, double costheta ) const {
00920     // cout<<"DedxCorrecSvc::StandardCorrec"<<endl;
00921     //int layid =  MdcID::layer(mdcid);
00922     //int localwid = MdcID::wire(mdcid);
00923     //int w0id = geosvc->Layer(layid)->Wirst();
00924     //int wid = w0id + localwid;
00925     //double pathl = PathL(ntpFlag, hel, layid, localwid, z); 
00927     double ex = adc;
00928     if( runNO<0 ) return ex;
00929     ex = ex*1.5/pathl;
00930     //double ex = adc/pathl;
00931     //if( runNO<0 ) return ex;
00932     if( ntpFlag ==0) return ex;
00933     //double ex = adc/pathl;
00934     if( m_rung_flag == 0 && m_wireg_flag == 0 &&   m_dcasin_flag==0 && m_ddg_flag == 0 
00935                 && m_layerg_flag == 0 && m_zdep_flag == 0 && 
00936                 m_ggs_flag == 0) return ex;
00937 
00938     if(m_rung_flag) {
00939         ex = RungCorrec( runNO, ex );
00940     }
00941 
00942     if( m_wireg_flag)  {
00943         ex = WireGainCorrec(wid, ex);
00944     }
00945 
00946     if( m_dcasin_flag) {
00947       if(runFlag<3) {
00948         ex = DriftDistCorrec( layid, dd, ex );
00949       }
00950       else{ ex = DocaSinCorrec(layid, dd, eangle, ex);}
00951     }
00952 
00953     if(m_enta_flag && runFlag>=3){
00954       ex = EntaCorrec(layid, eangle, ex);
00955     }
00956 
00957     // ddg is not use anymore, it's replaced by DocaSin
00958     if(m_ddg_flag)  {
00959         ex = DriftDistCorrec( layid, dd, ex );
00960     }
00961 
00962     if(m_ggs_flag) {
00963       if(runFlag <3 ){
00964         ex =  SaturCorrec( layid, costheta, ex );
00965       }
00966       else{ ex = CosthetaCorrec( costheta, ex );}
00967       // Staur is OLD, Costheta is NEW.
00968     }
00969 
00970     if( m_sat_flag) {
00971         ex =  HadronCorrec( costheta, ex );
00972     }  
00973 
00974     if( m_zdep_flag) {
00975         ex = ZdepCorrec( layid, z, ex );
00976     } 
00977 
00978     if( m_layerg_flag)  {
00979         ex = LayerGainCorrec( layid, ex );
00980     } 
00981 
00982     if( m_dip_flag){
00983         ex = DipAngCorrec(layid, costheta, ex);
00984     } 
00985 
00986     if( m_mdcg_flag)  {
00987         ex = GlobalCorrec( ex );
00988     } 
00989     return ex;
00990 }
00991 
00992 double 
00993 DedxCorrecSvc::StandardHitCorrec(int calib_rec_Flag, int runFlag, int ntpFlag, int runNO, double pathl,  int wid, int layid, double adc, double dd, double eangle, double costheta ) const {
00994   if(m_debug) cout<<"DedxCorrecSvc::StandardHitCorrec"<<endl;
00995   //int layid =  MdcID::layer(mdcid);
00996   //int localwid = MdcID::wire(mdcid);
00997   //int w0id = geosvc->Layer(layid)->Wirst();
00998   //int wid = w0id + localwid;
00999   //double pathl = PathL(ntpFlag, hel, layid, localwid, z); 
01000   //cout<<"DedxCorrecSvc::StandardHitCorrec pathl= "<<pathl<<endl;
01001   //pathl= PathLCosmic(hel, layid, localwid, z, sigmaz );
01002   double ex = adc;
01003   if( runNO<0 ) return ex;
01004   ex = ex*1.5/pathl;
01005   //if( runNO<0 ) return ex;
01006   //double ex = adc/pathl;
01007   if( ntpFlag ==0) return ex;
01008   //if(ntpFlag>0) cout<<"dE/dx value after path correction: "<<ex<<endl; 
01009   //double ex = adc/pathl;
01010   //cout<<m_rung_flag << "    "<<m_wireg_flag<<"    "<<m_ddg_flag<<"     "<<m_ggs_flag<<endl;
01011   if( m_rung_flag == 0 && m_wireg_flag == 0 && m_ddg_flag == 0 
01012       && m_layerg_flag == 0 && m_zdep_flag == 0 &&   m_dcasin_flag==0 
01013       && m_ggs_flag == 0 && m_enta_flag==0) return ex;
01014 
01015   if(m_rung_flag) {
01016     ex = RungCorrec( runNO, ex );
01017   }
01018   //if(ntpFlag>0) cout<<"dE/dx value after run by run correction: "<<ex<<endl;
01019 
01020   if( m_wireg_flag)  {
01021     ex = WireGainCorrec(wid, ex);
01022   }
01023   //if(ntpFlag>0) cout<<"dE/dx value after wire gain correction: "<<ex<<endl;
01024 
01025   if( m_dcasin_flag) {
01026     if(runFlag<3){
01027       ex = DriftDistCorrec( layid, dd, ex );
01028     }
01029     else{ 
01030       //cout<<layid<<"        "<<dd<<"       "<<eangle<<"         "<<ex<<endl;    
01031       ex = DocaSinCorrec(layid, dd, eangle, ex);
01032     }
01033   } 
01034 
01035   // 1D entrance angle correction
01036   if(m_enta_flag && runFlag>=3){
01037     ex = EntaCorrec(layid, eangle, ex);
01038   }
01039 
01040   // ddg is not used anymore
01041   if( m_ddg_flag)  {
01042     ex = DriftDistCorrec( layid, dd, ex );
01043   }
01044   //if(ntpFlag>0) cout<<"dE/dx value after ddg correction: "<<ex<<endl;
01045 
01046   if(m_ggs_flag) {
01047     if(runFlag <3 ){
01048       ex =  SaturCorrec( layid, costheta, ex );
01049     }
01050     else{ ex = ex;} // do not do the cos(theta) correction at hit level
01051   } 
01052   //if(ntpFlag>0) cout<<"dE/dx value after costheta correction: "<<ex<<endl; 
01053   return ex;
01054 }
01055 
01056 
01057 double 
01058 DedxCorrecSvc::StandardTrackCorrec(int calib_rec_Flag, int runFlag, int ntpFlag, int runNO, double ex, double costheta, double t0 ) const {
01059   if(m_debug)  cout<<"DedxCorrecSvc::StandardTrackCorrec"<<endl;
01060   if( runNO<0 ) return ex;
01061   if( ntpFlag ==0) return ex;
01062   if( runFlag <3) return ex;
01063   if( calib_rec_Flag ==1){
01064     ex =  CosthetaCorrec( costheta, ex );   
01065     if(m_t0_flag) ex= T0Correc(t0, ex);
01066     return ex;                        
01067   }
01068 
01069   //if(ntpFlag>0) cout<<"trcak value before costheta correction: "<<ex<<endl;
01070   if( m_ggs_flag) {
01071     ex =  CosthetaCorrec( costheta, ex );
01072   }  
01073   //if(ntpFlag>0) cout<<"trcak value after costheta correction: "<<ex<<endl;
01074   if(m_t0_flag){
01075     if(runFlag==3) {ex= T0Correc(t0, ex);}
01076     else if(runFlag>3) {ex=ex;}
01077   }
01078   //if(ntpFlag>0) cout<<"trcak value after all correction: "<<ex<<endl;
01079   if( m_sat_flag) {
01080     ex =  HadronCorrec( costheta, ex );
01081   }  
01082 
01083   if(m_rung_flag && calib_rec_Flag ==2){
01084     double rungain_temp =RungCorrec( runNO, ex )/ex;
01085     ex = ex/rungain_temp;
01086   }  
01087 
01088   //if(ntpFlag>0) cout<<"trcak value no run gain correction: "<<ex<<endl;
01089   return ex;
01090 
01091 }
01092 
01093 
01094 
01095 void
01096 DedxCorrecSvc::init_param() {
01097 
01098     //cout<<"DedxCorrecSvc::init_param()"<<endl;
01099 
01100     for( int i = 0; i<6796 ; i++) {
01101         m_valid[i] = 1.0;
01102         m_wire_g[i] = 1.0;
01103     }
01104     m_dedx_gain = 1.0;
01105     m_dedx_resol = 1.0;
01106     for(int j = 0; j<100; j++){
01107         m_rung[0][j] =1;
01108     }
01109     for(int j = 0; j<43; j++) {
01110         m_layer_g[j] = 1.0;
01111         m_ggs[0][j] = 1.0;
01112         m_ddg[0][j] = 1.0;
01113         m_enta[0][j] = 1.0;
01114         m_zdep[0][j] = 1.0;
01115         for(int k = 1; k<4; k++ ) {
01116             m_ggs[k][j] = 0.0;
01117             m_ddg[k][j] = 0.0;
01118             m_enta[k][j] = 0.0;
01119             m_zdep[k][j] = 0.0;
01120         }
01121     }
01122 
01123     std::cout<<"DedxCorrecSvc::init_param()!!!"<<std::endl;
01124     std::cout<<"hello,init_param"<<endl;
01125     m_initConstFlg =true;
01126 
01127 }
01128 
01129 
01130 void
01131 DedxCorrecSvc::set_flag( int calib_F ) {
01132   // cout<<"DedxCorrecSvc::set_flag"<<endl;
01133   cout<<"calib_F is = "<<calib_F<<endl;
01134   if(calib_F<0){ m_enta_flag = 0; calib_F = abs(calib_F); }
01135   else m_enta_flag = 1;
01136   m_rung_flag    = ( calib_F &  1 )? 1 : 0;
01137   m_wireg_flag   = ( calib_F &  2 )? 1 : 0;
01138   m_dcasin_flag  = ( calib_F &  4 )? 1 : 0;
01139   m_ggs_flag     = ( calib_F &  8 )? 1 : 0;
01140   m_ddg_flag = 0;
01141   //m_ddg_flag     = ( calib_F &  8 )? 1 : 0;
01142   m_t0_flag      = ( calib_F & 16 )? 1 : 0;
01143   m_sat_flag     = ( calib_F & 32 )? 1 : 0;
01144   m_layerg_flag  = ( calib_F & 64 )? 1 : 0;
01145   //m_dcasin_flag  = ( calib_F & 128 )? 1 : 0;
01146   m_dip_flag     = ( calib_F & 256 )? 1 : 0;
01147   m_mdcg_flag    = ( calib_F & 512 )? 1 : 0;
01148 }
01149 
01150 
01151 
01152 //funcation to calculate the path pength of Cosmic data in the layer 
01153 /*double
01154   DedxCorrecSvc::PathLCosmic(const Dedx_Helix& hel, int layer, int cellid, double z,double sigmaz ) const {
01156 HepPoint3D piv(hel.pivot()); 
01157 double dr  = hel.a()[0];
01158 double phi0  = hel.a()[1];
01159 double tanl  = hel.a()[4];
01160 double dz = hel.a()[3];  
01161 
01162 double dr  = -19.1901;
01163 double phi0  = 3.07309;
01164 double tanl  = -0.466654;
01165 double dz = 64.8542;
01166 
01167 double csf0 = cos(phi0);
01168 double snf0 = sin(phi0);
01169 double x_c = dr*csf0;
01170 double y_c = dr*snf0;
01171 double z_c = hel.a()[3];
01172 
01175 
01176 double m_crio[2];
01177 double m_zb, m_zf, Calpha;
01178 //  double sintheta = sin(M_PI_2-atan(hel.a()[4]));
01179 double sintheta = sin(M_PI_2-atan(tanl));
01180 // retrieve Mdc  geometry information
01181 double rcsiz1= geosvc->Layer(layer)->RCSiz1();
01182 double rcsiz2= geosvc->Layer(layer)->RCSiz2();
01183 double rlay= geosvc->Layer(layer)->Radius();
01184 int ncell= geosvc->Layer(layer)->NCell();
01185 double phioffset= geosvc->Layer(layer)->Offset();
01186 float shift= geosvc->Layer(layer)->Shift();
01187 double slant= geosvc->Layer(layer)->Slant();
01188 double length = geosvc->Layer(layer)->Length();
01189 int type = geosvc->Layer(layer)->Sup()->Type();
01190 //  shift = shift*type;
01191 
01192 //conversion of the units(mm=>cm)
01193 length = 0.1*length;
01194 rcsiz1 = 0.1*rcsiz1;
01195 rcsiz2 = 0.1*rcsiz2;
01196 rlay = 0.1*rlay;
01197 m_zb = 0.5*length;
01198 m_zf = -0.5*length;
01199 m_crio[0] = rlay - rcsiz1;
01200 m_crio[1] = rlay + rcsiz2;
01201 
01202 if(z == 0)
01203 { if(rlay<= fabs(dr)) rlay = rlay + rcsiz2;
01204 if(rlay<fabs(dr)) return -1;
01205 double t_digi = sqrt(rlay*rlay - dr*dr);
01206 double x_t_digi = x_c - t_digi*snf0;
01207 double y_t_digi = y_c + t_digi*csf0;
01208 double z_t_digi = z_c + t_digi*tanl; 
01209 double x__t_digi = x_c + t_digi*snf0;
01210 double y__t_digi = y_c - t_digi*csf0;
01211 double z__t_digi = z_c - t_digi*tanl; 
01212 double phi_t_digi = atan2(y_t_digi,x_t_digi);
01213 double phi__t_digi = atan2(y__t_digi,x__t_digi);
01214 phi_t_digi = fmod( phi_t_digi+4*M_PI,2*M_PI );
01215 phi__t_digi = fmod( phi__t_digi+4*M_PI,2*M_PI );
01216 double phibin_digi = 2.0*M_PI/ncell;
01217 double phi_cellid_digi = phioffset + shift*phibin_digi*0.5 + cellid *phibin_digi;
01218 if(fabs(phi_cellid_digi - phi_t_digi)<fabs(phi_cellid_digi - phi__t_digi))
01219 z = z_t_digi;
01220 else if (fabs(phi_cellid_digi - phi_t_digi)>fabs(phi_cellid_digi - phi__t_digi))
01221 z = z__t_digi;
01222 else  z = z_t_digi;
01223 }
01224 
01225 int sign = 1;  ///assumed the first half circle
01226 int epflag[2];
01227 Hep3Vector iocand;
01228 Hep3Vector cell_IO[2];
01229 double dphi, downin;
01230 Hep3Vector zvector;
01231 if( type ) {
01232     downin = (z*z-m_zb*m_zb)*pow(tan(slant),2);
01233     m_crio[0] = sqrt(m_crio[0]*m_crio[0]+downin);
01234     m_crio[1] = sqrt(m_crio[1]*m_crio[1]+downin);
01235 }
01236 
01237 // calculate t value
01238 double t[2];
01239 if(m_crio[1]<fabs(dr)) return -1;
01240 else if(m_crio[0]<fabs(dr)) {
01241     t[0] = sqrt(m_crio[1]*m_crio[1] - dr*dr);
01242     t[1] = -t[0];
01243 }
01244 else{
01245     for( int i = 0; i < 2; i++ ) {
01246         if(m_crio[i]<fabs(dr)) return -1;
01247         t[i] = sqrt(m_crio[i]*m_crio[i] - dr*dr);
01248     }  
01249 }
01250 
01251 // calculate the cooridate of hits
01252 double x_t[2],y_t[2],z_t[2];
01253 double x__t[2],y__t[2],z__t[2];
01254 double x_p[2],y_p[2],z_p[2];
01255 for( int i = 0; i < 2; i++){
01256     x_t[i] = x_c - t[i]*snf0;
01257     y_t[i] = y_c + t[i]*csf0;
01258     z_t[i] = z_c + t[i]*tanl; 
01259     x__t[i] = x_c + t[i]*snf0;
01260     y__t[i] = y_c - t[i]*csf0;
01261     z__t[i] = z_c - t[i]*tanl; 
01262 }
01263 
01264 double  phi_in_t,phi_in__t, phi_out_t,phi_out__t,phi_t,phi__t; 
01265 double  phibin = 2.0*M_PI/ncell;
01266 double  phi_cellid = phioffset + shift*phibin*(0.5-z/length) + cellid *phibin;
01267 phi_in_t = atan2(y_t[0],x_t[0]);
01268 phi_out_t = atan2(y_t[1],x_t[1]);
01269 phi_in__t = atan2(y__t[0],x__t[0]);
01270 phi_out__t = atan2(y__t[1],x__t[1]);
01271 phi_t = atan2(((y_t[0]+y_t[1])/2),((x_t[0]+x_t[1])/2));
01272 phi__t = atan2(((y__t[0]+y__t[1])/2),((x__t[0]+x__t[1])/2));
01273 
01274 phi_in_t = fmod( phi_in_t+4*M_PI,2*M_PI );
01275 phi_out_t = fmod( phi_out_t+4*M_PI,2*M_PI );
01276 phi_in__t = fmod( phi_in__t+4*M_PI,2*M_PI );
01277 phi_out__t = fmod( phi_out__t+4*M_PI,2*M_PI );
01278 phi_t = fmod( phi_t+4*M_PI,2*M_PI );
01279 phi__t = fmod( phi__t+4*M_PI,2*M_PI );
01280 phi_cellid = fmod( phi_cellid+4*M_PI,2*M_PI );
01281 
01282 if(fabs(phi_cellid - phi_t)<fabs(phi_cellid - phi__t))
01283 {
01284     for(int i =0; i<2; i++ )
01285     { x_p[i] = x_t[i];
01286         y_p[i] = y_t[i];
01287         z_p[i] = z_t[i];}
01288 }
01289 else if (fabs(phi_cellid - phi_t)>fabs(phi_cellid - phi__t))
01290 { 
01291     for(int i =0; i<2; i++ )
01292     { x_p[i] = x__t[i];
01293         y_p[i] = y__t[i];
01294         z_p[i] = z__t[i];}
01295 }
01296 else  
01297 {
01298     for(int i =0; i<2; i++ )
01299     { x_p[i] = x_t[i];
01300         y_p[i] = y_t[i];
01301         z_p[i] = z_t[i];}
01302 }
01303 
01304 //calculate path length in r_phi plane and all length in this layer
01305 double ch_ltrk_rp, ch_ltrk;
01306 ch_ltrk_rp = sqrt((x_p[0]-x_p[1])*(x_p[0]-x_p[1])+(y_p[0]-y_p[1])*(y_p[0]-y_p[1]));
01307 ch_ltrk = ch_ltrk_rp/sintheta;
01308 //give cellid of in and out of this layer
01309 double phi_in,phi_out;
01310 phi_in = atan2(y_p[0],x_p[0]);
01311 phi_out = atan2(y_p[1],x_p[1]);
01312 phi_in = fmod( phi_in+4*M_PI,2*M_PI );
01313 phi_out = fmod( phi_out+4*M_PI,2*M_PI ); 
01314 
01315 //determine the in/out cellid
01316 double  inlow, inup, outlow, outup, gap, phi1, phi2, phi_mid, phi_midin, phi_midout;
01317 int cid_in, cid_out;
01318 //  int cid_in_t,cid_in__t,cid_out_t,cid_out__t;
01319 //cache sampl in each cell for this layer
01320 std::vector<double> phi_entrance; 
01321 std::vector<double> sampl;  
01322 sampl.resize(ncell);
01323 phi_entrance.resize(ncell);
01324 for(int k=0; k<ncell; k++) {
01325     sampl[k] = -1.0;
01326     phi_entrance[k] = 0;
01327 }     
01328 
01329 cid_in = cid_out = -1;
01330 phibin = 2.0*M_PI/ncell;
01331 //determine the in/out cell id
01332 double stphi[2], phioff[2];
01333 stphi[0] = shift*phibin*(0.5-z_p[0]/length);
01334 stphi[1] = shift*phibin*(0.5-z_p[1]/length);
01335 phioff[0] = phioffset+stphi[0];
01336 phioff[1] = phioffset+stphi[1];
01337 for(int i=0; i<ncell; i++) {
01338     // for stereo layer
01339     //    phi[i] = phioff[0]+phibin*i;
01340     inlow = phioff[0]+phibin*i-phibin*0.5;
01341     inup = phioff[0]+phibin*i+phibin*0.5;
01342     outlow = phioff[1]+phibin*i-phibin*0.5;
01343     outup = phioff[1]+phibin*i+phibin*0.5;
01344     inlow = fmod( inlow+4*M_PI,2*M_PI );
01345     inup = fmod( inup+4*M_PI,2*M_PI );
01346     outlow = fmod( outlow+4*M_PI,2*M_PI );
01347     outup = fmod( outup+4*M_PI,2*M_PI );
01348 #ifdef YDEBUG
01349     cout << "shift " << shift<< " cellid " << i
01350         <<" phi_in " << phi_in << " phi_out " << phi_out 
01351         << " inup "<< inup << " inlow " << inlow 
01352         << " outup "<< outup << " outlow " << outlow << endl;
01353 #endif
01354     if(phi_in>=inlow&&phi_in<inup) cid_in = i;
01355     if(phi_out>=outlow&&phi_out<outup) cid_out = i;
01356     if ( inlow>inup) {
01357         if((phi_in>=inlow&&phi_in<2.0*M_PI)||(phi_in>=0.0&&phi_in<inup)) cid_in = i;
01358     } 
01359     if ( outlow>outup) {
01360         if((phi_out>=outlow&&phi_out<2.0*M_PI)||(phi_out>=0.0&&phi_out<outup)) cid_out = i;
01361     }
01362 }                           
01363 
01364 // judge how many cells are traversed and deal with different situations
01365 phi_midin = phi_midout = phi1 = phi2 = -999.0;
01366 gap = -999.0; 
01367 //only one cell traversed
01368 if( cid_in == cid_out) {
01369     sampl[cid_in] = ch_ltrk;
01370     //   return ch_ltrk;
01371 
01372 } else  if(cid_in < cid_out) {
01373     //in cell id less than out cell id
01374     //deal with the special case crossing x axis
01375     if( cid_out-cid_in>ncell/2  ) {
01376         //      if(gap>=M_PI) gap = 2.0*M_PI-gap;
01377         phi_midin = phioff[0]+phibin*cid_in-phibin*0.5;
01378         phi_midout = phioff[1]+phibin*cid_out+phibin*0.5;
01379         phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01380         phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01381         phi1 = phi_midout-phi_out;
01382         phi2 = phi_in-phi_midin;
01383         phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01384         phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01385         gap = phi1+phi2+(ncell-1-cid_out+cid_in)*phibin;
01386         gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01387         sampl[cid_in]=phi2/gap*ch_ltrk;
01388         sampl[cid_out]=phi1/gap*ch_ltrk;
01389 
01390         for( int jj = cid_out+1; jj<ncell; jj++) {
01391             sampl[jj]=phibin/gap*ch_ltrk;
01392         }
01393         for( int jj = 0; jj<cid_in; jj++) {
01394             sampl[jj]=phibin/gap*ch_ltrk;
01395         }           
01396 
01397     } else {
01398         //normal case
01399         phi_midin = phioff[0]+phibin*cid_in+phibin*0.5;
01400         phi_midout = phioff[1]+phibin*cid_out-phibin*0.5;
01401         phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01402         phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01403         phi1 = phi_midin-phi_in;
01404         phi2 = phi_out-phi_midout;
01405         phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01406         phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01407         gap = phi1+phi2+(cid_out-cid_in-1)*phibin;
01408         gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01409         sampl[cid_in]=phi1/gap*ch_ltrk;
01410         sampl[cid_out]=phi2/gap*ch_ltrk;
01411         phi_entrance[cid_in] = phi_in;
01412         phi_entrance[cid_out] = phi_midout;
01413         for( int jj = cid_in+1; jj<cid_out; jj++) {
01414             sampl[jj]=phibin/gap*ch_ltrk;
01415         }
01416     }
01417 } else  if(cid_in > cid_out) {
01418     //in cell id greater than out cell id
01419     //deal with the special case crossing x axis
01420     if( cid_in-cid_out>ncell/2  ) {
01421         //      if(gap>=M_PI) gap = 2.0*M_PI-gap;
01422         phi_midin = phioff[0]+phibin*cid_in+phibin*0.5;
01423         phi_midout = phioff[1]+phibin*cid_out-phibin*0.5;
01424         phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01425         phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01426         phi1 = phi_midin-phi_in;
01427         phi2 = phi_out-phi_midout;
01428         phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01429         phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01430         gap = phi1+phi2+(ncell-1-cid_in+cid_out)*phibin;
01431         gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01432         sampl[cid_out]=phi2/gap*ch_ltrk;
01433         sampl[cid_in]=phi1/gap*ch_ltrk; 
01434 
01435         for( int jj = cid_in+1; jj<ncell; jj++) {
01436             sampl[jj]=phibin/gap*ch_ltrk;
01437         }
01438         for( int jj = 0; jj<cid_out; jj++) {
01439             sampl[jj]=phibin/gap*ch_ltrk;
01440         }
01441     } else {
01442         //normal case
01443         phi_midin = phioff[0]+phibin*cid_in-phibin*0.5;
01444         phi_midout = phioff[1]+phibin*cid_out+phibin*0.5;
01445         phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01446         phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01447         phi1 = phi_midout-phi_out;
01448         phi2 = phi_in-phi_midin;
01449         phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01450         phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01451         gap = phi1+phi2+(cid_in-cid_out-1)*phibin;
01452         gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01453         sampl[cid_in]=phi2/gap*ch_ltrk;
01454         sampl[cid_out]=phi1/gap*ch_ltrk;
01455         for( int jj = cid_out+1; jj<cid_in; jj++) {
01456             sampl[jj]=phibin/gap*ch_ltrk;
01457         }
01458     }
01459 }
01460 
01461 #ifdef YDEBUG
01462 if(sampl[cellid]<0.0) {
01463     if(cid_in!=cid_out) cout<<"?????????"<<endl;
01464     cout<< "layerId  " << layer <<"  cell id  "<< cellid <<"  shift  " << shift 
01465         << "  cid_in  " << cid_in << "  cid_out  " << cid_out << endl;
01466 
01467     cout <<" phi_in " << phi_in <<" phi_midin " << phi_midin << " phi_out " 
01468         << phi_out << " phi_midout " << phi_midout <<endl;
01469     cout<<"total sampl " << ch_ltrk << " gap "<< gap << " phi1 " 
01470         << phi1 << " phi2 " << phi2 << " phibin " <<  phibin << endl; 
01471 
01472 
01473     for(int l=0; l<ncell; l++) {
01474         if(sampl[l]>=0.0) 
01475           cout<<"picked cellid  " << l << "  sampling length  "<< sampl[l]<< endl;
01476     }
01477 }
01478 #endif
01479 return sampl[cellid];
01480 
01481 }
01482 
01483 */
01484 
01485 
01486 // function to calculate the path length in the layer
01487 
01488 double
01489 DedxCorrecSvc::PathL( int ntpFlag, const Dedx_Helix& hel, int layer, int cellid, double z ) const {
01490 
01491     double length = geosvc->Layer(layer)->Length();
01492     int genlay = geosvc->Layer(layer)->Gen();
01493     double East_lay_X = geosvc->GeneralLayer(genlay)->SxEast();
01494     double East_lay_Y = geosvc->GeneralLayer(genlay)->SyEast();
01495     double East_lay_Z = geosvc->GeneralLayer(genlay)->SzEast();
01496     double West_lay_X = geosvc->GeneralLayer(genlay)->SxWest();
01497     double West_lay_Y = geosvc->GeneralLayer(genlay)->SyWest();
01498     double West_lay_Z = geosvc->GeneralLayer(genlay)->SzWest();
01499 
01500     HepPoint3D East_origin(East_lay_X, East_lay_Y, East_lay_Z);
01501     HepPoint3D West_origin(West_lay_X, West_lay_Y, West_lay_Z); 
01502     Hep3Vector wire = (CLHEP::Hep3Vector)East_origin - (CLHEP::Hep3Vector)West_origin;
01503     HepPoint3D piovt_z =(z*10+length/2 )/length * wire + West_origin;
01504     piovt_z = piovt_z*0.1; // conversion of the units(mm=>cm)
01505 
01506 
01507     //-------------------------------temp -------------------------------//
01508     HepPoint3D piv(hel.pivot());
01509     //-------------------------------temp -------------------------------//
01510 
01511     double dr0   = hel.a()[0];
01512     double phi0  = hel.a()[1];
01513     double kappa = hel.a()[2];
01514     double dz0   = hel.a()[3];
01515     double tanl  = hel.a()[4];
01516 
01517     // Choose the local field !!
01518     double Bz = 1000*(m_pIMF->getReferField());
01519     double ALPHA_loc = 1000/(2.99792458*Bz);
01520 
01521     // Choose the local field !!
01522     //double Bz = 1.0; // will be obtained from magnetic field service
01523     //double ALPHA_loc = 1000/(2.99792458*Bz);
01524     //double ALPHA_loc = 333.564095;
01525     int charge = ( kappa >= 0 )? 1 : -1;
01526     double rho = ALPHA_loc/kappa;
01527     double pt = fabs( 1.0/kappa );
01528     double lambda = atan( tanl );
01529     double theta = M_PI_2- lambda;
01530     double sintheta = sin(M_PI_2-atan(tanl));
01531     //  theta = 2.0*M_PI*theta/360.;
01532     double phi = fmod(phi0 + M_PI*4, M_PI*2);
01533     double csf0 = cos(phi);
01534     double snf0 = (1. - csf0) * (1. + csf0);
01535     snf0 = sqrt((snf0 > 0.) ? snf0 : 0.);
01536     if(phi > M_PI) snf0 = - snf0;
01537     //if(ntpFlag>0)
01538     //cout<<"rho = "<<rho<<"           hel.dr() + rho = "<<hel.dr() + rho<<endl; 
01539 
01540     //-------------------------------temp -------------------------------//
01541     double x_c = piv.x() + ( hel.dr() + rho )*csf0;
01542     double y_c = piv.y() + ( hel.dr() + rho )*snf0;
01543     double z_c = piv.z() + hel.dz();
01544     HepPoint3D ccenter(x_c, y_c, 0.0);
01545     double m_c_perp(ccenter.perp());
01546     Hep3Vector m_c_unit((HepPoint3D)ccenter.unit());
01547     //-------------------------------temp -------------------------------//
01548 
01550     double x_c_boost = x_c - piovt_z.x();
01551     double y_c_boost = y_c - piovt_z.y();
01552     double z_c_boost = z_c - piovt_z.z();
01553     HepPoint3D ccenter_boost(x_c_boost, y_c_boost, 0.0);
01554     double m_c_perp_boost(ccenter_boost.perp());
01555     //if(ntpFlag>0)
01556     //cout<<" ccenter = "<<ccenter<<"         m_c_perp ="<<m_c_perp<<endl;
01557     Hep3Vector m_c_unit_boost((HepPoint3D)ccenter_boost.unit());
01558 
01559     //phi region estimation
01560     double phi_io[2];
01561     HepPoint3D IO = (-1)*charge*m_c_unit;
01562     double dphi0 = fmod( IO.phi()+4*M_PI,2*M_PI ) - phi;
01563     double IO_phi = fmod( IO.phi()+4*M_PI,2*M_PI );
01564     //double dphi0_bak = IO_phi - phi;
01565     //if(dphi0 != 0)
01566     //cout<<"dphi value is : "<<dphi0<<"     phi:"<<phi<<"     IO.phi:"<<IO_phi<<endl;
01567     if( dphi0 > M_PI ) dphi0 -= 2*M_PI;
01568     else if( dphi0 < -M_PI ) dphi0 += 2*M_PI;
01569     //cout<<"dphi value is : "<<dphi0<<"     phi:"<<phi<<"     IO.phi:"<<IO_phi<<endl;
01570     //cout<<"charge is = "<<charge<<endl;
01571     phi_io[0] = -(1+charge)*M_PI_2 - charge*dphi0;
01572     //phi_io[0] = -(1+charge)*M_PI_2 + charge*dphi0;
01573     phi_io[1] = phi_io[0]+1.5*M_PI;
01574     //cout<<"phi_io[0] is : "<<phi_io[0]<<"           phi_io[1]:"<<phi_io[1]<<endl;
01575     double m_crio[2];
01576     double m_zb, m_zf, Calpha;
01577 
01578     // retrieve Mdc  geometry information
01579     double rcsiz1= geosvc->Layer(layer)->RCSiz1();
01580     double rcsiz2= geosvc->Layer(layer)->RCSiz2();
01581     double rlay= geosvc->Layer(layer)->Radius();
01582     int ncell= geosvc->Layer(layer)->NCell();
01583     double phioffset= geosvc->Layer(layer)->Offset();
01584     float shift= geosvc->Layer(layer)->Shift();
01585     double slant= geosvc->Layer(layer)->Slant();
01586     //double length = geosvc->Layer(layer)->Length();
01587     int type = geosvc->Layer(layer)->Sup()->Type();
01588 
01590     //check the value if same //
01592     int w0id = geosvc->Layer(layer)->Wirst();
01593     int wid = w0id + cellid;
01594     HepPoint3D backkward = geosvc->Wire(wid)->BWirePos(); 
01595     HepPoint3D forward = geosvc->Wire(wid)->FWirePos();
01596     double x_lay_backward = geosvc->Wire(layer, cellid)->Backward().x();
01597     double y_lay_backward = geosvc->Wire(layer, cellid)->Backward().y();
01598     double x_lay_forward = geosvc->Wire(layer, cellid)->Forward().x();
01599     double y_lay_forward = geosvc->Wire(layer, cellid)->Forward().y();
01600     double r_lay_backward = sqrt(x_lay_backward*x_lay_backward+y_lay_backward*y_lay_backward);
01601     double r_lay_forward = sqrt(x_lay_forward*x_lay_forward+y_lay_forward*y_lay_forward);
01602     double r_lay_use = ((z*10+length/2)/length)*(r_lay_backward-r_lay_forward) + r_lay_forward;
01603     /*for(int i=0; i<43; i++){  
01604       double r_up= geosvc->Layer(i)->RCSiz1(); 
01605       double r_down= geosvc->Layer(i)->RCSiz2(); 
01606       cout<<i<<"       "<<r_up<<"        "<<r_down<<endl; 
01607       }*/ 
01608     //  shift = shift*type;
01609     //  cout<< "type "<< type <<endl;
01610     r_lay_use = 0.1*r_lay_use; 
01611     rcsiz1 = 0.1*rcsiz1;
01612     rcsiz2 = 0.1*rcsiz2;
01613     rlay = 0.1*rlay;
01614     length = 0.1*length; 
01615     m_zb = 0.5*length;
01616     m_zf = -0.5*length;
01617     m_crio[0] = rlay - rcsiz1;
01618     m_crio[1] = rlay + rcsiz2;
01619     //conversion of the units(mm=>cm)
01620     int sign = -1;  
01621     int epflag[2];
01622     Hep3Vector iocand[2];
01623     Hep3Vector cell_IO[2];
01624     double dphi, downin;
01625     Hep3Vector zvector;
01626     //if (ntpFlag>0) cout<<"z = "<<z<<" , m_zb = "<<m_zb<<" , m_zf = "<<m_zf<<endl;
01627     if( type ) {
01628         downin = (z*z-m_zb*m_zb)*pow(tan(slant),2);
01629         m_crio[0] = sqrt(m_crio[0]*m_crio[0]+downin);
01630         m_crio[1] = sqrt(m_crio[1]*m_crio[1]+downin);
01631     }
01632 
01633     //int stced[2];
01634 
01635     for( int i = 0; i < 2; i++ ) {
01636         double cos_alpha = m_c_perp_boost*m_c_perp_boost + m_crio[i]*m_crio[i] - rho*rho;
01637         cos_alpha = 0.5*cos_alpha/( m_c_perp_boost*m_crio[i] );
01638         if(fabs(cos_alpha)>1&&i==0) return(-1.0);
01639         if(fabs(cos_alpha)>1&&i==1) {
01640             cos_alpha = m_c_perp_boost*m_c_perp_boost + m_crio[0]*m_crio[0] - rho*rho;
01641             cos_alpha = 0.5*cos_alpha/( m_c_perp_boost*m_crio[0] );
01642             Calpha = 2.0*M_PI-acos( cos_alpha );
01643         } else {
01644             Calpha =  acos( cos_alpha );
01645         }
01646         epflag[i] = 0;
01647         iocand[i] = m_c_unit_boost;
01648         iocand[i].rotateZ( charge*sign*Calpha );
01649         iocand[i]*= m_crio[i];
01650         //if (ntpFlag>0) cout<<"iocand corridate is : "<<iocand[i]<<endl;
01651 
01653         //compare with standard coordinate system results //
01655         //-------------------------------temp-------------------------//
01656         iocand[i] = iocand[i]+ piovt_z; //change to standard coordinate system      
01657         //iocand[i].y() = iocand[i].y() + piovt_z.y();
01658         //if (ntpFlag>0) cout<<i<<setw(10)<<iocand[i].x()<<setw(10)<<iocand[i].y()<<endl;
01659         //------------------------------temp -------------------------//    
01660 
01661         double xx = iocand[i].x() - x_c;
01662         double yy = iocand[i].y() - y_c;
01663         //dphi = atan2(yy, xx) - phi0 - M_PI_2*(1+charge);
01664         dphi = atan2(yy, xx) - phi0 - M_PI_2*(1-charge);
01665         dphi = fmod( dphi + 8.0*M_PI, 2*M_PI );
01666 
01667         if( dphi < phi_io[0] ) {
01668             dphi += 2*M_PI;
01669         }
01670         else if( phi_io[1] < dphi ) {
01671             dphi -= 2*M_PI;
01672         }
01674 
01675         //Hep3Vector zvector( 0., 0., z_c-rho*dphi*tanl);
01676         Hep3Vector zvector( 0., 0., z_c-rho*dphi*tanl-piovt_z.z());
01677         //if (ntpFlag>0) cout<<"z_c-rho*dphi*tanl : "<<z_c-rho*dphi*tanl<<endl;
01678         cell_IO[i] = iocand[i];
01679         cell_IO[i] += zvector;
01680         //---------------------------------temp ---------------------------------//
01681         //cell_IO[i] = hel.x(dphi);//compare with above results 
01682         //---------------------------------temp ---------------------------------//     
01683 
01684         double xcio, ycio, phip;
01686         /*
01687            float delrin = 2.0;
01688            if( m_zf-delrin > zvector.z() ){
01689            phip = z_c - m_zb + delrin;
01690            phip = phip/( rho*tanl );
01691            phip = phip + phi0;
01692            xcio = x_c - rho*cos( phip );
01693            ycio = y_c - rho*sin( phip );
01694            cell_IO[i].setX( xcio );
01695            cell_IO[i].setY( ycio );
01696            cell_IO[i].setZ( m_zb+delrin );
01697            epflag[i] = 1;
01698            }
01699            else if( m_zb+delrin < zvector.z() ){
01700            phip = z_c - m_zf-delrin;
01701            phip = phip/( rho*tanl );
01702            phip = phip + phi0;
01703            xcio = x_c - rho*cos( phip );
01704            ycio = y_c - rho*sin( phip );
01705            cell_IO[i].setX( xcio );
01706            cell_IO[i].setY( ycio );
01707            cell_IO[i].setZ( m_zf-delrin );
01708            epflag[i] = 1;
01709            }
01710            else{
01711          */
01712         //      cell_IO[i] = iocand;
01713         //      cell_IO[i] += zvector;
01714         //    }
01715         //dphi = dphi -M_PI;  
01716         cell_IO[i] = hel.x(dphi);
01717         //if (ntpFlag>0)  { cout<<"cell_IO corridate : "<<cell_IO[i]<<endl;}
01718         // if(i==0) cout<<"zhit value is = "<<z<<endl; 
01719     }
01720 
01721     // path length estimation
01722     // phi calculation
01723     Hep3Vector cl = cell_IO[1] - cell_IO[0];
01724 
01725     //float ch_tphi, ch_tdphi;
01726     double ch_theta;
01727     double ch_dphi;
01728     double ch_ltrk = 0;
01729     double ch_ltrk_rp = 0;
01730     ch_dphi = cl.perp()*0.5/(ALPHA_loc*pt);
01731     ch_dphi = 2.0 * asin( ch_dphi );
01732     ch_ltrk_rp = ch_dphi*ALPHA_loc*pt;
01733     double rpi_path = sqrt(cl.x()*cl.x()+cl.y()*cl.y());
01734     ch_ltrk = sqrt( ch_ltrk_rp*ch_ltrk_rp + cl.z()*cl.z() );
01735     double path = ch_ltrk_rp/ sintheta;
01736     ch_theta = cl.theta();
01737     /*   if (ntpFlag>0)
01738          { 
01739     // if((ch_ltrk_rp-rpi_path)>0.001 || (ch_ltrk-path)>0.001)
01740     cout<<"ch_ltrk_rp : "<<ch_ltrk_rp<<"       rpi_path: "<<rpi_path<<endl;
01741     cout<<"ch_ltrk : "<<ch_ltrk<<"       path:"<<path<<endl; 
01742     }
01743      */
01744     /*
01745        if( ch_theta < theta*0.85 || 1.15*theta < ch_theta)
01746        ch_ltrk *= -1; /// miss calculation
01747 
01748        if( epflag[0] == 1 || epflag [1] == 1 )
01749        ch_ltrk *= -1;  /// invalid region for dE/dx or outside of Mdc
01750      */
01751     // judge how many cells are traversed and deal with different situations
01752     double phibin, phi_in, phi_out, inlow, inup, outlow, outup, gap, phi1, phi2, phi_mid, phi_midin, phi_midout;
01753     int cid_in, cid_out;
01754     double inlow_z, inup_z, outlow_z, outup_z, gap_z, phi1_z, phi2_z, phi_mid_z, phi_midin_z, phi_midout_z;
01755     //cache sampl in each cell for this layer
01756 
01757     std::vector<double> sampl;  
01758     sampl.resize(ncell);
01759     for(int k=0; k<ncell; k++) {
01760         sampl[k] = -1.0;
01761     }
01762 
01763     cid_in = cid_out = -1;
01764     phi_in = cell_IO[0].phi();
01765     phi_out = cell_IO[1].phi();
01766     //phi_in = iocand[0].phi();
01767     //phi_out = iocand[1].phi();
01768     phi_in = fmod( phi_in+4*M_PI,2*M_PI );
01769     phi_out = fmod( phi_out+4*M_PI,2*M_PI );
01770     phibin = 2.0*M_PI/ncell;
01771     //  gap = fabs(phi_out-phi_in);
01772 
01773     //determine the in/out cell id
01774     Hep3Vector cell_mid=0.5*(cell_IO[0]+cell_IO[1]);
01775     //Hep3Vector cell_mid=0.5*(iocand[0]+iocand[0]);
01776     //cout<<cell_mid<<endl;
01777     //double stereophi = shift*phibin*(0.5-cell_mid.z()/length);
01778     //phioffset = phioffset+stereophi;
01779     double stphi[2], phioff[2];
01780     stphi[0] = shift*phibin*(0.5-cell_IO[0].z()/length);
01781     stphi[1] = shift*phibin*(0.5-cell_IO[1].z()/length);
01782     //stphi[0] = shift*phibin*(0.5-z/length);
01783     //stphi[1] = shift*phibin*(0.5-z/length);
01784     phioff[0] = phioffset+stphi[0];
01785     phioff[1] = phioffset+stphi[1];
01786 
01787     for(int i=0; i<ncell; i++) {
01788 
01789         double x_lay_backward_cell = geosvc->Wire(layer, i)->Backward().x()*0.1;
01790         double y_lay_backward_cell = geosvc->Wire(layer, i)->Backward().y()*0.1;
01791         double x_lay_forward_cell = geosvc->Wire(layer, i)->Forward().x()*0.1;
01792         double y_lay_forward_cell = geosvc->Wire(layer, i)->Forward().y()*0.1;
01793         //if(ntpFlag>0) cout<<layer<<setw(10)<<i<<setw(10)<<x_lay_forward<<setw(10)<<y_lay_forward<<setw(10)<<x_lay_backward<<setw(10)<<y_lay_backward<<setw(10)<<r_lay_forward<<setw(10)<<endl;
01794         //Hep3Vector lay_backward, lay_forward;  
01795         Hep3Vector lay_backward(x_lay_backward_cell, y_lay_backward_cell, 0);
01796         Hep3Vector lay_forward(x_lay_forward_cell, y_lay_forward_cell, 0);
01797         Hep3Vector Cell_z[2];
01798         Cell_z[0] = ((cell_IO[0].z()+length/2)/length)*(lay_backward - lay_forward) + lay_forward;
01799         Cell_z[1] = ((cell_IO[1].z()+length/2)/length)*(lay_backward - lay_forward) + lay_forward;
01800         double z_phi[2];
01801         z_phi[0] = Cell_z[0].phi();
01802         z_phi[1] = Cell_z[1].phi();
01803         z_phi[0] = fmod( z_phi[0]+4*M_PI,2*M_PI );
01804         z_phi[1] = fmod( z_phi[1]+4*M_PI,2*M_PI );
01805         //double backward_phi = lay_backward.phi();
01806         //double forward_phi = lay_forward.phi();
01807         //backward_phi = fmod( backward_phi+4*M_PI,2*M_PI );
01808         //forward_phi = fmod( forward_phi+4*M_PI,2*M_PI );
01809         //if(ntpFlag>0) cout<<"backward_phi cal : "<<atan2(y_lay_backward,x_lay_backward)<<"       forward_phi : "<<atan2(y_lay_forward,x_lay_forward)<<endl;
01810         //if(ntpFlag>0) cout<<layer<<"       backward_phi : "<<backward_phi<<"       forward_phi : "<<forward_phi<<endl;
01811 
01812         //double z_phi[2];
01813         //z_phi[0] = ((cell_IO[0].z()+length/2)/length)*(backward_phi - forward_phi) + forward_phi; 
01814         //z_phi[1] = ((cell_IO[1].z()+length/2)/length)*(backward_phi - forward_phi) + forward_phi;
01815         //if(ntpFlag>0) cout<<"phi z : "<<z_phi[0]<<"          "<<z_phi[1]<<endl;
01816         inlow_z = z_phi[0] - phibin*0.5;
01817         inup_z = z_phi[0] + phibin*0.5;
01818         outlow_z = z_phi[1] - phibin*0.5;
01819         outup_z = z_phi[1] + phibin*0.5;
01820         inlow_z = fmod( inlow_z+4*M_PI,2*M_PI );
01821         inup_z = fmod( inup_z+4*M_PI,2*M_PI );
01822         outlow_z = fmod( outlow_z+4*M_PI,2*M_PI );
01823         outup_z = fmod( outup_z+4*M_PI,2*M_PI );
01824 
01825 
01826         // for stereo layer
01827         inlow = phioff[0]+phibin*i-phibin*0.5;
01828         inup = phioff[0]+phibin*i+phibin*0.5;
01829         outlow = phioff[1]+phibin*i-phibin*0.5;
01830         outup = phioff[1]+phibin*i+phibin*0.5;
01831         inlow = fmod( inlow+4*M_PI,2*M_PI );
01832         inup = fmod( inup+4*M_PI,2*M_PI );
01833         outlow = fmod( outlow+4*M_PI,2*M_PI );
01834         outup = fmod( outup+4*M_PI,2*M_PI );
01835 
01836 #ifdef YDEBUG
01837         if(ntpFlag > 0)  cout << "shift " << shift 
01838           <<" phi_in " << phi_in << " phi_out " << phi_out 
01839               << " inup "<< inup << " inlow " << inlow 
01840               << " outup "<< outup << " outlow " << outlow << endl;
01841 #endif
01842 
01843         /*if(phi_in>=inlow&&phi_in<inup) cid_in = i;
01844           if(phi_out>=outlow&&phi_out<outup) cid_out = i;
01845           if ( inlow>inup) {
01846           if((phi_in>=inlow&&phi_in<2.0*M_PI)||(phi_in>=0.0&&phi_in<inup)) cid_in = i;
01847           } 
01848           if ( outlow>outup) {
01849           if((phi_out>=outlow&&phi_out<2.0*M_PI)||(phi_out>=0.0&&phi_out<outup)) cid_out = i;
01850           }*/
01851 
01852         if(phi_in>=inlow_z&&phi_in<inup_z) cid_in = i;
01853         if(phi_out>=outlow_z&&phi_out<outup_z) cid_out = i;
01854         if ( inlow_z>inup_z) {
01855             if((phi_in>=inlow_z&&phi_in<2.0*M_PI)||(phi_in>=0.0&&phi_in<inup_z)) cid_in = i;
01856         }
01857         if ( outlow_z>outup_z) {
01858             if((phi_out>=outlow_z&&phi_out<2.0*M_PI)||(phi_out>=0.0&&phi_out<outup_z)) cid_out = i;
01859         }
01860     }
01861 
01862     phi_midin = phi_midout = phi1 = phi2 = -999.0;
01863     gap = -999.0; 
01864     //only one cell traversed
01865     //if(ntpFlag > 0) cout<<"cid_in =  "<<cid_in<<"        cid_out = "<<cid_out<<endl;
01866     if(cid_in == -1 || cid_out == -1) return -1;
01867 
01868     if( cid_in == cid_out) {
01869         sampl[cid_in]= ch_ltrk;
01870         //return ch_ltrk;
01871         return sampl[cellid];
01872     } else  if(cid_in < cid_out) {
01873         //in cell id less than out cell id
01874         //deal with the special case crossing x axis
01875         if( cid_out-cid_in>ncell/2  ) {
01876 
01877             //      if(gap>=M_PI) gap = 2.0*M_PI-gap;
01878             double x_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().x()*0.1;
01879             double y_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().y()*0.1;
01880             double x_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().x()*0.1;
01881             double y_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().y()*0.1;
01882             Hep3Vector lay_backward_cin(x_lay_backward_cin, y_lay_backward_cin, 0);
01883             Hep3Vector lay_forward_cin(x_lay_forward_cin, y_lay_forward_cin, 0);
01884             Hep3Vector Cell_z[2];
01885             Cell_z[0]=((cell_IO[0].z()+length/2)/length)*(lay_backward_cin-lay_forward_cin)+lay_forward_cin;
01886             double phi_cin_z = Cell_z[0].phi();
01887             phi_cin_z = fmod( phi_cin_z+4*M_PI,2*M_PI );
01888             double x_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().x()*0.1;
01889             double y_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().y()*0.1;
01890             double x_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().x()*0.1;
01891             double y_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().y()*0.1;
01892             Hep3Vector lay_backward_cout(x_lay_backward_cout, y_lay_backward_cout, 0);
01893             Hep3Vector lay_forward_cout(x_lay_forward_cout, y_lay_forward_cout, 0);
01894             Cell_z[1]=((cell_IO[1].z()+length/2)/length)*(lay_backward_cout-lay_forward_cout)+lay_forward_cout;
01895             double phi_cout_z = Cell_z[1].phi();
01896             phi_cout_z = fmod( phi_cout_z+4*M_PI,2*M_PI );
01897 
01898             phi_midin_z = phi_cin_z-phibin*0.5;
01899             phi_midout_z = phi_cout_z+phibin*0.5;
01900             phi_midin_z = fmod( phi_midin_z+4*M_PI,2*M_PI );
01901             phi_midout_z = fmod( phi_midout_z+4*M_PI,2*M_PI );
01902             phi1_z = phi_midout_z-phi_out;
01903             phi2_z = phi_in-phi_midin_z;
01904             phi1_z = fmod(phi1_z+2.0*M_PI,2.0*M_PI);
01905             phi2_z = fmod(phi2_z+2.0*M_PI,2.0*M_PI);
01906             gap_z = phi1_z+phi2_z+(ncell-1-cid_out+cid_in)*phibin;
01907             gap_z = fmod(gap_z+2.0*M_PI,2.0*M_PI);
01908             sampl[cid_in]=phi2_z/gap_z*ch_ltrk;
01909             sampl[cid_out]=phi1_z/gap_z*ch_ltrk;
01910             for( int jj = cid_out+1; jj<ncell; jj++) {
01911                 sampl[jj]=phibin/gap_z*ch_ltrk;
01912             }
01913             for( int jj = 0; jj<cid_in; jj++) {
01914                 sampl[jj]=phibin/gap_z*ch_ltrk;
01915             }
01916 
01917             /*
01918             //      if(gap>=M_PI) gap = 2.0*M_PI-gap;
01919             phi_midin = phioff[0]+phibin*cid_in-phibin*0.5;
01920             phi_midout = phioff[1]+phibin*cid_out+phibin*0.5;
01921             phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01922             phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01923             phi1 = phi_midout-phi_out;
01924             phi2 = phi_in-phi_midin;
01925             phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01926             phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01927             gap = phi1+phi2+(ncell-1-cid_out+cid_in)*phibin;
01928             gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01929             sampl[cid_in]=phi2/gap*ch_ltrk;
01930             sampl[cid_out]=phi1/gap*ch_ltrk;
01931             for( int jj = cid_out+1; jj<ncell; jj++) {
01932             sampl[jj]=phibin/gap*ch_ltrk;
01933             }
01934             for( int jj = 0; jj<cid_in; jj++) {
01935             sampl[jj]=phibin/gap*ch_ltrk;
01936             }*/
01937             /*
01938                cout<<"LLLLLLLLLLLLL"<<endl;
01939                cout<< "layerId  " << layer <<"  cell id  "<< cellid <<"  shift  " << shift 
01940                << "  cid_in  " << cid_in << "  cid_out  " << cid_out << endl;
01941                cout <<" phi_in " << phi_in <<" phi_midin " << phi_midin << " phi_out " 
01942                << phi_out << " phi_midout " << phi_midout <<endl;
01943                cout<<"total sampl " << ch_ltrk << " gap "<< gap << " phi1 " 
01944                << phi1 << " phi2 " << phi2 << " phibin " <<  phibin << endl; 
01945              */
01946         } else {
01947             //normal case
01948             double x_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().x()*0.1;
01949             double y_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().y()*0.1;
01950             double x_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().x()*0.1;
01951             double y_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().y()*0.1;
01952             Hep3Vector lay_backward_cin(x_lay_backward_cin, y_lay_backward_cin, 0);
01953             Hep3Vector lay_forward_cin(x_lay_forward_cin, y_lay_forward_cin, 0);
01954             Hep3Vector Cell_z[2];
01955             Cell_z[0]=((cell_IO[0].z()+length/2)/length)*(lay_backward_cin-lay_forward_cin)+lay_forward_cin;
01956             double phi_cin_z = Cell_z[0].phi();
01957             phi_cin_z = fmod( phi_cin_z+4*M_PI,2*M_PI );
01958             double x_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().x()*0.1;
01959             double y_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().y()*0.1;
01960             double x_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().x()*0.1;
01961             double y_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().y()*0.1;
01962             Hep3Vector lay_backward_cout(x_lay_backward_cout, y_lay_backward_cout, 0);
01963             Hep3Vector lay_forward_cout(x_lay_forward_cout, y_lay_forward_cout, 0);
01964             Cell_z[1]=((cell_IO[1].z()+length/2)/length)*(lay_backward_cout-lay_forward_cout)+lay_forward_cout;
01965             double phi_cout_z = Cell_z[1].phi();
01966             phi_cout_z = fmod( phi_cout_z+4*M_PI,2*M_PI );
01967 
01968             phi_midin_z = phi_cin_z+phibin*0.5;
01969             phi_midout_z = phi_cout_z-phibin*0.5;
01970             phi_midin_z = fmod( phi_midin_z+4*M_PI,2*M_PI );
01971             phi_midout_z = fmod( phi_midout_z+4*M_PI,2*M_PI );
01972             phi1_z = phi_midin_z-phi_in;
01973             phi2_z = phi_out-phi_midout_z;
01974             phi1_z = fmod(phi1_z+2.0*M_PI,2.0*M_PI);
01975             phi2_z = fmod(phi2_z+2.0*M_PI,2.0*M_PI);
01976             gap_z = phi1_z+phi2_z+(cid_out-cid_in-1)*phibin;
01977             gap_z = fmod(gap_z+2.0*M_PI,2.0*M_PI);
01978             sampl[cid_in]=phi1_z/gap_z*ch_ltrk;
01979             sampl[cid_out]=phi2_z/gap_z*ch_ltrk;
01980             for( int jj = cid_in+1; jj<cid_out; jj++) {
01981                 sampl[jj]=phibin/gap_z*ch_ltrk;
01982             }
01983             //normal case
01984             /*phi_midin = phioff[0]+phibin*cid_in+phibin*0.5;
01985               phi_midout = phioff[1]+phibin*cid_out-phibin*0.5;
01986               phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
01987               phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
01988               phi1 = phi_midin-phi_in;
01989               phi2 = phi_out-phi_midout;
01990               phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
01991               phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
01992               gap = phi1+phi2+(cid_out-cid_in-1)*phibin;
01993               gap = fmod(gap+2.0*M_PI,2.0*M_PI);
01994               sampl[cid_in]=phi1/gap*ch_ltrk;
01995               sampl[cid_out]=phi2/gap*ch_ltrk;
01996               for( int jj = cid_in+1; jj<cid_out; jj++) {
01997               sampl[jj]=phibin/gap*ch_ltrk;
01998               }*/
01999         }
02000 
02001     } else  if(cid_in > cid_out) {
02002         //in cell id greater than out cell id
02003         //deal with the special case crossing x axis
02004         if( cid_in-cid_out>ncell/2  ) {
02005             double x_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().x()*0.1;
02006             double y_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().y()*0.1;
02007             double x_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().x()*0.1;
02008             double y_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().y()*0.1;
02009             Hep3Vector lay_backward_cin(x_lay_backward_cin, y_lay_backward_cin, 0);
02010             Hep3Vector lay_forward_cin(x_lay_forward_cin, y_lay_forward_cin, 0);
02011             Hep3Vector Cell_z[2];
02012             Cell_z[0]=((cell_IO[0].z()+length/2)/length)*(lay_backward_cin-lay_forward_cin)+lay_forward_cin;
02013             double phi_cin_z = Cell_z[0].phi();
02014             phi_cin_z = fmod( phi_cin_z+4*M_PI,2*M_PI );
02015             double x_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().x()*0.1;
02016             double y_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().y()*0.1;
02017             double x_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().x()*0.1;
02018             double y_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().y()*0.1;
02019             Hep3Vector lay_backward_cout(x_lay_backward_cout, y_lay_backward_cout, 0);
02020             Hep3Vector lay_forward_cout(x_lay_forward_cout, y_lay_forward_cout, 0);
02021             Cell_z[1]=((cell_IO[1].z()+length/2)/length)*(lay_backward_cout-lay_forward_cout)+lay_forward_cout;
02022             double phi_cout_z = Cell_z[1].phi();
02023             phi_cout_z = fmod( phi_cout_z+4*M_PI,2*M_PI );
02024 
02025             phi_midin_z = phi_cin_z+phibin*0.5;
02026             phi_midout_z = phi_cout_z-phibin*0.5;
02027             phi_midin_z = fmod( phi_midin_z+4*M_PI,2*M_PI );
02028             phi_midout_z = fmod( phi_midout_z+4*M_PI,2*M_PI );
02029             phi1_z = phi_midin_z-phi_in;
02030             phi2_z = phi_out-phi_midout_z;
02031             phi1_z = fmod(phi1_z+2.0*M_PI,2.0*M_PI);
02032             phi2_z = fmod(phi2_z+2.0*M_PI,2.0*M_PI);
02033             gap_z = phi1_z+phi2_z+(ncell-1-cid_in+cid_out)*phibin;
02034             gap_z = fmod(gap_z+2.0*M_PI,2.0*M_PI);
02035             sampl[cid_out]=phi2_z/gap_z*ch_ltrk;
02036             sampl[cid_in]=phi1_z/gap_z*ch_ltrk;
02037             for( int jj = cid_in+1; jj<ncell; jj++) {
02038                 sampl[jj]=phibin/gap_z*ch_ltrk;
02039             }
02040             for( int jj = 0; jj<cid_out; jj++) {
02041                 sampl[jj]=phibin/gap_z*ch_ltrk;
02042             }
02043 
02044             //      if(gap>=M_PI) gap = 2.0*M_PI-gap;
02045             /*phi_midin = phioff[0]+phibin*cid_in+phibin*0.5;
02046               phi_midout = phioff[1]+phibin*cid_out-phibin*0.5;
02047               phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
02048               phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
02049               phi1 = phi_midin-phi_in;
02050               phi2 = phi_out-phi_midout;
02051               phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
02052               phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
02053               gap = phi1+phi2+(ncell-1-cid_in+cid_out)*phibin;
02054               gap = fmod(gap+2.0*M_PI,2.0*M_PI);
02055               sampl[cid_out]=phi2/gap*ch_ltrk;
02056               sampl[cid_in]=phi1/gap*ch_ltrk;
02057               for( int jj = cid_in+1; jj<ncell; jj++) {
02058               sampl[jj]=phibin/gap*ch_ltrk;
02059               }
02060               for( int jj = 0; jj<cid_out; jj++) {
02061               sampl[jj]=phibin/gap*ch_ltrk;
02062               }*/
02063             /*
02064                cout<<"LLLLLLLLLLLLL"<<endl;
02065                cout<< "layerId  " << layer <<"  cell id  "<< cellid <<"  shift  " << shift 
02066                << "  cid_in  " << cid_in << "  cid_out  " << cid_out << endl;
02067                cout <<" phi_in " << phi_in <<" phi_midin " << phi_midin << " phi_out " 
02068                << phi_out << " phi_midout " << phi_midout <<endl;
02069                cout<<"total sampl " << ch_ltrk << " gap "<< gap << " phi1 " 
02070                << phi1 << " phi2 " << phi2 << " phibin " <<  phibin << endl; 
02071              */
02072         } else {
02073             double x_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().x()*0.1;
02074             double y_lay_backward_cin = geosvc->Wire(layer, cid_in)->Backward().y()*0.1;
02075             double x_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().x()*0.1;
02076             double y_lay_forward_cin = geosvc->Wire(layer, cid_in)->Forward().y()*0.1;
02077             Hep3Vector lay_backward_cin(x_lay_backward_cin, y_lay_backward_cin, 0);
02078             Hep3Vector lay_forward_cin(x_lay_forward_cin, y_lay_forward_cin, 0);
02079             Hep3Vector Cell_z[2];
02080             Cell_z[0]=((cell_IO[0].z()+length/2)/length)*(lay_backward_cin-lay_forward_cin)+lay_forward_cin;
02081             double phi_cin_z = Cell_z[0].phi();
02082             phi_cin_z = fmod( phi_cin_z+4*M_PI,2*M_PI );
02083             double x_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().x()*0.1;
02084             double y_lay_backward_cout = geosvc->Wire(layer, cid_out)->Backward().y()*0.1;
02085             double x_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().x()*0.1;
02086             double y_lay_forward_cout = geosvc->Wire(layer, cid_out)->Forward().y()*0.1;
02087             Hep3Vector lay_backward_cout(x_lay_backward_cout, y_lay_backward_cout, 0);
02088             Hep3Vector lay_forward_cout(x_lay_forward_cout, y_lay_forward_cout, 0);
02089             Cell_z[1]=((cell_IO[1].z()+length/2)/length)*(lay_backward_cout-lay_forward_cout)+lay_forward_cout;
02090             double phi_cout_z = Cell_z[1].phi();
02091             phi_cout_z = fmod( phi_cout_z+4*M_PI,2*M_PI );
02092 
02093             phi_midin_z = phi_cin_z-phibin*0.5;
02094             phi_midout_z = phi_cout_z+phibin*0.5;
02095             phi_midin_z = fmod( phi_midin_z+4*M_PI,2*M_PI );
02096             phi_midout_z = fmod( phi_midout_z+4*M_PI,2*M_PI );
02097             phi1_z = phi_midout_z-phi_out;
02098             phi2_z = phi_in-phi_midin_z;
02099             phi1_z = fmod(phi1_z+2.0*M_PI,2.0*M_PI);
02100             phi2_z = fmod(phi2_z+2.0*M_PI,2.0*M_PI);
02101             gap_z = phi1_z+phi2_z+(cid_in-cid_out-1)*phibin;
02102             gap_z = fmod(gap_z+2.0*M_PI,2.0*M_PI);
02103             sampl[cid_in]=phi2_z/gap_z*ch_ltrk;
02104             sampl[cid_out]=phi1_z/gap_z*ch_ltrk;
02105             for( int jj = cid_out+1; jj<cid_in; jj++) {
02106                 sampl[jj]=phibin/gap_z*ch_ltrk;
02107             }
02108 
02109             //normal case
02110             /*phi_midin = phioff[0]+phibin*cid_in-phibin*0.5;
02111               phi_midout = phioff[1]+phibin*cid_out+phibin*0.5;
02112               phi_midin = fmod( phi_midin+4*M_PI,2*M_PI );
02113               phi_midout = fmod( phi_midout+4*M_PI,2*M_PI );
02114               phi1 = phi_midout-phi_out;
02115               phi2 = phi_in-phi_midin;
02116               phi1 = fmod(phi1+2.0*M_PI,2.0*M_PI);
02117               phi2 = fmod(phi2+2.0*M_PI,2.0*M_PI);
02118               gap = phi1+phi2+(cid_in-cid_out-1)*phibin;
02119               gap = fmod(gap+2.0*M_PI,2.0*M_PI);
02120               sampl[cid_in]=phi2/gap*ch_ltrk;
02121               sampl[cid_out]=phi1/gap*ch_ltrk;
02122               for( int jj = cid_out+1; jj<cid_in; jj++) {
02123               sampl[jj]=phibin/gap*ch_ltrk;
02124               }*/
02125         }
02126     }
02127 
02128 #ifdef YDEBUG
02129     if(sampl[cellid]<0.0) {
02130         if(cid_in!=cid_out) cout<<"?????????"<<endl;
02131         cout<< "layerId  " << layer <<"  cell id  "<< cellid <<"  shift  " << shift 
02132             << "  cid_in  " << cid_in << "  cid_out  " << cid_out << endl;
02133 
02134         cout <<" phi_in " << phi_in <<" phi_midin " << phi_midin << " phi_out " 
02135             << phi_out << " phi_midout " << phi_midout <<endl;
02136         cout<<"total sampl " << ch_ltrk << " gap "<< gap << " phi1 " 
02137             << phi1 << " phi2 " << phi2 << " phibin " <<  phibin << endl; 
02138 
02139 
02140         for(int l=0; l<ncell; l++) {
02141             if(sampl[l]>=0.0) 
02142               cout<<"picked cellid  " << l << "  sampling length  "<< sampl[l]<< endl;
02143         }
02144     }
02145 #endif
02146     return sampl[cellid];
02147 }
02148 
02149 
02150 
02151 
02152 // function to convert measured ionization (D) to actural ionization (I)
02153 double DedxCorrecSvc::D2I(const double& cosTheta, const double& D) const
02154 {
02155     //cout<<"DedxCorrecSvc::D2I"<<endl;
02156     double absCosTheta   = fabs(cosTheta);
02157     double projection    = pow(absCosTheta,m_power) + m_delta;   // Projected length on wire
02158     double chargeDensity = D/projection;
02159     double numerator     = 1 + m_alpha*chargeDensity;
02160     double denominator   = 1 + m_gamma*chargeDensity;
02161     double I             = D;
02162 
02163     //if(denominator > 0.1)
02164 
02165     I = D * m_ratio* numerator/denominator;
02166 //    cout << "m_alpha " << m_alpha << endl;
02167 //    cout << "m_gamma " << m_gamma << endl;
02168 //    cout << "m_delta " << m_delta << endl;
02169 //    cout << "m_power " << m_power << endl;
02170 //    cout << "m_ratio " << m_ratio << endl;
02171     return I;
02172 }
02173 
02174 // Convert actural ionization (I) to measured ionization (D)
02175 double DedxCorrecSvc::I2D(const double& cosTheta, const double& I) const
02176 {
02177     // cout<<" DedxCorrecSvc::I2D"<<endl;
02178     double absCosTheta = fabs(cosTheta);
02179     double projection  = pow(absCosTheta,m_power) + m_delta;   // Projected length on wire
02180 
02181     // Do the quadratic to compute d of the electron
02182     double a =  m_alpha / projection;
02183     double b =  1 - m_gamma / projection*(I/m_ratio);
02184     double c = -I/m_ratio;
02185 
02186     if(b==0 && a==0){
02187         cout<<"both a and b coefficiants for hadron correction are 0"<<endl;
02188         return I;
02189     }
02190 
02191     double D = a != 0? (-b + sqrt(b*b - 4.0*a*c))/(2.0*a) : -c/b;
02192     if(D<0){    
02193         cout<<"D is less 0! will try another solution"<<endl;
02194         D=a != 0? (-b - sqrt(b*b + 4.0*a*c))/(2.0*a) : -c/b;
02195         if(D<0){
02196             cout<<"D is still less 0! just return uncorrectecd value"<<endl;
02197             return I;
02198         }               
02199     }
02200 
02201     return D;
02202 }
02203 
02204 

Generated on Tue Nov 29 23:12:46 2016 for BOSS_7.0.2 by  doxygen 1.4.7