00001 #include <iostream.h>
00002 #include <fstream.h>
00003
00004 void fms_db_fmsgaincorr(char* opt, char* dataspec) {
00005
00006
00007 TString data(dataspec);
00008 if(data.Contains("run8") && data.Contains("dAu200"))
00009 TString storeTime = "2007-11-09 12:00:00";
00010 else
00011 if(data.Contains("run8") && data.Contains("pp200"))
00012 TString storeTime = "2008-01-28 12:00:00";
00013 else
00014 if(data.Contains("run9") && data.Contains("pp200"))
00015 TString storeTime = "2009-01-16 00:00:00";
00016 else std::cout<<"Invalid year range"<<std::endl;
00017
00018
00019 int readDate = 20091005;
00020 int readTime = 0;
00021
00022 TString option(opt);
00023 std::cout << "Opt =" << opt << "\n";
00024 std::cout << "testinput = " << option.Contains("testinput") << "\n";
00025 std::cout << "readtext = " << option.Contains("readtext") << "\n";
00026 std::cout << "readdb = " << option.Contains("readdb") << "\n";
00027 std::cout << "writedb = " << option.Contains("writedb") << "\n";
00028 std::cout << "writetext = " << option.Contains("writetext") << "\n";
00029
00030 gROOT->Macro("LoadLogger.C");
00031 gSystem->Load("St_base.so");
00032 gSystem->Load("libStDb_Tables.so");
00033 gSystem->Load("StDbLib.so");
00034
00035
00036 const Int_t MAX = 2500;
00037
00038
00039 fmsGainCorrection_st gaincorr[MAX];
00040
00041 if(option.Contains("testinput")){
00042
00043 gaincorr[0].detectorId = 2;
00044 gaincorr[0].ch = 7;
00045 gaincorr[0].corr = .15;
00046
00047 gaincorr[1].detectorId = 1;
00048 gaincorr[1].ch = 3;
00049 gaincorr[1].corr = .7;
00050
00051 gaincorr[2].detectorId = 2;
00052 gaincorr[2].ch = 20;
00053 gaincorr[2].corr = .15;
00054
00055 gaincorr[3].detectorId = 1;
00056 gaincorr[3].ch = 13;
00057 gaincorr[3].corr = .9;
00058 }
00059
00060 int ew[2] = {0,1};
00061 int detId[14] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
00062 int detType[14] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5};
00063 int detNx[14] = { 7, 7, 7, 7, 48, 48, 1, 1, 17, 17, 12, 12, 9, 9};
00064 int detNy[14] = { 7, 7, 1, 1, 1, 1, 48, 48, 34, 34, 24, 24, 12, 12};
00065
00066
00067 int module[8] = { 1, 2, 5, 6, 1, 2, 3, 4};
00068 int maxch[8] = {49, 49, 7, 7, 578, 578, 288, 288};
00069 int ngc = 0;
00070
00071 if(option.Contains("readtext")){
00072 FILE* fp;
00073 int rew,rmod,rch;
00074 float rgaincorr;
00075 ngc = 0;
00076 cout << "Reading "<<dataspec<<"\n";
00077 if(fp=fopen(dataspec,"r")){
00078 for(int iew=0; iew<2; iew++){
00079 for(int mod=0; mod<4; mod++){
00080 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00081 if(!feof(fp)) fscanf(fp,"%d %d %d %f",&rew,&rmod,&rch,&rgaincorr);
00082
00083 if(rmod!=module[mod+iew*4]) cout << "Error mod# ="<<rmod<<" "<<module[mod+iew*4]<<"\n";
00084 if(rch !=ch0 +1) cout << "Error ch# ="<<rch <<" "<<ch0+1<<"\n";
00085 gaincorr[ngc].detectorId=(rew-1)*8+rmod-1;
00086 gaincorr[ngc].ch=rch;
00087 if(data.Contains("run8"))
00088 gaincorr[ngc].corr=rgaincorr;
00089 else
00090 if(data.Contains("run9"))
00091 gaincorr[ngc].corr=1.;
00092 ngc++;
00093 }
00094 }
00095 }
00096 }
00097 fclose(fp);
00098 }
00099
00100 if(option.Contains("readdb")){
00101 gSystem->Load("StChain");
00102 gSystem->Load("StBFChain");
00103 gSystem->Load("StUtilities");
00104 gSystem->Load("StIOMaker");
00105 gSystem->Load("StarClassLibrary");
00106 gSystem->Load("St_Tables");
00107 gSystem->Load("StDbLib");
00108 gSystem->Load("StDbBroker");
00109 gSystem->Load("St_db_Maker");
00110
00111 St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
00112 dbMk->SetDebug();
00113 dbMk->SetDateTime(readDate,readTime);
00114 dbMk->SetFlavor("ofl");
00115
00116 dbMk->Init();
00117 dbMk->Make();
00118
00119
00120 TDataSet *DB = 0;
00121
00122
00123 DB = dbMk->GetInputDB("Calibrations/fms");
00124 if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
00125
00126
00127 St_fmsGainCorrection *dbppmap = 0;
00128 dbppmap = (St_fmsGainCorrection*) DB->Find("fmsGainCorrection");
00129
00130 ngc = 0;
00131 if (dbppmap) {
00132 std::cout << "Reading fmsGainCorrection table\n";
00133 fmsGainCorrection_st *pptable = dbppmap->GetTable();
00134 for(int iew=0; iew<2; iew++){
00135 for(int mod=0; mod<4; mod++){
00136 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00137 printf("%3d%8d%8d%8.4f\n",int(pptable[ngc].detectorId/8+1),int(pptable[ngc].detectorId%8+1),
00138 int(pptable[ngc].ch),float(pptable[ngc].corr) );
00139 ngc++;
00140 }
00141 }
00142 }
00143 memcpy(gaincorr,pptable,sizeof(gaincorr));
00144 }
00145 else {
00146 std::cout << "WARNING: No data in fmsGainCorrection table (wrong timestamp?). Nothing to return, then.\n";
00147 }
00148 }
00149
00150
00151 if(option.Contains("writetext")){
00152 int ii = data.Length();
00153 data.Insert(ii-4,"_out");
00154 char* file=data.Data();
00155 FILE* fp;
00156 cout << "Writing "<<file<<"\n";
00157 ngc = 0;
00158 if(fp=fopen(file,"w")){
00159 for(int iew=0; iew<2; iew++){
00160 for(int mod=0; mod<4; mod++){
00161 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00162 if(gaincorr[ngc].detectorId/8==0)
00163 fprintf(fp,"%2d%2d%3d%6.2f\n",gaincorr[ngc].detectorId/8+1,gaincorr[ngc].detectorId%8+1,
00164 gaincorr[ngc].ch,gaincorr[ngc].corr);
00165 else
00166 if(gaincorr[ngc].detectorId/8==1)
00167 fprintf(fp,"%3d%8d%8d%8.4f\n",gaincorr[ngc].detectorId/8+1,gaincorr[ngc].detectorId%8+1,
00168 gaincorr[ngc].ch,gaincorr[ngc].corr);
00169 else printf("ERROR: detectorId %d not recognized.\n",gaincorr[ngc].detectorId);
00170 ngc++;
00171 }
00172 }
00173 }
00174 }
00175 fclose(fp);
00176 }
00177
00178 if(option.Contains("writedb")) {
00179 gSystem->Setenv("DB_ACCESS_MODE","write");
00180
00181
00182
00183 StDbManager* mgr = StDbManager::Instance();
00184 StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
00185 StDbTable* table = node->addDbTable("fmsGainCorrection");
00186 mgr->setStoreTime(storeTime.Data());
00187
00188 table->SetTable((char*)&gaincorr,ngc);
00189
00190
00191 mgr->storeDbTable(table);
00192
00193
00194 gSystem->Unsetenv("DB_ACCESS_MODE");
00195
00196 std::cout << "Done with database upload \n";
00197 }
00198 }
00199