Skip to content

Commit

Permalink
Merge pull request #614 from tontyoutoure/filter_inversion
Browse files Browse the repository at this point in the history
Add a "invert" command for filters of actors.
  • Loading branch information
tbaudier authored Feb 8, 2024
2 parents 3621e0b + e57a4ff commit b0e7a43
Show file tree
Hide file tree
Showing 24 changed files with 112 additions and 30 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ jobs:
t27_Hits_2SD,
t28_Singles_2SD,
t29_optical_digi,
t30_dna]
t30_dna,
t32_isotopes,
t33_invert_filter]

steps:
- name: Checkout github repo
Expand Down
4 changes: 4 additions & 0 deletions docs/tools_to_interact_with_the_simulation_actors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,10 @@ Filters

Filters are used to add selectrion criteria on actors. They are also used with reduction variance techniques. They are filters on particle type, particle ID, energy, direction....

All filters listed below can be inverted and generate exact opposite selection::

/gate/actor/[Actor Name]/[Filter Name]/invert

Filter on particle type
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
2 changes: 2 additions & 0 deletions source/general/include/GateAngleFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ See LICENSE.md for further details

#include "G4UIcmdWithADoubleAndUnit.hh"
#include "G4UIcmdWith3Vector.hh"
#include "G4UIcmdWithoutParameter.hh"


class GateAngleFilter;
Expand All @@ -41,6 +42,7 @@ protected:

G4UIcmdWithADoubleAndUnit * pSetAngleCmd;
G4UIcmdWith3Vector * pSetDirectionCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEANGLEFILTERMESSENGER_HH */
2 changes: 2 additions & 0 deletions source/general/include/GateCreatorProcessFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "G4UImessenger.hh"

#include "G4UIcmdWithAString.hh"
#include "G4UIcmdWithoutParameter.hh"

class GateCreatorProcessFilter;

Expand All @@ -36,6 +37,7 @@ class GateCreatorProcessFilterMessenger :
GateCreatorProcessFilter *pFilter;

G4UIcmdWithAString* pAddCreatorProcessCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif
2 changes: 2 additions & 0 deletions source/general/include/GateEnergyFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See LICENSE.md for further details
#include "G4UImessenger.hh"

#include "G4UIcmdWithADoubleAndUnit.hh"
#include "G4UIcmdWithoutParameter.hh"


class GateEnergyFilter;
Expand All @@ -39,6 +40,7 @@ protected:

G4UIcmdWithADoubleAndUnit * pSetEminCmd;
G4UIcmdWithADoubleAndUnit * pSetEmaxCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEENEFILTERMESSENGER_HH */
2 changes: 2 additions & 0 deletions source/general/include/GateIDFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See LICENSE.md for further details
#include "G4UImessenger.hh"

#include "G4UIcmdWithAnInteger.hh"
#include "G4UIcmdWithoutParameter.hh"


class GateIDFilter;
Expand All @@ -39,6 +40,7 @@ protected:

G4UIcmdWithAnInteger* pAddIDCmd;
G4UIcmdWithAnInteger* pAddParentIDCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEIDFILTERMESSENGER_HH */
2 changes: 2 additions & 0 deletions source/general/include/GateMaterialFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ See LICENSE.md for further details
#include "globals.hh"
#include "G4UImessenger.hh"
#include "G4UIcmdWithAString.hh"
#include "G4UIcmdWithoutParameter.hh"


class GateMaterialFilter;
Expand All @@ -33,6 +34,7 @@ protected:
GateMaterialFilter * pMaterialFilter;

G4UIcmdWithAString* pAddMaterialCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEMATFILTERMESSENGER_HH */
2 changes: 2 additions & 0 deletions source/general/include/GateParticleFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ See LICENSE.md for further details
#include "G4UIcmdWithAString.hh"

#include "G4UIcmdWithAnInteger.hh"
#include "G4UIcmdWithoutParameter.hh"

class GateParticleFilter;

Expand All @@ -43,6 +44,7 @@ protected:
G4UIcmdWithAnInteger* pAddParticlePDGCmd;
G4UIcmdWithAString* pAddParentParticleCmd;
G4UIcmdWithAString* pAddDirectParentParticleCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEPARTFILTERMESSENGER_HH */
6 changes: 6 additions & 0 deletions source/general/include/GateVFilter.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ See LICENSE.md for further details
#include "G4Step.hh"
#include "G4Track.hh"

#define _FILTER_RETURN_WITH_INVERSION return IsInverted !=

class GateVFilter :
public GateNamedObject
{
Expand All @@ -40,10 +42,14 @@ public:

virtual void show();

void setInvert(){IsInverted = true;}


private:

protected:
bool IsInverted = false;

};


Expand Down
2 changes: 2 additions & 0 deletions source/general/include/GateVolumeFilterMessenger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ See LICENSE.md for further details
#include "G4UImessenger.hh"

#include "G4UIcmdWithAString.hh"
#include "G4UIcmdWithoutParameter.hh"


class GateVolumeFilter;
Expand All @@ -38,6 +39,7 @@ protected:
GateVolumeFilter * pVolumeFilter;

