ATLAS Offline Software
SpclMcValidationTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // SpclMcValidationTool.cxx
7 // Implementation file for class SpclMcValidationTool
8 // Author: S.Binet<binet@cern.ch>
10 
11 
12 // FrameWork includes
13 
15 
16 // McParticleEvent includes
18 
19 // McParticleKernel includes
21 
22 // McParticleTools includes
23 #include "SpclMcValidationTool.h"
24 
28 
32  const std::string& name,
33  const IInterface* parent ) :
35 {
36  //
37  // Property declaration
38  //
39  declareProperty( "McEvents",
40  m_mcEventsName = "GEN_AOD",
41  "Name of the reference McEventCollection" );
42 
43  declareProperty( "TruthParticles",
44  m_truthParticlesName = "SpclMC",
45  "Name of the 'to-be-validated' TruthParticleContainer" );
46 
47  declareProperty( "McAodWriterTool",
48  m_mcAodWriterTool = McAodTool_t( "McAodWriterTool", this ),
49  "Tool to write the TruthParticleContainer into a "
50  "dedicated file" );
51 }
52 
56 {
57  ATH_MSG_DEBUG("Calling destructor");
58 }
59 
63 
65 {
66  ATH_MSG_INFO("Initializing " << name() << "...");
67  // retrieve and configure McAod writer tool
68  if ( setupMcAodWriterTool().isFailure() ) {
69  ATH_MSG_ERROR("Could not configure the McAod writer tools !!");
70  return StatusCode::FAILURE;
71  }
72 
73  return StatusCode::SUCCESS;
74 }
75 
77 {
78  ATH_MSG_INFO("Finalizing " << name() << "...");
79  return StatusCode::SUCCESS;
80 }
81 
83 {
84  // retrieve McEventCollection
85  const McEventCollection * mcEvents = nullptr;
86  if ( evtStore()->retrieve( mcEvents, m_mcEventsName ).isFailure() ||
87  nullptr == mcEvents ) {
88  ATH_MSG_ERROR("Could not retrieve McEventCollection at ["
89  << m_mcEventsName << "] !!");
90  return StatusCode::FAILURE;
91  } else {
92  ATH_MSG_VERBOSE("Successfully retrieved McEventCollection at ["
93  << m_mcEventsName << "]");
94  }
95 
96  // retrieve TruthParticleContainer
97  const TruthParticleContainer * mcParts = nullptr;
98  if ( evtStore()->retrieve( mcParts, m_truthParticlesName ).isFailure() ||
99  nullptr == mcParts ) {
100  ATH_MSG_ERROR("Could not retrieve TruthParticleContainer at ["
101  << m_truthParticlesName << "] !!");
102  return StatusCode::FAILURE;
103  } else {
104  ATH_MSG_VERBOSE("Successfully TruthParticleContainer at ["
105  << m_truthParticlesName << "]");
106  }
107 
108  return executeTool( mcEvents, mcParts );
109 }
110 
111 StatusCode
113  const TruthParticleContainer* mcParts )
114 {
115  if ( nullptr == mcEvents ) {
116  ATH_MSG_ERROR("NULL pointer to reference McEventCollection !!");
117  return StatusCode::FAILURE;
118  }
119 
120  if ( nullptr == mcParts ) {
121  ATH_MSG_ERROR("NULL pointer to the 'to-be-validated' "\
122  "TruthParticleContainer !!");
123  return StatusCode::FAILURE;
124  }
125 
126  if ( mcEvents->empty() ) {
127  ATH_MSG_WARNING("Empty McEventCollection !");
128  return mcParts->empty() ? StatusCode::SUCCESS : StatusCode::FAILURE;
129  }
130 
131  const HepMC::GenEvent * genEvt = (*mcEvents)[0];
132  if ( nullptr == genEvt ) {
133  ATH_MSG_ERROR("NULL pointer to reference GenEvent !!");
134  return StatusCode::FAILURE;
135  }
136 
137  int particles_size=genEvt->particles_size();
138  if ( static_cast<int>( mcParts->size() ) != particles_size ) {
139  ATH_MSG_ERROR("TruthParticleContainer and McEventCollection don't have "\
140  "the same number of particles !!" << endmsg
141  << "\tTruthParticleContainer: " << mcParts->size() << endmsg
142  << "\tHepMC::GenEvent: " << particles_size);
143  return StatusCode::FAILURE;
144  }
145 
146  if ( !m_mcAodWriterTool->execute().isSuccess() ) {
147  ATH_MSG_WARNING("Could NOT write out the TruthParticleContainer through the IIOMcAodTool !!");
148  }
149 
150  const TruthParticleContainer::const_iterator itrEnd = mcParts->end();
151  for ( TruthParticleContainer::const_iterator itrPart = mcParts->begin();
152  itrPart != itrEnd;
153  ++itrPart ) {
154  const TruthParticle * mc = *itrPart;
155  auto hepMc = mc->genParticle();
156 
157  if ( hepMc->momentum() != HepMC::FourVector(mc->hlv().x(),mc->hlv().y(),mc->hlv().z(),mc->hlv().t()) ) {
158  ATH_MSG_ERROR("TruthParticle and GenParticle-link don't have same 4-mom !!");
159  return StatusCode::FAILURE;
160  }
161 
162  auto genPart = HepMC::barcode_to_particle(genEvt,HepMC::barcode(hepMc));
163 
164  if ( hepMc->momentum() != genPart->momentum() ) {
165  ATH_MSG_ERROR("GenParticle-link and GenParticle from McEvtColl "\
166  "don't have same 4-mom !!");
167  return StatusCode::FAILURE;
168  }
169 
170 
171  }
172  return StatusCode::SUCCESS;
173 }
174 
176 {
177  if ( !m_mcAodWriterTool.retrieve().isSuccess() ) {
178  ATH_MSG_ERROR("Creation of algTool ["
179  << m_mcAodWriterTool.type() << "/"
180  //<< m_mcAodWriterTool.name()
181  << "] FAILED !");
182  return StatusCode::FAILURE;
183  }
184 
185  // now we configure the tools
186  StringProperty prop( "TruthParticles", m_truthParticlesName.value() );
187  if ( m_mcAodWriterTool->setProperty( prop ).isFailure() ) {
188  ATH_MSG_ERROR("Could not set property [" << prop.name() << "] for tool ["
189  << m_mcAodWriterTool.type() << "/"
190  //<< m_mcAodWriterTool.name()
191  << "] !!");
192  return StatusCode::FAILURE;
193  }
194  return StatusCode::SUCCESS;
195 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
SpclMcValidationTool::~SpclMcValidationTool
virtual ~SpclMcValidationTool()
Destructor:
Definition: SpclMcValidationTool.cxx:55
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SpclMcValidationTool::executeTool
virtual StatusCode executeTool()
Main method to perform the validation.
Definition: SpclMcValidationTool.cxx:82
TruthParticleContainer.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
TruthParticleValidationBaseTool
Definition: TruthParticleValidationBaseTool.h:29
TruthParticle
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticle.h:58
SpclMcValidationTool::m_mcAodWriterTool
McAodTool_t m_mcAodWriterTool
Tool to write the TruthParticleContainer into a dedicated file.
Definition: SpclMcValidationTool.h:99
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
TruthParticleContainer
Definition: PhysicsAnalysis/TruthParticleID/McParticleEvent/McParticleEvent/TruthParticleContainer.h:42
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
SpclMcValidationTool::setupMcAodWriterTool
StatusCode setupMcAodWriterTool()
Request pointers to the IIOMcAodTool to be able to write out TruthParticleContainer for further compa...
Definition: SpclMcValidationTool.cxx:175
mc
Definition: mc.PG_single_nu_valid.py:1
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
HepMC::barcode_to_particle
GenParticle * barcode_to_particle(const GenEvent *e, int id)
Definition: GenEvent.h:628
McEventCollection.h
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
test_pyathena.parent
parent
Definition: test_pyathena.py:15
SpclMcValidationTool.h
McEventCollection
This defines the McEventCollection, which is really just an ObjectVector of McEvent objects.
Definition: McEventCollection.h:33
SpclMcValidationTool::finalizeTool
virtual StatusCode finalizeTool()
Definition: SpclMcValidationTool.cxx:76
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SpclMcValidationTool::m_mcEventsName
StringProperty m_mcEventsName
Location of the reference McEventCollection.
Definition: SpclMcValidationTool.h:89
SpclMcValidationTool::initializeTool
virtual StatusCode initializeTool()
Non-const methods:
Definition: SpclMcValidationTool.cxx:64
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
IIOMcAodTool.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
SpclMcValidationTool::McAodTool_t
ToolHandle< IIOMcAodTool > McAodTool_t
Definition: SpclMcValidationTool.h:95
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
SpclMcValidationTool::m_truthParticlesName
StringProperty m_truthParticlesName
Location of the 'to-be-validated' TruthParticleContainer.
Definition: SpclMcValidationTool.h:93
DataVector::empty
bool empty() const noexcept
Returns true if the collection is empty.
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
SpclMcValidationTool::SpclMcValidationTool
SpclMcValidationTool()
Default constructor: