forked from PandoraPFA/LArRecoND
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasterThreeDAlgorithm.cc
380 lines (312 loc) · 17.9 KB
/
MasterThreeDAlgorithm.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
* @file src/MasterThreeDAlgorithm.cc
*
* @brief Implementation of the 3D master algorithm class.
*
* $Log: $
*/
#include "Api/PandoraApi.h"
#include "Pandora/AlgorithmHeaders.h"
#include "LArNDContent.h"
#include "MasterThreeDAlgorithm.h"
#include "larpandoracontent/LArContent.h"
#include "larpandoracontent/LArHelpers/LArClusterHelper.h"
#include "larpandoracontent/LArHelpers/LArFileHelper.h"
#include "larpandoracontent/LArHelpers/LArMCParticleHelper.h"
#include "larpandoracontent/LArHelpers/LArPfoHelper.h"
#include "larpandoracontent/LArHelpers/LArStitchingHelper.h"
#include "larpandoracontent/LArObjects/LArCaloHit.h"
#include "larpandoracontent/LArObjects/LArMCParticle.h"
#include "larpandoracontent/LArPlugins/LArPseudoLayerPlugin.h"
#include "larpandoracontent/LArPlugins/LArRotationalTransformationPlugin.h"
#include "larpandoracontent/LArUtility/PfoMopUpBaseAlgorithm.h"
#ifdef LIBTORCH_DL
#include "larpandoradlcontent/LArDLContent.h"
#endif
using namespace pandora;
namespace lar_content
{
StatusCode MasterThreeDAlgorithm::Run()
{
std::cout << "Should run slicing? " << m_shouldRunSlicing << std::endl;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->Reset());
if (!m_workerInstancesInitialized)
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->InitializeWorkerInstances());
if (m_passMCParticlesToWorkerInstances)
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->CopyMCParticles());
PfoToFloatMap stitchedPfosToX0Map;
VolumeIdToHitListMap volumeIdToHitListMap;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->GetVolumeIdToHitListMap(volumeIdToHitListMap));
if (m_shouldRunAllHitsCosmicReco)
{
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RunCosmicRayReconstruction(volumeIdToHitListMap));
PfoToLArTPCMap pfoToLArTPCMap;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RecreateCosmicRayPfos(pfoToLArTPCMap));
if (m_shouldRunStitching)
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->StitchCosmicRayPfos(pfoToLArTPCMap, stitchedPfosToX0Map));
}
if (m_shouldRunCosmicHitRemoval)
{
PfoList clearCosmicRayPfos, ambiguousPfos;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->TagCosmicRayPfos(stitchedPfosToX0Map, clearCosmicRayPfos, ambiguousPfos));
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RunCosmicRayHitRemoval(ambiguousPfos));
}
SliceVector sliceVector;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RunSlicing(volumeIdToHitListMap, sliceVector));
if (m_shouldRunNeutrinoRecoOption || m_shouldRunCosmicRecoOption)
{
SliceHypotheses nuSliceHypotheses, crSliceHypotheses;
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RunSliceReconstruction(sliceVector, nuSliceHypotheses, crSliceHypotheses));
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->SelectBestSliceHypotheses(nuSliceHypotheses, crSliceHypotheses));
}
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode MasterThreeDAlgorithm::RunSlicing(const VolumeIdToHitListMap &volumeIdToHitListMap, SliceVector &sliceVector) const
{
std::cout << "There are " << volumeIdToHitListMap.size() << " volumes" << std::endl;
for (const VolumeIdToHitListMap::value_type &mapEntry : volumeIdToHitListMap)
{
std::cout << "- Volume has " << mapEntry.second.m_allHitList.size() << " hits" << std::endl;
for (const CaloHit *const pCaloHit : (m_shouldRemoveOutOfTimeHits ? mapEntry.second.m_truncatedHitList : mapEntry.second.m_allHitList))
{
if (!PandoraContentApi::IsAvailable(*this, pCaloHit))
continue;
if (m_shouldRunSlicing)
{
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->Copy(m_pSlicingWorkerInstance, pCaloHit));
}
else
{
if (sliceVector.empty())
sliceVector.push_back(CaloHitList());
sliceVector.back().push_back(pCaloHit);
}
}
}
if (m_shouldRunSlicing)
{
if (m_printOverallRecoStatus)
std::cout << "Running slicing worker instance" << std::endl;
const PfoList *pSlicePfos(nullptr);
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::ProcessEvent(*m_pSlicingWorkerInstance));
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::GetCurrentPfoList(*m_pSlicingWorkerInstance, pSlicePfos));
if (m_visualizeOverallRecoStatus)
{
PANDORA_MONITORING_API(VisualizeParticleFlowObjects(this->GetPandora(), pSlicePfos, "OnePfoPerSlice", BLUE));
PANDORA_MONITORING_API(ViewEvent(this->GetPandora()));
}
for (const Pfo *const pSlicePfo : *pSlicePfos)
{
sliceVector.push_back(CaloHitList());
LArPfoHelper::GetCaloHits(pSlicePfo, TPC_VIEW_U, sliceVector.back());
LArPfoHelper::GetCaloHits(pSlicePfo, TPC_VIEW_V, sliceVector.back());
LArPfoHelper::GetCaloHits(pSlicePfo, TPC_VIEW_W, sliceVector.back());
LArPfoHelper::GetCaloHits(pSlicePfo, TPC_3D, sliceVector.back());
}
}
if (m_printOverallRecoStatus)
std::cout << "Identified " << sliceVector.size() << " slice(s)" << std::endl;
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
const Pandora *MasterThreeDAlgorithm::CreateWorkerInstance(
const LArTPC &larTPC, const DetectorGapList &gapList, const std::string &settingsFile, const std::string &name) const
{
// The Pandora instance
const Pandora *const pPandora(new Pandora(name));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArContent::RegisterAlgorithms(*pPandora));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArContent::RegisterBasicPlugins(*pPandora));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArNDContent::RegisterAlgorithms(*pPandora));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::SetPseudoLayerPlugin(*pPandora, new lar_content::LArPseudoLayerPlugin));
PANDORA_THROW_RESULT_IF(
STATUS_CODE_SUCCESS, !=, PandoraApi::SetLArTransformationPlugin(*pPandora, new lar_content::LArRotationalTransformationPlugin));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RegisterCustomContent(pPandora));
MultiPandoraApi::AddDaughterPandoraInstance(&(this->GetPandora()), pPandora);
// The LArTPC
PandoraApi::Geometry::LArTPC::Parameters larTPCParameters;
larTPCParameters.m_larTPCVolumeId = larTPC.GetLArTPCVolumeId();
larTPCParameters.m_centerX = larTPC.GetCenterX();
larTPCParameters.m_centerY = larTPC.GetCenterY();
larTPCParameters.m_centerZ = larTPC.GetCenterZ();
larTPCParameters.m_widthX = larTPC.GetWidthX();
larTPCParameters.m_widthY = larTPC.GetWidthY();
larTPCParameters.m_widthZ = larTPC.GetWidthZ();
larTPCParameters.m_wirePitchU = larTPC.GetWirePitchU();
larTPCParameters.m_wirePitchV = larTPC.GetWirePitchV();
larTPCParameters.m_wirePitchW = larTPC.GetWirePitchW();
larTPCParameters.m_wireAngleU = larTPC.GetWireAngleU();
larTPCParameters.m_wireAngleV = larTPC.GetWireAngleV();
larTPCParameters.m_wireAngleW = larTPC.GetWireAngleW();
larTPCParameters.m_sigmaUVW = larTPC.GetSigmaUVW();
larTPCParameters.m_isDriftInPositiveX = larTPC.IsDriftInPositiveX();
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::Geometry::LArTPC::Create(*pPandora, larTPCParameters));
const float tpcMinX(larTPC.GetCenterX() - 0.5f * larTPC.GetWidthX()), tpcMaxX(larTPC.GetCenterX() + 0.5f * larTPC.GetWidthX());
// The Gaps
for (const DetectorGap *const pGap : gapList)
{
const LineGap *const pLineGap(dynamic_cast<const LineGap *>(pGap));
if (pLineGap && (((pLineGap->GetLineEndX() >= tpcMinX) && (pLineGap->GetLineEndX() <= tpcMaxX)) ||
((pLineGap->GetLineStartX() >= tpcMinX) && (pLineGap->GetLineStartX() <= tpcMaxX))))
{
PandoraApi::Geometry::LineGap::Parameters lineGapParameters;
const LineGapType lineGapType(pLineGap->GetLineGapType());
lineGapParameters.m_lineGapType = lineGapType;
lineGapParameters.m_lineStartX = pLineGap->GetLineStartX();
lineGapParameters.m_lineEndX = pLineGap->GetLineEndX();
if (m_fullWidthCRWorkerWireGaps &&
((lineGapType == TPC_WIRE_GAP_VIEW_U) || (lineGapType == TPC_WIRE_GAP_VIEW_V) || (lineGapType == TPC_WIRE_GAP_VIEW_W)))
{
lineGapParameters.m_lineStartX = -std::numeric_limits<float>::max();
lineGapParameters.m_lineEndX = std::numeric_limits<float>::max();
}
lineGapParameters.m_lineStartZ = pLineGap->GetLineStartZ();
lineGapParameters.m_lineEndZ = pLineGap->GetLineEndZ();
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::Geometry::LineGap::Create(*pPandora, lineGapParameters));
}
}
// Configuration
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::ReadSettings(*pPandora, settingsFile));
return pPandora;
}
//------------------------------------------------------------------------------------------------------------------------------------------
const Pandora *MasterThreeDAlgorithm::CreateWorkerInstance(
const LArTPCMap &larTPCMap, const DetectorGapList &gapList, const std::string &settingsFile, const std::string &name) const
{
if (larTPCMap.empty())
{
std::cout << "MasterThreeDAlgorithm::CreateWorkerInstance - no LArTPC details provided" << std::endl;
throw StatusCodeException(STATUS_CODE_NOT_INITIALIZED);
}
// The Pandora instance
const Pandora *const pPandora(new Pandora(name));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArContent::RegisterAlgorithms(*pPandora));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArContent::RegisterBasicPlugins(*pPandora));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArNDContent::RegisterAlgorithms(*pPandora));
// Deep Learning algorithms (e.g. vertexing)
#ifdef LIBTORCH_DL
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, LArDLContent::RegisterAlgorithms(*pPandora));
#endif
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::SetPseudoLayerPlugin(*pPandora, new lar_content::LArPseudoLayerPlugin));
PANDORA_THROW_RESULT_IF(
STATUS_CODE_SUCCESS, !=, PandoraApi::SetLArTransformationPlugin(*pPandora, new lar_content::LArRotationalTransformationPlugin));
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, this->RegisterCustomContent(pPandora));
MultiPandoraApi::AddDaughterPandoraInstance(&(this->GetPandora()), pPandora);
// The Parent LArTPC
const LArTPC *const pFirstLArTPC(larTPCMap.begin()->second);
float parentMinX(pFirstLArTPC->GetCenterX() - 0.5f * pFirstLArTPC->GetWidthX());
float parentMaxX(pFirstLArTPC->GetCenterX() + 0.5f * pFirstLArTPC->GetWidthX());
float parentMinY(pFirstLArTPC->GetCenterY() - 0.5f * pFirstLArTPC->GetWidthY());
float parentMaxY(pFirstLArTPC->GetCenterY() + 0.5f * pFirstLArTPC->GetWidthY());
float parentMinZ(pFirstLArTPC->GetCenterZ() - 0.5f * pFirstLArTPC->GetWidthZ());
float parentMaxZ(pFirstLArTPC->GetCenterZ() + 0.5f * pFirstLArTPC->GetWidthZ());
for (const LArTPCMap::value_type &mapEntry : larTPCMap)
{
const LArTPC *const pLArTPC(mapEntry.second);
parentMinX = std::min(parentMinX, pLArTPC->GetCenterX() - 0.5f * pLArTPC->GetWidthX());
parentMaxX = std::max(parentMaxX, pLArTPC->GetCenterX() + 0.5f * pLArTPC->GetWidthX());
parentMinY = std::min(parentMinY, pLArTPC->GetCenterY() - 0.5f * pLArTPC->GetWidthY());
parentMaxY = std::max(parentMaxY, pLArTPC->GetCenterY() + 0.5f * pLArTPC->GetWidthY());
parentMinZ = std::min(parentMinZ, pLArTPC->GetCenterZ() - 0.5f * pLArTPC->GetWidthZ());
parentMaxZ = std::max(parentMaxZ, pLArTPC->GetCenterZ() + 0.5f * pLArTPC->GetWidthZ());
}
PandoraApi::Geometry::LArTPC::Parameters larTPCParameters;
larTPCParameters.m_larTPCVolumeId = 0;
larTPCParameters.m_centerX = 0.5f * (parentMaxX + parentMinX);
larTPCParameters.m_centerY = 0.5f * (parentMaxY + parentMinY);
larTPCParameters.m_centerZ = 0.5f * (parentMaxZ + parentMinZ);
larTPCParameters.m_widthX = parentMaxX - parentMinX;
larTPCParameters.m_widthY = parentMaxY - parentMinY;
larTPCParameters.m_widthZ = parentMaxZ - parentMinZ;
larTPCParameters.m_wirePitchU = std::max(pFirstLArTPC->GetWirePitchU(), pFirstLArTPC->GetWirePitchV());
larTPCParameters.m_wirePitchV = std::max(pFirstLArTPC->GetWirePitchU(), pFirstLArTPC->GetWirePitchV());
larTPCParameters.m_wirePitchW = pFirstLArTPC->GetWirePitchW();
larTPCParameters.m_wireAngleU = pFirstLArTPC->GetWireAngleU();
larTPCParameters.m_wireAngleV = pFirstLArTPC->GetWireAngleV();
larTPCParameters.m_wireAngleW = pFirstLArTPC->GetWireAngleW();
larTPCParameters.m_sigmaUVW = pFirstLArTPC->GetSigmaUVW();
larTPCParameters.m_isDriftInPositiveX = pFirstLArTPC->IsDriftInPositiveX();
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::Geometry::LArTPC::Create(*pPandora, larTPCParameters));
// The Gaps
for (const DetectorGap *const pGap : gapList)
{
const LineGap *const pLineGap(dynamic_cast<const LineGap *>(pGap));
if (pLineGap)
{
PandoraApi::Geometry::LineGap::Parameters lineGapParameters;
lineGapParameters.m_lineGapType = pLineGap->GetLineGapType();
lineGapParameters.m_lineStartX = pLineGap->GetLineStartX();
lineGapParameters.m_lineEndX = pLineGap->GetLineEndX();
lineGapParameters.m_lineStartZ = pLineGap->GetLineStartZ();
lineGapParameters.m_lineEndZ = pLineGap->GetLineEndZ();
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::Geometry::LineGap::Create(*pPandora, lineGapParameters));
}
}
// Configuration
PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraApi::ReadSettings(*pPandora, settingsFile));
return pPandora;
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode MasterThreeDAlgorithm::InitializeWorkerInstances()
{
// ATTN Used to be in the regular Initialize callback, but detector gap list cannot be extracted in client app before the first event
if (m_workerInstancesInitialized)
return STATUS_CODE_ALREADY_INITIALIZED;
try
{
const LArTPCMap &larTPCMap(this->GetPandora().GetGeometry()->GetLArTPCMap());
const DetectorGapList &gapList(this->GetPandora().GetGeometry()->GetDetectorGapList());
for (const LArTPCMap::value_type &mapEntry : larTPCMap)
{
const unsigned int volumeId(mapEntry.second->GetLArTPCVolumeId());
m_crWorkerInstances.push_back(
this->CreateWorkerInstance(*(mapEntry.second), gapList, m_crSettingsFile, "CRWorkerInstance" + std::to_string(volumeId)));
}
if (m_shouldRunSlicing)
m_pSlicingWorkerInstance = this->CreateWorkerInstance(larTPCMap, gapList, m_slicingSettingsFile, "SlicingWorker");
if (m_shouldRunNeutrinoRecoOption)
m_pSliceNuWorkerInstance = this->CreateWorkerInstance(larTPCMap, gapList, m_nuSettingsFile, "SliceNuWorker");
if (m_shouldRunCosmicRecoOption)
m_pSliceCRWorkerInstance = this->CreateWorkerInstance(larTPCMap, gapList, m_crSettingsFile, "SliceCRWorker");
}
catch (const StatusCodeException &statusCodeException)
{
std::cout << "MasterAlgorithm: Exception during initialization of worker instances " << statusCodeException.ToString() << std::endl;
return statusCodeException.GetStatusCode();
}
m_workerInstancesInitialized = true;
return STATUS_CODE_SUCCESS;
}
//------------------------------------------------------------------------------------------------------------------------------------------
StatusCode MasterThreeDAlgorithm::GetVolumeIdToHitListMap(VolumeIdToHitListMap &volumeIdToHitListMap) const
{
const LArTPCMap &larTPCMap(this->GetPandora().GetGeometry()->GetLArTPCMap());
const unsigned int nLArTPCs(larTPCMap.size());
const CaloHitList *pCaloHitList(nullptr);
PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*this, m_inputHitListName, pCaloHitList));
for (const CaloHit *const pCaloHit : *pCaloHitList)
{
const LArCaloHit *const pLArCaloHit(dynamic_cast<const LArCaloHit *>(pCaloHit));
if (!pLArCaloHit && (1 != nLArTPCs))
return STATUS_CODE_INVALID_PARAMETER;
const unsigned int volumeId(pLArCaloHit ? pLArCaloHit->GetLArTPCVolumeId() : 0);
const LArTPC *const pLArTPC(larTPCMap.at(volumeId));
LArTPCHitList &larTPCHitList(volumeIdToHitListMap[volumeId]);
larTPCHitList.m_allHitList.push_back(pCaloHit);
if (((pCaloHit->GetPositionVector().GetX() >= (pLArTPC->GetCenterX() - 0.5f * pLArTPC->GetWidthX())) &&
(pCaloHit->GetPositionVector().GetX() <= (pLArTPC->GetCenterX() + 0.5f * pLArTPC->GetWidthX()))))
{
larTPCHitList.m_truncatedHitList.push_back(pCaloHit);
}
else
std::cout << "Hit of type " << pCaloHit->GetHitType() << " outside TPC " << volumeId << "? "
<< pCaloHit->GetPositionVector().GetX() << ", " << pLArTPC->GetCenterX() - 0.5f * pLArTPC->GetWidthX() << ", "
<< pLArTPC->GetCenterX() + 0.5f * pLArTPC->GetWidthX() << std::endl;
}
return STATUS_CODE_SUCCESS;
}
StatusCode MasterThreeDAlgorithm::ReadSettings(const pandora::TiXmlHandle xmlHandle)
{
return MasterAlgorithm::ReadSettings(xmlHandle);
}
} // namespace lar_content