ATLAS Offline Software
BPhysPVThinningTool.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 // ThinningToolExample.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 
16 #include "xAODBPhys/BPhysHelper.h"
18 #include <algorithm>
19 #include <numeric>
20 #include <vector>
21 #include <string>
22 
23 using namespace std;
24 using namespace xAOD;
25 // Constructor
27  const std::string& n,
28  const IInterface* p ) :
29  AthAlgTool(t,n,p),
30  m_TrackContainerName("InDetTrackParticles"),
31  m_PVContainerName("PrimaryVertices"),
32  m_ntot(0),
33  m_npass(0), m_tracks_kept(0), m_keepTracks(false)
34 {
35  declareInterface<DerivationFramework::IThinningTool>(this);
36  declareProperty("CandidateCollections" , m_BPhyCandList);
37  declareProperty("KeepPVTracks", m_keepTracks);
38  declareProperty("TrackParticleContainerName", m_TrackContainerName);
39  declareProperty("PrimaryVertexContainerName", m_PVContainerName);
40 }
41 
42 // Destructor
44 }
45 
46 // Athena initialize and finalize
48 {
49  ATH_MSG_VERBOSE("initialize() ...");
50  ATH_CHECK(m_BPhyCandList.initialize());
51  if(not m_TrackContainerName.key().empty()) ATH_CHECK(m_TrackContainerName.initialize(m_streamName));
52  ATH_CHECK(m_PVContainerName.initialize(m_streamName));
53 
54  return StatusCode::SUCCESS;
55 }
57 {
58  ATH_MSG_VERBOSE("finalize() ...");
59  ATH_MSG_INFO("Processed "<< m_ntot <<" PV, "<< m_npass<< " were retained ");
60  if(m_keepTracks) ATH_MSG_INFO("Additional tracks kept " << m_tracks_kept);
61  return StatusCode::SUCCESS;
62 }
63 
64 // The thinning itself
66 {
67  const EventContext& ctx = Gaudi::Hive::currentContext();
68  // Get the track container
69  SG::ThinningHandle<xAOD::VertexContainer> PV_col(m_PVContainerName, ctx);
70  if(!PV_col.isValid()) {
71  ATH_MSG_ERROR ("Couldn't retrieve VertexContainer with key PrimaryVertices");
72  return StatusCode::FAILURE;
73  }
74  m_ntot+=PV_col->size();
75  // Loop over tracks, see if they pass, set mask
76  std::vector<bool> mask(PV_col->size(), false);
77 
78  BPhysHelper::pv_type pvtypes[] = {BPhysHelper::PV_MAX_SUM_PT2,
79  BPhysHelper::PV_MIN_A0,
80  BPhysHelper::PV_MIN_Z0,
81  BPhysHelper::PV_MIN_Z0_BA};
82 
83 
84 
85  for(auto &str : m_BPhyCandList) {
87  ATH_CHECK(Container.isValid());
88  size_t s = Container->size();
89  for(size_t i = 0; i<s; i++) {
90  xAOD::BPhysHelper vtx(Container->at(i));
91 
92  for(size_t i =0; i < 4; i++) {
93  const xAOD::Vertex* origPv = vtx.origPv(pvtypes[i]);
94  if(origPv==nullptr) continue;
95  auto pvit = std::find (PV_col->begin(), PV_col->end(), origPv);
96  if(pvit == PV_col->end()) {
97  ATH_MSG_WARNING("PV not found in container");
98  continue;
99  }
100  size_t x = std::distance(PV_col->begin(), pvit);
101  mask.at(x) = true;
102  }
103 
104  }
105  }
106 
107  m_npass += std::accumulate(mask.begin(), mask.end(), 0);
108 
109  if(m_keepTracks){
110  SG::ThinningHandle<xAOD::TrackParticleContainer> importedTrackParticles(m_TrackContainerName, ctx);
111  std::vector<bool> trackmask(importedTrackParticles->size(), false);
112  size_t pvnum = mask.size();
113  for(size_t i =0; i<pvnum;i++){
114  if(mask[i] == false) continue;
115  auto vtx = PV_col->at(i);
116  size_t s = vtx->nTrackParticles();
117  for(size_t j=0;j<s;j++){
118  auto trackit = std::find(importedTrackParticles->begin(), importedTrackParticles->end(), vtx->trackParticle(j));
119  if(trackit == importedTrackParticles->end()){
120  ATH_MSG_WARNING("track not found in container");
121  continue;
122  }
123  size_t x = std::distance(importedTrackParticles->begin(), trackit);
124  trackmask.at(x) = true;
125  }
126  }
127  importedTrackParticles.keep(trackmask);
128  m_tracks_kept += std::accumulate(trackmask.begin(), trackmask.end(), 0);
129  }
130  PV_col.keep(mask);
131 
132  return StatusCode::SUCCESS;
133 }
134 
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
DerivationFramework::BPhysPVThinningTool::m_keepTracks
bool m_keepTracks
Definition: BPhysPVThinningTool.h:47
xAOD::BPhysHelper
Definition: BPhysHelper.h:71
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
DerivationFramework::BPhysPVThinningTool::~BPhysPVThinningTool
~BPhysPVThinningTool()
Destructor.
Definition: BPhysPVThinningTool.cxx:43
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
ThinningHandle.h
Handle for requesting thinning for a data object.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
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
DerivationFramework::BPhysPVThinningTool::finalize
virtual StatusCode finalize() override
Definition: BPhysPVThinningTool.cxx:56
xAOD
ICaloAffectedTool is abstract interface for tools checking if 4 mom is in calo affected region.
Definition: ICaloAffectedTool.h:24
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
DerivationFramework::BPhysPVThinningTool::m_PVContainerName
SG::ThinningHandleKey< xAOD::VertexContainer > m_PVContainerName
Definition: BPhysPVThinningTool.h:43
x
#define x
python.utils.AtlRunQueryLookup.mask
string mask
Definition: AtlRunQueryLookup.py:460
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
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
xAOD::BPhysHelper::pv_type
pv_type
: Enum type of the PV
Definition: BPhysHelper.h:475
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
DerivationFramework::BPhysPVThinningTool::doThinning
virtual StatusCode doThinning() const override
Check that the current event passes this filter.
Definition: BPhysPVThinningTool.cxx:65
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
xAOD::BPhysHelper::origPv
const xAOD::Vertex * origPv(const pv_type vertexType=BPhysHelper::PV_MIN_A0)
original PV
Definition: BPhysHelper.cxx:807
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
BPhysHelper.h
: B-physics xAOD helpers.
TrackParticle.h
BPhysPVThinningTool.h
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DerivationFramework::BPhysPVThinningTool::m_BPhyCandList
SG::ReadHandleKeyArray< xAOD::VertexContainer > m_BPhyCandList
Definition: BPhysPVThinningTool.h:41
DerivationFramework::BPhysPVThinningTool::initialize
virtual StatusCode initialize() override
Definition: BPhysPVThinningTool.cxx:47
str
Definition: BTagTrackIpAccessor.cxx:11
DerivationFramework::BPhysPVThinningTool::m_TrackContainerName
SG::ThinningHandleKey< xAOD::TrackParticleContainer > m_TrackContainerName
Definition: BPhysPVThinningTool.h:42
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
AthAlgTool
Definition: AthAlgTool.h:26
DerivationFramework::BPhysPVThinningTool::BPhysPVThinningTool
BPhysPVThinningTool(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
Definition: BPhysPVThinningTool.cxx:26
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrackParticleContainer.h