27 #include <HepMC/SimpleVector.h>
30 #include <TDatabasePDG.h>
34 #include <TParticlePDG.h>
37 #include <Geant4/G4RunManager.hh>
38 #include <Geant4/G4ScoringManager.hh>
39 #include <Geant4/G4String.hh>
40 #include <Geant4/G4SystemOfUnits.hh>
41 #include <Geant4/G4THitsMap.hh>
42 #include <Geant4/G4ThreeVector.hh>
43 #include <Geant4/G4Types.hh>
44 #include <Geant4/G4UImanager.hh>
45 #include <Geant4/G4VScoringMesh.hh>
46 #include <Geant4/G4Version.hh>
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wshadow"
50 #include <boost/format.hpp>
51 #pragma GCC diagnostic pop
70 G4RunManager *runManager = G4RunManager::GetRunManager();
71 if (runManager ==
nullptr)
73 cout <<
"PHG4ScoringManager::InitRun - fatal error: G4RunManager was not initialized yet. Please do include the Geant4 simulation in this Fun4All run." << endl;
78 G4ScoringManager *scoringManager = G4ScoringManager::GetScoringManager();
82 G4UImanager *UImanager = G4UImanager::GetUIpointer();
89 cout <<
"PHG4ScoringManager::InitRun - execute Geatn4 command: " <<
cmd << endl;
91 UImanager->ApplyCommand(
cmd.c_str());
96 cout <<
"PHG4ScoringManager::InitRun - Making PHTFileServer " <<
m_outputFileName
102 TH1D *
h =
new TH1D(
"hNormalization",
103 "Normalization;Items;Summed quantity", 10, .5, 10.5);
105 h->GetXaxis()->SetBinLabel(i++,
"Event count");
106 h->GetXaxis()->SetBinLabel(i++,
"Collision count");
107 h->GetXaxis()->SetBinLabel(i++,
"Event count accepted");
108 h->GetXaxis()->SetBinLabel(i++,
"Collision count accepted");
110 h->GetXaxis()->LabelsOption(
"v");
114 "Charged particle #eta distribution;#eta;Count",
118 "Vertex z distribution;z [cm];Count",
138 TH1D *h_norm =
dynamic_cast<TH1D *
>(hm->
getHisto(
"hNormalization"));
141 h_norm->Fill(
"Event count", 1);
143 PHHepMCGenEventMap *geneventmap = findNode::getClass<PHHepMCGenEventMap>(topNode,
"PHHepMCGenEventMap");
146 static bool once =
true;
150 cout <<
"PHG4ScoringManager::process_event - - missing node PHHepMCGenEventMap. Skipping HepMC stat." << std::endl;
155 h_norm->Fill(
"Collision count", geneventmap->
size());
157 TH1D *hVertexZ =
dynamic_cast<TH1D *
>(hm->
getHisto(
"hVertexZ"));
160 for (
const auto genevntpair : geneventmap->
get_map())
181 h_norm->Fill(
"Collision count accepted", geneventmap->
size());
184 TH1D *hNChEta =
dynamic_cast<TH1D *
>(hm->
getHisto(
"hNChEta"));
187 PHG4InEvent *ineve = findNode::getClass<PHG4InEvent>(topNode,
"PHG4INEVENT");
190 cout <<
"PHG4ScoringManager::process_event - Error - "
191 <<
"unable to find DST node "
192 <<
"PHG4INEVENT" << endl;
197 for (
auto particle_iter = primary_range.first;
198 particle_iter != primary_range.second;
203 TParticlePDG *pdg_p = TDatabasePDG::Instance()->GetParticle(p->
get_pid());
205 if (fabs(pdg_p->Charge()) > 0)
208 if (pvec.Perp2() > 0)
211 hNChEta->Fill(pvec.PseudoRapidity());
217 h_norm->Fill(
"Event count accepted", 1);
228 cout <<
"PHG4ScoringManager::End - save results to " <<
m_outputFileName << endl;
234 for (
unsigned int i = 0;
i < hm->
nHistos();
i++)
248 G4ScoringManager *scoringManager = G4ScoringManager::GetScoringManager();
251 for (
unsigned int imesh = 0; imesh < scoringManager->GetNumberOfMesh(); ++imesh)
253 G4VScoringMesh *g4mesh = scoringManager->GetMesh(imesh);
256 const string meshName(g4mesh->GetWorldName().data());
259 cout <<
"PHG4ScoringManager::makeScoringHistograms - processing mesh " << meshName <<
": " << endl;
264 G4int nMeshSegments[3];
265 g4mesh->GetNumberOfSegments(nMeshSegments);
267 G4String divisionAxisNames[3];
268 g4mesh->GetDivisionAxisNames(divisionAxisNames);
271 const G4ThreeVector meshSize = g4mesh->GetSize();
272 const G4ThreeVector meshTranslate = g4mesh->GetTranslation();
273 #if G4VERSION_NUMBER >= 1060
274 const G4VScoringMesh::MeshShape meshShape = g4mesh->GetShape();
276 const MeshShape meshShape = g4mesh->GetShape();
279 vector<double> meshBoundMin = {std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN()};
281 vector<double> meshBoundMax = {std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN(), std::numeric_limits<double>::signaling_NaN()};
282 #if G4VERSION_NUMBER >= 1060
285 if (meshShape == boxMesh)
288 meshBoundMin[0] = (-meshSize[0] + meshTranslate[0]) /
cm;
289 meshBoundMax[0] = (meshSize[0] + meshTranslate[0]) /
cm;
290 meshBoundMin[1] = (-meshSize[1] + meshTranslate[1]) /
cm;
291 meshBoundMax[1] = (meshSize[1] + meshTranslate[1]) /
cm;
292 meshBoundMin[2] = (-meshSize[2] + meshTranslate[2]) /
cm;
293 meshBoundMax[2] = (meshSize[2] + meshTranslate[2]) /
cm;
295 divisionAxisNames[0] +=
" [cm]";
296 divisionAxisNames[1] +=
" [cm]";
297 divisionAxisNames[2] +=
" [cm]";
299 #if G4VERSION_NUMBER >= 1060
300 else if (meshShape == G4VScoringMesh::MeshShape::cylinder)
302 else if (meshShape == cylinderMesh)
314 meshBoundMin[0] = (-meshSize[1] + meshTranslate[0]) /
cm;
315 meshBoundMax[0] = (meshSize[1] + meshTranslate[0]) /
cm;
317 meshBoundMax[1] = 2 * M_PI;
319 meshBoundMax[2] = meshSize[0] /
cm;
321 divisionAxisNames[0] +=
" [cm]";
322 divisionAxisNames[1] +=
" [rad]";
323 divisionAxisNames[2] +=
" [cm]";
327 cout <<
"PHG4ScoringManager::makeScoringHistograms - Error - unsupported mesh shape " << (int) meshShape <<
". Skipping this mesh!" << endl;
332 #if G4VERSION_NUMBER >= 1060
333 G4VScoringMesh::MeshScoreMap fSMap = g4mesh->GetScoreMap();
334 G4VScoringMesh::MeshScoreMap::const_iterator msMapItr = fSMap.begin();
336 MeshScoreMap fSMap = g4mesh->GetScoreMap();
337 MeshScoreMap::const_iterator msMapItr = fSMap.begin();
339 for (; msMapItr != fSMap.end(); ++msMapItr)
341 G4String psname = msMapItr->first;
342 #if G4VERSION_NUMBER >= 1040
343 std::map<G4int, G4StatDouble *> &
score = *(msMapItr->second->GetMap());
345 std::map<G4int, G4double *> &score = *(msMapItr->second->GetMap());
347 G4double unitValue = g4mesh->GetPSUnitValue(psname);
348 G4String unit = g4mesh->GetPSUnit(psname);
351 const string htitle =
boost::str(
boost::format(
"Mesh %1%, Primitive scorer %2%: score [%3%]") % meshName.c_str() % psname.data() % unit.data());
355 cout <<
"PHG4ScoringManager::makeScoringHistograms - processing mesh " << meshName
356 <<
" scorer " << psname
358 <<
"# i" << divisionAxisNames[0]
359 <<
", i" << divisionAxisNames[1]
360 <<
", i" << divisionAxisNames[2]
362 <<
"[unit: " << unit <<
"]."
363 <<
" Saving to histogram " << hname <<
" : " << htitle
367 TH3 *
h =
new TH3D(hname.c_str(),
370 meshBoundMin[0], meshBoundMax[0],
372 meshBoundMin[1], meshBoundMax[1],
374 meshBoundMin[2], meshBoundMax[2]);
377 h->GetXaxis()->SetTitle(divisionAxisNames[0].
data());
378 h->GetYaxis()->SetTitle(divisionAxisNames[1].
data());
379 h->GetZaxis()->SetTitle(divisionAxisNames[2].
data());
382 for (
int x = 0;
x < nMeshSegments[0];
x++)
384 for (
int y = 0;
y < nMeshSegments[1];
y++)
386 for (
int z = 0;
z < nMeshSegments[2];
z++)
388 const int idx =
x * nMeshSegments[1] * nMeshSegments[2] +
y * nMeshSegments[2] +
z;
390 #if G4VERSION_NUMBER >= 1040
391 std::map<G4int, G4StatDouble *>::iterator
value = score.find(idx);
393 std::map<G4int, G4double *>::iterator value = score.find(idx);
395 if (value != score.end())
397 #if G4VERSION_NUMBER >= 1040
398 h->SetBinContent(
x + 1,
y + 1, z + 1, (value->second->mean()) / unitValue);
400 h->SetBinContent(
x + 1,
y + 1, z + 1, *(value->second) / unitValue);
416 static string histname(
"PHG4ScoringManager_HISTOS");
423 <<
"PHG4ScoringManager::get_HistoManager - Making Fun4AllHistoManager " << histname