G4UIcmdWithAString* pAddVolumeCmd;
G4UIcmdWithoutParameter* pInvertCmd;
};

#endif /* end #define GATEVOLUMEFILTERMESSENGER_HH */
4 changes: 2 additions & 2 deletions source/general/src/GateAngleFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ G4bool GateAngleFilter::Accept(const G4Track* aTrack)
G4ThreeVector stepdirection = aTrack->GetMomentumDirection();
if(stepdirection.x()*mDirection.x()
+ stepdirection.y()*mDirection.y()
+ stepdirection.z()*mDirection.z() < std::cos(mAngle) ) return false;
+ stepdirection.z()*mDirection.z() < std::cos(mAngle) ) _FILTER_RETURN_WITH_INVERSION false;

return true;
_FILTER_RETURN_WITH_INVERSION true;
}
//---------------------------------------------------------------------------

Expand Down
9 changes: 9 additions & 0 deletions source/general/src/GateAngleFilterMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ GateAngleFilterMessenger::~GateAngleFilterMessenger()
{
delete pSetDirectionCmd;
delete pSetAngleCmd;
delete pInvertCmd;
}
//-----------------------------------------------------------------------------

Expand All @@ -54,6 +55,11 @@ void GateAngleFilterMessenger::BuildCommands(G4String base)
pSetDirectionCmd->SetGuidance(guidance);
pSetDirectionCmd->SetParameterName("direction_x","direction_y", "direction_z", false, false);

bb = base+"/invert";
pInvertCmd = new G4UIcmdWithoutParameter(bb,this);
guidance = "Invert the filter";
pInvertCmd->SetGuidance(guidance);

}
//-----------------------------------------------------------------------------

Expand All @@ -70,6 +76,9 @@ void GateAngleFilterMessenger::SetNewValue(G4UIcommand* command, G4String param)
{
pAngleFilter->SetMomentum( pSetDirectionCmd->GetNew3VectorValue(param) );
}

if(command==pInvertCmd)
pAngleFilter->setInvert();
}
//-----------------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions source/general/src/GateCreatorProcessFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ GateCreatorProcessFilter::~GateCreatorProcessFilter()
G4bool GateCreatorProcessFilter::Accept(const G4Track* aTrack)
{
const G4VProcess *creatorProcess = aTrack->GetCreatorProcess();
if (!creatorProcess) return false;
if (!creatorProcess) _FILTER_RETURN_WITH_INVERSION false;

G4String creatorProcessName = creatorProcess->GetProcessName();
for (CreatorProcesses::const_iterator iter=creatorProcesses.begin(); iter!=creatorProcesses.end(); iter++)
if (*iter==creatorProcessName)
return true;
_FILTER_RETURN_WITH_INVERSION true;

return false;
_FILTER_RETURN_WITH_INVERSION false;
}

//---------------------------------------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions source/general/src/GateCreatorProcessFilterMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ GateCreatorProcessFilterMessenger::GateCreatorProcessFilterMessenger(GateCreator
G4String guidance = "Add creator process";
pAddCreatorProcessCmd->SetGuidance(guidance);
pAddCreatorProcessCmd->SetParameterName("Creator process name",false);

bb = base+"/invert";
pInvertCmd = new G4UIcmdWithoutParameter(bb,this);
guidance = "Invert the filter";
pInvertCmd->SetGuidance(guidance);
}
}
//-----------------------------------------------------------------------------
Expand All @@ -30,6 +35,7 @@ GateCreatorProcessFilterMessenger::GateCreatorProcessFilterMessenger(GateCreator
GateCreatorProcessFilterMessenger::~GateCreatorProcessFilterMessenger()
{
delete pAddCreatorProcessCmd;
delete pInvertCmd;
}
//-----------------------------------------------------------------------------

Expand All @@ -38,6 +44,7 @@ GateCreatorProcessFilterMessenger::~GateCreatorProcessFilterMessenger()
void GateCreatorProcessFilterMessenger::SetNewValue(G4UIcommand* command, G4String param)
{
if(command==pAddCreatorProcessCmd) pFilter->AddCreatorProcess(param);
if(command==pInvertCmd) pFilter->setInvert();
}
//-----------------------------------------------------------------------------

12 changes: 6 additions & 6 deletions source/general/src/GateEnergyFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ G4bool GateEnergyFilter::Accept(const G4Step* aStep)
G4double kinetic = aStep->GetPreStepPoint()->GetKineticEnergy();


if ( fLowEnergy!=0. && kinetic < fLowEnergy ) return false;
if ( fHighEnergy!= 0. && kinetic >= fHighEnergy ) return false;
if ( fLowEnergy!=0. && kinetic < fLowEnergy ) _FILTER_RETURN_WITH_INVERSION false;
if ( fHighEnergy!= 0. && kinetic >= fHighEnergy ) _FILTER_RETURN_WITH_INVERSION false;

return true;
_FILTER_RETURN_WITH_INVERSION true;
}
//---------------------------------------------------------------------------

