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