9#include <tracking/trackFindingCDC/mclookup/CDCMCTrackStore.h>
11#include <tracking/trackFindingCDC/mclookup/CDCSimHitLookUp.h>
12#include <tracking/trackFindingCDC/mclookup/CDCMCMap.h>
13#include <tracking/trackFindingCDC/mclookup/CDCMCManager.h>
15#include <cdc/topology/CDCWireTopology.h>
17#include <tracking/trackingUtilities/utilities/Functional.h>
18#include <tracking/trackingUtilities/utilities/Algorithms.h>
20#include <cdc/dataobjects/CDCHit.h>
21#include <cdc/dataobjects/CDCSimHit.h>
22#include <mdst/dataobjects/MCParticle.h>
24#include <Math/Vector3D.h>
28using namespace TrackFindingCDC;
29using namespace TrackingUtilities;
40 B2DEBUG(29,
"In CDCMCTrackStore::clear()");
59 B2DEBUG(29,
"In CDCMCTrackStore::fill()");
83 B2DEBUG(28,
"m_inTrackIds.size(): " <<
m_inTrackIds.size());
86 B2DEBUG(28,
"m_nLoops.size(): " <<
m_nLoops.size());
94 B2WARNING(
"CDCMCMap not set. Cannot create tracks");
102 const MCParticle* ptrMCParticle = std::get<const MCParticle* const>(relation);
103 const CDCHit* ptrHit = std::get<const CDCHit*>(relation);
129 ITrackType mcParticleIdx = mcTrackAndMCParticleIdx.first;
132 if (mcTrack.empty())
continue;
137 auto superLayerRanges = adjacent_groupby(mcTrack.begin(), mcTrack.end(), [](
const CDCHit * hit) {
138 return hit->getISuperLayer();
141 std::vector<CDCHitVector> superLayerRuns;
142 for (
const auto& superLayerRange : superLayerRanges) {
143 superLayerRuns.push_back({superLayerRange.begin(), superLayerRange.end()});
146 std::vector<std::vector<CDCHitVector>::iterator> smallSuperLayerRuns;
147 for (
auto itSuperLayerRun = superLayerRuns.begin();
148 itSuperLayerRun != superLayerRuns.end();
150 if (itSuperLayerRun->size() < 3) smallSuperLayerRuns.push_back(itSuperLayerRun);
154 for (
auto itSuperLayerRun : smallSuperLayerRuns) {
156 ISuperLayer iSL = itSuperLayerRun->front()->getISuperLayer();
159 auto itSuperLayerRunBefore = superLayerRuns.end();
160 int hitDistanceBefore = INT_MAX;
161 if (std::distance(superLayerRuns.begin(), itSuperLayerRun) >= 2) {
162 itSuperLayerRunBefore = itSuperLayerRun - 2;
163 if (itSuperLayerRunBefore->front()->getISuperLayer() == iSL) {
164 hitDistanceBefore = (itSuperLayerRunBefore - 1)->size();
166 itSuperLayerRunBefore = superLayerRuns.end();
170 auto itSuperLayerRunAfter = superLayerRuns.end();
171 int hitDistanceAfter = INT_MAX;
172 if (std::distance(itSuperLayerRun, superLayerRuns.end()) > 2) {
173 itSuperLayerRunAfter = itSuperLayerRun + 2;
174 if (itSuperLayerRunAfter->front()->getISuperLayer() == iSL) {
175 hitDistanceAfter = (itSuperLayerRunAfter + 1)->size();
177 itSuperLayerRunAfter = superLayerRuns.end();
181 auto itMergeSuperLayerRun = superLayerRuns.end();
182 bool mergeBefore =
false;
183 if (hitDistanceBefore < hitDistanceAfter) {
184 itMergeSuperLayerRun = itSuperLayerRunBefore;
187 itMergeSuperLayerRun = itSuperLayerRunAfter;
191 if (itMergeSuperLayerRun == superLayerRuns.end())
continue;
192 else if (mergeBefore) {
193 itMergeSuperLayerRun->insert(itMergeSuperLayerRun->end(), itSuperLayerRun->begin(), itSuperLayerRun->end());
194 itSuperLayerRun->clear();
196 itMergeSuperLayerRun->insert(itMergeSuperLayerRun->begin(), itSuperLayerRun->begin(), itSuperLayerRun->end());
197 itSuperLayerRun->clear();
202 erase_remove_if(superLayerRuns,
Size() == 0u);
206 if (lhs.empty() or rhs.empty())
return true;
207 if (lhs.front()->getISuperLayer() != rhs.front()->getISuperLayer())
return false;
208 lhs.insert(lhs.end(), rhs.begin(), rhs.end());
212 erase_unique(superLayerRuns, mergeSameSuperLayer);
215 for (
const CDCHitVector& superLayerRun : superLayerRuns) {
217 auto areNeighbors = [&wireTopology](
const CDCHit * lhs,
const CDCHit * rhs) {
219 WireID rhsWireID(rhs->getISuperLayer(), rhs->getILayer(), rhs->getIWire());
223 lhsWireID == rhsWireID);
226 auto segmentRanges = unique_ranges(superLayerRun.begin(), superLayerRun.end(), areNeighbors);
228 for (
const ConstVectorRange<const CDCHit*>& segmentRange : segmentRanges) {
229 mcSegments.emplace_back(segmentRange.begin(), segmentRange.end());
243 B2WARNING(
"CDCMCMap not set. Cannot sort track");
249 std::stable_sort(mcTrack.begin(), mcTrack.end(),
250 [&simHitLookUp](
const CDCHit * ptrHit,
const CDCHit * ptrOtherHit) ->
bool {
252 const CDCSimHit* ptrSimHit = simHitLookUp.getClosestPrimarySimHit(ptrHit);
253 const CDCSimHit* ptrOtherSimHit = simHitLookUp.getClosestPrimarySimHit(ptrOtherHit);
257 B2FATAL(
"No CDCSimHit for CDCHit");
260 if (not ptrOtherSimHit)
262 B2FATAL(
"No CDCSimHit for CDCHit");
265 double secondaryFlightTime = ptrSimHit->getFlightTime();
266 double otherSecondaryFlightTime = ptrOtherSimHit->getFlightTime();
269 return (secondaryFlightTime < std::fmin(INFINITY, otherSecondaryFlightTime));
284 const CDCHitVector& mcTrack = mcTrackAndMCParticleIdx.second;
288 for (
const CDCHit* ptrHit : mcTrack) {
299 const std::vector<CDCHitVector>& mcSegments = mcSegmentsAndMCParticleIdx.second;
304 for (
const CDCHit* ptrHit : mcSegment) {
317 const std::vector<CDCHitVector>& mcSegments = mcSegmentsAndMCParticleIdx.second;
320 int nPassedSuperLayers = 0;
325 ++nPassedSuperLayers;
329 if (ptrLastMCSegment->front()->getISuperLayer() == 0 and
330 mcSegment.front()->getISuperLayer() == 0) {
335 for (
const CDCHit* ptrHit : mcSegment) {
340 ptrLastMCSegment = &mcSegment;
349 const CDCHit* ptrHit = mcSegment.front();
350 const CDCHit* ptrNextHit = nextMCSegment.front();
355 const CDCHit& hit = *ptrHit;
356 const CDCHit& nextHit = *ptrNextHit;
364 ROOT::Math::XYZVector pos(ptrSimHit->
getPosTrack());
365 ROOT::Math::XYZVector mom(ptrSimHit->
getMomentum());
366 ROOT::Math::XYZVector nextMom(ptrNextSimHit->
getMomentum());
367 ROOT::Math::XYZVector nextPos(ptrNextSimHit->
getPosTrack());
369 const auto dotXY = [](
const ROOT::Math::XYZVector & lhs,
const ROOT::Math::XYZVector & rhs) {
return lhs.X() * rhs.X() + lhs.Y() * rhs.Y(); };
370 if (dotXY(pos, nextPos) < 0)
return true;
371 if (dotXY(nextPos - pos, nextMom) < 0)
return true;
372 if (dotXY(nextPos - pos, mom) < 0)
return true;
386 return itFoundHit ==
m_inTrackIds.end() ? c_InvalidIndex : itFoundHit->second;
412 auto itFoundHit =
m_nLoops.find(ptrHit);
413 return itFoundHit ==
m_nLoops.end() ? c_InvalidIndex : itFoundHit->second;
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
unsigned short getIWire() const
Getter for iWire.
unsigned short getISuperLayer() const
Getter for iSuperLayer.
unsigned short getILayer() const
Getter for iLayer.
B2Vector3D getPosTrack() const
The method to get position on the track.
B2Vector3D getMomentum() const
The method to get momentum.
Class representing the sense wire arrangement in the whole of the central drift chamber.
bool arePrimaryNeighbors(const WireID &wireID, const WireID &otherWireID) const
Checks if two wires are primary neighbors.
bool areSeconaryNeighbors(const WireID &wireID, const WireID &otherWireID) const
Checks if two wires are secondary neighbors.
static CDCWireTopology & getInstance()
Getter for the singleton instance of the wire topology.
A Class to store the Monte Carlo particle information.
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
static const CDCMCTrackStore & getMCTrackStore()
Getter for the singleton instance of the CDCMCTrackStore.
Class to organize and present the Monte Carlo hit information.
static bool isBackground(const CDCSimHit *simHit)
Indicates if the CDCSimHit is considered background.
const std::multimap< const MCParticle *, const CDCHit * > & getHitsByMCParticle() const
Getter for the MCParticle -> CDCHit relations.
std::map< const CDCHit *, int > m_inTrackIds
Look up table for index of the hit within its track.
TrackingUtilities::Index getNPassedSuperLayers(const CDCHit *ptrHit) const
Getter for the number of super layers traversed until this hit.
std::map< ITrackType, std::vector< CDCHitVector > > m_mcSegmentsByMCParticleIdx
The memory for the segments made of CDCHits sorted for the time of flight and associated to the Monte...
void fillMCTracks()
Construct the tracks by grouping the hits by the mc particle id and sorted them for the FlightTime of...
void fillNLoopsAndNPassedSuperLayers()
Fill the look up table of the number of traversed super layers until each hit.
static const CDCMCTrackStore & getInstance()
Getter for the singletone instance.
TrackingUtilities::Index getInTrackSegmentId(const CDCHit *ptrHit) const
Getter for the index of the segment of the hit within its track.
std::vector< const CDCHit * > CDCHitVector
Type for an ordered sequence of pointers to the CDCHit.
const CDCSimHitLookUp * m_ptrSimHitLookUp
Reference to the CDCSimHit look up for additional information about related primary sim hits.
std::map< const CDCHit *, int > m_nPassedSuperLayers
Look up table for the number of super layers the particle traversed before making the individual hit.
const std::map< ITrackType, Belle2::TrackFindingCDC::CDCMCTrackStore::CDCHitVector > & getMCTracksByMCParticleIdx() const
Getter for the stored Monte Carlo tracks ordered by their Monte Carlo Id.
const CDCMCMap * m_ptrMCMap
Reference to the MC map of the current event.
void fill(const CDCMCMap *ptrMCMap, const CDCSimHitLookUp *ptrSimHitLookUp)
Fill the store with the tracks from Monte Carlo information.
TrackingUtilities::Index getNLoops(const CDCHit *ptrHit) const
Getter for the number of traversed loops until this hit.
std::map< const CDCHit *, int > m_nLoops
Look up table for the number of loops the particle traversed before making the individual hit.
const std::map< ITrackType, std::vector< Belle2::TrackFindingCDC::CDCMCTrackStore::CDCHitVector > > & getMCSegmentsByMCParticleIdx() const
Getter for the stored Monte Carlo segments ordered by their Monte Carlo Id.
void clear()
Clear all Monte Carlo hits.
void arrangeMCTrack(CDCHitVector &mcTrack) const
Sorts the given track for the FlightTime of the associated CDCSimHits.
void fillInTrackId()
Fill the look up table for the in track index of each hit.
CDCMCTrackStore()=default
Default constructor - for cppcheck.
TrackingUtilities::Index getInTrackId(const CDCHit *ptrHit) const
Getter for the index of the hit within its track.
void fillMCSegments()
Construct the segments by dividing the mc tracks in to disconnected parts and sorted them for the Fli...
std::map< const CDCHit *, int > m_inTrackSegmentIds
Look up table for index of the segment of the hits within their respective tracks.
void fillInTrackSegmentId()
Fill the look up table for the in track segment index of each hit.
bool changedSuperLayer(const CDCHitVector &mcSegment, const CDCHitVector &nextMCSegment) const
Helper function to decide whether the number of passed superlayers changed from one segment to the ne...
std::map< ITrackType, CDCHitVector > m_mcTracksByMCParticleIdx
The memory for the tracks made of CDCHits sorted for the time of flight and associated to the Monte C...
Singletone class to gather local information about the hits.
TrackingUtilities::MayBePtr< const CDCSimHit > getClosestPrimarySimHit(const CDCSimHit *ptrSimHit) const
Helper function to find the closest primary hit for the given CDCSimHit from the same MCParticle - nu...
Class to identify a wire inside the CDC.
signed short ISuperLayer
The type of the layer and superlayer ids.
Abstract base class for different kinds of events.
Functor to get the .size() from an arbitrary objects.