art::FindManyP throws if art::Handle is invalid
I use the following code to get showers and hits associated with showers:
// ShowerList handle art::Handle< std::vector<recob::Shower> > showerListHandle; std::vector<art::Ptr<recob::Shower> > showerlist; if (evt.getByLabel(fShowerModuleLabel,showerListHandle)) art::fill_ptr_vector(showerlist, showerListHandle); art::FindManyP<recob::Hit> fmsht(showerListHandle,evt, fShowerModuleLabel);
FindManyP will show an exception of
showerListHandle is invalid. If I insert
if (!showerListHandle.isValid()) showerListHandle.clear();
FindManyP, I do not get the exception.
Would it be ok to make
FindManyP (and others Find*) not show when
art::Handle is invalid? One can check if the association is valid later.
#3 Updated by Christopher Green about 4 years ago
- Description updated (diff)
- Category set to Navigation
- SSI Package art added
- SSI Package deleted (
Unfortunately, the method you are using to avoid throwing an exception actually exposes a bug in
art::Handle that results in a null pointer dereference, which I have reported as issue #9769 and attached to this issue. If it doesn't cause an outright crash, it could cause hard-to-diagnose issues with memory corruption and bad results down the line.
It would be relatively straightforward to delay the exception throw until someone attempts to use a
FindMany without first checking its validity, but I think it is important to continue to preserve the difference between an empty collection and one that could not be found as specified.
#4 Updated by Tingjun Yang about 4 years ago
I understand it is important to preserve the difference between an empty collection and one that does not exist. But I find it inconvenient if FindMany throws at all because it means I have to put everything into an if block:
art::FindManyP<recob::Hit> fmsht(showerListHandle,evt, fShowerModuleLabel);
Loop over showers and get associations...
But I guess I can start coding in this way.