MergeMatches_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // \file MergeMatches_module.cc
3 // \brief Merge LEM match lists from split running
4 // \author Christopher Backhouse - bckhouse@caltech.edu
5 ////////////////////////////////////////////////////////////////////////
6 
7 #include "LEM/func/FindMatches.h" // Just for the list size
9 #include "LEM/func/MatchList.h"
10 
11 #include "RecoBase/Cluster.h"
13 #include "Utilities/AssociationUtil.h"
14 
15 #include "TH2.h"
16 
17 // Framework includes
24 #include "fhiclcpp/ParameterSet.h"
25 
26 #include <iostream>
27 
28 namespace lem
29 {
30  /// Merge %LEM match lists from split running
32  {
33  public:
34  explicit MergeMatches(const fhicl::ParameterSet& pset);
35  ~MergeMatches();
36 
37  virtual void beginRun(art::Run& run);
38  virtual void produce(art::Event& evt);
39 
40  virtual void reconfigure(const fhicl::ParameterSet& pset);
41 
42  protected:
44  };
45 
46  //......................................................................
48  {
49  reconfigure(pset);
50 
51  produces<std::vector<lem::MatchList>>();
52  // MatchLists are associated with the slice they are against
53  produces<art::Assns<lem::MatchList, rb::Cluster>>();
54 
55  // Really should be file level, not run level, but don't know how to do
56  // that
57  produces<lem::LibrarySummary, art::InRun>();
58 
59  // Copies from one of the split parts
60  produces<std::vector<geo::OfflineChan> >();
61 
62  produces<std::vector<TH2F>>(); // copies from best match part
63  }
64 
65  //......................................................................
67  {
68  }
69 
70  //......................................................................
72  {
73  fSplitLabel = pset.get<std::string>("SplitLabel");
74  }
75 
76  //......................................................................
78  {
79  std::vector<art::Handle<lem::LibrarySummary> > libsums;
80  run.getManyByType(libsums);
81 
82  std::unique_ptr<lem::LibrarySummary> libout(new lem::LibrarySummary);
83 
84  for(unsigned int n = 0; n < libsums.size(); ++n){
85  *libout += *libsums[n];
86  }
87 
88  run.put(std::move(libout));
89  }
90 
91  //......................................................................
93  {
94  std::unique_ptr<std::vector<lem::MatchList>> outcol(new std::vector<lem::MatchList>);
95  std::unique_ptr<std::vector<TH2F>> th2outcol(new std::vector<TH2F>);
96 
97  // Associate matches with the slice they came from
98  std::unique_ptr<art::Assns<lem::MatchList, rb::Cluster>> assns(new art::Assns<lem::MatchList, rb::Cluster>);
99 
100  std::unique_ptr<std::vector<geo::OfflineChan>> chanoutcol(new std::vector<geo::OfflineChan>);
101 
102 
103  std::vector<art::Handle<std::vector<lem::MatchList>>> matchcol;
104  std::vector<art::Handle<std::vector<TH2F>>> th2col;
105  std::vector<art::Handle<std::vector<geo::OfflineChan>>> chancol;
106 
107  evt.getManyByType(matchcol);
108  // TODO: this is bad if anyone else puts TH2s or chans in the event
109  evt.getManyByType(th2col);
110  evt.getManyByType(chancol);
111  assert(!matchcol.empty());
112  assert(th2col.size() == matchcol.size());
113 
114  // Just copy them from the first split job
115  chanoutcol->insert(chanoutcol->end(),
116  chancol[0]->begin(), chancol[0]->end());
117 
118  const unsigned int partMax = matchcol.size();
119  const unsigned int sliceMax = matchcol[0]->size();
120 
121  std::cout << "Merging from " << partMax << " jobs over "
122  << sliceMax << " slices" << std::endl;
123 
124  // Note: these are indexes into the list of slices that got PID'd: not all
125  // slices.
126  for(unsigned int sliceIdx = 0; sliceIdx < sliceMax; ++sliceIdx){
127 
128  std::vector<MatchSummary> matches;
129  std::vector<MatchSummary> enrichedMatches;
130 
131  for(unsigned int partIdx = 0; partIdx < partMax; ++partIdx){
132  assert(matchcol[partIdx]->size() == sliceMax);
133 
134  const MatchList& ms = (*matchcol[partIdx])[sliceIdx];
135 
136  matches.insert(matches.end(), ms.matches.begin(), ms.matches.end());
137  }
138 
139 
140  std::sort(matches.begin(), matches.end());
141  matches.resize(kMaxNumMatches);
142 
143  int bestMatchPart = -1;
144  const double bestPotential = matches[0].potential;
145  for(unsigned int partIdx = 0; partIdx < partMax; ++partIdx){
146  if((*matchcol[partIdx])[sliceIdx].matches.empty()) continue;
147  if((*matchcol[partIdx])[sliceIdx].matches[0].potential == bestPotential){
148  bestMatchPart = partIdx;
149  }
150  } // end for partIdx
151 
152  if(bestMatchPart >= 0){
153  th2outcol->insert(th2outcol->end(),
154  th2col[bestMatchPart]->begin(),
155  th2col[bestMatchPart]->end());
156  }
157  else{
158  mf::LogWarning("MergeMatches") << "Couldn't figure out which part had the best match to copy its histograms";
159  }
160 
161  art::FindManyP<rb::Cluster> fmt(matchcol[0], evt, fSplitLabel);
162  // Get all the slices associated with this match list
163  std::vector<art::Ptr<rb::Cluster> > slice = fmt.at(sliceIdx);
164  assert(slice.size() == 1);
165 
166  // Duplicate the same association in our output
167  util::CreateAssn(*this, evt, *outcol, slice[0], *assns);
168  }
169 
170  evt.put(std::move(outcol));
171  evt.put(std::move(assns));
172  evt.put(std::move(th2outcol));
173  evt.put(std::move(chanoutcol));
174  }
175 
177 
178 } //namespace lem
179 ////////////////////////////////////////////////////////////////////////
static constexpr Double_t ms
Definition: Munits.h:192
static bool CreateAssn(art::EDProducer const &prod, art::Event &evt, std::vector< T > &a, art::Ptr< U > b, art::Assns< T, U > &assn, size_t indx=UINT_MAX, std::string const &instance=std::string())
Create a 1 to 1 association between a new product and one already in the event.
std::vector< MatchSummary > matches
Definition: MatchList.h:24
const unsigned int kMaxNumMatches
Definition: FindMatches.h:29
virtual void beginRun(art::Run &run)
virtual void reconfigure(const fhicl::ParameterSet &pset)
virtual void produce(art::Event &evt)
art::ProductID put(std::unique_ptr< PROD > &&)
Definition: Run.h:149
DEFINE_ART_MODULE(TestTMapFile)
Definition: Run.h:31
Collection of MatchSummary objects.
PID
Definition: FillPIDs.h:14
Core of the LEM match-finding algorithm.
ProductID put(std::unique_ptr< PROD > &&product)
Definition: Event.h:102
Details of the library LEM matches were made against.
Collection of MatchSummary objects.
Definition: MatchList.h:17
Merge LEM match lists from split running.
T get(std::string const &key) const
Definition: ParameterSet.h:231
int evt
void getManyByType(std::vector< Handle< PROD >> &results) const
Definition: DataViewImpl.h:446
Details of the library LEM matches were made against.
Definition: run.py:1
OStream cout
Definition: OStream.cxx:6
MergeMatches(const fhicl::ParameterSet &pset)
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
assert(nhit_max >=nhit_nbins)
Simple object representing a (plane, cell) pair.
enum BeamMode string