test_drifter.cxx
Go to the documentation of this file.
2 
6 #include "WireCellUtil/Units.h"
7 #include "WireCellUtil/Testing.h"
8 
10 #include "WireCellIface/IRandom.h"
11 
12 #include "TCanvas.h"
13 #include "TView.h"
14 #include "TPolyLine3D.h"
15 #include "TPolyMarker3D.h"
16 #include "TColor.h"
17 
18 using namespace WireCell;
19 using namespace std;
20 
22  // warning, this bipases some logic in TrackDepos::configure().
23  Gen::TrackDepos td;
24  td.add_track( 10*units::us, Ray(Point(-10,0,0)*units::cm, Point(-100,0,10)*units::cm));
25  td.add_track(120*units::us, Ray(Point( -1,0,0)*units::cm, Point( -2,-100,0)*units::cm));
26  td.add_track( 99*units::us, Ray(Point(-130,50,50)*units::cm, Point(-11, -50,-30)*units::cm));
27  return td;
28 }
30 {
32  IDepo::vector* ret = new IDepo::vector;
33  while (true) {
34  IDepo::pointer depo;
35  bool ok = td(depo);
36  if (!ok or !depo) {
37  break;
38  }
39  ret->push_back(depo);
40  }
41  return IDepo::shared_vector(ret);
42 }
43 
44 std::vector<IDepo::pointer> track_depos(double t, const Ray& ray)
45 {
46  Gen::TrackDepos td;
47  td.add_track(t, ray);
48  std::vector<IDepo::pointer> ret;
49  while (true) {
50  IDepo::pointer depo;
51  bool ok = td(depo);
52  ret.push_back(depo);
53  if (!ok or !depo) {
54  return ret;
55  }
56  }
57 }
58 
59 std::deque<IDepo::pointer> test_depos(const std::vector<IDepo::pointer>& depos, std::string tn="Drifter");
60 std::deque<IDepo::pointer> test_depos(const std::vector<IDepo::pointer>& depos, std::string tn)
61 {
62  AssertMsg(depos.back() == nullptr, "Not given EOS terminated stream of depos");
63  auto drifter = Factory::find_tn<IDrifter>(tn);
65  for (auto depo: depos) {
67  if (!depo) {
68  cerr << "test_depos: sending EOS to drifter\n";
69  }
70  bool ok = (*drifter)(depo, outq);
71  Assert(ok);
72  if (outq.size()) {
73  cerr << "test_depos: got " << outq.size() << " drifted\n";
74  }
75  drifted.insert(drifted.end(), outq.begin(), outq.end());
76  }
77  AssertMsg(drifted.back() == nullptr, "Drifter did not pass on EOS");
78 
79  for (auto out : drifted) {
80  if (!out) { break; }
82  AssertMsg(vec.size() > 1, "The history of the drifted deposition is truncated.");
83  }
84 
85  cerr << "test_depos: start with: " << depos.size()
86  << ", after drifting have: " << drifted.size() << endl;
87  return drifted;
88 }
89 
90 void test_tracks(std::string tn="Drifter");
92 {
93  std::vector<std::pair<double, double> > endpoints = {
94  std::pair<double,double>{-1*units::cm, 1*units::cm },
95  { 20*units::cm, 21*units::cm},
96  { -20*units::cm, -21*units::cm},
97  { -10*units::cm+3*units::mm, -10*units::cm-3*units::mm},
98  { +10*units::cm+3*units::mm, +10*units::cm-3*units::mm},
99  { -2*units::m+3*units::mm, -2*units::m-3*units::mm},
100  { +2*units::m+3*units::mm, +2*units::m-3*units::mm}};
101 
102  for (auto& ends : endpoints) {
103  Ray ray(Point(ends.first, 0,0), Point(ends.second, 0,0));
104  auto depos = track_depos(0, ray);
105  cerr << "test_tracks: " << ray/units::cm << "cm produces "<<depos.size()<<"depos\n";
106  test_depos(depos, tn);
107  }
108 }
109 
110 void test_time(std::string tn="Drifter");
112 {
113  Ray ray(Point(1*units::m, 0,0), Point(1*units::m+1*units::cm, 0,0));
114  std::vector<IDepo::pointer> alldepos;
115  for (double t : {0.0, 10.0*units::us, 3*units::ms} ) {
116  auto depos = track_depos(t, ray);
117  Assert(depos.back() == nullptr);
118  depos.pop_back();
119  cerr << "test_time: " << t/units::us << "us produces "<<depos.size()<<"depos\n";
120  alldepos.insert(alldepos.end(), depos.begin(), depos.end());
121  }
122  alldepos.push_back(nullptr);
123  test_depos(alldepos, tn);
124 }
125 
126 void test_order(std::string tn="Drifter");
128 {
129  const double x1 = 1*units::m;
130  const double x2 = 12*units::cm;
131  const double t1 = 1*units::us;
132  const double t2 = (x1-x2)/(1.0*units::mm/units::us) - 3*units::us;
133  const double t3 = 3*units::ms;
134 
135  Ray ray1(Point(x1, 0,0), Point(x1+1*units::cm, 0,0));
136  Ray ray2(Point(x2, 0,0), Point(x2+1*units::cm, 0,0));
137 
138  auto depos1 = track_depos(t1, ray1);
139  auto depos2 = track_depos(t2, ray2);
140  auto depos3 = track_depos(t3, ray1);
141  std::vector<IDepo::pointer> alldepos;
142  for (auto depos : {depos1, depos2, depos3}) {
143  Assert(depos.back() == nullptr);
144  depos.pop_back();
145  alldepos.insert(alldepos.end(), depos.begin(), depos.end());
146  }
147  alldepos.push_back(nullptr);
148  auto drifted = test_depos(alldepos, tn);
149  for (auto& depo : drifted) {
150  if (!depo) {
151  break;
152  }
153  cerr << "t="<<depo->time()/units::us
154  << "("<<depo->prior()->time()/units::us<<")"
155  <<"us, x="<<depo->pos().x()/units::cm
156  << "(" <<depo->prior()->pos().x()/units::cm<<")cm\n";
157  }
158 }
159 
160 
161 
163 {
165  activity.push_back(nullptr); // EOS
166 
167  auto drifter = Factory::find_tn<IDrifter>(tn);
168 
170  for (auto in : activity) {
171  outq.clear();
172  if (!in) {
173  cerr << "test_drifter: sending EOS to drifter\n";
174  }
175  bool ok = (*drifter)(in, outq);
176  Assert(ok);
177  for (auto d : outq) {
178  result.push_back(d);
179  }
180  }
181  Assert(!result.back());
182 
183  for (auto out : result) {
184  if (!out) { break; }
186  AssertMsg(vec.size() > 1, "The history of the drifted deposition is truncated.");
187  }
188 
189  cerr << "test_drifter: start with: " << activity.size()
190  << ", after drifting have: " << result.size() << endl;
191 
192  return result;
193 }
194 
195 
197 {
198  BoundingBox bbox(Ray(Point(-1,-1,-1), Point(1,1,1)));
200  for (auto depo : activity) {
201  bbox(depo->pos());
202  }
203  Ray bb = bbox.bounds();
204  cout << "Bounds: " << bb.first << " --> " << bb.second << endl;
205  return bb;
206 }
207 
208 int main(int argc, char* argv[])
209 {
211  pm.add("WireCellGen");
212  {
213  auto icfg = Factory::lookup<IConfigurable>("Random");
214  auto cfg = icfg->default_configuration();
215  icfg->configure(cfg);
216  }
217  {
218  auto icfg = Factory::lookup<IConfigurable>("Drifter");
219  auto cfg = icfg->default_configuration();
220  //cerr << cfg << endl;
221  cfg["drift_speed"] = 1.0 * units::mm/units::us;
222  cfg["xregions"][0]["cathode"] = 2*units::m;
223  cfg["xregions"][0]["anode"] = 10*units::cm;
224  cfg["xregions"][1]["anode"] = -10*units::cm;
225  cfg["xregions"][1]["cathode"] = -2*units::m;
226  icfg->configure(cfg);
227  }
228 
229  test_tracks("Drifter");
230  test_time("Drifter");
231  test_order("Drifter");
232 
233  IDepo::vector drifted = test_drifted("Drifter");
234 
235  Ray bb = make_bbox();
236 
237 
238  TCanvas c("c","c",800,800);
239 
240  TView* view = TView::CreateView(1);
241  view->SetRange(bb.first.x(),bb.first.y(),bb.first.z(),
242  bb.second.x(),bb.second.y(),bb.second.z());
243  view->ShowAxis();
244 
245 
246  // draw raw activity
248  TPolyMarker3D orig(activity.size(), 6);
249  orig.SetMarkerColor(2);
250  int indx=0;
251  for (auto depo : activity) {
252  const Point& p = depo->pos();
253  orig.SetPoint(indx++, p.x(), p.y(), p.z());
254  }
255  orig.Draw();
256 
257  // draw drifted
258  double tmin=-1, tmax=-1;
259  for (auto depo : drifted) {
260  if (!depo) {
261  cerr << "Reached EOI"<< endl;
262  break;
263  }
264  auto history = depo_chain(depo);
265  Assert(history.size() > 1);
266 
267  if (tmin<0 && tmax<0) {
268  tmin = tmax = depo->time();
269  continue;
270  }
271  tmin = min(tmin, depo->time());
272  tmax = max(tmax, depo->time());
273  }
274  cerr << "Time bounds: " << tmin << " < " << tmax << endl;
275 
276  for (auto depo : drifted) {
277  if (!depo) {
278  cerr << "Reached EOI"<< endl;
279  break;
280  }
281 
282 
283  TPolyMarker3D* pm = new TPolyMarker3D(1,8);
284  const Point& p = depo->pos();
285  pm->SetPoint(0, p.x(), p.y(), p.z());
286 
287  double rel = depo->time()/(tmax-tmin);
288  int col = TColor::GetColorPalette( int(rel*TColor::GetNumberOfColors()) );
289  pm->SetMarkerColor(col);
290 
291  pm->Draw();
292  }
293 
294  c.Print(String::format("%s.pdf", argv[0]).c_str());
295 
296 
297  return 0;
298 }
std::pair< Point, Point > Ray
A line segment running from a first (tail) to a second (head) point.
Definition: Point.h:21
std::shared_ptr< const IDepo > pointer
Definition: IData.h:19
static const double m
Definition: Units.h:63
IDepo::vector depo_chain(IDepo::pointer recent)
Definition: IDepo.cxx:4
void add_track(double time, const WireCell::Ray &ray, double dedx=-1.0)
Definition: TrackDepos.cxx:85
static QCString result
Gen::TrackDepos make_tracks()
D3Vector< double > Point
A 3D Cartesian point in double precision.
Definition: Point.h:15
Ray make_bbox()
IDepo::vector test_drifted(std::string tn)
std::string string
Definition: nybbler.cc:12
std::deque< IDepo::pointer > test_depos(const std::vector< IDepo::pointer > &depos, std::string tn="Drifter")
void test_order(std::string tn="Drifter")
STL namespace.
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
static PluginManager & instance()
static const double ms
Definition: Units.h:104
std::deque< output_pointer > output_queue
A producer of depositions created from some number of simple, linear tracks.
Definition: TrackDepos.h:17
#define Assert
Definition: Testing.h:7
def activity(output, slices, slice_line, cluster_tap_file)
Definition: main.py:144
int main(int argc, char *argv[])
static int max(int a, int b)
static const double cm
Definition: Units.h:59
void test_tracks(std::string tn="Drifter")
static const double mm
Definition: Units.h:55
std::string format(const std::string &form, TYPES...objs)
Definition: String.h:45
Definition: Main.h:22
void test_time(std::string tn="Drifter")
p
Definition: test.py:223
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
#define AssertMsg
Definition: Testing.h:8
static const double us
Definition: Units.h:105
std::vector< IDepo::pointer > track_depos(double t, const Ray &ray)
Plugin * add(const std::string &plugin_name, const std::string &libname="")
Add a plugin. If libname is not given, try to derive it.
IDepo::shared_vector get_depos()
std::shared_ptr< const vector > shared_vector
Definition: IData.h:22
const Ray & bounds() const
Return the ray representing the bounds.
Definition: BoundingBox.h:36
QTextStream & endl(QTextStream &s)