LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::BdtBeamParticleIdTool Class Reference

BdtBeamParticleIdTool class. More...

#include "BdtBeamParticleIdTool.h"

Inheritance diagram for lar_content::BdtBeamParticleIdTool:
lar_content::SliceIdBaseTool

Classes

class  Plane
 Plane class. More...
 
class  SliceFeatureParameters
 SliceFeatureParameters class. More...
 
class  SliceFeatures
 Slice features class. More...
 

Public Member Functions

 BdtBeamParticleIdTool ()
 Constructor. More...
 
 BdtBeamParticleIdTool (const BdtBeamParticleIdTool &)=default
 Copy constructor. More...
 
BdtBeamParticleIdTooloperator= (const BdtBeamParticleIdTool &)=default
 Assignment operator. More...
 
 ~BdtBeamParticleIdTool ()=default
 Destructor. More...
 
void SelectOutputPfos (const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &beamSliceHypotheses, const SliceHypotheses &crSliceHypotheses, pandora::PfoList &selectedPfos)
 Select which reconstruction hypotheses to use; neutrino outcomes or cosmic-ray muon outcomes for each slice. More...
 

Private Types

typedef std::vector< PlanePlaneVector
 
typedef std::vector< SliceFeaturesSliceFeaturesVector
 
typedef std::pair< unsigned int, float > UintFloatPair
 
typedef std::unordered_map< const pandora::MCParticle *, int > MCParticleToIntMap
 

Private Member Functions

pandora::StatusCode Initialize ()
 
void GetSliceFeatures (const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, SliceFeaturesVector &sliceFeaturesVector) const
 Get the features of each slice. More...
 
void SelectAllPfos (const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &hypotheses, pandora::PfoList &selectedPfos) const
 Select all pfos under the same hypothesis. More...
 
void SelectPfos (const pandora::PfoList &pfos, pandora::PfoList &selectedPfos) const
 Add the given pfos to the selected Pfo list. More...
 
void GetBestMCSliceIndices (const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, pandora::IntVector &bestSliceIndices) const
 Get the slice with the most neutrino induced hits using Monte-Carlo information. More...
 
void PopulateMCParticleToHitsMap (MCParticleToIntMap &mcParticleToIntMap, const pandora::CaloHitList &caloHitList) const
 Fill mc particle to nHits map from calo hit list. More...
 
void Collect2DHits (const pandora::PfoList &pfos, pandora::CaloHitList &caloHitList, const pandora::CaloHitSet &reconstructableCaloHitSet) const
 Collect all 2D hits in a supplied list of Pfos and push them on to an existing hit list, check so not to double count. More...
 
bool PassesQualityCuts (const float purity, const float completeness) const
 Determine if the event passes the selection cuts for training. More...
 
void SelectPfosByAdaBDTScore (const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, const SliceFeaturesVector &sliceFeaturesVector, pandora::PfoList &selectedPfos) const
 Select pfos based on the AdaBDT score that the slice contains a beam particle interaction. More...
 
pandora::StatusCode ReadSettings (const pandora::TiXmlHandle xmlHandle)
 

Private Attributes

bool m_useTrainingMode
 Should use training mode. If true, training examples will be written to the output file. More...
 
std::string m_trainingOutputFile
 Output file name for training examples. More...
 
std::string m_caloHitListName
 Name of input calo hit list. More...
 
std::string m_mcParticleListName
 Name of input MC particle list. More...
 
float m_minPurity
 Minimum purity of the best slice to use event for training. More...
 
float m_minCompleteness
 Minimum completeness of the best slice to use event for training. More...
 
AdaBoostDecisionTree m_adaBoostDecisionTree
 The adaptive boost decision tree. More...
 
std::string m_filePathEnvironmentVariable
 The environment variable providing a list of paths to bdt files. More...
 
unsigned int m_maxNeutrinos
 The maximum number of neutrinos to select in any one event. More...
 
float m_minAdaBDTScore
 Minimum score required to classify a slice as a beam particle. More...
 
SliceFeatureParameters m_sliceFeatureParameters
 Geometry information block. More...
 

Detailed Description

BdtBeamParticleIdTool class.

Definition at line 24 of file BdtBeamParticleIdTool.h.

Member Typedef Documentation

typedef std::unordered_map<const pandora::MCParticle *, int> lar_content::BdtBeamParticleIdTool::MCParticleToIntMap
private

Definition at line 305 of file BdtBeamParticleIdTool.h.

Definition at line 83 of file BdtBeamParticleIdTool.h.

typedef std::pair<unsigned int, float> lar_content::BdtBeamParticleIdTool::UintFloatPair
private

Definition at line 304 of file BdtBeamParticleIdTool.h.

Constructor & Destructor Documentation

lar_content::BdtBeamParticleIdTool::BdtBeamParticleIdTool ( )

Constructor.

Definition at line 23 of file BdtBeamParticleIdTool.cc.

