00001
00002
00003 #include "StDraw3D.h"
00004 #include "TCanvas.h"
00005 #include "TTRAP.h"
00006 #include "TGeometry.h"
00007 #include "TVolume.h"
00008 #include "TVolumePosition.h"
00009 #include "TRotMatrix.h"
00010 #include "TMath.h"
00011 #include "TPolyMarker3D.h"
00012 #include "TPolyLine3D.h"
00013 #include "TSystem.h"
00014
00015 #include "TColor.h"
00016 #include "TEnv.h"
00017 #include "StCheckQtEnv.h"
00018 #include "TStyle.h"
00019 #include "TVirtualViewer3D.h"
00020 #include <cassert>
00021 #include <cmath>
00022
00023 static Color_t colorDefault = Color_t(-1);
00024 static Style_t styDefault = Style_t(-1);
00025 static Size_t sizDefault = Size_t (-1);
00026
00027 Color_t StDraw3D::fgColorDefault = Color_t(-1);
00028 Style_t StDraw3D::fgStyDefault = Style_t(-1);
00029 Size_t StDraw3D::fgSizDefault = Size_t (-1);
00030 Color_t StDraw3D::fgBkColor = kBlack;
00031
00032
00033 Int_t StDraw3D::fDrawCanvasCounter = -1;
00034
00035 namespace {
00036 const double p2 = TMath::PiOver2();
00037
00038 static inline void ForceAnimate(unsigned int times=0, int msecDelay=0)
00039 {
00040 unsigned int counter = times;
00041 while( (!times || counter) && !gSystem->ProcessEvents()) { --counter; if (msecDelay) gSystem->Sleep(msecDelay);}
00042 }
00043 }
00044
00045
00046 static inline TVirtualViewer3D *InitCoin(TVirtualPad *pad,const char *detectorName)
00047 {
00048 TVirtualViewer3D *viewer = 0;
00049
00050 bool CheckCoin = true;
00051 if (!StCheckQtEnv::SetQtEnv(false)) { CheckCoin = true; }
00052
00053
00054 if (CheckCoin) {
00055
00056 TString backShape = detectorName;
00057 backShape.ReplaceAll(",",".iv,");
00058 backShape+= ".iv";
00059 printf(" Setting the background shape to be %s\n", backShape.Data());
00060 gEnv->SetValue("Gui.InventorShapeDir",":.:StRoot/macros/graphics:$STAR/StRoot/macros/graphics:/afs/rhic.bnl.gov/star/doc/www/comp/2/vis/iv");
00061 if ( (viewer = TVirtualViewer3D::Viewer3D(pad,"oiv") )) {
00062 viewer->SetDrawOption(backShape.Data());
00063
00064
00065 viewer->BeginScene();
00066 viewer->EndScene();
00067 }
00068 }
00069 return viewer;
00070 }
00072
00076
00077 Color_t StDraw3DStyle::Pt2Color(double pt)
00078 {
00079 const Int_t lightness = 50;
00080 const Int_t saturation = 100;
00081 Int_t hue = (pt > 1.5 ) ? 0 : Int_t(256.*(1.-pt/1.5));
00082 Int_t r,g,b;
00083 TColor::HLS2RGB(hue, lightness, saturation, r, g, b);
00084
00085 float factor = 1./sqrt(1.*r*r+1.*g*g+1.*b*b);
00086 return TColor::GetColor(r*factor,g*factor,b*factor);
00087 }
00088
00089
00090
00091
00092 class view_3D {
00093 private:
00094 TObject *fModel;
00095 TString fComment;
00096 TString fObjectInfo;
00097 void makeInfo()
00098 {
00099 fObjectInfo="";
00100 if (fModel) {
00101 fObjectInfo = Form("( %s *)%p ",fModel->ClassName(),fModel);
00102 }
00103 if (!fComment.IsNull()) fObjectInfo += fComment;
00104 }
00105 public:
00106 view_3D(TObject *model = 0, const char *comment="") : fModel(model),fComment(comment)
00107 { makeInfo(); }
00108 ~view_3D(){;}
00109 TObject *model() const { return fModel; }
00110 void setModel(TObject *model) { fModel = model ; makeInfo(); }
00111 void setComment(const char *comment) { fComment = comment; makeInfo(); }
00112 void addComment(const char *comment) { fComment += comment; makeInfo(); }
00113 const TString &info() const { return fObjectInfo; }
00114 };
00115 namespace {
00116
00117
00118
00119
00120 class poly_line_3D : public TPolyLine3D, public view_3D {
00121 public:
00122 poly_line_3D(Int_t n, Float_t *p, Option_t *option="") : TPolyLine3D(n,p),view_3D()
00123 { SetBit(kCanDelete);}
00124 poly_line_3D(Int_t n, Double_t *p, Option_t *option="") : TPolyLine3D(n,p),view_3D()
00125 { SetBit(kCanDelete);}
00126 virtual ~poly_line_3D(){;}
00127 virtual char *GetObjectInfo(Int_t x, Int_t y) const
00128 {
00129 const TString &customInfo = info();
00130 const char *info = 0;
00131 if (customInfo.IsNull())
00132 info = TPolyLine3D::GetObjectInfo(x,y);
00133 else
00134 info = customInfo.Data();
00135 return (char *)info;
00136 }
00137 void Inspect() const {
00138 if ( model() ) model()->Inspect();
00139 else TPolyLine3D::Inspect();
00140 }
00141 };
00142
00143
00144
00145
00146
00147 class poly_marker_3D : public TPolyMarker3D, public view_3D {
00148 public:
00149 poly_marker_3D(Int_t n, Float_t *p, Option_t *option="") : TPolyMarker3D(n,p,1,option),view_3D()
00150 { SetBit(kCanDelete);}
00151 poly_marker_3D(Int_t n, Double_t *p, Option_t *option="") : TPolyMarker3D(n,p,1,option),view_3D()
00152 { SetBit(kCanDelete);}
00153 virtual ~poly_marker_3D(){;}
00154 virtual char *GetObjectInfo(Int_t x, Int_t y) const
00155 {
00156 const TString &customInfo = info();
00157 const char *info = 0;
00158 if (customInfo.IsNull())
00159 info = TPolyMarker3D::GetObjectInfo(x,y);
00160 else
00161 info = customInfo.Data();
00162 return (char *)info;
00163 }
00164 void Inspect() const {
00165 if ( model() ) model()->Inspect();
00166 else TPolyMarker3D::Inspect();
00167 }
00168 };
00169
00170
00171
00172
00173
00174 class volume_view_3D : public TVolume, public view_3D {
00175 public:
00176 volume_view_3D(const Text_t *name, const Text_t *title, TShape *shape, Option_t *option="")
00177 : TVolume(name,title, shape,option),view_3D()
00178 { SetBit(kCanDelete); }
00179 volume_view_3D() : TVolume(),view_3D()
00180 { SetBit(kCanDelete); }
00181 virtual ~volume_view_3D(){
00182 if (fListOfShapes) fListOfShapes->Delete();
00183 ; }
00184 virtual char *GetObjectInfo(Int_t x, Int_t y) const
00185 {
00186 const TString &customInfo = info();
00187 const char *info = 0;
00188 if (customInfo.IsNull())
00189 info = TVolume::GetObjectInfo(x,y);
00190 else
00191 info = customInfo.Data();
00192 return (char *)info;
00193 }
00194 void Inspect() const {
00195 if ( model() ) model()->Inspect();
00196 else TVolume::Inspect();
00197 }
00198 };
00199 }
00200
00202
00246
00247 StDraw3D::StDraw3D(const char *detectorName,TVirtualPad *pad): fPad(pad),fBkColor(fgBkColor),fViewer(0),fView(0)
00248 , fDetectorName(detectorName),fMaster(0),fTopVolume(0),fWantPad(0),fOwnViewer(kTRUE),fOwnPad(pad?kFALSE:kTRUE)
00249 {
00250
00251
00252
00253
00254 Init();
00255 }
00256
00257 void StDraw3D::Init()
00258 {
00259 static const Style_t UHitSty = 4; static const Size_t UHitSiz = 0.0; static const Color_t UHitCol=kBlue;
00260 static const Style_t NHitSty = 2; static const Size_t NHitSiz = 0.0; static const Color_t NHitCol=kGreen;
00261 static const Style_t TrakSty = 1; static const Size_t TrakSiz = 2.00; static const Color_t TrakCol=kRed;
00262 static const Style_t VertSty = 5; static const Size_t VertSiz = 3.50; static const Color_t VertCol=kYellow;
00263 AddStyle(kVtx, VertCol,VertSty,VertSiz);
00264 AddStyle(kPrimaryTrack,TrakCol,TrakSty,TrakSiz);
00265 AddStyle(kGlobalTrack, TrakCol,TrakSty,TrakSiz);
00266 AddStyle(kTrackBegin, VertCol,VertSty,VertSiz);
00267 AddStyle(kTrackEnd, VertCol,VertSty,VertSiz);
00268 AddStyle(kUsedHit, UHitCol,UHitSty,UHitSiz);
00269 AddStyle(kUnusedHit, NHitCol,NHitSty,NHitSiz);
00270 }
00271
00273
00275 StDraw3D::StDraw3D(TVirtualViewer3D *viewer,TVirtualPad *pad): fPad(pad),fBkColor(fgBkColor),fViewer(viewer),fView(0)
00276 , fDetectorName(),fMaster(),fTopVolume(),fWantPad(0),fOwnViewer(kFALSE),fOwnPad(kFALSE)
00277 {
00278 Init();
00279 }
00280
00281
00282 TVirtualPad *StDraw3D::InitPad()
00283 {
00284 if (fMaster) fMaster->InitPad();
00285 else if (!fPad && !fWantPad ) {
00286 fDrawCanvasCounter++;
00287 TString canvasName = "STAR";
00288 TString canvasTitle;
00289 if (fDrawCanvasCounter) {
00290 canvasName+="_";
00291 canvasName += fDrawCanvasCounter;
00292 canvasTitle += fDrawCanvasCounter;
00293 canvasTitle += " : ";
00294 }
00295 canvasTitle += "STAR Event Viewer";
00296 fPad = new TCanvas(canvasName.Data(),canvasTitle.Data(), 400,400);
00297 fPad->SetFillColor(fBkColor);
00298 fPad->Modified();
00299 fPad->Update();
00300 fPad->GetCanvas()->GetCanvasImp()->Iconify();
00301 }
00302 return Pad();
00303 }
00304
00305 void StDraw3D::InitViewer()
00306 {
00307
00308
00309 if (fMaster) fMaster->InitViewer();
00310 else if ( !fViewer ) fViewer = InitCoin(fPad,fDetectorName);
00311 assert(Viewer());
00312 }
00313
00314 StDraw3D::~StDraw3D()
00315 {
00316 if (fPad) {
00317 if (!fMaster) fPad->Clear();
00318 if (fOwnPad) delete fPad;
00319 fPad = 0;
00320 fMaster = 0;
00321 fTopVolume = 0;
00322 fViewer = 0;
00323 }
00324 }
00325
00326
00327
00329
00330 const TString &StDraw3D::DetectorNames() const
00331 {
00332
00333 return fDetectorName;
00334 }
00335
00336
00339
00340 TVirtualPad *StDraw3D::Pad() const
00341 {
00342 return fMaster ? fMaster->Pad() : fPad;
00343 }
00344
00345
00347
00348 TVirtualViewer3D *StDraw3D::Viewer() const
00349 {
00350 return fMaster ? fMaster->Viewer() : fViewer;
00351 }
00352
00353
00355
00360
00361 void StDraw3D::SetDetectors(const char*nameDetectors)
00362 {
00363
00364
00365
00366 if (fViewer) {
00367 Warning("StDraw3D::SetDetectors","Can not change the detector names. It is too late. The viewer had been created");
00368 } else {
00369 fDetectorName = nameDetectors;
00370 }
00371 }
00372
00373
00375
00380
00381 void StDraw3D::AddDetectors(const char*nameDetectors)
00382 {
00383
00384
00385
00386
00387 if (fViewer) {
00388 Warning("StDraw3D::AddDetectors","Can not the change detector names. It is too late. The viewer had been created");
00389 } else if (nameDetectors && nameDetectors[0]){
00390 fDetectorName += ",";
00391 fDetectorName += nameDetectors;
00392 }
00393 }
00394
00396
00397 void StDraw3D::Clear(Option_t *opt)
00398 {
00399
00400 TVirtualPad *pad = Pad();
00401 if (pad) {
00402 pad->Clear();
00403 fTopVolume = 0;
00404 if ( !strcmp(opt,"update") ) Update();
00405 } else if ( TVirtualViewer3D *viewer = Viewer() ) {
00406 viewer->Clear();
00407 }
00408 if (gGeometry) {
00409 gGeometry->GetListOfMatrices()->Clear();
00410 gGeometry->GetListOfShapes()->Delete();
00411 }
00412 TCollection::EmptyGarbageCollection();
00413 }
00414
00415
00416 TObject *StDraw3D::Draw(TObject *o,const char *option)
00417 {
00418
00419
00420 if (o) {
00421 TVirtualPad *sav = gPad;
00422 if (!Pad()) InitPad();
00423 TVirtualPad *thisPad = Pad();
00424 if (thisPad) {
00425 if (thisPad != sav) thisPad->cd();
00426 assert (fPad==gPad);
00427 o->Draw(option);
00428 }
00429 if (thisPad && sav && (thisPad != sav)) sav->cd();
00430 if (!Viewer()) InitViewer();
00431 if (!thisPad) {
00432
00433
00434 Viewer()->ObjectPaint(o,option);
00435 }
00436 }
00437 return o;
00438 }
00439
00440
00442
00445
00446 void StDraw3D::SetBkColor(Color_t newBkColor)
00447 {
00448
00449 fBkColor = newBkColor;
00450 TVirtualPad *pad = Pad();
00451 if (pad && pad->GetFillColor() != fBkColor)
00452 pad->SetFillColor(fBkColor);
00453 }
00454
00455
00456
00458
00467
00468 const StDraw3DStyle &StDraw3D::AddStyle(EDraw3DStyle type,Color_t col,Style_t sty,Size_t siz)
00469 {
00470 fStyles.insert(std::pair<EDraw3DStyle,StDraw3DStyle>(type,StDraw3DStyle(type,col,sty,siz)));
00471 return Style(type);
00472 }
00473
00474
00475
00477
00480
00481 const StDraw3DStyle &StDraw3D::Style(EDraw3DStyle type) const
00482 {
00483 return fStyles.find(type)->second;
00484 }
00485
00486
00487 StDraw3DStyle &StDraw3D::Style(EDraw3DStyle type)
00488 {
00489 return fStyles[type];
00490 }
00491
00492
00494
00502
00503 TObject *StDraw3D::Points(int n, const float *xyz, Color_t col,Style_t sty,Size_t siz)
00504 {
00505
00506
00507
00508
00509
00510
00511 poly_marker_3D *plMk = new poly_marker_3D(n,(Float_t*)xyz);
00512 if (col != colorDefault) plMk->SetMarkerColor(col);
00513 if (sty != styDefault) plMk->SetMarkerStyle(sty);
00514 if (siz != sizDefault) plMk->SetMarkerSize(siz);
00515 fView = plMk;
00516 return Draw(plMk);
00517 }
00518
00519
00521
00529
00530 TObject *StDraw3D::Points(int n, const double *xyz, Color_t col,Style_t sty,Size_t siz)
00531 {
00532
00533
00534
00535
00536
00537
00538 poly_marker_3D *plMk = new poly_marker_3D(n,(Double_t*)xyz);
00539 if (col != colorDefault) plMk->SetMarkerColor(col);
00540 if (sty != styDefault) plMk->SetMarkerStyle(sty);
00541 if (siz != sizDefault) plMk->SetMarkerSize(siz);
00542 fView = plMk;
00543 return Draw(plMk);
00544 }
00545
00546
00548
00555
00556 TObject *StDraw3D::Points(const std::vector<float> &xyz, Color_t col,Style_t sty,Size_t siz)
00557 {
00558
00559
00560
00561
00562
00563 return Points(xyz.size()/3,&xyz[0],col,sty,siz);
00564 }
00565
00566
00568
00575
00576 TObject *StDraw3D::Points(const std::vector<double> &xyz, Color_t col,Style_t sty,Size_t siz)
00577 {
00578
00579
00580
00581
00582
00583 return Points(xyz.size()/3,&xyz[0],col,sty,siz);
00584 }
00585
00586
00588
00594
00595 TObject *StDraw3D::Points(int n, const float *xyz, EDraw3DStyle sty)
00596 {
00597
00598
00599
00600
00601
00602
00603
00604
00605 const StDraw3DStyle &style = Style(sty);
00606 return Points(n, xyz, style.Col(),style.Sty(),style.Siz());
00607 }
00608
00609
00611
00617
00618 TObject *StDraw3D::Points(int n, const double *xyz, EDraw3DStyle sty)
00619 {
00620
00621
00622
00623
00624
00625
00626
00627
00628 const StDraw3DStyle &style = Style(sty);
00629 return Points(n, xyz, style.Col(),style.Sty(),style.Siz());
00630 }
00631
00632
00634
00639
00640 TObject *StDraw3D::Points(const std::vector<float> &xyz, EDraw3DStyle sty)
00641 {
00642
00643
00644
00645
00646
00647
00648
00649
00650 const StDraw3DStyle &style = Style(sty);
00651 return Points(xyz, style.Col(),style.Sty(),style.Siz());
00652 }
00653
00654
00656
00661
00662 TObject *StDraw3D::Points(const std::vector<double> &xyz, EDraw3DStyle sty)
00663 {
00664
00665
00666
00667
00668
00669
00670
00671
00672 const StDraw3DStyle &style = Style(sty);
00673 return Points(xyz, style.Col(),style.Sty(),style.Siz());
00674 }
00675
00676
00678
00684
00685 TObject *StDraw3D::Draw3D(int n, const float *xyz)
00686 {
00687
00688
00689
00690
00691
00692
00693
00694 return Points(n,xyz,kVtx);
00695 }
00696
00697
00699
00705
00706 TObject *StDraw3D::Draw3D(int n, const double *xyz)
00707 {
00708
00709
00710
00711
00712
00713
00714
00715 return Points(n,xyz,kVtx);
00716 }
00717
00718 TObject *StDraw3D::Point(float x, float y, float z, Color_t col,Style_t sty,Size_t siz)
00719 {
00720
00721
00722
00723
00724
00725 float xyz[]={x,y,z};
00726 return Points(1,xyz,col,sty,siz);
00727 }
00728
00729
00730 TObject *StDraw3D::Point(float x, float y, float z, EDraw3DStyle sty)
00731 {
00732
00733
00734
00735
00736
00737
00738
00739 const StDraw3DStyle &style = Style(sty);
00740 return Point(x,y, z, style.Col(),style.Sty(),style.Siz());
00741 }
00742
00743
00745
00753
00754 TObject *StDraw3D::Line(int n, const float *xyz, Color_t col,Style_t sty,Size_t siz)
00755 {
00756
00757
00758
00759
00760
00761 poly_line_3D *plLine = new poly_line_3D(n,(Float_t*)xyz);
00762 if (col != colorDefault) plLine->SetLineColor(col);
00763 if (sty != styDefault) plLine->SetLineStyle(sty);
00764 if (siz != sizDefault) plLine->SetLineWidth(Width_t(siz));
00765 fView = plLine;
00766 return Draw(plLine);
00767 }
00768
00769
00771
00779
00780 TObject *StDraw3D::Line(float x0, float y0, float z0, float x1, float y1, float z1, Color_t col,Style_t sty,Size_t siz)
00781 {
00782
00783
00784
00785
00786
00787 std::vector<float> line(6);
00788 int i = 0;
00789 line[i++]=x0;line[i++]=y0;line[i++]=z0;
00790 line[i++]=x1;line[i++]=y1;line[i++]=z1;
00791 return Line(line,col,sty,siz);
00792 }
00793
00794
00795
00797
00805
00806 TObject *StDraw3D::Line(int n, const double *xyz, Color_t col,Style_t sty,Size_t siz)
00807 {
00808
00809
00810
00811
00812
00813 poly_line_3D *plLine = new poly_line_3D(n,(Float_t*)xyz);
00814 if (col != colorDefault) plLine->SetLineColor(col);
00815 if (sty != styDefault) plLine->SetLineStyle(sty);
00816 if (siz != sizDefault) plLine->SetLineWidth(Width_t(siz));
00817 fView = plLine;
00818 return Draw(plLine);
00819 }
00820
00822
00829
00830 TObject *StDraw3D::Line(const std::vector<float> &xyz, Color_t col,Style_t sty,Size_t siz)
00831 {
00832
00833
00834
00835
00836
00837 return Line(xyz.size()/3, &xyz[0], col,sty,siz);
00838 }
00839
00840
00842
00849
00850 TObject *StDraw3D::Line(const std::vector<double> &xyz, Color_t col,Style_t sty,Size_t siz)
00851 {
00852
00853
00854
00855
00856
00857 return Line(xyz.size()/3, &xyz[0], col,sty,siz);
00858 }
00859
00860
00862
00867
00868 TObject *StDraw3D::Line(const std::vector<float> &xyz, EDraw3DStyle sty)
00869 {
00870
00871
00872
00873
00874
00875
00876
00877 const StDraw3DStyle &style = Style(sty);
00878 return Line(xyz, style.Col(),style.Sty(),style.Siz() );
00879 }
00880
00881
00883
00888
00889 TObject *StDraw3D::Line(const std::vector<double> &xyz, EDraw3DStyle sty)
00890 {
00891
00892
00893
00894
00895
00896
00897
00898 const StDraw3DStyle &style = Style(sty);
00899 return Line(xyz, style.Col(),style.Sty(),style.Siz() );
00900 }
00901
00902
00904
00910
00911 TObject *StDraw3D::Line(int n, const float *xyz,EDraw3DStyle sty)
00912 {
00913
00914
00915
00916
00917
00918
00919
00920 const StDraw3DStyle &style = Style(sty);
00921 return Line(n,xyz, style.Col(),style.Sty(),style.Siz() );
00922 }
00923
00924
00926
00932
00933 TObject *StDraw3D::Line(int n, const double *xyz,EDraw3DStyle sty)
00934 {
00935
00936
00937
00938
00939
00940
00941
00942 const StDraw3DStyle &style = Style(sty);
00943 return Line(n,xyz, style.Col(),style.Sty(),style.Siz() );
00944 }
00945
00946
00947 void StDraw3D::Joint(StDraw3D *dsp)
00948 {
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958 if (dsp) dsp->SetMaster(this);
00959 }
00960
00961 void StDraw3D::Redraw()
00962 {
00963
00964 if (fMaster && fPad) {
00965 TList *p = fPad->GetListOfPrimitives();
00966 if (p) {
00967 TObject *o = 0;
00968 TIter next(p);
00969 while ( (o=next()) ) Draw(o);
00970 p->Clear();
00971 }
00972 }
00973 }
00974
00975 void StDraw3D::SetMaster(StDraw3D *master)
00976 {
00977
00978 if (fMaster != master) {
00979 if (fMaster)
00980 Error("StDraw3D::SetMaster"
00981 ,"The object (StDraw3D*)%p already has another master %p", this, fMaster);
00982 fMaster = master;
00983 Redraw();
00984 }
00985 }
00986
00987
00988 void StDraw3D::SetModel(TObject *model)
00989 {
00990
00991 if (fView) fView->setModel(model);
00992 }
00993
00994
00995 void StDraw3D::SetComment(const char *cmnt)
00996 {
00997
00998 if (fView) fView->setComment(cmnt);
00999 }
01000
01001
01002 void StDraw3D::AddComment(const char *cmnt)
01003 {
01004
01005 if (fView) fView->addComment(cmnt);
01006 }
01007
01008
01010
01030
01031 void StDraw3D::Print(const char *filename) const
01032 {
01033 Save(filename,"wrl");
01034 }
01035
01037
01043
01044 void StDraw3D::Print(const char *filename, const char*type) const
01045 {
01046 Save(filename,type);
01047 }
01048
01049
01051
01057
01058 void StDraw3D::Save(const char *filename, const char*type) const
01059 {
01060 if ( TVirtualViewer3D *viewer = Viewer() ) {
01061 viewer->Print(filename);
01062
01063 }
01064 else if (Pad()) Pad()->Print(filename,type);
01065 }
01066
01067
01068
01070
01095
01096 void StDraw3D::SetDrawOption(Option_t *options)
01097 {
01098 if ( TVirtualViewer3D *viewer = Viewer() )
01099 viewer->SetDrawOption(options);
01100 }
01101
01102
01104
01107
01108 void StDraw3D::Update(bool asap)
01109 {
01110 TVirtualPad *pad = Pad();
01111 if (pad) {
01112 TVirtualPad *sav = gPad;
01113 if (pad != sav) pad->cd();
01114 assert (pad==gPad);
01115 pad->Update();
01116 if (sav && (pad != sav)) sav->cd();
01117 } else {
01118 UpdateViewer(0);
01119 }
01120 if (asap) ForceAnimate(1);
01121 }
01122
01123
01124 void StDraw3D::Modified()
01125 {
01126
01127
01128 TVirtualPad *pad = Pad();
01129 if (pad) {
01130 TVirtualPad *sav = gPad;
01131 if (pad != sav) pad->cd();
01132 assert (pad==gPad);
01133 pad->Modified();
01134 if (sav && (pad != sav)) sav->cd();
01135 }
01136 }
01137
01138
01139 void StDraw3D::UpdateModified()
01140 {
01141
01142
01143 TVirtualPad *pad = Pad();
01144 if (pad) {
01145 TVirtualPad *sav = gPad;
01146 if (pad != sav) pad->cd();
01147 assert (pad==gPad);
01148 pad->Modified();
01149 pad->Update();
01150 if (sav && (pad != sav)) sav->cd();
01151 }
01152 }
01153
01154
01155 void StDraw3D::UpdateViewer(TVirtualPad *pad)
01156 {
01157 TVirtualViewer3D *viewer = Viewer();
01158 if (viewer) {
01159 if (fTopVolume) Draw(fTopVolume,"same");
01160 viewer->PadPaint(pad);
01161 }
01162 }
01163
01164
01166
01176
01177 void StDraw3D::Draw3DTest(){
01178
01179
01180
01181
01182
01183
01184
01185 float xyz[] = { 189.195, 27.951, 123.966
01186 ,187.195, 28.6187, 122.89
01187 ,181.195 ,30.6788 ,119.556
01188 ,179.195 ,31.3387 ,118.454
01189 ,177.195 ,32.0065 ,117.328
01190 ,175.195 ,32.6132 ,116.26
01191 ,173.195 ,33.2385 ,115.146
01192 ,171.195 ,33.8552 ,114.016
01193 ,169.195 ,34.3924 ,112.964
01194 };
01195
01196 int sizeXYZ = sizeof(xyz)/sizeof(float)/3;
01197
01198 Draw3D(sizeXYZ,xyz);
01199 SetComment("The hits from the TPC sector");
01200
01201 Line(sizeXYZ,xyz,kGlobalTrack);
01202 SetComment("The recontstructed track");
01203
01204 Tower(192,0.365*p2,TMath::Pi()/22
01205 ,TMath::Pi()/65,TMath::Pi()/80
01206 ,kYellow, kBarrelStyle+4050, 250);
01207 SetComment(Form("The EMC tower lambda=%f phi=%f energy=%f",0.365*p2,TMath::Pi()/22,250.0));
01208 Tower(192,-0.365*p2,-TMath::Pi()/22-TMath::Pi()/4
01209 ,TMath::Pi()/65,TMath::Pi()/80
01210 ,kBlue, kBarrelStyle+4050, 50);
01211 Tower(230,-0.365*p2,-TMath::Pi()/22-3*TMath::Pi()/4
01212 ,TMath::Pi()/65,TMath::Pi()/80
01213 ,kCyan, 4050, 50);
01214 SetComment(Form("The EndCup tower lambda=%f phi=%f energy=%f",-0.365*p2,-TMath::Pi()/22,50.0));
01215 Tower(230,0.365*p2,-TMath::Pi()/22+p2
01216 ,TMath::Pi()/65,TMath::Pi()/80
01217 ,kGreen, 0, 150);
01218
01219 int sector=20;
01220 double stepEta = (1.0-0.0)/sector;
01221 StarRoot::StEta eta(1-stepEta,stepEta);
01222 StarRoot::StEta eta2(1,stepEta);
01223 StarRoot::StEta eta3(-1,stepEta);
01224 StarRoot::StEta eta4(-1+stepEta,stepEta);
01225 float phi = 0;
01226 int n = sector;
01227 for (int i=0;i<n;i++) {
01228 Tower(193,eta,phi,TMath::Pi()/120,(i+1)%8,kBarrelStyle,5*i+25);
01229 SetComment(Form("The East EMC tower pseudorapidity=%f phi=%f energy=%f",eta.Eta(),phi,5*i+25.));
01230
01231 Tower(193,eta4,phi+0.2,TMath::Pi()/120,(i+1)%8,kBarrelStyle,5*i+25);
01232 SetComment(Form("The West EMC tower pseudorapidity=%f phi=%f energy=%f",eta4.Eta(),phi+0.2,5*i+25.));
01233
01234 Tower(230,eta2,phi+0.1,TMath::Pi()/60,(i+2)%8,4060,10*i+40);
01235 SetComment(Form("The East Endcap tower pseudorapidity=%f phi=%f energy=%f",eta2.Eta(),phi,10*i+40.));
01236
01237 Tower(230,eta3,phi+0.1,TMath::Pi()/60,(i+2)%8,4060,10*i+40);
01238 SetComment(Form("The West Endcap tower pseudorapidity=%f phi=%f energy=%f",eta2.Eta(),phi,10*i+40.));
01239
01240 eta -=stepEta;
01241 eta2 +=stepEta;
01242 eta3 -=stepEta;
01243 eta4 +=stepEta;
01244 phi += 4*TMath::Pi()/n;
01245 }
01246 if (!Pad()) Update();
01247 }
01248
01249
01250 void StDraw3D::ShowDetectorTest(const char *detectorName)
01251 {
01252
01253 StDraw3D *viewer = new StDraw3D(detectorName);
01254 if (!viewer->Viewer()) viewer->InitViewer();
01255 }
01256
01257 void StDraw3D::ShowTest()
01258 {
01259
01260
01261
01262
01263
01264
01265
01266
01267
01268
01269
01270 static StDraw3D *fine[2]={0};
01271 if (!fine[0]) {
01272 fine[0] = new StDraw3D;
01273 fine[1] = new StDraw3D(0);
01274 } else {
01275 fine[0]->Clear();
01276 fine[1]->Clear();
01277 }
01278
01279 float NodX[100][3]= {{189.195, 27.951, 123.966}
01280 ,{187.195, 28.6187, 122.89 }
01281 ,{181.195 ,30.6788 ,119.556}
01282 ,{179.195 ,31.3387 ,118.454}
01283 ,{177.195 ,32.0065 ,117.328}
01284 ,{175.195 ,32.6132 ,116.26 }
01285 ,{173.195 ,33.2385 ,115.146}
01286 ,{171.195 ,33.8552 ,114.016}
01287 ,{169.195 ,34.3924 ,112.964}};
01288
01289 float HitX[100][3]= {{189.195, 27.951, 123.966}
01290 ,{187.195, 28.6187, 122.89 }
01291 ,{181.195 ,30.6788 ,119.556}
01292 ,{179.195 ,31.3387 ,118.454}
01293 ,{177.195 ,32.0065 ,117.328}
01294 ,{175.195 ,32.6132 ,116.26 }
01295 ,{173.195 ,33.2385 ,115.146}
01296 ,{171.195 ,33.8552 ,114.016}
01297 ,{169.195 ,34.3924 ,112.964}};
01298
01299 float NodL[100][3]= {{189.195+5, 27.951+10, 123.966-50}
01300 ,{187.195+5, 28.6187+10, 122.89-50 }
01301 ,{181.195+5 ,30.6788+10 ,119.556-50}
01302 ,{179.195+5 ,31.3387+10 ,118.454-50}
01303 ,{177.195+5 ,32.0065+10 ,117.328-50}
01304 ,{175.195+5 ,32.6132+10 ,116.26-50 }
01305 ,{173.195+5 ,33.2385+10 ,115.146-50}
01306 ,{171.195+5 ,33.8552+10 ,114.016-50}};
01307 float HitL[100][3]= {{189.195+5, 27.951+10, 123.966-50}
01308 ,{187.195+5, 28.6187+10, 122.89-50 }
01309 ,{181.195+5 ,30.6788+10 ,119.556-50}
01310 ,{179.195+5 ,31.3387+10 ,118.454-50}
01311 ,{177.195+5 ,32.0065+10 ,117.328-50}
01312 ,{175.195+5 ,32.6132+10 ,116.26-50 }
01313 ,{173.195+5 ,33.2385+10 ,115.146-50}
01314 ,{171.195+5 ,33.8552+10 ,114.016-50}};
01315 int nN=9,nH=9;
01316
01317 fine[0]->Points(nH, HitX[0], kVtx);
01318 fine[0]->SetComment("Hits and Geometry");
01319
01320 fine[0]->Line (nN, NodX[0], kGlobalTrack);
01321 fine[0]->SetComment("Track and Geometry");
01322
01323 fine[1]->Points(nH, HitL[0], kVtx);
01324 fine[1]->SetComment("Hits no Geometry");
01325
01326 fine[1]->Line (nN, NodL[0], kGlobalTrack);
01327 fine[1]->SetComment("Track no Geometry");
01328 for (int i=0;i<2;i++) { fine[i]->UpdateModified(); }
01329
01330 }
01331
01333
01372
01373 TObject *StDraw3D::Tower(float radius
01374 , float lambda, float phi
01375 , float dlambda, float dphi
01376 , Color_t col,Style_t sty, Size_t siz)
01377 {
01378 float d2 = dlambda/2;
01379 return Tower(radius, lambda,lambda-d2, lambda+d2,phi, dphi, col, sty, siz);
01380 }
01382
01423
01424 TObject *StDraw3D::Tower(float radius, float lambda, float lambda1, float lambda2, float phi,float dphi, Color_t col,Style_t sty, Size_t siz)
01425 {
01426 if (gGeometry) {
01427 gGeometry->GetListOfMatrices()->Clear();
01428 }
01429 if (lambda2-lambda1 < 0 ) {
01430 Warning("StDraw3D::Tower", "The illegal negative value for dlambda = %f", lambda2-lambda1);
01431 float swp = lambda1;
01432 lambda1 = lambda2;
01433 lambda2 = swp;
01434 }
01435 if (dphi < 0 ) {
01436 Warning("StDraw3D::Tower", "The illegal negative value for dphi = %f", dphi);
01437 dphi = -dphi;
01438 }
01439 float zNear, xNear, x1Near,x2Near, yNear, y1Near,y2Near, zFar, xFar, yFar, x1Far,x2Far, y1Far, y2Far;
01440
01441 siz = siz *TMath::Cos(lambda);
01442
01443 bool barrel = (sty >= kBarrelStyle);
01444 if (barrel) {
01445 lambda = -lambda;
01446
01447
01448 }
01449
01450 zNear = radius;
01451
01452 yNear = zNear*TMath::Tan(lambda);
01453 y1Near = zNear*TMath::Tan(lambda1);
01454 y2Near = zNear*TMath::Tan(lambda2);
01455
01456 xNear = 0;
01457 x1Near = TMath::Sqrt(y1Near*y1Near + zNear*zNear) * TMath::Tan(dphi/2);
01458 x2Near = TMath::Sqrt(y2Near*y2Near + zNear*zNear) * TMath::Tan(dphi/2);
01459
01460 zFar = radius+siz;
01461
01462 yFar = zFar*TMath::Tan(lambda);
01463 y1Far = zFar*TMath::Tan(lambda1);
01464 y2Far = zFar*TMath::Tan(lambda2);
01465
01466 xFar = 0;
01467 x1Far = TMath::Sqrt(y1Far*y1Far + zFar*zFar) * TMath::Tan(dphi/2);
01468 x2Far = TMath::Sqrt(y2Far*y2Far + zFar*zFar) * TMath::Tan(dphi/2);
01469
01470 float dy = TMath::Tan(lambda )*siz/2;
01471
01472
01473
01474
01475
01476 TTRAP *trap = new TTRAP( "CALO", Form("Angle%d",lambda)
01477 , "Barrel"
01478 , siz/2
01479 , lambda*TMath::RadToDeg()
01480 , 90
01481 , (y2Near-y1Near )/2
01482 , x1Near
01483 , x2Near
01484 , 0
01485 , (y2Far-y1Far )/2
01486 , x1Far
01487 , x2Far
01488 , 0
01489 );
01490 if (gGeometry) gGeometry->GetListOfShapes()->Remove(trap);
01491 bool draw = false;
01492 if (!fTopVolume) {
01493 draw = true;
01494 fTopVolume = new volume_view_3D();
01495 }
01496 volume_view_3D *thisShape = new volume_view_3D(Form("Lamda=%f : Phi=%f; ",lambda,phi),"tower",trap);
01497
01498 static double rotmatrixZ[] = { 1, 0, 0
01499 , 0, 0, -1
01500 , 0, 1, 0
01501 };
01502 static double rotmatrixZNeg[] = { 1, 0, 0
01503 , 0, -1, 0
01504 , 0, 0, -1
01505 };
01506 float a = -phi+p2;
01507 double rotmatrixX[] = { cos(a), -sin(a), 0
01508 , sin(a), cos(a), 0
01509 , 0, 0, 1
01510 };
01511 TRotMatrix *rootMatrixX = new TRotMatrix("rotx","rotx",rotmatrixX);
01512 if (gGeometry) gGeometry->GetListOfMatrices()->Remove(rootMatrixX);
01513 TVolumePosition *position = fTopVolume->Add(thisShape,0,0,0,rootMatrixX);
01514 position->SetMatrixOwner();
01515 if (barrel) {
01516 TRotMatrix *rootMatrixZ = new TRotMatrix("rotz","rotZ",rotmatrixZ);
01517 if (gGeometry) gGeometry->GetListOfMatrices()->Remove(rootMatrixZ);
01518 TVolumePosition zpos(thisShape,0,0,0,rootMatrixZ);
01519 zpos.SetMatrixOwner();
01520 position->Mult(zpos);
01521 } else if (lambda < 0) {
01522 TRotMatrix *rootMatrixZ = new TRotMatrix("rotz","rotZ",rotmatrixZNeg);
01523 if (gGeometry) gGeometry->GetListOfMatrices()->Remove(rootMatrixZ);
01524 TVolumePosition zpos(thisShape,0,0,0,rootMatrixZ);
01525 zpos.SetMatrixOwner();
01526 position->Mult(zpos);
01527 }
01528 TVolumePosition rpos(thisShape,0,yNear+dy,zNear + siz/2);
01529 position->Mult(rpos);
01530 thisShape->SetFillColor(col);
01531 thisShape->SetLineColor(col);
01532 thisShape->SetFillStyle(barrel ? sty-kBarrelStyle : sty );
01533 if ( draw ) {
01534 Draw(fTopVolume,"same");
01535 }
01536 fView = thisShape;
01537 return thisShape;
01538 }
01540
01578
01579 TObject *StDraw3D::Tower( float radius, const StarRoot::StEta &eta
01580 , float phi, float dphi
01581 , Color_t col,Style_t sty, Size_t siz)
01582 {
01583 bool barrel = (sty >= kBarrelStyle);
01584 double lambda,lambda1,lambda2;
01585 lambda = eta;
01586 if ( lambda > p2 ) lambda -= 2*p2;
01587 if (barrel) {
01588 lambda = p2-eta;
01589 lambda2 = p2-eta.dLambda2();
01590 lambda1 = p2-eta.dLambda1();
01591 } else {
01592 lambda2 = eta.dLambda1();
01593 lambda1 = eta.dLambda2();
01594 }
01595 return Tower(radius,lambda,lambda1,lambda2, phi, dphi, col, sty, siz);
01596 }
01598
01601
01602 void StDraw3D::SetFooter(const char *footer)
01603 {
01604 TString viewerFooter = "{footer:";
01605 viewerFooter += footer; viewerFooter += "}";
01606 SetDrawOption(viewerFooter.Data());
01607 }
01608
01609
01611
01622
01623 void StDraw3D::Animate()
01624 {
01625 TVirtualPad *pad = Pad();
01626 if (pad && pad->IsModified()) {
01627 Update();
01628 }
01629 ForceAnimate(0,200);
01630 }
01631
01632
01633 ClassImp(StDraw3D)