00001
00002
00003
00004
00005
00006
00007
00008
00009
00011
00012 #ifndef __CINT__
00013 #include "TROOT.h"
00014 #include "TFile.h"
00015 #include "TNtuple.h"
00016 #include <iostream>
00017 #include <fstream>
00018 #include "TTimeStamp.h"
00019 #include "TDatime.h"
00020 #endif
00021
00022
00023
00024 const int size1 = 6;
00025 const int size2 = 9;
00026 const int size3 = 5;
00027
00028 ifstream in1("currents.txt");
00029 ifstream in2("run_times.txt");
00030 ofstream out2("dbout.csv");
00031 ofstream out3("dbout.txt");
00032
00033 int writing=0;
00034
00036
00037
00038 Float_t ff1[15];
00039 Float_t ff2[9];
00040
00041 Float_t ff3[5];
00042 Float_t ff4[5];
00043
00044 TFile scf("cur.root","RECREATE");
00045 TNtuple sc("cur","current","t:Ioe:Iie:Iow:Iiw:res:run:Y:M:D:h:m:s:begin:end");
00046
00047 int li1=0;
00048 int li2=0;
00049 const int LI=1000000;
00050
00051 int tcnt = 0;
00052
00053
00054 int dataid;
00055 char* eT;
00056
00057 float mincur,maxcur,dcur,maxdcur=0;
00058 float pflag = 0;
00059 float last_missing_resistance=0;
00060 float last_resistor=0;
00061 float maxsec = 16777215;
00062 float nexttime = 0;
00063 float resistor = 0;
00064 UInt_t tOffset = 0;
00065
00066 void Finish() {
00067 printf("Maximum current difference = %f\n",maxdcur);
00068 sc.Write();
00069 scf.Close();
00070 abort();
00071 }
00072
00073 void ReadLine1() {
00074 if (in1.eof()) Finish();
00075 int i;
00076 ff1[0] = nexttime;
00077 for (i=1; i<size1; i++) in1 >> ff1[i];
00078 in1 >> nexttime;
00079 ff1[3]-=0.377;
00080 if (in1.eof()) nexttime = maxsec;
00081 for (i=size1; i<size1+size2; i++) ff1[i] = 0;
00082 if ((++li1)%LI==0) printf("Line1 = %d , time = %f\n",li1,ff1[0]);
00083 }
00084
00085 void ReadLine2() {
00086 for (int i=0; i<size2; i++) in2 >> ff2[i];
00087 if (in2.eof()) {
00088 for (int i=0; i<size2; i++) ff2[i]=0;
00089 ff2[7] = maxsec-1;
00090 ff2[8] = maxsec;
00091 return;
00092 }
00093 if ((++li2)%LI==0) printf("Line2 = %d , time = %f\n",li2,ff2[7]);
00094 }
00095
00096 void CopyF2F1() {
00097 for (int i=0; i<size2; i++) ff1[i+size1] = ff2[i];
00098 }
00099
00100
00101 void FindTOffset() {
00102 TTimeStamp begin1((UInt_t) ff2[1],(UInt_t) ff2[2],(UInt_t) ff2[3],
00103 (UInt_t) ff2[4],(UInt_t) ff2[5],(UInt_t) ff2[6]);
00104 tOffset = begin1.GetSec() - (UInt_t) ff2[7];
00105 printf("Found time offset to be: %u\n",tOffset);
00106 }
00107
00108 void UpdateF2() {
00109 TDatime newbegin(tOffset + (UInt_t) ff1[0]);
00110 newbegin.Set(newbegin.Convert(kTRUE));
00111 ff2[1] = newbegin.GetYear();
00112 ff2[2] = newbegin.GetMonth();
00113 ff2[3] = newbegin.GetDay();
00114 ff2[4] = newbegin.GetHour();
00115 ff2[5] = newbegin.GetMinute();
00116 ff2[6] = newbegin.GetSecond();
00117 CopyF2F1();
00118 }
00119
00120 void WriteTable() {
00121
00122 Int_t ii[6];
00123 for (int i=0; i<6; i++) ii[i] = (int) ff2[i+1];
00124
00125
00126
00127 Float_t ring = 80.5;
00128
00129 Float_t reference = TMath::Max(ff3[1],ff3[2]);
00130 Float_t missing_resistance = 364.44*(1.0 - reference/ff3[3]);
00131
00132
00133
00134 if (last_missing_resistance && TMath::Abs(missing_resistance - last_missing_resistance) > 0.5) {
00135 printf("BIG RESISTANCE JUMP: %f -> %f :: dif = %f\n",
00136 last_missing_resistance,missing_resistance,missing_resistance-last_missing_resistance);
00137 pflag=3;
00138 }
00139 if ((last_missing_resistance && TMath::Abs(missing_resistance - last_missing_resistance) < 0.1)
00140 || (last_missing_resistance > 0.2 && missing_resistance > 0.2)
00141 || (last_missing_resistance < 0.2 && missing_resistance < 0.2)) {
00142
00143 printf("SMALL RESISTANCE JUMP: %f -> %f :: dif = %f\n",
00144 last_missing_resistance,missing_resistance,missing_resistance-last_missing_resistance);
00145 return;
00146 }
00147
00148 dataid++;
00149 TString outname = Form("dbout/tpcFieldCageShort.%04d%02d%02d.%02d%02d%02d.C",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
00150 ofstream* out1=0;
00151 if (writing) out1 = new ofstream(outname.Data());
00152
00153 float missing_res2 = missing_resistance;
00154 missing_res2 = (missing_resistance > 0.2 ? 0.345 : 0.0);
00155
00156 TString buffer = Form("\"%d\",\"%s\",\"33\",\"1\",",dataid,eT);
00157
00158 buffer += Form("\"%04d-%02d-%02d %02d:%02d:%02d\",\"ofl\",\"1\",\"0\",",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
00159 buffer += Form("\"1.0\",\"1.0\",\"%5.3f\",\"%7.5f\",\"%7.5f\"",ring,0.0,missing_res2);
00160 if (writing) (*out1) << buffer << endl;
00161 out2 << buffer << endl;
00162
00163 buffer = Form("%04d%02d%02d %02d%02d%02d",ii[0],ii[1],ii[2],ii[3],ii[4],ii[5]);
00164 buffer += Form(" 1.0 1.0 %5.3f %7.5f %7.5f",ring,0.0,missing_res2);
00165 out3 << buffer << endl;
00166
00167 last_missing_resistance = missing_resistance;
00168 last_resistor = resistor;
00169
00170
00171 if (ff3[2]==0) {printf("ZERO CURRENT!...\n"); pflag=2;}
00172 int tt = (int) (ff4[0]-ff3[0]);
00173
00174 {pflag=0; cout << outname << " :: " << dcur << " :: " << tcnt << " :: " << ff3[0] <<
00175 "," << tt << "," << (int) ff2[0] << "," << ff3[3] << endl;}
00176
00177
00178 if (writing) {out1->close(); delete out1; out1=0;}
00179 }
00180
00181
00182 void ResetMeasure() {
00183 if (tcnt) {
00184
00185 dcur = maxcur-mincur;
00186 if (dcur>0.15) {printf("LARGE CURRENT SPREAD: %f\n",dcur); pflag=1;}
00187 if (dcur>maxdcur) maxdcur = dcur;
00188
00189
00190 if (tcnt>1) for (int i=1; i<size3; i++) ff3[i] /= ff3[0];
00191 WriteTable();
00192 UpdateF2();
00193 tcnt = 0;
00194 }
00195 for (int i=0; i<size3; i++) { ff3[i]=0; ff4[i]=0; }
00196
00197 maxcur = 0;
00198 mincur = 100;
00199 }
00200
00201 void IncludeThisMeasure() {
00202
00203 for (int i=1; i<size3; i++) if (ff1[i] < 76.0) return;
00204
00205
00206 if (tcnt) {
00207
00208 float avg = ff3[3];
00209 if (tcnt>1) avg /= ff3[0];
00210 if (TMath::Abs(avg-ff1[3]) > 0.045) ResetMeasure();
00211 }
00212
00213
00214 if (tcnt==1) for (int i=0; i<size3; i++) ff3[i]=0;
00215 Float_t tdiff = 2;
00216 if (tcnt>0) tdiff = ff1[0]-ff4[0];
00217 if (tdiff == 0) tdiff = 0.001;
00218 ff3[0] += tdiff;
00219
00220 for (int i=1; i<size3; i++) ff3[i] += tdiff*0.5*(ff1[i]+ff4[i]);
00221 for (int i=0; i<size3; i++) ff4[i] = ff1[i];
00222 tcnt++;
00223 resistor = ff1[5];
00224
00225
00226 if (ff1[3]>maxcur) maxcur = ff1[3];
00227 if (ff1[3]<mincur) mincur = ff1[3];
00228 }
00229
00230 void FindNextRun() {
00231 ResetMeasure();
00232 ReadLine2();
00233 FindTOffset();
00234 }
00235
00236 void UseThisRun() {
00237 CopyF2F1();
00238 IncludeThisMeasure();
00239 }
00240
00241
00242 void run() {
00243 for (int i=0; i<size2; i++) ff2[i]=0;
00244 tcnt = 0;
00245 in1 >> nexttime;
00246
00247 while (1) {
00248 ReadLine1();
00249 Float_t time = ff1[0];
00250
00251 while (time > ff2[8]) FindNextRun();
00252
00253 if (nexttime > ff2[7]) UseThisRun();
00254
00255 sc.Fill(ff1);
00256 }
00257
00258 }
00259
00260 void matchTpcFieldCageShorts(char* T, float lr, float lm, int i) {
00261 dataid = i;
00262 eT = T;
00263 last_resistor = lr;
00264 last_missing_resistance = lm;
00265 run();
00266 }
00267
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281