23  :
24  m_useTrainingMode(false),
26  m_minPurity(0.8f),
27  m_minCompleteness(0.8f),
28  m_adaBoostDecisionTree(AdaBoostDecisionTree()),
29  m_filePathEnvironmentVariable("FW_SEARCH_PATH"),
30  m_maxNeutrinos(std::numeric_limits<int>::max()),
32  m_sliceFeatureParameters(SliceFeatureParameters())
33 {
34 }
std::string m_filePathEnvironmentVariable
The environment variable providing a list of paths to bdt files.
float m_minAdaBDTScore
Minimum score required to classify a slice as a beam particle.
bool m_useTrainingMode
Should use training mode. If true, training examples will be written to the output file...
TFile f
Definition: plotHisto.C:6
std::string m_trainingOutputFile
Output file name for training examples.
float m_minPurity
Minimum purity of the best slice to use event for training.
float m_minCompleteness
Minimum completeness of the best slice to use event for training.
unsigned int m_maxNeutrinos
The maximum number of neutrinos to select in any one event.
AdaBoostDecisionTree m_adaBoostDecisionTree
The adaptive boost decision tree.
SliceFeatureParameters m_sliceFeatureParameters
Geometry information block.
lar_content::BdtBeamParticleIdTool::BdtBeamParticleIdTool ( const BdtBeamParticleIdTool )
default

Copy constructor.

Parameters
BdtBeamParticleIdToolto copy
lar_content::BdtBeamParticleIdTool::~BdtBeamParticleIdTool ( )
default

Destructor.

Member Function Documentation

void lar_content::BdtBeamParticleIdTool::Collect2DHits ( const pandora::PfoList &  pfos,
pandora::CaloHitList &  caloHitList,
const pandora::CaloHitSet &  reconstructableCaloHitSet 
) const
private

Collect all 2D hits in a supplied list of Pfos and push them on to an existing hit list, check so not to double count.

Parameters
pfosinput list of pfos
caloHitListoutput list of all 2d hits in the input pfos
reconstructableCaloHitSetto check if part of before adding

Definition at line 266 of file BdtBeamParticleIdTool.cc.

References lar_content::LArPfoHelper::GetCaloHits().

Referenced by GetBestMCSliceIndices().

267 {
268  CaloHitList collectedHits;
269  LArPfoHelper::GetCaloHits(pfos, TPC_VIEW_U, collectedHits);
270  LArPfoHelper::GetCaloHits(pfos, TPC_VIEW_V, collectedHits);
271  LArPfoHelper::GetCaloHits(pfos, TPC_VIEW_W, collectedHits);
272 
273  for (const CaloHit *const pCaloHit : collectedHits)
274  {
275  // ATTN hits collected from Pfos are copies of hits passed from master instance, we need to access their parent to use MC info
276  const CaloHit *pParentCaloHit(static_cast<const CaloHit *>(pCaloHit->GetParentAddress()));
277 
278  if (!reconstructableCaloHitSet.count(pParentCaloHit))
279  continue;
280 
281  // Ensure no hits have been double counted
282  if (std::find(reconstructedCaloHitList.begin(), reconstructedCaloHitList.end(), pParentCaloHit) == reconstructedCaloHitList.end())
283  reconstructedCaloHitList.push_back(pParentCaloHit);
284  }
285 }
static void GetCaloHits(const pandora::PfoList &pfoList, const pandora::HitType &hitType, pandora::CaloHitList &caloHitList)
Get a list of calo hits of a particular hit type from a list of pfos.
void lar_content::BdtBeamParticleIdTool::GetBestMCSliceIndices ( const pandora::Algorithm *const  pAlgorithm,
const SliceHypotheses nuSliceHypotheses,
const SliceHypotheses crSliceHypotheses,
pandora::IntVector bestSliceIndices 
) const
private

Get the slice with the most neutrino induced hits using Monte-Carlo information.

Parameters
pAlgorithmaddress of the master algorithm
nuSliceHypothesesthe input neutrino slice hypotheses
crSliceHypothesesthe input cosmic slice hypotheses
bestSliceIndicesvector of slice indices passing quality cuts

Definition at line 163 of file BdtBeamParticleIdTool.cc.

References Collect2DHits(), f, lar_content::LArMCParticleHelper::GetMCPrimaryMap(), lar_content::LArMCParticleHelper::GetNuanceCode(), m_caloHitListName, lar_content::LArMCParticleHelper::PrimaryParameters::m_maxPhotonPropagation, m_mcParticleListName, lar_content::LArMCParticleHelper::PrimaryParameters::m_selectInputHits, PassesQualityCuts(), PopulateMCParticleToHitsMap(), and lar_content::LArMCParticleHelper::SelectCaloHits().

Referenced by SelectOutputPfos().

