StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
MakesvtRDOstripped.C
1 #if !defined(__CINT__)
2 // code that should be seen ONLY by the compiler
3 #else
4 #if !defined(__CINT__) || defined(__MAKECINT__)
5 // code that should be seen by the compiler AND rootcint
6 #else
7 // code that should always be seen
8 #endif
9 #endif
10 //________________________________________________________________________________
11 #if !defined(__CINT__) || defined(__MAKECINT__)
12 #include "Riostream.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include "TROOT.h"
16 #include "TSystem.h"
17 #include "TMath.h"
18 #include "TString.h"
19 #include "TObjString.h"
20 #include "tables/St_svtRDOstripped_Table.h"
21 
22 #endif
23 //#define DEBUG
24 class svtRDOstripped_st;
25 struct RDO_t {
26  Char_t *name;
27  Int_t ladNum, barNum;
28  Char_t *rdo;
29  Int_t ndet;
30 };
31 const Double_t DeltahVolt = 5;
32 Int_t nCFW = 0;
33 Int_t nCFWFault = 0;
34 Char_t tags[72][14];
35 static const Int_t NRDOS = 72;
36 static const RDO_t RDOS[72] = {
37  {"L01B1E", 1,1, "E1", 2},{"L02B1E", 2,1, "E2", 2},{"L03B1E", 3,1, "E4", 2},{"L04B1E", 4,1, "E5", 2},{"L05B1E", 5,1, "E7", 2},
38  {"L06B1E", 6,1, "E8", 2},{"L07B1E", 7,1,"E10", 2},{"L08B1E", 8,1,"E11", 2},
39  {"L01B2E", 1,2, "E3", 2},{"L02B2E", 2,2, "E3", 3},
40  {"L03B2E", 3,2, "E3", 3},{"L04B2E", 4,2, "E6", 3},{"L05B2E", 5,2, "E6", 3},{"L06B2E", 6,2, "E6", 3},{"L07B2E", 7,2, "E9", 3},
41  {"L08B2E", 8,2, "E9", 3},{"L09B2E", 9,2, "E9", 3},{"L10B2E",10,2,"E12", 3},{"L11B2E",11,2,"E12", 3},{"L12B2E",12,2,"E12", 3},
42 
43  {"L01B3E", 1,3, "E1", 3},{"L02B3E", 2,3, "E1", 4},{"L03B3E", 3,3, "E2", 3},{"L04B3E", 4,3, "E2", 4},{"L05B3E", 5,3, "E4", 3},
44  {"L06B3E", 6,3, "E4", 4},{"L07B3E", 7,3, "E5", 3},{"L08B3E", 8,3, "E5", 4},{"L09B3E", 9,3, "E7", 3},{"L10B3E",10,3, "E7", 4},
45  {"L11B3E",11,3, "E8", 3},{"L12B3E",12,3, "E8", 4},{"L13B3E",13,3,"E10", 3},{"L14B3E",14,3,"E10", 4},{"L15B3E",15,3,"E11", 3},
46  {"L16B3E",16,3,"E11", 4},
47 
48  {"L01B1W", 1,1, "W1", 4},{"L02B1W", 2,1, "W2", 4},{"L03B1W", 3,1, "W4", 4},{"L04B1W", 4,1, "W5", 4},{"L05B1W", 5,1, "W7", 4},
49  {"L06B1W", 6,1, "W8", 4},{"L07B1W", 7,1,"W10", 4},{"L08B1W", 8,1,"W11", 4},
50  {"L01B2W", 1,2, "W3", 4},{"L02B2W", 2,2, "W3", 6},
51  {"L03B2W", 3,2, "W3", 6},{"L04B2W", 4,2, "W6", 6},{"L05B2W", 5,2, "W6", 6},{"L06B2W", 6,2, "W6", 6},{"L07B2W", 7,2, "W9", 6},
52  {"L08B2W", 8,2, "W9", 6},{"L09B2W", 9,2, "W9", 6},{"L10B2W",10,2,"W12", 6},{"L11B2W",11,2,"W12", 6},{"L12B2W",12,2,"W12", 6},
53 
54  {"L01B3W", 1,3, "W1", 7},{"L02B3W", 2,3, "W1", 7},{"L03B3W", 3,3, "W2", 7},{"L04B3W", 4,3, "W2", 7},{"L05B3W", 5,3, "W4", 7},
55  {"L06B3W", 6,3, "W4", 7},{"L07B3W", 7,3, "W5", 7},{"L08B3W", 8,3, "W5", 7},{"L09B3W", 9,3, "W7", 7},{"L10B3W",10,3, "W7", 7},
56  {"L11B3W",11,3, "W8", 7},{"L12B3W",12,3, "W8", 7},{"L13B3W",13,3,"W10", 7},{"L14B3W",14,3,"W10", 7},{"L15B3W",15,3,"W11", 7},
57  {"L16B3W",16,3,"W11", 7}
58 };
59 //________________________________________________________________________________
60 Int_t CheckRows(svtRDOstripped_st *rows) {
61  Int_t iok = 0;
62  for (Int_t i = 0; i < NRDOS; i++, rows++) {
63  if (! rows->lvFault) iok++;
64  }
65  return iok;
66 }
67 //________________________________________________________________________________
68 void WritesvtRDOstripped(svtRDOstripped_st *rows, Int_t date, Int_t time) {
69  TString fOut = Form("svtRDOstripped.%8i.%06i.C",date,time);
70  if (! CheckRows(rows)) {
71  fOut += ".Fault";
72  if (nCFWFault) fOut += Form("_%i",nCFWFault);
73  nCFWFault++;
74  } else nCFWFault = 0;
75  ofstream out;
76  cout << "Create " << fOut << endl;
77  out.open(fOut.Data());
78  out << "TDataSet *CreateTable() {" << endl;
79  out << " if (!gROOT->GetClass(\"St_svtRDOstripped\")) return 0;" << endl;
80  out << " svtRDOstripped_st row[" << NRDOS << "] = {" << endl;
81  for (Int_t i = 0; i < NRDOS; i++, rows++) {
82  out << "\t{" << Form("%1i,%2i,\"%s\",\t",rows->barNum,rows->ladNum, rows->rdo);
83  out << Form("%13.7f, %13.7f, %13.7f,%13.7f, %13.7f", rows->northTemp, rows->southTemp,rows->hvBoardTemp,rows->hvVolt,rows->hvCurr);
84  out << Form(", %2i, %8i, %6i, %8i, %6i}" ,rows->lvFault, rows->date, rows->time, rows->dateOff, rows->timeOff);
85  if (i != NRDOS - 1) out << ",";
86  if (tags[i]) out << "// " << tags[i];
87  out << endl;
88  }
89  out << " };" << endl;
90  out << " St_svtRDOstripped *tableSet = new St_svtRDOstripped(\"svtRDOstripped\"," << NRDOS << ");" << endl;
91  out << " for (Int_t i = 0; i < " << NRDOS << "; i++) tableSet->AddAt(&row[i].barNum, i);" << endl;
92  out << " return (TDataSet *)tableSet;" << endl;
93  out << "}" << endl;
94  out.close();
95  nCFW++;
96 }
97 //________________________________________________________________________________
98 void MakesvtRDOstripped(Char_t *FileName="./svtRDOs.txt") {
99  /* file svtRDOs.txt is created by
100  mysql Conditions_svt -h onldb2.starp.bnl.gov -P 3502 \
101  -e 'select beginTime,flavor,deactive,barNum,ladNum,rdo,northTemp,southTemp,hvBoardTemp,hvVolt,hvCurr,lvFault from Conditions_svt.svtRDOs where deactive=0 and beginTime > "2007-01-01"' > svtRDOs.txt
102  */
104  Int_t Year = 2007;
105  gSystem->Load("libStDb_Tables.so");
106  FILE *fp = fopen(FileName,"r");
107  if (! fp) return;
108  svtRDOstripped_st rows[72];
109  memset(rows, 0, NRDOS*sizeof(svtRDOstripped_st));
110  memset(tags, 0, 72*14);
111  for (Int_t i = 0; i < NRDOS; i++) {
112  rows[i].barNum = RDOS[i].barNum;
113  rows[i].ladNum = RDOS[i].ladNum;
114  strcpy(rows[i].rdo,RDOS[i].rdo);
115  rows[i].lvFault = -1;
116  // cout << i << "\tB" << rows[i].barNum << "\tL" << rows[i].ladNum << "\t" << rows[i].rdo << endl;
117  }
118  svtRDOstripped_st row;
119  Int_t year, month, day;
120  Int_t hour, mins, secs;
121  Int_t date, time, dateOld = 0, timeOld = 0;
122  Double_t datimeOld = 0;
123  char flavor[3];
124  Int_t deactive;
125  char line[121];
126 
127  fgets(&line[0],120,fp); // skip the first line
128  Int_t nRowsModified = 0;
129  while (fgets(&line[0],120,fp)) {
130 #ifdef DEBUG
131  if (nCFW > 100) break;
132 #endif
133  memset (&row, 0, sizeof(svtRDOstripped_st));
134 
135  // sscanf(&line[0],"%8d.%06d %s %d %d %d %s %f %f %f %f %f %d",
136  // &date,&time,
137  /*
138 beginTime flavor deactive barNum ladNum rdo hvBoardTemp hvVolt hvCurr lvFault
139 2007-03-24 04:37:23 ofl 0 1 1 W1 0.00000000 0.10000000 -0.89999998 0
140  */
141  sscanf(&line[0],"%4d-%2d-%2d %02d:%02d:%02d %s %d %d %d %s %f %f %f %f %f %d",
142  &year,&month,&day, &hour,&mins,&secs,
143  flavor, &deactive,
144  &row.barNum,&row.ladNum,row.rdo,&row.northTemp,
145  &row.southTemp,&row.hvBoardTemp,&row.hvVolt,&row.hvCurr,&row.lvFault);
146 #ifdef DEBUG
147  printf("%4i-%02i-%02i %02i:%02i:%02i %s %i %i %i %s %f %f %f %f %f %i\n",
148  year,month,day, hour,mins,secs,
149  flavor, deactive,
150  row.barNum,row.ladNum,row.rdo,row.northTemp,
151  row.southTemp,row.hvBoardTemp,row.hvVolt,row.hvCurr,row.lvFault);
152 #endif
153  date = day + 100*(month + 100*year);
154  time = secs + 100*(mins + 100*hour);
155 #ifdef DEBUG
156  cout << line;
157  printf("%8d.%06d %s %d %d %d %s %f %f %f %f %f %d",
158  date,time,
159  flavor, deactive,
160  row.barNum,row.ladNum,row.rdo,row.northTemp,
161  row.southTemp,row.hvBoardTemp,row.hvVolt,row.hvCurr,row.lvFault);
162 #endif
163  TString Ofl(flavor);
164  if (Ofl != "ofl") continue;
165  if (year != Year) {
166  cout << "Wrong date: " << line;
167  continue;
168  }
169  Double_t datime = date + ((Double_t) time)/1000000.;
170  if (TMath::Abs(row.hvVolt+1500) >= DeltahVolt) row.hvVolt = 0;
171  if (datimeOld > 0 && datime < datimeOld) {
172  cout << "incosistent date time new " << date << "/" << time << " and old " << dateOld << "/" << timeOld << endl;
173  cout << date << "\t" << time << "\t"
174  << row.barNum << "\t" << row.ladNum << "\t" << row.rdo << "\t" << row.northTemp << "\t"
175  << row.southTemp << "\t" << row.hvBoardTemp << "\t" << row.hvVolt << "\t" << row.hvCurr << "\t"
176  << row.lvFault << endl;
177  // cout << Form("Read %i-%i-%i %i:%i:%i",year,month,day,hour,mins,secs) << endl;
178  cout << Form("Read %8d.%06d",date,time) << endl;
179  }
180  if (row.barNum < 1 || row.barNum > 3 ||
181  row.ladNum < 1 || row.ladNum > 16) {
182  cout << line;
183  cout << date << "\t" << time << endl;
184  cout << "Wrong barel or ladder number "
185  << row.barNum << "\t" << row.ladNum << "\t" << row.rdo << "\t" << row.northTemp << "\t"
186  << row.southTemp << "\t" << row.hvBoardTemp << "\t" << row.hvVolt << "\t" << row.hvCurr << "\t"
187  << row.lvFault << endl;
188  return;
189  }
190  if (date != dateOld || time != timeOld) { // new beginTime
191  if (nRowsModified > 10) {
192  // Create Cint Table
193  WritesvtRDOstripped(rows,dateOld,timeOld);
194  nRowsModified = 0;
195  }
196  dateOld = date; timeOld = time;
197  datimeOld = dateOld + ((Double_t) timeOld)/1000000.;
198  }
199  // Find row
200  Int_t irow = -1;
201  for (Int_t i = 0; i <= NRDOS; i++) {
202  if (row.barNum != rows[i].barNum ||
203  row.ladNum != rows[i].ladNum ||
204  strcmp(row.rdo,rows[i].rdo)) continue;
205  irow = i;
206  if ( rows[i].lvFault == -1 && row.lvFault) {break;}
207  if ( rows[i].lvFault == row.lvFault && TMath::Abs(rows[i].hvVolt - row.hvVolt) < DeltahVolt) {break;}
208 
209  // rows[i] = row;
210  if ( TMath::Abs(rows[i].hvVolt - row.hvVolt) >= DeltahVolt) {
211  if (TMath::Abs(rows[i].hvVolt + 1500) < DeltahVolt &&
212  TMath::Abs( row.hvVolt + 1500) >= DeltahVolt) {
213  rows[i].dateOff = date;
214  rows[i].timeOff = time;
215  } else {
216  rows[i].date = date;
217  rows[i].time = time;
218  }
219  nRowsModified++;
220  }
221  if (TMath::Abs(rows[i].hvVolt + 1500) < DeltahVolt && rows[i].lvFault != row.lvFault) nRowsModified++;
222  rows[i].northTemp = row.northTemp;
223  rows[i].southTemp = row.southTemp;
224  rows[i].hvBoardTemp = row.hvBoardTemp;
225  rows[i].hvVolt = row.hvVolt;
226  rows[i].hvCurr = row.hvCurr;
227  rows[i].lvFault = row.lvFault;
228  strcpy(tags[i],flavor);
229  break;
230  }
231  if (irow < 0) {
232  cout << line;
233  cout << date << "\t" << time << endl;
234  cout << "Match has not been found for "
235  << row.barNum << "\t" << row.ladNum << "\t" << row.rdo << "\t" << row.northTemp << "\t"
236  << row.southTemp << "\t" << row.hvBoardTemp << "\t" << row.hvVolt << "\t" << row.hvCurr << "\t"
237  << row.lvFault << endl;
238  return;
239  }
240  }
241  if (nRowsModified > 0) WritesvtRDOstripped(rows,dateOld,timeOld);
242  fclose(fp);
243 }