Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2026-08-31 08:19:54

0001 // Tell emacs that this is a C++ source
0002 //  -*- C++ -*-.
0003 #ifndef NNSCALEMAP_H
0004 #define NNSCALEMAP_H
0005 
0006 #include <fun4all/SubsysReco.h>
0007 
0008 #include <array>
0009 #include <string>
0010 #include <vector>
0011 
0012 class PHCompositeNode;
0013 class SvtxTrackMap;
0014 
0015 // Runs after track reconstruction. Reads the tracks in an existing
0016 // SvtxTrackMap, applies a per-track pT scale factor kappa(q, pT, eta, phi)
0017 // taken from a trilinearly-interpolated lookup table (as produced by the
0018 // MomentumScaleStudies ml_momentum_calibration_reso*.py training scripts,
0019 // kappa_lookup.csv), and writes the corrected tracks into a new
0020 // SvtxTrackMap node. The input track map and node tree are left untouched.
0021 //
0022 // By default (setUseCDB(true)) the lookup CSV is located from the
0023 // Calibration Database, keyed by run number/timestamp via CDBInterface,
0024 // under the configured domain name -- with the fixed local path from
0025 // setKappaLookupFile(), if any, used as the fallback if the CDB has no
0026 // payload for this run. Call setUseCDB(false) to always use the fixed
0027 // local path instead. CDB resolution happens once per run in InitRun(),
0028 // so a job spanning multiple runs picks up the correct map for each one
0029 // automatically.
0030 //
0031 // The CSV needs at least columns named "q", "pT", "eta", "phi", "kappa"
0032 // (located by header name, so extra diagnostic columns -- e.g. "curv",
0033 // "kappa_curv", "eps", "delta" from a curvature-space training run -- are
0034 // ignored and column order doesn't matter). "kappa" must already be the
0035 // pT multiplier: pT_corrected = kappa * pT_reco.
0036 class NNScaleMap : public SubsysReco
0037 {
0038  public:
0039   explicit NNScaleMap(const std::string& name = "NNScaleMap");
0040   ~NNScaleMap() override = default;
0041 
0042   int Init(PHCompositeNode* topNode) override;
0043   int InitRun(PHCompositeNode* topNode) override;
0044   int process_event(PHCompositeNode* topNode) override;
0045   int End(PHCompositeNode* topNode) override;
0046 
0047   void Print(const std::string& what = "ALL") const override;
0048 
0049   //! local path to a kappa_lookup.csv. Used directly if setUseCDB(false); used
0050   //! as the CDBInterface::getUrl() fallback filename if setUseCDB(true) (the
0051   //! default) and the CDB has no payload for this run.
0052   void setKappaLookupFile(const std::string& path) { m_lookupFile = path; }
0053 
0054   //! look up the lookup CSV from the Calibration Database per-run instead of
0055   //! (or as an override of) the fixed local path (default true)
0056   void setUseCDB(bool doIt) { m_useCDB = doIt; }
0057 
0058   //! CDB domain/payload name to query when setUseCDB(true) (default "TRACKING_MOMENTUM_SCALE")
0059   void setCdbDomainName(const std::string& domain) { m_cdbDomainName = domain; }
0060 
0061   //! node name of the input track map to read (default "SvtxTrackMap")
0062   void setInputTrackMapName(const std::string& name) { m_inputTrackMapName = name; }
0063 
0064   //! node name of the corrected track map this module creates (default "SvtxTrackMapMomentumScaleCorrected")
0065   void setOutputTrackMapName(const std::string& name) { m_outputTrackMapName = name; }
0066 
0067   //! also rescale the momentum block of the track covariance matrix (default true)
0068   void setScaleCovariance(bool doIt) { m_scaleCovariance = doIt; }
0069 
0070  private:
0071   int CreateNodes(PHCompositeNode* topNode);
0072   int GetNodes(PHCompositeNode* topNode);
0073   std::string ResolveLookupFile() const;
0074   bool LoadLookupTable(const std::string& path);
0075   float GetKappa(int charge, float pt, float eta, float phi) const;
0076 
0077   std::string m_lookupFile;
0078   bool m_useCDB = true;
0079   std::string m_cdbDomainName = "TRACKING_MOMENTUM_SCALE";
0080   std::string m_loadedFile;  // path actually loaded, so InitRun can skip a needless reparse
0081 
0082   std::string m_inputTrackMapName = "SvtxTrackMap";
0083   std::string m_outputTrackMapName = "SvtxTrackMapMomentumScaleCorrected";
0084   bool m_scaleCovariance = true;
0085 
0086   // lookup grid axes, read directly from the CSV (not assumed a priori;
0087   // a curvature-space training run yields a grid that is non-uniform in
0088   // pT -- dense below ~3 GeV, sparse above)
0089   std::vector<double> m_ptEdges;
0090   std::vector<double> m_etaEdges;
0091   std::vector<double> m_phiEdges;
0092 
0093   // kappa[chargeIdx][(ipt*nEta + ieta)*nPhi + iphi], chargeIdx: 0 -> q<0, 1 -> q>0
0094   std::array<std::vector<float>, 2> m_kappaGrid;
0095 
0096   SvtxTrackMap* m_inputTrackMap = nullptr;
0097   SvtxTrackMap* m_outputTrackMap = nullptr;
0098 
0099   unsigned int m_nEvents = 0;
0100   unsigned long m_nTracksSeen = 0;
0101   unsigned long m_nTracksCorrected = 0;
0102   unsigned long m_nTracksSkippedZeroPt = 0;
0103 };
0104 
0105 #endif  // NNSCALEMAP_H