Belle II Software development
SpacePointHelperFunctions.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <utility>
12#include <vector>
13
14#include <svd/calibration/SVDHitTimeSelection.h>
15#include <svd/calibration/SVDNoiseCalibrations.h>
16#include <svd/dataobjects/SVDCluster.h>
17#include <svd/dataobjects/SVDShaperDigit.h>
18#include <svd/dbobjects/SVDSpacePointSNRFractionSelector.h>
19#include <svd/reconstruction/SVDMaxSumAlgorithm.h>
20
21#include <framework/datastore/RelationArray.h>
22#include <framework/datastore/StoreArray.h>
23#include <framework/datastore/StoreObjPtr.h>
24#include <framework/database/DBObjPtr.h>
25#include <framework/utilities/MathHelpers.h>
26#include <mdst/dataobjects/EventLevelTrackingInfo.h>
27
28#include <vxd/dataobjects/VxdID.h>
29
30#include <unordered_map>
31
32#include <TH2.h>
33#include <math.h>
34#include <TFile.h>
35
36namespace Belle2 {
41
42
48
49 public:
50
57 inline void addCluster(const SVDCluster* entry, int index)
58 {
59 vxdID = entry->getSensorID();
60 if (entry->isUCluster() == true) {
61 clustersU.push_back(entry);
62 indicesU.push_back(index);
63 return;
64 }
65 clustersV.push_back(entry);
66 indicesV.push_back(index);
67 }
68
71
76 std::vector<const SVDCluster*> clustersU;
77
82 std::vector<const SVDCluster*> clustersV;
83
85 std::vector<int> indicesU;
86
88 std::vector<int> indicesV;
89
90 };
91
97 template <class SpacePointType> void provideSVDClusterSingles(const StoreArray<SVDCluster>& svdClusters,
98 StoreArray<SpacePointType>& spacePoints)
99 {
100 // RelationArray is much cheaper than addRelationTo
101 RelationArray spacePointsToClusters(spacePoints, svdClusters);
102 for (unsigned int i = 0; i < uint(svdClusters.getEntries()); ++i) {
103 const SVDCluster* currentCluster = svdClusters[i];
104 std::vector<const SVDCluster*> currentClusterCombi = { currentCluster };
105 const int spacePointIndex = spacePoints.getEntries();
106 spacePoints.appendNew(currentClusterCombi);
107 spacePointsToClusters.add(spacePointIndex, i);
108 }
109 }
110
111
112
115 std::vector<float>& inputVector,
116 const SVDNoiseCalibrations& noiseCal)
117 {
118 inputVector.clear();
119 inputVector.resize(3, 0.0);
120
121 auto shaperDigits = cls->getRelationsTo<SVDShaperDigit>();
122 float noise = 0;
123 for (auto iSD : shaperDigits) {
124 auto samples = iSD.getSamples();
125 std::vector<float> selectedSamples;
126 if (samples.size() == 6) {
127 Belle2::SVD::SVDMaxSumAlgorithm maxSum(samples);
128 auto maxSamples = maxSum.getSelectedSamples();
129 selectedSamples.assign(maxSamples.begin(), maxSamples.end());
130 } else {
131 selectedSamples.assign(samples.begin(), samples.end());
132 }
133 if (selectedSamples.size() < 3) continue;
134
135 inputVector[0] += selectedSamples[0];
136 inputVector[1] += selectedSamples[1];
137 inputVector[2] += selectedSamples[2];
138
139 VxdID thisSensorID = iSD.getSensorID();
140 bool thisSide = iSD.isUStrip();
141 int thisCellID = iSD.getCellID();
142 float thisNoise = noiseCal.getNoise(thisSensorID, thisSide, thisCellID);
143 noise += thisNoise * thisNoise;
144 }
145 noise = sqrt(noise);
146 inputVector[0] = inputVector[0] / noise;
147 inputVector[1] = inputVector[1] / noise;
148 inputVector[2] = inputVector[2] / noise;
149 }
150
160 std::vector< std::vector<const SVDCluster*> >& foundCombinations,
161 std::vector< std::pair<int, int> >& foundCombinationIndices, const SVDHitTimeSelection& hitTimeCut,
162 const bool& useSVDGroupInfo, const int& numberOfSignalGroups, const bool& formSingleSignalGroup,
163 const SVDNoiseCalibrations& noiseCal, const DBObjPtr<SVDSpacePointSNRFractionSelector>& svdSpacePointSelectionFunction,
164 bool useSVDSpacePointSNRFractionSelector)
165 {
166
167 for (size_t iU = 0; iU < aSensor.clustersU.size(); ++iU) {
168 const SVDCluster* uCluster = aSensor.clustersU[iU];
169 if (! hitTimeCut.isClusterInTime(uCluster->getSensorID(), 1, uCluster->getClsTime())) {
170 B2DEBUG(29, "Cluster rejected due to timing cut. Cluster time: " << uCluster->getClsTime());
171 continue;
172 }
173 for (size_t iV = 0; iV < aSensor.clustersV.size(); ++iV) {
174 const SVDCluster* vCluster = aSensor.clustersV[iV];
175 if (! hitTimeCut.isClusterInTime(vCluster->getSensorID(), 0, vCluster->getClsTime())) {
176 B2DEBUG(29, "Cluster rejected due to timing cut. Cluster time: " << vCluster->getClsTime());
177 continue;
178 }
179
180 if (! hitTimeCut.areClusterTimesCompatible(vCluster->getSensorID(), uCluster->getClsTime(), vCluster->getClsTime())) {
181 B2DEBUG(29, "Cluster combination rejected due to timing cut. Cluster time U (" << uCluster->getClsTime() <<
182 ") is incompatible with Cluster time V (" << vCluster->getClsTime() << ")");
183 continue;
184 }
185
186 if (useSVDGroupInfo) {
187 const std::vector<int>& uTimeGroupId = uCluster->getTimeGroupId();
188 const std::vector<int>& vTimeGroupId = vCluster->getTimeGroupId();
189
190 if (int(uTimeGroupId.size()) && int(vTimeGroupId.size())) { // indirect check if the clusterizer module is disabled
191 bool isContinue = true;
192 for (const auto& uitem : uTimeGroupId) {
193 if (uitem < 0 || uitem >= numberOfSignalGroups) continue;
194 for (const auto& vitem : vTimeGroupId) {
195 if (vitem < 0 || vitem >= numberOfSignalGroups) continue;
196 if ((uitem == vitem) || formSingleSignalGroup) { isContinue = false; break; }
197 }
198 if (!isContinue) break;
199 }
200
201 if (isContinue) {
202 B2DEBUG(29, "Cluster combination rejected due to different time-group Id.");
203 continue;
204 }
205 }
206 }
207
208 if (useSVDSpacePointSNRFractionSelector) {
209 std::vector<float> inputU;
210 std::vector<float> inputV;
211
212 storeInputVectorFromSingleCluster(uCluster, inputU, noiseCal);
213 storeInputVectorFromSingleCluster(vCluster, inputV, noiseCal);
214
215 bool pass = svdSpacePointSelectionFunction->passSNRFractionSelection(inputU, inputV);
216 if (!pass) {
217 B2DEBUG(29, "Cluster combination rejected due to SVDSpacePointSNRFractionSelector");
218 continue;
219 }
220 }
221
222 foundCombinations.push_back({uCluster, vCluster});
223 foundCombinationIndices.emplace_back(aSensor.indicesU[iU], aSensor.indicesV[iV]);
224
225
226 }
227 }
228
229
230
231
232 }
233
238
239 inline void spPDFName(const VxdID& sensor, int uSize, int vSize, int maxClusterSize, std::string& PDFName,
240 std::string& errorPDFName, bool useLegacyNaming)
241 {
242 if (useLegacyNaming == true) {
243
244 if (uSize > maxClusterSize) uSize = maxClusterSize;
245 if (vSize > maxClusterSize) vSize = maxClusterSize;
246
247 std::string sensorName;
248
249 if (sensor.getLayerNumber() == 3) sensorName = "l3";
250 if (sensor.getLayerNumber() > 3 && sensor.getSensorNumber() == 1) sensorName = "trap";
251 if (sensor.getLayerNumber() > 3 && sensor.getSensorNumber() > 1) sensorName = "large";
252
253 PDFName = sensorName + std::to_string(uSize) + std::to_string(vSize);
254 errorPDFName = "error" + PDFName;
255 } else {
256
257 if (uSize > maxClusterSize) uSize = maxClusterSize;
258 if (vSize > maxClusterSize) vSize = maxClusterSize;
259
260 int layer = sensor.getLayerNumber();
261 int ladder = sensor.getLadderNumber();
262 int sens = sensor.getSensorNumber();
263
264 PDFName = std::to_string(layer) + "." + std::to_string(ladder) + "." + std::to_string(sens) + "." + std::to_string(
265 uSize) + "." + std::to_string(vSize);
266 errorPDFName = PDFName + "_Error";
267 }
268
269
270
271 }
272
273
279
280
281 inline void calculatePairingProb(TFile* pdfFile, const std::vector<const SVDCluster*>& clusters, double& prob, double& error,
282 bool useLegacyNaming)
283 {
284
285 int maxSize;
286 int pdfEntries = pdfFile->GetListOfKeys()->GetSize();
287 if (useLegacyNaming == true) {
288 maxSize = floor(sqrt((pdfEntries - 4) / 6)); //4(time+size)+3(sensors)*2(prob/error)*size^2(u/v combo.)
289 } else {
290 maxSize = floor(sqrt((pdfEntries - 4) / 344)); //4(time+size)+172(sensorType)*2(prob/error)*size^2(u/v combo.)
291 }
292 std::string chargeProbInput;
293 std::string chargeErrorInput;
294
295 spPDFName(clusters[0]->getSensorID(), clusters[0]->getSize(), clusters[1]->getSize(), maxSize,
296 chargeProbInput, chargeErrorInput, useLegacyNaming);
297 std::string timeProbInput = "timeProb";
298 std::string timeErrorInput = "timeError";
299 std::string sizeProbInput = "sizeProb";
300 std::string sizeErrorInput = "sizeError";
301
302
303 TH2F* chargePDF = nullptr;
304 TH2F* chargeError = nullptr;
305 TH2F* timePDF = nullptr;
306 TH2F* timeError = nullptr;
307 TH2F* sizePDF = nullptr;
308 TH2F* sizeError = nullptr;
309
310 pdfFile->GetObject(chargeProbInput.c_str(), chargePDF);
311 pdfFile->GetObject(chargeErrorInput.c_str(), chargeError);
312 pdfFile->GetObject(timeProbInput.c_str(), timePDF);
313 pdfFile->GetObject(timeErrorInput.c_str(), timeError);
314 pdfFile->GetObject(sizeProbInput.c_str(), sizePDF);
315 pdfFile->GetObject(sizeErrorInput.c_str(), sizeError);
316
317 int xChargeBin = chargePDF->GetXaxis()->FindFixBin(clusters[0]->getCharge());
318 int yChargeBin = chargePDF->GetYaxis()->FindFixBin(clusters[1]->getCharge());
319
320 int xTimeBin = timePDF->GetXaxis()->FindFixBin(clusters[0]->getClsTime());
321 int yTimeBin = timePDF->GetYaxis()->FindFixBin(clusters[1]->getClsTime());
322
323
324 int xSizeBin = sizePDF->GetXaxis()->FindFixBin(clusters[0]->getSize());
325 int ySizeBin = sizePDF->GetYaxis()->FindFixBin(clusters[1]->getSize());
326
327 double chargeProb = chargePDF->GetBinContent(xChargeBin, yChargeBin);
328 double timeProb = timePDF->GetBinContent(xTimeBin, yTimeBin);
329 double sizeProb = sizePDF->GetBinContent(xSizeBin, ySizeBin);
330 double chargeProbError = chargePDF->GetBinContent(xChargeBin, yChargeBin);
331 double timeProbError = timePDF->GetBinContent(xTimeBin, yTimeBin);
332 double sizeProbError = sizePDF->GetBinContent(xSizeBin, ySizeBin);
333
334
335 if (chargeProbError == 0) {
336 B2DEBUG(21, "svdClusterProbabilityEstimator has not been run, spacePoint QI will return zero!");
337 }
338
339 prob = chargeProb * timeProb * sizeProb * clusters[0]->getQuality() * clusters[1]->getQuality();
340 error = prob * sqrt(square(timeProb * sizeProb * clusters[0]->getQuality() * clusters[1]->getQuality() * chargeProbError) +
341 square(chargeProb * sizeProb * clusters[0]->getQuality() * clusters[1]->getQuality() * timeProbError) +
342 square(chargeProb * timeProb * clusters[0]->getQuality() * clusters[1]->getQuality() * sizeProbError) +
343 square(chargeProb * timeProb * sizeProb * clusters[1]->getQuality() * clusters[0]->getQualityError()) +
344 square(chargeProb * timeProb * sizeProb * clusters[0]->getQuality() * clusters[1]->getQualityError()));
345 }
346
354 template <class SpacePointType> void provideSVDClusterCombinations(const StoreArray<SVDCluster>& svdClusters,
355 StoreArray<SpacePointType>& spacePoints, SVDHitTimeSelection& hitTimeCut, bool useQualityEstimator, TFile* pdfFile,
356 bool useLegacyNaming, unsigned int numMaxSpacePoints, std::string m_eventLevelTrackingInfoName, const bool& useSVDGroupInfo,
357 const int& numberOfSignalGroups, const bool& formSingleSignalGroup,
358 const SVDNoiseCalibrations& noiseCal, const DBObjPtr<SVDSpacePointSNRFractionSelector>& svdSpacePointSelectionFunction,
359 bool useSVDSpacePointSNRFractionSelector)
360 {
361 std::unordered_map<VxdID::baseType, ClustersOnSensor>
362 activatedSensors; // collects one entry per sensor, each entry will contain all Clusters on it TODO: better to use a sorted vector/list?
363 std::vector<std::vector<const SVDCluster*> >
364 foundCombinations; // collects all combinations of Clusters which were possible (condition: 1u+1v-Cluster on the same sensor)
365 std::vector<std::pair<int, int> >
366 foundCombinationIndices; // StoreArray indices of the (u, v) clusters of each entry of foundCombinations
367
368 // sort Clusters by sensor. After the loop, each entry of activatedSensors contains all U and V-type clusters on that sensor
369 for (unsigned int i = 0; i < uint(svdClusters.getEntries()); ++i) {
370 SVDCluster* currentCluster = svdClusters[i];
371
372 activatedSensors[currentCluster->getSensorID().getID()].addCluster(currentCluster, i);
373 }
374
375
376 for (const auto& aSensor : activatedSensors)
377 findPossibleCombinations(aSensor.second, foundCombinations, foundCombinationIndices, hitTimeCut, useSVDGroupInfo,
378 numberOfSignalGroups, formSingleSignalGroup, noiseCal, svdSpacePointSelectionFunction,
379 useSVDSpacePointSNRFractionSelector);
380
381 // Do not make space-points if their number would be too large to be considered by tracking
382 if (foundCombinations.size() > numMaxSpacePoints) {
383 StoreObjPtr<EventLevelTrackingInfo> m_eventLevelTrackingInfo(m_eventLevelTrackingInfoName);
384 if (m_eventLevelTrackingInfo.isValid()) {
385 m_eventLevelTrackingInfo->setSVDSpacePointCreatorAbortionFlag();
386 }
387 return;
388 }
389
390 // RelationArray is much cheaper than addRelationTo
391 // The relation weight codes the type of the cluster: +1 for u and -1 for v.
392 RelationArray spacePointsToClusters(spacePoints, svdClusters);
393
394 for (size_t iCombi = 0; iCombi < foundCombinations.size(); ++iCombi) {
395 auto& clusterCombi = foundCombinations[iCombi];
396 const int spacePointIndex = spacePoints.getEntries();
397 SpacePointType* newSP = spacePoints.appendNew(clusterCombi);
398 if (useQualityEstimator == true) {
399 double probability;
400 double error;
401 calculatePairingProb(pdfFile, clusterCombi, probability, error, useLegacyNaming);
402 newSP->setQualityEstimation(probability);
403 newSP->setQualityEstimationError(error);
404 }
405 // the first cluster of the combination is always the u one, the second one the v one
406 spacePointsToClusters.add(spacePointIndex, foundCombinationIndices[iCombi].first, 1.);
407 spacePointsToClusters.add(spacePointIndex, foundCombinationIndices[iCombi].second, -1.);
408 }
409 }
410
411
413} //Belle2 namespace
Class for accessing objects in the database.
Definition DBObjPtr.h:21
Low-level class to create/modify relations between StoreArrays.
void add(index_type from, index_type to, weight_type weight=1.0)
Add a new element to the relation.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition SVDCluster.h:29
float getClsTime() const
Get average of waveform maximum times of cluster strip signals.
Definition SVDCluster.h:134
VxdID getSensorID() const
Get the sensor ID.
Definition SVDCluster.h:102
bool isUCluster() const
Get the direction of strips.
Definition SVDCluster.h:110
const std::vector< int > & getTimeGroupId() const
Get ID of the time-group.
Definition SVDCluster.h:184
This class defines the dbobject and the methods to access the calibration of the cluster reconstructi...
bool isClusterInTime(const Belle2::VxdID &sensorID, const bool &isU, const double &svdTime, const double &svdTimeError=0, const double &t0=0, const double &t0Error=0) const
Return whether the cluster is estimated to be in time with the event or off-time.
bool areClusterTimesCompatible(const Belle2::VxdID &sensorID, const double &uTime, const double &vTime=0) const
Return whether the cluster is estimated to be in time with the event or off-time.
This class defines the dbobject and the method to access SVD calibrations from the noise local runs.
float getNoise(const VxdID &sensorID, const bool &isU, const unsigned short &strip) const
This is the method for getting the noise.
The SVD ShaperDigit class.
Class implementing the MaxSum algorithm.
std::vector< float > getSelectedSamples()
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
T * appendNew()
Construct a new T object at the end of the array.
Definition StoreArray.h:247
int getEntries() const
Get the number of objects in the array.
Definition StoreArray.h:216
Type-safe access to single objects in the data store.
Definition StoreObjPtr.h:96
bool isValid() const
Check whether the object was created.
Class to uniquely identify a any structure of the PXD and SVD.
Definition VxdID.h:32
baseType getID() const
Get the unique id.
Definition VxdID.h:93
constexpr T square(const T &x)
Calculate the square of the input.
Definition MathHelpers.h:21
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
void findPossibleCombinations(const Belle2::ClustersOnSensor &aSensor, std::vector< std::vector< const SVDCluster * > > &foundCombinations, std::vector< std::pair< int, int > > &foundCombinationIndices, const SVDHitTimeSelection &hitTimeCut, const bool &useSVDGroupInfo, const int &numberOfSignalGroups, const bool &formSingleSignalGroup, const SVDNoiseCalibrations &noiseCal, const DBObjPtr< SVDSpacePointSNRFractionSelector > &svdSpacePointSelectionFunction, bool useSVDSpacePointSNRFractionSelector)
stores all possible 2-Cluster-combinations.
void provideSVDClusterSingles(const StoreArray< SVDCluster > &svdClusters, StoreArray< SpacePointType > &spacePoints)
simply store one spacePoint for each existing SVDCluster.
void storeInputVectorFromSingleCluster(const SVDCluster *cls, std::vector< float > &inputVector, const SVDNoiseCalibrations &noiseCal)
Store the input values for SVDSpacePoint selection from the given SVDCluster.
void provideSVDClusterCombinations(const StoreArray< SVDCluster > &svdClusters, StoreArray< SpacePointType > &spacePoints, SVDHitTimeSelection &hitTimeCut, bool useQualityEstimator, TFile *pdfFile, bool useLegacyNaming, unsigned int numMaxSpacePoints, std::string m_eventLevelTrackingInfoName, const bool &useSVDGroupInfo, const int &numberOfSignalGroups, const bool &formSingleSignalGroup, const SVDNoiseCalibrations &noiseCal, const DBObjPtr< SVDSpacePointSNRFractionSelector > &svdSpacePointSelectionFunction, bool useSVDSpacePointSNRFractionSelector)
finds all possible combinations of U and V Clusters for SVDClusters.
void spPDFName(const VxdID &sensor, int uSize, int vSize, int maxClusterSize, std::string &PDFName, std::string &errorPDFName, bool useLegacyNaming)
Function to set name of PDF for spacePoint quality estimation.
void calculatePairingProb(TFile *pdfFile, const std::vector< const SVDCluster * > &clusters, double &prob, double &error, bool useLegacyNaming)
Function to extract probability of correct (pair from signal hit) cluster pairing from preconfigured ...
Abstract base class for different kinds of events.
small struct for storing all clusters of the same sensor in one container.
std::vector< const SVDCluster * > clustersU
stores all SVDclusters of U type.
std::vector< const SVDCluster * > clustersV
stores all SVDclusters of V type.
VxdID vxdID
Id of sensor, TODO can be removed if struct is used in a map.
std::vector< int > indicesV
stores the StoreArray indices of the clusters in clustersV, in the same order
std::vector< int > indicesU
stores the StoreArray indices of the clusters in clustersU, in the same order
void addCluster(const SVDCluster *entry, int index)
member function to automatically add the cluster to its corresponding entry