165 {
166  // Get all hits in all slices to find true number of mc hits
167  const CaloHitList *pAllReconstructedCaloHitList(nullptr);
168  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*pAlgorithm, m_caloHitListName, pAllReconstructedCaloHitList));
169 
170  const MCParticleList *pMCParticleList(nullptr);
171  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*pAlgorithm, m_mcParticleListName, pMCParticleList));
172 
173  // Obtain map: [mc particle -> primary mc particle]
174  LArMCParticleHelper::MCRelationMap mcToPrimaryMCMap;
175  LArMCParticleHelper::GetMCPrimaryMap(pMCParticleList, mcToPrimaryMCMap);
176 
177  // Remove non-reconstructable hits, e.g. those downstream of a neutron
178  CaloHitList reconstructableCaloHitList;
179  LArMCParticleHelper::PrimaryParameters parameters;
180  LArMCParticleHelper::SelectCaloHits(pAllReconstructedCaloHitList, mcToPrimaryMCMap, reconstructableCaloHitList,
181  parameters.m_selectInputHits, parameters.m_maxPhotonPropagation);
182 
183  MCParticleToIntMap mcParticleToReconstructableHitsMap;
184  this->PopulateMCParticleToHitsMap(mcParticleToReconstructableHitsMap, reconstructableCaloHitList);
185 
186  const CaloHitSet reconstructableCaloHitSet(reconstructableCaloHitList.begin(), reconstructableCaloHitList.end());
187 
188  for (unsigned int sliceIndex = 0, nSlices = nuSliceHypotheses.size(); sliceIndex < nSlices; ++sliceIndex)
189  {
190  // All hits in a slice - No double counting
191  CaloHitList reconstructedCaloHitList;
192  this->Collect2DHits(crSliceHypotheses.at(sliceIndex), reconstructedCaloHitList, reconstructableCaloHitSet);
193 
194  if (nuSliceHypotheses.at(sliceIndex).size() == 1)
195  {
196  const PfoList &nuFinalStates(nuSliceHypotheses.at(sliceIndex).front()->GetDaughterPfoList());
197  this->Collect2DHits(nuFinalStates, reconstructedCaloHitList, reconstructableCaloHitSet);
198  }
199 
200  const unsigned int nRecoHits(reconstructedCaloHitList.size());
201 
202  // MCParticle to hits in slice map
203  MCParticleToIntMap mcParticleToHitsInSliceMap;
204  this->PopulateMCParticleToHitsMap(mcParticleToHitsInSliceMap, reconstructedCaloHitList);
205 
206  if (mcParticleToHitsInSliceMap.empty())
207  continue;
208 
209  // Get best mc particle for slice
210  const MCParticle *pBestMCParticle(nullptr);
211  unsigned int nSharedHits(0);
212 
213  for (const auto &iter : mcParticleToHitsInSliceMap)
214  {
215  if (iter.second > static_cast<int>(nSharedHits))
216  {
217  pBestMCParticle = iter.first;
218  nSharedHits = iter.second;
219  }
220  }
221 
222  // Only consider if target beam particles
223  if (2001 != LArMCParticleHelper::GetNuanceCode(pBestMCParticle))
224  continue;
225 
226  const unsigned int nMCHits(mcParticleToReconstructableHitsMap.at(pBestMCParticle));
227  const float purity(nRecoHits > 0 ? static_cast<float>(nSharedHits) / static_cast<float>(nRecoHits) : 0.f);
228  const float completeness(nMCHits > 0 ? static_cast<float>(nSharedHits) / static_cast<float>(nMCHits) : 0.f);
229 
230  if (this->PassesQualityCuts(purity, completeness))
231  bestSliceIndices.push_back(sliceIndex);
232  }
233  return;
234 }
std::string m_mcParticleListName
Name of input MC particle list.
static void GetMCPrimaryMap(const pandora::MCParticleList *const pMCParticleList, MCRelationMap &mcPrimaryMap)
Get mapping from individual mc particles (in a provided list) and their primary parent mc particles...
static unsigned int GetNuanceCode(const pandora::MCParticle *const pMCParticle)
Get the nuance code of an MCParticle.
bool PassesQualityCuts(const float purity, const float completeness) const
Determine if the event passes the selection cuts for training.
TFile f
Definition: plotHisto.C:6
void Collect2DHits(const pandora::PfoList &pfos, pandora::CaloHitList &caloHitList, const pandora::CaloHitSet &reconstructableCaloHitSet) const
Collect all 2D hits in a supplied list of Pfos and push them on to an existing hit list...
void PopulateMCParticleToHitsMap(MCParticleToIntMap &mcParticleToIntMap, const pandora::CaloHitList &caloHitList) const
Fill mc particle to nHits map from calo hit list.
std::unordered_map< const pandora::MCParticle *, int > MCParticleToIntMap
std::string m_caloHitListName
Name of input calo hit list.
std::unordered_map< const pandora::MCParticle *, const pandora::MCParticle * > MCRelationMap
static void SelectCaloHits(const pandora::CaloHitList *const pCaloHitList, const MCRelationMap &mcToTargetMCMap, pandora::CaloHitList &selectedCaloHitList, const bool selectInputHits, const float maxPhotonPropagation)
Select a subset of calo hits representing those that represent "reconstructable" regions of the event...
void lar_content::BdtBeamParticleIdTool::GetSliceFeatures ( const SliceHypotheses nuSliceHypotheses,
const SliceHypotheses crSliceHypotheses,
SliceFeaturesVector sliceFeaturesVector 
) const
private

Get the features of each slice.

Parameters
nuSliceHypothesesthe input neutrino slice hypotheses
crSliceHypothesesthe input cosmic slice hypotheses
sliceFeaturesVectorvector to hold the slice features

Definition at line 130 of file BdtBeamParticleIdTool.cc.

