Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:20:37

0001 // Tell emacs that this is a C++ source
0002 //  -*- C++ -*-.
0003 #ifndef MVTX_TB_SETUP_STAVE_GEOM_H
0004 #define MVTX_TB_SETUP_STAVE_GEOM_H
0005 
0006 #define NMAXRU 2
0007 #define NMAXRUCHN 28
0008 #define NLAYER 4
0009 #define NCHIP 9
0010 #define NROW 512
0011 #define NCOL 1024
0012 #define NREGION 32
0013 #define NCOL_PER_REGION 32
0014 
0015 #include <trackbase/TrkrDefs.h>
0016 #include <mvtx/MvtxDefs.h>
0017 #include <mvtx/SegmentationAlpide.h>
0018 
0019 #include <TGeoMatrix.h>
0020 
0021 typedef SegmentationAlpide Segmentation;
0022 
0023 template <typename T = TGeoHMatrix>
0024 class MatrixCache
0025 {
0026  public:
0027   MatrixCache()  = default;
0028   ~MatrixCache() = default;
0029   MatrixCache(const MatrixCache& src) = delete;
0030   MatrixCache& operator=(const MatrixCache src) = delete;
0031 
0032   ///set size of matrix cache
0033   void setSize(int s)
0034   {
0035     if (!m_cache.size())
0036       m_cache.resize(s);
0037   }
0038 
0039   /// get the size of the cache
0040   int getSize() const { return m_cache.size(); }
0041   /// assign matrix to a slot
0042   void setMatrix(const T& mat, int sensID)
0043   {
0044     // assign matrix for given sensor. The cache must be booked in advance
0045     //if ((unsigned int)sensID >= mCache.size()) {
0046     //  LOG(FATAL) << "SensID " << sensID << " exceeds cache size of " << mCache.size();
0047     //}
0048     m_cache[sensID] = mat;
0049   }
0050 
0051   const T& getMatrix(int sensID) const { return m_cache[sensID]; }
0052   bool isFilled() const { return !m_cache.empty(); }
0053 
0054  private:
0055   std::vector<T> m_cache;
0056 };
0057 
0058 /** @Brief Class to create MVTX testbeam setup geometry
0059  *
0060  */
0061 class MvtxPrototype2Geom
0062 {
0063  public:
0064   MvtxPrototype2Geom():m_verbose(0)
0065   {
0066     if (s_instance) {
0067       exit(1);
0068     }
0069     Build();
0070   }
0071 
0072   ~MvtxPrototype2Geom() = default;
0073   MvtxPrototype2Geom(const MvtxPrototype2Geom& src) = delete;
0074   MvtxPrototype2Geom& operator=(const MvtxPrototype2Geom& geo) = delete;
0075 
0076   const MatrixCache<TGeoHMatrix>& getCacheL2G() const { return m_l2G; }
0077   const TGeoHMatrix& getMatrixL2G(int sensID) const { return m_l2G.getMatrix(sensID); }
0078   bool isBuilt() const { return m_size != 0; }
0079   int getSize() const { return m_size; }
0080   // before calling fillMatrixCache, detector implementation should set the size of the matrix cache
0081   void setSize(int s)
0082   {
0083   // set the size of the matrix cache, can be done only once
0084   //if (mSize != 0) {
0085   //  LOG(FATAL) << "Cache size (N sensors) was already set to " << mSize;
0086   //}
0087     m_size = s;
0088   }
0089   int getLastChipIndex(int lay) const { return m_lastChipIndex[lay]; }
0090   int getFirstChipIndex(int lay) const { return (lay == 0) ? 0 : m_lastChipIndex[lay - 1] + 1; }
0091   int getChipIndex(int lay, int chipInStave)
0092   {
0093     return getFirstChipIndex(lay) + chipInStave;
0094   }
0095   int getChipIndex(TrkrDefs::hitsetkey _key) {
0096     return getChipIndex(TrkrDefs::getLayer(_key), MvtxDefs::getChipId(_key));
0097   }
0098   int getLayer(int index) const { return index / m_numOfChips; }
0099   int getChipInLay(int index) const { return index % m_numOfChips; }
0100 
0101   static MvtxPrototype2Geom* Instance()
0102   {
0103     // get (create if needed) a unique instance of the object
0104     if (!s_instance)
0105       s_instance = std::unique_ptr<MvtxPrototype2Geom>(new MvtxPrototype2Geom());
0106     return s_instance.get();
0107   }
0108 
0109   void Build();
0110 
0111   void setVerbose(int v) { m_verbose = v; }
0112   int Verbose() { return m_verbose; }
0113 
0114   public:
0115     static constexpr float s_pitchChip_IB = 100.e-4; ///< nominal chip pitch in IB stave = 100 um.
0116     static constexpr float s_gapLayers_TB = 4.;       ///< nominal gap between staves in 2019 TB setup = 4 cm.
0117 
0118   //  static bool detectorToStave(int chip, int iRow, int iCol, int& sRow, int& sCol);
0119   //  static bool staveToDetector(int sRow, int sCol, int& chip, int& iRow, int& iCol);
0120     bool detectorToGlobal(int index, int iCol, int iRow, double* glo);
0121     bool detectorToGlobal(TrkrDefs::hitsetkey, TrkrDefs::hitkey, double*);
0122 
0123  private:
0124   static int getNumberOfChipsInLay()
0125   {
0126     return m_numOfStaves * m_numOfChips;
0127   }
0128   TGeoHMatrix* extractMatrixSensor(int index);
0129   void fillMatrixCache();
0130 
0131  private:
0132   int m_verbose;                     ///< verbosity level
0133   static std::unique_ptr<MvtxPrototype2Geom> s_instance;
0134   int m_size = 0;                    ///< prebooked number of sensors
0135   MatrixCache<TGeoHMatrix> m_l2G;    ///< Local to Global matrices
0136 
0137   static constexpr int m_numOfLayers = 4; ///< Number of layers
0138   static constexpr int m_numOfStaves = 1; ///< Number of staves/layer
0139   static constexpr int m_numOfChips  = 9; ///< Number of Chips/stave
0140 
0141   std::vector<int> m_lastChipIndex;
0142 
0143 };
0144 
0145 //_________________________________________________________________________________________________
0146 /* inline bool MvtxPrototype2Geom::detectorToStave(int chip, int iRow, int iCol, int& sRow, int& sCol)
0147 {
0148   //convert to concecutive row/col in a stave from row/col in chip
0149   if (iRow<0 || iRow>=Segmentation::NRows || iCol<0 || iCol>=Segmentation::NCols)
0150     return false;
0151   sRow = iRow;
0152   sCol = chip * Segmentation::NCols + iCol;
0153   return true;
0154 }
0155 
0156 //_________________________________________________________________________________________________
0157 inline bool MvtxPrototype2Geom::staveToDetector(int sRow, int sCol, int& chip, int& iRow, int& iCol)
0158 {
0159   //convert to chip row/col from consecutive stave from row/col in chip
0160   if (sRow<0 || sRow>=Segmentation::NRows || sCol<0 || sCol>=(m_numOfChips * Segmentation::NCols))
0161     return false;
0162   chip = sCol / Segmentation::NCols;
0163   iCol = sCol % Segmentation::NCols;
0164   iRow = sRow;
0165   return true;
0166 }
0167 */
0168 //_________________________________________________________________________________________________
0169 inline bool MvtxPrototype2Geom::detectorToGlobal(int index, int iCol, int iRow, double* glo)
0170 {
0171   if (iRow<0 || iRow>=Segmentation::NRows || iCol<0 || iCol>=Segmentation::NCols)
0172     return false;
0173   TVector3 locPoint;
0174   Segmentation::detectorToLocal(iRow, iCol, locPoint);
0175   const TGeoHMatrix& matL2G = Instance()->getMatrixL2G(index);
0176   //matL2G.Print();
0177   double loc[3];
0178   locPoint.GetXYZ(loc);
0179   matL2G.LocalToMaster(loc, glo);
0180   return true;
0181 }
0182 
0183 inline bool MvtxPrototype2Geom::detectorToGlobal(TrkrDefs::hitsetkey _hitsetkey, TrkrDefs::hitkey _hitkey, double* glo)
0184 {
0185   return detectorToGlobal(getChipIndex(_hitsetkey), MvtxDefs::getCol(_hitkey), MvtxDefs::getRow(_hitkey), glo);
0186 }
0187 
0188 #endif