Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:13:47

0001 #ifndef L1Trigger_TrackFindingTracklet_interface_Util_h
0002 #define L1Trigger_TrackFindingTracklet_interface_Util_h
0003 
0004 #include <sstream>
0005 #include <fstream>
0006 #include <cassert>
0007 #include <cmath>
0008 #include <string>
0009 #include <algorithm>
0010 #include <sys/types.h>
0011 #include <sys/stat.h>
0012 
0013 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0014 #include "FWCore/Utilities/interface/Exception.h"
0015 
0016 namespace trklet {
0017 
0018   //Converts string in binary to hex (used in writing out memory content)
0019   inline std::string hexFormat(const std::string& binary) {
0020     std::stringstream ss;
0021 
0022     unsigned int radix = 1, value = 0;
0023     for (int i = binary.length() - 1; i >= 0; i--) {
0024       if (binary.at(i) != '0' && binary.at(i) != '1')
0025         continue;
0026       value += (binary.at(i) - '0') * radix;
0027       if (radix == 8) {
0028         ss << std::hex << value;
0029         radix = 1;
0030         value = 0;
0031       } else
0032         radix <<= 1;
0033     }
0034     if (radix != 1)
0035       ss << std::hex << value;
0036 
0037     std::string str = ss.str() + "x0";
0038     std::reverse(str.begin(), str.end());
0039     return str;
0040   }
0041 
0042   inline double bendstrip(double r, double rinv, double stripPitch) {
0043     constexpr double dr = 0.18;
0044     double delta = r * dr * 0.5 * rinv;
0045     double bend = delta / stripPitch;
0046     return bend;
0047   }
0048 
0049   inline double rinv(double phi1, double phi2, double r1, double r2) {
0050     assert(r1 < r2);  //Can not form tracklet should not call function with r2<=r1
0051 
0052     double dphi = phi2 - phi1;
0053     double dr = r2 - r1;
0054 
0055     return 2.0 * sin(dphi) / dr / sqrt(1.0 + 2 * r1 * r2 * (1.0 - cos(dphi)) / (dr * dr));
0056   }
0057 
0058   inline std::string convertHexToBin(const std::string& stubwordhex) {
0059     std::string stubwordbin = "";
0060 
0061     for (char word : stubwordhex) {
0062       std::string hexword = "";
0063       if (word == '0')
0064         hexword = "0000";
0065       else if (word == '1')
0066         hexword = "0001";
0067       else if (word == '2')
0068         hexword = "0010";
0069       else if (word == '3')
0070         hexword = "0011";
0071       else if (word == '4')
0072         hexword = "0100";
0073       else if (word == '5')
0074         hexword = "0101";
0075       else if (word == '6')
0076         hexword = "0110";
0077       else if (word == '7')
0078         hexword = "0111";
0079       else if (word == '8')
0080         hexword = "1000";
0081       else if (word == '9')
0082         hexword = "1001";
0083       else if (word == 'A')
0084         hexword = "1010";
0085       else if (word == 'B')
0086         hexword = "1011";
0087       else if (word == 'C')
0088         hexword = "1100";
0089       else if (word == 'D')
0090         hexword = "1101";
0091       else if (word == 'E')
0092         hexword = "1110";
0093       else if (word == 'F')
0094         hexword = "1111";
0095       else {
0096         throw cms::Exception("Inconsistency")
0097             << __FILE__ << " " << __LINE__ << " hex string format invalid: " << stubwordhex;
0098       }
0099       stubwordbin += hexword;
0100     }
0101     return stubwordbin;
0102   }
0103 
0104   inline int ilog2(double factor) {
0105     double power = log(factor) / log(2);
0106     int ipower = round(power);
0107     assert(std::abs(power - ipower) < 0.1);
0108     return ipower;
0109   }
0110 
0111   /******************************************************************************
0112  * Checks to see if a directory exists. Note: This method only checks the
0113  * existence of the full path AND if path leaf is a dir.
0114  *
0115  * @return   1 if dir exists AND is a dir,
0116  *           0 if dir does not exist OR exists but not a dir,
0117  *          -1 if an error occurred (errno is also set)
0118  *****************************************************************************/
0119   inline int dirExists(const std::string& path) {
0120     struct stat info;
0121 
0122     int statRC = stat(path.c_str(), &info);
0123     if (statRC != 0) {
0124       if (errno == ENOENT) {
0125         return 0;
0126       }  // something along the path does not exist
0127       if (errno == ENOTDIR) {
0128         return 0;
0129       }  // something in path prefix is not a dir
0130       return -1;
0131     }
0132 
0133     return (info.st_mode & S_IFDIR) ? 1 : 0;
0134   }
0135 
0136   //Open file - create directory if not existent.
0137   inline std::ofstream openfile(const std::string& dir, const std::string& fname, const char* file, int line) {
0138     if (dirExists(dir) != 1) {
0139       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0140       int fail = system((std::string("mkdir -p ") + dir).c_str());
0141       if (fail) {
0142         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0143       }
0144     }
0145 
0146     std::ofstream out(dir + "/" + fname);
0147 
0148     if (out.fail()) {
0149       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0150     }
0151 
0152     return out;
0153   }
0154 
0155   //Open file - create directory if not existent.
0156   //If first==true open file in create mode, if first==false open in append mode
0157   inline void openfile(
0158       std::ofstream& out, bool first, const std::string& dir, const std::string& fname, const char* file, int line) {
0159     if (dirExists(dir) != 1) {
0160       edm::LogVerbatim("Tracklet") << "Creating directory : " << dir;
0161       int fail = system((std::string("mkdir -p ") + dir).c_str());
0162       if (fail) {
0163         throw cms::Exception("BadDir") << file << " " << line << " could not create directory " << dir;
0164       }
0165     }
0166 
0167     if (first) {
0168       out.open(fname);
0169     } else {
0170       out.open(fname, std::ofstream::app);
0171     }
0172 
0173     if (out.fail()) {
0174       throw cms::Exception("BadFile") << file << " " << line << " could not create file " << fname << " in " << dir;
0175     }
0176   }
0177 
0178 };  // namespace trklet
0179 #endif