00001 #include <iostream.h>
00002 #include <fstream.h>
00003
00004 void fms_db_map(char* opt) {
00005
00006
00007
00008 TString storeTime = "2007-11-09 00:00:00";
00009
00010
00011 int readDate = 20080301;
00012 int readTime = 0;
00013
00014 TString option(opt);
00015 std::cout << "Opt =" << opt << "\n";
00016 std::cout << "testinput = " << option.Contains("testinput") << "\n";
00017 std::cout << "readtext = " << option.Contains("readtext") << "\n";
00018 std::cout << "readdb = " << option.Contains("readdb") << "\n";
00019 std::cout << "writedb = " << option.Contains("writedb") << "\n";
00020 std::cout << "writetext = " << option.Contains("writetext") << "\n";
00021
00022 gROOT->Macro("LoadLogger.C");
00023 gSystem->Load("St_base.so");
00024 gSystem->Load("libStDb_Tables.so");
00025 gSystem->Load("StDbLib.so");
00026
00027
00028 const Int_t MAX = 2000;
00029
00030
00031 fmsMap_st ppmap[MAX];
00032
00033 if(option.Contains("testinput")){
00034
00035 ppmap[0].ppPanel[0] = 2;
00036 ppmap[0].ppColumn[0] = 7;
00037 ppmap[0].ppRow[0] = 15;
00038
00039 ppmap[1].ppPanel[0] = 1;
00040 ppmap[1].ppColumn[0] = 3;
00041 ppmap[1].ppRow[0] = 7;
00042
00043 ppmap[2].ppPanel[0] = 2;
00044 ppmap[2].ppColumn[0] = 20;
00045 ppmap[2].ppRow[0] = 15;
00046
00047 ppmap[3].ppPanel[0] = 1;
00048 ppmap[3].ppColumn[0] = 13;
00049 ppmap[3].ppRow[0] = 9;
00050 }
00051
00052 int module[4] = {1,3,2,4};
00053 int maxch[4] = {578,578,288,288};
00054
00055 if(option.Contains("readtext")){
00056 char* file="qtmap2pp.txt";
00057 FILE* fp;
00058 int rmod,rch,pp,col,row;
00059 cout << "Reading "<<file<<"\n";
00060 if(fp=fopen(file,"r")){
00061 for(int mod=0; mod<4; mod++){
00062 for(int ch=0; ch<maxch[module[mod]-1]; ch++){
00063 if(!feof(fp)) fscanf(fp,"%d %d %d %d %d",&rmod,&rch,&pp,&row,&col);
00064
00065 if(rmod!=module[mod]) cout << "Error mod# ="<<rmod<<" "<<module[mod]<<"\n";
00066 if(rch !=ch +1) cout << "Error ch# ="<<rch <<" "<<ch+1<<"\n";
00067 ppmap[mod].ppPanel[ch]=pp;
00068 ppmap[mod].ppRow[ch]=row;
00069 ppmap[mod].ppColumn[ch]=col;
00070 }
00071 }
00072 }
00073 fclose(fp);
00074 }
00075
00076 if(option.Contains("readdb")){
00077 gSystem->Load("StChain");
00078 gSystem->Load("StBFChain");
00079 gSystem->Load("StUtilities");
00080 gSystem->Load("StIOMaker");
00081 gSystem->Load("StarClassLibrary");
00082 gSystem->Load("St_Tables");
00083 gSystem->Load("StDbLib");
00084 gSystem->Load("StDbBroker");
00085 gSystem->Load("St_db_Maker");
00086
00087 St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
00088 dbMk->SetDebug();
00089 dbMk->SetDateTime(readDate,readTime);
00090 dbMk->SetFlavor("ofl");
00091
00092 dbMk->Init();
00093 dbMk->Make();
00094
00095
00096 TDataSet *DB = 0;
00097
00098
00099 DB = dbMk->GetInputDB("Calibrations/fms/mapping");
00100 if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
00101
00102
00103 St_fmsPatchPanelMap *dbppmap = 0;
00104 dbppmap = (St_fmsPatchPanelMap*) DB->Find("fmsPatchPanelMap");
00105
00106 if (dbppmap) {
00107 std::cout << "Reading fmsPatchPanelMap table\n";
00108 fmsPatchPanelMap_st *pptable = dbppmap->GetTable();
00109 for(int mod=0; mod<4; mod++){
00110 for(int ch=0; ch<maxch[module[mod]-1]; ch++){
00111 printf("%3d %3d %3d %3d %3d\n",module[mod],ch+1,
00112 int(pptable[mod].ppPanel[ch]),
00113 int(pptable[mod].ppRow[ch]),
00114 int(pptable[mod].ppColumn[ch]) );
00115 }
00116 }
00117 memcpy(ppmap,pptable,sizeof(ppmap));
00118 } else {
00119 std::cout << "WARNING: No data in fmsPatchPanelMap table (wrong timestamp?). Nothing to return, then.\n";
00120 }
00121 }
00122
00123 if(option.Contains("writetext")){
00124 char* file="qtmap2pp_out.txt";
00125 FILE* fp;
00126 cout << "Writing "<<file<<"\n";
00127 if(fp=fopen(file,"w")){
00128 for(int mod=0; mod<4; mod++){
00129 for(int ch=0; ch<maxch[module[mod]-1]; ch++){
00130 fprintf(fp,"%3d %3d %3d %3d %3d\n",module[mod],ch+1,
00131 ppmap[mod].ppPanel[ch],
00132 ppmap[mod].ppRow[ch],
00133 ppmap[mod].ppColumn[ch]);
00134 }
00135 }
00136 }
00137 fclose(fp);
00138 }
00139
00140 if(option.Contains("writedb")) {
00141
00142
00143
00144 StDbManager* mgr = StDbManager::Instance();
00145 StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
00146 StDbTable* table = node->addDbTable("fmsPatchPanelMap");
00147 mgr->setStoreTime(storeTime.Data());
00148
00149 table->SetTable((char*)&ppmap,MAX_PP_MAP);
00150
00151
00152 mgr->storeDbTable(table);
00153
00154 std::cout << "Done with database upload \n";
00155 }
00156 }
00157