Skip to content

Commit

Permalink
KFin emulator (cms-sw#143)
Browse files Browse the repository at this point in the history
* initial commit.

* move ProducerTBout.cc from test to plugins

* KFin fully debugged.

* Producer TBout added to NTupleMaker

* chriss change to KFout

* Louises first comments.

* fixing rebase errors.

* KF reintegrated, stub residuals are not bit accurate yet.

* code format.

* added 1k ttbar@0PU events for internal kf maths tuning.

* Track builder r residual debug and KF digi function corrected.

* bug in z residual base fixed.

* Ians first comments.

* Ians secodn comments.

* recalculate z ressid from TTStubRefs, use TTStubRefs for seed stubs instead of fake stubs.

* Ians third comment.

* fixed comments TTDTC::Frames -> tt::Frames

* old stub uncertainty caclulation re-enabled.

* Ians 4th comments.

* code-checks.

* tilted stub z uncertainty fixed. not yet debugged against f/w.

* set max layers a kff adds to a track to 7 as we did in the past, no clue why it is 4 again ...

* agrrement with f/w, ready to merge.
  • Loading branch information
tschuh authored and tomalin committed May 31, 2022
1 parent daadb9c commit 05dca1b
Show file tree
Hide file tree
Showing 67 changed files with 1,666 additions and 744 deletions.
6 changes: 3 additions & 3 deletions L1Trigger/TrackFindingTMTT/interface/KFParamsComb.h
Expand Up @@ -8,7 +8,7 @@
///===
///=== All variable names & equations come from Fruhwirth KF paper
///=== http://dx.doi.org/10.1016/0168-9002%2887%2990887-4
///===
///===
///=== Summary of variables:
///=== m = hit position (phi,z)
///=== V = hit position 2x2 covariance matrix in (phi,z).
Expand All @@ -24,8 +24,8 @@
///=== S = H*C (2x4 matrix); St = Transpose S
///=== R = V + H*C*Ht (KF paper) = V + H*St (used here at simpler): 2x2 matrix
///=== Rinv = Inverse R
///=== K = St * Rinv : 2x2 Kalman gain matrix * det(R)
///=== r = m - H*x
///=== K = St * Rinv : 2x2 Kalman gain matrix * det(R)
///=== r = m - H*x
///=== x' = x + K*r
///=== C' = C - K*H*C (KF paper) = C - K*S (used here as simpler)
///=== delta(chi2) = r(transpose) * Rinv * r : Increase in chi2 from new stub added during iteration.
Expand Down
9 changes: 8 additions & 1 deletion L1Trigger/TrackFindingTMTT/interface/KFbase.h
Expand Up @@ -58,7 +58,14 @@ namespace tmtt {
// element.second is kalman layer when stub is from endcap, 7 is invalid

static constexpr std::pair<unsigned, unsigned> layerMap[nEta / 2][nGPlayID + 1] = {
{{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6 -- Ultimate config with "maybe layer"
{{7, 7},
{0, 7},
{1, 7},
{5, 7},
{4, 7},
{3, 7},
{7, 7},
{2, 7}}, // B1 B2 B3 B4 B5 B6 -- Ultimate config with "maybe layer"
{{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
{{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
{{7, 7}, {0, 7}, {1, 7}, {5, 7}, {4, 7}, {3, 7}, {7, 7}, {2, 7}}, // B1 B2 B3 B4 B5 B6
Expand Down
40 changes: 29 additions & 11 deletions L1Trigger/TrackFindingTracklet/interface/ChannelAssignment.h
Expand Up @@ -23,17 +23,31 @@ namespace trklet {
ChannelAssignment(const edm::ParameterSet& iConfig, const tt::Setup* setup);
~ChannelAssignment() {}
// sets channelId of given TTTrackRef from TrackBuilder or PurgeDuplicate (if enabled), return false if track outside pt range
bool channelId(const TTTrackRef& ttTrackRef, int& channelId) const;
// number of used channels
int numChannels() const { return numChannels_; }
// sets layerId (0-7 in sequence the seed type projects to) aka stub channel id of given TTStubRef and seedType, returns false if seeed stub
bool channelId(const TTTrackRef& ttTrackRef, int& channelId);
// number of used channels for tracks
int numChannelsTrack() const { return numChannelsTrack_; }
// number of used channels for stubs
int numChannelsStub() const { return numChannelsStub_; }
// sets layerId (0-7 in sequence the seed type projects to) of given TTStubRef and seedType, returns false if seeed stub
bool layerId(int seedType, const TTStubRef& ttStubRef, int& layerId) const;
// return tracklet layerId (barrel: [0-5], endcap: [6-10]) for given TTStubRef
int trackletLayerId(const TTStubRef& ttStubRef) const { return setup_->trackletLayerId(ttStubRef); };
// max number layers a sedd type may project to
int maxNumProjectionLayers() const { return maxNumProjectionLayers_; }
int trackletLayerId(const TTStubRef& ttStubRef) const;
// number layers a given seed type projects to
int numProjectionLayers(int seedType) const { return (int)seedTypesProjectionLayers_.at(seedType).size(); }
// map of used DTC tfp channels in InputRouter
std::vector<int> channelEncoding() const { return channelEncoding_; }
// index of first stub channel belonging to given track channel
int offsetStub(int channelTrack) const;
// seed layers for given seed type id
std::vector<int> seedingLayers(int seedType) const { return seedTypesSeedLayers_.at(seedType); }
//
tt::SensorModule::Type type(const TTStubRef& ttStubRef) const { return setup_->type(ttStubRef); }
//
int layerId(int seedType, int channel) const { return seedTypesProjectionLayers_.at(seedType).at(channel); }
//
int channelId(int seedType, int layerId) const;
// max number of seeding layers
int numSeedingLayers() const { return numSeedingLayers_; }

private:
// helper class to store configurations
Expand All @@ -46,16 +60,20 @@ namespace trklet {
std::vector<std::string> seedTypeNames_;
// number of used seed types in tracklet algorithm
int numSeedTypes_;
// number of used channels
int numChannels_;
// max number layers a seed type may project to
int maxNumProjectionLayers_;
// number of used channels for tracks
int numChannelsTrack_;
// number of used channels for stubs
int numChannelsStub_;
// seeding layers of seed types using default layer id [barrel: 1-6, discs: 11-15]
std::vector<std::vector<int>> seedTypesSeedLayers_;
// layers a seed types can project to using default layer id [barrel: 1-6, discs: 11-15]
std::vector<std::vector<int>> seedTypesProjectionLayers_;
// map of used DTC tfp channels in InputRouter
std::vector<int> channelEncoding_;
// accumulated number of projections layer from seed 0 to vector index
std::vector<int> offsetsStubs_;
// max number of seeding layers
int numSeedingLayers_;
};

} // namespace trklet
Expand Down
144 changes: 144 additions & 0 deletions L1Trigger/TrackFindingTracklet/interface/KFin.h
@@ -0,0 +1,144 @@
#ifndef L1Trigger_TrackFindingTracklet_KFin_h
#define L1Trigger_TrackFindingTracklet_KFin_h

#include "L1Trigger/TrackTrigger/interface/Setup.h"
#include "L1Trigger/TrackerTFP/interface/DataFormats.h"
#include "L1Trigger/TrackerTFP/interface/LayerEncoding.h"
#include "L1Trigger/TrackFindingTracklet/interface/ChannelAssignment.h"
#include "L1Trigger/TrackFindingTracklet/interface/Settings.h"
#include "SimTracker/TrackTriggerAssociation/interface/TTTypes.h"

#include <vector>

namespace trklet {

/*! \class trklet::KFin
* \brief Class to emulate transformation of tracklet tracks and stubs into TMTT format
* \author Thomas Schuh
* \date 2021, Dec
*/
class KFin {
public:
KFin(const edm::ParameterSet& iConfig,
const tt::Setup* setup_,
const trackerTFP::DataFormats* dataFormats,
const trackerTFP::LayerEncoding* layerEncoding,
const ChannelAssignment* channelAssignment,
const Settings* settings,
int region);
~KFin() {}

// read in and organize input tracks and stubs
void consume(const tt::StreamsTrack& streamsTrack, const tt::StreamsStub& streamsStub);
// fill output products
void produce(tt::StreamsStub& accpetedStubs,
tt::StreamsTrack& acceptedTracks,
tt::StreamsStub& lostStubs,
tt::StreamsTrack& lostTracks);

private:
// truncates double precision of val into base precision, +1.e-12 restores robustness of addition of 2 digitised values
double digi(double val, double base) const { return (floor(val / base + 1.e-12) + .5) * base; }
// basetransformation of val from baseLow into baseHigh using widthMultiplier bit multiplication
double redigi(double val, double baseLow, double baseHigh, int widthMultiplier) const;
struct Stub {
Stub(const TTStubRef& ttStubRef, int layer, double r, double phi, double z, bool psTilt)
: valid_(true), ttStubRef_(ttStubRef), layer_(layer), r_(r), phi_(phi), z_(z), psTilt_(psTilt) {}
bool valid_;
TTStubRef ttStubRef_;
int layer_;
// radius w.r.t. chosenRofPhi in cm
double r_;
// phi residual in rad
double phi_;
// z residual in cm
double z_;
// phi uncertainty * sqrt(12) + additional terms in rad
double dPhi_;
// z uncertainty * sqrt(12) + additional terms in cm
double dZ_;
// true if barrel tilted module or encap PS module
bool psTilt_;
};
struct Track {
static constexpr int max_ = 8;
Track() { stubs_.reserve(max_); }
Track(const TTTrackRef& ttTrackRef,
bool valid,
double inv2R,
double phiT,
double cot,
double zT,
const std::vector<Stub*>& stubs)
: ttTrackRef_(ttTrackRef),
valid_(valid),
sector_(-1),
inv2R_(inv2R),
phiT_(phiT),
cot_(cot),
zT_(zT),
stubs_(stubs) {}
TTTrackRef ttTrackRef_;
bool valid_;
TTBV maybe_;
int sector_;
double inv2R_;
double phiT_;
double cot_;
double zT_;
std::vector<Stub*> stubs_;
};

// true if truncation is enbaled
bool enableTruncation_;
// stub residuals are recalculated from seed parameter and TTStub position
bool useTTStubResiduals_;
// provides run-time constants
const tt::Setup* setup_;
// provides dataformats
const trackerTFP::DataFormats* dataFormats_;
// helper class to encode layer
const trackerTFP::LayerEncoding* layerEncoding_;
// helper class to assign tracks to channel
const ChannelAssignment* channelAssignment_;
// provides tracklet constants
const Settings* settings_;
// processing region (0 - 8) aka processing phi nonant
const int region_;
// storage of input tracks
std::vector<Track> tracks_;
// storage of input stubs
std::vector<Stub> stubs_;
// h/w liked organized pointer to input tracks
std::vector<std::vector<Track*>> input_;
// unified tracklet digitisation granularity
double baseUinv2R_;
double baseUphiT_;
double baseUcot_;
double baseUzT_;
double baseUr_;
double baseUphi_;
double baseUz_;
// KF input format digitisation granularity (identical to TMTT)
double baseLinv2R_;
double baseLphiT_;
double baseLcot_;
double baseLzT_;
double baseLr_;
double baseLphi_;
double baseLz_;
// Finer granularity (by powers of 2) than the TMTT one. Used to transform from Tracklet to TMTT base.
double baseHinv2R_;
double baseHphiT_;
double baseHcot_;
double baseHzT_;
double baseHr_;
double baseHphi_;
double baseHz_;
// digitisation granularity used for inverted cot(theta)
double baseInvCot_;
};

} // namespace trklet

#endif
17 changes: 5 additions & 12 deletions L1Trigger/TrackFindingTracklet/interface/MatchEngineUnit.h
Expand Up @@ -6,7 +6,7 @@
* implementation (required to meet II=1)
*
* A total of `nMatchEngines_` MEUs are used in the MP
****************************************************************/
****************************************************************/
#ifndef L1Trigger_TrackFindingTracklet_interface_MatchEngineUnit_h
#define L1Trigger_TrackFindingTracklet_interface_MatchEngineUnit_h

Expand Down Expand Up @@ -60,13 +60,9 @@ namespace trklet {

void setAlmostFull();

void setimeu(int imeu) {
imeu_ = imeu;
}
void setimeu(int imeu) { imeu_ = imeu; }

void setprint(bool print) {
print_ = print;
}
void setprint(bool print) { print_ = print; }

void reset();

Expand All @@ -78,7 +74,6 @@ namespace trklet {
void processPipeline();

private:

//Provide access to constants
const Settings& settings_;

Expand All @@ -98,13 +93,13 @@ namespace trklet {
int projfinephi_;
std::vector<std::pair<unsigned int, unsigned int>> use_;
bool isPSseed_;
Tracklet *proj_;
Tracklet* proj_;

bool idle_;

unsigned int layerdisk_;

//Save state at the start of istep
//Save state at the start of istep
bool almostfullsave_;

//LUT for bend consistency with rinv
Expand All @@ -127,8 +122,6 @@ namespace trklet {
//debugging help
int imeu_;
bool print_;


};

}; // namespace trklet
Expand Down
2 changes: 1 addition & 1 deletion L1Trigger/TrackFindingTracklet/interface/PurgeDuplicate.h
Expand Up @@ -8,7 +8,7 @@
#include <vector>

// Run algorithm to remove duplicate tracks.
// Returns Track object that represent output at end of L1 track chain,
// Returns Track object that represent output at end of L1 track chain,
// (and are later converted to TTTrack). Duplicate Track objects are flagged,
// but only deleted if using the Hybrid algo.
// Also writes to memory the same tracks in more detailed Tracklet format,
Expand Down
7 changes: 4 additions & 3 deletions L1Trigger/TrackFindingTracklet/interface/Settings.h
Expand Up @@ -430,7 +430,7 @@ namespace trklet {
}

// DTC in given ATCA crate slot.
std::string slotToDTCname(unsigned int slot) const {return slotToDTCname_.at(slot);}
std::string slotToDTCname(unsigned int slot) const { return slotToDTCname_.at(slot); }

// Tracker layers read by given DTC.
const std::vector<int>& dtcLayers(const std::string& dtcName) const {
Expand Down Expand Up @@ -504,7 +504,8 @@ namespace trklet {

// FIX: There should be 3 PS10G slots & 3 PS (5G) ones.
// (Will change output files used by HLS).
std::vector<std::string> slotToDTCname_{"PS10G_1","PS10G_2","PS10G_3","PS10G_4","PS_1","PS_2","2S_1","2S_2","2S_3","2S_4","2S_5","2S_6"};
std::vector<std::string> slotToDTCname_{
"PS10G_1", "PS10G_2", "PS10G_3", "PS10G_4", "PS_1", "PS_2", "2S_1", "2S_2", "2S_3", "2S_4", "2S_5", "2S_6"};

std::map<std::string, std::vector<int> > dtclayers_{{"PS10G_1", {0, 6, 8, 10}},
{"PS10G_2", {0, 7, 9}},
Expand Down Expand Up @@ -935,7 +936,7 @@ namespace trklet {
bool extended_{false}; // turn on displaced tracking
bool reduced_{false}; // use reduced (Summer Chain) config

bool combined_{false}; // use combined TP (TE+TC) and MP (PR+ME+MC) configuration
bool combined_{false}; // use combined TP (TE+TC) and MP (PR+ME+MC) configuration

std::string skimfile_{""}; //if not empty events will be written out in ascii format to this file

Expand Down
6 changes: 3 additions & 3 deletions L1Trigger/TrackFindingTracklet/interface/Track.h
Expand Up @@ -18,8 +18,8 @@ namespace trklet {
class Track {
public:
// Create track from digitized helix params & stubs
Track(TrackPars<int> ipars, // digi helix
int ichisqrphi, // digi chi2
Track(TrackPars<int> ipars, // digi helix
int ichisqrphi, // digi chi2
int ichisqrz,
double chisqrphi,
double chisqrz,
Expand Down Expand Up @@ -79,7 +79,7 @@ namespace trklet {
}

private:
TrackPars<int> ipars_; //digitized track parameters
TrackPars<int> ipars_; //digitized track parameters
int ichisqrphi_;
int ichisqrz_;

Expand Down
11 changes: 9 additions & 2 deletions L1Trigger/TrackFindingTracklet/interface/TrackletConfigBuilder.h
Expand Up @@ -16,7 +16,9 @@
#include <fstream>
#include <cstdlib>

namespace tt {class Setup;}
namespace tt {
class Setup;
}

namespace trklet {

Expand Down Expand Up @@ -278,7 +280,12 @@ namespace trklet {
{-1, -1, -1, -1, -1, -1, -1, 1, 2, 3, 4}, //L1D1
{1, -1, -1, -1, -1, -1, -1, 2, 3, 4, -1}}; //L2D1

struct DTCinfo {std::string name; int layer; float phimin; float phimax;};
struct DTCinfo {
std::string name;
int layer;
float phimin;
float phimax;
};
std::list<DTCinfo> vecDTCinfo_;

//Settings
Expand Down

0 comments on commit 05dca1b

Please sign in to comment.