Geant4  10.01.p02
DicomPartialDetectorConstruction.cc
Go to the documentation of this file.
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 //
26 // $Id: DicomPartialDetectorConstruction.cc 84839 2014-10-21 13:44:55Z gcosmo $
27 //
30 //
31 
32 #include "globals.hh"
33 
35 
36 #include "G4Box.hh"
37 #include "G4LogicalVolume.hh"
38 #include "G4VPhysicalVolume.hh"
39 #include "G4PVPlacement.hh"
40 #include "G4PVParameterised.hh"
41 #include "G4Material.hh"
42 #include "G4Element.hh"
43 #include "G4VisAttributes.hh"
44 #include "G4Colour.hh"
45 #include "G4ios.hh"
47 #include "G4NistManager.hh"
48 #include "G4UIcommand.hh"
49 #include "G4Tubs.hh"
50 
51 #include "G4tgbVolumeMgr.hh"
52 #include "G4tgbMaterialMgr.hh"
53 #include "G4tgrMessenger.hh"
54 #include "G4tgrMessenger.hh"
55 
56 #include "G4SystemOfUnits.hh"
57 
58 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
61  fPartialPhantomParam(0),
62  fNVoxels(0),
63  fDimX(0),
64  fDimY(0),
65  fDimZ(0),
66  fOffsetX(0),
67  fOffsetY(0),
68  fOffsetZ(0),
69  fMateIDs(0)
70 {
71 }
72 
73 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
75 {
76 }
77 
78 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
80 {
82 
83  //----- Build world
84  G4double worldXDimension = 1.*m;
85  G4double worldYDimension = 1.*m;
86  G4double worldZDimension = 1.*m;
87 
88  G4Box* world_box = new G4Box( "WorldSolid",
89  worldXDimension,
90  worldYDimension,
91  worldZDimension );
92 
93  fWorld_logic = new G4LogicalVolume( world_box,
94  fAir,
95  "WorldLogical",
96  0, 0, 0 );
97 
98  G4VPhysicalVolume* world_phys = new G4PVPlacement( 0,
99  G4ThreeVector(0,0,0),
100  "World",
101  fWorld_logic,
102  0,
103  false,
104  0 );
105 
106  ReadPhantomData();
107 
110 
111  return world_phys;
112 }
113 
114 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
116 {
117  G4String dataFile = "Data.dat";
118  std::ifstream finDF(dataFile.c_str());
119  G4String fname;
120  if(finDF.good() != 1 ) {
121  G4Exception(" DicomPartialDetectorConstruction::ReadPhantomData",
122  "",
124  " Problem reading data file: Data.dat");
125  }
126 
127  G4int compression;
128  finDF >> compression; // not used here
129 
130  G4int numFiles;
131  finDF >> numFiles; // only 1 file supported for the moment
132  if( numFiles != 1 ) {
133  G4Exception("DicomPartialDetectorConstruction::ReadPhantomData",
134  "",
136  "More than 1 DICOM file is not supported");
137  }
138  for(G4int i = 0; i < numFiles; i++ ) {
139  finDF >> fname;
140  //--- Read one data file
141  fname += ".g4pdcm";
142  ReadPhantomDataFile(fname);
143  }
144 
145  finDF.close();
146 
147 }
148 
149 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
151 const G4String& fname)
152 {
153  // G4String filename = "phantom.g4pdcm";
154 #ifdef G4VERBOSE
155  G4cout << " DicomDetectorConstruction::ReadPhantomDataFile opening file "
156  << fname << G4endl;
157 #endif
158  std::ifstream fin(fname.c_str(), std::ios_base::in);
159  if( !fin.is_open() ) {
160  G4Exception("DicomPartialDetectorConstruction::ReadPhantomDataFile",
161  "",
163  G4String("File not found " + fname).c_str());
164  }
165  G4int nMaterials;
166  fin >> nMaterials;
167  G4String stemp;
168  G4int nmate;
169  for( G4int ii = 0; ii < nMaterials; ii++ ){
170  fin >> nmate >> stemp;
171  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData reading nmate "
172  << ii << " = " << nmate << " mate " << stemp << G4endl;
173  if( ii != nmate )
174  G4Exception("DicomPartialDetectorConstruction::ReadPhantomData",
175  "",
177  "Material number should be in increasing order: \
178  wrong material number ");
179 
180  }
181 
182  fin >> fNVoxelX >> fNVoxelY >> fNVoxelZ;
183  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData nVoxel X/Y/Z "
184  << fNVoxelX << " " << fNVoxelY << " " << fNVoxelZ << G4endl;
185  fin >> fOffsetX >> fDimX;
186  fDimX = (fDimX - fOffsetX)/fNVoxelX;
187  fin >> fOffsetY >> fDimY;
188  fDimY = (fDimY - fOffsetY)/fNVoxelY;
189  fin >> fOffsetZ >> fDimZ;
190  fDimZ = (fDimZ - fOffsetZ)/fNVoxelZ;
191  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData voxelDimX "
192  << fDimX << " fOffsetX " << fOffsetX << G4endl;
193  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData voxelDimY "
194  << fDimY << " fOffsetY " << fOffsetY << G4endl;
195  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData voxelDimZ "
196  << fDimZ << " fOffsetZ " << fOffsetZ << G4endl;
197 
198  //--- Read voxels that are filled
199  fNVoxels = 0;
200  // G4bool* isFilled = new G4bool[fNVoxelX*fNVoxelY*fNVoxelZ];
201  // fFilledIDs = new size_t[fNVoxelZ*fNVoxelY+1];
202  //? fFilledIDs.insert(0);
203  int ifxmin1, ifxmax1;
204  for( G4int iz = 0; iz < fNVoxelZ; iz++ ) {
205  std::map< G4int, G4int > ifmin, ifmax;
206  for( G4int iy = 0; iy < fNVoxelY; iy++ ) {
207  fin >> ifxmin1 >> ifxmax1;
208  // check coherence ...
209 
210  ifmin[iy] = ifxmin1;
211  ifmax[iy] = ifxmax1;
212  G4int ifxdiff = ifxmax1-ifxmin1+1;
213  if( ifxmax1 == -1 && ifxmin1 == -1 ) ifxdiff = 0;
214  fFilledIDs.insert(std::pair<size_t,size_t>(ifxdiff+fNVoxels-1, ifxmin1));
215  G4cout << "DicomPartialDetectorConstruction::ReadPhantomData insert "
216  << " FilledIDs " << ifxdiff+fNVoxels-1 << " min " << ifxmin1
217  << " N= " << fFilledIDs.size() << G4endl;
218  //filledIDs[iz*fNVoxelY+iy+1] = ifxmax1-ifxmin1+1;
219  for( G4int ix = 0; ix < fNVoxelX; ix++ ) {
220  if( ix >= G4int(ifxmin1) && ix <= G4int(ifxmax1) ) {
221  fNVoxels++;
222  } else {
223  }
224  }
225  }
226  fFilledMins[iz] = ifmin;
227  fFilledMaxs[iz] = ifmax;
228  }
229 
230  //--- Read material IDs
231  G4int mateID1;
232  fMateIDs = new size_t[fNVoxelX*fNVoxelY*fNVoxelZ];
233  G4int copyNo = 0;
234  for( G4int iz = 0; iz < fNVoxelZ; iz++ ) {
235  std::map< G4int, G4int > ifmin = fFilledMins[iz];
236  std::map< G4int, G4int > ifmax = fFilledMaxs[iz];
237  for( G4int iy = 0; iy < fNVoxelY; iy++ ) {
238  ifxmin1 = ifmin[iy];
239  ifxmax1 = ifmax[iy];
240  for( G4int ix = 0; ix < fNVoxelX; ix++ ) {
241  if( ix >= G4int(ifxmin1) && ix <= G4int(ifxmax1) ) {
242  fin >> mateID1;
243  if( mateID1 < 0 || mateID1 >= nMaterials ) {
244  G4Exception("DicomPartialDetectorConstruction::ReadPhantomData",
245  "",
247  G4String("Wrong index in phantom file: It should be between 0 and "
248  + G4UIcommand::ConvertToString(nMaterials-1)
249  + ", while it is "
250  + G4UIcommand::ConvertToString(mateID1)).c_str());
251  }
252  fMateIDs[copyNo] = mateID1;
253  copyNo++;
254  }
255  }
256  }
257  }
258 
260 
261  fin.close();
262 }
263 
264 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
266 {
267  std::map<G4int, std::pair<G4double,G4double> > densiMinMax;
268  std::map<G4int, std::pair<G4double,G4double> >::iterator mpite;
269  for( size_t ii = 0; ii < fOriginalMaterials.size(); ii++ ){
270  densiMinMax[ii] = std::pair<G4double,G4double>(DBL_MAX,-DBL_MAX);
271  }
272 
273  //----- Define density differences (maximum density difference to create
274  // a new material)
275  char* part = getenv( "DICOM_CHANGE_MATERIAL_DENSITY" );
276  G4double densityDiff = -1.;
277  if( part ) densityDiff = G4UIcommand::ConvertToDouble(part);
278  std::map<G4int,G4double> densityDiffs;
279  if( densityDiff != -1. ) {
280  for( size_t ii = 0; ii < fOriginalMaterials.size(); ii++ ){
281  densityDiffs[ii] = densityDiff; //currently all materials
282  //with same step
283  }
284  }else {
285  if( fMaterials.size() == 0 ) { // do it only for first slice
286  for( unsigned int ii = 0; ii < fOriginalMaterials.size(); ii++ ){
287  fMaterials.push_back( fOriginalMaterials[ii] );
288  }
289  }
290  }
291 
292  // densityDiffs[0] = 0.0001; //fAir
293 
294  //--- Calculate the average material density for each material/density bin
295  std::map< std::pair<G4Material*,G4int>, matInfo* > newMateDens;
296 
297  G4double dens1;
298  size_t ifxmin1, ifxmax1;
299 
300  //---- Read the material densities
301  G4int copyNo = 0;
302  for( G4int iz = 0; iz < fNVoxelZ; iz++ ) {
303  std::map< G4int, G4int > ifmin = fFilledMins[iz];
304  std::map< G4int, G4int > ifmax = fFilledMaxs[iz];
305  for( G4int iy = 0; iy < fNVoxelY; iy++ ) {
306  ifxmin1 = ifmin[iy];
307  ifxmax1 = ifmax[iy];
308  for( G4int ix = 0; ix < fNVoxelX; ix++ ) {
309  if( ix >= G4int(ifxmin1) && ix <= G4int(ifxmax1) ) {
310  fin >> dens1;
311  //--- store the minimum and maximum density for each material
312  //(just for printing)
313  mpite = densiMinMax.find( fMateIDs[copyNo] );
314  if( dens1 < (*mpite).second.first ) (*mpite).second.first = dens1;
315  if( dens1 > (*mpite).second.second ) (*mpite).second.second = dens1;
316 
317  //--- Get material from original list of material in file
318  int mateID = fMateIDs[copyNo];
319  G4Material* mate = fOriginalMaterials[mateID];
320  // G4cout << copyNo << " mateID " << mateID << G4endl;
321  //--- Check if density is equal to the original material density
322  if( std::fabs(dens1 - mate->GetDensity()/g*cm3 ) < 1.e-9 ) continue;
323 
324  //--- Build material name with fOriginalMaterials name + density
325  // float densityBin = densityDiffs[mateID] *
326  // (G4int(dens1/densityDiffs[mateID])+0.5);
327  G4String newMateName = mate->GetName();
328 
329  G4int densityBin = 0;
330  // G4cout << " densityBin " << densityBin << " "
331  // << dens1 << " " <<densityDiffs[mateID] << G4endl;
332 
333  if( densityDiff != -1.) {
334  densityBin = (G4int(dens1/densityDiffs[mateID]));
335  newMateName =
336  mate->GetName()+G4UIcommand::ConvertToString(densityBin);
337  }
338 
339  //--- Look if it is the first voxel with this material/densityBin
340  std::pair<G4Material*,G4int> matdens(mate, densityBin );
341 
342  std::map< std::pair<G4Material*,G4int>, matInfo* >::iterator mppite
343  = newMateDens.find( matdens );
344  if( mppite != newMateDens.end() ){
345  matInfo* mi = (*mppite).second;
346  mi->sumdens += dens1;
347  mi->nvoxels++;
348  fMateIDs[copyNo] = fOriginalMaterials.size()-1 + mi->id;
349  // G4cout << copyNo << " mat new again "
350  //<< fOriginalMaterials.size()-1 + mi->id << " " << mi->id << G4endl;
351  } else {
352  matInfo* mi = new matInfo;
353  mi->sumdens = dens1;
354  mi->nvoxels = 1;
355  mi->id = newMateDens.size()+1;
356  newMateDens[matdens] = mi;
357  fMateIDs[copyNo] = fOriginalMaterials.size()-1 + mi->id;
358  // G4cout << copyNo << " mat new first "
359  // << fOriginalMaterials.size()-1 + mi->id << G4endl;
360  }
361  copyNo++;
362  // G4cout << ix << " " << iy << " " << iz
363  // << " filling fMateIDs " << copyNo << " = " << atoi(cid)-1 << G4endl;
364  // fMateIDs[copyNo] = atoi(cid)-1;
365  }
366  }
367  }
368  }
369 
370  //----- Build the list of phantom materials that go to Parameterisation
371  //--- Add original materials
372  std::vector<G4Material*>::const_iterator mimite;
373  for( mimite = fOriginalMaterials.begin(); mimite != fOriginalMaterials.end();
374  mimite++ ){
375  fPhantomMaterials.push_back( (*mimite) );
376  }
377  //
378  //---- Build and add new materials
379  std::map< std::pair<G4Material*,G4int>, matInfo* >::iterator mppite;
380  for( mppite= newMateDens.begin(); mppite != newMateDens.end(); mppite++ ){
381  G4double averdens = (*mppite).second->sumdens/(*mppite).second->nvoxels;
382  G4double saverdens = G4int(1000.001*averdens)/1000.;
383  G4cout << "GmReadPhantomGeometry::ReadVoxelDensities AVER DENS "
384  << averdens << " -> " << saverdens << " -> "
385  << G4int(1000*averdens) << " " << G4int(1000*averdens)/1000 << " "
386  << G4int(1000*averdens)/1000. << G4endl;
387 
388  G4cout << "GmReadPhantomGeometry::ReadVoxelDensities AVER DENS "
389  << averdens << " -> " << saverdens << " -> "
390  << G4UIcommand::ConvertToString(saverdens) << " Nvoxels "
391  <<(*mppite).second->nvoxels << G4endl;
392  G4String mateName = ((*mppite).first).first->GetName() + "_" +
393  G4UIcommand::ConvertToString(saverdens);
395  (*mppite).first.first,
396  averdens, mateName ) );
397  }
398 
399 }
400 
401 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
403 {
404  //build a clinder that encompass the partial phantom built in the example
405  // /dicom/intersectWithUserVolume 0. 0. 0. 0.*deg 0. 0. TUBE 0. 200. 100.
406  //---- Extract number of voxels and voxel dimensions
407 
408  G4String contname= "PhantomContainer";
409 
410  //----- Define the volume that contains all the voxels
411  G4Tubs* container_tube = new G4Tubs(contname,0., 200., 100., 0., 360*deg);
412 
414  new G4LogicalVolume( container_tube,
415  fAir,
416  contname,
417  0, 0, 0 );
418 
419  G4ThreeVector posCentreVoxels(0.,0.,0.);
420  //G4cout << " PhantomContainer posCentreVoxels " << posCentreVoxels << G4endl;
422 
424  new G4PVPlacement(rotm, // rotation
425  posCentreVoxels,
426  fContainer_logic, // The logic volume
427  contname, // Name
428  fWorld_logic, // Mother
429  false, // No op. bool.
430  1); // Copy number
431 
432 }
433 
434 //....oooOO0OOooo........oooOO0OOooo........oooOO0OOooo........oooOO0OOooo......
436 {
437  G4String OptimAxis = "kXAxis";
438  G4bool bSkipEqualMaterials = 0;
439  G4int regStructureID = 2;
440 
441  G4ThreeVector posCentreVoxels(0.,0.,0.);
442 
443  //----- Build phantom
444  G4String voxelName = "phantom";
445  G4Box* phantom_solid = new G4Box(voxelName, fDimX/2., fDimY/2., fDimZ/2. );
446  G4LogicalVolume* phantom_logic =
447  new G4LogicalVolume( phantom_solid,
448  fAir,
449  voxelName,
450  0, 0, 0 );
451  G4bool pVis = 0;
452  if( !pVis ) {
453  G4VisAttributes* visAtt = new G4VisAttributes( FALSE );
454  phantom_logic->SetVisAttributes( visAtt );
455  }
456 
457  G4double theSmartless = 0.5;
458  // fContainer_logic->SetSmartless( theSmartless );
459  phantom_logic->SetSmartless( theSmartless );
460 
462 
467 
469 
471 
473 
474  // G4cout << " Number of Materials " << fPhantomMaterials.size() << G4endl;
475  // G4cout << " SetMaterialIndices(0) " << fMateIDs[0] << G4endl;
476 
477  G4PVParameterised * phantom_phys = 0;
478  if( OptimAxis == "kUndefined" ) {
479  phantom_phys =
480  new G4PVParameterised(voxelName,phantom_logic,fContainer_logic,
482  } else if( OptimAxis == "kXAxis" ) {
483  // G4cout << " optim kX " << G4endl;
484  phantom_phys =
485  new G4PVParameterised(voxelName,phantom_logic,fContainer_logic,
486  // kXAxis, fNVoxelX*fNVoxelY*fNVoxelZ, fPartialPhantomParam);
488  fPartialPhantomParam->SetSkipEqualMaterials(bSkipEqualMaterials);
489  } else {
490  G4Exception("GmReadPhantomGeometry::ConstructPhantom",
491  "",
493  G4String("Wrong argument to parameter \
494  GmReadPhantomGeometry:Phantom:OptimAxis: \
495  Only allowed 'kUndefined' or 'kXAxis', it is: "+OptimAxis).c_str());
496  }
497 
498  phantom_phys->SetRegularStructureId(regStructureID);
499 
500 }
std::map< G4int, std::map< G4int, G4int > > fFilledMins
CLHEP::Hep3Vector G4ThreeVector
CLHEP::HepRotation G4RotationMatrix
Definition: G4Box.hh:64
const G4String & GetName() const
Definition: G4Material.hh:178
Definition: G4Tubs.hh:85
G4double GetDensity() const
Definition: G4Material.hh:180
static G4String ConvertToString(G4bool boolVal)
Definition: G4UIcommand.cc:371
int G4int
Definition: G4Types.hh:78
void SetFilledIDs(std::multimap< G4int, G4int > fid)
G4PartialPhantomParameterisation * fPartialPhantomParam
void ReadVoxelDensitiesPartial(std::ifstream &fin, std::map< G4int, std::map< G4int, G4int > > ifxmin, std::map< G4int, std::map< G4int, G4int > > ifxmax)
G4Material * BuildMaterialWithChangingDensity(const G4Material *origMate, float density, G4String newMateName)
G4GLOB_DLL std::ostream G4cout
void SetVoxelDimensions(G4double halfx, G4double halfy, G4double halfz)
Definition of the DicomPartialDetectorConstruction class.
static const double deg
Definition: G4SIunits.hh:133
bool G4bool
Definition: G4Types.hh:79
G4double iz
Definition: TRTMaterials.hh:39
void SetFilledMins(std::map< G4int, std::map< G4int, G4int > > fmins)
#define FALSE
Definition: globals.hh:52
static G4double ConvertToDouble(const char *st)
Definition: G4UIcommand.cc:443
static const double cm3
Definition: G4SIunits.hh:108
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
Definition: G4Exception.cc:41
virtual void SetRegularStructureId(G4int Code)
void SetSmartless(G4double s)
static const double g
Definition: G4SIunits.hh:162
std::map< G4int, std::map< G4int, G4int > > fFilledMaxs
void SetNoVoxel(size_t nx, size_t ny, size_t nz)
Dicom detector construction.
#define G4endl
Definition: G4ios.hh:61
static const double m
Definition: G4SIunits.hh:110
void SetMaterialIndices(size_t *matInd)
double G4double
Definition: G4Types.hh:76
void SetMaterials(std::vector< G4Material * > &mates)
std::vector< G4Material * > fMaterials
std::vector< G4Material * > fOriginalMaterials
#define DBL_MAX
Definition: templates.hh:83
void SetVisAttributes(const G4VisAttributes *pVA)
void SetSkipEqualMaterials(G4bool skip)