ATLAS Offline Software
Loading...
Searching...
No Matches
ParticleSelectionAlg.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5*/
6
7// ParticleSelectionAlg.cxx
8// Implementation file for class ParticleSelectionAlg
9// Author: Karsten Koeneke <karsten.koeneke@cern.ch>
11
12// EventUtils includes
14
15// FrameWork includes
16#include "Gaudi/Property.h"
18
19// EDM includes
22#include "xAODBase/IParticle.h"
51
53// Public methods:
55
56// Constructors
59 ISvcLocator* pSvcLocator ) :
60 ExpressionParserUser< ::AthAnalysisAlgorithm>( name, pSvcLocator )
61{
62 m_cutBKCName = name; // cppcheck-suppress useInitializationList; deprecated Property constructor
63}
64
65
66
67// Destructor
71
72
73
74// Athena Algorithm's Hooks
77{
78 ATH_MSG_DEBUG ("Initializing " << name() << "...");
79
80 // Print out the used configuration
81 ATH_MSG_DEBUG ( " using EventInfo = " << m_evtInfoName );
82 ATH_MSG_DEBUG ( " using InputContainer = " << m_inCollKey );
83 ATH_MSG_DEBUG ( " using OutputContainer = " << m_outCollKey );
84 ATH_MSG_DEBUG ( " using WriteSplitOutputContainer = " << m_writeSplitAux );
85 ATH_MSG_DEBUG ( " using OutputContainerOwnershipPolicy = " << m_outOwnPolicyName );
86 ATH_MSG_DEBUG ( " using SelectionToolList = " << m_selTools );
87 ATH_MSG_DEBUG ( " using Selection = " << m_selection );
88 ATH_MSG_DEBUG ( " using DoCutBookkeeping = " << m_doCutFlow );
89 ATH_MSG_DEBUG ( " using CutBookkeeperContainer = " << m_cutBKCName );
90
91
92 // initialize proxy loaders for expression parsing
93 if ( !(m_selection.value().empty()) ){
95 }
96
97 // initialize the counters
99
100 // Determine the ownership policy of the output container
101 if ( m_outOwnPolicyName.value() == "OWN_ELEMENTS" ) {
103 }
104 else if ( m_outOwnPolicyName.value() == "VIEW_ELEMENTS" ) {
106 }
107 else {
108 ATH_MSG_ERROR("Unrecognized ownership policy for the output container: " << m_outOwnPolicyName );
109 return StatusCode::FAILURE;
110 }
111
112 // Retrieve all tools
113 ATH_CHECK( m_selTools.retrieve() );
114
115 ATH_MSG_DEBUG ( "==> done with initialize " << name() << "..." );
116 return StatusCode::SUCCESS;
117}
118
119
120
122{
123 ATH_MSG_DEBUG ("Finalizing " << name() << "...");
124
125 // Release all tools and services
126 ATH_CHECK( m_selTools.release() );
127
129
130 return StatusCode::SUCCESS;
131}
132
133
134
136{
137 ATH_MSG_DEBUG ("start " << name() << "...");
138
139 // Nothing to be done here, if cut-flow bookkeeping was not requested
140 if (!m_doCutFlow){ return StatusCode::SUCCESS; }
141
142 // Get some properties from the input meta data
143 std::string inputStreamName = "Stream";
145 std::vector<std::string> streamNameList;
146 inputMetaStore()->keys(clid, streamNameList);
147 if ( streamNameList.size() >=1 ){ inputStreamName = streamNameList[0]; }
148 // Get the processing cycle number that we need to set for us
149 int maxInputCycle = -1;
151 xAOD::CutBookkeeperContainer* inCutBKCont = nullptr;
152 ATH_CHECK( inputMetaStore()->retrieve(inCutBKCont, m_cutBKCName.value() ) );
153 if (inCutBKCont){
154 maxInputCycle = inCutBKCont->maxCycle();
155 }
156 }
157 if (maxInputCycle<0){ maxInputCycle = 0; }
158 else { maxInputCycle += 1; }
159
160 // Check if we already have a container in the output meta-data store
161 xAOD::CutBookkeeperContainer* cutBKCont = nullptr;
163 ATH_CHECK( inputMetaStore()->retrieve(cutBKCont, m_cutBKCName.value() ) );
164 // Set the index where we will start having our CutBookkeepers in this container
165 m_cutBKStartIdx = cutBKCont->size();
166 }
167 else {
168 // Create and register the container that will hold the cut-flow information
169 cutBKCont = new xAOD::CutBookkeeperContainer();
170 // Take care of the peculiarities of the new xAOD EDM, i.e., create the needed AuxStore
172 cutBKCont->setStore( cutBKAuxCont ); //gives it a new associated aux container
173 ATH_CHECK( outputMetaStore()->record( cutBKCont, m_cutBKCName.value() ) );
174 ATH_CHECK( outputMetaStore()->record( cutBKAuxCont, m_cutBKCName.value()+"Aux." ) );
175 ATH_MSG_VERBOSE( "Recorded xAOD::CutBookkeeperContainer " << m_cutBKCName.value() << "Aux." );
176 }
177
178 //------------- for the AsgSelectionTools --------------
179 // Now, register one CutBookkeeper per cut that will be applied.
180 // For each of the registered tools, get the TAccept and ask it for all known cuts.
181 for ( std::size_t toolIdx=0; toolIdx < m_selTools.size(); ++toolIdx ){
182 const ToolHandle<IAsgSelectionTool>& tool = m_selTools[toolIdx];
183 // Fill the index bookkeeping at what index in the CutBookkeeperContainer
184 // the CutBookkeepers for this tool start.
185 m_selToolIdxOffset.push_back( cutBKCont->size() );
186 // Get some needed quantities
187 const std::string toolName = tool->name();
188 const asg::AcceptInfo& acceptInfo = tool->getAcceptInfo();
189 const unsigned int nCuts = acceptInfo.getNCuts();
190 for ( unsigned int iCut=0; iCut<nCuts; ++iCut ){
191 // Get the name and description of this cut
192 const std::string cutName = acceptInfo.getCutName(iCut);
193 const std::string cutDescr = acceptInfo.getCutDescription(iCut);
194 // Create a new xAOD::CutBookkeeper and add it to the container
196 cutBKCont->push_back(cutBK);
197 // Now, set its properties
198 cutBK->setName(toolName+"_"+cutName);
199 cutBK->setDescription(cutDescr);
200 cutBK->setCutLogic(xAOD::CutBookkeeper::CutLogic::REQUIRE); // Each cut must be passed, thus REQUIRE, i.e, logical AND
201 cutBK->setInputStream(inputStreamName);
202 cutBK->setCycle(maxInputCycle);
203 }
204 }
205
206 //------------- for the ExpressionParsing in this algorithm --------------
207 if ( !(m_selection.value().empty()) ){
208 // Create a new xAOD::CutBookkeeper and add it to the container
210 cutBKCont->push_back(cutBK);
211 // Now, set its properties
212 cutBK->setName(this->name());
213 cutBK->setDescription(m_selection.value());
214 cutBK->setCutLogic(xAOD::CutBookkeeper::CutLogic::REQUIRE); // Each cut must be passed, thus REQUIRE, i.e, logical AND
215 cutBK->setInputStream(inputStreamName);
216 cutBK->setCycle(maxInputCycle);
217 m_idxSelParster = cutBK->index();
218 }
219
220 return StatusCode::SUCCESS;
221}
222
223
224
225
227{
228 // Increase the event counter
230 // Simple status message at the beginning of each event execute,
231 ATH_MSG_DEBUG ( "==> execute " << name() << " on " << m_nEventsProcessed << ". event..." );
232
233
234 // Figure out what type the input container has, if we didn't do it yet
235 if ( m_contType == UNKNOWN ){
248 }
249 if ( m_contType == UNKNOWN ){
250 ATH_MSG_FATAL("We couldn't determine the type of the container... abort!");
251 return StatusCode::FAILURE;
252 }
253
254 // Get the input container and create the output containers
255 const xAOD::IParticleContainer* inContainer = nullptr;
256 ATH_CHECK( evtStore()->retrieve( inContainer, m_inCollKey.value() ) );
257 ATH_MSG_DEBUG ( "Input collection = " << m_inCollKey.value()
258 << " retrieved from StoreGate which has " << inContainer->size() << " entries." );
259
260 // --------------------------------------------------------------------------
261 // Do the expression parsing once per event already here
262 // --------------------------------------------------------------------------
263 std::vector<int> resultVec;
264 resultVec.reserve(inContainer->size());
265 if ( !(m_selection.value().empty()) ){
266 resultVec = m_parser->evaluateAsVector();
267 // Check that the lengths of the input container and the result vector are the same
268 if ( inContainer->size() != resultVec.size() ) {
269 ATH_MSG_ERROR("We got an input container, but its size (" << inContainer->size()
270 << ") doesn't match the size of the result vector: " << resultVec.size() );
271 return StatusCode::FAILURE;
272 }
273 ATH_MSG_VERBOSE("Have a container of size " << inContainer->size()
274 << " and a result vector of size " << resultVec.size() );
275 } // End: If expression parsing was requested in the first place
276
277
278 // --------------------------------------------------------------------------
279 // Do the heavy lifting of actually creating the new and reduced output container(s)
280 // --------------------------------------------------------------------------
281 if ( m_contType == PHOTON ){
283 }
284 else if ( m_contType == ELECTRON ){
286 }
287 else if ( m_contType == MUON ){
289 }
290 else if ( m_contType == TAU ){
292 }
293 else if ( m_contType == JET ){
295 }
296 else if ( m_contType == TRUTHPARTICLE ){
298 }
299 else if ( m_contType == COMPOSITEPARTICLE ){
301 }
302 else if ( m_contType == PARITCLEFLOW ){
304 }
305 else if ( m_contType == NEUTRALPARTICLE ){
307 }
308 else if ( m_contType == TRACKPARTICLE ){
310 }
311 else if ( m_contType == PARTICLE ){
313 }
314 else if ( m_contType == CALOCLUSTER ){
316 }
317
318 return StatusCode::SUCCESS;
319}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_DEBUG(x)
This file contains the class definition for the EventStreamInfo class.
uint32_t CLID
The Class ID type.
const ServiceHandle< StoreGateSvc > & outputMetaStore() const
Const accessor for the output metadata store.
const ServiceHandle< StoreGateSvc > & inputMetaStore() const
Const accessor for the input metadata store.
AthAnalysisAlgorithm(const std::string &name)
Constructor taking just a name.
value_type push_back(value_type pElem)
Add an element to the end of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
std::conditional< 1==1, std::unique_ptr< ExpressionParsing::ExpressionParser >, std::array< std::unique_ptr< ExpressionParsing::ExpressionParser >, 1 > >::type m_parser
StatusCode initializeParser(const ExpressionParsing::SelectionArg< 1 > &selection_string)
virtual StatusCode execute() override
Athena algorithm's execute hook.
virtual StatusCode start() override
Athena algorithm's beginRun hook (called once before running over the events, after initialize)
Gaudi::Property< std::string > m_outOwnPolicyName
Defines the ownership policy of the output container.
contType_t m_contType
The variable that holds the value that we find for the input container.
ToolHandleArray< IAsgSelectionTool > m_selTools
The list of IAsgSelectionTools.
Gaudi::Property< std::string > m_evtInfoName
Name of the EventInfo object.
std::size_t m_idxSelParster
Store the index of the CutBookKeeper in the CutBookkeeperContainer for the selection using the Expres...
SG::OwnershipPolicy m_outOwnPolicy
The internally used translation for the ownership policy.
virtual StatusCode finalize() override
Athena algorithm's finalize hook.
std::vector< std::size_t > m_selToolIdxOffset
The list of pairs of the tool index of the AsgSelectionTools and the starting index of the correspond...
virtual StatusCode initialize() override
Athena algorithm's initalize hook.
Gaudi::Property< std::string > m_selection
The selection string that will select which xAOD::IParticles to keep from an xAOD::IParticleContainer...
Gaudi::Property< std::string > m_inCollKey
Input container name.
Gaudi::Property< bool > m_doCutFlow
If true (default: false), do the bookkeeping of how many particles passed which selection cuts.
Gaudi::Property< std::string > m_outCollKey
Output collection name (deep copies of the original ones)
ParticleSelectionAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
unsigned long m_nEventsProcessed
Internal event counter.
Gaudi::Property< std::string > m_cutBKCName
The name of the resulting xAOD::CutBookkeeperContainer.
std::size_t m_cutBKStartIdx
The starting index of where in the CutBookkeeperContainer our new CutBookkeepers start.
virtual ~ParticleSelectionAlg()
Destructor:
StatusCode selectParticles(const xAOD::IParticleContainer *inContainer, const std::vector< int > &resultVec) const
Private function to perform the actualy work.
Gaudi::Property< bool > m_writeSplitAux
Decide if we want to write a fully-split AuxContainer such that we can remove any variables.
size_t index() const
Return the index of this element within its container.
unsigned int getNCuts() const
Get the number of cuts defined.
Definition AcceptInfo.h:46
const std::string & getCutName(unsigned int cutPosition) const
Get the name of a cut, based on the cut position (slow, avoid usage)
const std::string & getCutDescription(const std::string &cutName) const
Get the description of a cut, based on the cut name.
int maxCycle() const
Get the maximum cycle number of any xAOD::CutBookkeepers in the container.
void setName(const std::string &name)
Set the name of this CutBookkeeper.
void setDescription(const std::string &description)
Set the description of this CutBookkeeper.
void setCutLogic(CutLogic logic)
Set the cut logic of this CutBookkeeper, e.g., ACCEPT, REQUIRE, VETO.
void setInputStream(const std::string &inputstream)
Set the name of the current input-file stream object for this CutBookkeeper.
void setCycle(int cycle)
Set the skimming cycle that this CutBookkeeper is running in.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
@ OWN_ELEMENTS
this data object owns its elements
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
CutBookkeeperAuxContainer_v1 CutBookkeeperAuxContainer
Define the latest version of the CutBookkeeperAuxContainer class.
CutBookkeeper_v1 CutBookkeeper
Define the latest version of the CutBookkeeper class.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.
CutBookkeeperContainer_v1 CutBookkeeperContainer
Define the latest version of the CutBookkeeperContainer class.