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 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /digits_hits/utils/src/G4VScoreWriter.cc (Version 11.3.0) and /digits_hits/utils/src/G4VScoreWriter.cc (Version 10.3)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // G4VScoreWriter                              <<  26 //
 27 // ------------------------------------------- <<  27 // $Id: G4VScoreWriter.cc 99154 2016-09-07 08:06:30Z gcosmo $
                                                   >>  28 //
 28                                                    29 
 29 #include "G4VScoreWriter.hh"                       30 #include "G4VScoreWriter.hh"
 30                                                    31 
 31 #include "G4MultiFunctionalDetector.hh"            32 #include "G4MultiFunctionalDetector.hh"
                                                   >>  33 #include "G4SDParticleFilter.hh"
 32 #include "G4VPrimitiveScorer.hh"                   34 #include "G4VPrimitiveScorer.hh"
 33 #include "G4VScoringMesh.hh"                       35 #include "G4VScoringMesh.hh"
 34                                                    36 
 35 #include <map>                                     37 #include <map>
 36 #include <fstream>                                 38 #include <fstream>
 37                                                    39 
 38 void G4VScoreWriter::SetScoringMesh(G4VScoring <<  40 G4VScoreWriter::G4VScoreWriter()
 39 {                                              <<  41   : fScoringMesh(nullptr), verboseLevel(0) {
 40   // Should be checked for nullptr!            <<  42   fNMeshSegments[0] = fNMeshSegments[1] = fNMeshSegments[2] = 0;
 41   fScoringMesh = sm;                           <<  43 }
 42   fScoringMesh->GetNumberOfSegments(fNMeshSegm <<  44 
                                                   >>  45 G4VScoreWriter::~G4VScoreWriter() {
                                                   >>  46 }
                                                   >>  47 
                                                   >>  48 void G4VScoreWriter::SetScoringMesh(G4VScoringMesh * sm) {
                                                   >>  49     fScoringMesh = sm;
                                                   >>  50     fScoringMesh->GetNumberOfSegments(fNMeshSegments);
 43 }                                                  51 }
 44                                                    52 
 45 void G4VScoreWriter::DumpQuantityToFile(const      53 void G4VScoreWriter::DumpQuantityToFile(const G4String& psName,
 46                                         const      54                                         const G4String& fileName,
 47                                         const  <<  55                                         const G4String& option) {
 48 {                                              <<  56 
 49   // change the option string into lowercase t     57   // change the option string into lowercase to the case-insensitive.
 50   G4String opt = option;                           58   G4String opt = option;
 51   std::transform(opt.begin(), opt.end(), opt.b     59   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
 52                                                    60 
 53   // confirm the option                            61   // confirm the option
 54   if(opt.empty())                              <<  62   if(opt.size() == 0) opt = "csv";
 55     opt = "csv";                               << 
 56   if(opt.find("csv") == std::string::npos &&       63   if(opt.find("csv") == std::string::npos &&
 57      opt.find("sequence") == std::string::npos <<  64      opt.find("sequence") == std::string::npos) {
 58   {                                            <<  65     G4cerr << "ERROR : DumpToFile : Unknown option -> "
 59     G4cerr << "ERROR : DumpToFile : Unknown op <<  66      << option << G4endl;
 60     return;                                        67     return;
 61   }                                                68   }
 62                                                    69 
 63   // open the file                                 70   // open the file
 64   std::ofstream ofile(fileName);                   71   std::ofstream ofile(fileName);
 65   if(!ofile)                                   <<  72   if(!ofile) {
 66   {                                            <<  73     G4cerr << "ERROR : DumpToFile : File open error -> "
 67     G4cerr << "ERROR : DumpToFile : File open  <<  74      << fileName << G4endl;
 68     return;                                        75     return;
 69   }                                                76   }
 70   ofile << "# mesh name: " << fScoringMesh->Ge     77   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
 71                                                    78 
 72   using MeshScoreMap = G4VScoringMesh::MeshSco <<  79   
 73   // retrieve the map                              80   // retrieve the map
 74   MeshScoreMap fSMap = fScoringMesh->GetScoreM     81   MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
                                                   >>  82   
 75                                                    83 
 76   // NOLINTNEXTLINE(modernize-use-auto): Expli << 
 77   MeshScoreMap::const_iterator msMapItr = fSMa     84   MeshScoreMap::const_iterator msMapItr = fSMap.find(psName);
 78   if(msMapItr == fSMap.end())                  <<  85   if(msMapItr == fSMap.end()) {
 79   {                                            <<  86     G4cerr << "ERROR : DumpToFile : Unknown quantity, \""
 80     G4cerr << "ERROR : DumpToFile : Unknown qu <<  87      << psName << "\"." << G4endl;
 81            << G4endl;                          << 
 82     return;                                        88     return;
 83   }                                                89   }
 84                                                    90 
 85   std::map<G4int, G4StatDouble*>* score = msMa <<  91 
                                                   >>  92   std::map<G4int, G4StatDouble*> * score = msMapItr->second->GetMap();
 86   ofile << "# primitive scorer name: " << msMa     93   ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
 87   if(fact != 1.0)                              <<  94 
 88   {                                            << 
 89     ofile << "# multiplied factor : " << fact  << 
 90   }                                            << 
 91                                                    95 
 92   G4double unitValue = fScoringMesh->GetPSUnit     96   G4double unitValue = fScoringMesh->GetPSUnitValue(psName);
 93   G4String unit      = fScoringMesh->GetPSUnit <<  97   G4String unit = fScoringMesh->GetPSUnit(psName);
 94   G4String divisionAxisNames[3];                   98   G4String divisionAxisNames[3];
 95   fScoringMesh->GetDivisionAxisNames(divisionA     99   fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
 96   // index order                                  100   // index order
 97   ofile << "# i" << divisionAxisNames[0] << ", << 101   ofile << "# i" << divisionAxisNames[0]
 98         << ", i" << divisionAxisNames[2];      << 102   << ", i" << divisionAxisNames[1]
                                                   >> 103   << ", i" << divisionAxisNames[2];
 99   // unit of scored value                         104   // unit of scored value
100   ofile << ", total(value) ";                     105   ofile << ", total(value) ";
101   if(!unit.empty())                            << 106   if(unit.size() > 0) ofile << "[" << unit << "]";
102     ofile << "[" << unit << "]";               << 
103   ofile << ", total(val^2), entry" << G4endl;     107   ofile << ", total(val^2), entry" << G4endl;
104                                                   108 
105   // "sequence" option: write header info      << 109   // "sequence" option: write header info 
106   if(opt.find("sequence") != std::string::npos << 110   if(opt.find("sequence") != std::string::npos) {
107   {                                            << 111     ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
108     ofile << fNMeshSegments[0] << " " << fNMes << 112     << G4endl;
109           << fNMeshSegments[2] << G4endl;      << 
110   }                                               113   }
111                                                   114 
112   // write quantity                               115   // write quantity
113   G4long count = 0;                            << 116   long count = 0;
114   ofile << std::setprecision(16);  // for doub << 117   ofile << std::setprecision(16); // for double value with 8 bytes
115   for(G4int x = 0; x < fNMeshSegments[0]; x++) << 118   for(int x = 0; x < fNMeshSegments[0]; x++) {
116   {                                            << 119     for(int y = 0; y < fNMeshSegments[1]; y++) {
117     for(G4int y = 0; y < fNMeshSegments[1]; y+ << 120       for(int z = 0; z < fNMeshSegments[2]; z++) {
118     {                                          << 121   G4int idx = GetIndex(x, y, z);
119       for(G4int z = 0; z < fNMeshSegments[2];  << 122   
120       {                                        << 123   if(opt.find("csv") != std::string::npos)
121         G4int idx = GetIndex(x, y, z);         << 124     ofile << x << "," << y << "," << z << ",";
122                                                << 125 
123         if(opt.find("csv") != std::string::npo << 126   std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
124           ofile << x << "," << y << "," << z < << 127   if(value == score->end()) {
125                                                << 128     ofile << 0. << "," << 0. << "," << 0;
126         auto value = score->find(idx);         << 129   } else {
127         if(value == score->end())              << 130     ofile << (value->second->sum_wx())/unitValue << ","
128         {                                      << 131                 << (value->second->sum_wx2())/unitValue/unitValue << ","
129           ofile << 0. << "," << 0. << "," << 0 << 132                 << value->second->n();
130         }                                      << 133   }
131         else                                   << 134 
132         {                                      << 135   if(opt.find("csv") != std::string::npos) {
133           ofile << (value->second->sum_wx()) / << 136     ofile << G4endl;
134                 << (value->second->sum_wx2())  << 137   } else if(opt.find("sequence") != std::string::npos) {
135                      fact                      << 138     ofile << " ";
136                 << "," << value->second->n();  << 139     if(count++%5 == 4) ofile << G4endl;
137         }                                      << 140   }
138                                                << 141 
139         if(opt.find("csv") != std::string::npo << 142       } // z
140         {                                      << 143     } // y
141           ofile << G4endl;                     << 144   } // x
142         }                                      << 
143         else if(opt.find("sequence") != std::s << 
144         {                                      << 
145           ofile << " ";                        << 
146           if(count++ % 5 == 4)                 << 
147             ofile << G4endl;                   << 
148         }                                      << 
149                                                << 
150       }  // z                                  << 
151     }    // y                                  << 
152   }      // x                                  << 
153   ofile << std::setprecision(6);                  145   ofile << std::setprecision(6);
154                                                   146 
155   // close the file                               147   // close the file
156   ofile.close();                                  148   ofile.close();
                                                   >> 149   
157 }                                                 150 }
158                                                   151 
159 void G4VScoreWriter::DumpAllQuantitiesToFile(c    152 void G4VScoreWriter::DumpAllQuantitiesToFile(const G4String& fileName,
160                                              c << 153                                              const G4String& option) {
161 {                                              << 154 
162   // change the option string into lowercase t    155   // change the option string into lowercase to the case-insensitive.
163   G4String opt = option;                          156   G4String opt = option;
164   std::transform(opt.begin(), opt.end(), opt.b << 157   std::transform(opt.begin(), opt.end(), opt.begin(), (int (*)(int))(tolower));
165                                                   158 
166   // confirm the option                           159   // confirm the option
167   if(opt.empty())                              << 160   if(opt.size() == 0) opt = "csv";
168     opt = "csv";                               << 
169   if(opt.find("csv") == std::string::npos &&      161   if(opt.find("csv") == std::string::npos &&
170      opt.find("sequence") == std::string::npos << 162      opt.find("sequence") == std::string::npos) {
171   {                                            << 163     G4cerr << "ERROR : DumpToFile : Unknown option -> "
172     G4cerr << "ERROR : DumpToFile : Unknown op << 164      << option << G4endl;
173     return;                                       165     return;
174   }                                               166   }
175                                                   167 
176   // open the file                                168   // open the file
177   std::ofstream ofile(fileName);                  169   std::ofstream ofile(fileName);
178   if(!ofile)                                   << 170   if(!ofile) {
179   {                                            << 171     G4cerr << "ERROR : DumpToFile : File open error -> "
180     G4cerr << "ERROR : DumpToFile : File open  << 172      << fileName << G4endl;
181     return;                                       173     return;
182   }                                               174   }
183   ofile << "# mesh name: " << fScoringMesh->Ge    175   ofile << "# mesh name: " << fScoringMesh->GetWorldName() << G4endl;
184   if(fact != 1.0)                              << 
185   {                                            << 
186     ofile << "# multiplied factor : " << fact  << 
187   }                                            << 
188                                                   176 
189   // retrieve the map                             177   // retrieve the map
190   using MeshScoreMap = G4VScoringMesh::MeshSco << 
191   MeshScoreMap fSMap = fScoringMesh->GetScoreM    178   MeshScoreMap fSMap = fScoringMesh->GetScoreMap();
192   // NOLINTNEXTLINE(modernize-use-auto): Expli << 
193   MeshScoreMap::const_iterator msMapItr = fSMa    179   MeshScoreMap::const_iterator msMapItr = fSMap.begin();
194   std::map<G4int, G4StatDouble*>* score;       << 180   std::map<G4int, G4StatDouble*> * score;
195   for(; msMapItr != fSMap.end(); msMapItr++)   << 181   for(; msMapItr != fSMap.end(); msMapItr++) {
196   {                                            << 182 
197     G4String psname = msMapItr->first;            183     G4String psname = msMapItr->first;
198                                                   184 
199     score = msMapItr->second->GetMap();           185     score = msMapItr->second->GetMap();
200     ofile << "# primitive scorer name: " << ms    186     ofile << "# primitive scorer name: " << msMapItr->first << std::endl;
201                                                   187 
202     G4double unitValue = fScoringMesh->GetPSUn    188     G4double unitValue = fScoringMesh->GetPSUnitValue(psname);
203     G4String unit      = fScoringMesh->GetPSUn << 189     G4String unit = fScoringMesh->GetPSUnit(psname);
204     G4String divisionAxisNames[3];                190     G4String divisionAxisNames[3];
205     fScoringMesh->GetDivisionAxisNames(divisio    191     fScoringMesh->GetDivisionAxisNames(divisionAxisNames);
206     // index order                                192     // index order
207     ofile << "# i" << divisionAxisNames[0] <<  << 193     ofile << "# i" << divisionAxisNames[0]
208           << ", i" << divisionAxisNames[2];    << 194     << ", i" << divisionAxisNames[1]
                                                   >> 195     << ", i" << divisionAxisNames[2];
209     // unit of scored value                       196     // unit of scored value
210     ofile << ", total(value) ";                   197     ofile << ", total(value) ";
211     if(!unit.empty())                          << 198     if(unit.size() > 0) ofile << "[" << unit << "]";
212       ofile << "[" << unit << "]";             << 
213     ofile << ", total(val^2), entry" << G4endl    199     ofile << ", total(val^2), entry" << G4endl;
214                                                   200 
215     // "sequence" option: write header info    << 201 
216     if(opt.find("sequence") != std::string::np << 202     // "sequence" option: write header info 
217     {                                          << 203     if(opt.find("sequence") != std::string::npos) {
218       ofile << fNMeshSegments[0] << " " << fNM << 204       ofile << fNMeshSegments[0] << " " << fNMeshSegments[1] << " " << fNMeshSegments[2]
219             << fNMeshSegments[2] << G4endl;    << 205       << G4endl;
220     }                                             206     }
221                                                   207 
222     // write quantity                             208     // write quantity
223     G4long count = 0;                          << 209     long count = 0;
224     ofile << std::setprecision(16);  // for do << 210     ofile << std::setprecision(16); // for double value with 8 bytes
225     for(G4int x = 0; x < fNMeshSegments[0]; x+ << 211     for(int x = 0; x < fNMeshSegments[0]; x++) {
226     {                                          << 212       for(int y = 0; y < fNMeshSegments[1]; y++) {
227       for(G4int y = 0; y < fNMeshSegments[1];  << 213   for(int z = 0; z < fNMeshSegments[2]; z++) {
228       {                                        << 214     G4int idx = GetIndex(x, y, z);
229         for(G4int z = 0; z < fNMeshSegments[2] << 215     
230         {                                      << 216     if(opt.find("csv") != std::string::npos)
231           G4int idx = GetIndex(x, y, z);       << 217       ofile << x << "," << y << "," << z << ",";
232                                                << 218     
233           if(opt.find("csv") != std::string::n << 219     std::map<G4int, G4StatDouble*>::iterator value = score->find(idx);
234             ofile << x << "," << y << "," << z << 220       if(value == score->end()) {
235                                                << 221       ofile << 0. << "," << 0. << "," << 0;
236           auto value = score->find(idx);       << 222     } else {
237           if(value == score->end())            << 223       ofile << (value->second->sum_wx())/unitValue << ","
238           {                                    << 224                   << (value->second->sum_wx2())/unitValue/unitValue << ","
239             ofile << 0. << "," << 0. << "," << << 225                   << value->second->n();
240           }                                    << 226     }
241           else                                 << 227 
242           {                                    << 228     if(opt.find("csv") != std::string::npos) {
243             ofile << (value->second->sum_wx()) << 229       ofile << G4endl;
244                   << (value->second->sum_wx2() << 230     } else if(opt.find("sequence") != std::string::npos) {
245                        fact                    << 231       ofile << " ";
246                   << "," << value->second->n() << 232       if(count++%5 == 4) ofile << G4endl;
247           }                                    << 233     }
248                                                << 234 
249           if(opt.find("csv") != std::string::n << 235   } // z
250           {                                    << 236       } // y
251             ofile << G4endl;                   << 237     } // x
252           }                                    << 
253           else if(opt.find("sequence") != std: << 
254           {                                    << 
255             ofile << " ";                      << 
256             if(count++ % 5 == 4)               << 
257               ofile << G4endl;                 << 
258           }                                    << 
259                                                << 
260         }  // z                                << 
261       }    // y                                << 
262     }      // x                                << 
263     ofile << std::setprecision(6);                238     ofile << std::setprecision(6);
264                                                   239 
265   }  // for(; msMapItr ....)                   << 240   } // for(; msMapItr ....)
266                                                   241 
267   // close the file                               242   // close the file
268   ofile.close();                                  243   ofile.close();
                                                   >> 244   
269 }                                                 245 }
270                                                   246 
271 G4int G4VScoreWriter::GetIndex(G4int x, G4int  << 247 G4int G4VScoreWriter::GetIndex(G4int x, G4int y, G4int z) const {
272 {                                              << 248     //return x + y*fNMeshSegments[0] + z*fNMeshSegments[0]*fNMeshSegments[1];
273   return x * fNMeshSegments[1] * fNMeshSegment << 249     return x*fNMeshSegments[1]*fNMeshSegments[2] +y*fNMeshSegments[2]+z;
274 }                                                 250 }
                                                   >> 251 
275                                                   252