]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliModulesComposition.cxx
Merging the VirtualMC branch to the main development branch (HEAD)
[u/mrichter/AliRoot.git] / AliGeant4 / AliModulesComposition.cxx
1 // $Id$
2 // Category: geometry
3 //
4 // Author: I. Hrivnacova
5 //
6 // Class AliModulesComposition
7 // ---------------------------
8 // See the class description in the header file.
9
10 #include "AliModulesComposition.h"
11 #include "AliModuleConstruction.h"
12 #include "AliGlobals.h"
13 #include "AliFiles.h"
14 #include "AliModule.h"
15
16 #include "TG4GeometryManager.h"
17 #include "TG4MagneticField.h"
18 #include "TG4UniformMagneticField.h"
19
20 #include <G4Material.hh>
21
22 //_____________________________________________________________________________
23 AliModulesComposition::AliModulesComposition()
24   : AliVerbose("modulesComposition"),
25     fMessenger(this),
26     fMagneticFieldType(kMCApplicationField), 
27     fMagneticField(0),
28     fReadGeometry(false),
29     fWriteGeometry(false) {
30 //
31 }
32
33 //_____________________________________________________________________________
34 AliModulesComposition::AliModulesComposition(const AliModulesComposition& right)
35   : AliVerbose("modulesComposition"),
36     fMessenger(this)
37 {
38 //
39   AliGlobals::Exception("AliModulesComposition is protected from copying.");  
40 }
41
42 //_____________________________________________________________________________
43 AliModulesComposition::~AliModulesComposition() {
44 //   
45   delete fMagneticField;
46 }
47
48 // operators
49
50 //_____________________________________________________________________________
51 AliModulesComposition& 
52 AliModulesComposition::operator=(const AliModulesComposition& right)
53 {
54   // check assignement to self
55   if (this == &right) return *this;
56   
57   AliGlobals::Exception("AliModulesComposition is protected from assigning.");  
58
59   return *this;  
60 }    
61           
62 //
63 // private methods
64 //
65
66 //_____________________________________________________________________________
67 void AliModulesComposition::CreateMagneticField()
68 {
69 // Creates standard magnetic field (defined by TVirtualMCApplication).
70 // ---
71
72   switch (fMagneticFieldType) {
73   
74     case kMCApplicationField:
75       fMagneticField = new TG4MagneticField();
76       G4cout << "kMCApplicationField" << G4endl;
77       break;
78
79     case kUniformField:
80       fMagneticField = new TG4UniformMagneticField();
81       G4cout << "kUniformField" << G4endl;
82       break;
83       
84     case kNoField:
85       G4cout << "kNoField" << G4endl;
86       ;;
87   }  
88 }
89
90 //_____________________________________________________________________________
91 void AliModulesComposition::Configure()
92
93 // Executes the detectors setup Root macros
94 // (extracted from AliRoot Config.C) and
95 // G4 macros.
96 // ---
97   
98   // number of modules
99   G4int nofModules = fModuleConstructionVector.size();
100
101   if (nofModules == 0) {
102     AliGlobals::Warning(
103       "AliModulesComposition::Configure: No modules are defined.");
104     return;  
105   }
106   
107   for (G4int i=0; i<nofModules; i++) 
108     fModuleConstructionVector[i]->Configure();
109 }      
110
111
112 //_____________________________________________________________________________
113 void AliModulesComposition::CreateG4Geometry()
114
115 // Constructs geometry.
116 // G3 tables are process for all modules alltogether.
117 // --
118
119   // number of modules
120   G4int nofModules = fModuleConstructionVector.size();
121
122   if (nofModules == 0) {
123     AliGlobals::Warning(
124       "AliModulesComposition::CreateG4Geometry: No modules are defined.");
125     return;  
126   }
127
128   // get geometry manager
129   TG4GeometryManager* pGeometryManager = TG4GeometryManager::Instance();
130
131   G4int i;
132   for (i=0; i<nofModules; i++) {
133
134     // fModuleConstructionVector[i]->Configure(files);
135   
136     // register module name in the name map
137     AliModule* module = fModuleConstructionVector[i]->GetAliModule();
138     pGeometryManager->SetMapSecond(module->GetName());  
139
140     G4bool readGeometry = fModuleConstructionVector[i]->GetReadGeometry();
141     G4bool writeGeometry = fModuleConstructionVector[i]->GetWriteGeometry();
142     G4String dataFilePath = fModuleConstructionVector[i]->GetDataFilePath();
143
144     if (readGeometry) {
145       // TG4GeometryManager uses g3tog4 methods for reading
146       // g3calls.dat files - as these methods do not fill name map
147       // they cannot be used for constructing more modules
148       // together
149       //
150       // pGeometryManager->SetWriteGeometry(false);
151       // pGeometryManager->ReadG3Geometry(dataFilePath);
152         
153       G4String text = "AliModulesComposition::Construct - Limitation:\n";
154       text = text + "    Reading g3calls.dat is not implemented.";
155       AliGlobals::Exception(text);
156     }
157     else {                  
158       // set geometry output stream for this module
159       pGeometryManager->SetWriteGeometry(writeGeometry);
160       if (writeGeometry) 
161         pGeometryManager->OpenOutFile(dataFilePath);
162         
163       // create geometry from AliRoot
164       
165       // construct materials
166       module->CreateMaterials();   
167
168       // construct G3 geometry
169       module->CreateGeometry();
170       
171       if (writeGeometry) 
172         pGeometryManager->CloseOutFile();
173     }   
174   }  
175   
176   // construct G4 geometry
177   pGeometryManager->CreateG4Geometry();
178
179   // print name map
180   // pGeometryManager->PrintNameMap();
181
182 /*
183   // moved to AliSDConstruction
184   // to be performed after Init  (required for MUON)
185   
186   for (i=0; i<nofModules; i++) {
187
188     // construct geometry for display
189     fModuleConstructionVector[i]->GetAliModule()->BuildGeometry();
190   }
191 */
192
193   // reset TG4GeometryManager 
194   pGeometryManager->ClearG3Tables();
195 }
196
197 //_____________________________________________________________________________
198 void AliModulesComposition::SetReadGeometryToModules(G4bool readGeometry)
199 {
200 // Sets readGeometry control to all modules.
201 // ---
202
203   for (G4int i=0; i<G4int(fModuleConstructionVector.size()); i++) 
204     fModuleConstructionVector[i]->SetReadGeometry(readGeometry);
205 }    
206   
207 //_____________________________________________________________________________
208 void AliModulesComposition::SetWriteGeometryToModules(G4bool writeGeometry)
209 {
210 // Sets writeGeometry control to all modules.
211 // ---
212
213   for (G4int i=0; i<G4int(fModuleConstructionVector.size()); i++) 
214     fModuleConstructionVector[i]->SetWriteGeometry(writeGeometry);
215 }    
216
217 //
218 // protected methods
219 //
220
221 //_____________________________________________________________________________
222 void AliModulesComposition::AddModule(const G4String& name, 
223                                       G4int version, 
224                                       AliModuleType moduleType)
225 {
226 // Adds module to the module construction vector.
227 // ---
228
229   AliModuleConstruction* moduleConstruction 
230     = new AliModuleConstruction(name, version, moduleType);
231
232   fModuleConstructionVector.push_back(moduleConstruction);
233 }  
234                                   
235 //_____________________________________________________________________________
236 void AliModulesComposition::ConstructModules()
237 {
238 // Construct geometry of all modules (both standalone and dependent.)
239 // ---
240
241   // create magnetic field
242   CreateMagneticField();  
243
244   // set common options
245   SetReadGeometryToModules(fReadGeometry);
246   SetWriteGeometryToModules(fWriteGeometry);
247   
248   // configure modules
249   Configure();
250
251   // construct dependent modules
252   CreateG4Geometry();
253 }  
254
255 //_____________________________________________________________________________
256 void AliModulesComposition::SetProcessConfigToModules(G4bool processConfig)
257 {
258 // Sets processConfig control to all modules.
259 // ---
260
261   for (G4int i=0; i<G4int(fModuleConstructionVector.size()); i++) 
262     fModuleConstructionVector[i]->SetProcessConfig(processConfig);
263 }    
264
265 //
266 // public methods
267 //
268
269 //_____________________________________________________________________________
270 void AliModulesComposition::PrintMaterials() const
271 {
272 // Prints all materials.
273 // ---
274
275   const G4MaterialTable* matTable = G4Material::GetMaterialTable();
276   G4cout << *matTable;
277 }
278
279
280 //_____________________________________________________________________________
281 void AliModulesComposition::SetFieldType(TG4MagneticFieldType fieldType)
282 {
283 // Selects from available magnetic field types:
284 // field defined by TVirtualMCApplication, uniform magnetic field
285 // or no magnetic field.
286 // Applicable only when no field has been yet created (PreInit).
287 // ---
288
289   if (fMagneticField) {
290      G4String text = "AliModulesComposition::SetFieldType :\n";
291      text = text + "    The magnetic field already exists.";
292      text = text + "    Selection was ignored.";
293      AliGlobals::Warning(text);
294   }   
295
296   fMagneticFieldType = fieldType;
297 }  
298
299 //_____________________________________________________________________________
300 void AliModulesComposition::SetUniformFieldValue(G4double fieldValue)
301 {
302 // Sets uniform magnetic field to specified value.
303 // ---
304   
305   if (!fMagneticField) {
306      G4String text = "AliModulesComposition::SetUniformMagField: \n";
307      text = text + "    Magnetic field is not defined.";
308      AliGlobals::Exception(text);
309   }   
310
311   // Check field type 
312   TG4UniformMagneticField* uniformField 
313     =dynamic_cast<TG4UniformMagneticField*>(fMagneticField);
314
315   if (!uniformField) {
316      G4String text = "AliModulesComposition::SetUniformMagField: \n";
317      text = text + "    Defined magnetic field is not uniform.";
318      AliGlobals::Exception(text);
319   }   
320
321   // Set value
322   uniformField->SetFieldValue(fieldValue);
323 }