StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
matchTpcFieldCageShorts.C
1 //
3 // matchTpcFieldCageShorts.C
4 // Author: G. Van Buren, BNL
5 // Created: August 24, 2005
6 // Modified: April 11, 2007
7 //
8 // Creates an ntuple with IFC currents and daqSummary to indicate runs
9 //
11 
12 #if !defined(__CINT__) || defined(__MAKECINT__)
13 #include "TROOT.h"
14 #include "TFile.h"
15 #include "TNtupleD.h"
16 #include <iostream>
17 #include <fstream>
18 #include "TTimeStamp.h"
19 #include "TDatime.h"
20 #endif
21 
22 
23 
24  const int size1 = 6; // number of elements per line 1
25  const int size2 = 9; // " 2
26  const int size3 = 5; // time + 4 currents
27 
28  ifstream in1("currents.txt");
29  ifstream in2("run_times.txt");
30  ofstream out2("dbout.csv");
31  ofstream out3("dbout.txt");
32 
33  int writing=0;
34 
36 
37  // data arrays
38  Double_t ff1[15];
39  Double_t ff2[9];
40  // for averaging:
41  Double_t ff3[5];
42  Double_t ff4[5];
43 
44  TFile scf("cur.root","RECREATE");
45  TNtupleD sc("cur","current","t:Ioe:Iie:Iow:Iiw:res:run:Y:M:D:h:m:s:begin:end");
46 
47  int li1=0;
48  int li2=0;
49  const int LI=1000000;
50 
51  int tcnt = 0;
52 
53  // db entry values
54  int dataid;
55  char* eT;
56 
57  double mincur,maxcur,dcur,maxdcur=0;
58  double pflag = 0;
59  double last_missing_resistance=0;
60  double last_resistor=0;
61  double maxsec = 2147483647.0; // 2^31-1
62  double nexttime = 0;
63  double resistor = 0;
64  UInt_t tOffset = 0;
65 
66 void Finish() {
67  printf("Maximum current difference = %f\n",maxdcur);
68  sc.Write();
69  scf.Close();
70  abort();
71 }
72 
73 void ReadLine1() {
74  if (in1.eof()) Finish();
75  int i;
76  ff1[0] = nexttime;
77  for (i=1; i<size1; i++) in1 >> ff1[i];
78  in1 >> nexttime;
79  //ff1[4]-=0.377; // only if we need to ignore permanent short at locaton 181.4
80  if (in1.eof()) nexttime = maxsec;
81  for (i=size1; i<size1+size2; i++) ff1[i] = 0;
82  if ((++li1)%LI==0) printf("Line1 = %d , time = %f\n",li1,ff1[0]);
83 }
84 
85 void ReadLine2() {
86  for (int i=0; i<size2; i++) in2 >> ff2[i];
87  if (in2.eof()) {
88  for (int i=0; i<size2; i++) ff2[i]=0;
89  ff2[7] = maxsec-1;
90  ff2[8] = maxsec;
91  return;
92  }
93  if ((++li2)%LI==0) printf("Line2 = %d , time = %f\n",li2,ff2[7]);
94 }
95 
96 void CopyF2F1() {
97  for (int i=0; i<size2; i++) ff1[i+size1] = ff2[i];
98 }
99 
100 
101 void FindTOffset() {
102  TTimeStamp begin1((UInt_t) ff2[1],(UInt_t) ff2[2],(UInt_t) ff2[3],
103  (UInt_t) ff2[4],(UInt_t) ff2[5],(UInt_t) ff2[6]);
104  tOffset = begin1.GetSec() - (UInt_t) ff2[7];
105  printf("Found time offset to be: %u\n",tOffset);
106 }
107 
108 void UpdateF2() {
109  TDatime newbegin(tOffset + (UInt_t) ff1[0]);
110  newbegin.Set(newbegin.Convert(kTRUE)); // Fix a locatltime <-> GMT bug
111  ff2[1] = newbegin.GetYear();
112  ff2[2] = newbegin.GetMonth();
113  ff2[3] = newbegin.GetDay();
114  ff2[4] = newbegin.GetHour();
115  ff2[5] = newbegin.GetMinute();
116  ff2[6] = newbegin.GetSecond();
117  CopyF2F1();
118 }
119 
120 void WriteTable() {
121 
122  Int_t ii[6];
123  for (int i=0; i<6; i++) ii[i] = (int) ff2[i+1];
124  // Modify from current->resistance
125 
126  // Only do Iow (which is index i=4)
127  Double_t ring = 80.5;
128  Double_t reference = TMath::Max(ff3[2],ff3[3]); // Use max(Ioe,Iiw) as reference
129  Double_t missing_resistance = 364.44*(1.0 - reference/ff3[4]); // delR = R*(1-(I1/I2))
130 //printf("GGGG ref = %g , cur = %g\n",reference,ff3[4]);
131  //missing_resistance += resistor;
132 
133  if (last_missing_resistance && TMath::Abs(missing_resistance - last_missing_resistance) > 0.5) {
134  printf("BIG RESISTANCE JUMP: %f -> %f :: dif = %f\n",
135  last_missing_resistance,missing_resistance,missing_resistance-last_missing_resistance);
136  pflag=3;
137  }
138  if ((last_missing_resistance && TMath::Abs(missing_resistance - last_missing_resistance) < 0.1)
139  || (last_missing_resistance > 0.2 && missing_resistance > 0.2)
140  || (last_missing_resistance < 0.2 && missing_resistance < 0.2)) {
141  //&& TMath::Abs(last_resistor - resistor) < 0.1) {
142  printf("SMALL RESISTANCE JUMP: %f -> %f :: dif = %f\n",
143  last_missing_resistance,missing_resistance,missing_resistance-last_missing_resistance);
144  return;
145  }
146 
147  dataid++;
148  TString outname = Form("dbout/tpcFieldCageShort.%04d%02d%02d.%02d%02d%02d.C",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
149  ofstream* out1=0;
150  if (writing) out1 = new ofstream(outname.Data());
151 
152  double missing_res2 = missing_resistance;
153  missing_res2 = (missing_resistance > 0.2 ? 0.345 : 0.0);
154 
155  TString buffer = Form("\"%d\",\"%s\",\"33\",\"1\",",dataid,eT);
156  //TString buffer = "\"33\",\"1\",";
157  buffer += Form("\"%04d-%02d-%02d %02d:%02d:%02d\",\"ofl\",\"1\",\"0\",",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
158  buffer += Form("\"1.0\",\"1.0\",\"%5.3f\",\"%7.5f\",\"%7.5f\"",ring,0.0,missing_res2);
159  if (writing) (*out1) << buffer << endl;
160  out2 << buffer << endl;
161 
162  buffer = Form("%04d%02d%02d %02d%02d%02d",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
163  buffer += Form(" 1.0 1.0 %5.3f %7.5f %7.5f",ring,0.0,missing_res2);
164  out3 << buffer << endl;
165 
166  last_missing_resistance = missing_resistance;
167  last_resistor = resistor;
168 
169  // QA
170  if (ff3[2]==0) {printf("ZERO CURRENT!...\n"); pflag=2;}
171  int tt = (int) (ff4[0]-ff3[0]);
172  //if (pflag) {pflag=0; cout << outname << " :: " << dcur << " :: " << tcnt << " :: " << ff3[0] <<
173  {pflag=0; cout << outname << " :: " << dcur << " :: " << tcnt << " :: " << ff3[0] <<
174  "," << tt << "," << (int) ff2[0] << "," << ff3[4] << endl;}
175 
176 
177  if (writing) {out1->close(); delete out1; out1=0;}
178 }
179 
180 
181 void ResetMeasure() {
182  if (tcnt) {
183  // Keep track of maximum current difference over a measure
184  dcur = maxcur-mincur;
185  if (dcur>0.15) {printf("LARGE CURRENT SPREAD: %f\n",dcur); pflag=1;}
186  if (dcur>maxdcur) maxdcur = dcur;
187 
188  //printf("Writing table with tcnt=%d , ff3:0=%f\n",tcnt,ff3[0]);
189  if (tcnt>1) for (int i=1; i<size3; i++) ff3[i] /= ff3[0];
190  WriteTable();
191  UpdateF2();
192  tcnt = 0;
193  }
194  for (int i=0; i<size3; i++) { ff3[i]=0; ff4[i]=0; }
195 
196  maxcur = 0;
197  mincur = 100;
198 }
199 
200 void IncludeThisMeasure() {
201  // Use value for only 1 measure, otherwise average over time difference
202  for (int i=1; i<size3; i++) if (ff1[i] < 76.0) return; // Don't use if any currents are way off
203 
204 // Big change test
205  if (tcnt) {
206  //if (tcnt>2) { // don't decide based on one or two points?
207  double avg = ff3[4];
208  if (tcnt>1) avg /= ff3[0];
209  if (TMath::Abs(avg-ff1[4]) > 0.045) ResetMeasure(); // 45nA change from the average
210  }
211 
212 
213  if (tcnt==1) for (int i=0; i<size3; i++) ff3[i]=0;
214  Double_t tdiff = 2;
215  if (tcnt>0) tdiff = ff1[0]-ff4[0];
216  if (tdiff == 0) tdiff = 0.001;
217  ff3[0] += tdiff;
218  // average this (ff1) and previous (ff4) currents, store in ff3.
219  for (int i=1; i<size3; i++) ff3[i] += tdiff*0.5*(ff1[i]+ff4[i]);
220  for (int i=0; i<size3; i++) ff4[i] = ff1[i];
221  tcnt++;
222  resistor = ff1[5];
223 
224  // Keep track of maximum and minimum current over a measure
225  if (ff1[4]>maxcur) maxcur = ff1[4];
226  if (ff1[4]<mincur) mincur = ff1[4];
227 }
228 
229 void FindNextRun() {
230  ResetMeasure();
231  ReadLine2();
232  FindTOffset();
233 }
234 
235 void UseThisRun() {
236  CopyF2F1();
237  IncludeThisMeasure();
238 }
239 
240 // Main routine
241 void run() {
242  for (int i=0; i<size2; i++) ff2[i]=0;
243  tcnt = 0;
244  in1 >> nexttime;
245 
246  while (1) {
247  ReadLine1();
248  Double_t time = ff1[0];
249 
250  while (time > ff2[8]) FindNextRun();
251 
252  if (nexttime > ff2[7]) UseThisRun();
253 
254  sc.Fill(ff1);
255  }
256 
257 }
258 
259 void matchTpcFieldCageShorts(char* T, double lr, double lm, int i) {
260  dataid = i;
261  eT = T;
262  last_resistor = lr;
263  last_missing_resistance = lm;
264  run();
265 }
266 
268 // $Id: matchTpcFieldCageShorts.C,v 1.5 2018/03/30 04:24:29 genevb Exp $
269 // $Log: matchTpcFieldCageShorts.C,v $
270 // Revision 1.5 2018/03/30 04:24:29 genevb
271 // Corrected currents order, no permanent shorts, compile pragmas
272 //
273 // Revision 1.4 2013/09/12 17:09:02 genevb
274 // Update DBs, use full unixtime, small improvements
275 //
276 // Revision 1.3 2010/01/08 19:48:36 genevb
277 // Update for permanent short at OFCW ring 181.4
278 //
279 // Revision 1.2 2009/09/23 00:06:55 genevb
280 // More precise resistances
281 //
282 // Revision 1.1 2009/02/20 18:50:58 genevb
283 // Placement in CVS of automatic TpcFieldCageShort calib codes
284 //
285 //
286