<commit_before>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
/* $Id$ */

//_________________________________________________________________________
// Implementation version v0 of EMCAL Manager class 
// An object of this class does not produce hits nor digits
// It is the one to use if you do not want to produce outputs in TREEH or TREED
// This class places a Geometry of the EMCAL in the ALICE Detector as defined in AliEMCALGeometry.cxx                 
//*-- Author: <NAME> (SUBATECH)
//*-- and   : <NAME> (LBL / UCT)
//          : <NAME> (WSU)     SHASHLYK

// This Version of AliEMCALv0 reduces the number of volumes placed in XEN1 (the envelope) to less than five hundred
// The Envelope is Placed in Alice, And the Aluminium layer. Mini envelopes (XU) are then placed in XEN1.
// Each mini envelope contains 1 scintillator, and 1 lead layer, except the last one which contains just one scintillator layer.
// At the moment I cannot place the 36 and above layers in the mini envelopes so all layers are still placed in XEN1


// --- ROOT system ---

#include "TNode.h"
#include "TBRIK.h"
#include "TTRD1.h"
#include "TTRD2.h"
#include "TTRAP.h"
#include "TPGON.h"
#include "TTUBS.h"
#include "TGeometry.h"
#include "TVirtualMC.h"
#include "TArrayI.h"
#include "TROOT.h"
#include "TArrayF.h"
#include "TList.h"
#include "TVector2.h"

#include "AliEMCALShishKebabModule.h"
#include "AliEMCALShishKebabTrd1Module.h"
//#include <TGeant3.h> // can not include - I don't know why

// --- Standard library ---

//#include <stdio.h>

// --- AliRoot header files ---

#include "AliEMCALv0.h"
#include "AliEMCALGeometry.h"
#include "AliRun.h"
#include "AliLog.h"

ClassImp(AliEMCALv0)

TArrayF ENVELOP1; // now global for BuildGeometry() - 30-aug-2004
int idAIR=1599, idPB = 1600, idSC = 1601, idSTEEL = 1603; // global
Int_t *idtmed=0, idrotm=0;
double sampleWidth=0.;
double parEMOD[5], smodPar0=0., smodPar1=0., smodPar2=0.;


//______________________________________________________________________
AliEMCALv0::AliEMCALv0(const char *name, const char *title):
  AliEMCAL(name,title)
{
  // ctor : title is used to identify the layout
  GetGeometry() ; 
  fShishKebabModules = 0;
}

//______________________________________________________________________
void AliEMCALv0::BuildGeometry()
{
    // Display Geometry for display.C

    const Int_t kColorArm1   = kBlue ;

    AliEMCALGeometry * geom = GetGeometry() ; 
    TString gn(geom->GetName());
    gn.ToUpper(); 

    // Define the shape of the Calorimeter 
    TNode * top = gAlice->GetGeometry()->GetNode("alice") ; // See AliceGeom/Nodes
    TNode * envelopNode = 0;
    char *envn = "Envelop1";
    if(!gn.Contains("SHISH") || gn.Contains("TRD2")){
      new TTUBS(envn, "Tubs that contains arm 1", "void", 
	      geom->GetEnvelop(0) -10, // rmin 
	      geom->GetEnvelop(1) +40 ,// rmax
	      geom->GetEnvelop(2)/2.0, // half length in Z
	      geom->GetArm1PhiMin(),   // minimum phi angle
	      geom->GetArm1PhiMax()    // maximum phi angle
	);
      top->cd();
      envelopNode = new TNode(envn, "Arm1 Envelop", "Envelop1", 0., 0., 0., "") ;
    } else {
      if(gn.Contains("WSUC")) {
        envelopNode = BuildGeometryOfWSUC();
      } else { // Shish-kebab now for compact, twist and TRD1 cases (ALIC)
        envn="Envelop2";
        TPGON *pgon = new TPGON(envn, "PGON that contains arm 1", "void", 
        geom->GetArm1PhiMin(),geom->GetArm1PhiMax()-geom->GetArm1PhiMin(),geom->GetNPhiSuperModule(), 2);
      // define section
        pgon->DefineSection(0, ENVELOP1[4],  ENVELOP1[5], ENVELOP1[6]);
        pgon->DefineSection(1, ENVELOP1[7],  ENVELOP1[5], ENVELOP1[6]);
        top->cd();
        envelopNode = new TNode(envn, "Arm1 Envelop2", envn, 0., 0., 0., "") ;
      }
    }
				
    envelopNode->SetLineColor(kColorArm1) ;
    fNodes->Add(envelopNode);
}

TNode *AliEMCALv0::BuildGeometryOfWSUC()
{ // June 8, 2005; see directory geant3/TGeant3/G3toRoot.cxx
  // enum EColor { kWhite, kBlack, kRed, kGreen, kBlue, kYellow, kMagenta, kCyan } - see $ROOTSYS/include/Gtypes.h
   AliEMCALGeometry * g = GetGeometry(); 
   TNode * top = gAlice->GetGeometry()->GetNode("alice") ; // See AliceGeom/Nodes
   top->cd();

   TNode *envelopNode = 0;
   char *name = "";
   /*
    name = "WSUC";
   new TBRIK(name, "WSUC(XEN1 in Geant)","void",ENVELOP1[0],ENVELOP1[1],ENVELOP1[2]);
    envelopNode = new TNode(name, "envelope for WSUC", name, 0., 0., 0., "");
   envelopNode->SetVisibility(0);
   */

   TNode *emod=0, *scmx=0;
   name = "SMOD"; // super module
   new TBRIK(name, "SMOD(SMOD in Geant)","void", smodPar0,smodPar1,smodPar2);
   if(envelopNode) envelopNode->cd();
   TNode *smod = new TNode(name, "SMOD", name, 0., 0., 0., "");   
   smod->SetLineColor(kBlue) ;
   if(envelopNode==0) envelopNode = smod;

   name = "EMOD"; // see CreateEMOD 
   TTRD1 *EMOD = new TTRD1(name, "EMOD(EMOD in Geant)","void", float(parEMOD[0]),
   float(parEMOD[1]),float(parEMOD[2]),float(parEMOD[3]));

   // SCMX = EMOD/4 for simplicity of drawing
   name = "SCMX";
   Float_t dz=0.,theta=0.,phi=0.,h1=0.,bl1=0.,tl1=0.,alpha1=0.,h2=0.,bl2=0.,tl2=0.,alpha2=0.;
   h1     = EMOD->GetDy()/2.;
   bl1    = EMOD->GetDx()/2.;
   tl1    = bl1;
   alpha1 = 0.;
   h2     = EMOD->GetDy()/2.;
   bl2    = EMOD->GetDx2()/2.;
   tl2    = bl2;
   alpha2 = 0.;

   dz       = EMOD->GetDz();
   double dr = TMath::Sqrt((h2-h1)*(h2-h1)+(bl2-bl1)*(bl2-bl1));
   theta    = TMath::ATan2(dr,2.*dz) * TMath::RadToDeg(); 
   phi      = 180.;

   TTRAP *SCMX = new TTRAP(name, "SCMX(SCMX as in Geant)","void",
   	     dz,theta,phi, h1,bl1,tl1,alpha1, h2,bl2,tl2,alpha2);
   //   SCMX->Dump();
   Float_t xShiftSCMX = (EMOD->GetDx() + EMOD->GetDx2())/4.;
   Float_t yShiftSCMX = EMOD->GetDy()/2.;
   printf(" xShiftSCMX %7.4f yShiftSCMX %7.4f \n",xShiftSCMX,yShiftSCMX);

   name = "EMOD"; // see CreateEMOD 
   smod->cd();
   
   AliEMCALShishKebabTrd1Module *mod=0;
   Double_t  angle=90., xpos=0.,ypos=0.,zpos=0., xposSCMX=0.,yposSCMX=0.,zposSCMX=0.;
   char rtmn[100], rtmt[100];
   TRotMatrix *rtm=0, *rtmSCMX=0;
   int numEmod=0;
   for(int iz=0; iz<g->GetNZ(); iz++) {
     mod   = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(iz);
     zpos = mod->GetPosZ()      - smodPar2;
     ypos = mod->GetPosXfromR() - smodPar1;

     angle = mod->GetThetaInDegree();
     sprintf(rtmn,"rmEmod%5.1f",angle);
     sprintf(rtmt,"rotation matrix for EMOD, iz=%i, angle = %6.3f",iz, angle);
     if(iz==0) rtm = new TRotMatrix(rtmn, rtmt,0.,0., 90.,0., 90.,90.); // z'(x); y'(y); x'(z)
     else      rtm = new TRotMatrix(rtmn, rtmt,90.-angle,270., 90.0,0.0, angle,90.);

     TGeometry *tg = gAlice->GetGeometry();
     for(int ix=0; ix<g->GetNPhi(); ix++) { // flat in phi
       xpos = g->GetPhiModuleSize()*(2*ix+1 - g->GetNPhi())/2.;
       sprintf(rtmt,"EMOD, iz %i, ix %i, angle %6.3f",iz,ix, angle);
       TString namNode=name;
       namNode += numEmod++;
       smod->cd();
       emod = new TNode(namNode.Data(), rtmt, (TShape*)EMOD, xpos,ypos,zpos,rtm);   
       //       emod->SetLineColor(kGreen) ;
       emod->SetVisibility(0); // SCMX will bi visible 
       if(SCMX) { // 4(2x2) sensetive volume inside EMOD
         emod->cd();
         zposSCMX = 0.;
         for(int jy=0; jy<2; jy++){ // division on y
           yposSCMX = yShiftSCMX *(2*jy - 1); 
           for(int jx=0; jx<2; jx++){ // division on x
             Double_t theta1=90.,phi1=0., theta2=90.,phi2=90., theta3=0.,phi3=0 ; 
             xposSCMX = xShiftSCMX *(2*jx - 1);              
             namNode = "SCMX";
             namNode += jy;
             namNode += jx;
             sprintf(rtmn,"rm%s",namNode.Data());
             sprintf(rtmt,"rotation matrix for %s inside EMOD",namNode.Data());
             rtmSCMX = tg->GetRotMatrix(rtmn);
             if(jx == 1) {
               phi1 = 180.; // x' = -x
               phi2 = 270.; // y' = -y
             }
             if(rtmSCMX == 0) rtmSCMX = new TRotMatrix(rtmn,rtmt, theta1,phi1, theta2,phi2, theta3,phi3);
             sprintf(rtmt,"%s inside %s", namNode.Data(), emod->GetName());
             scmx = new TNode(namNode.Data(), rtmt, (TShape*)SCMX, xposSCMX,yposSCMX,zposSCMX,rtmSCMX);   
             scmx->SetLineColor(kMagenta);
           }
         }
       }
     }
   }
   //   emod->Draw(); // for testing

   return envelopNode;
}

