APDPlots.C
Go to the documentation of this file.
1 #include <TSQLServer.h>
2 #include <TSQLResult.h>
3 #include <iostream.h>
4 #include <fstream>
5 #include <iomanip.h>
6 #include <sstream>
7 #include <string>
8 #include <cstring>
9 #include <algorithm>
10 
11 void APDPlots(int diblock, int dcm, int port) {
12 
13  gROOT->Reset();
14  gStyle->SetOptStat(0);
15  gStyle->SetOptFit(1);
16  gStyle->SetOptTitle(1);
17  gStyle->SetPalette(1);
18  gStyle->SetFillColor(0);
19  gStyle->SetPadColor(0);
20  gStyle->SetCanvasColor(0);
21  gStyle->SetStatColor(0);
22  gStyle->SetTitleColor(1);
23  gStyle->SetTitleSize(0.06);
24  gStyle->SetPadBorderMode(0);
25  gStyle->SetPadBottomMargin(0.15);
26  gStyle->SetPadLeftMargin(0.12);
27  gStyle->SetPadRightMargin(0.09);
28  gStyle->SetFrameBorderMode(0);
29  gStyle->SetCanvasBorderMode(0);
30 
31  TDatime da(2010,01,01,1,00,00);
32  gStyle->SetTimeOffset(da.Convert());
33 
34  TDatime *TNow = new TDatime;//.TDatime();
35  int XNow=TNow->Convert();
36  unsigned int X2DaysAgo =XNow - 2*60*60*24;
37  unsigned int X4WeekAgo =XNow - 60*60*24*7*4;
38  TDatime *T2d = new TDatime;
39  T2d->Set(X2DaysAgo,false);
40  TDatime *T4w = new TDatime;
41  T4w->Set(X4WeekAgo,false);
42 
43  TNow->Print();
44  cout<<XNow<<"\t"<<X2DaysAgo<<"\t"<<60*60*24*2<<"\t"<<X2DaysAgo-XNow<<endl;
45  cout<<"DATES: " <<TNow->AsSQLString()<< " " << T2d->AsSQLString()<< " " << T4w->AsSQLString() << endl;
46 
47  std::stringstream dbstream;
48 
49  dbstream << "select channel_info.pe,channel_info.pecorr,channel_info.rate_low,channel_info.rate_hi,"
50  << " channel_info.nhits,channel_info.nn_rate,daq_info.channel,daq_info.pixel,"
51  << " apd_header.time_last,temp_info.temp_ave,temp_info.temp_min,temp_info.temp_max,apd_header.num_temp,"
52  << " channel_info.pesq,channel_info.pecorrsq,apd_header.time_elapsed"
53  << " from channel_info,daq_info,apd_header,temp_info where "
54  << " daq_info.dcm="<<dcm
55  << " and temp_info.dcm="<<dcm
56  << " and daq_info.diblock="<<diblock
57  << " and temp_info.diblock="<<diblock
58  << " and daq_info.port="<<port
59  << " and temp_info.port="<<port
60  << " and channel_info.channel=daq_info.channel"
61  << " and apd_header.run=channel_info.run"
62  << " and apd_header.subrun=channel_info.subrun"
63  << " and apd_header.run=temp_info.run"
64  << " and apd_header.subrun=temp_info.subrun"
65  << " and apd_header.time_first> '"<<T2d->AsSQLString()<<"' "
66  << " and apd_header.time_last<'"<<TNow->AsSQLString()<<" ' "
67  << " order by daq_info.pixel";
68 
69  std::string dbstring = dbstream.str();
70 
71  TH1F *pe_pix_2d = new TH1F("pe_pix_2d", "pe_hist;pixel number;# p.e. per hit (last 48h)", 32, 0, 31);
72  TH1F *pec_pix_2d = new TH1F("pec_pix_2d", "pecorr_hist;pixel number; # corrected p.e. per hit (if calibrated, last 48h)", 32, 0, 31);
73  TH1F *nn_pix_2d = new TH1F("nn_pix_2d", "nn_hist;pixel number;nearest neighbor variable (last 48h)", 32, 0, 31);
74  TH1F *rh_pix_2d = new TH1F("rh_pix_2d", "ratehi_hist;pixel number; number hits above 100 p.e. per second (last 48h)", 32, 0, 31);
75  TH1F *rl_pix_2d = new TH1F("rl_pix_2d", "ratelo_hist;pixel number; number of hits below 100 p.e. per second (last 48h)", 32, 0, 31);
76 
77  TH1F *pe_apd_2d = new TH1F("pe_apd_2d", "pe_hist;(48 - #) hours ago; # p.e. per hit (whole APD)", 48, 1, 48);
78  TH1F *pec_apd_2d = new TH1F("pec_apd_2d", "pecorr_hist;(48 - #) hours ago; # corrected p.e. per hit (if calibrated, whole APD)", 48, 1, 48);
79  TH1F *nn_apd_2d = new TH1F("nn_apd_2d", "nn_hist;(48 - #) hours ago;nearest neighbor variable (whole APD)", 48, 1, 48);
80  TH1F *rh_apd_2d = new TH1F("rh_apd_2d", "ratehi_hist;(48 - #) hours ago; # hits above 100 p.e. per second (whole APD)", 48, 1, 48);
81  TH1F *rl_apd_2d = new TH1F("rl_apd_2d", "ratelo_hist;(48 - #) hours ago; # hits below 100 p.e. per second (whole APD)", 48, 1, 48);
82  TH1F *apd_temp = new TH1F("apd_temp", "apd_temp;# hours ago; average temperature of APD (degrees C)", 48, 1, 48);
83 
84  char * dbcstr;
85  dbcstr = new char [dbstring.size()+1];
86  strcpy(dbcstr, dbstring.c_str());
87 
88  //password removed
89  TSQLServer *db = TSQLServer::Connect("pgsql://131.215.116.171", "", "");
90 
91  std::cout << "Connected? " << db->IsConnected() << std::endl;
92 
93  TSQLStatement *stmt = db->Statement(dbcstr,1000);
94 
95  std::cout << " about to send statement: " << dbcstr << std::endl;
96 
97  if(stmt->Process()){
98  // store result of statement in buffer
99  stmt->StoreResult();
100 
101  // std::cout << " we got some results I think! " << std::endl;
102 
103  double pe_pix[34] = {0.};
104  double pecorr_pix[34] = {0.};
105  double pesq_pix[34] = {0.};
106  double pecorrsq_pix[34] = {0.};
107  double nn_pix[34] = {0.};
108  double rate_hi_pix[34] = {0.};
109  double rate_low_pix[34] = {0.};
110  int nhit_pix[34] = {0.};
111  int count_pix[34] = {0.};
112  int telapse_pix[34] = {0.};
113 
114  double pe_2d[50] = {0.};
115  double pecorr_2d[50] = {0.};
116  double pesq_2d[50] = {0.};
117  double pecorrsq_2d[50] = {0.};
118  double nn_2d[50] = {0.};
119  double rate_hi_2d[50] = {0.};
120  double rate_low_2d[50] = {0.};
121  int nhit_2d[50] = {0.};
122  int count_2d[50] = {0.};
123  double tave[50] = {0.};
124  double tmin[50] = {0.};
125  double tmax[50] = {0.};
126  int telapse_2d[50] = {0.};
127 
128  int tpix, tnh, tchan, ttn, telapse;
129  double tpe, tpec, tnn, trh, trl, tpesq, tpecorrsq;
130  double ttave, ttmin, ttmax;
131 
132  int counter = 0;
133 
134  while (stmt->NextResultRow())
135  {
136  counter++;
137  // std::cout << " reading in row number: " << counter << std::endl;
138  // Extract rows one after another
139 
140  tpe = stmt->GetDouble(0);
141  tpec = stmt->GetDouble(1);
142  trl = stmt->GetDouble(2);
143  trh = stmt->GetDouble(3);
144  tnh = stmt->GetInt(4);
145  tnn = stmt->GetDouble(5);
146  tchan = stmt->GetInt(6);
147  tpix = stmt->GetInt(7);
148  const char* time_last = stmt->GetString(8);
149  ttave = stmt->GetDouble(9);
150  ttmin = stmt->GetDouble(10);
151  ttmax = stmt->GetDouble(11);
152  ttn = stmt->GetInt(12);
153  tpesq = stmt->GetDouble(13);
154  tpecorrsq = stmt->GetDouble(14);
155  telapse = stmt->GetInt(15);
156 
157  pe_pix[tpix+1] += tnh*tpe;
158  pecorr_pix[tpix+1] += tnh*tpec;
159  pesq_pix[tpix+1] += tnh*tpesq;
160  pecorrsq_pix[tpix+1] += tnh*tpecorrsq;
161  nn_pix[tpix+1] += tnn*telapse;
162  rate_hi_pix[tpix+1] += trh*telapse;
163  rate_low_pix[tpix+1] += trl*telapse;
164  nhit_pix[tpix+1] += tnh;
165  count_pix[tpix+1] ++;
166  telapse_pix[tpix+1] += telapse;
167 
168  TDatime *ltime = new TDatime;
169  ltime->Set(time_last);
170  unsigned int tlint = ltime->Convert();
171 
172  int tbin = (tlint-X2DaysAgo) / 3600;
173  if(tbin == 48) tbin = 47;
174  tbin = tbin + 1;
175 
176  // std::cout << " " << tpe << " " << tpix << " " << tnh << " " << ttmin << " " << ttmax << " " << tbin << std::endl;
177 
178  pe_2d[tbin] += tpe*tnh;
179  pecorr_2d[tbin] += tpec*tnh;
180  pesq_2d[tbin] += tpesq*tnh;
181  pecorrsq_2d[tbin] += tpecorrsq*tnh;
182  nn_2d[tbin] += tnn*telapse;
183  rate_hi_2d[tbin] += trh*telapse;
184  rate_low_2d[tbin] += trl*telapse;
185  nhit_2d[tbin] += tnh;
186  count_2d[tbin] += ttn;
187  telapse_2d[tbin] += telapse;
188 
189  tave[tbin] += ttn*ttave;
190  if(ttmin==0) ttmin = ttave - (ttmax-ttave); // sometimes we get no temperature data, not the same as bad data, cheat a reasonable value
191  if(tmin[tbin]==0) tmin[tbin]=ttmin;
192  else tmin[tbin] = min(ttmin,tmin[tbin]);
193  if(tmax[tbin]==0) tmax[tbin]=ttmax;
194  else tmax[tbin] = max(ttmax,tmax[tbin]);
195 
196  }
197 
198  double error1[34] = {0.};
199  double error2[34] = {0.};
200  double error3[34] = {0.};
201  double error4[34] = {0.};
202  double error5[34] = {0.};
203 
204  for(unsigned int n = 1; n < 33; ++n){
205 
206  if(nhit_pix[n]!=0) {
207 
208  error4[n] = sqrt(rate_hi_pix[n]) / telapse_pix[n];
209  error5[n] = sqrt(rate_low_pix[n]) / telapse_pix[n];
210 
211  pe_pix[n] = pe_pix[n]/nhit_pix[n];
212  pecorr_pix[n] = pecorr_pix[n]/nhit_pix[n];
213  pesq_pix[n] = pesq_pix[n]/nhit_pix[n];
214  pecorrsq_pix[n] = pecorrsq_pix[n]/nhit_pix[n];
215  nn_pix[n] = nn_pix[n]/telapse_pix[n];
216  rate_hi_pix[n] = rate_hi_pix[n]/telapse_pix[n];
217  rate_low_pix[n] = rate_low_pix[n]/telapse_pix[n];
218 
219  error3[n] = ( sqrt(nhit_pix[n]) / nhit_pix[n] ) * nn_pix[n];
220 
221  if(pesq_pix[n]==0) {
222  error1[n] = ( 1 / sqrt(nhit_pix[n]) ) * pe_pix[n];
223  error2[n] = ( 1 / sqrt(nhit_pix[n]) ) * pecorr_pix[n];
224  }
225  else if(pesq_pix[n]>0) {
226  error1[n] = sqrt( fabs(pesq_pix[n] - (pe_pix[n]*pe_pix[n])) ) * 1.5 / sqrt(nhit_pix[n]);
227  error2[n] = sqrt( fabs(pecorrsq_pix[n] - (pecorr_pix[n]*pecorr_pix[n])) * 1.5 / sqrt(nhit_pix[n]) );
228  }
229  else {
230  std::cout << "Negative squares??? Something is wrong!" << std::endl;
231  }
232 
233  }
234  }
235 
236  pe_pix_2d->SetContent(pe_pix);
237  pec_pix_2d->SetContent(pecorr_pix);
238  nn_pix_2d->SetContent(nn_pix);
239  rh_pix_2d->SetContent(rate_hi_pix);
240  rl_pix_2d->SetContent(rate_low_pix);
241 
242  pe_pix_2d->SetError(error1);
243  pec_pix_2d->SetError(error2);
244  nn_pix_2d->SetError(error3);
245  rh_pix_2d->SetError(error4);
246  rl_pix_2d->SetError(error5);
247 
248  double err1[50] = {0.};
249  double err2[50] = {0.};
250  double err3[50] = {0.};
251  double err4[50] = {0.};
252  double err5[50] = {0.};
253  double err6[50] = {0.};
254 
255  for(unsigned int n = 1; n < 49; ++n){
256 
257  if(nhit_2d[n]!=0) {
258 
259  err4[n] = sqrt(rate_hi_2d[n]) / telapse_2d[n];
260  err5[n] = sqrt(rate_low_2d[n]) / telapse_2d[n];
261 
262  pe_2d[n] = pe_2d[n]/nhit_2d[n];
263  pecorr_2d[n] = pecorr_2d[n]/nhit_2d[n];
264  pesq_2d[n] = pesq_2d[n]/nhit_2d[n];
265  pecorrsq_2d[n] = pecorrsq_2d[n]/nhit_2d[n];
266  nn_2d[n] = nn_2d[n]/nhit_2d[n];
267  rate_hi_2d[n] = rate_hi_2d[n]/telapse_2d[n];
268  rate_low_2d[n] = rate_low_2d[n]/telapse_2d[n];
269 
270  err3[n] = ( sqrt(nhit_2d[n]) / nhit_2d[n] ) * nn_2d[n];
271 
272  if(pesq_2d[n]==0) {
273  err1[n] = ( 1 / sqrt(nhit_2d[n]) ) * pe_2d[n];
274  err2[n] = ( 1 / sqrt(nhit_2d[n]) ) * pecorr_2d[n];
275  }
276  else if(pesq_2d[n]>0) {
277  err1[n] = sqrt( fabs(pesq_2d[n] - (pe_2d[n]*pe_2d[n])) ) * 1.5 / sqrt(nhit_2d[n]);
278  err2[n] = sqrt( fabs(pecorrsq_2d[n] - (pecorr_2d[n]*pecorr_2d[n])) * 1.5 / sqrt(nhit_2d[n]) );
279  }
280  else {
281  std::cout << "Negative squares??? Something is wrong!" << std::endl;
282  }
283 
284  }
285  if(count_2d[n]!=0) {
286  tave[n] = tave[n]/count_2d[n];
287  err6[n] = (tmax[n]-tmin[n])/2.0;
288  }
289  }
290 
291  pe_apd_2d->SetContent(pe_2d);
292  pec_apd_2d->SetContent(pecorr_2d);
293  nn_apd_2d->SetContent(nn_2d);
294  rh_apd_2d->SetContent(rate_hi_2d);
295  rl_apd_2d->SetContent(rate_low_2d);
296  apd_temp->SetContent(tave);
297 
298  pe_apd_2d->SetError(err1);
299  pec_apd_2d->SetError(err2);
300  nn_apd_2d->SetError(err3);
301  rh_apd_2d->SetError(err4);
302  rl_apd_2d->SetError(err5);
303  apd_temp->SetError(err6);
304 
305  delete stmt;
306 
307  }
308 
309  std::stringstream dbstream2;
310 
311  dbstream2 << "select pixel,channel from daq_info where "
312  << " dcm="<<dcm
313  << " and diblock="<<diblock
314  << " and port="<<port
315  << " order by pixel";
316 
317  std::string dbstring2 = dbstream2.str();
318 
319  char * dbcstr2;
320  dbcstr2 = new char [dbstring2.size()+1];
321  strcpy(dbcstr2, dbstring2.c_str());
322 
323  TSQLStatement *stmt2 = db->Statement(dbcstr2,1000);
324 
325  if(stmt2->Process()){
326  // store result of statement in buffer
327  stmt2->StoreResult();
328 
329  while (stmt2->NextResultRow()) {
330 
331  const char* pix = stmt2->GetString(0);
332  const char* chann = stmt2->GetString(1);
333 
334  cout << "pixel: " << pix << " channel: " << chann << endl;
335 
336  }
337  }
338 
339  delete db;
340 
341 }
diblock
print "ROW IS " print row
Definition: geo2elec.py:31
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
TDatime * TNow
Definition: AnaPlotMaker.h:41
T sqrt(T number)
Definition: d0nt_math.hpp:156
OStream cout
Definition: OStream.cxx:6
static float min(const float a, const float b, const float c)
Definition: absgeo.cxx:45
void APDPlots(int diblock, int dcm, int port)
Definition: APDPlots.C:11
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
int port
Definition: client_test.C:9
enum BeamMode string