8#include <tracking/trackFindingCDC/mclookup/CDCMCMap.h>
10#include <framework/datastore/StoreArray.h>
12#include <cdc/dataobjects/CDCHit.h>
13#include <cdc/dataobjects/CDCSimHit.h>
14#include <mdst/dataobjects/MCParticle.h>
19using namespace TrackFindingCDC;
20using namespace TrackingUtilities;
23 template <
class MapType>
24 void print_map(
const MapType& m)
26 using const_iterator =
typename MapType::const_iterator;
27 for (const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter) {
28 std::cout << iter->first <<
"-->" << iter->second << std::endl;
35 B2DEBUG(25,
"In CDCMCMap::clear()");
47 B2DEBUG(25,
"In CDCMCMap::fill()");
78 int nRelatedHits = relatedHits.
size();
79 if (nRelatedHits > 1) {
80 B2WARNING(
"CDCSimHit as more than one related CDCHit - reorganize the mapping");
83 for (
const CDCHit& hit : relatedHits) {
84 const CDCHit* ptrHit = &hit;
87 B2WARNING(
"CDCHit as more than one related CDCSimHit - reorganize the mapping");
90 itInsertHint =
m_simHitsByHit.insert(itInsertHint, {ptrHit, ptrSimHit});
95 for (
const CDCHit& hit : hits) {
96 const CDCHit* ptrHit = &hit;
99 B2WARNING(
"CDCHit has no related CDCSimHit in CDCMCMap::fill()");
108 std::map<const MCParticle*, std::vector<const CDCSimHit*>> primarySimHitsByMCParticle;
110 for (
const CDCHit& hit : hits) {
111 const CDCHit* ptrHit = &hit;
114 const int nRelatedMCParticles = relatedMCParticles.
size();
116 if (nRelatedMCParticles == 0 and not
isBackground(ptrHit)) {
117 B2WARNING(
"CDCHit has no related MCParticle but CDCHit indicates that it is no "
118 "background in CDCMCMap::fill()");
121 if (nRelatedMCParticles > 1) {
122 B2WARNING(
"CDCHit as more than one related MCParticle - reorganize the mapping");
127 for (
int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
128 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
129 double weight = relatedMCParticles.
weight(iRelatedMCParticle);
136 primarySimHitsByMCParticle[ptrMCParticle].push_back(ptrSimHit);
145 int nSortedIncorretly = 0;
154 for (std::pair<
const MCParticle*
const, std::vector<const CDCSimHit*>>&
155 primarySimHitsForMCParticle : primarySimHitsByMCParticle) {
157 const MCParticle* ptrMCParticle = primarySimHitsForMCParticle.first;
158 std::vector<const CDCSimHit*>& simHits = primarySimHitsForMCParticle.second;
159 std::sort(simHits.begin(), simHits.end(), lessArrayIndex);
160 auto itSorted = std::is_sorted_until(simHits.begin(), simHits.end(), lessFlightTime);
161 if (itSorted != simHits.end()) {
164 "CDCSimHits for MCParticle " << ptrMCParticle->
getArrayIndex()
165 <<
" only sorted correctly up to hit number "
166 << std::distance(simHits.begin(), itSorted));
169 "Between wire " << (*itSorted)->getWireID() <<
" " << (*itSorted)->getFlightTime()
171 << (*itSorted)->getArrayIndex());
174 "and wire " << (*itSorted)->getWireID() <<
" " << (*itSorted)->getFlightTime()
176 << (*itSorted)->getArrayIndex());
179 if (nSortedIncorretly) {
180 B2WARNING(
"(BII-2136) CDCSimHits for "
182 <<
" primary mc particles are not sorted correctly by their time of flight");
190 for (
const CDCSimHit& simHit : simHits) {
194 const int nRelatedMCParticles = relatedMCParticles.
size();
196 if (nRelatedMCParticles == 0 and not
isBackground(ptrSimHit)) {
197 B2WARNING(
"CDCSimHit has no related MCParticle but CDCSimHit indicates that it is no "
198 "background in CDCMCMap::fill()");
201 if (nRelatedMCParticles > 1) {
202 B2WARNING(
"CDCSimHit as more than one related MCParticle - reorganize the mapping");
207 for (
int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
208 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
209 double weight = relatedMCParticles.
weight(iRelatedMCParticle);
224 for (
const CDCHit& hit : hits) {
225 const CDCHit* ptrHit = &hit;
232 if (ptrMCParticle != ptrMCParticleFromSimHit) {
233 B2WARNING(
"MCParticle from CDCHit and MCParticle from related CDCSimHit mismatch in "
234 "CDCMCMap::validateRelations()");
245 B2WARNING(
"CDCHit is reassigned secondary but related CDCSimHit is not.");
Class containing the result of the unpacker in raw data and the result of the digitizer in simulation...
double getFlightTime() const
The method to get flight time.
A Class to store the Monte Carlo particle information.
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Class for type safe access to objects that are referred to in relations.
size_t size() const
Get number of relations.
float weight(int index) const
Get weight with index.
int getArrayIndex() const
Returns this object's array index (in StoreArray), or -1 if not found.
T * getRelated(const std::string &name="", const std::string &namedRelation="") const
Get the object to or from which this object has a relation.
FROM * getRelatedFrom(const std::string &name="", const std::string &namedRelation="") const
Get the object from which this object has a relation.
virtual unsigned short getBackgroundTag() const
Get background tag.
Accessor to arrays stored in the data store.
TrackingUtilities::MayBePtr< const CDCSimHit > getSimHit(const CDCHit *hit) const
Seeks the CDCSimHit related to the CDCHit.
std::set< const CDCHit * > m_reassignedSecondaryHits
The set of reassigned secondary CDCHits.
void fillSimHitByHitMap()
Retrieve the relations array from CDCSimHits to CDCHits and fill it in to the local map which does th...
std::set< const CDCSimHit * > m_reassignedSecondarySimHits
The set of reassigned secondary CDCSimHits.
std::multimap< const CDCHit *, const CDCSimHit * > m_simHitsByHit
Memory for a one to one relation from CDCHit to CDCSimHits.
void validateRelations() const
Checks if the relations CDCHit -> MCParticle and CDCHit -> CDCSimHit -> MCParticle commute.
void fillMCParticleByHitMap()
Retrieve the relations array from MCParticle to CDCHits and fill it in to the local map which does th...
static bool indicatesReassignedSecondary(double weight)
Indicate if the given weight suggests that the corresponding hit to MCParticle relation has been redi...
TrackingUtilities::MayBePtr< const CDCHit > getHit(const CDCSimHit *simHit) const
Seeks the CDCHit related to the CDCSimHit - nullptr if no CDCHit is related.
void fillMCParticleBySimHitMap()
Retrieve the relations array from MCParticle to CDCSimHits and fill it in to the local map which does...
TrackingUtilities::MayBePtr< const MCParticle > getMCParticle(const CDCHit *hit) const
Seeks the MCParticle related to the CDCHit.
std::multimap< const MCParticle *, const CDCHit * > m_hitsByMCParticle
Memory for a one to n relation from MCParticles to CDCHit.
static bool isBackground(const CDCSimHit *simHit)
Indicates if the CDCSimHit is considered background.
bool isReassignedSecondary(const CDCSimHit *ptrSimHit) const
Indicates if the CDCSimHit has been reassigned to a primary MCParticle.
std::multimap< const MCParticle *, const CDCSimHit * > m_simHitsByMCParticle
Memory for a one to n relation from MCParticles to CDCSimHit.
void clear()
Clear all information from the former event.
void validateReassignedSecondaries() const
Checks if each CDCHit is marked as reassigned secondary is related to a reassigned secondary CDCSimHi...
void fill()
Fill the Monte Carlo information retrieved from the DataStore into the local multimaps.
bool isPrimaryParticle() const
Check if particle is a primary particle which was created by the generator (and not,...
Abstract base class for different kinds of events.