<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

// --- ROOT system ---
#include <cassert>

#include <TGeometry.h>
#include <TGeoPhysicalNode.h>
#include <TGeoManager.h>
#include <TGeoMatrix.h>
#include <TVirtualMC.h>
#include <TArrayI.h>
#include <TROOT.h>
#include <TList.h>
#include <TVector2.h>
#include <cassert>

//--- EMCAL system---
#include "AliEMCALShishKebabTrd1Module.h"

// --- Standard library ---

//#include <stdio.h>

// --- AliRoot header files ---

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

ClassImp(AliEMCALv0)

// EMCAL material: look to the AliEMCAL.cxx
enum
 {
  kIdAIR   = 1599, 
  kIdPB    = 1600, 
  kIdSC    = 1601, 
  kIdAL    = 1602, 
  kIdSTEEL = 1603,
  kIdPAPER = 1604
 };


//______________________________________________________________________
AliEMCALv0::AliEMCALv0()
  : AliEMCAL(),
    fShishKebabModules(),fEnvelop1(0),fIdRotm(0),fIdTmedArr(0),
    fSampleWidth(0),fSmodPar0(0),fSmodPar1(0),fSmodPar2(0),fCalFrame(0)
{
  //default ctor
  for(Int_t i = 0; i < 5 ; i++) fParEMOD[i]=0.0;
}

//______________________________________________________________________
AliEMCALv0::AliEMCALv0(const char *name, const char *title)
  : AliEMCAL(name,title),
    fShishKebabModules(),fEnvelop1(0),fIdRotm(0),fIdTmedArr(0),
    fSampleWidth(0),fSmodPar0(0),fSmodPar1(0),fSmodPar2(0),fCalFrame(0)
{
  // ctor : title is used to identify the layout
  // Apr 25, 2006
  // Nov 22, 2006 - case of 1X1  
  
  for(Int_t i = 0; i < 5 ; i++) fParEMOD[i]=0.0;
  TString ntmp(GetTitle());
  ntmp.ToUpper();

  AliEMCALGeometry *g = GetGeometry() ; 
  TString gn(g->GetName()); gn.ToUpper();
  fShishKebabModules = g->GetShishKebabTrd1Modules(); 
  fGeometry = g;
  fSampleWidth = double(g->GetECPbRadThick()+g->GetECScintThick());
  if(gn.Contains("FIRSTYEARV1")) fSampleWidth += 2.*g->GetTrd1BondPaperThick();
  printf("<I> AliEMCALv0::AliEMCALv : fGeometry %p : gMC %p : fSampleWidth %5.4f\n", 
	 fGeometry, gMC, fSampleWidth);
}

//______________________________________________________________________
void AliEMCALv0::CreateGeometry()
{
  // Create the EMCAL geometry for Geant
  // Geometry of a tower
  
  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
  fIdTmedArr = fIdtmed->GetArray() - 1599 ;
  
  fIdRotm = 1;
  //  gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3) - see AliModule
  AliMatrix(fIdRotm, 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("WSUC") ) { // TRD1 for WSUC facility
    // 17-may-05 - just BOX
    envelopA[0] = 26;
    envelopA[1] = 15;
    envelopA[2] = 30;
    gMC->Gsvolu("XEN1", "BOX", fIdTmedArr[kIdSC], envelopA, 3) ;
    fEnvelop1.Set(3);
    for(int i=0; i<3; i++) fEnvelop1[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, fIdRotm, "ONLY") ;
  } else { 
    envelopA[0] = geom->GetArm1PhiMin();                         // minimum phi angle
    envelopA[1] = geom->GetArm1PhiMax() - geom->GetArm1PhiMin(); // angular range in phi
    envelopA[2] = geom->GetNPhiSuperModule();                    // number of sections in phi
    envelopA[3] = 2;                                             // 2 z coordinates
    envelopA[4] = -geom->GetEnvelop(2)/2.;                       // zmin - includes padding
    envelopA[5] = geom->GetEnvelop(0) ;                          // rmin at z1 - includes padding
    envelopA[6] = geom->GetEnvelop(1) ;                          // rmax at z1 - includes padding
    envelopA[7] = geom->GetEnvelop(2)/2.;                        // zmax includes padding
    
    envelopA[8] = envelopA[5] ;                                  // radii are the same.
    envelopA[9] = envelopA[6] ;                                  // radii are the same.

    gMC->Gsvolu("XEN1", "PGON", fIdTmedArr[kIdAIR], envelopA, 10) ;   // Polygone filled with air 
    fEnvelop1.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(geom->GetNameOfEMCALEnvelope(), 1, "ALIC", 0.0, 0.0, 0.0, fIdRotm, "ONLY") ;
  }

  // COMPACT, TRD1
  AliDebug(2,Form("Shish-Kebab geometry : %s", GetTitle())); 
  CreateShishKebabGeometry();

  //Space Frame
  AliDebug(2,"Creating EMCAL Space Frame");
  fCalFrame = new AliEMCALSpaceFrame();
  fCalFrame->CreateGeometry();

}