References m_sliceFeatureParameters.

Referenced by SelectOutputPfos().

132 {
133  for (unsigned int sliceIndex = 0, nSlices = nuSliceHypotheses.size(); sliceIndex < nSlices; ++sliceIndex)
134  sliceFeaturesVector.push_back(SliceFeatures(nuSliceHypotheses.at(sliceIndex), crSliceHypotheses.at(sliceIndex), m_sliceFeatureParameters));
135 }
SliceFeatureParameters m_sliceFeatureParameters
Geometry information block.
StatusCode lar_content::BdtBeamParticleIdTool::Initialize ( )
private

Definition at line 38 of file BdtBeamParticleIdTool.cc.

References lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::Initialize(), and m_sliceFeatureParameters.

39 {
40  // Get global LArTPC geometry information
41  const LArTPCMap &larTPCMap(this->GetPandora().GetGeometry()->GetLArTPCMap());
42  const LArTPC *const pFirstLArTPC(larTPCMap.begin()->second);
43  float parentMinX(pFirstLArTPC->GetCenterX() - 0.5f * pFirstLArTPC->GetWidthX());
44  float parentMaxX(pFirstLArTPC->GetCenterX() + 0.5f * pFirstLArTPC->GetWidthX());
45  float parentMinY(pFirstLArTPC->GetCenterY() - 0.5f * pFirstLArTPC->GetWidthY());
46  float parentMaxY(pFirstLArTPC->GetCenterY() + 0.5f * pFirstLArTPC->GetWidthY());
47  float parentMinZ(pFirstLArTPC->GetCenterZ() - 0.5f * pFirstLArTPC->GetWidthZ());
48  float parentMaxZ(pFirstLArTPC->GetCenterZ() + 0.5f * pFirstLArTPC->GetWidthZ());
49 
50  for (const LArTPCMap::value_type &mapEntry : larTPCMap)
51  {
52  const LArTPC *const pLArTPC(mapEntry.second);
53  parentMinX = std::min(parentMinX, pLArTPC->GetCenterX() - 0.5f * pLArTPC->GetWidthX());
54  parentMaxX = std::max(parentMaxX, pLArTPC->GetCenterX() + 0.5f * pLArTPC->GetWidthX());
55  parentMinY = std::min(parentMinY, pLArTPC->GetCenterY() - 0.5f * pLArTPC->GetWidthY());
56  parentMaxY = std::max(parentMaxY, pLArTPC->GetCenterY() + 0.5f * pLArTPC->GetWidthY());
57  parentMinZ = std::min(parentMinZ, pLArTPC->GetCenterZ() - 0.5f * pLArTPC->GetWidthZ());
58  parentMaxZ = std::max(parentMaxZ, pLArTPC->GetCenterZ() + 0.5f * pLArTPC->GetWidthZ());
59  }
60 
61  try
62  {
63  m_sliceFeatureParameters.Initialize(parentMinX, parentMaxX, parentMinY, parentMaxY, parentMinZ, parentMaxZ);
64  }
65  catch (const StatusCodeException &statusCodeException)
66  {
67  std::cout << "BdtBeamParticleIdTool::Initialize - unable to initialize LArTPC geometry parameters" << std::endl;
68  return STATUS_CODE_FAILURE;
69  }
70 
71  return STATUS_CODE_SUCCESS;
72 }
void Initialize(const float larTPCMinX, const float larTPCMaxX, const float larTPCMinY, const float larTPCMaxY, const float larTPCMinZ, const float larTPCMaxZ)
Set LArTPC geometry information.
SliceFeatureParameters m_sliceFeatureParameters
Geometry information block.
BdtBeamParticleIdTool& lar_content::BdtBeamParticleIdTool::operator= ( const BdtBeamParticleIdTool )
default

Assignment operator.

Parameters
TheBdtBeamParticleIdTool to assign
bool lar_content::BdtBeamParticleIdTool::PassesQualityCuts ( const float  purity,
const float  completeness 
) const
private

Determine if the event passes the selection cuts for training.

Parameters
puritypurity of best slice
completenesscompleteness of best slice
Returns
does the evenr pass the quality cuts on purity and completeness

Definition at line 289 of file BdtBeamParticleIdTool.cc.

References m_minCompleteness, and m_minPurity.

Referenced by GetBestMCSliceIndices().

290 {
291  if ((purity < m_minPurity) || (completeness < m_minCompleteness))
292  return false;
293 
294  return true;
295 }
float m_minPurity
Minimum purity of the best slice to use event for training.
float m_minCompleteness
Minimum completeness of the best slice to use event for training.
void lar_content::BdtBeamParticleIdTool::PopulateMCParticleToHitsMap ( MCParticleToIntMap mcParticleToIntMap,
const pandora::CaloHitList &  caloHitList 
) const
private

Fill mc particle to nHits map from calo hit list.

Parameters
mcParticleToIntMapmap to fill
caloHitListthe input calo hits

Definition at line 238 of file BdtBeamParticleIdTool.cc.

References lar_content::LArMCParticleHelper::GetParentMCParticle().

Referenced by GetBestMCSliceIndices().

