00001
00002
00003
00004 #include <TROOT.h>
00005 #include <TH2.h>
00006 #include <TStyle.h>
00007 #include <TCanvas.h>
00008 #include "Plex/PlexHandle.h"
00009 #include <iostream>
00010 #include <TGraphErrors.h>
00011 #include <TF1.h>
00012 #include <TSystem.h>
00013 #include <TStyle.h>
00014 #include <stdio.h>
00015
00016
00017 #include "mysql.h"
00018 #include <TPostScript.h>
00019 #include "TMySQLServer.h"
00020 #include "TSQLResult.h"
00021 #include "TSQLRow.h"
00022 #include "Calibrator/Calibrator.h"
00023
00031
00032
00033
00034
00035
00036 using namespace std;
00037
00038
00039
00040
00041
00042 TCanvas* GetCanvas(const char* name)
00043 {
00044 TCanvas* ctmp = (TCanvas*) gROOT->FindObject(name);
00045 if(!ctmp) ctmp = new TCanvas(name,name);
00046 return ctmp;
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070 static std::vector<TObject*> cleanup;
00071 static TGraph* gTemperature1= 0;
00072 static TGraph* gTemperature2= 0;
00073 TMySQLServer* server = 0;
00074
00075 void MakeTemperaturePlot(TMySQLServer* server,
00076 Detector::Detector_t det,
00077 UInt_t start, UInt_t stop)
00078 {
00079 const char* query;
00080 if(det == Detector::kFar) {
00081 query = Form("select "
00082 "UNIX_TIMESTAMP(DCS_ENV_FARVLD.TIMESTART), "
00083 "DCS_ENV_FAR.FAR_TEMPERATURE1, "
00084 "DCS_ENV_FAR.FAR_TEMPERATURE2 "
00085 "from DCS_ENV_FARVLD, DCS_ENV_FAR "
00086 "where (DCS_ENV_FAR.SEQNO=DCS_ENV_FARVLD.SEQNO) "
00087 "and (UNIX_TIMESTAMP(DCS_ENV_FARVLD.TIMESTART)>%d) "
00088 "and (UNIX_TIMESTAMP(DCS_ENV_FARVLD.TIMESTART)<%d) ",
00089 start,stop
00090 );
00091 } else {
00092
00093 query = Form("select "
00094 "UNIX_TIMESTAMP(DCS_ENV_NEARVLD.TIMESTART), "
00095 "DCS_ENV_NEAR.NEAR_THERM_V3b, "
00096 "DCS_ENV_NEAR.NEAR_THERM_V3t "
00097 "from DCS_ENV_NEARVLD, DCS_ENV_NEAR "
00098 "where (DCS_ENV_NEAR.SEQNO=DCS_ENV_NEARVLD.SEQNO) "
00099 "and (UNIX_TIMESTAMP(DCS_ENV_NEARVLD.TIMESTART)>%d) "
00100 "and (UNIX_TIMESTAMP(DCS_ENV_NEARVLD.TIMESTART)<%d) ",
00101 start,stop
00102 );
00103 }
00104
00105 cout << query << endl;
00106
00107 TSQLResult* result = server->Query(query);
00108 if(!result) return;
00109
00110 if(gTemperature1) delete gTemperature1;
00111 if(gTemperature2) delete gTemperature2;
00112
00113 cout << "Building temperature plot with "
00114 << " rows=" << result->GetRowCount() << endl;
00115
00116 gTemperature1 = new TGraph(result->GetRowCount());
00117 gTemperature2 = new TGraph(result->GetRowCount());
00118
00119 double* x1 = gTemperature1->GetX();
00120 double* y1 = gTemperature1->GetY();
00121 double* x2 = gTemperature2->GetX();
00122 double* y2 = gTemperature2->GetY();
00123
00124 int p = 0;
00125
00126 TSQLRow* row = 0;
00127 while( ( row = result->Next() ) ) {
00128
00129 UInt_t time;
00130 Float_t farenheit1;
00131 Float_t farenheit2;
00132
00133 sscanf(row->GetField(0),"%u",&time);
00134 sscanf(row->GetField(1),"%f",&farenheit1);
00135 sscanf(row->GetField(2),"%f",&farenheit2);
00136
00137 x1[p] = time;
00138 x2[p] = time;
00139 y1[p] = (farenheit1-32.0)/1.8;
00140 y2[p] = (farenheit2-32.0)/1.8;
00141
00142 p++;
00143 }
00144
00145 }
00146
00147
00148 int PlotPmt(
00149 TMySQLServer* server,
00150 PlexPixelSpotId tube)
00151 {
00152 for(UInt_t i=0;i<cleanup.size();i++) delete cleanup[i];
00153 cleanup.clear();
00154
00155 UInt_t encoded = tube.GetUniquePmtEncodedValue();
00156
00157 PlexHandle plex(VldContext(tube.GetDetector(), SimFlag::kData, VldTimeStamp()));
00158 PlexPixelSpotId psid = tube;
00159 psid.SetPixel(1);
00160 psid.SetSpot(1);
00161 RawChannelId rcid = plex.GetRawChannelId(psid);
00162
00163 char title[1000];
00164 sprintf(title,"%s (%u) %s",tube.AsString("t"),encoded,rcid.AsString("e"));
00165
00166 cout << title << endl;
00167
00168
00169 Int_t point = 0;
00170
00171 const char* query = Form("select "
00172 "UNIX_TIMESTAMP(CALPMTDRIFTVLD.TIMESTART) "
00173 ",CALPMTDRIFT.DRIFT "
00174 ",CALPMTDRIFT.STATERROR "
00175 ",CALPMTDRIFT.SYSERROR "
00176 ",CALPMTDRIFT.SPOTSUSED "
00177 ",CALPMTDRIFT.SPOTSAVAIL "
00178 ",CALPMTDRIFT.CRUDEDRIFT "
00179 ",CALPMTDRIFT.MINDRIFT "
00180 ",CALPMTDRIFT.MAXDRIFT "
00181 "from CALPMTDRIFTVLD, CALPMTDRIFT where "
00182 "(CALPMTDRIFT.PMT = %u) "
00183 "and (CALPMTDRIFT.SEQNO=CALPMTDRIFTVLD.SEQNO) "
00184 "and (CALPMTDRIFTVLD.TASK=1) "
00185
00186
00187
00188 "and (CALPMTDRIFTVLD.DETECTORMASK=%d) order by CALPMTDRIFTVLD.TIMESTART;",
00189 encoded,
00190 tube.GetDetector()
00191 );
00192
00193 cout << query << endl;
00194
00195 TSQLResult* result = server->Query(query);
00196 if(!result) return 0;
00197
00198 cout << tube.AsString("t")
00199 << " " << encoded
00200 << " rows=" << result->GetRowCount() << endl;
00201
00202
00203 int npoints = result->GetRowCount();
00204
00205 TGraphErrors* gFinal = new TGraphErrors(npoints); cleanup.push_back(gFinal);
00206 TGraphErrors* gRaw = new TGraphErrors(npoints); cleanup.push_back(gRaw);
00207 TGraph* gHigh = new TGraph(npoints); cleanup.push_back(gHigh);
00208 TGraph* gLow = new TGraph(npoints); cleanup.push_back(gLow);
00209 TGraph* gNgood = new TGraph(npoints); cleanup.push_back(gNgood);
00210 TGraph* gNspot = new TGraph(npoints); cleanup.push_back(gNspot);
00211
00212
00213 TSQLRow* row = 0;
00214 while( ( row = result->Next() ) ) {
00215
00216 if(point>npoints) break;
00217 UInt_t time;
00218 Float_t drift;
00219 Float_t staterr;
00220 Float_t syserr;
00221 Float_t ngood;
00222 Float_t nspot;
00223 Float_t rawdrift;
00224 Float_t mindrift = -999;
00225 Float_t maxdrift = -999;
00226
00227 int irow=0;
00228 sscanf(row->GetField(irow++),"%u",&time);
00229 sscanf(row->GetField(irow++),"%f",&drift);
00230 sscanf(row->GetField(irow++),"%f",&staterr);
00231 sscanf(row->GetField(irow++),"%f",&syserr);
00232 sscanf(row->GetField(irow++),"%f",&ngood);
00233 sscanf(row->GetField(irow++),"%f",&nspot);
00234 sscanf(row->GetField(irow++),"%f",&rawdrift);
00235 sscanf(row->GetField(irow++),"%f",&mindrift);
00236 sscanf(row->GetField(irow++),"%f",&maxdrift);
00237
00238
00239 gFinal->SetPoint (point,time, drift);
00240 gFinal->SetPointError(point,0, staterr);
00241 gRaw->SetPoint (point,time, rawdrift);
00242 gRaw->SetPointError (point,0, 0);
00243 gHigh->SetPoint (point,time, mindrift);
00244 gLow->SetPoint (point,time, maxdrift);
00245 gNgood->SetPoint (point,time, ngood);
00246 gNspot->SetPoint (point,time, nspot);
00247
00248
00249
00250
00251
00252
00253 point++;
00254 }
00255
00256 if(point==0) return 0;
00257
00258
00259
00260 double finalmean = 0;
00261 double rawmean = 0;
00262 double finaln = 0;
00263 double rawn = 0;
00264 double* finalX = gFinal->GetX();
00265 double* finalY = gFinal->GetY();
00266 double* finalEY = gFinal->GetEY();
00267 double* rawY = gRaw->GetY();
00268 double* rawEY = gRaw->GetEY();
00269 for(int i=0;i<point;i++) {
00270 if(finalY[i]>10.) {
00271 finalmean += finalY[i];
00272 finaln+=1.;
00273 }
00274 if(rawY[i]>10.) {
00275 rawmean += rawY[i];
00276 rawn += 1.0;
00277 }
00278 }
00279 finalmean /= rawn;
00280 rawmean /= finaln;
00281
00282
00283 TGraphErrors* gRelFinal = new TGraphErrors(npoints); cleanup.push_back(gRelFinal);
00284 TGraphErrors* gRelRaw = new TGraphErrors(npoints); cleanup.push_back(gRelRaw);
00285
00286 Double_t* relFinalX = gRelFinal->GetX();
00287 Double_t* relFinalY = gRelFinal->GetY();
00288 Double_t* relFinalEX= gRelFinal->GetEX();
00289 Double_t* relFinalEY= gRelFinal->GetEY();
00290 Double_t* relRawX = gRelRaw->GetX();
00291 Double_t* relRawY = gRelRaw->GetY();
00292 Double_t* relRawEX= gRelRaw->GetEX();
00293 Double_t* relRawEY= gRelRaw->GetEY();
00294 for(Int_t i=0;i<npoints;i++) {
00295 relFinalX[i] = finalX[i];
00296 relFinalY[i] = (finalY[i]-finalmean)/finalmean * 100.;
00297 relFinalEX[i] = 0;
00298 relFinalEY[i] = finalEY[i]/finalmean * 100.;
00299
00300 relRawX[i] = finalX[i];
00301 relRawY[i] = (rawY[i]-rawmean)/rawmean * 100.;
00302 relRawEX[i] = 0;
00303 relRawEY[i] = rawEY[i]/rawmean * 100.;
00304 }
00305
00306
00307 TCanvas* cpmt = GetCanvas("cpmt");
00308 cpmt->cd();
00309 cpmt->Clear();
00310 cpmt->Divide(1,3,0.001,0.001,kWhite);
00311 cpmt->cd(1);
00312
00313
00314 double tlow = gFinal->GetX()[0];
00315 double thigh = gFinal->GetX()[point-1];
00316
00317
00318
00319
00320
00321
00322
00323 if(gTemperature1==0) MakeTemperaturePlot(server,
00324 tube.GetDetector(),
00325 (UInt_t)tlow,
00326 (UInt_t)thigh);
00327
00328 double ymax = 150.;
00329 if(tube.GetDetector()==Detector::kNear) ymax=500.;
00330 TH2F* hFrame1 = new TH2F("hFrame1",
00331 title,
00332 100,tlow,thigh,
00333 100,0,ymax);
00334 hFrame1->SetDirectory(0);
00335 cleanup.push_back(hFrame1);
00336
00337 hFrame1->GetXaxis()->SetTimeDisplay(1);
00338 hFrame1->GetXaxis()->SetTimeFormat("%d/%b/%y %Hh");
00339
00340 hFrame1->SetXTitle("Date");
00341 hFrame1->SetYTitle("Gain (ADC counts)");
00342 hFrame1->SetStats(0);
00343 hFrame1->Draw("");
00344
00345
00346 gRaw->SetLineColor(kRed);
00347 gRaw->SetMarkerColor(kRed);
00348 gRaw->SetMarkerStyle(20);
00349 gRaw->SetMarkerSize(0.45);
00350 gRaw->Draw("P");
00351
00352 gFinal->SetMarkerStyle(20);
00353 gFinal->SetMarkerSize(0.5);
00354 gFinal->Draw("P");
00355
00356 gLow->SetMarkerStyle(23);
00357 gLow->SetMarkerSize(0.6);
00358 gLow->SetMarkerColor(kBlue);
00359 gLow->Draw("P");
00360 gHigh->SetMarkerStyle(24);
00361 gHigh->SetMarkerSize(0.6);
00362 gHigh->SetMarkerColor(kBlue);
00363 gHigh->Draw("P");
00364
00365 gNspot->SetLineColor(kBlack);
00366 gNspot->Draw("L");
00367
00368 gNgood->SetLineColor(kGreen);
00369 gNgood->Draw("L");
00370
00371 if(gTemperature1) {
00372 gTemperature1->SetLineColor(kBlue);
00373 gTemperature1->Draw("L");
00374 }
00375
00376
00377 cpmt->cd(2);
00378 TH2F* hFrame2 = new TH2F("hFrame2",
00379 title,
00380 100,tlow,thigh,
00381 100,-4,4.);
00382 hFrame2->SetDirectory(0);
00383 cleanup.push_back(hFrame2);
00384
00385 hFrame2->SetStats(0);
00386 hFrame2->GetXaxis()->SetTimeDisplay(1);
00387 hFrame2->GetXaxis()->SetTimeFormat("%d/%b/%y %Hh");
00388 hFrame2->SetLabelSize(0.04,"x");
00389 hFrame2->SetXTitle("Date");
00390 hFrame2->SetYTitle("Relative Gain (%)");
00391 hFrame2->Draw("");
00392
00393
00394 gRelRaw->SetLineColor(kRed);
00395 gRelRaw->SetMarkerColor(kRed);
00396 gRelRaw->SetMarkerStyle(21);
00397 gRelRaw->SetMarkerSize(0.5);
00398 gRelRaw->Draw("P");
00399
00400 gRelFinal->SetMarkerStyle(20);
00401 gRelFinal->SetMarkerSize(0.5);
00402 gRelFinal->Draw("P");
00403
00404 gStyle->SetOptFit(11111);
00405
00406 TF1 *f1 = new TF1("f1","[0]",0,1e99);
00407 gRelFinal->Fit(f1,"RQ");
00408 f1->Draw("l same");
00409
00410
00411 cpmt->cd(3);
00412 TH2F* hFrame3 = new TH2F("hFrame3","Temperature",
00413 100,tlow,thigh,
00414 100,15,30.);
00415 hFrame3->SetDirectory(0);
00416 cleanup.push_back(hFrame3);
00417
00418 hFrame3->SetStats(0);
00419 hFrame3->GetXaxis()->SetTimeDisplay(1);
00420 hFrame2->GetXaxis()->SetTimeFormat("%d/%b/%y %Hh");
00421 hFrame3->SetLabelSize(0.04,"x");
00422 hFrame3->SetXTitle("Date");
00423 hFrame3->SetYTitle("Temperature");
00424 hFrame3->Draw("");
00425
00426 if(gTemperature2) {
00427 gTemperature2->SetLineColor(kCyan);
00428 gTemperature2->SetMarkerColor(kCyan);
00429 gTemperature2->SetMarkerStyle(20);
00430 gTemperature2->SetMarkerSize(0.01);
00431 gTemperature2->Draw("P");
00432 }
00433
00434 if(gTemperature1) {
00435 gTemperature1->SetLineColor(kBlue);
00436 gTemperature1->SetMarkerColor(kBlue);
00437 gTemperature1->SetMarkerStyle(20);
00438 gTemperature1->SetMarkerSize(0.01);
00439 gTemperature1->Draw("P");
00440 }
00441
00442 cout << "Done." << endl;
00443
00444 cpmt->Update();
00445 return point;
00446 }
00447
00448
00449 void pmt_plot()
00450 {
00451 TCanvas* cpmt = (TCanvas*) gROOT->FindObject("cpmt");
00452 if(cpmt) delete cpmt;
00453 cpmt = new TCanvas("cpmt","cpmt",1000,750);
00454
00455 TPostScript* ps = 0;
00456 ps = new TPostScript("pmt_drift.ps",112);
00457 gStyle->SetTimeOffset(0);
00458
00459
00460
00461 Calibrator::Instance();
00462
00463
00464 TString host(getenv("ENV_TSQL_URL") );
00465 host.ReplaceAll("odbc:","");
00466 if(!server) {
00467
00468 server = new TMySQLServer( "mysql://localhost/offline", "reader","numi96");
00469
00470
00471
00472
00473 }
00474
00475 PlexPixelSpotId tube;
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490 Detector::Detector_t det = Detector::kFar;
00491
00492
00493 VldContext cx(det,SimFlag::kData,VldTimeStamp());
00494 PlexHandle plex(cx);
00495 const std::vector<PlexPixelSpotId>& tubes = plex.GetAllTubes();
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 if(ps) ps->NewPage();
00510
00511 for(UInt_t i=0;i<tubes.size();i++) {
00512 tube = tubes[i];
00513 int res = PlotPmt(server, tube);
00514 if(res) {
00515 ps->NewPage();
00516 cpmt->Print(Form("pmt_drifts/%s.gif",tube.AsString("t")));
00517 }
00518 }
00519
00520 if(ps) ps->Close();
00521 }
00522
00526
00527
00528
00529 void draw(UInt_t itube)
00530 {
00531
00532 TString host(getenv("ENV_TSQL_URL") );
00533 host.ReplaceAll("odbc:","");
00534 if(!server) server = new TMySQLServer( host.Data(),
00535 getenv("ENV_TSQL_USER"),
00536 getenv("ENV_TSQL_PSWD")
00537 );
00538
00539 TCanvas* cpmt = (TCanvas*) gROOT->FindObject("cpmt");
00540 if(!cpmt) cpmt = new TCanvas("cpmt","cpmt",1000,750);
00541
00542 PlexPixelSpotId tube(itube);
00543
00544 cout << "Trying tube " << itube << " " << tube.AsString() << tube.GetUniquePmtEncodedValue() << endl;
00545 PlotPmt(server, tube);
00546 }