00001 #include <iostream.h>
00002 #include <fstream.h>
00003
00004 void fms_db_fmsgain(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 fmsGain_st gain[MAX];
00040
00041 if(option.Contains("testinput")){
00042
00043 gain[0].detectorId = 2;
00044 gain[0].ch = 7;
00045 gain[0].gain = .15;
00046
00047 gain[1].detectorId = 1;
00048 gain[1].ch = 3;
00049 gain[1].gain = .7;
00050
00051 gain[2].detectorId = 2;
00052 gain[2].ch = 20;
00053 gain[2].gain = .15;
00054
00055 gain[3].detectorId = 1;
00056 gain[3].ch = 13;
00057 gain[3].gain = .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 ngain = 0;
00070
00071 if(option.Contains("readtext")){
00072
00073 FILE* fp;
00074 int rew,rmod,rch;
00075 float rgain;
00076 ngain = 0;
00077 cout << "Reading "<<dataspec<<"\n";
00078 if(fp=fopen(dataspec,"r")){
00079 for(int iew=0; iew<2; iew++){
00080 for(int mod=0; mod<4; mod++){
00081 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00082 if(!feof(fp)) fscanf(fp,"%d %d %d %f",&rew,&rmod,&rch,&rgain);
00083
00084 if(rmod!=module[mod+iew*4]) cout << "Error mod# ="<<rmod<<" "<<module[mod+iew*4]<<"\n";
00085 if(rch !=ch0 +1) cout << "Error ch# ="<<rch <<" "<<ch0+1<<"\n";
00086 gain[ngain].detectorId=(rew-1)*8+rmod-1;
00087 gain[ngain].ch=rch;
00088 if(data.Contains("run8"))
00089 gain[ngain].gain=rgain;
00090 else
00091 if(data.Contains("run9"))
00092 gain[ngain].gain=1.;
00093 ngain++;
00094 }
00095 }
00096 }
00097 }
00098 fclose(fp);
00099 }
00100
00101 if(option.Contains("readdb")){
00102 gSystem->Load("StChain");
00103 gSystem->Load("StBFChain");
00104 gSystem->Load("StUtilities");
00105 gSystem->Load("StIOMaker");
00106 gSystem->Load("StarClassLibrary");
00107 gSystem->Load("St_Tables");
00108 gSystem->Load("StDbLib");
00109 gSystem->Load("StDbBroker");
00110 gSystem->Load("St_db_Maker");
00111
00112 St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
00113 dbMk->SetDebug();
00114 dbMk->SetDateTime(readDate,readTime);
00115 dbMk->SetFlavor("ofl");
00116
00117 dbMk->Init();
00118 dbMk->Make();
00119
00120
00121 TDataSet *DB = 0;
00122
00123
00124 DB = dbMk->GetInputDB("Calibrations/fms");
00125 if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
00126
00127
00128 St_fmsGain *dbppmap = 0;
00129 dbppmap = (St_fmsGain*) DB->Find("fmsGain");
00130
00131 ngain = 0;
00132 if (dbppmap) {
00133 std::cout << "Reading fmsGain table\n";
00134 fmsGain_st *pptable = dbppmap->GetTable();
00135 for(int iew=0; iew<2; iew++){
00136 for(int mod=0; mod<4; mod++){
00137 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00138 printf("%3d%8d%8d%8.4f\n",int(pptable[ngain].detectorId/8+1),int(pptable[ngain].detectorId%8+1),
00139 int(pptable[ngain].ch),float(pptable[ngain].gain) );
00140 ngain++;
00141 }
00142 }
00143 }
00144 memcpy(gain,pptable,sizeof(gain));
00145 }
00146 else {
00147 std::cout << "WARNING: No data in fmsGain table (wrong timestamp?). Nothing to return, then.\n";
00148 }
00149 }
00150
00151
00152 if(option.Contains("writetext")){
00153 int ii = data.Length();
00154 data.Insert(ii-4,"_out");
00155 char* file=data.Data();
00156 FILE* fp;
00157 cout << "Writing "<<file<<"\n";
00158 ngain = 0;
00159 if(fp=fopen(file,"w")){
00160 for(int iew=0; iew<2; iew++){
00161 for(int mod=0; mod<4; mod++){
00162 for(int ch0=0; ch0<maxch[mod+iew*4]; ch0++){
00163 if(gain[ngain].detectorId/8==0)
00164 fprintf(fp,"%2d%2d%3d%6.1f\n",gain[ngain].detectorId/8+1,gain[ngain].detectorId%8+1,
00165 gain[ngain].ch,gain[ngain].gain);
00166 else
00167 if(gain[ngain].detectorId/8==1)
00168 fprintf(fp,"%3d%8d%8d%8.4f\n",gain[ngain].detectorId/8+1,gain[ngain].detectorId%8+1,
00169 gain[ngain].ch,gain[ngain].gain);
00170 else printf("ERROR: detectorId %d not recognized.\n",gain[ngain].detectorId);
00171 ngain++;
00172 }
00173 }
00174 }
00175 }
00176 fclose(fp);
00177 }
00178
00179 if(option.Contains("writedb")) {
00180 gSystem->Setenv("DB_ACCESS_MODE","write");
00181
00182
00183
00184 StDbManager* mgr = StDbManager::Instance();
00185 StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
00186 StDbTable* table = node->addDbTable("fmsGain");
00187 mgr->setStoreTime(storeTime.Data());
00188
00189 table->SetTable((char*)&gain,ngain);
00190
00191
00192 mgr->storeDbTable(table);
00193
00194
00195 gSystem->Unsetenv("DB_ACCESS_MODE");
00196
00197 std::cout << "Done with database upload \n";
00198 }
00199 }
00200