239 {
240  for (const CaloHit *const pCaloHit : caloHitList)
241  {
242  try
243  {
244  const MCParticle *pParentMCParticle(LArMCParticleHelper::GetParentMCParticle(MCParticleHelper::GetMainMCParticle(pCaloHit)));
245  MCParticleToIntMap::iterator iter(mcParticleToIntMap.find(pParentMCParticle));
246 
247  if (iter != mcParticleToIntMap.end())
248  {
249  (*iter).second += 1;
250  }
251  else
252  {
253  mcParticleToIntMap.insert(MCParticleToIntMap::value_type(pParentMCParticle, 1));
254  }
255  }
256  catch (const StatusCodeException &statusCodeException)
257  {
258  if (STATUS_CODE_NOT_INITIALIZED != statusCodeException.GetStatusCode())
259  throw statusCodeException;
260  }
261  }
262 }
intermediate_table::iterator iterator
static const pandora::MCParticle * GetParentMCParticle(const pandora::MCParticle *const pMCParticle)
Get the parent mc particle.
StatusCode lar_content::BdtBeamParticleIdTool::ReadSettings ( const pandora::TiXmlHandle  xmlHandle)
private

Definition at line 691 of file BdtBeamParticleIdTool.cc.

References f, lar_content::LArFileHelper::FindFileInPath(), lar_content::AdaBoostDecisionTree::Initialize(), m_adaBoostDecisionTree, m_caloHitListName, m_filePathEnvironmentVariable, m_maxNeutrinos, m_mcParticleListName, m_minAdaBDTScore, m_minCompleteness, m_minPurity, m_sliceFeatureParameters, m_trainingOutputFile, m_useTrainingMode, lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::SetBeamDirection(), lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::SetBeamLArTPCIntersection(), lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::SetContainmentLimit(), lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::SetNSelectedHits(), and lar_content::BdtBeamParticleIdTool::SliceFeatureParameters::SetSelectedFraction().

