forked from JustynaMedrala/NewAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitFinderTools.cpp
More file actions
384 lines (364 loc) · 12.1 KB
/
Copy pathHitFinderTools.cpp
File metadata and controls
384 lines (364 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* @copyright Copyright 2020 The J-PET Framework Authors. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may find a copy of the License in the LICENCE file.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @file HitFinderTools.cpp
*/
#include "HitFinderTools.h"
#include "UniversalFileLoader.h"
#include <TMath.h>
#include <cmath>
#include <map>
#include <vector>
using namespace tot_energy_converter;
using namespace std;
/**
* Helper method for sotring signals in vector
*/
void HitFinderTools::sortByTime(vector<JPetPhysSignal>& sigVec)
{
sort(sigVec.begin(), sigVec.end(), [](const JPetPhysSignal& sig1, const JPetPhysSignal& sig2) { return sig1.getTime() < sig2.getTime(); });
}
/**
* Method distributing Signals according to Scintillator they belong to
*/
map<int, vector<JPetPhysSignal>> HitFinderTools::getSignalsBySlot(const JPetTimeWindow* timeWindow, bool useCorrupts)
{
map<int, vector<JPetPhysSignal>> signalSlotMap;
if (!timeWindow)
{
WARNING("Pointer of Time Window object is not set, returning empty map");
return signalSlotMap;
}
const unsigned int nSignals = timeWindow->getNumberOfEvents();
for (unsigned int i = 0; i < nSignals; i++)
{
auto physSig = dynamic_cast<const JPetPhysSignal&>(timeWindow->operator[](i));
if (!useCorrupts && physSig.getRecoFlag() == JPetBaseSignal::Corrupted)
{
continue;
}
int slotID = physSig.getBarrelSlot().getID();
auto search = signalSlotMap.find(slotID);
if (search == signalSlotMap.end())
{
vector<JPetPhysSignal> tmp;
tmp.push_back(physSig);
signalSlotMap.insert(pair<int, vector<JPetPhysSignal>>(slotID, tmp));
}
else
{
search->second.push_back(physSig);
}
}
return signalSlotMap;
}
/**
* Loop over all Scins invoking matching procedure
*/
vector<JPetHit> HitFinderTools::matchAllSignals(map<int, vector<JPetPhysSignal>>& allSignals, const map<unsigned int, vector<double>>& velocitiesMap,
double timeDiffAB, int refDetScinId, bool convertToT, const ToTEnergyConverter& totConverter,
JPetStatistics& stats, bool saveHistos)
{
vector<JPetHit> allHits;
for (auto& slotSigals : allSignals)
{
// Loop for Reference Detector ID
if (slotSigals.first == refDetScinId)
{
for (auto refSignal : slotSigals.second)
{
auto refHit = createDummyRefDetHit(refSignal);
allHits.push_back(refHit);
}
continue;
}
// Loop for other slots than reference one
auto slotHits = matchSignals(slotSigals.second, velocitiesMap, timeDiffAB, convertToT, totConverter, stats, saveHistos);
allHits.insert(allHits.end(), slotHits.begin(), slotHits.end());
}
return allHits;
}
/**
* Method matching signals on the same Scintillator
*/
vector<JPetHit> HitFinderTools::matchSignals(vector<JPetPhysSignal>& slotSignals, const map<unsigned int, vector<double>>& velocitiesMap,
double timeDiffAB, bool convertToT, const ToTEnergyConverter& totConverter, JPetStatistics& stats,
bool saveHistos)
{
vector<JPetHit> slotHits;
vector<JPetPhysSignal> remainSignals;
sortByTime(slotSignals);
while (slotSignals.size() > 0)
{
auto physSig = slotSignals.at(0);
if (slotSignals.size() == 1)
{
remainSignals.push_back(physSig);
break;
}
for (unsigned int j = 1; j < slotSignals.size(); j++)
{
if (slotSignals.at(j).getTime() - physSig.getTime() < timeDiffAB)
{
if (physSig.getPM().getSide() != slotSignals.at(j).getPM().getSide())
{
auto hit = createHit(physSig, slotSignals.at(j), velocitiesMap, convertToT, totConverter, stats, saveHistos);
slotHits.push_back(hit);
slotSignals.erase(slotSignals.begin() + j);
slotSignals.erase(slotSignals.begin() + 0);
break;
}
else
{
if (j == slotSignals.size() - 1)
{
remainSignals.push_back(physSig);
slotSignals.erase(slotSignals.begin() + 0);
break;
}
else
{
continue;
}
}
}
else
{
if (saveHistos && physSig.getPM().getSide() != slotSignals.at(j).getPM().getSide())
{
stats.fillHistogram("remain_signals_tdiff", slotSignals.at(j).getTime() - physSig.getTime());
}
remainSignals.push_back(physSig);
slotSignals.erase(slotSignals.begin() + 0);
break;
}
}
}
if (remainSignals.size() > 0 && saveHistos)
{
stats.fillHistogram("remain_signals_per_scin", (float)(remainSignals.at(0).getPM().getScin().getID()), remainSignals.size());
}
return slotHits;
}
/**
* Method for Hit creation - setting all fields, that make sense here
*/
JPetHit HitFinderTools::createHit(const JPetPhysSignal& signal1, const JPetPhysSignal& signal2,
const map<unsigned int, vector<double>>& velocitiesMap, bool convertToT, const ToTEnergyConverter& totConverter,
JPetStatistics& stats, bool saveHistos)
{
JPetPhysSignal signalA;
JPetPhysSignal signalB;
if (signal1.getPM().getSide() == JPetPM::SideA)
{
signalA = signal1;
signalB = signal2;
}
else
{
signalA = signal2;
signalB = signal1;
}
auto radius = signalA.getPM().getBarrelSlot().getLayer().getRadius();
auto theta = TMath::DegToRad() * signalA.getPM().getBarrelSlot().getTheta();
auto velocity = UniversalFileLoader::getConfigurationParameter(velocitiesMap, getProperChannel(signalA));
checkTheta(theta);
JPetHit hit;
hit.setSignalA(signalA);
hit.setSignalB(signalB);
hit.setTime((signalA.getTime() + signalB.getTime()) / 2.0);
hit.setQualityOfTime(-1.0);
hit.setTimeDiff(signalB.getTime() - signalA.getTime());
hit.setQualityOfTimeDiff(-1.0);
hit.setScintillator(signalA.getPM().getScin());
hit.setBarrelSlot(signalA.getPM().getBarrelSlot());
hit.setPosX(radius * cos(theta));
hit.setPosY(radius * sin(theta));
hit.setPosZ(velocity * hit.getTimeDiff() / 2000.0);
if (convertToT)
{
auto tot = calculateTOT(hit);
/// Checking if provided conversion function accepts calculated value of ToT
if (tot > totConverter.getRange().first && tot < totConverter.getRange().second)
{
auto energy = totConverter(tot);
if (!isnan(energy))
{
/// ToDo change setEnergy() to setTOT() once it is available
hit.setEnergy(energy);
stats.fillHistogram("conv_tot_range", tot);
stats.fillHistogram("conv_dep_energy", energy);
stats.fillHistogram("conv_dep_energy_vs_tot", energy, tot);
}
else
{
hit.setEnergy(-1.0);
}
}
else
{
hit.setEnergy(-1.0);
}
}
else
{
hit.setEnergy(-1.0);
}
hit.setQualityOfEnergy(-1.0);
if (signalA.getRecoFlag() == JPetBaseSignal::Good && signalB.getRecoFlag() == JPetBaseSignal::Good)
{
hit.setRecoFlag(JPetHit::Good);
if (saveHistos)
{
stats.fillHistogram("good_vs_bad_hits", 1);
stats.fillHistogram("time_diff_per_scin", hit.getTimeDiff(), (float)(hit.getScintillator().getID()));
stats.fillHistogram("hit_pos_per_scin", hit.getPosZ(), (float)(hit.getScintillator().getID()));
}
}
else if (signalA.getRecoFlag() == JPetBaseSignal::Corrupted || signalB.getRecoFlag() == JPetBaseSignal::Corrupted)
{
hit.setRecoFlag(JPetHit::Corrupted);
if (saveHistos)
{
stats.fillHistogram("good_vs_bad_hits", 2);
}
}
else
{
hit.setRecoFlag(JPetHit::Unknown);
if (saveHistos)
{
stats.fillHistogram("good_vs_bad_hits", 3);
}
}
return hit;
}
/**
* Method for Hit creation in case of reference detector.
* Setting only some necessary fields.
*/
JPetHit HitFinderTools::createDummyRefDetHit(const JPetPhysSignal& signalB)
{
JPetHit hit;
hit.setSignalB(signalB);
hit.setTime(signalB.getTime());
hit.setQualityOfTime(-1.0);
hit.setTimeDiff(0.0);
hit.setQualityOfTimeDiff(-1.0);
hit.setEnergy(-1.0);
hit.setQualityOfEnergy(-1.0);
hit.setScintillator(signalB.getPM().getScin());
hit.setBarrelSlot(signalB.getPM().getBarrelSlot());
return hit;
}
/**
* Helper method for getting TOMB channel
*/
int HitFinderTools::getProperChannel(const JPetPhysSignal& signal)
{
auto someSigCh = signal.getRecoSignal().getRawSignal().getPoints(JPetSigCh::Leading, JPetRawSignal::ByThrValue)[0];
return someSigCh.getTOMBChannel().getChannel();
}
/**
* Helper method for checking if theta is in radians
*/
void HitFinderTools::checkTheta(const double& theta)
{
if (theta > 2 * TMath::Pi())
{
WARNING("Probably wrong values of Barrel Slot theta - conversion to radians failed. Check please.");
}
}
/**
* Calculation of the total TOT of the hit - Time over Threshold:
* the weighted sum of the TOTs on all of the thresholds (1-4) and on the both sides (A,B)
*/
HitFinderTools::TOTCalculationType HitFinderTools::getTOTCalculationType(const std::string& type)
{
if (type == "rectangular")
{
return TOTCalculationType::kThresholdRectangular;
}
else if (type == "trapeze")
{
return TOTCalculationType::kThresholdTrapeze;
}
else if (type == "standard")
{
return TOTCalculationType::kSimplified;
}
else
{
WARNING("Undefinied type for the calculation of the TOTs. Probably missing option in userParams, typo, or mistake.");
return TOTCalculationType::kSimplified;
}
}
double HitFinderTools::calculateTOT(const JPetHit& hit, TOTCalculationType type)
{
double tot = 0.0;
std::map<int, double> thrToTOT_sideA = hit.getSignalA().getRecoSignal().getRawSignal().getTOTsVsThresholdValue();
std::map<int, double> thrToTOT_sideB = hit.getSignalB().getRecoSignal().getRawSignal().getTOTsVsThresholdValue();
tot += calculateTOTside(thrToTOT_sideA, type);
tot += calculateTOTside(thrToTOT_sideB, type);
return tot;
}
double HitFinderTools::calculateTOTside(const std::map<int, double>& thrToTOT_side, TOTCalculationType type)
{
double tot = 0., weight = 1.;
if (!thrToTOT_side.empty())
{
double firstThr = thrToTOT_side.begin()->first;
tot += weight * thrToTOT_side.begin()->second;
if (thrToTOT_side.size() > 1)
{
for (auto it = std::next(thrToTOT_side.begin(), 1); it != thrToTOT_side.end(); it++)
{
switch (type)
{
case TOTCalculationType::kSimplified:
weight = 1.;
break;
case TOTCalculationType::kThresholdRectangular:
weight = (it->first - std::prev(it, 1)->first) / firstThr;
break;
case TOTCalculationType::kThresholdTrapeze:
weight = (it->first - std::prev(it, 1)->first) / firstThr;
tot += weight * fabs(std::prev(it, 1)->second - it->second) / 2; // fabs, because we do not know which TOT is lower
break;
}
tot += weight * it->second;
}
}
}
else
return 0;
return tot;
}
void HitFinderTools::saveTOTsync(std::vector<JPetHit>& hits, std::string TOTCalculationType, const boost::property_tree::ptree& syncTree)
{
for (auto& hit : hits)
{
auto tot = HitFinderTools::calculateTOT(hit, HitFinderTools::getTOTCalculationType(TOTCalculationType));
tot = HitFinderTools::syncTOT(hit, tot, syncTree);
hit.setEnergy(tot);
}
}
double HitFinderTools::syncTOT(const JPetHit& hit, double TOT, const boost::property_tree::ptree& syncTree)
{
double syncTOT = 0;
double factorA = syncTree.get("scin." + to_string(hit.getScintillator().getID()) + ".tot_scaling_factor_a",
1.0); // default 1.f
double factorB = syncTree.get("scin." + to_string(hit.getScintillator().getID()) + ".tot_scaling_factor_b",
0.0); // default 0.f
syncTOT = TOT * factorA + factorB;
return syncTOT;
}