Belle II Software  release-05-01-25
observers.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Eugenio Paoloni (eugenio.paoloni@pi.infn.it) *
7  * Jakob Lettenbichler (jakob.lettenbichler@oeaw.ac.at) *
8  * *
9  * This software is provided "as is" without any warranty. *
10  **************************************************************************/
11 
12 #include <gtest/gtest.h>
13 
14 #include <tracking/spacePointCreation/SpacePoint.h>
15 #include <vxd/geometry/SensorInfoBase.h>
16 #include <tracking/trackFindingVXD/filterMap/twoHitVariables/Distance3DSquared.h>
17 
18 #include <tracking/trackFindingVXD/filterMap/filterFramework/Shortcuts.h>
19 
20 #include <svd/dataobjects/SVDCluster.h>
21 #include <pxd/dataobjects/PXDCluster.h>
22 #include <mdst/dataobjects/MCParticle.h>
23 
24 #include <framework/datastore/StoreArray.h>
25 
26 #include <utility>
27 #include <map>
28 #include <string>
29 #include <iostream>
30 #include <cmath>
31 
32 #include <functional>
33 
34 using namespace std;
35 
36 using namespace Belle2;
47 namespace VXDTFObserversTest {
48 
49 
51  VXD::SensorInfoBase provideSensorInfo(VxdID aVxdID, double globalX = 0., double globalY = 0., double globalZ = -0.)
52  {
53  // (SensorType type, VxdID id, double width, double length, double thickness, int uCells, int vCells, double width2=-1, double splitLength=-1, int vCells2=0)
54  VXD::SensorInfoBase sensorInfoBase(VXD::SensorInfoBase::PXD, aVxdID, 2.3, 4.2, 0.3, 2, 4, -1);
55 
56  TGeoRotation r1;
57  r1.SetAngles(45, 20, 30); // rotation defined by Euler angles
58  TGeoTranslation t1(globalX, globalY, globalZ);
59  TGeoCombiTrans c1(t1, r1);
60  TGeoHMatrix transform = c1;
61  sensorInfoBase.setTransformation(transform);
62  // also need the reco transform
63  sensorInfoBase.setTransformation(transform, true);
64 
65  return sensorInfoBase;
66  }
67 
68 
69 
71  PXDCluster providePXDCluster(double u, double v, VxdID aVxdID, double uError = 0.1, double vError = 0.1)
72  {
73  return PXDCluster(aVxdID, u, v, uError, vError, 0, 0, 1, 1, 1, 1 , 1, 1);
74  }
75 
76 
78  SVDCluster provideSVDCluster(VxdID aVxdID, bool isU, double position, double error = 0.1)
79  {
80  return SVDCluster(aVxdID, isU, position, error, 0.1, 0.1, 1, 1, 1, 1);
81  }
82 
83 
85  SpacePoint provideSpacePointDummy(unsigned short i, double X, double Y, double Z)
86  {
87  VxdID aVxdID = VxdID(i, i, i);
88  VXD::SensorInfoBase sensorInfoBase = provideSensorInfo(aVxdID, X, Y, Z);
89 
90  PXDCluster aCluster = PXDCluster(aVxdID, 0., 0., 0.1, 0.1, 0, 0, 1, 1, 1, 1, 1, 1);
91 
92  return SpacePoint(&aCluster, &sensorInfoBase);
93  }
94 
95 
96 
98  class ObserversTest : public ::testing::Test {
99  protected:
100 
102  virtual void SetUp()
103  {
104  spacePointData.registerInDataStore();
105  pxdClusterData.registerInDataStore();
106  svdClusterData.registerInDataStore();
107  mcParticleData.registerInDataStore();
108 
109  spacePointData.registerRelationTo(pxdClusterData);
110  pxdClusterData.registerRelationTo(mcParticleData);
111 
112  spacePointData.registerRelationTo(svdClusterData);
113  svdClusterData.registerRelationTo(mcParticleData);
114 
115  DataStore::Instance().setInitializeActive(false);
116 
117 
118  for (unsigned int i = 1; i < 3; ++i) { // prepare mcParticles, pxdClusters and spacePoints related to them
119  MCParticle* aParticle = mcParticleData.appendNew();
120 
121  aParticle->setMomentum(float(i), float(i), float(i));
122 
123  VxdID aVxdID = VxdID(i, i, i);
124 
125  VXD::SensorInfoBase aSensorInfo = provideSensorInfo(aVxdID, (unsigned short)i, (unsigned short)i + 1., (unsigned short)i + 2.);
126 
127  const PXDCluster* pxdCluster = pxdClusterData.appendNew(providePXDCluster(0., 0., aVxdID));
128  pxdCluster->addRelationTo(aParticle);
129 
130  SpacePoint* newSP = spacePointData.appendNew(pxdCluster, &aSensorInfo);
131  B2DEBUG(10, " setup: new spacePoint got arrayIndex: " << newSP->getArrayIndex() << " and VxdID " << newSP->getVxdID());
132  newSP->addRelationTo(pxdCluster);
133  }
134 
135  mcParticleData[0]->setPDG(11);
136  mcParticleData[1]->setPDG(13);
137  mcParticleData[1]->addStatus(MCParticle::c_PrimaryParticle);
138 
139 
140  for (unsigned int i = 3; i < 7; ++i) { // now SVDClusters and their related spacePoints
141 
142  VxdID aVxdID = VxdID(i, i, i);
143 
144  VXD::SensorInfoBase aSensorInfo = provideSensorInfo(aVxdID, (unsigned short)i, (unsigned short)i + 1., (unsigned short)i + 2.);
145 
146  unsigned int pID = (i - 3) / 2;
147 
148  const SVDCluster* clusterU = svdClusterData.appendNew(provideSVDCluster(aVxdID, true, 0.));
149  clusterU->addRelationTo(mcParticleData[pID]);
150  const SVDCluster* clusterV = svdClusterData.appendNew(provideSVDCluster(aVxdID, false, 0.));
151  clusterV->addRelationTo(mcParticleData[pID]);
152 
153  std::vector<const SVDCluster*> clusterVector = {clusterU, clusterV};
154 
155  SpacePoint* newSP = spacePointData.appendNew(clusterVector, &aSensorInfo);
156  newSP->addRelationTo(clusterU);
157  newSP->addRelationTo(clusterV);
158  }
159 
160  B2INFO("ObserversTest:SetUP: created " << mcParticleData.getEntries() << "/" << pxdClusterData.getEntries() << "/" <<
161  svdClusterData.getEntries() << "/" << spacePointData.getEntries() << " mcParticles/pxdClusters/svdClusters/SpacePoints");
162  }
163 
164 
165 
167  virtual void TearDown()
168  {
169  DataStore::Instance().reset();
170  }
171 
176  };
177 
178 
179 
180 
181 
183  template < class T>
184  class counter {
185  public:
186  static unsigned int used;
187  static unsigned int accepted;
188  static unsigned int rejected;
189  static unsigned int wasInf;
190  static unsigned int wasNan;
191  counter() {};
192  ~counter() {};
193  static void resetCounter()
194  {
195  counter<T>::used = 0;
198  counter<T>::wasInf = 0;
199  counter<T>::wasNan = 0;
200  }
201  };
202 
203 
204 
207  public:
208  CountContainer() { m_container.clear(); }
211  typedef std::vector<int> Particles;
212 
213 
219  typedef std::pair< bool, Particles > Key;
220 
221 
224  AcceptRejectPair() : accept(0), reject(0) {}
225 
229  void Increase(bool accepted)
230  {
231  if (accepted) {
232  accept += 1;
233  } else {
234  reject += 1;
235  }
236  }
237 
238  unsigned accept;
239  unsigned reject;
240  };
241 
242 
244  void clear() { m_container.clear(); }
245 
246 
248  unsigned int size() { return m_container.size(); }
249 
250 
252  template<class ContainerType> static void uniqueIdentifier(ContainerType& particles)
253  {
254  std::sort(particles.begin(), particles.end());
255  auto newEndIterator = std::unique(particles.begin(), particles.end());
256  particles.resize(std::distance(particles.begin(), newEndIterator));
257  }
258 
259 
261  bool IncreaseCounter(Key& aKey, bool accepted)
262  {
263  bool keyAlreadyExisted = true;
264  auto foundPos = m_container.find(aKey);
265  if (foundPos == m_container.end()) {
266  B2DEBUG(100, " the IDs " << key2str(aKey) << " collected haven't been found yet");
267  foundPos = m_container.insert({aKey, AcceptRejectPair() }).first;
268  keyAlreadyExisted = false;
269  } else { B2DEBUG(100, " the IDs " << key2str(aKey) << " collected were already there"); }
270 
271  foundPos->second.Increase(accepted);
272 
273  return keyAlreadyExisted;
274  }
275 
276 
277 
280  {
281 
282  auto foundPos = m_container.find(givenKey);
283 
284  if (foundPos == m_container.end()) {
285  return AcceptRejectPair();
286  }
287 
288  return foundPos->second;
289  }
290 
291 
292 
295  {
296  for (const auto& aKey : m_container) {
297  B2DEBUG(100, "comparing given particles: " << vec2str(givenKey) << " with entry: " << vec2str(
298  aKey.first.second) << " with result " << (aKey.first.second == givenKey));
299  if (aKey.first.second == givenKey) { return aKey.second; }
300  }
301 
302  return AcceptRejectPair();
303  }
304 
305 
306 
308  void PrintResults(const string& identifier = "unknown")
309  {
310  for (auto& entry : m_container) {
311  B2WARNING(" for " << identifier << "-combination: " <<
312  key2str(entry.first) <<
313  ", combi was accepted/rejected: " <<
314  entry.second.accept <<
315  "/" <<
316  entry.second.reject);
317  }
318  }
319 
320 
321 
323  static std::string key2str(const Key& aKey)
324  {
325  return key2str(&aKey);
326  }
327 
328 
330  static std::string key2str(const Key* aKey)
331  {
332  string output;
333 
334  if (aKey->first == true) { output += "GoodCombi: "; }
335  else { output += "BadCombi: "; }
336 
337  output += vec2str(aKey->second);
338 
339  return output;
340  }
341 
342 
344  template<class Type> static std::string vec2str(const vector<Type>& vec)
345  {
346  string output;
347 
348  for (const auto& entry : vec) {
349  output += " " + std::to_string(entry);
350  }
351 
352  return output;
353  }
354  protected:
355  std::map< Key, AcceptRejectPair> m_container;
356  };
357 
358 
359 
361  template < class T>
362  class counterMC {
363  public:
364  static CountContainer
366  static CountContainer
368  counterMC() {};
369  ~counterMC() {};
370  static void resetCounter()
371  {
374  }
375  };
376 
377 
378 
380  template<class T> unsigned int counter<T>::used(0);
381  template<class T> unsigned int counter<T>::accepted(0);
382  template<class T> unsigned int counter<T>::rejected(0);
383  template<class T> unsigned int counter<T>::wasInf(0);
384  template<class T> unsigned int counter<T>::wasNan(0);
385 
386  template<class T> CountContainer counterMC< T >::pdGacceptedRejected =
387  CountContainer();
388  template<class T> CountContainer counterMC< T >::mcIDacceptedRejected =
389  CountContainer();
397  public:
399  template<class Var, typename ... otherTypes>
400  static void notify(const Var&,
401  otherTypes ...)
402  {
403  counter<Var>::used ++ ;
404  }
405  };
406 
407 
408 
409 
410 
413  public:
415  template<class Var, class RangeType>
416  static void notify(const Var&,
417  typename Var::variableType fResult,
418  const RangeType& range,
419  const typename Var::argumentType&,
420  const typename Var::argumentType&)
421  {
422  if (range.contains(fResult)) {
424  } else {
426  }
427  }
428  };
429 
430 
431 
432 
433 
436  public:
438  template<class Var, typename ... otherTypes>
439  static void notify(const Var&,
440  typename Var::variableType fResult,
441  otherTypes ...)
442  {
443  if (std::isinf(fResult)) {
445  } else if (std::isnan(fResult)) {
447  }
448  }
449  };
450 
451 
452 
453 
454 
456  class InfoObserver : public VoidObserver {
457  public:
459  template<class Var, class RangeType>
460  static void notify(const Var& filterType,
461  typename Var::variableType fResult,
462  const RangeType& range,
463  const typename Var::argumentType& outerHit,
464  const typename Var::argumentType& innerHit)
465  {
466 
467  stringstream outputStream;
468  outputStream << filterType.name()
469  << " with outer-/innerhit: "
470  << outerHit.getPosition().PrintStringXYZ()
471  << "/"
472  << innerHit.getPosition().PrintStringXYZ()
473  << " having indices "
474  << outerHit.getArrayIndex()
475  << "/"
476  << innerHit.getArrayIndex()
477  << " and VxdIDs "
478  << outerHit.getVxdID()
479  << "/"
480  << innerHit.getVxdID()
481  << " results in "
482  << fResult
483  << " & accepted: "
484  << (range.contains(fResult) ? string("true") : string("false"))
485  << " in range "
486  << range.getInf()
487  << "/"
488  << range.getSup();
489 
490  if (range.contains(fResult)) {
491  B2INFO(outputStream.str());
492  } else {
493  B2WARNING(outputStream.str());
494  }
495  }
496  };
497 
498 
499 
500 
503  public:
504 
506  template<class Var, class RangeType>
507  static void notify(const Var&,
508  typename Var::variableType fResult,
509  const RangeType& range,
510  const typename Var::argumentType& outerHit,
511  const typename Var::argumentType& innerHit)
512  {
513  B2INFO("CountAcceptedRejectedMCParticleObserver called" << endl
514  << "range: ( " << range.getInf() << " , " << range.getSup() << " )" << endl
515  << "var = " << fResult << endl
516  << "outerHit: (" << outerHit.X() << " , "
517  << outerHit.Y() << " , "
518  << outerHit.Z() << " ) (x,y,z) " << endl
519  << "innerHit: (" << innerHit.X() << " , "
520  << innerHit.Y() << " , "
521  << innerHit.Z() << " ) (x,y,z) " << endl
522  );
523 
524  // we use the pdgCodes of the particles as identifier (not unique, since more than one primary particle can have the same PDG-code) and count their acceptance rate:
525  CountContainer::Key myPDGs = createKey(outerHit, innerHit, true);
526 
527  counterMC< Var >::pdGacceptedRejected.IncreaseCounter(myPDGs, range.contains(fResult));
528 
529 
530  // now we do the same but with mcParticleIDs:
531 
532  CountContainer::Key myPIDs = createKey(outerHit, innerHit, false);
533 
534  counterMC< Var >::mcIDacceptedRejected.IncreaseCounter(myPIDs, range.contains(fResult));
535  }
536 
537 
538 
546  template <class hitType>
547  static void collectPDGs(const hitType& aHit, vector< pair< bool, int> >& collectedPDGs)
548  {
549 
550  std::vector<const MCParticle*> collectedParticles;
551 
552  collectMCParticles(aHit, collectedParticles);
553 
554  for (const MCParticle* aParticle : collectedParticles) {
555  collectedPDGs.push_back({aParticle->hasStatus(MCParticle::c_PrimaryParticle), aParticle->getPDG()});
556  }
557  }
558 
559 
560 
568  template <class hitType>
569  static void collectParticleIDs(const hitType& aHit, vector< pair< bool, int> >& collectedIDS)
570  {
571 
572  std::vector<const MCParticle*> collectedParticles;
573 
574  collectMCParticles(aHit, collectedParticles);
575 
576  for (const MCParticle* aParticle : collectedParticles) {
577  collectedIDS.push_back({aParticle->hasStatus(MCParticle::c_PrimaryParticle), aParticle->getIndex()});
578  }
579  }
580 
582  template <class hitType>
583  static CountContainer::Key createKey(const hitType& hitA, const hitType& hitB, bool usePDG)
584  {
585  CountContainer::Key newKey;
586 
587  vector< pair< bool, int> > collectedIDS;
588 
589  if (usePDG) {
590  collectPDGs(hitA, collectedIDS);
591  collectPDGs(hitB, collectedIDS);
592  } else {
593  collectParticleIDs(hitA, collectedIDS);
594  collectParticleIDs(hitB, collectedIDS);
595  }
596 
597  CountContainer::uniqueIdentifier(collectedIDS);
598 
599  if (collectedIDS.size() == 1) {
600  newKey.first = collectedIDS[0].first;
601  newKey.second = { collectedIDS[0].second };
602  return newKey;
603  }
604 
605  newKey.first = false;
606 
607  for (auto& entry : collectedIDS) {
608  newKey.second.push_back(entry.second);
609  }
610 
611  return newKey;
612  }
613 
614 
622  template <class hitType>
623  static void collectMCParticles(const hitType& aHit, std::vector<const MCParticle*>& collectedParticles)
624  {
625  RelationVector<PXDCluster> relatedToPXDClusters = aHit.template getRelationsTo<PXDCluster>();
626  RelationVector<SVDCluster> relatedToSVDClusters = aHit.template getRelationsTo<SVDCluster>();
627 
628  for (const PXDCluster& aCluster : relatedToPXDClusters) {
629  for (const MCParticle& aParticle : aCluster.getRelationsTo<MCParticle>()) {
630  collectedParticles.push_back(&aParticle);
631  }
632  }
633 
634  for (const SVDCluster& aCluster : relatedToSVDClusters) {
635  for (const MCParticle& aParticle : aCluster.getRelationsTo<MCParticle>()) {
636  collectedParticles.push_back(&aParticle);
637  }
638  }
639  }
640 
641 
642 
644  template<class Type> static std::string vec2str(const vector<Type>& vec)
645  {
646  string output;
647 
648  for (const auto& entry : vec) {
649  output += " " + std::to_string(entry);
650  }
651 
652  return output;
653  }
654  };
655 
656 
659  public:
661  template<class Var, class RangeType>
662  static void notify(const Var& filterType,
663  typename Var::variableType fResult,
664  const RangeType& range,
665  const typename Var::argumentType& outerHit,
666  const typename Var::argumentType& innerHit)
667  {
668  CountUsedObserver::notify(filterType);
669  CountAcceptRejectObserver::notify(filterType, fResult, range, outerHit, innerHit);
670  CountBadCaseObserver::notify(filterType, fResult, range, outerHit, innerHit);
671  InfoObserver::notify(filterType, fResult, range, outerHit, innerHit);
672  }
673  };
674 
675 
676 
677 
678 
693  template<class FilterType> class VectorOfObservers : public VoidObserver {
694  public:
695 
697  typedef std::function< void (const FilterType&, typename FilterType::variableType, const typename FilterType::argumentType&, const typename FilterType::argumentType&)>
699 
701  using CStyleFunctionPointer = void(*)(const typename FilterType::argumentType&, const typename FilterType::argumentType&,
702  const FilterType&, typename FilterType::variableType) ;
703 
705  template<typename range, typename ... otherTypes>
706  static void notify(const FilterType& filterType,
707  typename FilterType::variableType filterResult,
708  const range&,
709  const typename FilterType::argumentType& outerHit,
710  const typename FilterType::argumentType& innerHit,
711  otherTypes ...)
712  {
713  B2INFO(" Filter " << filterType.name() << " with Mag of outer-/innerHit " << outerHit.getPosition().Mag() << "/" <<
714  innerHit.getPosition().Mag() << " got result of " << filterResult << " and Observer-Vector sm_collectedObservers got " <<
715  VectorOfObservers<FilterType>::sm_collectedObservers.size() << " observers collected");
716  B2INFO(" Filter " << filterType.name() << " with Mag of outer-/innerHit " << outerHit.getPosition().Mag() << "/" <<
717  innerHit.getPosition().Mag() << " got result of " << filterResult << " and Observer-Vector sm_collectedObserversCSTYLE got " <<
718  VectorOfObservers<FilterType>::sm_collectedObserversCSTYLE.size() << " observers collected");
719 
721  // for(auto& anObserver : CollectedObservers<FilterType>::collectedObservers) {
722  // anObserver(outerHit, innerHit, filterResult);
723  // }
725  }
726 
727 
728 
730  template <typename ObserverType>
731  static void addObserver(observerFunction newObserver)
732  {
733  VectorOfObservers<FilterType>::sm_collectedObservers.push_back(std::bind(&newObserver, std::placeholders::_1, std::placeholders::_2,
734  FilterType(), std::placeholders::_3));
735  }
736 
737 
738 
740  static std::vector< observerFunction > sm_collectedObservers;
742  static std::vector< CStyleFunctionPointer > sm_collectedObserversCSTYLE;
743 
745 // static std::vector< std::_Bind<void (*(std::_Placeholder<1>, std::_Placeholder<2>, Belle2::Distance3DSquared, std::_Placeholder<3>))(Belle2::SpacePoint const&, Belle2::SpacePoint const&, Belle2::Distance3DSquared const&, float)> > sm_collectedObserversTry2;
746  };
747 
748 
750  template<typename FilterType> std::vector< typename VectorOfObservers<FilterType>::observerFunction >
753  template<typename FilterType> std::vector< typename VectorOfObservers<FilterType>::CStyleFunctionPointer >
754  VectorOfObservers<FilterType>::sm_collectedObserversCSTYLE = {};
756 // template<typename FilterType> std::vector< typename VectorOfObservers<FilterType>::CStyleFunctionPointer > VectorOfObservers<FilterType>::sm_collectedObserversTry2 = {};
757 
758 
759 
760 
761 
763  TEST_F(ObserversTest, TestRelationSetup)
764  {
765  EXPECT_EQ(6, spacePointData.getEntries());
766 
767  for (SpacePoint& aSP : spacePointData) {
768  unsigned nullptrTrap = 0;
769  RelationVector<PXDCluster> pxDClusters = aSP.getRelationsTo<PXDCluster>();
770  for (PXDCluster& aCluster : pxDClusters) {
771  MCParticle* aParticle = aCluster.getRelatedTo<MCParticle>();
772  if (aParticle == NULL) { nullptrTrap = 1; }
773  EXPECT_EQ(0, nullptrTrap);
774 
775  EXPECT_EQ(aSP.getArrayIndex(), aCluster.getArrayIndex());
776  EXPECT_EQ(aSP.getArrayIndex(), aParticle->getArrayIndex());
777  EXPECT_EQ(int(aParticle->getMomentum().X()), aParticle->getArrayIndex() + 1);
778  EXPECT_EQ(int(aParticle->getMomentum().Y()), aParticle->getArrayIndex() + 1);
779  EXPECT_EQ(int(aParticle->getMomentum().Z()), aParticle->getArrayIndex() + 1);
780 
781  nullptrTrap = 0;
782  }
783 
784  RelationVector<SVDCluster> svDClusters = aSP.getRelationsTo<SVDCluster>();
785  for (SVDCluster& aCluster : svDClusters) {
786  MCParticle* aParticle = aCluster.getRelatedTo<MCParticle>();
787  if (aParticle == NULL) { nullptrTrap = 2; }
788  EXPECT_EQ(0, nullptrTrap);
789 
790  EXPECT_EQ(aSP.getArrayIndex(), 2 + aCluster.getArrayIndex() / 2);
791  EXPECT_EQ(aSP.getArrayIndex() / 4, aParticle->getArrayIndex());
792  EXPECT_EQ(int(aParticle->getMomentum().X()), aParticle->getArrayIndex() + 1);
793  EXPECT_EQ(int(aParticle->getMomentum().Y()), aParticle->getArrayIndex() + 1);
794  EXPECT_EQ(int(aParticle->getMomentum().Z()), aParticle->getArrayIndex() + 1);
795 
796  nullptrTrap = 0;
797  }
798 
799  // at least one cluster type has to be connected to given spacePoint!
800  EXPECT_NE(0, pxDClusters.size() + svDClusters.size());
801  }
802  }
803 
804 
805 
806 
807 
809  TEST_F(ObserversTest, TestMCDataAccess)
810  {
812  3.5));
814  unobservedFilter);
815  auto mcCounter = counterMC< Distance3DSquared<SpacePoint> >();
816 
817  for (int i = 1 ; i < spacePointData.getEntries(); i++) {
818  SpacePoint& spA = *spacePointData[i];
819  SpacePoint& spB = *spacePointData[i - 1];
820  B2DEBUG(10, "spData-Sps got arraIndices: " << spacePointData[i]->getArrayIndex() << "/" << spacePointData[i - 1]->getArrayIndex() <<
821  " and VxdIDs " << spacePointData[i]->getVxdID() << "/" << spacePointData[i - 1]->getVxdID());
822  observedFilter.accept(spA, spB);
823  }
824 
825  EXPECT_EQ(3, mcCounter.pdGacceptedRejected.size());
826  EXPECT_EQ(3, mcCounter.mcIDacceptedRejected.size());
827 
828  mcCounter.pdGacceptedRejected.PrintResults("pdgCode");
829 
831  // the filter was too loose, everything is accepted
832  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11}).accept);
833  EXPECT_EQ(3, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11, 13}).accept);
834  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{13}).accept);
835  // and nothing was rejected...
836  EXPECT_EQ(0, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11}).reject);
837  EXPECT_EQ(0, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11, 13}).reject);
838  EXPECT_EQ(0, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{13}).reject);
839 
840 
841  mcCounter.mcIDacceptedRejected.PrintResults("pID");
842 
843  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1}).accept);
844  EXPECT_EQ(3, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1, 2}).accept);
845  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{2}).accept);
846 
847  EXPECT_EQ(0, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1}).reject);
848  EXPECT_EQ(0, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1, 2}).reject);
849  EXPECT_EQ(0, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{2}).reject);
850 
851 
852  // now we set the filter using values which are too strict
854  (3.1, 3.5));
856  observedFilterStrict(
857  unobservedFilterStrict);
858 
859  for (int i = 1 ; i < spacePointData.getEntries(); i++) {
860  SpacePoint& spA = *spacePointData[i];
861  SpacePoint& spB = *spacePointData[i - 1];
862  observedFilterStrict.accept(spA, spB);
863  }
864 
865  EXPECT_EQ(3, mcCounter.pdGacceptedRejected.size());
866  EXPECT_EQ(3, mcCounter.mcIDacceptedRejected.size());
867 
868  // first filter does write into the same container, therefore values didn't change for accepted
869  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11}).accept);
870  EXPECT_EQ(3, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11, 13}).accept);
871  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{13}).accept);
872 
873  // second filter does reject everything:
874  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11}).reject);
875  EXPECT_EQ(3, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{11, 13}).reject);
876  EXPECT_EQ(1, mcCounter.pdGacceptedRejected.ReturnResult(CountContainer::Particles{13}).reject);
877 
878  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1}).accept);
879  EXPECT_EQ(3, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1, 2}).accept);
880  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{2}).accept);
881 
882  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1}).reject);
883  EXPECT_EQ(3, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{1, 2}).reject);
884  EXPECT_EQ(1, mcCounter.mcIDacceptedRejected.ReturnResult(CountContainer::Particles{2}).reject);
885 
886  }
887 
888 
889 
890 
891 
893  TEST_F(ObserversTest, TestObserverFlexibility)
894  {
895  // Very verbose declaration, see below for convenient shortcuts
897  1.));
898 
899  // Filter< Distance3DSquared<SpacePoint>, Range<double, double>, VectorOfObservers<Distance3DSquared> > filter(unobservedFilter);
900  Filter< Distance3DSquared<SpacePoint>, Range<double, double>, InfoObserver > observedFilter(unobservedFilter);
901  SpacePoint x1 = provideSpacePointDummy(1, 0.0f , 0.0f, 0.0f);
902  SpacePoint x2 = provideSpacePointDummy(1, 0.5f , 0.0f, 0.0f);
903  SpacePoint x3 = provideSpacePointDummy(1, 2.0f , 0.0f, 0.0f);
904  auto myCounter = counter<Distance3DSquared<SpacePoint>>();
905  myCounter.resetCounter();
906 
908 // auto storeFuncVariantA = std::bind( ((VectorOfObservers<Distance3DSquared>::observerFunction) &CountingObserver::notify), std::placeholders::_1, std::placeholders::_2, Distance3DSquared(), std::placeholders::_3);
909  // VectorOfObservers<Distance3DSquared>::sm_collectedObservers.push_back(storeFuncVariantA);
910 
912  auto storeFuncVariantB = std::bind(((VectorOfObservers<Distance3DSquared<SpacePoint>>::CStyleFunctionPointer)
913  &CountUsedObserver::notify), std::placeholders::_1, std::placeholders::_2, Distance3DSquared<SpacePoint>(),
914  std::placeholders::_3);
915 
916  char* realname(NULL);
917  int status(0);
918  realname = abi::__cxa_demangle(typeid(storeFuncVariantB).name(), 0, 0, &status);
919  std::string name(realname);
920  free(realname);
921  B2INFO("storeFuncVariantB is of type: " << name);
922 
923 // VectorOfObservers<Distance3DSquared>::sm_collectedObserversTry2.push_back(storeFuncVariantB);
924 
925 
927 // VectorOfObservers<Distance3DSquared>::sm_collectedObserversCSTYLE.push_back(storeFuncVariantB);
928 
930  // VectorOfObservers<Distance3DSquared>::addObserver(CountUsedObserver);
931  // VectorOfObservers<Distance3DSquared>::addObserver(WarningObserver);
932 
933  observedFilter.accept(x2, x1);
934  observedFilter.accept(x3, x1);
935  EXPECT_EQ(0 , myCounter.used);
936 
938  unobservedFilter);
939 
940  anotherObservedFilter.accept(x2, x1);
941  anotherObservedFilter.accept(x3, x1);
942  EXPECT_EQ(2 , myCounter.used);
943  EXPECT_EQ(1 , myCounter.accepted);
944  EXPECT_EQ(1 , myCounter.rejected);
945  EXPECT_EQ(0 , myCounter.wasInf);
946  EXPECT_EQ(0 , myCounter.wasNan);
947  }
948 
949 
950 
951 
952 
955  {
956  //garbage:
957  // VectorOfObservers<Distance3DSquared>::addObserver(CountUsedObserver::notify);
958  // auto storeFunc = std::bind((void(*)(const SpacePoint&, const SpacePoint&, const Belle2::Distance3DSquared&, float))&CountUsedObserver::notify, std::placeholders::_1, std::placeholders::_2, Distance3DSquared(), std::placeholders::_3);
959  // auto storeFunc1 = std::bind(void(*)(const typename Distance3DSquared::argumentType&, const typename Distance3DSquared::argumentType&, const Distance3DSquared&, typename Distance3DSquared::variableType) /*&CountUsedObserver::notify*/), std::placeholders::_1, std::placeholders::_2, Distance3DSquared(), std::placeholders::_3);
960 
961 // ObserverVector<Distance3DSquared>::sm_collectedObservers.push_back(storeFunc);
962  // ObserverVector<Distance3DSquared>::sm_collectedObservers.push_back(std::bind((void(*)(const SpacePoint&, const SpacePoint&, const Belle2::Distance3DSquared&, float))&CountUsedObserver::notify, std::placeholders::_1, std::placeholders::_2, Distance3DSquared(), std::placeholders::_3));
963  // std::vector< typename ObserverVector<FilterType>::observerFunction >();
964  // ObserverVector<VectorOfObservers>::sm_collectedObservers = std::vector< typename VectorOfObservers<Distance3DSquared>::observerFunction >();
965 // ErrorObserver anErrorBla();
966  // WarningObserver aWarningBla();
967  // ObserverVector<Distance3DSquared>::addObserver(aWarningBla/*, filterBla*/);
968  // ObserverVector<Distance3DSquared>::addObserver(aBla/*, filterBla*/);
969  // // // // // // Distance3DSquared filterBla();
970 
972 // template<class FilterType> class CollectedObservers {
973 // public:
974 // typedef std::function< void (const typename FilterType::argumentType&, const typename FilterType::argumentType&, const FilterType&, typename FilterType::variableType)> observerFunction;
975 //
976 // /** collects observers to be executed during notify */
977 // static std::vector< observerFunction > collectedObservers;
978 // CollectedObservers() {};
979 // ~CollectedObservers() {};
980 // };
981 
982 // static void addObserver( CountUsedObserver& newObserver) {
983 // // static void addObserver(observerFunction newObserver) {
984 // ObserverVector<FilterType>::sm_collectedObservers.push_back(std::bind(&newObserver::notify, std::placeholders::_1, std::placeholders::_2, FilterType(), std::placeholders::_3));
985 // }
986  }
987 
988 }
VXDTFObserversTest::InfoObserver::notify
static void notify(const Var &filterType, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &outerHit, const typename Var::argumentType &innerHit)
notifier producing a info message if SelectionVariable was accepted and a Warning if otherwise
Definition: observers.cc:460
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::notify
static void notify(const Var &, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &outerHit, const typename Var::argumentType &innerHit)
notifier producing a info message if SelectionVariable was accepted and a Warning if otherwise
Definition: observers.cc:507
VXDTFObserversTest::CountBadCaseObserver
this observer does simply count the number of times, the attached Filter resulted in isinf or isnan
Definition: observers.cc:435
VXDTFObserversTest::CountContainer::clear
void clear()
clear container
Definition: observers.cc:244
Belle2::VxdID
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:43
VXDTFObserversTest::counter::wasNan
static unsigned int wasNan
count number of times result was nan
Definition: observers.cc:190
VXDTFObserversTest::VectorOfObservers::sm_collectedObservers
static std::vector< observerFunction > sm_collectedObservers
collects observers with std::function to be executed during notify (variant A)
Definition: observers.cc:740
VXDTFObserversTest::CountContainer::ReturnResult
AcceptRejectPair ReturnResult(const Particles &givenKey)
for given key, the function returns the result found.
Definition: observers.cc:294
VXDTFObserversTest::InfoObserver
this observer does simply print the name of the SelectionVariable and the result of its value-functio...
Definition: observers.cc:456
VXDTFObserversTest::CountContainer::Particles
std::vector< int > Particles
a vector containing IDs
Definition: observers.cc:211
VXDTFObserversTest::VectorOfObservers::CStyleFunctionPointer
void(*)(const typename FilterType::argumentType &, const typename FilterType::argumentType &, const FilterType &, typename FilterType::variableType) CStyleFunctionPointer
a typedef to make the c-style pointer more readable (can not be done with classic typedef)
Definition: observers.cc:702
VXDTFObserversTest::CountContainer::size
unsigned int size()
size of container
Definition: observers.cc:248
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::collectMCParticles
static void collectMCParticles(const hitType &aHit, std::vector< const MCParticle * > &collectedParticles)
collects all mcParticles connected to given hit.
Definition: observers.cc:623
VXDTFObserversTest::ObserversTest::mcParticleData
StoreArray< MCParticle > mcParticleData
some mcParticles to test RelationsInterface for observers.
Definition: observers.cc:175
VXDTFObserversTest::ObserversTest::TearDown
virtual void TearDown()
clear datastore
Definition: observers.cc:167
VXDTFObserversTest::ObserversTest::pxdClusterData
StoreArray< PXDCluster > pxdClusterData
some pxd clusters to test RelationsInterface for observers.
Definition: observers.cc:173
Belle2::RelationsInterface::addRelationTo
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).
Definition: RelationsObject.h:144
Belle2::VXD::SensorInfoBase
Base class to provide Sensor Information for PXD and SVD.
Definition: SensorInfoBase.h:40
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::collectParticleIDs
static void collectParticleIDs(const hitType &aHit, vector< pair< bool, int > > &collectedIDS)
collects all particleIDs connected to given hit.
Definition: observers.cc:569
Belle2::RelationsInterface::getRelatedTo
TO * getRelatedTo(const std::string &name="", const std::string &namedRelation="") const
Get the object to which this object has a relation.
Definition: RelationsObject.h:250
VXDTFObserversTest::VectorOfObservers::notify
static void notify(const FilterType &filterType, typename FilterType::variableType filterResult, const range &, const typename FilterType::argumentType &outerHit, const typename FilterType::argumentType &innerHit, otherTypes ...)
iterate over all stored Observers and execute their notify-function
Definition: observers.cc:706
VXDTFObserversTest::CountContainer::vec2str
static std::string vec2str(const vector< Type > &vec)
small helper for easily printing vectors
Definition: observers.cc:344
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::vec2str
static std::string vec2str(const vector< Type > &vec)
small helper for easily printing vectors
Definition: observers.cc:644
Belle2::MCParticle::getArrayIndex
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:255
VXDTFObserversTest::counter::used
static unsigned int used
count number of times used
Definition: observers.cc:186
VXDTFObserversTest::counter
a tiny counter class for counting stuff
Definition: observers.cc:184
Belle2::RelationsInterface::getRelationsTo
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
Definition: RelationsObject.h:199
Belle2::SpacePoint
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:52
VXDTFObserversTest::ProvideBasicInfoObserver
this observer combines all the easy-to-get info retrievable by observers and prints it to screen
Definition: observers.cc:658
VXDTFObserversTest::ObserversTest::svdClusterData
StoreArray< SVDCluster > svdClusterData
some svd clusters to test RelationsInterface for observers.
Definition: observers.cc:174
VXDTFObserversTest::CountContainer::key2str
static std::string key2str(const Key &aKey)
small helper for easily printing vectors
Definition: observers.cc:323
VXDTFObserversTest::ProvideBasicInfoObserver::notify
static void notify(const Var &filterType, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &outerHit, const typename Var::argumentType &innerHit)
notify function is called by the filter, this one combines all the easy-to-get info retrievable by ob...
Definition: observers.cc:662
VXDTFObserversTest::CountContainer
a container for counting accepted and rejected stuff, just delivers some interfaces to make the code ...
Definition: observers.cc:206
VXDTFObserversTest::CountContainer::ReturnResult
AcceptRejectPair ReturnResult(const Key &givenKey)
for given key, the function returns the result found.
Definition: observers.cc:279
VXDTFObserversTest::provideSVDCluster
SVDCluster provideSVDCluster(VxdID aVxdID, bool isU, double position, double error=0.1)
returns a svdCluster with given sensorID, uType and local position
Definition: observers.cc:78
VXDTFObserversTest
TODO next steps:
Definition: observers.cc:47
VXDTFObserversTest::counterMC
a tiny counter class for counting stuff retrieved from MC-bla
Definition: observers.cc:362
VXDTFObserversTest::providePXDCluster
PXDCluster providePXDCluster(double u, double v, VxdID aVxdID, double uError=0.1, double vError=0.1)
returns a pxdCluster with given sensorID and local coordinates
Definition: observers.cc:71
Belle2::RelationVector
Class for type safe access to objects that are referred to in relations.
Definition: DataStore.h:38
VXDTFObserversTest::CountContainer::AcceptRejectPair::reject
unsigned reject
counts nTimes when it was rejected
Definition: observers.cc:239
VXDTFObserversTest::VectorOfObservers::observerFunction
std::function< void(const FilterType &, typename FilterType::variableType, const typename FilterType::argumentType &, const typename FilterType::argumentType &)> observerFunction
a typedef to make the stuff more readable
Definition: observers.cc:698
Belle2::VXD::SensorInfoBase::setTransformation
void setTransformation(const TGeoHMatrix &transform, bool reco=false)
Set the transformation matrix of the Sensor.
Definition: SensorInfoBase.h:305
VXDTFObserversTest::CountBadCaseObserver::notify
static void notify(const Var &, typename Var::variableType fResult, otherTypes ...)
notifier counting how often a SelectionVariable was resulting in nan or inf
Definition: observers.cc:439
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Range
Represents a range of arithmetic types.
Definition: Range.h:39
VXDTFObserversTest::provideSensorInfo
VXD::SensorInfoBase provideSensorInfo(VxdID aVxdID, double globalX=0., double globalY=0., double globalZ=-0.)
this is a small helper function to create a sensorInfo to be used
Definition: observers.cc:51
VXDTFObserversTest::CountContainer::IncreaseCounter
bool IncreaseCounter(Key &aKey, bool accepted)
accepts a key (first parameter) and if given key was accepted by the filter (second parameter)
Definition: observers.cc:261
VXDTFObserversTest::CountContainer::m_container
std::map< Key, AcceptRejectPair > m_container
collects the data
Definition: observers.cc:355
VXDTFObserversTest::counter::rejected
static unsigned int rejected
count number of times result was rejected
Definition: observers.cc:188
Belle2::TEST_F
TEST_F(GlobalLabelTest, LargeNumberOfTimeDependentParameters)
Test large number of time-dep params for registration and retrieval.
Definition: globalLabel.cc:65
VXDTFObserversTest::CountAcceptRejectObserver::notify
static void notify(const Var &, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &, const typename Var::argumentType &)
notifier counting how often a SelectionVariable was accepted/rejected
Definition: observers.cc:416
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::collectPDGs
static void collectPDGs(const hitType &aHit, vector< pair< bool, int > > &collectedPDGs)
collects all PDGs connected to given hit.
Definition: observers.cc:547
Belle2::PXDCluster
The PXD Cluster class This class stores all information about reconstructed PXD clusters The position...
Definition: PXDCluster.h:41
Belle2::RelationsInterface::getArrayIndex
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
Definition: RelationsObject.h:387
VXDTFObserversTest::counterMC::pdGacceptedRejected
static CountContainer pdGacceptedRejected
counts nCases accepted/rejected for each pdgCode-combination occured
Definition: observers.cc:365
VXDTFObserversTest::CountContainer::AcceptRejectPair
simple struct for counting accepted and rejected cases.
Definition: observers.cc:223
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver::createKey
static CountContainer::Key createKey(const hitType &hitA, const hitType &hitB, bool usePDG)
for two hits given, a key for the CountContainer is returned.
Definition: observers.cc:583
Belle2::SVDCluster
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition: SVDCluster.h:38
VXDTFObserversTest::VectorOfObservers::sm_collectedObserversCSTYLE
static std::vector< CStyleFunctionPointer > sm_collectedObserversCSTYLE
collects observers with c-style function pointers to be executed during notify (variant B)
Definition: observers.cc:742
VXDTFObserversTest::VectorOfObservers::addObserver
static void addObserver(observerFunction newObserver)
collects observers to be executed during notify (can not be used so far, but is long-term goal)
Definition: observers.cc:731
VXDTFObserversTest::CountAcceptRejectObserver
this observer does simply count the number of times, the attached SelectionVariable was accepted or r...
Definition: observers.cc:412
VXDTFObserversTest::counter::~counter
~counter()
constructor.
Definition: observers.cc:192
VXDTFObserversTest::VectorOfObservers
WARNING TODO:
Definition: observers.cc:693
VXDTFObserversTest::counterMC::~counterMC
~counterMC()
constructor.
Definition: observers.cc:369
VXDTFObserversTest::CountUsedObserver::notify
static void notify(const Var &, otherTypes ...)
notifier counting how often a SelectionVariable was used
Definition: observers.cc:400
Belle2::MCParticle::getMomentum
TVector3 getMomentum() const
Return momentum.
Definition: MCParticle.h:209
VXDTFObserversTest::CountUsedObserver
this observer does simply count the number of times, the attached SelectionVariable was used
Definition: observers.cc:396
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< SpacePoint >
VXDTFObserversTest::CountAcceptedRejectedMCParticleObserver
this observer identifies the McParticles responsible for creating underlying clusters for given space...
Definition: observers.cc:502
VXDTFObserversTest::counter::wasInf
static unsigned int wasInf
count number of times result was inf
Definition: observers.cc:189
VXDTFObserversTest::CountContainer::key2str
static std::string key2str(const Key *aKey)
small helper for easily printing vectors
Definition: observers.cc:330
VXDTFObserversTest::CountContainer::AcceptRejectPair::Increase
void Increase(bool accepted)
Increase respective counter if accepted or not.
Definition: observers.cc:229
VXDTFObserversTest::CountContainer::PrintResults
void PrintResults(const string &identifier="unknown")
for easy printing of results collected so far
Definition: observers.cc:308
VXDTFObserversTest::ObserversTest::SetUp
virtual void SetUp()
prepare related storearrays of SpacePoints, SVD- and PXDClusters and MCParticles
Definition: observers.cc:102
Belle2::MCParticle::setMomentum
void setMomentum(const TVector3 &momentum)
Set particle momentum.
Definition: MCParticle.h:425
Belle2::Filter
This class is used to select pairs, triplets...
Definition: Filter.h:44
VXDTFObserversTest::ObserversTest::spacePointData
StoreArray< SpacePoint > spacePointData
some spacePoints to test RelationsInterface for observers.
Definition: observers.cc:172
VXDTFObserversTest::counterMC::counterMC
counterMC()
map for mcParticleIDs (key, vector of mcParticleIDs (pair: first: true, if combination was from the s...
Definition: observers.cc:368
Belle2::VoidObserver
The most CPU efficient Observer for the VXDTF filter tools (even if useless).
Definition: VoidObserver.h:40
Belle2::SpacePoint::getVxdID
VxdID getVxdID() const
Return the VxdID of the sensor on which the the cluster of the SpacePoint lives.
Definition: SpacePoint.h:158
VXDTFObserversTest::counter::accepted
static unsigned int accepted
count number of times result was accepted
Definition: observers.cc:187
VXDTFObserversTest::CountContainer::AcceptRejectPair::accept
unsigned accept
counts nTimes when it was accepted
Definition: observers.cc:238
VXDTFObserversTest::counterMC::mcIDacceptedRejected
static CountContainer mcIDacceptedRejected
map for pdgCodes (key: vector of pdgCodes found for given hits, sorted) storing how often it was acce...
Definition: observers.cc:367
VXDTFObserversTest::CountContainer::Key
std::pair< bool, Particles > Key
key for the internal container of this map
Definition: observers.cc:219
VXDTFObserversTest::CountContainer::uniqueIdentifier
static void uniqueIdentifier(ContainerType &particles)
cleans a container of double entries
Definition: observers.cc:252
VXDTFObserversTest::ObserversTest
Test class for testing and developing new Observers.
Definition: observers.cc:98