Expand All @@ -43,10 +43,10 @@ G4bool GateEnergyFilter::Accept(const G4Track* aTrack)
G4double kinetic = aTrack->GetKineticEnergy();


if ( fLowEnergy!=0. && kinetic < fLowEnergy ) return false;
if ( fHighEnergy!= 0. && kinetic >= fHighEnergy ) return false;
if ( fLowEnergy!=0. && kinetic < fLowEnergy ) _FILTER_RETURN_WITH_INVERSION false;
if ( fHighEnergy!= 0. && kinetic >= fHighEnergy ) _FILTER_RETURN_WITH_INVERSION false;

return true;
_FILTER_RETURN_WITH_INVERSION true;
}
//---------------------------------------------------------------------------

Expand Down
8 changes: 8 additions & 0 deletions source/general/src/GateEnergyFilterMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ GateEnergyFilterMessenger::~GateEnergyFilterMessenger()
{
delete pSetEminCmd;
delete pSetEmaxCmd;
delete pInvertCmd;
}
//-----------------------------------------------------------------------------

Expand All @@ -55,6 +56,11 @@ void GateEnergyFilterMessenger::BuildCommands(G4String base)
pSetEmaxCmd->SetParameterName("Emax", false);
pSetEmaxCmd->SetDefaultUnit("MeV");

bb = base+"/invert";
pInvertCmd = new G4UIcmdWithoutParameter(bb,this);
guidance = "Invert the filter";
pInvertCmd->SetGuidance(guidance);

}
//-----------------------------------------------------------------------------

Expand All @@ -71,6 +77,8 @@ void GateEnergyFilterMessenger::SetNewValue(G4UIcommand* command, G4String param
{
pEnergyFilter->SetEmax(pSetEmaxCmd->GetNewDoubleValue(param) );
}
if(command==pInvertCmd)
pEnergyFilter->setInvert();
}
//-----------------------------------------------------------------------------

Expand Down
6 changes: 3 additions & 3 deletions source/general/src/GateIDFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ GateIDFilter::GateIDFilter(G4String name)
G4bool GateIDFilter::Accept(const G4Track* aTrack)
{

if(mID!=0) if(aTrack->GetTrackID()!=mID) return false;
if(mID!=0) if(aTrack->GetTrackID()!=mID) _FILTER_RETURN_WITH_INVERSION false;


if(mParentID!=0)
if(aTrack->GetParentID()!=mParentID) return false;
if(aTrack->GetParentID()!=mParentID) _FILTER_RETURN_WITH_INVERSION false;

return true;
_FILTER_RETURN_WITH_INVERSION true;
}
//---------------------------------------------------------------------------

Expand Down
9 changes: 9 additions & 0 deletions source/general/src/GateIDFilterMessenger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ GateIDFilterMessenger::GateIDFilterMessenger(GateIDFilter* idFilter)
GateIDFilterMessenger::~GateIDFilterMessenger()
{
delete pAddIDCmd;
delete pAddParentIDCmd;
delete pInvertCmd;
}
//-----------------------------------------------------------------------------

Expand All @@ -52,6 +54,11 @@ void GateIDFilterMessenger::BuildCommands(G4String base)
pAddParentIDCmd->SetGuidance(guidance);
pAddParentIDCmd->SetParameterName("Parent ID",false);

bb = base+"/invert";
pInvertCmd = new G4UIcmdWithoutParameter(bb,this);
guidance = "Invert the filter";
pInvertCmd->SetGuidance(guidance);


}
//-----------------------------------------------------------------------------
Expand All @@ -64,6 +71,8 @@ void GateIDFilterMessenger::SetNewValue(G4UIcommand* command, G4String param)
pIDFilter->addID(pAddIDCmd->GetNewIntValue(param));
if(command==pAddParentIDCmd)
pIDFilter->addParentID(pAddParentIDCmd->GetNewIntValue(param));
if(command==pInvertCmd)
pIDFilter->setInvert();
}
//-----------------------------------------------------------------------------

Expand Down
8 changes: 4 additions & 4 deletions source/general/src/GateMaterialFilter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ G4bool GateMaterialFilter::Accept(const G4Step* aStep)
for ( size_t i = 0; i < theMdef.size(); i++){
if ( theMdef[i] == aStep->GetPreStepPoint()->GetMaterial()->GetName() ) {
nFilteredParticles++;
return true;
_FILTER_RETURN_WITH_INVERSION true;
}
}
return false;
_FILTER_RETURN_WITH_INVERSION false;
}
//---------------------------------------------------------------------------

Expand All @@ -53,10 +53,10 @@ G4bool GateMaterialFilter::Accept(const G4Track* aTrack)
for ( size_t i = 0; i < theMdef.size(); i++){
if ( theMdef[i] == aTrack->GetMaterial()->GetName() ) {
nFilteredParticles++;
return true;
_FILTER_RETURN_WITH_INVERSION true;
}
}
return false;
_FILTER_RETURN_WITH_INVERSION false;
}
//---------------------------------------------------------------------------

Expand Down
Loading

0 comments on commit b0e7a43

Please sign in to comment.