9#include <gtest/gtest.h>
11#include <tracking/spacePointCreation/PurityCalculatorTools.h>
15#include <framework/logging/Logger.h>
16#include <vxd/geometry/SensorInfoBase.h>
17#include <framework/datastore/StoreArray.h>
18#include <framework/datastore/RelationVector.h>
19#include <mdst/dataobjects/MCParticle.h>
20#include <pxd/dataobjects/PXDTrueHit.h>
21#include <pxd/dataobjects/PXDCluster.h>
22#include <svd/dataobjects/SVDTrueHit.h>
23#include <svd/dataobjects/SVDCluster.h>
24#include <tracking/spacePointCreation/SpacePoint.h>
27#include <tracking/spacePointCreation/SpacePointTrackCand.h>
32namespace PurityCalcTests {
38 template<
typename TrueHitType>
39 TrueHitType createTrueHit(
VxdID sensorId,
float u = 0.1,
float v = 0.2)
41 float position[3] = { u, v, 0. };
42 float momentum[3] = { 0.1, 0.1, 0.1 };
44 return TrueHitType(sensorId, position, position, position, momentum, momentum, momentum, 0.2, 0.1);
64 r1.SetAngles(45, 20, 30);
65 TGeoTranslation t1(0., 0., -0.);
66 TGeoCombiTrans c1(t1, r1);
67 TGeoHMatrix transform = c1;
68 sensorInfoBase.setTransformation(transform);
70 return sensorInfoBase;
78 SpacePoint createSpacePoint(
VxdID sensorId,
bool pxd,
short nClusters = 0,
double u = 0.1,
double v = 0.2)
82 PXDCluster pxdCluster(sensorId, u, v, 0.1, 0.1, 0, 0, 1, 1, 1, 1, 1, 1);
86 vector<const SVDCluster*> clusters;
89 clusters.push_back(
new SVDCluster(sensorId,
true, u, 1.0, 0.1, 0.001, 1, 1, 1, 1.0));
90 clusters.push_back(
new SVDCluster(sensorId,
false, v, 1.0, 0.1, 0.001, 1, 1, 1, 1.0));
92 bool setU = (nClusters > 0);
93 clusters.push_back(
new SVDCluster(sensorId, setU, u, 1.0, 0.1, 0.001, 1, 1, 1, 1.0));
121 return info.getParticleID() ==
i;
160 VxdID pxdId(1, 1, 1);
190 VxdID svdId(3, 1, 1);
266 B2INFO(
"Contents of DataStore after SetUp: pxdTrueHits: " << m_pxdTrueHits.getEntries() <<
267 " svdTrueHits: " << m_svdTrueHits.getEntries() <<
" mcParticles: " << m_mcParticles.getEntries() <<
268 " spacePoints: " << m_spacePoints.getEntries());
271 EXPECT_EQ(m_pxdTrueHits.getEntries(), 3);
272 EXPECT_EQ(m_svdTrueHits.getEntries(), 3);
273 EXPECT_EQ(m_spacePoints.getEntries(), 14);
274 EXPECT_EQ(m_mcParticles.getEntries(), 2);
277 for (
int i = 0; i < m_spacePoints.getEntries(); ++i) {
278 EXPECT_EQ(m_spacePoints[i]->getNClustersAssigned(), m_assignedClusters[i]);
284 ASSERT_EQ(pxdTrueHits.
size(), 1);
285 EXPECT_EQ(pxdTrueHits[0]->getArrayIndex(), 0);
286 EXPECT_EQ(pxdTrueHits.
weight(0), 1);
288 pxdTrueHits = m_spacePoints[2]->getRelationsTo<
PXDTrueHit>(
"ALL");
289 ASSERT_EQ(pxdTrueHits.
size(), 2);
290 EXPECT_EQ(pxdTrueHits[0]->getArrayIndex(), 0);
291 EXPECT_EQ(pxdTrueHits[0]->getRelatedFrom<MCParticle>(
"ALL")->getArrayIndex(), 0);
292 EXPECT_EQ(pxdTrueHits.
weight(0), 1);
293 EXPECT_EQ(pxdTrueHits[1]->getArrayIndex(), 1);
294 EXPECT_EQ(pxdTrueHits[1]->getRelatedFrom<MCParticle>(
"ALL")->getArrayIndex(), 1);
295 EXPECT_EQ(pxdTrueHits.
weight(1), 1);
298 ASSERT_EQ(svdTrueHits.
size(), 1);
299 EXPECT_EQ(svdTrueHits[0]->getArrayIndex(), 0);
300 EXPECT_EQ(svdTrueHits[0]->getRelatedFrom<MCParticle>(
"ALL")->getArrayIndex(), 0);
301 EXPECT_EQ(svdTrueHits.
weight(0), 2);
303 svdTrueHits = m_spacePoints[5]->getRelationsTo<
SVDTrueHit>(
"ALL");
304 ASSERT_EQ(svdTrueHits.
size(), 2);
305 EXPECT_EQ(svdTrueHits[0]->getArrayIndex(), 0);
306 EXPECT_EQ(svdTrueHits.
weight(0), 11);
307 EXPECT_EQ(svdTrueHits[1]->getArrayIndex(), 1);
308 EXPECT_EQ(svdTrueHits.
weight(1), 21);
309 EXPECT_EQ(svdTrueHits[1]->getRelatedFrom<MCParticle>(
"ALL")->getArrayIndex(), 1);
311 svdTrueHits = m_spacePoints[6]->getRelationsTo<
SVDTrueHit>(
"ALL");
312 ASSERT_EQ(svdTrueHits.
size(), 0);
314 svdTrueHits = m_spacePoints[7]->getRelationsTo<
SVDTrueHit>(
"ALL");
315 ASSERT_EQ(svdTrueHits.
size(), 2);
316 EXPECT_EQ(svdTrueHits[0]->getArrayIndex(), 0);
317 EXPECT_EQ(svdTrueHits.
weight(0), 2);
318 EXPECT_EQ(svdTrueHits.
weight(1), 11);
319 EXPECT_EQ(svdTrueHits[1]->getArrayIndex(), 1);
325 TEST_F(PurityCalculatorToolsTest, testFindWeightInVector)
327 std::vector<std::pair<int, double> > vec;
328 vec.push_back(std::make_pair(1, 0));
329 vec.push_back(std::make_pair(1, 1.1));
330 vec.push_back(std::make_pair(1, 2));
331 vec.push_back(std::make_pair(1, 11));
332 vec.push_back(std::make_pair(1, 21));
333 vec.push_back(std::make_pair(1, 11));
334 vec.push_back(std::make_pair(1, 2.5));
345 TEST_F(PurityCalculatorToolsTest, testGetAccessorsFromWeight)
349 EXPECT_TRUE(accs.empty());
351 EXPECT_TRUE(accs.empty());
355 EXPECT_EQ(accs[0], 0);
356 EXPECT_EQ(accs.size(), 1);
358 EXPECT_EQ(accs.size(), 2);
359 EXPECT_EQ(accs[0], 1);
360 EXPECT_EQ(accs[1], 2);
362 EXPECT_EQ(accs[0], 1);
363 EXPECT_EQ(accs.size(), 1);
365 EXPECT_EQ(accs[0], 2);
366 EXPECT_EQ(accs.size(), 1);
372 TEST_F(PurityCalculatorToolsTest, testIncreaseClusterCounters)
375 std::array<unsigned, 3> ctrArray = { {0, 0, 0} };
376 for (
size_t i = 0; i < 3; ++i) { EXPECT_EQ(ctrArray[i], 0); }
379 EXPECT_EQ(ctrArray[0], 1);
381 EXPECT_EQ(ctrArray[0], 2);
384 EXPECT_EQ(ctrArray[1], 1);
385 EXPECT_EQ(ctrArray[2], 1);
388 EXPECT_EQ(ctrArray[2], 2);
389 EXPECT_EQ(ctrArray[1], 1);
391 EXPECT_EQ(ctrArray[1], 2);
392 EXPECT_EQ(ctrArray[2], 2);
403 TEST_F(PurityCalculatorToolsTest, testGetMCParticlesPXD)
405 std::vector<std::pair<int, double> > mcParts = getMCParticles<PXDTrueHit>(m_spacePoints[0]);
406 ASSERT_EQ(mcParts.size(), 1);
407 EXPECT_EQ(mcParts[0].first, 0);
408 EXPECT_DOUBLE_EQ(mcParts[0].second, 1);
410 mcParts = getMCParticles<PXDTrueHit>(m_spacePoints[1]);
411 ASSERT_EQ(mcParts.size(), 1);
412 EXPECT_EQ(mcParts[0].first, -2);
413 EXPECT_EQ(mcParts[0].second, 1);
415 mcParts = getMCParticles<PXDTrueHit>(m_spacePoints[2]);
416 ASSERT_EQ(mcParts.size(), 2);
417 auto findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(0));
418 ASSERT_FALSE(findIt == mcParts.end());
419 EXPECT_DOUBLE_EQ(findIt->second, 1);
420 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(1));
421 ASSERT_FALSE(findIt == mcParts.end());
422 EXPECT_DOUBLE_EQ(findIt->second, 1);
424 B2INFO(
"The occurring error message is expected! It is used to discover some corner cases in real usage!");
425 mcParts = getMCParticles<PXDTrueHit>(m_spacePoints[3]);
426 ASSERT_EQ(mcParts.size(), 1);
427 EXPECT_EQ(mcParts[0].first, 1);
428 EXPECT_DOUBLE_EQ(mcParts[0].second, 1);
439 TEST_F(PurityCalculatorToolsTest, testGetMCParticlesSVD)
441 std::vector<std::pair<int, double> > mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[4]);
442 ASSERT_EQ(mcParts.size(), 1);
443 EXPECT_EQ(mcParts[0].first, 0);
444 EXPECT_DOUBLE_EQ(mcParts[0].second, 2);
446 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[5]);
447 ASSERT_EQ(mcParts.size(), 2);
448 auto findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(0));
449 ASSERT_FALSE(findIt == mcParts.end());
450 EXPECT_DOUBLE_EQ(findIt->second, 11);
451 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(1));
452 ASSERT_FALSE(findIt == mcParts.end());
453 EXPECT_DOUBLE_EQ(findIt->second, 21);
455 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[6]);
456 ASSERT_EQ(mcParts.size(), 1);
457 EXPECT_EQ(mcParts[0].first, -2);
458 EXPECT_DOUBLE_EQ(mcParts[0].second, 2);
460 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[7]);
461 ASSERT_EQ(mcParts.size(), 2);
462 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(1));
463 ASSERT_FALSE(findIt == mcParts.end());
464 EXPECT_DOUBLE_EQ(findIt->second, 11);
465 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(0));
466 ASSERT_FALSE(findIt == mcParts.end());
467 EXPECT_DOUBLE_EQ(findIt->second, 2);
469 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[8]);
470 ASSERT_EQ(mcParts.size(), 2);
471 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(-2));
472 ASSERT_FALSE(findIt == mcParts.end());
473 EXPECT_DOUBLE_EQ(findIt->second, 11);
474 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(0));
475 ASSERT_FALSE(findIt == mcParts.end());
476 EXPECT_DOUBLE_EQ(findIt->second, 21);
478 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[9]);
479 ASSERT_EQ(mcParts.size(), 2);
480 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(-2));
481 ASSERT_FALSE(findIt == mcParts.end());
482 EXPECT_DOUBLE_EQ(findIt->second, 21);
483 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(1));
484 ASSERT_FALSE(findIt == mcParts.end());
485 EXPECT_DOUBLE_EQ(findIt->second, 11);
487 mcParts = getMCParticles<SVDTrueHit>(m_spacePoints[11]);
488 ASSERT_EQ(mcParts.size(), 3);
489 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(0));
490 ASSERT_FALSE(findIt == mcParts.end());
491 EXPECT_DOUBLE_EQ(findIt->second, 11);
492 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(1));
493 ASSERT_FALSE(findIt == mcParts.end());
494 EXPECT_DOUBLE_EQ(findIt->second, 11);
495 findIt = std::find_if(mcParts.begin(), mcParts.end(), compFirst(-2));
496 ASSERT_FALSE(findIt == mcParts.end());
497 EXPECT_DOUBLE_EQ(findIt->second, 21);
506 TEST_F(PurityCalculatorToolsTest, testCreatePurityInfos)
509 std::vector<const SpacePoint*> spacePoints;
510 for (
size_t i = 0; i < 12; ++i) { spacePoints.push_back(m_spacePoints[i]); }
512 EXPECT_EQ(sptc.getNHits(), 12);
513 unsigned totCls = 20;
514 float ndf = 4 * 2 + 8 * 2;
516 B2INFO(
"There will be WARNING and ERROR messages! Those are expected!");
519 EXPECT_EQ(purities.size(), 4);
522 for (
size_t i = 0; i < purities.size() - 1; ++i) {
523 EXPECT_TRUE(purities[i].getPurity().second >= purities[i + 1].getPurity().second);
526 auto findIt = find_if(purities.begin(), purities.end(), compMCId(1));
527 ASSERT_FALSE(findIt == purities.end());
528 EXPECT_EQ(findIt->getNClustersTotal(), totCls);
529 EXPECT_EQ(findIt->getNPXDClustersTotal(), 4);
530 EXPECT_EQ(findIt->getNClustersFound(), 6);
531 EXPECT_EQ(findIt->getNPXDClusters(), 2);
532 EXPECT_EQ(findIt->getNSVDUClusters(), 3);
533 EXPECT_EQ(findIt->getNSVDVClusters(), 1);
534 EXPECT_FLOAT_EQ(findIt->getPurity().second, 8. / ndf);
536 findIt = find_if(purities.begin(), purities.end(), compMCId(0));
537 ASSERT_FALSE(findIt == purities.end());
538 EXPECT_EQ(findIt->getNClustersTotal(), totCls);
539 EXPECT_EQ(findIt->getNSVDUClustersTotal(), 8);
540 EXPECT_EQ(findIt->getNClustersFound(), 10);
541 EXPECT_EQ(findIt->getNPXDClusters(), 2);
542 EXPECT_EQ(findIt->getNSVDUClusters(), 5);
543 EXPECT_EQ(findIt->getNSVDVClusters(), 3);
544 EXPECT_FLOAT_EQ(findIt->getPurity().second, 12. / ndf);
546 findIt = find_if(purities.begin(), purities.end(), compMCId(-1));
547 ASSERT_FALSE(findIt == purities.end());
548 EXPECT_EQ(findIt->getNClustersTotal(), totCls);
549 EXPECT_EQ(findIt->getNSVDVClustersTotal(), 8);
550 EXPECT_EQ(findIt->getNClustersFound(), 1);
551 EXPECT_EQ(findIt->getNPXDClusters(), 0);
552 EXPECT_EQ(findIt->getNSVDUClusters(), 0);
553 EXPECT_EQ(findIt->getNSVDVClusters(), 1);
554 EXPECT_FLOAT_EQ(findIt->getPurity().second, 1. / ndf);
556 findIt = find_if(purities.begin(), purities.end(), compMCId(-2));
557 ASSERT_FALSE(findIt == purities.end());
558 EXPECT_EQ(findIt->getNClustersTotal(), totCls);
559 EXPECT_EQ(findIt->getNClustersFound(), 6);
560 EXPECT_EQ(findIt->getNPXDClusters(), 1);
561 EXPECT_EQ(findIt->getNSVDUClusters(), 2);
562 EXPECT_EQ(findIt->getNSVDVClusters(), 3);
563 EXPECT_FLOAT_EQ(findIt->getPurity().second, 7. / ndf);
static DataStore & Instance()
Instance of singleton Store.
void setInitializeActive(bool active)
Setter for m_initializeActive.
void reset(EDurability durability)
Frees memory occupied by data store items and removes all objects from the map.
A Class to store the Monte Carlo particle information.
The MC VXD Purity info container class.
The PXD Cluster class This class stores all information about reconstructed PXD clusters The position...
Class PXDTrueHit - Records of tracks that either enter or leave the sensitive volume.
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
void addRelationTo(const RelationsInterface< BASE > *object, float weight=1.0, const std::string &namedRelation="") const
Add a relation from this object to another object (with caching).
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Class SVDTrueHit - Records of tracks that either enter or leave the sensitive volume.
Storage for (VXD) SpacePoint-based track candidates.
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
bool registerInDataStore(DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut)
Register the object/array in the DataStore.
Accessor to arrays stored in the data store.
T * appendNew()
Construct a new T object at the end of the array.
bool registerRelationTo(const StoreArray< TO > &toArray, DataStore::EDurability durability=DataStore::c_Event, DataStore::EStoreFlags storeFlags=DataStore::c_WriteOut, const std::string &namedRelation="") const
Register a relation to the given StoreArray.
Base class to provide Sensor Information for PXD and SVD.
SensorType
Enum specifing the type of sensor the SensorInfo represents.
Class to uniquely identify a any structure of the PXD and SVD.
static bool findWeightInVector(std::vector< std::pair< int, double > > &vec, double weight)
find the given weight in the given vector of pair<int,double> NOTE: the criteria for finding are rath...
static std::vector< size_t > getAccessorsFromWeight(double weight)
convert the relation weight (SpacePoint <-> TrueHit) to a type that can be used to access arrays
static void increaseClusterCounters(const Belle2::SpacePoint *spacePoint, std::array< unsigned, 3 > &clusterCtr)
increase the appropriate Cluster counter by asking the SpacePoint which type he has and which Cluster...
static std::vector< Belle2::MCVXDPurityInfo > createPurityInfos(const SPContainer *container)
create a vector of MCVXDPurityInfos objects for any given container holding SpacePoints and providing...
Abstract base class for different kinds of events.
small helper functor to find a pair in a container of pairs, where .first matches the passed integer ...
bool operator()(const pair< int, short > &p)
comparison operator returning true if .first is j (from ctor)
compFirst(int j)
explicit constructor for use as functor with argument
int i
only member holding the value that has to be matched by the .first element of a pair
small helper functor to get the MCVXDPurityInfo with the passed ParticleId from a container of MCVXDP...
compMCId(int j)
explicit constructor for use as functor with arguments
int i
member holding the value that has to be mateched by the .first element of a pair
bool operator()(const MCVXDPurityInfo &info)
operator comparing the particle Id and the passed argument