/home/bes3soft/bes3soft/Boss/7.0.2/dist/7.0.2/Analysis/VertexFit/VertexFit-00-02-78/src/VertexDbSvc.cxx

Go to the documentation of this file.
00001 //     This service is used to read the vertex information from the database
00002 //
00003 //     an example to use this service is shown in test/test_read.cxx
00004 //
00005 //    the joboption for the example is shown in share/job-test.txt
00006 //  
00007 //     the joboption for this service is shown in  share/jobOptions_VertexDbSvc.txt
00008 
00009 
00010 #include <iostream>
00011 #include <sstream>
00012 #include <cstring>
00013 #include <cstdlib>
00014 #include <cstdio>
00015 
00016 #ifndef BEAN
00017 #include "VertexFit/VertexDbSvc.h"
00018 #include "GaudiKernel/Kernel.h"
00019 #include "GaudiKernel/IInterface.h"
00020 #include "GaudiKernel/StatusCode.h"
00021 #include "GaudiKernel/SvcFactory.h"
00022 #include "GaudiKernel/MsgStream.h"
00023 #include "GaudiKernel/ISvcLocator.h"
00024 #include "GaudiKernel/SmartDataPtr.h"
00025 #include "GaudiKernel/IDataProviderSvc.h"
00026 #include "GaudiKernel/PropertyMgr.h"
00027 
00028 
00029 #include "GaudiKernel/IIncidentSvc.h"
00030 #include "GaudiKernel/Incident.h"
00031 #include "GaudiKernel/IIncidentListener.h"
00032 
00033 #include "GaudiKernel/ISvcLocator.h"
00034 #include "GaudiKernel/Bootstrap.h"
00035 #include "EventModel/EventModel.h"
00036 #include "EventModel/Event.h"
00037 #include "EventModel/EventHeader.h"
00038 
00039 #include <TMath.h>
00040 #else
00041 #include "VertexFit/VertexDbSvc.h"
00042 #endif
00043 
00044 using namespace std;
00045 
00046 
00047 #ifndef BEAN
00048 VertexDbSvc::VertexDbSvc( const string& name, ISvcLocator* svcloc) :
00049   Service (name, svcloc){
00050   // declare properties
00051   declareProperty("Host" , host = std::string("bes3db2.ihep.ac.cn"));
00052   declareProperty("DbName" , dbName = std::string("offlinedb"));
00053   declareProperty("UserName" , userName = std::string("guest"));
00054   declareProperty("Password" , password = std::string("guestpass"));
00055   declareProperty("BossVer" , m_bossver = std::string("default"));
00056   declareProperty("VerPar" , m_verpar = std::string("default"));
00057   declareProperty("BossRelease",m_bossRelease = std::string("default"));
00058 }
00059 
00060 VertexDbSvc::~VertexDbSvc(){
00061 }
00062 
00063 StatusCode VertexDbSvc::queryInterface(const InterfaceID& riid, void** ppvInterface){
00064      if( IID_IVertexDbSvc.versionMatch(riid) ){
00065           *ppvInterface = static_cast<IVertexDbSvc*> (this);
00066      } else{
00067           return Service::queryInterface(riid, ppvInterface);
00068      }
00069      return StatusCode::SUCCESS;
00070 }
00071 
00072 StatusCode VertexDbSvc::initialize(){
00073   MsgStream log(messageService(), name());
00074   log << MSG::INFO << "VertexDbSvc::initialize()" << endreq;
00075 
00076   StatusCode sc = Service::initialize();
00077   if( sc.isFailure() ) return sc;
00078   
00079    
00080   IIncidentSvc* incsvc;
00081   sc = service("IncidentSvc", incsvc);
00082   int priority = 100;
00083   if( sc.isSuccess() ){
00084     incsvc -> addListener(this, "NewRun", priority);
00085   }
00086 
00087   sc = serviceLocator()->service("DatabaseSvc",m_dbsvc,true);
00088   if (sc .isFailure() ) {
00089     log << MSG::ERROR << "Unable to find DatabaseSvc " << endreq;
00090     return sc;
00091   }
00092 
00093   sc = serviceLocator()->service("EventDataSvc", m_eventSvc, true);
00094   if (sc .isFailure() ) {
00095     log << MSG::ERROR << "Unable to find EventDataSvc " << endreq;
00096     return sc;
00097   }
00098   return StatusCode::SUCCESS;
00099 }
00100 
00101 StatusCode VertexDbSvc::finalize(){
00102      MsgStream log(messageService(), name());
00103      log << MSG::INFO << "VertexDbSvc::finalize()" << endreq;
00104    //  if(m_connect_offline) delete m_connect_offline;
00105     return StatusCode::SUCCESS;
00106 }
00107 
00108 void VertexDbSvc::handle(const Incident& inc){
00109      MsgStream log( messageService(), name() );
00110      log << MSG::DEBUG << "handle: " << inc.type() << endreq;
00111 
00112      if ( inc.type() == "NewRun" ){
00113           log << MSG::DEBUG << "NewRun" << endreq;
00114          getVertexTableInfo();
00115      } 
00116 }
00117 
00118 
00119 #else
00120 // -------------------------- BEAN ------------------------------------
00121 
00122 VertexDbSvc* VertexDbSvc::m_vdb = 0;
00123 
00124 //-----------------------------------------------------------------------------
00125 VertexDbSvc::VertexDbSvc()
00126 //-----------------------------------------------------------------------------
00127 {
00128   // use functions instead of "declareProperty"
00129   dbName    = "offlinedb";
00130 #ifdef ROOTEVENTDATAVERSION
00131   m_bossver = ROOTEVENTDATAVERSION;
00132   m_bossRelease = ROOTEVENTDATAVERSION;
00133 #else
00134   m_bossver = "default";
00135   m_bossRelease = "default";
00136 #endif
00137   m_verpar  = "default";
00138   
00139   m_dbsvc = DatabaseSvc::instance();
00140 }
00141 
00142 //-----------------------------------------------------------------------------
00143 void VertexDbSvc::handle(int new_run)
00144 //-----------------------------------------------------------------------------
00145 {
00146   static int save_run = 0;
00147   if( new_run == save_run ) return;
00148 
00149   cout << "Begin New Run " << new_run << endl;
00150   getVertexTableInfo(new_run);
00151   save_run = new_run;
00152 }
00153 #endif
00154 
00155 double* VertexDbSvc::PrimaryVertex() 
00156 {
00157   if( !m_isRunNumberValid ) {
00158     cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
00159     memset(m_primaryVertex,0,sizeof(m_primaryVertex));
00160   }
00161   return m_primaryVertex;
00162 }
00163 
00164 double* VertexDbSvc::SigmaPrimaryVertex() 
00165 {
00166   if( !m_isRunNumberValid ) {
00167     cerr << "WARNING in VertexDbSvc: runNo is invalid!\n";
00168     memset(m_sigmaPrimaryVertex,0,sizeof(m_sigmaPrimaryVertex));
00169   }
00170   return m_sigmaPrimaryVertex;
00171 }
00172 
00173 #ifndef BEAN
00174 StatusCode VertexDbSvc::getVertexTableInfo(){
00175   MsgStream log(messageService(), name());
00176   SmartDataPtr<Event::EventHeader> eventHeader(m_eventSvc,"/Event/EventHeader");
00177   int  run = eventHeader->runNumber();
00178 #else
00179 //-----------------------------------------------------------------------------
00180 void VertexDbSvc::getVertexTableInfo(int run)
00181 //-----------------------------------------------------------------------------
00182 {
00183 #endif
00184   m_isRunNumberValid = false;
00185   int save_run = run;
00186 
00187   if( run < 0 ) {
00188 #ifndef BEAN
00189     log << MSG::INFO << "This data is the MC sample with the Run Number: " << run << endreq;
00190 #else
00191     cout << "This data is the MC sample with the Run Number: " << run << endl;
00192 #endif
00193     run = -run;
00194   }
00195 //#ifndef BEAN
00196 //  if(m_bossver=="default") m_bossver = getenv("BES_RELEASE");
00197 //#endif
00198 
00199   bool ret_vtx = getReadBunchInfo(run);
00200 
00201   if( !ret_vtx ) {
00202     cout << " VertexDbSvc:: can not found vertex information for run:"
00203          << run << ", boss version " << m_bossver << endl;
00204     exit(1);
00205   }
00206 
00207 /*  if( !ret_vtx && save_run<0 ) {
00208     bool ret = false;
00209     int real_run = run;
00210     for(int kk = 1; kk <= 10000; kk++) {
00211        real_run = run+kk;
00212        if( (ret = getReadBunchInfo(real_run)) ) break;
00213 
00214        if( run-kk > 0 ) { 
00215          real_run = run-kk;
00216          if( (ret = getReadBunchInfo(real_run)) ) break;
00217        }
00218     }
00219     if( !ret ) {
00220 #ifndef BEAN
00221       log << MSG::ERROR << "Can not find vertex information for run:" <<run<< endreq;
00222 #else
00223       cout << "Can not find vertex information for run:" << run << endl;
00224 #endif
00225       exit(1);
00226     }
00227 #ifndef BEAN
00228     log << MSG::INFO << "Use Bunch infor. of run " << real_run 
00229         << " instead of run " << run << endreq;
00230 #else
00231     cout << "Use Bunch infor. of run " << real_run
00232          << " instead of run " << run << endl;
00233 #endif
00234   }
00235 */
00236   m_isRunNumberValid = true;
00237 #ifndef BEAN
00238   log << MSG::INFO << "Successfully fetch the vertex information for run: " 
00239       << save_run << endreq;
00240   return StatusCode::SUCCESS;
00241 #else
00242   cout << "Successfully fetch the vertex information for run: " 
00243        << save_run << endl;
00244 #endif
00245 }
00246 
00247 //-----------------------------------------------------------------------------
00248 bool VertexDbSvc::getReadBunchInfo(int run)
00249 //-----------------------------------------------------------------------------
00250 {
00251   if(m_bossver == "default") {
00252     if(m_bossRelease == "default") {
00253 #ifndef BEAN
00254       MsgStream log(messageService(), name());
00255       log << MSG::FATAL << "ERROR BossRelease must be set! Current value is " 
00256           << m_bossRelease << "." << endreq;
00257 #else
00258       cout << "ERROR BossRelease must be set! Current value is " 
00259            << m_bossRelease << "." << endl;
00260 #endif
00261       exit(1);
00262     }
00263     else {
00264       char stmt1[400];
00265       sprintf(stmt1, "select SftVer, ParVer from CalVtxLumVer where BossRelease = '%s' and RunFrom <= %d and RunTo >= %d and DataType = 'LumVtx'", m_bossRelease.c_str(), run, run);
00266 
00267       DatabaseRecordVector records;
00268       int rowNo = m_dbsvc->query("offlinedb",stmt1,records);
00269       if(rowNo == 0) {
00270 #ifndef BEAN
00271         MsgStream log(messageService(), name());
00272         log << MSG::ERROR << "ERROR: can not find records for run = " << run 
00273             << " and BossRelease = " << m_bossRelease << endreq;
00274 #else
00275         cout << "ERROR: can not find records for run = " << run 
00276              << " and BossRelease = " << m_bossRelease << endl;
00277 #endif
00278         exit(1);
00279       }
00280       DatabaseRecord* recordst = records[0];
00281       m_bossver = recordst->GetString("SftVer");
00282       m_verpar =  recordst->GetString("ParVer");
00283       cout << "Using the SftVer and ParVer (" << m_bossver 
00284            << ", " << m_verpar << ") for run " << run << ". " << endl;
00285     }
00286   }
00287 
00288   string stmt = "select Vx, Vy, Vz, SigmaVx, SigmaVy, SigmaVz ";
00289   stringstream tmp;
00290   tmp << "from BeamPar where RunNo = " << run
00291       << " and SftVer=\'" << m_bossver << "\'";
00292   if( m_verpar == "default" ) {
00293     tmp << " group by ParVer";
00294   } else {
00295     tmp << " and ParVer = " << m_verpar;
00296   }
00297   stmt += tmp.str();
00298   // cerr << "query(" << dbName << ", " << stmt << ", res);" << endl;
00299 
00300   DatabaseRecordVector res;
00301   int row_no = m_dbsvc->query(dbName,stmt,res);
00302 
00303   if( row_no > 0 ) {
00304     DatabaseRecord& dbrec = *res[row_no-1];
00305     m_primaryVertex[0] = dbrec.GetDouble("Vx");
00306     m_primaryVertex[1] = dbrec.GetDouble("Vy");
00307     m_primaryVertex[2] = dbrec.GetDouble("Vz");
00308     m_sigmaPrimaryVertex[0] = dbrec.GetDouble("SigmaVx");
00309     m_sigmaPrimaryVertex[1] = dbrec.GetDouble("SigmaVy");
00310     m_sigmaPrimaryVertex[2] = dbrec.GetDouble("SigmaVz");
00311     return true;
00312   }
00313   
00314   return false;
00315 }

Generated on Tue Nov 29 22:57:40 2016 for BOSS_7.0.2 by  doxygen 1.4.7