ROOT logo
//____________________________________________________________________
//
//
// $Id$
//
// Script I used for rapid prototyping of the FMD3 geometry - in
// particular the support cone 
//
/** @defgroup geo_geom Simple geometry
    @ingroup FMD_script
*/
#ifndef __CINT___
# ifndef ROOT_TGeoVolume
#  include <TGeoVolume.h>
# endif
# ifndef ROOT_TGeoMaterial
#  include <TGeoMaterial.h>
# endif
# ifndef ROOT_TGeoMedium
#  include <TGeoMedium.h>
# endif
# ifndef ROOT_TGeoMatrix
#  include <TGeoMatrix.h>
# endif
# ifndef ROOT_TGeoXtru
#  include <TGeoXtru.h>
# endif
# ifndef ROOT_TGeoPcon
#  include <TGeoPcon.h>
# endif
# ifndef ROOT_TGeoTube
#  include <TGeoTube.h>
# endif
# ifndef ROOT_TGeoManager
#  include <TGeoManager.h>
# endif
# ifndef ROOT_TGeoPhysicalNode
#  include <TGeoPhysicalNode.h>
# endif
# ifndef ROOT_TVector3
#  include <TVector3.h>
# endif
# ifndef ROOT_TString
#  include <TString.h>
# endif
# ifndef ROOT_TRandom
#  include <TRandom.h>
# endif
# ifndef ROOT_TBrowser
#  include <TBrowser.h>
# endif
# ifndef ROOT_TCanvas
#  include <TCanvas.h>
# endif
# ifndef __CSTDARG__
#  include <cstdarg>
# endif
# ifndef __IOSTREAM__
#  include <iostream>
# endif
#endif

//____________________________________________________________________
/** @brief Simple geometry 
    @ingroup geo_geom
    @code 
    gSystem->Load("libPhysics.so");
    gSystem->Load("libGeom.so");
    gROOT->LoadMacro("GeoGeometry.C+");
    Geometry g;
    g.Exec();
    @endcode 
 */
class Geometry 
{
public:
  /** Constructor */
  Geometry();
  /** Destructor */
  virtual ~Geometry() {}
  /** Initialize  */
  virtual void Initialize();
  /** Register  */
  virtual void Register();
  /** @e Do-It member function  */
  virtual void Exec();
  /** Try to align */
  virtual void Align();
  /** Convert detector coordinates to spatial coordinates 
      @param sector Sector number 
      @param strip  Strip number
      @param xyz    Spatial coordinates
  */
  virtual void Detector2XYZ(UInt_t sector, UInt_t strip, TVector3& xyz);
  /** Debug level */
  static Int_t fgDebug;
  /** Print debugging messages 
      @param lvl Acceptance level 
      @param where Where it happened
      @param format Message format. */
  static void Debug(Int_t lvl, const char* where, const char* format, ...) 
  {
    if (lvl > fgDebug) return;
    static char buf[2048];
    va_list ap;
    va_start(ap, format);
    vsnprintf(buf, 2048, format, ap);
    std::cout << "D: " << where << ": " << buf << std::endl;
    va_end(ap);
  }
protected:
  /** List of translations */
  TObjArray*    fMatricies;
  /** Shape parameter */
  TVector2    	fA;
  /** Shape parameter */
  TVector2    	fB;
  /** Shape parameter */
  TVector2    	fC;
  /** Spacing between sensor and hybrid */
  Double_t      fSpacer;
  /** Thickness of aluminum plates in honey comb */
  Double_t    	fAlThickness;
  /** Width of bonding pads */
  Double_t      fBondingWidth;
  /** chip layer thickenss */
  Double_t      fChipThickness;
  /** Copper layer thickness */
  Double_t      fCopperThickness;
  /** Upper radious */
  Double_t 	fHighR;
  /** Thickness of honey comb */
  Double_t	fHoneycombThickness;
  /** Inner honey comb inner radius  */
  Double_t	fInnerHoneyHighR;
  /** Inner honey comb outer radius  */
  Double_t	fInnerHoneyLowR;
  /** Z coordinate of inner ring  */
  Double_t	fInnerZ;
  /** Length of support legs */
  Double_t	fLegLength;
  /** Offset from edge of legs */
  Double_t	fLegOffset;
  /** Radius of support legs */
  Double_t	fLegRadius;
  /** Inner radius */
  Double_t	fLowR;
  /** Spacing between modules */
  Double_t	fModuleSpacing;
  /** Number of strps */
  Int_t		fNStrips;
  /** Outer honey comb inner radius */
  Double_t	fOuterHoneyHighR;
  /** Outer honey comb outer radius */
  Double_t	fOuterHoneyLowR;
  /** Z coordinate of outer */
  Double_t	fOuterZ;
  /** Thickness of print board */
  Double_t	fPrintboardThickness;
  /** Cache of ring depth */
  Double_t	fRingDepth;
  /** Thickness of silicon sensor */
  Double_t	fSiThickness;
  /** ??? */
  Double_t	fSpacerHeight;
  /** Opening angle of sensor */
  Double_t	fTheta;
  /** Radius of wafer the sensor is cut out of */
  Double_t	fWaferRadius;
  /** Air tracking medium */
  TGeoMedium* 	fAir;
  /** Aluminum tracking medium */
  TGeoMedium* 	fAl;
  /** Carbon tracking medium */
  TGeoMedium*   fCarbon;
  /** Chip tracking medium */
  TGeoMedium*   fChip;
  /** Copper tracking medium */
  TGeoMedium*   fCopper;
  /** Kapton tracking medium */
  TGeoMedium*   fKapton;
  /** PCB tracking medium */
  TGeoMedium*	fPCB;
  /** Plastic tracking medium */
  TGeoMedium*	fPlastic;
  /** Active silicon tracking medium */
  TGeoMedium*	fSi;
  /** Vacuum  tracking medium */
  TGeoMedium*	fVacuum;
  /** Use assemblies */
  Bool_t        fDoubleAssembly;
  /** Make a detailed geometry */
  void MakeDetailed(TGeoVolume* mother);
};

//____________________________________________________________________
Int_t Geometry::fgDebug = 10;