692 {
693  // BDT Settings
694  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "UseTrainingMode", m_useTrainingMode));
695 
696  if (m_useTrainingMode)
697  {
698  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "TrainingOutputFileName", m_trainingOutputFile));
699 
700  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "CaloHitListName", m_caloHitListName));
701 
702  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "MCParticleListName", m_mcParticleListName));
703  }
704  else
705  {
706  std::string bdtName;
707  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "BdtName", bdtName));
708 
709  std::string bdtFileName;
710  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "BdtFileName", bdtFileName));
711 
712  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "MinAdaBDTScore", m_minAdaBDTScore));
713 
714  const std::string fullBdtFileName(LArFileHelper::FindFileInPath(bdtFileName, m_filePathEnvironmentVariable));
715  const StatusCode statusCode(m_adaBoostDecisionTree.Initialize(fullBdtFileName, bdtName));
716 
717  if (STATUS_CODE_SUCCESS != statusCode)
718  {
719  std::cout << "BdtBeamParticleIdTool::ReadSettings - unable to load bdt" << std::endl;
720  return statusCode;
721  }
722  }
723 
724  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "MinimumPurity", m_minPurity));
725 
726  PANDORA_RETURN_RESULT_IF_AND_IF(
727  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "MinimumCompleteness", m_minCompleteness));
728 
729  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
730  XmlHelper::ReadValue(xmlHandle, "FilePathEnvironmentVariable", m_filePathEnvironmentVariable));
731 
732  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "MaximumNeutrinos", m_maxNeutrinos));
733 
734  // Geometry Information for training
735  FloatVector beamLArTPCIntersection;
736  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
737  XmlHelper::ReadVectorOfValues(xmlHandle, "BeamTPCIntersection", beamLArTPCIntersection));
738 
739  if (3 == beamLArTPCIntersection.size())
740  {
741  pandora::CartesianVector beamLArTPCIntersectionCartesianVector(
742  beamLArTPCIntersection.at(0), beamLArTPCIntersection.at(1), beamLArTPCIntersection.at(2));
743  m_sliceFeatureParameters.SetBeamLArTPCIntersection(beamLArTPCIntersectionCartesianVector);
744  }
745  else if (!beamLArTPCIntersection.empty())
746  {
747  std::cout << "BdtBeamParticleIdTool::ReadSettings - invalid BeamTPCIntersection specified " << std::endl;
748  return STATUS_CODE_INVALID_PARAMETER;
749  }
750  else
751  {
752  // Default for protoDUNE.
753  pandora::CartesianVector beamLArTPCIntersectionCartesianVector(-33.051f, 461.06f, 0.f);
754  m_sliceFeatureParameters.SetBeamLArTPCIntersection(beamLArTPCIntersectionCartesianVector);
755  }
756 
757  FloatVector beamDirection;
758  PANDORA_RETURN_RESULT_IF_AND_IF(
759  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(xmlHandle, "BeamDirection", beamDirection));
760 
761  if (3 == beamDirection.size())
762  {
763  CartesianVector beamDirectionCartesianVector(beamDirection.at(0), beamDirection.at(1), beamDirection.at(2));
764  m_sliceFeatureParameters.SetBeamDirection(beamDirectionCartesianVector);
765  }
766  else if (!beamDirection.empty())
767  {
768  std::cout << "BdtBeamParticleIdTool::ReadSettings - invalid BeamDirection specified " << std::endl;
769  return STATUS_CODE_INVALID_PARAMETER;
770  }
771  else
772  {
773  // Default for protoDUNE.
774  const float thetaXZ0(-11.844f * M_PI / 180.f);
775  CartesianVector beamDirectionCartesianVector(std::sin(thetaXZ0), 0.f, std::cos(thetaXZ0));
776  m_sliceFeatureParameters.SetBeamDirection(beamDirectionCartesianVector);
777  }
778 
779  float selectedFraction(0.f);
780 
781  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "SelectedFraction", selectedFraction));
782 
783  if (selectedFraction > std::numeric_limits<float>::epsilon())
785 
786  unsigned int nSelectedHits(0);
787 
788  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "NSelectedHits", nSelectedHits));
789 
790  if (nSelectedHits > 0)
792 
793  float containmentLimit(0.f);
794 
795  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle, "ContainmentLimit", containmentLimit));
796 
797  if (containmentLimit < 0.f)
798  {
799  std::cout << "BdtBeamParticleIdTool::ReadSettings - invalid ContainmentLimit specified " << std::endl;
800  return STATUS_CODE_INVALID_PARAMETER;
801  }
802  else if (containmentLimit > 0.f)
803  {
805  }
806 
807  return STATUS_CODE_SUCCESS;
808 }
void SetBeamLArTPCIntersection(const pandora::CartesianVector &beamLArTPCIntersection)
Set m_beamLArTPCIntersection.
std::string m_filePathEnvironmentVariable
The environment variable providing a list of paths to bdt files.
std::string m_mcParticleListName
Name of input MC particle list.
float m_minAdaBDTScore
Minimum score required to classify a slice as a beam particle.
bool m_useTrainingMode
Should use training mode. If true, training examples will be written to the output file...
TFile f
Definition: plotHisto.C:6
void SetBeamDirection(const pandora::CartesianVector &beamDirection)
Set m_beamDirection.
void SetNSelectedHits(const unsigned int nSelectedHits)
Set m_nSelectedHits.
std::string m_trainingOutputFile
Output file name for training examples.
void SetSelectedFraction(const float selectedFraction)
Set m_selectedFraction.
float m_minPurity
Minimum purity of the best slice to use event for training.
float m_minCompleteness
Minimum completeness of the best slice to use event for training.
static std::string FindFileInPath(const std::string &unqualifiedFileName, const std::string &environmentVariable, const std::string &delimiter=":")
Find the fully-qualified file name by searching through a list of delimiter-separated paths in a name...
void SetContainmentLimit(const float containmentLimit)
Set m_containmentLimit.
pandora::StatusCode Initialize(const std::string &parameterLocation, const std::string &bdtName)
Initialize the bdt model.
unsigned int m_maxNeutrinos
The maximum number of neutrinos to select in any one event.
std::string m_caloHitListName
Name of input calo hit list.
AdaBoostDecisionTree m_adaBoostDecisionTree
The adaptive boost decision tree.
SliceFeatureParameters m_sliceFeatureParameters
Geometry information block.
void lar_content::BdtBeamParticleIdTool::SelectAllPfos ( const pandora::Algorithm *const  pAlgorithm,
const SliceHypotheses hypotheses,
pandora::PfoList &  selectedPfos 
) const
private

Select all pfos under the same hypothesis.

Parameters
pAlgorithmaddress of the master algorithm
hypothesesthe lists of slices under a certain hypothesis
selectedPfosthe list of pfos to populate

Definition at line 139 of file BdtBeamParticleIdTool.cc.

References SelectPfos().

Referenced by SelectOutputPfos().

140 {
141  for (const PfoList &pfos : hypotheses)
142  {
143  for (const ParticleFlowObject *const pPfo : pfos)
144  {
145  object_creation::ParticleFlowObject::Metadata metadata;
146  metadata.m_propertiesToAdd["TestBeamScore"] = -1.f;
147  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*pAlgorithm, pPfo, metadata));
148  }
149 
150  this->SelectPfos(pfos, selectedPfos);
151  }
152 }
void SelectPfos(const pandora::PfoList &pfos, pandora::PfoList &selectedPfos) const
Add the given pfos to the selected Pfo list.
void lar_content::BdtBeamParticleIdTool::SelectOutputPfos ( const pandora::Algorithm *const  pAlgorithm,
const SliceHypotheses nuSliceHypotheses,
const SliceHypotheses crSliceHypotheses,
pandora::PfoList &  selectedPfos 
)
virtual

Select which reconstruction hypotheses to use; neutrino outcomes or cosmic-ray muon outcomes for each slice.

Parameters
pAlgorithmthe address of the master instance, used to access MCParticles when in training mode
nuSliceHypothesesthe parent pfos representing the neutrino outcome for each slice
crSliceHypothesesthe parent pfos representing the cosmic-ray muon outcome for each slice
sliceNuPfosto receive the list of selected pfos

Implements lar_content::SliceIdBaseTool.

Definition at line 76 of file BdtBeamParticleIdTool.cc.

