Skip to content

Commit 42a23d0

Browse files
authored
Merge pull request #585 from SBNSoftware/feature/gputnam-crt-hit-truth-dev
Add truth matching info to CRT hits
2 parents eae8075 + 23faebc commit 42a23d0

4 files changed

Lines changed: 120 additions & 3 deletions

File tree

sbncode/CAFMaker/CAFMakerParams.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ namespace caf
321321
"crthit" // icarus
322322
};
323323

324+
Atom<string> CRTSimChanLabel {
325+
Name("CRTSimChanLabel"),
326+
Comment("Label of AuxDetSimChannels."),
327+
"genericcrt" // icarus
328+
};
329+
324330
Atom<string> CRTTrackLabel {
325331
Name("CRTTrackLabel"),
326332
Comment("Label of sbn CRT tracks."),

sbncode/CAFMaker/CAFMaker_module.cc

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
#include "sbnanaobj/StandardRecord/Flat/FlatRecord.h"
142142
#include "lardataobj/RawData/ExternalTrigger.h"
143143
#include "lardataobj/RawData/TriggerData.h"
144+
#include "lardataobj/Simulation/AuxDetSimChannel.h"
144145

145146
// // CAFMaker
146147
#include "sbncode/CAFMaker/AssociationUtil.h"
@@ -288,6 +289,11 @@ class CAFMaker : public art::EDProducer {
288289
std::vector<std::vector<geo::BoxBoundedGeo>> fTPCVolumes;
289290
std::vector<geo::BoxBoundedGeo> fActiveVolumes;
290291

292+
// CRT geometry info
293+
//
294+
// ICARUS
295+
std::map<std::pair<int, int>, int> fFEBChannel2AuxDetID;
296+
291297
// random number generator for fake reco
292298
CLHEP::HepRandomEngine& fFakeRecoRandomEngine;
293299

@@ -316,6 +322,7 @@ class CAFMaker : public art::EDProducer {
316322
double GetBlindPOTScale() const;
317323

318324
void InitVolumes(); ///< Initialize volumes from Gemotry service
325+
void InitCRTMapping(); ///< Initialize CRT mapping
319326

320327
void FixPMTReferenceTimes(StandardRecord &rec, double PMT_reference_time);
321328
void FixCRTReferenceTimes(StandardRecord &rec, double CRTT0_reference_time, double CRTT1_reference_time);
@@ -424,6 +431,9 @@ class CAFMaker : public art::EDProducer {
424431
// setup volume definitions
425432
InitVolumes();
426433

434+
// setup CRT mapping
435+
InitCRTMapping();
436+
427437
fSaveGENIEEventRecord = fParams.SaveGENIEEventRecord();
428438

429439
}
@@ -597,6 +607,36 @@ void CAFMaker::FixCRTReferenceTimes(StandardRecord &rec, double CRTT0_reference_
597607

598608
}
599609

610+
void CAFMaker::InitCRTMapping() {
611+
612+
// ICARUS
613+
cet::search_path searchPath("FW_SEARCH_PATH");
614+
std::string fullFileName;
615+
searchPath.find_file("feb_map.txt",fullFileName);
616+
std::ifstream fin;
617+
fin.open(fullFileName, std::ios::in);
618+
619+
if (fin.good()) {
620+
std::string line;
621+
622+
while(std::getline(fin,line)) {
623+
std::vector<std::string> row;
624+
std::string word;
625+
626+
std::stringstream s(line);
627+
while (std::getline(s, word, ',')) {
628+
row.push_back(word);
629+
}
630+
int auxDetID = std::stoi(row[0]); // auxDetID
631+
int mac5 = std::stoi(row[1]); // FEB ID
632+
int chan = std::stoi(row[2]); // FEB channel
633+
fFEBChannel2AuxDetID[std::make_pair(mac5, chan)] = auxDetID;
634+
}
635+
636+
fin.close();
637+
}
638+
}
639+
600640
void CAFMaker::InitVolumes() {
601641
const geo::GeometryCore *geometry = lar::providerFrom<geo::Geometry>();
602642

@@ -1676,16 +1716,36 @@ void CAFMaker::produce(art::Event& evt) noexcept {
16761716
caf::SRSBNDFrameShiftInfo srsbndframeshiftinfo;
16771717
caf::SRSBNDTimingInfo srsbndtiminginfo;
16781718

1719+
// Mapping of (feb, channel) to truth information (AuxDetSimChannel) -- filled for ICARUS
1720+
std::map<std::pair<int, int>, sim::AuxDetSimChannel> crtsimchanmap;
1721+
16791722
if(fDet == kICARUS)
16801723
{
16811724
art::Handle<std::vector<sbn::crt::CRTHit>> crthits_handle;
16821725
GetByLabelStrict(evt, fParams.CRTHitLabel(), crthits_handle);
1726+
1727+
art::Handle<std::vector<sim::AuxDetSimChannel>> auxdetsimchan_handle;
1728+
GetByLabelStrict(evt, fParams.CRTSimChanLabel(), auxdetsimchan_handle);
1729+
16831730
// fill into event
16841731
if (crthits_handle.isValid()) {
16851732
const std::vector<sbn::crt::CRTHit> &crthits = *crthits_handle;
1733+
1734+
std::vector<sim::AuxDetSimChannel> empty;
1735+
const std::vector<sim::AuxDetSimChannel> &crtsimchanvec = (auxdetsimchan_handle.isValid()) ? *auxdetsimchan_handle : empty;
1736+
// Turn the AuxDetSimChannel's into a map that can be looked up from a CRT Hit
1737+
for (const sim::AuxDetSimChannel &crtsimchan: crtsimchanvec) {
1738+
for (auto const &map_pair: fFEBChannel2AuxDetID) {
1739+
if (map_pair.second == (int)crtsimchan.AuxDetID()) {
1740+
crtsimchanmap[map_pair.first] = crtsimchan;
1741+
break;
1742+
}
1743+
}
1744+
}
1745+
16861746
for (unsigned i = 0; i < crthits.size(); i++) {
16871747
srcrthits.emplace_back();
1688-
FillCRTHit(crthits[i], fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, srcrthits.back());
1748+
FillCRTHit(crthits[i], fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, srcrthits.back());
16891749
}
16901750
}
16911751

@@ -2383,7 +2443,7 @@ void CAFMaker::produce(art::Event& evt) noexcept {
23832443
crthittagginginfo = fmCRTHitMatchInfo.at(iPart);
23842444
}
23852445

2386-
FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, trk);
2446+
FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, trk);
23872447
}
23882448
// NOTE: SEE TODO AT fmCRTTrackMatch
23892449
if (fmCRTTrackMatch.isValid() && fDet == kICARUS) {

sbncode/CAFMaker/FillReco.cxx

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ namespace caf
6868
bool use_ts0,
6969
int64_t CRT_T0_reference_time, // ns, signed
7070
double CRT_T1_reference_time, // us
71+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
7172
caf::SRCRTHit &srhit,
7273
bool allowEmpty) {
7374

@@ -86,6 +87,52 @@ namespace caf
8687
srhit.pe = hit.peshit;
8788
srhit.plane = hit.plane;
8889

90+
// lookup truth matching information
91+
std::map<int, float> true_energy;
92+
std::set<std::pair<int, int>> checked; // keep track of what AuxDetSimChannels we have looked at
93+
for (auto const &mac_to_pes: hit.pesmap) {
94+
int mac = (int)mac_to_pes.first;
95+
for (const std::pair<int,float> &chan_pe: mac_to_pes.second) {
96+
int chan = chan_pe.first;
97+
98+
// check for the 3-pos Minos ("m") modules, or the 1-pos other modules
99+
bool is_minos_module = mac < 94;
100+
int pos = is_minos_module ? (chan/10 + 1) : 1;
101+
102+
std::pair<int, int> key {mac, pos};
103+
if (checked.count(key)) continue; // already looked here
104+
105+
checked.insert(key);
106+
107+
if (crtsimchanmap.count(key)) {
108+
const sim::AuxDetSimChannel &crtsimchan = crtsimchanmap.at(key);
109+
for (const sim::AuxDetIDE &ide: crtsimchan.AuxDetIDEs()) {
110+
double hit_time_us = srhit.time;
111+
double tru_time_us = ide.entryT/1e3; // ns -> us
112+
113+
// 1us cut on truth matching
114+
if (abs(hit_time_us - tru_time_us) < 1) {
115+
// std::cout << "Hit at time: " << (srhit.time/1e3) << " " << (srhit.t0/1e3) << " " << (srhit.t1/1e3) << " pes: " << chan_pe.second << " of " << srhit.pe << " on FEB: " << mac << " channel: " << chan << " matched to AuxDetID: " << crtsimchan.AuxDetID() << " G4ID: " << ide.trackID << " E: " << ide.energyDeposited << " T: " << (ide.entryT/1e6) << std::endl;
116+
true_energy[ide.trackID] += ide.energyDeposited;
117+
}
118+
119+
}
120+
}
121+
}
122+
}
123+
124+
// sort results by energy
125+
std::vector<std::pair<float, int>> true_energy_list;
126+
for (auto const &pair: true_energy) true_energy_list.push_back(std::make_pair(pair.second, pair.first));
127+
std::sort(true_energy_list.begin(), true_energy_list.end(), std::greater<>());
128+
129+
// Save to the SRCRTHit truth
130+
for (auto const &pair: true_energy_list) {
131+
srhit.truth.match_e.push_back(pair.first);
132+
srhit.truth.match_id.push_back(pair.second);
133+
}
134+
if (true_energy_list.size() > 0) srhit.truth.bestmatch_id = true_energy_list[0].second;
135+
89136
}
90137

91138
void FillCRTTrack(const sbn::crt::CRTTrack &track,
@@ -658,6 +705,7 @@ namespace caf
658705
bool use_ts0,
659706
int64_t CRT_T0_reference_time, // ns, signed
660707
double CRT_T1_reference_time, // us
708+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
661709
caf::SRTrack &srtrack,
662710
bool allowEmpty)
663711
{
@@ -684,7 +732,7 @@ namespace caf
684732
srtrack.crthit.hit.plane = t0match[0]->fID;
685733
}
686734
if (hitmatch.size()) {
687-
FillCRTHit(*hitmatch[0], use_ts0, CRT_T0_reference_time, CRT_T1_reference_time, srtrack.crthit.hit, allowEmpty);
735+
FillCRTHit(*hitmatch[0], use_ts0, CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, srtrack.crthit.hit, allowEmpty);
688736
}
689737
}
690738

sbncode/CAFMaker/FillReco.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "lardataobj/AnalysisBase/T0.h"
2626
#include "lardataobj/RecoBase/PFParticleMetadata.h"
2727
#include "lardataobj/RecoBase/MCSFitResult.h"
28+
#include "lardataobj/Simulation/AuxDetSimChannel.h"
2829
#include "larrecodnn/CVN/func/Result.h"
2930
#include "sbnobj/Common/Reco/Stub.h"
3031
#include "sbnobj/Common/Reco/RangeP.h"
@@ -193,6 +194,7 @@ namespace caf
193194
bool use_ts0,
194195
int64_t CRT_T0_reference_time, // ns, signed
195196
double CRT_T1_reference_time, // us
197+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
196198
caf::SRTrack &srtrack,
197199
bool allowEmpty = false);
198200

@@ -260,6 +262,7 @@ namespace caf
260262
bool use_ts0,
261263
int64_t CRT_T0_reference_time, // ns, signed
262264
double CRT_T1_reference_time, // us
265+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
263266
caf::SRCRTHit &srhit,
264267
bool allowEmpty = false);
265268
void FillCRTTrack(const sbn::crt::CRTTrack &track,

0 commit comments

Comments
 (0)