TrackDepos.cxx
Go to the documentation of this file.
4 
5 #include "WireCellUtil/Point.h"
6 #include "WireCellUtil/Units.h"
7 #include "WireCellUtil/Testing.h"
8 #include "WireCellUtil/Persist.h"
9 
10 #include <sstream>
11 
14 
15 using namespace std;
16 using namespace WireCell;
17 
18 Gen::TrackDepos::TrackDepos(double stepsize, double clight)
19  : m_stepsize(stepsize)
20  , m_clight(clight)
21  , m_count(0)
22  , l(Log::logger("sim"))
23 {
24 }
25 
26 Gen::TrackDepos::~TrackDepos()
27 {
28 }
29 
31 {
33  cfg["step_size"] = 1.0*units::mm;
34  cfg["clight"] = 1.0; // fraction of speed of light the track goes
35  cfg["tracks"] = Json::arrayValue;
36  cfg["group_time"] = -1; // if positive then chunk the
37  // stream of output depos into
38  // groups delineated by EOS
39  // markers and such that each
40  // group spans a time period no
41  // more than group_time.
42  return cfg;
43 }
44 
46 {
47  m_stepsize = get<double>(cfg, "step_size", m_stepsize);
48  Assert(m_stepsize > 0);
49  m_clight = get<double>(cfg, "clight", m_clight);
50  for (auto track : cfg["tracks"]) {
51  double time = get<double>(track, "time", 0.0);
52  double charge = get<double>(track, "charge", -1.0);
53  Ray ray = get<Ray>(track, "ray");
54  add_track(time, ray, charge);
55  }
56  double gt = get<double>(cfg, "group_time", -1);
57  if (m_depos.empty() or gt <= 0.0) {
58  m_depos.push_back(nullptr);
59  return;
60  }
61  std::deque<WireCell::IDepo::pointer> grouped;
62  double now = m_depos.front()->time();
63  double end = now + gt;
64  for (auto depo : m_depos) {
65  if (depo->time() < end) {
66  grouped.push_back(depo);
67  continue;
68  }
69  grouped.push_back(nullptr);
70  now = depo->time();
71  end = now + gt;
72  grouped.push_back(depo);
73  }
74  grouped.push_back(nullptr);
75  m_depos = grouped;
76 }
77 
79 {
80  std::stringstream ss;
81  ss << "q=" << d->charge()/units::eplus << "eles, t=" << d->time()/units::us << "us, r=" << d->pos()/units::mm << "mm";
82  return ss.str();
83 }
84 
85 void Gen::TrackDepos::add_track(double time, const WireCell::Ray& ray, double charge)
86 {
87  l->debug("add_track({} us, ({} -> {})cm, {})",
88  time/units::us, ray.first/units::cm, ray.second/units::cm, charge);
89  m_tracks.push_back(track_t(time, ray, charge));
90 
92  const double length = WireCell::ray_length(ray);
93  double step = 0;
94  int count = 0;
95 
96  double charge_per_depo = units::eplus; // charge of one positron
97  if (charge > 0) {
98  charge_per_depo = -charge / (length / m_stepsize);
99  }
100  else if (charge <= 0) {
101  charge_per_depo = charge;
102  }
103 
104  while (step < length) {
105  const double now = time + step/(m_clight*units::clight);
106  const WireCell::Point here = ray.first + dir * step;
107  SimpleDepo* sdepo = new SimpleDepo(now, here, charge_per_depo);
108  m_depos.push_back(WireCell::IDepo::pointer(sdepo));
109  step += m_stepsize;
110  ++count;
111  }
112 
113  // earliest first
114  std::sort(m_depos.begin(), m_depos.end(), ascending_time);
115  l->debug("depos: {} over {}mm", m_depos.size(), length/units::mm);
116 }
117 
118 
120 {
121  if (m_depos.empty()) {
122  return false;
123  }
124  out = m_depos.front();
125  m_depos.pop_front();
126 
127  if (!out) { // chirp
128  l->debug("EOS at call {}", m_count);
129  }
130 
131  ++m_count;
132  return true;
133 }
134 
135 
136 
138 {
139  return WireCell::IDepo::vector(m_depos.begin(), m_depos.end());
140 }
std::pair< Point, Point > Ray
A line segment running from a first (tail) to a second (head) point.
Definition: Point.h:21
float clight
Definition: units.py:274
std::shared_ptr< const IDepo > pointer
Definition: IData.h:19
static const double eplus
Definition: Units.h:110
void add_track(double time, const WireCell::Ray &ray, double dedx=-1.0)
Definition: TrackDepos.cxx:85
std::string string
Definition: nybbler.cc:12
virtual bool operator()(IDepo::pointer &out)
ISourceNode.
Definition: TrackDepos.cxx:119
static const double clight
Definition: Units.h:275
STL namespace.
cfg
Definition: dbjson.py:29
std::vector< pointer > vector
Definition: IData.h:21
string dir
static QStrList * l
Definition: config.cpp:1044
std::shared_ptr< const IDepo > output_pointer
Definition: ISourceNode.h:38
A producer of depositions created from some number of simple, linear tracks.
Definition: TrackDepos.h:17
#define Assert
Definition: Testing.h:7
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:72
double ray_length(const Ray &ray)
Definition: Point.cxx:62
std::deque< WireCell::IDepo::pointer > m_depos
Definition: TrackDepos.h:46
virtual void configure(const WireCell::Configuration &config)
Accept a configuration.
Definition: TrackDepos.cxx:45
WireCell::IDepo::vector depos()
Definition: TrackDepos.cxx:137
Vector ray_unit(const Ray &ray)
Definition: Point.cxx:71
std::vector< track_t > m_tracks
Definition: TrackDepos.h:47
static const double cm
Definition: Units.h:59
Monte Carlo Simulation.
logptr_t logger(std::string name)
Definition: Logging.cxx:71
static const double mm
Definition: Units.h:55
Definition: Main.h:22
bool ascending_time(const WireCell::IDepo::pointer &lhs, const WireCell::IDepo::pointer &rhs)
Compare two IDepo::pointer by time (ascending). x is used to break tie.
Definition: IDepo.cxx:17
Json::Value Configuration
Definition: Configuration.h:50
static std::string dump(IDepo::pointer d)
Definition: TrackDepos.cxx:78
static const double us
Definition: Units.h:105
virtual WireCell::Configuration default_configuration() const
Optional, override to return a hard-coded default configuration.
Definition: TrackDepos.cxx:30
WIRECELL_FACTORY(TrackDepos, WireCell::Gen::TrackDepos, WireCell::IDepoSource, WireCell::IConfigurable) using namespace std
std::tuple< double, Ray, double > track_t
Definition: TrackDepos.h:40