Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iss1376 geant4 object pointer for comparison #1633

Open
wants to merge 23 commits into
base: trunk
Choose a base branch
from

Conversation

LiamBrennan-UCSB
Copy link
Contributor

@LiamBrennan-UCSB LiamBrennan-UCSB commented Feb 23, 2025

Fixing Issue #1286 and Issue #1376. Adds functionality to call a Geant4 pointer to compare to another pointer within Simcore, Biasing, etc such that a pointer to pointer comparison can be made rather than completing string to string as was done previously.

What are the issues that this addresses?

Check List

  • I successfully compiled ldmx-sw with my developments
  • I ran my developments and the following shows that they are successful.

Liam Brennan and others added 2 commits February 22, 2025 21:23
return volume;
}
}
return nullptr; // Volume not found
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we warn here?


if (processName.compareTo("eBrem") == 0 &&
auto electron = G4Electron::Definition();
auto eBrem_process = Geant4_PtrRetrieval::GetProcess(electron, "eBrem");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we can just do pointer comparison, no need to do string comparison right?

Copy link
Member

@tvami tvami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have several style comments, but I can point those out privately.
Bigger picture tho: why did you need two classes and not just one? Geant4_PtrRetrieval is still not a good name, there are a billion pointers in GEANT4. I honestly dont have a bullet proof suggestion, but something like a GeometryHelper, or VolumeCheckHelper would be better (and it should have the conent of the current pointer retrieval and volume test file). @tomeichlersmith @EinarElen do you have a preference, new suggestion?

@LiamBrennan-UCSB
Copy link
Contributor Author

One thing of note though: I wasn't able to fix this line

      const G4BiasingProcessInterface* wrapperProcess =
          (sharedData->GetPhysicsBiasingProcessInterfaces())[iprocess];

      if (wrapperProcess->GetWrappedProcess()->GetProcessName().compareTo(
              process) == 0) {
        return true;

Because its a wrapped process the pointer it creates is not the same as the unwrapped pointer. So when I would try to do comparisons and printouts.

biasWrapper(GammaToMuPair) Looking for process with name: biasWrapper(photonNuclear) 
Checking wrapped process: 0x5576eefb0778 (GammaToMuPair) against process: 0x5576f3fcfd20 (biasWrapper(photonNuclear)) 
Checking wrapped process: 0x5576ecfb9b90 (photonNuclear) against process: 0x5576f3fcfd20 (biasWrapper(photonNuclear)) 
Checking wrapped process: 0x5576ee922950 (conv) against process: 0x5576f3fcfd20 (biasWrapper(photonNuclear)) 
Checking wrapped process: 0x5576ee920240 (compt) against process: 0x5576f3fcfd20 (biasWrapper(photonNuclear)) 
Checking wrapped process: 0x5576ee91dc20 (phot) against process: 0x5576f3fcfd20 (biasWrapper(photonNuclear)) 
[ fire ] 0 fatal: [BiasSetup] : photonNuclear is not found in list of biased processes!

Clearly photonNuclear is in there. But the pointer is different because of the wrapping. I couldn't figure out how to get rid of the wrapping without going back to just doing string to string, but more complicated. If anyone has any suggestions on how this could be fixed I would appreciate it @tomeichlersmith @EinarElen

@tomeichlersmith
Copy link
Member

Could you use the pointer returned by GetWrappedProcess() for the comparison?

In any case, I think that's okay. The main goal of switching to comparing pointers rather than strings is the performance improvement. This is only called at the beginning of a run so it doesn't have as large of a performance effect.

@tomeichlersmith
Copy link
Member

why did you need two classes and not just one?

@tvami What are you referring to here? The functions are just in the Geant4_PtrRetrieval namespace, there are no classes.

As far as the naming, I would maybe rename it to be more clearly just a namespace where we have used all lower case, no under scores.
Maybe simcore::g4ptr so that it is within the simcore namespace?
I also don't have a great idea for a name. While I tend to agree that Geant4_PtrRetrieval and g4ptr are too generic, the functions in those namespaces aren't really doing much else.

@tvami
Copy link
Member

tvami commented Feb 25, 2025

I meant to not have two header files (Geant4_PtrRetrieval.h and VolumeTests.h), just one.

Haha it's too generic and too specific at the same time. Can we not expect to retrieve something else than a pointer? Or then it will be another namespace? VolumeTests.h already just returns booleans, so if we merge the two then g4ptr is too specific. What about simcore::g4helper with the idea of extending this with more stuff in the future?

@tomeichlersmith
Copy link
Member

tomeichlersmith commented Feb 25, 2025

We have the G4User submodule of SimCore: https://github.com/LDMX-Software/ldmx-sw/tree/trunk/SimCore/include/SimCore/G4User

Right now, it holds implementations of Geant4's user actions, but I think including a PtrRetrieval.h and VolumeTests.h headers there would make sense since we are "using" Geant4. I still think those could be their own namespace just to keep the functions organized (simcore::g4user::ptr and simcore::g4user::volumetests respectively).

(If this sounds good but you are having trouble implementing it @LiamBrennan-UCSB , let me know and I can push what I'm thinking to this branch.)

@tvami
Copy link
Member

tvami commented Feb 25, 2025

OK but then volumetests should be more like volumechecks? It's not actually testing the volumes, right?

@tomeichlersmith
Copy link
Member

Yea that makes sense 👍

@@ -56,7 +56,6 @@ class TargetENProcessFilter : public simcore::UserAction {
* target parent volume and so it will break if the target parent volume
* changes its name.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You removed the variable but not the corresponding comment

Comment on lines 61 to 63
// auto volume{track->GetVolume()->GetLogicalVolume()
// ? track->GetVolume()->GetLogicalVolume()->GetName()
// : "undefined"};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete these, although I'm not sure what the advantage of your new lines are wrt to what is now to be deleted

return process;
}
}
std::cerr << "[WARNING] Process '" << processName << "' not found.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dont use cerr use the ldmx logging

Suggested change
std::cerr << "[WARNING] Process '" << processName << "' not found.\n";
ldmx_log(warn) << "Process '" << processName << "' not found.";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talking with Liam I understand this might not be simple, as this is a namespace. Should we not throw the warning in the user class end? i.e. if the getProcess returned a nullptr then have the warning in the respective filter? @EinarElen @tomeichlersmith

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I vote to have the warning in the user class end although its not technically difficult to support logging within a namespace. The only requirement for ldmx_log to function is for a framework::logging::logger named theLog_ to be present within scope. A namespace-level variable could then support logging for all of the functions under one "channel", but I think it makes more sense to have the warning be in the user classes.

inline G4Region* getRegion(const std::string& name) {
G4Region* region = G4RegionStore::GetInstance()->GetRegion(name);
if (!region) {
std::cerr << "[WARNING] Region '" << name << "' not found.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

inline G4VPhysicalVolume* getPhysicalVolume(const std::string& name) {
auto* volume = G4PhysicalVolumeStore::GetInstance()->GetVolume(name);
if (!volume) {
std::cerr << "[WARNING] Volume '" << name << "' not found.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too

inline G4LogicalVolume* getLogicalVolume(const std::string& name) {
auto* volume = G4LogicalVolumeStore::GetInstance()->GetVolume(name);
if (!volume) {
std::cerr << "[WARNING] Volume '" << name << "' not found.\n";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

* @param[in] vol G4LogicalVolume to check
* @param[in] vol_to_bias UNUSED name of volume to bias
*/
[[maybe_unused]] inline bool isInHcal(G4LogicalVolume* vol,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about the [[maybe_unused]] here and in the next occasions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Me too, it is certainly used within DetectorConstruction.cxx to I think these attributes should be removed to make sure that the compilation is appropriately using this single definition in multiple places.

if (processName.compareTo("eBrem") == 0 &&
auto electron = G4Electron::Definition();
auto ebrem_process =
simcore::g4user::ptrretrieval::getProcess(electron, "eBrem");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it really the electron that's supposed to be here @tomeichlersmith @EinarElen ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so... thats the particle that would do the eBrem. Are you asking if this should be a photon whose creator process is eBrem?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that was my thinking, but I guess it works as it is

@tvami tvami requested a review from EinarElen February 27, 2025 04:45
@@ -6,7 +6,7 @@ namespace simcore {

XsecBiasingOperator::XsecBiasingOperator(
std::string name, const framework::config::Parameters& parameters)
: G4VBiasingOperator(name) {}
: G4VBiasingOperator(name), theLog_{framework::logging::makeLogger(name)} {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you merged but should have rebased, bc these changes here are already merged in https://github.com/LDMX-Software/ldmx-sw/pull/1630/files

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git switch trunk
git fetch
git pull
git switch -
git rebase trunk

@LiamBrennan-UCSB LiamBrennan-UCSB marked this pull request as ready for review March 3, 2025 18:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants