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;
22 template <
class MapType>
23 void print_map(
const MapType& m)
25 using const_iterator =
typename MapType::const_iterator;
26 for (const_iterator iter = m.begin(), iend = m.end(); iter != iend; ++iter) {
27 std::cout << iter->first <<
"-->" << iter->second << std::endl;
34 B2DEBUG(25,
"In CDCMCMap::clear()");
46 B2DEBUG(25,
"In CDCMCMap::fill()");
77 int nRelatedHits = relatedHits.
size();
78 if (nRelatedHits > 1) {
79 B2WARNING(
"CDCSimHit as more than one related CDCHit - reorganize the mapping");
82 for (
const CDCHit& hit : relatedHits) {
83 const CDCHit* ptrHit = &hit;
86 B2WARNING(
"CDCHit as more than one related CDCSimHit - reorganize the mapping");
89 itInsertHint =
m_simHitsByHit.insert(itInsertHint, {ptrHit, ptrSimHit});
94 for (
const CDCHit& hit : hits) {
95 const CDCHit* ptrHit = &hit;
98 B2WARNING(
"CDCHit has no related CDCSimHit in CDCMCMap::fill()");
107 std::map<const MCParticle*, std::vector<const CDCSimHit*>> primarySimHitsByMCParticle;
109 for (
const CDCHit& hit : hits) {
110 const CDCHit* ptrHit = &hit;
113 const int nRelatedMCParticles = relatedMCParticles.
size();
115 if (nRelatedMCParticles == 0 and not
isBackground(ptrHit)) {
116 B2WARNING(
"CDCHit has no related MCParticle but CDCHit indicates that it is no "
117 "background in CDCMCMap::fill()");
120 if (nRelatedMCParticles > 1) {
121 B2WARNING(
"CDCHit as more than one related MCParticle - reorganize the mapping");
126 for (
int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
127 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
128 double weight = relatedMCParticles.
weight(iRelatedMCParticle);
135 primarySimHitsByMCParticle[ptrMCParticle].push_back(ptrSimHit);
144 int nSortedIncorretly = 0;
153 for (std::pair<
const MCParticle*
const, std::vector<const CDCSimHit*>>&
154 primarySimHitsForMCParticle : primarySimHitsByMCParticle) {
156 const MCParticle* ptrMCParticle = primarySimHitsForMCParticle.first;
157 std::vector<const CDCSimHit*>& simHits = primarySimHitsForMCParticle.second;
158 std::sort(simHits.begin(), simHits.end(), lessArrayIndex);
159 auto itSorted = std::is_sorted_until(simHits.begin(), simHits.end(), lessFlightTime);
160 if (itSorted != simHits.end()) {
163 "CDCSimHits for MCParticle " << ptrMCParticle->
getArrayIndex()
164 <<
" only sorted correctly up to hit number "
165 << std::distance(simHits.begin(), itSorted));
168 "Between wire " << (*itSorted)->getWireID() <<
" " << (*itSorted)->getFlightTime()
170 << (*itSorted)->getArrayIndex());
173 "and wire " << (*itSorted)->getWireID() <<
" " << (*itSorted)->getFlightTime()
175 << (*itSorted)->getArrayIndex());
178 if (nSortedIncorretly) {
179 B2WARNING(
"(BII-2136) CDCSimHits for "
181 <<
" primary mc particles are not sorted correctly by their time of flight");
189 for (
const CDCSimHit& simHit : simHits) {
193 const int nRelatedMCParticles = relatedMCParticles.
size();
195 if (nRelatedMCParticles == 0 and not
isBackground(ptrSimHit)) {
196 B2WARNING(
"CDCSimHit has no related MCParticle but CDCSimHit indicates that it is no "
197 "background in CDCMCMap::fill()");
200 if (nRelatedMCParticles > 1) {
201 B2WARNING(
"CDCSimHit as more than one related MCParticle - reorganize the mapping");
206 for (
int iRelatedMCParticle = 0; iRelatedMCParticle < nRelatedMCParticles; ++iRelatedMCParticle) {
207 const MCParticle* ptrMCParticle = relatedMCParticles[iRelatedMCParticle];
208 double weight = relatedMCParticles.
weight(iRelatedMCParticle);
223 for (
const CDCHit& hit : hits) {
224 const CDCHit* ptrHit = &hit;
231 if (ptrMCParticle != ptrMCParticleFromSimHit) {
232 B2WARNING(
"MCParticle from CDCHit and MCParticle from related CDCSimHit mismatch in "
233 "CDCMCMap::validateRelations()");
244 B2WARNING(
"CDCHit is reassigned secondary but related CDCSimHit is not.");
251 return hit ? hit->getRelated<
CDCSimHit>() :
nullptr;
271 return hit ? hit->getRelated<
MCParticle>() :
nullptr;
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.
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...
bool isBackground(const CDCSimHit *simHit) const
Indicates if the CDCSimHit is considered background.
static bool indicatesReassignedSecondary(double weight)
Indicate if the given weight suggests that the corresponding hit to MCParticle relation has been redi...
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...
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.
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.