ATLAS Offline Software
Loading...
Searching...
No Matches
AsgViewFromSelectionAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8
9//
10// includes
11//
12
14
15#include <CxxUtils/fpcompare.h>
26
27//
28// method implementations
29//
30
31namespace CP
32{
33 template<typename Type> StatusCode AsgViewFromSelectionAlg ::
34 executeTemplate (const CP::SystematicSet& sys)
35 {
36 const Type *input = nullptr;
37 ANA_CHECK (evtStore()->retrieve (input, m_inputHandle.getName (sys)));
38 auto viewCopy = std::make_unique<ConstDataVector<Type>> (SG::VIEW_ELEMENTS);
39 for (const auto particle : *input)
40 {
41 bool keep = true;
42 for (const auto& accessor : m_accessors)
43 {
44 if ((accessor.first->getBits (*particle) | accessor.second) != selectionAccept())
45 {
46 keep = false;
47 break;
48 }
49 }
50 if (keep)
51 {
52 viewCopy->push_back (particle);
53 }
54 }
55
56 if (m_sortPt)
57 {
58 std::sort (viewCopy->begin(), viewCopy->end(), [] (const xAOD::IParticle *a, const xAOD::IParticle *b) {return CxxUtils::fpcompare::greater (a->pt(), b->pt());});
59 }
60
61 // If anyone might be concerned about efficiency here, this will
62 // add/sort a couple more entries than needed only to remove them
63 // from the vector afterwards, so there is a slight efficiency
64 // loss. However, this option is not expected to be used very
65 // often and the algorithm is still expected to run quickly, so I
66 // decided to keep the code above simpler and just do this as a
67 // separate step, instead of trying to optimize this by
68 // integrating it with the code above.
69 if (viewCopy->size() > m_sizeLimit)
70 viewCopy->resize (m_sizeLimit);
71
72 // In case we want to output a view copy, do that here.
73 if (!m_deepCopy)
74 {
75 ANA_CHECK (evtStore()->record (viewCopy.release(),
76 m_outputHandle.getName (sys)));
77 return StatusCode::SUCCESS;
78 }
79
80 // Apparently we want to make a deep copy. So set that one up.
81 auto deepCopy = std::make_unique<Type> ();
82 auto aux = std::make_unique<xAOD::AuxContainerBase> ();
83 deepCopy->setStore (aux.get());
84 deepCopy->reserve (viewCopy->size());
85 for (auto particle : *viewCopy)
86 {
87 auto pcopy = std::make_unique<typename Type::base_value_type> ();
88 typename Type::value_type pcopyPtr = pcopy.get();
89 deepCopy->push_back (std::move (pcopy));
90 *pcopyPtr = *particle;
91 }
92
93 // Record the deep copy into the event store.
94 ANA_CHECK (evtStore()->record (deepCopy.release(),
95 m_outputHandle.getName (sys)));
96 ANA_CHECK (evtStore()->record (aux.release(),
97 m_outputHandle.getName (sys) + "Aux."));
98
99 return StatusCode::SUCCESS;
100 }
101
102
103
104
105 StatusCode AsgViewFromSelectionAlg ::
106 executeFindType (const CP::SystematicSet& sys)
107 {
108 if( m_allowMissing ) {
109 const std::string& name = m_inputHandle.getName( sys );
112 return StatusCode::SUCCESS;
113 }
114 }
115
116 const xAOD::IParticleContainer *input = nullptr;
117 ANA_CHECK (m_inputHandle.retrieve (input, sys));
118
119 if (dynamic_cast<const xAOD::ElectronContainer*> (input))
120 {
121 m_function =
123 }
124 else if (dynamic_cast<const xAOD::PhotonContainer*> (input))
125 {
126 m_function =
128 }
129 else if (dynamic_cast<const xAOD::JetContainer*> (input))
130 {
131 m_function =
133 }
134 else if (dynamic_cast<const xAOD::MuonContainer*> (input)) {
135 m_function =
137 }
138 else if (dynamic_cast<const xAOD::TauJetContainer*> (input))
139 {
140 m_function =
142 }
143 else if (dynamic_cast<const xAOD::DiTauJetContainer*> (input))
144 {
145 m_function =
147 }
148 else if (dynamic_cast<const xAOD::TrackParticleContainer*> (input))
149 {
150 m_function =
152 }
153 else if (dynamic_cast<const xAOD::TruthParticleContainer*> (input))
154 {
155 m_function =
157 }
158 else
159 {
160 ANA_MSG_ERROR ("unknown type contained in AsgViewFromSelectionAlg, please extend it");
161 return StatusCode::FAILURE;
162 }
163
164 return (this->*m_function) (sys);
165 }
166
167
168
169 StatusCode AsgViewFromSelectionAlg ::
170 executeMissing (const CP::SystematicSet&)
171 {
172 return StatusCode::SUCCESS;
173 }
174
175
176
177
178 StatusCode AsgViewFromSelectionAlg ::
179 initialize ()
180 {
181 ANA_CHECK (m_systematicsList.service().registerCopy (m_inputHandle.getNamePattern(), m_outputHandle.getNamePattern()));
184 ANA_CHECK (m_systematicsList.initialize());
185
186 if (m_ignore.size() > m_selection.size())
187 {
188 ANA_MSG_ERROR ("ignore property can't have more properties than selection property");
189 return StatusCode::FAILURE;
190 }
191 for (std::size_t iter = 0, end = m_selection.size(); iter != end; ++ iter)
192 {
193 SelectionType ignore = 0;
194 if (iter < m_ignore.size())
195 ignore = m_ignore[iter];
196 std::unique_ptr<ISelectionReadAccessor> accessor;
198 m_accessors.push_back (std::make_pair (std::move (accessor), ignore));
199 }
200
201#ifndef XAOD_STANDALONE
202 const CLID clidRead = detail::getClidForDependency<xAOD::IParticleContainer>("", "deco", false);
203 const CLID clidWrite = detail::getClidForDependency<xAOD::IParticleContainer>("", "deco", true);
204 std::function<void(const DataObjID&, Gaudi::DataHandle::Mode)> addAlgDependency = [this] (const DataObjID& id, Gaudi::DataHandle::Mode mode) {
205 this->addDependency(id, mode);
206 };
207 for (const auto& decoName : m_systematicsList.service().getObjectDecorations(m_inputHandle.getNamePattern()))
208 {
209 ANA_CHECK (detail::addSysDependency(msg(), m_systematicsList.service(), addAlgDependency, clidRead, m_inputHandle.getNamePattern(), Gaudi::DataHandle::Reader, decoName, false));
210 ANA_CHECK (detail::addSysDependency(msg(), m_systematicsList.service(), addAlgDependency, clidWrite, m_outputHandle.getNamePattern(), Gaudi::DataHandle::Writer, decoName, true));
211 }
212#endif
213
214 return StatusCode::SUCCESS;
215 }
216
217
218
219 StatusCode AsgViewFromSelectionAlg ::
220 execute (const EventContext& /*ctx*/)
221 {
222 for (const auto& sys : m_systematicsList.systematicsVector())
223 {
224 ANA_CHECK ((this->*m_function) (sys));
225 }
226 return StatusCode::SUCCESS;
227 }
228}
DataVector adapter that acts like it holds const pointers.
#define ANA_MSG_ERROR(xmsg,...)
Macro printing error messages.
#define ANA_CHECK(EXP)
check whether the given expression was successful
uint32_t CLID
The Class ID type.
static Double_t a
ServiceHandle< StoreGateSvc > & evtStore()
Gaudi::Property< std::vector< std::string > > m_selection
StatusCode executeTemplate(const CP::SystematicSet &sys)
the templated version of execute for a single systematic
Gaudi::Property< bool > m_sortPt
Sort the output (view) container by pT.
Gaudi::Property< std::size_t > m_sizeLimit
StatusCode(AsgViewFromSelectionAlg::* m_function)(const CP::SystematicSet &sys)
SysReadHandle< xAOD::IParticleContainer > m_inputHandle
the input collection we run on
Gaudi::Property< bool > m_allowMissing
Allow the input container to be missing.
StatusCode executeMissing(const CP::SystematicSet &sys)
The version of execute for missing input containers.
SysWriteHandle< xAOD::IParticleContainer > m_outputHandle
the output view container we produce
Gaudi::Property< std::vector< SelectionType > > m_ignore
Gaudi::Property< bool > m_deepCopy
Perform a deep copy for creating the output container.
std::vector< std::pair< std::unique_ptr< ISelectionReadAccessor >, SelectionType > > m_accessors
the list of accessors and cut ignore list
SysListHandle m_systematicsList
the systematics list we run
Class to wrap a set of SystematicVariations.
Class providing the definition of the 4-vector interface.
Workaround x86 precision issues for FP inequality comparisons.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:116
CLID getClidForDependency(const std::string &typeName, const std::string &decoName, bool decoWrite)
StatusCode addSysDependency(MsgStream &msg, const ISystematicsSvc &svc, const std::function< void(const DataObjID &, Gaudi::DataHandle::Mode)> &addAlgDependency, const CLID clid, const std::string &name, Gaudi::DataHandle::Mode mode, const std::string &decoName, bool decoWrite)
Select isolated Photons, Electrons and Muons.
StatusCode makeSelectionReadAccessor(const std::string &expr, std::unique_ptr< ISelectionReadAccessor > &accessor, bool defaultToChar)
make the ISelectionReadAccessor for the given name
uint32_t SelectionType
the type for selection decorations that are meant to hold a asg::AcceptData
constexpr SelectionType selectionAccept()
the selection decoration to apply for objects that are selected
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
PhotonContainer_v1 PhotonContainer
Definition of the current "photon container version".
ElectronContainer_v1 ElectronContainer
Definition of the current "electron container version".
DiTauJetContainer_v1 DiTauJetContainer
Definition of the current DiTauJet container version.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".
JetContainer_v1 JetContainer
Definition of the current "jet container version".
TauJetContainer_v3 TauJetContainer
Definition of the current "taujet container version".
MuonContainer_v1 MuonContainer
Definition of the current "Muon container version".
TruthParticleContainer_v1 TruthParticleContainer
Declare the latest version of the truth particle container.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.
MsgStream & msg
Definition testRead.cxx:32