10#include <tracking/trackFindingCDC/mclookup/CDCMCHitCollectionLookUp.h>
12#include <tracking/trackFindingCDC/eventdata/trajectories/CDCTrajectory3D.h>
14#include <tracking/trackFindingCDC/utilities/ReversedRange.h>
15#include <tracking/trackFindingCDC/utilities/Functional.h>
17#include <cdc/dataobjects/CDCHit.h>
18#include <cdc/dataobjects/CDCSimHit.h>
19#include <mdst/dataobjects/MCParticle.h>
21#include <TDatabasePDG.h>
28 namespace TrackFindingCDC {
30 template <
class ACDCHitCollection>
33 B2DEBUG(25,
"Clearing CDCMCHitCollectionLookUp<ACDCHitCollection>");
36 template <
class ACDCHitCollection>
37 std::map<ITrackType, size_t>
39 const ACDCHitCollection& hits)
const
43 std::map<ITrackType, size_t> hitCountByMCTrackId;
44 for (
const CDCHit* ptrHit : hits) {
47 if (hitCountByMCTrackId.count(mcTrackId) == 0) hitCountByMCTrackId[mcTrackId] = 0;
48 ++(hitCountByMCTrackId[mcTrackId]);
50 return hitCountByMCTrackId;
53 template <
class ACDCHitCollection>
55 const ACDCHitCollection& hits)
const
57 std::map<ITrackType, size_t> hitCountByMCTrackId = getHitCountByMCTrackId(hits);
60 std::pair<ITrackType, size_t> highestHitCountMCTrackId(0, 0);
61 static_cast<void>(std::max_element(hitCountByMCTrackId.begin(), hitCountByMCTrackId.end(),
LessOf<Second>()));
63 for (
const auto& hitCountForMCTrackId : hitCountByMCTrackId) {
65 nHits += hitCountForMCTrackId.second;
67 if (highestHitCountMCTrackId.second < hitCountForMCTrackId.second) {
68 highestHitCountMCTrackId = hitCountForMCTrackId;
74 int correctRLVote = 0;
75 for (
const auto& recoHit : hits) {
76 const CDCHit* hit = recoHit;
79 if (rlInfo == mcRLInfo) {
86 const float purity =
static_cast<float>(highestHitCountMCTrackId.second) / nHits;
90 template <
class ACDCHitCollection>
94 if (not ptrHits)
return INVALID_ITRACK;
95 const ACDCHitCollection& hits = *ptrHits;
97 if (mcTrackIdAndPurity.
getPurity() >= m_minimalMatchPurity) {
100 return INVALID_ITRACK;
104 template <
class ACDCHitCollection>
106 const ACDCHitCollection* ptrHits)
const
108 if (not ptrHits)
return INVALID_ITRACK;
109 const ACDCHitCollection& hits = *ptrHits;
111 if (mcTrackIdAndPurity.
getPurity() >= m_minimalMatchPurity) {
118 template <
class ACDCHitCollection>
123 if (fbInfo == EForwardBackward::c_Invalid)
return NAN;
125 int correctRLVote = getCorrectRLVote(ptrHits);
127 if (fbInfo == EForwardBackward::c_Backward) {
128 correctRLVote = -correctRLVote;
131 int nCorrectRL = (correctRLVote + ptrHits->size()) / 2;
132 float rlPurity = 1.0 * nCorrectRL / ptrHits->size();
136 template <
class ACDCHitCollection>
138 const ACDCHitCollection* ptrHits)
const
140 const CDCHit* ptrHit = getFirstHit(ptrHits);
145 template <
class ACDCHitCollection>
149 if (not ptrHits)
return nullptr;
150 const ACDCHitCollection& hits = *ptrHits;
152 ITrackType mcTrackId = getMCTrackId(ptrHits);
153 if (mcTrackId == INVALID_ITRACK)
return nullptr;
157 for (
const CDCHit* hit : hits) {
158 if (mcTrackId == mcHitLookUp.
getMCTrackId(hit))
return hit;
163 template <
class ACDCHitCollection>
168 if (not ptrHits)
return nullptr;
169 const ACDCHitCollection& hits = *ptrHits;
171 ITrackType mcTrackId = getMCTrackId(ptrHits);
172 if (mcTrackId == INVALID_ITRACK)
return nullptr;
176 for (
const CDCHit* hit : reversedRange(hits)) {
177 if (mcTrackId == mcHitLookUp.
getMCTrackId(hit))
return hit;
182 template <
class ACDCHitCollection>
184 const ACDCHitCollection* ptrHits)
const
186 Index firstInTrackId = getFirstInTrackId(ptrHits);
187 Index lastInTrackId = getLastInTrackId(ptrHits);
188 if (firstInTrackId == c_InvalidIndex or lastInTrackId == c_InvalidIndex) {
189 return EForwardBackward::c_Invalid;
190 }
else if (firstInTrackId < lastInTrackId) {
191 return EForwardBackward::c_Forward;
192 }
else if (firstInTrackId > lastInTrackId) {
193 return EForwardBackward::c_Backward;
194 }
else if (firstInTrackId == lastInTrackId) {
195 return EForwardBackward::c_Unknown;
197 return EForwardBackward::c_Invalid;
200 template <
class ACDCHitCollection>
202 const ACDCHitCollection* ptrFromHits,
203 const ACDCHitCollection* ptrToHits)
const
206 ITrackType fromMCTrackId = getMCTrackId(ptrFromHits);
207 if (fromMCTrackId == INVALID_ITRACK)
return EForwardBackward::c_Invalid;
209 ITrackType toMCTrackId = getMCTrackId(ptrToHits);
210 if (toMCTrackId == INVALID_ITRACK)
return EForwardBackward::c_Invalid;
212 if (fromMCTrackId != toMCTrackId)
return EForwardBackward::c_Invalid;
216 if (fromFBInfo == EForwardBackward::c_Invalid)
return EForwardBackward::c_Invalid;
219 if (toFBInfo == EForwardBackward::c_Invalid)
return EForwardBackward::c_Invalid;
221 if (fromFBInfo != toFBInfo)
return EForwardBackward::c_Invalid;
226 Index lastNPassedSuperLayersOfFromHits = getLastNPassedSuperLayers(ptrFromHits);
229 if (lastNPassedSuperLayersOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
231 Index firstNPassedSuperLayersOfToHits = getFirstNPassedSuperLayers(ptrToHits);
233 if (firstNPassedSuperLayersOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
236 if (lastNPassedSuperLayersOfFromHits < firstNPassedSuperLayersOfToHits) {
237 if (fromFBInfo == EForwardBackward::c_Forward and
238 toFBInfo == EForwardBackward::c_Forward) {
239 return EForwardBackward::c_Forward;
241 return EForwardBackward::c_Invalid;
243 }
else if (firstNPassedSuperLayersOfToHits < lastNPassedSuperLayersOfFromHits) {
244 if (fromFBInfo == EForwardBackward::c_Backward and
245 toFBInfo == EForwardBackward::c_Backward) {
246 return EForwardBackward::c_Backward;
248 return EForwardBackward::c_Invalid;
256 Index lastInTrackSegmentIdOfFromHits = getLastInTrackSegmentId(ptrFromHits);
259 if (lastInTrackSegmentIdOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
261 Index firstInTrackSegmentIdOfToHits = getFirstInTrackSegmentId(ptrToHits);
263 if (firstInTrackSegmentIdOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
266 if (lastInTrackSegmentIdOfFromHits < firstInTrackSegmentIdOfToHits) {
267 if (fromFBInfo == EForwardBackward::c_Forward and
268 toFBInfo == EForwardBackward::c_Forward) {
269 return EForwardBackward::c_Forward;
271 return EForwardBackward::c_Invalid;
273 }
else if (firstInTrackSegmentIdOfToHits < lastInTrackSegmentIdOfFromHits) {
276 if (fromFBInfo == EForwardBackward::c_Backward and
277 toFBInfo == EForwardBackward::c_Backward) {
278 return EForwardBackward::c_Backward;
280 return EForwardBackward::c_Invalid;
288 Index lastInTrackIdOfFromHits = getLastInTrackId(ptrFromHits);
290 if (lastInTrackIdOfFromHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
292 Index firstInTrackIdOfToHits = getFirstInTrackId(ptrToHits);
294 if (firstInTrackIdOfToHits == c_InvalidIndex)
return EForwardBackward::c_Invalid;
299 if (lastInTrackIdOfFromHits - 1 < firstInTrackIdOfToHits + 1) {
300 if (fromFBInfo == EForwardBackward::c_Forward and
301 toFBInfo == EForwardBackward::c_Forward) {
302 return EForwardBackward::c_Forward;
306 if (firstInTrackIdOfToHits - 1 < lastInTrackIdOfFromHits + 1) {
307 if (fromFBInfo == EForwardBackward::c_Backward and
308 toFBInfo == EForwardBackward::c_Backward) {
309 return EForwardBackward::c_Backward;
314 return EForwardBackward::c_Invalid;
317 template <
class ACDCHitCollection>
319 const ACDCHitCollection* ptrFromHits,
320 const ACDCHitCollection* ptrToHits)
const
323 if (result == EForwardBackward::c_Invalid)
return result;
325 int fromCorrectRLVote = getCorrectRLVote(ptrFromHits);
326 int toCorrectRLVote = getCorrectRLVote(ptrToHits);
328 if (result == EForwardBackward::c_Backward) {
329 fromCorrectRLVote = -fromCorrectRLVote;
330 toCorrectRLVote = -toCorrectRLVote;
333 int fromNCorrectRL = (fromCorrectRLVote + ptrFromHits->size()) / 2;
334 int toNCorrectRL = (toCorrectRLVote + ptrToHits->size()) / 2;
336 float fromRLPurity = 1.0 * fromNCorrectRL / ptrFromHits->size();
337 float toRLPurity = 1.0 * toNCorrectRL / ptrToHits->size();
341 if (fromRLPurity > m_minimalRLPurity and toRLPurity > m_minimalRLPurity and
342 fromNCorrectRL > 2.5 and toNCorrectRL > 2.5) {
346 return EForwardBackward::c_Invalid;
349 template <
class ACDCHitCollection>
351 const ACDCHitCollection* ptrHits)
const
354 B2WARNING(
"Segment is nullptr. Could not get fit.");
360 const CDCHit* ptrFirstHit = getFirstHit(ptrHits);
363 if (not ptrPrimarySimHit) {
365 ptrPrimarySimHit = mcHitLookUp.
getSimHit(ptrFirstHit);
366 if (not ptrPrimarySimHit) {
371 const CDCSimHit& primarySimHit = *ptrPrimarySimHit;
378 const TParticlePDG* ptrTPDGParticle = TDatabasePDG::Instance()->GetParticle(pdgCode);
380 if (not ptrTPDGParticle) {
381 B2WARNING(
"No particle for PDG code " << pdgCode <<
". Could not get fit");
385 const TParticlePDG& tPDGParticle = *ptrTPDGParticle;
387 double charge = tPDGParticle.Charge() / 3.0;
389 ESign chargeSign = sign(charge);
395 if (chargeSign != settedChargeSign) {
396 B2WARNING(
"Charge sign of mc particle is not the same as the one of the fit");
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
int getPDGCode() const
The method to get PDG code.
double getFlightTime() const
The method to get flight time.
B2Vector3D getPosTrack() const
The method to get position on the track.
B2Vector3D getMomentum() const
The method to get momentum.
A Class to store the Monte Carlo particle information.
EForwardBackward isForwardOrBackwardToMCTrack(const ACDCHitCollection *ptrHits) const
Returns the orientation of the collection of hits relative to its matched track.
ITrackType getMCTrackId(const ACDCHitCollection *ptrHits) const
Getter for the Monte Carlo track id matched to this collection of hits.
std::map< ITrackType, size_t > getHitCountByMCTrackId(const ACDCHitCollection &hits) const
Fill a map with the number of hits for each track id contained in the given hit range.
MCTrackIdPurityPair getHighestPurity(const ACDCHitCollection &hits) const
Get the track id with the highest corresponding purity.
const CDCHit * getLastHit(const ACDCHitCollection *ptrHits) const
Getter for the last hit in the collection of hits which has the Monte Carlo track id matched to this ...
EForwardBackward areAlignedInMCTrackWithRLCheck(const ACDCHitCollection *ptrFromHits, const ACDCHitCollection *ptrToHits) const
Returns if the second collection of hits follows the first collection of hits in their common Monte C...
const MCParticle * getMCParticle(const ACDCHitCollection *ptrHits) const
Getter for the mc particle matched to this collection of hits.
int getCorrectRLVote(const ACDCHitCollection *ptrHits) const
Getter for the difference of correct versus incorrect right left passage information.
void clear()
Clears all Monte Carlo information left from the last event.
const CDCHit * getFirstHit(const ACDCHitCollection *ptrHits) const
Getter for the first hit in the collection of hits which has the Monte Carlo track id matched to this...
double getRLPurity(const ACDCHitCollection *ptrHits) const
Getter for the right left passge purity which respects the forward backward reconstruction.
CDCTrajectory3D getTrajectory3D(const ACDCHitCollection *ptrHits) const
Returns the trajectory of the collection of hits.
EForwardBackward areAlignedInMCTrack(const ACDCHitCollection *ptrFromHits, const ACDCHitCollection *ptrToHits) const
Returns if the second collection of hits follows the first collection of hits in their common Monte C...
Interface class to the Monte Carlo information for individual hits.
const CDCSimHit * getClosestPrimarySimHit(const CDCHit *ptrHit) const
Getter for the closest simulated hit of a primary particle to the given hit - may return nullptr of n...
ITrackType getMCTrackId(const CDCHit *ptrHit) const
Returns the track id for the hit.
const Belle2::MCParticle * getMCParticle(const CDCHit *ptrHit) const
Getter for the MCParticle which is related to the CDCHit contained in the given wire hit.
const Belle2::CDCSimHit * getSimHit(const CDCHit *ptrHit) const
Getter for the CDCSimHit which is related to the CDCHit contained in the given wire hit.
static const CDCMCHitLookUp & getInstance()
Getter for the singletone instance.
ERightLeft getRLInfo(const CDCHit *ptrHit) const
Returns the true right left passage information.
Particle full three dimensional trajectory.
ESign getChargeSign() const
Gets the charge sign of the trajectory.
A three dimensional vector.
ESign
Enumeration for the distinct sign values of floating point variables.
EForwardBackward
Enumeration to represent the distinct possibilities of the right left passage information.
ERightLeft
Enumeration to represent the distinct possibilities of the right left passage.
Abstract base class for different kinds of events.
Functor factory turning a binary functor and two functors into a new functor which executes the binar...
Structure representing a matched Monte Carlo track id with the corresponding purity.
int getCorrectRLVote() const
Getter for the rl vote.
float getPurity() const
Getter for the purity.
ITrackType getMCTrackId() const
Getter for the Monte Carlo track Id.