Commit f391282c authored by Nicholas Styles's avatar Nicholas Styles Committed by Graeme Stewart
Browse files

fix for memory management issue (TrkTruthToTrack-00-02-01)

parent 2edc7ca8
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
// Dear emacs, this is -*-c++-*-
/**
* @file
*
* Extrapolation for HepMC particles.
*
* @author Andrei Gaponenko, 2005, 2009
*/
// FIXME: don't export this header (move to src/)
#ifndef TRUTHTOTRACK_IMP
#warning "Header TruthToTrack/TruthToTrack.h is deprecated and will be removed in the future. Please use the tool via this interface: TrkToolInterfaces/ITruthToTrack.h"
#endif/*TRUTHTOTRACK_IMP*/
#ifndef TRUTHTOTRACK_H
#define TRUTHTOTRACK_H
#include "GaudiKernel/ToolHandle.h"
#include "GaudiKernel/AlgTool.h"
#include "TrkToolInterfaces/ITruthToTrack.h"
#include "AthenaBaseComps/AthMessaging.h"
namespace HepPDT { class ParticleDataTable; }
namespace Trk { class IExtrapolator; }
namespace Trk {
class TruthToTrack : virtual public ITruthToTrack,
public ::AlgTool,
public ::AthMessaging
{
public:
// FIXME: interfaceID() should not be here, but clients that use TruthToTrack directly
// (not via ITruthToTrack) will break without this:
// static const InterfaceID& interfaceID() { return ITruthToTrack::interfaceID(); }
TruthToTrack(const std::string& type, const std::string& name, const IInterface* parent);
virtual StatusCode initialize();
/** Implements interface method. */
virtual const Trk::TrackParameters* makeProdVertexParameters(const HepMC::GenParticle* part) const;
/** Implements interface method.
* You can configure an extrapolator through the job options.
*/
virtual const Trk::TrackParameters* makePerigeeParameters(const HepMC::GenParticle* part) const;
private:
const HepPDT::ParticleDataTable *m_particleDataTable;
ToolHandle<Trk::IExtrapolator> m_extrapolator;
};
}
#endif/*TRUTHTOTRACK_H*/
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#ifndef TRUTHRECORDTOTRACK_H
#define TRUTHRECORDTOTRACK_H
#include "AthenaBaseComps/AthAlgTool.h"
#include "TrkToolInterfaces/ITruthToTrack.h"
#include "GaudiKernel/ToolHandle.h"
#include "TrkParameters/TrackParameters.h"
#include <string>
namespace HepPDT { class ParticleDataTable; }
namespace HepMC { class GenParticle; }
namespace Trk {
class IExtrapolator;
/**
@class TruthTrackRecordToTrack
TruthTrackRecordToTrack is an AlgTool to create
Trk::TrackParameters from a HepMC::GenParticle expressed
at a "TrackRecordCollection" anywhere in ATLAS. The purpose
is to analyse non-collision particles (therefore uses TrackRecord,
not GenParticle's prod vertex) in the reconstructed Track's
reference frame. The code stems from old cosmics-testbeam analysis
refurbished under the ITruthToTrack interface.
@author Wolfgang.Liebig -at- cern.ch
*/
class TruthTrackRecordToTrack : virtual public ITruthToTrack, public AthAlgTool {
public:
TruthTrackRecordToTrack(const std::string& type, const std::string& name, const IInterface* parent);
virtual StatusCode initialize();
// virtual ~TruthTrackRecordToTrack ()
/** This function produces a Trk::TrackParameters object
* corresponding to the HepMC::GenParticle at the TrackRecord's
* layer. The method is a TrackParameter factory (client obtains
* ownership!)
*/
virtual const Trk::TrackParameters* makeProdVertexParameters(const HepMC::GenParticle* part) const;
/** This function produces Trk::TrackParameters corresponding to
MC truth and in addition extrapolates them to the perigee.
The return object will be of flavour Trk::Perigee.
Once more it employs factory design: clients need to take care
of deleting the returned parameters object.
*/
virtual const Trk::TrackParameters* makePerigeeParameters(const HepMC::GenParticle* part) const;
private:
const HepPDT::ParticleDataTable *m_particleDataTable;
ToolHandle<Trk::IExtrapolator> m_extrapolator;
std::string m_reccollkey;
/** Forbid copying of Truth...ToTrack to not mess with the
internal pointers. */
TruthTrackRecordToTrack(const TruthTrackRecordToTrack&);
TruthTrackRecordToTrack& operator=(const TruthTrackRecordToTrack&);
};
}
#endif/*TRUTHRECORDTOTRACK_H*/
package TrkTruthToTrack
author Andrei Gaponenko <AGaponenko@lbl.gov>
use AtlasPolicy AtlasPolicy-*
# stuff included in the exported headers should be in the public section
use GaudiInterface GaudiInterface-* External
use AthenaBaseComps AthenaBaseComps-* Control
use TrkToolInterfaces TrkToolInterfaces-* Tracking/TrkTools
use TrkParameters TrkParameters-* Tracking/TrkEvent
apply_pattern component_library
library TrkTruthToTrack *.cxx components/*.cxx
# everithing else can be private
private
use AtlasHepMC AtlasHepMC-* External
use HepPDT v* LCG_Interfaces
use TrackRecord * Simulation/G4Sim
use TrkExInterfaces TrkExInterfaces-* Tracking/TrkExtrapolation
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
/**
@mainpage TrkTruthToTrack
@author andrei.gaponenko@cern.ch
@section TrkTruthToTrack_Intro Introduction
This package provied a way to create a Trk::TrackParameters object
from a HepMC::GenParticle. This allows to use Trk::Extrapolator for
extrapolation of "true" track parameters.
@ref used_TrkTruthToTrack
@ref requirements_TrkTruthToTrack
*/
/**
@page used_TrkTruthToTrack Used Packages
@htmlinclude used_packages.html
*/
/**
@page requirements_TrkTruthToTrack Requirements
@include requirements
*/
include.block("TrkTruthToTrack/TrkTruthToTrack.py")
from TrkTruthToTrack.TrkTruthToTrackConf import Trk__TruthToTrack
TrkTruthToTrack = Trk__TruthToTrack( name = "TrkTruthToTrack" )
ToolSvc = Service("ToolSvc")
ToolSvc += TrkTruthToTrack
print TrkTruthToTrack
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#define TRUTHTOTRACK_IMP
#include "TrkTruthToTrack/TruthToTrack.h"
#include <cmath>
#include <memory>
#include "GaudiKernel/IPartPropSvc.h"
#include "HepMC/GenParticle.h"
#include "HepMC/GenVertex.h"
//#include "CLHEP/Geometry/Transform3D.h"
//#include "TrkEventPrimitives/GlobalPosition.h"
//#include "TrkEventPrimitives/GlobalMomentum.h"
#include "HepPDT/ParticleDataTable.hh"
//#include "TrkParameters/Perigee.h"
#include "TrkParameters/TrackParameters.h"
//#include "TrkParameters/AtaPlane.h"
#include "TrkExInterfaces/IExtrapolator.h"
//================================================================
Trk::TruthToTrack::TruthToTrack(const std::string& type, const std::string& name, const IInterface* parent)
: ::AlgTool(type,name,parent)
, ::AthMessaging(msgSvc(), name)
, m_particleDataTable(0)
, m_extrapolator("Trk::Extrapolator/AtlasExtrapolator")
{
declareInterface<ITruthToTrack>(this);
declareProperty("Extrapolator", m_extrapolator);
}
//================================================================
StatusCode Trk::TruthToTrack::initialize() {
// get the Particle Properties Service
IPartPropSvc* partPropSvc = 0;
StatusCode sc = service("PartPropSvc", partPropSvc, true);
if (sc.isFailure()) {
ATH_MSG_ERROR("Could not initialize Particle Properties Service");
return StatusCode::FAILURE;
}
m_particleDataTable = partPropSvc->PDT();
if ( m_extrapolator.retrieve().isFailure() ) {
ATH_MSG_FATAL("Failed to retrieve tool " << m_extrapolator);
return StatusCode::FAILURE;
} else {
ATH_MSG_INFO("Retrieved tool " << m_extrapolator);
}
return StatusCode::SUCCESS;
}
//================================================================
const Trk::TrackParameters* Trk::TruthToTrack::makeProdVertexParameters(const HepMC::GenParticle* part) const {
Trk::TrackParameters *result = 0;
if(part && part->production_vertex() && m_particleDataTable) {
HepMC::ThreeVector tv = part->production_vertex()->point3d();
Amg::Vector3D hv(tv.x(),tv.y(),tv.z());
Amg::Vector3D globalPos = hv;
HepMC::FourVector fv = part->momentum();
Amg::Vector3D hv2(fv.px(),fv.py(),fv.pz());
Amg::Vector3D globalMom = hv2;
int id = part->pdg_id();
// the table seems to lack antiparticles, thus the use of abs()
const HepPDT::ParticleData* pd = m_particleDataTable->particle(std::abs(id));
if(pd) {
// pd could point to an antiparticle. recover the sign:
double charge = (id>0) ? pd->charge() : -pd->charge();
// Trk::PlaneSurface surface(new HepGeom::Translate3D(part->production_vertex()->point3d()));
Amg::Translation3D tmpTransl(hv);
Amg::Transform3D tmpTransf = tmpTransl * Amg::RotationMatrix3D::Identity();
Trk::PlaneSurface surface(new Amg::Transform3D(tmpTransf));
result = new Trk::AtaPlane(globalPos, globalMom, charge, surface);
}
else {
ATH_MSG_WARNING("Could not get particle data for particle ID="<<id);
}
}
return result;
}
//================================================================
const Trk::TrackParameters* Trk::TruthToTrack::makePerigeeParameters(const HepMC::GenParticle* part) const {
const Trk::TrackParameters* generatedTrackPerigee = 0;
if(part && part->production_vertex() && m_particleDataTable && m_extrapolator) {
std::auto_ptr<const Trk::TrackParameters> productionVertexTrackParams( makeProdVertexParameters(part) );
if(productionVertexTrackParams.get()) {
// Extrapolate the TrackParameters object to the perigee. Direct extrapolation,
// no material effects.
generatedTrackPerigee = m_extrapolator->extrapolateDirectly( *productionVertexTrackParams,
Trk::PerigeeSurface(),
Trk::anyDirection,
false,
Trk::nonInteracting );
}
}
return generatedTrackPerigee;
}
//================================================================
/*
Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
*/
#include "TrkTruthToTrack/TruthTrackRecordToTrack.h"
#include <cmath>
#include <memory>
#include "GaudiKernel/IPartPropSvc.h"
#include "HepMC/GenParticle.h"
#include "HepMC/GenVertex.h"
//#include "CLHEP/Geometry/Transform3D.h"
//#include "TrkEventPrimitives/GlobalPosition.h"
//#include "TrkEventPrimitives/GlobalMomentum.h"
#include "HepPDT/ParticleDataTable.hh"
//#include "TrkParameters/Perigee.h"
//#include "TrkParameters/AtaPlane.h"
#include "TrkExInterfaces/IExtrapolator.h"
#include "TrackRecord/TrackRecord.h"
#include "TrackRecord/TrackRecordCollection.h"
//================================================================
Trk::TruthTrackRecordToTrack::TruthTrackRecordToTrack(const std::string& type, const std::string& name,
const IInterface* parent)
: AthAlgTool(type,name,parent),
m_particleDataTable(0),
m_extrapolator("Trk::Extrapolator/AtlasExtrapolator")
{
declareInterface<ITruthToTrack>(this);
declareProperty("Extrapolator", m_extrapolator);
declareProperty("TrackRecordKey",m_reccollkey="CosmicPerigee");
}
//================================================================
StatusCode Trk::TruthTrackRecordToTrack::initialize() {
// get the Particle Properties Service
IPartPropSvc* partPropSvc = 0;
StatusCode sc = service("PartPropSvc", partPropSvc, true);
if (sc.isFailure()) {
ATH_MSG_ERROR ("Could not initialize Particle Properties Service");
return StatusCode::FAILURE;
}
m_particleDataTable = partPropSvc->PDT();
if ( m_extrapolator.retrieve().isFailure() ) {
ATH_MSG_FATAL ("Failed to retrieve tool " << m_extrapolator );
return StatusCode::FAILURE;
} else {
ATH_MSG_INFO("Retrieved tool " << m_extrapolator);
}
return StatusCode::SUCCESS;
}
//================================================================
const Trk::TrackParameters* Trk::TruthTrackRecordToTrack::makeProdVertexParameters(const HepMC::GenParticle* part) const {
if (part == NULL || m_particleDataTable==NULL) return 0;
Trk::TrackParameters *result = 0;
Amg::Vector3D prodVertexVector;
Amg::Vector3D globalPos;
Amg::Vector3D globalMom;
int id=0;
double charge = 0.0;
const HepPDT::ParticleData* pd = 0;
/* // -- backup: get the genparticle prod vertex
if(part->production_vertex()) {
HepMC::ThreeVector tv = part->production_vertex()->point3d();
prodVertexVector = CLHEP::Hep3Vector(tv.x(),tv.y(),tv.z());
globalPos = prodVertexVector;
HepMC::FourVector fv = part->momentum();
CLHEP::Hep3Vector hv2(fv.px(),fv.py(),fv.pz());
globalMom = hv2;
id = part->pdg_id();
// the table seems to lack antiparticles, thus the use of abs()
pd = m_particleDataTable->particle(std::abs(id));
}*/
const TrackRecordCollection* recordCollection;
StatusCode sc=evtStore()->retrieve(recordCollection, m_reccollkey);
if (sc==StatusCode::FAILURE) {
ATH_MSG_ERROR ("Could not get track record!");
return 0;
}
ATH_MSG_DEBUG("reading from track record, size=" << recordCollection->size());
if (recordCollection->size() == 0) ATH_MSG_WARNING ("action required but record size is 0");
for (TrackRecordCollection::const_iterator record = recordCollection->begin(); record != recordCollection->end();++record){
if ( (*record)->GetBarCode() == part->barcode() ) {
id = (**record).GetPDGCode();
pd = m_particleDataTable->particle(std::abs(id));
if (!pd) {
ATH_MSG_WARNING ("found barcode but could not digest pdg_id. " <<
part->barcode() << " , " << id);
continue;
}
HepMC::ThreeVector tv = (**record).GetPosition();
prodVertexVector = Amg::Vector3D(tv.x(),tv.y(),tv.z());
globalPos = prodVertexVector;
Amg::Vector3D hv2((**record).GetMomentum().x(), (**record).GetMomentum().y(),
(**record).GetMomentum().z());
globalMom = hv2;
ATH_MSG_DEBUG("found barcode " << part->barcode() << " with pdg ID " <<
id << ", momentum " << hv2 << " production " << globalPos);
} // if barcodes match
} // loop over G4 records
if (pd) {
charge = (id>0) ? pd->charge() : -pd->charge();
Amg::Translation3D prodSurfaceCentre( prodVertexVector.x(),
prodVertexVector.y(),
prodVertexVector.z() );
Amg::Transform3D tmpTransf = prodSurfaceCentre * Amg::RotationMatrix3D::Identity();
Trk::PlaneSurface planeSurface(&tmpTransf, 5., 5. );
result = new Trk::AtaPlane(globalPos, globalMom, charge, planeSurface);
} else {
ATH_MSG_WARNING ("Could not get particle data for particle ID="<<id);
}
return result;
}
//================================================================
const Trk::TrackParameters* Trk::TruthTrackRecordToTrack::makePerigeeParameters(const HepMC::GenParticle* part) const {
const Trk::TrackParameters* generatedTrackPerigee = 0;
if(part && part->production_vertex() && m_particleDataTable && m_extrapolator) {
MsgStream log(msgSvc(), name());
std::auto_ptr<const Trk::TrackParameters> productionVertexTrackParams( makeProdVertexParameters(part) );
if(productionVertexTrackParams.get()) {
// Extrapolate the TrackParameters object to the perigee. Direct extrapolation,
// no material effects.
generatedTrackPerigee = m_extrapolator->extrapolateDirectly( *productionVertexTrackParams,
Trk::PerigeeSurface(),
Trk::anyDirection,
false,
Trk::nonInteracting );
}
}
return generatedTrackPerigee;
}
//================================================================
#include "GaudiKernel/DeclareFactoryEntries.h"
#define TRUTHTOTRACK_IMP
#include "TrkTruthToTrack/TruthToTrack.h"
#include "TrkTruthToTrack/TruthTrackRecordToTrack.h"
DECLARE_NAMESPACE_TOOL_FACTORY(Trk, TruthToTrack)
DECLARE_NAMESPACE_TOOL_FACTORY(Trk, TruthTrackRecordToTrack)
DECLARE_FACTORY_ENTRIES(TrkTruthToTrack) {
DECLARE_NAMESPACE_TOOL(Trk, TruthToTrack)
DECLARE_NAMESPACE_TOOL(Trk, TruthTrackRecordToTrack)
}
#include "GaudiKernel/LoadFactoryEntries.h"
LOAD_FACTORY_ENTRIES(TrkTruthToTrack)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment