28 : fX(x), fY(y), fZ(z), fCWire(cwire), fWire1(wire1), fWire2(wire2), fPred(0), fNeiPotential(0)
37 nei.fSC->fNeiPotential += dq * nei.fCoupling;
45 : fChannel(chan), fCharge(q), fCrossings(cross)
48 std::cout <<
"Trying to construct collection wire with negative charge " << q
49 <<
" this should never happen." << std::endl;
53 const double p = q / cross.size();
82 double Metric(
const std::vector<SpaceCharge*>& scs,
double alpha)
86 std::set<InductionWireHit*> iwires;
88 if (
sc->fWire1) iwires.insert(
sc->fWire1);
89 if (
sc->fWire2) iwires.insert(
sc->fWire2);
92 ret -= alpha *
sqr(
sc->fPred);
95 ret -= alpha *
sc->fPred *
sc->fNeiPotential;
100 ret +=
Metric(iwire->fCharge, iwire->fPred);
107 double Metric(
const std::vector<CollectionWireHit*>& cwires,
double alpha)
109 std::vector<SpaceCharge*> scs;
111 scs.insert(scs.end(), cwire->fCrossings.begin(), cwire->fCrossings.end());
112 return Metric(scs, alpha);
124 const double scip = sci->
fPred;
125 const double scjp = scj->
fPred;
128 ret -= alpha *
sqr(scip + x);
129 ret -= alpha *
sqr(scjp - x);
140 ret += 2 * alpha * (scip +
x) * scjp * n.
fCoupling;
141 ret += 2 * alpha * (scjp - x) * scip * n.
fCoupling;
144 ret -= 2 * alpha * (scip +
x) * (scjp - x) * n.
fCoupling;
153 const double qi1 = iwire1 ? iwire1->
fCharge : 0;
154 const double pi1 = iwire1 ? iwire1->
fPred : 0;
156 const double qj1 = jwire1 ? jwire1->
fCharge : 0;
157 const double pj1 = jwire1 ? jwire1->
fPred : 0;
159 if (iwire1 == jwire1) {
161 if (iwire1) ret +=
Metric(qi1, pi1);
164 if (iwire1) ret +=
Metric(qi1, pi1 + x);
165 if (jwire1) ret +=
Metric(qj1, pj1 - x);
171 const double qi2 = iwire2 ? iwire2->
fCharge : 0;
172 const double pi2 = iwire2 ? iwire2->
fPred : 0;
174 const double qj2 = jwire2 ? jwire2->
fCharge : 0;
175 const double pj2 = jwire2 ? jwire2->
fPred : 0;
177 if (iwire2 == jwire2) {
178 if (iwire2) ret +=
Metric(qi2, pi2);
181 if (iwire2) ret +=
Metric(qi2, pi2 + x);
182 if (jwire2) ret +=
Metric(qj2, pj2 - x);
197 const double scp = sc->
fPred;
200 ret -= alpha *
sqr(scp + x);
218 const double chisq0 = chisq.
Eval(0);
224 const double xmin = -sci->
fPred;
225 const double xmax = scj->
fPred;
228 x = std::min(xmax, x);
229 x = std::max(xmin, x);
231 const double chisq_new = chisq.
Eval(x);
236 const double chisq_p = chisq.
Eval(xmax);
237 const double chisq_n = chisq.
Eval(xmin);
239 if (std::min(std::min(chisq_p, chisq_n), chisq_new) > chisq0 + 1) {
240 std::cout <<
"Solution at " << x <<
" is worse than current state! Scan from " << xmin <<
" to " 241 << xmax << std::endl;
242 for (
double x = xmin; x < xmax; x += .01 * (xmax - xmin)) {
243 std::cout << x <<
" " << chisq.
Eval(x) << std::endl;
246 std::cout <<
"Soln, original, up edge, low edge:" << std::endl;
247 std::cout << chisq_new <<
" " << chisq0 <<
" " << chisq_p <<
" " << chisq_n << std::endl;
251 if (std::min(chisq_n, chisq_p) < chisq_new) {
252 if (chisq_n < chisq_p)
return xmin;
263 const unsigned int N = cwire->
fCrossings.size();
265 for (
unsigned int i = 0; i + 1 < N; ++i) {
268 for (
unsigned int j = i + 1; j < N; ++j) {
273 if (x == 0)
continue;
291 const double xmin = -sc->
fPred;
294 x = std::max(xmin, x);
296 const double chisq_new = chisq.
Eval(x);
301 const double chisq_n = chisq.
Eval(xmin);
303 if (chisq_n < chisq_new)
310 void Iterate(
const std::vector<CollectionWireHit*>& cwires,
311 const std::vector<SpaceCharge*>& orphanSCs,
316 unsigned int cwireIdx = 0;
317 if (!cwires.empty()) {
319 Iterate(cwires[cwireIdx], alpha);
321 const unsigned int prime = 1299827;
322 cwireIdx = (cwireIdx + prime) % cwires.size();
323 }
while (cwireIdx != 0);
void AddCharge(double dq)
double SolvePair(SpaceCharge *sci, SpaceCharge *scj, double alpha)
std::vector< Neighbour > fNeighbours
SpaceCharge(double x, double y, double z, CollectionWireHit *cwire, InductionWireHit *wire1, InductionWireHit *wire2)
double fNeiPotential
Neighbour-induced potential.
void Iterate(CollectionWireHit *cwire, double alpha)
std::vector< SpaceCharge * > fCrossings
InductionWireHit * fWire2
InductionWireHit * fWire1
double Metric(double q, double p)
Neighbour(SpaceCharge *sc, double coupling)
InductionWireHit(int chan, double q)
double Eval(double x) const
Vector cross(Vector const &a, Vector const &b)
Return cross product of two vectors.
CollectionWireHit(int chan, double q, const std::vector< SpaceCharge * > &cross)