RemoveSpatialNoise_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: RemoveSpatialNoise
3 // Module Type: producer
4 // File: RemoveSpatialNoise_module.cc
5 //
6 // Generated at Wed June 5 by Matthew Tamsett based on SpaceSlice
7 //
8 ////////////////////////////////////////////////////////////////////////
9 
14 
19 
20 #include "DAQChannelMap/DAQChannelMap.h"
21 //---------------------------------------------------------------------
22 namespace novaddt {
23  class RemoveSpatialNoise;
24 }
25 //---------------------------------------------------------------------
27  public:
28  explicit RemoveSpatialNoise(fhicl::ParameterSet const & p);
29  virtual ~RemoveSpatialNoise();
30  virtual bool filter(art::Event & event);
31  void endJob() override;
32  private:
33  // Declare member data here.
36  int fMaxXYGap;
37  unsigned int fMinXYHits;
39  // functions
42  void printDDTSlice(HitList & hits);
43  // Counters
44  int _nEvents = 0;
45  int _nInputSlices = 0;
46  int _nOutputSlices = 0;
47  int _nHitsPruned = 0;
48 };
49 //---------------------------------------------------------------------
51 :
52  fInputLabel (p.get< std::string >("InputLabel" )),
53  fInputInstance (p.get< std::string >("InputInstance")),
54  fMaxXYGap (p.get< int >("MaxXYGap" )),
55  fMinXYHits (p.get< int >("MinXYHits" ))
56  // Initialize member data here.
57 {
58  std::cout << "=== novaddt::RemoveSpatialNoise instantiate" << std::endl;
59  std::cout << "\t input hits label: " << fInputLabel << std::endl;
60  std::cout << "\t input hits instance: " << fInputInstance << std::endl;
61  std::cout << "\t Max XY gap: " << fMaxXYGap << std::endl;
62  std::cout << "\t Min X/Y hits: " << fMinXYHits << std::endl;
63  // Call appropriate Produces<>() functions here.
64  produces<std::vector<HitList>>("CleanTimeSpaceSlice");
65 }
66 //---------------------------------------------------------------------
68 {
69  // Clean up dynamic memory and other resources here.
70 }
71 //---------------------------------------------------------------------
73 {
74  LOG_DEBUG("RemoveSpatialNoise") << "=== novaddt::RemoveSpatialNoise filter. Event: "
75  << event.id().event()
76  << std::endl;
77  _nEvents++;
79  event.getByLabel(fInputLabel, fInputInstance, hit_lists);
80  LOG_DEBUG("RemoveSpatialNoise") << "\t got " << hit_lists->size() << " hit lists" << std::endl;
81 
82  //make a hit lists out of hits/hit lists that pass the spatial noise removal
83  std::unique_ptr< std::vector<HitList> >final_product(new std::vector<HitList>);
84 
85  for(unsigned int i=0; i<hit_lists->size(); ++i){
86  _nInputSlices++;
87  HitList product = hit_lists->at(i);
88  LOG_DEBUG("RemoveSpatialNoise") << "\t hit list[" << i << "]: " << product.size() << " hits" << std::endl;
89  //printDDTSlice(product);
90 
91  // prune outliers in X
92  sorted_by_cell = false;
94  // prune outliers in Y
95  sorted_by_cell = true;
97 
98  //printDDTSlice(product);
99  if (product.size() > 0) final_product->push_back(product);
100  } // end of loop on hits lists
101  //put the hits into the event for later use
102  LOG_DEBUG("RemoveSpatialNoise") << "\t produced " << final_product->size() << " hit lists" << std::endl;
103  _nOutputSlices+=final_product->size();
104  event.put(std::move(final_product),"CleanTimeSpaceSlice");
105 
106  return true;
107 }
108 //---------------------------------------------------------------------
111 {
112  unsigned int n_hits = hits.size();
113 
114  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t hit list, has " << n_hits << " hits" << std::endl;
115 
116  if (!sorted_by_cell) std::sort(hits.begin(), hits.end(), CompareDAQHit<Cell>());
117 
118  std::vector<int> current_slice_indices;
119  std::vector<int> to_erase;
120  int current_end = -9; // dummy value to start with
121 
122  for (unsigned int i_hit = 0; i_hit < n_hits; ++i_hit){
123  DAQHit hit = hits[i_hit];
124 
125  // check that the current cell is in the correct view
126  if (!(hit.View() == view)) continue;
127  //LOG_DEBUG("RemoveSpatialNoise") << "\t\t - found hit in this view: " << i_hit << std::endl;
128  // if current end not yet initialised do this now
129  if (current_end == -9){
130  current_end = i_hit;
131  current_slice_indices.push_back(i_hit);
132  continue;
133  }
134 
135  const int thisCell = hit.Cell().val;
136  const int endCell = hits[current_end].Cell().val;
137 
138  //LOG_DEBUG("RemoveSpatialNoise") << "\t\t - xy gap[" << current_end << "," << i_hit
139  //<< "]: " << endCell
140  //<< " - " << thisCell
141  //<< std::endl;
142 
143  // check for a large seperation
144  if(thisCell - endCell > fMaxXYGap){
145  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t - large gap[" << current_end << "," << i_hit
146  << "]: " << endCell
147  << " - " << thisCell
148  << ", current slice size: "
149  << current_slice_indices.size() << std::endl;
150  // if the current slice is small, mark the hits for removal
151  if (current_slice_indices.size() < fMinXYHits){
152  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t - erase small slice: "
153  << current_slice_indices.size() << " hits"
154  << std::endl;
155  for (auto const &i: current_slice_indices){
156  to_erase.push_back(i);
157  }
158  }
159  // restart the current slice
160  current_slice_indices.clear();
161  current_slice_indices.push_back(i_hit);
162  current_end = i_hit;
163  } else {
164  current_slice_indices.push_back(i_hit);
165  current_end = i_hit;
166  }
167  } // end of loop on hits
168 
169  LOG_DEBUG("RemoveSpatialNoise") << "\t\t - end of loop, current slice size: "
170  << current_slice_indices.size() << std::endl;
171  // finalise
172  if (current_slice_indices.size() < fMinXYHits){
173  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t - erase small final slice: "
174  << current_slice_indices.size() << " hits"
175  << std::endl;
176  for (auto const &i: current_slice_indices){
177  to_erase.push_back(i);
178  }
179  } // end of finalise
180 
181  // erase outliers
182  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t - erasing " << to_erase.size() << " hits" << std::endl;
183  // sort in reverse order
184  std::sort(to_erase.begin(), to_erase.end(), std::greater<int>());
185  for (auto const &i: to_erase){
186  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t\t - erasing " << i << std::endl;
187  hits.erase(hits.begin()+i);
188  _nHitsPruned++;
189  }
190  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\t - final slice size: " << hits.size() << std::endl;
191 }
192 //---------------------------------------------------------------------
194 {
195  // print them
196  LOG_DEBUG("RemoveSpatialNoise") << "\t\t - Hit list: " << hits.size()
197  << std::endl;
198  // sort them
199  sort(hits.begin(), hits.end(), novaddt::CompareDAQHit<novaddt::Cell>());
200  // loop over hits in each slice
201  for(unsigned int j = 0; j < hits.size(); ++j){
202  novaddt::DAQHit hit(hits[j]);
203  std::string view = "y";
204  if( hit.View().val == daqchannelmap::X_VIEW ){
205  view = "x";
206  }
207  LOG_DEBUG("RemoveSpatialNoise") << "\t\t\thit[" << j
208  << "]: TDC: " << hit.TDC().val
209  << ", ADC: " << hit.ADC().val
210  << ", plane: " << hit.Plane().val
211  << ", " << view
212  << "-cell: " << hit.Cell().val
213  << std::endl;
214  } // end of loop on hits
215 }
216 //---------------------------------------------------------------------
218 {
219  std::cout << "=== novaddt::RemoveSpatialNoise endJob" << std::endl;
220  std::cout << "\tNumber of events: " << _nEvents << std::endl;
221  std::cout << "\tNumber of slices: " << _nInputSlices << std::endl;
222  std::cout << "\tNumber of slices filtered: " << _nOutputSlices << std::endl;
223  std::cout << "\tNumber of hits pruned: " << _nHitsPruned << std::endl;
224 }
225 //---------------------------------------------------------------------
#define LOG_DEBUG(stream)
Definition: Messenger.h:149
void pruneOutlyingCells(HitList &hits, novaddt::View view)
value_type val
Definition: BaseProducts.h:34
novaddt::Plane const & Plane() const
Definition: DAQHit.h:70
novaddt::TDC const & TDC() const
Definition: DAQHit.h:74
std::vector< DAQHit > HitList
Definition: HitList.h:15
const char * p
Definition: xmltok.h:285
value_type val
Definition: BaseProducts.h:109
DEFINE_ART_MODULE(TestTMapFile)
Identifier for the Y measuring view of the detector (side)
void hits()
Definition: readHits.C:15
novaddt::ADC const & ADC() const
Definition: DAQHit.h:73
novaddt::View const & View() const
Definition: DAQHit.h:72
Identifier for the X measuring view of the detector (top)
const double j
Definition: BetheBloch.cxx:29
value_type val
Definition: BaseProducts.h:84
RemoveSpatialNoise(fhicl::ParameterSet const &p)
OStream cout
Definition: OStream.cxx:6
T product(std::vector< T > dims)
Definition: structs.h:12
value_type val
Definition: BaseProducts.h:137
novaddt::Cell const & Cell() const
Definition: DAQHit.h:71
value_type val
Definition: BaseProducts.h:65
virtual bool filter(art::Event &event)
enum BeamMode string