]> git.uio.no Git - u/mrichter/AliRoot.git/blob - AliGeant4/AliLVStructure.cxx
Made a new abstract base class; AliL3HoughBaseTransformer for different implementations
[u/mrichter/AliRoot.git] / AliGeant4 / AliLVStructure.cxx
1 // $Id$
2 // Category: geometry
3 //
4 // Author: I. Hrivnacova
5 //
6 // Class AliLVStructure
7 // --------------------
8 // See the class description in the header file.
9
10 #include "AliLVStructure.h"
11 #include "AliGlobals.h"
12
13 #ifdef G4VIS_USE
14 #include "AliColourStore.h"
15
16 #include <G4Colour.hh>
17 #include <G4VisAttributes.hh>
18 #endif //G4VIS_USE
19 #include <G4LogicalVolume.hh>
20 #include <G4BooleanSolid.hh>
21 #include <g4std/set>
22
23 //_____________________________________________________________________________
24 AliLVStructure::AliLVStructure(G4String path)
25   : fPathName(path),
26     fDirName(path),
27     fVerboseLevel(0)
28 {
29 //
30   G4int i = fDirName.length();
31   if (i > 1) {
32     fDirName.remove(i-1);
33     G4int isl = fDirName.last('/');
34     fDirName.remove(0,isl+1);
35     fDirName += "/";
36   }
37 }
38
39 //_____________________________________________________________________________
40 AliLVStructure::AliLVStructure(const AliLVStructure& right)
41 {
42   // copy stuff
43   *this = right;
44 }
45
46 //_____________________________________________________________________________
47 AliLVStructure::AliLVStructure() {
48 //
49 }
50
51 //_____________________________________________________________________________
52 AliLVStructure::~AliLVStructure() {
53 //
54
55
56   ClearAndDestroy(&fStructures);
57   
58   
59   fLogicalVolumes.resize(0);
60 }
61
62 // operators
63
64 //_____________________________________________________________________________
65 AliLVStructure& AliLVStructure::operator=(const AliLVStructure &right)
66 {
67   // check assignement to self
68   if (this == &right) return *this;
69
70   // copy vector of structures
71   ClearAndDestroy(&fStructures);
72   G4int i;
73   for (i=0; i<right.fStructures.size(); i++) {
74     // new full structure tree has to be created
75     AliLVStructure* rhsStructure = right.fStructures[i];
76     fStructures.push_back(new AliLVStructure(*rhsStructure)); 
77   }  
78   
79   // copy vector of logical volumes
80   fLogicalVolumes.resize(0);
81   for (i=0; i<right.fLogicalVolumes.size(); i++) {
82     G4LogicalVolume* rhsLV = right.fLogicalVolumes[i];
83     fLogicalVolumes.push_back(rhsLV); 
84   }  
85   
86   fPathName = right.fPathName;
87   fDirName = right.fPathName;
88   fVerboseLevel = right.fVerboseLevel;
89
90   return *this;
91 }
92
93 //_____________________________________________________________________________
94 G4int AliLVStructure::operator==(const AliLVStructure &right) const
95 {
96   // check == to self
97   if (this == &right) return true;
98
99   return false;
100 }
101
102 // private methods
103
104 //_____________________________________________________________________________
105 AliLVStructure* AliLVStructure::FindSubDirectory(const G4String& subDir) const
106 {
107 // Finds the subdirectory.
108 // ---
109
110   for( G4int i=0; i<fStructures.size(); i++ ) {
111     if (subDir == fStructures[i]->fDirName) return fStructures[i];
112   } 
113   return 0;
114 }
115
116 //_____________________________________________________________________________
117 G4String AliLVStructure::ExtractDirName(const G4String& name) const
118 {
119 // Extracts the directory name from the path.
120 // ---
121
122   G4String subDir = name;
123   G4int i = name.first('/');
124   if (i != G4std::string::npos) subDir.remove(i+1);
125   return subDir;
126 }
127
128 //_____________________________________________________________________________
129 void AliLVStructure::ClearAndDestroy(LVStructuresVector* structures) 
130 {
131 // Clears the structures vectors and deletes all contained 
132 // elements.
133 // (According to geant4.3.2/source/global/STLInterface/g4rw/tpordvec.icc.)
134 // ---
135
136   G4std::set<AliLVStructure*,G4std::greater<AliLVStructure*> > tmp;
137   for (size_t sz=0; sz<structures->size(); sz++)
138     {
139       AliLVStructure* current;
140       current=(*structures)[sz];
141       if (current)
142          tmp.insert(current);
143     }
144     
145   G4std::set<AliLVStructure*, G4std::greater<AliLVStructure*> >::iterator it;
146   for (it=tmp.begin(); it!=tmp.end(); it++)
147     {
148       delete *it;
149     }
150   // std_pvector::erase(std_pvector::begin(), std_pvector::end());
151   structures->resize(0);
152 }
153
154 // public methods
155
156 //_____________________________________________________________________________
157 void AliLVStructure::AddNewVolume(G4LogicalVolume* lv, 
158                                   const G4String& treeStructure)
159 {
160 // Adds new logical volume to the structure.
161 // ---
162
163   G4String remainingPath = treeStructure;
164   remainingPath.remove(0, fPathName.length());  
165   if (!remainingPath.isNull()) { 
166     // The lv should be kept in subdirectory.
167     // First, check if the subdirectoy exists.
168     G4String subDir = ExtractDirName( remainingPath );
169     AliLVStructure* targetLVS = FindSubDirectory(subDir);
170     if (targetLVS == 0) { 
171       // Subdirectory not found. Create a new directory.
172       subDir.prepend(fPathName);
173       targetLVS = new AliLVStructure(subDir);
174       fStructures.push_back( targetLVS );
175     }
176     targetLVS->AddNewVolume(lv, treeStructure);
177   }
178   else { 
179     // the logical volumes should be kept in this directory.
180     G4LogicalVolume* targetLV = GetVolume(lv->GetName());
181     if (targetLV != 0) {
182       // G4cout << lv->GetName() << " has been already stored in "
183       //        << fPathName << G4endl;
184     }
185     else {
186       fLogicalVolumes.push_back(lv);
187     }
188   }
189 }
190
191 //_____________________________________________________________________________
192 G4LogicalVolume* AliLVStructure::GetVolume(const G4String& lvName) const
193 {
194 // Returns logical volume of lvName if present in the structure,
195 // returns 0 otherwise.
196 // ---
197
198   for (G4int i=0; i<fLogicalVolumes.size(); i++) {
199     G4LogicalVolume* targetLV = fLogicalVolumes[i];
200     if (lvName == targetLV->GetName()) return targetLV;
201   }
202   return 0;
203 }
204
205 //_____________________________________________________________________________
206 G4LogicalVolume* AliLVStructure::FindVolume(const G4String& name) const
207 {
208 // Finds logical volume of given name in all structure tree.
209 // ---
210
211   G4String path = name;
212   path.remove(0, fPathName.length());
213   if (path.first('/') != G4std::string::npos) { 
214     // SD exists in sub-directory
215     G4String subDir = ExtractDirName(path);
216     AliLVStructure* targetLVS = FindSubDirectory(subDir);
217     if (targetLVS == 0) {  
218       // The subdirectory is not found
219       G4String text = subDir + " is not found in " + fPathName;
220       AliGlobals:: Warning(text);
221       return 0;
222     }
223     else { 
224       return targetLVS->FindVolume(name); 
225     }
226   }
227   else { 
228     // LV must exist in this directory
229     G4LogicalVolume* targetLV = GetVolume(path);
230     if (targetLV == 0) {  
231       // The fLogicalVolumes is not found.
232       G4String text = path + " is not found in " + fPathName;
233       AliGlobals::Warning(text);
234     }
235     return targetLV;
236   }
237 }
238
239 //_____________________________________________________________________________
240 void AliLVStructure::ListTree() const
241 {
242 // Prints LV tree structure.
243 // ---
244
245   for (G4int i=0; i<fLogicalVolumes.size(); i++) {
246     G4LogicalVolume* lv = fLogicalVolumes[i];
247     G4cout << fPathName << lv->GetName() << G4endl;
248   }
249   for (G4int j=0; j<fStructures.size(); j++) { 
250     fStructures[j]->ListTree(); 
251   }
252 }
253         
254 //_____________________________________________________________________________
255 void AliLVStructure::ListTreeLong() const
256 {
257 // Prints LV tree structure with number of
258 // daughters (physical volume), indicates Boolean solid.
259 // ---
260
261   for (G4int i=0; i<fLogicalVolumes.size(); i++) {
262     G4LogicalVolume* lv = fLogicalVolumes[i];
263
264     G4cout << fPathName << lv->GetName() << " (" << lv->GetNoDaughters();
265             
266     if (dynamic_cast<G4BooleanSolid*>(lv->GetSolid()))
267       G4cout << ", B";
268
269     G4cout << ")" << G4endl;
270   }
271   for (G4int j=0; j<fStructures.size(); j++) { 
272     fStructures[j]->ListTreeLong(); 
273   }
274 }
275         
276 //_____________________________________________________________________________
277 void AliLVStructure::SetVerboseLevel(G4int verbose) 
278 {
279 // Sets verbose level.
280 // ---
281
282   fVerboseLevel = verbose;  
283   for (G4int i=0; i<fStructures.size(); i++) { 
284     fStructures[i]->SetVerboseLevel(verbose); 
285   }
286 }
287
288 #ifdef G4VIS_USE
289 //_____________________________________________________________________________
290 void AliLVStructure::SetTreeVisibility(G4bool visibility)       
291 {
292 // Sets visibility to all logical volumes in the structure 
293 // tree.
294 // ---
295
296   for (G4int i=0; i<fLogicalVolumes.size(); i++) {
297     G4LogicalVolume* lv = fLogicalVolumes[i];
298
299     const G4VisAttributes* kpVisAttributes = lv->GetVisAttributes();
300     G4VisAttributes* newVisAttributes; 
301     if (kpVisAttributes) {
302       G4Colour colour   = kpVisAttributes->GetColour();
303       newVisAttributes = new G4VisAttributes(colour); 
304     }
305     else
306       newVisAttributes = new G4VisAttributes();
307     delete kpVisAttributes;
308
309     newVisAttributes->SetVisibility(visibility); 
310
311     lv->SetVisAttributes(newVisAttributes);
312   }
313   for (G4int j=0; j<fStructures.size(); j++) { 
314     fStructures[j]->SetTreeVisibility(visibility); 
315   }
316 }
317
318 //_____________________________________________________________________________
319 void AliLVStructure::SetTreeColour(const G4String& colName)
320 {
321 // Sets colour specified  by name to all logical volumes
322 // in the structure tree.
323 // ---
324
325   for (G4int i=0; i<fLogicalVolumes.size(); i++) {
326     G4LogicalVolume* lv = fLogicalVolumes[i];
327
328     const G4VisAttributes* kpVisAttributes = lv->GetVisAttributes ();
329     G4VisAttributes* newVisAttributes; 
330     if (kpVisAttributes) {
331       G4bool oldVisibility = kpVisAttributes->IsVisible();
332       newVisAttributes = new G4VisAttributes(oldVisibility); 
333     }
334     else
335       newVisAttributes = new G4VisAttributes();
336     delete kpVisAttributes;
337
338     AliColourStore* pColours = AliColourStore::Instance();
339     G4Colour colour = pColours->GetColour(colName);
340     newVisAttributes->SetColour(colour);
341
342     lv->SetVisAttributes(newVisAttributes);
343   }
344   for (G4int j=0; j<fStructures.size(); j++) { 
345     fStructures[j]->SetTreeColour(colName); 
346   }
347 }
348 #endif             
349
350