]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliDetConstruction.cxx
Some additional changes related to the previous changes. AliL3Transform
[u/mrichter/AliRoot.git] / AliGeant4 / AliDetConstruction.cxx
1 // $Id$
2 // Category: geometry
3 //
4 // Author: I. Hrivnacova
5 //
6 // Class AliDetConstruction
7 // ------------------------
8 // See the class description in the header file.
9
10 #include "AliDetConstruction.h"
11 #include "AliDetSwitch.h"
12 #include "AliLVTree.h"
13 #include "AliGlobals.h"
14 #include "AliFiles.h"
15 #include "AliRun.h"
16 #include "AliModule.h"
17
18 #include "TG4XMLGeometryGenerator.h"
19 #include "TG4GeometryServices.h"
20
21 #include <G4VPhysicalVolume.hh>
22
23 //_____________________________________________________________________________
24 AliDetConstruction::AliDetConstruction()
25   : AliModulesComposition()
26 {
27   // initialize det switch vector: 
28   // moduleName nofVersions defaultVersion [type]
29         // det switch objects are deleted in fDetSwitchVector destructor
30
31   fDetSwitchVector.Add(new AliDetSwitch("MAG",    1, 0, kStructure));
32   fDetSwitchVector.Add(new AliDetSwitch("ABSO",   1, 0, kStructure));
33   fDetSwitchVector.Add(new AliDetSwitch("DIPO",   3, 2, kStructure));
34   fDetSwitchVector.Add(new AliDetSwitch("FRAME",  3, 2, kStructure));
35   fDetSwitchVector.Add(new AliDetSwitch("HALL",   1, 0, kStructure));
36   fDetSwitchVector.Add(new AliDetSwitch("PIPE",   5, 0, kStructure));
37   fDetSwitchVector.Add(new AliDetSwitch("SHIL",   2, 1, kStructure));
38   fDetSwitchVector.Add(new AliDetSwitch("CASTOR", 2, 1));
39   fDetSwitchVector.Add(new AliDetSwitch("FMD",    2, 1));
40   fDetSwitchVector.Add(new AliDetSwitch("ITS",    7, 5));
41   fDetSwitchVector.Add(new AliDetSwitch("MUON",   2, 1));
42   fDetSwitchVector.Add(new AliDetSwitch("PHOS",   2, 1));
43   fDetSwitchVector.Add(new AliDetSwitch("PMD",    3, 1));
44   fDetSwitchVector.Add(new AliDetSwitch("RICH",   3, 1));
45   fDetSwitchVector.Add(new AliDetSwitch("START",  2, 1));
46   fDetSwitchVector.Add(new AliDetSwitch("TOF",    5, 2));
47   fDetSwitchVector.Add(new AliDetSwitch("TPC",    4, 2));
48   fDetSwitchVector.Add(new AliDetSwitch("TRD",    2, 1));
49   fDetSwitchVector.Add(new AliDetSwitch("ZDC",    3, 2));
50
51   // update messenger
52   fDetSwitchVector.UpdateMessenger();
53
54   // instantiate LVtree browser
55   AliLVTree::Instance();
56 }
57
58 //_____________________________________________________________________________
59 AliDetConstruction::AliDetConstruction(const AliDetConstruction& right)
60   : AliModulesComposition(right)
61 {
62   // AliModuleComposition is protected from copying
63 }  
64
65 //_____________________________________________________________________________
66 AliDetConstruction::~AliDetConstruction() 
67 {
68   // delete LVtree browser
69   delete AliLVTree::Instance();
70 }
71
72 // operators
73
74 //_____________________________________________________________________________
75 AliDetConstruction& 
76 AliDetConstruction::operator=(const AliDetConstruction& right)
77 {
78   // check assignement to self
79   if (this == &right) return *this;
80
81   // base class assignement
82   // AliModuleComposition is protected from assigning
83   AliModulesComposition::operator=(right);
84
85   return *this;  
86 }    
87           
88 // private methods
89
90 //_____________________________________________________________________________
91 void AliDetConstruction::BuildDetectors()
92 {
93 // Create module constructions for AliModules 
94 // that have been created and registered by gAlice 
95 // ---
96
97   TObjArray* pDetectors = gAlice->Detectors();
98   TIter next(pDetectors);
99
100   // the first AliModule is expected to be the top volume
101   AliModule *module = (AliModule*)next();
102   if (G4String(module->GetName()) != "BODY") {
103     G4String text = "AliDetConstruction::BuildDetectors():\n";
104     text = text + "    Instead of BODY - the first module ";
105     text = text + module->GetName() + " has been found.";
106     AliGlobals::Exception(text);
107   }  
108   AddModule("BODY", 0, kStructure);
109
110   G4bool first = true;
111   while ((module = (AliModule*)next())) {
112   
113     // register moduleConstruction in fDetSwitchVector
114     // in order to keep availability of /aliDet/list command
115     G4String modName = module->GetName();
116     G4int modVersion = module->IsVersion();
117     if (first)
118       // skip registering of the top volume 
119       first = false;
120     else 
121       fDetSwitchVector.SwitchDetOn(modName, modVersion);
122  
123     // all modules will be processed alltogether
124     AddModule(modName, modVersion, fDetSwitchVector.GetDetSwitch(modName)->GetType());
125
126     if (VerboseLevel() > 0) {
127       G4cout << "Created module construction for " 
128              << modName << "v" << modVersion << "." << G4endl;   
129     }        
130   }
131   
132   // do not process Config.C 
133   // (it was processed when creating modules by gAlice)
134   SetProcessConfigToModules(false);     
135 }
136
137 //_____________________________________________________________________________
138 void AliDetConstruction::CreateDetectors()
139 {
140 // Creates AliModules and their module constructions 
141 // according to the fDetSwitchVector
142 // ---
143
144   // add top volume (AliBODY) construction first
145   AddModule("BODY", 0, kStructure);
146
147   // add modules constructions
148   for (G4int i=0; i<fDetSwitchVector.GetSize(); i++)
149   {
150     AliDetSwitch* detSwitch = fDetSwitchVector.GetDetSwitch(i);
151     G4String detName = detSwitch->GetDetName();
152     G4int version = detSwitch->GetSwitchedVersion();
153     AliModuleType type = detSwitch->GetType();
154     
155     if (version > -1)
156       AddModule(detName, version, type);
157   }    
158 }
159
160 //_____________________________________________________________________________
161 void AliDetConstruction::CheckDependence(const G4String& master, 
162                                          const G4String& slave)
163 {
164 // Checks modules dependence.
165 // If master is switch on and slave off, the default version
166 // of slave is switched on and a  warning is issued.
167 // ---
168
169   AliDetSwitch* masterSwitch = fDetSwitchVector.GetDetSwitch(master);
170   AliDetSwitch* slaveSwitch = fDetSwitchVector.GetDetSwitch(slave);
171
172   if ( masterSwitch->GetSwitchedVersion() > -1 && 
173        slaveSwitch->GetSwitchedVersion() < 0 ) {
174      
175     slaveSwitch->SwitchOnDefault();
176     
177     // warning
178     G4String text = "AliDetConstruction::CheckDetDependence: \n";
179     text = text + "    Switched " + master + " requires " + slave + ".\n"; 
180     text = text + "    The det switch for " + slave + " has been changed."; 
181     AliGlobals::Warning(text);
182   }
183 }  
184   
185 //_____________________________________________________________________________
186 void AliDetConstruction::CheckDetDependencies()
187 {
188 // Checks modules dependencies.
189 // ---
190
191   CheckDependence("MUON", "DIPO");
192   CheckDependence("TOF", "FRAME");
193   CheckDependence("TRD", "FRAME");
194   CheckDependence("ZDC", "PIPE");
195   CheckDependence("ZDC", "ABSO");
196   CheckDependence("ZDC", "DIPO");
197   CheckDependence("ZDC", "SHIL");
198 }  
199
200 // public methods
201
202 //_____________________________________________________________________________
203 G4VPhysicalVolume* AliDetConstruction::Construct()
204 {
205 // Constructs geometry.
206 // This method is called by G4RunManager in initialization.
207 // ---
208
209   if (gAlice->Modules()->GetLast() < 0) {
210     // create geometry (including AliModules) according to 
211     // the fDetSwitchVector
212     CheckDetDependencies();
213     CreateDetectors();
214   }   
215   else {
216     // create geometry for AliModules 
217     // that have been created and registered by gAlice 
218     BuildDetectors();
219   }  
220   // construct modules geometry
221   ConstructModules();
222
223   return TG4GeometryServices::Instance()->GetWorld();      
224 }
225
226 //_____________________________________________________________________________
227 void AliDetConstruction::GenerateXMLGeometry() const 
228 {
229 // Generates XML geometry file from the top volume.
230 // The file name is set according the last switched detector
231 // registered in the det switch vector.
232 // ---
233
234   G4VPhysicalVolume* world = TG4GeometryServices::Instance()->GetWorld();
235
236   // XML filename
237   // according to last switched detector
238   G4String detName;
239   G4String detVersion = "";
240   G4int version = -1;
241   for (G4int i=fDetSwitchVector.GetSize()-1; i>=0; i--) {
242     version = fDetSwitchVector.GetDetSwitch(i)->GetSwitchedVersion();
243     if (version > -1) {
244       detName = fDetSwitchVector.GetDetSwitch(i)->GetDetName();
245       AliGlobals::AppendNumberToString(detVersion,version); 
246       break;
247     }  
248   }  
249   G4String filePath 
250     = AliFiles::Instance()->GetXMLFilePath(detName, version);
251   
252   // set top volume name
253   G4String topName = world->GetName() + "_comp";
254   
255   // generate XML
256   
257   TG4XMLGeometryGenerator xml;
258   xml.OpenFile(filePath);
259
260   // generate materials 
261   // not implemented
262   // xml.GenerateMaterials(version, "today", "Generated from G4",
263   //                       "v4", world->GetLogicalVolume());
264
265   // generate volumes tree
266   xml.GenerateSection(detName, detVersion, "today", "Generated from Geant4",
267                       topName, world->GetLogicalVolume());
268   xml.CloseFile();
269   
270   if (VerboseLevel() > 0) {
271     G4cout << "File " << detName << "v" << version << ".xml has been generated." 
272            << G4endl;
273   }        
274 }  
275