ATLAS Offline Software
Thin_vtxDuplicates.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_vtxDuplicates.cxx
8 // Matteo Bedognetti (matteo.bedognetti@cern.ch)
9 //Based on Thin_vtxTrk.cxx, by
10 
11 
12 
16 #include <vector>
17 #include <string>
18 #include <algorithm> // for the sort function
19 #include <iomanip>
21 // Constructor
22 DerivationFramework::Thin_vtxDuplicates::Thin_vtxDuplicates(const std::string& t, const std::string& n, const IInterface* p ) :
23  AthAlgTool(t,n,p),
24  // m_acceptanceR(-1.),
25  m_noFlags(false),
26  m_nVtxTot(0),
27  m_nVtxPass(0)
28 {
29  declareInterface<DerivationFramework::IThinningTool>(this);
30 
31  declareProperty("VertexContainerName" , m_vertexContainerNames);
32  declareProperty("PassFlags" , m_passFlags);
33  //declareProperty("AcceptanceRadius" , m_acceptanceR);
34  declareProperty("IgnoreFlags" , m_noFlags);
35  //declareProperty("ApplyAndForTracks" , m_trackAnd = false);
36  //declareProperty("ThinTracks" , m_thinTracks = true);
37 }
38 
39 // Destructor
41 
42 // Athena initialize and finalize
44 {
45  // Decide which collections need to be checked for ID TrackParticles
46  ATH_MSG_VERBOSE("initialize() ...");
47  ATH_CHECK(m_vertexContainerNames.initialize(m_streamName));
48 
49 
50  if (m_passFlags.empty()) {
51  ATH_MSG_FATAL("No pass flags provided for thinning.");
52  return StatusCode::FAILURE;
53  } else {
54  for(auto itr = m_passFlags.cbegin(); itr!=m_passFlags.cend(); ++itr) {
55  ATH_MSG_INFO("Vertices must pass the \"" << itr->key() << "\" selection");
56  }
57  }
58 
59  for(auto &key : m_passFlags){
60  key = m_vertexContainerNames.key() + '.' + key.key();
61  }
62  ATH_CHECK(m_passFlags.initialize());
63  return StatusCode::SUCCESS;
64 }
65 
67 {
68  ATH_MSG_VERBOSE("finalize() ...");
69  ATH_MSG_INFO("Processed "<< m_nVtxTot <<" vertices, "<< m_nVtxPass<< " were retained ");
70 
71  return StatusCode::SUCCESS;
72 }
73 
74 // The thinning itself
76 {
77  // retieve vertex
78  const EventContext& ctx = Gaudi::Hive::currentContext();
79  SG::ThinningHandle< xAOD::VertexContainer > vertexContainer(m_vertexContainerNames, ctx);
80  std::vector<bool> vtxMask(vertexContainer->size(), true); // default: keep all vertices
81  int vtxTot = 0;
82  int nVtxPass = 0;
83  // loop over vertices
84  int k = 0;
85  std::vector<SG::ReadDecorHandle<xAOD::VertexContainer, Char_t>> handles;
86  handles.reserve(m_passFlags.size());
87  for(const auto &key : m_passFlags){
88  handles.emplace_back(key, ctx);
89  if(!handles.back().isPresent()) return StatusCode::FAILURE;
90  }
91  for(auto vtxItr = vertexContainer->cbegin(); vtxItr!=vertexContainer->cend(); ++vtxItr, ++k) {
92  const xAOD::Vertex* vtx = *vtxItr;
93  // check if the vertex passed the required selections criteria (is run when the vertex is already excluded, because the counter needs the info)
94  bool passed = false;
95  if(m_noFlags){passed = true; vtxTot++; }
96  else{
97  for(auto &flagAcc : handles) {
98  if(flagAcc(*vtx) != 0) {
99  passed = true;
100  vtxTot++;//Have to count the ones which are accepted to start with
101  break;
102  }
103  } // end of loop over flags
104  }
105 
106  // Skip if it has already been identified as duplicate
107  if(vtxMask[k] == false)continue; //After the flag-check to have the total-passed work correctly
108 
109  if(!passed)vtxMask[k]= false;
110 
111  if(passed) {
112  // vertex passed the selection
113  nVtxPass++;
114 
115  // determine the sum of the tracks at vertex as centre for the cone
116  std::vector<const xAOD::TrackParticle*> presentVertex, compareVertex;
117 
118  //Fill in the present vertex, for later comparison against other vertices
119  presentVertex.clear();
120  for(uint j=0; j<vtx->nTrackParticles(); ++j) {
121  presentVertex.push_back(vtx->trackParticle(j));
122  }
123  sort( presentVertex.begin(), presentVertex.end() ); //Sort the trackparticles BY POINTER ADDRESS
124 
125  //Loop over the remaining vertices and remove them if needed
126  int loop_k = k+1;
127  for(auto vtxLoopItr = vtxItr+1; vtxLoopItr!=vertexContainer->cend(); vtxLoopItr++, loop_k++){
128 
129  const xAOD::Vertex* loop_vtx = *vtxLoopItr;
130 
131  //Vertices are distinct if have different size
132  if(vtx->nTrackParticles() != loop_vtx->nTrackParticles())continue;
133 
134  //If the vertex is still active load and compare
135  if(vtxMask[loop_k]){
136 
137  compareVertex.clear();
138  for(uint j=0; j<loop_vtx->nTrackParticles(); ++j) {
139  compareVertex.push_back(loop_vtx->trackParticle(j));
140  }
141 
142  std::sort( compareVertex.begin(), compareVertex.end());
143 
144  vtxMask[loop_k] = false;
145 
146  ATH_MSG_DEBUG("Compared tracks: ");
147  ATH_MSG_DEBUG(std::setw(14)<<compareVertex[0]<<std::setw(14) << compareVertex[1]<<std::setw(14)<<compareVertex[2]);
148  ATH_MSG_DEBUG(std::setw(14)<<presentVertex[0]<<std::setw(14) << presentVertex[1]<<std::setw(14)<<presentVertex[2]);
149 
150  for(uint j=0; j<loop_vtx->nTrackParticles(); ++j) {
151  if( compareVertex[j] != presentVertex[j] ){vtxMask[loop_k] = true; break;}
152  }
153  ATH_MSG_DEBUG("Verdict:"<<(vtxMask[loop_k]? "keep": "erase") );
154  }
155 
156  } // Endo of extra loop over remaining vertices
157 
158  } // if( passed )
159  } // end of loop over vertices
160 
161  // Execute the thinning service based on the vtxMask.
162  vertexContainer.keep(vtxMask);
163 
164  m_nVtxTot.fetch_add( vtxTot, std::memory_order_relaxed);
165  m_nVtxPass.fetch_add( nVtxPass, std::memory_order_relaxed);
166 
167 
168 
169  return StatusCode::SUCCESS;
170 }
171 
DerivationFramework::Thin_vtxDuplicates::Thin_vtxDuplicates
Thin_vtxDuplicates(const std::string &t, const std::string &n, const IInterface *p)
Definition: Thin_vtxDuplicates.cxx:22
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
DerivationFramework::Thin_vtxDuplicates::m_passFlags
SG::ReadDecorHandleKeyArray< xAOD::VertexContainer > m_passFlags
Definition: Thin_vtxDuplicates.h:32
DerivationFramework::Thin_vtxDuplicates::initialize
virtual StatusCode initialize()
Definition: Thin_vtxDuplicates.cxx:43
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
SG::ThinningHandle
Handle for requesting thinning for a data object.
Definition: ThinningHandle.h:84
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
Thin_vtxDuplicates.h
DerivationFramework::Thin_vtxDuplicates::m_vertexContainerNames
SG::ThinningHandleKey< xAOD::VertexContainer > m_vertexContainerNames
Definition: Thin_vtxDuplicates.h:31
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
BPhysHypoHelper.h
: B-physcis xAOD helpers.
DerivationFramework::Thin_vtxDuplicates::finalize
virtual StatusCode finalize()
Definition: Thin_vtxDuplicates.cxx:66
DerivationFramework::Thin_vtxDuplicates::~Thin_vtxDuplicates
~Thin_vtxDuplicates()
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_vtxDuplicates::doThinning
virtual StatusCode doThinning() const
Pass the thinning service
Definition: Thin_vtxDuplicates.cxx:75
DerivationFramework::Thin_vtxDuplicates::m_noFlags
bool m_noFlags
Definition: Thin_vtxDuplicates.h:27
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
AthAlgTool
Definition: AthAlgTool.h:26
TrackParticleContainer.h
fitman.k
k
Definition: fitman.py:528
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37