TGeoOverlap.cxx

Go to the documentation of this file.
00001 // @(#)root/geom:$Id: TGeoOverlap.cxx 24893 2008-07-18 15:08:05Z brun $
00002 // Author: Andrei Gheata   09-02-03
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TVirtualPad.h"
00013 #include "TMath.h"
00014 #include "TNamed.h"
00015 #include "TBrowser.h"
00016 #include "TGeoManager.h"
00017 #include "TGeoVolume.h"
00018 #include "TGeoNode.h"
00019 #include "TGeoBBox.h"
00020 #include "TRandom3.h"
00021 #include "TPolyMarker3D.h"
00022 #include "TVirtualGeoPainter.h"
00023 
00024 #include "TGeoOverlap.h"
00025 
00026 ClassImp(TGeoOverlap)
00027 
00028 //______________________________________________________________________________
00029 // TGeoOverlap - base class describing geometry overlaps. Overlaps apply
00030 //   to the nodes contained inside a volume. These should not overlap to
00031 //   each other nor extrude the shape of their mother volume.
00032 //______________________________________________________________________________
00033 
00034 //______________________________________________________________________________
00035 TGeoOverlap::TGeoOverlap()
00036 {
00037 // Default ctor.
00038    fOverlap = 0;
00039    fVolume1 = 0;
00040    fVolume2 = 0;
00041    fMatrix1 = 0;
00042    fMatrix2 = 0;
00043    fMarker  = 0;
00044 }
00045 
00046 //______________________________________________________________________________
00047 TGeoOverlap::TGeoOverlap(const char *name, TGeoVolume *vol1, TGeoVolume *vol2,
00048                          const TGeoMatrix *matrix1, const TGeoMatrix *matrix2,
00049                          Bool_t isovlp, Double_t ovlp)
00050             :TNamed("",name)
00051 {
00052 // Creates a named overlap belonging to volume VOL and having the size OVLP.
00053    fOverlap = ovlp;
00054    fVolume1  = vol1;
00055    fVolume2  = vol2;
00056    fMatrix1 = new TGeoHMatrix();
00057    *fMatrix1 = matrix1;
00058    fMatrix2 = new TGeoHMatrix();
00059    *fMatrix2 = matrix2;
00060    fMarker  = new TPolyMarker3D();
00061    fMarker->SetMarkerColor(2);
00062    SetIsOverlap(isovlp);
00063    fMarker->SetMarkerStyle(6);
00064 //   fMarker->SetMarkerSize(0.5);
00065 }
00066 
00067 //______________________________________________________________________________
00068 TGeoOverlap::~TGeoOverlap()
00069 {
00070 // Destructor.
00071    if (fMarker) delete fMarker;
00072    if (fMatrix1) delete fMatrix1;
00073    if (fMatrix2) delete fMatrix2;
00074 }
00075 
00076 //______________________________________________________________________________
00077 void TGeoOverlap::Browse(TBrowser *b)
00078 {
00079 // Define double-click action
00080    if (!b) return;
00081    Draw();
00082 }
00083 
00084 //______________________________________________________________________________
00085 Int_t TGeoOverlap::Compare(const TObject *obj) const
00086 {
00087 // Method to compare this overlap with another. Returns :
00088 //   -1 - this is smaller than OBJ
00089 //    0 - equal
00090 //    1 - greater 
00091    TGeoOverlap *other = 0;
00092    other = (TGeoOverlap*)obj;
00093    if (!other) {
00094       Error("Compare", "other object is not TGeoOverlap");
00095       return 0;
00096    }
00097    if (IsExtrusion()) {
00098       if (other->IsExtrusion()) return (fOverlap<=other->GetOverlap())?1:-1;
00099       return -1;
00100    } else {   
00101       if (other->IsExtrusion()) return 1;
00102       return (fOverlap<=other->GetOverlap())?1:-1;
00103    }
00104 }
00105 
00106 //______________________________________________________________________________
00107 Int_t TGeoOverlap::DistancetoPrimitive(Int_t px, Int_t py)
00108 {
00109 // Distance to primitive for an overlap.
00110    return fVolume1->GetGeoManager()->GetGeomPainter()->DistanceToPrimitiveVol(fVolume1, px, py);
00111 }
00112 
00113 //______________________________________________________________________________
00114 void TGeoOverlap::Draw(Option_t *option)
00115 {
00116 // Draw the overlap. One daughter will be blue, the other green,
00117 // extruding points red.
00118    fVolume1->GetGeoManager()->GetGeomPainter()->DrawOverlap(this, option);
00119    PrintInfo();
00120 }
00121 
00122 //______________________________________________________________________________
00123 void TGeoOverlap::ExecuteEvent(Int_t event, Int_t px, Int_t py)
00124 {
00125 // Event interception.
00126    fVolume1->GetGeoManager()->GetGeomPainter()->ExecuteVolumeEvent(fVolume1, event, px, py);
00127 }
00128 
00129 //______________________________________________________________________________
00130 void TGeoOverlap::Paint(Option_t *option)
00131 {
00132 // Paint the overlap.
00133    fVolume1->GetGeoManager()->GetGeomPainter()->PaintOverlap(this, option);
00134 }
00135 
00136 //______________________________________________________________________________
00137 void TGeoOverlap::Print(Option_t *) const
00138 {
00139 // Print detailed info.
00140    PrintInfo();
00141    printf(" - first volume: %s at position:\n", fVolume1->GetName());
00142    fMatrix1->Print();   
00143    fVolume1->InspectShape();   
00144    printf(" - second volume: %s at position:\n", fVolume2->GetName());
00145    fMatrix2->Print();   
00146    fVolume2->InspectShape();   
00147 }
00148 
00149 //______________________________________________________________________________
00150 void TGeoOverlap::PrintInfo() const
00151 {
00152 // Print some info.
00153    printf(" = Overlap %s: %s ovlp=%g\n", GetName(), GetTitle(),fOverlap);
00154 }
00155 
00156 //______________________________________________________________________________
00157 void TGeoOverlap::SetNextPoint(Double_t x, Double_t y, Double_t z)
00158 {
00159 // Set next overlapping point.
00160    fMarker->SetNextPoint(x,y,z);
00161 }
00162 
00163 //______________________________________________________________________________
00164 void TGeoOverlap::SampleOverlap(Int_t npoints)
00165 {
00166 // Draw overlap and sample with random points the overlapping region.
00167    Draw();
00168    // Select bounding box of the second volume (may extrude first)
00169    TPolyMarker3D *marker = 0;
00170    TGeoBBox *box = (TGeoBBox*)fVolume2->GetShape();
00171    Double_t dx = box->GetDX();
00172    Double_t dy = box->GetDY();
00173    Double_t dz = box->GetDZ();
00174    Double_t pt[3];
00175    Double_t master[3];
00176    const Double_t *orig = box->GetOrigin();
00177    Int_t ipoint = 0;
00178    Int_t itry = 0;
00179    Int_t iovlp = 0;
00180    while (ipoint < npoints) {
00181    // Shoot randomly in the bounding box.
00182       pt[0] = orig[0] - dx + 2.*dx*gRandom->Rndm();
00183       pt[1] = orig[1] - dy + 2.*dy*gRandom->Rndm();
00184       pt[2] = orig[2] - dz + 2.*dz*gRandom->Rndm();
00185       if (!fVolume2->Contains(pt)) {
00186          itry++;
00187          if (itry>10000 && !ipoint) {
00188             Error("SampleOverlap", "No point inside volume!!! - aborting");
00189             break;
00190          }
00191          continue;
00192       }  
00193       ipoint++;          
00194       // Check if the point is inside the first volume
00195       fMatrix2->LocalToMaster(pt, master);
00196       fMatrix1->MasterToLocal(master, pt);
00197       Bool_t in = fVolume1->Contains(pt);
00198       if (IsOverlap() && !in) continue;
00199       if (!IsOverlap() && in) continue;
00200       // The point is in the overlapping region.
00201       iovlp++;
00202       if (!marker) {
00203          marker = new TPolyMarker3D();
00204          marker->SetMarkerColor(kRed);
00205       }   
00206       marker->SetNextPoint(master[0], master[1], master[2]);
00207    }
00208    if (!iovlp) return;
00209    marker->Draw("SAME");
00210    gPad->Modified();
00211    gPad->Update();
00212    Double_t capacity = fVolume1->GetShape()->Capacity();
00213    capacity *= Double_t(iovlp)/Double_t(npoints);
00214    Double_t err = 1./TMath::Sqrt(Double_t(iovlp));
00215    Info("SampleOverlap", "#Overlap %s has %g +/- %g [cm3]",
00216          GetName(), capacity, err*capacity);
00217 }        
00218 
00219 //______________________________________________________________________________
00220 void TGeoOverlap::Sizeof3D() const
00221 {
00222 // Get 3D size of this.
00223    fVolume1->GetShape()->Sizeof3D();
00224    fVolume2->GetShape()->Sizeof3D();
00225 }
00226 
00227 //______________________________________________________________________________
00228 void TGeoOverlap::Validate() const
00229 {
00230 // Validate this overlap.
00231    Double_t point[3];
00232    Double_t local[3];
00233    Double_t safe1,safe2;
00234    Int_t npoints = fMarker->GetN();
00235    for (Int_t i=0; i<npoints; i++) {
00236       fMarker->GetPoint(i, point[0], point[1], point[2]);
00237       if (IsExtrusion()) {
00238          fMatrix1->MasterToLocal(point,local);
00239          safe1 = fVolume1->GetShape()->Safety(local, kFALSE);
00240          printf("point %d: safe1=%f\n", i, safe1);
00241       } else {
00242          fMatrix1->MasterToLocal(point,local);  
00243          safe1 = fVolume1->GetShape()->Safety(local, kTRUE);
00244          fMatrix2->MasterToLocal(point,local);  
00245          safe2 = fVolume2->GetShape()->Safety(local, kTRUE);
00246          printf("point %d: safe1=%f safe2=%f\n", i, safe1,safe2);
00247       }
00248    }
00249 }
00250          
00251          

Generated on Tue Jul 5 14:13:22 2011 for ROOT_528-00b_version by  doxygen 1.5.1