ATLAS Offline Software
Thin_vtxTrk.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // Thin_vtxTrk.cxx
8 // Author: James Catmore (James.Catmore@cern.ch)
9 // This is a trivial example of an implementation of a thinning tool
10 // which removes all ID tracks which do not pass a user-defined cut
11 
13 
16 #include <vector>
17 #include <string>
18 // Constructor
19 DerivationFramework::Thin_vtxTrk::Thin_vtxTrk(const std::string& t, const std::string& n, const IInterface* p ) :
20  AthAlgTool(t,n,p),
21  m_ntot(0),
22  m_npass(0),
23  m_acceptanceR(-1.), // Do not add tracks within a cone from the vertex by default
24  m_nVtxTot(0),
25  m_nVtxPass(0),
26  m_noFlags(false)
27 {
28  declareInterface<DerivationFramework::IThinningTool>(this);
29 
30  declareProperty("TrackParticleContainerName", m_trackParticleContainerName = "InDetTrackParticles");
31  declareProperty("VertexContainerNames" , m_vertexContainerName);
32  declareProperty("PassFlags" , m_passFlags);
33  declareProperty("AcceptanceRadius" , m_acceptanceR);
34  declareProperty("IgnoreFlags" , m_noFlags);
35  declareProperty("ThinTracks" , m_thinTracks = true);
36 }
37 
38 // Destructor
40 
41 // Athena initialize and finalize
43 {
44  // Decide which collections need to be checked for ID TrackParticles
45  ATH_MSG_VERBOSE("initialize() ...");
46  ATH_CHECK(m_trackParticleContainerName.initialize(m_streamName));
47 
48 
49  if( m_noFlags){
50  ATH_MSG_INFO("IgnoreFlags is set, all vertices in the container will be kept");
51  }
52 
53  if( ! m_noFlags){
54  if (m_passFlags.empty()) {
55  ATH_MSG_FATAL("No pass flags provided for thinning.");
56  return StatusCode::FAILURE;
57  } else {
58  for(auto itr = m_passFlags.begin(); itr!=m_passFlags.end(); ++itr) {
59  ATH_MSG_INFO("Vertices must pass the \"" << *itr << "\" selection");
60  }
61  }
62  }
63 
64  if (m_acceptanceR > 0.) {
65  ATH_MSG_INFO("Extra tracks must be within cone of "<<m_acceptanceR<<" from vertex candidate.");
66  }
67 
68  for(auto &handle : m_vertexContainerName){
69  ATH_CHECK(handle.initialize(m_streamName));
70  }
71  for(const auto &tracknames : m_vertexContainerName){
72  for(const auto &str : m_passFlags){
73  m_passArray.emplace_back(tracknames.key() + '.' + str);
74  }
75  }
76  ATH_CHECK(m_passArray.initialize());
77  return StatusCode::SUCCESS;
78 }
79 
81 {
82  ATH_MSG_VERBOSE("finalize() ...");
83  ATH_MSG_INFO("Processed "<< m_ntot <<" tracks, "<< m_npass<< " were retained ");
84  ATH_MSG_INFO("Processed "<< m_nVtxTot <<" vertices, "<< m_nVtxPass<< " were retained ");
85 
86  return StatusCode::SUCCESS;
87 }
88 
89 // The thinning itself
91 {
92  const EventContext& ctx = Gaudi::Hive::currentContext();
93  // Retrieve main TrackParticle collection
94  SG::ThinningHandle<xAOD::TrackParticleContainer> importedTrackParticles(m_trackParticleContainerName, ctx);
95 
96  // Check the event contains tracks
97  unsigned int nTracks = importedTrackParticles->size();
98  if (nTracks==0) return StatusCode::SUCCESS;
99 
100  // Set up a trackMask with the same entries as the full TrackParticle collection
101  std::vector<bool> trackMask(nTracks,false); // default: don't keep any tracks
102  m_ntot += nTracks;
103  int nVtxTot =0;
104  int nVtxPass=0;
105 
106  std::unordered_map<std::string, SG::ReadDecorHandle<xAOD::VertexContainer, Char_t>> handles;
107  handles.reserve(m_passArray.size());
108  for(const auto &key : m_passArray){
109  auto it = handles.emplace(std::piecewise_construct, std::forward_as_tuple(key.key()), std::forward_as_tuple(key, ctx));
110  if(!(*it.first).second.isPresent()) return StatusCode::FAILURE;
111  }
112 
113  // retieve vertex
114  for(const auto& name : m_vertexContainerName){
115  SG::ThinningHandle<xAOD::VertexContainer> vertexContainer(name, ctx);
116  std::vector<bool> vtxMask(vertexContainer->size(), false); // default: don't keep any vertices
117 
118  // loop over vertices
119  int k = 0;
120  for(auto vtxItr = vertexContainer->begin(); vtxItr!=vertexContainer->end(); ++vtxItr, ++k) {
121  const xAOD::Vertex* vtx = *vtxItr;
122  nVtxTot++;
123 
124  // check if the vertex passed the required selections criteria
125  bool passed = false;
126  for(std::vector<std::string>::const_iterator flagItr = m_passFlags.begin(); flagItr!=m_passFlags.end(); ++flagItr) {
127  std::string lookupstr = name.key() + '.' + (*flagItr);
128  const auto& handle = handles.at(lookupstr);
129  if(handle(*vtx) != 0) {
130  passed = true;
131  break;
132  }
133  } // end of loop over flags
134 
135  if(passed || m_noFlags) {
136  // vertex passed the selection
137  vtxMask[k] = true;
138  nVtxPass++;
139 
140  // Add tracks according to DR selection
141  if(m_acceptanceR > 0.){
142 
143  // determine the sum of the tracks at vertex as centre for the cone
144  TLorentzVector centreCandidate;
145  for(uint j=0; j<vtx->nTrackParticles(); ++j) {
146  centreCandidate += vtx->trackParticle(j)->p4();
147  }
148 
149  for(uint i=0; i<nTracks; ++i) {
150  if(!trackMask[i]) { // do this only for tracks that haven't been selected, yet
151  const xAOD::TrackParticle* track = (*importedTrackParticles)[i];
152  if(centreCandidate.DeltaR(track->p4()) < m_acceptanceR) trackMask[i]= true;
153  }
154  }
155  }// end adding tracks according to DR selection
156 
157  if(m_thinTracks) {
158  // loop over all tracks
159  for(uint i=0; i<nTracks; ++i) {
160  if(!trackMask[i]) { // do this only for tracks that haven't been selected, yet
161  const xAOD::TrackParticle* track = (*importedTrackParticles)[i];
162  // loop over tracks at vertex
163  for(uint j=0; j<vtx->nTrackParticles(); ++j) {
164  if(vtx->trackParticle(j) == track) {
165  trackMask[i] = true; // accept track
166  }
167  } // end of loop over tracks at vertex
168  }
169  } // end of loop over all tracks
170  }
171  }
172  } // end of loop over vertices
173 
174  // Execute the thinning service based on the vtxMask.
175  vertexContainer.keep(vtxMask);
176  }
177 
178  // Count up the trackMask contents
179  m_npass += std::accumulate(trackMask.begin(), trackMask.end(), 0);
180  m_nVtxTot += nVtxTot;
181  m_nVtxPass+= nVtxPass;
182  if(m_thinTracks || m_acceptanceR > 0.) {
183  // Execute the thinning service based on the trackMask. Finish.
184  importedTrackParticles.keep(trackMask);
185  }
186 
187  return StatusCode::SUCCESS;
188 }
189 
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
xAOD::Vertex_v1::nTrackParticles
size_t nTrackParticles() const
Get the number of tracks associated with this vertex.
Definition: Vertex_v1.cxx:270
TrigCompositeUtils::passed
bool passed(DecisionID id, const DecisionIDContainer &idSet)
checks if required decision ID is in the set of IDs in the container
Definition: TrigCompositeUtilsRoot.cxx:117
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ThinningHandle.h
Handle for requesting thinning for a data object.
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
skel.it
it
Definition: skel.GENtoEVGEN.py:396
DerivationFramework::Thin_vtxTrk::~Thin_vtxTrk
~Thin_vtxTrk()
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Thin_vtxTrk.h
DerivationFramework::Thin_vtxTrk::m_trackParticleContainerName
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_trackParticleContainerName
Definition: Thin_vtxTrk.h:43
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
xAOD::TrackParticle_v1::p4
virtual FourMom_t p4() const override final
The full 4-momentum of the particle.
Definition: TrackParticle_v1.cxx:129
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
DerivationFramework::Thin_vtxTrk::m_passFlags
std::vector< std::string > m_passFlags
Definition: Thin_vtxTrk.h:45
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
BPhysHypoHelper.h
: B-physcis xAOD helpers.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::Vertex_v1::trackParticle
const TrackParticle * trackParticle(size_t i) const
Get the pointer to a given track that was used in vertex reco.
Definition: Vertex_v1.cxx:249
DerivationFramework::Thin_vtxTrk::m_vertexContainerName
SG::ThinningHandleKeyArray< xAOD::VertexContainer > m_vertexContainerName
Definition: Thin_vtxTrk.h:44
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
DerivationFramework::Thin_vtxTrk::initialize
StatusCode initialize()
Definition: Thin_vtxTrk.cxx:42
DerivationFramework::Thin_vtxTrk::Thin_vtxTrk
Thin_vtxTrk(const std::string &t, const std::string &n, const IInterface *p)
Definition: Thin_vtxTrk.cxx:19
DerivationFramework::Thin_vtxTrk::m_thinTracks
bool m_thinTracks
Definition: Thin_vtxTrk.h:47
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
str
Definition: BTagTrackIpAccessor.cxx:11
DerivationFramework::Thin_vtxTrk::doThinning
virtual StatusCode doThinning() const
Pass the thinning service
Definition: Thin_vtxTrk.cxx:90
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
DerivationFramework::Thin_vtxTrk::finalize
StatusCode finalize()
Definition: Thin_vtxTrk.cxx:80
AthAlgTool
Definition: AthAlgTool.h:26
fitman.k
k
Definition: fitman.py:528
DerivationFramework::Thin_vtxTrk::m_noFlags
bool m_noFlags
Definition: Thin_vtxTrk.h:48
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
DerivationFramework::Thin_vtxTrk::m_acceptanceR
double m_acceptanceR
Definition: Thin_vtxTrk.h:40