Belle II Software  release-05-01-25
ObserverCheckMCPurity.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Jakob Lettenbichler *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <tracking/spacePointCreation/MCVXDPurityInfo.h>
14 #include <tracking/spacePointCreation/PurityCalculatorTools.h> // determines purity
15 #include <tracking/trackFindingVXD/filterMap/filterFramework/TBranchLeafType.h>
16 
17 #include <tracking/spacePointCreation/SpacePoint.h>
18 
19 #include <vector>
20 #include <map>
21 #include <string>
22 
23 #include <TTree.h>
24 
25 namespace Belle2 {
32  class ObserverCheckMCPurity {
33  public:
34 
36 // // static std::map<std::string, std::unique_ptr<double>> s_results;
37  static std::map<std::string, double*> s_results;
38 
39 
41 // // static std::map<std::string, std::unique_ptr<bool>> s_wasAccepted;
42  static std::map<std::string, bool*> s_wasAccepted;
43 
44 
46 // // static std::map<std::string, std::unique_ptr<bool>> s_wasUsed;
47  static std::map<std::string, bool*> s_wasUsed;
48 
50  static TTree* s_ttree;
51 
54 
57 
60 
63 
66 
68  static int s_mainMCParticleID;
69 
71  static double s_mainPurity;
72 
73 
75  template<class Var, class RangeType>
76  static void notify(const Var&,
77  typename Var::variableType fResult,
78  const RangeType& range,
79  const typename Var::argumentType&,
80  const typename Var::argumentType&,
81  const typename Var::argumentType&)
82  {
83  // create input-container for purity-check:
84 // std::vector<const Belle2::SpacePoint*> spacePoints = {&outerHit, &centerHit, &innerHit};
85 
86  generalNotify<Var, RangeType>(fResult, range);
87  }
88 
89 
91  template<class Var, class RangeType>
92  static void notify(const Var&,
93  typename Var::variableType fResult,
94  const RangeType& range,
95  const typename Var::argumentType&,
96  const typename Var::argumentType&)
97  {
98 // // create input-container for purity-check:
99 // std::vector<const Belle2::SpacePoint*> spacePoints = {&outerHit, &innerHit};
100 
101  generalNotify<Var, RangeType>(fResult, range);
102  }
103 
104 
107 
108 
109 
114  template <class Var, class Range, typename ... types >
115  static bool initialize(Var var, Range, const types& ...)
116  {
117  auto varName = var.name();
118 
119  auto newResult = new double;
120  s_results.insert(std::make_pair(varName, newResult));
121  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_val")).c_str(), newResult, TBranchLeafType(newResult));
122 
123  auto newAccepted = new bool(false);
124  s_wasAccepted.insert(std::make_pair(varName, newAccepted));
125  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_accepted")).c_str(), newAccepted, TBranchLeafType(newAccepted));
126 
127  auto newWasUsed = new bool;
128  s_wasUsed.insert(std::make_pair(varName, newWasUsed));
129  if (s_ttree != nullptr) s_ttree->Branch((varName + std::string("_used")).c_str(), newWasUsed, TBranchLeafType(newWasUsed));
130 
131  return true;
132  }
133 
134 
136  static bool initialize(TTree* attree)
137  {
138  s_ttree = attree;
139 
140  if (s_ttree != nullptr) {
141  // added some protection for creating branches twice, as the observer is initialized recursively
142  if (!s_ttree->GetBranch("outerHit")) s_ttree->Branch("outerHit", &s_outerHitOfTwo);
143  if (!s_ttree->GetBranch("innerHit")) s_ttree->Branch("innerHit", &s_innerHitOfTwo);
144 
145  if (!s_ttree->GetBranch("mcParticleID")) s_ttree->Branch("mcParticleID", &s_mainMCParticleID);
146  if (!s_ttree->GetBranch("mcPurity")) s_ttree->Branch("mcPurity", &s_mainPurity);
147  } else {
148  return false;
149  }
150 
151  return true;
152  }
153 
154 // /** _static_ method used by the observed object to initialize the observer (partial template specialization of the general version).
155 // */
156 // template <class Var, typename ... types >
157 // static void initialize( const types& ... args);
158 //
159 
162  static void prepare(const SpacePoint& outerHit,
163  const SpacePoint& innerHit)
164  {
165  // store the hits:
166  s_outerHitOfTwo = outerHit;
167  s_innerHitOfTwo = innerHit;
168 
169  // collect purity for each particle attached to the hits
170  std::vector<const Belle2::SpacePoint*> hits = {&outerHit, &innerHit};
171 // hits.push_back(&outerHit);
172 // hits.push_back(&innerHit);
173  std::vector<Belle2::MCVXDPurityInfo> particlesFound;
174  particlesFound = createPurityInfosVec(hits);
175  // the dominating-particle is the uppermost one:
176  auto purityPack = particlesFound.at(0).getPurity();
177  s_mainMCParticleID = purityPack.first;
178  s_mainPurity = purityPack.second;
179 
180  // reset the wasUsed-flag to catch when filters are not triggered.
181  for (auto& entry : s_wasUsed) {
182  *(entry.second) = false;
183  }
184  }
185 
186 
189  template < typename ... types >
190  static void terminate(const types& ...)
191  {
192  s_results.clear();
193  s_wasAccepted.clear();
194  s_wasUsed.clear();
195  }
196 
197 
199  template < typename ... types >
200  static void collect(const types& ...)
201  {
202  // TODO:
203 // // // // collect purity for each particle attached to the hits
204 // // // std::vector<Belle2::MCVXDPurityInfo> particlesFound;
205 // // // particlesFound = createPurityInfosVec(hits);
206 
207  s_ttree->Fill();
208  }
209 
210 
211  protected:
212 
216  template<class Var, class RangeType>
217  static void generalNotify(typename Var::variableType fResult,
218  const RangeType& range)
219  {
220  // store the data retrieved:
221  auto varName = Var().name();
222  *(s_results.at(varName)) = double(fResult);
223  *(s_wasUsed.at(varName)) = true;
224  *(s_wasAccepted.at(varName)) = range.contains(fResult);
225  }
226  };
227 
228 
231 // template <class Var, typename ... types >
232 // static void initialize( TFile * rootFile, const types& ... args)
233 // {
234 // ObserverCheckMCPurity::initialize( args ...);
235 //
236 // for(auto& entry : ObserverCheckMCPurity::s_wasUsed) {
237 // *(entry.second) = false;
238 // // rootFile->Branch("expNo", &(m_data2Hit.expNo));
239 // }
240 // }
242 }
243 
244 
Belle2::TBranchLeafType
char TBranchLeafType(const char *)
Overloading TBranchLeafType to be able to get identifier 'C' for type char*.
Definition: TBranchLeafType.h:29
Belle2::ObserverCheckMCPurity::generalNotify
static void generalNotify(typename Var::variableType fResult, const RangeType &range)
unified part of the notifier function.
Definition: ObserverCheckMCPurity.h:225
Belle2::ObserverCheckMCPurity::s_innerHitOfTwo
static SpacePoint s_innerHitOfTwo
stores the inner hit of a two-hit-combination.
Definition: ObserverCheckMCPurity.h:64
Belle2::ObserverCheckMCPurity::s_mainPurity
static double s_mainPurity
purity for the dominating particleID.
Definition: ObserverCheckMCPurity.h:79
Belle2::ObserverCheckMCPurity::s_results
static std::map< std::string, double * > s_results
stores the results calculated (->value) for a selectionVariableName (->Key).
Definition: ObserverCheckMCPurity.h:45
Belle2::ObserverCheckMCPurity::terminate
static void terminate(const types &...)
static method used by the observed object to terminate the observer.
Definition: ObserverCheckMCPurity.h:198
Belle2::ObserverCheckMCPurity::notify
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...
Definition: ObserverCheckMCPurity.h:84
Belle2::ObserverCheckMCPurity::s_wasAccepted
static std::map< std::string, bool * > s_wasAccepted
stores if hits were accepted (->value) for a selectionVariableName (->Key).
Definition: ObserverCheckMCPurity.h:50
Belle2::SpacePoint
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition: SpacePoint.h:52
Belle2::ObserverCheckMCPurity::s_centerHitOfThree
static SpacePoint s_centerHitOfThree
stores the center hit of a three-hit-combination.
Definition: ObserverCheckMCPurity.h:70
Belle2::ObserverCheckMCPurity::collect
static void collect(const types &...)
fill the tree.
Definition: ObserverCheckMCPurity.h:208
Belle2::ObserverCheckMCPurity::ObserverCheckMCPurity
ObserverCheckMCPurity()
empty constructor:
Definition: ObserverCheckMCPurity.h:114
Belle2::ObserverCheckMCPurity::s_wasUsed
static std::map< std::string, bool * > s_wasUsed
stores if the filter was actually used this time (->value) for a selectionVariableName (->Key).
Definition: ObserverCheckMCPurity.h:55
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
Belle2::ObserverCheckMCPurity::s_outerHitOfThree
static SpacePoint s_outerHitOfThree
stores the outer hit of a three-hit-combination.
Definition: ObserverCheckMCPurity.h:67
Belle2::ObserverCheckMCPurity::s_ttree
static TTree * s_ttree
a ttree to store all the collected data.
Definition: ObserverCheckMCPurity.h:58
Belle2::ObserverCheckMCPurity::prepare
static void prepare(const SpacePoint &outerHit, const SpacePoint &innerHit)
static method used by the observed object to reset the stored values of the observer.
Definition: ObserverCheckMCPurity.h:170
Belle2::ObserverCheckMCPurity::s_outerHitOfTwo
static SpacePoint s_outerHitOfTwo
stores the outer hit of a two-hit-combination.
Definition: ObserverCheckMCPurity.h:61
Belle2::ObserverCheckMCPurity::initialize
static bool initialize(Var var, Range, const types &...)
static method used by the observed object to initialize the observer.
Definition: ObserverCheckMCPurity.h:123
Belle2::ObserverCheckMCPurity::s_mainMCParticleID
static int s_mainMCParticleID
dominating mcParticleID.
Definition: ObserverCheckMCPurity.h:76
Belle2::createPurityInfosVec
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>.
Definition: PurityCalculatorTools.h:159
Belle2::ObserverCheckMCPurity::s_innerHitOfThree
static SpacePoint s_innerHitOfThree
stores the inner hit of a three-hit-combination.
Definition: ObserverCheckMCPurity.h:73