ATLAS Offline Software
Loading...
Searching...
No Matches
ReVertex.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2018 CERN for the benefit of the ATLAS collaboration
3*/
4// ****************************************************************************
5// ----------------------------------------------------------------------------
6// ReVertex
7//
8// Konstantin Beloborodov <Konstantin.Beloborodov@cern.ch>
9//
10// ----------------------------------------------------------------------------
11// ****************************************************************************
12#include "ReVertex.h"
17
19#include "BPhysPVTools.h"
24
25using namespace DerivationFramework;
26
27ReVertex::ReVertex(const std::string& t,
28 const std::string& n,
29 const IInterface* p) :
30 base_class(t,n,p), m_vertexEstimator("InDet::VertexPointEstimator"), m_iVertexFitter("Trk::TrkVKalVrtFitter"),
31 m_massConst(0.),
33 m_v0Tools("Trk::V0Tools"),
34 m_pvRefitter("Analysis::PrimaryVertexRefitter", this),
35 m_doMassConst(false),
37 m_chi2cut(-1.0),
38 m_trkDeltaZ(-1.0),
40{
41
42 declareProperty("TrackIndices", m_TrackIndices);
43 declareProperty("TrkVertexFitterTool", m_iVertexFitter);
44 declareProperty("VertexPointEstimator",m_vertexEstimator);
45
46 declareProperty("OutputVtxContainerName", m_OutputContainerName);
47 declareProperty("InputVtxContainerName", m_inputContainerName);
48 declareProperty("TrackContainerName", m_trackContainer = "InDetTrackParticles");
49 declareProperty("UseVertexFittingWithPV", m_vertexFittingWithPV);
50
51 declareProperty("HypothesisNames",m_hypoNames);
52
53 declareProperty("V0Tools" , m_v0Tools);
54 declareProperty("PVRefitter" , m_pvRefitter);
55 declareProperty("DefaultPVContainerName", m_defaultPVContainerName = "PrimaryVertices");
56 declareProperty("PVContainerName" , m_pvContainerName = "PrimaryVertices");
57 declareProperty("RefPVContainerName" , m_refPVContainerName = "RefittedPrimaryVertices");
58
59 declareProperty("UseMassConstraint", m_doMassConst);
60 declareProperty("VertexMass", m_totalMassConst);
61 declareProperty("SubVertexMass", m_massConst);
62 declareProperty("MassInputParticles", m_trkMasses);
63 declareProperty("SubVertexTrackIndices", m_indices);
64
65 declareProperty("UseAdditionalTrack", m_useAdditionalTrack);
66
67 declareProperty("RefitPV" , m_refitPV = false);
68 //This parameter will allow us to optimize the number of PVs under consideration as the probability
69 //of a useful primary vertex drops significantly the higher you go
70 declareProperty("MaxPVrefit" , m_PV_max = 1000);
71 declareProperty("DoVertexType" , m_DoVertexType = 7);
72 // minimum number of tracks for PV to be considered for PV association
73 declareProperty("MinNTracksInPV" , m_PV_minNTracks = 0);
74 declareProperty("Do3d" , m_do3d = false);
75 declareProperty("AddPVData" , m_AddPVData = true);
76 declareProperty("StartingPoint0" , m_startingpoint0 = false);
77 declareProperty("BMassUpper",m_BMassUpper = std::numeric_limits<double>::max() );
78 declareProperty("BMassLower",m_BMassLower = std::numeric_limits<double>::min() );
79 declareProperty("Chi2Cut",m_chi2cut = std::numeric_limits<double>::max() );
80 declareProperty("TrkDeltaZ",m_trkDeltaZ);
81
82
83}
84
86 ATH_MSG_DEBUG("in initialize()");
87 if(m_TrackIndices.empty()) {
88 ATH_MSG_FATAL("No Indices provided");
89 return StatusCode::FAILURE;
90 }
91 ATH_CHECK(m_iVertexFitter.retrieve());
92 ATH_CHECK(m_v0Tools.retrieve());
93 ATH_CHECK(m_pvRefitter.retrieve());
94 ATH_CHECK(m_vertexEstimator.retrieve());
95 m_VKVFitter = dynamic_cast<Trk::TrkVKalVrtFitter*>(&(*m_iVertexFitter));
96 if(m_VKVFitter==nullptr) return StatusCode::FAILURE;
97 ATH_CHECK(m_OutputContainerName.initialize());
98 ATH_CHECK(m_inputContainerName.initialize());
99 ATH_CHECK(m_trackContainer.initialize());
101 ATH_CHECK(m_pvContainerName.initialize());
102 ATH_CHECK(m_refPVContainerName.initialize());
103 ATH_CHECK(m_eventInfo_key.initialize());
104 ATH_CHECK(m_RelinkContainers.initialize());
105 ATH_CHECK(m_CollectionsToCheck.initialize());
106 return StatusCode::SUCCESS;
107}
108
109
110StatusCode ReVertex::addBranches(const EventContext& ctx) const {
112 ATH_CHECK(vtxContainer.record(std::make_unique<xAOD::VertexContainer>(), std::make_unique<xAOD::VertexAuxContainer>()));
113
114 const size_t Ntracks = m_TrackIndices.size();
115
118 ATH_CHECK(InVtxContainer.isValid());
119 ATH_CHECK(importedTrackCollection.isValid());
120 //----------------------------------------------------
121 // retrieve primary vertices
122 //----------------------------------------------------
124 ATH_CHECK(defaultPVContainer.isValid());
125
127 ATH_CHECK(pvContainer.isValid());
128
129 std::vector<const xAOD::TrackParticle*> fitpair(Ntracks + m_useAdditionalTrack);
130 for(const xAOD::Vertex* v : *InVtxContainer)
131 {
132
133 bool passed = false;
134 for(size_t i=0;i<m_hypoNames.size();i++) {
135 xAOD::BPhysHypoHelper onia(m_hypoNames.at(i), v);
136 passed |= onia.pass();
137 }
138 if (!passed && m_hypoNames.size()) continue;
139
140 for(size_t i =0; i<Ntracks; i++)
141 {
142 size_t trackN = m_TrackIndices[i];
143 if(trackN >= v->nTrackParticles())
144 {
145 ATH_MSG_FATAL("Indices exceeds limit in particle");
146 return StatusCode::FAILURE;
147 }
148 fitpair[i] = v->trackParticle(trackN);
149 }
150
152 {
153 // Loop over ID tracks, call vertexing
154 for (auto trkItr=importedTrackCollection->cbegin(); trkItr!=importedTrackCollection->cend(); ++trkItr) {
155 const xAOD::TrackParticle* tp (*trkItr);
156 fitpair.back() = nullptr;
157 if (Analysis::JpsiUpsilonCommon::isContainedIn(tp,fitpair)) continue; // remove tracks which were used to build J/psi+2Tracks
158 fitpair.back() = tp;
159
160 // Daniel Scheirich: remove track too far from the Jpsi+2Tracks vertex (DeltaZ cut)
161 if(m_trkDeltaZ>0 &&
162 std::abs((tp)->z0() + (tp)->vz() - v->z()) > m_trkDeltaZ )
163 continue;
164
165 fitAndStore(vtxContainer.ptr(),v,InVtxContainer.cptr(),fitpair,importedTrackCollection.cptr(),pvContainer.cptr());
166 }
167 }
168 else
169 {
170 fitAndStore(vtxContainer.ptr(),v,InVtxContainer.cptr(),fitpair,importedTrackCollection.cptr(),pvContainer.cptr());
171 }
172 }
173
174 if(m_AddPVData){
175 // Give the helper class the ptr to v0tools and beamSpotsSvc to use
177 if(not evt.isValid()) ATH_MSG_ERROR("Cannot Retrieve " << evt.key() );
178 BPhysPVTools helper(&(*m_v0Tools), evt.cptr());
179 helper.SetMinNTracksInPV(m_PV_minNTracks);
180 helper.SetSave3d(m_do3d);
181
182 if(m_refitPV) {
183 //----------------------------------------------------
184 // Try to retrieve refitted primary vertices
185 //----------------------------------------------------
187 ATH_CHECK(refPvContainer.record(std::make_unique<xAOD::VertexContainer>(), std::make_unique<xAOD::VertexAuxContainer>()));
188
189 if(vtxContainer->size() >0){
190 ATH_CHECK(helper.FillCandwithRefittedVertices(vtxContainer.ptr(), pvContainer.cptr(), refPvContainer.ptr(), &(*m_pvRefitter) , m_PV_max, m_DoVertexType));
191 }
192 }else{
193 if(pvContainer->size()==0) {
194 if(vtxContainer->size() >0) ATH_CHECK(helper.FillCandExistingVertices(vtxContainer.ptr(), defaultPVContainer.cptr(), m_DoVertexType));
195 }
196 else {
197 if(vtxContainer->size() >0) ATH_CHECK(helper.FillCandExistingVertices(vtxContainer.ptr(), pvContainer.cptr(), m_DoVertexType));
198 }
199 }
200 }
201
203
204 std::vector<const xAOD::TrackParticleContainer*> trackCols;
205 for(const auto &str : m_RelinkContainers){
207 trackCols.push_back(handle.cptr());
208 }
209 if(not trackCols.empty()){
210 for(xAOD::Vertex* vtx : *vtxContainer){
211 try{
213 }catch(std::runtime_error const& e){
214 ATH_MSG_ERROR(e.what());
215 return StatusCode::FAILURE;
216 }
217 }
218 }
219 return StatusCode::SUCCESS;
220}
221
223 const xAOD::Vertex* v,
224 const xAOD::VertexContainer *InVtxContainer,
225 const std::vector<const xAOD::TrackParticle*> &inputTracks,
226 const xAOD::TrackParticleContainer* importedTrackCollection,
227 const xAOD::VertexContainer* pvContainer) const
228{
229 std::unique_ptr<xAOD::Vertex> ptr(fit(inputTracks, importedTrackCollection, nullptr));
230 if(!ptr)return;
231
232 double chi2DOF = ptr->chiSquared()/ptr->numberDoF();
233 ATH_MSG_DEBUG("Candidate chi2/DOF is " << chi2DOF);
234 bool chi2CutPassed = (m_chi2cut <= 0.0 || chi2DOF < m_chi2cut);
235 if(!chi2CutPassed) { ATH_MSG_DEBUG("Chi Cut failed!"); return; }
236 xAOD::BPhysHelper bHelper(ptr.get());//"get" does not "release" still automatically deleted
237 bHelper.setRefTrks();
238 if (m_trkMasses.size()==inputTracks.size()) {
239 TLorentzVector bMomentum = bHelper.totalP(m_trkMasses);
240 double bMass = bMomentum.M();
241 bool passesCuts = (m_BMassUpper > bMass && bMass > m_BMassLower);
242 if(!passesCuts)return;
243 }
244
245 DerivationFramework::BPhysPVTools::PrepareVertexLinks( ptr.get(), importedTrackCollection );
246 std::vector<const xAOD::Vertex*> thePreceding;
247 thePreceding.push_back(v);
249 //
251 if (!closestRefPV.get()) return;
252 std::unique_ptr<xAOD::Vertex> ptrPV(fit(inputTracks, importedTrackCollection, closestRefPV.get()));
253 if(!ptrPV) return;
254
255 double chi2DOFPV = ptrPV->chiSquared()/ptrPV->numberDoF();
256 ATH_MSG_DEBUG("CandidatePV chi2/DOF is " << chi2DOFPV);
257 bool chi2CutPassed = (m_chi2cut <= 0.0 || chi2DOFPV < m_chi2cut);
258 if(!chi2CutPassed) { ATH_MSG_DEBUG("Chi Cut failed!"); return; }
259 xAOD::BPhysHelper bHelperPV(ptrPV.get());//"get" does not "release" still automatically deleted
260 bHelperPV.setRefTrks();
261 if (m_trkMasses.size()==inputTracks.size()) {
262 TLorentzVector bMomentumPV = bHelperPV.totalP(m_trkMasses);
263 double bMass = bMomentumPV.M();
264 bool passesCuts = (m_BMassUpper > bMass && bMass > m_BMassLower);
265 if(!passesCuts)return;
266 }
267
268 bHelperPV.setPrecedingVertices(thePreceding, InVtxContainer);
269 vtxContainer->push_back(ptrPV.release());
270 return; //Don't store other vertex
271 }
272 bHelper.setPrecedingVertices(thePreceding, InVtxContainer);
273 vtxContainer->push_back(ptr.release());
274}
275
276 // *********************************************************************************
277
278 // ---------------------------------------------------------------------------------
279 // fit - does the fit
280 // ---------------------------------------------------------------------------------
281
282xAOD::Vertex* ReVertex::fit(const std::vector<const xAOD::TrackParticle*> &inputTracks,
283 const xAOD::TrackParticleContainer* importedTrackCollection,
284 const xAOD::Vertex* pv) const
285{
286 std::unique_ptr<Trk::IVKalState> state = m_VKVFitter->makeState();
287 if (m_doMassConst && (m_trkMasses.size()==inputTracks.size())) {
288 m_VKVFitter->setMassInputParticles(m_trkMasses, *state);
289 if (m_totalMassConst) m_VKVFitter->setMassForConstraint(m_totalMassConst, *state);
290 if (m_massConst) m_VKVFitter->setMassForConstraint(m_massConst, m_indices, *state);
291 }
292 if (pv) {
293 m_VKVFitter->setCnstType(8, *state);
294 m_VKVFitter->setVertexForConstraint(pv->position().x(),
295 pv->position().y(),
296 pv->position().z(), *state);
297 m_VKVFitter->setCovVrtForConstraint(pv->covariancePosition()(Trk::x,Trk::x),
298 pv->covariancePosition()(Trk::y,Trk::x),
299 pv->covariancePosition()(Trk::y,Trk::y),
300 pv->covariancePosition()(Trk::z,Trk::x),
301 pv->covariancePosition()(Trk::z,Trk::y),
302 pv->covariancePosition()(Trk::z,Trk::z), *state );
303 }
304
305 // Do the fit itself.......
306 // Starting point (use the J/psi position)
307 const Trk::Perigee& aPerigee1 = inputTracks[0]->perigeeParameters();
308 const Trk::Perigee& aPerigee2 = inputTracks[1]->perigeeParameters();
309 int sflag = 0;
310 int errorcode = 0;
311 Amg::Vector3D startingPoint = m_vertexEstimator->getCirclesIntersectionPoint(&aPerigee1,&aPerigee2,sflag,errorcode);
312 if (errorcode != 0) {startingPoint(0) = 0.0; startingPoint(1) = 0.0; startingPoint(2) = 0.0;}
313 xAOD::Vertex* theResult = m_VKVFitter->fit(inputTracks, startingPoint, *state);
314
315 // Added by ASC
316 if(theResult != 0){
317 std::vector<ElementLink<DataVector<xAOD::TrackParticle> > > newLinkVector;
318 for(unsigned int i=0; i< theResult->trackParticleLinks().size(); i++)
319 {
320 ElementLink<DataVector<xAOD::TrackParticle> > mylink=theResult->trackParticleLinks()[i]; //makes a copy (non-const)
321 mylink.setStorableObject( *importedTrackCollection, true);
322 newLinkVector.push_back( mylink );
323 }
324 theResult->clearTracks();
325 theResult->setTrackParticleLinks( newLinkVector );
326 }
327
328 return theResult;
329}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_DEBUG(x)
: B-physics xAOD helpers.
bool passed(DecisionID id, const DecisionIDContainer &)
checks if required decision ID is in the set of IDs in the container
const xAOD::Vertex * get() const
static bool isContainedIn(const xAOD::TrackParticle *, const std::vector< const xAOD::TrackParticle * > &)
static Analysis::CleanUpVertex ClosestRefPV(xAOD::BPhysHelper &, const xAOD::VertexContainer *, const Analysis::PrimaryVertexRefitter *)
value_type push_back(value_type pElem)
Add an element to the end of the collection.
static void PrepareVertexLinks(xAOD::Vertex *theResult, const xAOD::TrackParticleContainer *importedTrackCollection)
static void RelinkVertexTracks(const std::vector< const xAOD::TrackParticleContainer * > &trkcols, xAOD::Vertex *vtx)
Trk::TrkVKalVrtFitter * m_VKVFitter
Definition ReVertex.h:64
xAOD::Vertex * fit(const std::vector< const xAOD::TrackParticle * > &inputTracks, const xAOD::TrackParticleContainer *importedTrackCollection, const xAOD::Vertex *pv) const
Definition ReVertex.cxx:282
SG::ReadHandleKey< xAOD::VertexContainer > m_defaultPVContainerName
Definition ReVertex.h:70
std::vector< int > m_indices
Definition ReVertex.h:73
virtual StatusCode initialize() override
Definition ReVertex.cxx:85
void fitAndStore(xAOD::VertexContainer *vtxContainer, const xAOD::Vertex *v, const xAOD::VertexContainer *InVtxContainer, const std::vector< const xAOD::TrackParticle * > &inputTracks, const xAOD::TrackParticleContainer *importedTrackCollection, const xAOD::VertexContainer *pvContainer) const
Definition ReVertex.cxx:222
ToolHandle< InDet::VertexPointEstimator > m_vertexEstimator
Definition ReVertex.h:62
std::vector< std::string > m_hypoNames
Definition ReVertex.h:76
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo_key
Definition ReVertex.h:80
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackContainer
Definition ReVertex.h:67
ReVertex(const std::string &t, const std::string &n, const IInterface *p)
Definition ReVertex.cxx:27
ToolHandle< Trk::IVertexFitter > m_iVertexFitter
Definition ReVertex.h:63
SG::ReadHandleKeyArray< xAOD::VertexContainer > m_CollectionsToCheck
Definition ReVertex.h:99
ToolHandle< Trk::V0Tools > m_v0Tools
Definition ReVertex.h:78
std::vector< double > m_trkMasses
Definition ReVertex.h:72
SG::ReadHandleKeyArray< xAOD::TrackParticleContainer > m_RelinkContainers
Definition ReVertex.h:100
SG::WriteHandleKey< xAOD::VertexContainer > m_refPVContainerName
Definition ReVertex.h:68
ToolHandle< Analysis::PrimaryVertexRefitter > m_pvRefitter
Definition ReVertex.h:79
std::vector< int > m_TrackIndices
Definition ReVertex.h:61
SG::WriteHandleKey< xAOD::VertexContainer > m_OutputContainerName
Definition ReVertex.h:65
SG::ReadHandleKey< xAOD::VertexContainer > m_pvContainerName
Definition ReVertex.h:69
SG::ReadHandleKey< xAOD::VertexContainer > m_inputContainerName
Definition ReVertex.h:66
virtual StatusCode addBranches(const EventContext &ctx) const override
Definition ReVertex.cxx:110
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
pointer_type ptr()
Dereference the pointer.
bool setPrecedingVertices(const std::vector< const xAOD::Vertex * > &vertices, const xAOD::VertexContainer *vertexContainer)
Sets links to preceding vertices.
bool setRefTrks(std::vector< float > px, std::vector< float > py, std::vector< float > pz)
Sets refitted track momenta.
TVector3 totalP()
: Returns total 3-momentum calculated from the refitted tracks
bool pass() const
get the pass flag for this hypothesis
void setTrackParticleLinks(const TrackParticleLinks_t &trackParticles)
Set all track particle links at once.
void clearTracks()
Remove all tracks from the vertex.
const TrackParticleLinks_t & trackParticleLinks() const
Get all the particles associated with the vertex.
Eigen::Matrix< double, 3, 1 > Vector3D
THE reconstruction tool.
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
@ x
Definition ParamDefs.h:55
@ z
global position (cartesian)
Definition ParamDefs.h:57
@ y
Definition ParamDefs.h:56
TrackParticle_v1 TrackParticle
Reference the current persistent version:
VertexContainer_v1 VertexContainer
Definition of the current "Vertex container version".
Vertex_v1 Vertex
Define the latest version of the vertex class.
TrackParticleContainer_v1 TrackParticleContainer
Definition of the current "TrackParticle container version".