56 Data[barID].NextBar.Clear();
57 Data[barID].NextBarLog.clear();
60 Data[barID].NextPlane[b].Clear();
61 Data[barID].NextPlaneLog[b].clear();
71 Data[barID - 1].NextBar.Clear();
72 Data[barID - 1].NextBarLog.clear();
81 Data[b].NextPlane[bar].Clear();
82 Data[b].NextPlaneLog[bar].clear();
91 if (HitMask[plane] == 0UL)
96 if ((HitMask[plane] & (1UL << bar)) == 0UL)
100 auto& barData = Data[barID];
102 if ((bar <
BarsPerPlane - 1) && (HitMask[plane] & (1UL << (bar + 1))))
104 auto tdiff = EventData[barID + 1] - EventData[barID];
115 barData.NextBarLog.push_back(tdiff);
118 SamplingHistogram.Reset();
119 for (
auto t : barData.NextBarLog)
120 SamplingHistogram.Fill(t);
121 const auto max = SamplingHistogram.GetBinCenter(SamplingHistogram.GetMaximumBin());
122 barData.NextBar = TH1F(
"",
"",
NBins, max - 10, max + 10);
123 barData.NextBar.SetDirectory(
nullptr);
124 for (
auto t : barData.NextBarLog)
125 barData.NextBar.Fill(t);
130 barData.NextBar.Fill(tdiff);
134 const auto nextPlane = plane + 1;
138 for (
auto barInNextPlane = 0; barInNextPlane <
BarsPerPlane; ++barInNextPlane)
140 if ((HitMask[nextPlane] & (1UL << barInNextPlane)) == 0UL)
143 const auto nextPlaneBarID = nextPlane *
BarsPerPlane + barInNextPlane;
144 auto tdiff = EventData[nextPlaneBarID] - EventData[barID];
155 barData.NextPlaneLog[barInNextPlane].push_back(tdiff);
158 SamplingHistogram.Reset();
159 for (
auto t : barData.NextPlaneLog[barInNextPlane])
160 SamplingHistogram.Fill(t);
161 const auto max = SamplingHistogram.GetBinCenter(SamplingHistogram.GetMaximumBin());
162 barData.NextPlane[barInNextPlane] = TH1F(
"",
"",
NBins, max - 25, max + 25);
163 barData.NextPlane[barInNextPlane].SetDirectory(
nullptr);
164 for (
auto t : barData.NextPlaneLog[barInNextPlane])
165 barData.NextPlane[barInNextPlane].Fill(t);
170 barData.NextPlane[barInNextPlane].Fill(tdiff);
174 HitMask[plane] = 0UL;
182 std::vector<ValueErrorPair> solution(nBars, {
NaN,
NaN });
183 std::vector<Double_t> scaleFactors(nBars, 0.);
187 UInt_t numberOfEquations = 0;
188 for (
auto b = 0; b < nBars; ++b)
190 if (!std::isnan(Data[b].TSyncNextBar.Value))
192 scaleFactors[b] += 1. /
Sqr(Data[b].TSyncNextBar.Error);
193 scaleFactors[b + 1] += 1. /
Sqr(Data[b].TSyncNextBar.Error);
203 if (!std::isnan(Data[b].TSyncNextPlane[ob].Value))
205 scaleFactors[b] += 1. /
Sqr(Data[b].TSyncNextPlane[ob].Error);
206 scaleFactors[plane *
BarsPerPlane + ob] += 1. /
Sqr(Data[b].TSyncNextPlane[ob].Error);
214 if (numberOfEquations < nBars)
216 LOG(info) <<
"Can not synchronize NeuLAND. Not enough equations (" << numberOfEquations <<
").";
220 for (
auto b = 0; b < nBars; ++b)
221 scaleFactors[b] = 1. / sqrt(scaleFactors[b]);
232 std::array<UInt_t, 2> Index;
233 std::array<Double_t, 2> Value;
236 std::vector<Element> lhs(numberOfEquations);
238 auto function = [&lhs, numberOfVariables](
241 double* x = xVec->elements;
242 double* y = yVec->elements;
246 for (uint r = 0; r < lhs.size(); ++r)
248 *y += x[lhs[r].Index[0]] * lhs[r].Value[0] + x[lhs[r].Index[1]] * lhs[r].Value[1];
255 for (uint r = 0; r < lhs.size(); ++r)
257 x[lhs[r].Index[0]] += (*y) * lhs[r].Value[0];
258 x[lhs[r].Index[1]] += (*y) * lhs[r].Value[1];
265 y = yVec->elements + lhs.size();
269 for (
auto i = 0; i < numberOfVariables; i++)
274 for (
auto i = 0; i < numberOfVariables; i++)
279 alloc_lsqr_mem(&input, &output, &work, numberOfEquations + 1, nBars);
281 input->
num_rows = numberOfEquations;
291 for (UInt_t
id = 0;
id < Data.size(); ++id)
294 if (!std::isnan(Data[
id].TSyncNextBar.Value))
296 const auto weight = 1. / Data[id].TSyncNextBar.Error;
297 lhs[nEQ] = { { id,
id + 1U }, { -weight * scaleFactors[id], weight * scaleFactors[
id + 1U] } };
301 for (UInt_t barInNextPlane = 0; barInNextPlane <
BarsPerPlane; ++barInNextPlane)
303 if (!std::isnan(Data[
id].TSyncNextPlane[barInNextPlane].Value))
305 const auto barInNextPlaneID =
BarsPerPlane * (plane + 1) + barInNextPlane;
306 const auto weight = 1. / Data[id].TSyncNextPlane[barInNextPlane].Error;
307 lhs[nEQ] = { { id, barInNextPlaneID },
308 { -weight * scaleFactors[id], weight * scaleFactors[barInNextPlaneID] } };
309 input->
rhs_vec->
elements[nEQ] = Data[id].TSyncNextPlane[barInNextPlane].Value * weight;
315 for (
auto bar = 0; bar < nBars; ++bar)
320 LOG(debug) <<
"Syncing Neuland with " << numberOfEquations <<
" equations...";
322 lsqr(input, output, work, function, &lhs);
324 for (
auto id = 0;
id < nBars; ++id)
void lsqr(lsqr_input *input, lsqr_output *output, lsqr_work *work, std::function< void(long, dvec *, dvec *, void *)> mat_vec_prod, void *prod)