ATLAS Offline Software
TauElecSubtractAlg.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 #include "TauElecSubtractAlg.h"
6 #include <algorithm>
7 
9 {
17  ATH_CHECK( m_eleLHSelectTool.retrieve() );
18  ATH_CHECK( m_stdJetTVADecoKey.initialize() );
19 
20  return StatusCode::SUCCESS;
21 }
22 
23 StatusCode TauElecSubtractAlg::execute (const EventContext& ctx) const
24 {
28  if(!electronInput.isValid() || !clustersInput.isValid() || !tracksInput.isValid())
29  {
30  if (!electronInput.isValid()) ATH_MSG_ERROR( "Collection " << electronInput.key() << " is not valid" );
31  if (!clustersInput.isValid()) ATH_MSG_ERROR( "Collection " << clustersInput.key() << " is not valid" );
32  if (!tracksInput.isValid()) ATH_MSG_ERROR( "Collection " << tracksInput.key() << " is not valid" );
33  return StatusCode::FAILURE;
34  }
35 
37  if (!stdJetTVADecoHandle.isPresent())
38  {
39  ATH_MSG_ERROR("Standard jet TVA decoration is not valid for InDetTrackParticles");
40  return StatusCode::FAILURE;
41  }
42 
44  ATH_CHECK( clustersOutputHandle.record(std::make_unique<xAOD::CaloClusterContainer>(), std::make_unique<xAOD::CaloClusterAuxContainer>()) );
45 
47  ATH_CHECK( tracksOutputHandle.record(std::make_unique<xAOD::TrackParticleContainer>(), std::make_unique<xAOD::TrackParticleAuxContainer>()) );
48 
50  ATH_CHECK( removedClustersOutputHandle.record(std::make_unique<xAOD::CaloClusterContainer>(), std::make_unique<xAOD::CaloClusterAuxContainer>()) );
51 
53  ATH_CHECK( removedTracksOutputHandle.record(std::make_unique<xAOD::TrackParticleContainer>(), std::make_unique<xAOD::TrackParticleAuxContainer>()) );
54 
55  std::vector<bool> selectElectron(electronInput->size(), false);
56 
57  for (const xAOD::Electron* elec : *electronInput.cptr()) {
58  selectElectron.at(elec->index()) = static_cast<bool>(m_eleLHSelectTool->accept(elec));
59  }
60 
61  // early stopping: if no electron is selected, the EleRM reconstruction should not run
62  // the simplest strategy is to write out empty cluster and track containers
63  if (std::find(selectElectron.begin(),selectElectron.end(),true) == selectElectron.end()) return StatusCode::SUCCESS;
64 
65  xAOD::CaloClusterContainer* clustersOutputContainer = clustersOutputHandle.ptr();
66  clustersOutputContainer->reserve(clustersInput->size());
67 
68  xAOD::TrackParticleContainer* tracksOutputContainer = tracksOutputHandle.ptr();
69  tracksOutputContainer->reserve(tracksInput->size());
70 
71  std::vector<const xAOD::TrackParticle *> tracks_to_remove;
72  std::vector<const xAOD::CaloCluster *> clusters_to_remove;
73  if(!m_doNothing){
74  for (const xAOD::Electron* elec : *electronInput.cptr())
75  {
76  if (selectElectron.at(elec->index()))
77  {
78  tracks_to_remove = xAOD::EgammaHelpers::getTrackParticlesVec(elec, true, true);
79  std::vector<ElementLink< xAOD::CaloClusterContainer>> elec_cluster_links = elec->caloClusterLinks();
80  for (const auto& elec_cluster_link : elec_cluster_links){
81  if (elec_cluster_link.isValid()){
82  std::vector<const xAOD::CaloCluster*> orig_clusters = xAOD::EgammaHelpers::getAssociatedTopoClusters(*elec_cluster_link);
83  clusters_to_remove.insert(clusters_to_remove.end(), orig_clusters.cbegin(), orig_clusters.cend());
84  }
85  }
86  }
87  }
88  } else {
89  ATH_MSG_WARNING("DOING NOTHING... For validation only, please check your config.");
90  }
91  for (const xAOD::TrackParticle* old_track : *tracksInput.cptr())
92  {
93  auto where = std::find_if(tracks_to_remove.cbegin(), tracks_to_remove.cend(),
94  [&](const xAOD::TrackParticle* target){return (target == old_track);}
95  );
96  if (where == tracks_to_remove.cend())
97  {
98  auto *new_track = new xAOD::TrackParticle();
99  tracksOutputContainer->push_back(new_track);
100  *new_track = *old_track;
101  auto link = ElementLink< xAOD::TrackParticleContainer >( *tracksInput, old_track->index() );
102  static const SG::AuxElement::Accessor<ElementLink<xAOD::TrackParticleContainer>> acc_originalObjectDecor("ERMOriginalTrack");
103  acc_originalObjectDecor(*new_track) = link;
104  }
105  }
106  ATH_MSG_DEBUG("Old tracks size = " << tracksInput->size() << ", new tracks size = " << tracksOutputContainer->size() << ", expected diff = " << tracks_to_remove.size());
107 
108  for (const xAOD::CaloCluster* old_cluster : *clustersInput.cptr()) {
109  auto where = std::find_if(clusters_to_remove.cbegin(), clusters_to_remove.cend(),
110  [&](const xAOD::CaloCluster* target){return (target == old_cluster);}
111  );
112  if (where == clusters_to_remove.cend()){
113  auto *new_cluster = new xAOD::CaloCluster();
114  clustersOutputContainer->push_back(new_cluster);
115  *new_cluster = *old_cluster;
116  auto link = ElementLink< xAOD::CaloClusterContainer >( *clustersInput, old_cluster->index() );
117  static const SG::AuxElement::Accessor<ElementLink<xAOD::CaloClusterContainer>> acc_originalObjectDecor("ERMOriginalCaloCluster");
118  acc_originalObjectDecor(*new_cluster) = link;
119  }
120  }
121  ATH_MSG_DEBUG("Old cluster size = " << clustersInput->size() << ", new cluster size = " << clustersOutputContainer->size() << ", expected diff = " << clusters_to_remove.size());
122 
123  xAOD::CaloClusterContainer* removedClustersOutputCont = removedClustersOutputHandle.ptr();
124  removedClustersOutputCont->reserve(clusters_to_remove.size());
125  for (const xAOD::CaloCluster* cls : clusters_to_remove){
126  auto *new_removed_cluster = new xAOD::CaloCluster();
127  *new_removed_cluster = *cls;
128  removedClustersOutputCont->push_back(new_removed_cluster);
129  }
130 
131  xAOD::TrackParticleContainer* removedTracksOutputCont = removedTracksOutputHandle.ptr();
132  removedTracksOutputCont->reserve(tracks_to_remove.size());
133  for (const xAOD::TrackParticle* trk : tracks_to_remove){
134  auto *new_removed_track = new xAOD::TrackParticle();
135  *new_removed_track = *trk;
136  removedTracksOutputCont->push_back(new_removed_track);
137  }
138 
139  return StatusCode::SUCCESS;
140 }
DataVector::reserve
void reserve(size_type n)
Attempt to preallocate enough memory for a specified number of elements.
TauElecSubtractAlg::m_removedTracksOutput
SG::WriteHandleKey< xAOD::TrackParticleContainer > m_removedTracksOutput
Definition: TauElecSubtractAlg.h:50
xAOD::EgammaHelpers::getAssociatedTopoClusters
std::vector< const xAOD::CaloCluster * > getAssociatedTopoClusters(const xAOD::CaloCluster *cluster)
Return a vector of all the topo clusters associated with the egamma cluster.
Definition: EgammaxAODHelpers.cxx:65
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
TauElecSubtractAlg::m_tracksOutput
SG::WriteHandleKey< xAOD::TrackParticleContainer > m_tracksOutput
Definition: TauElecSubtractAlg.h:48
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
TauElecSubtractAlg::m_stdJetTVADecoKey
SG::ReadDecorHandleKey< xAOD::TrackParticleContainer > m_stdJetTVADecoKey
Definition: TauElecSubtractAlg.h:53
SG::ReadDecorHandle::isPresent
bool isPresent() const
Is the referenced container present in SG?
CaloClusterListBadChannel.cls
cls
Definition: CaloClusterListBadChannel.py:8
TauElecSubtractAlg::m_elecInput
SG::ReadHandleKey< xAOD::ElectronContainer > m_elecInput
Definition: TauElecSubtractAlg.h:44
xAOD::EgammaHelpers::getTrackParticlesVec
std::vector< const xAOD::TrackParticle * > getTrackParticlesVec(const xAOD::Egamma *eg, bool useBremAssoc=true, bool allParticles=true)
Return a list of all or only the best TrackParticle associated to the object.
Definition: EgammaxAODHelpers.cxx:141
xAOD::CaloCluster
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloCluster.h:19
xAOD::TrackParticle
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Definition: Event/xAOD/xAODTracking/xAODTracking/TrackParticle.h:13
TauElecSubtractAlg::m_doNothing
Gaudi::Property< bool > m_doNothing
Definition: TauElecSubtractAlg.h:56
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TauElecSubtractAlg::m_removedClustersOutput
SG::WriteHandleKey< xAOD::CaloClusterContainer > m_removedClustersOutput
Definition: TauElecSubtractAlg.h:49
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
TauElecSubtractAlg::initialize
virtual StatusCode initialize() override
Definition: TauElecSubtractAlg.cxx:8
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::WriteHandle::ptr
pointer_type ptr()
Dereference the pointer.
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
python.Utils.unixtools.where
def where(filename, prepath=[])
"which" for python files -------------------------------------------------—
Definition: unixtools.py:53
TauElecSubtractAlg::m_tracksInput
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_tracksInput
Definition: TauElecSubtractAlg.h:47
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TauElecSubtractAlg::m_clustersOutput
SG::WriteHandleKey< xAOD::CaloClusterContainer > m_clustersOutput
Definition: TauElecSubtractAlg.h:46
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
TauElecSubtractAlg.h
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
xAOD::Electron_v1
Definition: Electron_v1.h:34
TauElecSubtractAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override
Definition: TauElecSubtractAlg.cxx:23
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
copySelective.target
string target
Definition: copySelective.py:37
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
TauElecSubtractAlg::m_eleLHSelectTool
ToolHandle< IAsgElectronLikelihoodTool > m_eleLHSelectTool
Definition: TauElecSubtractAlg.h:55
TauElecSubtractAlg::m_clustersInput
SG::ReadHandleKey< xAOD::CaloClusterContainer > m_clustersInput
Definition: TauElecSubtractAlg.h:45
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.