//______________________________________________________________________
void AliEMCALv0::CreateGeometry()
{
  // Create the EMCAL geometry for Geant
  // Geometry of a tower
  //|-----------------------------------------------------| XEN1
  //| |                                                 | |
  //| |    Al thickness = GetAlFrontThickness()         | |
  //| |                                                 | |
  //| |                                                 | |
  //| |                                                 | |
  //|  -------------------------------------------------  |
  //| |    Air Gap = GetGap2Active()                    | |
  //| |                                                 | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPST (ECAL e = GetECScintThick() )     | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPBX (ECAL e = GetECPbRadThick() )     | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPST (ECAL e = GetECScintThick()       | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPBX (ECAL e = GetECPbRadThick() )     | |
  //|  -------------------------------------------------  |
  //|    etc ..... GetNECLayers() - 1 times               |
  //|  -------------------------------------------------  |
  //| |    XUNLayer : XPST (ECAL e = GetECScintThick()  | |
  //|  -------------------------------------------------  |

    Float_t etamin,etamax;
    Float_t *dum=0;

    AliEMCALGeometry * geom = GetGeometry() ; 
    TString gn(geom->GetName());
    gn.ToUpper(); 

    if(!(geom->IsInitialized())){
	Error("CreateGeometry","EMCAL Geometry class has not been set up.");
    } // end if

    // Get pointer to the array containing media indices
    idtmed = fIdtmed->GetArray() - 1599 ;

    idrotm = 1;
    //  gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3) - see AliModule
    AliMatrix(idrotm, 90.0, 0., 90.0, 90.0, 0.0, 0.0) ; 

    // Create the EMCAL Mother Volume (a polygone) within which to place the Detector and named XEN1 

    Float_t envelopA[10];
    if(gn.Contains("TRD2")) { // TUBS
       envelopA[0] = geom->GetEnvelop(0) - 10.; // rmin 
       envelopA[1] = geom->GetEnvelop(1) + 12.; // rmax
       //       envelopA[2] = geom->ZFromEtaR(geom->GetEnvelop(1), geom->GetArm1EtaMin());
       envelopA[2] = 390.; // 6-feb-05
       envelopA[3] = geom->GetArm1PhiMin();
       envelopA[4] = geom->GetArm1PhiMax();
       gMC->Gsvolu("XEN1", "TUBS", idtmed[1599], envelopA, 5) ;   // Tubs filled with air 
       ENVELOP1.Set(5, envelopA);
    // Position the EMCAL Mother Volume (XEN1) in Alice (ALIC)  
       gMC->Gspos("XEN1", 1, "ALIC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    } else if(gn.Contains("TRD1") && gn.Contains("WSUC") ) { // TRD1 for WSUC facility
      // 17-may-05 - just BOX
      envelopA[0] = 26;
      envelopA[1] = 15;
      envelopA[2] = 30;
      gMC->Gsvolu("XEN1", "BOX", idtmed[idSC], envelopA, 3) ;
      ENVELOP1.Set(3);
      for(int i=0; i<3; i++) ENVELOP1[i] = envelopA[i]; // 23-may-05  
      // Position the EMCAL Mother Volume (XEN1) in WSUC  
      gMC->Gspos("XEN1", 1, "WSUC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    } else { 
      envelopA[0] = geom->GetArm1PhiMin();                         // minimum phi angle
      envelopA[1] = geom->GetArm1PhiMax() - geom->GetArm1PhiMin(); // angular range in phi
      envelopA[2] = geom->GetNPhi();                               // number of sections in phi
      envelopA[3] = 2;                                             // 2 z coordinates
      envelopA[4] = geom->ZFromEtaR(geom->GetEnvelop(1),
				   geom->GetArm1EtaMin());       // z coordinate 1
    //add some padding for mother volume
      envelopA[5] = geom->GetEnvelop(0) ;                          // rmin at z1
      envelopA[6] = geom->GetEnvelop(1) ;                          // rmax at z1
      envelopA[7] = geom->ZFromEtaR(geom->GetEnvelop(1),
				  geom->GetArm1EtaMax());        // z coordinate 2
      envelopA[8] = envelopA[5] ;                                  // radii are the same.
      envelopA[9] = envelopA[6] ;                                  // radii are the same.

      if(gn.Contains("SHISH")) envelopA[2] = geom->GetNPhiSuperModule();

      gMC->Gsvolu("XEN1", "PGON ", idtmed[1599], envelopA, 10) ;   // Polygone filled with air 
      ENVELOP1.Set(10, envelopA);
      if (gDebug==2) {
        printf("CreateGeometry: XEN1 = %f, %f\n", envelopA[5], envelopA[6]); 
        printf("CreateGeometry: XU0 = %f, %f\n", envelopA[5], envelopA[6]); 
      }
    // Position the EMCAL Mother Volume (XEN1) in Alice (ALIC)  
      gMC->Gspos("XEN1", 1, "ALIC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    }

    if(gn.Contains("SHISH")){
      // Compact and twist
      CreateShishKebabGeometry();
      return;
    }

    if (AliLog::GetGlobalDebugLevel()>=2) {
      printf("CreateGeometry: XEN1 = %f, %f\n", envelopA[5], envelopA[6]); 
      printf("CreateGeometry: XU0 = %f, %f\n", envelopA[5], envelopA[6]); 
    }

    // Create mini-envelopes which will contain the Tower scintillator-radiator
    
    TString label ;
    
    envelopA[5] = envelopA[5] + geom->GetGap2Active() // we are at the first scintllator
      + geom->GetAlFrontThickness();                  // rmin at z1
    envelopA[6] = envelopA[5] ;


    Int_t i ; 

    Int_t nLayers = geom->GetNECLayers();

    for (i = 0; i < (nLayers-1); i++ ){
	label = "XU" ;
	label += i ;
	Float_t tseg ; 
	tseg = geom->GetECScintThick()+geom->GetECPbRadThick();       // thickness of scintillator+Pb in E Cal
	envelopA[5] = envelopA[6] ;                                   // rmin at z1
	envelopA[4] = geom->ZFromEtaR(envelopA[5] + tseg,
				      geom->GetArm1EtaMin());         // z coordinate 1
	envelopA[7] = geom->ZFromEtaR(envelopA[5] + tseg,
				      geom->GetArm1EtaMax());         // z coordinate 2
	envelopA[6] = envelopA[5] + tseg ;                            // rmax at z1
	envelopA[8] = envelopA[5] ;                                   // radii are the same.
	envelopA[9] = envelopA[6] ;                                   // radii are the same.
 
	gMC->Gsvolu(label.Data(), "PGON", idtmed[1599], envelopA, 10);// Polygone filled with air 

	// Position XUi in XEN1
	
	gMC->Gspos(label.Data(), 1, "XEN1", 0.0, 0.0, 0.0, idrotm, "ONLY") ;

	if (AliLog::GetGlobalDebugLevel() >= 2)
	  printf("CreateGeometry: XU%d = %f, %f\n", i, envelopA[5], envelopA[6]); 

    } // end  i
 
  
    // Create one mini-envelope which will contain the last scintillator XU(nlayers-1) because there is one more scintillator than Pb layer XU(nlayers-1)

    label = "XU" ;
    label += i ;
    envelopA[5] = envelopA[6] ;                                   // rmin at z1
    envelopA[4] = geom->ZFromEtaR(envelopA[5] + geom->GetECScintThick(),
				  geom->GetArm1EtaMin());         // z coordinate 1
    envelopA[7] = geom->ZFromEtaR(envelopA[5] + geom->GetECScintThick(),
				  geom->GetArm1EtaMax());         // z coordinate 2
    envelopA[6] = envelopA[5] + geom->GetECScintThick() ;         // rmax at z1
    envelopA[8] = envelopA[5] ;                                   // radii are the same.
    envelopA[9] = envelopA[6] ;                                   // radii are the same.

    gMC->Gsvolu(label.Data(), "PGON", idtmed[1599], envelopA, 10); // Polygone filled with air

    // Position the last minienvelope in XEN1
  
    gMC->Gspos(label.Data(), 1, "XEN1", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    
    if(AliLog::GetGlobalDebugLevel() >= 2) 
    printf("CreateGeometry: XEN%d = %f, %f\n", i, envelopA[5], envelopA[6]);
  
    // Create the shapes of active material (LEAD/Aluminium/Scintillator)
    // to be placed
    Float_t envelopB[10]; // First Layer of Aluminium
    Float_t envelopC[10]; // Scintillator Layers
    Float_t envelopD[10]; // Lead Layers

    envelopC[0] = envelopD[0] = envelopB[0] = envelopA[0] ;  // starting position in Phi
    envelopC[1] = envelopD[1] = envelopB[1] = envelopA[1] ;  // angular range in phi          
    envelopC[2] = envelopD[2] = envelopB[2] = envelopA[2] ;  // number of sections in Phi
    envelopC[3] = envelopD[3] = envelopB[3] = envelopA[3] ;  // 2 z coordinates

    Float_t dist = geom->GetEnvelop(0) + geom->GetAlFrontThickness() + geom->GetGap2Active() ; 
    envelopB[4] = geom->ZFromEtaR(dist,
				  geom->GetArm1EtaMin());   // z co-ordinate 1
    envelopB[5] = geom->GetEnvelop(0) ;                     // rmin at z1
    envelopB[6] = envelopB[5] + geom->GetAlFrontThickness();// rmax at z1
    envelopB[7] = geom->ZFromEtaR(dist,
				  geom->GetArm1EtaMax());   // z co-ordinate 2
    envelopB[8] = envelopB[5] ;                             // radii are the same.
    envelopB[9] = envelopB[6] ;                             // radii are the same.

    // Define active volumes completely
    
    gMC->Gsvolu("XALU", "PGON", idtmed[1602], envelopB, 10); // PGON filled with Al
    
    gMC->Gspos("XALU", 1, "XEN1", 0.0, 0.0, 0.0 , idrotm, "ONLY") ; // Position Aluminium Layer in XEN1

    gMC->Gsvolu("XPST", "PGON", idtmed[1601], dum, 0);      // PGON filled with Scintillator (shape to be defined by GSPOSP)
  
    gMC->Gsvolu("XPBX", "PGON", idtmed[1600], dum, 0);      // PGON filled with Lead (shape to be defined by GSPOSP)
  
    //gMC->Gsvolu("XCUX", "PGON", idtmed[1603], dum, 0);      // PGON filled with Copper (shape to be defined by GSPOSP)

    gMC->Gsdvn("XPHI", "XPST", geom->GetNPhi(), 2);         // Divide eta section of scintillators into phi segments.
 
    // Position alternatively scintillator and  Lead Layers in XUi.

    envelopD[6] = envelopB[6] + geom->GetGap2Active() ;// gap between Al layer and XU0
    
    for (int i = 0; i < nLayers; i++ ){
      label = "XU" ;
      label += i  ; // we will place one layer in each mini envelope)	

      Float_t scthick ; // scintillator thickness 
      scthick = geom->GetECScintThick() ;

      envelopC[5] = envelopD[6] ;           //rmin
      envelopC[6] = envelopC[5] + scthick ; //rmax
      envelopC[8] = envelopC[5] ;           //rmin
      envelopC[9] = envelopC[6] ;           //rmax

      if(AliLog::GetGlobalDebugLevel() >= 2 ) 
	printf("CreateGeometry: volume = %s, name = XPST thickness = %f deb = %f/%f fin = %f/%f", label.Data(), scthick, envelopC[5], envelopC[8], envelopC[6], envelopC[9]) ; 

      for (int j =0; j < (geom->GetNEta()) ; j++){
	etamin = geom->GetArm1EtaMin()+
	  (j*geom->GetDeltaEta());
	etamax = geom->GetArm1EtaMin()+
	  ((j+1)*geom->GetDeltaEta());
	envelopC[4] = geom->ZFromEtaR(envelopC[5],etamin); //z begin  
	envelopC[7] = geom->ZFromEtaR(envelopC[5],etamax);// z end 
	
	gMC->Gsposp("XPST",1+j+i*(geom->GetNEta()), label.Data(), 
		    0.0, 0.0, 0.0 , idrotm, "ONLY", envelopC, 10); // Position and define layer
      } // end for j
      
	Float_t radthick ; // radiator thickness 
	TString radname ;  // radiator name
	radthick = geom->GetECPbRadThick();
	radname  =  "XPBX" ; 

	if ( i < nLayers -1 ) { // except for the last XU which contains only one scintillator layer 

	  envelopD[5] = envelopC[6] ; //rmin
	  envelopD[8] = envelopD[5] ; //rmin
	  envelopD[6] = envelopD[5] + radthick ; // rmax
	  envelopD[9] = envelopD[6] ; //rmax
	  
	  if(AliLog::GetGlobalDebugLevel() >= 2 ) 
	    printf("CreateGeometry: volume = %s, name = %s thickness = %f deb = %f/%f fin = %f/%f", label.Data(), radname.Data(), radthick, envelopD[5], envelopD[8], envelopD[6], envelopD[9]) ; 

	  for (int j =0; j < (geom->GetNEta()) ; j++){
	    etamin = geom->GetArm1EtaMin()+
	      (j*geom->GetDeltaEta());
	    etamax = geom->GetArm1EtaMin()+
	      ((j+1)*geom->GetDeltaEta());
	    envelopD[4] = geom->ZFromEtaR(envelopD[5],etamin);//z begin  
	    envelopD[7] = geom->ZFromEtaR(envelopD[5],etamax);// z end
	    
	    // Position and Define Layer
	    
	    gMC->Gsposp(radname.Data(),1+j+i*(geom->GetNEta()), label.Data(), 
			0.0, 0.0, 0.0 , idrotm, "ONLY", envelopD, 10);
	     } // end for j
	  } // if not last layer
    }  // for i
}

//______________________________________________________________________
void AliEMCALv0::Init(void)
{
    // Just prints an information message
  
  if(AliLog::GetGlobalDebugLevel()>0) { 
    TString message("\n") ; 
    message += "*****************************************\n" ;
    
    // Here the EMCAL initialisation code (if any!)
    
    AliEMCALGeometry * geom = GetGeometry() ; 
    
    if (geom!=0) {   
      message += "AliEMCAL " ; 
      message += Version() ; 
      message += "EMCAL geometry initialized for " ; 
      message += geom->GetName()  ;
    }
    else {
      message += "AliEMCAL " ; 
      message += Version() ;  
      message += "EMCAL geometry initialization failed !" ; 
    }
    message += "\n*****************************************" ;
    printf(message.Data() ) ; 
  }
}

// 24-aug-04 by PAI
void AliEMCALv0::CreateShishKebabGeometry()
{  // TWIST, TRD1 and TRD2 
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  // see AliModule::fIdtmed
  //  idtmed = fIdtmed->GetArray() - 1599 ; // see AliEMCAL::::CreateMaterials()
  //  int idAIR=1599, idPB = 1600, idSC = 1601, idSTEEL = 1603;
  //  idAL = 1602;
  Double_t par[10], xpos=0., ypos=0., zpos=0.;

  CreateSmod("XEN1");        // 18-may-05 

  CreateEmod("SMOD","EMOD"); // 18-may-05

  if(gn.Contains("110DEG")) CreateEmod("SM10","EMOD"); // 12-oct-05

  // Sensitive SC  (2x2 tiles)
  double parSCM0[5], *dummy = 0, parTRAP[11];
  Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad(), tanTmp = TMath::Tan(trd1Angle/2.);
   if(!gn.Contains("TRD")) { // standard module
    par[0] = (g->GetECPbRadThick()+g->GetECScintThick())*g->GetNECLayers()/2.;
    par[1] = par[2] = g->GetPhiTileSize();   // Symetric case
    gMC->Gsvolu("SCM0", "BOX", idtmed[idSC], par, 3); // 2x2 tiles
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
    // Division to tile size
    gMC->Gsdvn("SCM1","SCM0", g->GetNPHIdiv(), 2); // y-axis
    gMC->Gsdvn("SCM2","SCM1", g->GetNETAdiv(), 3); // z-axis
  // put LED to the SCM2 
    par[0] = g->GetECPbRadThick()/2.;
    par[1] = par[2] = g->GetPhiTileSize()/2.; // Symetric case
    gMC->Gsvolu("PBTI", "BOX", idtmed[idPB], par, 3);

    printf(" Pb tiles \n");
    int nr=0;
    ypos = zpos = 0.0;
    xpos = -sampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
    for(int ix=0; ix<g->GetNECLayers(); ix++){
      gMC->Gspos("PBTI", ++nr, "SCM2", xpos, ypos, zpos, 0, "ONLY") ;
    //    printf(" %i xpos %f \n", ix+1, xpos);
      xpos += sampleWidth;
    } 
    printf(" Number of Pb tiles in SCM2 %i \n", nr);
  } else if(gn.Contains("TRD1")) { // TRD1 - 30-sep-04
    if(gn.Contains("MAY05")){
      Double_t dzTmp = g->GetFrontSteelStrip()+g->GetPassiveScintThick();
      parSCM0[0] = parEMOD[0] + tanTmp*dzTmp; // dx1
      parSCM0[1] = parEMOD[1];                // dx2
      parSCM0[2] = parEMOD[2];                // dy
      for(int i=0; i<3; i++) parSCM0[i] -= g->GetLateralSteelStrip();
      parSCM0[3] = parEMOD[3] - dzTmp/2.; // dz

      gMC->Gsvolu("SCM0", "TRD1", idtmed[idAIR], parSCM0, 4);
      gMC->Gspos("SCM0", 1, "EMOD", 0., 0., dzTmp/2., 0, "ONLY") ;
    } else { // before MAY 2005
      double wallThickness = g->GetPhiModuleSize()/2. -  g->GetPhiTileSize(); // Need check
      for(int i=0; i<3; i++) parSCM0[i] = parEMOD[i] - wallThickness;
      parSCM0[3] = parEMOD[3];
      gMC->Gsvolu("SCM0", "TRD1", idtmed[idAIR], parSCM0, 4);
      gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
    }

    if(g->GetNPHIdiv()==2 && g->GetNETAdiv()==2) {
    // Division to tile size - 1-oct-04
      printf("<I> Divide SCM0 on y-axis %i\n", g->GetNETAdiv());
      gMC->Gsdvn("SCMY","SCM0", g->GetNETAdiv(), 2); // y-axis
    // Trapesoid 2x2
      parTRAP[0] = parSCM0[3];    // dz
      parTRAP[1] = TMath::ATan2((parSCM0[1]-parSCM0[0])/2.,2.*parSCM0[3])*180./TMath::Pi(); // theta
      parTRAP[2] = 0.;           // phi
      // bottom
      parTRAP[3] = parSCM0[2]/2.; // H1
      parTRAP[4] = parSCM0[0]/2.; // BL1
      parTRAP[5] = parTRAP[4];    // TL1
      parTRAP[6] = 0.0;           // ALP1
      // top
      parTRAP[7] = parSCM0[2]/2.; // H2
      parTRAP[8] = parSCM0[1]/2.; // BL2
      parTRAP[9] = parTRAP[8];    // TL2
      parTRAP[10]= 0.0;           // ALP2
      printf(" ** TRAP ** \n");
      for(int i=0; i<11; i++) printf(" par[%2.2i] %9.4f\n", i, parTRAP[i]);

      gMC->Gsvolu("SCMX", "TRAP", idtmed[idSC], parTRAP, 11);
      xpos = +(parSCM0[1]+parSCM0[0])/4.;
      gMC->Gspos("SCMX", 1, "SCMY", xpos, 0.0, 0.0, 0, "ONLY") ;

      // Using rotation because SCMX should be the same due to Pb tiles
      xpos = -xpos; 
      AliMatrix(idrotm, 90.0,180., 90.0, 270.0, 0.0,0.0) ;
      gMC->Gspos("SCMX", 2, "SCMY", xpos, 0.0, 0.0, idrotm, "ONLY");
    // put LED to the SCM0 
      AliEMCALShishKebabTrd1Module *mod = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(0);
      gMC->Gsvolu("PBTI", "BOX", idtmed[idPB], dummy, 0);

      par[1] = parSCM0[2]/2;            // y 
      par[2] = g->GetECPbRadThick()/2.; // z

      int nr=0;
      ypos = 0.0;
      zpos = -sampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
      double xCenterSCMX =  (parTRAP[4] +  parTRAP[8])/2.;
      if(!gn.Contains("NOPB")) { // for testing - 11-jul-05
        printf(" Pb tiles \n");
        for(int iz=0; iz<g->GetNECLayers(); iz++){
          par[0] = (parSCM0[0] + mod->GetTanBetta()*sampleWidth*iz)/2.;
          xpos   = par[0] - xCenterSCMX;
          gMC->Gsposp("PBTI", ++nr, "SCMX", xpos, ypos, zpos, 0, "ONLY", par, 3) ;
          printf(" %i xpos %f zpos %f par[0] %f \n", iz+1, xpos, zpos, par[0]);
          zpos += sampleWidth;
        } 
        printf(" Number of Pb tiles in SCMX %i \n", nr);
      }
    } else if(g->GetNPHIdiv()==3 && g->GetNETAdiv()==3) {
      Trd1Tower3X3(parSCM0);
    } else if(g->GetNPHIdiv()==4 && g->GetNETAdiv()==4) {
      Trd1Tower4X4();
    }
  } else if(gn.Contains("TRD2")) {    // TRD2 - 14-jan-05
    //    Scm0InTrd2(g, parEMOD, parSCM0); // First dessin 
    PbmoInTrd2(g, parEMOD, parSCM0); // Second dessin 
  }
}

void AliEMCALv0::CreateSmod(const char* mother)
{ // 18-may-05; mother="XEN1"; 
  // child="SMOD" from first to 10th, "SM10" (11th and 12th) (TRD1 case)
  // child="SMON" and "SMOP"("TRD2" case)
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper();

  Double_t par[1], parTubs[5], xpos=0., ypos=0., zpos=0., rpos=0., dphi=0., phi=0.0, phiRad=0.;
  Double_t par1C = 0.;
  //  ===== define Super Module from air - 14x30 module ==== ;
  sampleWidth = double(g->GetECPbRadThick()+g->GetECScintThick());
  printf("\n ## Super Module | sampleWidth %5.3f ## %s \n", sampleWidth, gn.Data());
  par[0] = g->GetShellThickness()/2.;
  par[1] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
  par[2] = g->GetEtaModuleSize()*15.; <commit_msg>Corrected array size<commit_after>/**************************************************************************
 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
 *                                                                        *
 * Author: The ALICE Off-line Project.                                    *
 * Contributors are mentioned in the code where appropriate.              *
 *                                                                        *
 * Permission to use, copy, modify and distribute this software and its   *
 * documentation strictly for non-commercial purposes is hereby granted   *
 * without fee, provided that the above copyright notice appears in all   *
 * copies and that both the copyright notice and this permission notice   *
 * appear in the supporting documentation. The authors make no claims     *
 * about the suitability of this software for any purpose. It is          *
 * provided "as is" without express or implied warranty.                  *
 **************************************************************************/
/* $Id$ */

//_________________________________________________________________________
// Implementation version v0 of EMCAL Manager class 
// An object of this class does not produce hits nor digits
// It is the one to use if you do not want to produce outputs in TREEH or TREED
// This class places a Geometry of the EMCAL in the ALICE Detector as defined in AliEMCALGeometry.cxx                 
//*-- Author: <NAME> (SUBATECH)
//*-- and   : <NAME> (LBL / UCT)
//          : <NAME> (WSU)     SHASHLYK

// This Version of AliEMCALv0 reduces the number of volumes placed in XEN1 (the envelope) to less than five hundred
// The Envelope is Placed in Alice, And the Aluminium layer. Mini envelopes (XU) are then placed in XEN1.
// Each mini envelope contains 1 scintillator, and 1 lead layer, except the last one which contains just one scintillator layer.
// At the moment I cannot place the 36 and above layers in the mini envelopes so all layers are still placed in XEN1


// --- ROOT system ---

#include "TNode.h"
#include "TBRIK.h"
#include "TTRD1.h"
#include "TTRD2.h"
#include "TTRAP.h"
#include "TPGON.h"
#include "TTUBS.h"
#include "TGeometry.h"
#include "TVirtualMC.h"
#include "TArrayI.h"
#include "TROOT.h"
#include "TArrayF.h"
#include "TList.h"
#include "TVector2.h"

#include "AliEMCALShishKebabModule.h"
#include "AliEMCALShishKebabTrd1Module.h"
//#include <TGeant3.h> // can not include - I don't know why

// --- Standard library ---

//#include <stdio.h>

// --- AliRoot header files ---

#include "AliEMCALv0.h"
#include "AliEMCALGeometry.h"
#include "AliRun.h"
#include "AliLog.h"

ClassImp(AliEMCALv0)

TArrayF ENVELOP1; // now global for BuildGeometry() - 30-aug-2004
int idAIR=1599, idPB = 1600, idSC = 1601, idSTEEL = 1603; // global
Int_t *idtmed=0, idrotm=0;
double sampleWidth=0.;
double parEMOD[5], smodPar0=0., smodPar1=0., smodPar2=0.;


//______________________________________________________________________
AliEMCALv0::AliEMCALv0(const char *name, const char *title):
  AliEMCAL(name,title)
{
  // ctor : title is used to identify the layout
  GetGeometry() ; 
  fShishKebabModules = 0;
}

//______________________________________________________________________
void AliEMCALv0::BuildGeometry()
{
    // Display Geometry for display.C

    const Int_t kColorArm1   = kBlue ;

    AliEMCALGeometry * geom = GetGeometry() ; 
    TString gn(geom->GetName());
    gn.ToUpper(); 

    // Define the shape of the Calorimeter 
    TNode * top = gAlice->GetGeometry()->GetNode("alice") ; // See AliceGeom/Nodes
    TNode * envelopNode = 0;
    char *envn = "Envelop1";
    if(!gn.Contains("SHISH") || gn.Contains("TRD2")){
      new TTUBS(envn, "Tubs that contains arm 1", "void", 
	      geom->GetEnvelop(0) -10, // rmin 
	      geom->GetEnvelop(1) +40 ,// rmax
	      geom->GetEnvelop(2)/2.0, // half length in Z
	      geom->GetArm1PhiMin(),   // minimum phi angle
	      geom->GetArm1PhiMax()    // maximum phi angle
	);
      top->cd();
      envelopNode = new TNode(envn, "Arm1 Envelop", "Envelop1", 0., 0., 0., "") ;
    } else {
      if(gn.Contains("WSUC")) {
        envelopNode = BuildGeometryOfWSUC();
      } else { // Shish-kebab now for compact, twist and TRD1 cases (ALIC)
        envn="Envelop2";
        TPGON *pgon = new TPGON(envn, "PGON that contains arm 1", "void", 
        geom->GetArm1PhiMin(),geom->GetArm1PhiMax()-geom->GetArm1PhiMin(),geom->GetNPhiSuperModule(), 2);
      // define section
        pgon->DefineSection(0, ENVELOP1[4],  ENVELOP1[5], ENVELOP1[6]);
        pgon->DefineSection(1, ENVELOP1[7],  ENVELOP1[5], ENVELOP1[6]);
        top->cd();
        envelopNode = new TNode(envn, "Arm1 Envelop2", envn, 0., 0., 0., "") ;
      }
    }
				
    envelopNode->SetLineColor(kColorArm1) ;
    fNodes->Add(envelopNode);
}

TNode *AliEMCALv0::BuildGeometryOfWSUC()
{ // June 8, 2005; see directory geant3/TGeant3/G3toRoot.cxx
  // enum EColor { kWhite, kBlack, kRed, kGreen, kBlue, kYellow, kMagenta, kCyan } - see $ROOTSYS/include/Gtypes.h
   AliEMCALGeometry * g = GetGeometry(); 
   TNode * top = gAlice->GetGeometry()->GetNode("alice") ; // See AliceGeom/Nodes
   top->cd();

   TNode *envelopNode = 0;
   char *name = "";
   /*
    name = "WSUC";
   new TBRIK(name, "WSUC(XEN1 in Geant)","void",ENVELOP1[0],ENVELOP1[1],ENVELOP1[2]);
    envelopNode = new TNode(name, "envelope for WSUC", name, 0., 0., 0., "");
   envelopNode->SetVisibility(0);
   */

   TNode *emod=0, *scmx=0;
   name = "SMOD"; // super module
   new TBRIK(name, "SMOD(SMOD in Geant)","void", smodPar0,smodPar1,smodPar2);
   if(envelopNode) envelopNode->cd();
   TNode *smod = new TNode(name, "SMOD", name, 0., 0., 0., "");   
   smod->SetLineColor(kBlue) ;
   if(envelopNode==0) envelopNode = smod;

   name = "EMOD"; // see CreateEMOD 
   TTRD1 *EMOD = new TTRD1(name, "EMOD(EMOD in Geant)","void", float(parEMOD[0]),
   float(parEMOD[1]),float(parEMOD[2]),float(parEMOD[3]));

   // SCMX = EMOD/4 for simplicity of drawing
   name = "SCMX";
   Float_t dz=0.,theta=0.,phi=0.,h1=0.,bl1=0.,tl1=0.,alpha1=0.,h2=0.,bl2=0.,tl2=0.,alpha2=0.;
   h1     = EMOD->GetDy()/2.;
   bl1    = EMOD->GetDx()/2.;
   tl1    = bl1;
   alpha1 = 0.;
   h2     = EMOD->GetDy()/2.;
   bl2    = EMOD->GetDx2()/2.;
   tl2    = bl2;
   alpha2 = 0.;

   dz       = EMOD->GetDz();
   double dr = TMath::Sqrt((h2-h1)*(h2-h1)+(bl2-bl1)*(bl2-bl1));
   theta    = TMath::ATan2(dr,2.*dz) * TMath::RadToDeg(); 
   phi      = 180.;

   TTRAP *SCMX = new TTRAP(name, "SCMX(SCMX as in Geant)","void",
   	     dz,theta,phi, h1,bl1,tl1,alpha1, h2,bl2,tl2,alpha2);
   //   SCMX->Dump();
   Float_t xShiftSCMX = (EMOD->GetDx() + EMOD->GetDx2())/4.;
   Float_t yShiftSCMX = EMOD->GetDy()/2.;
   printf(" xShiftSCMX %7.4f yShiftSCMX %7.4f \n",xShiftSCMX,yShiftSCMX);

   name = "EMOD"; // see CreateEMOD 
   smod->cd();
   
   AliEMCALShishKebabTrd1Module *mod=0;
   Double_t  angle=90., xpos=0.,ypos=0.,zpos=0., xposSCMX=0.,yposSCMX=0.,zposSCMX=0.;
   char rtmn[100], rtmt[100];
   TRotMatrix *rtm=0, *rtmSCMX=0;
   int numEmod=0;
   for(int iz=0; iz<g->GetNZ(); iz++) {
     mod   = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(iz);
     zpos = mod->GetPosZ()      - smodPar2;
     ypos = mod->GetPosXfromR() - smodPar1;

     angle = mod->GetThetaInDegree();
     sprintf(rtmn,"rmEmod%5.1f",angle);
     sprintf(rtmt,"rotation matrix for EMOD, iz=%i, angle = %6.3f",iz, angle);
     if(iz==0) rtm = new TRotMatrix(rtmn, rtmt,0.,0., 90.,0., 90.,90.); // z'(x); y'(y); x'(z)
     else      rtm = new TRotMatrix(rtmn, rtmt,90.-angle,270., 90.0,0.0, angle,90.);

     TGeometry *tg = gAlice->GetGeometry();
     for(int ix=0; ix<g->GetNPhi(); ix++) { // flat in phi
       xpos = g->GetPhiModuleSize()*(2*ix+1 - g->GetNPhi())/2.;
       sprintf(rtmt,"EMOD, iz %i, ix %i, angle %6.3f",iz,ix, angle);
       TString namNode=name;
       namNode += numEmod++;
       smod->cd();
       emod = new TNode(namNode.Data(), rtmt, (TShape*)EMOD, xpos,ypos,zpos,rtm);   
       //       emod->SetLineColor(kGreen) ;
       emod->SetVisibility(0); // SCMX will bi visible 
       if(SCMX) { // 4(2x2) sensetive volume inside EMOD
         emod->cd();
         zposSCMX = 0.;
         for(int jy=0; jy<2; jy++){ // division on y
           yposSCMX = yShiftSCMX *(2*jy - 1); 
           for(int jx=0; jx<2; jx++){ // division on x
             Double_t theta1=90.,phi1=0., theta2=90.,phi2=90., theta3=0.,phi3=0 ; 
             xposSCMX = xShiftSCMX *(2*jx - 1);              
             namNode = "SCMX";
             namNode += jy;
             namNode += jx;
             sprintf(rtmn,"rm%s",namNode.Data());
             sprintf(rtmt,"rotation matrix for %s inside EMOD",namNode.Data());
             rtmSCMX = tg->GetRotMatrix(rtmn);
             if(jx == 1) {
               phi1 = 180.; // x' = -x
               phi2 = 270.; // y' = -y
             }
             if(rtmSCMX == 0) rtmSCMX = new TRotMatrix(rtmn,rtmt, theta1,phi1, theta2,phi2, theta3,phi3);
             sprintf(rtmt,"%s inside %s", namNode.Data(), emod->GetName());
             scmx = new TNode(namNode.Data(), rtmt, (TShape*)SCMX, xposSCMX,yposSCMX,zposSCMX,rtmSCMX);   
             scmx->SetLineColor(kMagenta);
           }
         }
       }
     }
   }
   //   emod->Draw(); // for testing

   return envelopNode;
}

//______________________________________________________________________
void AliEMCALv0::CreateGeometry()
{
  // Create the EMCAL geometry for Geant
  // Geometry of a tower
  //|-----------------------------------------------------| XEN1
  //| |                                                 | |
  //| |    Al thickness = GetAlFrontThickness()         | |
  //| |                                                 | |
  //| |                                                 | |
  //| |                                                 | |
  //|  -------------------------------------------------  |
  //| |    Air Gap = GetGap2Active()                    | |
  //| |                                                 | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPST (ECAL e = GetECScintThick() )     | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPBX (ECAL e = GetECPbRadThick() )     | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPST (ECAL e = GetECScintThick()       | |
  //|  -------------------------------------------------  |
  //| |    XU1 : XPBX (ECAL e = GetECPbRadThick() )     | |
  //|  -------------------------------------------------  |
  //|    etc ..... GetNECLayers() - 1 times               |
  //|  -------------------------------------------------  |
  //| |    XUNLayer : XPST (ECAL e = GetECScintThick()  | |
  //|  -------------------------------------------------  |

    Float_t etamin,etamax;
    Float_t *dum=0;

    AliEMCALGeometry * geom = GetGeometry() ; 
    TString gn(geom->GetName());
    gn.ToUpper(); 

    if(!(geom->IsInitialized())){
	Error("CreateGeometry","EMCAL Geometry class has not been set up.");
    } // end if

    // Get pointer to the array containing media indices
    idtmed = fIdtmed->GetArray() - 1599 ;

    idrotm = 1;
    //  gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3) - see AliModule
    AliMatrix(idrotm, 90.0, 0., 90.0, 90.0, 0.0, 0.0) ; 

    // Create the EMCAL Mother Volume (a polygone) within which to place the Detector and named XEN1 

    Float_t envelopA[10];
    if(gn.Contains("TRD2")) { // TUBS
       envelopA[0] = geom->GetEnvelop(0) - 10.; // rmin 
       envelopA[1] = geom->GetEnvelop(1) + 12.; // rmax
       //       envelopA[2] = geom->ZFromEtaR(geom->GetEnvelop(1), geom->GetArm1EtaMin());
       envelopA[2] = 390.; // 6-feb-05
       envelopA[3] = geom->GetArm1PhiMin();
       envelopA[4] = geom->GetArm1PhiMax();
       gMC->Gsvolu("XEN1", "TUBS", idtmed[1599], envelopA, 5) ;   // Tubs filled with air 
       ENVELOP1.Set(5, envelopA);
    // Position the EMCAL Mother Volume (XEN1) in Alice (ALIC)  
       gMC->Gspos("XEN1", 1, "ALIC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    } else if(gn.Contains("TRD1") && gn.Contains("WSUC") ) { // TRD1 for WSUC facility
      // 17-may-05 - just BOX
      envelopA[0] = 26;
      envelopA[1] = 15;
      envelopA[2] = 30;
      gMC->Gsvolu("XEN1", "BOX", idtmed[idSC], envelopA, 3) ;
      ENVELOP1.Set(3);
      for(int i=0; i<3; i++) ENVELOP1[i] = envelopA[i]; // 23-may-05  
      // Position the EMCAL Mother Volume (XEN1) in WSUC  
      gMC->Gspos("XEN1", 1, "WSUC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    } else { 
      envelopA[0] = geom->GetArm1PhiMin();                         // minimum phi angle
      envelopA[1] = geom->GetArm1PhiMax() - geom->GetArm1PhiMin(); // angular range in phi
      envelopA[2] = geom->GetNPhi();                               // number of sections in phi
      envelopA[3] = 2;                                             // 2 z coordinates
      envelopA[4] = geom->ZFromEtaR(geom->GetEnvelop(1),
				   geom->GetArm1EtaMin());       // z coordinate 1
    //add some padding for mother volume
      envelopA[5] = geom->GetEnvelop(0) ;                          // rmin at z1
      envelopA[6] = geom->GetEnvelop(1) ;                          // rmax at z1
      envelopA[7] = geom->ZFromEtaR(geom->GetEnvelop(1),
				  geom->GetArm1EtaMax());        // z coordinate 2
      envelopA[8] = envelopA[5] ;                                  // radii are the same.
      envelopA[9] = envelopA[6] ;                                  // radii are the same.

      if(gn.Contains("SHISH")) envelopA[2] = geom->GetNPhiSuperModule();

      gMC->Gsvolu("XEN1", "PGON ", idtmed[1599], envelopA, 10) ;   // Polygone filled with air 
      ENVELOP1.Set(10, envelopA);
      if (gDebug==2) {
        printf("CreateGeometry: XEN1 = %f, %f\n", envelopA[5], envelopA[6]); 
        printf("CreateGeometry: XU0 = %f, %f\n", envelopA[5], envelopA[6]); 
      }
    // Position the EMCAL Mother Volume (XEN1) in Alice (ALIC)  
      gMC->Gspos("XEN1", 1, "ALIC", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    }

    if(gn.Contains("SHISH")){
      // Compact and twist
      CreateShishKebabGeometry();
      return;
    }

    if (AliLog::GetGlobalDebugLevel()>=2) {
      printf("CreateGeometry: XEN1 = %f, %f\n", envelopA[5], envelopA[6]); 
      printf("CreateGeometry: XU0 = %f, %f\n", envelopA[5], envelopA[6]); 
    }

    // Create mini-envelopes which will contain the Tower scintillator-radiator
    
    TString label ;
    
    envelopA[5] = envelopA[5] + geom->GetGap2Active() // we are at the first scintllator
      + geom->GetAlFrontThickness();                  // rmin at z1
    envelopA[6] = envelopA[5] ;


    Int_t i ; 

    Int_t nLayers = geom->GetNECLayers();

    for (i = 0; i < (nLayers-1); i++ ){
	label = "XU" ;
	label += i ;
	Float_t tseg ; 
	tseg = geom->GetECScintThick()+geom->GetECPbRadThick();       // thickness of scintillator+Pb in E Cal
	envelopA[5] = envelopA[6] ;                                   // rmin at z1
	envelopA[4] = geom->ZFromEtaR(envelopA[5] + tseg,
				      geom->GetArm1EtaMin());         // z coordinate 1
	envelopA[7] = geom->ZFromEtaR(envelopA[5] + tseg,
				      geom->GetArm1EtaMax());         // z coordinate 2
	envelopA[6] = envelopA[5] + tseg ;                            // rmax at z1
	envelopA[8] = envelopA[5] ;                                   // radii are the same.
	envelopA[9] = envelopA[6] ;                                   // radii are the same.
 
	gMC->Gsvolu(label.Data(), "PGON", idtmed[1599], envelopA, 10);// Polygone filled with air 

	// Position XUi in XEN1
	
	gMC->Gspos(label.Data(), 1, "XEN1", 0.0, 0.0, 0.0, idrotm, "ONLY") ;

	if (AliLog::GetGlobalDebugLevel() >= 2)
	  printf("CreateGeometry: XU%d = %f, %f\n", i, envelopA[5], envelopA[6]); 

    } // end  i
 
  
    // Create one mini-envelope which will contain the last scintillator XU(nlayers-1) because there is one more scintillator than Pb layer XU(nlayers-1)

    label = "XU" ;
    label += i ;
    envelopA[5] = envelopA[6] ;                                   // rmin at z1
    envelopA[4] = geom->ZFromEtaR(envelopA[5] + geom->GetECScintThick(),
				  geom->GetArm1EtaMin());         // z coordinate 1
    envelopA[7] = geom->ZFromEtaR(envelopA[5] + geom->GetECScintThick(),
				  geom->GetArm1EtaMax());         // z coordinate 2
    envelopA[6] = envelopA[5] + geom->GetECScintThick() ;         // rmax at z1
    envelopA[8] = envelopA[5] ;                                   // radii are the same.
    envelopA[9] = envelopA[6] ;                                   // radii are the same.

    gMC->Gsvolu(label.Data(), "PGON", idtmed[1599], envelopA, 10); // Polygone filled with air

    // Position the last minienvelope in XEN1
  
    gMC->Gspos(label.Data(), 1, "XEN1", 0.0, 0.0, 0.0, idrotm, "ONLY") ;
    
    if(AliLog::GetGlobalDebugLevel() >= 2) 
    printf("CreateGeometry: XEN%d = %f, %f\n", i, envelopA[5], envelopA[6]);
  
    // Create the shapes of active material (LEAD/Aluminium/Scintillator)
    // to be placed
    Float_t envelopB[10]; // First Layer of Aluminium
    Float_t envelopC[10]; // Scintillator Layers
    Float_t envelopD[10]; // Lead Layers

    envelopC[0] = envelopD[0] = envelopB[0] = envelopA[0] ;  // starting position in Phi
    envelopC[1] = envelopD[1] = envelopB[1] = envelopA[1] ;  // angular range in phi          
    envelopC[2] = envelopD[2] = envelopB[2] = envelopA[2] ;  // number of sections in Phi
    envelopC[3] = envelopD[3] = envelopB[3] = envelopA[3] ;  // 2 z coordinates

    Float_t dist = geom->GetEnvelop(0) + geom->GetAlFrontThickness() + geom->GetGap2Active() ; 
    envelopB[4] = geom->ZFromEtaR(dist,
				  geom->GetArm1EtaMin());   // z co-ordinate 1
    envelopB[5] = geom->GetEnvelop(0) ;                     // rmin at z1
    envelopB[6] = envelopB[5] + geom->GetAlFrontThickness();// rmax at z1
    envelopB[7] = geom->ZFromEtaR(dist,
				  geom->GetArm1EtaMax());   // z co-ordinate 2
    envelopB[8] = envelopB[5] ;                             // radii are the same.
    envelopB[9] = envelopB[6] ;                             // radii are the same.

    // Define active volumes completely
    
    gMC->Gsvolu("XALU", "PGON", idtmed[1602], envelopB, 10); // PGON filled with Al
    
    gMC->Gspos("XALU", 1, "XEN1", 0.0, 0.0, 0.0 , idrotm, "ONLY") ; // Position Aluminium Layer in XEN1

    gMC->Gsvolu("XPST", "PGON", idtmed[1601], dum, 0);      // PGON filled with Scintillator (shape to be defined by GSPOSP)
  
    gMC->Gsvolu("XPBX", "PGON", idtmed[1600], dum, 0);      // PGON filled with Lead (shape to be defined by GSPOSP)
  
    //gMC->Gsvolu("XCUX", "PGON", idtmed[1603], dum, 0);      // PGON filled with Copper (shape to be defined by GSPOSP)

    gMC->Gsdvn("XPHI", "XPST", geom->GetNPhi(), 2);         // Divide eta section of scintillators into phi segments.
 
    // Position alternatively scintillator and  Lead Layers in XUi.

    envelopD[6] = envelopB[6] + geom->GetGap2Active() ;// gap between Al layer and XU0
    
    for (int i = 0; i < nLayers; i++ ){
      label = "XU" ;
      label += i  ; // we will place one layer in each mini envelope)	

      Float_t scthick ; // scintillator thickness 
      scthick = geom->GetECScintThick() ;

      envelopC[5] = envelopD[6] ;           //rmin
      envelopC[6] = envelopC[5] + scthick ; //rmax
      envelopC[8] = envelopC[5] ;           //rmin
      envelopC[9] = envelopC[6] ;           //rmax

      if(AliLog::GetGlobalDebugLevel() >= 2 ) 
	printf("CreateGeometry: volume = %s, name = XPST thickness = %f deb = %f/%f fin = %f/%f", label.Data(), scthick, envelopC[5], envelopC[8], envelopC[6], envelopC[9]) ; 

      for (int j =0; j < (geom->GetNEta()) ; j++){
	etamin = geom->GetArm1EtaMin()+
	  (j*geom->GetDeltaEta());
	etamax = geom->GetArm1EtaMin()+
	  ((j+1)*geom->GetDeltaEta());
	envelopC[4] = geom->ZFromEtaR(envelopC[5],etamin); //z begin  
	envelopC[7] = geom->ZFromEtaR(envelopC[5],etamax);// z end 
	
	gMC->Gsposp("XPST",1+j+i*(geom->GetNEta()), label.Data(), 
		    0.0, 0.0, 0.0 , idrotm, "ONLY", envelopC, 10); // Position and define layer
      } // end for j
      
	Float_t radthick ; // radiator thickness 
	TString radname ;  // radiator name
	radthick = geom->GetECPbRadThick();
	radname  =  "XPBX" ; 

	if ( i < nLayers -1 ) { // except for the last XU which contains only one scintillator layer 

	  envelopD[5] = envelopC[6] ; //rmin
	  envelopD[8] = envelopD[5] ; //rmin
	  envelopD[6] = envelopD[5] + radthick ; // rmax
	  envelopD[9] = envelopD[6] ; //rmax
	  
	  if(AliLog::GetGlobalDebugLevel() >= 2 ) 
	    printf("CreateGeometry: volume = %s, name = %s thickness = %f deb = %f/%f fin = %f/%f", label.Data(), radname.Data(), radthick, envelopD[5], envelopD[8], envelopD[6], envelopD[9]) ; 

	  for (int j =0; j < (geom->GetNEta()) ; j++){
	    etamin = geom->GetArm1EtaMin()+
	      (j*geom->GetDeltaEta());
	    etamax = geom->GetArm1EtaMin()+
	      ((j+1)*geom->GetDeltaEta());
	    envelopD[4] = geom->ZFromEtaR(envelopD[5],etamin);//z begin  
	    envelopD[7] = geom->ZFromEtaR(envelopD[5],etamax);// z end
	    
	    // Position and Define Layer
	    
	    gMC->Gsposp(radname.Data(),1+j+i*(geom->GetNEta()), label.Data(), 
			0.0, 0.0, 0.0 , idrotm, "ONLY", envelopD, 10);
	     } // end for j
	  } // if not last layer
    }  // for i
}

//______________________________________________________________________
void AliEMCALv0::Init(void)
{
    // Just prints an information message
  
  if(AliLog::GetGlobalDebugLevel()>0) { 
    TString message("\n") ; 
    message += "*****************************************\n" ;
    
    // Here the EMCAL initialisation code (if any!)
    
    AliEMCALGeometry * geom = GetGeometry() ; 
    
    if (geom!=0) {   
      message += "AliEMCAL " ; 
      message += Version() ; 
      message += "EMCAL geometry initialized for " ; 
      message += geom->GetName()  ;
    }
    else {
      message += "AliEMCAL " ; 
      message += Version() ;  
      message += "EMCAL geometry initialization failed !" ; 
    }
    message += "\n*****************************************" ;
    printf(message.Data() ) ; 
  }
}

// 24-aug-04 by PAI
void AliEMCALv0::CreateShishKebabGeometry()
{  // TWIST, TRD1 and TRD2 
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  // see AliModule::fIdtmed
  //  idtmed = fIdtmed->GetArray() - 1599 ; // see AliEMCAL::::CreateMaterials()
  //  int idAIR=1599, idPB = 1600, idSC = 1601, idSTEEL = 1603;
  //  idAL = 1602;
  Double_t par[10], xpos=0., ypos=0., zpos=0.;

  CreateSmod("XEN1");        // 18-may-05 

  CreateEmod("SMOD","EMOD"); // 18-may-05

  if(gn.Contains("110DEG")) CreateEmod("SM10","EMOD"); // 12-oct-05

  // Sensitive SC  (2x2 tiles)
  double parSCM0[5], *dummy = 0, parTRAP[11];
  Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad(), tanTmp = TMath::Tan(trd1Angle/2.);
   if(!gn.Contains("TRD")) { // standard module
    par[0] = (g->GetECPbRadThick()+g->GetECScintThick())*g->GetNECLayers()/2.;
    par[1] = par[2] = g->GetPhiTileSize();   // Symetric case
    gMC->Gsvolu("SCM0", "BOX", idtmed[idSC], par, 3); // 2x2 tiles
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
    // Division to tile size
    gMC->Gsdvn("SCM1","SCM0", g->GetNPHIdiv(), 2); // y-axis
    gMC->Gsdvn("SCM2","SCM1", g->GetNETAdiv(), 3); // z-axis
  // put LED to the SCM2 
    par[0] = g->GetECPbRadThick()/2.;
    par[1] = par[2] = g->GetPhiTileSize()/2.; // Symetric case
    gMC->Gsvolu("PBTI", "BOX", idtmed[idPB], par, 3);

    printf(" Pb tiles \n");
    int nr=0;
    ypos = zpos = 0.0;
    xpos = -sampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
    for(int ix=0; ix<g->GetNECLayers(); ix++){
      gMC->Gspos("PBTI", ++nr, "SCM2", xpos, ypos, zpos, 0, "ONLY") ;
    //    printf(" %i xpos %f \n", ix+1, xpos);
      xpos += sampleWidth;
    } 
    printf(" Number of Pb tiles in SCM2 %i \n", nr);
  } else if(gn.Contains("TRD1")) { // TRD1 - 30-sep-04
    if(gn.Contains("MAY05")){
      Double_t dzTmp = g->GetFrontSteelStrip()+g->GetPassiveScintThick();
      parSCM0[0] = parEMOD[0] + tanTmp*dzTmp; // dx1
      parSCM0[1] = parEMOD[1];                // dx2
      parSCM0[2] = parEMOD[2];                // dy
      for(int i=0; i<3; i++) parSCM0[i] -= g->GetLateralSteelStrip();
      parSCM0[3] = parEMOD[3] - dzTmp/2.; // dz

      gMC->Gsvolu("SCM0", "TRD1", idtmed[idAIR], parSCM0, 4);
      gMC->Gspos("SCM0", 1, "EMOD", 0., 0., dzTmp/2., 0, "ONLY") ;
    } else { // before MAY 2005
      double wallThickness = g->GetPhiModuleSize()/2. -  g->GetPhiTileSize(); // Need check
      for(int i=0; i<3; i++) parSCM0[i] = parEMOD[i] - wallThickness;
      parSCM0[3] = parEMOD[3];
      gMC->Gsvolu("SCM0", "TRD1", idtmed[idAIR], parSCM0, 4);
      gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
    }

    if(g->GetNPHIdiv()==2 && g->GetNETAdiv()==2) {
    // Division to tile size - 1-oct-04
      printf("<I> Divide SCM0 on y-axis %i\n", g->GetNETAdiv());
      gMC->Gsdvn("SCMY","SCM0", g->GetNETAdiv(), 2); // y-axis
    // Trapesoid 2x2
      parTRAP[0] = parSCM0[3];    // dz
      parTRAP[1] = TMath::ATan2((parSCM0[1]-parSCM0[0])/2.,2.*parSCM0[3])*180./TMath::Pi(); // theta
      parTRAP[2] = 0.;           // phi
      // bottom
      parTRAP[3] = parSCM0[2]/2.; // H1
      parTRAP[4] = parSCM0[0]/2.; // BL1
      parTRAP[5] = parTRAP[4];    // TL1
      parTRAP[6] = 0.0;           // ALP1
      // top
      parTRAP[7] = parSCM0[2]/2.; // H2
      parTRAP[8] = parSCM0[1]/2.; // BL2
      parTRAP[9] = parTRAP[8];    // TL2
      parTRAP[10]= 0.0;           // ALP2
      printf(" ** TRAP ** \n");
      for(int i=0; i<11; i++) printf(" par[%2.2i] %9.4f\n", i, parTRAP[i]);

      gMC->Gsvolu("SCMX", "TRAP", idtmed[idSC], parTRAP, 11);
      xpos = +(parSCM0[1]+parSCM0[0])/4.;
      gMC->Gspos("SCMX", 1, "SCMY", xpos, 0.0, 0.0, 0, "ONLY") ;

      // Using rotation because SCMX should be the same due to Pb tiles
      xpos = -xpos; 
      AliMatrix(idrotm, 90.0,180., 90.0, 270.0, 0.0,0.0) ;
      gMC->Gspos("SCMX", 2, "SCMY", xpos, 0.0, 0.0, idrotm, "ONLY");
    // put LED to the SCM0 
      AliEMCALShishKebabTrd1Module *mod = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(0);
      gMC->Gsvolu("PBTI", "BOX", idtmed[idPB], dummy, 0);

      par[1] = parSCM0[2]/2;            // y 
      par[2] = g->GetECPbRadThick()/2.; // z

      int nr=0;
      ypos = 0.0;
      zpos = -sampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
      double xCenterSCMX =  (parTRAP[4] +  parTRAP[8])/2.;
      if(!gn.Contains("NOPB")) { // for testing - 11-jul-05
        printf(" Pb tiles \n");
        for(int iz=0; iz<g->GetNECLayers(); iz++){
          par[0] = (parSCM0[0] + mod->GetTanBetta()*sampleWidth*iz)/2.;
          xpos   = par[0] - xCenterSCMX;
          gMC->Gsposp("PBTI", ++nr, "SCMX", xpos, ypos, zpos, 0, "ONLY", par, 3) ;
          printf(" %i xpos %f zpos %f par[0] %f \n", iz+1, xpos, zpos, par[0]);
          zpos += sampleWidth;
        } 
        printf(" Number of Pb tiles in SCMX %i \n", nr);
      }
    } else if(g->GetNPHIdiv()==3 && g->GetNETAdiv()==3) {
      Trd1Tower3X3(parSCM0);
    } else if(g->GetNPHIdiv()==4 && g->GetNETAdiv()==4) {
      Trd1Tower4X4();
    }
  } else if(gn.Contains("TRD2")) {    // TRD2 - 14-jan-05
    //    Scm0InTrd2(g, parEMOD, parSCM0); // First dessin 
    PbmoInTrd2(g, parEMOD, parSCM0); // Second dessin 
  }
}

void AliEMCALv0::CreateSmod(const char* mother)
{ // 18-may-05; mother="XEN1"; 
  // child="SMOD" from first to 10th, "SM10" (11th and 12th) (TRD1 case)
  // child="SMON" and "SMOP"("TRD2" case)
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper();

  Double_t par[3], parTubs[5], xpos=0., ypos=0., zpos=0., rpos=0., dphi=0., phi=0.0, phiRad=0.;
  Double_t par1C = 0.;
  //  ===== define Super Module from air - 14x30 module ==== ;
  sampleWidth = double(g->GetECPbRadThick()+g->GetECScintThick());
  printf("\n ## Super Module | sampleWidth %5.3f ## %s \n", sampleWidth, gn.Data());
  par[0] = g->GetShellThickness()/2.;
  par[1] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
  par[2] = g->GetEtaModuleSize()*15.; 
{'fraction_non_alphanumeric': 0.19158523440122988, 'fraction_numerical': 0.03767383506211235, 'mean_word_length': 2.243652203817275, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 417, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 62, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2315994', 'n_tokens_mistral': 22756, 'n_tokens_neox': 20982, 'n_words': 4594}