]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliModulesComposition.cxx
Updated class description: added class title, author;
[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(const G4String& moduleName)
152 {
153 // Returns the detector switch with given detector name.
154 // ---
155
156   DetSwitchIterator it;
157   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
158     if ((*it)->GetDetName() == moduleName) return *it; 
159
160   G4String text = "AliModulesComposition::GetDetSwitch:\n";
161   text = text + "Wrong detector name for " + moduleName;   
162   AliGlobals::Exception(text);
163   return 0;  
164
165
166 //_____________________________________________________________________________
167 void AliModulesComposition::SetReadGeometryToModules(G4bool readGeometry)
168 {
169 // Sets readGeometry control to all modules.
170 // ---
171
172   // single module constructions
173   SingleModuleIterator it;
174   for (it  = fModuleConstructionVector.begin(); 
175        it != fModuleConstructionVector.end(); it++)        
176     (*it)->SetReadGeometry(readGeometry);
177
178   // more modules construction
179   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
180     fMoreModulesConstruction
181       ->GetModuleConstruction(i)->SetReadGeometry(readGeometry);
182 }    
183   
184 //_____________________________________________________________________________
185 void AliModulesComposition::SetWriteGeometryToModules(G4bool writeGeometry)
186 {
187 // Sets writeGeometry control to all modules.
188 // ---
189
190   // single module constructions
191   SingleModuleIterator it;
192   for (it  = fModuleConstructionVector.begin(); 
193        it != fModuleConstructionVector.end(); it++)        
194     (*it)->SetWriteGeometry(writeGeometry);
195
196   // more modules construction
197   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
198     fMoreModulesConstruction
199       ->GetModuleConstruction(i)->SetWriteGeometry(writeGeometry);
200 }    
201
202 //_____________________________________________________________________________
203 void AliModulesComposition::SetProcessConfigToModules(G4bool processConfig)
204 {
205 // Sets processConfig control to all modules.
206 // ---
207
208   // single module constructions
209   SingleModuleIterator it;
210   for (it  = fModuleConstructionVector.begin(); 
211        it != fModuleConstructionVector.end(); it++)        
212     (*it)->SetProcessConfig(processConfig);
213   
214   // more modules construction
215   for (G4int i=0; i<fMoreModulesConstruction->GetNofModules(); i++) 
216     fMoreModulesConstruction
217       ->GetModuleConstruction(i)->SetProcessConfig(processConfig);
218 }    
219
220 // public methods
221
222 //_____________________________________________________________________________
223 void AliModulesComposition::SwitchDetOn(const G4String& moduleNameVer)
224
225 // Switchs on module specified by name and version.
226 // ---
227
228   DetSwitchIterator it;
229
230   if (moduleNameVer == "ALL") {
231     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
232       (*it)->SwitchOnDefault(); 
233   }
234   else if (moduleNameVer == "PPR") {
235     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
236       (*it)->SwitchOnPPR(); 
237     AliFiles::Instance()->SetMacroName("ConfigPPR");
238   }
239   else if (moduleNameVer == "NONE") {
240     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
241       (*it)->SwitchOff(); 
242   }
243   else {
244     // get version number
245     G4int len = moduleNameVer.length();
246     G4String moduleName = moduleNameVer.substr(0, len-1);
247     G4String version = moduleNameVer.substr(len-1, 1);
248     G4int iVersion = AliGlobals::StringToInt(version);
249
250     if (iVersion < 0) {
251       // in case the version number is not provided
252       // the default one is set
253       SwitchDetOnDefault(moduleNameVer);
254     }  
255     else 
256       SwitchDetOn(moduleName, iVersion);
257   }
258 }
259
260 //_____________________________________________________________________________
261 void AliModulesComposition::SwitchDetOn(const G4String& moduleName, 
262                                         G4int version)
263
264 // Switchs on module specified by name and version.
265 // ---
266
267   GetDetSwitch(moduleName)->SwitchOn(version);
268 }
269
270 //_____________________________________________________________________________
271 void AliModulesComposition::SwitchDetOnDefault(const G4String& moduleName)
272
273 // Switchs on module specified by name with default version.
274 // ---
275
276   GetDetSwitch(moduleName)->SwitchOnDefault();
277 }
278
279 //_____________________________________________________________________________
280 void AliModulesComposition::SwitchDetOnPPR(const G4String& moduleName)
281
282 // Switchs on module specified by name with PPR version.
283 // ---
284
285   GetDetSwitch(moduleName)->SwitchOnPPR();
286 }
287
288 //_____________________________________________________________________________
289 void AliModulesComposition::SwitchDetOff(const G4String& moduleName)
290
291 // Switchs off module specified by name.
292 // ---
293
294   if (moduleName == "ALL") {
295     DetSwitchIterator it;
296     for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
297       (*it)->SwitchOff(); 
298   }
299   else 
300     GetDetSwitch(moduleName)->SwitchOff();
301 }
302
303 //_____________________________________________________________________________
304 void AliModulesComposition::PrintSwitchedDets() const
305
306 // Lists switched detectors.
307 // ---
308
309   G4String svList = GetSwitchedDetsList();
310     
311   G4cout << "Switched Alice detectors: " << G4endl;
312   G4cout << "--------------------------" << G4endl;
313   G4cout << svList << G4endl;
314 }
315
316 //_____________________________________________________________________________
317 void AliModulesComposition::PrintAvailableDets() const
318
319 // Lists available detectors.
320 // ---
321
322   G4String avList = GetAvailableDetsList();
323     
324   G4cout << "Available Alice detectors: " << G4endl;
325   G4cout << "---------------------------" << G4endl;
326   G4cout << avList << G4endl;
327 }
328
329 //_____________________________________________________________________________
330 void AliModulesComposition::PrintMaterials() const
331 {
332 // Prints all materials.
333 // ---
334
335   const G4MaterialTable* matTable = G4Material::GetMaterialTable();
336   G4cout << *matTable;
337 }
338
339 //_____________________________________________________________________________
340 void AliModulesComposition::GenerateXMLGeometry() const 
341 {
342 // Generates XML geometry file from the top volume.
343 // The file name is set according the last switched detector
344 // registered in the det switch vector.
345 // ---
346
347   G4VPhysicalVolume* world = AliSingleModuleConstruction::GetWorld();
348
349   // XML filename
350   // according to last switched detector
351   G4String detName;
352   G4String detVersion = "";
353   G4int version = -1;
354   for (G4int i=fDetSwitchVector.size()-1; i>=0; i--) {
355     version = fDetSwitchVector[i]->GetSwitchedVersion();
356     if (version > -1) {
357       detName = fDetSwitchVector[i]->GetDetName();
358       AliGlobals::AppendNumberToString(detVersion,version); 
359       break;
360     }  
361   }  
362   G4String filePath 
363     = AliFiles::Instance()->GetXMLFilePath(detName, version);
364   
365   // set top volume name
366   G4String topName = world->GetName() + "_comp";
367   
368   // generate XML
369   
370   TG4XMLGeometryGenerator xml;
371   xml.OpenFile(filePath);
372
373   // generate materials 
374   // not yet implemented
375   // xml.GenerateMaterials(version, "today", "Generated from G4",
376   //                     "v4", world->GetLogicalVolume());
377
378   // generate volumes tree
379   xml.GenerateSection(detName, detVersion, "today", "Generated from Geant4",
380                       topName, world->GetLogicalVolume());
381   xml.CloseFile();
382   
383   // set verbose
384   G4cout << "File " << detName << "v" << version << ".xml has been generated." 
385          << G4endl;
386 }  
387
388 //_____________________________________________________________________________
389 G4String AliModulesComposition::GetSwitchedDetsList() const
390
391 // Returns list of switched detectors.
392 // ---
393
394   G4String svList = "";  
395   G4int nofSwitchedDets = 0;
396   DetSwitchConstIterator it;
397   
398   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
399     G4int iVersion = (*it)->GetSwitchedVersion();
400     if (iVersion > -1) {
401       nofSwitchedDets++;
402       G4String moduleNameVer = (*it)->GetDetName();
403       AliGlobals::AppendNumberToString(moduleNameVer, iVersion);
404       svList += moduleNameVer;
405       svList += " "; 
406     }
407   }
408
409   if (nofSwitchedDets == fDetSwitchVector.size()) svList = "ALL: " + svList;
410   if (nofSwitchedDets == 0) svList = "NONE";   
411
412   return svList;
413 }
414
415 //_____________________________________________________________________________
416 G4String AliModulesComposition::GetAvailableDetsList() const
417
418 // Returns list of available detectors.
419 // ---
420
421   G4String svList = "";
422   DetSwitchConstIterator it;
423   
424   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
425     for (G4int iv=0; iv<(*it)->GetNofVersions(); iv++) {
426       G4String moduleNameVer = (*it)->GetDetName();
427       AliGlobals::AppendNumberToString(moduleNameVer, iv);
428       svList += moduleNameVer;
429       svList += " ";
430     } 
431
432   return svList;
433 }
434
435 //_____________________________________________________________________________
436 G4String AliModulesComposition::GetAvailableDetsListWithCommas() const
437
438 // Returns list of available detectors with commas.
439 // ---
440
441   G4String svList = "";
442   G4int id =0;
443   DetSwitchConstIterator it;
444
445   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++)
446     for (G4int iv=0; iv<(*it)->GetNofVersions(); iv++) {
447       G4String moduleNameVer = (*it)->GetDetName();
448       AliGlobals::AppendNumberToString(moduleNameVer, iv);
449       svList += moduleNameVer;
450       if (iv < (*it)->GetNofVersions()-1)        svList += "/";
451       else if (id++ < fDetSwitchVector.size()-1) svList += ", ";
452     }
453
454   return svList;
455 }
456
457 //_____________________________________________________________________________
458 G4String AliModulesComposition::GetDetNamesList() const
459
460 // Returns list of detector names.
461 // ---
462
463   G4String svList = "";
464   DetSwitchConstIterator it;
465   
466   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
467     svList += (*it)->GetDetName();
468     svList += " ";
469   }
470
471   return svList;
472 }
473
474 //_____________________________________________________________________________
475 G4String AliModulesComposition::GetDetNamesListWithCommas() const
476
477 // Returns list of detector names with commas.
478 // ---
479
480   G4String svList = "";
481   G4int id =0;
482   DetSwitchConstIterator it;
483
484   for (it = fDetSwitchVector.begin(); it != fDetSwitchVector.end(); it++) {
485     svList += (*it)->GetDetName();
486     if (id++ < fDetSwitchVector.size()-1) svList += ", ";
487   }
488
489   return svList;
490 }
491
492 //_____________________________________________________________________________
493 void AliModulesComposition::SetMagField(G4double fieldValue)
494 {
495 // Sets uniform magnetic field to specified value.
496 // ---
497
498   // create fields if it does not exist
499   if (!fMagneticField) fMagneticField = new AliMagneticField();
500   
501   // set value
502   fMagneticField->SetFieldValue(fieldValue);
503 }
504