9 #include "Pandora/AlgorithmHeaders.h" 25 m_trainingSetMode(false),
26 m_testBeamMode(false),
27 m_enableProbability(true),
28 m_useThreeDInformation(true),
29 m_minProbabilityCut(0.5
f),
31 m_applyFiducialCut(false),
38 m_applyReconstructabilityChecks(false),
39 m_filePathEnvironmentVariable(
"FW_SEARCH_PATH")
55 bool isTrueTrack(
false);
59 const MCParticle *
const pMCParticle(MCParticleHelper::GetMainMCParticle(pCluster));
60 isTrueTrack = ((
PHOTON != pMCParticle->GetParticleId()) && (E_MINUS !=
std::abs(pMCParticle->GetParticleId())));
62 catch (
const StatusCodeException &)
89 object_creation::ParticleFlowObject::Metadata
metadata;
90 metadata.m_propertiesToAdd[
"TrackScore"] = -1.f;
91 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*
this, pPfo, metadata));
93 return (pPfo->GetParticleId() == MU_MINUS);
97 ClusterList wClusterList;
106 const MCParticleList *pMCParticleList(
nullptr);
107 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*
this,
m_mcParticleListName, pMCParticleList));
109 const CaloHitList *pCaloHitList(
nullptr);
110 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*
this,
m_caloHitListName, pCaloHitList));
126 if (pfoToReconstructable2DHitsMap.empty())
132 pfoToReconstructable2DHitsMap, mcParticlesToGoodHitsMaps, pfoToMCParticleHitSharingMap, mcParticleToPfoHitSharingMap);
133 if (pfoToMCParticleHitSharingMap.empty())
136 unsigned int nHitsInBestMCParticleTotal(0);
137 unsigned int nHitsSharedWithBestMCParticleTotal(0);
138 int bestMCParticlePdgCode(0);
139 CartesianVector threeDVertexPosition(0.
f, 0.
f, 0.
f);
140 float hitsShower(0), hitsTrack(0);
145 const pandora::MCParticle *
const pAssociatedMCParticle(mcParticleCaloHitListPair.first);
146 const CaloHitList &allMCHits(targetMCParticleToHitsMap.at(pAssociatedMCParticle));
147 const CaloHitList &associatedMCHits(mcParticleCaloHitListPair.second);
149 if ((
PHOTON == pAssociatedMCParticle->GetParticleId()) || (E_MINUS ==
std::abs(pAssociatedMCParticle->GetParticleId())))
150 hitsShower += associatedMCHits.size();
152 hitsTrack += associatedMCHits.size();
154 if (associatedMCHits.size() > nHitsSharedWithBestMCParticleTotal)
156 nHitsSharedWithBestMCParticleTotal = associatedMCHits.size();
157 nHitsInBestMCParticleTotal = allMCHits.size();
158 bestMCParticlePdgCode = pAssociatedMCParticle->GetParticleId();
159 threeDVertexPosition = pAssociatedMCParticle->GetVertex();
163 const float trackShowerHitsRatio((hitsTrack + hitsShower) > 0 ? hitsTrack / (hitsTrack + hitsShower) : 0.
f);
164 const bool isTrueTrack(trackShowerHitsRatio >= 0.5);
166 const int nHitsInPfoTotal(pfoToReconstructable2DHitsMap.at(pPfo).size());
167 const float purity((nHitsInPfoTotal > 0) ? nHitsSharedWithBestMCParticleTotal / static_cast<float>(nHitsInPfoTotal) : 0.f);
168 const float completeness(
169 (nHitsInBestMCParticleTotal > 0) ? nHitsSharedWithBestMCParticleTotal / static_cast<float>(nHitsInBestMCParticleTotal) : 0.f);
171 CaloHitList checkHitListW;
173 CaloHitList checkHitListU;
175 CaloHitList checkHitListV;
177 CaloHitList checkHitListAll;
178 checkHitListAll.splice(checkHitListAll.end(), checkHitListW);
179 checkHitListAll.splice(checkHitListAll.end(), checkHitListU);
180 checkHitListAll.splice(checkHitListAll.end(), checkHitListV);
189 unsigned int showerCount(0), allCount(0);
190 for (
const CaloHit *pHit : checkHitListAll)
192 if (hitToMCMap.find(pHit) != hitToMCMap.end())
194 const MCParticle *pHitMCParticle(hitToMCMap.at(pHit));
195 if ((
PHOTON == pHitMCParticle->GetParticleId()) || (E_MINUS ==
std::abs(pHitMCParticle->GetParticleId())))
203 const float showerProbability(showerCount / static_cast<float>(allCount));
204 const bool mischaracterisedPfo((showerProbability < 0.5f && !isTrueTrack) || (showerProbability > 0.5 && isTrueTrack) ?
true :
false);
205 const bool isMainMCParticleSet(bestMCParticlePdgCode != 0);
207 if (isMainMCParticleSet)
212 const std::string end = ((wClusterList.empty()) ?
"noChargeInfo.txt" :
".txt");
213 outputFile.append(end);
222 bool isTrueTrack(
false);
223 bool isMainMCParticleSet(
false);
228 isTrueTrack = ((
PHOTON != pMCParticle->GetParticleId()) && (E_MINUS !=
std::abs(pMCParticle->GetParticleId())));
229 isMainMCParticleSet = (pMCParticle->GetParticleId() != 0);
231 catch (
const StatusCodeException &)
235 if (isMainMCParticleSet)
238 outputFile.append(wClusterList.empty() ?
"noChargeInfo.txt" :
".txt");
247 if (!featureValue.IsInitialized())
251 object_creation::ParticleFlowObject::Metadata
metadata;
252 metadata.m_propertiesToAdd[
"TrackScore"] = -1.f;
253 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*
this, pPfo, metadata));
255 return (pPfo->GetParticleId() == MU_MINUS);
267 object_creation::ParticleFlowObject::Metadata
metadata;
268 metadata.m_propertiesToAdd[
"TrackScore"] =
score;
269 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::ParticleFlowObject::AlterMetadata(*
this, pPfo, metadata));
276 template <
typename T>
279 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
282 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
285 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
288 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
291 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
294 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
297 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
300 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"TrainingSetMode",
m_trainingSetMode));
302 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MinCaloHitsCut",
m_minCaloHitsCut));
304 PANDORA_RETURN_RESULT_IF_AND_IF(
305 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"UseThreeDInformation",
m_useThreeDInformation));
307 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
311 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"BdtFileName",
m_mvaFileName));
312 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"SvmFileName",
m_mvaFileName));
313 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MvaFileName",
m_mvaFileName));
315 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"BdtName",
m_mvaName));
316 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"SvmName",
m_mvaName));
317 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MvaName",
m_mvaName));
321 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
323 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
325 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
328 PANDORA_RETURN_RESULT_IF_AND_IF(
329 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"BdtNameNoChargeInfo",
m_mvaNameNoChargeInfo));
330 PANDORA_RETURN_RESULT_IF_AND_IF(
331 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"SvmNameNoChargeInfo",
m_mvaNameNoChargeInfo));
332 PANDORA_RETURN_RESULT_IF_AND_IF(
333 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MvaNameNoChargeInfo",
m_mvaNameNoChargeInfo));
336 PANDORA_RETURN_RESULT_IF_AND_IF(
337 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"EnableProbability",
m_enableProbability));
339 PANDORA_RETURN_RESULT_IF_AND_IF(
340 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MinProbabilityCut",
m_minProbabilityCut));
344 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"CaloHitListName",
m_caloHitListName));
345 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"MCParticleListName",
m_mcParticleListName));
346 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"TrainingOutputFileName",
m_trainingOutputFile));
347 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"TestBeamMode",
m_testBeamMode));
348 PANDORA_RETURN_RESULT_IF_AND_IF(
349 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"ApplyFiducialCut",
m_applyFiducialCut));
352 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMinX",
m_fiducialMinX));
353 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMaxX",
m_fiducialMaxX));
354 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMinY",
m_fiducialMinY));
355 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMaxY",
m_fiducialMaxY));
356 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMinZ",
m_fiducialMinZ));
357 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle,
"FiducialCutMaxZ",
m_fiducialMaxZ));
359 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
366 std::cout <<
"MvaPfoCharacterisationAlgorithm: MvaFileName and MvaName must be set if in classification mode " <<
std::endl;
367 return STATUS_CODE_INVALID_PARAMETER;
377 std::cout <<
"MvaPfoCharacterisationAlgorithm: MvaFileNameNoChargeInfo and MvaNameNoChargeInfo must be set if in classification mode for no charge info in 3D mode " 379 return STATUS_CODE_INVALID_PARAMETER;
386 AlgorithmToolVector algorithmToolVector;
387 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ProcessAlgorithmToolList(*
this, xmlHandle,
"FeatureTools", algorithmToolVector));
391 AlgorithmToolVector algorithmToolVectorNoChargeInfo;
392 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=,
393 XmlHelper::ProcessAlgorithmToolList(*
this, xmlHandle,
"FeatureToolsNoChargeInfo", algorithmToolVectorNoChargeInfo));
395 for (AlgorithmTool *
const pAlgorithmTool : algorithmToolVector)
398 for (AlgorithmTool *
const pAlgorithmTool : algorithmToolVectorNoChargeInfo)
403 for (AlgorithmTool *
const pAlgorithmTool : algorithmToolVector)
412 template <
typename T>
415 const float vx(vertex.GetX()), vy(vertex.GetY()), vz(vertex.GetZ());
Header file for the mva pfo characterisation algorithm class.
bool m_enableProbability
Whether to use probabilities instead of binary classification.
end
while True: pbar.update(maxval-len(onlies[E][S])) #print iS, "/", len(onlies[E][S]) found = False for...
std::string m_trainingOutputFile
The training output file.
static double CalculateProbability(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained mva to calculate a classification probability for an example.
unsigned int m_minPrimaryGoodViews
the minimum number of primary good views
static bool IsThreeD(const pandora::ParticleFlowObject *const pPfo)
Does Pfo contain 3D clusters.
Header file for the pfo helper class.
float m_fiducialMinZ
Fiducial volume minimum z.
std::unordered_map< const pandora::MCParticle *, pandora::CaloHitList > MCContributionMap
std::string m_caloHitListName
Name of input calo hit list.
bool m_selectInputHits
whether to select input hits
float m_fiducialMaxX
Fiducial volume maximum x.
MvaTypes::MvaFeatureVector MvaFeatureVector
std::pair< const pandora::MCParticle *, pandora::CaloHitList > MCParticleCaloHitListPair
LArMCParticleHelper::PrimaryParameters m_primaryParameters
The mc particle primary selection parameters.
static void GetClusters(const pandora::PfoList &pfoList, const pandora::HitType &hitType, pandora::ClusterList &clusterList)
Get a list of clusters of a particular hit type from a list of pfos.
MvaPfoCharacterisationAlgorithm class.
std::string m_mcParticleListName
Name of input MC particle list.
PfoCharacterisationFeatureTool::FeatureToolVector m_featureToolVectorThreeD
The feature tool map for 3D info.
unsigned int m_minCaloHitsCut
The minimum number of calo hits to qualify as a track.
bool m_testBeamMode
Whether the training set is from a test beam experiment.
unsigned int m_minPrimaryGoodHits
the minimum number of primary good Hits
T m_mvaNoChargeInfo
The mva for missing W view.
static void GetPfoToReconstructable2DHitsMap(const pandora::PfoList &pfoList, const MCContributionMap &selectedMCParticleToHitsMap, PfoContributionMap &pfoToReconstructable2DHitsMap, const bool foldBackHierarchy)
Get mapping from Pfo to reconstructable 2D hits (=good hits belonging to a selected reconstructable M...
std::unordered_map< const pandora::CaloHit *, const pandora::MCParticle * > CaloHitToMCMap
bool m_applyReconstructabilityChecks
Whether to apply reconstructability checks during training.
static void GetPfoMCParticleHitSharingMaps(const PfoContributionMap &pfoToReconstructable2DHitsMap, const MCContributionMapVector &selectedMCParticleToHitsMaps, PfoToMCParticleHitSharingMap &pfoToMCParticleHitSharingMap, MCParticleToPfoHitSharingMap &mcParticleToPfoHitSharingMap)
Get the mappings from Pfo -> pair (reconstructable MCparticles, number of reconstructable 2D hits sha...
static bool Classify(const MvaInterface &classifier, TLISTS &&...featureLists)
Use the trained classifier to predict the boolean class of an example.
std::map< const pandora::MCParticle *, PfoToSharedHitsVector > MCParticleToPfoHitSharingMap
bool m_applyFiducialCut
Whether to apply a fiducial volume cut during training.
bool m_foldBackHierarchy
whether to fold the hierarchy back to the primary (neutrino) or leading particles (test beam) ...
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 const pandora::MCParticle * GetMainMCParticle(const pandora::ParticleFlowObject *const pPfo)
Find the mc particle making the largest contribution to 2D clusters in a specified pfo...
unsigned int m_minHitsForGoodView
the minimum number of Hits for a good view
float m_maxPhotonPropagation
the maximum photon propagation length
static void SelectReconstructableMCParticles(const pandora::MCParticleList *pMCParticleList, const pandora::CaloHitList *pCaloHitList, const PrimaryParameters ¶meters, std::function< bool(const pandora::MCParticle *const)> fCriteria, MCContributionMap &selectedMCParticlesToHitsMap)
Select target, reconstructable mc particles that match given criteria.
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TLISTS &&...featureLists)
Produce a training example with the given features and result.
InitializedDouble class used to define mva features.
float m_fiducialMinY
Fiducial volume minimum y.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
static void GetMCParticleToCaloHitMatches(const pandora::CaloHitList *const pCaloHitList, const MCRelationMap &mcToTargetMCMap, CaloHitToMCMap &hitToMCMap, MCContributionMap &mcToTrueHitListMap)
Match calo hits to their parent particles.
static bool IsBeamParticle(const pandora::MCParticle *const pMCParticle)
Returns true if passed a primary beam MCParticle.
std::string m_mvaNameNoChargeInfo
The name of the mva to find for PFOs missing the W view, and thus charge info.
Header file for the lar two dimensional sliding fit result class.
ClusterCharacterisationFeatureTool::FeatureToolVector m_featureToolVector
The feature tool map.
bool PassesFiducialCut(const pandora::CartesianVector &vertex) const
Checks if the interaction vertex is within the fiducial volume.
float m_fiducialMaxY
Fiducial volume maximum y.
std::string m_mvaName
The name of the mva to find.
bool m_trainingSetMode
Whether to train.
static pandora::StatusCode AddFeatureToolToVector(pandora::AlgorithmTool *const pFeatureTool, MvaFeatureToolVector< Ts... > &featureToolVector)
Add a feature tool to a vector of feature tools.
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...
Header file for the file helper class.
static int max(int a, int b)
float m_minHitSharingFraction
the minimum Hit sharing fraction
std::vector< MCContributionMap > MCContributionMapVector
float m_minProbabilityCut
The minimum probability to label a cluster as track-like.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
std::string m_mvaFileName
The mva input file.
std::string m_mvaFileNameNoChargeInfo
The mva input file for PFOs missing the W view, and thus charge info.
virtual bool IsClearTrack(const pandora::ParticleFlowObject *const pPfo) const
Whether pfo is identified as a clear track.
std::vector< MvaFeatureTool< Ts... > * > FeatureToolVector
PfoCharacterisationFeatureTool::FeatureToolVector m_featureToolVectorNoChargeInfo
The feature tool map for missing W view.
float m_fiducialMinX
Fiducial volume minimum x.
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&...args)
Calculate the features in a given feature tool vector.
std::vector< MCParticleCaloHitListPair > MCParticleToSharedHitsVector
std::string m_filePathEnvironmentVariable
The environment variable providing a list of paths to mva files.
std::unordered_map< const pandora::ParticleFlowObject *, pandora::CaloHitList > PfoContributionMap
float m_fiducialMaxZ
Fiducial volume maximum z.
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.
static bool IsBeamNeutrinoFinalState(const pandora::MCParticle *const pMCParticle)
Returns true if passed a primary neutrino final state MCParticle.
std::unordered_map< const pandora::MCParticle *, const pandora::MCParticle * > MCRelationMap
std::map< const pandora::ParticleFlowObject *, MCParticleToSharedHitsVector > PfoToMCParticleHitSharingMap
bool m_useThreeDInformation
Whether to use 3D information.
QTextStream & endl(QTextStream &s)