//____________________________________________________________________
Geometry::Geometry()
  : fMatricies(0),
    fA( 4.3000,   1.3972),
    fB(17.2000,   2.1452),
    fC(15.3327,   4.9819)
{
  fBondingWidth           =   0.5000;
  fWaferRadius            =   6.7000;
  fSiThickness            =   0.0300;
  fLowR                   =   4.3000;
  fHighR                  =  17.2000;
  fTheta                  =  18.0000;
  fNStrips                =      512;
  fRingDepth              =   2.1600;
  fLegRadius              =   0.5000;
  fLegLength              =   1.0000;
  fLegOffset              =   2.0000;
  fModuleSpacing          =   1.0000;
  fPrintboardThickness    =   0.1000;
  fSpacerHeight           =   0.0300;

  fInnerZ                 =  83.4000;
  fOuterZ                 =  75.2000;
  fHoneycombThickness     =   1.0000;
  fAlThickness            =   0.1000;
  fInnerHoneyLowR         =   5.3000;
  fInnerHoneyHighR        =  18.2000;
  fOuterHoneyLowR         =  16.6000;
  fOuterHoneyHighR        =  29.0000;

  fCopperThickness        =   0.01;
  fChipThickness          =   0.01;
  fAlThickness            =   0.10;
  fHoneycombThickness     =   1.00;
  fSpacer                 =   0.10;
  fLegLength              =   1.00;
}

//____________________________________________________________________
void 
Geometry::Exec()
{
  Initialize();
  Register();
  // gGeoManager->DefaultColors();
  gGeoManager->ViewLeaves(kFALSE);
  TCanvas* c = new TCanvas;
  c->SetFillColor(0);
  gGeoManager->GetTopVolume()->Draw();
  new TBrowser("b", "Browser");
}

//____________________________________________________________________
void 
Geometry::Initialize()
{
  Double_t mMax  = 10;
  Double_t mType = 2;
  
  // Air 
  Double_t pAir[]     = { 0., mType, mMax, 1.,  .001, 1., .001, .001 };
  TGeoMixture* fmdAir = new TGeoMixture("FMD air", 4, .00120479);
  fmdAir->DefineElement(0, 12.0107,  6., 0.000124);
  fmdAir->DefineElement(1, 14.0067,  7., 0.755267);
  fmdAir->DefineElement(2, 15.9994,  8., 0.231781);
  fmdAir->DefineElement(3, 39.948,  18., 0.012827);
  fmdAir->SetTransparency('0');
  fAir = new TGeoMedium("FMD Air", 1, fmdAir,  pAir);

  // Silicon 
  Double_t pSi[]      = { 1., mType, mMax, 1.,  .001, 1., .001, .001 };
  TGeoMaterial* fmdSi = new TGeoMaterial("FMD Si", 28.0855, 14, 2.33);
  fmdSi->SetFillColor(2);
  fSi = new TGeoMedium("FMD Si", 1, fmdSi,  pSi);

  // Vacumm 
  Double_t pVacuum[]  = { 0., mType, mMax, 10,  .01, .1, .003, .003 };
  TGeoMaterial* fmdVacuum = new TGeoMaterial("FMD Vacuum",1e-16,1e-16,1e-16);
  fmdVacuum->SetTransparency('0');
  fVacuum = new TGeoMedium("FMD Vacuum", 1, fmdVacuum,pVacuum);


  // PCB 
  Double_t pPCB[]     = { 0., mType, mMax, 1.,  .001, 1., .001, .001 };
  TGeoMixture* fmdPCB = new TGeoMixture("FMD PCB", 14, 1.8);
  fmdPCB->DefineElement(0,  28.0855,   14, 0.15144894);
  fmdPCB->DefineElement(1,  40.078,    20, 0.08147477);
  fmdPCB->DefineElement(2,  26.981538, 13, 0.04128158);
  fmdPCB->DefineElement(3,  24.305,    12, 0.00904554);
  fmdPCB->DefineElement(4,  10.811,     5, 0.01397570);
  fmdPCB->DefineElement(5,  47.867,    22, 0.00287685);
  fmdPCB->DefineElement(6,  22.98977,  11, 0.00445114);
  fmdPCB->DefineElement(7,  39.0983,   19, 0.00498089);
  fmdPCB->DefineElement(8,  55.845,    26, 0.00209828);
  fmdPCB->DefineElement(9,  18.9984,    9, 0.00420000);
  fmdPCB->DefineElement(10, 15.9994,    8, 0.36043788);
  fmdPCB->DefineElement(11, 12.0107,    6, 0.27529425);
  fmdPCB->DefineElement(12, 14.0067,    7, 0.01415852);
  fmdPCB->DefineElement(13,  1.00794,   1, 0.03427566);
  fmdPCB->SetFillColor(7);
  fPCB = new TGeoMedium("FMD PCB", 1, fmdPCB,  pPCB);

  // Chip 
  Double_t pChip[]  = { 0., mType, mMax, 10., .01,  1., .003, .003 };
  TGeoMixture* fmdChip = new TGeoMixture("FMD Chip", 6, 2.36436);
  fmdChip->DefineElement(0,  12.0107,   6, 0.039730642);
  fmdChip->DefineElement(1,  14.0067,   7, 0.001396798);
  fmdChip->DefineElement(2,  15.9994,   8, 0.01169634);
  fmdChip->DefineElement(3,   1.00794,  1, 0.004367771);
  fmdChip->DefineElement(4,  28.0855,  14, 0.844665);
  fmdChip->DefineElement(5, 107.8682,  47, 0.0981434490);
  fmdChip->SetFillColor(4);
  fChip = new TGeoMedium("FMD Chip", 1, fmdChip, pChip);

  // Carbon 
  Double_t pC[]       = { 0., mType, mMax, 10., .01,  1., .003, .003 };
  TGeoMaterial* fmdC = new TGeoMaterial("FMD C", 12.011, 6, 2.265);
  fmdC->SetFillColor(6);
  fCarbon = new TGeoMedium("FMD C", 1, fmdC, pC);
  
  // Kapton (inside of Honeycombs)
  Double_t pKapton[]  = { 0., mType, mMax, 1.,  .001, 1., .001, .001 };
  TGeoMaterial* fmdKapton = new TGeoMaterial("FMD Kapton", 12.011,   6.,0.01);
  fmdKapton->SetFillColor(3);
  fKapton                 = new TGeoMedium("FMD Kapton",1,fmdKapton,pKapton);

  // Plastic 
  Double_t pPlastic[] = { 0., mType, mMax, 10., .01,  1., .003, .003 };
  TGeoMixture* fmdPlastic = new TGeoMixture("FMD Plastic", 2, 1.03);
  fmdPlastic->DefineElement(0,  1.01,   1, .5);
  fmdPlastic->DefineElement(1,  12.01,  6, .5);
  fmdPlastic->SetFillColor(4);
  fPlastic = new TGeoMedium("FMD Plastic", 1, fmdPlastic,  pPlastic);

  // Aluminium 
  Double_t pAl[]  = { 0., mType, mMax, 10., .001,  -1., .003, .003 };
  TGeoMaterial* fmdAl = new TGeoMaterial("FMD Al", 26.981539, 13, 2.7);
  fmdAl->SetFillColor(3);
  fAl = new TGeoMedium("FMD Al", 1, fmdAl, pAl);

  // Copper 
  Double_t pCopper[]  = { 0., mType, mMax, 10., .01,  1., .003, .003 };
  TGeoMaterial* fmdCopper = new TGeoMaterial("FMD Copper", 63.546,  29.,8.96);
  fmdCopper->SetFillColor(3);
  fCopper = new TGeoMedium("FMD Copper",  1, fmdCopper, pCopper);
}

