Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:16:56

0001 #ifndef CALOBASE_RAWTOWERDEFS_H
0002 #define CALOBASE_RAWTOWERDEFS_H
0003 
0004 #include <bitset>
0005 #include <cstdlib>
0006 #include <iostream>
0007 #include <string>
0008 
0009 /*! Namespace with functions to encode / decode CaloTowerID. The highest 8 bits of the tower ID encode a unique ID
0010  * for the calorimeter the tower is in. The lower 24 bits uniquely identify the tower within a calorimeter.
0011  *
0012  */
0013 namespace RawTowerDefs
0014 {
0015   /*! Define data type of unique tower ID, i.e. for CaloTowerID
0016    */
0017   typedef unsigned int keytype;
0018 
0019   /*! Bit ranges for encoding calorimeter ID and tower indices in combined tower ID
0020    */
0021   static unsigned int calo_idbits = 8;
0022   static unsigned int tower_idbits = sizeof(keytype) * 8 - calo_idbits;
0023   static unsigned int index1_idbits = tower_idbits / 2;
0024 
0025   /*! Enum with all available calorimeter IDs. This enum can be extended up to 254 entries.
0026    * If adding new CalorimeterIDs, please also add them to the decode_caloname function below.
0027    */
0028   enum CalorimeterId
0029   {
0030     NONE = 0,
0031     CEMC = 1,
0032     HCALOUT = 2,
0033     HCALIN = 3,
0034     EEMC = 4,
0035     FEMC = 5,
0036     FHCAL = 6,
0037     DRCALO = 7,
0038     EHCAL = 8,
0039     EEMC_crystal = 9,
0040     EEMC_glass = 10,
0041     LFHCAL = 11,
0042     BECAL = 12,
0043     ZDC = 13,
0044     B0ECAL = 14,
0045     BWD_0 = 15,
0046     BWD_1 = 16,
0047     BWD_2 = 17,
0048     BWD_3 = 18,
0049     BWD_4 = 19
0050   };
0051 
0052   /*! Returns CaloTowerID for given calorimeter ID, tower index 1, and tower index 2
0053    */
0054   inline RawTowerDefs::keytype
0055   encode_towerid(const CalorimeterId calo_id, const unsigned int tower_index_1,
0056                  const unsigned int tower_index_2)
0057   {
0058     RawTowerDefs::keytype calo_tower_id = 0;
0059 
0060     if (tower_index_1 < 0xFFF && tower_index_2 < 0xFFF)
0061     {
0062       calo_tower_id = (calo_id << RawTowerDefs::tower_idbits) + (tower_index_1 << RawTowerDefs::index1_idbits) + tower_index_2;
0063     }
0064     else
0065     {
0066       std::cout << "too large index1 and/or index2; index1: "
0067                 << tower_index_1 << " (max val " << 0xFFF << ")"
0068                 << ", index2: "
0069                 << tower_index_2 << " (max val " << 0xFFF << ")" << std::endl;
0070       exit(1);
0071     }
0072 
0073     return calo_tower_id;
0074   }
0075 
0076   /*! Returns CaloTowerID for given calorimeter ID, tower index
0077    */
0078   inline RawTowerDefs::keytype
0079   encode_towerid(const CalorimeterId calo_id, const unsigned int tower_index)
0080   {
0081     RawTowerDefs::keytype calo_tower_id = 0;
0082 
0083     if (tower_index < 0xFFFFFF)
0084     {
0085       calo_tower_id = (calo_id << RawTowerDefs::tower_idbits) + tower_index;
0086     }
0087     else
0088     {
0089       std::cout << "too large index; index: " << tower_index
0090                 << " (max val " << 0xFFFFFF << ")" << std::endl;
0091       exit(1);
0092     }
0093 
0094     return calo_tower_id;
0095   }
0096 
0097   /*! Extract calorimeter ID from CaloTowerID
0098    */
0099   inline CalorimeterId
0100   decode_caloid(const unsigned int calo_tower_id)
0101   {
0102     return static_cast<CalorimeterId>((calo_tower_id >> RawTowerDefs::tower_idbits) & 0xFFF);
0103   }
0104 
0105   /*! Extract tower index of calorimeter tower from CaloTowerID
0106    */
0107   inline unsigned int
0108   decode_index(const unsigned int calo_tower_id)
0109   {
0110     return (calo_tower_id) &0xFFFFFF;
0111   }
0112 
0113   /*! Extract tower index 1 of calorimeter tower from CaloTowerID
0114    */
0115   inline unsigned int
0116   decode_index1(const unsigned int calo_tower_id)
0117   {
0118     return (calo_tower_id >> RawTowerDefs::index1_idbits) & 0xFFF;
0119   }
0120 
0121   /*! Extract tower index 2 of calorimeter tower from CaloTowerID
0122    */
0123   inline unsigned int
0124   decode_index2(const unsigned int calo_tower_id)
0125   {
0126     return calo_tower_id & 0xFFF;
0127   }
0128 
0129   /*! Extract tower index 1 of calorimeter tower from CaloTowerID with 3 indices
0130    */
0131   inline unsigned int
0132   decode_index1v2(const unsigned int calo_tower_id)
0133   {
0134     //     static unsigned int bitsIndex1 = 10;
0135     static unsigned int bitsIndex2 = 10;  // max 0x3FF (1023)
0136     static unsigned int bitsIndex3 = 4;   // max 0xF (15)
0137 
0138     //     std::cout << std::bitset<32>(calo_tower_id) << "\t index 1: " << ((calo_tower_id >> (bitsIndex2+bitsIndex3) & 0x3FF)) <<  "\t"<< std::bitset<32>((calo_tower_id >> (bitsIndex2+bitsIndex3))) << "\t"<< std::bitset<32>((calo_tower_id >> (bitsIndex2+bitsIndex3))& 0x3FF) <<std::endl;
0139     return (calo_tower_id >> (bitsIndex2 + bitsIndex3)) & 0x3FF;
0140   }
0141 
0142   /*! Extract tower index 2 of calorimeter tower from CaloTowerID with 3 indices
0143    */
0144   inline unsigned int
0145   decode_index2v2(const unsigned int calo_tower_id)
0146   {
0147     static unsigned int bitsIndex3 = 4;  // max 0xF (15)
0148                                          //     std::cout << std::bitset<32>(calo_tower_id) << "\t index 2: " << ((calo_tower_id >> (bitsIndex3) & 0x3FF)) <<  "\t"<< std::bitset<32>((calo_tower_id >> (bitsIndex3))) << "\t"<< std::bitset<32>((calo_tower_id >> (bitsIndex3))& 0x3FF) <<std::endl;
0149     return (calo_tower_id >> (bitsIndex3)) & 0x3FF;
0150   }
0151 
0152   /*! Extract tower index 3 of calorimeter tower from CaloTowerID with 3 indices
0153    */
0154   inline unsigned int
0155   decode_index3v2(const unsigned int calo_tower_id)
0156   {
0157     //     std::cout << std::bitset<32>(calo_tower_id) << "\t index 3: " << (calo_tower_id & 0xF) <<  "\t"<< std::bitset<32>((calo_tower_id & 0xF)) <<std::endl;
0158     return calo_tower_id & 0xF;
0159   }
0160 
0161   /*! Returns CaloTowerID for given calorimeter ID, tower index 1, tower index 2 and tower index 3
0162    */
0163   inline RawTowerDefs::keytype
0164   encode_towerid(const CalorimeterId calo_id, const unsigned int tower_index_1,
0165                  const unsigned int tower_index_2, const unsigned int tower_index_3)
0166   {
0167     RawTowerDefs::keytype calo_tower_id = 0;
0168 
0169     //     static unsigned int bitsIndex1 = 10; // max 0x3FF (1023)
0170     static unsigned int bitsIndex2 = 10;  // max 0x3FF (1023)
0171     static unsigned int bitsIndex3 = 4;   // max 0xF (15)
0172 
0173     if (tower_index_1 < 0x3FF && tower_index_2 < 0x3FF && tower_index_3 < 0xF)
0174     {
0175       calo_tower_id = (calo_id << RawTowerDefs::tower_idbits) + (tower_index_1 << (bitsIndex2 + bitsIndex3)) + (tower_index_2 << bitsIndex3) + tower_index_3;
0176     }
0177     else
0178     {
0179       std::cout << "too large index1 and/or index2; index1: "
0180                 << tower_index_1 << " (max val " << 0x3FF << ")"
0181                 << ", index2: "
0182                 << tower_index_2 << " (max val " << 0x3FF << ")"
0183                 << ", index3: "
0184                 << tower_index_3 << " (max val " << 0xF << ")" << std::endl;
0185       exit(1);
0186     }
0187     //     std::cout << std::bitset<32>(calo_tower_id) << "\t" << std::bitset<8>(calo_id) << "\t"<< tower_index_1 << "\t"<<  std::bitset<10>(tower_index_1) << "\t"<< tower_index_2 << "\t"<<  std::bitset<10>(tower_index_2) << "\t"<< tower_index_3<< "\t"<<  std::bitset<4>(tower_index_3)  << std::endl;
0188     //     decode_index1v2(calo_tower_id);
0189     //     decode_index2v2(calo_tower_id);
0190     //     decode_index3v2(calo_tower_id);
0191     return calo_tower_id;
0192   }
0193 
0194   /*! Convert calorimeter ID to name string
0195    */
0196   inline std::string
0197   convert_caloid_to_name(const RawTowerDefs::CalorimeterId calo_id)
0198   {
0199     switch (calo_id)
0200     {
0201     case NONE:
0202       return "NONE";
0203       break;
0204 
0205     case DRCALO:
0206       return "DRCALO";
0207       break;
0208 
0209     case CEMC:
0210       return "CEMC";
0211       break;
0212 
0213     case HCALIN:
0214       return "HCALIN";
0215       break;
0216 
0217     case HCALOUT:
0218       return "HCALOUT";
0219       break;
0220 
0221     case EEMC:
0222       return "EEMC";
0223       break;
0224 
0225     case EHCAL:
0226       return "EHCAL";
0227       break;
0228 
0229     case FEMC:
0230       return "FEMC";
0231       break;
0232 
0233     case FHCAL:
0234       return "FHCAL";
0235       break;
0236 
0237     case BECAL:
0238       return "BECAL";
0239       break;
0240 
0241     case EEMC_crystal:
0242       return "EEMC_crystal";
0243       break;
0244 
0245     case EEMC_glass:
0246       return "EEMC_glass";
0247       break;
0248 
0249     case LFHCAL:
0250       return "LFHCAL";
0251       break;
0252 
0253     case ZDC:
0254       return "ZDC";
0255       break;
0256 
0257     case B0ECAL:
0258       return "B0ECAL";
0259       break;
0260 
0261     case BWD_0:
0262       return "BWD_0";
0263       break;
0264 
0265     case BWD_1:
0266       return "BWD_1";
0267       break;
0268 
0269     case BWD_2:
0270       return "BWD_2";
0271       break;
0272 
0273     case BWD_3:
0274       return "BWD_3";
0275       break;
0276 
0277     case BWD_4:
0278       return "BWD_4";
0279       break;
0280 
0281     default:
0282       std::cout
0283           << "Invalid calorimeter ID passed to RawTowerDefs::convert_caloid_to_name"
0284           << std::endl;
0285       exit(1);
0286     }
0287   }
0288 
0289   /*! Convert name string to calorimeter ID
0290    */
0291   inline RawTowerDefs::CalorimeterId
0292   convert_name_to_caloid(const std::string &caloname)
0293   {
0294     if (caloname == "NONE")
0295       return NONE;
0296 
0297     else if (caloname == "CEMC")
0298       return CEMC;
0299 
0300     else if (caloname == "DRCALO")
0301       return DRCALO;
0302 
0303     else if (caloname == "HCALIN")
0304       return HCALIN;
0305 
0306     else if (caloname == "HCALOUT")
0307       return HCALOUT;
0308 
0309     else if (caloname == "EEMC")
0310       return EEMC;
0311 
0312     else if (caloname == "EHCAL")
0313       return EHCAL;
0314 
0315     else if (caloname == "FEMC")
0316       return FEMC;
0317 
0318     else if (caloname == "FHCAL")
0319       return FHCAL;
0320 
0321     else if (caloname == "EEMC_crystal")
0322       return EEMC_crystal;
0323 
0324     else if (caloname == "EEMC_glass")
0325       return EEMC_glass;
0326 
0327     else if (caloname == "LFHCAL")
0328       return LFHCAL;
0329 
0330     else if (caloname == "BECAL")
0331       return BECAL;
0332 
0333     else if (caloname == "ZDC")
0334       return ZDC;
0335 
0336     else if (caloname == "B0ECAL")
0337       return B0ECAL;
0338 
0339     else if (caloname == "BWD_0")
0340       return BWD_0;
0341 
0342     else if (caloname == "BWD_1")
0343       return BWD_1;
0344 
0345     else if (caloname == "BWD_2")
0346       return BWD_2;
0347 
0348     else if (caloname == "BWD_3")
0349       return BWD_3;
0350 
0351     else if (caloname == "BWD_4")
0352       return BWD_4;
0353 
0354     else
0355     {
0356       std::cout << "Invalid calorimeter name " << caloname
0357                 << " passed to RawTowerDefs::convert_name_to_caloid" << std::endl;
0358       exit(1);
0359     }
0360   }
0361 
0362 }  // end namespace RawTowerDefs
0363 
0364 #endif