forked from kvtsang/Supera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuperaMCParticle.cxx
444 lines (397 loc) · 18.4 KB
/
SuperaMCParticle.cxx
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#ifndef __SUPERAMCPARTICLE_CXX__
#define __SUPERAMCPARTICLE_CXX__
#include "SuperaMCParticle.h"
#include "ImageMetaMakerFactory.h"
#include "PulledPork3DSlicer.h"
#include "Voxel3DSlicer.h"
#include "larcv/core/DataFormat/EventParticle.h"
#include "LAr2Image.h"
namespace larcv {
static SuperaMCParticleProcessFactory __global_SuperaMCParticleProcessFactory__;
SuperaMCParticle::SuperaMCParticle(const std::string name)
: SuperaBase(name)
{}
/*
const std::vector<std::pair<size_t, size_t> >& SuperaMCParticle::Particle2MCNode(int part_index) const
{
if (part_index >= (int)(_part2mcnode_vv.size()))
throw larbys("Invalid Particle index requested");
return _part2mcnode_vv[part_index];
}
*/
void SuperaMCParticle::configure(const PSet& cfg)
{
SuperaBase::configure(cfg);
supera::ParamsParticle::configure(cfg);
supera::ImageMetaMaker::configure(cfg);
/*
_store_part = cfg.get<bool>("StoreParticle", true);
_store_g4_secondary_part = cfg.get<bool>("StoreG4SecondaryParticle", true);
_store_g4_primary_part = cfg.get<bool>("StoreG4PrimaryParticle", true);
*/
_mcpt.configure(cfg.get<supera::Config_t>("MCParticleTree"));
_mcpart_maker.configure(cfg.get<supera::Config_t>("MCParticleMaker"));
_pass_origin = cfg.get<unsigned short>("Origin");
_mcpt.FilterOrigin(_pass_origin);
_filter_pdg = cfg.get<std::vector<int> >("FilterTargetPDG");
_filter_min_einit = cfg.get<std::vector<double> >("FilterTargetInitEMin");
_filter_min_edep = cfg.get<std::vector<double> >("FilterTargetDepEMin");
if (_filter_pdg.size() != _filter_min_einit.size()) {
LARCV_CRITICAL() << "FilterTargetPDG and FilterTargetInitEMin not the same length!" << std::endl;
throw larbys();
}
if (_filter_pdg.size() != _filter_min_edep.size()) {
LARCV_CRITICAL() << "FilterTargetPDG and FilterTargetDepEMin not the same length!" << std::endl;
throw larbys();
}
_shower_min_einit = cfg.get<double>("ShowerInitEMin");
_shower_min_edep = cfg.get<double>("ShowerDepEMin");
_track_min_einit = cfg.get<double>("TrackInitEMin");
_track_min_edep = cfg.get<double>("TrackDepEMin");
/*
_filter_min_cols = cfg.get<size_t>("FilterParticleMinCols");
_filter_min_rows = cfg.get<size_t>("FilterParticleMinRows");
*/
}
void SuperaMCParticle::initialize()
{
SuperaBase::initialize();
}
bool SuperaMCParticle::process(IOManager& mgr)
{
SuperaBase::process(mgr);
if(supera::PulledPork3DSlicer::Is(supera::ImageMetaMaker::MetaMakerPtr())) {
auto ptr = (supera::PulledPork3DSlicer*)(supera::ImageMetaMaker::MetaMakerPtr());
ptr->ClearEventData();
ptr->AddConstraint(LArData<supera::LArMCTruth_t>());
ptr->GenerateMeta(LArData<supera::LArSimCh_t>(),TimeOffset());
}else if(supera::Voxel3DSlicer::Is(supera::ImageMetaMaker::MetaMakerPtr())) {
auto ptr = (supera::Voxel3DSlicer*)(supera::ImageMetaMaker::MetaMakerPtr());
ptr->ClearEventData();
ptr->AddConstraint(LArData<supera::LArMCTruth_t>());
ptr->GenerateMeta(LArData<supera::LArSimCh_t>(),TimeOffset());
}
bool use_meta3d=Meta3D().valid();
LARCV_INFO() << "Using " << (use_meta3d ? "3D" : "2D") << " meta..." << std::endl;
auto ev_part = (EventParticle*)(mgr.get_data("particle", OutParticleLabel()));
if (!ev_part) {
LARCV_CRITICAL() << "Output part could not be created!" << std::endl;
throw larbys();
}
if (!(ev_part->as_vector().empty())) {
LARCV_CRITICAL() << "Output part array not empty!" << std::endl;
throw larbys();
}
LARCV_INFO() << "Running MCParticleTree::Register" << std::endl;
_mcpt.Register(LArData<supera::LArMCTrack_t>(), LArData<supera::LArMCShower_t>());
//_mcpt.dump();
auto primary_v = _mcpt.PrimaryArray();
LARCV_INFO() << "Found " << primary_v.size() << " primary particles" << std::endl;
std::vector<supera::LArSimCh_t> empty_sch_v;
_part_v.clear();
//_part2mcnode_vv.clear();
for (size_t primary_idx = 0; primary_idx < primary_v.size(); ++primary_idx) {
auto& primary = primary_v[primary_idx];
// filter out primary of certain origin, if specified
if (_pass_origin && primary.origin != _pass_origin) {
LARCV_INFO() << "Skipping a primary " << primary_idx
<< " (origin type " << primary.origin
<< " PDG " << primary.pdg
<< ") with " << primary.daughter_v.size() << " children"
<< std::endl;
continue;
}
if (!FilterNode(primary)) {
LARCV_INFO() << "Skipping a primary (TrackID " << primary.track_id
<< " Origin " << primary.origin
<< " PDG " << primary.pdg
<< ") due to FilterNode()" << std::endl;
continue;
}
// at this point we decided to store primary. create one
_part_v.resize(_part_v.size() + 1);
auto& pri_part = _part_v.back();
if(use_meta3d) {
if (LArDataLabel(supera::LArDataType_t::kLArSimCh_t).empty())
pri_part = MakeParticle(primary, empty_sch_v, Meta3D());
else
pri_part = MakeParticle(primary, LArData<supera::LArSimCh_t>(), Meta3D());
}else{
if (LArDataLabel(supera::LArDataType_t::kLArSimCh_t).empty())
pri_part = MakeParticle(primary, empty_sch_v);
else
pri_part = MakeParticle(primary, LArData<supera::LArSimCh_t>());
}
LARCV_INFO() << "Analyzing primary " << primary_idx << " PDG " << pri_part.pdg_code()
<< " Origin " << primary.origin
<< " PDG << " << primary.pdg
<< " with " << primary.daughter_v.size() << " children" << std::endl;
std::vector<larcv::Particle> sec_part_v;
//std::vector<size_t> sec_idx_v;
for (size_t daughter_idx = 0; daughter_idx < primary.daughter_v.size(); ++daughter_idx) {
auto const& daughter = primary.daughter_v[daughter_idx];
if (_pass_origin && daughter.origin != _pass_origin) {
LARCV_INFO() << "Skipping a daughter " << daughter_idx
<< " (origin type " << daughter.origin
<< " PDG " << daughter.pdg
<< ")"
<< std::endl;
continue;
}
if (!FilterNode(daughter)) {
LARCV_INFO() << "Skipping a daughter " << daughter_idx
<< " (TrackID " << daughter.track_id
<< " Origin " << daughter.origin
<< " PDG " << daughter.pdg
<< ") due to FilterNode()" << std::endl;
continue;
}
larcv::Particle sec_part;
try {
if(use_meta3d) {
if (LArDataLabel(supera::LArDataType_t::kLArSimCh_t).empty())
sec_part = MakeParticle(daughter, empty_sch_v, Meta3D());
else
sec_part = MakeParticle(daughter, LArData<supera::LArSimCh_t>(), Meta3D());
}else{
if (LArDataLabel(supera::LArDataType_t::kLArSimCh_t).empty())
sec_part = MakeParticle(daughter, empty_sch_v);
else
sec_part = MakeParticle(daughter, LArData<supera::LArSimCh_t>());
}
} catch (const larcv::larbys& err) {
LARCV_NORMAL() << "Skipping a secondary (PDG,TrackID) = ("
<< daughter.pdg << "," << daughter.track_id << ") as it could not be turned into Particle" << std::endl;
continue;
}
LARCV_INFO() << "Registering a daughter " << daughter_idx
<< " (TrackID " << daughter.track_id
<< " Origin " << daughter.origin
<< " PDG " << sec_part.pdg_code() << ")" << std::endl;
sec_part.mct_index(pri_part.mct_index());
pri_part.energy_deposit(pri_part.energy_deposit() + sec_part.energy_deposit());
//sec_part_v.emplace_back(std::move(sec_part));
//sec_idx_v.push_back(daughter_idx);
_part_v.emplace_back(std::move(sec_part));
}
/*
std::vector<std::pair<size_t, size_t> > part2mcnode_v;
part2mcnode_v.clear();
//
// Register primary Particle
//
LARCV_INFO() << "Storing primary Particle (PDG " << primary.part.PdgCode()
<< " Shape " << primary.part.Shape()
<< " MCTIndex " << primary.part.mct_index()
<< " MCSTIndex) " << primary.part.mcst_index()
<< std::endl;
_part_v.push_back(primary.part);
// Record incorporated mcnode
part2mcnode_v.emplace_back(primary_idx, larcv::kINVALID_SIZE);
for (size_t daughter_idx = 0; daughter_idx < sec_part_v.size(); ++daughter_idx) {
auto const& secondary_idx = sec_idx_v[daughter_idx];
auto const& secondary_part = sec_part_v[daughter_idx];
if ( (_store_g4_primary_part && secondary_part.TrackID() == secondary_part.ParentTrackID())
||
(_store_g4_secondary_part && secondary_part.TrackID() != secondary_part.ParentTrackID())
)
part2mcnode_v.emplace_back(primary_idx, secondary_idx);
}
for (size_t daughter_idx = 0; daughter_idx < sec_part_v.size(); ++daughter_idx) {
auto const& daughter_part = sec_part_v[daughter_idx];
LARCV_INFO() << " Associated secondary (PDG " << daughter_part.PdgCode()
<< " Shape " << daughter_part.Shape()
<< " MCTIndex " << daughter_part.mct_index()
<< " MCSTIndex) " << daughter_part.mcst_index()
<< std::endl;
}
_part2mcnode_vv.emplace_back(std::move(part2mcnode_v));
//
// Register secondary Particle
//
part2mcnode_v.clear();
for (size_t daughter_idx = 0; daughter_idx < sec_part_v.size(); ++daughter_idx) {
auto const& secondary_idx = sec_idx_v[daughter_idx];
auto& secondary_part = sec_part_v[daughter_idx];
if ( (_store_g4_primary_part && secondary_part.TrackID() == secondary_part.ParentTrackID())
||
(_store_g4_secondary_part && secondary_part.TrackID() != secondary_part.ParentTrackID())
)
{
LARCV_INFO() << "Storing secondary Particle (PDG " << secondary_part.PdgCode()
<< " Shape " << secondary_part.Shape()
<< " MCTIndex " << secondary_part.mct_index()
<< " MCSTIndex) " << secondary_part.mcst_index()
<< std::endl;
part2mcnode_v.clear();
part2mcnode_v.emplace_back(primary_idx, secondary_idx);
_part_v.emplace_back(std::move(secondary_part));
_part2mcnode_vv.emplace_back(std::move(part2mcnode_v));
}
}
*/
}
/*
if (_store_part)
ev_part->Emplace(std::move(_part_v));
*/
ev_part->emplace(std::move(_part_v));
return true;
}
bool SuperaMCParticle::FilterNode(const supera::MCNode & node) const
{
if (node.source_type == supera::MCNode::SourceType_t::kMCTrack) {
auto const& mctrack = LArData<supera::LArMCTrack_t>()[node.source_index];
LARCV_DEBUG() << "MCTrack InitE " << mctrack.Start().E() << " ... DepE "
<< (mctrack.size() > 1 ? mctrack.front().E() - mctrack.back().E() : 0) << std::endl;
if (mctrack.Start().E() < _track_min_einit) return false;
if (_track_min_edep > 0) {
if (mctrack.size() < 2) return false;
if ( (mctrack.front().E() - mctrack.back().E()) < _track_min_edep ) return false;
}
for (size_t filter_idx = 0; filter_idx < _filter_pdg.size(); ++filter_idx) {
auto const& pdg = _filter_pdg[filter_idx];
if (pdg != mctrack.PdgCode()) continue;
auto const& filter_min_einit = _filter_min_einit[filter_idx];
if (mctrack.Start().E() < filter_min_einit) return false;
auto const& filter_min_edep = _filter_min_edep[filter_idx];
if (filter_min_edep > 0) {
if (mctrack.size() < 2) return false;
if ( (mctrack.front().E() - mctrack.back().E()) < filter_min_edep ) return false;
}
}
}
else if (node.source_type == supera::MCNode::SourceType_t::kMCShower) {
auto const& mcshower = LArData<supera::LArMCShower_t>()[node.source_index];
LARCV_DEBUG() << "MCShower InitE " << mcshower.Start().E() << " ... DepE "
<< mcshower.DetProfile().E() << std::endl;
if (mcshower.Start().E() < _shower_min_einit) return false;
if (mcshower.DetProfile().E() < _shower_min_edep) return false;
for (size_t filter_idx = 0; filter_idx < _filter_pdg.size(); ++filter_idx) {
auto const& pdg = _filter_pdg[filter_idx];
if (pdg != mcshower.PdgCode()) continue;
auto const& filter_min_einit = _filter_min_einit[filter_idx];
if (mcshower.Start().E() < filter_min_einit) return false;
auto const& filter_min_edep = _filter_min_edep[filter_idx];
if ( mcshower.DetProfile().E() < filter_min_edep) return false;
}
}
return true;
}
larcv::Particle SuperaMCParticle::MakeParticle(const supera::MCNode & node,
const std::vector<supera::LArSimCh_t>& sch_v) const
{
larcv::Particle res;
if (node.source_type == supera::MCNode::SourceType_t::kMCTrack) {
auto const& mctrack = LArData<supera::LArMCTrack_t>().at(node.source_index);
res = _mcpart_maker.MakeParticle(mctrack, sch_v, TimeOffset());
res.mcst_index(node.source_index);
}
else if (node.source_type == supera::MCNode::SourceType_t::kMCShower) {
auto const& mcshower = LArData<supera::LArMCShower_t>().at(node.source_index);
res = _mcpart_maker.MakeParticle(mcshower, sch_v, TimeOffset());
res.mcst_index(node.source_index);
} else
throw larbys("Unexpected SourceType_t!");
// format Particle
/*
std::vector<larcv::BBox2D> bb_v;
for (size_t plane = 0; plane < res.boundingbox_2d().size(); ++plane) {
auto const& part_meta = res.boundingbox_2d().at(plane);
auto const& event_meta = Meta().at(plane);
bb_v.push_back(FormatMeta(part_meta, event_meta));
}
res.boundingbox_2d(bb_v);
*/
return res;
}
larcv::Particle SuperaMCParticle::MakeParticle(const supera::MCNode & node,
const std::vector<supera::LArSimCh_t>& sch_v,
const Voxel3DMeta& meta3d) const
{
larcv::Particle res;
if (node.source_type == supera::MCNode::SourceType_t::kMCTrack) {
auto const& mctrack = LArData<supera::LArMCTrack_t>().at(node.source_index);
res = _mcpart_maker.MakeParticle(mctrack, sch_v, TimeOffset(), meta3d);
res.mcst_index(node.source_index);
}
else if (node.source_type == supera::MCNode::SourceType_t::kMCShower) {
auto const& mcshower = LArData<supera::LArMCShower_t>().at(node.source_index);
res = _mcpart_maker.MakeParticle(mcshower, sch_v, TimeOffset(), meta3d);
res.mcst_index(node.source_index);
} else
throw larbys("Unexpected SourceType_t!");
return res;
}
/*
larcv::ImageMeta SuperaMCParticle::FormatMeta(const larcv::ImageMeta & part_image,
const larcv::ImageMeta & event_image) const
{
LARCV_DEBUG() << "Before format " << part_image.dump();
const size_t modular_row = event_image.height() / event_image.rows();
const size_t modular_col = event_image.width() / event_image.cols();
if (event_image.rows() < modular_row || event_image.cols() < modular_col) {
LARCV_ERROR() << "Event image too small to format Particle!" << std::endl;
throw larbys();
}
double min_x = (part_image.min_x() < event_image.min_x() ? event_image.min_x() : part_image.min_x() );
double max_y = (part_image.max_y() > event_image.max_y() ? event_image.max_y() : part_image.max_y() );
double width = (part_image.width() + min_x > event_image.max_x() ? event_image.max_x() - min_x : part_image.width());
double height = (max_y - part_image.height() < event_image.min_y() ? max_y - event_image.min_y() : part_image.height());
size_t rows = height / part_image.pixel_height();
size_t cols = width / part_image.pixel_width();
if (modular_col > 1 && cols % modular_col) {
int npixels = (modular_col - (cols % modular_col));
if (event_image.width() < (width + npixels * part_image.pixel_width())) npixels -= modular_col;
cols += npixels;
width += part_image.pixel_width() * npixels;
if (npixels > 0) {
// If expanded, make sure it won't go across event_image boundary
if ( (min_x + width) > event_image.max_x() ) {
LARCV_DEBUG() << "X: " << min_x << " => " << min_x + width
<< " exceeds event boundary " << event_image.max_x() << std::endl;
min_x = event_image.max_x() - width;
} else if (min_x < event_image.min_x()) {
LARCV_DEBUG() << "X: " << min_x << " => " << min_x + width
<< " exceeds event boundary " << event_image.min_x() << std::endl;
min_x = event_image.min_x();
}
}
}
if (modular_row > 1 && rows % modular_row) {
int npixels = (modular_row - (rows % modular_row));
if (event_image.height() < (height + npixels * part_image.pixel_height())) npixels -= modular_row;
rows += npixels;
height += part_image.pixel_height() * npixels;
if (npixels > 0) {
// If expanded, make sure it won't go across event_image boundary
if ( (max_y - height) < event_image.min_y() ) {
LARCV_DEBUG() << "Y: " << max_y - height << " => " << max_y
<< " exceeds event boundary " << event_image.min_y() << std::endl;
max_y = event_image.min_y() + height;
} else if (max_y > event_image.max_y()) {
LARCV_DEBUG() << "Y: " << max_y - height << " => " << max_y
<< " exceeds event boundary " << event_image.max_y() << std::endl;
max_y = event_image.max_y();
}
}
}
LARCV_INFO() << "Creating ImageMeta Width=" << width
<< " Height=" << height
<< " NRows=" << (modular_row ? rows / modular_row : rows)
<< " NCols=" << (modular_col ? cols / modular_col : cols)
<< " Origin @ (" << min_x << "," << max_y - height << ")" << std::endl;
larcv::ImageMeta res(min_x, max_y - height, min_x + width, max_y,
cols, rows,
part_image.id(), larcv::kUnitWireTime);
LARCV_DEBUG() << "Event image " << event_image.dump();
LARCV_DEBUG() << "After format " << res.dump();
//res = event_image.overlap(res);
//LARCV_DEBUG() << "After overlap " << res.dump();
return res;
}
*/
void SuperaMCParticle::finalize()
{}
}
#endif