ATLAS Offline Software
MCTrueSeedFinder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /*********************************************************************
6  MCTrueSeedFinder.cxx - Description in header file
7 *********************************************************************/
8 
9 #include <utility>
10 
11 
12 
14 #include "TrkTrack/Track.h"
18 
19 #include "AtlasHepMC/GenEvent.h"
20 #include "AtlasHepMC/GenParticle.h"
21 #include "AtlasHepMC/GenVertex.h"
25 #include "GaudiKernel/IPartPropSvc.h"
26 
27 // Helper functors:
28 #include "HepPDT/ParticleDataTable.hh"
29 
30 namespace {
32  class Interactions_pair {
33  public:
34  float first;
36  Interactions_pair(float p1, Amg::Vector3D p2)
37  : first(p1), second(std::move(p2)) {};
39  bool operator> (const Interactions_pair& other) const
40  {return first < other.first;}
42  bool operator< (const Interactions_pair& other) const
43  {return first > other.first;}
44  };
45 }
46 
47 namespace Trk
48 {
49 
50  MCTrueSeedFinder::MCTrueSeedFinder(const std::string& t, const std::string& n, const IInterface* p) :
51  base_class(t,n,p),
52  m_partPropSvc( "PartPropSvc", n ),
53  m_removeInTimePileUp(false),
54  m_removeHardScattering(false)
55  {
56  declareProperty("RemoveHardScattering", m_removeHardScattering, "Do not consider hard-scattering");
57  declareProperty("RemoveInTimePileUp", m_removeInTimePileUp, "Do not consider in-time pile-up");
58  declareProperty( "PartPropSvc", m_partPropSvc, "Handle to the particle property service" );
59  }
60 
61 
63  = default;
64 
65 
67  {
69  return StatusCode::SUCCESS;
70  }
71 
72 
74  MCTrueSeedFinder::findSeed(const std::vector<const Trk::Track*>& perigeeList,
75  const xAOD::Vertex* constraint) const
76  {
77  std::vector<Amg::Vector3D> seeds = findMultiSeeds (perigeeList, constraint);
78  if (seeds.empty()) {
79  return Amg::Vector3D(0.,0.,0.);
80  }
81  return seeds[0];
82  }
83 
84 
86  MCTrueSeedFinder::findSeed(const std::vector<const Trk::TrackParameters*>& vectorTrk,
87  const xAOD::Vertex* constraint) const
88  {
89  std::vector<Amg::Vector3D> seeds = findMultiSeeds (vectorTrk, constraint);
90  if (seeds.empty()) {
91  return Amg::Vector3D(0.,0.,0.);
92  }
93  return seeds[0];
94  }
95 
96 
97  std::vector<Amg::Vector3D> MCTrueSeedFinder::findMultiSeeds(const std::vector<const Trk::TrackParameters*>& /* perigeeList */,const xAOD::Vertex * /* constraint */) const
98  {
99  std::vector<Amg::Vector3D> seeds;
100  if( retrieveInteractionsInfo (seeds).isFailure() ) {
101  seeds.clear();
102  }
103 
104  return seeds;
105  }
106 
107 
108  std::vector<Amg::Vector3D> MCTrueSeedFinder::findMultiSeeds(const std::vector<const Trk::Track*>& /* vectorTrk */,const xAOD::Vertex * /* constraint */) const
109  {
110  std::vector<Amg::Vector3D> seeds;
111  if( retrieveInteractionsInfo (seeds).isFailure() ) {
112  seeds.clear();
113  }
114 
115  return seeds;
116  }
117 
118  StatusCode
119  MCTrueSeedFinder::retrieveInteractionsInfo (std::vector<Amg::Vector3D>& interactions) const
120  {
121  ATH_MSG_DEBUG("Retrieving interactions information");
122  msg(MSG::DEBUG) << "StoreGate Step: MCTrueSeedFinder retrieves -- " << m_mcEventCollectionKey.key() << endmsg;
124  if ( !mcEventCollection.isValid() ) {
125  msg(MSG::DEBUG)
126  << "Could not retrieve McEventCollection/" << m_mcEventCollectionKey.key() << " from StoreGate." << endmsg;
127  return StatusCode::FAILURE;
128  }
129 
130  interactions.clear();
131  std::vector<Interactions_pair> interactionsColl;
132  McEventCollection::const_iterator itr = mcEventCollection->begin();
133  for ( ; itr != mcEventCollection->end(); ++itr ) {
134  const HepMC::GenEvent* myEvent=(*itr);
135  if(!pass( myEvent, mcEventCollection.cptr())) continue; //checked if events is acceptable
136 
137 
138  //get "intensity" (scalar sum ot p_T^2)
139  double sum_pt2(0.0);
140  for (const auto& part: *myEvent) {
141  if(!pass(part, mcEventCollection.cptr())) continue; //select stable charged particles
142  sum_pt2 += part->momentum().perp2();
143  }
144  ATH_MSG_DEBUG("Calculated Sum P_T^2 = " << sum_pt2);
145 
146  //get position of interaction from first non-zero vertex
147  Amg::Vector3D vtxPosition;
148 #ifdef HEPMC3
149  auto Vert = myEvent->vertices().begin();
150 #else
151  HepMC::GenEvent::vertex_const_iterator Vert = myEvent->vertices_begin();
152 #endif
153  msg(MSG::DEBUG) << "Retrieved position x: " << (*Vert)->position().x() <<
154  " y: " << (*Vert)->position().y() <<
155  " z: " << (*Vert)->position().z() << endmsg;
156  vtxPosition = Amg::Vector3D((*Vert)->position().x(),
157  (*Vert)->position().y(),
158  (*Vert)->position().z());
159 
160  //now store info about position and "intensity" (here: scalar sum of p_T^2)
161  interactionsColl.emplace_back(sum_pt2, vtxPosition );
162  } // end loop over GenEvent
163 
164  //now sort the container and store results to interactions
165  std::sort(interactionsColl.begin(), interactionsColl.end());
166  std::vector<Interactions_pair>::iterator itIp = interactionsColl.begin();
167  ATH_MSG_DEBUG("Sorted collection:");
168  for (; itIp < interactionsColl.end(); ++itIp) {
169  interactions.push_back(itIp->second);
170  ATH_MSG_DEBUG("(" << itIp->second.x() << ", " << itIp->second.y() << ", "
171  << itIp->second.z() << "), SumPt2 = " << itIp->first);
172  }
173 
174  ATH_MSG_DEBUG("New interactions info stored successfully: " << interactions.size() << " interactions found.");
175  return StatusCode::SUCCESS;
176 
177  }
178 
179  bool MCTrueSeedFinder::pass( const HepMC::GenEvent* evt,
180  const McEventCollection* coll ) const {
181 
182  //we select in-time pile-up interactions and hard-scattering, if valid
183 
184 
185  bool isEmpty = ( evt->particles_size() == 0 );
186  bool isDummy = ( ( evt->event_number() == -1 ) &&
187  ( HepMC::signal_process_id(evt) == 0 ) );
188  if( isDummy ) isEmpty = false;
189 
190  if( isEmpty ) return false;
191  if( isDummy ) return false;
192 
193  // We can't do the further selection without the full truth record:
194  if( ! coll ) return true;
195 
198 
199  if( *iter == evt ) {
200  //this is the hard-scattering event (first of the collection)
201  return ! m_removeHardScattering;
202  }
203 
204  int gotzero = 1;
205  for( ; iter != end; ++iter ) {
206  if( ( ( ( *iter )->event_number() == -1 ) &&
207  ( HepMC::signal_process_id( *iter ) == 0 ) ) ) {
208  ++gotzero;
209  }
210  if( evt == *iter ) break;
211  }
212 
213  if( gotzero == 1 && m_removeInTimePileUp ) return false;
214  if( gotzero == 2 ) return false; //remove2BCPileUp
215  if( gotzero == 3 ) return false; //remove800nsPileUp
216  if( gotzero == 4 ) return false; //removeCavernBkg
217 
218  return true;
219  }
220 
222  const McEventCollection* coll ) const {
223 
224  // Check if the particle is coming from a "good" GenEvent:
225  if( ! pass( part->parent_event(), coll ) ) return false;
226 
227  // Now check for stable particles
229  if( ! MC::isGenStable( part ) ) return false;
230  if( ! MC::isSimInteracting( part ) ) return false;
231  }
232 
233  // finally require it to be charged
234  int pdg = part->pdg_id();
235 
237  if( std::abs(pdg) < 7 || std::abs(pdg) == 21 ) return false;
238 
239  const HepPDT::ParticleData* pd = m_partPropSvc->PDT()->particle( std::abs( pdg ) );
240  if( ! pd ) {
241  ATH_MSG_DEBUG( "Could not get particle data for = " << part
242  << " process id " <<HepMC::signal_process_id(part->parent_event()));
243  return false;
244  }
245  float charge = pd->charge();
246 
247  return std::abs( charge ) >= 1E-5;
248  }
249 
250 
251 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
operator<
bool operator<(const DataVector< T > &a, const DataVector< T > &b)
Vector ordering relation.
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
GenEvent.h
Trk::MCTrueSeedFinder::~MCTrueSeedFinder
virtual ~MCTrueSeedFinder()
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
TrackParameters.h
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::ReadHandle< McEventCollection >
HepMC::signal_process_id
int signal_process_id(const GenEvent &e)
Definition: GenEvent.h:513
GenVertex.h
Trk::MCTrueSeedFinder::findMultiSeeds
virtual std::vector< Amg::Vector3D > findMultiSeeds(const std::vector< const Trk::Track * > &vectorTrk, const xAOD::Vertex *constraint=0) const override final
Finds full vector of linearization points from a vector of tracks and returns it as an Amg::Vector3D ...
Definition: MCTrueSeedFinder.cxx:108
MCTrueSeedFinder.h
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Trk::MCTrueSeedFinder::m_mcEventCollectionKey
SG::ReadHandleKey< McEventCollection > m_mcEventCollectionKey
Definition: MCTrueSeedFinder.h:95
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
ParamDefs.h
GenParticle.h
MC::isGenStable
bool isGenStable(const T &p)
Determine if the particle is stable at the generator (not det-sim) level,.
Definition: HepMCHelpers.h:36
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
Track.h
GeoPrimitives.h
Trk::MCTrueSeedFinder::initialize
virtual StatusCode initialize() override
Definition: MCTrueSeedFinder.cxx:66
HepMC::is_simulation_particle
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
Definition: MagicNumbers.h:299
beamspotman.n
n
Definition: beamspotman.py:731
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::MCTrueSeedFinder::pass
bool pass(const HepMC::GenEvent *evt, const McEventCollection *coll=0) const
Function selecting GenEvent objects.
Definition: MCTrueSeedFinder.cxx:179
Trk::MCTrueSeedFinder::m_partPropSvc
ServiceHandle< IPartPropSvc > m_partPropSvc
Get particle properties.
Definition: MCTrueSeedFinder.h:103
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
operator>
bool operator>(const DataVector< T > &a, const DataVector< T > &b)
Based on operator<.
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::MCTrueSeedFinder::m_removeInTimePileUp
bool m_removeInTimePileUp
Flag to consider in-time pile-up interactions.
Definition: MCTrueSeedFinder.h:106
Trk::MCTrueSeedFinder::m_removeHardScattering
bool m_removeHardScattering
Flag to consider hard-scattering interaction.
Definition: MCTrueSeedFinder.h:107
MagicNumbers.h
VP1PartSpect::E
@ E
Definition: VP1PartSpectFlags.h:21
Trk::MCTrueSeedFinder::MCTrueSeedFinder
MCTrueSeedFinder(const std::string &t, const std::string &n, const IInterface *p)
Definition: MCTrueSeedFinder.cxx:50
charge
double charge(const T &p)
Definition: AtlasPID.h:494
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
Trk::MCTrueSeedFinder::findSeed
virtual Amg::Vector3D findSeed(const std::vector< const Trk::Track * > &vectorTrk, const xAOD::Vertex *constraint=0) const override final
Finds a linearization point out of a vector of tracks and returns it as an Amg::Vector3D object.
Definition: MCTrueSeedFinder.cxx:74
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
DeMoScan.first
bool first
Definition: DeMoScan.py:534
DEBUG
#define DEBUG
Definition: page_access.h:11
Trk::MCTrueSeedFinder::retrieveInteractionsInfo
StatusCode retrieveInteractionsInfo(std::vector< Amg::Vector3D > &interactions) const
Definition: MCTrueSeedFinder.cxx:119
MC::isSimInteracting
bool isSimInteracting(const T &p)
Identify if the particle could interact with the detector during the simulation, e....
Definition: HepMCHelpers.h:42
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
HepMCHelpers.h
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.