ATLAS Offline Software
Loading...
Searching...
No Matches
ParticleRemoverAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// EventUtils includes
7
10
12#include "xAODBase/IParticle.h"
27
28ParticleRemoverAlg::ParticleRemoverAlg( const std::string& name, ISvcLocator* pSvcLocator )
29 : AthAlgorithm( name, pSvcLocator )
30{
31}
32
33
35
36
38{
39 ATH_MSG_DEBUG ("Initializing " << name() << "...");
40
41 // Print the configuration to the log file
42 ATH_MSG_DEBUG( "Using: " << m_inCont );
43 ATH_MSG_DEBUG( "Using: " << m_outCont );
44 ATH_MSG_DEBUG( "Using: " << m_separator );
45 ATH_MSG_DEBUG( "Using: " << m_suffixes );
46 ATH_MSG_DEBUG( "Using: " << m_viewContNames );
47 ATH_MSG_DEBUG( "Using: " << m_resetViewConts );
48 ATH_MSG_DEBUG( "Using: " << m_outPrefix );
49
50 // Perform some sanity checks on the given container names
51 if ( m_inCont.value().empty() || m_outCont.value().empty() || m_viewContNames.value().empty() ) {
52 ATH_MSG_ERROR("Wrong user setup! You need to give a valid name for both the Input, Output, and SelectedViewContainers!");
53 return StatusCode::FAILURE;
54 }
55
56 // Abort on an unchecked systematics code
57 // StatusCode::enableFailure();
58
59 // Build the vector of all input and output container names
60 const std::size_t totSize = 1 + m_suffixes.value().size(); // the '1' comes from the InputContainer
61 m_inContNameList.resize(totSize);
62 m_inContList.resize(totSize);
63 m_outContNameList.resize(totSize);
64 m_inContNameList[0] = m_inCont.value();
65 m_outContNameList[0] = m_outCont.value();
66 for ( std::size_t i=1; i<totSize; ++i ) {
67 const std::string& currentSuffix = m_suffixes.value()[i-1];
68 ATH_MSG_VERBOSE("Using current suffix " << currentSuffix << " to search for matching containers");
69 if ( currentSuffix.rfind( m_separator.value(),0 ) == 0 ) { // If the currentSuffix starts with m_separator.value()
70 m_inContNameList[i] = m_inCont.value() + currentSuffix;
71 m_outContNameList[i] = m_outCont.value() + currentSuffix;
72 }
73 else {
74 m_inContNameList[i] = m_inCont.value() + m_separator.value() + currentSuffix;
75 m_outContNameList[i] = m_outCont.value() + m_separator.value() + currentSuffix;
76 }
77 }
78 // Print out the matches that we found
79 if ( msgLvl(MSG::VERBOSE) ) {
80 for ( std::size_t i=0; i<m_inContNameList.size(); ++i ){
81 ATH_MSG_VERBOSE("Matched input number " << i << " with input name " << m_inContNameList[i] << " to output name " << m_outContNameList[i]);
82 }
83 }
84
85
86
87 // Now, also try to map all the view container names to the input (shallow
88 // copy) containers. Set up with that the correct mapping of the new output
89 // view container names.
90 // Assume that all names where we have a suffix, but where we cannot match it
91 // to a suffix of the input container, it belongs to the origninal one.
92 if ( m_resetViewConts.value() || !(m_outPrefix.value().empty()) ){
93 ATH_MSG_VERBOSE("Going to iterate over " << totSize << " elements");
94 m_inViewContNameListList.reserve(totSize);
95 m_outViewContNameListList.reserve(totSize);
96 for ( std::size_t i=0; i<totSize; ++i ) {
97 ATH_MSG_VERBOSE("At " << i << "-th element");
98 if (i==0){
99 // This is the master container without any "___" in its name
100 std::vector<std::string> inViewNames;
101 std::vector<std::string> outViewNames;
102 for ( const std::string& inViewName : m_viewContNames.value() ){
103 ATH_MSG_VERBOSE("Looking at input view container name: " << inViewName);
104 std::size_t pos = inViewName.find(m_separator.value());
105 if ( pos == std::string::npos ){ // the separator is not found
106 ATH_MSG_VERBOSE("No seperator found");
107 inViewNames.push_back(inViewName);
108 outViewNames.push_back( m_outPrefix.value() + inViewName );
109 ATH_MSG_VERBOSE("Added input name " << inViewNames.back() << " and output name " << outViewNames.back());
110 }
111 else {
112 pos += m_separator.value().length();
113 const std::string foundSuffix = inViewName.substr(pos, std::string::npos);
114 ATH_MSG_VERBOSE("Seperator found and suffix found: " << foundSuffix);
115 // the separator is found, but the found suffix doesn't match any of the provided ones
116 if ( std::find( m_suffixes.value().begin(), m_suffixes.value().end(), foundSuffix) == m_suffixes.value().end() ){
117 inViewNames.push_back(inViewName);
118 outViewNames.push_back( m_outPrefix.value() + inViewName );
119 ATH_MSG_VERBOSE("Added2 input name " << inViewNames.back() << " and output name " << outViewNames.back());
120 }
121 }
122 }
123 m_inViewContNameListList.push_back(inViewNames);
124 m_outViewContNameListList.push_back(outViewNames);
125 }
126 else {
127 const std::string& currentSuffix = m_suffixes.value()[i-1];
128 ATH_MSG_VERBOSE("Looking at current suffix: " << currentSuffix);
129 std::vector<std::string> inViewNames;
130 std::vector<std::string> outViewNames;
131 for ( const std::string& inViewName : m_viewContNames.value() ){
132 ATH_MSG_VERBOSE("Looking at current input view container name: " << inViewName);
133 if ( inViewName.find(m_separator.value()+currentSuffix) != std::string::npos ){ // the suffix is found
134 inViewNames.push_back(inViewName);
135 outViewNames.push_back( m_outPrefix.value() + inViewName );
136 ATH_MSG_VERBOSE("Added3 input name " << inViewNames.back() << " and output name " << outViewNames.back());
137 }
138 }
139 m_inViewContNameListList.push_back(inViewNames);
140 m_outViewContNameListList.push_back(outViewNames);
141 }
142 } // End: loop over all containers
143 }
144 // Some sanity printouts
145 if ( msgLvl(MSG::VERBOSE) ) {
146 ATH_MSG_VERBOSE("Printing final input and output names...");
147 for ( std::size_t i=0; i<m_inViewContNameListList.size(); ++i ){
148 ATH_MSG_VERBOSE(" At i=" << i << "-th element");
149 const std::vector<std::string>& inViewNameList = m_inViewContNameListList[i];
150 const std::vector<std::string>& outViewNameList = m_outViewContNameListList[i];
151 ATH_MSG_VERBOSE(" Have " << inViewNameList.size() << " in view elements and " << outViewNameList.size() << " out view elements");
152 for ( std::size_t j=0; j<inViewNameList.size(); ++j ){
153 ATH_MSG_VERBOSE(" At j=" << j << "-th element");
154 const std::string& inName = inViewNameList[j];
155 const std::string& outName = outViewNameList[j];
156 ATH_MSG_VERBOSE(" Have input name " << inName << " paired with out name " << outName);
157 }
158 }
159 }
160
161 return StatusCode::SUCCESS;
162}
163
164
165
167{
168 ATH_MSG_DEBUG ("Finalizing " << name() << "...");
169
170 return StatusCode::SUCCESS;
171}
172
173
174
176{
177 ATH_MSG_DEBUG ("Executing " << name() << "...");
178 // Let's first clear some stuff
179 m_inContList.clear();
180 m_inContList.resize(m_inContNameList.size());
181 m_outContList.clear();
182
183 // Figure out what type the input container has, if we didn't do it yet
184 if ( m_contType == UNKNOWN ){
189 else if ( evtStore()->contains<xAOD::JetContainer>(m_inCont.value()) ){ m_contType = JET; }
197 }
198 if ( m_contType == UNKNOWN ){
199 ATH_MSG_FATAL("We couldn't determine the type of the container... abort!");
200 return StatusCode::FAILURE;
201 }
202
203 // Open the input container
204 for ( std::size_t i=0; i<m_inContNameList.size(); ++i ) {
205 ATH_CHECK( evtStore()->retrieve( m_inContList[i], m_inContNameList.at(i) ));
206 }
207
208 // Make a quick check that all input containers have the same size
209 const std::size_t inContSize = m_inContList[0]->size();
210 for ( const xAOD::IParticleContainer* inCont : m_inContList ){
211 if ( inContSize != inCont->size() ){
212 ATH_MSG_FATAL("The input container and its shallow copies don't have the same size! Aborting...");
213 return StatusCode::FAILURE;
214 }
215 }
216
217 // Create a vector of bools with the same size as the input container. This
218 // will be used to say if we want to keep that particular object.
219 // All entries will be initialized to false.
220 std::vector<bool> keepParticleVec (inContSize, false);
221 // Now, loop over all view containers and flag the particles that we want to
222 // keep with true in the above vector of bools.
223 for ( const std::string& viewContName : m_viewContNames.value() ){
224 const xAOD::IParticleContainer* inViewCont = nullptr;
225 ATH_CHECK( evtStore()->retrieve( inViewCont, viewContName ) );
226 // Make a quick check that the provided view containers are not larger
227 if ( inViewCont->size() > inContSize ){
228 ATH_MSG_FATAL("One of the input view containers is larger than the input container... aborting.");
229 return StatusCode::FAILURE;
230 }
231 for ( const xAOD::IParticle* part : *inViewCont ){
232 const std::size_t idx = part->index();
233 keepParticleVec[idx] = true;
234 }
235 }
236
237 // Do the heavy lifting of actually creating the new and reduced output container(s)
238 if ( m_contType == PHOTON ){
239 ATH_CHECK( this->removeParticles<xAOD::PhotonContainer>(keepParticleVec) );
240 }
241 else if ( m_contType == ELECTRON ){
242 ATH_CHECK( this->removeParticles<xAOD::ElectronContainer>(keepParticleVec) );
243 }
244 else if ( m_contType == MUON ){
245 ATH_CHECK( this->removeParticles<xAOD::MuonContainer>(keepParticleVec) );
246 }
247 else if ( m_contType == TAU ){
248 ATH_CHECK( this->removeParticles<xAOD::TauJetContainer>(keepParticleVec) );
249 }
250 else if ( m_contType == JET ){
251 ATH_CHECK( this->removeParticles<xAOD::JetContainer>(keepParticleVec) );
252 }
253 else if ( m_contType == TRUTHPARTICLE ){
255 }
256 else if ( m_contType == COMPOSITEPARTICLE ){
258 }
259 else if ( m_contType == PARITCLEFLOW ){
260 ATH_CHECK( this->removeParticles<xAOD::PFOContainer>(keepParticleVec) );
261 }
262 else if ( m_contType == NEUTRALPARTICLE ){
264 }
265 else if ( m_contType == TRACKPARTICLE ){
267 }
268 else if ( m_contType == PARTICLE ){
269 ATH_CHECK( this->removeParticles<xAOD::ParticleContainer>(keepParticleVec) );
270 }
271 else if ( m_contType == CALOCLUSTER ){
273 }
274
275 return StatusCode::SUCCESS;
276}
#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)
Handle mappings between names and auxid_t.
Interface for factory objects that create vectors.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
bool msgLvl(const MSG::Level lvl) const
size_type size() const noexcept
Returns the number of elements in the collection.
Gaudi::Property< std::vector< std::string > > m_suffixes
The names of all suffixes for the input and output container names.
std::vector< std::vector< std::string > > m_inViewContNameListList
Vector of all input view container names.
std::vector< std::vector< std::string > > m_outViewContNameListList
Vector of all output view container names.
StatusCode removeParticles(const std::vector< bool > &keepParticleVec)
Private function to perform the actualy work.
Gaudi::Property< bool > m_resetViewConts
Boolean to decide if the existing view containers should be re-mapped (default: true)
std::vector< xAOD::IParticleContainer * > m_outContList
Vector of all output containers.
virtual StatusCode initialize()
Standard Gaudi initialize method called once before the event loop.
std::vector< std::string > m_outContNameList
Vector of all output container names.
ParticleRemoverAlg(const std::string &name, ISvcLocator *pSvcLocator)
Standard constructor.
Gaudi::Property< std::string > m_separator
The string separator between the output container name and the sytematic variation (default="___")
virtual StatusCode execute()
Standard Gaudi execute method called once for every event.
virtual StatusCode finalize()
Standard Gaudi finalize method called once after the event loop.
virtual ~ParticleRemoverAlg()
Standard destructor.
Gaudi::Property< std::vector< std::string > > m_viewContNames
The names of all view containers that contain particles that we want to retain.
std::vector< std::string > m_inContNameList
Vector of all input container names.
contType_t m_contType
The variable that holds the value that we find for the input container.
std::vector< const xAOD::IParticleContainer * > m_inContList
Vector of all input containers.
Gaudi::Property< std::string > m_outCont
The output container name.
Gaudi::Property< std::string > m_outPrefix
Prefix to be used for all created output view containers.
Gaudi::Property< std::string > m_inCont
The input container name.
Class providing the definition of the 4-vector interface.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.