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