ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DicomNestedPhantomParameterisation.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file DicomNestedPhantomParameterisation.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
28 //
29 //
30 
32 #include "DicomHandler.hh"
33 
34 #include "G4VPhysicalVolume.hh"
35 #include "G4VTouchable.hh"
36 #include "G4ThreeVector.hh"
37 #include "G4Box.hh"
38 #include "G4LogicalVolume.hh"
39 #include "G4Material.hh"
40 
41 #include "G4VisAttributes.hh"
42 #include "G4VVisManager.hh"
43 
44 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
45 
47  DicomHandler::GetDicomDataPath() + "/ColourMap.dat";
48 
49 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
52  std::vector<G4Material*>& mat,
53  G4int fnZ_, G4int fnY_, G4int fnX_,
54  G4String colorfile)
55 :
56  //G4VNestedParameterisation(),
57  fdX(voxelSize.x()), fdY(voxelSize.y()), fdZ(voxelSize.z()),
58  fnX(fnX_), fnY(fnY_), fnZ(fnZ_),
59  fMaterials(mat),
60  fMaterialIndices(0)
61 {
62  ReadColourData(colorfile);
63 }
64 
65 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
67 {
68 }
69 
70 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
72 {
73  //----- Add a G4VisAttributes for materials not defined in file;
74  G4VisAttributes* blankAtt = new G4VisAttributes;
75  blankAtt->SetVisibility( FALSE );
76  fColours["Default"] = blankAtt;
77 
78  std::ifstream fin(colourFile.c_str());
79  G4int nMate;
80  G4String mateName;
81  G4double cred, cgreen, cblue, copacity;
82  fin >> nMate;
83  for( G4int ii = 0; ii < nMate; ii++ )
84  {
85  fin >> mateName;
86  if(fin.eof())
87  break;
88  fin >> cred >> cgreen >> cblue >> copacity;
89  G4Colour colour( cred, cgreen, cblue, copacity );
90  G4VisAttributes* visAtt = new G4VisAttributes( colour );
91  visAtt->SetVisibility(true);
92  fColours[mateName] = visAtt;
93  mColours[ii] = new G4VisAttributes(*visAtt);
94  }
95 }
96 
97 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
99 SetNoVoxel( unsigned int nx, unsigned int ny, unsigned int nz )
100 {
101  fnX = nx;
102  fnY = ny;
103  fnZ = nz;
104 }
105 
106 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
109  const G4VTouchable* parentTouch)
110 {
111 
112  // protection for initialization and vis at idle state
113  //
114  if(parentTouch == nullptr)
115  return fMaterials[0];
116 
117  // Copy number of voxels.
118  // Copy number of X and Y are obtained from replication number.
119  // Copy nymber of Z is the copy number of current voxel.
120  G4int ix = parentTouch->GetReplicaNumber(0);
121  G4int iy = parentTouch->GetReplicaNumber(1);
122 
123  G4int copyID = ix + fnX*iy + fnX*fnY*iz;
124 
125  std::size_t matIndex = GetMaterialIndex(copyID);
126  static G4Material* mate = nullptr;
127  mate = fMaterials[matIndex];
128 
129  if(G4VVisManager::GetConcreteInstance() && physVol)
130  {
131  G4String mateName = fMaterials.at(matIndex)->GetName();
132  std::string::size_type iuu = mateName.find("__");
133  if( iuu != std::string::npos )
134  mateName = mateName.substr( 0, iuu );
135 
136  if(0 < fColours.count(mateName))
138  fColours.find(mateName)->second);
139  else
140  {
141  bool found = false;
142  for(const auto& itr : fColours)
143  {
144  G4String mat_color = itr.first;
145  auto len = mat_color.length();
146  if(mateName.find(mat_color) == 0 &&
147  mateName.length() > len && mateName[len] == '_')
148  {
150  fColours.find(mat_color)->second);
151  found = true;
152  }
153  if(found)
154  break;
155  }
156  if(!found)
157  {
158 
159  static uintmax_t n = 0;
160  if(n++ < 100)
161  G4cout << "Unknown material name " << mateName
162  << " for index " << matIndex << G4endl;
163  if(mColours.find(matIndex) != mColours.end())
165  mColours.find(matIndex)->second);
166  else
168  fColours.begin()->second);
169  }
170  }
171  }
172 
173  return mate;
174 }
175 
176 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
178 GetMaterialIndex( unsigned int copyNo ) const
179 {
180  //return *(fMaterialIndices+copyNo);
181  return unsigned(fMaterialIndices[copyNo]);
182 }
183 
184 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
185 // Number of Materials
186 // Material scanner is required for preparing physics tables and so on before
187 // starting simulation, so that G4 has to know number of materials.
188 //
190 {
191  return G4int(fMaterials.size());
192 }
193 
194 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
195 //
196 // GetMaterial
197 // This is needed for material scanner and realizing geometry.
198 //
200 {
201  return fMaterials[i];
202 }
203 
204 //
205 // Transformation of voxels.
206 //
208 ComputeTransformation(const G4int copyNo, G4VPhysicalVolume* physVol) const
209 {
210  // Position of voxels.
211  // x and y positions are already defined in DetectorConstruction by using
212  // replicated volume. Here only we need to define is z positions of voxels.
213  physVol->SetTranslation(G4ThreeVector(0.,0.,(2.*static_cast<double>(copyNo)
214  +1.)*fdZ - fdZ*fnZ));
215 }
216 
217 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
218 //
219 // Dimensions are always same in this RE02 example.
220 //
223 {
224  box.SetXHalfLength(fdX);
225  box.SetYHalfLength(fdY);
226  box.SetZHalfLength(fdZ);
227 }