Skip to content

Commit

Permalink
Merge pull request cms-sw#306 from trackreco/no-hit-map-remap
Browse files Browse the repository at this point in the history
Use original hit indices in mkFit hits and tracks.
  • Loading branch information
osschar committed Mar 24, 2021
2 parents 4538ab4 + 6ce25ff commit b954fe3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 185 deletions.
5 changes: 3 additions & 2 deletions mkFit/HitStructures.cc
Expand Up @@ -276,7 +276,8 @@ void LayerOfHits::EndRegistrationOfHits(bool build_original_to_internal_map)

m_phi_bin_infos[q_bin][phi_bin].second++;

// m_hit_ranks[i] will never be used again ... use it to point to external index.
// m_hit_ranks[i] will never be used again ... use it to point to external/original index,
// as it does in standalone case.
m_hit_ranks[i] = k;
}

Expand All @@ -285,7 +286,7 @@ void LayerOfHits::EndRegistrationOfHits(bool build_original_to_internal_map)
if (m_max_ext_idx - m_min_ext_idx + 1 > 8*size)
{
// If this happens we might:
// a) Really use external indices for everything.
// a) Use external indices for everything. -- *** We are now. ***
// b) Build these maps for seeding layers only.
// c) Have a flag in hit-on-track that tells us if the hit index has been remapped,
// essentially, if it is a seed hit. This might not be too stupid anyway.
Expand Down
10 changes: 3 additions & 7 deletions mkFit/HitStructures.h
Expand Up @@ -231,7 +231,7 @@ class LayerOfHits
void RegisterHit(int idx);
void EndRegistrationOfHits(bool build_original_to_internal_map);

// Use this to map original indices to sorted internal ones.
// Use this to map original indices to sorted internal ones. m_ext_idcs needs to be initialized.
int GetHitIndexFromOriginal(int i) const { return m_ext_idcs[i - m_min_ext_idx]; }
// Use this to remap internal hit index to external one.
int GetOriginalHitIndex(int i) const { return m_hit_ranks[i]; }
Expand All @@ -240,12 +240,8 @@ class LayerOfHits
const Hit& GetHit(int i) const { return m_hits[i]; }
const Hit* GetHitArray() const { return m_hits; }
#else
const Hit& GetHit(int i) const { return (*m_ext_hits)[m_hit_ranks[i]]; }
const Hit* GetHitArray() const { return & (*m_ext_hits)[0]; }

// This would also be possible for COPY_SORTED_HITS, but somebody must guarantee they stay const
// after suck in -- and we need to add m_ext_hits for that case, too.
const Hit& GetHitWithOriginalIndex(int i) const { return (*m_ext_hits)[i]; }
const Hit& GetHit(int i) const { return (*m_ext_hits)[i]; }
const Hit* GetHitArray() const { return m_ext_hits->data(); }
#endif

// void SelectHitIndices(float q, float phi, float dq, float dphi, std::vector<int>& idcs, bool isForSeeding=false, bool dump=false);
Expand Down
163 changes: 0 additions & 163 deletions mkFit/MkBuilder.cc
Expand Up @@ -584,151 +584,6 @@ inline void MkBuilder::fit_one_seed_set(TrackVec& seedtracks, int itrack, int en
}
*/

//------------------------------------------------------------------------------
// Common functions for validation
//------------------------------------------------------------------------------

void MkBuilder::map_track_hits(TrackVec & tracks)
{
// map hit indices from global m_event->layerHits_[i] to hit indices in
// structure m_event_of_hits.m_layers_of_hits[i].m_hits

const int max_layer = Config::nTotalLayers;

int max = 0;
int min = std::numeric_limits<int>::max();

std::vector<int> layer_has_hits(max_layer);
for (auto&& track : tracks)
{
for (int i = 0; i < track.nTotalHits(); ++i)
{
if (track.getHitIdx(i) >= 0) ++layer_has_hits[track.getHitLyr(i)];
}
}

for (int ilayer = 0; ilayer < max_layer; ++ilayer)
{
if (layer_has_hits[ilayer])
{
const auto &loh = m_job->m_event_of_hits.m_layers_of_hits[ilayer];
const auto size = m_event->layerHits_[ilayer].size();

for (size_t index = 0; index < size; ++index)
{
const auto mcHitID = loh.GetHit(index).mcHitID();
min = std::min(min, mcHitID);
max = std::max(max, mcHitID);
}
}
}

std::vector<int> trackHitMap(max-min+1);

for (int ilayer = 0; ilayer < max_layer; ++ilayer)
{
if (layer_has_hits[ilayer])
{
const auto &loh = m_job->m_event_of_hits.m_layers_of_hits[ilayer];
const auto size = m_event->layerHits_[ilayer].size();

for (size_t index = 0; index < size; ++index)
{
trackHitMap[loh.GetHit(index).mcHitID()-min] = index;
}
}
}

for (auto&& track : tracks)
{
for (int i = 0; i < track.nTotalHits(); ++i)
{
int hitidx = track.getHitIdx(i);
int hitlyr = track.getHitLyr(i);
if (hitidx >= 0)
{
const auto & global_hit_vec = m_event->layerHits_[hitlyr];
track.setHitIdx(i, trackHitMap[global_hit_vec[hitidx].mcHitID()-min]);
// printf("YYY mapped %d/%d to %d\n", hitidx, hitlyr, trackHitMap[global_hit_vec[hitidx].mcHitID()-min]);
}
}
}
}

void MkBuilder::remap_track_hits(TrackVec & tracks)
{
// map cand hit indices from hit indices in structure
// m_event_of_hits.m_layers_of_hits[i].m_hits to global m_event->layerHits_[i]

const int max_layer = Config::nTotalLayers;

int max = 0;
int min = std::numeric_limits<int>::max();

for (int ilayer = 0; ilayer < max_layer; ++ilayer)
{
const auto & global_hit_vec = m_event->layerHits_[ilayer];
const auto size = global_hit_vec.size();
for (size_t index = 0; index < size; ++index)
{
const auto mcHitID = global_hit_vec[index].mcHitID();
min = std::min(min, mcHitID);
max = std::max(max, mcHitID);
}
}

std::vector<int> trackHitMap(max-min+1);

for (int ilayer = 0; ilayer < max_layer; ++ilayer)
{
const auto & global_hit_vec = m_event->layerHits_[ilayer];
const auto size = global_hit_vec.size();
for (size_t index = 0; index < size; ++index)
{
trackHitMap[global_hit_vec[index].mcHitID()-min] = index;
}
}

for (auto&& track : tracks)
{
for (int i = 0; i < track.nTotalHits(); ++i)
{
int hitidx = track.getHitIdx(i);
int hitlyr = track.getHitLyr(i);
if (hitidx >= 0)
{
const auto & loh = m_job->m_event_of_hits.m_layers_of_hits[hitlyr];
track.setHitIdx(i, trackHitMap[loh.GetHit(hitidx).mcHitID() - min]);
}
}
}

// Matti ... this can now be just something like this (not tested):
/*
for (auto&& track : tracks)
{
for (int i = 0; i < track.nTotalHits(); ++i)
{
int hitidx = track.getHitIdx(i);
int hitlyr = track.getHitLyr(i);
if (hitidx >= 0)
{
const auto & loh = m_event_of_hits.m_layers_of_hits[hitlyr];
track.setHitIdx(i, loh.GetOriginalHitIndex(hitidx));
}
}
}
*/
// Note that the indices after this are correct CMSSW "large-vector" indices.
// So no hit/layer mapping code in CMSSW producer is needed.
//
// We could really store original indices into HitOnTrack.index
// from the start. One just needs to be careful when getting hits in backwards fit,
// to use (a currently non-existent) LayerOfHits::GetHitWithOriginalIndex(hot.index)
//
// Further, somebody should walk over quite a bit of validation code that is
// rather involved doing such remappings.
}

//------------------------------------------------------------------------------
// Non-ROOT validation
Expand All @@ -738,10 +593,6 @@ void MkBuilder::quality_val()
{
quality_reset();

// remap hits
remap_track_hits(m_event->seedTracks_);
remap_track_hits(m_event->candidateTracks_);

std::map<int,int> cmsswLabelToPos;
if (Config::dumpForPlots && Config::readCmsswTracks)
{
Expand Down Expand Up @@ -1075,17 +926,12 @@ void MkBuilder::root_val_dumb_cmssw()

void MkBuilder::root_val()
{
// remap hits first before prepping extras
remap_track_hits(m_event->seedTracks_);
remap_track_hits(m_event->candidateTracks_);

// score the tracks
score_tracks(m_event->seedTracks_);
score_tracks(m_event->candidateTracks_);

// deal with fit tracks
if (Config::backwardFit){
remap_track_hits(m_event->fitTracks_);
score_tracks(m_event->fitTracks_);
}
else m_event->fitTracks_ = m_event->candidateTracks_;
Expand All @@ -1101,10 +947,6 @@ void MkBuilder::root_val()
void MkBuilder::cmssw_export()
{
// get the tracks ready for export
remap_track_hits(m_event->candidateTracks_);
if(Config::backwardFit) {
remap_track_hits(m_event->fitTracks_);
}
// prep_(reco)tracks doesn't actually do anything useful for CMSSW.
// We don't need the extra (seed index is obtained via canidate
// track label()), and sorting the hits by layer is actually
Expand Down Expand Up @@ -1326,8 +1168,6 @@ void MkBuilder::PrepareSeeds()
// create_seeds_from_sim_tracks();

seed_post_cleaning(m_event->seedTracks_, true, true);

map_track_hits(m_event->seedTracks_);
}
else if (Config::seedInput == cmsswSeeds)
{
Expand Down Expand Up @@ -1386,9 +1226,6 @@ void MkBuilder::PrepareSeeds()

// in rare corner cases, seed tracks could be fully cleaned out: skip mapping if so
if (m_event->seedTracks_.empty()) return;

// map seed track hits into layer_of_hits
map_track_hits(m_event->seedTracks_);
}
else if (Config::seedInput == findSeeds)
{
Expand Down
16 changes: 9 additions & 7 deletions mkFit/MkFinder.cc
Expand Up @@ -357,8 +357,8 @@ void MkFinder::SelectHitIndices(const LayerOfHits &layer_of_hits,
const float dphi = dphiv[itrack];
const float dq = dqv[itrack];

dprintf(" %2d: %6.3f %6.3f %6.6f %7.5f %3d %3d %4d %4d\n",
itrack, q, phi, dq, dphi, qb1, qb2, pb1, pb2);
dprintf(" %2d/%2d: %6.3f %6.3f %6.6f %7.5f %3d %3d %4d %4d\n",
L.layer_id(), itrack, q, phi, dq, dphi, qb1, qb2, pb1, pb2);

// MT: One could iterate in "spiral" order, to pick hits close to the center.
// http://stackoverflow.com/questions/398299/looping-in-a-spiral
Expand All @@ -383,10 +383,12 @@ void MkFinder::SelectHitIndices(const LayerOfHits &layer_of_hits,
{
// MT: Access into m_hit_zs and m_hit_phis is 1% run-time each.

if (m_iteration_hit_mask && (*m_iteration_hit_mask)[L.GetOriginalHitIndex(hi)])
int hi_orig = L.GetOriginalHitIndex(hi);

if (m_iteration_hit_mask && (*m_iteration_hit_mask)[hi_orig])
{
// printf("Yay, denying masked hit on layer %d, hi %d, orig idx %d\n",
// L.m_layer_info->m_layer_id, hi, L.GetOriginalHitIndex(hi));
// L.m_layer_info->m_layer_id, hi, hi_orig);
continue;
}

Expand All @@ -412,14 +414,14 @@ void MkFinder::SelectHitIndices(const LayerOfHits &layer_of_hits,
// Avi says we should have *minimal* search windows per layer.
// Also ... if bins are sufficiently small, we do not need the extra
// checks, see above.
if (L.GetHit(hi).mcHitID() == -7)
if (L.GetHit(hi_orig).mcHitID() == -7)
{
//ARH: This will need a better treatment but works for now
XWsrResult[itrack].m_in_gap = true;
}
else
{
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi;
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi_orig;
}
}
else
Expand All @@ -433,7 +435,7 @@ void MkFinder::SelectHitIndices(const LayerOfHits &layer_of_hits,

if (XHitSize[itrack] < MPlexHitIdxMax)
{
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi;
XHitArr.At(itrack, XHitSize[itrack]++, 0) = hi_orig;
}
}
} //hi
Expand Down
2 changes: 1 addition & 1 deletion mkFit/MkStdSeqs.cc
Expand Up @@ -57,7 +57,7 @@ void Cmssw_LoadHits_End(EventOfHits &eoh)
{
for (auto &&l : eoh.m_layers_of_hits)
{
l.EndRegistrationOfHits(true);
l.EndRegistrationOfHits(false);
}
}

Expand Down
3 changes: 3 additions & 0 deletions mkFit/MkStdSeqs.h
Expand Up @@ -15,6 +15,9 @@ namespace StdSeq

void Cmssw_LoadHits_Begin(EventOfHits &eoh, const std::vector<const HitVec*> &orig_hitvectors);
void Cmssw_LoadHits_End(EventOfHits &eoh);

// Not used anymore. Left here if we want to experiment again with
// COPY_SORTED_HITS in class LayerOfHits.
void Cmssw_Map_TrackHitIndices(const EventOfHits &eoh, TrackVec &seeds);
void Cmssw_ReMap_TrackHitIndices(const EventOfHits &eoh, TrackVec &out_tracks);

Expand Down
6 changes: 1 addition & 5 deletions mkFit/buildtestMPlex.cc
Expand Up @@ -409,7 +409,7 @@ double runBtbCe_MultiIter(Event& ev, const EventOfHits &eoh, MkBuilder& builder,
ev.clean_cms_seedtracks_iter(&seeds, Config::ItrInfo[it]);

builder.seed_post_cleaning(seeds, true, true);
builder.map_track_hits(seeds);

for (auto &s : seeds) assignSeedTypeForRanking(s);

builder.find_tracks_load_seeds(seeds);
Expand Down Expand Up @@ -524,8 +524,6 @@ void run_OneIteration(const TrackerInfo& trackerInfo, const IterationConfig &itc
// the track parameter coordinate transformation.
builder.seed_post_cleaning(seeds, true, true);

StdSeq::Cmssw_Map_TrackHitIndices(eoh, seeds);

for (auto &s : seeds) assignSeedTypeForRanking(s);

builder.find_tracks_load_seeds(seeds);
Expand All @@ -548,8 +546,6 @@ void run_OneIteration(const TrackerInfo& trackerInfo, const IterationConfig &itc
// builder.export_best_comb_cands(out_tracks);
}

StdSeq::Cmssw_ReMap_TrackHitIndices(eoh, out_tracks);

if (do_remove_duplicates)
{
StdSeq::find_duplicates(out_tracks);
Expand Down

0 comments on commit b954fe3

Please sign in to comment.