285#include "TRestGeant4AnalysisProcess.h"
357string CapitalizeFirst(
string name) {
359 name[0] = toupper(name[0]);
364bool EndsWith(
const string& text,
const string& suffix) {
365 return text.size() >= suffix.size() &&
366 text.compare(text.size() - suffix.size(), suffix.size(), suffix) == 0;
369bool StartsWith(
const string& text,
const string& prefix) {
return text.rfind(prefix, 0) == 0; }
377 static const map<string, string> processNameMap = {
379 {
"Photoelectric",
"phot"},
380 {
"Compton",
"compt"},
381 {
"Rayleigh",
"Rayl"},
382 {
"Conversion",
"conv"},
383 {
"PhotonNuclear",
"photonNuclear"},
385 {
"Bremsstrahlung",
"eBrem"},
386 {
"Bremstralung",
"eBrem"},
387 {
"Annihilation",
"annihil"},
390 {
"IonIoni",
"ionIoni"},
391 {
"MuIoni",
"muIoni"},
393 {
"CoulombScat",
"CoulombScat"},
395 {
"HadElastic",
"hadElastic"},
396 {
"NeutronInelastic",
"neutronInelastic"},
397 {
"NInelastic",
"neutronInelastic"},
398 {
"ProtonInelastic",
"protonInelastic"},
399 {
"NCapture",
"nCapture"},
401 {
"RadioactiveDecay",
"RadioactiveDecay"},
402 {
"RadiactiveDecay",
"RadioactiveDecay"},
405 return processNameMap;
421 const auto it = processNameMap.find(name);
422 if (it != processNameMap.end()) {
426 for (
const auto& [restName, geant4Name] : processNameMap) {
427 if (CapitalizeFirst(geant4Name) == name) {
434 if (CapitalizeFirst(geant4Name.Data()) == name) {
435 return geant4Name.Data();
448 cout <<
"??????????????????????????????????????????????????" << endl;
449 cout <<
"REST warning : TRestGeant4AnalysisProcess." << endl;
450 cout <<
"------------------------------------------" << endl;
452 cout <<
" Volume " << volumeName <<
" is not an active volume" << endl;
454 cout <<
"List of active volumes : " << endl;
455 cout <<
"------------------------ " << endl;
459 cout <<
"??????????????????????????????????????????????????" << endl;
493 std::vector<string> fObservables;
494 fObservables = TRestEventProcess::ReadObservables();
495 std::map<std::string, std::string> aliasObsToVol = GetAliasObservableNameToVolume();
498 const auto availableProcesses = physicsInfo.GetAllProcesses();
506 const vector<tuple<string, string, vector<string>>> perProcessDefinitions = {
507 {
"PerProcessPhotoelectric",
"", {
"phot"}},
508 {
"PerProcessCompton",
"", {
"compt"}},
509 {
"PerProcessElectronicIoni",
"", {
"eIoni"}},
510 {
"PerProcessAlphaIoni",
"alpha", {
"ionIoni",
"alphaIoni"}},
511 {
"PerProcessIonIoni",
"", {
"ionIoni"}},
512 {
"PerProcessHadronicIoni",
"", {
"hIoni"}},
513 {
"PerProcessProtonIoni",
"proton", {
"hIoni"}},
514 {
"PerProcessMsc",
"", {
"msc"}},
515 {
"PerProcessHadronElastic",
"", {
"hadElastic"}},
516 {
"PerProcessNeutronElastic",
"neutron", {
"hadElastic"}},
518 for (
const auto& [obsName, particle, processes] : perProcessDefinitions) {
523 fAnalysisTree->AddObservable((
string)GetName() +
"_" + obsName,
"double",
524 "Energy deposited in the sensitive volumes by a given process");
529 for (
unsigned int i = 0; i < fObservables.size(); i++) {
530 if (fObservables[i].find(
"VolumeEDep") != string::npos) {
531 TString volName = fObservables[i].substr(0, fObservables[i].length() - 10).c_str();
532 if (aliasObsToVol.find(fObservables[i]) != aliasObsToVol.end()) {
533 volName = aliasObsToVol[fObservables[i]].c_str();
540 fVolumeName.push_back(volName.Data());
548 if (fObservables[i].find(
"MeanPos") != string::npos) {
549 TString volName2 = fObservables[i].substr(0, fObservables[i].length() - 8).c_str();
550 if (aliasObsToVol.find(fObservables[i]) != aliasObsToVol.end()) {
551 volName2 = aliasObsToVol[fObservables[i]].c_str();
553 std::string dirId = fObservables[i].substr(fObservables[i].length() - 1, 1);
566 if ((dirId !=
"X") && (dirId !=
"Y") && (dirId !=
"Z")) {
568 cout <<
"??????????????????????????????????????????????????" << endl;
569 cout <<
"REST warning : TRestGeant4AnalysisProcess." << endl;
570 cout <<
"------------------------------------------" << endl;
572 cout <<
" Direction " << dirId <<
" is not valid" << endl;
573 cout <<
" Only X, Y or Z accepted" << endl;
578 if (StartsWith(fObservables[i],
"containsProcess")) {
579 const string processSuffix = fObservables[i].substr(
string(
"containsProcess").length());
585 if (geant4Name.empty()) {
586 RESTWarning <<
"TRestGeant4AnalysisProcess: unknown process '" << processSuffix
587 <<
"' in observable '" << fObservables[i] <<
"'. It will not be filled."
591 if (availableProcesses.count(geant4Name) == 0) {
592 RESTWarning <<
"TRestGeant4AnalysisProcess: Geant4 process '" << geant4Name
593 <<
"' (observable '" << fObservables[i]
594 <<
"') is not registered in the physics info of this simulation. "
595 <<
"Its value will always be 0 (processes sharing type and subtype, e.g. "
596 <<
"eIoni and ionIoni, are registered only once)" <<
RESTendl;
604 if (EndsWith(fObservables[i],
"Process") && !StartsWith(fObservables[i],
"firstTrackInSensitive")) {
605 const string nameWithoutSuffix =
606 fObservables[i].substr(0, fObservables[i].length() -
string(
"Process").length());
607 const bool hasAlias = aliasObsToVol.find(fObservables[i]) != aliasObsToVol.end();
612 for (
size_t length = 1; length <= nameWithoutSuffix.length(); length++) {
614 if (length == nameWithoutSuffix.length() && !hasAlias) {
617 const string candidate = nameWithoutSuffix.substr(nameWithoutSuffix.length() - length);
619 if (!candidateGeant4Name.empty()) {
620 processName = candidate;
621 geant4Name = candidateGeant4Name;
625 if (geant4Name.empty()) {
626 RESTWarning <<
"TRestGeant4AnalysisProcess: no known process name found in observable '"
627 << fObservables[i] <<
"'. It will not be filled." <<
RESTendl;
628 RESTWarning <<
"Valid process names are:";
630 RESTWarning <<
" " << restName;
632 RESTWarning <<
" (or a Geant4 process name with the first letter capitalized)" <<
RESTendl;
636 TString volName3 = nameWithoutSuffix.substr(0, nameWithoutSuffix.length() - processName.length());
638 volName3 = aliasObsToVol[fObservables[i]].c_str();
647 if (availableProcesses.count(geant4Name) == 0) {
648 RESTWarning <<
"TRestGeant4AnalysisProcess: Geant4 process '" << geant4Name
649 <<
"' (observable '" << fObservables[i]
650 <<
"') is not registered in the physics info of this simulation. "
651 <<
"It will be 0 unless energy is deposited under this process name "
652 <<
"(processes sharing type and subtype, e.g. eIoni and ionIoni, are "
653 <<
"registered only once)" <<
RESTendl;
660 if (fObservables[i].find(
"TracksCounter") != string::npos) {
661 TString particleName = fObservables[i].substr(0, fObservables[i].length() - 13).c_str();
666 if (fObservables[i].find(
"TracksEDep") != string::npos) {
667 TString particleName = fObservables[i].substr(0, fObservables[i].length() - 10).c_str();
685 const auto& sensitiveVolumes =
fG4Metadata->GetSensitiveVolumes();
686 auto isSensitiveVolume = [&sensitiveVolumes](
const TString& volumeName) {
687 return std::find(sensitiveVolumes.begin(), sensitiveVolumes.end(), volumeName) !=
688 sensitiveVolumes.end();
691 Double_t sensitiveVolumeEnergy = 0;
692 const auto energyInVolume =
fOutputG4Event->GetEnergyInVolumeMap();
693 for (
const auto& sensitiveVolume : sensitiveVolumes) {
694 const auto volume = energyInVolume.find(sensitiveVolume.Data());
695 if (volume != energyInVolume.end()) {
696 sensitiveVolumeEnergy += volume->second;
701 double hitTime = std::numeric_limits<double>::infinity();
702 std::set<int> trackIdsInSensitiveVolume;
705 const auto& hits = track.GetHits();
706 for (
size_t hitIndex = 0; hitIndex < hits.GetNumberOfHits(); hitIndex++) {
707 const auto volumeName = hits.GetVolumeName(hitIndex);
708 if (!isSensitiveVolume(volumeName)) {
711 const double energy = hits.GetEnergy(hitIndex);
716 const double time = hits.GetTime(hitIndex);
717 if (time < hitTime) {
721 trackIdsInSensitiveVolume.insert(track.GetTrackID());
727 std::set<int> trackParentsOfInterest;
728 for (
const auto& trackIdInSensitiveVolume : trackIdsInSensitiveVolume) {
729 const auto track =
fOutputG4Event->GetTrackByID(trackIdInSensitiveVolume);
732 while (parent !=
nullptr && !found) {
734 const auto& hits = parent->GetHits();
735 if (hits.GetNumberOfHits() > 0 && !isSensitiveVolume(hits.GetVolumeName(0))) {
736 for (
size_t hitIndex = 0; hitIndex < hits.GetNumberOfHits(); hitIndex++) {
737 const auto volumeName = hits.GetVolumeName(hitIndex);
738 if (!isSensitiveVolume(volumeName)) {
740 trackParentsOfInterest.insert(parent->GetTrackID());
746 parent = parent->GetParentTrack();
750 const double missingValue = std::numeric_limits<double>::quiet_NaN();
751 TVector3 position(missingValue, missingValue, missingValue);
752 double energy = missingValue;
754 string parentParticleName;
755 string creatorProcess;
758 if (!trackParentsOfInterest.empty()) {
759 const auto track =
fOutputG4Event->GetTrackByID(*trackParentsOfInterest.begin());
761 position = track->GetInitialPosition();
762 energy = track->GetInitialKineticEnergy();
763 particleName = track->GetParticleName().Data();
765 if (track->GetParentTrack() !=
nullptr) {
766 parentParticleName = track->GetParentTrack()->GetParticleName().Data();
768 creatorProcess = track->GetCreatorProcess().Data();
769 volumeName = track->GetHits().GetVolumeName(0).Data();
783 cout <<
"----------------------------" << endl;
785 cout <<
"Sensitive volume Energy : " << sensitiveVolumeEnergy << endl;
786 cout <<
"Total energy : " <<
fOutputG4Event->GetTotalDepositedEnergy() << endl;
800 Double_t xDirection =
fOutputG4Event->GetPrimaryEventDirection(0).X();
803 Double_t yDirection =
fOutputG4Event->GetPrimaryEventDirection(0).Y();
806 Double_t zDirection =
fOutputG4Event->GetPrimaryEventDirection(0).Z();
810 string subEventPrimaryParticleName;
812 subEventPrimaryParticleName =
fOutputG4Event->GetSubEventPrimaryEventParticleName();
814 subEventPrimaryParticleName =
"generator";
818 TVector3 primaryDirection(xDirection, yDirection, zDirection);
819 primaryDirection = primaryDirection.Unit();
822 SetObservableValue(
"zenithYDegrees", TMath::ACos(-1 * yDirection) * TMath::RadToDeg());
824 sourceDirection = sourceDirection.Unit();
826 TMath::ACos(primaryDirection.Dot(sourceDirection)) * TMath::RadToDeg());
828 Double_t energyPrimary =
fOutputG4Event->GetPrimaryEventEnergy(0);
841 const Int_t processID = physicsInfo.GetProcessID(processName);
843 const bool processIsRegistered = physicsInfo.GetProcessName(processID) == processName;
844 Int_t containsProcess = 0;
845 if (processIsRegistered &&
fOutputG4Event->ContainsProcess(processID)) {
852 const auto energyInVolumePerProcess =
fOutputG4Event->GetEnergyInVolumePerProcessMap();
856 if (volume != energyInVolumePerProcess.end()) {
857 const auto process = volume->second.find(
fProcessName[n]);
858 if (process != volume->second.end()) {
859 energy = process->second;
867 const auto energyInVolumePerParticlePerProcess =
871 for (
const auto& sensitiveVolumeName : sensitiveVolumes) {
872 const auto sensitiveVolume =
873 energyInVolumePerParticlePerProcess.find(sensitiveVolumeName.Data());
874 if (sensitiveVolume == energyInVolumePerParticlePerProcess.end()) {
877 for (
const auto& [particle, energyPerProcess] : sensitiveVolume->second) {
882 const auto process = energyPerProcess.find(processName);
883 if (process != energyPerProcess.end()) {
884 energy += process->second;
890 energy = sensitiveVolumeEnergy > 0 ? energy / sensitiveVolumeEnergy : 0;
918 if (
fDirID[n] == (TString)
"X")
921 else if (
fDirID[n] == (TString)
"Y")
924 else if (
fDirID[n] == (TString)
"Z")
931 cout <<
"G4 Tracks : " <<
fOutputG4Event->GetNumberOfTracks() << endl;
932 cout <<
"----------------------------" << endl;
938std::map<std::string, std::string> TRestGeant4AnalysisProcess::GetAliasObservableNameToVolume() {
940 std::map<std::string, std::string> obsNamesToVolumes;
942 while (e !=
nullptr) {
943 const char* obschr = e->Attribute(
"name");
944 const char* _value = e->Attribute(
"value");
945 const char* _vol = e->Attribute(
"volume");
948 if (_value ==
nullptr)
954 if (obschr !=
nullptr) {
958 obsNamesToVolumes[(string)obschr] = volume;
963 e = e->NextSiblingElement(
"observable");
966 return obsNamesToVolumes;
TRestAnalysisTree * fAnalysisTree
TRestRun * GetRunInfo() const
Return the pointer of the hosting TRestRun object.
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
A pure analysis process to extract information from a TRestGeant4Event.
std::vector< std::string > fPerProcessParticle
The particle restriction of each PerProcessXxx observable (empty means any particle).
void Initialize() override
Function to initialize input/output event members and define the section name.
std::vector< std::string > fEnergyInObservables
It stores the name of observables xxxVolumeEDep related to the energy deposition in volume xxx.
std::vector< std::string > fDirID
std::string GetGeant4ProcessName(const std::string &name) const
It returns the Geant4 process name corresponding to name.
void InitProcess() override
Process initialization. Observable names are interpreted and auxiliar observable members,...
TRestGeant4Event * fInputG4Event
A pointer to the specific TRestGeant4Event input.
void LoadConfig(const std::string &configFilename, const std::string &name="")
Function to load the configuration from an external configuration file.
TRestGeant4Metadata * fG4Metadata
A pointer to the simulation metadata information accessible to TRestRun.
~TRestGeant4AnalysisProcess()
Default destructor.
std::vector< std::string > fTracksEDepObservables
A std::vector storing the observable name xxxTracksEDep for a given xxx particle.
std::vector< std::string > fParticleTrackCounter
A std::vector storing the xxx particle name extracted from xxxTracksCounter.
std::vector< std::string > fProcessObservables
std::vector< Int_t > fVolumeID2
A std::vector storing the active volume ids corresponding mean position observable xxxMeanPosX,...
Bool_t fPerProcessSensitiveEnergyNorm
If true, PerProcessXxx observables are normalized to the total energy in the sensitive volumes.
std::vector< std::string > fProcessVolumeName
A std::vector storing the volume name corresponding to each process observable.
void LoadDefaultConfig()
Function to load the default config in absence of RML input.
std::vector< std::string > fPerProcessObservables
A std::vector storing the observable names PerProcessXxx (only if fPerProcessSensitiveEnergy).
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
The main processing event function.
std::vector< std::string > fContainsProcessObservables
A std::vector storing the observable names containsProcessXxx.
std::vector< Int_t > fVolumeID
A std::vector storing the active volume ids of observables xxxVolumeEDep.
Bool_t fPerProcessSensitiveEnergy
TRestGeant4Event * fOutputG4Event
A pointer to the specific TRestGeant4Event output.
std::vector< std::string > fTrackCounterObservables
A std::vector storing the observable name xxxTracksCounter for a given xxx particle.
std::vector< std::vector< std::string > > fPerProcessG4Names
The Geant4 process names contributing to each PerProcessXxx observable.
std::vector< Int_t > fVolumeID3
A std::vector storing the active volume ids corresponding process observable .
static const std::map< std::string, std::string > & GetRestToGeant4ProcessNameMap()
It returns the map between the process names used in REST observables (<volume><Process>Process) and ...
TRestGeant4AnalysisProcess()
Default constructor.
std::vector< std::string > fMeanPosObservables
A std::vector storing the name of active volumes.
std::vector< std::string > fProcessName
A std::vector storing the Geant4 name of the process corresponding to each process observable.
void EndProcess() override
Function to include required actions after all events have been processed.
std::vector< std::string > fContainsProcessG4Name
A std::vector storing the Geant4 process name corresponding to each containsProcessXxx observable.
std::vector< std::string > fParticleTrackEdep
A std::vector storing the xxx particle name extracted from xxxTracksEDep.
void PrintNotActiveVolumeWarning(const TString &volumeName)
It prints a warning when the volume used by an observable is not an active volume.
An event class to store geant4 generated event information.
void InitializeReferences(TRestRun *run) override
Initialize dynamical references when loading the event from a root file.
Double_t GetBoundingBoxSize()
This method returns the event size as the size of the bounding box enclosing all hits.
@ REST_Debug
+show the defined debug messages
std::string ToUpper(std::string in)
Convert string to its upper case. Alternative of TString::ToUpper.