25 m_persistFeatures(false),
26 m_trainingSetMode(false),
27 m_testBeamMode(false),
28 m_enableProbability(true),
29 m_useThreeDInformation(true),
30 m_minProbabilityCut(0.5f),
32 m_applyFiducialCut(false),
33 m_fiducialMinX(-std::numeric_limits<float>::max()),
34 m_fiducialMaxX(std::numeric_limits<float>::max()),
35 m_fiducialMinY(-std::numeric_limits<float>::max()),
36 m_fiducialMaxY(std::numeric_limits<float>::max()),
37 m_fiducialMinZ(-std::numeric_limits<float>::max()),
38 m_fiducialMaxZ(std::numeric_limits<float>::max()),
39 m_applyReconstructabilityChecks(false),
40 m_filePathEnvironmentVariable(
"FW_SEARCH_PATH")
89 if (m_enableProbability)
92 metadata.m_propertiesToAdd[
"TrackScore"] = -1.f;
103 const StringVector chosenFeatureToolOrder(wClusterList.empty() ? m_algorithmToolNamesNoChargeInfo : m_algorithmToolNames);
108 for (
auto const &[featureKey, featureValue] : featureMap)
112 if (!featureValue.IsInitialized())
114 if (m_enableProbability)
117 metadata.m_propertiesToAdd[
"TrackScore"] = -1.f;
124 if (m_trainingSetMode && m_applyReconstructabilityChecks)
145 PfoList(1, pPfo), mcParticlesToGoodHitsMaps, pfoToReconstructable2DHitsMap, m_primaryParameters.m_foldBackHierarchy);
146 if (pfoToReconstructable2DHitsMap.empty())
152 pfoToReconstructable2DHitsMap, mcParticlesToGoodHitsMaps, pfoToMCParticleHitSharingMap, mcParticleToPfoHitSharingMap);
153 if (pfoToMCParticleHitSharingMap.empty())
156 unsigned int nHitsInBestMCParticleTotal(0);
157 unsigned int nHitsSharedWithBestMCParticleTotal(0);
158 int bestMCParticlePdgCode(0);
160 float hitsShower(0), hitsTrack(0);
166 const CaloHitList &allMCHits(targetMCParticleToHitsMap.at(pAssociatedMCParticle));
167 const CaloHitList &associatedMCHits(mcParticleCaloHitListPair.second);
170 hitsShower += associatedMCHits.size();
172 hitsTrack += associatedMCHits.size();
174 if (associatedMCHits.size() > nHitsSharedWithBestMCParticleTotal)
176 nHitsSharedWithBestMCParticleTotal = associatedMCHits.size();
177 nHitsInBestMCParticleTotal = allMCHits.size();
178 bestMCParticlePdgCode = pAssociatedMCParticle->
GetParticleId();
179 threeDVertexPosition = pAssociatedMCParticle->
GetVertex();
183 const float trackShowerHitsRatio((hitsTrack + hitsShower) > 0 ? hitsTrack / (hitsTrack + hitsShower) : 0.f);
184 const bool isTrueTrack(trackShowerHitsRatio >= 0.5);
186 const int nHitsInPfoTotal(pfoToReconstructable2DHitsMap.at(pPfo).size());
187 const float purity((nHitsInPfoTotal > 0) ? nHitsSharedWithBestMCParticleTotal /
static_cast<float>(nHitsInPfoTotal) : 0.f);
188 const float completeness(
189 (nHitsInBestMCParticleTotal > 0) ? nHitsSharedWithBestMCParticleTotal /
static_cast<float>(nHitsInBestMCParticleTotal) : 0.f);
198 checkHitListAll.splice(checkHitListAll.end(), checkHitListW);
199 checkHitListAll.splice(checkHitListAll.end(), checkHitListU);
200 checkHitListAll.splice(checkHitListAll.end(), checkHitListV);
209 unsigned int showerCount(0), allCount(0);
210 for (
const CaloHit *pHit : checkHitListAll)
212 if (hitToMCMap.find(pHit) != hitToMCMap.end())
214 const MCParticle *pHitMCParticle(hitToMCMap.at(pHit));
223 const float showerProbability(showerCount /
static_cast<float>(allCount));
224 const bool mischaracterisedPfo((showerProbability < 0.5f && !isTrueTrack) || (showerProbability > 0.5 && isTrueTrack) ?
true :
false);
225 const bool isMainMCParticleSet(bestMCParticlePdgCode != 0);
227 if (isMainMCParticleSet)
229 if (completeness >= 0.f && purity >= 0.f && !mischaracterisedPfo && (!m_applyFiducialCut || this->PassesFiducialCut(threeDVertexPosition)))
231 std::string outputFile(m_trainingOutputFile);
232 const std::string end = ((wClusterList.empty()) ?
"noChargeInfo.txt" :
".txt");
233 outputFile.append(end);
240 else if (m_trainingSetMode)
242 bool isTrueTrack(
false);
243 bool isMainMCParticleSet(
false);
255 if (isMainMCParticleSet)
257 std::string outputFile(m_trainingOutputFile);
258 outputFile.append(wClusterList.empty() ?
"noChargeInfo.txt" :
".txt");
266 if (!m_enableProbability)
268 return LArMvaHelper::Classify((wClusterList.empty() ? m_mvaNoChargeInfo : m_mva), featureOrder, featureMap);
274 metadata.m_propertiesToAdd[
"TrackScore"] = score;
275 if (m_persistFeatures)
277 for (
auto const &[name, value] : featureMap)
279 metadata.m_propertiesToAdd[name] = value.Get();
283 return (m_minProbabilityCut <= score);
293 XmlHelper::ReadValue(xmlHandle,
"MinPrimaryGoodHits", m_primaryParameters.m_minPrimaryGoodHits));
296 XmlHelper::ReadValue(xmlHandle,
"MinHitsForGoodView", m_primaryParameters.m_minHitsForGoodView));
299 XmlHelper::ReadValue(xmlHandle,
"MinPrimaryGoodViews", m_primaryParameters.m_minPrimaryGoodViews));
305 XmlHelper::ReadValue(xmlHandle,
"MinHitSharingFraction", m_primaryParameters.m_minHitSharingFraction));
308 XmlHelper::ReadValue(xmlHandle,
"MaxPhotonPropagation", m_primaryParameters.m_maxPhotonPropagation));
320 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"UseThreeDInformation", m_useThreeDInformation));
334 if (m_useThreeDInformation)
344 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"BdtNameNoChargeInfo", m_mvaNameNoChargeInfo));
346 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"SvmNameNoChargeInfo", m_mvaNameNoChargeInfo));
348 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"MvaNameNoChargeInfo", m_mvaNameNoChargeInfo));
352 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"EnableProbability", m_enableProbability));
355 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"MinProbabilityCut", m_minProbabilityCut));
357 if (m_trainingSetMode)
364 STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
XmlHelper::ReadValue(xmlHandle,
"ApplyFiducialCut", m_applyFiducialCut));
365 if (m_applyFiducialCut)
375 XmlHelper::ReadValue(xmlHandle,
"ApplyReconstructabilityChecks", m_applyReconstructabilityChecks));
379 if (m_mvaFileName.empty() || m_mvaName.empty())
381 std::cout <<
"MvaPfoCharacterisationAlgorithm: MvaFileName and MvaName must be set if in classification mode " << std::endl;
382 return STATUS_CODE_INVALID_PARAMETER;
386 m_mva.Initialize(fullMvaFileName, m_mvaName);
388 if (m_useThreeDInformation)
390 if (m_mvaFileNameNoChargeInfo.empty() || m_mvaNameNoChargeInfo.empty())
392 std::cout <<
"MvaPfoCharacterisationAlgorithm: MvaFileNameNoChargeInfo and MvaNameNoChargeInfo must be set if in classification mode for no charge info in 3D mode "
394 return STATUS_CODE_INVALID_PARAMETER;
397 m_mvaNoChargeInfo.Initialize(fullMvaFileNameNoChargeInfo, m_mvaNameNoChargeInfo);
405 if (m_useThreeDInformation)
411 *
this, xmlHandle,
"FeatureToolsNoChargeInfo", m_algorithmToolNamesNoChargeInfo, algorithmToolMapNoChargeInfo));
413 for (
auto const &[pAlgorithmToolName, pAlgorithmTool] : algorithmToolMap)
417 for (
auto const &[pAlgorithmToolName, pAlgorithmTool] : algorithmToolMapNoChargeInfo)
423 for (
auto const &[pAlgorithmToolName, pAlgorithmTool] : algorithmToolMap)
static pandora::StatusCode GetList(const pandora::Algorithm &algorithm, const std::string &listName, const T *&pT)
Get a named list.
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...
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 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 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...
static void GetMCParticleToCaloHitMatches(const pandora::CaloHitList *const pCaloHitList, const MCRelationMap &mcToTargetMCMap, CaloHitToMCMap &hitToMCMap, MCContributionMap &mcToTrueHitListMap)
Match calo hits to their parent particles.
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.
static pandora::StatusCode ProcessAlgorithmToolListToMap(const pandora::Algorithm &algorithm, const pandora::TiXmlHandle &xmlHandle, const std::string &listName, pandora::StringVector &algorithToolNameVector, AlgorithmToolMap &algorithmToolMap)
Process a list of algorithms tools in an xml file, using a map. Idea is for this to go to XmlHelper i...
static double CalculateProbability(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained mva to calculate a classification probability for an example.
static bool Classify(const MvaInterface &classifier, TCONTAINER &&featureContainer)
Use the trained classifier to predict the boolean class of an example.
static MvaFeatureVector CalculateFeatures(const MvaFeatureToolVector< Ts... > &featureToolVector, TARGS &&... args)
Calculate the features in a given feature tool vector.
static pandora::StatusCode AddFeatureToolToMap(pandora::AlgorithmTool *const pFeatureTool, std::string pFeatureToolName, MvaFeatureToolMap< Ts... > &featureToolMap)
Add a feature tool to a map of feature tools.
static pandora::StatusCode ProduceTrainingExample(const std::string &trainingOutputFile, const bool result, TCONTAINER &&featureContainer)
Produce a training example with the given features and result.
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.
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 const MCParticle * GetMainMCParticle(const T *const pT)
Find the mc particle making the largest contribution to a specified calo hit, track or cluster.