ATLAS Offline Software
JetCaloClusterThinning.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // JetCaloClusterThinning.cxx, (c) ATLAS Detector software
7 // @author Danilo E. Ferreira de Lima <dferreir@cern.ch>
9 
11 
13 #include "xAODJet/Jet.h"
14 
15 #include "GaudiKernel/ThreadLocalContext.h"
18 
19 #include <string>
20 #include <vector>
21 
22 // Constructor
24  const std::string& t,
25  const std::string& n,
26  const IInterface* p)
27  : base_class(t, n, p)
28  , m_ntotTopo(0)
29  , m_npassTopo(0)
30  , m_selectionString("")
31 {
32  declareInterface<DerivationFramework::IThinningTool>(this);
33  declareProperty("SelectionString", m_selectionString);
34 }
35 
36 // Destructor
38 
39 // Athena initialize and finalize
42 {
43  // Decide which collections need to be checked for ID TrackParticles
44  ATH_MSG_VERBOSE("initialize() ...");
45  ATH_CHECK(m_TopoClSGKey.initialize(m_streamName));
46  ATH_MSG_INFO("Using " << m_TopoClSGKey.key()
47  << "as the source collection for topo calo clusters");
48  if (m_sgKey.empty()) {
49  ATH_MSG_FATAL("No jet collection provided for thinning.");
50  return StatusCode::FAILURE;
51  } else {
53  "Calo clusters associated with objects in "
54  << m_sgKey.key()
55  << " will be retained in this format with the rest being thinned away");
56  ATH_CHECK(m_sgKey.initialize());
57  }
58 
59  for(unsigned int i=0; i < m_addClusterSGKey.size(); i++){
60  m_tmpAddClusterKey = m_addClusterSGKey[i];
61  ATH_CHECK(m_tmpAddClusterKey.initialize(m_streamName));
62  m_addClusterKeys.push_back(m_tmpAddClusterKey);
63  }
64 
65  // Set up the text-parsing machinery for selectiong the photon directly
66  // according to user cuts
67  if (!m_selectionString.empty()) {
68  ATH_CHECK(initializeParser(m_selectionString));
69  }
70 
71  return StatusCode::SUCCESS;
72 }
73 
76 {
77  ATH_MSG_VERBOSE("finalize() ...");
78  ATH_MSG_INFO("Processed " << m_ntotTopo << " topo clusters, of which "
79  << m_npassTopo << " were retained ");
80  ATH_CHECK(finalizeParser());
81  return StatusCode::SUCCESS;
82 }
83 
84 // The thinning itself
87 {
88  const EventContext& ctx = Gaudi::Hive::currentContext();
89 
90  // Retrieve CalCaloTopo collection if required
91  SG::ThinningHandle<xAOD::CaloClusterContainer> importedTopoCaloCluster(
92  m_TopoClSGKey, ctx);
93 
94  // Check the event contains tracks
95  unsigned int nTopoClusters = importedTopoCaloCluster->size();
96  if (nTopoClusters == 0)
97  return StatusCode::SUCCESS;
98 
99  // Set up a mask with the same entries as the full CaloCalTopoClusters collection(s)
100  std::vector<bool> topomask;
101  topomask.assign(nTopoClusters, false);
102  m_ntotTopo += nTopoClusters;
103 
104  // Retrieve jet container
105  const xAOD::JetContainer* importedJets(nullptr);
106  SG::ReadHandle<xAOD::JetContainer> importedJetsHandle{ m_sgKey, ctx };
107  importedJets = importedJetsHandle.ptr();
108  if (importedJets == nullptr) {
109  ATH_MSG_ERROR("No jet collection with name " << m_sgKey.key()
110  << " found in StoreGate!");
111  return StatusCode::FAILURE;
112  }
113  unsigned int nJets(importedJets->size());
114  if (nJets == 0)
115  return StatusCode::SUCCESS;
116  std::vector<const xAOD::Jet*> jetToCheck;
117  jetToCheck.clear();
118 
119  // Execute the text parsers if requested
120  if (!m_selectionString.empty()) {
121  std::vector<int> entries = m_parser->evaluateAsVector();
122  unsigned int nEntries = entries.size();
123  // check the sizes are compatible
124  if (nJets != nEntries) {
125  ATH_MSG_ERROR("Sizes incompatible! Are you sure your selection string "
126  "used jets objects??");
127  return StatusCode::FAILURE;
128  } else {
129  // identify which e-gammas to keep for the thinning check
130  for (unsigned int i = 0; i < nJets; ++i)
131  if (entries[i] == 1)
132  jetToCheck.push_back((*importedJets)[i]);
133  }
134 
135  if(jetToCheck.empty())
136  return StatusCode::SUCCESS;
137 
138  for( const xAOD::Jet* jet : jetToCheck){
139  const auto& links = jet->constituentLinks();
140  for( const auto& link : links ) {
141  // Check that the link is valid:
142  if( ! link.isValid() ) {
143  continue;
144  }
145  topomask.at( link.index() ) = true;
146  }
147  }
148  }
149  else{
150  for( const xAOD::Jet* jet : *importedJets){
151  const auto& links = jet->constituentLinks();
152  for( const auto& link : links ) {
153  // Check that the link is valid:
154  if( ! link.isValid() ) {
155  continue;
156  }
157  topomask.at( link.index() ) = true;
158  }
159  }
160  }
161 
162  // Count up the mask contents
163  for (unsigned int i = 0; i < nTopoClusters; ++i) {
164  if (topomask[i])
165  ++m_npassTopo;
166  }
167 
168  // Execute the thinning service based on the mask. Finish.
169  importedTopoCaloCluster.keep(topomask);
170 
171  for(const auto & addClusterKey : m_addClusterKeys){
172  SG::ThinningHandle<xAOD::CaloClusterContainer> tempClusters(addClusterKey);
173  tempClusters.keep(topomask);
174  }
175 
176  return StatusCode::SUCCESS;
177 }
Jet.h
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
DerivationFramework::JetCaloClusterThinning::initialize
virtual StatusCode initialize() override
Definition: JetCaloClusterThinning.cxx:41
ThinningHandle.h
Handle for requesting thinning for a data object.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
JetCaloClusterThinning.h
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
JetConstituentVector.h
This file defines helper classes to deal with jet constituents.
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
SG::ThinningHandleBase::keep
void keep(size_t ndx)
Mark that index ndx in the container should be kept (not thinned away).
Definition: ThinningHandleBase.cxx:68
DerivationFramework::JetCaloClusterThinning::JetCaloClusterThinning
JetCaloClusterThinning(const std::string &t, const std::string &n, const IInterface *p)
Definition: JetCaloClusterThinning.cxx:23
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
DerivationFramework::JetCaloClusterThinning::m_selectionString
std::string m_selectionString
Definition: JetCaloClusterThinning.h:57
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
CaloCluster.h
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
DMTest::links
links
Definition: CLinks_v1.cxx:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
DerivationFramework::JetCaloClusterThinning::finalize
virtual StatusCode finalize() override
Definition: JetCaloClusterThinning.cxx:75
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::JetCaloClusterThinning::~JetCaloClusterThinning
virtual ~JetCaloClusterThinning()
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DerivationFramework::JetCaloClusterThinning::doThinning
virtual StatusCode doThinning() const override
Definition: JetCaloClusterThinning.cxx:86