//______________________________________________________________________
void AliEMCALv0::Init(void)
{
    // Just prints an information message
  AliEMCAL::Init();
  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("%s",message.Data() ) ; 
  }
}

// 24-aug-04 by PAI
//______________________________________________________________________
void AliEMCALv0::CreateShishKebabGeometry()
{  
  // Oct 26,2010
  // TRD1
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad(), tanTrd1 = TMath::Tan(trd1Angle/2.);
  // see AliModule::fFIdTmedArr
  //  fIdTmedArr = fIdtmed->GetArray() - 1599 ; // see AliEMCAL::::CreateMaterials()
  //  int kIdAIR=1599, kIdPB = 1600, kIdSC = 1601, kIdSTEEL = 1603;
  //  idAL = 1602;
  Double_t par[10], xpos=0., ypos=0., zpos=0.;

  CreateSmod(g->GetNameOfEMCALEnvelope());

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

  if(g->GetKey110DEG()) CreateEmod("SM10","EMOD"); // Nov 1,2006 

  // Sensitive SC  (2x2 tiles)
  double parSCM0[5]={0,0,0,0}, *dummy = 0, parTRAP[11];

  if(!gn.Contains("FIRSTYEARV1")) {
    double wallThickness = g->GetPhiModuleSize()/g->GetNPHIdiv() -  g->GetPhiTileSize();
    for(int i=0; i<3; i++) parSCM0[i] = fParEMOD[i] - wallThickness;
    parSCM0[3] = fParEMOD[3];
    gMC->Gsvolu("SCM0", "TRD1", fIdTmedArr[kIdAIR], parSCM0, 4);
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
  } else {
    double wTh = g->GetLateralSteelStrip();
    parSCM0[0] = fParEMOD[0] - wTh + tanTrd1*g->GetTrd1AlFrontThick();
    parSCM0[1] = fParEMOD[1] - wTh;
    parSCM0[2] = fParEMOD[2] - wTh;
    parSCM0[3] = fParEMOD[3] - g->GetTrd1AlFrontThick()/2.;
    gMC->Gsvolu("SCM0", "TRD1", fIdTmedArr[kIdAIR], parSCM0, 4);
    double zshift = g->GetTrd1AlFrontThick()/2.;
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., zshift, 0, "ONLY");
    // 
    CreateAlFrontPlate("EMOD","ALFP");
  }

  if(g->GetNPHIdiv()==2 && g->GetNETAdiv()==2) {
    // Division to tile size - 1-oct-04
    AliDebug(2,Form(" 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
    AliDebug(2,Form(" ** TRAP ** \n"));
    for(int i=0; i<11; i++) AliDebug(3, Form(" par[%2.2i] %9.4f\n", i, parTRAP[i]));
    
    gMC->Gsvolu("SCMX", "TRAP", fIdTmedArr[kIdSC], 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(fIdRotm, 90.0,180., 90.0, 270.0, 0.0,0.0) ;
    gMC->Gspos("SCMX", 2, "SCMY", xpos, 0.0, 0.0, fIdRotm, "ONLY");
    // put LED to the SCM0 
    AliEMCALShishKebabTrd1Module *mod = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(0);
    Double_t tanBetta = mod->GetTanBetta();

    int nr=0; 
    ypos = 0.0; 
    double xCenterSCMX =  (parTRAP[4] +  parTRAP[8])/2.;
    if(!gn.Contains("FIRSTYEARV1")) {
      par[1] = parSCM0[2]/2;            // y 
      par[2] = g->GetECPbRadThick()/2.; // z
      gMC->Gsvolu("PBTI", "BOX", fIdTmedArr[kIdPB], dummy, 0);
      zpos = -fSampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
      AliDebug(2,Form(" Pb tiles \n"));
      for(int iz=0; iz<g->GetNECLayers(); iz++){
        par[0] = (parSCM0[0] + tanBetta*fSampleWidth*iz)/2.;
        xpos   = par[0] - xCenterSCMX;
        gMC->Gsposp("PBTI", ++nr, "SCMX", xpos, ypos, zpos, 0, "ONLY", par, 3) ;
        AliDebug(3,Form(" %i xpos %f zpos %f par[0] %f \n", iz+1, xpos, zpos, par[0]));
        zpos += fSampleWidth;
      } 
      AliDebug(2,Form(" Number of Pb tiles in SCMX %i \n", nr));
    } else {
      // Oct 26, 2010
      // First sheet of paper
      par[1] = parSCM0[2]/2.;                 // y 
      par[2] = g->GetTrd1BondPaperThick()/2.; // z
      par[0] = parSCM0[0]/2.;                 // x 
      gMC->Gsvolu("PAP1", "BOX", fIdTmedArr[kIdPAPER], par, 3);
      xpos = par[0] - xCenterSCMX;
      zpos = -parSCM0[3] + g->GetTrd1BondPaperThick()/2.;
      gMC->Gspos("PAP1", 1, "SCMX", xpos, ypos, zpos, 0, "ONLY");
      for(int iz=0; iz<g->GetNECLayers()-1; iz++){
        nr = iz + 1;
        Double_t dz = g->GetECScintThick() + g->GetTrd1BondPaperThick() + fSampleWidth*iz;
	// PB + 2 paper sheets
        par[2] = g->GetECPbRadThick()/2. + g->GetTrd1BondPaperThick(); // z
        par[0] = (parSCM0[0] + tanBetta*dz)/2.;
        TString pa(Form("PA%2.2i",nr));
	gMC->Gsvolu(pa.Data(), "BOX", fIdTmedArr[kIdPAPER], par, 3);
        xpos   = par[0] - xCenterSCMX;
        zpos   = -parSCM0[3] + dz + par[2];
        gMC->Gspos(pa.Data(), 1, "SCMX", xpos, ypos, zpos, 0, "ONLY") ;
	// Pb
        TString pb(Form("PB%2.2i",nr));
        par[2] = g->GetECPbRadThick()/2.; // z
	gMC->Gsvolu(pb.Data(), "BOX", fIdTmedArr[kIdPB], par, 3);
        gMC->Gspos(pb.Data(), 1, pa.Data(), 0.0, 0.0, 0.0, 0, "ONLY") ;
      } 
    }
    
  } else if(g->GetNPHIdiv()==3 && g->GetNETAdiv()==3) {
    printf(" before AliEMCALv0::Trd1Tower3X3() : parSCM0");
    for(int i=0; i<4; i++) printf(" %7.4f ", parSCM0[i]);
    printf("\n"); 
    Trd1Tower3X3(parSCM0);
  } else if(g->GetNPHIdiv()==1 && g->GetNETAdiv()==1) {
    // no division in SCM0
    Trd1Tower1X1(parSCM0);
  } else if(g->GetNPHIdiv()==4 && g->GetNETAdiv()==4) {
    Trd1Tower4X4();
  }
  
}

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

  Double_t par[3], 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 ==== ;
  AliDebug(2,Form("\n ## Super Module | fSampleWidth %5.3f ## %s \n", fSampleWidth, gn.Data()));
  par[0] = g->GetShellThickness()/2.;
  par[1] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
  par[2] = g->GetEtaModuleSize()*15.; 
  fIdRotm=0;
  int nphism = g->GetNumberOfSuperModules()/2; // 20-may-05
  if(nphism>0) {
    dphi = (g->GetArm1PhiMax() - g->GetArm1PhiMin())/nphism;
    //    if(g->GetKey110DEG()) dphi = (g->GetArm1PhiMax() - g->GetArm1PhiMin())/(nphism-1);
    rpos = (g->GetEnvelop(0) + g->GetEnvelop(1))/2.;
    AliDebug(2,Form(" rpos %8.2f : dphi %6.1f degree \n", rpos, dphi));
  }

  if(gn.Contains("WSUC")) {
    par[0] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
    par[1] = g->GetShellThickness()/2.;
    par[2] = g->GetEtaModuleSize()*g->GetNZ()/2. + 5; 

    gMC->Gsvolu("SMOD", "BOX", fIdTmedArr[kIdAIR], par, 3);

    AliDebug(2,Form("SMOD in WSUC : tmed %i | dx %7.2f dy %7.2f dz %7.2f (SMOD, BOX)\n", 
		    fIdTmedArr[kIdAIR], par[0],par[1],par[2]));
    fSmodPar0 = par[0]; 
    fSmodPar1 = par[1];
    fSmodPar2 = par[2];
    nphism   =  g->GetNumberOfSuperModules();
  } else {
    par[2]  = 350./2.; // 11-oct-04 - for 26 division
    AliDebug(2,Form(" par[0] %7.2f (old) \n",  par[0]));
    Float_t *parSM = g->GetSuperModulesPars(); 
    for(int i=0; i<3; i++) par[i] = parSM[i];
  }
  gMC->Gsvolu("SMOD", "BOX", fIdTmedArr[kIdAIR], par, 3);
  AliDebug(2,Form("tmed %i | dx %7.2f dy %7.2f dz %7.2f (SMOD, BOX)\n", 
		  fIdTmedArr[kIdAIR], par[0],par[1],par[2]));
  fSmodPar0 = par[0]; 
  fSmodPar2 = par[2];
  if(g->GetKey110DEG()) { // 12-oct-05
    par1C = par[1];
    par[1] /= 2.;
    gMC->Gsvolu("SM10", "BOX", fIdTmedArr[kIdAIR], par, 3);
    AliDebug(2,Form(" Super module with name \"SM10\" was created too par[1] = %f\n", par[1]));
    par[1] = par1C;
  }
  // Steel plate
  if(g->GetSteelFrontThickness() > 0.0) { // 28-mar-05
    par[0] = g->GetSteelFrontThickness()/2.;
    gMC->Gsvolu("STPL", "BOX", fIdTmedArr[kIdSTEEL], par, 3);
    printf("tmed %i | dx %7.2f dy %7.2f dz %7.2f (STPL) \n", fIdTmedArr[kIdSTEEL], par[0],par[1],par[2]);
    xpos = -(g->GetShellThickness() - g->GetSteelFrontThickness())/2.;
    gMC->Gspos("STPL", 1, "SMOD", xpos, 0.0, 0.0, 0, "ONLY") ;
  }

  int nr=0, nrsmod=0, i0=0;

  // Turn whole super module
  for(int i=i0; i<nphism; i++) {
    if(gn.Contains("WSUC")) {
      xpos = ypos = zpos = 0.0;
      fIdRotm = 0;
      gMC->Gspos("SMOD", 1, mother, xpos, ypos, zpos, fIdRotm, "ONLY") ;
      printf(" fIdRotm %3i phi %6.1f(%5.3f) xpos %7.2f ypos %7.2f zpos %7.2f \n", 
      fIdRotm, phi, phiRad, xpos, ypos, zpos);
      nr++;
    } else { // TRD1 
      TString smName("SMOD"); // 12-oct-05
      if(i==5 && g->GetKey110DEG()) {
        smName = "SM10";
        nrsmod = nr;
        nr     = 0;
      }
      phi    = g->GetArm1PhiMin() + dphi*(2*i+1)/2.; // phi= 70, 90, 110, 130, 150, 170
      phiRad = phi*TMath::Pi()/180.;

      AliMatrix(fIdRotm, 90.0, phi, 90.0, 90.0+phi, 0.0, 0.0);

      xpos = rpos * TMath::Cos(phiRad);
      ypos = rpos * TMath::Sin(phiRad);
      zpos = fSmodPar2; // 21-sep-04
      if(i==5 && g->GetKey110DEG()) {
        xpos += (par1C/2. * TMath::Sin(phiRad)); 
        ypos -= (par1C/2. * TMath::Cos(phiRad)); 
      }
      
      // 1th module in z-direction;
      gMC->Gspos(smName.Data(), ++nr, mother, xpos, ypos, zpos, fIdRotm, "ONLY") ;
      AliDebug(3, Form(" %s : %2i fIdRotm %3i phi %6.1f(%5.3f) xpos %7.2f ypos %7.2f zpos %7.2f : i %i \n", 
		       smName.Data(), nr, fIdRotm, phi, phiRad, xpos, ypos, zpos, i));
      // 2th module in z-direction;
      // turn arround X axis; 0<phi<360
      double phiy = 90. + phi + 180.;
      if(phiy>=360.) phiy -= 360.;
      
      AliMatrix(fIdRotm, 90.0, phi, 90.0, phiy, 180.0, 0.0);
      gMC->Gspos(smName.Data(), ++nr, mother, xpos, ypos, -zpos, fIdRotm, "ONLY");
      AliDebug(3, Form(" %s : %2i fIdRotm %3i phiy %6.1f  xpos %7.2f ypos %7.2f zpos %7.2f \n", 
		       smName.Data(), nr, fIdRotm, phiy, xpos, ypos, -zpos));
    }
  }
  AliDebug(2,Form(" Number of Super Modules %i \n", nr+nrsmod));
}

//______________________________________________________________________
void AliEMCALv0::CreateEmod(const char* mother, const char* child)
{ 
  // 17-may-05; mother="SMOD"; child="EMOD"
  // Oct 26,2010 
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  // Module definition
  Double_t xpos=0., ypos=0., zpos=0.;
  Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad(), tanTrd1 = TMath::Tan(trd1Angle/2.);

  if(strcmp(mother,"SMOD")==0) {<commit_msg>fixed compilation warning<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

// --- ROOT system ---
#include <cassert>

#include <TGeometry.h>
#include <TGeoPhysicalNode.h>
#include <TGeoManager.h>
#include <TGeoMatrix.h>
#include <TVirtualMC.h>
#include <TArrayI.h>
#include <TROOT.h>
#include <TList.h>
#include <TVector2.h>
#include <cassert>

//--- EMCAL system---
#include "AliEMCALShishKebabTrd1Module.h"

// --- Standard library ---

//#include <stdio.h>

// --- AliRoot header files ---

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

ClassImp(AliEMCALv0)

// EMCAL material: look to the AliEMCAL.cxx
enum
 {
  kIdAIR   = 1599, 
  kIdPB    = 1600, 
  kIdSC    = 1601, 
  kIdAL    = 1602, 
  kIdSTEEL = 1603,
  kIdPAPER = 1604
 };


//______________________________________________________________________
AliEMCALv0::AliEMCALv0()
  : AliEMCAL(),
    fShishKebabModules(),fEnvelop1(0),fIdRotm(0),fIdTmedArr(0),
    fSampleWidth(0),fSmodPar0(0),fSmodPar1(0),fSmodPar2(0),fCalFrame(0)
{
  //default ctor
  for(Int_t i = 0; i < 5 ; i++) fParEMOD[i]=0.0;
}

//______________________________________________________________________
AliEMCALv0::AliEMCALv0(const char *name, const char *title)
  : AliEMCAL(name,title),
    fShishKebabModules(),fEnvelop1(0),fIdRotm(0),fIdTmedArr(0),
    fSampleWidth(0),fSmodPar0(0),fSmodPar1(0),fSmodPar2(0),fCalFrame(0)
{
  // ctor : title is used to identify the layout
  // Apr 25, 2006
  // Nov 22, 2006 - case of 1X1  
  
  for(Int_t i = 0; i < 5 ; i++) fParEMOD[i]=0.0;
  TString ntmp(GetTitle());
  ntmp.ToUpper();

  AliEMCALGeometry *g = GetGeometry() ; 
  TString gn(g->GetName()); gn.ToUpper();
  fShishKebabModules = g->GetShishKebabTrd1Modules(); 
  fGeometry = g;
  fSampleWidth = double(g->GetECPbRadThick()+g->GetECScintThick());
  if(gn.Contains("FIRSTYEARV1")) fSampleWidth += 2.*g->GetTrd1BondPaperThick();
  printf("<I> AliEMCALv0::AliEMCALv : fGeometry %p : gMC %p : fSampleWidth %5.4f\n", 
	 fGeometry, gMC, fSampleWidth);
}

//______________________________________________________________________
void AliEMCALv0::CreateGeometry()
{
  // Create the EMCAL geometry for Geant
  // Geometry of a tower
  
  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
  fIdTmedArr = fIdtmed->GetArray() - 1599 ;
  
  fIdRotm = 1;
  //  gMC->Matrix(nmat, theta1, phi1, theta2, phi2, theta3, phi3) - see AliModule
  AliMatrix(fIdRotm, 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("WSUC") ) { // TRD1 for WSUC facility
    // 17-may-05 - just BOX
    envelopA[0] = 26;
    envelopA[1] = 15;
    envelopA[2] = 30;
    gMC->Gsvolu("XEN1", "BOX", fIdTmedArr[kIdSC], envelopA, 3) ;
    fEnvelop1.Set(3);
    for(int i=0; i<3; i++) fEnvelop1[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, fIdRotm, "ONLY") ;
  } else { 
    envelopA[0] = geom->GetArm1PhiMin();                         // minimum phi angle
    envelopA[1] = geom->GetArm1PhiMax() - geom->GetArm1PhiMin(); // angular range in phi
    envelopA[2] = geom->GetNPhiSuperModule();                    // number of sections in phi
    envelopA[3] = 2;                                             // 2 z coordinates
    envelopA[4] = -geom->GetEnvelop(2)/2.;                       // zmin - includes padding
    envelopA[5] = geom->GetEnvelop(0) ;                          // rmin at z1 - includes padding
    envelopA[6] = geom->GetEnvelop(1) ;                          // rmax at z1 - includes padding
    envelopA[7] = geom->GetEnvelop(2)/2.;                        // zmax includes padding
    
    envelopA[8] = envelopA[5] ;                                  // radii are the same.
    envelopA[9] = envelopA[6] ;                                  // radii are the same.

    gMC->Gsvolu("XEN1", "PGON", fIdTmedArr[kIdAIR], envelopA, 10) ;   // Polygone filled with air 
    fEnvelop1.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(geom->GetNameOfEMCALEnvelope(), 1, "ALIC", 0.0, 0.0, 0.0, fIdRotm, "ONLY") ;
  }

  // COMPACT, TRD1
  AliDebug(2,Form("Shish-Kebab geometry : %s", GetTitle())); 
  CreateShishKebabGeometry();

  //Space Frame
  AliDebug(2,"Creating EMCAL Space Frame");
  fCalFrame = new AliEMCALSpaceFrame();
  fCalFrame->CreateGeometry();

}

//______________________________________________________________________
void AliEMCALv0::Init(void)
{
    // Just prints an information message
  AliEMCAL::Init();
  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("%s",message.Data() ) ; 
  }
}

// 24-aug-04 by PAI
//______________________________________________________________________
void AliEMCALv0::CreateShishKebabGeometry()
{  
  // Oct 26,2010
  // TRD1
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad(), tanTrd1 = TMath::Tan(trd1Angle/2.);
  // see AliModule::fFIdTmedArr
  //  fIdTmedArr = fIdtmed->GetArray() - 1599 ; // see AliEMCAL::::CreateMaterials()
  //  int kIdAIR=1599, kIdPB = 1600, kIdSC = 1601, kIdSTEEL = 1603;
  //  idAL = 1602;
  Double_t par[10], xpos=0., ypos=0., zpos=0.;

  CreateSmod(g->GetNameOfEMCALEnvelope());

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

  if(g->GetKey110DEG()) CreateEmod("SM10","EMOD"); // Nov 1,2006 

  // Sensitive SC  (2x2 tiles)
  double parSCM0[5]={0,0,0,0}, *dummy = 0, parTRAP[11];

  if(!gn.Contains("FIRSTYEARV1")) {
    double wallThickness = g->GetPhiModuleSize()/g->GetNPHIdiv() -  g->GetPhiTileSize();
    for(int i=0; i<3; i++) parSCM0[i] = fParEMOD[i] - wallThickness;
    parSCM0[3] = fParEMOD[3];
    gMC->Gsvolu("SCM0", "TRD1", fIdTmedArr[kIdAIR], parSCM0, 4);
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., 0., 0, "ONLY") ;
  } else {
    double wTh = g->GetLateralSteelStrip();
    parSCM0[0] = fParEMOD[0] - wTh + tanTrd1*g->GetTrd1AlFrontThick();
    parSCM0[1] = fParEMOD[1] - wTh;
    parSCM0[2] = fParEMOD[2] - wTh;
    parSCM0[3] = fParEMOD[3] - g->GetTrd1AlFrontThick()/2.;
    gMC->Gsvolu("SCM0", "TRD1", fIdTmedArr[kIdAIR], parSCM0, 4);
    double zshift = g->GetTrd1AlFrontThick()/2.;
    gMC->Gspos("SCM0", 1, "EMOD", 0., 0., zshift, 0, "ONLY");
    // 
    CreateAlFrontPlate("EMOD","ALFP");
  }

  if(g->GetNPHIdiv()==2 && g->GetNETAdiv()==2) {
    // Division to tile size - 1-oct-04
    AliDebug(2,Form(" 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
    AliDebug(2,Form(" ** TRAP ** \n"));
    for(int i=0; i<11; i++) AliDebug(3, Form(" par[%2.2i] %9.4f\n", i, parTRAP[i]));
    
    gMC->Gsvolu("SCMX", "TRAP", fIdTmedArr[kIdSC], 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(fIdRotm, 90.0,180., 90.0, 270.0, 0.0,0.0) ;
    gMC->Gspos("SCMX", 2, "SCMY", xpos, 0.0, 0.0, fIdRotm, "ONLY");
    // put LED to the SCM0 
    AliEMCALShishKebabTrd1Module *mod = (AliEMCALShishKebabTrd1Module*)fShishKebabModules->At(0);
    Double_t tanBetta = mod->GetTanBetta();

    int nr=0; 
    ypos = 0.0; 
    double xCenterSCMX =  (parTRAP[4] +  parTRAP[8])/2.;
    if(!gn.Contains("FIRSTYEARV1")) {
      par[1] = parSCM0[2]/2;            // y 
      par[2] = g->GetECPbRadThick()/2.; // z
      gMC->Gsvolu("PBTI", "BOX", fIdTmedArr[kIdPB], dummy, 0);
      zpos = -fSampleWidth*g->GetNECLayers()/2. + g->GetECPbRadThick()/2.;
      AliDebug(2,Form(" Pb tiles \n"));
      for(int iz=0; iz<g->GetNECLayers(); iz++){
        par[0] = (parSCM0[0] + tanBetta*fSampleWidth*iz)/2.;
        xpos   = par[0] - xCenterSCMX;
        gMC->Gsposp("PBTI", ++nr, "SCMX", xpos, ypos, zpos, 0, "ONLY", par, 3) ;
        AliDebug(3,Form(" %i xpos %f zpos %f par[0] %f \n", iz+1, xpos, zpos, par[0]));
        zpos += fSampleWidth;
      } 
      AliDebug(2,Form(" Number of Pb tiles in SCMX %i \n", nr));
    } else {
      // Oct 26, 2010
      // First sheet of paper
      par[1] = parSCM0[2]/2.;                 // y 
      par[2] = g->GetTrd1BondPaperThick()/2.; // z
      par[0] = parSCM0[0]/2.;                 // x 
      gMC->Gsvolu("PAP1", "BOX", fIdTmedArr[kIdPAPER], par, 3);
      xpos = par[0] - xCenterSCMX;
      zpos = -parSCM0[3] + g->GetTrd1BondPaperThick()/2.;
      gMC->Gspos("PAP1", 1, "SCMX", xpos, ypos, zpos, 0, "ONLY");
      for(int iz=0; iz<g->GetNECLayers()-1; iz++){
        nr = iz + 1;
        Double_t dz = g->GetECScintThick() + g->GetTrd1BondPaperThick() + fSampleWidth*iz;
	// PB + 2 paper sheets
        par[2] = g->GetECPbRadThick()/2. + g->GetTrd1BondPaperThick(); // z
        par[0] = (parSCM0[0] + tanBetta*dz)/2.;
        TString pa(Form("PA%2.2i",nr));
	gMC->Gsvolu(pa.Data(), "BOX", fIdTmedArr[kIdPAPER], par, 3);
        xpos   = par[0] - xCenterSCMX;
        zpos   = -parSCM0[3] + dz + par[2];
        gMC->Gspos(pa.Data(), 1, "SCMX", xpos, ypos, zpos, 0, "ONLY") ;
	// Pb
        TString pb(Form("PB%2.2i",nr));
        par[2] = g->GetECPbRadThick()/2.; // z
	gMC->Gsvolu(pb.Data(), "BOX", fIdTmedArr[kIdPB], par, 3);
        gMC->Gspos(pb.Data(), 1, pa.Data(), 0.0, 0.0, 0.0, 0, "ONLY") ;
      } 
    }
    
  } else if(g->GetNPHIdiv()==3 && g->GetNETAdiv()==3) {
    printf(" before AliEMCALv0::Trd1Tower3X3() : parSCM0");
    for(int i=0; i<4; i++) printf(" %7.4f ", parSCM0[i]);
    printf("\n"); 
    Trd1Tower3X3(parSCM0);
  } else if(g->GetNPHIdiv()==1 && g->GetNETAdiv()==1) {
    // no division in SCM0
    Trd1Tower1X1(parSCM0);
  } else if(g->GetNPHIdiv()==4 && g->GetNETAdiv()==4) {
    Trd1Tower4X4();
  }
  
}

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

  Double_t par[3], 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 ==== ;
  AliDebug(2,Form("\n ## Super Module | fSampleWidth %5.3f ## %s \n", fSampleWidth, gn.Data()));
  par[0] = g->GetShellThickness()/2.;
  par[1] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
  par[2] = g->GetEtaModuleSize()*15.; 
  fIdRotm=0;
  int nphism = g->GetNumberOfSuperModules()/2; // 20-may-05
  if(nphism>0) {
    dphi = (g->GetArm1PhiMax() - g->GetArm1PhiMin())/nphism;
    //    if(g->GetKey110DEG()) dphi = (g->GetArm1PhiMax() - g->GetArm1PhiMin())/(nphism-1);
    rpos = (g->GetEnvelop(0) + g->GetEnvelop(1))/2.;
    AliDebug(2,Form(" rpos %8.2f : dphi %6.1f degree \n", rpos, dphi));
  }

  if(gn.Contains("WSUC")) {
    par[0] = g->GetPhiModuleSize()*g->GetNPhi()/2.; 
    par[1] = g->GetShellThickness()/2.;
    par[2] = g->GetEtaModuleSize()*g->GetNZ()/2. + 5; 

    gMC->Gsvolu("SMOD", "BOX", fIdTmedArr[kIdAIR], par, 3);

    AliDebug(2,Form("SMOD in WSUC : tmed %i | dx %7.2f dy %7.2f dz %7.2f (SMOD, BOX)\n", 
		    fIdTmedArr[kIdAIR], par[0],par[1],par[2]));
    fSmodPar0 = par[0]; 
    fSmodPar1 = par[1];
    fSmodPar2 = par[2];
    nphism   =  g->GetNumberOfSuperModules();
  } else {
    par[2]  = 350./2.; // 11-oct-04 - for 26 division
    AliDebug(2,Form(" par[0] %7.2f (old) \n",  par[0]));
    Float_t *parSM = g->GetSuperModulesPars(); 
    for(int i=0; i<3; i++) par[i] = parSM[i];
  }
  gMC->Gsvolu("SMOD", "BOX", fIdTmedArr[kIdAIR], par, 3);
  AliDebug(2,Form("tmed %i | dx %7.2f dy %7.2f dz %7.2f (SMOD, BOX)\n", 
		  fIdTmedArr[kIdAIR], par[0],par[1],par[2]));
  fSmodPar0 = par[0]; 
  fSmodPar2 = par[2];
  if(g->GetKey110DEG()) { // 12-oct-05
    par1C = par[1];
    par[1] /= 2.;
    gMC->Gsvolu("SM10", "BOX", fIdTmedArr[kIdAIR], par, 3);
    AliDebug(2,Form(" Super module with name \"SM10\" was created too par[1] = %f\n", par[1]));
    par[1] = par1C;
  }
  // Steel plate
  if(g->GetSteelFrontThickness() > 0.0) { // 28-mar-05
    par[0] = g->GetSteelFrontThickness()/2.;
    gMC->Gsvolu("STPL", "BOX", fIdTmedArr[kIdSTEEL], par, 3);
    printf("tmed %i | dx %7.2f dy %7.2f dz %7.2f (STPL) \n", fIdTmedArr[kIdSTEEL], par[0],par[1],par[2]);
    xpos = -(g->GetShellThickness() - g->GetSteelFrontThickness())/2.;
    gMC->Gspos("STPL", 1, "SMOD", xpos, 0.0, 0.0, 0, "ONLY") ;
  }

  int nr=0, nrsmod=0, i0=0;

  // Turn whole super module
  for(int i=i0; i<nphism; i++) {
    if(gn.Contains("WSUC")) {
      xpos = ypos = zpos = 0.0;
      fIdRotm = 0;
      gMC->Gspos("SMOD", 1, mother, xpos, ypos, zpos, fIdRotm, "ONLY") ;
      printf(" fIdRotm %3i phi %6.1f(%5.3f) xpos %7.2f ypos %7.2f zpos %7.2f \n", 
      fIdRotm, phi, phiRad, xpos, ypos, zpos);
      nr++;
    } else { // TRD1 
      TString smName("SMOD"); // 12-oct-05
      if(i==5 && g->GetKey110DEG()) {
        smName = "SM10";
        nrsmod = nr;
        nr     = 0;
      }
      phi    = g->GetArm1PhiMin() + dphi*(2*i+1)/2.; // phi= 70, 90, 110, 130, 150, 170
      phiRad = phi*TMath::Pi()/180.;

      AliMatrix(fIdRotm, 90.0, phi, 90.0, 90.0+phi, 0.0, 0.0);

      xpos = rpos * TMath::Cos(phiRad);
      ypos = rpos * TMath::Sin(phiRad);
      zpos = fSmodPar2; // 21-sep-04
      if(i==5 && g->GetKey110DEG()) {
        xpos += (par1C/2. * TMath::Sin(phiRad)); 
        ypos -= (par1C/2. * TMath::Cos(phiRad)); 
      }
      
      // 1th module in z-direction;
      gMC->Gspos(smName.Data(), ++nr, mother, xpos, ypos, zpos, fIdRotm, "ONLY") ;
      AliDebug(3, Form(" %s : %2i fIdRotm %3i phi %6.1f(%5.3f) xpos %7.2f ypos %7.2f zpos %7.2f : i %i \n", 
		       smName.Data(), nr, fIdRotm, phi, phiRad, xpos, ypos, zpos, i));
      // 2th module in z-direction;
      // turn arround X axis; 0<phi<360
      double phiy = 90. + phi + 180.;
      if(phiy>=360.) phiy -= 360.;
      
      AliMatrix(fIdRotm, 90.0, phi, 90.0, phiy, 180.0, 0.0);
      gMC->Gspos(smName.Data(), ++nr, mother, xpos, ypos, -zpos, fIdRotm, "ONLY");
      AliDebug(3, Form(" %s : %2i fIdRotm %3i phiy %6.1f  xpos %7.2f ypos %7.2f zpos %7.2f \n", 
		       smName.Data(), nr, fIdRotm, phiy, xpos, ypos, -zpos));
    }
  }
  AliDebug(2,Form(" Number of Super Modules %i \n", nr+nrsmod));
}

//______________________________________________________________________
void AliEMCALv0::CreateEmod(const char* mother, const char* child)
{ 
  // 17-may-05; mother="SMOD"; child="EMOD"
  // Oct 26,2010 
  AliEMCALGeometry * g = GetGeometry(); 
  TString gn(g->GetName()); gn.ToUpper(); 
  // Module definition
  Double_t xpos=0., ypos=0., zpos=0.;
  //Double_t trd1Angle = g->GetTrd1Angle()*TMath::DegToRad();tanTrd1 = TMath::Tan(trd1Angle/2.);

  if(strcmp(mother,"SMOD")==0) {
{'fraction_non_alphanumeric': 0.1935142658315936, 'fraction_numerical': 0.048768267223382046, 'mean_word_length': 2.7681980281099223, 'pattern_counts': {'":': 0, '<': 59, '<?xml version=': 0, '>': 271, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7419694', 'n_tokens_mistral': 15414, 'n_tokens_neox': 14285, 'n_words': 2943}