References lar_content::BdtBeamParticleIdTool::SliceFeatures::FillFeatureVector(), GetBestMCSliceIndices(), GetSliceFeatures(), m_trainingOutputFile, m_useTrainingMode, lar_content::LArMvaHelper::ProduceTrainingExample(), SelectAllPfos(), and SelectPfosByAdaBDTScore().

78 {
79  if (nuSliceHypotheses.size() != crSliceHypotheses.size())
80  throw StatusCodeException(STATUS_CODE_INVALID_PARAMETER);
81 
82  const unsigned int nSlices(nuSliceHypotheses.size());
83  if (nSlices == 0)
84  return;
85 
86  SliceFeaturesVector sliceFeaturesVector;
87  this->GetSliceFeatures(nuSliceHypotheses, crSliceHypotheses, sliceFeaturesVector);
88 
90  {
91  // ATTN in training mode, just return everything as a cosmic-ray
92  this->SelectAllPfos(pAlgorithm, crSliceHypotheses, selectedPfos);
93 
94  pandora::IntVector bestSliceIndices;
95  this->GetBestMCSliceIndices(pAlgorithm, nuSliceHypotheses, crSliceHypotheses, bestSliceIndices);
96 
97  for (unsigned int sliceIndex = 0; sliceIndex < nSlices; ++sliceIndex)
98  {
99  const SliceFeatures &features(sliceFeaturesVector.at(sliceIndex));
100  if (!features.IsFeatureVectorAvailable())
101  continue;
102 
103  LArMvaHelper::MvaFeatureVector featureVector;
104 
105  try
106  {
107  features.FillFeatureVector(featureVector);
108  }
109  catch (const StatusCodeException &statusCodeException)
110  {
111  std::cout << "BdtBeamParticleIdTool::SelectOutputPfos - unable to fill feature vector" << std::endl;
112  continue;
113  }
114 
115  bool isGoodTrainingSlice(false);
116  if (std::find(bestSliceIndices.begin(), bestSliceIndices.end(), sliceIndex) != bestSliceIndices.end())
117  isGoodTrainingSlice = true;
118 
119  LArMvaHelper::ProduceTrainingExample(m_trainingOutputFile, isGoodTrainingSlice, featureVector);
120  }
121 
122  return;
123  }
124 
125  this->SelectPfosByAdaBDTScore(pAlgorithm, nuSliceHypotheses, crSliceHypotheses, sliceFeaturesVector, selectedPfos);
126 }
MvaTypes::MvaFeatureVector MvaFeatureVector
Definition: LArMvaHelper.h:75
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
Produce a training example with the given features and result.
Definition: LArMvaHelper.h:285
bool m_useTrainingMode
Should use training mode. If true, training examples will be written to the output file...
void SelectPfosByAdaBDTScore(const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, const SliceFeaturesVector &sliceFeaturesVector, pandora::PfoList &selectedPfos) const
Select pfos based on the AdaBDT score that the slice contains a beam particle interaction.
std::vector< SliceFeatures > SliceFeaturesVector
void SelectAllPfos(const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &hypotheses, pandora::PfoList &selectedPfos) const
Select all pfos under the same hypothesis.
std::vector< int > IntVector
std::string m_trainingOutputFile
Output file name for training examples.
void GetBestMCSliceIndices(const pandora::Algorithm *const pAlgorithm, const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, pandora::IntVector &bestSliceIndices) const
Get the slice with the most neutrino induced hits using Monte-Carlo information.
void GetSliceFeatures(const SliceHypotheses &nuSliceHypotheses, const SliceHypotheses &crSliceHypotheses, SliceFeaturesVector &sliceFeaturesVector) const
Get the features of each slice.
void lar_content::BdtBeamParticleIdTool::SelectPfos ( const pandora::PfoList &  pfos,
pandora::PfoList &  selectedPfos 
) const
private

Add the given pfos to the selected Pfo list.

Parameters
pfosthe pfos to select
selectedPfosthe list of pfos to populate

Definition at line 156 of file BdtBeamParticleIdTool.cc.

Referenced by SelectAllPfos(), and SelectPfosByAdaBDTScore().

157 {
158  selectedPfos.insert(selectedPfos.end(), pfos.begin(), pfos.end());
159 }
void lar_content::BdtBeamParticleIdTool::SelectPfosByAdaBDTScore ( const pandora::Algorithm *const  pAlgorithm,
const SliceHypotheses nuSliceHypotheses,
const SliceHypotheses crSliceHypotheses,
const SliceFeaturesVector sliceFeaturesVector,
pandora::PfoList &  selectedPfos 
) const
private

Select pfos based on the AdaBDT score that the slice contains a beam particle interaction.

Parameters
pAlgorithmaddress of the master algorithm
nuSliceHypothesesthe input neutrino slice hypotheses
crSliceHypothesesthe input cosmic slice hypotheses
sliceFeaturesVectorvector holding the slice features
selectedPfosthe list of pfos to populate

Definition at line 299 of file BdtBeamParticleIdTool.cc.

References m_adaBoostDecisionTree, m_maxNeutrinos, m_minAdaBDTScore, and SelectPfos().

Referenced by SelectOutputPfos().