#define DEGRAD TMath::Pi()/180.
//____________________________________________________________________
void 
Geometry::Detector2XYZ(UInt_t sector, UInt_t strip, TVector3& xyz)
{
  UInt_t   mod      = sector / 2;
  if (!fMatricies) {
    Warning("Detector2XYZ", "No matricies");
    return;
  }
  TGeoMatrix* m = static_cast<TGeoMatrix*>(fMatricies->At(mod));
  if (!m) {
    Warning("Detector2XYZ", "No matrix found for module %d", mod);
    return;
  }
  Debug(10, "Detector2XYZ", "Transforming (%2d,%3d)", sector, strip);
  Double_t rmax     = fB.Mod();
  Double_t stripoff = fA.Mod();
  Double_t dstrip   = (rmax - stripoff) / fNStrips;
  Double_t r        = (strip + .5) * dstrip + stripoff; // fLowR
  Double_t theta    = ((sector % 2) - .5) * fTheta;
  Double_t modThick = (fSiThickness 
		       + fPrintboardThickness 
		       + fCopperThickness
		       + fChipThickness 
		       + fSpacer);
  Debug(10,"Detector2XYZ", "Radius %7.3f, angle %7.3f (%f, %f)", r, theta, 
       fLowR, stripoff);
  Double_t local[] = {
    r * TMath::Cos(theta * DEGRAD), 
    r * TMath::Sin(theta * DEGRAD), 
    -modThick + fSiThickness / 2
  };
  Double_t master[3];
  Debug(10, "Detector2XYZ", "Local (%7.3f,%7.3f,%7.3f)", 
       local[0], local[1], local[2]);
  m->LocalToMaster(local, master);
  Debug(10, "Detector2XYZ", "Master (%7.3f,%7.3f,%7.3f)", 
       master[0], master[1], master[2]);
  xyz.SetXYZ(master[0], master[1], master[2]);
}
//____________________________________________________________________
void
Geometry::MakeDetailed(TGeoVolume* caveVolume)
{
  Info("MakeSimple", "Using a detailed geometry");
  Double_t xv[6] = { fA.X(), fC.X(), fB.X(),  fB.X(),  fC.X(),  fA.X() };
  Double_t yv[6] = { fA.Y(), fC.Y(), fB.Y(), -fB.Y(), -fC.Y(), -fA.Y() };
  Double_t rmax     = fB.Mod();
  Double_t stripoff = fA.Mod();
  Double_t dstrip   = (rmax - stripoff) / fNStrips;

  // Double_t hybridThick = (fPrintboardThickness + fCopperThickness 
  //			     + fChipThickness);
  // Double_t modThick    = fSiThickness + fSpacer + hybridThick;
  // Double_t fmdWidth    = (modThick + fHoneycombThickness + fModuleSpacing
  // 			     + fLegLength + fSpacer);
  
  // Top
  // TGeoTube*   fmdShape     = new TGeoTube(fLowR-.1,fHighR+.1,fmdWidth/2+.1);
  // TGeoPcon*   fmdShape     = new TGeoPcon(0, 360, 2);
  // fmdShape->DefineSection(0, -fmdWidth / 2 - .1, fLowR-.1, fHighR+.1);
  // fmdShape->DefineSection(1,  fmdWidth / 2 + .1, fLowR-.1, fHighR+.1);
  // TGeoVolume* fmdVolume    = new TGeoVolume("FMD1", fmdShape, fVacuum);
  
  
  // Sensor 
  TGeoXtru* sensorShape = new TGeoXtru(2);
  sensorShape->DefinePolygon(6, xv, yv);
  sensorShape->DefineSection(0, - fSiThickness/2);
  sensorShape->DefineSection(1, fSiThickness/2);
  TGeoVolume* sensorVolume= new TGeoVolume("FISE",sensorShape,fSi);
  sensorVolume->SetLineColor(2);
  // sensorVolume->VisibleDaughters(kFALSE);
  // sensorVolume->SetVisibility(kTRUE);

  // Virtual volume shape to divide 
  TGeoTubeSeg* activeShape   = new TGeoTubeSeg(fLowR, rmax, fSiThickness/2, 
					       - fTheta, fTheta);
  TGeoVolume*  activeVolume  = new TGeoVolume("FIAC", activeShape,fSi);
  activeVolume->SetLineColor(3);
  sensorVolume->AddNodeOverlap(activeVolume, 0);
  // activeVolume->SetTransparency(0x3f);
  TGeoVolume* sectorVolume   = activeVolume->Divide("FISC",2,2,-fTheta,
						    0,0,"N");
  TGeoVolume* stripVolume    = sectorVolume->Divide("FIST",1,fNStrips,
						    stripoff,dstrip,0,"SX");
  (void)stripVolume;
  
  // Position
  Double_t x, y, z;
  
  // Make PCB volume 
  for (Int_t i = 0; i < 3; i++) yv[i] -= fBondingWidth;
  for (Int_t i = 3; i < 6; i++) yv[i] += fBondingWidth;
  Double_t off = (TMath::Tan(TMath::Pi() * fTheta / 180) * fBondingWidth);

  // PCB layer 
  TGeoXtru* pcbShape      = new TGeoXtru(2);
  pcbShape->DefinePolygon(6, xv, yv);
  pcbShape->DefineSection(0, - fPrintboardThickness/2);
  pcbShape->DefineSection(1, fPrintboardThickness/2);
  TGeoVolume* pcbVolume   = new TGeoVolume("FPCB",pcbShape,fPCB);
  pcbVolume->SetLineColor(4);

  // Copper layer
  TGeoXtru* cuShape       = new TGeoXtru(2);
  cuShape->DefinePolygon(6, xv, yv);
  cuShape->DefineSection(0, - fCopperThickness/2);
  cuShape->DefineSection(1, fCopperThickness/2);
  TGeoVolume* cuVolume    = new TGeoVolume("FCOP",cuShape,fCopper);
  cuVolume->SetLineColor(4);

  // Chip layer
  TGeoXtru*   chipShape   = new TGeoXtru(2);
  chipShape->DefinePolygon(6, xv, yv);
  chipShape->DefineSection(0, - fChipThickness/2);
  chipShape->DefineSection(1, fChipThickness/2);
  TGeoVolume* chipVolume = new TGeoVolume("FCHI",chipShape,fChip);
  chipVolume->SetLineColor(4);

  // Short leg shape 
  TGeoTube*   shortLegShape  = new TGeoTube(0, fLegRadius, fLegLength / 2);
  TGeoVolume* shortLegVolume = new TGeoVolume("FISL", shortLegShape, fPlastic);
  shortLegVolume->SetLineColor(2);
  // Long leg shape
  TGeoTube*   longLegShape   = new TGeoTube(0, fLegRadius, 
					    (fLegLength + fModuleSpacing) / 2);
  TGeoVolume* longLegVolume  = new TGeoVolume("FILL", longLegShape, fPlastic);
  longLegVolume->SetLineColor(2);
  
  // Make a front volume 
  TGeoVolume* frontVolume    =  new TGeoVolumeAssembly("FIFV");
  z                          =  fPrintboardThickness / 2;
  frontVolume->AddNode(pcbVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fPrintboardThickness + fCopperThickness) / 2;
  frontVolume->AddNode(cuVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fCopperThickness + fChipThickness) / 2;
  frontVolume->AddNode(chipVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fChipThickness + fModuleSpacing 
				 + fLegLength) / 2;
  x                          =  fA.X() + fLegOffset + fLegRadius;
  y                          =  0;
  frontVolume->AddNode(longLegVolume, 0, new TGeoTranslation(x, y, z));
  x                          =  fC.X();
  y                          =  fC.Y() - fLegOffset - fLegRadius - off;
  frontVolume->AddNode(longLegVolume, 1, new TGeoTranslation(x,y,z));
  y                          =  -y;
  frontVolume->AddNode(longLegVolume, 2, new TGeoTranslation(x,y,z));

  // Make a back volume 
  TGeoVolume* backVolume     =  new TGeoVolumeAssembly("FIBV");
  z                          =  fPrintboardThickness / 2;
  backVolume->AddNode(pcbVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fPrintboardThickness + fCopperThickness) / 2;
  backVolume->AddNode(cuVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fCopperThickness + fChipThickness) / 2;
  backVolume->AddNode(chipVolume, 0, new TGeoTranslation(0, 0, z));
  z                          += (fChipThickness + fLegLength) / 2;
  x                          =  fA.X() + fLegOffset + fLegRadius;
  y                          =  0;
  backVolume->AddNode(shortLegVolume, 0, new TGeoTranslation(x, y, z));
  x                          =  fC.X();
  y                          =  fC.Y() - fLegOffset - fLegRadius - off;
  backVolume->AddNode(shortLegVolume, 1, new TGeoTranslation(x,y,z));
  y                          =  -y;
  backVolume->AddNode(shortLegVolume, 2, new TGeoTranslation(x,y,z));

#if 1
  TGeoVolume* ringTopVolume  = new TGeoVolumeAssembly("FITV");
  TGeoVolume* ringBotVolume  = new TGeoVolumeAssembly("FIBV");
  Double_t dz = 0;
#else
  Double_t dz = (fSiThickness + fSpacer + fModuleSpacing + 
		 fPrintboardThickness + fCopperThickness + 
		 fChipThickness + fLegLength);
  TGeoTubeSeg* topRingShape = new TGeoTubeSeg(fA.X(), rmax, dz/2, 0, 180);
  TGeoTubeSeg* botRingShape = new TGeoTubeSeg(fA.X(), rmax, dz/2, 180, 360);
  TGeoVolume* ringTopVolume = new TGeoVolume("FITV", topRingShape, fAir);
  TGeoVolume* ringBotVolume = new TGeoVolume("FIBV", botRingShape, fAir);
#endif

  TGeoVolume* half = ringTopVolume;
  // Place in mother 
  for (Int_t i = 0; i < 10; i++) {
    if (i > 4)  half  = ringBotVolume;
    Bool_t      front = ((i % 2) == 0);
    z                 =  -dz/2 + fSiThickness / 2 + (i % 2) * fModuleSpacing;
    TGeoMatrix* m1    = new TGeoCombiTrans(0,0,z,0);
    m1->RotateZ((2 * i + 1) * fTheta);
    half->AddNode(sensorVolume, i, m1);
    TGeoVolume* vol   = (front ? frontVolume : backVolume);
    z                 += fSpacer + fSiThickness / 2;
    TGeoMatrix* m2    = new TGeoCombiTrans(0,0,z,0);
    m2->RotateZ((2 * i + 1) * fTheta);
    half->AddNode(vol, i, m2);    
  }

  TGeoVolume* fmdTopVolume  = new TGeoVolumeAssembly("FMT1");
  TGeoVolume* fmdBotVolume  = new TGeoVolumeAssembly("FMB1");
  fmdTopVolume->AddNode(ringTopVolume, 0, new TGeoTranslation(0,0,dz/2));
  fmdBotVolume->AddNode(ringBotVolume, 0, new TGeoTranslation(0,0,dz/2));
  
  // Top of Honeycomb
  TGeoTubeSeg* hcShape       = new TGeoTubeSeg(fLowR, fHighR,
					       fHoneycombThickness / 2,
					       0, 180);
  TGeoVolume*  hcVolume      = new TGeoVolume("F1II", hcShape, fAl);
  hcVolume->VisibleDaughters(kFALSE);
  hcVolume->SetLineColor(5);
  // Air in top of honeycomb
  TGeoTubeSeg* ihcShape      = new TGeoTubeSeg(fLowR + fAlThickness, 
					       fHighR - fAlThickness, 
					       (fHoneycombThickness 
						- fAlThickness) / 2,
					       0, 180);
  TGeoVolume*  ihcVolume     = new TGeoVolume("F1IK", ihcShape, fKapton);
  hcVolume->AddNode(ihcVolume, 0);

  // Add honey comb to mothers. 
  z = (fSiThickness + fSpacer + fPrintboardThickness + fCopperThickness
       + fChipThickness + fModuleSpacing + fLegLength + fHoneycombThickness/2);
  fmdTopVolume->AddNode(hcVolume, 0, new TGeoTranslation(0,0,z));
  TGeoMatrix* r = new TGeoCombiTrans(0,0,z, 0); r->RotateZ(180);
  fmdBotVolume->AddNode(hcVolume, 1, r);

  z = 0;
  caveVolume->AddNode(fmdTopVolume, 1, new TGeoTranslation(0,0,z));
  caveVolume->AddNode(fmdBotVolume, 1, new TGeoTranslation(0,0,z));
}

//____________________________________________________________________
void
Geometry::Register()
{
  // Framework::Task::Register(option);
  TGeoShape* caveShape = new TGeoBBox(fHighR+10, fHighR+10, fHighR+10);
  TGeoVolume* caveVolume = new TGeoVolume("Cave", caveShape, fVacuum);
  gGeoManager->SetTopVolume(caveVolume);

  if (!gGeoManager->GetVolume("FM1T")) MakeDetailed(caveVolume);
  gGeoManager->CloseGeometry();
}

//____________________________________________________________________
void
Geometry::Align()
{
  if (!gGeoManager)                 return;
  if (!gGeoManager->GetTopVolume()) return;
  if (!gGeoManager->IsClosed())     return;
  
  if (!fMatricies) fMatricies = new TObjArray;
  TGeoIterator next(gGeoManager->GetTopVolume());
  TGeoNode* node = 0;

  while ((node = static_cast<TGeoNode*>(next()))) {
    // node->Print();
    if (node->GetName()[0] == 'F' && node->GetName()[2] == 'S' && 
	node->GetName()[3] == 'E') {
      // We go an FMD module 
      TString path("/");
      path.Append(gGeoManager->GetNode(0)->GetName());
      Int_t nlevel = next.GetLevel();
      Debug(10, "Exec", "Level is %d", nlevel);
      for (int lvl = 0; lvl <= nlevel; lvl++) {
	TGeoNode* p = next.GetNode(lvl);
	if (!p) continue;
	Debug(10, "Exec", "Adding '%s' to path '%s'",
	      p->GetName(), path.Data());
	if (!path.IsNull()) path.Append("/");
	path.Append(p->GetName());
      }
      TGeoPhysicalNode* pnode = gGeoManager->MakePhysicalNode(path.Data());
      TGeoHMatrix* matrix = new TGeoHMatrix(*node->GetMatrix());
      TGeoRotation pertub;
      Double_t angles[] = { gRandom->Uniform(0, 3), 
			    gRandom->Uniform(0, 3), 
			    gRandom->Uniform(0, 3) };
      pertub.RotateX(angles[0]);
      pertub.RotateY(angles[1]);
      pertub.RotateZ(angles[1]);
      *matrix *= pertub;
      Debug(5, "Exec", "Aliging %s (%f,%f,%f)",
	    pnode->GetName(), angles[0], angles[1], angles[2]);
      pnode->Align(matrix);
    }
  }
}

