9 template<
class T> T
sqr(T
x){
return x*
x;}
13 : fChannel(chan), fCharge(q), fPred(0)
19 : fSC(sc), fCoupling(coupling)
27 : fX(x), fY(y), fZ(z),
28 fCWire(cwire), fWire1(wire1), fWire2(wire2),
40 nei.fSC->fNeiPotential += dq * nei.fCoupling;
48 const std::vector<SpaceCharge*>&
cross)
49 : fChannel(chan), fCharge(q), fCrossings(cross)
51 const double p = q/cross.size();
55 if(iwires->fWire1) iwires->fWire1->fPred += p;
56 if(iwires->fWire2) iwires->fWire2->fPred += p;
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 if(iwire1 && jwire1){
154 const double qi1 = iwire1->
fCharge;
155 const double pi1 = iwire1->
fPred;
157 const double qj1 = jwire1->
fCharge;
158 const double pj1 = jwire1->
fPred;
160 if(iwire1 == jwire1){
164 ret +=
Metric(qi1, pi1 + x);
165 ret +=
Metric(qj1, pj1 - x);
171 if(iwire2 && jwire2){
172 const double qi2 = iwire2->
fCharge;
173 const double pi2 = iwire2->
fPred;
175 const double qj2 = jwire2->
fCharge;
176 const double pj2 = jwire2->
fPred;
178 if(iwire2 == jwire2){
182 ret +=
Metric(qi2, pi2 + x);
183 ret +=
Metric(qj2, pj2 - x);
196 const double chisq0 = chisq.
Eval(0);
202 const double xmin = -sci->
fPred;
203 const double xmax = scj->
fPred;
209 const double chisq_new = chisq.
Eval(x);
214 const double chisq_p = chisq.
Eval(xmax);
215 const double chisq_n = chisq.
Eval(xmin);
218 for(
double x = xmin; x < xmax; x += .01*(xmax-xmin)){
219 std::cout << x <<
" " << chisq.
Eval(x) << std::endl;
222 std::cout << chisq_new <<
" " << chisq0 <<
" " << chisq_p <<
" " << chisq_n << std::endl;
226 if(
std::min(chisq_n, chisq_p) < chisq_new){
227 if(chisq_n < chisq_p)
return xmin;
238 const unsigned int N = cwire->
fCrossings.size();
240 for(
unsigned int i = 0; i+1 < N; ++i){
246 for(
unsigned int j = i+1; j < N; ++j){
253 if(iwire1 == jwire2 || iwire2 == jwire1) abort();
256 if(iwire1 == jwire1 && iwire2 == jwire2)
continue;
258 const double x =
SolvePair(cwire, sci, scj, alpha);
270 void Iterate(
const std::vector<CollectionWireHit*>& cwires,
void AddCharge(double dq)
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
double SolvePair(CollectionWireHit *cwire, SpaceCharge *sci, SpaceCharge *scj, double alpha)
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)