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