Belle II Software  release-08-01-10
ObserverCheckMCPurity.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 <tracking/spacePointCreation/MCVXDPurityInfo.h>
12 #include <tracking/spacePointCreation/PurityCalculatorTools.h> // determines purity
13 #include <tracking/trackFindingVXD/filterMap/filterFramework/TBranchLeafType.h>
14 
15 #include <tracking/spacePointCreation/SpacePoint.h>
16 
17 #include <vector>
18 #include <map>
19 #include <string>
20 
21 #include <TTree.h>
22 
23 namespace Belle2 {
31  public:
32 
34 // // static std::map<std::string, std::unique_ptr<double>> s_results;
35  static std::map<std::string, double*> s_results;
36 
37 
39 // // static std::map<std::string, std::unique_ptr<bool>> s_wasAccepted;
40  static std::map<std::string, bool*> s_wasAccepted;
41 
42 
44 // // static std::map<std::string, std::unique_ptr<bool>> s_wasUsed;
45  static std::map<std::string, bool*> s_wasUsed;
46 
48  static TTree* s_ttree;
49 
52 
55 
58 
61 
64 
66  static int s_mainMCParticleID;
67 
69  static double s_mainPurity;
70 
71 
73  template<class Var, class RangeType>
74  static void notify(const Var&,
75  typename Var::variableType fResult,
76  const RangeType& range,
77  const typename Var::argumentType&,
78  const typename Var::argumentType&,
79  const typename Var::argumentType&)
80  {
81  // create input-container for purity-check:
82 // std::vector<const Belle2::SpacePoint*> spacePoints = {&outerHit, &centerHit, &innerHit};
83 
84  generalNotify<Var, RangeType>(fResult, range);
85  }
86 
87 
89  template<class Var, class RangeType>
90  static void notify(const Var&,
91  typename Var::variableType fResult,
92  const RangeType& range,
93  const typename Var::argumentType&,
94  const typename Var::argumentType&)
95  {
96 // // create input-container for purity-check:
97 // std::vector<const Belle2::SpacePoint*> spacePoints = {&outerHit, &innerHit};
98 
99  generalNotify<Var, RangeType>(fResult, range);
100  }
101 
102 
105 
106 
107 
112  template <class Var, class Range, typename ... types >
113  static bool initialize(Var var, Range, const types& ...)
114  {
115  auto varName = var.name();
116 
117  auto newResult = new double;
118  s_results.insert(std::make_pair(varName, newResult));
119  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_val")).c_str(), newResult, TBranchLeafType(newResult));
120 
121  auto newAccepted = new bool(false);
122  s_wasAccepted.insert(std::make_pair(varName, newAccepted));
123  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_accepted")).c_str(), newAccepted, TBranchLeafType(newAccepted));
124 
125  auto newWasUsed = new bool;
126  s_wasUsed.insert(std::make_pair(varName, newWasUsed));
127  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_used")).c_str(), newWasUsed, TBranchLeafType(newWasUsed));
128 
129  return true;
130  }
131 
132 
134  static bool initialize(TTree* attree)
135  {
136  s_ttree = attree;
137 
138  if (s_ttree != nullptr) {
139  // added some protection for creating branches twice, as the observer is initialized recursively
140  if (!s_ttree->GetBranch("outerHit")) s_ttree->Branch("outerHit", &s_outerHitOfTwo);
141  if (!s_ttree->GetBranch("innerHit")) s_ttree->Branch("innerHit", &s_innerHitOfTwo);
142 
143  if (!s_ttree->GetBranch("mcParticleID")) s_ttree->Branch("mcParticleID", &s_mainMCParticleID);
144  if (!s_ttree->GetBranch("mcPurity")) s_ttree->Branch("mcPurity", &s_mainPurity);
145  } else {
146  return false;
147  }
148 
149  return true;
150  }
151 
152 // /** _static_ method used by the observed object to initialize the observer (partial template specialization of the general version).
153 // */
154 // template <class Var, typename ... types >
155 // static void initialize( const types& ... args);
156 //
157 
160  static void prepare(const SpacePoint& outerHit,
161  const SpacePoint& innerHit)
162  {
163  // store the hits:
164  s_outerHitOfTwo = outerHit;
165  s_innerHitOfTwo = innerHit;
166 
167  // collect purity for each particle attached to the hits
168  std::vector<const Belle2::SpacePoint*> hits = {&outerHit, &innerHit};
169 // hits.push_back(&outerHit);
170 // hits.push_back(&innerHit);
171  std::vector<Belle2::MCVXDPurityInfo> particlesFound;
172  particlesFound = createPurityInfosVec(hits);
173  // the dominating-particle is the uppermost one:
174  auto purityPack = particlesFound.at(0).getPurity();
175  s_mainMCParticleID = purityPack.first;
176  s_mainPurity = purityPack.second;
177 
178  // reset the wasUsed-flag to catch when filters are not triggered.
179  for (auto& entry : s_wasUsed) {
180  *(entry.second) = false;
181  }
182  }
183 
184 
187  template < typename ... types >
188  static void terminate(const types& ...)
189  {
190  s_results.clear();
191  s_wasAccepted.clear();
192  s_wasUsed.clear();
193  }
194 
195 
197  template < typename ... types >
198  static void collect(const types& ...)
199  {
200  // TODO:
201 // // // // collect purity for each particle attached to the hits
202 // // // std::vector<Belle2::MCVXDPurityInfo> particlesFound;
203 // // // particlesFound = createPurityInfosVec(hits);
204 
205  s_ttree->Fill();
206  }
207 
208 
209  protected:
210 
214  template<class Var, class RangeType>
215  static void generalNotify(typename Var::variableType fResult,
216  const RangeType& range)
217  {
218  // store the data retrieved:
219  auto varName = Var().name();
220  *(s_results.at(varName)) = double(fResult);
221  *(s_wasUsed.at(varName)) = true;
222  *(s_wasAccepted.at(varName)) = range.contains(fResult);
223  }
224  };
225 
226 
229 // template <class Var, typename ... types >
230 // static void initialize( TFile * rootFile, const types& ... args)
231 // {
232 // ObserverCheckMCPurity::initialize( args ...);
233 //
234 // for(auto& entry : ObserverCheckMCPurity::s_wasUsed) {
235 // *(entry.second) = false;
236 // // rootFile->Branch("expNo", &(m_data2Hit.expNo));
237 // }
238 // }
240 }
241 
242 
this observer searches for mcParticles attached to the hits given and stores the information found to...
static bool initialize(Var var, Range, const types &...)
static method used by the observed object to initialize the observer.
static SpacePoint s_innerHitOfTwo
stores the inner hit of a two-hit-combination.
static int s_mainMCParticleID
dominating mcParticleID.
static std::map< std::string, bool * > s_wasAccepted
stores if hits were accepted (->value) for a selectionVariableName (->Key).
static TTree * s_ttree
a ttree to store all the collected data.
static bool initialize(TTree *attree)
version for 2-hit-combinations.
static void terminate(const types &...)
static method used by the observed object to terminate the observer.
ObserverCheckMCPurity()
empty constructor:
static double s_mainPurity
purity for the dominating particleID.
static void prepare(const SpacePoint &outerHit, const SpacePoint &innerHit)
static method used by the observed object to reset the stored values of the observer.
static SpacePoint s_centerHitOfThree
stores the center hit of a three-hit-combination.
static void notify(const Var &, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &, const typename Var::argumentType &)
notifier which finds the mcParticles attached to given pair of spacePoints and determines the puritie...
static void generalNotify(typename Var::variableType fResult, const RangeType &range)
unified part of the notifier function.
static SpacePoint s_innerHitOfThree
stores the inner hit of a three-hit-combination.
static void notify(const Var &, typename Var::variableType fResult, const RangeType &range, const typename Var::argumentType &, const typename Var::argumentType &, const typename Var::argumentType &)
notifier which finds the mcParticles attached to given triplet of spacePoints and determines the puri...
static SpacePoint s_outerHitOfThree
stores the outer hit of a three-hit-combination.
static SpacePoint s_outerHitOfTwo
stores the outer hit of a two-hit-combination.
static std::map< std::string, double * > s_results
stores the results calculated (->value) for a selectionVariableName (->Key).
static void collect(const types &...)
fill the tree.
static std::map< std::string, bool * > s_wasUsed
stores if the filter was actually used this time (->value) for a selectionVariableName (->Key).
Represents a range of arithmetic types.
Definition: Range.h:29
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:42
char TBranchLeafType(const char *)
Overloading TBranchLeafType to be able to get identifier 'C' for type char*.
static std::vector< Belle2::MCVXDPurityInfo > createPurityInfosVec(const std::vector< const Belle2::SpacePoint * > &spacePoints)
create a vector of MCVXDPurityInfos objects for a std::vector<Belle2::SpacePoints>.
Abstract base class for different kinds of events.