//____________________________________________________________________
//
// EOF
//

  
 GeoGeometry.C:1
 GeoGeometry.C:2
 GeoGeometry.C:3
 GeoGeometry.C:4
 GeoGeometry.C:5
 GeoGeometry.C:6
 GeoGeometry.C:7
 GeoGeometry.C:8
 GeoGeometry.C:9
 GeoGeometry.C:10
 GeoGeometry.C:11
 GeoGeometry.C:12
 GeoGeometry.C:13
 GeoGeometry.C:14
 GeoGeometry.C:15
 GeoGeometry.C:16
 GeoGeometry.C:17
 GeoGeometry.C:18
 GeoGeometry.C:19
 GeoGeometry.C:20
 GeoGeometry.C:21
 GeoGeometry.C:22
 GeoGeometry.C:23
 GeoGeometry.C:24
 GeoGeometry.C:25
 GeoGeometry.C:26
 GeoGeometry.C:27
 GeoGeometry.C:28
 GeoGeometry.C:29
 GeoGeometry.C:30
 GeoGeometry.C:31
 GeoGeometry.C:32
 GeoGeometry.C:33
 GeoGeometry.C:34
 GeoGeometry.C:35
 GeoGeometry.C:36
 GeoGeometry.C:37
 GeoGeometry.C:38
 GeoGeometry.C:39
 GeoGeometry.C:40
 GeoGeometry.C:41
 GeoGeometry.C:42
 GeoGeometry.C:43
 GeoGeometry.C:44
 GeoGeometry.C:45
 GeoGeometry.C:46
 GeoGeometry.C:47
 GeoGeometry.C:48
 GeoGeometry.C:49
 GeoGeometry.C:50
 GeoGeometry.C:51
 GeoGeometry.C:52
 GeoGeometry.C:53
 GeoGeometry.C:54
 GeoGeometry.C:55
 GeoGeometry.C:56
 GeoGeometry.C:57
 GeoGeometry.C:58
 GeoGeometry.C:59
 GeoGeometry.C:60
 GeoGeometry.C:61
 GeoGeometry.C:62
 GeoGeometry.C:63
 GeoGeometry.C:64
 GeoGeometry.C:65
 GeoGeometry.C:66
 GeoGeometry.C:67
 GeoGeometry.C:68
 GeoGeometry.C:69
 GeoGeometry.C:70
 GeoGeometry.C:71
 GeoGeometry.C:72
 GeoGeometry.C:73
 GeoGeometry.C:74
 GeoGeometry.C:75
 GeoGeometry.C:76
 GeoGeometry.C:77
 GeoGeometry.C:78
 GeoGeometry.C:79
 GeoGeometry.C:80
 GeoGeometry.C:81
 GeoGeometry.C:82
 GeoGeometry.C:83
 GeoGeometry.C:84
 GeoGeometry.C:85
 GeoGeometry.C:86
 GeoGeometry.C:87
 GeoGeometry.C:88
 GeoGeometry.C:89
 GeoGeometry.C:90
 GeoGeometry.C:91
 GeoGeometry.C:92
 GeoGeometry.C:93
 GeoGeometry.C:94
 GeoGeometry.C:95
 GeoGeometry.C:96
 GeoGeometry.C:97
 GeoGeometry.C:98
 GeoGeometry.C:99
 GeoGeometry.C:100
 GeoGeometry.C:101
 GeoGeometry.C:102
 GeoGeometry.C:103
 GeoGeometry.C:104
 GeoGeometry.C:105
 GeoGeometry.C:106
 GeoGeometry.C:107
 GeoGeometry.C:108
 GeoGeometry.C:109
 GeoGeometry.C:110
 GeoGeometry.C:111
 GeoGeometry.C:112
 GeoGeometry.C:113
 GeoGeometry.C:114
 GeoGeometry.C:115
 GeoGeometry.C:116
 GeoGeometry.C:117
 GeoGeometry.C:118
 GeoGeometry.C:119
 GeoGeometry.C:120
 GeoGeometry.C:121
 GeoGeometry.C:122
 GeoGeometry.C:123
 GeoGeometry.C:124
 GeoGeometry.C:125
 GeoGeometry.C:126
 GeoGeometry.C:127
 GeoGeometry.C:128
 GeoGeometry.C:129
 GeoGeometry.C:130
 GeoGeometry.C:131
 GeoGeometry.C:132
 GeoGeometry.C:133
 GeoGeometry.C:134
 GeoGeometry.C:135
 GeoGeometry.C:136
 GeoGeometry.C:137
 GeoGeometry.C:138
 GeoGeometry.C:139
 GeoGeometry.C:140
 GeoGeometry.C:141
 GeoGeometry.C:142
 GeoGeometry.C:143
 GeoGeometry.C:144
 GeoGeometry.C:145
 GeoGeometry.C:146
 GeoGeometry.C:147
 GeoGeometry.C:148
 GeoGeometry.C:149
 GeoGeometry.C:150
 GeoGeometry.C:151
 GeoGeometry.C:152
 GeoGeometry.C:153
 GeoGeometry.C:154
 GeoGeometry.C:155
 GeoGeometry.C:156
 GeoGeometry.C:157
 GeoGeometry.C:158
 GeoGeometry.C:159
 GeoGeometry.C:160
 GeoGeometry.C:161
 GeoGeometry.C:162
 GeoGeometry.C:163
 GeoGeometry.C:164
 GeoGeometry.C:165
 GeoGeometry.C:166
 GeoGeometry.C:167
 GeoGeometry.C:168
 GeoGeometry.C:169
 GeoGeometry.C:170
 GeoGeometry.C:171
 GeoGeometry.C:172
 GeoGeometry.C:173
 GeoGeometry.C:174
 GeoGeometry.C:175
 GeoGeometry.C:176
 GeoGeometry.C:177
 GeoGeometry.C:178
 GeoGeometry.C:179
 GeoGeometry.C:180
 GeoGeometry.C:181
 GeoGeometry.C:182
 GeoGeometry.C:183
 GeoGeometry.C:184
 GeoGeometry.C:185
 GeoGeometry.C:186
 GeoGeometry.C:187
 GeoGeometry.C:188
 GeoGeometry.C:189
 GeoGeometry.C:190
 GeoGeometry.C:191
 GeoGeometry.C:192
 GeoGeometry.C:193
 GeoGeometry.C:194
 GeoGeometry.C:195
 GeoGeometry.C:196
 GeoGeometry.C:197
 GeoGeometry.C:198
 GeoGeometry.C:199
 GeoGeometry.C:200
 GeoGeometry.C:201
 GeoGeometry.C:202
 GeoGeometry.C:203
 GeoGeometry.C:204
 GeoGeometry.C:205
 GeoGeometry.C:206
 GeoGeometry.C:207
 GeoGeometry.C:208
 GeoGeometry.C:209
 GeoGeometry.C:210
 GeoGeometry.C:211
 GeoGeometry.C:212
 GeoGeometry.C:213
 GeoGeometry.C:214
 GeoGeometry.C:215
 GeoGeometry.C:216
 GeoGeometry.C:217
 GeoGeometry.C:218
 GeoGeometry.C:219
 GeoGeometry.C:220
 GeoGeometry.C:221
 GeoGeometry.C:222
 GeoGeometry.C:223
 GeoGeometry.C:224
 GeoGeometry.C:225
 GeoGeometry.C:226
 GeoGeometry.C:227
 GeoGeometry.C:228
 GeoGeometry.C:229
 GeoGeometry.C:230
 GeoGeometry.C:231
 GeoGeometry.C:232
 GeoGeometry.C:233
 GeoGeometry.C:234
 GeoGeometry.C:235
 GeoGeometry.C:236
 GeoGeometry.C:237
 GeoGeometry.C:238
 GeoGeometry.C:239
 GeoGeometry.C:240
 GeoGeometry.C:241
 GeoGeometry.C:242
 GeoGeometry.C:243
 GeoGeometry.C:244
 GeoGeometry.C:245
 GeoGeometry.C:246
 GeoGeometry.C:247
 GeoGeometry.C:248
 GeoGeometry.C:249
 GeoGeometry.C:250
 GeoGeometry.C:251
 GeoGeometry.C:252
 GeoGeometry.C:253
 GeoGeometry.C:254
 GeoGeometry.C:255
 GeoGeometry.C:256
 GeoGeometry.C:257
 GeoGeometry.C:258
 GeoGeometry.C:259
 GeoGeometry.C:260
 GeoGeometry.C:261
 GeoGeometry.C:262
 GeoGeometry.C:263
 GeoGeometry.C:264
 GeoGeometry.C:265
 GeoGeometry.C:266
 GeoGeometry.C:267
 GeoGeometry.C:268
 GeoGeometry.C:269
 GeoGeometry.C:270
 GeoGeometry.C:271
 GeoGeometry.C:272
 GeoGeometry.C:273
 GeoGeometry.C:274
 GeoGeometry.C:275
 GeoGeometry.C:276
 GeoGeometry.C:277
 GeoGeometry.C:278
 GeoGeometry.C:279
 GeoGeometry.C:280
 GeoGeometry.C:281
 GeoGeometry.C:282
 GeoGeometry.C:283
 GeoGeometry.C:284
 GeoGeometry.C:285
 GeoGeometry.C:286
 GeoGeometry.C:287
 GeoGeometry.C:288
 GeoGeometry.C:289
 GeoGeometry.C:290
 GeoGeometry.C:291
 GeoGeometry.C:292
 GeoGeometry.C:293
 GeoGeometry.C:294
 GeoGeometry.C:295
 GeoGeometry.C:296
 GeoGeometry.C:297
 GeoGeometry.C:298
 GeoGeometry.C:299
 GeoGeometry.C:300
 GeoGeometry.C:301
 GeoGeometry.C:302
 GeoGeometry.C:303
 GeoGeometry.C:304
 GeoGeometry.C:305
 GeoGeometry.C:306
 GeoGeometry.C:307
 GeoGeometry.C:308
 GeoGeometry.C:309
 GeoGeometry.C:310
 GeoGeometry.C:311
 GeoGeometry.C:312
 GeoGeometry.C:313
 GeoGeometry.C:314
 GeoGeometry.C:315
 GeoGeometry.C:316
 GeoGeometry.C:317
 GeoGeometry.C:318
 GeoGeometry.C:319
 GeoGeometry.C:320
 GeoGeometry.C:321
 GeoGeometry.C:322
 GeoGeometry.C:323
 GeoGeometry.C:324
 GeoGeometry.C:325
 GeoGeometry.C:326
 GeoGeometry.C:327
 GeoGeometry.C:328
 GeoGeometry.C:329
 GeoGeometry.C:330
 GeoGeometry.C:331
 GeoGeometry.C:332
 GeoGeometry.C:333
 GeoGeometry.C:334
 GeoGeometry.C:335
 GeoGeometry.C:336
 GeoGeometry.C:337
 GeoGeometry.C:338
 GeoGeometry.C:339
 GeoGeometry.C:340
 GeoGeometry.C:341
 GeoGeometry.C:342
 GeoGeometry.C:343
 GeoGeometry.C:344
 GeoGeometry.C:345
 GeoGeometry.C:346
 GeoGeometry.C:347
 GeoGeometry.C:348
 GeoGeometry.C:349
 GeoGeometry.C:350
 GeoGeometry.C:351
 GeoGeometry.C:352
 GeoGeometry.C:353
 GeoGeometry.C:354
 GeoGeometry.C:355
 GeoGeometry.C:356
 GeoGeometry.C:357
 GeoGeometry.C:358
 GeoGeometry.C:359
 GeoGeometry.C:360
 GeoGeometry.C:361
 GeoGeometry.C:362
 GeoGeometry.C:363
 GeoGeometry.C:364
 GeoGeometry.C:365
 GeoGeometry.C:366
 GeoGeometry.C:367
 GeoGeometry.C:368
 GeoGeometry.C:369
 GeoGeometry.C:370
 GeoGeometry.C:371
 GeoGeometry.C:372
 GeoGeometry.C:373
 GeoGeometry.C:374
 GeoGeometry.C:375
 GeoGeometry.C:376
 GeoGeometry.C:377
 GeoGeometry.C:378
 GeoGeometry.C:379
 GeoGeometry.C:380
 GeoGeometry.C:381
 GeoGeometry.C:382
 GeoGeometry.C:383
 GeoGeometry.C:384
 GeoGeometry.C:385
 GeoGeometry.C:386
 GeoGeometry.C:387
 GeoGeometry.C:388
 GeoGeometry.C:389
 GeoGeometry.C:390
 GeoGeometry.C:391
 GeoGeometry.C:392
 GeoGeometry.C:393
 GeoGeometry.C:394
 GeoGeometry.C:395
 GeoGeometry.C:396
 GeoGeometry.C:397
 GeoGeometry.C:398
 GeoGeometry.C:399
 GeoGeometry.C:400
 GeoGeometry.C:401
 GeoGeometry.C:402
 GeoGeometry.C:403
 GeoGeometry.C:404
 GeoGeometry.C:405
 GeoGeometry.C:406
 GeoGeometry.C:407
 GeoGeometry.C:408
 GeoGeometry.C:409
 GeoGeometry.C:410
 GeoGeometry.C:411
 GeoGeometry.C:412
 GeoGeometry.C:413
 GeoGeometry.C:414
 GeoGeometry.C:415
 GeoGeometry.C:416
 GeoGeometry.C:417
 GeoGeometry.C:418
 GeoGeometry.C:419
 GeoGeometry.C:420
 GeoGeometry.C:421
 GeoGeometry.C:422
 GeoGeometry.C:423
 GeoGeometry.C:424
 GeoGeometry.C:425
 GeoGeometry.C:426
 GeoGeometry.C:427
 GeoGeometry.C:428
 GeoGeometry.C:429
 GeoGeometry.C:430
 GeoGeometry.C:431
 GeoGeometry.C:432
 GeoGeometry.C:433
 GeoGeometry.C:434
 GeoGeometry.C:435
 GeoGeometry.C:436
 GeoGeometry.C:437
 GeoGeometry.C:438
 GeoGeometry.C:439
 GeoGeometry.C:440
 GeoGeometry.C:441
 GeoGeometry.C:442
 GeoGeometry.C:443
 GeoGeometry.C:444
 GeoGeometry.C:445
 GeoGeometry.C:446
 GeoGeometry.C:447
 GeoGeometry.C:448
 GeoGeometry.C:449
 GeoGeometry.C:450
 GeoGeometry.C:451
 GeoGeometry.C:452
 GeoGeometry.C:453
 GeoGeometry.C:454
 GeoGeometry.C:455
 GeoGeometry.C:456
 GeoGeometry.C:457
 GeoGeometry.C:458
 GeoGeometry.C:459
 GeoGeometry.C:460
 GeoGeometry.C:461
 GeoGeometry.C:462
 GeoGeometry.C:463
 GeoGeometry.C:464
 GeoGeometry.C:465
 GeoGeometry.C:466
 GeoGeometry.C:467
 GeoGeometry.C:468
 GeoGeometry.C:469
 GeoGeometry.C:470
 GeoGeometry.C:471
 GeoGeometry.C:472
 GeoGeometry.C:473
 GeoGeometry.C:474
 GeoGeometry.C:475
 GeoGeometry.C:476
 GeoGeometry.C:477
 GeoGeometry.C:478
 GeoGeometry.C:479
 GeoGeometry.C:480
 GeoGeometry.C:481
 GeoGeometry.C:482
 GeoGeometry.C:483
 GeoGeometry.C:484
 GeoGeometry.C:485
 GeoGeometry.C:486
 GeoGeometry.C:487
 GeoGeometry.C:488
 GeoGeometry.C:489
 GeoGeometry.C:490
 GeoGeometry.C:491
 GeoGeometry.C:492
 GeoGeometry.C:493
 GeoGeometry.C:494
 GeoGeometry.C:495
 GeoGeometry.C:496
 GeoGeometry.C:497
 GeoGeometry.C:498
 GeoGeometry.C:499
 GeoGeometry.C:500
 GeoGeometry.C:501
 GeoGeometry.C:502
 GeoGeometry.C:503
 GeoGeometry.C:504
 GeoGeometry.C:505
 GeoGeometry.C:506
 GeoGeometry.C:507
 GeoGeometry.C:508
 GeoGeometry.C:509
 GeoGeometry.C:510
 GeoGeometry.C:511
 GeoGeometry.C:512
 GeoGeometry.C:513
 GeoGeometry.C:514
 GeoGeometry.C:515
 GeoGeometry.C:516
 GeoGeometry.C:517
 GeoGeometry.C:518
 GeoGeometry.C:519
 GeoGeometry.C:520
 GeoGeometry.C:521
 GeoGeometry.C:522
 GeoGeometry.C:523
 GeoGeometry.C:524
 GeoGeometry.C:525
 GeoGeometry.C:526
 GeoGeometry.C:527
 GeoGeometry.C:528
 GeoGeometry.C:529
 GeoGeometry.C:530
 GeoGeometry.C:531
 GeoGeometry.C:532
 GeoGeometry.C:533
 GeoGeometry.C:534
 GeoGeometry.C:535
 GeoGeometry.C:536
 GeoGeometry.C:537
 GeoGeometry.C:538
 GeoGeometry.C:539
 GeoGeometry.C:540
 GeoGeometry.C:541
 GeoGeometry.C:542
 GeoGeometry.C:543
 GeoGeometry.C:544
 GeoGeometry.C:545
 GeoGeometry.C:546
 GeoGeometry.C:547
 GeoGeometry.C:548
 GeoGeometry.C:549
 GeoGeometry.C:550
 GeoGeometry.C:551
 GeoGeometry.C:552
 GeoGeometry.C:553
 GeoGeometry.C:554
 GeoGeometry.C:555
 GeoGeometry.C:556
 GeoGeometry.C:557
 GeoGeometry.C:558
 GeoGeometry.C:559
 GeoGeometry.C:560
 GeoGeometry.C:561
 GeoGeometry.C:562
 GeoGeometry.C:563
 GeoGeometry.C:564
 GeoGeometry.C:565
 GeoGeometry.C:566
 GeoGeometry.C:567
 GeoGeometry.C:568
 GeoGeometry.C:569
 GeoGeometry.C:570
 GeoGeometry.C:571
 GeoGeometry.C:572
 GeoGeometry.C:573
 GeoGeometry.C:574
 GeoGeometry.C:575
 GeoGeometry.C:576
 GeoGeometry.C:577
 GeoGeometry.C:578
 GeoGeometry.C:579
 GeoGeometry.C:580
 GeoGeometry.C:581
 GeoGeometry.C:582
 GeoGeometry.C:583
 GeoGeometry.C:584
 GeoGeometry.C:585
 GeoGeometry.C:586
 GeoGeometry.C:587
 GeoGeometry.C:588
 GeoGeometry.C:589
 GeoGeometry.C:590
 GeoGeometry.C:591
 GeoGeometry.C:592
 GeoGeometry.C:593
 GeoGeometry.C:594
 GeoGeometry.C:595
 GeoGeometry.C:596
 GeoGeometry.C:597
 GeoGeometry.C:598
 GeoGeometry.C:599
 GeoGeometry.C:600
 GeoGeometry.C:601
 GeoGeometry.C:602
 GeoGeometry.C:603
 GeoGeometry.C:604
 GeoGeometry.C:605
 GeoGeometry.C:606
 GeoGeometry.C:607
 GeoGeometry.C:608
 GeoGeometry.C:609
 GeoGeometry.C:610
 GeoGeometry.C:611
 GeoGeometry.C:612
 GeoGeometry.C:613
 GeoGeometry.C:614
 GeoGeometry.C:615
 GeoGeometry.C:616
 GeoGeometry.C:617
 GeoGeometry.C:618
 GeoGeometry.C:619
 GeoGeometry.C:620
 GeoGeometry.C:621
 GeoGeometry.C:622
 GeoGeometry.C:623
 GeoGeometry.C:624
 GeoGeometry.C:625
 GeoGeometry.C:626
 GeoGeometry.C:627
 GeoGeometry.C:628
 GeoGeometry.C:629
 GeoGeometry.C:630
 GeoGeometry.C:631
 GeoGeometry.C:632
 GeoGeometry.C:633
 GeoGeometry.C:634
 GeoGeometry.C:635
 GeoGeometry.C:636
 GeoGeometry.C:637
 GeoGeometry.C:638
 GeoGeometry.C:639
 GeoGeometry.C:640
 GeoGeometry.C:641
 GeoGeometry.C:642
 GeoGeometry.C:643