source: trunk/examples/extended/electromagnetic/TestEm3/src/DetectorConstruction.cc

Last change on this file was 1337, checked in by garnier, 14 years ago

tag geant4.9.4 beta 1 + modifs locales

File size: 18.6 KB
Line 
1//
2// ********************************************************************
3// * License and Disclaimer                                           *
4// *                                                                  *
5// * The  Geant4 software  is  copyright of the Copyright Holders  of *
6// * the Geant4 Collaboration.  It is provided  under  the terms  and *
7// * conditions of the Geant4 Software License,  included in the file *
8// * LICENSE and available at  http://cern.ch/geant4/license .  These *
9// * include a list of copyright holders.                             *
10// *                                                                  *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work  make  any representation or  warranty, express or implied, *
14// * regarding  this  software system or assume any liability for its *
15// * use.  Please see the license in the file  LICENSE  and URL above *
16// * for the full disclaimer and the limitation of liability.         *
17// *                                                                  *
18// * This  code  implementation is the result of  the  scientific and *
19// * technical work of the GEANT4 collaboration.                      *
20// * By using,  copying,  modifying or  distributing the software (or *
21// * any work based  on the software)  you  agree  to acknowledge its *
22// * use  in  resulting  scientific  publications,  and indicate your *
23// * acceptance of all terms of the Geant4 Software license.          *
24// ********************************************************************
25//
26// $Id: DetectorConstruction.cc,v 1.23 2009/03/04 18:49:17 maire Exp $
27// GEANT4 tag $Name: geant4-09-04-beta-01 $
28//
29//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
30//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
31
32#include "DetectorConstruction.hh"
33#include "DetectorMessenger.hh"
34
35#include "G4NistManager.hh"
36#include "G4Material.hh"
37#include "G4Box.hh"
38#include "G4LogicalVolume.hh"
39#include "G4PVPlacement.hh"
40#include "G4PVReplica.hh"
41#include "G4UniformMagField.hh"
42
43#include "G4GeometryManager.hh"
44#include "G4PhysicalVolumeStore.hh"
45#include "G4LogicalVolumeStore.hh"
46#include "G4SolidStore.hh"
47
48#include "G4UImanager.hh"
49#include "G4UnitsTable.hh"
50#include <iomanip>
51
52//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
53
54DetectorConstruction::DetectorConstruction()
55:defaultMaterial(0),solidWorld(0),logicWorld(0),physiWorld(0),
56 solidCalor(0),logicCalor(0),physiCalor(0),
57 solidLayer(0),logicLayer(0),physiLayer(0),
58 magField(0)
59{
60  // default parameter values of the calorimeter
61  NbOfAbsor = 2;
62  AbsorThickness[1] = 2.3*mm;
63  AbsorThickness[2] = 5.7*mm;
64  NbOfLayers        = 50;
65  CalorSizeYZ       = 40.*cm;
66  ComputeCalorParameters();
67
68  // materials
69  DefineMaterials();
70  SetWorldMaterial("Galactic");
71  SetAbsorMaterial(1,"Lead");
72  SetAbsorMaterial(2,"liquidArgon");
73
74  // create commands for interactive definition of the calorimeter
75  detectorMessenger = new DetectorMessenger(this);
76}
77
78//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
79
80DetectorConstruction::~DetectorConstruction()
81{
82  delete detectorMessenger;
83}
84
85//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
86
87G4VPhysicalVolume* DetectorConstruction::Construct()
88{
89  return ConstructCalorimeter();
90}
91
92//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
93
94void DetectorConstruction::DefineMaterials()
95{
96  // This function illustrates the possible ways to define materials using
97  // G4 database on G4Elements
98  G4NistManager* manager = G4NistManager::Instance();
99  manager->SetVerbose(0);
100  //
101  // define Elements
102  //
103  G4double z,a;
104
105  G4Element* H  = manager->FindOrBuildElement(1);
106  G4Element* C  = manager->FindOrBuildElement(6);
107  G4Element* N  = manager->FindOrBuildElement(7);
108  G4Element* O  = manager->FindOrBuildElement(8);
109  G4Element* Si = manager->FindOrBuildElement(14);
110  G4Element* Ge = manager->FindOrBuildElement(32);
111  G4Element* Sb = manager->FindOrBuildElement(51);
112  G4Element* I  = manager->FindOrBuildElement(53);
113  G4Element* Cs = manager->FindOrBuildElement(55);
114  G4Element* Pb = manager->FindOrBuildElement(82);
115  G4Element* Bi = manager->FindOrBuildElement(83);
116
117  //
118  // define an Element from isotopes, by relative abundance
119  //
120  G4int iz, n;                       //iz=number of protons  in an isotope;
121                                     // n=number of nucleons in an isotope;
122  G4int   ncomponents;                               
123  G4double abundance;                               
124
125  G4Isotope* U5 = new G4Isotope("U235", iz=92, n=235, a=235.01*g/mole);
126  G4Isotope* U8 = new G4Isotope("U238", iz=92, n=238, a=238.03*g/mole);
127
128  G4Element* U  = new G4Element("enriched Uranium", "U", ncomponents=2);
129  U->AddIsotope(U5, abundance= 90.*perCent);
130  U->AddIsotope(U8, abundance= 10.*perCent);
131
132  //
133  // define simple materials
134  //
135  G4double density;
136
137  new G4Material("liquidH2",    z=1.,  a= 1.008*g/mole,  density= 70.8*mg/cm3);
138  new G4Material("Aluminium",   z=13., a= 26.98*g/mole,  density= 2.700*g/cm3);
139  new G4Material("Titanium",    z=22., a= 47.867*g/mole, density= 4.54*g/cm3);
140  new G4Material("Iron",        z=26., a= 55.85*g/mole,  density= 7.870*g/cm3);
141  new G4Material("Copper",      z=29., a= 63.55*g/mole,  density= 8.960*g/cm3);
142  new G4Material("Tungsten",    z=74., a= 183.85*g/mole, density= 19.30*g/cm3);
143  new G4Material("Gold",        z=79., a= 196.97*g/mole, density= 19.32*g/cm3);
144  new G4Material("Uranium",     z=92., a= 238.03*g/mole, density= 18.95*g/cm3);
145
146  //
147  // define a material from elements.   case 1: chemical molecule
148  //
149  G4int natoms;
150
151  G4Material* H2O = 
152  new G4Material("Water", density= 1.000*g/cm3, ncomponents=2);
153  H2O->AddElement(H, natoms=2);
154  H2O->AddElement(O, natoms=1);
155  H2O->GetIonisation()->SetMeanExcitationEnergy(75.0*eV);
156  H2O->SetChemicalFormula("H_2O");
157 
158  G4Material* CH = 
159  new G4Material("Polystyrene", density= 1.032*g/cm3, ncomponents=2);
160  CH->AddElement(C, natoms=1);
161  CH->AddElement(H, natoms=1);
162
163  G4Material* Sci = 
164  new G4Material("Scintillator", density= 1.032*g/cm3, ncomponents=2);
165  Sci->AddElement(C, natoms=9);
166  Sci->AddElement(H, natoms=10);
167 
168  Sci->GetIonisation()->SetBirksConstant(0.126*mm/MeV);
169
170  G4Material* Lct =
171  new G4Material("Lucite", density= 1.185*g/cm3, ncomponents=3);
172  Lct->AddElement(C, 59.97*perCent);
173  Lct->AddElement(H, 8.07*perCent);
174  Lct->AddElement(O, 31.96*perCent);
175
176  G4Material* Sili = 
177  new G4Material("Silicon", density= 2.330*g/cm3, ncomponents=1);
178  Sili->AddElement(Si, natoms=1);
179
180  G4Material* SiO2 = 
181  new G4Material("quartz", density= 2.200*g/cm3, ncomponents=2);
182  SiO2->AddElement(Si, natoms=1);
183  SiO2->AddElement(O , natoms=2);
184
185  G4Material* G10 = 
186  new G4Material("NemaG10", density= 1.700*g/cm3, ncomponents=4);
187  G10->AddElement(Si, natoms=1);
188  G10->AddElement(O , natoms=2);
189  G10->AddElement(C , natoms=3);
190  G10->AddElement(H , natoms=3);
191
192  G4Material* CsI = 
193  new G4Material("CsI", density= 4.534*g/cm3, ncomponents=2);
194  CsI->AddElement(Cs, natoms=1);
195  CsI->AddElement(I , natoms=1);
196  CsI->GetIonisation()->SetMeanExcitationEnergy(553.1*eV);
197
198  G4Material* BGO = 
199  new G4Material("BGO", density= 7.10*g/cm3, ncomponents=3);
200  BGO->AddElement(O , natoms=12);
201  BGO->AddElement(Ge, natoms= 3);
202  BGO->AddElement(Bi, natoms= 4);
203
204  //SiNx
205  density= 3.1 *g/cm3;
206  G4Material* SiNx= new G4Material("SiNx", density, ncomponents=3);
207  SiNx-> AddElement(Si, 300);
208  SiNx-> AddElement(N, 310);
209  SiNx-> AddElement(H, 6);
210
211  //
212  // define gaseous materials using G4 NIST database
213  //
214  G4double fractionmass;
215 
216  G4Material* Air = manager->FindOrBuildMaterial("G4_AIR");
217  manager->ConstructNewGasMaterial("Air20","G4_AIR",293.*kelvin,1.*atmosphere);
218
219  G4Material* lAr = manager->FindOrBuildMaterial("G4_lAr");
220  G4Material* lArEm3 = new G4Material("liquidArgon", density= 1.390*g/cm3, ncomponents=1);
221  lArEm3->AddMaterial(lAr, fractionmass=1.0);
222
223  //
224  // define a material from elements and others materials (mixture of mixtures)
225  //
226
227  G4Material* Lead = new G4Material("Lead", density= 11.35*g/cm3, ncomponents=1);
228  Lead->AddElement(Pb, fractionmass=1.0);
229
230  G4Material* LeadSb = new G4Material("LeadSb", density= 11.35*g/cm3, ncomponents=2);
231  LeadSb->AddElement(Sb, fractionmass=4.*perCent);
232  LeadSb->AddElement(Pb, fractionmass=96.*perCent);
233
234  G4Material* Aerog = new G4Material("Aerogel", density= 0.200*g/cm3, ncomponents=3);
235  Aerog->AddMaterial(SiO2, fractionmass=62.5*perCent);
236  Aerog->AddMaterial(H2O , fractionmass=37.4*perCent);
237  Aerog->AddElement (C   , fractionmass= 0.1*perCent);
238
239  //
240  // examples of gas in non STP conditions
241  //
242  G4double temperature, pressure;
243 
244  G4Material* CO2 = 
245  new G4Material("CarbonicGas", density= 27.*mg/cm3, ncomponents=2,
246                 kStateGas, temperature= 325.*kelvin, pressure= 50.*atmosphere);
247  CO2->AddElement(C, natoms=1);
248  CO2->AddElement(O, natoms=2);
249
250  G4Material* steam = 
251  new G4Material("WaterSteam", density= 1.0*mg/cm3, ncomponents=1,
252                  kStateGas, temperature= 273*kelvin, pressure= 1*atmosphere);
253  steam->AddMaterial(H2O, fractionmass=1.);
254 
255  new G4Material("ArgonGas", z=18, a=39.948*g/mole, density= 1.782*mg/cm3,
256                 kStateGas, 273.15*kelvin, 1*atmosphere);
257  //
258  // examples of vacuum
259  //
260
261  density     = universe_mean_density;    //from PhysicalConstants.h
262  pressure    = 3.e-18*pascal;
263  temperature = 2.73*kelvin;
264  new G4Material("Galactic", z=1., a=1.008*g/mole, density,
265                             kStateGas,temperature,pressure);
266
267  density     = 1.e-5*g/cm3;
268  pressure    = 2.e-2*bar;
269  temperature = STP_Temperature;         //from PhysicalConstants.h
270  G4Material* beam = 
271  new G4Material("Beam", density, ncomponents=1,
272                         kStateGas,temperature,pressure);
273  beam->AddMaterial(Air, fractionmass=1.);
274
275  //  G4cout << *(G4Material::GetMaterialTable()) << G4endl;
276}
277
278//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
279
280void DetectorConstruction::ComputeCalorParameters()
281{
282  // Compute derived parameters of the calorimeter
283  LayerThickness = 0.;
284  for (G4int iAbs=1; iAbs<=NbOfAbsor; iAbs++) {
285    LayerThickness += AbsorThickness[iAbs];
286  }
287  CalorThickness = NbOfLayers*LayerThickness;     
288  WorldSizeX = 1.2*CalorThickness; 
289  WorldSizeYZ = 1.2*CalorSizeYZ;
290}
291
292//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
293
294G4VPhysicalVolume* DetectorConstruction::ConstructCalorimeter()
295{
296  // complete the Calor parameters definition
297  ComputeCalorParameters();
298
299  // Cleanup old geometry
300  G4GeometryManager::GetInstance()->OpenGeometry();
301  G4PhysicalVolumeStore::GetInstance()->Clean();
302  G4LogicalVolumeStore::GetInstance()->Clean();
303  G4SolidStore::GetInstance()->Clean();
304
305  //
306  // World
307  //
308
309  solidWorld = new G4Box("World",                               //its name
310                   WorldSizeX/2,WorldSizeYZ/2,WorldSizeYZ/2);   //its size
311
312  logicWorld = new G4LogicalVolume(solidWorld,          //its solid
313                                   defaultMaterial,     //its material
314                                   "World");            //its name
315
316  physiWorld = new G4PVPlacement(0,                     //no rotation
317                                 G4ThreeVector(),       //at (0,0,0)
318                                 logicWorld,            //its logical volume
319                                 "World",               //its name
320                                 0,                     //its mother  volume
321                                 false,                 //no boolean operation
322                                 0);                    //copy number
323  //
324  // Calorimeter
325  //
326
327  solidCalor = new G4Box("Calorimeter",                              //its name
328                       CalorThickness/2,CalorSizeYZ/2,CalorSizeYZ/2);//size
329
330  logicCalor = new G4LogicalVolume(solidCalor,          //its solid
331                                   defaultMaterial,     //its material
332                                   "Calorimeter");      //its name
333
334  physiCalor = new G4PVPlacement(0,                     //no rotation
335                                 G4ThreeVector(),       //at (0,0,0)
336                                 logicCalor,            //its logical volume
337                                 "Calorimeter",         //its name
338                                 logicWorld,            //its mother  volume
339                                 false,                 //no boolean operation
340                                 0);                    //copy number
341
342  //
343  // Layers
344  //
345
346  solidLayer = new G4Box("Layer",                                     //its name
347                       LayerThickness/2,CalorSizeYZ/2,CalorSizeYZ/2); //size
348
349  logicLayer = new G4LogicalVolume(solidLayer,          //its solid
350                                   defaultMaterial,     //its material
351                                   "Layer");            //its name
352  if (NbOfLayers > 1)
353    physiLayer = new G4PVReplica("Layer",               //its name
354                                 logicLayer,            //its logical volume
355                                 logicCalor,            //its mother
356                                 kXAxis,                //axis of replication
357                                 NbOfLayers,            //number of replica
358                                 LayerThickness);       //witdth of replica
359  else
360    physiLayer = new G4PVPlacement(0,                   //no rotation
361                                   G4ThreeVector(),     //at (0,0,0)
362                                   logicLayer,          //its logical volume
363                                   "Layer",             //its name
364                                   logicCalor,          //its mother  volume
365                                   false,               //no boolean operation
366                                   0);                  //copy number
367
368  //
369  // Absorbers
370  //
371
372  G4double xfront = -0.5*LayerThickness;
373  for (G4int k=1; k<=NbOfAbsor; k++) {
374    solidAbsor[k] = new G4Box("Absorber",               //its name
375                              AbsorThickness[k]/2,CalorSizeYZ/2,CalorSizeYZ/2);
376
377    logicAbsor[k] = new G4LogicalVolume(solidAbsor[k],    //its solid
378                                        AbsorMaterial[k], //its material
379                                        AbsorMaterial[k]->GetName());
380
381    G4double xcenter = xfront+0.5*AbsorThickness[k];
382    xfront += AbsorThickness[k];
383    physiAbsor[k] = new G4PVPlacement(0,                   //no rotation
384                        G4ThreeVector(xcenter,0.,0.),      //its position
385                        logicAbsor[k],                     //its logical volume
386                        AbsorMaterial[k]->GetName(),       //its name
387                        logicLayer,                        //its mother
388                        false,                             //no boulean operat
389                        k);                                //copy number
390
391  }
392
393
394  PrintCalorParameters();
395
396  //always return the physical World
397  //
398  return physiWorld;
399}
400
401//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
402
403void DetectorConstruction::PrintCalorParameters()
404{
405  G4cout << "\n-------------------------------------------------------------"
406         << "\n ---> The calorimeter is " << NbOfLayers << " layers of:";
407  for (G4int i=1; i<=NbOfAbsor; i++)
408     {
409      G4cout << "\n \t" << std::setw(12) << AbsorMaterial[i]->GetName() <<": "
410              << std::setw(6) << G4BestUnit(AbsorThickness[i],"Length");
411     }
412  G4cout << "\n-------------------------------------------------------------\n";
413 
414  G4cout << "\n" << defaultMaterial << G4endl;   
415  for (G4int j=1; j<=NbOfAbsor; j++)
416     G4cout << "\n" << AbsorMaterial[j] << G4endl;
417
418  G4cout << "\n-------------------------------------------------------------\n";
419}
420
421//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
422
423void DetectorConstruction::SetWorldMaterial(const G4String& material)
424{
425  // search the material by its name
426  G4Material* pttoMaterial = 
427    G4NistManager::Instance()->FindOrBuildMaterial(material);
428  if (pttoMaterial) defaultMaterial = pttoMaterial;
429}
430
431//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
432
433void DetectorConstruction::SetNbOfLayers(G4int ival)
434{
435  // set the number of Layers
436  //
437  if (ival < 1)
438    { G4cout << "\n --->warning from SetNbOfLayers: "
439             << ival << " must be at least 1. Command refused" << G4endl;
440      return;
441    }
442  NbOfLayers = ival;
443}
444
445//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
446
447void DetectorConstruction::SetNbOfAbsor(G4int ival)
448{
449  // set the number of Absorbers
450  //
451  if (ival < 1 || ival > (MaxAbsor-1))
452    { G4cout << "\n ---> warning from SetNbOfAbsor: "
453             << ival << " must be at least 1 and and most " << MaxAbsor-1
454             << ". Command refused" << G4endl;
455      return;
456    }
457  NbOfAbsor = ival;
458}
459
460//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
461
462void DetectorConstruction::SetAbsorMaterial(G4int ival, const G4String& material)
463{
464  // search the material by its name
465  //
466  if (ival > NbOfAbsor || ival <= 0)
467    { G4cout << "\n --->warning from SetAbsorMaterial: absor number "
468             << ival << " out of range. Command refused" << G4endl;
469      return;
470    }
471
472  G4Material* pttoMaterial = 
473    G4NistManager::Instance()->FindOrBuildMaterial(material);
474  if (pttoMaterial) AbsorMaterial[ival] = pttoMaterial;
475}
476
477//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
478
479void DetectorConstruction::SetAbsorThickness(G4int ival,G4double val)
480{
481  // change Absorber thickness
482  //
483  if (ival > NbOfAbsor || ival <= 0)
484    { G4cout << "\n --->warning from SetAbsorThickness: absor number "
485             << ival << " out of range. Command refused" << G4endl;
486      return;
487    }
488  if (val <= DBL_MIN)
489    { G4cout << "\n --->warning from SetAbsorThickness: thickness "
490             << val  << " out of range. Command refused" << G4endl;
491      return;
492    }
493  AbsorThickness[ival] = val;
494}
495
496//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
497
498void DetectorConstruction::SetCalorSizeYZ(G4double val)
499{
500  // change the transverse size
501  //
502  if (val <= DBL_MIN)
503    { G4cout << "\n --->warning from SetCalorSizeYZ: thickness "
504             << val  << " out of range. Command refused" << G4endl;
505      return;
506    }
507  CalorSizeYZ = val;
508}
509
510//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
511
512#include "G4FieldManager.hh"
513#include "G4TransportationManager.hh"
514
515void DetectorConstruction::SetMagField(G4double fieldValue)
516{
517  //apply a global uniform magnetic field along Z axis
518  //
519  G4FieldManager* fieldMgr
520   = G4TransportationManager::GetTransportationManager()->GetFieldManager();
521
522  if(magField) delete magField;         //delete the existing magn field
523
524  if(fieldValue!=0.)                    // create a new one if non nul
525  { magField = new G4UniformMagField(G4ThreeVector(0.,0.,fieldValue));
526    fieldMgr->SetDetectorField(magField);
527    fieldMgr->CreateChordFinder(magField);
528  } else {
529    magField = 0;
530    fieldMgr->SetDetectorField(magField);
531  }
532}
533
534//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
535
536#include "G4RunManager.hh"
537
538void DetectorConstruction::UpdateGeometry()
539{
540  G4RunManager::GetRunManager()->DefineWorldVolume(ConstructCalorimeter());
541}
542
543//....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
Note: See TracBrowser for help on using the repository browser.