ATLAS Offline Software
SimEventFilter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef SIMULATIONBASE
6 // ISF_Algs includes
7 #include "SimEventFilter.h"
8 // FrameWork includes
9 #include "Gaudi/Property.h"
10 // McEventCollection
12 //
14 
15 
17 // Public methods:
19 
20 // Constructors
22 ISF::SimEventFilter::SimEventFilter( const std::string& name, ISvcLocator* pSvcLocator ) :
23  ::AthReentrantAlgorithm( name, pSvcLocator )
24 {
25 }
26 
27 // Athena Algorithm's Hooks
30 {
31  ATH_CHECK(m_filterParams.initialize(false));
32  ATH_MSG_VERBOSE ( "--------------------------------------------------------" );
33  ATH_MSG_VERBOSE ( "Initializing the ISF Sim Filter " );
34 
35  if (!m_genParticleCommonFilters.empty()) ATH_CHECK(m_genParticleCommonFilters.retrieve());
36  if (!m_genParticleOldFilters.empty() ) ATH_CHECK(m_genParticleOldFilters.retrieve());
37  if (!m_genParticleNewFilters.empty() ) ATH_CHECK(m_genParticleNewFilters.retrieve());
38 
39  ATH_CHECK( m_inputHardScatterEvgenKey.initialize() );
40 
41  // intialziation successful
42  return StatusCode::SUCCESS;
43 }
44 
46 {
47  ATH_MSG_VERBOSE ( "Finalizing ..." );
48 
49  ATH_MSG_INFO(m_filterParams.summary());
50  ATH_MSG_VERBOSE(" =====================================================================");
51 
52  return StatusCode::SUCCESS;
53 }
54 
56 #ifdef HEPMC3
57 bool ISF::SimEventFilter::passesFilters(HepMC::ConstGenParticlePtr& part, const ToolHandleArray<IGenParticleFilter>& filters) const
58 #else
59 bool ISF::SimEventFilter::passesFilters(HepMC::ConstGenParticlePtr part, const ToolHandleArray<IGenParticleFilter>& filters) const
60 #endif
61 {
62  // TODO: implement this as a std::find_if with a lambda function
63  for ( const auto& filter : filters ) {
64  // determine if the particle passes current filter
65 #ifdef HEPMC3
66  bool passFilter = filter->pass(part);
67 #else
68  bool passFilter = filter->pass(*part);
69 #endif
70  ATH_MSG_VERBOSE("Filter '" << filter.typeAndName() << "' returned: "
71  << (passFilter ? "true, will keep particle."
72  : "false, will remove particle."));
73 
74  if (!passFilter) return false;
75  }
76 
77  return true;
78 }
79 
80 StatusCode ISF::SimEventFilter::execute(const EventContext &ctx) const
81 {
82  ATH_MSG_DEBUG ("Executing ...");
83 
84  FilterReporter filter(m_filterParams, false, ctx);
85  SG::ReadHandle<McEventCollection> inputHardScatterEvgen(m_inputHardScatterEvgenKey, ctx);
86  if (!inputHardScatterEvgen.isValid()) {
87  ATH_MSG_FATAL("Unable to read input GenEvent collection '" << inputHardScatterEvgen.key() << "'");
88  return StatusCode::FAILURE;
89  }
90 
91  bool pass = false;
92 
93  for ( const HepMC::GenEvent* eventPtr : *inputHardScatterEvgen ) {
94  // skip empty events
95  if (eventPtr == nullptr) { continue; }
96 
97  ATH_MSG_DEBUG("Starting check of GenEvent with"
98  " signal_process_id=" << HepMC::signal_process_id(eventPtr) <<
99  " and event_number=" << eventPtr->event_number() );
100  for (auto p : *eventPtr) {
101  ATH_MSG_VERBOSE("Checking filters for particle: "<< p);
102  ATH_MSG_VERBOSE("Common filters:");
103  bool b_common = passesFilters(p,m_genParticleCommonFilters);
104  ATH_MSG_VERBOSE("Old filters:");
105  bool b_old = passesFilters(p,m_genParticleOldFilters);
106  ATH_MSG_VERBOSE("New filters:");
107  bool b_new = passesFilters(p,m_genParticleNewFilters);
108 
109  if ( b_common && (b_old!=b_new) ) {
110  pass=true;
111  }
112  if ( b_common && m_genParticleOldFilters.empty() && m_genParticleNewFilters.empty()) {
113  pass=true;
114  }
115  if ( pass ) {
116  ATH_MSG_DEBUG("Different result for particle "<<p<<" common="<<b_common<<" old="<<b_old<<" new="<<b_new);
117  if (p->production_vertex ()) {
118  ATH_MSG_VERBOSE(" prod :"<<p->production_vertex());
119  }
120  if (p->end_vertex ()) {
121  ATH_MSG_VERBOSE(" decay:"<<p->end_vertex());
122  }
123 
124  for ( const auto& filter : m_genParticleCommonFilters ) {
125  // determine if the particle passes current filter
126 #ifdef HEPMC3
127  bool passFilter = filter->pass(p);
128 #else
129  bool passFilter = filter->pass(*p);
130 #endif
131  ATH_MSG_DEBUG(" GenParticleCommonFilter '" << filter.typeAndName() << "' returned: "
132  << (passFilter ? "true, will keep particle."
133  : "false, will remove particle."));
134  }
135  for ( const auto& filter : m_genParticleOldFilters ) {
136  // determine if the particle passes current filter
137 #ifdef HEPMC3
138  bool passFilter = filter->pass(p);
139 #else
140  bool passFilter = filter->pass(*p);
141 #endif
142  ATH_MSG_DEBUG(" GenParticleOldFilter '" << filter.typeAndName() << "' returned: "
143  << (passFilter ? "true, will keep particle."
144  : "false, will remove particle."));
145  }
146  for ( const auto& filter : m_genParticleNewFilters ) {
147  // determine if the particle passes current filter
148 #ifdef HEPMC3
149  bool passFilter = filter->pass(p);
150 #else
151  bool passFilter = filter->pass(*p);
152 #endif
153  ATH_MSG_DEBUG(" GenParticleNewFilter '" << filter.typeAndName() << "' returned: "
154  << (passFilter ? "true, will keep particle."
155  : "false, will remove particle."));
156  }
157 
158  break;
159  }
160  }
161  }
162 
163  ATH_MSG_DEBUG ("End SimEventFilter, difference in filters: "<<(pass ? "found" : "not found")<<"="<<pass<<", invert="<<m_invertfilter);
164 
165  if (m_invertfilter) {
166  pass =! pass;
167  }
168 
169  filter.setPassed(pass);
170 
171  return StatusCode::SUCCESS;
172 }
173 #endif // SimEventFilter currently will not compile in the AthSimulation Project
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
FilterReporter
a guard class for use with ref FilterReporterParams
Definition: FilterReporter.h:35
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ISF::SimEventFilter::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Athena algorithm's interface method execute()
Definition: SimEventFilter.cxx:80
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
ISF::SimEventFilter::passesFilters
bool passesFilters(HepMC::ConstGenParticlePtr part, const ToolHandleArray< IGenParticleFilter > &filters) const
check if the given particle passes all filters
Definition: SimEventFilter.cxx:59
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle< McEventCollection >
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:513
ISF::SimEventFilter::SimEventFilter
SimEventFilter(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters.
Definition: SimEventFilter.cxx:22
covarianceTool.passFilter
bool passFilter
Definition: covarianceTool.py:604
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
covarianceTool.filter
filter
Definition: covarianceTool.py:514
ISF::SimEventFilter::finalize
virtual StatusCode finalize() override final
Athena algorithm's interface method finalize()
Definition: SimEventFilter.cxx:45
McEventCollection.h
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
FilterReporter.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SG::VarHandleBase::key
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:64
SimEventFilter.h
ISF::SimEventFilter::initialize
virtual StatusCode initialize() override final
Athena algorithm's interface method initialize()
Definition: SimEventFilter.cxx:29