Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:39

0001 #include "InttBCOMap.h"
0002 
0003 #include <cdbobjects/CDBTTree.h>
0004 
0005 #include <ffamodules/CDBInterface.h>
0006 
0007 #include <filesystem>
0008 #include <iostream>
0009 #include <vector>
0010 #include <algorithm>
0011 
0012 InttBCOMap::InttBCOMap()
0013 {
0014   for (int felix_server = 0; felix_server < 8; felix_server++)
0015   {
0016     for (int felix_channel = 0; felix_channel < 14; felix_channel++)
0017     {
0018       m_bco[felix_server][felix_channel] = -1;
0019     }
0020   }
0021 }
0022 
0023 int InttBCOMap::LoadFromCDB(std::string const &calibname)
0024 {
0025   if (calibname.empty())
0026   {
0027     std::cout << "InttBCOMap::LoadFromCDB(std::string const& name)" << std::endl;
0028     std::cout << "\tArgument 'name' is empty string" << std::endl;
0029     return -1;
0030   }
0031 
0032   std::string database = CDBInterface::instance()->getUrl(calibname);
0033   if (database.empty())
0034   {
0035     std::cout << "InttBCOMap::LoadFromCDB(std::string const& name)" << std::endl;
0036     std::cout << "\tArgument 'database' is empty string. calibname invalid :" << calibname << std::endl;
0037     return -1;
0038   }
0039 
0040   return LoadFromFile(database);
0041 }
0042 
0043 int InttBCOMap::LoadFromFile(std::string const &filename)
0044 {
0045   if (filename.empty())
0046   {
0047     std::cout << "InttBCOMap::LoadFromFile(std::string const& name)" << std::endl;
0048     std::cout << "\tArgument 'filename' is empty string" << std::endl;
0049     return 1;
0050   }
0051 
0052   if (!std::filesystem::exists(filename))
0053   {
0054     std::cout << "int InttBCOMap::LoadFromFile(std::string const& filename)" << std::endl;
0055     std::cout << "\tFile '" << filename << "' does not exist" << std::endl;
0056     return 1;
0057   }
0058 
0059   std::cout << "CDBFile: " << filename << std::endl;
0060 
0061   CDBTTree cdbttree = CDBTTree(filename);
0062   cdbttree.LoadCalibrations();
0063 
0064   return LoadFromCDBTTree(cdbttree);
0065 }
0066 
0067 int InttBCOMap::LoadFromCDBTTree(CDBTTree &cdbttree)
0068 {
0069   std::cout << "LoadFromCDBTTree::LoadFromCDBTTree" << std::endl;
0070 
0071   uint64_t N = cdbttree.GetSingleIntValue("size");
0072   for (uint64_t n = 0; n < N; ++n)
0073   {
0074     int felix_server = cdbttree.GetIntValue(n, "felix_server");
0075     int felix_channel = cdbttree.GetIntValue(n, "felix_channel");
0076     int bco_diff = cdbttree.GetIntValue(n, "bco_diff");
0077     m_bco[felix_server][felix_channel] = bco_diff;
0078 
0079     if (m_verbosity > 0)
0080     {
0081       std::cout << "felix_server " << felix_server << " ";
0082       std::cout << "felix_channel " << felix_channel << " ";
0083       std::cout << "bco_diff " << bco_diff << std::endl;
0084     }
0085   }
0086 
0087   return GetFeeOffSet();
0088 }
0089 
0090 int InttBCOMap::GetFeeOffSet()
0091 {
0092   std::vector<int> bco_values;
0093   for (const auto &server_pair : m_bco)
0094   {
0095     for (const auto &channel_pair : server_pair)
0096     {
0097       bco_values.push_back(channel_pair);
0098     }
0099   }
0100 
0101   std::sort(bco_values.begin(), bco_values.end());
0102 
0103   int most_frequent_bco = bco_values[0];
0104   int max_count = 1;
0105   int current_count = 1;
0106 
0107   for (size_t i = 1; i < bco_values.size(); ++i)
0108   {
0109     if (bco_values[i] == bco_values[i - 1])
0110     {
0111       current_count++;
0112     }
0113     else
0114     {
0115       if (current_count > max_count)
0116       {
0117         max_count = current_count;
0118         most_frequent_bco = bco_values[i - 1];
0119       }
0120       current_count = 1;
0121     }
0122   }
0123 
0124   if (current_count > max_count)
0125   {
0126     most_frequent_bco = bco_values.back();
0127   }
0128 
0129   return most_frequent_bco;
0130 }
0131 
0132 bool InttBCOMap::IsBad(const int &felix_server, const int &felix_channel, uint64_t const &bco_full, const int &bco)
0133 {
0134   // Definition of BCO difference was changed in Aug/2024
0135   int bco_diff = (bco - (bco_full & 0x7fU)  + 128) % 128;
0136   //  int bco_diff = (bco_full & 0x7FU) - bco;
0137   // if (bco_diff < 0)
0138   // {
0139   //   bco_diff += 128;
0140   // }
0141   //////////////////////////////////////////////////////////////////////////////
0142   // Hits belongs to [peak+1,peak-1] (3BCO region) will survive after BCO cut //
0143   //////////////////////////////////////////////////////////////////////////////
0144   int bco_peak = m_bco[felix_server][felix_channel];
0145   int bco_minus = bco_peak - 1;
0146   if (bco_minus == -1)
0147   {
0148     bco_minus = 127;
0149   }
0150   int bco_plus = bco_peak + 1;
0151   if (bco_plus == 128)
0152   {
0153     bco_plus = 0;
0154   }
0155 
0156   // -1: m_bco is initial value, not load the parameter. accept all bco
0157   if (bco_peak == -1 || bco_diff == bco_peak || bco_diff == bco_minus || bco_diff == bco_plus)
0158   {
0159     // std::cout<<"m_bco is initial value, not load the parameter. accept all bco "<<felix_server<<" "<<felix_channel<<std::endl;
0160     return false;
0161   }
0162   else
0163   {
0164     return true;
0165   }
0166 }
0167 bool InttBCOMap::IsBad(InttNameSpace::RawData_s const &raw, uint64_t const &bco_full, const int &bco)
0168 {
0169   return IsBad(raw.felix_server, raw.felix_channel, bco_full, bco);
0170 }
0171 
0172 bool InttBCOMap::IsBad(InttNameSpace::Offline_s const &off, uint64_t const &bco_full, const int &bco)
0173 {
0174   return IsBad(InttNameSpace::ToRawData(off), bco_full, bco);
0175 }
0176 
0177