00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <iostream>
00004 #include <fstream>
00005 #include "TH1F.h"
00006 #include "TFile.h"
00007 #include "db_mysql.h"
00008 #include "TF1.h"
00009 using namespace std;
00010 db_mysql sqlonline;
00011 db_mysql sqloffline;
00012 int runNo;
00013 char* offline_user;
00014 char* offline_passwd;
00015 char* file;
00016 const char* server_ip = "bes3db1.ihep.ac.cn";
00017 char* Creator=getenv("USER");
00018 char* SftVer=getenv("BES_RELEASE");
00019 char* ParVer;
00020 char* LumTau_txt;
00021 bool ISNeedReFit=0;
00022 bool ISNeedReFitX=0;
00023 bool ISNeedReFitY=0;
00024 bool ISNeedReFitZ=0;
00025 int main(int argc, char* argv[]){
00026
00027 int WriteLumDB();
00028 int WriteBeamParToDb();
00029 int ReWriteBeamParToDb();
00030 int ReadFromDB();
00031 int WriteLumTauDB();
00032
00033 offline_user=argv[1];
00034 offline_passwd=argv[2];
00035 char* runNo_temp=argv[3];
00036 runNo=atoi(runNo_temp);
00037 ParVer=argv[4];
00038 file=argv[5];
00039 LumTau_txt = argv[6];
00040 std::cout<<"-----------------------------------"<<runNo<<"-------------------------"<<std::endl;
00041 sqloffline.Init(server_ip,offline_user,offline_passwd);
00042 sqloffline.Connect();
00043 sqloffline.SelectDB("offlinedb");
00044
00045 WriteLumDB();
00046 WriteBeamParToDb();
00047 if(ISNeedReFit){ReWriteBeamParToDb();}
00048 ReadFromDB();
00049 WriteLumTauDB();
00050
00051 sqloffline.Close();
00052
00053 return 1;
00054 }
00055
00056 int WriteLumDB(){
00057 char Notes[]="";
00058 TFile f(file);
00059 double LumValue;
00060 TH1F* lum;
00061 f.GetObject("/zhsLUM/lum",lum);
00062 LumValue=lum->GetBinContent(3);
00063 char sql_input[350];
00064 sqlonline.Init("202.122.33.123","guest","guestpass");
00065 sqlonline.Connect();
00066 sqlonline.SelectDB("RunInfo");
00067 sprintf(sql_input,"select IntLumEndcapEE from OnlineLum where run_number='%d'",runNo);
00068 sqlonline.Execute(sql_input);
00069 std::string intLumEndcapEE;
00070 if(sqlonline.GetRow(0)){
00071 intLumEndcapEE=sqlonline.GetField(0);
00072 }
00073 else intLumEndcapEE="-0";
00074 sqlonline.Close();
00075
00076
00077
00078 sprintf(sql_input,"delete from OfflineLum where RunNo=%d && SftVer='%s' && ParVer='%s'",runNo,SftVer,ParVer);
00079 sqloffline.Execute(sql_input);
00080
00081 sprintf(sql_input,"insert into OfflineLum(SerNo,RunNo,IntLumEndcapEE,OfflineTwoGam,SftVer,ParVer,Creator,CreateTime,Notes) values('','%d','%s','%f','%s','%s','%s',now(),'%s')",runNo,intLumEndcapEE.c_str(),LumValue,SftVer,ParVer,Creator,Notes);
00082 sqloffline.Execute(sql_input);
00083 return 1;
00084 }
00085
00086 int WriteBeamParToDb(){
00087 TFile f(file);
00088 TH1D* x_of_vertex_in_kal;
00089 TH1D* y_of_vertex_in_kal;
00090 TH1D* z_of_vertex_in_kal;
00091 f.GetObject("/zhsVER/x_of_vertex_in_kal",x_of_vertex_in_kal);
00092 f.GetObject("/zhsVER/y_of_vertex_in_kal",y_of_vertex_in_kal);
00093 f.GetObject("/zhsVER/z_of_vertex_in_kal",z_of_vertex_in_kal);
00094 TF1 *func;
00095 TF1 *funcY;
00096 TF1 *funcZ;
00097 if(runNo<=10878 || runNo>20433){
00098 func = new TF1("func", "gaus", -0.6, 0.6);
00099 funcY = new TF1("funcY", "gaus", -0.6, 0.2);
00100 funcZ = new TF1("funcZ", "gaus", -6, 6);
00101 }
00102 else if(runNo >10878 && runNo<= 20433){
00103 func = new TF1("func", "gaus", 0., 1.8);
00104 funcY = new TF1("funcY", "gaus", -1., 1.);
00105 funcZ = new TF1("funcZ", "gaus", -6, 6);
00106 }
00107 x_of_vertex_in_kal->Fit("func", "RQ");
00108 Double_t MeanXKal = func->GetParameter(1);
00109 Double_t SigmaXKal = func->GetParameter(2);
00110
00111 y_of_vertex_in_kal->Fit("funcY", "RQ");
00112 Double_t MeanYKal = funcY->GetParameter(1);
00113 Double_t SigmaYKal = funcY->GetParameter(2);
00114
00115 z_of_vertex_in_kal->Fit("funcZ", "RQ");
00116 Double_t MeanZKal = funcZ->GetParameter(1);
00117 Double_t SigmaZKal = funcZ->GetParameter(2);
00118 if(runNo<=10878 || runNo>20433){
00119 if(MeanXKal<-0.05 || MeanXKal>0.25 || SigmaXKal>0.12 || SigmaXKal<0.){
00120 std::cout<<"--------------x_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00121 ISNeedReFitX=1;ISNeedReFit=1;
00122 }
00123 if(MeanYKal<-0.4 || MeanYKal>0 || SigmaYKal>0.12 || SigmaYKal<0)
00124 {
00125 std::cout<<"--------------y_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00126 ISNeedReFitY=1;ISNeedReFit=1;
00127 }
00128 if(MeanZKal<-1 || MeanZKal>1.0 || SigmaZKal>1.5 || SigmaZKal<0)
00129 {
00130 std::cout<<"--------------z_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00131 ISNeedReFitZ=1;ISNeedReFit=1;
00132 }
00133 }
00134 else{
00135 if(MeanXKal<0.6 || MeanXKal>1.2 || SigmaXKal>0.18 || SigmaXKal<0.){
00136 std::cout<<"--------------x_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00137 ISNeedReFitX=1;ISNeedReFit=1;
00138 }
00139 if(MeanYKal<-0.4 || MeanYKal>0 || SigmaYKal>0.12 || SigmaYKal<0.)
00140 {
00141 std::cout<<"--------------y_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00142 ISNeedReFitY=1;ISNeedReFit=1;
00143 }
00144 if(MeanZKal<-0.8 || MeanZKal>0.8 || SigmaZKal>1.5 || SigmaZKal<0.)
00145 {
00146 std::cout<<"--------------z_of_vertex_in_kal-------out of range--runID is:"<<runNo<<std::endl;
00147 ISNeedReFitZ=1;ISNeedReFit=1;
00148 }
00149
00150 }
00151 char VerFitFunc[] = "Kalman";
00152 char DistFitFunc[] = "single Gaussian";
00153
00154 double AcSigmaVx, AcSigmaVy;
00155
00156 AcSigmaVx = 0.038;
00157 AcSigmaVy = 0.00057;
00158 char query[1024*100];
00159
00160 sprintf(query, "DELETE FROM BeamPar where RunNo = '%d' && SftVer='%s' && ParVer='%s'", runNo,SftVer,ParVer);
00161 sqloffline.Execute(query);
00162 sprintf(query,"INSERT INTO BeamPar(BeamParSerNo,RunNo,Vx,Vy,Vz,SigmaVx,SigmaVy,SigmaVz,VerFitFunc,DistFitFunc,SftVer,Creator,AcSigmaVx,AcSigmaVy,ParVer) values('','%d','%f','%f','%f','%f','%f','%f','%s','%s','%s','%s','%f','%f','%s')",runNo,MeanXKal,MeanYKal,MeanZKal,SigmaXKal,SigmaYKal,SigmaZKal,VerFitFunc,DistFitFunc,SftVer,Creator,AcSigmaVx,AcSigmaVy,ParVer);
00163 sqloffline.Execute(query);
00164
00165 return 1;
00166
00167 }
00168 int ReWriteBeamParToDb(){
00169 char query[1024*100];
00170 int irun=1;
00171 std::string Vx;
00172 std::string Vy;
00173 std::string Vz;
00174 std::string SigmaVx;
00175 std::string SigmaVy;
00176 std::string SigmaVz;
00177 bool Runloop=0;
00178 do{
00179 sprintf(query,"select Vx,Vy,Vz,SigmaVx,SigmaVy,SigmaVz,RunNo,SftVer,ParVer from BeamPar where RunNo='%d' && SftVer='%s' && ParVer='%s'",runNo-irun,SftVer,ParVer);
00180 sqloffline.Execute(query);
00181 if(sqloffline.GetRow(0)){
00182 Vx=sqloffline.GetField(0);
00183 Vy=sqloffline.GetField(1);
00184 Vz=sqloffline.GetField(2);
00185 SigmaVx=sqloffline.GetField(3);
00186 SigmaVy=sqloffline.GetField(4);
00187 SigmaVz=sqloffline.GetField(5);
00188 Runloop=0;
00189 }
00190 else Runloop=1;
00191 irun++;
00192 }while(Runloop);
00193 if(ISNeedReFitX){
00194 sprintf(query,"update BeamPar set Vx='%s',SigmaVx='%s' where RunNo='%d' && SftVer='%s' && ParVer='%s'",Vx.c_str(),SigmaVx.c_str(),runNo,SftVer,ParVer);
00195 sqloffline.Execute(query);
00196
00197 std::cout<<"------------------update Vx,SigmaVx successful!--------"<<std::endl;
00198 }
00199 if(ISNeedReFitY){
00200 sprintf(query,"update BeamPar set Vy='%s',SigmaVy='%s' where RunNo='%d' && SftVer='%s' && ParVer='%s'",Vy.c_str(),SigmaVy.c_str(),runNo,SftVer,ParVer);
00201 sqloffline.Execute(query);
00202
00203 std::cout<<"------------------update Vy,SigmaVy successful!---------"<<std::endl;
00204 }
00205 if(ISNeedReFitZ){
00206 sprintf(query,"update BeamPar set Vz='%s',SigmaVz='%s' where RunNo='%d' && SftVer='%s' && ParVer='%s'",Vz.c_str(),SigmaVz.c_str(),runNo,SftVer,ParVer);
00207 sqloffline.Execute(query);
00208
00209 std::cout<<"------------------update Vz,SigmaVz successful!---------"<<std::endl;
00210 }
00211
00212
00213 return 1;
00214 }
00215 int ReadFromDB(){
00216 std::cout<<"--------------check RUN LUM and Vertex info -----------------------------------"<<std::endl;
00217 char sql_input[550];
00218 sprintf(sql_input,"select SerNo,RunNo,IntLumEndcapEE,OfflineTwoGam,SftVer,ParVer,Creator,CreateTime,Notes from OfflineLum where RunNo=%d && SftVer='%s' && ParVer='%s'",runNo,SftVer,ParVer);
00219 sqloffline.Execute(sql_input);
00220 std::cout<<"LUM is:"<<"SerNo,RunNo,IntLumEndcapEE,OfflineTwoGam,SftVer,ParVer,Creator,CreateTime,Notes"<<std::endl;
00221 if(sqloffline.GetRow(0)){
00222 std::cout<<sqloffline.GetField(0)<<","<<sqloffline.GetField(1)<<","<<sqloffline.GetField(2)<<","<<sqloffline.GetField(3)<<","
00223 <<sqloffline.GetField(4)<<","<<sqloffline.GetField(5)<<","<<sqloffline.GetField(6)<<","<<sqloffline.GetField(7)<<","
00224 <<sqloffline.GetField(8)<<std::endl;
00225 std::cout<<"---------------------------------------------------------------LUM is OK!"<<std::endl;
00226 }
00227 else std::cout<<"-----------------------------------------ERROR to read LUM from OfflineLum db"<<std::endl;
00228
00229 sprintf(sql_input, "select BeamParSerNo,RunNo,Vx,Vy,Vz,SigmaVx,SigmaVy,SigmaVz,VerFitFunc,DistFitFunc,SftVer,Creator,AcSigmaVx,AcSigmaVy,ParVer FROM BeamPar where RunNo ='%d' && SftVer='%s' && ParVer='%s'", runNo,SftVer,ParVer);
00230 sqloffline.Execute(sql_input);
00231 std::cout<<"BeamPar is:"<<"BeamParSerNo,RunNo,Vx,Vy,Vz,SigmaVx,SigmaVy,SigmaVz,VerFitFunc,DistFitFunc,SftVer,Creator,AcSigmaVx,AcSigmaVy,ParVer"<<std::endl;
00232 if(sqloffline.GetRow(0)){
00233 std::cout<<sqloffline.GetField(0)<<","<<sqloffline.GetField(1)<<","<<sqloffline.GetField(2)<<","<<sqloffline.GetField(3)<<","
00234 <<sqloffline.GetField(4)<<","<<sqloffline.GetField(5)<<","<<sqloffline.GetField(6)<<","<<sqloffline.GetField(7)<<","
00235 <<sqloffline.GetField(8)<<","<<sqloffline.GetField(9)<<","<<sqloffline.GetField(10)<<","<<sqloffline.GetField(11)<<","
00236 <<sqloffline.GetField(12)<<","<<sqloffline.GetField(13)<<","<<sqloffline.GetField(14)<<std::endl;
00237 std::cout<<"-------------------------------------------------------BeamPar is OK!"<<std::endl;
00238 }
00239 else std::cout<<"-----------------------------------------ERROR to read Vertex from BeamPar db"<<std::endl;
00240 return 1;
00241 }
00242
00243 int WriteLumTauDB(){
00244 ifstream rwite;
00245 rwite.open(LumTau_txt);
00246 bool file_open = 1;
00247 if(!rwite){
00248 cout<<"************************************"<<endl;
00249 cout<<LumTau_txt<<" not exist!"<<endl;
00250 cout<<"************************************"<<endl;
00251 file_open = 0;
00252 }
00253 if(!file_open) return -1;
00254
00255 int m_runNo = -1;
00256 double runTime = -1;
00257 double Lum_start = -1;
00258 double Lum_end = -1;
00259 double Tau_Value = -1;
00260
00261 while(!rwite.eof()){
00262 rwite>>m_runNo>>runTime>>Lum_start>>Lum_end>>Tau_Value;
00263 if( Tau_Value<0 )
00264 Tau_Value = 99999;
00265 char sql_input[350];
00266 sprintf(sql_input,"update OfflineLum set runTime=%f, Lum_start=%f, Lum_end=%f, Tau_Value=%f where runNo=%d && SftVer='%s' && ParVer='%s' ",runTime,Lum_start,Lum_end,Tau_Value,m_runNo,SftVer,ParVer);
00267 sqloffline.Execute(sql_input);
00268
00269 cout<<"************************************"<<endl;
00270 cout<<"LumTau FOR RUN "<<m_runNo<<" is OK!"<<endl;
00271 cout<<"************************************"<<endl;
00272
00273
00274 return 1;
00275 }
00276 }