forked from JustynaMedrala/NewAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHitFinder.cpp
More file actions
275 lines (249 loc) · 10.7 KB
/
Copy pathHitFinder.cpp
File metadata and controls
275 lines (249 loc) · 10.7 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
/**
* @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 HitFinder.cpp
*/
using namespace std;
#include <JPetAnalysisTools/JPetAnalysisTools.h>
#include <JPetGeomMapping/JPetGeomMapping.h>
#include <JPetOptionsTools/JPetOptionsTools.h>
#include <JPetWriter/JPetWriter.h>
#include "HitFinder.h"
#include "HitFinderTools.h"
#include "ToTEnergyConverterFactory.h"
#include "UniversalFileLoader.h"
#include <map>
#include <string>
#include <vector>
using namespace tot_energy_converter;
using namespace jpet_options_tools;
using namespace boost::property_tree;
HitFinder::HitFinder(const char* name) : JPetUserTask(name) {}
HitFinder::~HitFinder() {}
bool HitFinder::init()
{
INFO("Hit finding Started");
fOutputEvents = new JPetTimeWindow("JPetHit");
// Reading values from the user options if available
// Getting bool for using bad signals
if (isOptionSet(fParams.getOptions(), kUseCorruptedSignalsParamKey))
{
fUseCorruptedSignals = getOptionAsBool(fParams.getOptions(), kUseCorruptedSignalsParamKey);
if (fUseCorruptedSignals)
{
WARNING("Hit Finder is using Corrupted Signals, as set by the user");
}
else
{
WARNING("Hit Finder is NOT using Corrupted Signals, as set by the user");
}
}
else
{
WARNING("Hit Finder is not using Corrupted Signals (default option)");
}
// Allowed time difference between signals on A and B sides
if (isOptionSet(fParams.getOptions(), kABTimeDiffParamKey))
{
fABTimeDiff = getOptionAsFloat(fParams.getOptions(), kABTimeDiffParamKey);
}
// Getting velocities file from user options
auto velocitiesFile = std::string("dummyCalibration.txt");
if (isOptionSet(fParams.getOptions(), kVelocityFileParamKey))
{
velocitiesFile = getOptionAsString(fParams.getOptions(), kVelocityFileParamKey);
}
else
{
WARNING("No path to the file with velocities was provided in user options.");
}
// Getting number of Reference Detector Scintillator ID
if (isOptionSet(fParams.getOptions(), kRefDetScinIDParamKey))
{
fRefDetScinID = getOptionAsInt(fParams.getOptions(), kRefDetScinIDParamKey);
}
else
{
WARNING(
Form("No value of the %s parameter provided by the user, indicating that Reference Detector was not used.", kRefDetScinIDParamKey.c_str()));
}
// Getting bool for saving histograms
if (isOptionSet(fParams.getOptions(), kSaveControlHistosParamKey))
{
fSaveControlHistos = getOptionAsBool(fParams.getOptions(), kSaveControlHistosParamKey);
}
// Use of velocities file
JPetGeomMapping mapper(getParamBank());
auto tombMap = mapper.getTOMBMapping();
fVelocities = UniversalFileLoader::loadConfigurationParameters(velocitiesFile, tombMap);
if (fVelocities.empty())
{
ERROR("Velocities map seems to be empty");
}
// Loading parameters for conversion to ToT to energy
if (isOptionSet(fParams.getOptions(), kConvertToTParamKey))
{
fConvertToT = getOptionAsBool(fParams.getOptions(), kConvertToTParamKey);
if (fConvertToT)
{
INFO("Hit finder performs conversion of ToT to deposited energy with provided params.");
fToTConverterFactory.loadConverterOptions(fParams.getOptions());
}
else
{
INFO("Hit finder will not convert ToT to deposited energy since no user parameters are provided.");
}
}
// Loading parameters for TOT synchronizationi
if (isOptionSet(fParams.getOptions(), kUseToTSyncParamKey))
{
fSyncToT = getOptionAsBool(fParams.getOptions(), kUseToTSyncParamKey);
if (fSyncToT)
{
if (isOptionSet(fParams.getOptions(), kTOTConstantsFileParamKey))
{
INFO("Hit finder will perform ToT synchronization.");
std::string kSync = getOptionAsString(fParams.getOptions(), kTOTConstantsFileParamKey);
read_json(kSync, fConstantsTree);
}
else
{
ERROR("No file for TOT synchronization provided. No synchroniztion "
"applied.");
}
}
else
{
WARNING("Hit finder won't performe the ToT synchronization.");
}
}
if (isOptionSet(fParams.getOptions(), kTOTCalculationType))
{
fTOTCalculationType = getOptionAsString(fParams.getOptions(), kTOTCalculationType);
}
else
{
WARNING("No TOT calculation option given by the user. Using standard sum.");
}
// Control histograms
if (fSaveControlHistos)
{
initialiseHistograms();
}
return true;
}
bool HitFinder::exec()
{
if (auto& timeWindow = dynamic_cast<const JPetTimeWindow* const>(fEvent))
{
auto signalsBySlot = HitFinderTools::getSignalsBySlot(timeWindow, fUseCorruptedSignals);
auto totConverter = fToTConverterFactory.getEnergyConverter();
auto allHits = HitFinderTools::matchAllSignals(signalsBySlot, fVelocities, fABTimeDiff, fRefDetScinID, fConvertToT, totConverter, getStatistics(),
fSaveControlHistos);
if (fSaveControlHistos)
{
getStatistics().fillHistogram("hits_per_time_slot", allHits.size());
}
if (fSyncToT)
{
HitFinderTools::saveTOTsync(allHits, fTOTCalculationType, fConstantsTree);
}
saveHits(allHits);
}
else
return false;
return true;
}
bool HitFinder::terminate()
{
INFO("Hit finding ended");
return true;
}
void HitFinder::saveHits(const std::vector<JPetHit>& hits)
{
auto sortedHits = JPetAnalysisTools::getHitsOrderedByTime(hits);
for (const auto& hit : sortedHits)
{
if (fSaveControlHistos)
{
auto tot = HitFinderTools::calculateTOT(hit, HitFinderTools::getTOTCalculationType(fTOTCalculationType));
// synchronization
if (fSyncToT)
{
//ToDo change getEnergy() to getTOT() once implemented
getStatistics().fillHistogram("SyncTOT_all_hits", hit.getEnergy());
}
getStatistics().fillHistogram("TOT_all_hits", tot);
getStatistics().fillHistogram("tot_per_scin", tot, (float)(hit.getScintillator().getID()));
if (hit.getRecoFlag() == JPetHit::Good)
{
getStatistics().fillHistogram("TOT_good_hits", tot);
}
else if (hit.getRecoFlag() == JPetHit::Corrupted)
{
getStatistics().fillHistogram("TOT_corr_hits", tot);
}
}
fOutputEvents->add<JPetHit>(hit);
}
}
void HitFinder::initialiseHistograms()
{
getStatistics().createHistogramWithAxes(new TH1D("good_vs_bad_hits", "Number of good and corrupted Hits created", 3, 0.5, 3.5), "Quality",
"Number of Hits");
std::vector<std::pair<unsigned, std::string>> binLabels;
binLabels.push_back(std::make_pair(1, "GOOD"));
binLabels.push_back(std::make_pair(2, "CORRUPTED"));
binLabels.push_back(std::make_pair(3, "UNKNOWN"));
getStatistics().setHistogramBinLabel("good_vs_bad_hits", getStatistics().AxisLabel::kXaxis, binLabels);
getStatistics().createHistogramWithAxes(new TH1D("hits_per_time_slot", "Number of Hits in Time Window", 100, -0.5, 99.5), "Hits in Time Slot",
"Number of Time Slots");
getStatistics().createHistogramWithAxes(new TH2D("time_diff_per_scin", "Signals Time Difference per Scintillator ID", 4 * fABTimeDiff / 10,
-2 * fABTimeDiff, 2 * fABTimeDiff, 192, 0.5, 192.5),
"A-B time difference", "ID of Scintillator");
getStatistics().createHistogramWithAxes(new TH2D("tot_per_scin", "Hit TOT per Scintillator ID", 2 * 250, -255., 199500.0, 192, 0.5, 192.5),
"TOT hit", "ID of Scintillator");
getStatistics().createHistogramWithAxes(new TH2D("hit_pos_per_scin", "Hit Position per Scintillator ID", 200, -49.75, 50.25, 192, 0.5, 192.5),
"Hit z position [cm]", "ID of Scintillator");
// TOT calculating for all hits and reco flags
getStatistics().createHistogramWithAxes(new TH1D("TOT_all_hits", "TOT of all hits", 400, -250.0, 199500.0), "Time over Threshold [ps]",
"Number of Hits");
getStatistics().createHistogramWithAxes(new TH1D("SyncTOT_all_hits", "Sync. TOT of all hits", 400, -250.0, 199500.0),
"Time over Threshold [ps] Synchronized", "Number of Hits");
getStatistics().createHistogramWithAxes(new TH1D("TOT_good_hits", "TOT of hits with GOOD flag", 400, -250.0, 199500.0), "Time over Threshold [ps]",
"Number of Hits");
getStatistics().createHistogramWithAxes(new TH1D("TOT_corr_hits", "TOT of hits with CORRUPTED flag", 400, -250.0, 199500.0),
"Time over Threshold [ps]", "Number of Hits");
getStatistics().createHistogramWithAxes(new TH1D("remain_signals_per_scin", "Number of Unused Signals in Scintillator", 192, 0.5, 192.5),
"ID of Scintillator", "Number of Unused Signals in Scintillator");
getStatistics().createHistogramWithAxes(
new TH1D("remain_signals_tdiff", "Time Diff of an unused signal and the consecutive one", 200, fABTimeDiff - 125.0, 49875.0 + fABTimeDiff),
"Time difference [ps]", "Number of Signals");
if (fConvertToT)
{
auto converterRange = fToTConverterFactory.getEnergyConverter().getRange();
auto totConverter = fToTConverterFactory.getEnergyConverter();
auto minToT = converterRange.first;
auto maxToT = converterRange.second;
auto minEDep = totConverter(converterRange.first);
auto maxEDep = totConverter(converterRange.second);
getStatistics().createHistogramWithAxes(new TH1D("conv_tot_range", "TOT of hits in range of conversion function", 200, minToT, maxToT),
"Time over Threshold [ps]", "Number of Hits");
getStatistics().createHistogramWithAxes(
new TH1D("conv_dep_energy", "Deposited energy of hits, converted from ToT with provied formula", 200, minEDep, maxEDep),
"Deposited energy [keV]", "Number of Hits");
getStatistics().createHistogramWithAxes(new TH2D("conv_dep_energy_vs_tot",
"Deposited energy of hits, converted from ToT with provied formula vs. input ToT", 200, minEDep,
maxEDep, 200, minToT, maxToT),
"Deposited energy [keV]", "ToT of Hit [ps]");
}
}