301 {
302  // Calculate the probability of each slice that passes the minimum probability cut
303  std::vector<UintFloatPair> sliceIndexAdaBDTScorePairs;
304  for (unsigned int sliceIndex = 0, nSlices = nuSliceHypotheses.size(); sliceIndex < nSlices; ++sliceIndex)
305  {
306  const float nuAdaBDTScore(sliceFeaturesVector.at(sliceIndex).GetAdaBoostDecisionTreeScore(m_adaBoostDecisionTree));
307 
308  for (const ParticleFlowObject *const pPfo : crSliceHypotheses.at(sliceIndex))
309  {
310  object_creation::ParticleFlowObject::Metadata metadata;
311  metadata.m_propertiesToAdd["TestBeamScore"] = nuAdaBDTScore;
312  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*pAlgorithm, pPfo, metadata));
313  }
314 
315  for (const ParticleFlowObject *const pPfo : nuSliceHypotheses.at(sliceIndex))
316  {
317  object_creation::ParticleFlowObject::Metadata metadata;
318  metadata.m_propertiesToAdd["TestBeamScore"] = nuAdaBDTScore;
319  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*pAlgorithm, pPfo, metadata));
320  }
321 
322  if (nuAdaBDTScore < m_minAdaBDTScore)
323  {
324  this->SelectPfos(crSliceHypotheses.at(sliceIndex), selectedPfos);
325  continue;
326  }
327 
328  sliceIndexAdaBDTScorePairs.push_back(UintFloatPair(sliceIndex, nuAdaBDTScore));
329  }
330 
331  // Sort the slices by probability
332  std::sort(sliceIndexAdaBDTScorePairs.begin(), sliceIndexAdaBDTScorePairs.end(),
333  [](const UintFloatPair &a, const UintFloatPair &b) { return (a.second > b.second); });
334 
335  // Select the first m_maxNeutrinos as neutrinos, and the rest as cosmic
336  unsigned int nNuSlices(0);
337  for (const UintFloatPair &slice : sliceIndexAdaBDTScorePairs)
338  {
339  if (nNuSlices < m_maxNeutrinos)
340  {
341  this->SelectPfos(nuSliceHypotheses.at(slice.first), selectedPfos);
342  nNuSlices++;
343  continue;
344  }
345 
346  this->SelectPfos(crSliceHypotheses.at(slice.first), selectedPfos);
347  }
348 }
float m_minAdaBDTScore
Minimum score required to classify a slice as a beam particle.
void SelectPfos(const pandora::PfoList &pfos, pandora::PfoList &selectedPfos) const
Add the given pfos to the selected Pfo list.
unsigned int m_maxNeutrinos
The maximum number of neutrinos to select in any one event.
std::pair< unsigned int, float > UintFloatPair
AdaBoostDecisionTree m_adaBoostDecisionTree
The adaptive boost decision tree.

Member Data Documentation

AdaBoostDecisionTree lar_content::BdtBeamParticleIdTool::m_adaBoostDecisionTree
private

The adaptive boost decision tree.

Definition at line 397 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings(), and SelectPfosByAdaBDTScore().

std::string lar_content::BdtBeamParticleIdTool::m_caloHitListName
private

Name of input calo hit list.

Definition at line 391 of file BdtBeamParticleIdTool.h.

Referenced by GetBestMCSliceIndices(), and ReadSettings().

std::string lar_content::BdtBeamParticleIdTool::m_filePathEnvironmentVariable
private

The environment variable providing a list of paths to bdt files.

Definition at line 398 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings().

unsigned int lar_content::BdtBeamParticleIdTool::m_maxNeutrinos
private

The maximum number of neutrinos to select in any one event.

Definition at line 399 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings(), and SelectPfosByAdaBDTScore().

std::string lar_content::BdtBeamParticleIdTool::m_mcParticleListName
private

Name of input MC particle list.

Definition at line 392 of file BdtBeamParticleIdTool.h.

Referenced by GetBestMCSliceIndices(), and ReadSettings().

float lar_content::BdtBeamParticleIdTool::m_minAdaBDTScore
private

Minimum score required to classify a slice as a beam particle.

Definition at line 400 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings(), and SelectPfosByAdaBDTScore().

float lar_content::BdtBeamParticleIdTool::m_minCompleteness
private

Minimum completeness of the best slice to use event for training.

Definition at line 394 of file BdtBeamParticleIdTool.h.

Referenced by PassesQualityCuts(), and ReadSettings().

float lar_content::BdtBeamParticleIdTool::m_minPurity
private

Minimum purity of the best slice to use event for training.

Definition at line 393 of file BdtBeamParticleIdTool.h.

Referenced by PassesQualityCuts(), and ReadSettings().

std::string lar_content::BdtBeamParticleIdTool::m_trainingOutputFile
private

Output file name for training examples.

Definition at line 390 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings(), and SelectOutputPfos().

bool lar_content::BdtBeamParticleIdTool::m_useTrainingMode
private

Should use training mode. If true, training examples will be written to the output file.

Definition at line 389 of file BdtBeamParticleIdTool.h.

Referenced by ReadSettings(), and SelectOutputPfos().


The documentation for this class was generated from the following files: