StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fcsTimedepGainCorr_db.C
1 #include <iostream.h>
2 #include <fstream.h>
3 #include <string>
4 
5 class StFcsDb;
6 StFcsDb* mFcsDb=0;
7 
8 static const int NPERIOD=9;
9 const int RUN0[NPERIOD+1]={22359013,23005043,23048036,23066056,23073059,23080057,23087057,23094050,23101043,23164000}; //starts of period
10 const int RUN1[NPERIOD] ={23007007,23007007,23048050,23067001,23074017,23081008,23087070,23095010,23101063}; //1st calib RUN near start
11 const int RUN2[NPERIOD] ={23007011,23007011,23048051,23067002,23074018,23081009,23087072,23095011,23101064}; //2nd calib run near start
12 const int RUN3[NPERIOD] ={23048002,23048002,23066013,23073042,23080044,23087033,23094044,23101005,23108014}; //1st calib run near end
13 const int RUN4[NPERIOD] ={23048003,23048003,23066017,23073043,23080045,23087034,23094045,23101013,23108015}; //2nd calib run near end
14 const char *gainfile[NPERIOD][2] ={{"period1/fcsgaincorr_007_final.txt","period1/fcsgaincorr_048P1_final.txt"}, //period0
15  {"period1/fcsgaincorr_007_final.txt","period1/fcsgaincorr_048P1_final.txt"}, //period1
16  {"period2/fcsgaincorr_048_03.txt", "period2/fcsgaincorr_066_03.txt"}, //period2
17  {"period3/fcsgaincorr_067_05.txt", "period3/fcsgaincorr_073_06.txt"}, //period3
18  {"period4/fcsgaincorr_074_final.txt","period4/fcsgaincorr_080_final.txt"}, //period4
19  {"period5/fcsgaincorr_081_03.txt", "period5/fcsgaincorr_087_04.txt"}, //period5
20  {"period6/fcsgaincorr_70_3.txt", "period6/fcsgaincorr_94_3.txt"}, //period6
21  {"period7/fcsgaincorr_095_2.txt", "period7/fcsgaincorr_101_3.txt"}, //period7
22  {"period8/fcsgaincorr_101_4.txt", "period8/fcsgaincorr_108_4.txt"}}; //period8
23 const int NIDX=1496;
24 float mGainCorrCalib[NIDX][2];
25 float mGainCorr[NIDX];
26 const int showidx=500;
27 
28 const int MAXSCL=350000;
29 const int MAXRUN=10000;
30 const int LIMIT[4] = {5e6,1e7,2e6,2e6};
31 const int STARTRUN=22354029;
32 
33 unsigned int NDATA=0;
34 unsigned int TIME[MAXSCL];
35 double BBCW[MAXSCL];
36 double BBCA[MAXSCL];
37 double ZDCW[MAXSCL];
38 double ZDCA[MAXSCL];
39 
40 unsigned int NRUN=0;
41 unsigned int RUNT[MAXRUN];
42 unsigned int RUNN[MAXRUN];
43 double ITGL[MAXRUN];
44 
45 
46 //read gain file
47 void readGainCorr(int period){
48  for(int i=0; i<2; i++){
49  printf("Reading GainCorr from %s\n",gainfile[period][i]);
50  FILE* F=fopen(gainfile[period][i],"r");
51  if(F == NULL){
52  printf("Could not open %s\n",gainfile[period][i]);
53  return;
54  }
55  int ehp,ns,dep,ch;
56  float gain;
57  while(fscanf(F,"%d %d %d %d %f",&ehp,&ns,&dep,&ch,&gain) != EOF){
58  int det,id,crt,slt;
59  mFcsDb->getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
60  int idx=det*748 + id;
61  mGainCorrCalib[idx][i]=gain;
62  if(idx==showidx){
63  printf("GAINCORR ehp%1d ns%1d dep%02d ch%02d id=%3d idx=%4d start/stop=%1d %f\n",
64  ehp,ns,dep,ch,id,idx,i,mGainCorrCalib[idx][i]);
65  }
66  }
67  fclose(F);
68  }
69 }
70 
71 //write gain file
72 void writeGainCorr(int run){
73  char file[200];
74  sprintf(file,"corr/%d.txt",run);
75  printf("Writing %s\n",file);
76  FILE* F=fopen(file,"w");
77  if(F == NULL){
78  printf("Could not open %s\n",file);
79  return;
80  }
81  int ehp=0;
82  for(int ns=0; ns<2; ns++){
83  for(int dep=0; dep<24; dep++){
84  for(int ch=0; ch<32; ch++){
85  int det,id,crt,slt;
86  mFcsDb->getIdfromDep(ehp,ns,dep,ch,det,id,crt,slt);
87  int idx = -1;
88  float gain=0;
89  if(det>=0 && det<2){
90  idx=det*748 + id;
91  gain=mGainCorr[idx];
92  }
93  fprintf(F,"%1d %1d %2d %2d %f\n",ehp,ns,dep,ch,gain);
94  }
95  }
96  }
97  fclose(F);
98 }
99 
100 // Reading scaler file
101 void readScaler(){
102  char filename[100],line[200];
103  sprintf(filename,"scaler.txt");
104  FILE *F=fopen(filename,"r");
105  if(F==NULL){
106  cout << "Cannot open " << filename << endl;
107  continue;
108  }
109  cout << "Reading " << filename << endl;
110  while(fgets(line, 200, F) != NULL){
111  unsigned int ts;
112  double rs2,rs3,rs7,rs8;
113  char d[100],t[100];
114  sscanf(line,"%lf %lf %lf %lf %d %s %s",&rs2,&rs3,&rs7,&rs8,&ts,d,t);
115  if(rs2>0 || rs3>0 || rs7>0 || rs8>0){
116  //if(rs2 > LIMIT[0]) continue;
117  //if(rs3 > LIMIT[1]) continue;
118  if(rs7 > LIMIT[2]) continue;
119  //if(rs8 > LIMIT[3]) continue;
120  BBCW[NDATA]=rs2;
121  BBCA[NDATA]=rs3;
122  ZDCW[NDATA]=rs7;
123  ZDCA[NDATA]=rs8;
124  TIME[NDATA]=ts;
125  NDATA++;
126  //printf("%d %f %f %f %f %d %s %s\n",NDATA,rs2,rs3,rs7,rs8,ts,d,t);
127  }
128  }
129  fclose(F);
130  printf("Read %d scaler data\n",NDATA);
131 }
132 
133 // Reading run file
134 void readRun(){
135  char filename[100],line[200];
136  sprintf(filename,"runs.txt");
137  FILE *F=fopen(filename,"r");
138  if(F==NULL){
139  cout << "Cannot open " << filename << endl;
140  continue;
141  }
142  cout << "Reading " << filename << endl;
143  while(fgets(line, 200, F) != NULL){
144  unsigned int run, ts;
145  sscanf(line,"%d %d",&run,&ts);
146  if(run>=STARTRUN){
147  RUNN[NRUN]=run;
148  RUNT[NRUN]=ts;
149  //printf("%d %d %d\n",NRUN,run,ts);
150  NRUN++;
151  }
152  }
153  fclose(F);
154  printf("Read %d runs\n",NRUN);
155 }
156 
157 void scaler(){
158  readScaler();
159  readRun();
160 
161  FILE* F=fopen("integralLumi.txt","w");
162  fprintf(F,"# run bbcW bbcE*W zdcW zdcE*W [10^9 counts]\n");
163  unsigned int itime=0;
164  double bbcw=0,bbca=0,zdcw=0,zdca=0;
165  for(unsigned int irun=0; irun<NRUN; irun++){
166  while(TIME[itime] < RUNT[irun]){
167  bbcw += BBCW[itime];
168  bbca += BBCA[itime];
169  zdcw += ZDCW[itime];
170  zdca += ZDCA[itime];
171  //printf("%8d %10.2f %10.2f %10.2f %10.2f TS=%12d %12d %12d %12d \n",RUNN[irun],bbcw,bbca,zdcw,zdca,RUNT[irun],TIME[itime],RUNT[irun]-TIME[itime],itime);
172  itime++;
173  if(itime >= NDATA) break;
174  }
175  double f=30e-9;
176  printf("%8d %14.6f %14.6f %14.6f %14.6f TS=%12d %12d %12d %12d\n",
177  RUNN[irun],bbcw*f,bbca*f,zdcw*f,zdca*f,RUNT[irun],TIME[itime],RUNT[irun]-TIME[itime],itime);
178  fprintf(F,"%8d %14.6f %14.6f %14.6f %14.6f\n",
179  RUNN[irun],bbcw*f,bbca*f,zdcw*f,zdca*f);
180  ITGL[irun]=bbca*f;
181  }
182  fclose(F);
183 
184  c1 = new TCanvas("c1","SCALER",50,0,1500,1200);
185  gStyle->SetLabelSize(0.03,"xy");
186  gStyle->SetPalette(1);
187  gStyle->SetOptStat(0);
188  c1->Divide(1,2);
189  c1->SaveAs("scaler.png");
190 }
191 
192 double getIntgLumi(int run){
193  for(unsigned int irun=0; irun<NRUN; irun++){
194  if(run==RUNN[irun]) return ITGL[irun];
195  }
196  return 0;
197 }
198 
199 //void fcsTimedepGainCorr_db(int period=1, char* opt = "writetxt", int onlydorun=23048002) {
200 void fcsTimedepGainCorr_db(int period=1, char* opt = "writetxt", int onlydorun=0) {
201  gROOT->Macro("LoadLogger.C");
202  gSystem->Load("St_base.so");
203  gSystem->Load("libStDb_Tables.so");
204  gSystem->Load("StDbLib.so");
205  gSystem->Load("StChain.so");
206  gSystem->Load("StBFChain");
207  gSystem->Load("StUtilities");
208  gSystem->Load("StIOMaker");
209  gSystem->Load("StarClassLibrary");
210  gSystem->Load("St_Tables");
211  gSystem->Load("StDbLib");
212  gSystem->Load("StDbBroker");
213  gSystem->Load("St_db_Maker");
214  gSystem->Load("StFcsDbMaker.so");
215 
216  //create and initialize StFcsDb
217  mFcsDb=new StFcsDb;
218  mFcsDb->Init();
219 
220  // structure to fill up
221  fcsEcalGainCorr_st ecorr;
222  int readTime, readDate;
223 
224  TString option(opt);
225  std::cout << "Opt =" << opt << "\n";
226  std::cout << "writedb = " << option.Contains("writedb") << "\n";
227 
228  // scaler analsys
229  scaler();
230 
231  int p=-1;
232  double l12,l34;
233  for(unsigned int irun=0; irun<NRUN; irun++){
234  unsigned int run=RUNN[irun];
235  //printf("irun=%d NRUN=%d run=%d\n",irun,NRUN,run);
236  if(run==RUN0[p+1]){ //new period
237  p++;
238  if(period==-1 || period==p){ //do this period
239  readGainCorr(p);
240  double l1=getIntgLumi(RUN1[p]);
241  double l2=getIntgLumi(RUN2[p]);
242  double l3=getIntgLumi(RUN3[p]);
243  double l4=getIntgLumi(RUN4[p]);
244  l12=(l1+l2)/2.0;
245  l34=(l3+l4)/2.0;
246  }
247  }
248  //printf("Run=%d p=%d Period=%d\n",run,p,period);
249  if(p>=0 && (period==-1 || period==p) ){ //do this run
250  if(onlydorun>0 && run!=onlydorun) continue;
251  //getting run start time from DB
252  int year=run/1000000-1;
253  int port=3400+year-1;
254  //printf("Year=%d Port=%d\n",year,port);
255  char cmd[400];
256  sprintf(cmd,"mysql -h db04.star.bnl.gov --port=%d -N -s -e \"SELECT startRunTime FROM RunLog.runDescriptor WHERE runNumber=%d LIMIT 1\"",port,run);
257  //printf("cmd=%s\n",cmd);
258  TString st=gSystem->GetFromPipe(cmd);
259  int starttime=st.Atoi();
260  readDate=gSystem->GetFromPipe(Form("date -u -d \@%d +%%Y%%m%%d",starttime)).Atoi();
261  readTime=gSystem->GetFromPipe(Form("date -u -d \@%d +%%H%%M%%S",starttime)).Atoi();
262 
263  //getting integrated lumionosity for the run and calc gain
264  double l=getIntgLumi(run);
265  double w1=(l34-l)/(l34-l12);
266  double w2=(l-l12)/(l34-l12);
267  for(int idx=0; idx<NIDX; idx++){
268  double c1 = mGainCorrCalib[idx][0];
269  double c2 = mGainCorrCalib[idx][1];
270  double cc = c1*w1 + c2*w2;
271  mGainCorr[idx]=cc;
272  ecorr.gaincorr[idx]=cc;
273  if(idx==showidx){
274  printf("Run=%8d Period=%1d Start=%d Date=%06d Time=%06d idx=%4d %6.3f %6.3f %8.4f %8.4f %8.4f\n",
275  run,p,starttime,readDate,readTime,idx,w1,w2,c1,c2,ecorr.gaincorr[idx]);
276  }
277  }
278  if(option.Contains("writetxt")) {
279  writeGainCorr(run);
280  }
281  if(option.Contains("writedb")) {
282  gSystem->Setenv("DB_ACCESS_MODE","write");
283  cout << "DB_ACCESS_MODE="<<gSystem->Getenv("DB_ACCESS_MODE")<<endl;
285  StDbConfigNode* node = mgr->initConfig("Calibrations_fcs");
286  mgr->setStoreTime(starttime);
287  StDbTable* table = node->addDbTable("fcsEcalGainCorr");
288  table->SetTable((char*)&ecorr,1);
289  table->setFlavor("ofl");
290  mgr->storeDbTable(table);
291  }
292  }
293  }
294 
295  //reqading back DB
296  printf("Reading back DB\n");
297  int date,time,from=0,n=0;
298  /*
299  TString datetime(storeTime),token;
300  datetime.ReplaceAll("-","");
301  datetime.ReplaceAll(":","");
302  while(datetime.Tokenize(token,from," ")){
303  if(n==0) date=atoi(token.Data());
304  if(n==1) time=atoi(token.Data());
305  n++;
306  }
307  std::cout << "Readout time="<<datetime<<" Date="<<date<<" Time="<<time<<endl;
308  */
309  std::cout << "Readout Date="<<readDate<<" Time="<<readTime<<endl;
310 
311  St_db_Maker *dbMk=new St_db_Maker("db", "MySQL:StarDb", "$STAR/StarDb");
312  dbMk->SetDebug();
313  dbMk->SetDateTime(readDate,readTime);
314  dbMk->SetFlavor("ofl");
315  dbMk->Init();
316  dbMk->Make();
317 
318  TDataSet *DB = 0;
319  DB = dbMk->GetInputDB("Calibrations/fcs");
320  if(!DB){std::cout << "ERROR: no db maker or Calibrations/fcs" << std::endl; }
321  St_fcsEcalGainCorr *dbTable_ec = (St_fcsEcalGainCorr*) DB->Find("fcsEcalGainCorr");
322  if(dbTable_ec){
323  std::cout << "Reading fcsEcalGainCorr table from DB\n";
324  fcsEcalGainCorr_st *dbSt_ec = dbTable_ec->GetTable();
325  Int_t rows = dbTable_ec->GetNRows();
326  for(int i=0; i<rows; i++){
327  for(int id=0; id<1496; id++){
328  printf("DbRead row=%2d id=%d gaincorr=%10.6f\n",
329  i,id,dbSt_ec[i].gaincorr[id]);
330  }
331  }
332  }else{
333  std::cout << "WARNING: No data in fcsEcalGainCorr table\n";
334  }
335 }
336 
virtual Int_t Make()
void getIdfromDep(int ehp, int ns, int dep, int ch, int &detectorId, int &id, int &crt, int &slt) const
Get DEP map.
Definition: StFcsDb.cxx:1031
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