20 #include "G4RunManager.hh"
21 #include "G4UnitsTable.hh"
22 #include "G4SystemOfUnits.hh"
23 #include "G4Version.hh"
28 auto analysisManager = G4AnalysisManager::Instance();
29 #if G4VERSION_NUMBER >= 1100
30 analysisManager->SetDefaultFileType(
"csv");
32 G4cout <<
"Using " << analysisManager->GetType() << G4endl;
34 analysisManager->SetVerboseLevel(
verbose);
40 #if G4VERSION_NUMBER < 1100
41 delete G4AnalysisManager::Instance();
59 if((runActions!=
nullptr) && !(runActions->empty()))
61 for(
auto ua : *runActions)
62 { ua.second->BeginOfRunAction(run); }
70 auto analysisManager = G4AnalysisManager::Instance();
72 if(
verbose>0) G4cout <<
"eASTRunAction::BeginOfRunAction ### <" <<
fileName <<
"> is opened." << G4endl;
84 if((runActions!=
nullptr) && !(runActions->empty()))
86 for(
auto ua : *runActions)
87 { ua.second->EndOfRunAction(run); }
93 auto analysisManager = G4AnalysisManager::Instance();
95 analysisManager->Write();
96 analysisManager->CloseFile();
97 if(
verbose>0) G4cout <<
"eASTRunAction::Flush ### <" <<
fileName <<
"> is closed." << G4endl;
107 auto analysisManager = G4AnalysisManager::Instance();
108 analysisManager->SetVerboseLevel(
verbose);
113 G4cout <<
"################## registered histograms/plots" << G4endl;
114 G4cout <<
"id\thistID\thistType\tdetName-X\tpsName-X\tcollID-X\tcopyNo-X\tdetName-Y\tpsName-Y\tcollID-Y\tcopyNo-Y" << G4endl;
115 for(
auto itr :
IDMap)
117 G4cout << itr.first <<
"\t" << itr.second->histID <<
"\t";
118 if(itr.second->histType==1)
119 { G4cout <<
"1-D hist\t" << itr.second->meshName <<
"\t" << itr.second->primName <<
"\t" << itr.second->collID <<
"\t" << itr.second->idx; }
120 else if(itr.second->histType==2)
121 { G4cout <<
"1-D prof\t" << itr.second->meshName <<
"\t" << itr.second->primName <<
"\t" << itr.second->collID; }
128 auto hItr =
IDMap.find(
id);
129 return (hItr!=
IDMap.end());
132 #include "G4SDManager.hh"
133 using namespace G4Analysis;
138 for(
auto hItr : IDMap)
140 valid = SetPlotting(hItr.first,val);
148 auto hItr = IDMap.find(
id);
149 if(hItr==IDMap.end())
return false;
150 auto ht = hItr->second;
151 auto hTyp = ht->histType;
152 auto analysisManager = G4AnalysisManager::Instance();
154 { analysisManager->SetH1Plotting(ht->histID,val); }
156 { analysisManager->SetP1Plotting(ht->histID,val); }
166 G4String collName = mName;
169 auto cID = G4SDManager::GetSDMpointer()->GetCollectionID(collName);
170 if(cID<0)
return cID;
172 G4int
id = (cID+id_offset)*id_factor+cn+1;
173 auto histoTypeItr = IDMap.find(
id);
174 if(histoTypeItr!=IDMap.end())
return false;
175 if(
verbose) G4cout <<
"eASTRunAction::Create1D for <" << collName
176 <<
", copyNo=" << cn <<
"> is registered for hitCollectionID "
181 histTyp->histType = 1;
182 histTyp->meshName = mName;
183 histTyp->primName = pName;
189 #include "G4SDManager.hh"
190 #include "G4ScoringManager.hh"
191 #include "G4VScoringMesh.hh"
192 #include "G4VPrimitivePlotter.hh"
195 using MeshShape = G4VScoringMesh::MeshShape;
197 G4String collName = mName;
200 auto cID = G4SDManager::GetSDMpointer()->GetCollectionID(collName);
201 if(cID<0)
return cID;
203 auto sm = G4ScoringManager::GetScoringManagerIfExist();
205 auto mesh = sm->FindMesh(mName);
208 auto shape = mesh->GetShape();
209 if(shape!=MeshShape::realWorldLogVol && shape!=MeshShape::probe)
212 mesh->GetNumberOfSegments(nBin);
214 auto prim = mesh->GetPrimitiveScorer(pName);
217 auto pp =
dynamic_cast<G4VPrimitivePlotter*
>(prim);
221 G4int id0 = (cID+id_offset)*id_factor+1;
222 for(G4int cn=0; cn<nBin[0]; cn++)
225 auto histoTypeItr = IDMap.find(
id);
226 if(histoTypeItr!=IDMap.end())
228 if(
verbose) G4cout <<
"eASTRunAction::Create1D for <" << collName
229 <<
", copyNo=" << cn <<
"> is registered for hitCollectionID "
234 histTyp->histType = 1;
235 histTyp->histDup = nBin[0];
236 histTyp->meshName = mName;
237 histTyp->primName = pName;
239 histTyp->pplotter = pp;
245 #include "G4UIcommand.hh"
247 G4String& schem, G4bool logVal)
251 auto hIt = IDMap.find(id0);
252 if(hIt==IDMap.end())
return false;
254 auto analysisManager = G4AnalysisManager::Instance();
255 auto dup = (hIt->second)->histDup;
256 for(G4int ii=0;ii<dup;ii++)
259 auto hItr = IDMap.find(
id);
260 auto ht = hItr->second;
261 G4String mNam = ht->primName;
262 G4String nam = ht->meshName +
"_" + ht->primName;
266 mNam += G4UIcommand::ConvertToString(ht->idx);
268 nam += G4UIcommand::ConvertToString(ht->idx);
272 { hid = analysisManager->CreateH1(mNam,nam,nBin,valMin,valMax,unit,
"none",
"linear"); }
276 { hid = analysisManager->CreateH1(mNam,nam,nBin,valMin,valMax,unit,
"log10",
"linear"); }
279 hid = analysisManager->CreateH1(mNam,nam,nBin,valMin,valMax,unit,
"none",
"log");
280 analysisManager->SetH1XAxisIsLog(hid,
true);
284 if(
verbose) G4cout <<
"eASTRunAction::Set1D for " << mNam <<
" / " << nam
285 <<
" has the histogram ID " << hid << G4endl;
287 auto pp = ht->pplotter;
288 if(pp!=
nullptr) pp->Plot(ht->idx,hid);
295 auto hItr = IDMap.find(
id);
296 if(hItr==IDMap.end())
return false;
298 auto analysisManager = G4AnalysisManager::Instance();
299 auto hid = hItr->second->histID;
300 analysisManager->SetH1Title(hid,title);
301 analysisManager->SetH1XAxisTitle(hid,x_axis);
302 analysisManager->SetH1YAxisTitle(hid,y_axis);
308 auto hIt = IDMap.find(id0);
309 if(hIt==IDMap.end())
return false;
310 auto analysisManager = G4AnalysisManager::Instance();
311 auto dup = (hIt->second)->histDup;
312 for(G4int ii=0;ii<dup;ii++)
315 auto hItr = IDMap.find(
id);
316 analysisManager->SetH1YAxisIsLog(hItr->second->histID,val);
325 G4String collName = mName;
328 auto cID = G4SDManager::GetSDMpointer()->GetCollectionID(collName);
329 if(cID<0)
return cID;
331 G4int
id = (cID+2*id_offset)*id_factor;
332 auto histoTypeItr = IDMap.find(
id);
333 if(histoTypeItr!=IDMap.end())
return false;
334 if(
verbose) G4cout <<
"eASTRunAction::Create1P for <" << collName
335 <<
"> is registered for hitCollectionID "
340 histTyp->histType = 2;
341 histTyp->meshName = mName;
342 histTyp->primName = pName;
349 G4String& funcX,G4String& funcY,G4String& schem)
353 if(
verbose) G4cout <<
"eASTRunAction::Set1P for id = " <<
id << G4endl;
354 auto hItr = IDMap.find(
id);
355 if(hItr==IDMap.end())
return false;
357 auto ht = hItr->second;
358 if(
verbose) G4cout <<
"eASTRunAction::Set1P for " << ht->meshName <<
" / " << ht->primName << G4endl;
359 auto analysisManager = G4AnalysisManager::Instance();
361 G4double valMin = -0.5;
362 G4double valMax = G4double(nBin) - 0.5;
363 G4String nam = ht->meshName +
"_" + ht->primName;
364 auto hid = analysisManager->CreateP1(nam,ht->primName,nBin,
365 valMin,valMax,valYMin,valYMax,
"none",unit,funcX,funcY,schem);
367 if(
verbose) G4cout <<
"eASTRunAction::Set1P for " << ht->meshName <<
" / " << ht->primName
368 <<
" has the histogram ID " << hid << G4endl;
375 auto hItr = IDMap.find(
id);
376 if(hItr==IDMap.end())
return false;
378 auto analysisManager = G4AnalysisManager::Instance();
379 auto hid = hItr->second->histID;
380 analysisManager->SetP1Title(hid,title);
381 analysisManager->SetP1XAxisTitle(hid,x_axis);
382 analysisManager->SetP1YAxisTitle(hid,y_axis);
390 G4String collName = mName;
393 auto cID = G4SDManager::GetSDMpointer()->GetCollectionID(collName);
394 if(cID<0)
return cID;
396 G4int
id = NTMap.size();
397 if(
verbose) G4cout <<
"eASTRunAction::NtupleColumn : <" << collName
398 <<
", copyNo=" << cn <<
"> is registered for nTuple column "
403 histTyp->meshName = mName;
404 histTyp->primName = pName;
405 histTyp->meshName2 = unit;
407 { histTyp->fuct = 1./(G4UnitDefinition::GetValueOf(unit)); }
413 #include "G4UIcommand.hh"
417 if(NTMap.size()==0)
return;
419 auto analysisManager = G4AnalysisManager::Instance();
420 analysisManager->CreateNtuple(
"eASTNtuple",
"Scores for each event");
422 for(
auto itr : NTMap)
424 G4String colNam = itr.second->meshName;
426 colNam += itr.second->primName;
427 if(itr.second->idx != -1)
428 { colNam +=
"_"; colNam += G4UIcommand::ConvertToString(itr.second->idx); }
429 if(itr.second->meshName2 !=
"none")
430 { colNam +=
"["; colNam += itr.second->meshName2; colNam +=
"]"; }
431 analysisManager->CreateNtupleDColumn(colNam);
434 analysisManager->FinishNtuple();
438 #include "G4Threading.hh"
439 #include "G4UImanager.hh"
443 if(NTMap.size()==0)
return;
444 if(!(G4Threading::IsMultithreadedApplication()))
return;
446 auto analysisManager = G4AnalysisManager::Instance();
449 if(analysisManager->GetType()!=
"Csv")
return;
452 G4String targetFN = fileName +
"_nt_eASTNtuple_total.csv";
453 target.open(targetFN,std::ios::out);
455 enum { BUFSIZE = 4096 };
456 char* line =
new char[BUFSIZE];
458 G4String titleFN = fileName +
"_nt_eASTNtuple.csv";
463 title.getline(line,BUFSIZE);
464 if(title.eof())
break;
465 G4cout << line << G4endl;
466 target << line << G4endl;
470 auto nWorker = G4Threading::GetNumberOfRunningWorkerThreads();
471 G4String sourceFNBase = fileName +
"_nt_eASTNtuple_t";
472 for(G4int i = 0; i < nWorker; i++)
474 G4String sourceFN = sourceFNBase;
475 sourceFN += G4UIcommand::ConvertToString(i);
477 std::ifstream source;
481 G4ExceptionDescription ed; ed <<
"Source file <" << sourceFN <<
"> is not found.";
482 G4Exception(
"eASTRunAction::MergeNtuple()",
"eASTim12345",FatalException,ed);
486 source.getline(line,BUFSIZE);
487 if(line[0]==
'#')
continue;
488 if(source.eof())
break;
489 target << line << G4endl;
492 G4String scmd =
"rm -f ";
494 auto rc = system(scmd);
497 G4ExceptionDescription ed;
498 ed <<
"File <" << sourceFN <<
"> could not be deleted, thought it is merged.";
499 G4Exception(
"eASTRunAction::MergeNtuple()",
"eASTim12345",JustWarning,ed);
505 G4String cmd =
"mv ";
509 auto rcd = system(cmd);
512 G4ExceptionDescription ed;
513 ed <<
"File <" << targetFN <<
"> could not be renamed.";
514 G4Exception(
"eASTRunAction::MergeNtuple()",
"eASTim12345",JustWarning,ed);