dicomrt_addmcdose.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function [totalMCdose,totalMCerror] = dicomrt_addmcdose(MCdose,MCerror)
  2. % dicomrt_addmcdose(MCdose,MCerror)
  3. %
  4. % Add segment's contribution of Monte Carlo 3D dose distribution calculated for rtplanformc
  5. %
  6. % MCdose and MCerror are two cell arrays with the following structure
  7. %
  8. % beam name 3d matrix/segment
  9. % --------------------------------------
  10. % | [beam 1] | [1st segment 3dmatrix ] |
  11. % | | [1st segment 3dmatrix ] |
  12. % | | |
  13. % | | [nth segment 3dmatrix ] |
  14. % --------------------------------------
  15. % | ... ... |
  16. % --------------------------------------
  17. % | [beam 2] | [1st segment 3dmatrix ] |
  18. % | | [1st segment 3dmatrix ] |
  19. % | | |
  20. % | | [nth segment 3dmatrix ] |
  21. % --------------------------------------
  22. %
  23. % Example:
  24. %
  25. % [totalMCdose,totalMCerror]=dicomrt_addmcdose(A,B)
  26. %
  27. % add all the 3D MC dose and errors from segments, stored respectively in A and B,
  28. % and return them in totalMCdose and totalMCerror
  29. %
  30. % See also dicomrt_read3ddose, dicomrt_loaddose, dicomrt_loadmcdose
  31. %
  32. % Copyright (C) 2002 Emiliano Spezi (emiliano.spezi@physics.org)
  33. % Check number of argument
  34. error(nargchk(1,2,nargin))
  35. % Check case and set-up some parameters and variables
  36. if nargin==1
  37. [study_temp,type_dose,label]=dicomrt_checkinput(MCdose,1);
  38. MCdose=dicomrt_varfilter(study_temp);
  39. else
  40. [study_temp,type_dose,label]=dicomrt_checkinput(MCdose,1);
  41. [studye_temp,type_dose,label]=dicomrt_checkinput(MCerror,1);
  42. MCdose=dicomrt_varfilter(study_temp);
  43. MCerror=dicomrt_varfilter(studye_temp);
  44. end
  45. % Check case
  46. if nargin==2 & (iscell(MCdose)~=1 | iscell(MCerror)~=1)
  47. error('dicomrt_addmcdose: Dose or error matrices do not have the expected format (i.e. cell). Exit now!')
  48. elseif iscell(MCdose)~=1
  49. error('dicomrt_addmcdose: Dose or error matrices do not have the expected format (i.e. cell). Exit now!')
  50. end
  51. % Add segment's dose
  52. if nargin == 2 % dose and error to be added
  53. for i=1:size(MCdose,1); % loop over beam
  54. if iscell(MCdose{i,2})==1 % likely segment contribution
  55. for j=1:size(MCdose{i,2},2); % loop over segment
  56. % NOTE: the sum of absolute errors is used to calculate the
  57. % total absolute error ...
  58. if i==1 & j==1
  59. totalMCdose=MCdose{i,2}{j};
  60. totalMCerror_abs=MCerror{i,2}{j}.*MCdose{i,2}{j};
  61. else
  62. totalMCdose=totalMCdose+MCdose{i,2}{j};
  63. totalMCerror_abs=totalMCerror_abs+MCerror{i,2}{j}.*MCdose{i,2}{j};
  64. end
  65. end % end loop over segments
  66. else % likely beam contribution
  67. if i==1
  68. totalMCdose=MCdose{i,2};
  69. totalMCerror_abs=MCerror{i,2}.*MCdose{i,2};
  70. else
  71. totalMCdose=totalMCdose+MCdose{i,2};
  72. totalMCerror_abs=totalMCerror_abs+MCerror{i,2}.*MCdose{i,2};
  73. end
  74. end
  75. end % end loop over beams
  76. % ... now we calculate the relative error for the total dose
  77. totalMCerror=totalMCerror_abs./totalMCdose;
  78. elseif nargin == 1 % only dose to be added
  79. for i=1:size(MCdose,1); % loop over beam
  80. if iscell(MCdose{i,2})==1 % likely segment contribution
  81. for j=1:size(MCdose{i,2},2); % loop over segment
  82. % NOTE: the sum of absolute errors is used to calculate the
  83. % total absolute error ...
  84. if i==1 & j==1
  85. totalMCdose=MCdose{i,2}{j};
  86. else
  87. totalMCdose=totalMCdose+MCdose{i,2}{j};
  88. end
  89. end % end loop over segments
  90. else % likely beam contribution
  91. if i==1
  92. totalMCdose=MCdose{i,2};
  93. else
  94. totalMCdose=totalMCdose+MCdose{i,2};
  95. end
  96. end
  97. end % end loop over beams
  98. end
  99. if nargin == 1
  100. totalMCdose=dicomrt_restorevarformat(study_temp,totalMCdose);
  101. else
  102. totalMCdose=dicomrt_restorevarformat(study_temp,totalMCdose);
  103. totalMCerror=dicomrt_restorevarformat(studye_temp,totalMCerror);
  104. end