DDTTrackMerge_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 // Class: DDTTrackMerge
3 // Module Type: filter
4 // File: DDTTrackMerge_module.cc
5 // Author: Zukai Wang
6 ////////////////////////////////////////////////////////////////////////
15 #include "fhiclcpp/ParameterSet.h"
17 
22 
23 #include "DAQChannelMap/DAQChannelMap.h"
25 
26 #include <memory>
27 #include <vector>
28 
29 //------------------------------------------------------------------------
30 namespace novaddt {
31  class DDTTrackMerge;
32 }
33 //------------------------------------------------------------------------
35 public:
36  explicit DDTTrackMerge(fhicl::ParameterSet const & p);
37  virtual ~DDTTrackMerge();
38  bool filter(art::Event & e) override;
39  void endJob() override;
40 
41  void simpleMerge(std::vector< art::Ptr<novaddt::Track> > tracks,
42  std::unique_ptr< std::vector<novaddt::Track3D> > & merged_tracks,
43  std::unique_ptr< std::vector<novaddt::HitList> > & hitListcol,
45 
46 private:
47  std::string _trackModuleLabel; ///< label of module making the Tracks
48  std::string _trackInstanceLabel; ///< instance label making the Tracks
49  std::string _sliceModuleLabel; ///< label of module making the HitList
50  std::string _sliceInstanceLabel; ///< instance label making the HitList
51  std::string _trackToSliceInstanceLabel;///< instance of track to slice association produced by track module
52 
53  float _Zdiff;
54 
55  int _nSlices = 0; ///< number of slices seen
56  int _nXTracks = 0; ///< number of x tracks seen
57  int _nYTracks = 0; ///< number of y tracks seen
58 
59  int _nMergedTracks = 0; ///< number of merged tracks produced
60 };
61 //------------------------------------------------------------------------
63 : _trackModuleLabel (p.get<std::string >("TrackModuleLabel" ))
64  , _trackInstanceLabel (p.get<std::string >("TrackInstanceLabel", ""))
65  , _sliceModuleLabel (p.get<std::string >("SliceModuleLabel" ))
66  , _sliceInstanceLabel (p.get<std::string >("SliceInstanceLabel", ""))
67  , _trackToSliceInstanceLabel (p.get<std::string >("TrackToSliceInstanceLabel", ""))
68  , _Zdiff (p.get<float> ("Z_Diff"))
69 
70 {
71  std::cout << "=== novaddt::DDTTrackMerge instantiate" << std::endl;
72  std::cout << "Here is the configuration"<<std::endl;
73  std::cout << "\t TrackModuleLabel: " << _trackModuleLabel << std::endl;
74  std::cout << "\t TrackInstanceLabel: " << _trackInstanceLabel << std::endl;
75  std::cout << "\t SliceModuleLabel: " << _sliceModuleLabel << std::endl;
76  std::cout << "\t SliceInstanceLabel: " << _sliceInstanceLabel << std::endl;
77  std::cout << "\t TrackToSliceInstanceLabel: " << _trackToSliceInstanceLabel << std::endl;
78 
79  std::cout << "\t Zdiff: " << _Zdiff << std::endl;
80 
81  produces< std::vector<novaddt::Track3D> >();
82  produces< std::vector<novaddt::HitList> >();
83 
84  produces< art::Assns<novaddt::Track3D, novaddt::HitList> >();
85  produces< art::Assns<novaddt::Track3D, novaddt::HitList> >(_trackToSliceInstanceLabel);
86 }
87 //------------------------------------------------------------------------
89 {
90  // Clean up dynamic memory and other resources here.
91 }
92 
93 //------------------------------------------------------------------------
95 {
96  // Implementation of required member function here.
97  std::cout << "=== novaddt::DDTTrackMerge filter. Event: "
98  << event.id().event()
99  << std::endl;
100 
101  // make the final data products
102  std::unique_ptr< std::vector<novaddt::Track3D> > merged_tracks (new std::vector<novaddt::Track3D>);
103  std::unique_ptr< std::vector<novaddt::HitList> > merged_hit_lists (new std::vector<novaddt::HitList>);
104  std::unique_ptr< art::Assns<novaddt::Track3D, novaddt::HitList> > assn(new art::Assns<novaddt::Track3D, novaddt::HitList>);
105  std::unique_ptr< art::Assns<novaddt::Track3D, novaddt::HitList> > assn_b(new art::Assns<novaddt::Track3D, novaddt::HitList>);
106 
107  // get the slices
109  event.getByLabel(_sliceModuleLabel, _sliceInstanceLabel, slices);
110 
111  _nSlices+=slices->size();
112 
113  //Now make it an art::PtrVector so we can use associations later
115  for(unsigned int i = 0; i < slices->size(); ++i){
116  art::Ptr<novaddt::HitList>slice_ptr(slices,i);
117  slicelist.push_back(slice_ptr);
118  }
119 
121  art::FindManyP<novaddt::Track> find_tracks(slices, event, the_slices);
122 
123  // loop over slices
124  for(size_t i_slice = 0; i_slice < slices->size(); ++i_slice){
125  // get the hit list
126  novaddt::HitList hit_list = slices->at(i_slice);
127  // find the tracks for this hit list
128  std::vector< art::Ptr<novaddt::Track> > tracks = find_tracks.at(i_slice);
129 
130  if (!tracks.size()) continue;
131 
132  // get the hit list for each track
134 
135  unsigned int current_size = merged_tracks->size();
136  // merge tracks
137  simpleMerge(tracks, merged_tracks, merged_hit_lists, fohl);
138 
139  // the number of merged tracks should ALWAYS be equal to the number of hit lists
140  assert(merged_tracks->size()==merged_hit_lists->size());
141 
142  // loop over new tracks and hit lists, save final products and associations
143  for (unsigned int i = current_size; i < merged_tracks->size(); ++i){
144  // association between this slice and each track
145  util::CreateAssn(*this, event, *merged_tracks, slicelist[i_slice], *assn_b, i);
146  // association between each track and it's hits
147  util::CreateAssn(*this, event, *merged_tracks, *merged_hit_lists, *assn, i, i+1, i);
148  }
149  } // end of loop on slices
150 
151  // store the merged tracks
152 
153  event.put(std::move(merged_tracks));
154  event.put(std::move(merged_hit_lists));
155  event.put(std::move(assn));
156  event.put(std::move(assn_b), _trackToSliceInstanceLabel);
157  return true;
158 }
159 
160 //------------------------------------------------------------------------
162  std::unique_ptr< std::vector<novaddt::Track3D> > & merged_tracks,
163  std::unique_ptr< std::vector<novaddt::HitList> > & merged_hit_lists,
165 {
166 
167  //First, we need to classify the tracks in the same view:
168  std::vector<art::Ptr<novaddt::Track> > Xtracks;
169  std::vector<art::Ptr<novaddt::Track> > Ytracks;
170  std::vector<bool> y_used;
171  std::vector<int> x_Tridx, y_Tridx;
172 
173  for(size_t i_track = 0; i_track < tracks.size(); ++i_track){
174  if (tracks[i_track]->View() == daqchannelmap::X_VIEW) {
175  Xtracks.emplace_back(tracks[i_track]);
176 
177  x_Tridx.emplace_back(i_track);
178  }
179  else {
180  Ytracks.emplace_back(tracks[i_track]);
181  y_used.push_back(0);
182  y_Tridx.emplace_back(i_track);
183  }
184  }
185 
186  _nXTracks+=Xtracks.size();
187  _nYTracks+=Ytracks.size();
188 
189 
190  if ( (Xtracks.size() == 0) || (Ytracks.size() == 0) ) return;
191 
192  //Start Merging:
193  novaddt::HitList merged_hit_list;
194 
195  for (size_t i=0; i!= Xtracks.size();++i) {
196  float x_low_z = std::min(Xtracks[i]->StartZ(),Xtracks[i]->EndZ());
197  float x_high_z = std::max(Xtracks[i]->StartZ(),Xtracks[i]->EndZ());
198  for (size_t j=0;j!=Ytracks.size();++j) {
199  if (y_used[j]) continue;
200  float y_low_z = std::min(Ytracks[j]->StartZ(),Ytracks[j]->EndZ());
201  float y_high_z = std::max(Ytracks[j]->StartZ(),Ytracks[j]->EndZ());
202  if (fabs(x_low_z-y_low_z)< _Zdiff && fabs(x_high_z-y_high_z)<_Zdiff) {
203  y_used[j] = true;
204 
205  TVector3 start;
206  TVector3 end;
207  float x0,y0,x1,y1;
208  x0=0;
209  y0=0;
210  x1=0;
211  y1=0;
212  float xz0 = 9999;
213  float yz0 = 9999;
214  float xz1 = 0;
215  float yz1 = 0;
216 
217 
218  // save merged hit list
219  novaddt::HitList merged_hit_list;
220 
221  for (auto const& hit : *fohl.at(x_Tridx[i]) ) {
222  merged_hit_list.emplace_back(hit);
223  if (xz0 > hit.Plane().val) {
224  x0 = hit.Cell().val;
225  xz0 = hit.Plane().val;
226  }
227  if (xz1 < hit.Plane().val) {
228  x1 = hit.Cell().val;
229  xz1 = hit.Plane().val;
230  }
231 
232  }
233 
234  for (auto const& hit : *fohl.at(y_Tridx[j]) ) {
235  merged_hit_list.emplace_back(hit);
236  if (yz0 > hit.Plane().val) {
237  y0 = hit.Cell().val;
238  yz0 = hit.Plane().val;
239  }
240  if (yz1 < hit.Plane().val) {
241  y1 = hit.Cell().val;
242  yz1 = hit.Plane().val;
243  }
244 
245  }
246  merged_hit_lists->push_back(merged_hit_list);
247 
248  ++_nMergedTracks;
249 
250  start.SetXYZ(x0,
251  y0,
252  std::min(xz0,yz0));
253  end.SetXYZ( x1,
254  y1,
255  std::max(xz1,yz1));
256 
257  novaddt::Track3D merged_track = Track3D(true, 2, start, end);
258  merged_tracks->push_back(merged_track);
259  break;
260  }
261  }
262  }
263 
264 }
265 
266 //------------------------------------------------------------------------
268 {
269  std::cout << "=== novaddt::DDTTrackMerge endJob" << std::endl;
270  std::cout << "\tNumber of slices seen: " << _nSlices << std::endl;
271  std::cout << "\tNumber of x tracks seen: " << _nXTracks << std::endl;
272  std::cout << "\tNumber of y tracks seen: " << _nYTracks << std::endl;
273  std::cout << "\tNumber of merged tracks made: " << _nMergedTracks << std::endl;
274 }
275 //------------------------------------------------------------------------
T max(const caf::Proxy< T > &a, T b)
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.
fvar< T > fabs(const fvar< T > &x)
Definition: fabs.hpp:15
Float_t y1[n_points_granero]
Definition: compare.C:5
DDTTrackMerge(fhicl::ParameterSet const &p)
Float_t x1[n_points_granero]
Definition: compare.C:5
std::vector< DAQHit > HitList
Definition: HitList.h:15
const char * p
Definition: xmltok.h:285
std::string _sliceModuleLabel
label of module making the HitList
DEFINE_ART_MODULE(TestTMapFile)
int _nSlices
number of slices seen
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:441
std::string _sliceInstanceLabel
instance label making the HitList
Identifier for the X measuring view of the detector (top)
void simpleMerge(std::vector< art::Ptr< novaddt::Track > > tracks, std::unique_ptr< std::vector< novaddt::Track3D > > &merged_tracks, std::unique_ptr< std::vector< novaddt::HitList > > &hitListcol, art::FindOneP< novaddt::HitList > fohl)
bool filter(art::Event &e) override
const double j
Definition: BetheBloch.cxx:29
std::string _trackToSliceInstanceLabel
instance of track to slice association produced by track module
Definition: View.py:1
std::string _trackModuleLabel
label of module making the Tracks
OStream cout
Definition: OStream.cxx:6
int _nXTracks
number of x tracks seen
Definition: structs.h:12
int _nYTracks
number of y tracks seen
assert(nhit_max >=nhit_nbins)
int _nMergedTracks
number of merged tracks produced
std::string _trackInstanceLabel
instance label making the Tracks
T min(const caf::Proxy< T > &a, T b)
Float_t e
Definition: plot.C:35
Definition: fwd.h:28
enum BeamMode string