24 #include "art/Framework/Core/ModuleMacros.h"
25 #include "art/Framework/Core/EDProducer.h"
26 #include "art/Framework/Principal/Event.h"
27 #include "art/Framework/Principal/Handle.h"
28 #include "canvas/Persistency/Common/Assns.h"
29 #include "canvas/Persistency/Common/FindManyP.h"
30 #include "canvas/Persistency/Common/Ptr.h"
31 #include "canvas/Persistency/Common/PtrVector.h"
32 #include "messagefacility/MessageLogger/MessageLogger.h"
51 void FilterHits(art::PtrVector<recob::Hit>& hits, art::PtrVector<recob::Hit>& used_hits)
53 if(used_hits.size() > 0)
56 std::stable_sort(hits.begin(), hits.end());
57 std::stable_sort(used_hits.begin(), used_hits.end());
60 art::PtrVector<recob::Hit>::iterator it = std::set_difference(hits.begin(), hits.end(), used_hits.begin(), used_hits.end(), hits.begin());
63 hits.erase(it, hits.end());
78 virtual void produce(art::Event &
e);
84 const art::Handle<std::vector<recob::PFParticle> >& pfParticleHandle,
85 const art::FindManyP<recob::Cluster>& partToClusAssns,
86 const art::FindManyP<recob::Hit>& clusToHitAssns,
87 std::set<const recob::PFParticle*>& taggedParticles,
88 art::PtrVector<recob::Hit>& hitVec);
116 fCosmicProducerLabel = pset.get<std::string>(
"CosmicProducerLabel");
117 fHitProducerLabel = pset.get<std::string>(
"HitProducerLabel");
118 fPFParticleProducerLabel = pset.get<std::string>(
"PFParticleProducerLabel");
119 fCosmicTagThreshold = pset.get<
double> (
"CosmicTagThreshold");
121 produces<std::vector<recob::Hit> >();
124 mf::LogInfo(
"CRHitRemovalByPCA") <<
"CRHitRemovalByPCA configured\n";
145 art::Handle< std::vector<recob::Hit> > hitHandle;
149 if (!hitHandle.isValid())
return;
153 std::unique_ptr<std::vector<recob::Hit> > outputHits(
new std::vector<recob::Hit>);
156 *outputHits = *hitHandle;
159 art::Handle<std::vector<recob::PFParticle> > pfParticleHandle;
163 if (!pfParticleHandle.isValid())
165 evt.put(std::move(outputHits));
171 art::Handle<std::vector<recob::Cluster> > clusterHandle;
175 if (!clusterHandle.isValid())
177 evt.put(std::move(outputHits));
182 art::Handle< std::vector<anab::CosmicTag> > cosmicTagHandle;
186 if (!cosmicTagHandle.isValid() || cosmicTagHandle->empty())
188 evt.put(std::move(outputHits));
194 art::FindManyP<recob::PFParticle> cosmicTagToPFPartAssns(cosmicTagHandle, evt,
fCosmicProducerLabel);
203 art::PtrVector<recob::Hit> taggedHits;
206 std::set<const recob::PFParticle*> taggedSet;
210 for(
size_t crIdx = 0; crIdx != cosmicTagHandle->size(); crIdx++)
212 art::Ptr<anab::CosmicTag> cosmicTag(cosmicTagHandle, crIdx);
218 std::vector<art::Ptr<recob::PFParticle> > pfPartVec = cosmicTagToPFPartAssns.at(crIdx);
220 if (pfPartVec.empty())
continue;
222 art::Ptr<recob::PFParticle> pfParticle = pfPartVec.front();
225 if (!pfParticle)
continue;
228 if (!pfParticle->IsPrimary())
continue;
231 if (taggedSet.find(pfParticle.get()) != taggedSet.end())
continue;
234 removeTaggedHits(pfParticle.get(), pfParticleHandle, clusterAssns, clusterHitAssns, taggedSet, taggedHits);
239 if (!taggedHits.empty())
244 art::PtrVector<recob::Hit> untaggedHits;
246 for(
const auto& pfParticle : *pfParticleHandle)
248 if (taggedSet.find(&pfParticle) != taggedSet.end())
continue;
251 std::vector<art::Ptr<recob::Cluster> > clusterVec = clusterAssns.at(pfParticle.Self());
254 for(
const auto&
cluster : clusterVec)
256 std::vector<art::Ptr<recob::Hit> > clusHitVec = clusterHitAssns.at(
cluster->ID());
257 untaggedHits.insert(untaggedHits.end(), clusHitVec.begin(), clusHitVec.end());
262 FilterHits(taggedHits, untaggedHits);
267 art::PtrVector<recob::Hit> originalHits;
270 for(
size_t hitIdx = 0; hitIdx != hitHandle->size(); hitIdx++)
272 art::Ptr<recob::Hit>
hit(hitHandle, hitIdx);
274 originalHits.push_back(hit);
278 FilterHits(originalHits, taggedHits);
284 for (
const auto&
hit : originalHits)
287 if (
hit->StartTick() > 6400 ||
hit->EndTick() < 3200)
continue;
289 outputHits->emplace_back(*
hit);
294 evt.put(std::move(outputHits));
313 const art::Handle<std::vector<recob::PFParticle> >& pfParticleHandle,
314 const art::FindManyP<recob::Cluster>& partToClusAssns,
315 const art::FindManyP<recob::Hit>& clusToHitAssns,
316 std::set<const recob::PFParticle*>& taggedParticles,
317 art::PtrVector<recob::Hit>& hitVec)
320 std::vector<art::Ptr<recob::Cluster> > clusterVec = partToClusAssns.at(pfParticle->
Self());
323 taggedParticles.insert(pfParticle);
326 for(
const auto&
cluster : clusterVec)
328 std::vector<art::Ptr<recob::Hit> > clusHitVec = clusToHitAssns.at(
cluster->ID());
329 hitVec.insert(hitVec.end(), clusHitVec.begin(), clusHitVec.end());
333 for(
const auto& daughterId : pfParticle->
Daughters())
335 art::Ptr<recob::PFParticle> daughter(pfParticleHandle, daughterId);
337 removeTaggedHits(daughter.get(), pfParticleHandle, partToClusAssns, clusToHitAssns, taggedParticles, hitVec);
350 mf::LogInfo(
"CRHitRemovalByPCA")
351 <<
"CRHitRemovalByPCA statistics:\n"
352 <<
" Number of events = " <<
fNumEvent <<
"\n"
354 <<
", " << aveCRPerEvent <<
" average/event";
const std::vector< size_t > & Daughters() const
Returns the collection of daughter particles.
virtual void endJob()
End job method.
size_t Self() const
Returns the index of this particle.
Declaration of signal hit object.
std::string fCosmicProducerLabel
Module that produced the PCA based cosmic tags.
CRHitRemovalByPCA(fhicl::ParameterSet const &pset)
std::string fPFParticleProducerLabel
PFParticle producer.
int fNumCRRejects
Number of tracks produced.
int fNumEvent
Number of events seen.
virtual void produce(art::Event &e)
Declaration of cluster object.
double fCosmicTagThreshold
Thresholds for tagging.
void removeTaggedHits(const recob::PFParticle *pfParticle, const art::Handle< std::vector< recob::PFParticle > > &pfParticleHandle, const art::FindManyP< recob::Cluster > &partToClusAssns, const art::FindManyP< recob::Hit > &clusToHitAssns, std::set< const recob::PFParticle * > &taggedParticles, art::PtrVector< recob::Hit > &hitVec)
Hierarchical representation of particle flow.
std::string fHitProducerLabel
The full collection of hits.