ATLAS Offline Software
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>
20 #include <xAODJet/JetContainer.h>
21 #include <xAODMuon/MuonContainer.h>
26 
27 //
28 // method implementations
29 //
30 
31 namespace CP
32 {
33  template<typename Type> StatusCode AsgViewFromSelectionAlg ::
35  {
36  const Type *input = nullptr;
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  typename Type::value_type pcopy = new typename Type::base_value_type();
88  deepCopy->push_back (pcopy);
89  *pcopy = *particle;
90  }
91 
92  // Record the deep copy into the event store.
93  ANA_CHECK (evtStore()->record (deepCopy.release(),
94  m_outputHandle.getName (sys)));
95  ANA_CHECK (evtStore()->record (aux.release(),
96  m_outputHandle.getName (sys) + "Aux."));
97 
98  return StatusCode::SUCCESS;
99  }
100 
101 
102 
103 
106  {
107  if( m_allowMissing ) {
108  const std::string& name = m_inputHandle.getName( sys );
109  if( ! evtStore()->contains< xAOD::IParticleContainer >( name ) ) {
111  return StatusCode::SUCCESS;
112  }
113  }
114 
115  const xAOD::IParticleContainer *input = nullptr;
116  ANA_CHECK (m_inputHandle.retrieve (input, sys));
117 
118  if (dynamic_cast<const xAOD::ElectronContainer*> (input))
119  {
120  m_function =
121  &AsgViewFromSelectionAlg::executeTemplate<xAOD::ElectronContainer>;
122  }
123  else if (dynamic_cast<const xAOD::PhotonContainer*> (input))
124  {
125  m_function =
126  &AsgViewFromSelectionAlg::executeTemplate<xAOD::PhotonContainer>;
127  }
128  else if (dynamic_cast<const xAOD::JetContainer*> (input))
129  {
130  m_function =
131  &AsgViewFromSelectionAlg::executeTemplate<xAOD::JetContainer>;
132  }
133  else if (dynamic_cast<const xAOD::MuonContainer*> (input)) {
134  m_function =
135  &AsgViewFromSelectionAlg::executeTemplate<xAOD::MuonContainer>;
136  }
137  else if (dynamic_cast<const xAOD::TauJetContainer*> (input))
138  {
139  m_function =
140  &AsgViewFromSelectionAlg::executeTemplate<xAOD::TauJetContainer>;
141  }
142  else if (dynamic_cast<const xAOD::DiTauJetContainer*> (input))
143  {
144  m_function =
145  &AsgViewFromSelectionAlg::executeTemplate<xAOD::DiTauJetContainer>;
146  }
147  else if (dynamic_cast<const xAOD::TrackParticleContainer*> (input))
148  {
149  m_function =
150  &AsgViewFromSelectionAlg::executeTemplate<xAOD::TrackParticleContainer>;
151  }
152  else if (dynamic_cast<const xAOD::TruthParticleContainer*> (input))
153  {
154  m_function =
155  &AsgViewFromSelectionAlg::executeTemplate<xAOD::TruthParticleContainer>;
156  }
157  else
158  {
159  ANA_MSG_ERROR ("unknown type contained in AsgViewFromSelectionAlg, please extend it");
160  return StatusCode::FAILURE;
161  }
162 
163  return (this->*m_function) (sys);
164  }
165 
166 
167 
170  {
171  return StatusCode::SUCCESS;
172  }
173 
174 
175 
176 
178  initialize ()
179  {
180  ANA_CHECK (m_systematicsList.service().registerCopy (m_inputHandle.getNamePattern(), m_outputHandle.getNamePattern()));
184 
185  if (m_ignore.size() > m_selection.size())
186  {
187  ANA_MSG_ERROR ("ignore property can't have more properties than selection property");
188  return StatusCode::FAILURE;
189  }
190  for (std::size_t iter = 0, end = m_selection.size(); iter != end; ++ iter)
191  {
192  SelectionType ignore = 0;
193  if (iter < m_ignore.size())
194  ignore = m_ignore[iter];
195  std::unique_ptr<ISelectionReadAccessor> accessor;
197  m_accessors.push_back (std::make_pair (std::move (accessor), ignore));
198  }
199 
200  return StatusCode::SUCCESS;
201  }
202 
203 
204 
206  execute ()
207  {
208  for (const auto& sys : m_systematicsList.systematicsVector())
209  {
210  ANA_CHECK ((this->*m_function) (sys));
211  }
212  return StatusCode::SUCCESS;
213  }
214 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
CP::AsgViewFromSelectionAlg::m_selection
Gaudi::Property< std::vector< std::string > > m_selection
Definition: AsgViewFromSelectionAlg.h:57
CP::AsgViewFromSelectionAlg::m_inputHandle
SysReadHandle< xAOD::IParticleContainer > m_inputHandle
the input collection we run on
Definition: AsgViewFromSelectionAlg.h:46
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
CP::AsgViewFromSelectionAlg::initialize
StatusCode initialize() override
Definition: AsgViewFromSelectionAlg.cxx:178
CP::AsgViewFromSelectionAlg::m_ignore
Gaudi::Property< std::vector< SelectionType > > m_ignore
Definition: AsgViewFromSelectionAlg.h:60
CP::AsgViewFromSelectionAlg::m_allowMissing
Gaudi::Property< bool > m_allowMissing
Allow the input container to be missing.
Definition: AsgViewFromSelectionAlg.h:68
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
CP::AsgViewFromSelectionAlg::executeMissing
StatusCode executeMissing(const CP::SystematicSet &sys)
The version of execute for missing input containers.
Definition: AsgViewFromSelectionAlg.cxx:169
TruthParticleContainer.h
CP::SelectionType
uint32_t SelectionType
the type for selection decorations that are meant to hold a asg::AcceptData
Definition: SelectionHelpers.h:26
AuxContainerBase.h
AsgViewFromSelectionAlg.h
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
CP::AsgViewFromSelectionAlg::executeFindType
StatusCode executeFindType(const CP::SystematicSet &sys)
the version of execute to find the type
Definition: AsgViewFromSelectionAlg.cxx:105
ANA_MSG_ERROR
#define ANA_MSG_ERROR(xmsg)
Macro printing error messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:294
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
DiTauJetContainer.h
CP::SysListHandle::systematicsVector
const std::vector< CP::SystematicSet > & systematicsVector() const
the list of systematics to loop over
Definition: SysListHandle.cxx:96
CP::AsgViewFromSelectionAlg::m_systematicsList
SysListHandle m_systematicsList
the systematics list we run
Definition: AsgViewFromSelectionAlg.h:42
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
CP
Select isolated Photons, Electrons and Muons.
Definition: Control/xAODRootAccess/xAODRootAccess/TEvent.h:48
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
CP::AsgViewFromSelectionAlg::m_outputHandle
SysWriteHandle< xAOD::IParticleContainer > m_outputHandle
the output view container we produce
Definition: AsgViewFromSelectionAlg.h:51
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
DiTauMassTools::ignore
void ignore(T &&)
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:54
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
CP::AsgViewFromSelectionAlg::m_sortPt
Gaudi::Property< bool > m_sortPt
Sort the output (view) container by pT.
Definition: AsgViewFromSelectionAlg.h:64
CP::SysListHandle::initialize
::StatusCode initialize()
intialize this property
Definition: SysListHandle.cxx:69
ElectronContainer.h
CP::ISystematicsSvc::registerCopy
virtual StatusCode registerCopy(const std::string &fromName, const std::string &toName) const =0
register a (shallow) copy from one object to the next
ReadCalibFromCool.keep
keep
Definition: ReadCalibFromCool.py:85
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
fpcompare.h
Workaround x86 precision issues for FP inequality comparisons.
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
CP::AsgViewFromSelectionAlg::execute
StatusCode execute() override
Definition: AsgViewFromSelectionAlg.cxx:206
TauJetContainer.h
CP::AsgViewFromSelectionAlg::m_sizeLimit
Gaudi::Property< std::size_t > m_sizeLimit
Definition: AsgViewFromSelectionAlg.h:75
xAODType
Definition: ObjectType.h:13
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
CP::AsgViewFromSelectionAlg::m_deepCopy
Gaudi::Property< bool > m_deepCopy
Perform a deep copy for creating the output container.
Definition: AsgViewFromSelectionAlg.h:72
CP::makeSelectionReadAccessor
StatusCode makeSelectionReadAccessor(const std::string &expr, std::unique_ptr< ISelectionReadAccessor > &accessor, bool defaultToChar)
make the ISelectionReadAccessor for the given name
Definition: ISelectionAccessor.cxx:54
xAOD::JetAttributeAccessor::accessor
const AccessorWrapper< T > * accessor(xAOD::JetAttribute::AttributeID id)
Returns an attribute accessor corresponding to an AttributeID.
Definition: JetAccessorMap.h:26
MuonContainer.h
a
TList * a
Definition: liststreamerinfos.cxx:10
JetContainer.h
CP::selectionAccept
constexpr SelectionType selectionAccept()
the selection decoration to apply for objects that are selected
Definition: SelectionHelpers.h:35
CP::AsgViewFromSelectionAlg::m_accessors
std::vector< std::pair< std::unique_ptr< ISelectionReadAccessor >, SelectionType > > m_accessors
the list of accessors and cut ignore list
Definition: AsgViewFromSelectionAlg.h:79
CP::AsgViewFromSelectionAlg::executeTemplate
StatusCode executeTemplate(const CP::SystematicSet &sys)
the templated version of execute for a single systematic
Definition: AsgViewFromSelectionAlg.cxx:34
CP::AsgViewFromSelectionAlg::m_function
StatusCode(AsgViewFromSelectionAlg::* m_function)(const CP::SystematicSet &sys)
Definition: AsgViewFromSelectionAlg.h:95
PhotonContainer.h
CP::SysListHandle::service
const ISystematicsSvc & service() const
the service we use
TrackParticleContainer.h