78{
79 if(IsMaster())
80 {
81 G4cout << " ###### EndOfTSRunAction ###### " << G4endl;
82
83 aRun->GetNumberOfEvent();
84 std::ofstream fileout;
86 std::stringstream separator;
87
88 separator << "============================================================";
89
90 typedef std::set<G4int> IDSet_t;
91 IDSet_t IDs;
92
93
94 const TSRun* tsRun =
static_cast<const TSRun*
>(aRun);
95
96
97
98
99
100 std::vector<G4String> primScorerNames{ "EnergyDeposit", "NumberOfSteps" };
101 std::vector<G4String> fnames{ "mfd_tl", "mfd_tg" };
102 std::vector<G4double> units{ CLHEP::eV, CLHEP::keV, 1, 1 };
103 std::vector<G4String> unitstr{ "keV", "steps" };
104
105
106
107 auto print = [](std::ostream& fout, G4int first, G4double second,
108 G4double unit1, G4double unit2,
G4String unit2str) {
109 if(fout)
110 fout << first << " " << second / unit1 << G4endl;
111
112 G4cout << " " << std::setw(10) << first << " " << std::setw(15)
113 << std::setprecision(6) << std::fixed << second / unit2 << " "
114 << unit2str << G4endl;
115 G4cout.unsetf(std::ios::fixed);
116 };
117
118
119 auto stat_print = [](std::ostream& fout, G4int first,
G4StatAnalysis* stat,
121 G4double unit2,
G4String unit2str) {
122 if(!stat || !conv)
123 return;
124 auto fsecond = (*stat);
125 auto psecond = (*stat);
126 fsecond /= unit1;
127 psecond /= unit2;
128 if(fout)
129 {
130 fout << first << " " << fsecond << G4endl;
131 conv->ShowResult(fout);
132 }
133 std::stringstream ss;
134 ss << " " << std::setw(10) << first << " " << std::setw(15)
135 << std::setprecision(6) << std::fixed << psecond << " " << unit2str;
136
137 G4cout << ss.str() << G4endl;
138 };
139
140
141 for(unsigned i = 0; i < primScorerNames.size(); ++i)
142 {
143 for(unsigned j = 0; j < fnames.size(); ++j)
144 {
145 fname = fnames.at(j) + "_" + primScorerNames.at(i) + ".out";
146 fileout.open(fname);
147 G4cout << separator.str() << G4endl;
148 G4cout << " opened file " << fname << " for output" << G4endl;
149 G4cout << separator.str() << G4endl;
150
151 G4bool valid = true;
152 if(j == 0)
153 {
160
161 if(hitmap && hitmap->size() != 0)
162 {
163 for(auto itr = hitmap->begin(); itr != hitmap->end(); itr++)
164 {
165 if(!hitmap->GetObject(itr))
166 continue;
167 IDs.insert(itr->first);
168 std::get<0>(
fTypeCompare[primScorerNames.at(i)][itr->first]) =
169 *itr->second / units.at(i);
170 print(fileout, itr->first, *itr->second, units.at(i),
171 units.at(i + 1), unitstr.at(i));
172 }
173 }
174 else
175 {
176 valid = false;
177 }
178
179 if(statmap && statmap->size() != 0 && convmap && convmap->size() != 0)
180 {
181 auto stat_fname = "stat_" + fname;
182 std::ofstream statout;
183 statout.open(stat_fname);
184 for(auto itr = statmap->begin(); itr != statmap->end(); itr++)
185 {
186 G4int _f = statmap->GetIndex(itr);
189 stat_print(statout, _f, _s, _c, units.at(i), units.at(i + 1),
190 unitstr.at(i));
191 }
192 statout.close();
193 }
194 else
195 {
196 std::stringstream ss;
197 ss << " StatMap/ConvMap is either not "
198 << "created or the StatMap/ConvMap was empty";
199 if(statmap)
200 ss << " (StatMap size == " << statmap->size() << ")";
201 if(convmap)
202 ss << " (ConvMap size == " << convmap->size() << ")";
203
204 G4Exception("TSRunAction", "002", JustWarning,
205 G4String(primScorerNames.at(i) + ss.str()).c_str());
206 }
207
208 if(!valid)
209 {
210 G4Exception("TSRunAction", "000", JustWarning,
212 " HitsMap is either not "
213 "created or the HitsMap was empty")
214 .c_str());
215 }
216 }
217 else
218 {
221 if(hitmap && hitmap->
size() != 0)
222 {
223 for(
auto itr = hitmap->
begin(); itr != hitmap->
end(); itr++)
224 {
225 IDs.insert(itr->first);
226 std::get<1>(
fTypeCompare[primScorerNames.at(i)][itr->first]) =
227 *itr->second / units.at(i);
228 print(fileout, itr->first, *itr->second, units.at(i),
229 units.at(i + 1), unitstr.at(i));
230 }
231 }
232 else
233 {
234 valid = false;
235 }
236
237 if(!valid)
238 {
239 G4Exception("TSRunAction", "001", JustWarning,
241 " HitsMap is either not "
242 "created or the HitsMap was empty")
243 .c_str());
244 }
245 }
246
247 fileout.close();
248 G4cout << separator.str() << G4endl;
249 G4cout << " closed file " << fname << " for output" << G4endl;
250 }
251
254 if(hitmap && hitmap->size() != 0)
255 {
256 for(auto itr = hitmap->begin(); itr != hitmap->end(); itr++)
257 {
258 IDs.insert(itr->first);
259 std::get<2>(
fTypeCompare[primScorerNames.at(i)][itr->first]) =
260 itr->second / units.at(i);
261 }
262 }
263 }
264
265
266
267
268
269
270 fname = "mfd_diff.out";
271 fileout.open(fname);
272
273 G4cout << separator.str() << G4endl;
274 G4cout << " opened file " << fname << " for difference output" << G4endl;
275 G4cout << separator.str() << G4endl;
276
277 fileout << " " << std::setw(10) << "ID"
278 << " " << std::setw(30) << std::setprecision(12) << std::fixed
279 << "MFD value"
280 << " " << std::setw(30) << std::setprecision(12) << std::fixed
281 << "Atomic Hits Map value"
282 << " " << std::setw(30) << std::setprecision(8)
283 << std::scientific << "Difference"
284 << " " << std::setw(30) << std::setprecision(8)
285 << std::scientific << "Diff (MFD - MUTEXED)"
286 << " " << std::setw(30) << std::setprecision(8)
287 << std::scientific << "Diff (ATOM_HIT_MAP - MUTEXED)" << G4endl
288 << G4endl;
289
290
291
292
293
294
295
296
297
298
299
300
301
302
304
305 auto tp = (tm) ? tm->GetThreadPool() : nullptr;
306
307 auto join_output = [](std::string& lhs, std::string&& rhs) {
308 return (lhs += rhs);
309 };
310
311
312 auto report_type_comparison = [=](
const G4String& id,
314
315 auto report_subtype_comparison = [](const G4int& idx,
317 std::stringstream streamout;
318 G4double d01 = std::fabs(std::get<0>(value) - std::get<1>(value));
319 G4double d02 = std::fabs(std::get<0>(value) - std::get<2>(value));
320 G4double d03 = std::fabs(std::get<1>(value) - std::get<2>(value));
321
322 auto _print_diff = [&](const G4double& _dval) {
323 if(_dval > 0.0)
324 streamout << std::setprecision(8) << std::scientific
325 << std::setw(30) << _dval << " ";
326 else
327 streamout << std::setprecision(1) << std::fixed << std::setw(30)
328 << _dval << " ";
329 };
330
331 streamout << " " << std::setw(10) << idx << " " << std::setw(30)
332 << std::setprecision(12) << std::fixed << std::get<0>(value)
333 << " " << std::setw(30) << std::setprecision(12)
334 << std::fixed << std::get<1>(value) << " ";
335
336 _print_diff(d01);
337 _print_diff(d02);
338 _print_diff(d03);
339
340 streamout << G4endl;
341 return streamout.str();
342 };
343
344 std::stringstream streamout;
345 streamout << "\n\nType = " << id << "\n" << G4endl;
346 if(tp)
347 {
348
349 G4TaskGroup<std::string> tg(join_output, tp);
350
351 for(auto titr = comp.begin(); titr != comp.end(); ++titr)
352 tg.exec(report_subtype_comparison, titr->first, titr->second);
353
354
355
356 streamout << tg.join();
357 }
358 else
359 {
360
361
362 for(auto titr = comp.begin(); titr != comp.end(); ++titr)
363 streamout << report_subtype_comparison(titr->first, titr->second);
364 }
365
366 return streamout.str();
367 };
368
370 if(tp)
371 {
372 G4cout << "\n\nGenerating diff output via tasking... ";
373
374 G4TaskGroup<std::string> tg(join_output, tp);
376 tg.exec(report_type_comparison, itr->first, itr->second);
377
378 tasking_result = tg.join();
379 }
380
381
382
383 if(tp)
384 {
385
388 serial_result += report_type_comparison(itr->first, itr->second);
389
390
391 fileout << tasking_result;
392
393
394 if(serial_result != tasking_result)
395 {
396 G4Exception("TSRunAction", "003", JustWarning,
397 "Output written via tasking did not match output written "
398 "serially. Appending serial result to output file");
399 fileout
400 << "\n\n#================CORRECT_SERIAL_OUTPUT================#\n\n";
401 fileout << serial_result;
402 }
403 }
404 else
405 {
406
408 fileout << report_type_comparison(itr->first, itr->second);
409 }
410
411 fileout.close();
412 G4cout << " closed file " << fname << " for difference output" << G4endl;
413 G4cout << separator.str() << G4endl;
414 }
415}
G4THitsDeque< _Tp > G4StatContainer
This is an implementation of G4THitsMap<T> where the underlying type is G4atomic<T>,...
virtual size_t size() const
TypeCompare_t fTypeCompare
std::map< G4int, Compare_t > IDcompare_t
std::tuple< G4double, G4double, G4double > Compare_t
MutexHitsMap_t * GetMutexHitsMap(const G4String &) const
G4TAtomicHitsMap< G4double > * GetAtomicHitsMap(const G4String &) const
G4StatContainer< G4StatAnalysis > * GetStatMap(const G4String &collname) const
std::map< G4int, G4double > MutexHitsMap_t
G4THitsMap< G4double > * GetHitsMap(const G4String &collname) const
G4StatContainer< G4ConvergenceTester > * GetConvMap(const G4String &) const