00001
00002
00003 #include <iostream>
00004 #include "PulserCalibration/Plotter.h"
00005 #include "TROOT.h"
00006 #include "TPad.h"
00007 #include "TCanvas.h"
00008 #include "TGraphErrors.h"
00009 #include "TGraph.h"
00010 #include "TAxis.h"
00011 #include "TF1.h"
00012
00013 using std::cout;
00014 using std::endl;
00015
00016
00017
00018
00019 Plotter::Plotter(VldContext vld) : fPinRP(vld), fNearRP(vld), fFarRP(vld), fPlex(vld)
00020 {
00021 fVld = vld;
00022 fRes = 0;
00023 cout<<"Got Tables for "<<vld<<endl;
00024 cout<<"Pin: "<<fPinRP.GetNumRows()<<" rows"<<endl;
00025 cout<<"Near: "<<fNearRP.GetNumRows()<<" rows"<<endl;
00026 cout<<"Far: "<<fFarRP.GetNumRows()<<" rows"<<endl;
00027 }
00028
00029 void Plotter::BasicPlot(int plane, int strip)
00030 {
00031
00032 PlexStripEndId seid(Detector::kCalDet, plane, strip, StripEnd::kEast);
00033 PlexStripEndId other(Detector::kCalDet, plane, strip, StripEnd::kWest);
00034
00035 PlexLedId led = myGetLedId(fPlex,seid);
00036 PlexLedId otherled = myGetLedId(fPlex,other);
00037
00038 std::pair<PlexPinDiodeId,PlexPinDiodeId> pins = myGetPinDiodeIds(fPlex,led);
00039 std::pair<PlexPinDiodeId,PlexPinDiodeId> otherpins = myGetPinDiodeIds(fPlex,otherled);
00040
00041 const PulserRawGainPin* nph = fPinRP.GetRowByIndex(pins.first.GetEncoded());
00042 const PulserRawGainPin* npl = fPinRP.GetRowByIndex(pins.second.GetEncoded());
00043 const PulserRawGainPin* fph = fPinRP.GetRowByIndex(otherpins.first.GetEncoded());
00044 const PulserRawGainPin* fpl = fPinRP.GetRowByIndex(otherpins.second.GetEncoded());
00045 cout<<"PIN Ids: "<<pins.first.GetEncoded()<<" "<<pins.second.GetEncoded()
00046 <<" "<<otherpins.first.GetEncoded()<<" "<<otherpins.second.GetEncoded()
00047 <<endl;
00048 TVirtualPad *parent = gPad;
00049 if (!parent) {
00050
00051 new TCanvas("plotter","Plotter",0,0,700,700);
00052 parent = gPad;
00053 }
00054 parent->Clear();
00055 parent->Divide(2,2,0,0,0);
00056
00057
00058 const PulserRawGain* near = fNearRP.GetRowByIndex(seid.BuildPlnStripEndKey());
00059 const PulserRawGainF* far = fFarRP.GetRowByIndex(other.BuildPlnStripEndKey());
00060 if (near && far) {
00061 parent->cd(1);
00062 TGraphErrors *g1h =
00063 new TGraphErrors(near->GetNumPoints(),
00064 nph->GetMean(),near->GetMean(),
00065 nph->GetError(),near->GetError());
00066 g1h->SetMarkerStyle(2);
00067 g1h->SetMarkerColor(2);
00068 g1h->Draw("AP");
00069 TGraphErrors *g1l =
00070 new TGraphErrors(near->GetNumPoints(),
00071 npl->GetMean(),near->GetMean(),
00072 npl->GetError(),near->GetError());
00073 g1l->SetMarkerStyle(2);
00074 g1l->SetMarkerColor(4);
00075 g1l->Draw("P");
00076
00077 parent->cd(2);
00078 TGraphErrors *g2h =
00079 new TGraphErrors(far->GetNumPoints(),
00080 nph->GetMean(),far->GetMean(),
00081 nph->GetError(),far->GetError());
00082 g2h->SetMarkerStyle(2);
00083 g2h->SetMarkerColor(2);
00084 g2h->Draw("AP");
00085 TGraphErrors *g2l =
00086 new TGraphErrors(far->GetNumPoints(),
00087 npl->GetMean(),far->GetMean(),
00088 npl->GetError(),far->GetError());
00089 g2l->SetMarkerStyle(2);
00090 g2l->SetMarkerColor(4);
00091 g2l->Draw("P");
00092 }
00093 near = fNearRP.GetRowByIndex(other.BuildPlnStripEndKey());
00094 far = fFarRP.GetRowByIndex(seid.BuildPlnStripEndKey());
00095 if (near && far) {
00096 parent->cd(4);
00097 TGraphErrors *g4h =
00098 new TGraphErrors(near->GetNumPoints(),
00099 fph->GetMean(),near->GetMean(),
00100 fph->GetError(),near->GetError());
00101 g4h->SetMarkerStyle(2);
00102 g4h->SetMarkerColor(2);
00103 g4h->Draw("AP");
00104 TGraphErrors *g4l =
00105 new TGraphErrors(near->GetNumPoints(),
00106 fpl->GetMean(),near->GetMean(),
00107 fpl->GetError(),near->GetError());
00108 g4l->SetMarkerStyle(2);
00109 g4l->SetMarkerColor(4);
00110 g4l->Draw("P");
00111
00112 parent->cd(3);
00113 TGraphErrors *g3h =
00114 new TGraphErrors(far->GetNumPoints(),
00115 fph->GetMean(),far->GetMean(),
00116 fph->GetError(),far->GetError());
00117 g3h->SetMarkerStyle(2);
00118 g3h->SetMarkerColor(2);
00119 g3h->Draw("AP");
00120 TGraphErrors *g3l =
00121 new TGraphErrors(far->GetNumPoints(),
00122 fpl->GetMean(),far->GetMean(),
00123 fpl->GetError(),far->GetError());
00124 g3l->SetMarkerStyle(2);
00125 g3l->SetMarkerColor(4);
00126 g3l->Draw("P");
00127 }
00128
00129 parent->cd(0);
00130
00131
00132 }
00133
00134
00135
00136 TGraphErrors * Plotter::Plot(int yplane, int ystrip, StripEnd::StripEnd_t yend,
00137 int xplane, int xstrip, StripEnd::StripEnd_t xend,
00138 int led, char *draw_opt)
00139 {
00140 TPad *parent = dynamic_cast<TPad *>(gPad);
00141 if (!parent) {
00142
00143 new TCanvas("plotter","Plotter",0,0,700,700);
00144 parent = dynamic_cast<TPad *>(gPad);
00145 }
00146
00147
00148 const float *x=0;
00149 const float *ex = 0;
00150 const float *y = 0;
00151 const float *ey = 0;
00152 int num;
00153
00154
00155 PlexStripEndId xid(Detector::kCalDet, xplane, xstrip, xend);
00156
00157 PlexLedId tryled = myGetLedId(fPlex, xid);
00158 if (tryled.GetLedInBox() == led) {
00159
00160 const PulserRawGain *xn = fNearRP.GetRowByIndex(xid.BuildPlnStripEndKey());
00161 if (!xn) cout<<" Oh Bugger!"<<endl;
00162 x = xn->GetMean();
00163 ex = xn->GetError();
00164 num = xn->GetNumPoints();
00165 }
00166 else if (myGetLedId(fPlex,xid.GetOppositeSEId()).GetLedInBox() == led) {
00167
00168 const PulserRawGainF *xn = fFarRP.GetRowByIndex(xid.BuildPlnStripEndKey());
00169 if (!xn) cout<<" Oh Bugger!"<<endl;
00170 x = xn->GetMean();
00171 ex = xn->GetError();
00172 num = xn->GetNumPoints();
00173 }
00174 else {
00175 cout<<" No X data for this LED"<<endl;
00176 return 0;
00177 }
00178
00179
00180 PlexStripEndId yid(Detector::kCalDet, yplane, ystrip, yend);
00181
00182
00183 if (myGetLedId(fPlex,yid).GetLedInBox() == led) {
00184
00185 const PulserRawGain *yn = fNearRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00186 if (!yn) cout<<" Oh Bugger!"<<endl;
00187 y = yn->GetMean();
00188 ey = yn->GetError();
00189 }
00190 else if (myGetLedId(fPlex,yid.GetOppositeSEId()).GetLedInBox() == led) {
00191
00192 const PulserRawGainF *yn = fFarRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00193 if (!yn) cout<<" Oh Bugger!"<<endl;
00194 y = yn->GetMean();
00195 ey = yn->GetError();
00196 }
00197 else {
00198 cout<<" No Y data for this LED"<<endl;
00199 return 0;
00200 }
00201
00202 TGraphErrors *g = new TGraphErrors(num,x,y,ex,ey);
00203 g->SetMarkerStyle(2);
00204 g->SetMarkerColor(2);
00205 g->SetTitle("");
00206 g->Draw(draw_opt);
00207 char name[100];
00208 sprintf(name,"Plane %d Strip %d %s",xplane,xstrip,StripEnd::AsString(xend));
00209 if (g->GetXaxis()) g->GetXaxis()->SetTitle(name);
00210 sprintf(name,"Plane %d Strip %d %s",yplane,ystrip,StripEnd::AsString(yend));
00211 if (g->GetXaxis()) g->GetYaxis()->SetTitle(name);
00212 g->Draw(draw_opt);
00213 return g;
00214 }
00215
00216 TGraphErrors * Plotter::Plot(int plane, int strip, StripEnd::StripEnd_t end,
00217 float *x, float *ex, char *xtitle,
00218 int led, char *draw_opt)
00219 {
00220 TPad *parent = dynamic_cast<TPad *>(gPad);
00221 if (!parent) {
00222
00223 new TCanvas("plotter","Plotter",0,0,700,700);
00224 parent = dynamic_cast<TPad *>(gPad);
00225 }
00226 const float *y = 0;
00227 const float *ey = 0;
00228 int num;
00229
00230
00231 PlexStripEndId yid(Detector::kCalDet, plane, strip, end);
00232
00233
00234 if (myGetLedId(fPlex,yid).GetLedInBox() == led) {
00235
00236 const PulserRawGain *yn = fNearRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00237 if (!yn) cout<<" Oh Bugger!"<<endl;
00238 y = yn->GetMean();
00239 ey = yn->GetError();
00240 num = yn->GetNumPoints();
00241 }
00242 else if (myGetLedId(fPlex,yid.GetOppositeSEId()).GetLedInBox() == led) {
00243
00244 const PulserRawGainF *yn = fFarRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00245 if (!yn) cout<<" Oh Bugger!"<<endl;
00246 y = yn->GetMean();
00247 ey = yn->GetError();
00248 num = yn->GetNumPoints();
00249 }
00250 else {
00251 cout<<" No Y data for this LED"<<endl;
00252 return 0;
00253 }
00254 TGraphErrors *g = new TGraphErrors(num,x,y,ex,ey);
00255 g->SetMarkerStyle(2);
00256 g->SetMarkerColor(2);
00257 g->SetTitle("");
00258 g->Draw(draw_opt);
00259 char name[100];
00260 if (g->GetXaxis()) g->GetXaxis()->SetTitle(xtitle);
00261 sprintf(name,"Plane %d Strip %d %s",plane,strip,StripEnd::AsString(end));
00262 if (g->GetXaxis()) g->GetYaxis()->SetTitle(name);
00263 g->Draw(draw_opt);
00264 return g;
00265
00266 }
00267
00268 TGraphErrors * Plotter::Plot(int plane, int strip, StripEnd::StripEnd_t end,
00269 int gain,
00270 int led, char *draw_opt)
00271 {
00272 const float *x = 0;
00273 const float *ex = 0;
00274 const float *y = 0;
00275 const float *ey = 0;
00276 int num;
00277
00278 PlexStripEndId yid(Detector::kCalDet, plane, strip, end);
00279
00280
00281 if (myGetLedId(fPlex,yid).GetLedInBox() == led) {
00282
00283 const PulserRawGain *yn = fNearRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00284 if (!yn) cout<<" Oh Bugger!"<<endl;
00285 y = yn->GetMean();
00286 ey = yn->GetError();
00287 num = yn->GetNumPoints();
00288 }
00289 else if (myGetLedId(fPlex,yid.GetOppositeSEId()).GetLedInBox() == led) {
00290
00291 const PulserRawGainF *yn = fFarRP.GetRowByIndex(yid.BuildPlnStripEndKey());
00292 if (!yn) cout<<" Oh Bugger!"<<endl;
00293 y = yn->GetMean();
00294 ey = yn->GetError();
00295 num = yn->GetNumPoints();
00296 }
00297 else {
00298 cout<<" No Y data for this LED"<<endl;
00299 return 0;
00300 }
00301
00302
00303 PlexLedId ledid(Detector::kCalDet, 0, led);
00304 cout<<ledid<<endl;
00305 std::pair<PlexPinDiodeId,PlexPinDiodeId> pinids = myGetPinDiodeIds(fPlex,ledid);
00306 cout<<pinids.first<<" "<<pinids.second<<endl;
00307 PlexPinDiodeId pinid;
00308 if (gain==1) {
00309 pinid = pinids.first;
00310 } else {
00311 pinid = pinids.second;
00312 }
00313
00314 const PulserRawGainPin *thepin = fPinRP.GetRowByIndex(pinid.GetEncoded());
00315 cout<<"PIN Id: "<<pinid.GetEncoded()<<endl;
00316 if (!thepin) cout<<"Oh Bugger"<<endl;
00317 x = thepin->GetMean();
00318 ex = thepin->GetError();
00319
00320 TGraphErrors *g = new TGraphErrors(num,x,y,ex,ey);
00321 g->SetMarkerStyle(2);
00322 g->SetMarkerColor(2);
00323 g->SetTitle("");
00324 g->Draw(draw_opt);
00325 char name[100];
00326 sprintf(name,"PIN Diode, LED %d gain %d",led,gain);
00327 if (g->GetXaxis()) g->GetXaxis()->SetTitle(name);
00328 sprintf(name,"Plane %d Strip %d %s",plane,strip,StripEnd::AsString(end));
00329 if (g->GetXaxis()) g->GetYaxis()->SetTitle(name);
00330 g->Draw(draw_opt);
00331 return g;
00332
00333 }
00334
00335 TGraphErrors * Plotter::Plot(int gain,
00336 float *x, float *ex, char *xtitle,
00337 int led, char *draw_opt)
00338 {
00339
00340 const float *y = 0;
00341 const float *ey = 0;
00342 int num;
00343
00344 PlexLedId ledid(Detector::kCalDet, 0, led);
00345 cout<<ledid<<endl;
00346 std::pair<PlexPinDiodeId,PlexPinDiodeId> pinids = myGetPinDiodeIds(fPlex,ledid);
00347 cout<<pinids.first<<" "<<pinids.second<<endl;
00348 PlexPinDiodeId pinid;
00349 if (gain==1) {
00350 pinid = pinids.first;
00351 } else {
00352 pinid = pinids.second;
00353 }
00354
00355 const PulserRawGainPin *thepin = fPinRP.GetRowByIndex(pinid.GetEncoded());
00356 cout<<"PIN Id: "<<pinid.GetEncoded()<<endl;
00357 if (!thepin) cout<<"Oh Bugger"<<endl;
00358 y = thepin->GetMean();
00359 ey = thepin->GetError();
00360 num = thepin->GetNumPoints();
00361
00362 TGraphErrors *g = new TGraphErrors(num,x,y,ex,ey);
00363 g->SetMarkerStyle(2);
00364 g->SetMarkerColor(2);
00365 g->SetTitle("");
00366 g->Draw(draw_opt);
00367 char name[100];
00368 sprintf(name,"PIN Diode, LED %d gain %d",led,gain);
00369 if (g->GetYaxis()) g->GetYaxis()->SetTitle(name);
00370
00371 if (g->GetXaxis()) g->GetXaxis()->SetTitle(xtitle);
00372 g->Draw(draw_opt);
00373 return g;
00374
00375
00376 }
00377
00378 TGraphErrors * Plotter::Plot(int gain,
00379 int gain2,
00380 int led, char *draw_opt)
00381 {
00382 const float *x = 0;
00383 const float *ex = 0;
00384 const float *y = 0;
00385 const float *ey = 0;
00386 int num;
00387
00388
00389 PlexLedId ledid(Detector::kCalDet, 0, led);
00390 cout<<ledid<<endl;
00391 std::pair<PlexPinDiodeId,PlexPinDiodeId> pinids = myGetPinDiodeIds(fPlex,ledid);
00392 cout<<pinids.first<<" "<<pinids.second<<endl;
00393 PlexPinDiodeId pinid;
00394 if (gain==1) {
00395 pinid = pinids.first;
00396 } else {
00397 pinid = pinids.second;
00398 }
00399
00400 const PulserRawGainPin *thepin = fPinRP.GetRowByIndex(pinid.GetEncoded());
00401 cout<<"PIN Id: "<<pinid.GetEncoded()<<endl;
00402 if (!thepin) cout<<"Oh Bugger"<<endl;
00403 y = thepin->GetMean();
00404 ey = thepin->GetError();
00405 num = thepin->GetNumPoints();
00406
00407
00408 {
00409 PlexLedId ledid(Detector::kCalDet, 0, led);
00410 cout<<ledid<<endl;
00411 std::pair<PlexPinDiodeId,PlexPinDiodeId> pinids = myGetPinDiodeIds(fPlex,ledid);
00412 cout<<pinids.first<<" "<<pinids.second<<endl;
00413 PlexPinDiodeId pinid;
00414 if (gain2==1) {
00415 pinid = pinids.first;
00416 } else {
00417 pinid = pinids.second;
00418 }
00419
00420 const PulserRawGainPin *thepin = fPinRP.GetRowByIndex(pinid.GetEncoded());
00421 cout<<"PIN Id: "<<pinid.GetEncoded()<<endl;
00422 if (!thepin) cout<<"Oh Bugger"<<endl;
00423 x = thepin->GetMean();
00424 ex = thepin->GetError();
00425 }
00426 TGraphErrors *g = new TGraphErrors(num,x,y,ex,ey);
00427 g->SetMarkerStyle(2);
00428 g->SetMarkerColor(2);
00429 g->SetTitle("");
00430 g->Draw(draw_opt);
00431 char name[100];
00432 sprintf(name,"PIN Diode, LED %d gain %d",led,gain);
00433 if (g->GetYaxis()) g->GetYaxis()->SetTitle(name);
00434 sprintf(name,"PIN Diode, LED %d gain %d",led,gain2);
00435 if (g->GetXaxis()) g->GetXaxis()->SetTitle(name);
00436 g->Draw(draw_opt);
00437 return g;
00438
00439
00440
00441 }
00442
00443
00444
00445 std::pair<PlexPinDiodeId,PlexPinDiodeId> Plotter::myGetPinDiodeIds(PlexHandle plex, PlexLedId led)
00446 {
00447 Int_t highpinchads[6]={4274,4146,4402,4786,4914,5042};
00448 Int_t lowpinchads[6]={4242,4114,4370,4626,4882,5010};
00449 PlexPinDiodeId high =
00450 plex.GetPinDiodeId(RawChannelId(Detector::kCalDet,ElecType::kVA,
00451 0,highpinchads[led.GetLedInBox()-1]));
00452
00453 PlexPinDiodeId low =
00454 plex.GetPinDiodeId(RawChannelId(Detector::kCalDet,ElecType::kVA,
00455 0,lowpinchads[led.GetLedInBox()-1]));
00456
00457 return std::make_pair(high,low);
00458 }
00459
00460 PlexLedId Plotter::myGetLedId(PlexHandle plex, PlexStripEndId seid)
00461 {
00462 int led = getLed(plex.GetRawChannelId(seid).GetCrate(),seid.GetStrip(),seid.GetPlane());
00463 return PlexLedId(Detector::kCalDet, 0 , led);
00464
00465 }
00466
00467 int Plotter::getLed(int crate,int strip, int plane)
00468 {
00469 switch (crate)
00470 {
00471 case 0:
00472 if (strip<5) {
00473 if (plane==40) return 2;
00474 if ((plane%4)==0||(plane%4)==1) return 1;
00475 return 2;
00476 } else if (strip<15) {
00477 if (plane==50) return 3;
00478 if (plane==40) return 1;
00479 if ((plane%4)==0||(plane%4)==1) return 2;
00480 return 1;
00481 } else {
00482 if (plane==50) return 1;
00483 return 3;
00484 }
00485 case 1:
00486 if (strip<11) {
00487 if (plane==56||plane==58) return 4;
00488 return 6;
00489 } else if (strip<21) {
00490 if (plane==58) return 6;
00491 if ((plane%4)==0||(plane%4)==1) return 5;
00492 return 4;
00493 } else {
00494 if ((plane%4)==0||(plane%4)==1) return 4;
00495 return 5;
00496 }
00497 default:
00498 return 0;
00499 }
00500
00501 }
00502
00503 void Plotter::Residuals(TGraphErrors *g)
00504 {
00505
00506
00507 fRes = new TCanvas("residuals","Residuals",600,0,600,600);
00508
00509
00510 double *x = g->GetX();
00511 double *y = g->GetY();
00512 int num = g->GetN();
00513
00514
00515 int pos;
00516 for (pos = num-1;;pos--) {
00517 if (y[pos]<8000) break;
00518 }
00519 for (;;pos--) {
00520 if (x[pos]<8000) break;
00521 }
00522 double xmax = x[pos];
00523 TF1 *func = new TF1("func","[0] + [1]*x",0,xmax);
00524 g->Fit("func","R");
00525
00526
00527 double *resx = new double[num];
00528 double *resy = new double[num];
00529
00530 for (int i=0;i<num;i++) {
00531 resx[i] = x[i];
00532 resy[i] = y[i] - func->GetParameter(0) - x[i]*func->GetParameter(1);
00533 if (y[i]>0.1) {
00534 resy[i]/=y[i];
00535 } else {
00536 resy[i] = 0;
00537 }
00538 }
00539
00540 TGraph *gres = new TGraph(num,resx,resy);
00541 gres->SetMarkerStyle(2);
00542 gres->SetMarkerColor(4);
00543 gres->SetMaximum(0.1);
00544 gres->SetMinimum(-0.1);
00545 gres->SetTitle("Residuals");
00546 gres->Draw("AP");
00547 if (g->GetXaxis()) {
00548 gres->GetXaxis()->SetTitle(g->GetXaxis()->GetTitle());
00549 gres->GetYaxis()->SetTitle(g->GetYaxis()->GetTitle());
00550 gres->Draw("AP");
00551 }
00552
00553 delete [] resx;
00554 delete [] resy;
00555 }