CaloClustering_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: CaloClustering
3 // Plugin Type: producer (art v2_11_02)
4 // File: CaloClustering_module.cc
5 //
6 ////////////////////////////////////////////////////////////////////////
7 
15 
18 #include "fhiclcpp/ParameterSet.h"
20 
24 
25 #include "Geometry/GeometryGAr.h"
27 
28 #include "RecoAlg/KNNClusterAlg.h"
29 
30 #include "CLHEP/Units/SystemOfUnits.h"
31 
32 #include <memory>
33 
34 namespace gar {
35  namespace rec {
36 
37  namespace alg{
38  class KNNClusterFinderAlg;
39  }
40 
42  public:
43  explicit CaloClustering(fhicl::ParameterSet const & p);
44  // The compiler-generated destructor is fine for non-base
45  // classes without bare pointers or other resource use.
46 
47  // Plugins should not be copied or assigned.
48  CaloClustering(CaloClustering const &) = delete;
49  CaloClustering(CaloClustering &&) = delete;
50  CaloClustering & operator = (CaloClustering const &) = delete;
51  CaloClustering & operator = (CaloClustering &&) = delete;
52 
53  // Required functions.
54  void produce(art::Event & e) override;
55 
56  private:
57 
58  // Declare member data here.
59  void CollectTracks(const art::Event &evt, const std::string &label, std::vector< art::Ptr<gar::rec::Track> > &trkVector);
60  void CollectHits(const art::Event &evt, const std::string &label, const std::string &instance, std::vector< art::Ptr<gar::rec::CaloHit> > &hitVector);
61 
62  std::string fTrackLabel; ///< label to find the reco tracks
63  std::string fCaloHitLabel; ///< label to find the right reco calo hits
64  std::string fInstanceName; ///< product instance name
65 
66  const detinfo::DetectorProperties* fDetProp; ///< detector properties
67  const geo::GeometryCore* fGeo; ///< pointer to the geometry
68 
69  std::unique_ptr<rec::alg::KNNClusterAlg> fClusterAlgo; //Cluster algorithm
70  };
71 
72 
74  {
75  fTrackLabel = p.get<std::string>("TrackLabel", "track");
76  fCaloHitLabel = p.get<std::string>("CaloHitLabel", "calohit");
77  fInstanceName = p.get<std::string >("InstanceLabelName", "");
78 
79  fGeo = gar::providerFrom<geo::GeometryGAr>();
80  fDetProp = gar::providerFrom<detinfo::DetectorPropertiesService>();
81 
82  //configure the cluster algorithm
83  auto fClusterAlgoPars = p.get<fhicl::ParameterSet>("ClusterAlgPars");
84  fClusterAlgo = std::make_unique<rec::alg::KNNClusterAlg>(fClusterAlgoPars);
85 
86  art::InputTag tag(fCaloHitLabel, fInstanceName);
87  consumes<std::vector<gar::rec::CaloHit>>(tag);
88  produces< std::vector<gar::rec::Cluster> >(fInstanceName);
89  produces< art::Assns<gar::rec::Cluster, gar::rec::CaloHit> >(fInstanceName);
90  if (fClusterAlgo->usesTracks()) produces< art::Assns<gar::rec::Cluster, gar::rec::Track> >(fInstanceName);
91  }
92 
94  {
95  //maps of the hit/track pointer to art ptr pointers
96  std::unordered_map< const gar::rec::CaloHit*, art::Ptr<gar::rec::CaloHit> > hitMaptoArtPtr;
97  std::unordered_map< const gar::rec::Track*, art::Ptr<gar::rec::Track> > trkMaptoArtPtr;
98 
99  //Collect the tracks to be passed to the algo. As of Jun 2019, the algo doesn't need tracks.
100  //But maybe someday it will!
101  std::vector< art::Ptr<gar::rec::Track> > artTrk;
102  if (fClusterAlgo->usesTracks())
103  this->CollectTracks(e, fTrackLabel, artTrk);
104 
105  //Collect the hits to be passed to the algo
106  std::vector< art::Ptr<gar::rec::CaloHit> > artHits;
107  this->CollectHits(e, fCaloHitLabel, fInstanceName, artHits);
108 
109  //Prepare the hits for clustering (tag isolated hits and possible mip hits)
110  //artTrk and trkMaptoArtPtr will be empty if ( !fClusterAlgo->usesTracks() )
111  fClusterAlgo->PrepareAlgo(artTrk, artHits, trkMaptoArtPtr, hitMaptoArtPtr);
112  //Perform the clustering
113  fClusterAlgo->DoClustering();
114  //Get back the cluster results
115  std::vector< gar::rec::Cluster* > ClusterVec = fClusterAlgo->GetFoundClusters();
116 
117  // make an art::PtrVector of the clusters
118  std::unique_ptr< std::vector<gar::rec::Cluster> > ClusterCol(new std::vector<gar::rec::Cluster>);
119  std::unique_ptr< art::Assns<gar::rec::Cluster, gar::rec::CaloHit> > ClusterHitAssns(new art::Assns<gar::rec::Cluster, gar::rec::CaloHit>);
120  std::unique_ptr< art::Assns<gar::rec::Cluster, gar::rec::Track> > ClusterTrackAssns(new art::Assns<gar::rec::Cluster, gar::rec::Track>);
121 
123 
124  MF_LOG_DEBUG("CaloClustering_module")
125  << "Found " << ClusterVec.size() << " Clusters";
126 
127  //Copy the clusters to the collection
128  for(auto const &it : ClusterVec)
129  {
130  MF_LOG_DEBUG("CaloClustering_module")
131  << "Cluster has " << it->CalorimeterHits().size() << " calo hits";
132 
133  ClusterCol->emplace_back(*it);
134 
135  art::Ptr<gar::rec::Cluster> clusterPtr = makeClusterPtr(ClusterCol->size() - 1);
136 
137  //get list of hits associated to the cluster
138  const std::vector< gar::rec::CaloHit* > hitVec = it->CalorimeterHits();
139  for (size_t i=0; i<hitVec.size(); ++i)
140  {
141  const gar::rec::CaloHit* pCaloHit = hitVec[i];
142  //Need to find the corresponding art ptr in the map
143  if(hitMaptoArtPtr.find(pCaloHit) != hitMaptoArtPtr.end())
144  {
145  // associate the hits to this cluster
146  art::Ptr<gar::rec::CaloHit> hitpointer = hitMaptoArtPtr[pCaloHit];
147  ClusterHitAssns->addSingle(clusterPtr, hitpointer);
148  }
149  }
150 
151  if (fClusterAlgo->usesTracks()) {
152  //get list of track associated to the cluster
153  const std::vector< gar::rec::Track* > trkVec = it->Tracks();
154  for(const gar::rec::Track *const pTrack : trkVec)
155  {
156  //Need to find the corresponding art ptr in the map
157  if(trkMaptoArtPtr.find(pTrack) != trkMaptoArtPtr.end())
158  {
159  // associate the hits to this cluster
160  art::Ptr<gar::rec::Track> trkpointer = trkMaptoArtPtr[pTrack];
161  ClusterTrackAssns->addSingle(clusterPtr, trkpointer);
162  }
163  }
164  }
165  }
166 
167  e.put(std::move(ClusterCol), fInstanceName);
168  e.put(std::move(ClusterHitAssns), fInstanceName);
169  if (fClusterAlgo->usesTracks())
170  e.put(std::move(ClusterTrackAssns), fInstanceName);
171 
172  return;
173  }
174 
176  {
177  art::InputTag itag(label,instance);
178  auto theHits = evt.getHandle< std::vector<gar::rec::CaloHit> >(itag);
179  if (!theHits) return;
180 
181  for (unsigned int i = 0; i < theHits->size(); ++i)
182  {
183  const art::Ptr<gar::rec::CaloHit> hit(theHits, i);
184  hitVector.push_back(hit);
185  }
186  }
187 
189  {
190  auto theTracks = evt.getHandle< std::vector<gar::rec::Track> >(label);
191  if (!theTracks) return;
192 
193  for (unsigned int i = 0; i < theTracks->size(); ++i)
194  {
195  const art::Ptr<gar::rec::Track> track(theTracks, i);
196  trkVector.push_back(track);
197  }
198  }
199 
201 
202  } // namespace rec
203 } // namespace gar
rec
Definition: tracks.py:88
std::string fTrackLabel
label to find the reco tracks
void CollectTracks(const art::Event &evt, const std::string &label, std::vector< art::Ptr< gar::rec::Track > > &trkVector)
Handle< PROD > getHandle(SelectorBase const &) const
Definition: DataViewImpl.h:382
std::string string
Definition: nybbler.cc:12
struct vector vector
void produces(std::string const &instanceName={}, Persistable const persistable=Persistable::Yes)
const std::string instance
Description of geometry of one entire detector.
Definition: GeometryCore.h:436
const double e
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
def move(depos, offset)
Definition: depos.py:107
p
Definition: test.py:223
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
std::string fCaloHitLabel
label to find the right reco calo hits
General GArSoft Utilities.
CaloClustering(fhicl::ParameterSet const &p)
std::string fInstanceName
product instance name
void CollectHits(const art::Event &evt, const std::string &label, const std::string &instance, std::vector< art::Ptr< gar::rec::CaloHit > > &hitVector)
const geo::GeometryCore * fGeo
pointer to the geometry
#define MF_LOG_DEBUG(id)
std::vector< gar::rec::Cluster * > ClusterVec
Definition: KNNClusterAlg.h:38
TCEvent evt
Definition: DataStructs.cxx:7
art framework interface to geometry description
const detinfo::DetectorProperties * fDetProp
detector properties
std::unique_ptr< rec::alg::KNNClusterAlg > fClusterAlgo
void produce(art::Event &e) override