]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliModulesComposition.cxx
const additions
[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 "AliSingleModuleConstruction.h"
12 #include "AliMoreModulesConstruction.h"
13 #include "AliDetSwitch.h"
14 #include "AliMagneticField.h"
15 #include "AliGlobals.h"
16 #include "AliFiles.h"
17
18 #include "TG4XMLGeometryGenerator.h"
19 #include "TG4GeometryManager.h"
20
21 #include <G4Material.hh>
22 #include <G4VPhysicalVolume.hh>
23
24 //_____________________________________________________________________________
25 AliModulesComposition::AliModulesComposition()
26   : fReadGeometry(false),
27     fWriteGeometry(false),
28     fMagneticField(0),
29     fMessenger(this)
30 {
31 //
32   fMoreModulesConstruction = new AliMoreModulesConstruction();
33 }
34
35 //_____________________________________________________________________________
36 AliModulesComposition::AliModulesComposition(const AliModulesComposition& right)
37   : fMessenger(this)
38 {
39 //
40   AliGlobals::Exception("AliModulesComposition is protected from copying.");  
41 }
42
43 //_____________________________________________________________________________
44 AliModulesComposition::~AliModulesComposition() {
45 //   
46   delete fMoreModulesConstruction;
47   delete fMagneticField;
48   
49   // destroy det switch vector
50   DetSwitchIterator it;
51   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
52     delete *it; 
53   
54   // destroy det construction vector
55   SingleModuleIterator itm;
56   for (itm = fModuleConstructionVector.begin(); 
57        itm != fModuleConstructionVector.end(); it++)
58     delete *itm;
59 }
60
61 // operators
62
63 //_____________________________________________________________________________
64 AliModulesComposition& 
65 AliModulesComposition::operator=(const AliModulesComposition& right)
66 {
67   // check assignement to self
68   if (this == &right) return *this;
69   
70   AliGlobals::Exception("AliModulesComposition is protected from assigning.");  
71
72   return *this;  
73 }    
74           
75 // protected methods
76
77 //_____________________________________________________________________________
78 void AliModulesComposition::AddDetSwitch(AliDetSwitch* detSwitch)
79 {
80 // Adds detSwitch to the detSwitch vector.
81 // ---
82
83   fDetSwitchVector.push_back(detSwitch);
84   fMessenger.SetCandidates();
85 }  
86   
87 //_____________________________________________________________________________
88 void AliModulesComposition::AddSingleModuleConstruction(
89                                const G4String& name, G4int version, 
90                                AliModuleType moduleType)
91 {
92 // Adds SingleModuleConstruction.
93 // ---
94
95   fModuleConstructionVector
96     .push_back(new AliSingleModuleConstruction(name, version, moduleType));
97 }  
98                   
99 //_____________________________________________________________________________
100 void AliModulesComposition::AddMoreModuleConstruction(
101                                const G4String& name, G4int version, 
102                                AliModuleType moduleType)
103 {
104 // Adds module to MoreModulesConstruction (construction of dependent
105 // modules.)
106 // ---
107
108   fMoreModulesConstruction->AddModule(name, version, moduleType);
109 }  
110                                   
111 //_____________________________________________________________________________
112 void AliModulesComposition::ConstructModules()
113 {
114 // Construct geometry of all modules (both standalone and dependent.)
115 // ---
116
117   // set common options
118   SetReadGeometryToModules(fReadGeometry);
119   SetWriteGeometryToModules(fWriteGeometry);
120   
121   // configure single modules
122   SingleModuleIterator it;
123   for (it  = fModuleConstructionVector.begin(); 
124        it != fModuleConstructionVector.end(); it++) {
125        
126     (*it)->Configure(*AliFiles::Instance());
127     cout << "Module " << (*it)->GetDetName() << " configured." << endl;
128   }  
129      
130   // configure dependent modules
131   if (fMoreModulesConstruction->GetNofModules() > 0)
132     fMoreModulesConstruction->Configure(*AliFiles::Instance());
133
134   // construct single modules
135   for (it  = fModuleConstructionVector.begin(); 
136        it != fModuleConstructionVector.end(); it++) {
137
138     G4cout << "Module " << (*it)->GetDetName()
139            << " will be constructed now." << G4endl;
140     (*it)->Construct();
141   }  
142     
143   // construct dependent modules
144   if (fMoreModulesConstruction->GetNofModules() > 0) {
145     G4cout << "Dependent modules will be constructed now." << G4endl;
146     fMoreModulesConstruction->Construct();
147   }  
148 }  
149
150 //_____________________________________________________________________________
151 AliDetSwitch* AliModulesComposition::GetDetSwitch(
152                                         const G4String& moduleName) const
153 {
154 // Returns the detector switch with given detector name.
155 // ---
156
157   DetSwitchConstIterator it;
158   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
159     if ((*it)->GetDetName() == moduleName) return *it; 
160
161   G4String text = "AliModulesComposition::GetDetSwitch:\n";
162   text = text + "Wrong detector name for " + moduleName;   
163   AliGlobals::Exception(text);
164   return 0;  
165
166
167 //_____________________________________________________________________________
168 void AliModulesComposition::SetReadGeometryToModules(G4bool readGeometry)
169 {
170 // Sets readGeometry control to all modules.
171 // ---
172
173   // single module constructions
174   SingleModuleIterator it;
175   for (it  = fModuleConstructionVector.begin(); 
176        it != fModuleConstructionVector.end(); it++)        
177     (*it)->SetReadGeometry(readGeometry);
178
179   // more modules construction
180   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
181     fMoreModulesConstruction
182       ->GetModuleConstruction(i)->SetReadGeometry(readGeometry);
183 }    
184   
185 //_____________________________________________________________________________
186 void AliModulesComposition::SetWriteGeometryToModules(G4bool writeGeometry)
187 {
188 // Sets writeGeometry control to all modules.
189 // ---
190
191   // single module constructions
192   SingleModuleIterator it;
193   for (it  = fModuleConstructionVector.begin(); 
194        it != fModuleConstructionVector.end(); it++)        
195     (*it)->SetWriteGeometry(writeGeometry);
196
197   // more modules construction
198   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
199     fMoreModulesConstruction
200       ->GetModuleConstruction(i)->SetWriteGeometry(writeGeometry);
201 }    
202
203 //_____________________________________________________________________________
204 void AliModulesComposition::SetProcessConfigToModules(G4bool processConfig)
205 {
206 // Sets processConfig control to all modules.
207 // ---
208
209   // single module constructions
210   SingleModuleIterator it;
211   for (it  = fModuleConstructionVector.begin(); 
212        it != fModuleConstructionVector.end(); it++)        
213     (*it)->SetProcessConfig(processConfig);
214   
215   // more modules construction
216   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
217     fMoreModulesConstruction
218       ->GetModuleConstruction(i)->SetProcessConfig(processConfig);
219 }    
220
221 // public methods
222
223 //_____________________________________________________________________________
224 void AliModulesComposition::SwitchDetOn(const G4String& moduleNameVer)
225
226 // Switchs on module specified by name and version.
227 // ---
228
229   DetSwitchIterator it;
230
231   if (moduleNameVer == "ALL") {
232     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
233       (*it)->SwitchOnDefault(); 
234   }
235   else if (moduleNameVer == "PPR") {
236     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
237       (*it)->SwitchOnPPR(); 
238     AliFiles::Instance()->SetMacroName("ConfigPPR");
239   }
240   else if (moduleNameVer == "NONE") {
241     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
242       (*it)->SwitchOff(); 
243   }
244   else {
245     // get version number
246     G4int len = moduleNameVer.length();
247     G4String moduleName = moduleNameVer.substr(0, len-1);
248     G4String version = moduleNameVer.substr(len-1, 1);
249     G4int iVersion = AliGlobals::StringToInt(version);
250
251     if (iVersion < 0) {
252       // in case the version number is not provided
253       // the default one is set
254       SwitchDetOnDefault(moduleNameVer);
255     }  
256     else 
257       SwitchDetOn(moduleName, iVersion);
258   }
259 }
260
261 //_____________________________________________________________________________
262 void AliModulesComposition::SwitchDetOn(const G4String& moduleName, 
263                                         G4int version)
264
265 // Switchs on module specified by name and version.
266 // ---
267
268   GetDetSwitch(moduleName)->SwitchOn(version);
269 }
270
271 //_____________________________________________________________________________
272 void AliModulesComposition::SwitchDetOnDefault(const G4String& moduleName)
273
274 // Switchs on module specified by name with default version.
275 // ---
276
277   GetDetSwitch(moduleName)->SwitchOnDefault();
278 }
279
280 //_____________________________________________________________________________
281 void AliModulesComposition::SwitchDetOnPPR(const G4String& moduleName)
282
283 // Switchs on module specified by name with PPR version.
284 // ---
285
286   GetDetSwitch(moduleName)->SwitchOnPPR();
287 }
288
289 //_____________________________________________________________________________
290 void AliModulesComposition::SwitchDetOff(const G4String& moduleName)
291
292 // Switchs off module specified by name.
293 // ---
294
295   if (moduleName == "ALL") {
296     DetSwitchIterator it;
297     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
298       (*it)->SwitchOff(); 
299   }
300   else 
301     GetDetSwitch(moduleName)->SwitchOff();
302 }
303
304 //_____________________________________________________________________________
305 void AliModulesComposition::PrintSwitchedDets() const
306
307 // Lists switched detectors.
308 // ---
309
310   G4String svList = GetSwitchedDetsList();
311     
312   G4cout << "Switched Alice detectors: " << G4endl;
313   G4cout << "--------------------------" << G4endl;
314   G4cout << svList << G4endl;
315 }
316
317 //_____________________________________________________________________________
318 void AliModulesComposition::PrintAvailableDets() const
319
320 // Lists available detectors.
321 // ---
322
323   G4String avList = GetAvailableDetsList();
324     
325   G4cout << "Available Alice detectors: " << G4endl;
326   G4cout << "---------------------------" << G4endl;
327   G4cout << avList << G4endl;
328 }
329
330 //_____________________________________________________________________________
331 void AliModulesComposition::PrintMaterials() const
332 {
333 // Prints all materials.
334 // ---
335
336   const G4MaterialTable* matTable = G4Material::GetMaterialTable();
337   G4cout << *matTable;
338 }
339
340 //_____________________________________________________________________________
341 void AliModulesComposition::GenerateXMLGeometry() const 
342 {
343 // Generates XML geometry file from the top volume.
344 // The file name is set according the last switched detector
345 // registered in the det switch vector.
346 // ---
347
348   G4VPhysicalVolume* world = AliSingleModuleConstruction::GetWorld();
349
350   // XML filename
351   // according to last switched detector
352   G4String detName;
353   G4String detVersion = "";
354   G4int version = -1;
355   for (G4int i=fDetSwitchVector.size()-1; i>=0; i--) {
356     version = fDetSwitchVector[i]->GetSwitchedVersion();
357     if (version > -1) {
358       detName = fDetSwitchVector[i]->GetDetName();
359       AliGlobals::AppendNumberToString(detVersion,version); 
360       break;
361     }  
362   }  
363   G4String filePath 
364     = AliFiles::Instance()->GetXMLFilePath(detName, version);
365   
366   // set top volume name
367   G4String topName = world->GetName() + "_comp";
368   
369   // generate XML
370   
371   TG4XMLGeometryGenerator xml;
372   xml.OpenFile(filePath);
373
374   // generate materials 
375   // not yet implemented
376   // xml.GenerateMaterials(version, "today", "Generated from G4",
377   //                     "v4", world->GetLogicalVolume());
378
379   // generate volumes tree
380   xml.GenerateSection(detName, detVersion, "today", "Generated from Geant4",
381                       topName, world->GetLogicalVolume());
382   xml.CloseFile();
383   
384   // set verbose
385   G4cout << "File " << detName << "v" << version << ".xml has been generated." 
386          << G4endl;
387 }  
388
389 //_____________________________________________________________________________
390 G4String AliModulesComposition::GetSwitchedDetsList() const
391
392 // Returns list of switched detectors.
393 // ---
394
395   G4String svList = "";  
396   G4int nofSwitchedDets = 0;
397   DetSwitchConstIterator it;
398   
399   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
400     G4int iVersion = (*it)->GetSwitchedVersion();
401     if (iVersion > -1) {
402       nofSwitchedDets++;
403       G4String moduleNameVer = (*it)->GetDetName();
404       AliGlobals::AppendNumberToString(moduleNameVer, iVersion);
405       svList += moduleNameVer;
406       svList += " "; 
407     }
408   }
409
410   if (nofSwitchedDets == fDetSwitchVector.size()) svList = "ALL: " + svList;
411   if (nofSwitchedDets == 0) svList = "NONE";   
412
413   return svList;
414 }
415
416 //_____________________________________________________________________________
417 G4String AliModulesComposition::GetAvailableDetsList() const
418
419 // Returns list of available detectors.
420 // ---
421
422   G4String svList = "";
423   DetSwitchConstIterator it;
424   
425   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
426     for (G4int iv=0; iv<(*it)->GetNofVersions(); iv++) {
427       G4String moduleNameVer = (*it)->GetDetName();
428       AliGlobals::AppendNumberToString(moduleNameVer, iv);
429       svList += moduleNameVer;
430       svList += " ";
431     } 
432
433   return svList;
434 }
435
436 //_____________________________________________________________________________
437 G4String AliModulesComposition::GetAvailableDetsListWithCommas() const
438
439 // Returns list of available detectors with commas.
440 // ---
441
442   G4String svList = "";
443   G4int id =0;
444   DetSwitchConstIterator it;
445
446   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
447     for (G4int iv=0; iv<(*it)->GetNofVersions(); iv++) {
448       G4String moduleNameVer = (*it)->GetDetName();
449       AliGlobals::AppendNumberToString(moduleNameVer, iv);
450       svList += moduleNameVer;
451       if (iv < (*it)->GetNofVersions()-1)        svList += "/";
452       else if (id++ < fDetSwitchVector.size()-1) svList += ", ";
453     }
454
455   return svList;
456 }
457
458 //_____________________________________________________________________________
459 G4String AliModulesComposition::GetDetNamesList() const
460
461 // Returns list of detector names.
462 // ---
463
464   G4String svList = "";
465   DetSwitchConstIterator it;
466   
467   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
468     svList += (*it)->GetDetName();
469     svList += " ";
470   }
471
472   return svList;
473 }
474
475 //_____________________________________________________________________________
476 G4String AliModulesComposition::GetDetNamesListWithCommas() const
477
478 // Returns list of detector names with commas.
479 // ---
480
481   G4String svList = "";
482   G4int id =0;
483   DetSwitchConstIterator it;
484
485   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
486     svList += (*it)->GetDetName();
487     if (id++ < fDetSwitchVector.size()-1) svList += ", ";
488   }
489
490   return svList;
491 }
492
493 //_____________________________________________________________________________
494 void AliModulesComposition::SetMagField(G4double fieldValue)
495 {
496 // Sets uniform magnetic field to specified value.
497 // ---
498
499   // create fields if it does not exist
500   if (!fMagneticField) fMagneticField = new AliMagneticField();
501   
502   // set value
503   fMagneticField->SetFieldValue(fieldValue);
504 }
505