1 #include "art/Framework/Principal/Handle.h"
2 #include "canvas/Persistency/Common/FindManyP.h"
3 #include "messagefacility/MessageLogger/MessageLogger.h"
7 //----------------------------------------------------------------
8 template <typename Evt>
9 const bool PhotonBackTracker::CanRun(Evt const& evt)
11 return !(evt.isRealData());
14 //----------------------------------------------------------------
15 template <typename Evt>
16 void PhotonBackTracker::PrepOpDetBTRs(Evt const& evt)
18 if (this->BTRsReady()) { return; }
19 const std::vector<art::InputTag> G4ModuleLabels =
20 (fG4ModuleLabels.empty()) ? std::vector{fG4ModuleLabel} : fG4ModuleLabels;
22 auto compareBTRlambda = [](art::Ptr<sim::OpDetBacktrackerRecord> a,
23 art::Ptr<sim::OpDetBacktrackerRecord> b) {
24 return (a->OpDetNum() < b->OpDetNum());
27 for (auto& G4ModuleLabel : G4ModuleLabels) {
28 auto const& btrHandle =
29 evt.template getValidHandle<std::vector<sim::OpDetBacktrackerRecord>>(G4ModuleLabel);
30 art::fill_ptr_vector(priv_OpDetBTRs, btrHandle);
31 if (!std::is_sorted(priv_OpDetBTRs.begin(), priv_OpDetBTRs.end(), compareBTRlambda))
32 std::sort(priv_OpDetBTRs.begin(), priv_OpDetBTRs.end(), compareBTRlambda);
34 // // // // // // // // // // // // // // // // // // // // // // // //
35 // DUNE-specific code which hasn't been migrated anywhere better yet //
36 // // // // // // // // // // // // // // // // // // // // // // // //
37 //art::fill_ptr_vector(priv_DivRecs, divrecHandle);
38 //auto compareDivReclambda = [](art::Ptr<sim::OpDetDivRec> a, art::Ptr<sim::OpDetDivRec> b) {return(a->OpDetNum() < b->OpDetNum());};
39 /*if (!std::is_sorted(priv_DivRecs.begin(), priv_DivRecs.end(), compareDivReclambda))
40 std::sort(priv_DivRecs.begin(), priv_DivRecs.end(), compareDivReclambda);*/
41 //art::FindManyP<raw::OpDetWaveform, sim::OpDetDivRec> fp(priv_OpDetBTRs, evt, fWavLabel);// fp;
42 //art::FindOneP<raw::OpDetWaveform, sim::OpDetDivRec> fp(priv_OpDetBTRs, evt, fWavLabel);// fp;
43 //They come in sorted by BTR. Now make an index matched vector of data_t sorted by BTR. No. I need easy, not efficient. Map of DetNum to data_t. data_t is then channel mapped.
46 for( size_t btr_iter=0; btr_iter<priv_OpDetBTRs.size(); ++btr_iter){
47 auto btr=priv_OpDetBTRs.at(btr_iter);
48 auto od = btr->OpDetNum();
49 auto const& dr = fp.data(btr_iter);
53 priv_od_to_DivRec[od]=*d;//->ref();
57 }else{throw cet::exception("PhotonBackTracker")<<"find Waveforms and DivRecs from BTRs failed.";}
59 // // // // // // // // // // // // // // // // // // // // // // // //
60 // DUNE-specific code which hasn't been migrated anywhere better yet //
61 // // // // // // // // // // // // // // // // // // // // // // // //
66 //----------------------------------------------------------------
67 //ToDo: Figure out why I get OpHit* out of here instead of art::Ptr.
68 template <typename Evt>
69 void PhotonBackTracker::PrepOpFlashToOpHits(Evt const& evt)
71 if (this->OpFlashToOpHitsReady()) { return; }
72 //std::vector< art::Handle< std::vector < recob::OpFlash >>> flashHandles;
73 //evt.getManyByType(flashHandles);
74 auto flashHandles = evt.template getMany<std::vector<recob::OpFlash>>();
75 for (const auto& handle : flashHandles) {
76 std::vector<art::Ptr<recob::OpFlash>> flash_vec;
77 if (handle.failedToGet()) {
78 mf::LogWarning("PhotonBackTracker")
79 << " failed to get handle to recob::OpFlash. Has reco run yet?";
82 art::fill_ptr_vector(flash_vec, handle);
83 auto tag = art::InputTag(handle.provenance()->moduleLabel());
84 art::FindManyP<recob::OpHit> flash_hit_assn(flash_vec, evt, tag);
85 // std::cout<<"flash_hit_assn.size: "<<flash_hit_assn.size()<<"\n";
86 for (size_t i = 0; i < flash_vec.size(); ++i) {
87 art::Ptr<recob::OpFlash> flashp = flash_vec.at(i);
88 std::vector<art::Ptr<recob::OpHit>> ophits = flash_hit_assn.at(i);
89 auto check = priv_OpFlashToOpHits.emplace(flashp, ophits);
90 if (check.second == false) {
92 // push_back to vector.
93 for (auto& ophitp : ophits) {
94 check.first->second.push_back(ophitp);
101 //----------------------------------------------------------------
102 template <typename Evt>
103 void PhotonBackTracker::PrepEvent(Evt const& evt)
105 if (!(this->CanRun(evt))) {
106 throw cet::exception("PhotonBackTracker") << "PhotonBackTracker cannot function."
107 << "Is this file real data?";
109 priv_OpDetBTRs.clear();
110 this->PrepOpDetBTRs(evt);
111 this->PrepOpFlashToOpHits(evt);