StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fms_db_fmsgaincorrB_chong.C
1 #include <iostream.h>
2 #include <fstream.h>
3 
4 // max index dimensions
5 static const Int_t MAX = 2500;
6 static const Int_t MAXD = 14;
7 static const Int_t MAXCH= 578;
8 
9 int getDetectorId(int ew, int nstb) {return (ew-1)*8+nstb-1;}
10 int getEW(int detid) {return detid/8 + 1;}
11 int getNSTB(int detid) {return detid%8 + 1;}
12 
13 void fms_db_fmsgaincorrB_chong(char* option = "",
14  char* filename = "chong/FmsGainCorr_16066033.txt",
15  char* flavor="fmsGainCorr-BNL-C",
16  int secOffset=10){
17 
18 
19  TString opt(option), file(filename);
20  int idx=file.Index("_");
21  TString f2=file(idx+1,8);
22  int run=f2.Atoi();
23  int year=run/1000000-1;
24  int port=3400+year-1;
25  printf("%s run=%d year=%d port=%d\n",file.Data(),run,year,port);
26 
27  gROOT->Macro("loadlib.C");
28  gSystem->Load("St_base.so");
29  gSystem->Load("libStDb_Tables.so");
30  gSystem->Load("StDbLib.so");
31 
32  char cmd[300]=Form("mysql -h db04.star.bnl.gov --port=%d -N -s -e \"SELECT startRunTime FROM RunLog.runDescriptor WHERE runNumber=%d LIMIT 1\"",
33  port,run);
34  TString st=gSystem->GetFromPipe(cmd);
35  int starttime=st.Atoi();
36  int readDate=gSystem->GetFromPipe(Form("date -u -d \@%d +%%Y%%m%%d",starttime)).Atoi();
37  int readTime=gSystem->GetFromPipe(Form("date -u -d \@%d +%%H%%M%%S",starttime)).Atoi();
38  printf("For run %d, online DB run starttime=%d - %d(secOffset) and DB readig date=%d time=%d\n",
39  run,starttime,secOffset,readDate,readTime);
40 
41  // structure to fill up
42  fmsGainCorrectionB_st corr;
43  memset(&corr,0,sizeof(corr));
44 
45  FILE* fp;
46  int rew,rnstb,rch;
47  float rcorr;
48  int nread = 0;
49  cout << "Reading "<<filename<<"\n";
50  if(fp=fopen(filename,"r")){
51  while(fscanf(fp,"%d %d %d %f",&rew,&rnstb,&rch,&rcorr) != EOF){
52  int detid=getDetectorId(rew,rnstb);
53  if(detid<0 || rch<1) {
54  printf("Invalid ch#: det=%d ew=%d nstb=%d rch=%d\n",detid,rew,rnstb,rch,rcorr);
55  continue; //hack! ignore
56  }
57  if(rew==1) continue; //hack! ignore east
58  corr.detectorId[nread]=detid;
59  corr.ch[nread]=rch;
60  corr.corr[nread]=rcorr;
61  printf("n=%4d det=%2d ew=%1d nstb=%1d ch=%3d corr=%8.6f\n",nread,detid,rew,rnstb,rch,rcorr);
62  nread++;
63  }
64  }
65  fclose(fp);
66  printf("read %d channels of gain corr\n",nread);
67 
68  if(opt.Contains("writedb")) {
69  gSystem->Setenv("DB_ACCESS_MODE","write");
70  //putenv("DB_ACCESS_MODE=write");
71  //char* env = getenv("DB_ACCESS_MODE");
72  //cout << "Setting DB_ACCESS_MODE " << env << endl;
74  StDbConfigNode* node = mgr->initConfig("Calibrations_fms");
75  StDbTable* table = node->addDbTable("fmsGainCorrectionB");
76  mgr->setStoreTime(starttime - secOffset);
77  // store data in the table
78  table->SetTable((char*)&corr,1);
79  // set store time
80  // store table in dBase
81  table->setFlavor(flavor);
82  mgr->storeDbTable(table);
83  //StDbTable* table = node->findTable("fmsGain");
84  //node->removeTable(table);
85  gSystem->Unsetenv("DB_ACCESS_MODE");
86  //unsetenv("DB_ACCESS_MODE");
87  std::cout << "Done with database upload"<< endl;
88  }
89 
90  if(opt.Contains("readdb")){
91  gSystem->Load("StChain");
92  gSystem->Load("StBFChain");
93  gSystem->Load("StUtilities");
94  gSystem->Load("StIOMaker");
95  gSystem->Load("StarClassLibrary");
96  gSystem->Load("St_Tables");
97  gSystem->Load("StDbLib");
98  gSystem->Load("StDbBroker");
99  gSystem->Load("St_db_Maker");
100 
101  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
102  dbMk->SetDebug();
103  dbMk->SetDateTime(readDate,readTime);
104  dbMk->SetFlavor(flavor);
105  //dbMk->SetFlavor("ofl");
106  dbMk->Init();
107  dbMk->Make();
108 
109  // this is done inside ::Make method
110  TDataSet *DB = 0;
111  // "dbMk->" will NOT be needed.
112  // if done inside your FmsDbMaker. Simply use DB = GetInputDb("Calibrations/fms/mapping")
113  DB = dbMk->GetInputDB("Calibrations/fms");
114  if (!DB) { std::cout << "ERROR: no db maker?" << std::endl; }
115 
116  // fetch ROOT descriptor of db table
117  St_fmsGainCorrectionB *dbppmap = 0;
118  dbppmap = (St_fmsGainCorrectionB*) DB->Find("fmsGainCorrectionB");
119  // fetch data and place it to appropriate structure
120  int ngain = 0;
121  if (dbppmap) {
122  std::cout << "Reading fmsGainCorrectionB table\n";
123  fmsGainCorrectionB_st *pptable = dbppmap->GetTable();
124  for(int i=0; i<MAX; i++){
125  if(pptable[0].ch[i]>0){
126  printf("%3d%8d%8d%8.4f\n",
127  int(pptable[0].detectorId[i]/8+1),int(pptable[0].detectorId[i]%8+1),
128  int(pptable[0].ch[i]),float(pptable[0].corr[i]) );
129  ngain++;
130  }
131  }
132  } else {
133  std::cout << "WARNING: No data in fmsGainCorr table (wrong timestamp?). Nothing to return, then.\n";
134  }
135  }
136 }
137 
virtual Int_t Make()
virtual void SetTable(char *data, int nrows, int *idList=0)
calloc&#39;d version of data for StRoot
Definition: StDbTable.cc:550
static StDbManager * Instance()
strdup(..) is not ANSI
Definition: StDbManager.cc:155
virtual TDataSet * Find(const char *path) const
Definition: TDataSet.cxx:362