Skip to content

Commit e3e2b42

Browse files
authored
Merge pull request #586 from SBNSoftware/feature/gputnam-crt-hit-truth
Add truth matching info to CRT hits
2 parents 7fbb950 + 5e0b929 commit e3e2b42

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
@@ -320,6 +320,12 @@ namespace caf
320320
"crthit" // icarus
321321
};
322322

323+
Atom<string> CRTSimChanLabel {
324+
Name("CRTSimChanLabel"),
325+
Comment("Label of AuxDetSimChannels."),
326+
"genericcrt" // icarus
327+
};
328+
323329
Atom<string> CRTTrackLabel {
324330
Name("CRTTrackLabel"),
325331
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
@@ -140,6 +140,7 @@
140140
#include "sbnanaobj/StandardRecord/Flat/FlatRecord.h"
141141
#include "lardataobj/RawData/ExternalTrigger.h"
142142
#include "lardataobj/RawData/TriggerData.h"
143+
#include "lardataobj/Simulation/AuxDetSimChannel.h"
143144

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

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

@@ -315,6 +321,7 @@ class CAFMaker : public art::EDProducer {
315321
double GetBlindPOTScale() const;
316322

317323
void InitVolumes(); ///< Initialize volumes from Gemotry service
324+
void InitCRTMapping(); ///< Initialize CRT mapping
318325

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

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

428438
}
@@ -596,6 +606,36 @@ void CAFMaker::FixCRTReferenceTimes(StandardRecord &rec, double CRTT0_reference_
596606

597607
}
598608

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

@@ -1649,16 +1689,36 @@ void CAFMaker::produce(art::Event& evt) noexcept {
16491689
caf::SRSBNDFrameShiftInfo srsbndframeshiftinfo;
16501690
caf::SRSBNDTimingInfo srsbndtiminginfo;
16511691

1692+
// Mapping of (feb, channel) to truth information (AuxDetSimChannel) -- filled for ICARUS
1693+
std::map<std::pair<int, int>, sim::AuxDetSimChannel> crtsimchanmap;
1694+
16521695
if(fDet == kICARUS)
16531696
{
16541697
art::Handle<std::vector<sbn::crt::CRTHit>> crthits_handle;
16551698
GetByLabelStrict(evt, fParams.CRTHitLabel(), crthits_handle);
1699+
1700+
art::Handle<std::vector<sim::AuxDetSimChannel>> auxdetsimchan_handle;
1701+
GetByLabelStrict(evt, fParams.CRTSimChanLabel(), auxdetsimchan_handle);
1702+
16561703
// fill into event
16571704
if (crthits_handle.isValid()) {
16581705
const std::vector<sbn::crt::CRTHit> &crthits = *crthits_handle;
1706+
1707+
std::vector<sim::AuxDetSimChannel> empty;
1708+
const std::vector<sim::AuxDetSimChannel> &crtsimchanvec = (auxdetsimchan_handle.isValid()) ? *auxdetsimchan_handle : empty;
1709+
// Turn the AuxDetSimChannel's into a map that can be looked up from a CRT Hit
1710+
for (const sim::AuxDetSimChannel &crtsimchan: crtsimchanvec) {
1711+
for (auto const &map_pair: fFEBChannel2AuxDetID) {
1712+
if (map_pair.second == (int)crtsimchan.AuxDetID()) {
1713+
crtsimchanmap[map_pair.first] = crtsimchan;
1714+
break;
1715+
}
1716+
}
1717+
}
1718+
16591719
for (unsigned i = 0; i < crthits.size(); i++) {
16601720
srcrthits.emplace_back();
1661-
FillCRTHit(crthits[i], fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, srcrthits.back());
1721+
FillCRTHit(crthits[i], fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, srcrthits.back());
16621722
}
16631723
}
16641724

@@ -2318,7 +2378,7 @@ void CAFMaker::produce(art::Event& evt) noexcept {
23182378
crthittagginginfo = fmCRTHitMatchInfo.at(iPart);
23192379
}
23202380

2321-
FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, trk);
2381+
FillTrackCRTHit(fmCRTHitMatch.at(iPart), crthitmatch, crthittagginginfo, fParams.CRTUseTS0(), CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, trk);
23222382
}
23232383
// NOTE: SEE TODO AT fmCRTTrackMatch
23242384
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,
@@ -635,6 +682,7 @@ namespace caf
635682
bool use_ts0,
636683
int64_t CRT_T0_reference_time, // ns, signed
637684
double CRT_T1_reference_time, // us
685+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
638686
caf::SRTrack &srtrack,
639687
bool allowEmpty)
640688
{
@@ -661,7 +709,7 @@ namespace caf
661709
srtrack.crthit.hit.plane = t0match[0]->fID;
662710
}
663711
if (hitmatch.size()) {
664-
FillCRTHit(*hitmatch[0], use_ts0, CRT_T0_reference_time, CRT_T1_reference_time, srtrack.crthit.hit, allowEmpty);
712+
FillCRTHit(*hitmatch[0], use_ts0, CRT_T0_reference_time, CRT_T1_reference_time, crtsimchanmap, srtrack.crthit.hit, allowEmpty);
665713
}
666714
}
667715

sbncode/CAFMaker/FillReco.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "lardataobj/AnalysisBase/T0.h"
2929
#include "lardataobj/RecoBase/PFParticleMetadata.h"
3030
#include "lardataobj/RecoBase/MCSFitResult.h"
31+
#include "lardataobj/Simulation/AuxDetSimChannel.h"
3132
#include "larrecodnn/CVN/func/Result.h"
3233
#include "sbnobj/Common/Reco/Stub.h"
3334
#include "sbnobj/Common/Reco/RangeP.h"
@@ -177,6 +178,7 @@ namespace caf
177178
bool use_ts0,
178179
int64_t CRT_T0_reference_time, // ns, signed
179180
double CRT_T1_reference_time, // us
181+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
180182
caf::SRTrack &srtrack,
181183
bool allowEmpty = false);
182184

@@ -250,6 +252,7 @@ namespace caf
250252
bool use_ts0,
251253
int64_t CRT_T0_reference_time, // ns, signed
252254
double CRT_T1_reference_time, // us
255+
const std::map<std::pair<int, int>, sim::AuxDetSimChannel> &crtsimchanmap,
253256
caf::SRCRTHit &srhit,
254257
bool allowEmpty = false);
255258
void FillCRTTrack(const sbn::crt::CRTTrack &track,

0 commit comments

Comments
 (0)